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);
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
);
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);
173 pa_core_unref(p
->core
);
178 static void update_introspection(struct object_entry
*oe
) {
180 void *interfaces_state
= NULL
;
181 struct interface_entry
*iface_entry
= NULL
;
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");
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
;
196 pa_strbuf_printf(buf
, " <interface name=\"%s\">\n", iface_entry
->name
);
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
);
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
);
206 pa_strbuf_puts(buf
, " </method>\n");
209 handlers_state
= NULL
;
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");
216 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
217 pa_strbuf_printf(buf
, " <signal name=\"%s\">\n", iface_entry
->signals
[i
].name
);
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
);
223 pa_strbuf_puts(buf
, " </signal>\n");
226 pa_strbuf_puts(buf
, " </interface>\n");
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"
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"
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"
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"
251 pa_strbuf_puts(buf
, "</node>\n");
253 pa_xfree(oe
->introspection
);
254 oe
->introspection
= pa_strbuf_tostring_free(buf
);
262 PROPERTY_ACCESS_DENIED
,
265 INVALID_MESSAGE_ARGUMENTS
268 static enum find_result_t
find_handler_by_property(struct object_entry
*obj_entry
,
270 const char *property
,
271 struct interface_entry
**iface_entry
,
272 pa_dbus_property_handler
**property_handler
) {
275 pa_assert(obj_entry
);
278 pa_assert(iface_entry
);
279 pa_assert(property_handler
);
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
;
288 pa_assert_not_reached();
292 return NO_SUCH_PROPERTY
;
295 static enum find_result_t
find_handler_by_method(struct object_entry
*obj_entry
,
297 struct interface_entry
**iface_entry
,
298 pa_dbus_method_handler
**method_handler
) {
301 pa_assert(obj_entry
);
303 pa_assert(iface_entry
);
304 pa_assert(method_handler
);
306 PA_HASHMAP_FOREACH(*iface_entry
, obj_entry
->interfaces
, state
) {
307 if ((*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, method
)))
311 return NO_SUCH_METHOD
;
314 static enum find_result_t
find_handler_from_properties_call(struct object_entry
*obj_entry
,
316 struct interface_entry
**iface_entry
,
317 pa_dbus_property_handler
**property_handler
,
318 const char **attempted_property
) {
319 const char *interface
;
321 pa_assert(obj_entry
);
323 pa_assert(iface_entry
);
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
;
330 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)))
331 return FOUND_GET_ALL
;
333 return NO_SUCH_METHOD
; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
336 pa_assert_se((*iface_entry
= pa_hashmap_first(obj_entry
->interfaces
)));
337 return FOUND_GET_ALL
;
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
;
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
;
351 pa_assert_not_reached();
353 return NO_SUCH_PROPERTY
;
355 return find_handler_by_property(obj_entry
, msg
, *attempted_property
, iface_entry
, property_handler
);
359 static enum find_result_t
find_handler(struct object_entry
*obj_entry
,
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
;
367 pa_assert(obj_entry
);
369 pa_assert(iface_entry
);
370 pa_assert(method_handler
);
371 pa_assert(property_handler
);
372 pa_assert(attempted_property
);
375 *method_handler
= NULL
;
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
);
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
))))
385 pa_log("Message has unknown interface or there's no method handler.");
386 return NO_SUCH_METHOD
;
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. */
394 /* Assume this is a .Properties call. */
395 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
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
);
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
;
411 pa_assert(connection
);
414 pa_assert(p
->objects
);
416 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
417 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
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
));
424 pa_assert_se((obj_entry
= pa_hashmap_get(p
->objects
, dbus_message_get_path(message
))));
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
));
435 switch (find_handler(obj_entry
, message
, &iface_entry
, &method_handler
, &property_handler
, &attempted_property
)) {
437 method_handler
->receive_cb(connection
, message
, iface_entry
->userdata
);
440 case FOUND_GET_PROPERTY
:
441 property_handler
->get_cb(connection
, message
, iface_entry
->userdata
);
444 case FOUND_SET_PROPERTY
:
445 property_handler
->set_cb(connection
, message
, iface_entry
->userdata
);
449 if (iface_entry
->get_all_properties_cb
)
450 iface_entry
->get_all_properties_cb(connection
, message
, iface_entry
->userdata
);
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
));
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
));
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
));
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
));
474 pa_assert_not_reached();
479 dbus_message_unref(reply
);
481 return DBUS_HANDLER_RESULT_HANDLED
;
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
493 static void register_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
494 struct connection_entry
*conn_entry
;
498 pa_assert(obj_entry
);
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
));
504 static pa_dbus_arg_info
*copy_args(const pa_dbus_arg_info
*src
, unsigned n
) {
505 pa_dbus_arg_info
*dst
;
513 dst
= pa_xnew0(pa_dbus_arg_info
, n
);
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
);
524 static pa_hashmap
*create_method_handlers(const pa_dbus_interface_info
*info
) {
525 pa_hashmap
*handlers
;
529 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
531 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
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
;
540 pa_hashmap_put(handlers
, h
->method_name
, h
);
546 static pa_hashmap
*create_property_handlers(const pa_dbus_interface_info
*info
) {
547 pa_hashmap
*handlers
;
551 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
553 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
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
;
562 pa_hashmap_put(handlers
, h
->property_name
, h
);
568 static pa_dbus_signal_info
*copy_signals(const pa_dbus_interface_info
*info
) {
569 pa_dbus_signal_info
*dst
;
574 if (info
->n_signals
== 0)
577 pa_assert(info
->signals
);
579 dst
= pa_xnew(pa_dbus_signal_info
, info
->n_signals
);
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
;
590 int pa_dbus_protocol_add_interface(pa_dbus_protocol
*p
,
592 const pa_dbus_interface_info
*info
,
594 struct object_entry
*obj_entry
;
595 struct interface_entry
*iface_entry
;
596 pa_bool_t obj_entry_created
= FALSE
;
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);
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
;
613 pa_hashmap_put(p
->objects
, path
, obj_entry
);
614 obj_entry_created
= TRUE
;
617 if (pa_hashmap_get(obj_entry
->interfaces
, info
->name
) != NULL
)
618 goto fail
; /* The interface was already registered. */
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
);
630 update_introspection(obj_entry
);
632 if (obj_entry_created
)
633 register_object(p
, obj_entry
);
635 pa_log_debug("Interface %s added for object %s", iface_entry
->name
, obj_entry
->path
);
640 if (obj_entry_created
) {
641 pa_hashmap_remove(p
->objects
, path
);
648 static void unregister_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
649 struct connection_entry
*conn_entry
;
653 pa_assert(obj_entry
);
655 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
656 pa_assert_se(dbus_connection_unregister_object_path(conn_entry
->connection
, obj_entry
->path
));
659 static void method_handler_free_cb(void *p
, void *userdata
) {
660 pa_dbus_method_handler
*h
= p
;
665 pa_xfree((char *) h
->method_name
);
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
);
673 pa_xfree((pa_dbus_arg_info
*) h
->arguments
);
676 static void property_handler_free_cb(void *p
, void *userdata
) {
677 pa_dbus_property_handler
*h
= p
;
681 pa_xfree((char *) h
->property_name
);
682 pa_xfree((char *) h
->type
);
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
;
694 pa_assert(interface
);
696 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
)))
699 if (!(iface_entry
= pa_hashmap_remove(obj_entry
->interfaces
, interface
)))
702 update_introspection(obj_entry
);
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
);
708 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
711 pa_xfree((char *) iface_entry
->signals
[i
].name
);
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
);
719 pa_xfree((pa_dbus_arg_info
*) iface_entry
->signals
[i
].arguments
);
722 pa_xfree(iface_entry
->signals
);
723 pa_xfree(iface_entry
);
725 if (pa_hashmap_isempty(obj_entry
->interfaces
)) {
726 unregister_object(p
, obj_entry
);
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
);
738 static void register_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
739 struct object_entry
*obj_entry
;
745 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
746 pa_assert_se(dbus_connection_register_object_path(conn
, obj_entry
->path
, &vtable
, p
));
749 int pa_dbus_protocol_register_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
, pa_client
*client
) {
750 struct connection_entry
*conn_entry
;
756 if (pa_hashmap_get(p
->connections
, conn
))
757 return -1; /* The connection was already registered. */
759 register_all_objects(p
, conn
);
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
);
768 pa_hashmap_put(p
->connections
, conn
, conn_entry
);
773 static void unregister_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
774 struct object_entry
*obj_entry
;
780 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
781 pa_assert_se(dbus_connection_unregister_object_path(conn
, obj_entry
->path
));
784 static void free_listened_object_name_cb(void *p
, void *userdata
) {
790 static void free_listening_signals_idxset_cb(void *p
, void *userdata
) {
795 pa_idxset_free(set
, free_listened_object_name_cb
, NULL
);
798 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
799 struct connection_entry
*conn_entry
;
804 if (!(conn_entry
= pa_hashmap_remove(p
->connections
, conn
)))
807 unregister_all_objects(p
, conn
);
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
);
817 pa_client
*pa_dbus_protocol_get_client(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
818 struct connection_entry
*conn_entry
;
823 if (!(conn_entry
= pa_hashmap_get(p
->connections
, conn
)))
826 return conn_entry
->client
;
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
;
837 pa_assert(objects
|| n_objects
== 0);
839 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
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
);
848 conn_entry
->listening_for_all_signals
= FALSE
;
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
);
855 for (i
= 0; i
< n_objects
; ++i
)
856 pa_idxset_put(object_set
, pa_xstrdup(objects
[i
]), NULL
);
859 conn_entry
->listening_for_all_signals
= TRUE
;
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
);
866 for (i
= 0; i
< n_objects
; ++i
)
867 pa_idxset_put(conn_entry
->all_signals_objects
, pa_xstrdup(objects
[i
]), NULL
);
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
;
878 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
881 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
882 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
887 conn_entry
->listening_for_all_signals
= FALSE
;
889 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
890 pa_xfree(object_path
);
892 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
893 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
897 void pa_dbus_protocol_send_signal(pa_dbus_protocol
*p
, DBusMessage
*signal
) {
898 struct connection_entry
*conn_entry
;
900 pa_idxset
*object_set
;
901 DBusMessage
*signal_copy
;
905 pa_assert(dbus_message_get_type(signal
) == DBUS_MESSAGE_TYPE_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
))
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
);
924 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol
*p
, unsigned *n
) {
925 const char **extensions
;
926 const char *ext_name
;
933 *n
= pa_idxset_size(p
->extensions
);
938 extensions
= pa_xnew(const char *, *n
);
940 while ((ext_name
= pa_idxset_iterate(p
->extensions
, &state
, NULL
)))
941 extensions
[i
++] = ext_name
;
946 int pa_dbus_protocol_register_extension(pa_dbus_protocol
*p
, const char *name
) {
952 internal_name
= pa_xstrdup(name
);
954 if (pa_idxset_put(p
->extensions
, internal_name
, NULL
) < 0) {
955 pa_xfree(internal_name
);
959 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED
], internal_name
);
964 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol
*p
, const char *name
) {
970 if (!(internal_name
= pa_idxset_remove_by_data(p
->extensions
, name
, NULL
)))
973 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED
], internal_name
);
975 pa_xfree(internal_name
);
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
) {
982 pa_assert(hook
< PA_DBUS_PROTOCOL_HOOK_MAX
);
985 return pa_hook_connect(&p
->hooks
[hook
], prio
, cb
, data
);