core/playlist: Move random setting into the playlist struct

I move the random variable into the playlist code since it is no longer
used by the queue layer.  This gives me the opportunity to change it
into a boolean rather than a bit flag.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-19 10:55:29 -04:00
parent f670a3796b
commit 1d09e967d0
18 changed files with 62 additions and 136 deletions

View File

@ -202,18 +202,13 @@ unsigned int playlist_size(struct playlist *playlist)
void playlist_set_random(struct playlist *playlist, bool enabled)
{
if (playlist && playlist->pl_ops->pl_set_flag) {
playlist->pl_ops->pl_set_flag(playlist, Q_RANDOM, enabled);
if (playlist && playlist->pl_ops->pl_set_random) {
playlist->pl_ops->pl_set_random(playlist, enabled);
if (playlist->pl_type < PL_MAX_TYPE)
playlist_types[playlist->pl_type]->pl_save();
}
}
bool playlist_get_random(struct playlist *playlist)
{
return playlist ? queue_has_flag(&playlist->pl_queue, Q_RANDOM) : false;
}
bool playlist_sort(struct playlist *playlist, enum compare_t sort)
{
if (!playlist || !playlist->pl_ops->pl_sort)

View File

@ -10,7 +10,7 @@ static struct file artist_file = FILE_INIT("playlist.artist", 0);
static struct playlist_ops pl_artist_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
@ -23,7 +23,7 @@ static struct playlist *__artist_pl_alloc(struct artist *artist)
playlist->pl_type = PL_ARTIST;
playlist->pl_id = artist_index(artist);
playlist->pl_ops = &pl_artist_ops;
playlist_generic_init(playlist, 0, artist_ops);
playlist_generic_init(playlist, artist_ops);
return playlist;
}

View File

@ -38,10 +38,9 @@ void playlist_generic_set_callbacks(struct playlist_callbacks *cb)
callbacks = cb;
}
void playlist_generic_init(struct playlist *playlist, unsigned int flags,
struct queue_ops *ops)
void playlist_generic_init(struct playlist *playlist, struct queue_ops *ops)
{
queue_init(&playlist->pl_queue, flags, ops, playlist);
queue_init(&playlist->pl_queue, ops, playlist);
playlist_generic_sort(playlist, COMPARE_ARTIST);
playlist_generic_sort(playlist, COMPARE_YEAR);
playlist_generic_sort(playlist, COMPARE_TRACK);
@ -63,7 +62,7 @@ void playlist_generic_save(struct playlist *playlist, struct file *file,
if (flags & PL_SAVE_FLAGS) {
sort = playlist->pl_queue.q_sort;
file_writef(file, "%u ", playlist->pl_queue.q_flags);
file_writef(file, "%u ", playlist->pl_random ? PL_RANDOM : 0);
file_writef(file, "%u", g_slist_length(sort));
while (sort) {
field = GPOINTER_TO_INT(sort->data);
@ -122,7 +121,7 @@ void playlist_generic_load(struct playlist *playlist, struct file *file,
g_free(line);
}
playlist->pl_queue.q_flags |= (f & Q_VALID_FLAGS);
playlist_generic_set_random(playlist, f == PL_RANDOM);
queue_iter_set(&playlist->pl_queue, &playlist->pl_queue.q_cur, it);
}
@ -158,12 +157,9 @@ bool playlist_generic_remove_track(struct playlist *playlist, struct track *trac
return queue_remove_all(&playlist->pl_queue, track);
}
void playlist_generic_set_flag(struct playlist *playlist,
enum queue_flags flag, bool enabled)
void playlist_generic_set_random(struct playlist *playlist, bool enabled)
{
if (enabled)
return queue_set_flag(&playlist->pl_queue, flag);
return queue_unset_flag(&playlist->pl_queue, flag);
playlist->pl_random = enabled;
}
void playlist_generic_sort(struct playlist *playlist, enum compare_t sort)
@ -199,7 +195,7 @@ struct track *playlist_generic_next(struct playlist *playlist)
if (size == 0)
return NULL;
else if (queue_has_flag(&playlist->pl_queue, Q_RANDOM)) {
else if (playlist->pl_random) {
pos = g_random_int_range(1, queue_size(&playlist->pl_queue));
pos += playlist->pl_queue.q_cur.it_pos;
queue_iter_set(&playlist->pl_queue,

View File

@ -28,7 +28,7 @@ static struct playlist *__lib_pl_alloc(struct library *library)
playlist->pl_type = PL_LIBRARY;
playlist->pl_id = library_index(library);
playlist->pl_ops = &pl_library_ops;
playlist_generic_init(playlist, 0, lib_ops);
playlist_generic_init(playlist, lib_ops);
return playlist;
}
@ -193,7 +193,7 @@ static bool pl_library_delete(struct playlist *playlist)
static struct playlist_ops pl_library_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_delete = pl_library_delete,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};

View File

@ -80,7 +80,7 @@ static struct playlist_ops favorites_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_delete = sys_pl_delete_clear,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
@ -130,7 +130,7 @@ static struct playlist_ops hidden_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_delete = sys_pl_hidden_clear,
.pl_remove = sys_pl_hidden_remove,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
@ -140,7 +140,7 @@ static struct playlist_ops hidden_ops = {
*/
static bool sys_pl_queued_load()
{
struct queue *queue = &pl_system_get(SYS_PL_QUEUED)->pl_queue;
struct playlist *playlist = pl_system_get(SYS_PL_QUEUED);
unsigned int num, i, flags = 0;
if (!file_open(&sys_deck_f, OPEN_READ))
@ -149,11 +149,11 @@ static bool sys_pl_queued_load()
file_readf(&sys_deck_f, "%u", &num);
for (i = 0; i < num; i++) {
file_readf(&sys_deck_f, "%u", &flags);
flags &= Q_VALID_FLAGS;
flags &= PL_RANDOM;
if (i == 0)
queue->q_flags |= flags;
playlist_generic_load(pl_system_get(SYS_PL_QUEUED),
&sys_deck_f, PL_SAVE_TRACKS);
playlist_generic_set_random(playlist,
flags == PL_RANDOM);
playlist_generic_load(playlist, &sys_deck_f, PL_SAVE_TRACKS);
}
file_close(&sys_deck_f);
@ -166,7 +166,7 @@ static struct playlist_ops queued_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_delete = sys_pl_delete_clear,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
@ -190,7 +190,7 @@ static bool sys_pl_collection_load()
static struct playlist_ops collection_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_remove = sys_pl_hidden_add,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
@ -215,7 +215,7 @@ static struct playlist_ops history_ops = {
*/
static struct playlist_ops dynamic_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
@ -399,7 +399,7 @@ void pl_system_init(struct queue_ops *ops)
switch (i) {
case SYS_PL_QUEUED:
case SYS_PL_HISTORY:
queue_init(&playlist->pl_queue, 0, ops, playlist);
queue_init(&playlist->pl_queue, ops, playlist);
break;
case SYS_PL_COLLECTION:
case SYS_PL_UNPLAYED:
@ -408,7 +408,7 @@ void pl_system_init(struct queue_ops *ops)
sys_pl_update(playlist);
case SYS_PL_FAVORITES:
case SYS_PL_HIDDEN:
playlist_generic_init(playlist, 0, ops);
playlist_generic_init(playlist, ops);
break;
}
}

View File

@ -16,7 +16,7 @@ static struct user_playlist *__user_db_alloc(gchar *name, unsigned int index)
playlist->pl_playlist.pl_type = PL_USER;
playlist->pl_playlist.pl_id = index;
playlist->pl_playlist.pl_ops = &user_ops;
playlist_generic_init(&playlist->pl_playlist, 0, user_pl_ops);
playlist_generic_init(&playlist->pl_playlist, user_pl_ops);
return playlist;
}
@ -81,7 +81,7 @@ static struct playlist_ops user_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_delete = pl_user_delete,
.pl_remove = playlist_generic_remove_track,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};

View File

@ -114,10 +114,8 @@ static inline struct track *__queue_selected(struct queue *queue, unsigned int p
return track;
}
void queue_init(struct queue *queue, unsigned int flags,
const struct queue_ops *ops, void *data)
void queue_init(struct queue *queue, const struct queue_ops *ops, void *data)
{
queue->q_flags = flags;
queue->q_length = 0;
queue->q_sort = NULL;
queue->q_ops = ops;
@ -136,18 +134,6 @@ void queue_deinit(struct queue *queue)
queue->q_sort = NULL;
}
void queue_set_flag(struct queue *queue, enum queue_flags flag)
{
queue->q_flags |= flag;
}
void queue_unset_flag(struct queue *queue, enum queue_flags flag)
{
if (!queue_has_flag(queue, flag))
return;
queue->q_flags &= ~flag;
}
unsigned int queue_add(struct queue *queue, struct track *track)
{
if (queue->q_sort)

View File

@ -124,8 +124,8 @@ void __gui_sidebar_selection_changed(GtkTreeSelection *selection, gpointer data)
__gui_sidebar_filter_iter_convert(&iter, &child);
playlist = gui_sidebar_iter_playlist(&child);
active = playlist_get_random(playlist);
sensitive = (playlist->pl_ops->pl_set_flag != NULL);
active = playlist->pl_random;
sensitive = (playlist->pl_ops->pl_set_random != NULL);
}
gui_treeview_set_playlist(playlist);

View File

@ -70,9 +70,6 @@ unsigned int playlist_size(struct playlist *);
/* Called to set the playlist's random flag. */
void playlist_set_random(struct playlist *, bool);
/* Called to check the playlist's random flag. */
bool playlist_get_random(struct playlist *);
/* Called to change the sort order of the playlist. */
bool playlist_sort(struct playlist *, enum compare_t);

View File

@ -23,7 +23,7 @@ struct playlist_callbacks {
void playlist_generic_set_callbacks(struct playlist_callbacks *);
/* Generic playlist init function. */
void playlist_generic_init(struct playlist *, unsigned int, struct queue_ops *);
void playlist_generic_init(struct playlist *, struct queue_ops *);
/* Generic playlist save function. */
void playlist_generic_save(struct playlist *, struct file *, unsigned int);
@ -44,8 +44,8 @@ bool playlist_generic_add_track_front(struct playlist *, struct track *);
/* Generic playlist remove track operation. */
bool playlist_generic_remove_track(struct playlist *, struct track *);
/* Generic playlist set_flag operation. */
void playlist_generic_set_flag(struct playlist *, enum queue_flags, bool);
/* Generic playlist set_random operation. */
void playlist_generic_set_random(struct playlist *, bool);
/* Generic playlist sorting operations. */
void playlist_generic_sort(struct playlist *, enum compare_t);

View File

@ -18,6 +18,9 @@ enum playlist_type_t {
};
#define PL_RANDOM (1 << 1)
struct playlist_ops {
/* Called to add a track to a playlist. */
bool (*pl_add)(struct playlist *, struct track *);
@ -32,7 +35,7 @@ struct playlist_ops {
bool (*pl_remove)(struct playlist *, struct track *);
/* Called to set a playlist flag. */
void (*pl_set_flag)(struct playlist *, enum queue_flags, bool);
void (*pl_set_random)(struct playlist *, bool);
/* Called to sort the playlist. */
void (*pl_sort)(struct playlist *, enum compare_t);
@ -44,6 +47,7 @@ struct playlist {
gchar *pl_name; /* This playlist's name. */
unsigned int pl_id; /* This playlist's identifier. */
bool pl_random; /* This playlist's random setting. */
void *pl_private; /* This playlist's private data. */
struct queue pl_queue; /* This playlist's queue of tracks. */

View File

@ -14,19 +14,6 @@
struct queue;
enum queue_flags {
Q_UNUSED_0 = (1 << 0), /* Removed: 6.5.4 */
Q_RANDOM = (1 << 1), /* Queue will pick songs randomly. */
Q_UNUSED_2 = (1 << 2), /* Removed: 6.5.4 */
Q_UNUSED_3 = (1 << 3), /* Removed: 6.5.4 */
Q_UNUSED_4 = (1 << 4), /* Removed: 6.5.4 */
Q_UNUSED_5 = (1 << 5), /* Removed: 6.5.4 */
Q_UNUSED_6 = (1 << 6), /* Removed: 6.5.4 */
};
#define Q_VALID_FLAGS (Q_RANDOM)
struct queue_ops {
/* Called to tell a higher layer that a queue has been initialized. */
void *(*qop_init)(struct queue *, void *);
@ -55,7 +42,6 @@ struct queue_iter {
struct queue {
unsigned int q_flags; /* The queue's set of flags. */
unsigned int q_length; /* The queue's total runtime (in seconds). */
GQueue q_tracks; /* The queue's list of tracks. */
GSList *q_sort; /* The queue's sort order. */
@ -108,24 +94,12 @@ static inline struct track *queue_iter_val(struct queue_iter *it)
/* Called to initialize a queue. */
void queue_init(struct queue *, unsigned int, const struct queue_ops *, void *);
void queue_init(struct queue *, const struct queue_ops *, void *);
/* Called to deinitialize a queue. */
void queue_deinit(struct queue *);
/* Called to set a queue flag. */
void queue_set_flag(struct queue *, enum queue_flags);
/* Called to clear a queue flag. */
void queue_unset_flag(struct queue *, enum queue_flags);
/* Called to check if the queue has a specific flag set. */
static inline bool queue_has_flag(struct queue *queue, enum queue_flags flag)
{
return (queue->q_flags & flag) == (unsigned int)flag;
}
/* Called to find the size of the queue. */
static inline unsigned int queue_size(struct queue *queue)
{

View File

@ -14,7 +14,7 @@ static void test_pl_sorted(struct playlist *playlist)
static struct playlist_ops test_noop;
static struct playlist_ops test_ops = {
.pl_can_select = playlist_generic_can_select,
.pl_set_flag = playlist_generic_set_flag,
.pl_set_random = playlist_generic_set_random,
.pl_sort = playlist_generic_sort,
};
static struct playlist_callbacks test_cb = {
@ -46,10 +46,7 @@ static void test_null()
g_assert_false(playlist_remove(NULL, NULL));
g_assert_false(playlist_remove(NULL, track_get(0)));
g_assert_false(playlist_get_random(NULL));
playlist_set_random(NULL, true);
g_assert_false(playlist_get_random(NULL));
g_assert_false(playlist_sort(NULL, COMPARE_TRACK));
g_assert_false(playlist_sort(NULL, COMPARE_TRACK));
playlist_generic_resort(NULL);
@ -65,7 +62,7 @@ static void test_sorting()
struct track *track;
unsigned int i;
playlist_generic_init(&p, 0, NULL);
playlist_generic_init(&p, NULL);
g_assert_cmpuint(g_slist_length(p.pl_queue.q_sort), ==, 3);
playlist_clear_sort(&p);
g_assert_cmpuint(g_slist_length(p.pl_queue.q_sort), ==, 0);
@ -126,7 +123,7 @@ static void test_next()
unsigned int i;
g_random_set_seed(0);
playlist_generic_init(&p, 0, NULL);
playlist_generic_init(&p, NULL);
for (i = 0; i < 13; i++)
playlist_generic_add_track(&p, track_get(i));
@ -144,7 +141,7 @@ static void test_next()
}
playlist_set_random(&p, true);
g_assert_true(playlist_get_random(&p));
g_assert_true(p.pl_random);
/* rand() = { 10, 4, 6, 1, 8, 4, 1 } */
g_assert_cmpuint(playlist_next()->tr_track, ==, 9);
@ -170,7 +167,7 @@ static void test_save_load()
queue_add(&p.pl_queue, track_get(i));
queue_add(&q.pl_queue, track_get(i));
}
queue_set_flag(&p.pl_queue, Q_RANDOM);
playlist_set_random(&p, true);
playlist_clear_sort(&p);
playlist_sort(&p, COMPARE_TRACK);
p.pl_queue.q_cur.it_pos = 3;
@ -187,14 +184,14 @@ static void test_save_load()
playlist_generic_load(&s, &f, PL_SAVE_ALL);
file_close(&f);
g_assert_true(queue_has_flag(&r.pl_queue, Q_RANDOM));
g_assert_true(r.pl_random);
g_assert_cmpuint(r.pl_queue.q_cur.it_pos, ==, 3);
g_assert_cmpuint(g_slist_length(r.pl_queue.q_sort), ==, 1);
g_assert_cmpuint(GPOINTER_TO_UINT(r.pl_queue.q_sort->data),
==, COMPARE_TRACK);
g_assert_cmpuint(g_queue_get_length(&r.pl_queue.q_tracks), ==, 0);
g_assert_false(queue_has_flag(&s.pl_queue, Q_RANDOM));
g_assert_false(s.pl_random);
g_assert_cmpuint(s.pl_queue.q_cur.it_pos, ==, 4);
g_assert(queue_iter_val(&s.pl_queue.q_cur) == queue_at(&q.pl_queue, 4));
g_assert_cmpuint(g_slist_length(s.pl_queue.q_sort), ==, 0);

View File

@ -69,11 +69,11 @@ void test_library()
playlist_selected(track_get(1));
playlist_played(track_get(1));
g_assert_false(playlist_get_random(playlist));
g_assert_false(playlist->pl_random);
playlist_set_random(playlist, true);
g_assert_true(playlist_get_random(playlist));
g_assert_true(playlist->pl_random);
playlist_set_random(playlist, false);
g_assert_false(playlist_get_random(playlist));
g_assert_false(playlist->pl_random);
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 3);
playlist_clear_sort(playlist);

View File

@ -76,15 +76,15 @@ static void test_random()
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
playlist = playlist_get(PL_SYSTEM, i);
g_assert_false(playlist_get_random(playlist_get(PL_SYSTEM, i)));
g_assert_false(playlist->pl_random);
playlist_set_random(playlist, true);
if (i == SYS_PL_HISTORY)
g_assert_false(playlist_get_random(playlist));
g_assert_false(playlist->pl_random);
else
g_assert_true(playlist_get_random(playlist));
g_assert_true(playlist->pl_random);
playlist_set_random(playlist, false);
g_assert_false(playlist_get_random(playlist));
g_assert_false(playlist->pl_random);
}
}

View File

@ -22,11 +22,11 @@ void test_user()
g_assert_null(playlist_new(PL_USER, "Test Playlist"));
g_assert_cmpuint(db->db_size, ==, 1);
g_assert_false(playlist_get_random(playlist));
g_assert_false(playlist->pl_random);
playlist_set_random(playlist, true);
g_assert_true(playlist_get_random(playlist));
g_assert_true(playlist->pl_random);
playlist_set_random(playlist, false);
g_assert_false(playlist_get_random(playlist));
g_assert_false(playlist->pl_random);
g_assert(playlist_current() != playlist);
g_assert_false(playlist_select(playlist));

View File

@ -62,12 +62,11 @@ static void test_init()
struct queue q;
struct queue_iter it;
queue_init(&q, 0, NULL, NULL);
queue_init(&q, NULL, NULL);
g_assert_cmpuint(count_init, ==, 0);
g_assert_null(q.q_private);
g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1);
g_assert_cmpuint(q.q_flags, ==, 0);
g_assert_cmpuint(q.q_length, ==, 0);
g_assert_null(q.q_sort);
g_assert_null(q.q_ops);
@ -80,12 +79,11 @@ static void test_init()
queue_deinit(&q);
g_assert_cmpuint(count_deinit, ==, 0);
queue_init(&q, Q_RANDOM, &test_ops, NULL);
queue_init(&q, &test_ops, NULL);
g_assert_cmpuint(count_init, ==, 1);
g_assert_cmpuint(GPOINTER_TO_UINT(q.q_private), ==, 1);
g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1);
g_assert_cmpuint(q.q_flags, ==, Q_RANDOM);
g_assert_cmpuint(q.q_length, ==, 0);
g_assert_null(q.q_sort);
g_assert(q.q_ops == &test_ops);
@ -94,26 +92,6 @@ static void test_init()
g_assert_cmpuint(count_deinit, ==, 1);
}
static void test_flags()
{
struct queue q;
queue_init(&q, 0, &test_ops, NULL);
g_assert_cmpuint(q.q_flags, ==, 0);
g_assert_false(queue_has_flag(&q, Q_UNUSED_0));
g_assert_false(queue_has_flag(&q, Q_RANDOM));
g_assert_false(queue_has_flag(&q, Q_UNUSED_2));
g_assert_false(queue_has_flag(&q, Q_UNUSED_3));
g_assert_false(queue_has_flag(&q, Q_UNUSED_4));
g_assert_false(queue_has_flag(&q, Q_UNUSED_5));
g_assert_false(queue_has_flag(&q, Q_UNUSED_6));
queue_set_flag(&q, Q_RANDOM);
g_assert_true(queue_has_flag(&q, Q_RANDOM));
queue_unset_flag(&q, Q_RANDOM);
g_assert_cmpuint(q.q_flags, ==, 0);
}
static void test_queue(gconstpointer arg)
{
unsigned int N = GPOINTER_TO_UINT(arg);
@ -129,7 +107,7 @@ static void test_queue(gconstpointer arg)
count_cleared = 0;
count_updated = 0;
queue_init(&q, 0, &test_ops, NULL);
queue_init(&q, &test_ops, NULL);
/* queue_add() */
for (i = 0; i < N; i++) {
@ -210,7 +188,7 @@ static void test_rand_select()
struct queue q;
g_random_set_seed(0);
queue_init(&q, Q_RANDOM, &test_ops, NULL);
queue_init(&q, &test_ops, NULL);
q.q_sort = g_slist_append(q.q_sort, GINT_TO_POINTER(COMPARE_TRACK));
for (i = 0; i < 13; i++)
@ -263,7 +241,6 @@ int main(int argc, char **argv)
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Core/Queue/Initialization", test_init);
g_test_add_func("/Core/Queue/Flags", test_flags);
g_test_add_data_func("/Core/Queue/n = 0", GUINT_TO_POINTER( 0), test_queue);
g_test_add_data_func("/Core/Queue/n = 13", GUINT_TO_POINTER( 13), test_queue);
g_test_add_data_func("/Core/Queue/n = 100,009)", GUINT_TO_POINTER(100009), test_queue);

View File

@ -140,9 +140,9 @@ static void test_sidebar_selection()
GTK_WIDGET(random)));
gtk_toggle_button_set_active(random, false);
g_assert_false(playlist_get_random(collection));
g_assert_false(collection->pl_random);
gtk_toggle_button_set_active(random, true);
g_assert_true(playlist_get_random(collection));
g_assert_true(collection->pl_random);
} else if (i == 1) {
g_assert(gui_model_get_playlist() ==
playlist_lookup(PL_SYSTEM, "History"));