From ae9b76db9871fc76e708bdfd08cfb61a83f59789 Mon Sep 17 00:00:00 2001 From: Mary Strodl Date: Sun, 11 Feb 2024 22:48:02 -0500 Subject: [PATCH] Add support for consume: oneshot --- doc/index.rst | 7 ++++--- src/command.c | 36 ++++++++++++++++++++++++++++++++++++ src/main.c | 2 +- src/status.c | 9 +++++++++ src/status_format.c | 10 ++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index 4a004a7..078dacd 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -139,8 +139,9 @@ ins). Player Commands ^^^^^^^^^^^^^^^ -:command:`consume ` - Toggle consume mode if state (:samp:`on` - or :samp:`off`) is not specified. +:command:`consume ` - Toggle consume mode if state (:samp:`on` + :samp:`once`, or :samp:`off`) is not specified. :samp:`once` toggles + to :samp:`off`. :command:`crossfade []` - Gets and sets the current amount of crossfading between songs (:samp:`0` disables crossfading). @@ -462,7 +463,7 @@ Other Commands %random% Current status of random mode. 'on' or 'off' %repeat% Current status of repeat mode. 'on' or 'off' %single% Current status of single mode. 'on', 'once', or 'off' - %consume% Current status of consume mode. 'on' or 'off' + %consume% Current status of consume mode. 'on', 'once', or 'off' %kbitrate% The bit rate in kbps for the current song. %audioformat% The audio format which MPD is currently playing as 'samplerate:bits:channels'. %samplerate% The sample rate in Hz extracted from the current MPD audio format. diff --git a/src/command.c b/src/command.c index fbf494a..5dfc5ea 100644 --- a/src/command.c +++ b/src/command.c @@ -1093,8 +1093,44 @@ cmd_single(int argc, char **argv, struct mpd_connection *conn) int cmd_consume(int argc, char **argv, struct mpd_connection *conn) { +#if LIBMPDCLIENT_CHECK_VERSION(2,21,0) + enum mpd_consume_state mode = MPD_CONSUME_UNKNOWN; + + if (argc == 1) { + if (strcasecmp(argv[0], "once") == 0) + mode = MPD_CONSUME_ONESHOT; + else { + int mode_i = get_boolean(argv[0]); + if (mode_i < 0) + return -1; + else if (mode_i) + mode = MPD_CONSUME_ON; + else + mode = MPD_CONSUME_OFF; + } + } else { + struct mpd_status *status; + status = getStatus(conn); + enum mpd_consume_state cur = mpd_status_get_consume_state(status); + + if (cur == MPD_CONSUME_ONESHOT || cur == MPD_CONSUME_ON) + mode = MPD_CONSUME_OFF; + else if (cur == MPD_CONSUME_OFF) + mode = MPD_CONSUME_ON; + + mpd_status_free(status); + } + + if (mode == MPD_CONSUME_UNKNOWN) + return -1; + else if (!mpd_run_consume_state(conn, mode)) + printErrorAndExit(conn); + + return 1; +#else return bool_cmd(argc, argv, conn, mpd_status_get_consume, mpd_run_consume); +#endif } int diff --git a/src/main.c b/src/main.c index 1c744eb..fbbcb90 100644 --- a/src/main.c +++ b/src/main.c @@ -56,7 +56,7 @@ static const struct command { {"clear", 0, 0, 0, cmd_clear, "", "Clear the queue"}, {"clearerror", 0, 0, 0, cmd_clearerror, "", "Clear the current error"}, {"clearplaylist", 1, 1, 0, cmd_clearplaylist, "", "Clear the playlist"}, - {"consume", 0, 1, 0, cmd_consume, "", "Toggle consume mode, or specify state"}, + {"consume", 0, 1, 0, cmd_consume, "", "Toggle consume mode, or specify state"}, {"crop", 0, 0, 0, cmd_crop, "", "Remove all but the currently playing song"}, {"crossfade", 0, 1, 0, cmd_crossfade, "[]", "Set and display crossfade settings"}, {"current", 0, 0, 0, cmd_current, "", "Show the currently playing song"}, diff --git a/src/status.c b/src/status.c index a95e41c..c559b0d 100644 --- a/src/status.c +++ b/src/status.c @@ -83,9 +83,18 @@ print_status(struct mpd_connection *conn) printf("off "); printf("consume: "); +#if LIBMPDCLIENT_CHECK_VERSION(2,21,0) + if (mpd_status_get_consume_state(status) == MPD_CONSUME_ON) + printf("on \n"); + else if (mpd_status_get_consume_state(status) == MPD_CONSUME_ONESHOT) + printf("once\n"); + else if (mpd_status_get_consume_state(status) == MPD_CONSUME_OFF) + printf("off \n"); +#else if (mpd_status_get_consume(status)) printf("on \n"); else printf("off\n"); +#endif if (mpd_status_get_error(status) != NULL) printf("ERROR: %s\n", diff --git a/src/status_format.c b/src/status_format.c index 7fdb404..8f5c935 100644 --- a/src/status_format.c +++ b/src/status_format.c @@ -89,11 +89,21 @@ status_value(const struct mpd_status *status, const char *name) return "off"; } } else if (strcmp(name, "consume") == 0) { +#if LIBMPDCLIENT_CHECK_VERSION(2,21,0) + if (mpd_status_get_consume_state(status) == MPD_CONSUME_ON) { + return "on"; + } else if (mpd_status_get_consume_state(status) == MPD_CONSUME_ONESHOT) { + return "once"; + } else if (mpd_status_get_consume_state(status) == MPD_CONSUME_OFF) { + return "off"; + } +#else if (mpd_status_get_consume(status)) { return "on"; } else { return "off"; } +#endif } else if (strcmp(name, "kbitrate") == 0) { sprintf(buffer, "%u", mpd_status_get_kbit_rate(status)); } else if (strcmp(name, "audioformat") == 0) {