From 5a261507173d28c762ba4f605d7983d928d4bf24 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 28 Dec 2019 21:32:03 +0100 Subject: command: add a playlist-unshuffle command Has a number of restrictions. See: #2491, #7294 --- common/playlist.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'common/playlist.c') diff --git a/common/playlist.c b/common/playlist.c index 300fc48bf9..1fd202b98f 100644 --- a/common/playlist.c +++ b/common/playlist.c @@ -33,6 +33,7 @@ struct playlist_entry *playlist_entry_new(const char *filename) char *local_filename = mp_file_url_to_filename(e, bstr0(filename)); e->filename = local_filename ? local_filename : talloc_strdup(e, filename); e->stream_flags = STREAM_ORIGIN_DIRECT; + e->original_index = -1; return e; } @@ -141,6 +142,8 @@ void playlist_add_file(struct playlist *pl, const char *filename) void playlist_shuffle(struct playlist *pl) { + for (int n = 0; n < pl->num_entries; n++) + pl->entries[n]->original_index = n; for (int n = 0; n < pl->num_entries - 1; n++) { int j = (int)((double)(pl->num_entries - n) * rand() / (RAND_MAX + 1.0)); MPSWAP(struct playlist_entry *, pl->entries[n], pl->entries[n + j]); @@ -148,6 +151,25 @@ void playlist_shuffle(struct playlist *pl) playlist_update_indexes(pl, 0, -1); } +#define CMP_INT(a, b) ((a) == (b) ? 0 : ((a) > (b) ? 1 : -1)) + +static int cmp_unshuffle(const void *a, const void *b) +{ + struct playlist_entry *ea = *(struct playlist_entry **)a; + struct playlist_entry *eb = *(struct playlist_entry **)b; + + if (ea->original_index >= 0 && ea->original_index != eb->original_index) + return CMP_INT(ea->original_index, eb->original_index); + return CMP_INT(ea->pl_index, eb->pl_index); +} + +void playlist_unshuffle(struct playlist *pl) +{ + if (pl->num_entries) + qsort(pl->entries, pl->num_entries, sizeof(pl->entries[0]), cmp_unshuffle); + playlist_update_indexes(pl, 0, -1); +} + // (Explicitly ignores current_was_replaced.) struct playlist_entry *playlist_get_first(struct playlist *pl) { -- cgit v1.2.3