commit 82e732c933afb90ceeef9e0ac16ca9053a4ea851 from: Omar Polo date: Sat Feb 19 10:16:25 2022 UTC add a heuristic to try to keep the current song after a `load' commit - 714abf92a1b01d8ddfd63d2fc73436399012c29d commit + 82e732c933afb90ceeef9e0ac16ca9053a4ea851 blob - 686e14fa39f3bb2032527d4639aef0424ce8813c blob + 97726c17a5b76fa26e0fb308e0b3ae010a93bef2 --- playlist.c +++ playlist.c @@ -18,6 +18,7 @@ #include #include +#include #include #include "log.h" @@ -35,8 +36,35 @@ ssize_t play_off = -1; void playlist_swap(struct playlist *p) { + ssize_t i = -1; + + if (play_off != -1) { + /* try to adjust play_off to match the same song */ + for (i = 0; i < p->len; ++i) { + if (!strcmp(playlist.songs[play_off], p->songs[i])) + break; + } + /* try to match one song before */ + if (i == p->len && play_off >= 1) + for (i = 0; i < p->len; ++i) + if (!strcmp(playlist.songs[play_off-1], + p->songs[i])) + break; + /* or one song after */ + if (i == p->len && play_off < playlist.len-1) + for (i = 0; i < p->len; ++i) + if (!strcmp(playlist.songs[play_off+1], + p->songs[i])) + break; + if (i == p->len) + i = -1; + } + playlist_truncate(); + if (i != -1) + play_off = i; + playlist.len = p->len; playlist.cap = p->cap; playlist.songs = p->songs;