]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-dbus.c
dbus-protocol: Take advantage of the helpers in dbus-util.
[pulseaudio] / src / pulsecore / protocol-dbus.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Tanu Kaskinen
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dbus/dbus.h>
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/dbus-util.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/shared.h>
35 #include <pulsecore/strbuf.h>
36
37 #include "protocol-dbus.h"
38
39 struct pa_dbus_protocol {
40 PA_REFCNT_DECLARE;
41
42 pa_core *core;
43 pa_hashmap *objects; /* Object path -> struct object_entry */
44 pa_hashmap *connections; /* DBusConnection -> struct connection_entry */
45 pa_idxset *extensions; /* Strings */
46
47 pa_hook hooks[PA_DBUS_PROTOCOL_HOOK_MAX];
48 };
49
50 struct object_entry {
51 char *path;
52 pa_hashmap *interfaces; /* Interface name -> struct interface_entry */
53 char *introspection;
54 };
55
56 struct connection_entry {
57 DBusConnection *connection;
58 pa_client *client;
59
60 pa_bool_t listening_for_all_signals;
61
62 /* Contains object paths. If this is empty, then signals from all objects
63 * are accepted. Only used when listening_for_all_signals == TRUE. */
64 pa_idxset *all_signals_objects;
65
66 /* Signal name -> idxset. The idxsets contain object paths. If an idxset is
67 * empty, then that signal is accepted from all objects. Only used when
68 * listening_for_all_signals == FALSE. */
69 pa_hashmap *listening_signals;
70 };
71
72 struct interface_entry {
73 char *name;
74 pa_hashmap *method_handlers;
75 pa_hashmap *property_handlers;
76 pa_dbus_receive_cb_t get_all_properties_cb;
77 pa_dbus_signal_info *signals;
78 unsigned n_signals;
79 void *userdata;
80 };
81
82 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type) {
83 char *address = NULL;
84 char *runtime_path = NULL;
85 char *escaped_path = NULL;
86
87 switch (server_type) {
88 case PA_SERVER_TYPE_USER:
89 pa_assert_se((runtime_path = pa_runtime_path(PA_DBUS_SOCKET_NAME)));
90 pa_assert_se((escaped_path = dbus_address_escape_value(runtime_path)));
91 address = pa_sprintf_malloc("unix:path=%s", escaped_path);
92 break;
93
94 case PA_SERVER_TYPE_SYSTEM:
95 pa_assert_se((escaped_path = dbus_address_escape_value(PA_DBUS_SYSTEM_SOCKET_PATH)));
96 address = pa_sprintf_malloc("unix:path=%s", escaped_path);
97 break;
98
99 case PA_SERVER_TYPE_NONE:
100 address = pa_xnew0(char, 1);
101 break;
102
103 default:
104 pa_assert_not_reached();
105 }
106
107 pa_xfree(runtime_path);
108 pa_xfree(escaped_path);
109
110 return address;
111 }
112
113 static pa_dbus_protocol *dbus_protocol_new(pa_core *c) {
114 pa_dbus_protocol *p;
115 unsigned i;
116
117 pa_assert(c);
118
119 p = pa_xnew(pa_dbus_protocol, 1);
120 PA_REFCNT_INIT(p);
121 p->core = pa_core_ref(c);
122 p->objects = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
123 p->connections = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
124 p->extensions = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
125
126 for (i = 0; i < PA_DBUS_PROTOCOL_HOOK_MAX; ++i)
127 pa_hook_init(&p->hooks[i], p);
128
129 pa_assert_se(pa_shared_set(c, "dbus-protocol", p) >= 0);
130
131 return p;
132 }
133
134 pa_dbus_protocol* pa_dbus_protocol_get(pa_core *c) {
135 pa_dbus_protocol *p;
136
137 if ((p = pa_shared_get(c, "dbus-protocol")))
138 return pa_dbus_protocol_ref(p);
139
140 return dbus_protocol_new(c);
141 }
142
143 pa_dbus_protocol* pa_dbus_protocol_ref(pa_dbus_protocol *p) {
144 pa_assert(p);
145 pa_assert(PA_REFCNT_VALUE(p) >= 1);
146
147 PA_REFCNT_INC(p);
148
149 return p;
150 }
151
152 void pa_dbus_protocol_unref(pa_dbus_protocol *p) {
153 unsigned i;
154
155 pa_assert(p);
156 pa_assert(PA_REFCNT_VALUE(p) >= 1);
157
158 if (PA_REFCNT_DEC(p) > 0)
159 return;
160
161 pa_assert(pa_hashmap_isempty(p->objects));
162 pa_assert(pa_hashmap_isempty(p->connections));
163 pa_assert(pa_idxset_isempty(p->extensions));
164
165 pa_hashmap_free(p->objects, NULL, NULL);
166 pa_hashmap_free(p->connections, NULL, NULL);
167 pa_idxset_free(p->extensions, NULL, NULL);
168
169 for (i = 0; i < PA_DBUS_PROTOCOL_HOOK_MAX; ++i)
170 pa_hook_done(&p->hooks[i]);
171
172 pa_assert_se(pa_shared_remove(p->core, "dbus-protocol") >= 0);
173
174 pa_core_unref(p->core);
175
176 pa_xfree(p);
177 }
178
179 static void update_introspection(struct object_entry *oe) {
180 pa_strbuf *buf;
181 void *interfaces_state = NULL;
182 struct interface_entry *iface_entry = NULL;
183
184 pa_assert(oe);
185
186 buf = pa_strbuf_new();
187 pa_strbuf_puts(buf, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
188 pa_strbuf_puts(buf, "<node>\n");
189
190 PA_HASHMAP_FOREACH(iface_entry, oe->interfaces, interfaces_state) {
191 pa_dbus_method_handler *method_handler;
192 pa_dbus_property_handler *property_handler;
193 void *handlers_state = NULL;
194 unsigned i;
195 unsigned j;
196
197 pa_strbuf_printf(buf, " <interface name=\"%s\">\n", iface_entry->name);
198
199 PA_HASHMAP_FOREACH(method_handler, iface_entry->method_handlers, handlers_state) {
200 pa_strbuf_printf(buf, " <method name=\"%s\">\n", method_handler->method_name);
201
202 for (i = 0; i < method_handler->n_arguments; ++i)
203 pa_strbuf_printf(buf, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n",
204 method_handler->arguments[i].name,
205 method_handler->arguments[i].type,
206 method_handler->arguments[i].direction);
207
208 pa_strbuf_puts(buf, " </method>\n");
209 }
210
211 handlers_state = NULL;
212
213 PA_HASHMAP_FOREACH(property_handler, iface_entry->property_handlers, handlers_state)
214 pa_strbuf_printf(buf, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n",
215 property_handler->property_name,
216 property_handler->type,
217 property_handler->get_cb ? (property_handler->set_cb ? "readwrite" : "read") : "write");
218
219 for (i = 0; i < iface_entry->n_signals; ++i) {
220 pa_strbuf_printf(buf, " <signal name=\"%s\">\n", iface_entry->signals[i].name);
221
222 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j)
223 pa_strbuf_printf(buf, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry->signals[i].arguments[j].name,
224 iface_entry->signals[i].arguments[j].type);
225
226 pa_strbuf_puts(buf, " </signal>\n");
227 }
228
229 pa_strbuf_puts(buf, " </interface>\n");
230 }
231
232 pa_strbuf_puts(buf, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE "\">\n"
233 " <method name=\"Introspect\">\n"
234 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
235 " </method>\n"
236 " </interface>\n"
237 " <interface name=\"" DBUS_INTERFACE_PROPERTIES "\">\n"
238 " <method name=\"Get\">\n"
239 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
240 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
241 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
242 " </method>\n"
243 " <method name=\"Set\">\n"
244 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
246 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
247 " </method>\n"
248 " <method name=\"GetAll\">\n"
249 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
250 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
251 " </method>\n"
252 " </interface>\n");
253
254 pa_strbuf_puts(buf, "</node>\n");
255
256 pa_xfree(oe->introspection);
257 oe->introspection = pa_strbuf_tostring_free(buf);
258 }
259
260 enum find_result_t {
261 FOUND_METHOD,
262 FOUND_GET_PROPERTY,
263 FOUND_SET_PROPERTY,
264 FOUND_GET_ALL,
265 PROPERTY_ACCESS_DENIED,
266 NO_SUCH_METHOD,
267 NO_SUCH_PROPERTY,
268 INVALID_MESSAGE_ARGUMENTS
269 };
270
271 static enum find_result_t find_handler_by_property(struct object_entry *obj_entry,
272 DBusMessage *msg,
273 const char *property,
274 struct interface_entry **iface_entry,
275 pa_dbus_property_handler **property_handler) {
276 void *state = NULL;
277
278 pa_assert(obj_entry);
279 pa_assert(msg);
280 pa_assert(property);
281 pa_assert(iface_entry);
282 pa_assert(property_handler);
283
284 PA_HASHMAP_FOREACH(*iface_entry, obj_entry->interfaces, state) {
285 if ((*property_handler = pa_hashmap_get((*iface_entry)->property_handlers, property))) {
286 if (dbus_message_has_member(msg, "Get"))
287 return (*property_handler)->get_cb ? FOUND_GET_PROPERTY : PROPERTY_ACCESS_DENIED;
288 else if (dbus_message_has_member(msg, "Set"))
289 return (*property_handler)->set_cb ? FOUND_SET_PROPERTY : PROPERTY_ACCESS_DENIED;
290 else
291 pa_assert_not_reached();
292 }
293 }
294
295 return NO_SUCH_PROPERTY;
296 }
297
298 static enum find_result_t find_handler_by_method(struct object_entry *obj_entry,
299 const char *method,
300 struct interface_entry **iface_entry,
301 pa_dbus_method_handler **method_handler) {
302 void *state = NULL;
303
304 pa_assert(obj_entry);
305 pa_assert(method);
306 pa_assert(iface_entry);
307 pa_assert(method_handler);
308
309 PA_HASHMAP_FOREACH(*iface_entry, obj_entry->interfaces, state) {
310 if ((*method_handler = pa_hashmap_get((*iface_entry)->method_handlers, method)))
311 return FOUND_METHOD;
312 }
313
314 return NO_SUCH_METHOD;
315 }
316
317 static enum find_result_t find_handler_from_properties_call(struct object_entry *obj_entry,
318 DBusMessage *msg,
319 struct interface_entry **iface_entry,
320 pa_dbus_property_handler **property_handler,
321 const char **attempted_property) {
322 const char *interface;
323
324 pa_assert(obj_entry);
325 pa_assert(msg);
326 pa_assert(iface_entry);
327
328 if (dbus_message_has_member(msg, "GetAll")) {
329 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface, DBUS_TYPE_INVALID))
330 return INVALID_MESSAGE_ARGUMENTS;
331
332 if (*interface) {
333 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)))
334 return FOUND_GET_ALL;
335 else {
336 return NO_SUCH_METHOD; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
337 }
338 } else {
339 pa_assert_se((*iface_entry = pa_hashmap_first(obj_entry->interfaces)));
340 return FOUND_GET_ALL;
341 }
342 } else {
343 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, attempted_property, DBUS_TYPE_INVALID))
344 return INVALID_MESSAGE_ARGUMENTS;
345
346 if (*interface) {
347 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)) &&
348 (*property_handler = pa_hashmap_get((*iface_entry)->property_handlers, *attempted_property))) {
349 if (dbus_message_has_member(msg, "Get"))
350 return (*property_handler)->get_cb ? FOUND_GET_PROPERTY : PROPERTY_ACCESS_DENIED;
351 else if (dbus_message_has_member(msg, "Set"))
352 return (*property_handler)->set_cb ? FOUND_SET_PROPERTY : PROPERTY_ACCESS_DENIED;
353 else
354 pa_assert_not_reached();
355 } else
356 return NO_SUCH_PROPERTY;
357 } else
358 return find_handler_by_property(obj_entry, msg, *attempted_property, iface_entry, property_handler);
359 }
360 }
361
362 static enum find_result_t find_handler(struct object_entry *obj_entry,
363 DBusMessage *msg,
364 struct interface_entry **iface_entry,
365 pa_dbus_method_handler **method_handler,
366 pa_dbus_property_handler **property_handler,
367 const char **attempted_property) {
368 const char *interface;
369
370 pa_assert(obj_entry);
371 pa_assert(msg);
372 pa_assert(iface_entry);
373 pa_assert(method_handler);
374 pa_assert(property_handler);
375 pa_assert(attempted_property);
376
377 *iface_entry = NULL;
378 *method_handler = NULL;
379
380 if (dbus_message_has_interface(msg, DBUS_INTERFACE_PROPERTIES))
381 return find_handler_from_properties_call(obj_entry, msg, iface_entry, property_handler, attempted_property);
382
383 else if ((interface = dbus_message_get_interface(msg))) {
384 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)) &&
385 (*method_handler = pa_hashmap_get((*iface_entry)->method_handlers, dbus_message_get_member(msg))))
386 return FOUND_METHOD;
387 else {
388 pa_log("Message has unknown interface or there's no method handler.");
389 return NO_SUCH_METHOD;
390 }
391
392 } else { /* The method call doesn't contain an interface. */
393 if (dbus_message_has_member(msg, "Get") || dbus_message_has_member(msg, "Set") || dbus_message_has_member(msg, "GetAll")) {
394 if (find_handler_by_method(obj_entry, dbus_message_get_member(msg), iface_entry, method_handler) == FOUND_METHOD)
395 return FOUND_METHOD; /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
396 else
397 /* Assume this is a .Properties call. */
398 return find_handler_from_properties_call(obj_entry, msg, iface_entry, property_handler, attempted_property);
399
400 } else /* This is not a .Properties call. */
401 return find_handler_by_method(obj_entry, dbus_message_get_member(msg), iface_entry, method_handler);
402 }
403 }
404
405 static DBusHandlerResult handle_message_cb(DBusConnection *connection, DBusMessage *message, void *user_data) {
406 pa_dbus_protocol *p = user_data;
407 struct object_entry *obj_entry = NULL;
408 struct interface_entry *iface_entry = NULL;
409 pa_dbus_method_handler *method_handler = NULL;
410 pa_dbus_property_handler *property_handler = NULL;
411 const char *attempted_property = NULL;
412
413 pa_assert(connection);
414 pa_assert(message);
415 pa_assert(p);
416 pa_assert(p->objects);
417
418 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
419 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
420
421 pa_log_debug("Received message: destination = %s, interface = %s, member = %s",
422 dbus_message_get_path(message),
423 dbus_message_get_interface(message),
424 dbus_message_get_member(message));
425
426 pa_assert_se((obj_entry = pa_hashmap_get(p->objects, dbus_message_get_path(message))));
427
428 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") ||
429 (!dbus_message_get_interface(message) && dbus_message_has_member(message, "Introspect"))) {
430 pa_dbus_send_basic_value_reply(connection, message, DBUS_TYPE_STRING, &obj_entry->introspection);
431 goto finish;
432 }
433
434 switch (find_handler(obj_entry, message, &iface_entry, &method_handler, &property_handler, &attempted_property)) {
435 case FOUND_METHOD:
436 method_handler->receive_cb(connection, message, iface_entry->userdata);
437 break;
438
439 case FOUND_GET_PROPERTY:
440 property_handler->get_cb(connection, message, iface_entry->userdata);
441 break;
442
443 case FOUND_SET_PROPERTY:
444 property_handler->set_cb(connection, message, iface_entry->userdata);
445 break;
446
447 case FOUND_GET_ALL:
448 if (iface_entry->get_all_properties_cb)
449 iface_entry->get_all_properties_cb(connection, message, iface_entry->userdata);
450 /* TODO: Write an else branch where a dummy response is sent. */
451 break;
452
453 case PROPERTY_ACCESS_DENIED:
454 pa_dbus_send_error(connection, message, DBUS_ERROR_ACCESS_DENIED, "%s access denied for property %s", dbus_message_get_member(message), attempted_property);
455 break;
456
457 case NO_SUCH_METHOD:
458 pa_dbus_send_error(connection, message, DBUS_ERROR_UNKNOWN_METHOD, "%s: No such method", dbus_message_get_member(message));
459 break;
460
461 case NO_SUCH_PROPERTY:
462 pa_dbus_send_error(connection, message, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s: No such property", attempted_property);
463 break;
464
465 case INVALID_MESSAGE_ARGUMENTS:
466 pa_dbus_send_error(connection, message, DBUS_ERROR_INVALID_ARGS, "Invalid arguments for %s", dbus_message_get_member(message));
467 break;
468
469 default:
470 pa_assert_not_reached();
471 }
472
473 finish:
474 return DBUS_HANDLER_RESULT_HANDLED;
475 }
476
477 static DBusObjectPathVTable vtable = {
478 .unregister_function = NULL,
479 .message_function = handle_message_cb,
480 .dbus_internal_pad1 = NULL,
481 .dbus_internal_pad2 = NULL,
482 .dbus_internal_pad3 = NULL,
483 .dbus_internal_pad4 = NULL
484 };
485
486 static void register_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
487 struct connection_entry *conn_entry;
488 void *state = NULL;
489
490 pa_assert(p);
491 pa_assert(obj_entry);
492
493 PA_HASHMAP_FOREACH(conn_entry, p->connections, state)
494 pa_assert_se(dbus_connection_register_object_path(conn_entry->connection, obj_entry->path, &vtable, p));
495 }
496
497 static pa_dbus_arg_info *copy_args(const pa_dbus_arg_info *src, unsigned n) {
498 pa_dbus_arg_info *dst;
499 unsigned i;
500
501 if (n == 0)
502 return NULL;
503
504 pa_assert(src);
505
506 dst = pa_xnew0(pa_dbus_arg_info, n);
507
508 for (i = 0; i < n; ++i) {
509 dst[i].name = pa_xstrdup(src[i].name);
510 dst[i].type = pa_xstrdup(src[i].type);
511 dst[i].direction = pa_xstrdup(src[i].direction);
512 }
513
514 return dst;
515 }
516
517 static pa_hashmap *create_method_handlers(const pa_dbus_interface_info *info) {
518 pa_hashmap *handlers;
519 unsigned i;
520
521 pa_assert(info);
522 pa_assert(info->method_handlers || info->n_method_handlers == 0);
523
524 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
525
526 for (i = 0; i < info->n_method_handlers; ++i) {
527 pa_dbus_method_handler *h = pa_xnew(pa_dbus_method_handler, 1);
528 h->method_name = pa_xstrdup(info->method_handlers[i].method_name);
529 h->arguments = copy_args(info->method_handlers[i].arguments, info->method_handlers[i].n_arguments);
530 h->n_arguments = info->method_handlers[i].n_arguments;
531 h->receive_cb = info->method_handlers[i].receive_cb;
532
533 pa_hashmap_put(handlers, h->method_name, h);
534 }
535
536 return handlers;
537 }
538
539 static pa_hashmap *create_property_handlers(const pa_dbus_interface_info *info) {
540 pa_hashmap *handlers;
541 unsigned i = 0;
542
543 pa_assert(info);
544 pa_assert(info->property_handlers || info->n_property_handlers == 0);
545
546 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
547
548 for (i = 0; i < info->n_property_handlers; ++i) {
549 pa_dbus_property_handler *h = pa_xnew(pa_dbus_property_handler, 1);
550 h->property_name = pa_xstrdup(info->property_handlers[i].property_name);
551 h->type = pa_xstrdup(info->property_handlers[i].type);
552 h->get_cb = info->property_handlers[i].get_cb;
553 h->set_cb = info->property_handlers[i].set_cb;
554
555 pa_hashmap_put(handlers, h->property_name, h);
556 }
557
558 return handlers;
559 }
560
561 static pa_dbus_signal_info *copy_signals(const pa_dbus_interface_info *info) {
562 pa_dbus_signal_info *dst;
563 unsigned i;
564
565 pa_assert(info);
566
567 if (info->n_signals == 0)
568 return NULL;
569
570 pa_assert(info->signals);
571
572 dst = pa_xnew(pa_dbus_signal_info, info->n_signals);
573
574 for (i = 0; i < info->n_signals; ++i) {
575 dst[i].name = pa_xstrdup(info->signals[i].name);
576 dst[i].arguments = copy_args(info->signals[i].arguments, info->signals[i].n_arguments);
577 dst[i].n_arguments = info->signals[i].n_arguments;
578 }
579
580 return dst;
581 }
582
583 int pa_dbus_protocol_add_interface(pa_dbus_protocol *p,
584 const char *path,
585 const pa_dbus_interface_info *info,
586 void *userdata) {
587 struct object_entry *obj_entry;
588 struct interface_entry *iface_entry;
589 pa_bool_t obj_entry_created = FALSE;
590
591 pa_assert(p);
592 pa_assert(path);
593 pa_assert(info);
594 pa_assert(info->name);
595 pa_assert(info->method_handlers || info->n_method_handlers == 0);
596 pa_assert(info->property_handlers || info->n_property_handlers == 0);
597 pa_assert(info->get_all_properties_cb || info->n_property_handlers == 0);
598 pa_assert(info->signals || info->n_signals == 0);
599
600 if (!(obj_entry = pa_hashmap_get(p->objects, path))) {
601 obj_entry = pa_xnew(struct object_entry, 1);
602 obj_entry->path = pa_xstrdup(path);
603 obj_entry->interfaces = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
604 obj_entry->introspection = NULL;
605
606 pa_hashmap_put(p->objects, path, obj_entry);
607 obj_entry_created = TRUE;
608 }
609
610 if (pa_hashmap_get(obj_entry->interfaces, info->name) != NULL)
611 goto fail; /* The interface was already registered. */
612
613 iface_entry = pa_xnew(struct interface_entry, 1);
614 iface_entry->name = pa_xstrdup(info->name);
615 iface_entry->method_handlers = create_method_handlers(info);
616 iface_entry->property_handlers = create_property_handlers(info);
617 iface_entry->get_all_properties_cb = info->get_all_properties_cb;
618 iface_entry->signals = copy_signals(info);
619 iface_entry->n_signals = info->n_signals;
620 iface_entry->userdata = userdata;
621 pa_hashmap_put(obj_entry->interfaces, iface_entry->name, iface_entry);
622
623 update_introspection(obj_entry);
624
625 if (obj_entry_created)
626 register_object(p, obj_entry);
627
628 pa_log_debug("Interface %s added for object %s", iface_entry->name, obj_entry->path);
629
630 return 0;
631
632 fail:
633 if (obj_entry_created) {
634 pa_hashmap_remove(p->objects, path);
635 pa_xfree(obj_entry);
636 }
637
638 return -1;
639 }
640
641 static void unregister_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
642 struct connection_entry *conn_entry;
643 void *state = NULL;
644
645 pa_assert(p);
646 pa_assert(obj_entry);
647
648 PA_HASHMAP_FOREACH(conn_entry, p->connections, state)
649 pa_assert_se(dbus_connection_unregister_object_path(conn_entry->connection, obj_entry->path));
650 }
651
652 static void method_handler_free_cb(void *p, void *userdata) {
653 pa_dbus_method_handler *h = p;
654 unsigned i;
655
656 pa_assert(h);
657
658 pa_xfree((char *) h->method_name);
659
660 for (i = 0; i < h->n_arguments; ++i) {
661 pa_xfree((char *) h->arguments[i].name);
662 pa_xfree((char *) h->arguments[i].type);
663 pa_xfree((char *) h->arguments[i].direction);
664 }
665
666 pa_xfree((pa_dbus_arg_info *) h->arguments);
667 }
668
669 static void property_handler_free_cb(void *p, void *userdata) {
670 pa_dbus_property_handler *h = p;
671
672 pa_assert(h);
673
674 pa_xfree((char *) h->property_name);
675 pa_xfree((char *) h->type);
676
677 pa_xfree(h);
678 }
679
680 int pa_dbus_protocol_remove_interface(pa_dbus_protocol *p, const char* path, const char* interface) {
681 struct object_entry *obj_entry;
682 struct interface_entry *iface_entry;
683 unsigned i;
684
685 pa_assert(p);
686 pa_assert(path);
687 pa_assert(interface);
688
689 if (!(obj_entry = pa_hashmap_get(p->objects, path)))
690 return -1;
691
692 if (!(iface_entry = pa_hashmap_remove(obj_entry->interfaces, interface)))
693 return -1;
694
695 update_introspection(obj_entry);
696
697 pa_xfree(iface_entry->name);
698 pa_hashmap_free(iface_entry->method_handlers, method_handler_free_cb, NULL);
699 pa_hashmap_free(iface_entry->property_handlers, property_handler_free_cb, NULL);
700
701 for (i = 0; i < iface_entry->n_signals; ++i) {
702 unsigned j;
703
704 pa_xfree((char *) iface_entry->signals[i].name);
705
706 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j) {
707 pa_xfree((char *) iface_entry->signals[i].arguments[j].name);
708 pa_xfree((char *) iface_entry->signals[i].arguments[j].type);
709 pa_assert(iface_entry->signals[i].arguments[j].direction == NULL);
710 }
711
712 pa_xfree((pa_dbus_arg_info *) iface_entry->signals[i].arguments);
713 }
714
715 pa_xfree(iface_entry->signals);
716 pa_xfree(iface_entry);
717
718 if (pa_hashmap_isempty(obj_entry->interfaces)) {
719 unregister_object(p, obj_entry);
720
721 pa_hashmap_remove(p->objects, path);
722 pa_xfree(obj_entry->path);
723 pa_hashmap_free(obj_entry->interfaces, NULL, NULL);
724 pa_xfree(obj_entry->introspection);
725 pa_xfree(obj_entry);
726 }
727
728 return 0;
729 }
730
731 static void register_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
732 struct object_entry *obj_entry;
733 void *state = NULL;
734
735 pa_assert(p);
736 pa_assert(conn);
737
738 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
739 pa_assert_se(dbus_connection_register_object_path(conn, obj_entry->path, &vtable, p));
740 }
741
742 int pa_dbus_protocol_register_connection(pa_dbus_protocol *p, DBusConnection *conn, pa_client *client) {
743 struct connection_entry *conn_entry;
744
745 pa_assert(p);
746 pa_assert(conn);
747 pa_assert(client);
748
749 if (pa_hashmap_get(p->connections, conn))
750 return -1; /* The connection was already registered. */
751
752 register_all_objects(p, conn);
753
754 conn_entry = pa_xnew(struct connection_entry, 1);
755 conn_entry->connection = dbus_connection_ref(conn);
756 conn_entry->client = client;
757 conn_entry->listening_for_all_signals = FALSE;
758 conn_entry->all_signals_objects = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
759 conn_entry->listening_signals = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
760
761 pa_hashmap_put(p->connections, conn, conn_entry);
762
763 return 0;
764 }
765
766 static void unregister_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
767 struct object_entry *obj_entry;
768 void *state = NULL;
769
770 pa_assert(p);
771 pa_assert(conn);
772
773 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
774 pa_assert_se(dbus_connection_unregister_object_path(conn, obj_entry->path));
775 }
776
777 static void free_listened_object_name_cb(void *p, void *userdata) {
778 pa_assert(p);
779
780 pa_xfree(p);
781 }
782
783 static void free_listening_signals_idxset_cb(void *p, void *userdata) {
784 pa_idxset *set = p;
785
786 pa_assert(set);
787
788 pa_idxset_free(set, free_listened_object_name_cb, NULL);
789 }
790
791 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol *p, DBusConnection *conn) {
792 struct connection_entry *conn_entry;
793
794 pa_assert(p);
795 pa_assert(conn);
796
797 if (!(conn_entry = pa_hashmap_remove(p->connections, conn)))
798 return -1;
799
800 unregister_all_objects(p, conn);
801
802 dbus_connection_unref(conn_entry->connection);
803 pa_idxset_free(conn_entry->all_signals_objects, free_listened_object_name_cb, NULL);
804 pa_hashmap_free(conn_entry->listening_signals, free_listening_signals_idxset_cb, NULL);
805 pa_xfree(conn_entry);
806
807 return 0;
808 }
809
810 pa_client *pa_dbus_protocol_get_client(pa_dbus_protocol *p, DBusConnection *conn) {
811 struct connection_entry *conn_entry;
812
813 pa_assert(p);
814 pa_assert(conn);
815
816 if (!(conn_entry = pa_hashmap_get(p->connections, conn)))
817 return NULL;
818
819 return conn_entry->client;
820 }
821
822 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal, char **objects, unsigned n_objects) {
823 struct connection_entry *conn_entry;
824 pa_idxset *object_set;
825 char *object_path;
826 unsigned i;
827
828 pa_assert(p);
829 pa_assert(conn);
830 pa_assert(objects || n_objects == 0);
831
832 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
833
834 /* all_signals_objects will either be emptied or replaced with new objects,
835 * so we empty it here unconditionally. If listening_for_all_signals is
836 * currently FALSE, the idxset is empty already. */
837 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
838 pa_xfree(object_path);
839
840 if (signal) {
841 conn_entry->listening_for_all_signals = FALSE;
842
843 /* Replace the old object list with a new one. */
844 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
845 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
846 object_set = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
847
848 for (i = 0; i < n_objects; ++i)
849 pa_idxset_put(object_set, pa_xstrdup(objects[i]), NULL);
850
851 } else {
852 conn_entry->listening_for_all_signals = TRUE;
853
854 /* We're not interested in individual signals anymore, so let's empty
855 * listening_signals. */
856 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
857 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
858
859 for (i = 0; i < n_objects; ++i)
860 pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL);
861 }
862 }
863
864 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal) {
865 struct connection_entry *conn_entry;
866 pa_idxset *object_set;
867
868 pa_assert(p);
869 pa_assert(conn);
870
871 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
872
873 if (signal) {
874 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
875 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
876
877 } else {
878 char *object_path;
879
880 conn_entry->listening_for_all_signals = FALSE;
881
882 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
883 pa_xfree(object_path);
884
885 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
886 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
887 }
888 }
889
890 void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
891 struct connection_entry *conn_entry;
892 void *state = NULL;
893 pa_idxset *object_set;
894 DBusMessage *signal_copy;
895
896 pa_assert(p);
897 pa_assert(signal);
898 pa_assert(dbus_message_get_type(signal) == DBUS_MESSAGE_TYPE_SIGNAL);
899
900 PA_HASHMAP_FOREACH(conn_entry, p->connections, state) {
901 if ((conn_entry->listening_for_all_signals /* Case 1: listening for all signals */
902 && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal), NULL)
903 || pa_idxset_isempty(conn_entry->all_signals_objects)))
904
905 || (!conn_entry->listening_for_all_signals /* Case 2: not listening for all signals */
906 && (object_set = pa_hashmap_get(conn_entry->listening_signals, signal))
907 && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal), NULL)
908 || pa_idxset_isempty(object_set)))) {
909
910 pa_assert_se(signal_copy = dbus_message_copy(signal));
911 pa_assert_se(dbus_connection_send(conn_entry->connection, signal_copy, NULL));
912 dbus_message_unref(signal_copy);
913 }
914 }
915 }
916
917 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol *p, unsigned *n) {
918 const char **extensions;
919 const char *ext_name;
920 void *state = NULL;
921 unsigned i = 0;
922
923 pa_assert(p);
924 pa_assert(n);
925
926 *n = pa_idxset_size(p->extensions);
927
928 if (*n <= 0)
929 return NULL;
930
931 extensions = pa_xnew(const char *, *n);
932
933 while ((ext_name = pa_idxset_iterate(p->extensions, &state, NULL)))
934 extensions[i++] = ext_name;
935
936 return extensions;
937 }
938
939 int pa_dbus_protocol_register_extension(pa_dbus_protocol *p, const char *name) {
940 char *internal_name;
941
942 pa_assert(p);
943 pa_assert(name);
944
945 internal_name = pa_xstrdup(name);
946
947 if (pa_idxset_put(p->extensions, internal_name, NULL) < 0) {
948 pa_xfree(internal_name);
949 return -1;
950 }
951
952 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED], internal_name);
953
954 return 0;
955 }
956
957 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol *p, const char *name) {
958 char *internal_name;
959
960 pa_assert(p);
961 pa_assert(name);
962
963 if (!(internal_name = pa_idxset_remove_by_data(p->extensions, name, NULL)))
964 return -1;
965
966 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED], internal_name);
967
968 pa_xfree(internal_name);
969
970 return 0;
971 }
972
973 pa_hook_slot *pa_dbus_protocol_hook_connect(pa_dbus_protocol *p, pa_dbus_protocol_hook_t hook, pa_hook_priority_t prio, pa_hook_cb_t cb, void *data) {
974 pa_assert(p);
975 pa_assert(hook < PA_DBUS_PROTOCOL_HOOK_MAX);
976 pa_assert(cb);
977
978 return pa_hook_connect(&p->hooks[hook], prio, cb, data);
979 }