]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-dbus.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[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_log_debug("Interface %s removed from object %s", iface_entry->name, obj_entry->path);
698
699 pa_xfree(iface_entry->name);
700 pa_hashmap_free(iface_entry->method_handlers, method_handler_free_cb, NULL);
701 pa_hashmap_free(iface_entry->property_handlers, property_handler_free_cb, NULL);
702
703 for (i = 0; i < iface_entry->n_signals; ++i) {
704 unsigned j;
705
706 pa_xfree((char *) iface_entry->signals[i].name);
707
708 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j) {
709 pa_xfree((char *) iface_entry->signals[i].arguments[j].name);
710 pa_xfree((char *) iface_entry->signals[i].arguments[j].type);
711 pa_assert(iface_entry->signals[i].arguments[j].direction == NULL);
712 }
713
714 pa_xfree((pa_dbus_arg_info *) iface_entry->signals[i].arguments);
715 }
716
717 pa_xfree(iface_entry->signals);
718 pa_xfree(iface_entry);
719
720 if (pa_hashmap_isempty(obj_entry->interfaces)) {
721 unregister_object(p, obj_entry);
722
723 pa_hashmap_remove(p->objects, path);
724 pa_xfree(obj_entry->path);
725 pa_hashmap_free(obj_entry->interfaces, NULL, NULL);
726 pa_xfree(obj_entry->introspection);
727 pa_xfree(obj_entry);
728 }
729
730 return 0;
731 }
732
733 static void register_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
734 struct object_entry *obj_entry;
735 void *state = NULL;
736
737 pa_assert(p);
738 pa_assert(conn);
739
740 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
741 pa_assert_se(dbus_connection_register_object_path(conn, obj_entry->path, &vtable, p));
742 }
743
744 int pa_dbus_protocol_register_connection(pa_dbus_protocol *p, DBusConnection *conn, pa_client *client) {
745 struct connection_entry *conn_entry;
746
747 pa_assert(p);
748 pa_assert(conn);
749 pa_assert(client);
750
751 if (pa_hashmap_get(p->connections, conn))
752 return -1; /* The connection was already registered. */
753
754 register_all_objects(p, conn);
755
756 conn_entry = pa_xnew(struct connection_entry, 1);
757 conn_entry->connection = dbus_connection_ref(conn);
758 conn_entry->client = client;
759 conn_entry->listening_for_all_signals = FALSE;
760 conn_entry->all_signals_objects = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
761 conn_entry->listening_signals = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
762
763 pa_hashmap_put(p->connections, conn, conn_entry);
764
765 return 0;
766 }
767
768 static void unregister_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
769 struct object_entry *obj_entry;
770 void *state = NULL;
771
772 pa_assert(p);
773 pa_assert(conn);
774
775 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
776 pa_assert_se(dbus_connection_unregister_object_path(conn, obj_entry->path));
777 }
778
779 static void free_listened_object_name_cb(void *p, void *userdata) {
780 pa_assert(p);
781
782 pa_xfree(p);
783 }
784
785 static void free_listening_signals_idxset_cb(void *p, void *userdata) {
786 pa_idxset *set = p;
787
788 pa_assert(set);
789
790 pa_idxset_free(set, free_listened_object_name_cb, NULL);
791 }
792
793 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol *p, DBusConnection *conn) {
794 struct connection_entry *conn_entry;
795
796 pa_assert(p);
797 pa_assert(conn);
798
799 if (!(conn_entry = pa_hashmap_remove(p->connections, conn)))
800 return -1;
801
802 unregister_all_objects(p, conn);
803
804 dbus_connection_unref(conn_entry->connection);
805 pa_idxset_free(conn_entry->all_signals_objects, free_listened_object_name_cb, NULL);
806 pa_hashmap_free(conn_entry->listening_signals, free_listening_signals_idxset_cb, NULL);
807 pa_xfree(conn_entry);
808
809 return 0;
810 }
811
812 pa_client *pa_dbus_protocol_get_client(pa_dbus_protocol *p, DBusConnection *conn) {
813 struct connection_entry *conn_entry;
814
815 pa_assert(p);
816 pa_assert(conn);
817
818 if (!(conn_entry = pa_hashmap_get(p->connections, conn)))
819 return NULL;
820
821 return conn_entry->client;
822 }
823
824 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal, char **objects, unsigned n_objects) {
825 struct connection_entry *conn_entry;
826 pa_idxset *object_set;
827 char *object_path;
828 unsigned i;
829
830 pa_assert(p);
831 pa_assert(conn);
832 pa_assert(objects || n_objects == 0);
833
834 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
835
836 /* all_signals_objects will either be emptied or replaced with new objects,
837 * so we empty it here unconditionally. If listening_for_all_signals is
838 * currently FALSE, the idxset is empty already. */
839 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
840 pa_xfree(object_path);
841
842 if (signal) {
843 conn_entry->listening_for_all_signals = FALSE;
844
845 /* Replace the old object list with a new one. */
846 if ((object_set = pa_hashmap_remove(conn_entry->listening_signals, signal)))
847 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
848 object_set = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
849
850 for (i = 0; i < n_objects; ++i)
851 pa_idxset_put(object_set, pa_xstrdup(objects[i]), NULL);
852
853 pa_hashmap_put(conn_entry->listening_signals, signal, object_set);
854
855 } else {
856 conn_entry->listening_for_all_signals = TRUE;
857
858 /* We're not interested in individual signals anymore, so let's empty
859 * listening_signals. */
860 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
861 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
862
863 for (i = 0; i < n_objects; ++i)
864 pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL);
865 }
866 }
867
868 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal) {
869 struct connection_entry *conn_entry;
870 pa_idxset *object_set;
871
872 pa_assert(p);
873 pa_assert(conn);
874
875 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
876
877 if (signal) {
878 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
879 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
880
881 } else {
882 char *object_path;
883
884 conn_entry->listening_for_all_signals = FALSE;
885
886 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
887 pa_xfree(object_path);
888
889 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
890 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
891 }
892 }
893
894 void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
895 struct connection_entry *conn_entry;
896 void *state = NULL;
897 pa_idxset *object_set;
898 DBusMessage *signal_copy;
899 char *signal_string;
900
901 pa_assert(p);
902 pa_assert(signal);
903 pa_assert(dbus_message_get_type(signal) == DBUS_MESSAGE_TYPE_SIGNAL);
904 pa_assert_se(dbus_message_get_interface(signal));
905 pa_assert_se(dbus_message_get_member(signal));
906
907 signal_string = pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal), dbus_message_get_member(signal));
908
909 PA_HASHMAP_FOREACH(conn_entry, p->connections, state) {
910 if ((conn_entry->listening_for_all_signals /* Case 1: listening for all signals */
911 && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal), NULL)
912 || pa_idxset_isempty(conn_entry->all_signals_objects)))
913
914 || (!conn_entry->listening_for_all_signals /* Case 2: not listening for all signals */
915 && (object_set = pa_hashmap_get(conn_entry->listening_signals, signal_string))
916 && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal), NULL)
917 || pa_idxset_isempty(object_set)))) {
918
919 pa_assert_se(signal_copy = dbus_message_copy(signal));
920 pa_assert_se(dbus_connection_send(conn_entry->connection, signal_copy, NULL));
921 dbus_message_unref(signal_copy);
922 }
923 }
924
925 pa_xfree(signal_string);
926 }
927
928 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol *p, unsigned *n) {
929 const char **extensions;
930 const char *ext_name;
931 void *state = NULL;
932 unsigned i = 0;
933
934 pa_assert(p);
935 pa_assert(n);
936
937 *n = pa_idxset_size(p->extensions);
938
939 if (*n <= 0)
940 return NULL;
941
942 extensions = pa_xnew(const char *, *n);
943
944 while ((ext_name = pa_idxset_iterate(p->extensions, &state, NULL)))
945 extensions[i++] = ext_name;
946
947 return extensions;
948 }
949
950 int pa_dbus_protocol_register_extension(pa_dbus_protocol *p, const char *name) {
951 char *internal_name;
952
953 pa_assert(p);
954 pa_assert(name);
955
956 internal_name = pa_xstrdup(name);
957
958 if (pa_idxset_put(p->extensions, internal_name, NULL) < 0) {
959 pa_xfree(internal_name);
960 return -1;
961 }
962
963 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED], internal_name);
964
965 return 0;
966 }
967
968 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol *p, const char *name) {
969 char *internal_name;
970
971 pa_assert(p);
972 pa_assert(name);
973
974 if (!(internal_name = pa_idxset_remove_by_data(p->extensions, name, NULL)))
975 return -1;
976
977 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED], internal_name);
978
979 pa_xfree(internal_name);
980
981 return 0;
982 }
983
984 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) {
985 pa_assert(p);
986 pa_assert(hook < PA_DBUS_PROTOCOL_HOOK_MAX);
987 pa_assert(cb);
988
989 return pa_hook_connect(&p->hooks[hook], prio, cb, data);
990 }