From da753196af6593b2d6e94f691c9d1287b3ea093b Mon Sep 17 00:00:00 2001 From: David Vaughan Date: Sun, 4 Feb 2024 10:58:09 -0800 Subject: [PATCH] player: change insert_next to insert_at Change the `playlist_insert_next` function to `playlist_insert_at` (ie, insert at the location of an entry, rather than after it, and rename to be clearer that it doesn't have anything to do with the currently-playing entry). Also, replace calls to `playlist_add` with calls to `playlist_insert_at`, since the former has become redundant. --- common/playlist.c | 23 +++++++---------------- common/playlist.h | 8 +++----- demux/demux_libarchive.c | 2 +- demux/demux_playlist.c | 8 ++++---- options/parse_commandline.c | 6 +++--- player/command.c | 9 ++++----- player/misc.c | 2 +- 7 files changed, 23 insertions(+), 35 deletions(-) diff --git a/common/playlist.c b/common/playlist.c index eda583224c..5ff34496c8 100644 --- a/common/playlist.c +++ b/common/playlist.c @@ -60,24 +60,15 @@ static void playlist_update_indexes(struct playlist *pl, int start, int end) pl->entries[n]->pl_index = n; } -void playlist_add(struct playlist *pl, struct playlist_entry *add) -{ - assert(add->filename); - MP_TARRAY_APPEND(pl, pl->entries, pl->num_entries, add); - add->pl = pl; - add->pl_index = pl->num_entries - 1; - add->id = ++pl->id_alloc; - talloc_steal(pl, add); -} - -// Inserts the entry so that it comes directly after "at" (or move to end, if at==NULL). -void playlist_insert_next(struct playlist *pl, struct playlist_entry *add, - struct playlist_entry *at) +// Inserts the entry so that it takes "at"'s place, shifting "at" and all +// further entires to the right (or append to end, if at==NULL). +void playlist_insert_at(struct playlist *pl, struct playlist_entry *add, + struct playlist_entry *at) { assert(add->filename); assert(!at || at->pl == pl); - int index = at ? at->pl_index + 1 : pl->num_entries; + int index = at ? at->pl_index : pl->num_entries; MP_TARRAY_INSERT_AT(pl, pl->entries, pl->num_entries, index, add); add->pl = pl; @@ -156,9 +147,9 @@ void playlist_move(struct playlist *pl, struct playlist_entry *entry, MPMAX(index + 1, old_index + 1)); } -void playlist_add_file(struct playlist *pl, const char *filename) +void playlist_append_file(struct playlist *pl, const char *filename) { - playlist_add(pl, playlist_entry_new(filename)); + playlist_insert_at(pl, playlist_entry_new(filename), NULL); } void playlist_populate_playlist_path(struct playlist *pl, const char *path) diff --git a/common/playlist.h b/common/playlist.h index f1ee07d77c..1e01b1a5e3 100644 --- a/common/playlist.h +++ b/common/playlist.h @@ -81,10 +81,8 @@ void playlist_entry_add_params(struct playlist_entry *e, struct playlist_entry *playlist_entry_new(const char *filename); -void playlist_add(struct playlist *pl, struct playlist_entry *add); - -void playlist_insert_next(struct playlist *pl, struct playlist_entry *entry, - struct playlist_entry *at); +void playlist_insert_at(struct playlist *pl, struct playlist_entry *entry, + struct playlist_entry *at); void playlist_remove(struct playlist *pl, struct playlist_entry *entry); void playlist_clear(struct playlist *pl); @@ -93,7 +91,7 @@ void playlist_clear_except_current(struct playlist *pl); void playlist_move(struct playlist *pl, struct playlist_entry *entry, struct playlist_entry *at); -void playlist_add_file(struct playlist *pl, const char *filename); +void playlist_append_file(struct playlist *pl, const char *filename); void playlist_populate_playlist_path(struct playlist *pl, const char *path); void playlist_shuffle(struct playlist *pl); void playlist_unshuffle(struct playlist *pl); diff --git a/demux/demux_libarchive.c b/demux/demux_libarchive.c index ec5049849d..a85e40ff89 100644 --- a/demux/demux_libarchive.c +++ b/demux/demux_libarchive.c @@ -91,7 +91,7 @@ static int open_file(struct demuxer *demuxer, enum demux_check check) qsort(files, num_files, sizeof(files[0]), cmp_filename); for (int n = 0; n < num_files; n++) - playlist_add_file(pl, files[n]); + playlist_append_file(pl, files[n]); playlist_set_stream_flags(pl, demuxer->stream_origin); diff --git a/demux/demux_playlist.c b/demux/demux_playlist.c index 63355be4ff..66be4b1270 100644 --- a/demux/demux_playlist.c +++ b/demux/demux_playlist.c @@ -202,7 +202,7 @@ static void pl_free_line(struct pl_parser *p, bstr line) static void pl_add(struct pl_parser *p, bstr entry) { char *s = bstrto0(NULL, entry); - playlist_add_file(p->pl, s); + playlist_append_file(p->pl, s); talloc_free(s); } @@ -266,7 +266,7 @@ ok: talloc_free(fn); e->title = talloc_steal(e, title); title = NULL; - playlist_add(p->pl, e); + playlist_insert_at(p->pl, e, NULL); } pl_free_line(p, line); line = pl_get_line(p); @@ -296,7 +296,7 @@ static int parse_ref_init(struct pl_parser *p) bstr burl = bstr0(p->s->url); if (bstr_eatstart0(&burl, "http://") && check_mimetype(p->s, mmsh_types)) { MP_INFO(p, "Redirecting to mmsh://\n"); - playlist_add_file(p->pl, talloc_asprintf(p, "mmsh://%.*s", BSTR_P(burl))); + playlist_append_file(p->pl, talloc_asprintf(p, "mmsh://%.*s", BSTR_P(burl))); return 0; } @@ -456,7 +456,7 @@ static bool scan_dir(struct pl_parser *p, char *path, scan_dir(p, file, dir_stack, num_dir_stack + 1); } else { - playlist_add_file(p->pl, dir_entries[n].path); + playlist_append_file(p->pl, dir_entries[n].path); } } diff --git a/options/parse_commandline.c b/options/parse_commandline.c index ad91b8d61a..ded8531da8 100644 --- a/options/parse_commandline.c +++ b/options/parse_commandline.c @@ -103,10 +103,10 @@ static void process_non_option(struct playlist *files, const char *arg) // Glob filenames on Windows (cmd.exe doesn't do this automatically) if (glob(arg, 0, NULL, &gg)) { - playlist_add_file(files, arg); + playlist_append_file(files, arg); } else { for (int i = 0; i < gg.gl_pathc; i++) - playlist_add_file(files, gg.gl_pathv[i]); + playlist_append_file(files, gg.gl_pathv[i]); globfree(&gg); } @@ -114,7 +114,7 @@ static void process_non_option(struct playlist *files, const char *arg) #else static void process_non_option(struct playlist *files, const char *arg) { - playlist_add_file(files, arg); + playlist_append_file(files, arg); } #endif diff --git a/player/command.c b/player/command.c index cbb5d787fe..2c5c67e78a 100644 --- a/player/command.c +++ b/player/command.c @@ -5543,11 +5543,10 @@ static void cmd_loadfile(void *p) playlist_entry_add_param(entry, bstr0(pairs[i]), bstr0(pairs[i + 1])); } - if (insert_next) { - playlist_insert_next(mpctx->playlist, entry, mpctx->playlist->current); - } else { - playlist_add(mpctx->playlist, entry); - } + struct playlist_entry *at = insert_next ? + playlist_get_next(mpctx->playlist, +1) : NULL; + + playlist_insert_at(mpctx->playlist, entry, at); struct mpv_node *res = &cmd->result; node_init(res, MPV_FORMAT_NODE_MAP, NULL); diff --git a/player/misc.c b/player/misc.c index e267a53a46..693fa5a7e7 100644 --- a/player/misc.c +++ b/player/misc.c @@ -322,7 +322,7 @@ void merge_playlist_files(struct playlist *pl) edl = talloc_strdup_append_buffer(edl, e->filename); } playlist_clear(pl); - playlist_add_file(pl, edl); + playlist_append_file(pl, edl); talloc_free(edl); }