• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   PulseAudio is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published
6   by the Free Software Foundation; either version 2.1 of the License,
7   or (at your option) any later version.
8 
9   PulseAudio is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public License
15   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16 ***/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 
25 #include <pulse/xmalloc.h>
26 
27 #include <pulsecore/core.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/macro.h>
31 
32 #include "message-handler.h"
33 
34 /* Message handler functions */
35 
36 /* Register message handler for the specified object. object_path must be a unique name starting with "/". */
pa_message_handler_register(pa_core * c,const char * object_path,const char * description,pa_message_handler_cb_t cb,void * userdata)37 void pa_message_handler_register(pa_core *c, const char *object_path, const char *description, pa_message_handler_cb_t cb, void *userdata) {
38     struct pa_message_handler *handler;
39 
40     pa_assert(c);
41     pa_assert(object_path);
42     pa_assert(cb);
43     pa_assert(userdata);
44 
45     /* Ensure that the object path is not empty and starts with "/". */
46     pa_assert(object_path[0] == '/');
47 
48     handler = pa_xnew0(struct pa_message_handler, 1);
49     handler->userdata = userdata;
50     handler->callback = cb;
51     handler->object_path = pa_xstrdup(object_path);
52     handler->description = pa_xstrdup(description);
53 
54     pa_assert_se(pa_hashmap_put(c->message_handlers, handler->object_path, handler) == 0);
55 }
56 
57 /* Unregister a message handler */
pa_message_handler_unregister(pa_core * c,const char * object_path)58 void pa_message_handler_unregister(pa_core *c, const char *object_path) {
59     struct pa_message_handler *handler;
60 
61     pa_assert(c);
62     pa_assert(object_path);
63 
64     pa_assert_se(handler = pa_hashmap_remove(c->message_handlers, object_path));
65 
66     pa_xfree(handler->object_path);
67     pa_xfree(handler->description);
68     pa_xfree(handler);
69 }
70 
71 /* Send a message to an object identified by object_path */
pa_message_handler_send_message(pa_core * c,const char * object_path,const char * message,const char * message_parameters,char ** response)72 int pa_message_handler_send_message(pa_core *c, const char *object_path, const char *message, const char *message_parameters, char **response) {
73     struct pa_message_handler *handler;
74 
75     pa_assert(c);
76     pa_assert(object_path);
77     pa_assert(message);
78     pa_assert(response);
79 
80     *response = NULL;
81 
82     if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
83         return -PA_ERR_NOENTITY;
84 
85     /* The handler is expected to return an error code and may also
86        return an error string in response */
87     return handler->callback(handler->object_path, message, message_parameters, response, handler->userdata);
88 }
89 
90 /* Set handler description */
pa_message_handler_set_description(pa_core * c,const char * object_path,const char * description)91 int pa_message_handler_set_description(pa_core *c, const char *object_path, const char *description) {
92     struct pa_message_handler *handler;
93 
94     pa_assert(c);
95     pa_assert(object_path);
96 
97     if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
98         return -PA_ERR_NOENTITY;
99 
100     pa_xfree(handler->description);
101     handler->description = pa_xstrdup(description);
102 
103     return PA_OK;
104 }
105