From 8bb7a20ff48043c901036d903deaf77895cbee03 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 12 Jan 2016 08:02:28 -0500 Subject: [PATCH] core/queue: Add qop_init() queue operation This function is used to notify a higher layer that a queue has been initialized. That higher layer can return a pointer to be attached to the "private data" field of the queue. Signed-off-by: Anna Schumaker --- core/queue.c | 10 +++++++++- gui/collection_tab.cpp | 6 ++++++ gui/history.cpp | 6 ++++++ gui/playlist_tab.cpp | 6 ++++++ gui/tabs.cpp | 6 ++++++ include/core/queue.h | 14 +++++++++----- tests/core/queue.c | 13 +++++++++++++ 7 files changed, 55 insertions(+), 6 deletions(-) diff --git a/core/queue.c b/core/queue.c index 787a0ad1..2fd3fdce 100644 --- a/core/queue.c +++ b/core/queue.c @@ -29,6 +29,13 @@ static int track_less_than(const void *a, const void *b, void *data) return res; } +static inline void *__queue_init(struct queue *queue) +{ + if (queue->q_ops) + return queue->q_ops->qop_init(queue); + return NULL; +} + static inline unsigned int __queue_added(struct queue *queue, struct track *track, unsigned int pos) @@ -89,8 +96,9 @@ void queue_init(struct queue *queue, unsigned int flags, queue->q_cur.it_pos = -1; queue->q_cur.it_iter = NULL; - _q_init(&queue->q_tracks); + + queue->q_private = __queue_init(queue); } void queue_deinit(struct queue *queue) diff --git a/gui/collection_tab.cpp b/gui/collection_tab.cpp index eeba3d50..685d38a5 100644 --- a/gui/collection_tab.cpp +++ b/gui/collection_tab.cpp @@ -53,6 +53,11 @@ public: static CollectionTab *collection_tab; +static void *collection_init(struct queue *queue) +{ + return collection_tab; +} + static void collection_added(struct queue *queue, unsigned int pos) { if (collection_tab) @@ -78,6 +83,7 @@ static void collection_updated(struct queue *queue, unsigned int pos) } struct queue_ops collection_ops = { + collection_init, collection_added, collection_removed, collection_cleared, diff --git a/gui/history.cpp b/gui/history.cpp index 013bfdc5..25a693c9 100644 --- a/gui/history.cpp +++ b/gui/history.cpp @@ -38,6 +38,11 @@ public: static HistoryTab *history_tab; +static void *history_init(struct queue *queue) +{ + return history_tab; +} + static void history_added(struct queue *queue, unsigned int pos) { if (history_tab) @@ -60,6 +65,7 @@ static void history_updated(struct queue *queue, unsigned int pos) } struct queue_ops history_ops = { + history_init, history_added, history_removed, history_cleared, diff --git a/gui/playlist_tab.cpp b/gui/playlist_tab.cpp index 82b2a7f6..44f1c963 100644 --- a/gui/playlist_tab.cpp +++ b/gui/playlist_tab.cpp @@ -84,6 +84,11 @@ static void on_favorite() playlist_remove(PL_FAVORITED, track); } +static void *playlist_init(struct queue *queue) +{ + return p_tab; +} + static void playlist_added(struct queue *queue, unsigned int pos) { if (p_tab) @@ -106,6 +111,7 @@ static void playlist_updated(struct queue *queue, unsigned int pos) } struct queue_ops playlist_ops = { + playlist_init, playlist_added, playlist_removed, playlist_cleared, diff --git a/gui/tabs.cpp b/gui/tabs.cpp index ad309e9d..3481557a 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -23,6 +23,11 @@ static compare_t sort_fields[] = { COMPARE_GENRE, COMPARE_COUNT, COMPARE_PLAYED }; +static void *tempq_init(struct queue *queue) +{ + return NULL; +} + static void tempq_added(struct queue *queue, unsigned int pos) { find_tab(queue)->on_track_added(pos); @@ -46,6 +51,7 @@ static void tempq_updated(struct queue *queue, unsigned int pos) } struct queue_ops tempq_ops = { + tempq_init, tempq_added, tempq_removed, tempq_cleared, diff --git a/include/core/queue.h b/include/core/queue.h index 9d73da4b..ccb6fd1e 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -27,6 +27,9 @@ enum queue_flags { struct queue_ops { + /* Called to tell a higher layer that a queue has been initialized. */ + void *(*qop_init)(struct queue *); + /* Called to tell a higher layer that a track has been added. */ void (*qop_added)(struct queue *, unsigned int); @@ -45,11 +48,12 @@ struct queue_ops { struct queue { - unsigned int q_flags; /* The queue's set of flags. */ - unsigned int q_length; /* The queue's total runtime (in seconds). */ - struct _queue q_tracks; /* The queue's list of tracks. */ - struct _q_iter q_cur; /* The queue's last-played position. */ - GSList *q_sort; /* The queue's sort order. */ + unsigned int q_flags; /* The queue's set of flags. */ + unsigned int q_length; /* The queue's total runtime (in seconds). */ + struct _queue q_tracks; /* The queue's list of tracks. */ + struct _q_iter q_cur; /* The queue's last-played position. */ + GSList *q_sort; /* The queue's sort order. */ + void *q_private; /* The queue's private data. */ const struct queue_ops *q_ops; /* The queue's operations vector. */ }; diff --git a/tests/core/queue.c b/tests/core/queue.c index d35c95a4..22899d40 100644 --- a/tests/core/queue.c +++ b/tests/core/queue.c @@ -8,6 +8,7 @@ #include +unsigned int count_init = 0; unsigned int count_added = 0; unsigned int count_deleted = 0; unsigned int count_cleared = 0; @@ -16,6 +17,12 @@ unsigned int count_sort = 0; unsigned int count_updated = 0; +static void *queue_op_init(struct queue *queue) +{ + count_init++; + return GUINT_TO_POINTER(count_init); +} + static void queue_op_added(struct queue *queue, unsigned int pos) { count_added++; @@ -46,6 +53,7 @@ static void queue_op_updated(struct queue *queue, unsigned int pos) static const struct queue_ops test_ops = { + .qop_init = queue_op_init, .qop_added = queue_op_added, .qop_removed = queue_op_removed, .qop_cleared = queue_op_cleared, @@ -91,6 +99,9 @@ static void test_init() __test_init_core(); queue_init(&q, 0, NULL); + test_equal(count_init, 0); + test_equal(GPOINTER_TO_UINT(q.q_private), 0); + test_equal(q.q_cur.it_pos, (unsigned int)-1); test_equal(q.q_flags, 0); test_equal(q.q_length, 0); @@ -99,6 +110,8 @@ static void test_init() test_equal((void *)queue_next(&q), (void *)NULL); queue_init(&q, Q_ENABLED | Q_RANDOM, &test_ops); + test_equal(count_init, 1); + test_equal(GPOINTER_TO_UINT(q.q_private), 1); test_equal(q.q_cur.it_pos, (unsigned int)-1); test_equal(q.q_flags, Q_ENABLED | Q_RANDOM);