Commit Diff


commit - 714abf92a1b01d8ddfd63d2fc73436399012c29d
commit + 82e732c933afb90ceeef9e0ac16ca9053a4ea851
blob - 686e14fa39f3bb2032527d4639aef0424ce8813c
blob + 97726c17a5b76fa26e0fb308e0b3ae010a93bef2
--- playlist.c
+++ playlist.c
@@ -18,6 +18,7 @@
 
 #include <regex.h>
 #include <stdlib.h>
+#include <string.h>
 #include <syslog.h>
 
 #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;