From 9e162d26046005ca0f58f22a057e1d8393900d50 Mon Sep 17 00:00:00 2001 From: David Vaughan Date: Sat, 3 Feb 2024 13:44:49 -0800 Subject: player: add loadfile insert-next commands This commit adds two new commands (`insert-next` and `insert-next-play`) which mirror the existing commands, `append` and `append-play` in functionality, with the difference that they insert directly after the current playlist entry, rather than at the end of the playlist. This change gives MPV a piece of functionality already found in (for example) Spotify's media player: "play next". Additionally, using the new `insert-next` command, users can trivially write a script to play a new piece of media immediately without otherwise clearing or altering the remainder of the playlist. --- common/playlist.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'common/playlist.c') diff --git a/common/playlist.c b/common/playlist.c index 5de8e62745..eda583224c 100644 --- a/common/playlist.c +++ b/common/playlist.c @@ -70,6 +70,25 @@ void playlist_add(struct playlist *pl, struct playlist_entry *add) 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) +{ + assert(add->filename); + assert(!at || at->pl == pl); + + int index = at ? at->pl_index + 1 : pl->num_entries; + MP_TARRAY_INSERT_AT(pl, pl->entries, pl->num_entries, index, add); + + add->pl = pl; + add->pl_index = index; + add->id = ++pl->id_alloc; + + playlist_update_indexes(pl, index, pl->num_entries); + + talloc_steal(pl, add); +} + void playlist_entry_unref(struct playlist_entry *e) { e->reserved--; -- cgit v1.2.3