[pulseaudio-discuss] [PATCH 3/4] pactl, pacmd, cli-command: Add send-message command

Georg Chini georg at chini.tk
Fri Jul 21 19:25:50 UTC 2017


---
 man/pactl.1.xml.in               |  6 ++++++
 man/pulse-cli-syntax.5.xml.in    |  6 ++++++
 shell-completion/bash/pulseaudio |  5 +++--
 shell-completion/zsh/_pulseaudio |  2 ++
 src/pulsecore/cli-command.c      | 44 ++++++++++++++++++++++++++++++++++++++++
 src/utils/pacmd.c                |  1 +
 src/utils/pactl.c                | 39 ++++++++++++++++++++++++++++++++++-
 7 files changed, 100 insertions(+), 3 deletions(-)

diff --git a/man/pactl.1.xml.in b/man/pactl.1.xml.in
index 39569b6b..e868babc 100644
--- a/man/pactl.1.xml.in
+++ b/man/pactl.1.xml.in
@@ -246,6 +246,12 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
       </p></optdesc> </option>
 
     <option>
+      <p><opt>send-message</opt> <arg>RECIPIENT</arg> <arg>MESSAGE</arg> <arg>MESSAGE_PARAMETERS</arg></p>
+      <optdesc><p>Send a message string to the specified recipient object. If applicable an additional string containing
+      message parameters can be specified. A string is returned as a response to the message.</p></optdesc>
+    </option>
+
+    <option>
       <p><opt>subscribe</opt></p>
       <optdesc><p>Subscribe to events, pactl does not exit by itself, but keeps waiting for new events.</p></optdesc>
     </option>
diff --git a/man/pulse-cli-syntax.5.xml.in b/man/pulse-cli-syntax.5.xml.in
index 0a0fabaf..adf33f58 100644
--- a/man/pulse-cli-syntax.5.xml.in
+++ b/man/pulse-cli-syntax.5.xml.in
@@ -298,6 +298,12 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
     </option>
 
     <option>
+      <p><opt>send-message</opt> <arg>recipient</arg> <arg>message</arg> <arg>message_parameters</arg></p>
+      <optdesc><p>Send a message string to the specified recipient object. If applicable an additional string containing
+      message parameters can be specified. A string is returned as a response to the message.</p></optdesc>
+    </option>
+
+    <option>
       <p><opt>exit</opt></p>
       <optdesc><p>Terminate the daemon. If you want to terminate a CLI
       connection ("log out") you might want to use ctrl+d</p></optdesc>
diff --git a/shell-completion/bash/pulseaudio b/shell-completion/bash/pulseaudio
index e473b9c2..ac02afac 100644
--- a/shell-completion/bash/pulseaudio
+++ b/shell-completion/bash/pulseaudio
@@ -120,7 +120,8 @@ _pactl() {
                     set-source-port set-sink-volume set-source-volume
                     set-sink-input-volume set-source-output-volume set-sink-mute
                     set-source-mute set-sink-input-mute set-source-output-mute
-                    set-sink-formats set-port-latency-offset subscribe help)
+                    set-sink-formats set-port-latency-offset send-message subscribe
+                    help)
 
     _init_completion -n = || return
     preprev=${words[$cword-2]}
@@ -270,7 +271,7 @@ _pacmd() {
                     move-sink-input move-source-output suspend-sink suspend-source
                     suspend set-card-profile set-sink-port set-source-port
                     set-port-latency-offset set-log-target set-log-level set-log-meta
-                    set-log-time set-log-backtrace)
+                    set-log-time set-log-backtrace send-message)
     _init_completion -n = || return
     preprev=${words[$cword-2]}
 
diff --git a/shell-completion/zsh/_pulseaudio b/shell-completion/zsh/_pulseaudio
index 0e9e89bd..a2817ebb 100644
--- a/shell-completion/zsh/_pulseaudio
+++ b/shell-completion/zsh/_pulseaudio
@@ -263,6 +263,7 @@ _pactl_completion() {
             'set-sink-input-mute: mute a stream'
             'set-source-output-mute: mute a recording stream'
             'set-sink-formats: set supported formats of a sink'
+            'send-message: send a message to a pulseaudio object'
             'subscribe: subscribe to events'
         )
 
@@ -561,6 +562,7 @@ _pacmd_completion() {
             'dump: show daemon configuration'
             'dump-volumes: show the state of all volumes'
             'shared: show shared properties'
+            'send-message: send a message to a pulseaudio object'
             'exit: ask the PulseAudio daemon to exit'
         )
         _describe 'pacmd commands' _pacmd_commands
