Skip to content

Commit

Permalink
Add support for consume: oneshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Mstrodl committed Feb 12, 2024
1 parent 5297c27 commit 52c493b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
7 changes: 4 additions & 3 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ ins).
Player Commands
^^^^^^^^^^^^^^^

:command:`consume <on|off>` - Toggle consume mode if state (:samp:`on`
or :samp:`off`) is not specified.
:command:`consume <on|once|off>` - Toggle consume mode if state (:samp:`on`
:samp:`once`, or :samp:`off`) is not specified. :samp:`once` toggles
to :samp:`off`.

:command:`crossfade [<seconds>]` - Gets and sets the current amount of
crossfading between songs (:samp:`0` disables crossfading).
Expand Down Expand Up @@ -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.
Expand Down
35 changes: 33 additions & 2 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,39 @@ cmd_single(int argc, char **argv, struct mpd_connection *conn)
int
cmd_consume(int argc, char **argv, struct mpd_connection *conn)
{
return bool_cmd(argc, argv, conn,
mpd_status_get_consume, mpd_run_consume);
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;
}

int
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<file>", "Clear the playlist"},
{"consume", 0, 1, 0, cmd_consume, "<on|off>", "Toggle consume mode, or specify state"},
{"consume", 0, 1, 0, cmd_consume, "<on|once|off>", "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, "[<seconds>]", "Set and display crossfade settings"},
{"current", 0, 0, 0, cmd_current, "", "Show the currently playing song"},
Expand Down
9 changes: 6 additions & 3 deletions src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ print_status(struct mpd_connection *conn)
printf("off ");

printf("consume: ");
if (mpd_status_get_consume(status))
printf("on \n");
else printf("off\n");
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");

if (mpd_status_get_error(status) != NULL)
printf("ERROR: %s\n",
Expand Down
10 changes: 6 additions & 4 deletions src/status_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ status_value(const struct mpd_status *status, const char *name)
return "off";
}
} else if (strcmp(name, "consume") == 0) {
if (mpd_status_get_consume(status)) {
return "on";
} else {
return "off";
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 (strcmp(name, "kbitrate") == 0) {
sprintf(buffer, "%u", mpd_status_get_kbit_rate(status));
Expand Down

0 comments on commit 52c493b

Please sign in to comment.