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

Georg Chini georg at chini.tk
Sat Aug 19 15:48:04 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.
---
 src/pulsecore/core-messages.c | 21 +++++++++++++++++++++
 src/pulsecore/core-messages.h |  3 +++
 src/pulsecore/core.c          | 23 +++++++++++++++++++++++
 3 files changed, 47 insertions(+)

diff --git a/src/pulsecore/core-messages.c b/src/pulsecore/core-messages.c
index 69055786..eca1e6cf 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"
 
@@ -116,3 +117,23 @@ int pa_core_message_handler_set_description(pa_core *c, const char *recipient_na
 
     return 0;
 }
+
+/* Returns a list of handlers. */
+char *pa_core_message_handler_list(pa_core *c) {
+    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))) {
+        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);
+}
diff --git a/src/pulsecore/core-messages.h b/src/pulsecore/core-messages.h
index d34e304e..8e78e35e 100644
--- a/src/pulsecore/core-messages.h
+++ b/src/pulsecore/core-messages.h
@@ -43,6 +43,9 @@ typedef int (*pa_core_message_handler_cb_t)(
 void pa_core_message_handler_register(pa_core *c, const char *recipient_name, const char *description, pa_core_message_handler_cb_t cb, void *userdata);
 void pa_core_message_handler_unregister(pa_core *c, const char *recipient_name);
 
+/* List handlers */
+char *pa_core_message_handler_list(pa_core *c);
+
 /* Send message to recipient */
 int pa_core_send_message(pa_core *c, const char *recipient_name, const char *message, const char *message_parameters, char **response);
 
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
index 3145caaf..ab0c6727 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,23 @@ 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, 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);
+
+    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 +124,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", core_message_handler, (void *) c);
+
     c->default_source = NULL;
     c->default_sink = NULL;
 
@@ -205,6 +226,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