[pulseaudio-discuss] [PATCH 5/5] pactl: Implement list message-handlers

Georg Chini georg at chini.tk
Sun Oct 29 19:51:22 UTC 2017


For better readability, "pactl list message-handlers" is introduced which
prints a formatted output of "pactl send-message /core list-handlers".

The patch also adds the function pa_split_message_response() for easy
parsing of the message response string.
---
 man/pactl.1.xml.in               |  2 +-
 shell-completion/bash/pulseaudio |  2 +-
 shell-completion/zsh/_pulseaudio |  1 +
 src/pulsecore/core-util.c        | 34 +++++++++++++++++++++++++++++++++
 src/pulsecore/core-util.h        |  1 +
 src/utils/pactl.c                | 41 ++++++++++++++++++++++++++++++++++++++--
 6 files changed, 77 insertions(+), 4 deletions(-)

diff --git a/man/pactl.1.xml.in b/man/pactl.1.xml.in
index 9669aca9..e444f973 100644
--- a/man/pactl.1.xml.in
+++ b/man/pactl.1.xml.in
@@ -76,7 +76,7 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
     <option>
       <p><opt>list</opt> [<arg>short</arg>] [<arg>TYPE</arg>]</p>
       <optdesc><p>Dump all currently loaded modules, available sinks, sources, streams, etc.  <arg>TYPE</arg> must be one of:
-      modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards.  If not specified, all info is listed.  If
+      modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards, message-handlers.  If not specified, all info is listed.  If
       short is given, output is in a tabular format, for easy parsing by scripts.</p></optdesc>
     </option>
 
