commit d1bc729d9a527817e505a2d5c40c5b1a745f9eec
parent 049d23ab28f2708506dbde362539e9b5f36a10c4
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 10 Mar 2024 02:56:05 +0100
Update to new mplay api
Diffstat:
4 files changed, 58 insertions(+), 27 deletions(-)
diff --git a/.gitmodules b/.gitmodules
@@ -1,3 +1,6 @@
[submodule "lib/liblist"]
path = lib/liblist
url = git@sinitax.com:sinitax/liblist-c
+[submodule "lib/mplay"]
+ path = lib/mplay
+ url = git@sinitax.com:sinitax/mplay
diff --git a/Makefile b/Makefile
@@ -9,6 +9,10 @@ endif
BACKEND ?= mplay
+ifeq "$(BACKEND)" "mplay"
+ CFLAGS += -I lib/mplay
+endif
+
ifeq "$(BACKEND)" "mpd"
LDLIBS += -lmpdclient
endif
diff --git a/lib/mplay b/lib/mplay
@@ -0,0 +1 @@
+Subproject commit c1d226be9672047fd66c20fde4bb2b3d197aa260
diff --git a/src/player_mplay.c b/src/player_mplay.c
@@ -6,6 +6,8 @@
#include "util.h"
#include "log.h"
+#include "mplay.h"
+
#include <sys/wait.h>
#include <sys/mman.h>
#include <unistd.h>
@@ -38,6 +40,7 @@ static void sigpipe_handler(int sig);
static void mplay_kill(void);
static bool mplay_run(struct track *track);
static char *mplay_readline(void);
+static char *mplay_info_arg(char *line, const char *prefix);
void
sigpipe_handler(int sig)
@@ -75,24 +78,23 @@ mplay_run(struct track *track)
if (!mplay.stdin) ERROR(SYSTEM, "fdopen");
setvbuf(mplay.stdin, NULL, _IONBF, 0);
} else {
- close(0);
- close(1);
- close(2);
dup2(input[0], 0);
dup2(output[1], 1);
dup2(output[1], 2);
+ close(input[0]);
+ close(input[1]);
close(output[0]);
close(output[1]);
path = aprintf("%s/%s/%s", datadir,
track->tag->name, track->name);
- execl("/usr/bin/mplay", "mplay", "-i", path, NULL);
+ execlp("mplay", "mplay", path, NULL);
abort();
}
player.loaded = true;
line = mplay_readline();
- if (!line || strcmp(line, "+READY")) {
+ if (!line || !mplay_info_arg(line, MPLAY_INFO_STR_READY)) {
mplay_kill();
MPLAY_STATUS(line);
return false;
@@ -130,6 +132,25 @@ mplay_readline(void)
return linebuf;
}
+char *
+mplay_info_arg(char *line, const char *prefix)
+{
+ if (strncmp(line, "mplay!", 6))
+ return NULL;
+ line += 6;
+
+ size_t prefixlen = strlen(prefix);
+ if (strncmp(line, prefix, prefixlen))
+ return NULL;
+ line += prefixlen;
+
+ if (line[0] == ':')
+ return line+1;
+ else if (!line[0])
+ return line;
+ return NULL;
+}
+
void
player_init(void)
{
@@ -170,6 +191,7 @@ player_update(void)
{
bool queue_empty;
char *tok, *line;
+ char *arg;
if (!player.loaded) {
queue_empty = list_empty(&player.queue);
@@ -185,26 +207,27 @@ player_update(void)
if (current_ms() >= mplay.update_ms + 330) {
mplay.update_ms = current_ms();
- fprintf(mplay.stdin, "status\n");
+ fputc(MPLAY_ACTION_KEY_STATUS, mplay.stdin);
line = mplay_readline();
- if (!line || strncmp(line, "+STATUS:", 8)) {
+ if (!line || !(arg = mplay_info_arg(line, MPLAY_INFO_STR_STATUS))) {
mplay_kill();
- if (!line || strncmp(line, "+EXIT:", 6))
- MPLAY_STATUS(line);
+ MPLAY_STATUS(line);
return;
}
- tok = line;
- while ((tok = strchr(tok, ' '))) {
- if (!strncmp(tok + 1, "vol:", 4)) {
- player.volume = atoi(tok + 5);
- } else if (!strncmp(tok + 1, "pause:", 6)) {
- player.state = atoi(tok + 7)
+ tok = arg;
+ while (1) {
+ if (!strncmp(tok, "volume=", 7)) {
+ player.volume = atoi(tok + 7);
+ } else if (!strncmp(tok, "pause=", 6)) {
+ player.state = atoi(tok + 6)
? PLAYER_STATE_PAUSED : PLAYER_STATE_PLAYING;
- } else if (!strncmp(tok + 1, "pos:", 4)) {
+ } else if (!strncmp(tok, "time=", 5)) {
player.time_pos = atoi(tok + 5);
player.time_end = MAX(player.time_pos, player.time_end);
}
+ tok = strchr(tok, ',');
+ if (!tok) break;
tok += 1;
}
}
@@ -247,7 +270,7 @@ player_toggle_pause(void)
{
char *line;
- fprintf(mplay.stdin, "pause\n");
+ fputc(MPLAY_ACTION_KEY_PAUSE, mplay.stdin);
line = mplay_readline();
if (!line || strncmp(line, "+PAUSE:", 7)) {
mplay_kill();
@@ -293,19 +316,18 @@ player_stop(void)
int
player_seek(int sec)
{
- char *line;
+ char *line, *arg;
- fprintf(mplay.stdin, "seek %i\n", sec);
+ putc(MPLAY_ACTION_KEY_SEEK, mplay.stdin);
+ fprintf(mplay.stdin, "%i\n", sec);
line = mplay_readline();
- if (!line || strncmp(line, "+SEEK:", 6)) {
+ if (!line || !(arg = mplay_info_arg(line, MPLAY_INFO_STR_SEEK))) {
mplay_kill();
- if (line && !strncmp(line, "+EXIT:", 6))
- return PLAYER_OK;
MPLAY_STATUS(line);
return PLAYER_ERR;
}
- player.time_pos = atoi(line + 7);
+ player.time_pos = atoi(arg);
player.time_end = MAX(player.time_pos, player.time_end);
return PLAYER_OK;
@@ -314,22 +336,23 @@ player_seek(int sec)
int
player_set_volume(unsigned int vol)
{
- char *line;
+ char *line, *arg;
if (player.volume == -1) {
PLAYER_STATUS("volume control not supported");
return PLAYER_ERR;
}
- fprintf(mplay.stdin, "vol %i\n", vol);
+ putc(MPLAY_ACTION_KEY_VOLUME, mplay.stdin);
+ fprintf(mplay.stdin, "%i\n", vol);
line = mplay_readline();
- if (!line || strncmp(line, "+VOLUME:", 8)) {
+ if (!line || !(arg = mplay_info_arg(line, MPLAY_INFO_STR_VOLUME))) {
mplay_kill();
MPLAY_STATUS(line);
return PLAYER_ERR;
}
- player.volume = atoi(line + 9);
+ player.volume = atoi(arg);
return PLAYER_OK;
}