From: Michal Nazarewicz <min...@mina86.com> This commit adds "only" argument to enable and disable commands. When specified, mpc will enable (disable) outputs specified on command line and disable (enable) all other outputs. This is useful if one wants to enable only a single output with a single command. --- src/command.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++-------- src/main.c | 4 +- 2 files changed, 70 insertions(+), 13 deletions(-)
diff --git a/src/command.c b/src/command.c index 2adc4c9..914f87b 100644 --- a/src/command.c +++ b/src/command.c @@ -368,47 +368,104 @@ cmd_outputs(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection * return( 0 ); } +static unsigned +count_outputs(struct mpd_connection *conn) +{ + struct mpd_output *output; + unsigned max = 0; + + mpd_send_outputs(conn); + + while ((output = mpd_recv_output(conn)) != NULL) { + max = mpd_output_get_id(output); + mpd_output_free(output); + } + + mpd_response_finish(conn); + + return max; +} + static int enable_disable(int argc, char **argv, struct mpd_connection *conn, - bool (*callback)(struct mpd_connection *conn, unsigned id)) + bool (*matched)(struct mpd_connection *conn, unsigned id), + bool (*not_matched)(struct mpd_connection *conn, unsigned id)) { - bool show_outputs = false; + unsigned *ids, *ids_end, max; + bool only = false; int arg; - if (!mpd_command_list_begin(conn, false)) { - printErrorAndExit(conn); + if (!strcmp(argv[0], "only")) { + only = true; + ++argv; + if (!--argc) { + DIE("No outputs specified."); + } } + ids = malloc(argc * sizeof *ids); + ids_end = ids; + for (int i = argc; i; --i, ++argv) { if (!parse_int(*argv, &arg) || arg <= 0) { fprintf(stderr, "%s: not a positive integer\n", *argv); } else { /* We decrement by 1 to make it natural to the user. */ - callback(conn, arg - 1); - show_outputs = true; + *ids_end++ = arg - 1; } } - if (!mpd_command_list_end(conn) || !mpd_response_finish(conn)) { + if (ids == ids_end) { + goto done; + } + + if (only) { + max = count_outputs(conn); + } + + if (!mpd_command_list_begin(conn, false)) { printErrorAndExit(conn); } - if (show_outputs) { - cmd_outputs(0, NULL, conn); + if (only) { + for (unsigned i = 0; i <= max; ++i) { + bool found = false; + for (unsigned *id = ids; + !found && id != ids_end; + ++id) { + found = *id == i; + } + (found ? matched : not_matched)(conn, i); + } + } else { + for (unsigned *id = ids; id != ids_end; ++id) { + matched(conn, *id); + } + } + + if (!mpd_command_list_end(conn) || !mpd_response_finish(conn)) { + printErrorAndExit(conn); } + + cmd_outputs(0, NULL, conn); + +done: + free(ids); return 0; } int cmd_enable(int argc, char **argv, struct mpd_connection *conn) { - return enable_disable(argc, argv, conn, mpd_send_enable_output); + return enable_disable(argc, argv, conn, mpd_send_enable_output, + mpd_send_disable_output); } int cmd_disable(mpd_unused int argc, char **argv, struct mpd_connection *conn) { - return enable_disable(argc, argv, conn, mpd_send_disable_output); + return enable_disable(argc, argv, conn, mpd_send_disable_output, + mpd_send_enable_output); } int cmd_play ( int argc, char ** argv, struct mpd_connection *conn ) diff --git a/src/main.c b/src/main.c index 36868e9..f8d6d73 100644 --- a/src/main.c +++ b/src/main.c @@ -73,8 +73,8 @@ static struct command { {"seek", 1, 1, 0, cmd_seek, "[+-][HH:MM:SS]|<0-100>%", "Seeks to the specified position"}, {"clear", 0, 0, 0, cmd_clear, "", "Clear the current playlist"}, {"outputs", 0, 0, 0, cmd_outputs, "", "Show the current outputs"}, - {"enable", 1, -1, 0, cmd_enable, "<output #> [...]", "Enable output(s)"}, - {"disable", 1, -1, 0, cmd_disable, "<output #> [...]", "Disable output(s)"}, + {"enable", 1, -1, 0, cmd_enable, "[only] <output #> [...]", "Enable output(s)"}, + {"disable", 1, -1, 0, cmd_disable, "[only] <output #> [...]", "Disable output(s)"}, {"shuffle", 0, 0, 0, cmd_shuffle, "", "Shuffle the current playlist"}, {"move", 2, 2, 0, cmd_move, "<from> <to>", "Move song in playlist"}, {"playlist", 0, 0, 0, cmd_playlist, "", "Print the current playlist"}, -- 1.7.7.3 ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Musicpd-dev-team mailing list Musicpd-dev-team@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/musicpd-dev-team