mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-23 06:59:53 -05:00
message-params: use JSON instead of custom format
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/525>
This commit is contained in:
parent
0ba768b2e9
commit
1dd05f4a9b
6 changed files with 93 additions and 69 deletions
|
|
@ -66,29 +66,27 @@ static void core_free(pa_object *o);
|
|||
|
||||
/* Returns a list of handlers. */
|
||||
static char *message_handler_list(pa_core *c) {
|
||||
pa_message_params *param;
|
||||
pa_json_encoder *encoder;
|
||||
void *state = NULL;
|
||||
struct pa_message_handler *handler;
|
||||
|
||||
param = pa_message_params_new();
|
||||
encoder = pa_json_encoder_new();
|
||||
|
||||
pa_message_params_begin_list(param);
|
||||
pa_json_encoder_begin_element_array(encoder);
|
||||
PA_HASHMAP_FOREACH(handler, c->message_handlers, state) {
|
||||
pa_message_params_begin_list(param);
|
||||
pa_json_encoder_begin_element_object(encoder);
|
||||
|
||||
/* object_path cannot contain characters that need escaping, therefore
|
||||
* pa_message_params_write_raw() can safely be used here. */
|
||||
pa_message_params_write_raw(param, handler->object_path, true);
|
||||
pa_message_params_write_string(param, handler->description);
|
||||
pa_json_encoder_add_member_string(encoder, "name", handler->object_path);
|
||||
pa_json_encoder_add_member_string(encoder, "description", handler->description);
|
||||
|
||||
pa_message_params_end_list(param);
|
||||
pa_json_encoder_end_object(encoder);
|
||||
}
|
||||
pa_message_params_end_list(param);
|
||||
pa_json_encoder_end_array(encoder);
|
||||
|
||||
return pa_message_params_to_string_free(param);
|
||||
return pa_json_encoder_to_string_free(encoder);
|
||||
}
|
||||
|
||||
static int core_message_handler(const char *object_path, const char *message, char *message_parameters, char **response, void *userdata) {
|
||||
static int core_message_handler(const char *object_path, const char *message, const pa_json_object *parameters, char **response, void *userdata) {
|
||||
pa_core *c;
|
||||
|
||||
pa_assert(c = (pa_core *) userdata);
|
||||
|
|
|
|||
|
|
@ -105,7 +105,8 @@ void pa_message_handler_unregister(pa_core *c, const char *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, *path_copy;
|
||||
char *path_copy;
|
||||
pa_json_object *parameters = NULL;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(object_path);
|
||||
|
|
@ -125,14 +126,21 @@ int pa_message_handler_send_message(pa_core *c, const char *object_path, const c
|
|||
return -PA_ERR_NOENTITY;
|
||||
}
|
||||
|
||||
parameter_copy = pa_xstrdup(message_parameters);
|
||||
pa_xfree(path_copy);
|
||||
|
||||
if (message_parameters) {
|
||||
parameters = pa_json_parse(message_parameters);
|
||||
|
||||
if (!parameters)
|
||||
return -PA_ERR_INVALID;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
ret = handler->callback(handler->object_path, message, parameters, response, handler->userdata);
|
||||
|
||||
pa_xfree(parameter_copy);
|
||||
pa_xfree(path_copy);
|
||||
if (parameters)
|
||||
pa_json_object_free(parameters);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
***/
|
||||
|
||||
#include <pulsecore/core.h>
|
||||
#include <pulse/json.h>
|
||||
|
||||
/* Message handler types and functions */
|
||||
|
||||
|
|
@ -26,7 +27,7 @@
|
|||
typedef int (*pa_message_handler_cb_t)(
|
||||
const char *object_path,
|
||||
const char *message,
|
||||
char *message_parameters,
|
||||
const pa_json_object *parameters,
|
||||
char **response,
|
||||
void *userdata);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue