From e9b4275d1f5b8d166fa772242af44b4d056ed482 Mon Sep 17 00:00:00 2001 From: Christian Soffke Date: Sun, 5 Nov 2023 02:32:39 +0100 Subject: Playlists: Fix resuming from control commands with first_index > 0 add_track_to_playlist_unlocked only increased a playlist's first index as necessary when its position parameter was negative (i.e. one of the special insert positions was specified). A negative value was not stored in the control file, but was always converted into an absolute position. Thus, any adjustments to first_index weren't repeated when resuming from the control file. In particular, shuffled playlists were affected (in case of first_index > 0), when inserting at positions <= first_index, including appending a track to the end of a playlist. This works by inserting at first_index and increasing first_index by 1 afterwards. Similarly, adding tracks in a shuffled fashion could increase first index, whenever that was the value randomly calculated for a track position, effectively appending it (I assume this is on purpose). To make sure that first_index adjustments are recovered when resuming from the control file, and to be able to differentiate between a prepended or appended track, store the special value PLAYLIST_INSERT_LAST_ROTATED as the insert position in the control file whenever first_index would have been used before, and a special position (other than PLAYLIST_PREPEND) was provided to the function. Change-Id: I31f26796627fb136daeddd046cb1892bdf1b4014 --- apps/playlist.c | 39 +++++++++++++++++++++++++++++++++------ apps/playlist.h | 3 ++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/apps/playlist.c b/apps/playlist.c index bd2d33ea78..1f31ec81c1 100644 --- a/apps/playlist.c +++ b/apps/playlist.c @@ -1208,6 +1208,8 @@ static int remove_all_tracks_unlocked(struct playlist_info *playlist, bool write * PLAYLIST_INSERT_FIRST - Add track immediately after current song, no * matter what other tracks have been inserted * PLAYLIST_INSERT_LAST - Add track to end of playlist + * PLAYLIST_INSERT_LAST_ROTATED - Add track to end of playlist, by inserting at + * first_index, then increasing first_index by 1 * PLAYLIST_INSERT_SHUFFLED - Add track at some random point between the * current playing track and end of playlist * PLAYLIST_INSERT_LAST_SHUFFLED - Add tracks in random order to the end of @@ -1260,11 +1262,15 @@ static int add_track_to_playlist_unlocked(struct playlist_info* playlist, playlist->last_insert_pos = position; break; case PLAYLIST_INSERT_LAST: - if (playlist->first_index > 0) - position = insert_position = playlist->first_index; - else + if (playlist->first_index <= 0) + { position = insert_position = playlist->amount; - + playlist->last_insert_pos = position; + break; + } + /* fallthrough */ + case PLAYLIST_INSERT_LAST_ROTATED: + position = insert_position = playlist->first_index; playlist->last_insert_pos = position; break; case PLAYLIST_INSERT_SHUFFLED: @@ -1330,16 +1336,37 @@ static int add_track_to_playlist_unlocked(struct playlist_info* playlist, if (orig_position < 0) { if (playlist->amount > 0 && insert_position <= playlist->index && - playlist->started) + playlist->started && orig_position != PLAYLIST_INSERT_LAST_ROTATED) playlist->index++; + /* + * When inserting into a playlist at positions before or equal to first_index + * (unless PLAYLIST_PREPEND is specified explicitly), adjust first_index, so + * that track insertion near the end does not affect the start of the playlist + */ if (playlist->amount > 0 && insert_position <= playlist->first_index && orig_position != PLAYLIST_PREPEND && playlist->started) + { + /* + * To ensure proper resuming from control file for a track that is supposed + * to be appended, but is inserted at first_index, store position as special + * value. + * If we were to store the position unchanged, i.e. use first_index, + * track would be prepended, instead, after resuming. + */ + if (insert_position == playlist->first_index) + position = PLAYLIST_INSERT_LAST_ROTATED; + playlist->first_index++; + } } + else if (playlist->amount > 0 && insert_position < playlist->first_index && + playlist->started) + playlist->first_index++; if (insert_position < playlist->last_insert_pos || - (insert_position == playlist->last_insert_pos && position < 0)) + (insert_position == playlist->last_insert_pos && position < 0 && + position != PLAYLIST_INSERT_LAST_ROTATED)) playlist->last_insert_pos++; if (seek_pos < 0 && playlist->control_fd >= 0) diff --git a/apps/playlist.h b/apps/playlist.h index 5ad90db6f1..4d814c7523 100644 --- a/apps/playlist.h +++ b/apps/playlist.h @@ -62,7 +62,8 @@ enum { PLAYLIST_INSERT_FIRST = -4, PLAYLIST_INSERT_SHUFFLED = -5, PLAYLIST_REPLACE = -6, - PLAYLIST_INSERT_LAST_SHUFFLED = -7 + PLAYLIST_INSERT_LAST_SHUFFLED = -7, + PLAYLIST_INSERT_LAST_ROTATED = -8 }; struct playlist_info -- cgit v1.2.3