[pulseaudio-discuss] [PATCH 3/3] core: add message handler

Georg Chini georg at chini.tk
Fri Aug 4 20:30:02 UTC 2017


This patch adds a small message handler to the core which enables
clients to list available handlers via the list-handlers message.
Command: pacmd send-message /core list-handlers
pactl can be used with the same parameters.
With list-all-handlers private handlers will also be listed.
---
 src/pulsecore/core-messages.c | 23 +++++++++++++++++++++++
 src/pulsecore/core-messages.h |  3 +++
 src/pulsecore/core.c          | 26 ++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)

diff --git a/src/pulsecore/core-messages.c b/src/pulsecore/core-messages.c
index cc224363..6aae29a8 100644
--- a/src/pulsecore/core-messages.c
+++ b/src/pulsecore/core-messages.c
@@ -31,6 +31,7 @@
 #include <pulsecore/core-util.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/strbuf.h>
 
 #include "core-messages.h"
 
@@ -419,6 +420,28 @@ int pa_core_message_handler_group_unsubscribe(pa_core *c, const char *recipient_
     return 0;
 }
 
+/* Returns a list of handlers. */
+char *pa_core_message_handler_list(pa_core *c, bool list_private) {
+    pa_strbuf *buf;
+    void *state = NULL;
+    struct pa_core_message_handler *handler;
+
+    buf = pa_strbuf_new();
+
+    pa_strbuf_puts(buf, "Name - Description");
+
+    while ((handler = pa_hashmap_iterate(c->message_handlers, &state, NULL))) {
+        if (list_private || !handler->is_private) {
+            pa_strbuf_puts(buf, "\n");
+            pa_strbuf_printf(buf, "%s", handler->recipient);
+            if (handler->description)
+                pa_strbuf_printf(buf, " - %s", handler->description);
+        }
+    }
+
+    return pa_strbuf_to_string_free(buf);
+}
+
 /* Send a signal */
 void pa_signal_post(pa_core *c, const char *sender, uint64_t facility, const char *signal, const char *signal_parameters) {
     pa_signal_descriptor *sd;
diff --git a/src/pulsecore/core-messages.h b/src/pulsecore/core-messages.h
index 89fa57fd..a3f06979 100644
--- a/src/pulsecore/core-messages.h
+++ b/src/pulsecore/core-messages.h
@@ -65,6 +65,9 @@ void pa_core_message_handler_unregister(pa_core *c, const char *recipient_name);
 /* Check availability and scope of handler */
 int pa_core_message_handler_test(pa_core *c, const char *recipient_name);
 
+/* List handlers */
+char *pa_core_message_handler_list(pa_core *c, bool list_private);
+
 /* Send message to recipient */
 int pa_core_send_message(pa_core *c, const char *recipient_name, const char *message, const char *message_parameters, void *message_data, char **response);
 
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
index 3145caaf..98aceb57 100644
--- a/src/pulsecore/core.c
+++ b/src/pulsecore/core.c
@@ -33,11 +33,13 @@
 #include <pulsecore/module.h>
 #include <pulsecore/core-rtclock.h>
 #include <pulsecore/core-util.h>
+#include <pulsecore/core-messages.h>
 #include <pulsecore/core-scache.h>
 #include <pulsecore/core-subscribe.h>
 #include <pulsecore/random.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/strbuf.h>
 
 #include "core.h"
 
@@ -61,6 +63,26 @@ static int core_process_msg(pa_msgobject *o, int code, void *userdata, int64_t o
 
 static void core_free(pa_object *o);
 
+static int core_message_handler(const char *recipient, const char *message, const char *message_parameters, void *message_data, char **response, void *userdata) {
+    pa_core *c;
+
+    pa_assert(c = (pa_core *) userdata);
+    pa_assert(message);
+    pa_assert(response);
+    pa_assert(pa_safe_streq(recipient, "/core"));
+
+    if (pa_streq(message, "list-handlers"))
+        *response = pa_core_message_handler_list(c, false);
+
+    else if (pa_streq(message, "list-all-handlers"))
+        *response = pa_core_message_handler_list(c, true);
+
+    else
+        *response = pa_xstrdup("Message not implemented");
+
+    return 0;
+}
+
 pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size) {
     pa_core* c;
     pa_mempool *pool;
@@ -105,6 +127,8 @@ pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t
     c->shared = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     c->message_handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
+    pa_core_message_handler_register(c, "/core", "Core message handler", false, core_message_handler, (void *) c);
+
     c->default_source = NULL;
     c->default_sink = NULL;
 
@@ -205,6 +229,8 @@ static void core_free(pa_object *o) {
     pa_assert(pa_hashmap_isempty(c->shared));
     pa_hashmap_free(c->shared);
 
+    pa_core_message_handler_unregister(c, "/core");
+
     pa_assert(pa_hashmap_isempty(c->message_handlers));
     pa_hashmap_free(c->message_handlers);
 
-- 
2.11.0



More information about the pulseaudio-discuss mailing list