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