mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-10-29 05:40:23 -04:00
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 functions pa_message_params_read_raw() and pa_message_params_read_string() for easy parsing of the message response string. Because the functions need to modify the parameter string, the message handler and the pa_context_string_callback function now receive a char* instead of a const char* as parameter argument. Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/51>
135 lines
4 KiB
C
135 lines
4 KiB
C
/***
|
|
This file is part of PulseAudio.
|
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
|
or (at your option) any later version.
|
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
|
***/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
#include <pulsecore/core.h>
|
|
#include <pulsecore/core-util.h>
|
|
#include <pulsecore/log.h>
|
|
#include <pulsecore/macro.h>
|
|
|
|
#include "message-handler.h"
|
|
|
|
/* Check if a string does not contain control characters. Currently these are
|
|
* only "{" and "}". */
|
|
static bool string_is_valid(const char *test_string) {
|
|
uint32_t i;
|
|
|
|
for (i = 0; test_string[i]; i++) {
|
|
if (test_string[i] == '{' ||
|
|
test_string[i] == '}')
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/* Message handler functions */
|
|
|
|
/* Register message handler for the specified object. object_path must be a unique name starting with "/". */
|
|
void pa_message_handler_register(pa_core *c, const char *object_path, const char *description, pa_message_handler_cb_t cb, void *userdata) {
|
|
struct pa_message_handler *handler;
|
|
|
|
pa_assert(c);
|
|
pa_assert(object_path);
|
|
pa_assert(cb);
|
|
pa_assert(userdata);
|
|
|
|
/* Ensure that the object path is not empty and starts with "/". */
|
|
pa_assert(object_path[0] == '/');
|
|
|
|
/* Ensure that object path and description are valid strings */
|
|
pa_assert(string_is_valid(object_path));
|
|
if (description)
|
|
pa_assert(string_is_valid(description));
|
|
|
|
handler = pa_xnew0(struct pa_message_handler, 1);
|
|
handler->userdata = userdata;
|
|
handler->callback = cb;
|
|
handler->object_path = pa_xstrdup(object_path);
|
|
handler->description = pa_xstrdup(description);
|
|
|
|
pa_assert_se(pa_hashmap_put(c->message_handlers, handler->object_path, handler) == 0);
|
|
}
|
|
|
|
/* Unregister a message handler */
|
|
void pa_message_handler_unregister(pa_core *c, const char *object_path) {
|
|
struct pa_message_handler *handler;
|
|
|
|
pa_assert(c);
|
|
pa_assert(object_path);
|
|
|
|
pa_assert_se(handler = pa_hashmap_remove(c->message_handlers, object_path));
|
|
|
|
pa_xfree(handler->object_path);
|
|
pa_xfree(handler->description);
|
|
pa_xfree(handler);
|
|
}
|
|
|
|
/* Send a message to an object identified by object_path */
|
|
int pa_message_handler_send_message(pa_core *c, const char *object_path, const char *message, const char *message_parameters, char **response) {
|
|
struct pa_message_handler *handler;
|
|
int ret;
|
|
char *parameter_copy;
|
|
|
|
pa_assert(c);
|
|
pa_assert(object_path);
|
|
pa_assert(message);
|
|
pa_assert(response);
|
|
|
|
*response = NULL;
|
|
|
|
if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
|
|
return -PA_ERR_NOENTITY;
|
|
|
|
parameter_copy = pa_xstrdup(message_parameters);
|
|
|
|
/* The handler is expected to return an error code and may also
|
|
return an error string in response */
|
|
ret = handler->callback(handler->object_path, message, parameter_copy, response, handler->userdata);
|
|
|
|
pa_xfree(parameter_copy);
|
|
return ret;
|
|
}
|
|
|
|
/* Set handler description */
|
|
int pa_message_handler_set_description(pa_core *c, const char *object_path, const char *description) {
|
|
struct pa_message_handler *handler;
|
|
|
|
pa_assert(c);
|
|
pa_assert(object_path);
|
|
|
|
if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
|
|
return -PA_ERR_NOENTITY;
|
|
|
|
if (description) {
|
|
if (!string_is_valid(description))
|
|
return -PA_ERR_INVALID;
|
|
}
|
|
|
|
pa_xfree(handler->description);
|
|
handler->description = pa_xstrdup(description);
|
|
|
|
return PA_OK;
|
|
}
|