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",
203 method_handler
->arguments
[i
].name
,
204 method_handler
->arguments
[i
].type
,
205 method_handler
->arguments
[i
].direction
);
207 pa_strbuf_puts(buf
, " </method>\n");
210 handlers_state
= NULL
;
212 PA_HASHMAP_FOREACH(property_handler
, iface_entry
->property_handlers
, handlers_state
)
213 pa_strbuf_printf(buf
, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n",
214 property_handler
->property_name
,
215 property_handler
->type
,
216 property_handler
->get_cb
? (property_handler
->set_cb
? "readwrite" : "read") : "write");
218 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
219 pa_strbuf_printf(buf
, " <signal name=\"%s\">\n", iface_entry
->signals
[i
].name
);
221 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
)
222 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry
->signals
[i
].arguments
[j
].name
,
223 iface_entry
->signals
[i
].arguments
[j
].type
);
225 pa_strbuf_puts(buf
, " </signal>\n");
228 pa_strbuf_puts(buf
, " </interface>\n");
231 pa_strbuf_puts(buf
, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE
"\">\n"
232 " <method name=\"Introspect\">\n"
233 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
236 " <interface name=\"" DBUS_INTERFACE_PROPERTIES
"\">\n"
237 " <method name=\"Get\">\n"
238 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
239 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
240 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
242 " <method name=\"Set\">\n"
243 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
244 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
247 " <method name=\"GetAll\">\n"
248 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
249 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
253 pa_strbuf_puts(buf
, "</node>\n");
255 pa_xfree(oe
->introspection
);
256 oe
->introspection
= pa_strbuf_tostring_free(buf
);
264 PROPERTY_ACCESS_DENIED
,
267 INVALID_MESSAGE_ARGUMENTS
270 static enum find_result_t
find_handler_by_property(struct object_entry
*obj_entry
,
272 const char *property
,
273 struct interface_entry
**iface_entry
,
274 pa_dbus_property_handler
**property_handler
) {
277 pa_assert(obj_entry
);
280 pa_assert(iface_entry
);
281 pa_assert(property_handler
);
283 PA_HASHMAP_FOREACH(*iface_entry
, obj_entry
->interfaces
, state
) {
284 if ((*property_handler
= pa_hashmap_get((*iface_entry
)->property_handlers
, property
))) {
285 if (dbus_message_has_member(msg
, "Get"))
286 return (*property_handler
)->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
287 else if (dbus_message_has_member(msg
, "Set"))
288 return (*property_handler
)->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
290 pa_assert_not_reached();
294 return NO_SUCH_PROPERTY
;
297 static enum find_result_t
find_handler_by_method(struct object_entry
*obj_entry
,
299 struct interface_entry
**iface_entry
,
300 pa_dbus_method_handler
**method_handler
) {
303 pa_assert(obj_entry
);
305 pa_assert(iface_entry
);
306 pa_assert(method_handler
);
308 PA_HASHMAP_FOREACH(*iface_entry
, obj_entry
->interfaces
, state
) {
309 if ((*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, method
)))
313 return NO_SUCH_METHOD
;
316 static enum find_result_t
find_handler_from_properties_call(struct object_entry
*obj_entry
,
318 struct interface_entry
**iface_entry
,
319 pa_dbus_property_handler
**property_handler
,
320 const char **attempted_property
) {
321 const char *interface
;
323 pa_assert(obj_entry
);
325 pa_assert(iface_entry
);
327 if (dbus_message_has_member(msg
, "GetAll")) {
328 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_STRING
, &interface
, DBUS_TYPE_INVALID
))
329 return INVALID_MESSAGE_ARGUMENTS
;
332 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)))
333 return FOUND_GET_ALL
;
335 return NO_SUCH_METHOD
; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
338 pa_assert_se((*iface_entry
= pa_hashmap_first(obj_entry
->interfaces
)));
339 return FOUND_GET_ALL
;
342 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_STRING
, &interface
, DBUS_TYPE_STRING
, attempted_property
, DBUS_TYPE_INVALID
))
343 return INVALID_MESSAGE_ARGUMENTS
;
346 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)) &&
347 (*property_handler
= pa_hashmap_get((*iface_entry
)->property_handlers
, *attempted_property
))) {
348 if (dbus_message_has_member(msg
, "Get"))
349 return (*property_handler
)->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
350 else if (dbus_message_has_member(msg
, "Set"))
351 return (*property_handler
)->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
353 pa_assert_not_reached();
355 return NO_SUCH_PROPERTY
;
357 return find_handler_by_property(obj_entry
, msg
, *attempted_property
, iface_entry
, property_handler
);
361 static enum find_result_t
find_handler(struct object_entry
*obj_entry
,
363 struct interface_entry
**iface_entry
,
364 pa_dbus_method_handler
**method_handler
,
365 pa_dbus_property_handler
**property_handler
,
366 const char **attempted_property
) {
367 const char *interface
;
369 pa_assert(obj_entry
);
371 pa_assert(iface_entry
);
372 pa_assert(method_handler
);
373 pa_assert(property_handler
);
374 pa_assert(attempted_property
);
377 *method_handler
= NULL
;
379 if (dbus_message_has_interface(msg
, DBUS_INTERFACE_PROPERTIES
))
380 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
382 else if ((interface
= dbus_message_get_interface(msg
))) {
383 if ((*iface_entry
= pa_hashmap_get(obj_entry
->interfaces
, interface
)) &&
384 (*method_handler
= pa_hashmap_get((*iface_entry
)->method_handlers
, dbus_message_get_member(msg
))))
387 pa_log("Message has unknown interface or there's no method handler.");
388 return NO_SUCH_METHOD
;
391 } else { /* The method call doesn't contain an interface. */
392 if (dbus_message_has_member(msg
, "Get") || dbus_message_has_member(msg
, "Set") || dbus_message_has_member(msg
, "GetAll")) {
393 if (find_handler_by_method(obj_entry
, dbus_message_get_member(msg
), iface_entry
, method_handler
) == FOUND_METHOD
)
394 return FOUND_METHOD
; /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
396 /* Assume this is a .Properties call. */
397 return find_handler_from_properties_call(obj_entry
, msg
, iface_entry
, property_handler
, attempted_property
);
399 } else /* This is not a .Properties call. */
400 return find_handler_by_method(obj_entry
, dbus_message_get_member(msg
), iface_entry
, method_handler
);
404 static DBusHandlerResult
handle_message_cb(DBusConnection
*connection
, DBusMessage
*message
, void *user_data
) {
405 pa_dbus_protocol
*p
= user_data
;
406 struct object_entry
*obj_entry
= NULL
;
407 struct interface_entry
*iface_entry
= NULL
;
408 pa_dbus_method_handler
*method_handler
= NULL
;
409 pa_dbus_property_handler
*property_handler
= NULL
;
410 const char *attempted_property
= NULL
;
411 DBusMessage
*reply
= NULL
;
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_assert_se((reply
= dbus_message_new_method_return(message
)));
431 pa_assert_se(dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &obj_entry
->introspection
, DBUS_TYPE_INVALID
));
432 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
437 switch (find_handler(obj_entry
, message
, &iface_entry
, &method_handler
, &property_handler
, &attempted_property
)) {
439 method_handler
->receive_cb(connection
, message
, iface_entry
->userdata
);
442 case FOUND_GET_PROPERTY
:
443 property_handler
->get_cb(connection
, message
, iface_entry
->userdata
);
446 case FOUND_SET_PROPERTY
:
447 property_handler
->set_cb(connection
, message
, iface_entry
->userdata
);
451 if (iface_entry
->get_all_properties_cb
)
452 iface_entry
->get_all_properties_cb(connection
, message
, iface_entry
->userdata
);
455 case PROPERTY_ACCESS_DENIED
:
456 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
)));
457 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
461 pa_assert_se((reply
= dbus_message_new_error_printf(message
, DBUS_ERROR_UNKNOWN_METHOD
, "%s: No such method", dbus_message_get_member(message
))));
462 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
465 case NO_SUCH_PROPERTY
:
466 pa_assert_se((reply
= dbus_message_new_error_printf(message
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "%s: No such property", attempted_property
)));
467 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
470 case INVALID_MESSAGE_ARGUMENTS
:
471 pa_assert_se((reply
= dbus_message_new_error_printf(message
, DBUS_ERROR_INVALID_ARGS
, "Invalid arguments for %s", dbus_message_get_member(message
))));
472 pa_assert_se(dbus_connection_send(connection
, reply
, NULL
));
476 pa_assert_not_reached();
481 dbus_message_unref(reply
);
483 return DBUS_HANDLER_RESULT_HANDLED
;
486 static DBusObjectPathVTable vtable
= {
487 .unregister_function
= NULL
,
488 .message_function
= handle_message_cb
,
489 .dbus_internal_pad1
= NULL
,
490 .dbus_internal_pad2
= NULL
,
491 .dbus_internal_pad3
= NULL
,
492 .dbus_internal_pad4
= NULL
495 static void register_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
496 struct connection_entry
*conn_entry
;
500 pa_assert(obj_entry
);
502 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
503 pa_assert_se(dbus_connection_register_object_path(conn_entry
->connection
, obj_entry
->path
, &vtable
, p
));
506 static pa_dbus_arg_info
*copy_args(const pa_dbus_arg_info
*src
, unsigned n
) {
507 pa_dbus_arg_info
*dst
;
515 dst
= pa_xnew0(pa_dbus_arg_info
, n
);
517 for (i
= 0; i
< n
; ++i
) {
518 dst
[i
].name
= pa_xstrdup(src
[i
].name
);
519 dst
[i
].type
= pa_xstrdup(src
[i
].type
);
520 dst
[i
].direction
= pa_xstrdup(src
[i
].direction
);
526 static pa_hashmap
*create_method_handlers(const pa_dbus_interface_info
*info
) {
527 pa_hashmap
*handlers
;
531 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
533 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
535 for (i
= 0; i
< info
->n_method_handlers
; ++i
) {
536 pa_dbus_method_handler
*h
= pa_xnew(pa_dbus_method_handler
, 1);
537 h
->method_name
= pa_xstrdup(info
->method_handlers
[i
].method_name
);
538 h
->arguments
= copy_args(info
->method_handlers
[i
].arguments
, info
->method_handlers
[i
].n_arguments
);
539 h
->n_arguments
= info
->method_handlers
[i
].n_arguments
;
540 h
->receive_cb
= info
->method_handlers
[i
].receive_cb
;
542 pa_hashmap_put(handlers
, h
->method_name
, h
);
548 static pa_hashmap
*create_property_handlers(const pa_dbus_interface_info
*info
) {
549 pa_hashmap
*handlers
;
553 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
555 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
557 for (i
= 0; i
< info
->n_property_handlers
; ++i
) {
558 pa_dbus_property_handler
*h
= pa_xnew(pa_dbus_property_handler
, 1);
559 h
->property_name
= pa_xstrdup(info
->property_handlers
[i
].property_name
);
560 h
->type
= pa_xstrdup(info
->property_handlers
[i
].type
);
561 h
->get_cb
= info
->property_handlers
[i
].get_cb
;
562 h
->set_cb
= info
->property_handlers
[i
].set_cb
;
564 pa_hashmap_put(handlers
, h
->property_name
, h
);
570 static pa_dbus_signal_info
*copy_signals(const pa_dbus_interface_info
*info
) {
571 pa_dbus_signal_info
*dst
;
576 if (info
->n_signals
== 0)
579 pa_assert(info
->signals
);
581 dst
= pa_xnew(pa_dbus_signal_info
, info
->n_signals
);
583 for (i
= 0; i
< info
->n_signals
; ++i
) {
584 dst
[i
].name
= pa_xstrdup(info
->signals
[i
].name
);
585 dst
[i
].arguments
= copy_args(info
->signals
[i
].arguments
, info
->signals
[i
].n_arguments
);
586 dst
[i
].n_arguments
= info
->signals
[i
].n_arguments
;
592 int pa_dbus_protocol_add_interface(pa_dbus_protocol
*p
,
594 const pa_dbus_interface_info
*info
,
596 struct object_entry
*obj_entry
;
597 struct interface_entry
*iface_entry
;
598 pa_bool_t obj_entry_created
= FALSE
;
603 pa_assert(info
->name
);
604 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
605 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
606 pa_assert(info
->get_all_properties_cb
|| info
->n_property_handlers
== 0);
607 pa_assert(info
->signals
|| info
->n_signals
== 0);
609 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
))) {
610 obj_entry
= pa_xnew(struct object_entry
, 1);
611 obj_entry
->path
= pa_xstrdup(path
);
612 obj_entry
->interfaces
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
613 obj_entry
->introspection
= NULL
;
615 pa_hashmap_put(p
->objects
, path
, obj_entry
);
616 obj_entry_created
= TRUE
;
619 if (pa_hashmap_get(obj_entry
->interfaces
, info
->name
) != NULL
)
620 goto fail
; /* The interface was already registered. */
622 iface_entry
= pa_xnew(struct interface_entry
, 1);
623 iface_entry
->name
= pa_xstrdup(info
->name
);
624 iface_entry
->method_handlers
= create_method_handlers(info
);
625 iface_entry
->property_handlers
= create_property_handlers(info
);
626 iface_entry
->get_all_properties_cb
= info
->get_all_properties_cb
;
627 iface_entry
->signals
= copy_signals(info
);
628 iface_entry
->n_signals
= info
->n_signals
;
629 iface_entry
->userdata
= userdata
;
630 pa_hashmap_put(obj_entry
->interfaces
, iface_entry
->name
, iface_entry
);
632 update_introspection(obj_entry
);
634 if (obj_entry_created
)
635 register_object(p
, obj_entry
);
637 pa_log_debug("Interface %s added for object %s", iface_entry
->name
, obj_entry
->path
);
642 if (obj_entry_created
) {
643 pa_hashmap_remove(p
->objects
, path
);
650 static void unregister_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
651 struct connection_entry
*conn_entry
;
655 pa_assert(obj_entry
);
657 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
658 pa_assert_se(dbus_connection_unregister_object_path(conn_entry
->connection
, obj_entry
->path
));
661 static void method_handler_free_cb(void *p
, void *userdata
) {
662 pa_dbus_method_handler
*h
= p
;
667 pa_xfree((char *) h
->method_name
);
669 for (i
= 0; i
< h
->n_arguments
; ++i
) {
670 pa_xfree((char *) h
->arguments
[i
].name
);
671 pa_xfree((char *) h
->arguments
[i
].type
);
672 pa_xfree((char *) h
->arguments
[i
].direction
);
675 pa_xfree((pa_dbus_arg_info
*) h
->arguments
);
678 static void property_handler_free_cb(void *p
, void *userdata
) {
679 pa_dbus_property_handler
*h
= p
;
683 pa_xfree((char *) h
->property_name
);
684 pa_xfree((char *) h
->type
);
689 int pa_dbus_protocol_remove_interface(pa_dbus_protocol
*p
, const char* path
, const char* interface
) {
690 struct object_entry
*obj_entry
;
691 struct interface_entry
*iface_entry
;
696 pa_assert(interface
);
698 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
)))
701 if (!(iface_entry
= pa_hashmap_remove(obj_entry
->interfaces
, interface
)))
704 update_introspection(obj_entry
);
706 pa_xfree(iface_entry
->name
);
707 pa_hashmap_free(iface_entry
->method_handlers
, method_handler_free_cb
, NULL
);
708 pa_hashmap_free(iface_entry
->property_handlers
, property_handler_free_cb
, NULL
);
710 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
713 pa_xfree((char *) iface_entry
->signals
[i
].name
);
715 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
) {
716 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].name
);
717 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].type
);
718 pa_assert(iface_entry
->signals
[i
].arguments
[j
].direction
== NULL
);
721 pa_xfree((pa_dbus_arg_info
*) iface_entry
->signals
[i
].arguments
);
724 pa_xfree(iface_entry
->signals
);
725 pa_xfree(iface_entry
);
727 if (pa_hashmap_isempty(obj_entry
->interfaces
)) {
728 unregister_object(p
, obj_entry
);
730 pa_hashmap_remove(p
->objects
, path
);
731 pa_xfree(obj_entry
->path
);
732 pa_hashmap_free(obj_entry
->interfaces
, NULL
, NULL
);
733 pa_xfree(obj_entry
->introspection
);
740 static void register_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
741 struct object_entry
*obj_entry
;
747 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
748 pa_assert_se(dbus_connection_register_object_path(conn
, obj_entry
->path
, &vtable
, p
));
751 int pa_dbus_protocol_register_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
, pa_client
*client
) {
752 struct connection_entry
*conn_entry
;
758 if (pa_hashmap_get(p
->connections
, conn
))
759 return -1; /* The connection was already registered. */
761 register_all_objects(p
, conn
);
763 conn_entry
= pa_xnew(struct connection_entry
, 1);
764 conn_entry
->connection
= dbus_connection_ref(conn
);
765 conn_entry
->client
= client
;
766 conn_entry
->listening_for_all_signals
= FALSE
;
767 conn_entry
->all_signals_objects
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
768 conn_entry
->listening_signals
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
770 pa_hashmap_put(p
->connections
, conn
, conn_entry
);
775 static void unregister_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
776 struct object_entry
*obj_entry
;
782 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
783 pa_assert_se(dbus_connection_unregister_object_path(conn
, obj_entry
->path
));
786 static void free_listened_object_name_cb(void *p
, void *userdata
) {
792 static void free_listening_signals_idxset_cb(void *p
, void *userdata
) {
797 pa_idxset_free(set
, free_listened_object_name_cb
, NULL
);
800 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
801 struct connection_entry
*conn_entry
;
806 if (!(conn_entry
= pa_hashmap_remove(p
->connections
, conn
)))
809 unregister_all_objects(p
, conn
);
811 dbus_connection_unref(conn_entry
->connection
);
812 pa_idxset_free(conn_entry
->all_signals_objects
, free_listened_object_name_cb
, NULL
);
813 pa_hashmap_free(conn_entry
->listening_signals
, free_listening_signals_idxset_cb
, NULL
);
814 pa_xfree(conn_entry
);
819 pa_client
*pa_dbus_protocol_get_client(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
820 struct connection_entry
*conn_entry
;
825 if (!(conn_entry
= pa_hashmap_get(p
->connections
, conn
)))
828 return conn_entry
->client
;
831 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
, char **objects
, unsigned n_objects
) {
832 struct connection_entry
*conn_entry
;
833 pa_idxset
*object_set
;
839 pa_assert(objects
|| n_objects
== 0);
841 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
843 /* all_signals_objects will either be emptied or replaced with new objects,
844 * so we empty it here unconditionally. If listening_for_all_signals is
845 * currently FALSE, the idxset is empty already. */
846 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
847 pa_xfree(object_path
);
850 conn_entry
->listening_for_all_signals
= FALSE
;
852 /* Replace the old object list with a new one. */
853 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
854 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
855 object_set
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
857 for (i
= 0; i
< n_objects
; ++i
)
858 pa_idxset_put(object_set
, pa_xstrdup(objects
[i
]), NULL
);
861 conn_entry
->listening_for_all_signals
= TRUE
;
863 /* We're not interested in individual signals anymore, so let's empty
864 * listening_signals. */
865 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
866 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
868 for (i
= 0; i
< n_objects
; ++i
)
869 pa_idxset_put(conn_entry
->all_signals_objects
, pa_xstrdup(objects
[i
]), NULL
);
873 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
) {
874 struct connection_entry
*conn_entry
;
875 pa_idxset
*object_set
;
880 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
883 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
884 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
889 conn_entry
->listening_for_all_signals
= FALSE
;
891 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
892 pa_xfree(object_path
);
894 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
895 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
899 void pa_dbus_protocol_send_signal(pa_dbus_protocol
*p
, DBusMessage
*signal
) {
900 struct connection_entry
*conn_entry
;
902 pa_idxset
*object_set
;
903 DBusMessage
*signal_copy
;
907 pa_assert(dbus_message_get_type(signal
) == DBUS_MESSAGE_TYPE_SIGNAL
);
909 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
) {
910 if ((conn_entry
->listening_for_all_signals
/* Case 1: listening for all signals */
911 && (pa_idxset_get_by_data(conn_entry
->all_signals_objects
, dbus_message_get_path(signal
), NULL
)
912 || pa_idxset_isempty(conn_entry
->all_signals_objects
)))
914 || (!conn_entry
->listening_for_all_signals
/* Case 2: not listening for all signals */
915 && (object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
))
916 && (pa_idxset_get_by_data(object_set
, dbus_message_get_path(signal
), NULL
)
917 || pa_idxset_isempty(object_set
)))) {
919 pa_assert_se(signal_copy
= dbus_message_copy(signal
));
920 pa_assert_se(dbus_connection_send(conn_entry
->connection
, signal_copy
, NULL
));
921 dbus_message_unref(signal_copy
);
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
);