]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-dbus.c
dbus: Take advantage of the PA_HASHMAP_FOREACH macro.
[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 PA_HASHMAP_FOREACH(iface_entry, oe->interfaces, interfaces_state) {
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 PA_HASHMAP_FOREACH(method_handler, iface_entry->method_handlers, handlers_state) {
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 PA_HASHMAP_FOREACH(*iface_entry, obj_entry->interfaces, state) {
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 PA_HASHMAP_FOREACH(*iface_entry, obj_entry->interfaces, state) {
307 if ((*method_handler = pa_hashmap_get((*iface_entry)->method_handlers, method)))
308 return FOUND_METHOD;
309 }
310
311 return NO_SUCH_METHOD;
312 }
313
314 static enum find_result_t find_handler_from_properties_call(struct object_entry *obj_entry,
315 DBusMessage *msg,
316 struct interface_entry **iface_entry,
317 pa_dbus_property_handler **property_handler,
318 const char **attempted_property) {
319 const char *interface;
320
321 pa_assert(obj_entry);
322 pa_assert(msg);
323 pa_assert(iface_entry);
324
325 if (dbus_message_has_member(msg, "GetAll")) {
326 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface, DBUS_TYPE_INVALID))
327 return INVALID_MESSAGE_ARGUMENTS;
328
329 if (*interface) {
330 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)))
331 return FOUND_GET_ALL;
332 else {
333 return NO_SUCH_METHOD; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
334 }
335 } else {
336 pa_assert_se((*iface_entry = pa_hashmap_first(obj_entry->interfaces)));
337 return FOUND_GET_ALL;
338 }
339 } else {
340 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, attempted_property, DBUS_TYPE_INVALID))
341 return INVALID_MESSAGE_ARGUMENTS;
342
343 if (*interface) {
344 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)) &&
345 (*property_handler = pa_hashmap_get((*iface_entry)->property_handlers, *attempted_property))) {
346 if (dbus_message_has_member(msg, "Get"))
347 return (*property_handler)->get_cb ? FOUND_GET_PROPERTY : PROPERTY_ACCESS_DENIED;
348 else if (dbus_message_has_member(msg, "Set"))
349 return (*property_handler)->set_cb ? FOUND_SET_PROPERTY : PROPERTY_ACCESS_DENIED;
350 else
351 pa_assert_not_reached();
352 } else
353 return NO_SUCH_PROPERTY;
354 } else
355 return find_handler_by_property(obj_entry, msg, *attempted_property, iface_entry, property_handler);
356 }
357 }
358
359 static enum find_result_t find_handler(struct object_entry *obj_entry,
360 DBusMessage *msg,
361 struct interface_entry **iface_entry,
362 pa_dbus_method_handler **method_handler,
363 pa_dbus_property_handler **property_handler,
364 const char **attempted_property) {
365 const char *interface;
366
367 pa_assert(obj_entry);
368 pa_assert(msg);
369 pa_assert(iface_entry);
370 pa_assert(method_handler);
371 pa_assert(property_handler);
372 pa_assert(attempted_property);
373
374 *iface_entry = NULL;
375 *method_handler = NULL;
376
377 if (dbus_message_has_interface(msg, DBUS_INTERFACE_PROPERTIES))
378 return find_handler_from_properties_call(obj_entry, msg, iface_entry, property_handler, attempted_property);
379
380 else if ((interface = dbus_message_get_interface(msg))) {
381 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)) &&
382 (*method_handler = pa_hashmap_get((*iface_entry)->method_handlers, dbus_message_get_member(msg))))
383 return FOUND_METHOD;
384 else {
385 pa_log("Message has unknown interface or there's no method handler.");
386 return NO_SUCH_METHOD;
387 }
388
389 } else { /* The method call doesn't contain an interface. */
390 if (dbus_message_has_member(msg, "Get") || dbus_message_has_member(msg, "Set") || dbus_message_has_member(msg, "GetAll")) {
391 if (find_handler_by_method(obj_entry, dbus_message_get_member(msg), iface_entry, method_handler) == FOUND_METHOD)
392 return FOUND_METHOD; /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
393 else
394 /* Assume this is a .Properties call. */
395 return find_handler_from_properties_call(obj_entry, msg, iface_entry, property_handler, attempted_property);
396
397 } else /* This is not a .Properties call. */
398 return find_handler_by_method(obj_entry, dbus_message_get_member(msg), iface_entry, method_handler);
399 }
400 }
401
402 static DBusHandlerResult handle_message_cb(DBusConnection *connection, DBusMessage *message, void *user_data) {
403 pa_dbus_protocol *p = user_data;
404 struct object_entry *obj_entry = NULL;
405 struct interface_entry *iface_entry = NULL;
406 pa_dbus_method_handler *method_handler = NULL;
407 pa_dbus_property_handler *property_handler = NULL;
408 const char *attempted_property = NULL;
409 DBusMessage *reply = NULL;
410
411 pa_assert(connection);
412 pa_assert(message);
413 pa_assert(p);
414 pa_assert(p->objects);
415
416 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
417 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
418
419 pa_log_debug("Received message: destination = %s, interface = %s, member = %s",
420 dbus_message_get_path(message),
421 dbus_message_get_interface(message),
422 dbus_message_get_member(message));
423
424 pa_assert_se((obj_entry = pa_hashmap_get(p->objects, dbus_message_get_path(message))));
425
426 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") ||
427 (!dbus_message_get_interface(message) && dbus_message_has_member(message, "Introspect"))) {
428 pa_assert_se((reply = dbus_message_new_method_return(message)));
429 pa_assert_se(dbus_message_append_args(reply, DBUS_TYPE_STRING, &obj_entry->introspection, DBUS_TYPE_INVALID));
430 pa_assert_se(dbus_connection_send(connection, reply, NULL));
431
432 goto finish;
433 }
434
435 switch (find_handler(obj_entry, message, &iface_entry, &method_handler, &property_handler, &attempted_property)) {
436 case FOUND_METHOD:
437 method_handler->receive_cb(connection, message, iface_entry->userdata);
438 break;
439
440 case FOUND_GET_PROPERTY:
441 property_handler->get_cb(connection, message, iface_entry->userdata);
442 break;
443
444 case FOUND_SET_PROPERTY:
445 property_handler->set_cb(connection, message, iface_entry->userdata);
446 break;
447
448 case FOUND_GET_ALL:
449 if (iface_entry->get_all_properties_cb)
450 iface_entry->get_all_properties_cb(connection, message, iface_entry->userdata);
451 break;
452
453 case PROPERTY_ACCESS_DENIED:
454 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)));
455 pa_assert_se(dbus_connection_send(connection, reply, NULL));
456 break;
457
458 case NO_SUCH_METHOD:
459 pa_assert_se((reply = dbus_message_new_error_printf(message, DBUS_ERROR_UNKNOWN_METHOD, "%s: No such method", dbus_message_get_member(message))));
460 pa_assert_se(dbus_connection_send(connection, reply, NULL));
461 break;
462
463 case NO_SUCH_PROPERTY:
464 pa_assert_se((reply = dbus_message_new_error_printf(message, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s: No such property", attempted_property)));
465 pa_assert_se(dbus_connection_send(connection, reply, NULL));
466 break;
467
468 case INVALID_MESSAGE_ARGUMENTS:
469 pa_assert_se((reply = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, "Invalid arguments for %s", dbus_message_get_member(message))));
470 pa_assert_se(dbus_connection_send(connection, reply, NULL));
471 break;
472
473 default:
474 pa_assert_not_reached();
475 }
476
477 finish:
478 if (reply)
479 dbus_message_unref(reply);
480
481 return DBUS_HANDLER_RESULT_HANDLED;
482 }
483
484 static DBusObjectPathVTable vtable = {
485 .unregister_function = NULL,
486 .message_function = handle_message_cb,
487 .dbus_internal_pad1 = NULL,
488 .dbus_internal_pad2 = NULL,
489 .dbus_internal_pad3 = NULL,
490 .dbus_internal_pad4 = NULL
491 };
492
493 static void register_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
494 struct connection_entry *conn_entry;
495 void *state = NULL;
496
497 pa_assert(p);
498 pa_assert(obj_entry);
499
500 PA_HASHMAP_FOREACH(conn_entry, p->connections, state)
501 pa_assert_se(dbus_connection_register_object_path(conn_entry->connection, obj_entry->path, &vtable, p));
502 }
503
504 static pa_dbus_arg_info *copy_args(const pa_dbus_arg_info *src, unsigned n) {
505 pa_dbus_arg_info *dst;
506 unsigned i;
507
508 if (n == 0)
509 return NULL;
510
511 pa_assert(src);
512
513 dst = pa_xnew0(pa_dbus_arg_info, n);
514
515 for (i = 0; i < n; ++i) {
516 dst[i].name = pa_xstrdup(src[i].name);
517 dst[i].type = pa_xstrdup(src[i].type);
518 dst[i].direction = pa_xstrdup(src[i].direction);
519 }
520
521 return dst;
522 }
523
524 static pa_hashmap *create_method_handlers(const pa_dbus_interface_info *info) {
525 pa_hashmap *handlers;
526 unsigned i;
527
528 pa_assert(info);
529 pa_assert(info->method_handlers || info->n_method_handlers == 0);
530
531 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
532
533 for (i = 0; i < info->n_method_handlers; ++i) {
534 pa_dbus_method_handler *h = pa_xnew(pa_dbus_method_handler, 1);
535 h->method_name = pa_xstrdup(info->method_handlers[i].method_name);
536 h->arguments = copy_args(info->method_handlers[i].arguments, info->method_handlers[i].n_arguments);
537 h->n_arguments = info->method_handlers[i].n_arguments;
538 h->receive_cb = info->method_handlers[i].receive_cb;
539
540 pa_hashmap_put(handlers, h->method_name, h);
541 }
542
543 return handlers;
544 }
545
546 static pa_hashmap *create_property_handlers(const pa_dbus_interface_info *info) {
547 pa_hashmap *handlers;
548 unsigned i = 0;
549
550 pa_assert(info);
551 pa_assert(info->property_handlers || info->n_property_handlers == 0);
552
553 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
554
555 for (i = 0; i < info->n_property_handlers; ++i) {
556 pa_dbus_property_handler *h = pa_xnew(pa_dbus_property_handler, 1);
557 h->property_name = pa_xstrdup(info->property_handlers[i].property_name);
558 h->type = pa_xstrdup(info->property_handlers[i].type);
559 h->get_cb = info->property_handlers[i].get_cb;
560 h->set_cb = info->property_handlers[i].set_cb;
561
562 pa_hashmap_put(handlers, h->property_name, h);
563 }
564
565 return handlers;
566 }
567
568 static pa_dbus_signal_info *copy_signals(const pa_dbus_interface_info *info) {
569 pa_dbus_signal_info *dst;
570 unsigned i;
571
572 pa_assert(info);
573
574 if (info->n_signals == 0)
575 return NULL;
576
577 pa_assert(info->signals);
578
579 dst = pa_xnew(pa_dbus_signal_info, info->n_signals);
580
581 for (i = 0; i < info->n_signals; ++i) {
582 dst[i].name = pa_xstrdup(info->signals[i].name);
583 dst[i].arguments = copy_args(info->signals[i].arguments, info->signals[i].n_arguments);
584 dst[i].n_arguments = info->signals[i].n_arguments;
585 }
586
587 return dst;
588 }
589
590 int pa_dbus_protocol_add_interface(pa_dbus_protocol *p,
591 const char *path,
592 const pa_dbus_interface_info *info,
593 void *userdata) {
594 struct object_entry *obj_entry;
595 struct interface_entry *iface_entry;
596 pa_bool_t obj_entry_created = FALSE;
597
598 pa_assert(p);
599 pa_assert(path);
600 pa_assert(info);
601 pa_assert(info->name);
602 pa_assert(info->method_handlers || info->n_method_handlers == 0);
603 pa_assert(info->property_handlers || info->n_property_handlers == 0);
604 pa_assert(info->get_all_properties_cb || info->n_property_handlers == 0);
605 pa_assert(info->signals || info->n_signals == 0);
606
607 if (!(obj_entry = pa_hashmap_get(p->objects, path))) {
608 obj_entry = pa_xnew(struct object_entry, 1);
609 obj_entry->path = pa_xstrdup(path);
610 obj_entry->interfaces = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
611 obj_entry->introspection = NULL;
612
613 pa_hashmap_put(p->objects, path, obj_entry);
614 obj_entry_created = TRUE;
615 }
616
617 if (pa_hashmap_get(obj_entry->interfaces, info->name) != NULL)
618 goto fail; /* The interface was already registered. */
619
620 iface_entry = pa_xnew(struct interface_entry, 1);
621 iface_entry->name = pa_xstrdup(info->name);
622 iface_entry->method_handlers = create_method_handlers(info);
623 iface_entry->property_handlers = create_property_handlers(info);
624 iface_entry->get_all_properties_cb = info->get_all_properties_cb;
625 iface_entry->signals = copy_signals(info);
626 iface_entry->n_signals = info->n_signals;
627 iface_entry->userdata = userdata;
628 pa_hashmap_put(obj_entry->interfaces, iface_entry->name, iface_entry);
629
630 update_introspection(obj_entry);
631
632 if (obj_entry_created)
633 register_object(p, obj_entry);
634
635 pa_log_debug("Interface %s added for object %s", iface_entry->name, obj_entry->path);
636
637 return 0;
638
639 fail:
640 if (obj_entry_created) {
641 pa_hashmap_remove(p->objects, path);
642 pa_xfree(obj_entry);
643 }
644
645 return -1;
646 }
647
648 static void unregister_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
649 struct connection_entry *conn_entry;
650 void *state = NULL;
651
652 pa_assert(p);
653 pa_assert(obj_entry);
654
655 PA_HASHMAP_FOREACH(conn_entry, p->connections, state)
656 pa_assert_se(dbus_connection_unregister_object_path(conn_entry->connection, obj_entry->path));
657 }
658
659 static void method_handler_free_cb(void *p, void *userdata) {
660 pa_dbus_method_handler *h = p;
661 unsigned i;
662
663 pa_assert(h);
664
665 pa_xfree((char *) h->method_name);
666
667 for (i = 0; i < h->n_arguments; ++i) {
668 pa_xfree((char *) h->arguments[i].name);
669 pa_xfree((char *) h->arguments[i].type);
670 pa_xfree((char *) h->arguments[i].direction);
671 }
672
673 pa_xfree((pa_dbus_arg_info *) h->arguments);
674 }
675
676 static void property_handler_free_cb(void *p, void *userdata) {
677 pa_dbus_property_handler *h = p;
678
679 pa_assert(h);
680
681 pa_xfree((char *) h->property_name);
682 pa_xfree((char *) h->type);
683
684 pa_xfree(h);
685 }
686
687 int pa_dbus_protocol_remove_interface(pa_dbus_protocol *p, const char* path, const char* interface) {
688 struct object_entry *obj_entry;
689 struct interface_entry *iface_entry;
690 unsigned i;
691
692 pa_assert(p);
693 pa_assert(path);
694 pa_assert(interface);
695
696 if (!(obj_entry = pa_hashmap_get(p->objects, path)))
697 return -1;
698
699 if (!(iface_entry = pa_hashmap_remove(obj_entry->interfaces, interface)))
700 return -1;
701
702 update_introspection(obj_entry);
703
704 pa_xfree(iface_entry->name);
705 pa_hashmap_free(iface_entry->method_handlers, method_handler_free_cb, NULL);
706 pa_hashmap_free(iface_entry->property_handlers, property_handler_free_cb, NULL);
707
708 for (i = 0; i < iface_entry->n_signals; ++i) {
709 unsigned j;
710
711 pa_xfree((char *) iface_entry->signals[i].name);
712
713 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j) {
714 pa_xfree((char *) iface_entry->signals[i].arguments[j].name);
715 pa_xfree((char *) iface_entry->signals[i].arguments[j].type);
716 pa_assert(iface_entry->signals[i].arguments[j].direction == NULL);
717 }
718
719 pa_xfree((pa_dbus_arg_info *) iface_entry->signals[i].arguments);
720 }
721
722 pa_xfree(iface_entry->signals);
723 pa_xfree(iface_entry);
724
725 if (pa_hashmap_isempty(obj_entry->interfaces)) {
726 unregister_object(p, obj_entry);
727
728 pa_hashmap_remove(p->objects, path);
729 pa_xfree(obj_entry->path);
730 pa_hashmap_free(obj_entry->interfaces, NULL, NULL);
731 pa_xfree(obj_entry->introspection);
732 pa_xfree(obj_entry);
733 }
734
735 return 0;
736 }
737
738 static void register_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
739 struct object_entry *obj_entry;
740 void *state = NULL;
741
742 pa_assert(p);
743 pa_assert(conn);
744
745 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
746 pa_assert_se(dbus_connection_register_object_path(conn, obj_entry->path, &vtable, p));
747 }
748
749 int pa_dbus_protocol_register_connection(pa_dbus_protocol *p, DBusConnection *conn, pa_client *client) {
750 struct connection_entry *conn_entry;
751
752 pa_assert(p);
753 pa_assert(conn);
754 pa_assert(client);
755
756 if (pa_hashmap_get(p->connections, conn))
757 return -1; /* The connection was already registered. */
758
759 register_all_objects(p, conn);
760
761 conn_entry = pa_xnew(struct connection_entry, 1);
762 conn_entry->connection = dbus_connection_ref(conn);
763 conn_entry->client = client;
764 conn_entry->listening_for_all_signals = FALSE;
765 conn_entry->all_signals_objects = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
766 conn_entry->listening_signals = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
767
768 pa_hashmap_put(p->connections, conn, conn_entry);
769
770 return 0;
771 }
772
773 static void unregister_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
774 struct object_entry *obj_entry;
775 void *state = NULL;
776
777 pa_assert(p);
778 pa_assert(conn);
779
780 PA_HASHMAP_FOREACH(obj_entry, p->objects, state)
781 pa_assert_se(dbus_connection_unregister_object_path(conn, obj_entry->path));
782 }
783
784 static void free_listened_object_name_cb(void *p, void *userdata) {
785 pa_assert(p);
786
787 pa_xfree(p);
788 }
789
790 static void free_listening_signals_idxset_cb(void *p, void *userdata) {
791 pa_idxset *set = p;
792
793 pa_assert(set);
794
795 pa_idxset_free(set, free_listened_object_name_cb, NULL);
796 }
797
798 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol *p, DBusConnection *conn) {
799 struct connection_entry *conn_entry;
800
801 pa_assert(p);
802 pa_assert(conn);
803
804 if (!(conn_entry = pa_hashmap_remove(p->connections, conn)))
805 return -1;
806
807 unregister_all_objects(p, conn);
808
809 dbus_connection_unref(conn_entry->connection);
810 pa_idxset_free(conn_entry->all_signals_objects, free_listened_object_name_cb, NULL);
811 pa_hashmap_free(conn_entry->listening_signals, free_listening_signals_idxset_cb, NULL);
812 pa_xfree(conn_entry);
813
814 return 0;
815 }
816
817 pa_client *pa_dbus_protocol_get_client(pa_dbus_protocol *p, DBusConnection *conn) {
818 struct connection_entry *conn_entry;
819
820 pa_assert(p);
821 pa_assert(conn);
822
823 if (!(conn_entry = pa_hashmap_get(p->connections, conn)))
824 return NULL;
825
826 return conn_entry->client;
827 }
828
829 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal, char **objects, unsigned n_objects) {
830 struct connection_entry *conn_entry;
831 pa_idxset *object_set;
832 char *object_path;
833 unsigned i;
834
835 pa_assert(p);
836 pa_assert(conn);
837 pa_assert(objects || n_objects == 0);
838
839 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
840
841 /* all_signals_objects will either be emptied or replaced with new objects,
842 * so we empty it here unconditionally. If listening_for_all_signals is
843 * currently FALSE, the idxset is empty already. */
844 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
845 pa_xfree(object_path);
846
847 if (signal) {
848 conn_entry->listening_for_all_signals = FALSE;
849
850 /* Replace the old object list with a new one. */
851 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
852 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
853 object_set = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
854
855 for (i = 0; i < n_objects; ++i)
856 pa_idxset_put(object_set, pa_xstrdup(objects[i]), NULL);
857
858 } else {
859 conn_entry->listening_for_all_signals = TRUE;
860
861 /* We're not interested in individual signals anymore, so let's empty
862 * listening_signals. */
863 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
864 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
865
866 for (i = 0; i < n_objects; ++i)
867 pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL);
868 }
869 }
870
871 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal) {
872 struct connection_entry *conn_entry;
873 pa_idxset *object_set;
874
875 pa_assert(p);
876 pa_assert(conn);
877
878 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
879
880 if (signal) {
881 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
882 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
883
884 } else {
885 char *object_path;
886
887 conn_entry->listening_for_all_signals = FALSE;
888
889 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
890 pa_xfree(object_path);
891
892 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
893 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
894 }
895 }
896
897 void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
898 struct connection_entry *conn_entry;
899 void *state = NULL;
900 pa_idxset *object_set;
901 DBusMessage *signal_copy;
902
903 pa_assert(p);
904 pa_assert(signal);
905 pa_assert(dbus_message_get_type(signal) == DBUS_MESSAGE_TYPE_SIGNAL);
906
907 PA_HASHMAP_FOREACH(conn_entry, p->connections, state) {
908 if ((conn_entry->listening_for_all_signals /* Case 1: listening for all signals */
909 && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal), NULL)
910 || pa_idxset_isempty(conn_entry->all_signals_objects)))
911
912 || (!conn_entry->listening_for_all_signals /* Case 2: not listening for all signals */
913 && (object_set = pa_hashmap_get(conn_entry->listening_signals, signal))
914 && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal), NULL)
915 || pa_idxset_isempty(object_set)))) {
916
917 pa_assert_se(signal_copy = dbus_message_copy(signal));
918 pa_assert_se(dbus_connection_send(conn_entry->connection, signal_copy, NULL));
919 dbus_message_unref(signal_copy);
920 }
921 }
922 }
923
924 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol *p, unsigned *n) {
925 const char **extensions;
926 const char *ext_name;
927 void *state = NULL;
928 unsigned i = 0;
929
930 pa_assert(p);
931 pa_assert(n);
932
933 *n = pa_idxset_size(p->extensions);
934
935 if (*n <= 0)
936 return NULL;
937
938 extensions = pa_xnew(const char *, *n);
939
940 while ((ext_name = pa_idxset_iterate(p->extensions, &state, NULL)))
941 extensions[i++] = ext_name;
942
943 return extensions;
944 }
945
946 int pa_dbus_protocol_register_extension(pa_dbus_protocol *p, const char *name) {
947 char *internal_name;
948
949 pa_assert(p);
950 pa_assert(name);
951
952 internal_name = pa_xstrdup(name);
953
954 if (pa_idxset_put(p->extensions, internal_name, NULL) < 0) {
955 pa_xfree(internal_name);
956 return -1;
957 }
958
959 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED], internal_name);
960
961 return 0;
962 }
963
964 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol *p, const char *name) {
965 char *internal_name;
966
967 pa_assert(p);
968 pa_assert(name);
969
970 if (!(internal_name = pa_idxset_remove_by_data(p->extensions, name, NULL)))
971 return -1;
972
973 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED], internal_name);
974
975 pa_xfree(internal_name);
976
977 return 0;
978 }
979
980 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) {
981 pa_assert(p);
982 pa_assert(hook < PA_DBUS_PROTOCOL_HOOK_MAX);
983 pa_assert(cb);
984
985 return pa_hook_connect(&p->hooks[hook], prio, cb, data);
986 }