From 9e715089a4279d08b7cc8d7fbdb3eb8696230e65 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 4 Aug 2013 23:20:56 +0200 Subject: Commands: new command "toggleoutput" --- NEWS | 2 ++ doc/protocol.xml | 14 ++++++++++++++ src/AllCommands.cxx | 1 + src/OutputCommand.cxx | 27 +++++++++++++++++++++++++++ src/OutputCommand.hxx | 7 +++++++ src/OutputCommands.cxx | 16 ++++++++++++++++ src/OutputCommands.hxx | 3 +++ 7 files changed, 70 insertions(+) diff --git a/NEWS b/NEWS index 46ee9dbb1..fb4145c30 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,6 @@ ver 0.18 (2012/??/??) +* protocol: + - new command "toggleoutput" * innput: - soup: plugin removed * decoder: diff --git a/doc/protocol.xml b/doc/protocol.xml index c03388a24..50e58db63 100644 --- a/doc/protocol.xml +++ b/doc/protocol.xml @@ -1876,6 +1876,20 @@ OK + + + + toggleoutput + ID + + + + + Turns an output on or off, depending on the current + state. + + + diff --git a/src/AllCommands.cxx b/src/AllCommands.cxx index 8f651ec04..cd06c323b 100644 --- a/src/AllCommands.cxx +++ b/src/AllCommands.cxx @@ -159,6 +159,7 @@ static const struct command commands[] = { { "swap", PERMISSION_CONTROL, 2, 2, handle_swap }, { "swapid", PERMISSION_CONTROL, 2, 2, handle_swapid }, { "tagtypes", PERMISSION_READ, 0, 0, handle_tagtypes }, + { "toggleoutput", PERMISSION_ADMIN, 1, 1, handle_toggleoutput }, { "unsubscribe", PERMISSION_READ, 1, 1, handle_unsubscribe }, { "update", PERMISSION_CONTROL, 0, 1, handle_update }, { "urlhandlers", PERMISSION_READ, 0, 0, handle_urlhandlers }, diff --git a/src/OutputCommand.cxx b/src/OutputCommand.cxx index bf051babf..f6b35c6ed 100644 --- a/src/OutputCommand.cxx +++ b/src/OutputCommand.cxx @@ -84,3 +84,30 @@ audio_output_disable_index(unsigned idx) return true; } + +bool +audio_output_toggle_index(unsigned idx) +{ + struct audio_output *ao; + + if (idx >= audio_output_count()) + return false; + + ao = audio_output_get(idx); + const bool enabled = ao->enabled = !ao->enabled; + idle_add(IDLE_OUTPUT); + + if (!enabled) { + Mixer *mixer = ao->mixer; + if (mixer != nullptr) { + mixer_close(mixer); + idle_add(IDLE_MIXER); + } + } + + ao->player_control->UpdateAudio(); + + ++audio_output_state_version; + + return true; +} diff --git a/src/OutputCommand.hxx b/src/OutputCommand.hxx index 74eaf8f1c..46fab92c5 100644 --- a/src/OutputCommand.hxx +++ b/src/OutputCommand.hxx @@ -41,4 +41,11 @@ audio_output_enable_index(unsigned idx); bool audio_output_disable_index(unsigned idx); +/** + * Toggles an audio output. Returns false if the specified output + * does not exist. + */ +bool +audio_output_toggle_index(unsigned idx); + #endif diff --git a/src/OutputCommands.cxx b/src/OutputCommands.cxx index 7d626477a..5e4a145da 100644 --- a/src/OutputCommands.cxx +++ b/src/OutputCommands.cxx @@ -64,6 +64,22 @@ handle_disableoutput(Client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_OK; } +enum command_return +handle_toggleoutput(Client *client, gcc_unused int argc, char *argv[]) +{ + unsigned device; + if (!check_unsigned(client, &device, argv[1])) + return COMMAND_RETURN_ERROR; + + if (!audio_output_toggle_index(device)) { + command_error(client, ACK_ERROR_NO_EXIST, + "No such audio output"); + return COMMAND_RETURN_ERROR; + } + + return COMMAND_RETURN_OK; +} + enum command_return handle_devices(Client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) diff --git a/src/OutputCommands.hxx b/src/OutputCommands.hxx index 4f7082bfb..5642a0680 100644 --- a/src/OutputCommands.hxx +++ b/src/OutputCommands.hxx @@ -30,6 +30,9 @@ handle_enableoutput(Client *client, int argc, char *argv[]); enum command_return handle_disableoutput(Client *client, int argc, char *argv[]); +enum command_return +handle_toggleoutput(Client *client, int argc, char *argv[]); + enum command_return handle_devices(Client *client, int argc, char *argv[]); -- cgit v1.2.3