Commit Diff
Commit:
74c987d53230b125b919cd1ab99426e6d81a8bbc
From:
Omar Polo <op@omarpolo.com>
Date:
Sat Feb 19 10:16:25 2022 UTC
Message:
keep current song in a dedicated variable playlist.songs[play_off] is not a good representation for the current playing song after all. After a `load' command, or a flush request, the play_off changes but we're still playing the "old" song. This saves the current song in a dedicated variable that's updated upon playlist_advance and playlist_previous, thus givin a more accurate view.
commit - 82e732c933afb90ceeef9e0ac16ca9053a4ea851
commit + 74c987d53230b125b919cd1ab99426e6d81a8bbc
blob - 927f73b9354d3ed73877cc26490df61152da57a6
blob + 9bfc968d47ef5e4636bfbe75f08f8d0e019269cc
--- amused.c
+++ amused.c
@@ -103,7 +103,7 @@ main_status(void)
break;
}
- if ((cur = playlist_current()) != NULL)
+ if ((cur = current_song) != NULL)
log_info("playing %s", cur);
else
log_info("not playing anything");
@@ -437,7 +437,7 @@ main_playlist_resume(void)
{
const char *song;
- if ((song = playlist_current()) == NULL)
+ if ((song = current_song) == NULL)
song = playlist_advance();
for (; song != NULL; song = playlist_advance()) {
@@ -487,7 +487,7 @@ main_restart_track(void)
{
const char *song;
- song = playlist_current();
+ song = current_song;
if (song == NULL)
return;
@@ -556,13 +556,11 @@ main_send_status(struct imsgev *iev)
main_send_status(struct imsgev *iev)
{
struct player_status s;
- const char *song;
memset(&s, 0, sizeof(s));
- song = playlist_current();
- if (song != NULL)
- strlcpy(s.path, song, sizeof(s.path));
+ if (current_song != NULL)
+ strlcpy(s.path, current_song, sizeof(s.path));
s.status = play_state;
imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
blob - 97726c17a5b76fa26e0fb308e0b3ae010a93bef2
blob + 5db3aae35675d0a73a6f4c8dbe5ddaa4c4cff778
--- playlist.c
+++ playlist.c
@@ -27,12 +27,23 @@ struct playlist playlist;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
-struct playlist playlist;
-enum play_state play_state;
-int repeat_one;
-int repeat_all = 1;
-ssize_t play_off = -1;
+struct playlist playlist;
+enum play_state play_state;
+int repeat_one;
+int repeat_all = 1;
+ssize_t play_off = -1;
+const char *current_song;
+static void
+setsong(ssize_t i)
+{
+ free((char *)current_song);
+ if (i == -1)
+ current_song = NULL;
+ else
+ current_song = xstrdup(playlist.songs[i]);
+}
+
void
playlist_swap(struct playlist *p)
{
@@ -92,17 +103,6 @@ playlist_current(void)
}
const char *
-playlist_current(void)
-{
- if (playlist.len == 0 || play_off == -1) {
- play_state = STATE_STOPPED;
- return NULL;
- }
-
- return playlist.songs[play_off];
-}
-
-const char *
playlist_advance(void)
{
if (playlist.len == 0) {
@@ -117,10 +117,12 @@ playlist_advance(void)
else {
play_state = STATE_STOPPED;
play_off = -1;
+ setsong(play_off);
return NULL;
}
}
+ setsong(play_off);
play_state = STATE_PLAYING;
return playlist.songs[play_off];
}
@@ -140,10 +142,12 @@ playlist_previous(void)
else {
play_state = STATE_STOPPED;
play_off = -1;
+ setsong(play_off);
return NULL;
}
}
+ setsong(play_off);
play_state = STATE_PLAYING;
return playlist.songs[play_off];
}
@@ -212,5 +216,6 @@ playlist_jump(const char *arg)
play_state = STATE_PLAYING;
play_off = i;
+ setsong(play_off);
return playlist.songs[i];
}
blob - 95978a15a59fdabc77eff797ba19b27535c9d8e1
blob + 996655b0ce47c796db811762bde003c0671313c1
--- playlist.h
+++ playlist.h
@@ -35,11 +35,11 @@ extern ssize_t play_off;
extern int repeat_one;
extern int repeat_all;
extern ssize_t play_off;
+extern const char *current_song;
void playlist_swap(struct playlist *);
void playlist_push(struct playlist *, const char *);
void playlist_enqueue(const char *);
-const char *playlist_current(void);
const char *playlist_advance(void);
const char *playlist_previous(void);
void playlist_reset(void);
Omar Polo