2015-12-02 10:22:11 -05:00
|
|
|
/*
|
2013-12-31 15:44:44 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
2015-12-02 10:22:11 -05:00
|
|
|
*
|
|
|
|
* Queues are lists of tracks that the user has requested to play next.
|
|
|
|
* Users of queues are expected to implement their own save and load functions,
|
|
|
|
* and to provide a filled out queue_ops structure during initialization.
|
2013-12-31 15:44:44 -05:00
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_QUEUE_H
|
|
|
|
#define OCARINA_CORE_QUEUE_H
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.h>
|
2014-12-02 08:24:36 -05:00
|
|
|
#include <core/tags/track.h>
|
2014-05-11 19:52:08 -04:00
|
|
|
|
2015-12-02 10:22:11 -05:00
|
|
|
struct queue;
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-12-07 09:57:43 -05:00
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
struct queue_ops {
|
2016-01-12 08:02:28 -05:00
|
|
|
/* Called to tell a higher layer that a queue has been initialized. */
|
2016-05-02 07:52:18 -04:00
|
|
|
void *(*qop_init)(struct queue *, void *);
|
2016-01-13 08:18:03 -05:00
|
|
|
|
|
|
|
/* Called to tell a higher layer that a queue is deinitializing. */
|
|
|
|
void (*qop_deinit)(struct queue *);
|
2016-01-12 08:02:28 -05:00
|
|
|
|
2015-11-29 19:45:39 -05:00
|
|
|
/* Called to tell a higher layer that a track has been added. */
|
|
|
|
void (*qop_added)(struct queue *, unsigned int);
|
2015-11-23 08:36:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-03 09:29:32 -04:00
|
|
|
struct queue_iter {
|
|
|
|
GList *it_iter; /* The current link in the GQueue. */
|
|
|
|
guint it_pos; /* The current index into the queue. */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
struct queue {
|
2016-01-12 08:02:28 -05:00
|
|
|
unsigned int q_length; /* The queue's total runtime (in seconds). */
|
2016-04-03 09:52:16 -04:00
|
|
|
GQueue q_tracks; /* The queue's list of tracks. */
|
2016-01-12 08:02:28 -05:00
|
|
|
GSList *q_sort; /* The queue's sort order. */
|
2016-04-03 09:52:16 -04:00
|
|
|
void *q_private; /* The queue's private data. */
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2016-04-03 09:40:01 -04:00
|
|
|
struct queue_iter q_cur; /* The queue's last-played position. */
|
|
|
|
const struct queue_ops *q_ops; /* The queue's operations vector. */
|
2013-12-31 15:44:44 -05:00
|
|
|
};
|
|
|
|
|
2015-11-19 08:08:14 -05:00
|
|
|
|
2016-04-03 09:29:32 -04:00
|
|
|
|
|
|
|
/* Called to initialize a queue iterator. */
|
|
|
|
static inline void queue_iter_init(struct queue *queue, struct queue_iter *it)
|
|
|
|
{
|
2016-04-03 09:52:16 -04:00
|
|
|
it->it_iter = g_queue_peek_head_link(&queue->q_tracks);
|
|
|
|
it->it_pos = g_queue_link_index(&queue->q_tracks, it->it_iter);
|
2016-04-03 09:29:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called to advance a queue iterator by one step. */
|
|
|
|
static inline void queue_iter_next(struct queue_iter *it)
|
|
|
|
{
|
|
|
|
it->it_iter = g_list_next(it->it_iter);
|
|
|
|
it->it_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called to rewind a queue iterator by one step. */
|
|
|
|
static inline void queue_iter_prev(struct queue_iter *it)
|
|
|
|
{
|
|
|
|
it->it_iter = g_list_previous(it->it_iter);
|
|
|
|
it->it_pos--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called to set a queue iterator to a specific position. */
|
|
|
|
static inline void queue_iter_set(struct queue *queue, struct queue_iter *it,
|
|
|
|
unsigned int pos)
|
|
|
|
{
|
2016-04-03 09:52:16 -04:00
|
|
|
it->it_iter = g_queue_peek_nth_link(&queue->q_tracks, pos);
|
2016-04-03 09:29:32 -04:00
|
|
|
it->it_pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called to access the value of a queue iterator. */
|
|
|
|
static inline struct track *queue_iter_val(struct queue_iter *it)
|
|
|
|
{
|
|
|
|
return (it->it_iter) ? it->it_iter->data : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define queue_for_each(queue, it) \
|
|
|
|
for (queue_iter_init(queue, it); (it)->it_iter; queue_iter_next(it))
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
/* Called to initialize a queue. */
|
2016-09-19 10:55:29 -04:00
|
|
|
void queue_init(struct queue *, const struct queue_ops *, void *);
|
2015-11-23 08:36:29 -05:00
|
|
|
|
2015-12-07 10:00:19 -05:00
|
|
|
/* Called to deinitialize a queue. */
|
|
|
|
void queue_deinit(struct queue *);
|
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
|
2015-11-19 13:48:59 -05:00
|
|
|
/* Called to find the size of the queue. */
|
|
|
|
static inline unsigned int queue_size(struct queue *queue)
|
|
|
|
{
|
2016-04-03 09:52:16 -04:00
|
|
|
return g_queue_get_length(&queue->q_tracks);
|
2015-11-19 13:48:59 -05:00
|
|
|
}
|
|
|
|
|
2015-11-19 13:50:08 -05:00
|
|
|
/* Called to access the queued track at a given index. */
|
|
|
|
static inline struct track *queue_at(struct queue *queue, unsigned int index)
|
|
|
|
{
|
2016-04-03 09:52:16 -04:00
|
|
|
return (struct track *)g_queue_peek_nth(&queue->q_tracks, index);
|
2015-11-19 13:50:08 -05:00
|
|
|
}
|
|
|
|
|
2015-11-19 13:48:59 -05:00
|
|
|
|
2015-11-25 08:24:04 -05:00
|
|
|
/* Called to add a track to the queue. */
|
|
|
|
unsigned int queue_add(struct queue *, struct track *);
|
|
|
|
|
2016-09-08 09:08:18 -04:00
|
|
|
/* Called to add a track to the front of the queue. */
|
|
|
|
unsigned int queue_add_front(struct queue *, struct track *);
|
2016-09-03 12:01:27 -04:00
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_QUEUE_H */
|