core/playlist: Move current track into the playlist struct

I keep using the queue_iter struct for now to reduce code churn in this
patch.  I'll be replacing it in the next patch.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-22 10:16:05 -04:00
parent 18f1bfe801
commit 03e7346900
9 changed files with 41 additions and 44 deletions

View File

@ -42,6 +42,8 @@ void playlist_generic_init(struct playlist *playlist, struct queue_ops *ops)
{ {
queue_init(&playlist->pl_queue, ops, playlist); queue_init(&playlist->pl_queue, ops, playlist);
playlist->pl_sort = NULL; playlist->pl_sort = NULL;
playlist->pl_current.it_iter = NULL;
playlist->pl_current.it_pos = UINT_MAX;
playlist_generic_sort(playlist, COMPARE_ARTIST); playlist_generic_sort(playlist, COMPARE_ARTIST);
playlist_generic_sort(playlist, COMPARE_YEAR); playlist_generic_sort(playlist, COMPARE_YEAR);
playlist_generic_sort(playlist, COMPARE_TRACK); playlist_generic_sort(playlist, COMPARE_TRACK);
@ -52,6 +54,7 @@ void playlist_generic_deinit(struct playlist *playlist)
{ {
if (playlist) { if (playlist) {
queue_deinit(&playlist->pl_queue); queue_deinit(&playlist->pl_queue);
playlist_generic_clear(playlist);
playlist_clear_sort(playlist); playlist_clear_sort(playlist);
playlist->pl_length = 0; playlist->pl_length = 0;
} }
@ -68,7 +71,7 @@ void playlist_generic_save(struct playlist *playlist, struct file *file,
return; return;
if (flags & PL_SAVE_ITER) if (flags & PL_SAVE_ITER)
file_writef(file, "%u ", playlist->pl_queue.q_cur.it_pos); file_writef(file, "%u ", playlist->pl_current.it_pos);
if (flags & PL_SAVE_FLAGS) { if (flags & PL_SAVE_FLAGS) {
sort = playlist->pl_sort; sort = playlist->pl_sort;
@ -149,9 +152,9 @@ void playlist_generic_clear(struct playlist *playlist)
n = playlist_size(playlist); n = playlist_size(playlist);
g_queue_clear(&playlist->pl_queue.q_tracks); g_queue_clear(&playlist->pl_queue.q_tracks);
playlist->pl_length = 0; playlist->pl_length = 0;
playlist->pl_queue.q_cur.it_iter = NULL; playlist->pl_current.it_iter = NULL;
playlist->pl_queue.q_cur.it_pos = UINT_MAX; playlist->pl_current.it_pos = UINT_MAX;
if (callbacks) if (callbacks)
callbacks->pl_cb_removed(playlist, NULL, n); callbacks->pl_cb_removed(playlist, NULL, n);
} }
@ -165,9 +168,9 @@ bool playlist_generic_add(struct playlist *playlist, struct track *track)
if (playlist->pl_sort) { if (playlist->pl_sort) {
g_queue_insert_sorted(&playlist->pl_queue.q_tracks, track, g_queue_insert_sorted(&playlist->pl_queue.q_tracks, track,
__playlist_generic_less_than, playlist); __playlist_generic_less_than, playlist);
playlist->pl_queue.q_cur.it_pos = g_queue_link_index( playlist->pl_current.it_pos = g_queue_link_index(
&playlist->pl_queue.q_tracks, &playlist->pl_queue.q_tracks,
playlist->pl_queue.q_cur.it_iter); playlist->pl_current.it_iter);
} else } else
g_queue_push_tail(&playlist->pl_queue.q_tracks, track); g_queue_push_tail(&playlist->pl_queue.q_tracks, track);
@ -199,10 +202,10 @@ bool playlist_generic_remove(struct playlist *playlist, struct track *track)
playlist_current_previous(playlist); playlist_current_previous(playlist);
count = g_queue_remove_all(&playlist->pl_queue.q_tracks, track); count = g_queue_remove_all(&playlist->pl_queue.q_tracks, track);
playlist->pl_length -= (count * track->tr_length); playlist->pl_length -= (count * track->tr_length);
playlist->pl_queue.q_cur.it_pos = g_queue_link_index( playlist->pl_current.it_pos = g_queue_link_index(
&playlist->pl_queue.q_tracks, &playlist->pl_queue.q_tracks,
playlist->pl_queue.q_cur.it_iter); playlist->pl_current.it_iter);
if (callbacks) if (callbacks)
callbacks->pl_cb_removed(playlist, track, count); callbacks->pl_cb_removed(playlist, track, count);
@ -253,7 +256,7 @@ struct track *playlist_generic_next(struct playlist *playlist)
return NULL; return NULL;
else if (playlist->pl_random) { else if (playlist->pl_random) {
pos = g_random_int_range(1, size); pos = g_random_int_range(1, size);
pos += playlist->pl_queue.q_cur.it_pos; pos += playlist->pl_current.it_pos;
playlist_current_set(playlist, pos % size); playlist_current_set(playlist, pos % size);
} else if (!playlist_current_next(playlist)) } else if (!playlist_current_next(playlist))
playlist_current_set(playlist, 0); playlist_current_set(playlist, 0);

View File

@ -23,8 +23,6 @@ void queue_init(struct queue *queue, const struct queue_ops *ops, void *data)
{ {
queue->q_ops = ops; queue->q_ops = ops;
g_queue_init(&queue->q_tracks); g_queue_init(&queue->q_tracks);
queue->q_cur.it_iter = NULL;
queue->q_cur.it_pos = -1;
queue->q_private = __queue_init(queue, data); queue->q_private = __queue_init(queue, data);
} }
@ -32,6 +30,4 @@ void queue_deinit(struct queue *queue)
{ {
g_queue_clear(&queue->q_tracks); g_queue_clear(&queue->q_tracks);
__queue_deinit(queue); __queue_deinit(queue);
queue->q_cur.it_pos = UINT_MAX;
queue->q_cur.it_iter = NULL;
} }

View File

@ -205,7 +205,7 @@ void gui_treeview_scroll()
GtkTreePath *path; GtkTreePath *path;
int pos; int pos;
pos = playlist ? playlist->pl_queue.q_cur.it_pos : -1; pos = playlist ? playlist->pl_current.it_pos : -1;
if (!can_scroll || pos < 0) if (!can_scroll || pos < 0)
return; return;

View File

@ -46,7 +46,7 @@ static inline bool playlist_current_set(struct playlist *playlist,
{ {
if (!playlist) if (!playlist)
return false; return false;
return playlist_iter_set(playlist, &playlist->pl_queue.q_cur, n); return playlist_iter_set(playlist, &playlist->pl_current, n);
} }
/* Called to advance the current track. */ /* Called to advance the current track. */
@ -54,7 +54,7 @@ static inline bool playlist_current_next(struct playlist *playlist)
{ {
if (!playlist) if (!playlist)
return false; return false;
return playlist_iter_next(&playlist->pl_queue.q_cur); return playlist_iter_next(&playlist->pl_current);
} }
/* Called to rewind the current track. */ /* Called to rewind the current track. */
@ -62,15 +62,15 @@ static inline bool playlist_current_previous(struct playlist *playlist)
{ {
if (!playlist) if (!playlist)
return false; return false;
playlist->pl_queue.q_cur.it_iter = g_list_previous(playlist->pl_queue.q_cur.it_iter); playlist->pl_current.it_iter = g_list_previous(playlist->pl_current.it_iter);
playlist->pl_queue.q_cur.it_pos--; playlist->pl_current.it_pos--;
return playlist->pl_queue.q_cur.it_iter != NULL; return playlist->pl_current.it_iter != NULL;
} }
/* Called to get a pointer to the current track. */ /* Called to get a pointer to the current track. */
static inline struct track *playlist_current_track(struct playlist *playlist) static inline struct track *playlist_current_track(struct playlist *playlist)
{ {
return playlist ? playlist_iter_track(&playlist->pl_queue.q_cur) : NULL; return playlist ? playlist_iter_track(&playlist->pl_current) : NULL;
} }
#endif /* OCARINA_CORE_PLAYLISTS_ITERATOR_H */ #endif /* OCARINA_CORE_PLAYLISTS_ITERATOR_H */

View File

@ -49,6 +49,7 @@ struct playlist {
unsigned int pl_length; /* This playlist's length, in seconds. */ unsigned int pl_length; /* This playlist's length, in seconds. */
bool pl_random; /* This playlist's random setting. */ bool pl_random; /* This playlist's random setting. */
struct queue_iter pl_current; /* This playlist's current track. */
GSList *pl_sort; /* This playlist's sort order. */ GSList *pl_sort; /* This playlist's sort order. */
void *pl_private; /* This playlist's private data. */ void *pl_private; /* This playlist's private data. */
struct queue pl_queue; /* This playlist's queue of tracks. */ struct queue pl_queue; /* This playlist's queue of tracks. */

View File

@ -33,7 +33,6 @@ struct queue {
GQueue q_tracks; /* The queue's list of tracks. */ GQueue q_tracks; /* The queue's list of tracks. */
void *q_private; /* The queue's private data. */ void *q_private; /* The queue's private data. */
struct queue_iter q_cur; /* The queue's last-played position. */
const struct queue_ops *q_ops; /* The queue's operations vector. */ const struct queue_ops *q_ops; /* The queue's operations vector. */
}; };

View File

@ -132,18 +132,18 @@ static void test_playlist()
g_assert(cb_playlist == &p); g_assert(cb_playlist == &p);
g_assert(cb_track == track_get(i)); g_assert(cb_track == track_get(i));
g_assert_cmpuint(p.pl_length, ==, ex_length); g_assert_cmpuint(p.pl_length, ==, ex_length);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, (11 - i)); g_assert_cmpuint(p.pl_current.it_pos, ==, (11 - i));
g_assert_cmpuint(playlist_size(&p), ==, (12 - i)); g_assert_cmpuint(playlist_size(&p), ==, (12 - i));
if (i < 12) if (i < 12)
g_assert_nonnull(p.pl_queue.q_cur.it_iter); g_assert_nonnull(p.pl_current.it_iter);
} }
g_assert_false(playlist_generic_remove(&p, NULL)); g_assert_false(playlist_generic_remove(&p, NULL));
g_assert_false(playlist_has(&p, track_get(i))); g_assert_false(playlist_has(&p, track_get(i)));
g_assert(cb_playlist == &p); g_assert(cb_playlist == &p);
g_assert(cb_track == track_get(12)); g_assert(cb_track == track_get(12));
g_assert_cmpuint(p.pl_length, ==, ex_length); g_assert_cmpuint(p.pl_length, ==, ex_length);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_current.it_iter);
/* Re-add the tracks! */ /* Re-add the tracks! */
for (i = 0; i < 13; i++) { for (i = 0; i < 13; i++) {
@ -164,15 +164,15 @@ static void test_playlist()
playlist_generic_clear(&p); playlist_generic_clear(&p);
g_assert_cmpuint(playlist_size(&p), ==, 0); g_assert_cmpuint(playlist_size(&p), ==, 0);
g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_length, ==, 0);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_current.it_iter);
/* Deinitialize the playlist. */ /* Deinitialize the playlist. */
playlist_generic_deinit(&p); playlist_generic_deinit(&p);
g_assert_cmpuint(playlist_size(&p), ==, 0); g_assert_cmpuint(playlist_size(&p), ==, 0);
g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_length, ==, 0);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_current.it_iter);
g_assert_null(p.pl_sort); g_assert_null(p.pl_sort);
} }
@ -255,8 +255,8 @@ static void test_sorting()
playlist_generic_deinit(&p); playlist_generic_deinit(&p);
g_assert_cmpuint(playlist_size(&p), ==, 0); g_assert_cmpuint(playlist_size(&p), ==, 0);
g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_length, ==, 0);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_current.it_iter);
g_assert_null(p.pl_sort); g_assert_null(p.pl_sort);
} }
@ -274,7 +274,7 @@ static void test_next()
g_assert_true(playlist_select(&p)); g_assert_true(playlist_select(&p));
g_assert(playlist_current() == &p); g_assert(playlist_current() == &p);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
/* Test playlist_next() with wraparound, but no random. */ /* Test playlist_next() with wraparound, but no random. */
for (i = 0; i < (13 * 2); i++) { for (i = 0; i < (13 * 2); i++) {
@ -301,8 +301,8 @@ static void test_next()
playlist_generic_deinit(&p); playlist_generic_deinit(&p);
g_assert_cmpuint(playlist_size(&p), ==, 0); g_assert_cmpuint(playlist_size(&p), ==, 0);
g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_length, ==, 0);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_current.it_iter);
g_assert_null(p.pl_sort); g_assert_null(p.pl_sort);
} }
@ -322,8 +322,8 @@ static void test_save_load()
playlist_set_random(&p, true); playlist_set_random(&p, true);
playlist_clear_sort(&p); playlist_clear_sort(&p);
playlist_sort(&p, COMPARE_TRACK); playlist_sort(&p, COMPARE_TRACK);
p.pl_queue.q_cur.it_pos = 3; playlist_current_set(&p, 3);
q.pl_queue.q_cur.it_pos = 4; playlist_current_set(&q, 4);
g_assert_false(file_exists(&f)); g_assert_false(file_exists(&f));
g_assert_true( file_open(&f, OPEN_WRITE)); g_assert_true( file_open(&f, OPEN_WRITE));
@ -337,13 +337,13 @@ static void test_save_load()
file_close(&f); file_close(&f);
g_assert_true(r.pl_random); g_assert_true(r.pl_random);
g_assert_cmpuint(r.pl_queue.q_cur.it_pos, ==, 3); g_assert_cmpuint(r.pl_current.it_pos, ==, 3);
g_assert_cmpuint(g_slist_length(r.pl_sort), ==, 1); g_assert_cmpuint(g_slist_length(r.pl_sort), ==, 1);
g_assert_cmpuint(GPOINTER_TO_UINT(r.pl_sort->data), ==, COMPARE_TRACK); g_assert_cmpuint(GPOINTER_TO_UINT(r.pl_sort->data), ==, COMPARE_TRACK);
g_assert_cmpuint(g_queue_get_length(&r.pl_queue.q_tracks), ==, 0); g_assert_cmpuint(g_queue_get_length(&r.pl_queue.q_tracks), ==, 0);
g_assert_false(s.pl_random); g_assert_false(s.pl_random);
g_assert_cmpuint(s.pl_queue.q_cur.it_pos, ==, 4); g_assert_cmpuint(s.pl_current.it_pos, ==, 4);
g_assert(playlist_current_track(&s) == queue_at(&q.pl_queue, 4)); g_assert(playlist_current_track(&s) == queue_at(&q.pl_queue, 4));
g_assert_cmpuint(g_slist_length(s.pl_sort), ==, 0); g_assert_cmpuint(g_slist_length(s.pl_sort), ==, 0);
g_assert_cmpuint(playlist_size(&s), ==, 13); g_assert_cmpuint(playlist_size(&s), ==, 13);
@ -352,8 +352,8 @@ static void test_save_load()
playlist_generic_deinit(&p); playlist_generic_deinit(&p);
g_assert_cmpuint(playlist_size(&p), ==, 0); g_assert_cmpuint(playlist_size(&p), ==, 0);
g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_length, ==, 0);
g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_cmpuint(p.pl_current.it_pos, ==, UINT_MAX);
g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_current.it_iter);
} }
int main(int argc, char **argv) int main(int argc, char **argv)