diff --git a/src/pulsecore/cli-command.c b/src/pulsecore/cli-command.c
index 01fea475..598cef44 100644
--- a/src/pulsecore/cli-command.c
+++ b/src/pulsecore/cli-command.c
@@ -135,6 +135,7 @@ static int pa_cli_command_sink_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf,
 static int pa_cli_command_source_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
 static int pa_cli_command_port_offset(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
 static int pa_cli_command_dump_volumes(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
+static int pa_cli_command_send_message_to_object(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
 
 /* A method table for all available commands */
 
@@ -191,6 +192,7 @@ static const struct command commands[] = {
     { "set-log-meta",            pa_cli_command_log_meta,           "Show source code location in log messages (args: bool)", 2},
     { "set-log-time",            pa_cli_command_log_time,           "Show timestamps in log messages (args: bool)", 2},
     { "set-log-backtrace",       pa_cli_command_log_backtrace,      "Show backtrace in log messages (args: frames)", 2},
+    { "send-message",            pa_cli_command_send_message_to_object, "Send a message to an object (args: recipient, message, message_parameters)", 4},
     { "play-file",               pa_cli_command_play_file,          "Play a sound file (args: filename, sink|index)", 3},
     { "dump",                    pa_cli_command_dump,               "Dump daemon configuration", 1},
     { "dump-volumes",            pa_cli_command_dump_volumes,       "Debug: Show the state of all volumes", 1 },
@@ -1773,6 +1775,48 @@ static int pa_cli_command_port_offset(pa_core *c, pa_tokenizer *t, pa_strbuf *bu
     return 0;
 }
 
+static int pa_cli_command_send_message_to_object(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) {
+     const char *recipient_name, *message, *message_parameters;
+     char *response = NULL;
+     pa_core_message_handler *handler;
+
+     pa_core_assert_ref(c);
+     pa_assert(t);
+     pa_assert(buf);
+     pa_assert(fail);
+
+
+     if (!(recipient_name = pa_tokenizer_get(t, 1))) {
+         pa_strbuf_puts(buf, "You need to specify a recipient for the message.\n");
+            return -1;
+     }
+
+     if (!(message = pa_tokenizer_get(t, 2))) {
+         pa_strbuf_puts(buf, "You need to specify a message string.\n");
+         return -1;
+     }
+
+     /* parameters may be NULL */
+     message_parameters = pa_tokenizer_get(t, 3);
+
+    if (pa_core_find_message_handler(c, recipient_name, &handler) < 0) {
+        pa_strbuf_puts(buf, "Invalid recipient name.\n");
+        return -1;
+    }
+
+    if (handler->callback(handler->recipient, message, message_parameters, &response, handler->userdata) < 0) {
+        pa_strbuf_puts(buf, "Send message failed.\n");
+        pa_xfree(response);
+        return -1;
+    }
+
+    pa_strbuf_puts(buf, (const char *)response);
+    pa_strbuf_puts(buf, "\n");
+
+    pa_xfree(response);
+    return 0;
+}
+
 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) {
     pa_module *m;
     pa_sink *sink;
diff --git a/src/utils/pacmd.c b/src/utils/pacmd.c
index 616573cc..6eae6c42 100644
--- a/src/utils/pacmd.c
+++ b/src/utils/pacmd.c
@@ -77,6 +77,7 @@ static void help(const char *argv0) {
     printf("%s %s %s\n", argv0, "set-log-meta", _("1|0"));
     printf("%s %s %s\n", argv0, "set-log-time", _("1|0"));
     printf("%s %s %s\n", argv0, "set-log-backtrace", _("FRAMES"));
+    printf("%s %s %s\n", argv0, "send-message", _("RECIPIENT MESSAGE [MESSAGE_PARAMETERS]"));
 
     printf(_("\n"
          "  -h, --help                            Show this help\n"
diff --git a/src/utils/pactl.c b/src/utils/pactl.c
index e9bf005b..5fde2bc6 100644
--- a/src/utils/pactl.c
+++ b/src/utils/pactl.c
@@ -56,7 +56,10 @@ static char
     *card_name = NULL,
     *profile_name = NULL,
     *port_name = NULL,
-    *formats = NULL;
+    *formats = NULL,
+    *recipient = NULL,
+    *message = NULL,
+    *message_args = NULL;
 
 static uint32_t
     sink_input_idx = PA_INVALID_INDEX,
@@ -130,6 +133,7 @@ static enum {
     SET_SOURCE_OUTPUT_MUTE,
     SET_SINK_FORMATS,
     SET_PORT_LATENCY_OFFSET,
+    SEND_OBJECT_MESSAGE,
     SUBSCRIBE
 } action = NONE;
 
@@ -834,6 +838,18 @@ static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
     complete_action();
 }
 
+static void string_callback(pa_context *c, const char *response, void *userdata) {
+    if (!response) {
+        pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
+        quit(1);
+        return;
+    }
+
+    printf("%s\n", response);
+
+    complete_action();
+}
+
 static void volume_relative_adjust(pa_cvolume *cv) {
     pa_assert(volume_flags & VOL_RELATIVE);
 
@@ -1404,6 +1420,10 @@ static void context_state_callback(pa_context *c, void *userdata) {
                     o = pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL);
                     break;
 
+                case SEND_OBJECT_MESSAGE:
+                    o = pa_context_send_message_to_object(c, recipient, message, message_args, string_callback, NULL);
+                    break;
+
                 case SUBSCRIBE:
                     pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
 
@@ -1580,6 +1600,7 @@ static void help(const char *argv0) {
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
+    printf("%s %s %s %s\n", argv0, _("[options]"), "send-message", _("RECIPIENT MESSAGE MESSAGE_PARAMETERS"));
     printf("%s %s %s\n",    argv0, _("[options]"), "subscribe");
     printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
              "can be used to specify the default sink, source and monitor.\n"));
@@ -2015,6 +2036,19 @@ int main(int argc, char *argv[]) {
                 goto quit;
             }
 
+        } else if (pa_streq(argv[optind], "send-message")) {
+            action = SEND_OBJECT_MESSAGE;
+
+            if (argc < optind+3) {
+                pa_log(_("You have to specify at least a recipient and a message"));
+                goto quit;
+            }
+
+            recipient = pa_xstrdup(argv[optind + 1]);
+            message = pa_xstrdup(argv[optind + 2]);
+            if (argc == optind+4)
+                message_args = pa_xstrdup(argv[optind + 3]);
+
         } else if (pa_streq(argv[optind], "subscribe"))
 
             action = SUBSCRIBE;
@@ -2108,6 +2142,9 @@ quit:
     pa_xfree(profile_name);
     pa_xfree(port_name);
     pa_xfree(formats);
+    pa_xfree(recipient);
+    pa_xfree(message);
+    pa_xfree(message_args);
 
     if (sndfile)
         sf_close(sndfile);
-- 
2.11.0



More information about the pulseaudio-discuss mailing list