diff --git a/shell-completion/bash/pulseaudio b/shell-completion/bash/pulseaudio
index 797ec067..24d2aa1c 100644
--- a/shell-completion/bash/pulseaudio
+++ b/shell-completion/bash/pulseaudio
@@ -113,7 +113,7 @@ _pactl() {
     local comps
     local flags='-h --help --version -s --server= --client-name='
     local list_types='short sinks sources sink-inputs source-outputs cards
-                    modules samples clients'
+                    modules samples clients message-handlers'
     local commands=(stat info list exit upload-sample play-sample remove-sample
                     load-module unload-module move-sink-input move-source-output
                     suspend-sink suspend-source set-card-profile set-sink-port
diff --git a/shell-completion/zsh/_pulseaudio b/shell-completion/zsh/_pulseaudio
index a2817ebb..c24d0387 100644
--- a/shell-completion/zsh/_pulseaudio
+++ b/shell-completion/zsh/_pulseaudio
@@ -285,6 +285,7 @@ _pactl_completion() {
                 'clients: list connected clients'
                 'samples: list samples'
                 'cards: list available cards'
+                'message-handlers: list available message-handlers'
             )
 
             if ((CURRENT == 2)); then
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index d4cfa20c..e7587f38 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -1140,6 +1140,40 @@ const char *pa_split_spaces_in_place(const char *c, int *n, const char **state)
     return current;
 }
 
+/* Split the specified string into elements. An element is defined as
+ * a sub-string between curly braces. The function is needed to parse
+ * the response of messages. Each time it is called returns a newly
+ * allocated string with pa_xmalloc(). The variable state points to,
+ * should be initialized to NULL before the first call. */
+char *pa_split_message_response(const char *c, const char **state) {
+    const char *current = *state ? *state : c;
+    const char *start_pos;
+    uint32_t open_braces = 1;
+
+    if (!*current || *c == 0)
+        return NULL;
+
+    current = strchr(current, '{');
+    if (!current)
+        return NULL;
+
+    start_pos = current + 1;
+
+    while (open_braces != 0 && *current != 0) {
+        current++;
+        if (*current == '{')
+            open_braces++;
+        if (*current == '}')
+            open_braces--;
+    }
+
+    pa_assert(open_braces == 0);
+
+    *state = current + 1;
+
+    return pa_xstrndup(start_pos, current - start_pos);
+}
+
 PA_STATIC_TLS_DECLARE(signame, pa_xfree);
 
 /* Return the name of an UNIX signal. Similar to Solaris sig2str() */
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index e28b6aa7..947dfc13 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -113,6 +113,7 @@ char *pa_split(const char *c, const char *delimiters, const char **state);
 const char *pa_split_in_place(const char *c, const char *delimiters, int *n, const char **state);
 char *pa_split_spaces(const char *c, const char **state);
 const char *pa_split_spaces_in_place(const char *c, int *n, const char **state);
+char *pa_split_message_response(const char *c, const char **state);
 
 char *pa_strip_nl(char *s);
 char *pa_strip(char *s);
diff --git a/src/utils/pactl.c b/src/utils/pactl.c
index 2e15b189..c7dc57ac 100644
--- a/src/utils/pactl.c
+++ b/src/utils/pactl.c
@@ -854,6 +854,34 @@ static void string_callback(pa_context *c, int success, const char *response, vo
     complete_action();
 }
 
+static void list_handlers_callback(pa_context *c, int success, const char *response, void *userdata) {
+    const char *state = NULL;
+    char *element;
+    char *handler_name;
+    char *description;
+
+    if (success < 0) {
+        pa_log(_("Send message failed: %s"), pa_strerror(pa_context_errno(c)));
+        quit(1);
+        return;
+    }
+
+    while ((element = pa_split_message_response(response, &state))) {
+       const char *state2 = NULL;
+       handler_name = pa_split_message_response(element, &state2);
+       description = pa_split_message_response(element, &state2);
+       if (short_list_format)
+           printf("%s\n", handler_name);
+       else
+           printf("Name: %s     Description: %s\n", handler_name, description);
+       pa_xfree(element);
+       pa_xfree(handler_name);
+       pa_xfree(description);
+    }
+
+    complete_action();
+}
+
 static void volume_relative_adjust(pa_cvolume *cv) {
     pa_assert(volume_flags & VOL_RELATIVE);
 
@@ -1265,6 +1293,8 @@ static void context_state_callback(pa_context *c, void *userdata) {
                             o = pa_context_get_sample_info_list(c, get_sample_info_callback, NULL);
                         else if (pa_streq(list_type, "cards"))
                             o = pa_context_get_card_info_list(c, get_card_info_callback, NULL);
+                        else if (pa_streq(list_type, "message-handlers"))
+                            o = pa_context_send_message_to_object(c, "/core", "list-handlers", NULL, list_handlers_callback, NULL);
                         else
                             pa_assert_not_reached();
                     } else {
@@ -1315,6 +1345,12 @@ static void context_state_callback(pa_context *c, void *userdata) {
                             actions++;
                         }
 
+                        o = pa_context_send_message_to_object(c, "/core", "list-handlers", NULL, list_handlers_callback, NULL);
+                        if (o) {
+                            pa_operation_unref(o);
+                            actions++;
+                        }
+
                         o = NULL;
                     }
                     break;
@@ -1701,12 +1737,13 @@ int main(int argc, char *argv[]) {
                 if (pa_streq(argv[i], "modules") || pa_streq(argv[i], "clients") ||
                     pa_streq(argv[i], "sinks")   || pa_streq(argv[i], "sink-inputs") ||
                     pa_streq(argv[i], "sources") || pa_streq(argv[i], "source-outputs") ||
-                    pa_streq(argv[i], "samples") || pa_streq(argv[i], "cards")) {
+                    pa_streq(argv[i], "samples") || pa_streq(argv[i], "cards") ||
+                    pa_streq(argv[i], "message-handlers")) {
                     list_type = pa_xstrdup(argv[i]);
                 } else if (pa_streq(argv[i], "short")) {
                     short_list_format = true;
                 } else {
-                    pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards");
+                    pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards, message-handlers");
                     goto quit;
                 }
             }
-- 
2.14.1



More information about the pulseaudio-discuss mailing list