2 This file is part of PulseAudio.
4 Copyright 2009 Tanu Kaskinen
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.
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.
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
26 #include <dbus/dbus.h>
28 #include <pulse/xmalloc.h>
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>
36 #include "protocol-dbus.h"
38 struct pa_dbus_protocol
{
42 pa_hashmap
*objects
; /* Object path -> struct object_entry */
43 pa_hashmap
*connections
; /* DBusConnection -> struct connection_entry */
44 pa_idxset
*extensions
; /* Strings */
46 pa_hook hooks
[PA_DBUS_PROTOCOL_HOOK_MAX
];
51 pa_hashmap
*interfaces
; /* Interface name -> struct interface_entry */
55 struct connection_entry
{
56 DBusConnection
*connection
;
59 pa_bool_t listening_for_all_signals
;
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
;
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
;
71 struct interface_entry
{
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
;
81 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type
) {
83 char *runtime_path
= NULL
;
84 char *escaped_path
= NULL
;
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
);
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
);
98 case PA_SERVER_TYPE_NONE
:
99 address
= pa_xnew0(char, 1);
103 pa_assert_not_reached();
106 pa_xfree(runtime_path
);
107 pa_xfree(escaped_path
);
112 static pa_dbus_protocol
*dbus_protocol_new(pa_core
*c
) {
118 p
= pa_xnew(pa_dbus_protocol
, 1);
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
);
125 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
126 pa_hook_init(&p
->hooks
[i
], p
);
128 pa_assert_se(pa_shared_set(c
, "dbus-protocol", p
) >= 0);
133 pa_dbus_protocol
* pa_dbus_protocol_get(pa_core
*c
) {
136 if ((p
= pa_shared_get(c
, "dbus-protocol")))
137 return pa_dbus_protocol_ref(p
);
139 return dbus_protocol_new(c
);
142 pa_dbus_protocol
* pa_dbus_protocol_ref(pa_dbus_protocol
*p
) {
144 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
151 void pa_dbus_protocol_unref(pa_dbus_protocol
*p
) {
155 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
157 if (PA_REFCNT_DEC(p
) > 0)
160 pa_assert(pa_hashmap_isempty(p
->objects
));
161 pa_assert(pa_hashmap_isempty(p
->connections
));
162 pa_assert(pa_idxset_isempty(p
->extensions
));
164 pa_hashmap_free(p
->objects
, NULL
, NULL
);
165 pa_hashmap_free(p
->connections
, NULL
, NULL
);
166 pa_idxset_free(p
->extensions
, NULL
, NULL
);
168 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
169 pa_hook_done(&p
->hooks
[i
]);
171 pa_assert_se(pa_shared_remove(p
->core
, "dbus-protocol") >= 0);
176 static void update_introspection(struct object_entry
*oe
) {
178 void *interfaces_state
= NULL
;
179 struct interface_entry
*iface_entry
= NULL
;
183 buf
= pa_strbuf_new();
184 pa_strbuf_puts(buf
, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
);
185 pa_strbuf_puts(buf
, "<node>\n");
187 while ((iface_entry
= pa_hashmap_iterate(oe
->interfaces
, &interfaces_state
, NULL
))) {
188 pa_dbus_method_handler
*method_handler
;
189 pa_dbus_property_handler
*property_handler
;
190 void *handlers_state
= NULL
;
194 pa_strbuf_printf(buf
, " <interface name=\"%s\">\n", iface_entry
->name
);
196 while ((method_handler
= pa_hashmap_iterate(iface_entry
->method_handlers
, &handlers_state
, NULL
))) {
197 pa_strbuf_printf(buf
, " <method name=\"%s\">\n", method_handler
->method_name
);
199 for (i
= 0; i
< method_handler
->n_arguments
; ++i
)
200 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n", method_handler
->arguments
[i
].name
,
201 method_handler
->arguments
[i
].type
,
202 method_handler
->arguments
[i
].direction
);
204 pa_strbuf_puts(buf
, " </method>\n");
207 handlers_state
= NULL
;
209 while ((property_handler
= pa_hashmap_iterate(iface_entry
->property_handlers
, &handlers_state
, NULL
)))
210 pa_strbuf_printf(buf
, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n", property_handler
->property_name
,
211 property_handler
->type
,
212 property_handler
->get_cb
? (property_handler
->set_cb
? "readwrite" : "read") : "write");
214 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
215 pa_strbuf_printf(buf
, " <signal name=\"%s\">\n", iface_entry
->signals
[i
].name
);
217 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
)
218 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry
->signals
[i
].arguments
[j
].name
,
219 iface_entry
->signals
[i
].arguments
[j
].type
);
221 pa_strbuf_puts(buf
, " </signal>\n");
224 pa_strbuf_puts(buf
, " </interface>\n");
227 pa_strbuf_puts(buf
, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE
"\">\n"
228 " <method name=\"Introspect\">\n"
229 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
232 " <interface name=\"" DBUS_INTERFACE_PROPERTIES
"\">\n"
233 " <method name=\"Get\">\n"
234 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
235 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
236 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
238 " <method name=\"Set\">\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=\"in\"/>\n"
243 " <method name=\"GetAll\">\n"
244 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
249 pa_strbuf_puts(buf
, "</node>\n");
251 pa_xfree(oe
->introspection
);
252 oe
->introspection
= pa_strbuf_tostring_free(buf
);
260 PROPERTY_ACCESS_DENIED
,
263 INVALID_MESSAGE_ARGUMENTS
266 static enum find_result_t
find_handler_by_property(struct object_entry
*obj_entry
,
268 const char *property
,
269 struct interface_entry
**iface_entry
,
270 pa_dbus_property_handler
**property_handler
) {
273 pa_assert(obj_entry
);
276 pa_assert(iface_entry
);
277 pa_assert(property_handler
);
279 while ((*iface_entry
= pa_hashmap_iterate(obj_entry
->interfaces
, &state
, NULL
))) {
280 if ((*property_handler
= pa_hashmap_get((*iface_entry
)->property_handlers
, property
))) {
281 if (dbus_message_has_member(msg
, "Get"))
282 return (*property_handler
)->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
283 else if (dbus_message_has_member(msg
, "Set"))
284 return (*property_handler
)->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
286 pa_assert_not_reached();
290 return NO_SUCH_PROPERTY
;
293 static enum find_result_t
find_handler_by_method(struct object_entry
*obj_entry
,
295 struct interface_entry
**iface_entry
,
296 pa_dbus_method_handler
**method_handler
) {
299 pa_assert(obj_entry
);
301 pa_assert(iface_entry
);
302 pa_assert(method_handler
);
304 while ((*iface_entry
= pa_hashmap_iterate(obj_entry
->interfaces
, &state
, NULL
))) {
305 if ((*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, method
)))
309 return NO_SUCH_METHOD
;
312 static enum find_result_t
find_handler_from_properties_call(struct object_entry
*obj_entry
,
314 struct interface_entry
**iface_entry
,
315 pa_dbus_property_handler
**property_handler
,
316 const char **attempted_property
) {
317 const char *interface
;
319 pa_assert(obj_entry
);
321 pa_assert(iface_entry
);
323 if (dbus_message_has_member(msg
, "GetAll")) {
324 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_STRING
, &interface
, DBUS_TYPE_INVALID
))
325 return INVALID_MESSAGE_ARGUMENTS
;
328 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)))
329 return FOUND_GET_ALL
;
331 return NO_SUCH_METHOD
; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
333 pa_assert_se((*iface_entry
= pa_hashmap_first(obj_entry
->interfaces
)));
334 return FOUND_GET_ALL
;
337 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_STRING
, &interface
, DBUS_TYPE_STRING
, attempted_property
, DBUS_TYPE_INVALID
))
338 return INVALID_MESSAGE_ARGUMENTS
;
341 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)) &&
342 (*property_handler
= pa_hashmap_get((*iface_entry
)->property_handlers
, *attempted_property
))) {
343 if (dbus_message_has_member(msg
, "Get"))
344 return (*property_handler
)->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
345 else if (dbus_message_has_member(msg
, "Set"))
346 return (*property_handler
)->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
348 pa_assert_not_reached();
350 return NO_SUCH_PROPERTY
;
352 return find_handler_by_property(obj_entry
, msg
, *attempted_property
, iface_entry
, property_handler
);
356 static enum find_result_t
find_handler(struct object_entry
*obj_entry
,
358 struct interface_entry
**iface_entry
,
359 pa_dbus_method_handler
**method_handler
,
360 pa_dbus_property_handler
**property_handler
,
361 const char **attempted_property
) {
362 const char *interface
;
364 pa_assert(obj_entry
);
366 pa_assert(iface_entry
);
367 pa_assert(method_handler
);
368 pa_assert(property_handler
);
369 pa_assert(attempted_property
);
372 *method_handler
= NULL
;
374 if (dbus_message_has_interface(msg
, DBUS_INTERFACE_PROPERTIES
))
375 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
377 else if ((interface
= dbus_message_get_interface(msg
))) {
378 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)) &&
379 (*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, dbus_message_get_member(msg
))))
382 return NO_SUCH_METHOD
;
384 } else { /* The method call doesn't contain an interface. */
385 if (dbus_message_has_member(msg
, "Get") || dbus_message_has_member(msg
, "Set") || dbus_message_has_member(msg
, "GetAll")) {
386 if (find_handler_by_method(obj_entry
, dbus_message_get_member(msg
), iface_entry
, method_handler
) == FOUND_METHOD
)
387 return FOUND_METHOD
; /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
389 /* Assume this is a .Properties call. */
390 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
392 } else /* This is not a .Properties call. */
393 return find_handler_by_method(obj_entry
, dbus_message_get_member(msg
), iface_entry
, method_handler
);
397 static DBusHandlerResult
handle_message_cb(DBusConnection
*connection
, DBusMessage
*message
, void *user_data
) {
398 pa_dbus_protocol
*p
= user_data
;
399 struct object_entry
*obj_entry
= NULL
;
400 struct interface_entry
*iface_entry
= NULL
;
401 pa_dbus_method_handler
*method_handler
= NULL
;
402 pa_dbus_property_handler
*property_handler
= NULL
;
403 const char *attempted_property
= NULL
;
404 DBusMessage
*reply
= NULL
;
406 pa_assert(connection
);
409 pa_assert(p
->objects
);
411 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
412 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
414 pa_assert_se((obj_entry
= pa_hashmap_get(p
->objects
, dbus_message_get_path(message
))));
416 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") ||
417 (!dbus_message_get_interface(message
) && dbus_message_has_member(message
, "Introspect"))) {
418 pa_assert_se((reply
= dbus_message_new_method_return(message
)));
419 pa_assert_se(dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &obj_entry
->introspection
, DBUS_TYPE_INVALID
));
420 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
422 pa_log_debug("%s.Introspect handled.", obj_entry
->path
);
427 switch (find_handler(obj_entry
, message
, &iface_entry
, &method_handler
, &property_handler
, &attempted_property
)) {
429 method_handler
->receive_cb(connection
, message
, iface_entry
->userdata
);
432 case FOUND_GET_PROPERTY
:
433 property_handler
->get_cb(connection
, message
, iface_entry
->userdata
);
436 case FOUND_SET_PROPERTY
:
437 property_handler
->set_cb(connection
, message
, iface_entry
->userdata
);
441 if (iface_entry
->get_all_properties_cb
)
442 iface_entry
->get_all_properties_cb(connection
, message
, iface_entry
->userdata
);
445 case PROPERTY_ACCESS_DENIED
:
446 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
)));
447 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
451 pa_assert_se((reply
= dbus_message_new_error_printf(message
, DBUS_ERROR_UNKNOWN_METHOD
, "%s: No such method", dbus_message_get_member(message
))));
452 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
455 case NO_SUCH_PROPERTY
:
456 pa_assert_se((reply
= dbus_message_new_error_printf(message
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "%s: No such property", attempted_property
)));
457 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
460 case INVALID_MESSAGE_ARGUMENTS
:
461 pa_assert_se((reply
= dbus_message_new_error_printf(message
, DBUS_ERROR_INVALID_ARGS
, "Invalid arguments for %s", dbus_message_get_member(message
))));
462 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
466 pa_assert_not_reached();
471 dbus_message_unref(reply
);
473 return DBUS_HANDLER_RESULT_HANDLED
;
476 static DBusObjectPathVTable vtable
= {
477 .unregister_function
= NULL
,
478 .message_function
= handle_message_cb
,
479 .dbus_internal_pad1
= NULL
,
480 .dbus_internal_pad2
= NULL
,
481 .dbus_internal_pad3
= NULL
,
482 .dbus_internal_pad4
= NULL
485 static void register_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
486 struct connection_entry
*conn_entry
;
490 pa_assert(obj_entry
);
492 while ((conn_entry
= pa_hashmap_iterate(p
->connections
, &state
, NULL
)))
493 pa_assert_se(dbus_connection_register_object_path(conn_entry
->connection
, obj_entry
->path
, &vtable
, p
));
496 static pa_dbus_arg_info
*copy_args(const pa_dbus_arg_info
*src
, unsigned n
) {
497 pa_dbus_arg_info
*dst
;
505 dst
= pa_xnew0(pa_dbus_arg_info
, n
);
507 for (i
= 0; i
< n
; ++i
) {
508 dst
[i
].name
= pa_xstrdup(src
[i
].name
);
509 dst
[i
].type
= pa_xstrdup(src
[i
].type
);
510 dst
[i
].direction
= pa_xstrdup(src
[i
].direction
);
516 static pa_hashmap
*create_method_handlers(const pa_dbus_interface_info
*info
) {
517 pa_hashmap
*handlers
;
521 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
523 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
525 for (i
= 0; i
< info
->n_method_handlers
; ++i
) {
526 pa_dbus_method_handler
*h
= pa_xnew(pa_dbus_method_handler
, 1);
527 h
->method_name
= pa_xstrdup(info
->method_handlers
[i
].method_name
);
528 h
->arguments
= copy_args(info
->method_handlers
[i
].arguments
, info
->method_handlers
[i
].n_arguments
);
529 h
->n_arguments
= info
->method_handlers
[i
].n_arguments
;
530 h
->receive_cb
= info
->method_handlers
[i
].receive_cb
;
532 pa_hashmap_put(handlers
, h
->method_name
, h
);
538 static pa_hashmap
*create_property_handlers(const pa_dbus_interface_info
*info
) {
539 pa_hashmap
*handlers
;
543 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
545 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
547 for (i
= 0; i
< info
->n_property_handlers
; ++i
) {
548 pa_dbus_property_handler
*h
= pa_xnew(pa_dbus_property_handler
, 1);
549 h
->property_name
= pa_xstrdup(info
->property_handlers
[i
].property_name
);
550 h
->type
= pa_xstrdup(info
->property_handlers
[i
].type
);
551 h
->get_cb
= info
->property_handlers
[i
].get_cb
;
552 h
->set_cb
= info
->property_handlers
[i
].set_cb
;
554 pa_hashmap_put(handlers
, h
->property_name
, h
);
560 static pa_dbus_signal_info
*copy_signals(const pa_dbus_interface_info
*info
) {
561 pa_dbus_signal_info
*dst
;
566 if (info
->n_signals
== 0)
569 pa_assert(info
->signals
);
571 dst
= pa_xnew(pa_dbus_signal_info
, info
->n_signals
);
573 for (i
= 0; i
< info
->n_signals
; ++i
) {
574 dst
[i
].name
= pa_xstrdup(info
->signals
[i
].name
);
575 dst
[i
].arguments
= copy_args(info
->signals
[i
].arguments
, info
->signals
[i
].n_arguments
);
576 dst
[i
].n_arguments
= info
->signals
[i
].n_arguments
;
582 int pa_dbus_protocol_add_interface(pa_dbus_protocol
*p
,
584 const pa_dbus_interface_info
*info
,
586 struct object_entry
*obj_entry
;
587 struct interface_entry
*iface_entry
;
588 pa_bool_t obj_entry_created
= FALSE
;
593 pa_assert(info
->name
);
594 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
595 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
596 pa_assert(info
->get_all_properties_cb
|| info
->n_property_handlers
== 0);
597 pa_assert(info
->signals
|| info
->n_signals
== 0);
599 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
))) {
600 obj_entry
= pa_xnew(struct object_entry
, 1);
601 obj_entry
->path
= pa_xstrdup(path
);
602 obj_entry
->interfaces
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
603 obj_entry
->introspection
= NULL
;
605 pa_hashmap_put(p
->objects
, path
, obj_entry
);
606 obj_entry_created
= TRUE
;
609 if (pa_hashmap_get(obj_entry
->interfaces
, info
->name
) != NULL
)
610 goto fail
; /* The interface was already registered. */
612 iface_entry
= pa_xnew(struct interface_entry
, 1);
613 iface_entry
->name
= pa_xstrdup(info
->name
);
614 iface_entry
->method_handlers
= create_method_handlers(info
);
615 iface_entry
->property_handlers
= create_property_handlers(info
);
616 iface_entry
->get_all_properties_cb
= info
->get_all_properties_cb
;
617 iface_entry
->signals
= copy_signals(info
);
618 iface_entry
->n_signals
= info
->n_signals
;
619 iface_entry
->userdata
= userdata
;
620 pa_hashmap_put(obj_entry
->interfaces
, iface_entry
->name
, iface_entry
);
622 update_introspection(obj_entry
);
624 if (obj_entry_created
)
625 register_object(p
, obj_entry
);
630 if (obj_entry_created
) {
631 pa_hashmap_remove(p
->objects
, path
);
632 pa_dbus_protocol_unref(p
);
639 static void unregister_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
640 struct connection_entry
*conn_entry
;
644 pa_assert(obj_entry
);
646 while ((conn_entry
= pa_hashmap_iterate(p
->connections
, &state
, NULL
)))
647 pa_assert_se(dbus_connection_unregister_object_path(conn_entry
->connection
, obj_entry
->path
));
650 static void method_handler_free_cb(void *p
, void *userdata
) {
651 pa_dbus_method_handler
*h
= p
;
656 pa_xfree((char *) h
->method_name
);
658 for (i
= 0; i
< h
->n_arguments
; ++i
) {
659 pa_xfree((char *) h
->arguments
[i
].name
);
660 pa_xfree((char *) h
->arguments
[i
].type
);
661 pa_xfree((char *) h
->arguments
[i
].direction
);
664 pa_xfree((pa_dbus_arg_info
*) h
->arguments
);
667 static void property_handler_free_cb(void *p
, void *userdata
) {
668 pa_dbus_property_handler
*h
= p
;
672 pa_xfree((char *) h
->property_name
);
673 pa_xfree((char *) h
->type
);
678 int pa_dbus_protocol_remove_interface(pa_dbus_protocol
*p
, const char* path
, const char* interface
) {
679 struct object_entry
*obj_entry
;
680 struct interface_entry
*iface_entry
;
685 pa_assert(interface
);
687 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
)))
690 if (!(iface_entry
= pa_hashmap_remove(obj_entry
->interfaces
, interface
)))
693 update_introspection(obj_entry
);
695 pa_xfree(iface_entry
->name
);
696 pa_hashmap_free(iface_entry
->method_handlers
, method_handler_free_cb
, NULL
);
697 pa_hashmap_free(iface_entry
->property_handlers
, property_handler_free_cb
, NULL
);
699 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
702 pa_xfree((char *) iface_entry
->signals
[i
].name
);
704 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
) {
705 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].name
);
706 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].type
);
707 pa_assert(iface_entry
->signals
[i
].arguments
[j
].direction
== NULL
);
710 pa_xfree((pa_dbus_arg_info
*) iface_entry
->signals
[i
].arguments
);
713 pa_xfree(iface_entry
->signals
);
714 pa_xfree(iface_entry
);
716 if (pa_hashmap_isempty(obj_entry
->interfaces
)) {
717 unregister_object(p
, obj_entry
);
719 pa_hashmap_remove(p
->objects
, path
);
720 pa_xfree(obj_entry
->path
);
721 pa_hashmap_free(obj_entry
->interfaces
, NULL
, NULL
);
722 pa_xfree(obj_entry
->introspection
);
729 static void register_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
730 struct object_entry
*obj_entry
;
736 while ((obj_entry
= pa_hashmap_iterate(p
->objects
, &state
, NULL
)))
737 pa_assert_se(dbus_connection_register_object_path(conn
, obj_entry
->path
, &vtable
, p
));
740 int pa_dbus_protocol_register_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
, pa_client
*client
) {
741 struct connection_entry
*conn_entry
;
747 if (pa_hashmap_get(p
->connections
, conn
))
748 return -1; /* The connection was already registered. */
750 register_all_objects(p
, conn
);
752 conn_entry
= pa_xnew(struct connection_entry
, 1);
753 conn_entry
->connection
= dbus_connection_ref(conn
);
754 conn_entry
->client
= client
;
755 conn_entry
->listening_for_all_signals
= FALSE
;
756 conn_entry
->all_signals_objects
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
757 conn_entry
->listening_signals
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
759 pa_hashmap_put(p
->connections
, conn
, conn_entry
);
764 static void unregister_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
765 struct object_entry
*obj_entry
;
771 while ((obj_entry
= pa_hashmap_iterate(p
->objects
, &state
, NULL
)))
772 pa_assert_se(dbus_connection_unregister_object_path(conn
, obj_entry
->path
));
775 static void free_listened_object_name_cb(void *p
, void *userdata
) {
781 static void free_listening_signals_idxset_cb(void *p
, void *userdata
) {
786 pa_idxset_free(set
, free_listened_object_name_cb
, NULL
);
789 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
790 struct connection_entry
*conn_entry
;
795 if (!(conn_entry
= pa_hashmap_remove(p
->connections
, conn
)))
798 unregister_all_objects(p
, conn
);
800 dbus_connection_unref(conn_entry
->connection
);
801 pa_idxset_free(conn_entry
->all_signals_objects
, free_listened_object_name_cb
, NULL
);
802 pa_hashmap_free(conn_entry
->listening_signals
, free_listening_signals_idxset_cb
, NULL
);
803 pa_xfree(conn_entry
);
808 pa_client
*pa_dbus_protocol_get_client(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
809 struct connection_entry
*conn_entry
;
814 if (!(conn_entry
= pa_hashmap_get(p
->connections
, conn
)))
817 return conn_entry
->client
;
820 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
, char **objects
, unsigned n_objects
) {
821 struct connection_entry
*conn_entry
;
822 pa_idxset
*object_set
;
828 pa_assert(objects
|| n_objects
== 0);
830 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
832 /* all_signals_objects will either be emptied or replaced with new objects,
833 * so we empty it here unconditionally. If listening_for_all_signals is
834 * currently FALSE, the idxset is empty already. */
835 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
836 pa_xfree(object_path
);
839 conn_entry
->listening_for_all_signals
= FALSE
;
841 /* Replace the old object list with a new one. */
842 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
843 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
844 object_set
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
846 for (i
= 0; i
< n_objects
; ++i
)
847 pa_idxset_put(object_set
, pa_xstrdup(objects
[i
]), NULL
);
850 conn_entry
->listening_for_all_signals
= TRUE
;
852 /* We're not interested in individual signals anymore, so let's empty
853 * listening_signals. */
854 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
855 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
857 for (i
= 0; i
< n_objects
; ++i
)
858 pa_idxset_put(conn_entry
->all_signals_objects
, pa_xstrdup(objects
[i
]), NULL
);
862 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
) {
863 struct connection_entry
*conn_entry
;
864 pa_idxset
*object_set
;
869 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
872 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
873 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
878 conn_entry
->listening_for_all_signals
= FALSE
;
880 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
881 pa_xfree(object_path
);
883 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
884 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
888 void pa_dbus_protocol_send_signal(pa_dbus_protocol
*p
, DBusMessage
*signal
) {
889 struct connection_entry
*conn_entry
;
891 pa_idxset
*object_set
;
892 DBusMessage
*signal_copy
;
896 pa_assert(dbus_message_get_type(signal
) == DBUS_MESSAGE_TYPE_SIGNAL
);
898 /* XXX: We have to do some linear searching to find connections that want
899 * to receive the signal. This shouldn't be very significant performance
900 * problem, and adding an (object path, signal name) -> connection mapping
901 * would be likely to create substantial complexity. */
903 while ((conn_entry
= pa_hashmap_iterate(p
->connections
, &state
, NULL
))) {
905 if ((conn_entry
->listening_for_all_signals
/* Case 1: listening for all signals */
906 && (pa_idxset_get_by_data(conn_entry
->all_signals_objects
, dbus_message_get_path(signal
), NULL
)
907 || pa_idxset_isempty(conn_entry
->all_signals_objects
)))
909 || (!conn_entry
->listening_for_all_signals
/* Case 2: not listening for all signals */
910 && (object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
))
911 && (pa_idxset_get_by_data(object_set
, dbus_message_get_path(signal
), NULL
)
912 || pa_idxset_isempty(object_set
)))) {
914 pa_assert_se(signal_copy
= dbus_message_copy(signal
));
915 pa_assert_se(dbus_connection_send(conn_entry
->connection
, signal_copy
, NULL
));
916 dbus_message_unref(signal_copy
);
921 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol
*p
, unsigned *n
) {
922 const char **extensions
;
923 const char *ext_name
;
930 *n
= pa_idxset_size(p
->extensions
);
935 extensions
= pa_xnew(const char *, *n
);
937 while ((ext_name
= pa_idxset_iterate(p
->extensions
, &state
, NULL
))) {
938 extensions
[i
] = ext_name
;
945 int pa_dbus_protocol_register_extension(pa_dbus_protocol
*p
, const char *name
) {
951 internal_name
= pa_xstrdup(name
);
953 if (pa_idxset_put(p
->extensions
, internal_name
, NULL
) < 0) {
954 pa_xfree(internal_name
);
958 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED
], internal_name
);
963 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol
*p
, const char *name
) {
969 if (!(internal_name
= pa_idxset_remove_by_data(p
->extensions
, name
, NULL
)))
972 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED
], internal_name
);
974 pa_xfree(internal_name
);
979 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(hook
< PA_DBUS_PROTOCOL_HOOK_MAX
);
984 return pa_hook_connect(&p
->hooks
[hook
], prio
, cb
, data
);