tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit d7c7fa7971229dfdd8cab495de6e31b86b237e03
parent 28482cd5d05a8ccb7c98fd6cf54bf5d7d69aa6eb
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 18 Dec 2022 02:45:18 +0100

Fix autoplay for mplay backend

Diffstat:
Msrc/player.h | 6++++++
Msrc/player_mpd.c | 6------
Msrc/player_mplay.c | 33++++++++++++++++++++++++++-------
3 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/src/player.h b/src/player.h @@ -3,6 +3,12 @@ #include "list.h" #include "util.h" +#define PLAYER_STATUS(lvl, ...) do { \ + player.status_lvl = PLAYER_STATUS_MSG_ ## lvl; \ + if (player.status) free(player.status); \ + player.status = aprintf(__VA_ARGS__); \ + } while (0) + enum { PLAYER_STATUS_OK, PLAYER_STATUS_ERR diff --git a/src/player_mpd.c b/src/player_mpd.c @@ -16,12 +16,6 @@ #include <sys/mman.h> #include <unistd.h> -#define PLAYER_STATUS(lvl, ...) do { \ - player.status_lvl = PLAYER_STATUS_MSG_ ## lvl; \ - if (player.status) free(player.status); \ - player.status = aprintf(__VA_ARGS__); \ - } while (0) - struct mpd_player { struct mpd_connection *conn; diff --git a/src/player_mplay.c b/src/player_mplay.c @@ -16,12 +16,6 @@ #include <stdlib.h> #include <string.h> -#define PLAYER_STATUS(lvl, ...) do { \ - player.status_lvl = PLAYER_STATUS_MSG_ ## lvl; \ - if (player.status) free(player.status); \ - player.status = aprintf(__VA_ARGS__); \ - } while (0) - struct mplay_player { FILE *stdin; FILE *stdout; @@ -31,12 +25,21 @@ struct mplay_player { struct player player; struct mplay_player mplay; +static void sigpipe_handler(int sig); + static bool mplay_alive(void); static void mplay_kill(void); static bool mplay_run(struct track *track); static char *mplay_readline(void); + static void player_clear_status(void); +void +sigpipe_handler(int sig) +{ + mplay_kill(); +} + bool mplay_alive(void) { @@ -115,8 +118,10 @@ mplay_readline(void) char *tok; /* TODO: add timeout */ - if (!fgets(linebuf, sizeof(linebuf), mplay.stdout)) + if (!mplay.stdout || !fgets(linebuf, sizeof(linebuf), mplay.stdout)) { + mplay_kill(); /* dont clear track yet */ return NULL; + } tok = strchr(linebuf, '\n'); if (tok) *tok = '\0'; @@ -154,6 +159,8 @@ player_init(void) player.status = NULL; player.status_lvl = PLAYER_STATUS_MSG_INFO; + + signal(SIGPIPE, sigpipe_handler); } void @@ -172,12 +179,24 @@ player_deinit(void) void player_update(void) { + bool queue_empty; char *tok, *line; + if (!player.loaded) { + queue_empty = list_empty(&player.queue); + if (player.track && player.autoplay || !queue_empty) { + if (player_next() != PLAYER_STATUS_OK) + player_clear_track(); + } else if (player.track) { + player_clear_track(); + } + } + if (!player.loaded) return; fprintf(mplay.stdin, "status\n"); line = mplay_readline(); + if (!player.loaded) return; if (!line || strncmp(line, "+STATUS:", 8)) { PLAYER_STATUS(ERR, "Bad response"); return;