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/dbus-util.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/shared.h>
35 #include <pulsecore/strbuf.h>
37 #include "protocol-dbus.h"
39 struct pa_dbus_protocol
{
43 pa_hashmap
*objects
; /* Object path -> struct object_entry */
44 pa_hashmap
*connections
; /* DBusConnection -> struct connection_entry */
45 pa_idxset
*extensions
; /* Strings */
47 pa_hook hooks
[PA_DBUS_PROTOCOL_HOOK_MAX
];
52 pa_hashmap
*interfaces
; /* Interface name -> struct interface_entry */
56 struct connection_entry
{
57 DBusConnection
*connection
;
60 pa_bool_t listening_for_all_signals
;
62 /* Contains object paths. If this is empty, then signals from all objects
63 * are accepted. Only used when listening_for_all_signals == TRUE. */
64 pa_idxset
*all_signals_objects
;
66 /* Signal name -> idxset. The idxsets contain object paths. If an idxset is
67 * empty, then that signal is accepted from all objects. Only used when
68 * listening_for_all_signals == FALSE. */
69 pa_hashmap
*listening_signals
;
72 struct interface_entry
{
74 pa_hashmap
*method_handlers
;
75 pa_hashmap
*property_handlers
;
76 pa_dbus_receive_cb_t get_all_properties_cb
;
77 pa_dbus_signal_info
*signals
;
82 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type
) {
84 char *runtime_path
= NULL
;
85 char *escaped_path
= NULL
;
87 switch (server_type
) {
88 case PA_SERVER_TYPE_USER
:
89 pa_assert_se((runtime_path
= pa_runtime_path(PA_DBUS_SOCKET_NAME
)));
90 pa_assert_se((escaped_path
= dbus_address_escape_value(runtime_path
)));
91 address
= pa_sprintf_malloc("unix:path=%s", escaped_path
);
94 case PA_SERVER_TYPE_SYSTEM
:
95 pa_assert_se((escaped_path
= dbus_address_escape_value(PA_DBUS_SYSTEM_SOCKET_PATH
)));
96 address
= pa_sprintf_malloc("unix:path=%s", escaped_path
);
99 case PA_SERVER_TYPE_NONE
:
100 address
= pa_xnew0(char, 1);
104 pa_assert_not_reached();
107 pa_xfree(runtime_path
);
108 pa_xfree(escaped_path
);
113 static pa_dbus_protocol
*dbus_protocol_new(pa_core
*c
) {
119 p
= pa_xnew(pa_dbus_protocol
, 1);
121 p
->core
= pa_core_ref(c
);
122 p
->objects
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
123 p
->connections
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
124 p
->extensions
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
126 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
127 pa_hook_init(&p
->hooks
[i
], p
);
129 pa_assert_se(pa_shared_set(c
, "dbus-protocol", p
) >= 0);
134 pa_dbus_protocol
* pa_dbus_protocol_get(pa_core
*c
) {
137 if ((p
= pa_shared_get(c
, "dbus-protocol")))
138 return pa_dbus_protocol_ref(p
);
140 return dbus_protocol_new(c
);
143 pa_dbus_protocol
* pa_dbus_protocol_ref(pa_dbus_protocol
*p
) {
145 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
152 void pa_dbus_protocol_unref(pa_dbus_protocol
*p
) {
156 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
158 if (PA_REFCNT_DEC(p
) > 0)
161 pa_assert(pa_hashmap_isempty(p
->objects
));
162 pa_assert(pa_hashmap_isempty(p
->connections
));
163 pa_assert(pa_idxset_isempty(p
->extensions
));
165 pa_hashmap_free(p
->objects
, NULL
, NULL
);
166 pa_hashmap_free(p
->connections
, NULL
, NULL
);
167 pa_idxset_free(p
->extensions
, NULL
, NULL
);
169 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
170 pa_hook_done(&p
->hooks
[i
]);
172 pa_assert_se(pa_shared_remove(p
->core
, "dbus-protocol") >= 0);
174 pa_core_unref(p
->core
);
179 static void update_introspection(struct object_entry
*oe
) {
181 void *interfaces_state
= NULL
;
182 struct interface_entry
*iface_entry
= NULL
;
186 buf
= pa_strbuf_new();
187 pa_strbuf_puts(buf
, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
);
188 pa_strbuf_puts(buf
, "<node>\n");
190 PA_HASHMAP_FOREACH(iface_entry
, oe
->interfaces
, interfaces_state
) {
191 pa_dbus_method_handler
*method_handler
;
192 pa_dbus_property_handler
*property_handler
;
193 void *handlers_state
= NULL
;
197 pa_strbuf_printf(buf
, " <interface name=\"%s\">\n", iface_entry
->name
);
199 PA_HASHMAP_FOREACH(method_handler
, iface_entry
->method_handlers
, handlers_state
) {
200 pa_strbuf_printf(buf
, " <method name=\"%s\">\n", method_handler
->method_name
);
202 for (i
= 0; i
< method_handler
->n_arguments
; ++i
)
203 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n",
204 method_handler
->arguments
[i
].name
,
205 method_handler
->arguments
[i
].type
,
206 method_handler
->arguments
[i
].direction
);
208 pa_strbuf_puts(buf
, " </method>\n");
211 handlers_state
= NULL
;
213 PA_HASHMAP_FOREACH(property_handler
, iface_entry
->property_handlers
, handlers_state
)
214 pa_strbuf_printf(buf
, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n",
215 property_handler
->property_name
,
216 property_handler
->type
,
217 property_handler
->get_cb
? (property_handler
->set_cb
? "readwrite" : "read") : "write");
219 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
220 pa_strbuf_printf(buf
, " <signal name=\"%s\">\n", iface_entry
->signals
[i
].name
);
222 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
)
223 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry
->signals
[i
].arguments
[j
].name
,
224 iface_entry
->signals
[i
].arguments
[j
].type
);
226 pa_strbuf_puts(buf
, " </signal>\n");
229 pa_strbuf_puts(buf
, " </interface>\n");
232 pa_strbuf_puts(buf
, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE
"\">\n"
233 " <method name=\"Introspect\">\n"
234 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
237 " <interface name=\"" DBUS_INTERFACE_PROPERTIES
"\">\n"
238 " <method name=\"Get\">\n"
239 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
240 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
241 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
243 " <method name=\"Set\">\n"
244 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
246 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
248 " <method name=\"GetAll\">\n"
249 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
250 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
254 pa_strbuf_puts(buf
, "</node>\n");
256 pa_xfree(oe
->introspection
);
257 oe
->introspection
= pa_strbuf_tostring_free(buf
);
265 PROPERTY_ACCESS_DENIED
,
268 INVALID_MESSAGE_ARGUMENTS
271 static enum find_result_t
find_handler_by_property(struct object_entry
*obj_entry
,
273 const char *property
,
274 struct interface_entry
**iface_entry
,
275 pa_dbus_property_handler
**property_handler
) {
278 pa_assert(obj_entry
);
281 pa_assert(iface_entry
);
282 pa_assert(property_handler
);
284 PA_HASHMAP_FOREACH(*iface_entry
, obj_entry
->interfaces
, state
) {
285 if ((*property_handler
= pa_hashmap_get((*iface_entry
)->property_handlers
, property
))) {
286 if (dbus_message_has_member(msg
, "Get"))
287 return (*property_handler
)->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
288 else if (dbus_message_has_member(msg
, "Set"))
289 return (*property_handler
)->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
291 pa_assert_not_reached();
295 return NO_SUCH_PROPERTY
;
298 static enum find_result_t
find_handler_by_method(struct object_entry
*obj_entry
,
300 struct interface_entry
**iface_entry
,
301 pa_dbus_method_handler
**method_handler
) {
304 pa_assert(obj_entry
);
306 pa_assert(iface_entry
);
307 pa_assert(method_handler
);
309 PA_HASHMAP_FOREACH(*iface_entry
, obj_entry
->interfaces
, state
) {
310 if ((*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, method
)))
314 return NO_SUCH_METHOD
;
317 static enum find_result_t
find_handler_from_properties_call(struct object_entry
*obj_entry
,
319 struct interface_entry
**iface_entry
,
320 pa_dbus_property_handler
**property_handler
,
321 const char **attempted_property
) {
322 const char *interface
;
324 pa_assert(obj_entry
);
326 pa_assert(iface_entry
);
328 if (dbus_message_has_member(msg
, "GetAll")) {
329 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_STRING
, &interface
, DBUS_TYPE_INVALID
))
330 return INVALID_MESSAGE_ARGUMENTS
;
333 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)))
334 return FOUND_GET_ALL
;
336 return NO_SUCH_METHOD
; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
339 pa_assert_se((*iface_entry
= pa_hashmap_first(obj_entry
->interfaces
)));
340 return FOUND_GET_ALL
;
343 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_STRING
, &interface
, DBUS_TYPE_STRING
, attempted_property
, DBUS_TYPE_INVALID
))
344 return INVALID_MESSAGE_ARGUMENTS
;
347 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)) &&
348 (*property_handler
= pa_hashmap_get((*iface_entry
)->property_handlers
, *attempted_property
))) {
349 if (dbus_message_has_member(msg
, "Get"))
350 return (*property_handler
)->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
351 else if (dbus_message_has_member(msg
, "Set"))
352 return (*property_handler
)->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
354 pa_assert_not_reached();
356 return NO_SUCH_PROPERTY
;
358 return find_handler_by_property(obj_entry
, msg
, *attempted_property
, iface_entry
, property_handler
);
362 static enum find_result_t
find_handler(struct object_entry
*obj_entry
,
364 struct interface_entry
**iface_entry
,
365 pa_dbus_method_handler
**method_handler
,
366 pa_dbus_property_handler
**property_handler
,
367 const char **attempted_property
) {
368 const char *interface
;
370 pa_assert(obj_entry
);
372 pa_assert(iface_entry
);
373 pa_assert(method_handler
);
374 pa_assert(property_handler
);
375 pa_assert(attempted_property
);
378 *method_handler
= NULL
;
380 if (dbus_message_has_interface(msg
, DBUS_INTERFACE_PROPERTIES
))
381 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
383 else if ((interface
= dbus_message_get_interface(msg
))) {
384 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)) &&
385 (*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, dbus_message_get_member(msg
))))
388 pa_log("Message has unknown interface or there's no method handler.");
389 return NO_SUCH_METHOD
;
392 } else { /* The method call doesn't contain an interface. */
393 if (dbus_message_has_member(msg
, "Get") || dbus_message_has_member(msg
, "Set") || dbus_message_has_member(msg
, "GetAll")) {
394 if (find_handler_by_method(obj_entry
, dbus_message_get_member(msg
), iface_entry
, method_handler
) == FOUND_METHOD
)
395 return FOUND_METHOD
; /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
397 /* Assume this is a .Properties call. */
398 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
400 } else /* This is not a .Properties call. */
401 return find_handler_by_method(obj_entry
, dbus_message_get_member(msg
), iface_entry
, method_handler
);
405 static DBusHandlerResult
handle_message_cb(DBusConnection
*connection
, DBusMessage
*message
, void *user_data
) {
406 pa_dbus_protocol
*p
= user_data
;
407 struct object_entry
*obj_entry
= NULL
;
408 struct interface_entry
*iface_entry
= NULL
;
409 pa_dbus_method_handler
*method_handler
= NULL
;
410 pa_dbus_property_handler
*property_handler
= NULL
;
411 const char *attempted_property
= NULL
;
413 pa_assert(connection
);
416 pa_assert(p
->objects
);
418 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
419 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
421 pa_log_debug("Received message: destination = %s, interface = %s, member = %s",
422 dbus_message_get_path(message
),
423 dbus_message_get_interface(message
),
424 dbus_message_get_member(message
));
426 pa_assert_se((obj_entry
= pa_hashmap_get(p
->objects
, dbus_message_get_path(message
))));
428 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") ||
429 (!dbus_message_get_interface(message
) && dbus_message_has_member(message
, "Introspect"))) {
430 pa_dbus_send_basic_value_reply(connection
, message
, DBUS_TYPE_STRING
, &obj_entry
->introspection
);
434 switch (find_handler(obj_entry
, message
, &iface_entry
, &method_handler
, &property_handler
, &attempted_property
)) {
436 method_handler
->receive_cb(connection
, message
, iface_entry
->userdata
);
439 case FOUND_GET_PROPERTY
:
440 property_handler
->get_cb(connection
, message
, iface_entry
->userdata
);
443 case FOUND_SET_PROPERTY
:
444 property_handler
->set_cb(connection
, message
, iface_entry
->userdata
);
448 if (iface_entry
->get_all_properties_cb
)
449 iface_entry
->get_all_properties_cb(connection
, message
, iface_entry
->userdata
);
450 /* TODO: Write an else branch where a dummy response is sent. */
453 case PROPERTY_ACCESS_DENIED
:
454 pa_dbus_send_error(connection
, message
, DBUS_ERROR_ACCESS_DENIED
, "%s access denied for property %s", dbus_message_get_member(message
), attempted_property
);
458 pa_dbus_send_error(connection
, message
, DBUS_ERROR_UNKNOWN_METHOD
, "%s: No such method", dbus_message_get_member(message
));
461 case NO_SUCH_PROPERTY
:
462 pa_dbus_send_error(connection
, message
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "%s: No such property", attempted_property
);
465 case INVALID_MESSAGE_ARGUMENTS
:
466 pa_dbus_send_error(connection
, message
, DBUS_ERROR_INVALID_ARGS
, "Invalid arguments for %s", dbus_message_get_member(message
));
470 pa_assert_not_reached();
474 return DBUS_HANDLER_RESULT_HANDLED
;
477 static DBusObjectPathVTable vtable
= {
478 .unregister_function
= NULL
,
479 .message_function
= handle_message_cb
,
480 .dbus_internal_pad1
= NULL
,
481 .dbus_internal_pad2
= NULL
,
482 .dbus_internal_pad3
= NULL
,
483 .dbus_internal_pad4
= NULL
486 static void register_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
487 struct connection_entry
*conn_entry
;
491 pa_assert(obj_entry
);
493 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
494 pa_assert_se(dbus_connection_register_object_path(conn_entry
->connection
, obj_entry
->path
, &vtable
, p
));
497 static pa_dbus_arg_info
*copy_args(const pa_dbus_arg_info
*src
, unsigned n
) {
498 pa_dbus_arg_info
*dst
;
506 dst
= pa_xnew0(pa_dbus_arg_info
, n
);
508 for (i
= 0; i
< n
; ++i
) {
509 dst
[i
].name
= pa_xstrdup(src
[i
].name
);
510 dst
[i
].type
= pa_xstrdup(src
[i
].type
);
511 dst
[i
].direction
= pa_xstrdup(src
[i
].direction
);
517 static pa_hashmap
*create_method_handlers(const pa_dbus_interface_info
*info
) {
518 pa_hashmap
*handlers
;
522 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
524 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
526 for (i
= 0; i
< info
->n_method_handlers
; ++i
) {
527 pa_dbus_method_handler
*h
= pa_xnew(pa_dbus_method_handler
, 1);
528 h
->method_name
= pa_xstrdup(info
->method_handlers
[i
].method_name
);
529 h
->arguments
= copy_args(info
->method_handlers
[i
].arguments
, info
->method_handlers
[i
].n_arguments
);
530 h
->n_arguments
= info
->method_handlers
[i
].n_arguments
;
531 h
->receive_cb
= info
->method_handlers
[i
].receive_cb
;
533 pa_hashmap_put(handlers
, h
->method_name
, h
);
539 static pa_hashmap
*create_property_handlers(const pa_dbus_interface_info
*info
) {
540 pa_hashmap
*handlers
;
544 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
546 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
548 for (i
= 0; i
< info
->n_property_handlers
; ++i
) {
549 pa_dbus_property_handler
*h
= pa_xnew(pa_dbus_property_handler
, 1);
550 h
->property_name
= pa_xstrdup(info
->property_handlers
[i
].property_name
);
551 h
->type
= pa_xstrdup(info
->property_handlers
[i
].type
);
552 h
->get_cb
= info
->property_handlers
[i
].get_cb
;
553 h
->set_cb
= info
->property_handlers
[i
].set_cb
;
555 pa_hashmap_put(handlers
, h
->property_name
, h
);
561 static pa_dbus_signal_info
*copy_signals(const pa_dbus_interface_info
*info
) {
562 pa_dbus_signal_info
*dst
;
567 if (info
->n_signals
== 0)
570 pa_assert(info
->signals
);
572 dst
= pa_xnew(pa_dbus_signal_info
, info
->n_signals
);
574 for (i
= 0; i
< info
->n_signals
; ++i
) {
575 dst
[i
].name
= pa_xstrdup(info
->signals
[i
].name
);
576 dst
[i
].arguments
= copy_args(info
->signals
[i
].arguments
, info
->signals
[i
].n_arguments
);
577 dst
[i
].n_arguments
= info
->signals
[i
].n_arguments
;
583 int pa_dbus_protocol_add_interface(pa_dbus_protocol
*p
,
585 const pa_dbus_interface_info
*info
,
587 struct object_entry
*obj_entry
;
588 struct interface_entry
*iface_entry
;
589 pa_bool_t obj_entry_created
= FALSE
;
594 pa_assert(info
->name
);
595 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
596 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
597 pa_assert(info
->get_all_properties_cb
|| info
->n_property_handlers
== 0);
598 pa_assert(info
->signals
|| info
->n_signals
== 0);
600 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
))) {
601 obj_entry
= pa_xnew(struct object_entry
, 1);
602 obj_entry
->path
= pa_xstrdup(path
);
603 obj_entry
->interfaces
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
604 obj_entry
->introspection
= NULL
;
606 pa_hashmap_put(p
->objects
, path
, obj_entry
);
607 obj_entry_created
= TRUE
;
610 if (pa_hashmap_get(obj_entry
->interfaces
, info
->name
) != NULL
)
611 goto fail
; /* The interface was already registered. */
613 iface_entry
= pa_xnew(struct interface_entry
, 1);
614 iface_entry
->name
= pa_xstrdup(info
->name
);
615 iface_entry
->method_handlers
= create_method_handlers(info
);
616 iface_entry
->property_handlers
= create_property_handlers(info
);
617 iface_entry
->get_all_properties_cb
= info
->get_all_properties_cb
;
618 iface_entry
->signals
= copy_signals(info
);
619 iface_entry
->n_signals
= info
->n_signals
;
620 iface_entry
->userdata
= userdata
;
621 pa_hashmap_put(obj_entry
->interfaces
, iface_entry
->name
, iface_entry
);
623 update_introspection(obj_entry
);
625 if (obj_entry_created
)
626 register_object(p
, obj_entry
);
628 pa_log_debug("Interface %s added for object %s", iface_entry
->name
, obj_entry
->path
);
633 if (obj_entry_created
) {
634 pa_hashmap_remove(p
->objects
, path
);
641 static void unregister_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
642 struct connection_entry
*conn_entry
;
646 pa_assert(obj_entry
);
648 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
649 pa_assert_se(dbus_connection_unregister_object_path(conn_entry
->connection
, obj_entry
->path
));
652 static void method_handler_free_cb(void *p
, void *userdata
) {
653 pa_dbus_method_handler
*h
= p
;
658 pa_xfree((char *) h
->method_name
);
660 for (i
= 0; i
< h
->n_arguments
; ++i
) {
661 pa_xfree((char *) h
->arguments
[i
].name
);
662 pa_xfree((char *) h
->arguments
[i
].type
);
663 pa_xfree((char *) h
->arguments
[i
].direction
);
666 pa_xfree((pa_dbus_arg_info
*) h
->arguments
);
669 static void property_handler_free_cb(void *p
, void *userdata
) {
670 pa_dbus_property_handler
*h
= p
;
674 pa_xfree((char *) h
->property_name
);
675 pa_xfree((char *) h
->type
);
680 int pa_dbus_protocol_remove_interface(pa_dbus_protocol
*p
, const char* path
, const char* interface
) {
681 struct object_entry
*obj_entry
;
682 struct interface_entry
*iface_entry
;
687 pa_assert(interface
);
689 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
)))
692 if (!(iface_entry
= pa_hashmap_remove(obj_entry
->interfaces
, interface
)))
695 update_introspection(obj_entry
);
697 pa_xfree(iface_entry
->name
);
698 pa_hashmap_free(iface_entry
->method_handlers
, method_handler_free_cb
, NULL
);
699 pa_hashmap_free(iface_entry
->property_handlers
, property_handler_free_cb
, NULL
);
701 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
704 pa_xfree((char *) iface_entry
->signals
[i
].name
);
706 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
) {
707 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].name
);
708 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].type
);
709 pa_assert(iface_entry
->signals
[i
].arguments
[j
].direction
== NULL
);
712 pa_xfree((pa_dbus_arg_info
*) iface_entry
->signals
[i
].arguments
);
715 pa_xfree(iface_entry
->signals
);
716 pa_xfree(iface_entry
);
718 if (pa_hashmap_isempty(obj_entry
->interfaces
)) {
719 unregister_object(p
, obj_entry
);
721 pa_hashmap_remove(p
->objects
, path
);
722 pa_xfree(obj_entry
->path
);
723 pa_hashmap_free(obj_entry
->interfaces
, NULL
, NULL
);
724 pa_xfree(obj_entry
->introspection
);
731 static void register_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
732 struct object_entry
*obj_entry
;
738 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
739 pa_assert_se(dbus_connection_register_object_path(conn
, obj_entry
->path
, &vtable
, p
));
742 int pa_dbus_protocol_register_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
, pa_client
*client
) {
743 struct connection_entry
*conn_entry
;
749 if (pa_hashmap_get(p
->connections
, conn
))
750 return -1; /* The connection was already registered. */
752 register_all_objects(p
, conn
);
754 conn_entry
= pa_xnew(struct connection_entry
, 1);
755 conn_entry
->connection
= dbus_connection_ref(conn
);
756 conn_entry
->client
= client
;
757 conn_entry
->listening_for_all_signals
= FALSE
;
758 conn_entry
->all_signals_objects
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
759 conn_entry
->listening_signals
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
761 pa_hashmap_put(p
->connections
, conn
, conn_entry
);
766 static void unregister_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
767 struct object_entry
*obj_entry
;
773 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
774 pa_assert_se(dbus_connection_unregister_object_path(conn
, obj_entry
->path
));
777 static void free_listened_object_name_cb(void *p
, void *userdata
) {
783 static void free_listening_signals_idxset_cb(void *p
, void *userdata
) {
788 pa_idxset_free(set
, free_listened_object_name_cb
, NULL
);
791 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
792 struct connection_entry
*conn_entry
;
797 if (!(conn_entry
= pa_hashmap_remove(p
->connections
, conn
)))
800 unregister_all_objects(p
, conn
);
802 dbus_connection_unref(conn_entry
->connection
);
803 pa_idxset_free(conn_entry
->all_signals_objects
, free_listened_object_name_cb
, NULL
);
804 pa_hashmap_free(conn_entry
->listening_signals
, free_listening_signals_idxset_cb
, NULL
);
805 pa_xfree(conn_entry
);
810 pa_client
*pa_dbus_protocol_get_client(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
811 struct connection_entry
*conn_entry
;
816 if (!(conn_entry
= pa_hashmap_get(p
->connections
, conn
)))
819 return conn_entry
->client
;
822 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
, char **objects
, unsigned n_objects
) {
823 struct connection_entry
*conn_entry
;
824 pa_idxset
*object_set
;
830 pa_assert(objects
|| n_objects
== 0);
832 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
834 /* all_signals_objects will either be emptied or replaced with new objects,
835 * so we empty it here unconditionally. If listening_for_all_signals is
836 * currently FALSE, the idxset is empty already. */
837 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
838 pa_xfree(object_path
);
841 conn_entry
->listening_for_all_signals
= FALSE
;
843 /* Replace the old object list with a new one. */
844 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
845 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
846 object_set
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
848 for (i
= 0; i
< n_objects
; ++i
)
849 pa_idxset_put(object_set
, pa_xstrdup(objects
[i
]), NULL
);
851 pa_hashmap_put(conn_entry
->listening_signals
, signal
, object_set
);
854 conn_entry
->listening_for_all_signals
= TRUE
;
856 /* We're not interested in individual signals anymore, so let's empty
857 * listening_signals. */
858 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
859 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
861 for (i
= 0; i
< n_objects
; ++i
)
862 pa_idxset_put(conn_entry
->all_signals_objects
, pa_xstrdup(objects
[i
]), NULL
);
866 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
) {
867 struct connection_entry
*conn_entry
;
868 pa_idxset
*object_set
;
873 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
876 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
877 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
882 conn_entry
->listening_for_all_signals
= FALSE
;
884 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
885 pa_xfree(object_path
);
887 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
888 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
892 void pa_dbus_protocol_send_signal(pa_dbus_protocol
*p
, DBusMessage
*signal
) {
893 struct connection_entry
*conn_entry
;
895 pa_idxset
*object_set
;
896 DBusMessage
*signal_copy
;
901 pa_assert(dbus_message_get_type(signal
) == DBUS_MESSAGE_TYPE_SIGNAL
);
902 pa_assert_se(dbus_message_get_interface(signal
));
903 pa_assert_se(dbus_message_get_member(signal
));
905 signal_string
= pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal
), dbus_message_get_member(signal
));
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
)))
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_string
))
914 && (pa_idxset_get_by_data(object_set
, dbus_message_get_path(signal
), NULL
)
915 || pa_idxset_isempty(object_set
)))) {
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
);
923 pa_xfree(signal_string
);
926 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol
*p
, unsigned *n
) {
927 const char **extensions
;
928 const char *ext_name
;
935 *n
= pa_idxset_size(p
->extensions
);
940 extensions
= pa_xnew(const char *, *n
);
942 while ((ext_name
= pa_idxset_iterate(p
->extensions
, &state
, NULL
)))
943 extensions
[i
++] = ext_name
;
948 int pa_dbus_protocol_register_extension(pa_dbus_protocol
*p
, const char *name
) {
954 internal_name
= pa_xstrdup(name
);
956 if (pa_idxset_put(p
->extensions
, internal_name
, NULL
) < 0) {
957 pa_xfree(internal_name
);
961 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED
], internal_name
);
966 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol
*p
, const char *name
) {
972 if (!(internal_name
= pa_idxset_remove_by_data(p
->extensions
, name
, NULL
)))
975 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED
], internal_name
);
977 pa_xfree(internal_name
);
982 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
) {
984 pa_assert(hook
< PA_DBUS_PROTOCOL_HOOK_MAX
);
987 return pa_hook_connect(&p
->hooks
[hook
], prio
, cb
, data
);