View File

@ -37,7 +37,6 @@ static void test_init()
g_assert_cmpuint(count_init, ==, 0); g_assert_cmpuint(count_init, ==, 0);
g_assert_null(q.q_private); g_assert_null(q.q_private);
g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1);
g_assert_null(q.q_ops); g_assert_null(q.q_ops);
queue_deinit(&q); queue_deinit(&q);
@ -47,7 +46,6 @@ static void test_init()
g_assert_cmpuint(count_init, ==, 1); g_assert_cmpuint(count_init, ==, 1);
g_assert_cmpuint(GPOINTER_TO_UINT(q.q_private), ==, 1); g_assert_cmpuint(GPOINTER_TO_UINT(q.q_private), ==, 1);
g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1);
g_assert(q.q_ops == &test_ops); g_assert(q.q_ops == &test_ops);
queue_deinit(&q); queue_deinit(&q);

View File

@ -63,7 +63,7 @@ void test_treeview_init()
g_assert_null(path); g_assert_null(path);
/* Okay, NOW we can scroll! */ /* Okay, NOW we can scroll! */
playlist_lookup(PL_SYSTEM, "Collection")->pl_queue.q_cur.it_pos = 4; playlist_current_set(playlist_lookup(PL_SYSTEM, "Collection"), 4);
gui_treeview_scroll(); gui_treeview_scroll();
gtk_tree_view_get_cursor(gui_treeview(), &path, NULL); gtk_tree_view_get_cursor(gui_treeview(), &path, NULL);
g_assert_nonnull(path); g_assert_nonnull(path);