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 |   71 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 src/main.c    |    4 +-
 2 files changed, 64 insertions(+), 11 deletions(-)

diff --git a/src/command.c b/src/command.c
index 3239517..57bd613 100644
--- a/src/command.c
+++ b/src/command.c
@@ -368,37 +368,90 @@ 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))
 {
-       int arg, ret = 0;
+       unsigned *ids, *ids_end;
+       bool only = false;
+       int arg;
+
+       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) {
+               if (!parse_int(*argv, &arg) || arg <= 0) {
                        fprintf(stderr, "%s: not a positive integer\n", *argv);
-                       ++ret;
                } else {
                        /* We decrement by 1 to make it natural to the user. */
-                       callback(conn, arg - 1);
+                       *ids_end++ = arg - 1;
+               }
+       }
+
+       if (ids == ids_end) {
+               /* nop */
+       } else if (only) {
+               unsigned max = count_outputs(conn);
+               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 (ret != argc) {
+       free(ids);
+
+       if (ids != ids_end) {
                cmd_outputs(0, NULL, conn);
        }
-       return ret;
+       return 0;
 }
 
 int
 cmd_enable(int argc, char **argv, struct mpd_connection *conn)
 {
-       return enable_disable(argc, argv, conn, mpd_run_enable_output);
+       return enable_disable(argc, argv, conn, mpd_run_enable_output,
+                             mpd_run_disable_output);
 }
 
 int
 cmd_disable(mpd_unused int argc, char **argv, struct mpd_connection *conn)
 {
-       return enable_disable(argc, argv, conn, mpd_run_disable_output);
+       return enable_disable(argc, argv, conn, mpd_run_disable_output,
+                             mpd_run_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

Reply via email to