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 -> signal paths entry. The entries contain object paths. If
67 * a path set is empty, then that signal is accepted from all objects. This
68 * variable is only used when listening_for_all_signals == FALSE. */
69 pa_hashmap
*listening_signals
;
72 /* Only used in connection entries' listening_signals hashmap. */
73 struct signal_paths_entry
{
78 struct interface_entry
{
80 pa_hashmap
*method_handlers
;
81 pa_hashmap
*method_signatures
; /* Derived from method_handlers. Contains only "in" arguments. */
82 pa_hashmap
*property_handlers
;
83 pa_dbus_receive_cb_t get_all_properties_cb
;
84 pa_dbus_signal_info
*signals
;
89 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type
) {
91 char *runtime_path
= NULL
;
92 char *escaped_path
= NULL
;
94 switch (server_type
) {
95 case PA_SERVER_TYPE_USER
:
96 pa_assert_se((runtime_path
= pa_runtime_path(PA_DBUS_SOCKET_NAME
)));
97 pa_assert_se((escaped_path
= dbus_address_escape_value(runtime_path
)));
98 address
= pa_sprintf_malloc("unix:path=%s", escaped_path
);
101 case PA_SERVER_TYPE_SYSTEM
:
102 pa_assert_se((escaped_path
= dbus_address_escape_value(PA_DBUS_SYSTEM_SOCKET_PATH
)));
103 address
= pa_sprintf_malloc("unix:path=%s", escaped_path
);
106 case PA_SERVER_TYPE_NONE
:
107 address
= pa_xnew0(char, 1);
111 pa_assert_not_reached();
114 pa_xfree(runtime_path
);
115 dbus_free(escaped_path
);
120 static pa_dbus_protocol
*dbus_protocol_new(pa_core
*c
) {
126 p
= pa_xnew(pa_dbus_protocol
, 1);
129 p
->objects
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
130 p
->connections
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
131 p
->extensions
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
133 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
134 pa_hook_init(&p
->hooks
[i
], p
);
136 pa_assert_se(pa_shared_set(c
, "dbus-protocol", p
) >= 0);
141 pa_dbus_protocol
* pa_dbus_protocol_get(pa_core
*c
) {
144 if ((p
= pa_shared_get(c
, "dbus-protocol")))
145 return pa_dbus_protocol_ref(p
);
147 return dbus_protocol_new(c
);
150 pa_dbus_protocol
* pa_dbus_protocol_ref(pa_dbus_protocol
*p
) {
152 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
159 void pa_dbus_protocol_unref(pa_dbus_protocol
*p
) {
163 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
165 if (PA_REFCNT_DEC(p
) > 0)
168 pa_assert(pa_hashmap_isempty(p
->objects
));
169 pa_assert(pa_hashmap_isempty(p
->connections
));
170 pa_assert(pa_idxset_isempty(p
->extensions
));
172 pa_hashmap_free(p
->objects
, NULL
, NULL
);
173 pa_hashmap_free(p
->connections
, NULL
, NULL
);
174 pa_idxset_free(p
->extensions
, NULL
, NULL
);
176 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
177 pa_hook_done(&p
->hooks
[i
]);
179 pa_assert_se(pa_shared_remove(p
->core
, "dbus-protocol") >= 0);
184 static void update_introspection(struct object_entry
*oe
) {
186 void *interfaces_state
= NULL
;
187 struct interface_entry
*iface_entry
= NULL
;
191 buf
= pa_strbuf_new();
192 pa_strbuf_puts(buf
, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
);
193 pa_strbuf_puts(buf
, "<node>\n");
195 PA_HASHMAP_FOREACH(iface_entry
, oe
->interfaces
, interfaces_state
) {
196 pa_dbus_method_handler
*method_handler
;
197 pa_dbus_property_handler
*property_handler
;
198 void *handlers_state
= NULL
;
202 pa_strbuf_printf(buf
, " <interface name=\"%s\">\n", iface_entry
->name
);
204 PA_HASHMAP_FOREACH(method_handler
, iface_entry
->method_handlers
, handlers_state
) {
205 pa_strbuf_printf(buf
, " <method name=\"%s\">\n", method_handler
->method_name
);
207 for (i
= 0; i
< method_handler
->n_arguments
; ++i
)
208 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n",
209 method_handler
->arguments
[i
].name
,
210 method_handler
->arguments
[i
].type
,
211 method_handler
->arguments
[i
].direction
);
213 pa_strbuf_puts(buf
, " </method>\n");
216 handlers_state
= NULL
;
218 PA_HASHMAP_FOREACH(property_handler
, iface_entry
->property_handlers
, handlers_state
)
219 pa_strbuf_printf(buf
, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n",
220 property_handler
->property_name
,
221 property_handler
->type
,
222 property_handler
->get_cb
? (property_handler
->set_cb
? "readwrite" : "read") : "write");
224 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
225 pa_strbuf_printf(buf
, " <signal name=\"%s\">\n", iface_entry
->signals
[i
].name
);
227 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
)
228 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry
->signals
[i
].arguments
[j
].name
,
229 iface_entry
->signals
[i
].arguments
[j
].type
);
231 pa_strbuf_puts(buf
, " </signal>\n");
234 pa_strbuf_puts(buf
, " </interface>\n");
237 pa_strbuf_puts(buf
, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE
"\">\n"
238 " <method name=\"Introspect\">\n"
239 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
242 " <interface name=\"" DBUS_INTERFACE_PROPERTIES
"\">\n"
243 " <method name=\"Get\">\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=\"out\"/>\n"
248 " <method name=\"Set\">\n"
249 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
250 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
251 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
253 " <method name=\"GetAll\">\n"
254 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
255 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
259 pa_strbuf_puts(buf
, "</node>\n");
261 pa_xfree(oe
->introspection
);
262 oe
->introspection
= pa_strbuf_tostring_free(buf
);
265 /* Return value of find_handler() and its subfunctions. */
267 /* The received message is a valid .Get call. */
270 /* The received message is a valid .Set call. */
273 /* The received message is a valid .GetAll call. */
276 /* The received message is a valid method call. */
279 /* The interface of the received message hasn't been registered for the
280 * destination object. */
283 /* No property handler was found for the received .Get or .Set call. */
286 /* The interface argument of a property call didn't match any registered
288 NO_SUCH_PROPERTY_INTERFACE
,
290 /* The received message called .Get or .Set for a property whose access
291 * mode doesn't match the call. */
292 PROPERTY_ACCESS_DENIED
,
294 /* The new value signature of a .Set call didn't match the expected
296 INVALID_PROPERTY_SIG
,
298 /* No method handler was found for the received message. */
301 /* The signature of the received message didn't match the expected
302 * signature. Despite the name, this can also be returned for a property
303 * call if its message signature is invalid. */
307 /* Data for resolving the correct reaction to a received message. */
309 DBusMessage
*message
; /* The received message. */
310 struct object_entry
*obj_entry
;
311 const char *interface
; /* Destination interface name (extracted from the message). */
312 struct interface_entry
*iface_entry
;
314 const char *property
; /* Property name (extracted from the message). */
315 const char *property_interface
; /* The interface argument of a property call is stored here. */
316 pa_dbus_property_handler
*property_handler
;
317 const char *expected_property_sig
; /* Property signature from the introspection data. */
318 const char *property_sig
; /* The signature of the new value in the received .Set message. */
319 DBusMessageIter variant_iter
; /* Iterator pointing to the beginning of the new value variant of a .Set call. */
321 const char *method
; /* Method name (extracted from the message). */
322 pa_dbus_method_handler
*method_handler
;
323 const char *expected_method_sig
; /* Method signature from the introspection data. */
324 const char *method_sig
; /* The signature of the received message. */
327 /* Called when call_info->property has been set and the property interface has
328 * not been given. In case of a Set call, call_info->property_sig is also set,
329 * which is checked against the expected value in this function. */
330 static enum find_result_t
find_handler_by_property(struct call_info
*call_info
) {
333 pa_assert(call_info
);
335 PA_HASHMAP_FOREACH(call_info
->iface_entry
, call_info
->obj_entry
->interfaces
, state
) {
336 if ((call_info
->property_handler
= pa_hashmap_get(call_info
->iface_entry
->property_handlers
, call_info
->property
))) {
337 if (pa_streq(call_info
->method
, "Get"))
338 return call_info
->property_handler
->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
340 else if (pa_streq(call_info
->method
, "Set")) {
341 call_info
->expected_property_sig
= call_info
->property_handler
->type
;
343 if (pa_streq(call_info
->property_sig
, call_info
->expected_property_sig
))
344 return call_info
->property_handler
->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
346 return INVALID_PROPERTY_SIG
;
349 pa_assert_not_reached();
353 return NO_SUCH_PROPERTY
;
356 static enum find_result_t
find_handler_by_method(struct call_info
*call_info
) {
359 pa_assert(call_info
);
361 PA_HASHMAP_FOREACH(call_info
->iface_entry
, call_info
->obj_entry
->interfaces
, state
) {
362 if ((call_info
->method_handler
= pa_hashmap_get(call_info
->iface_entry
->method_handlers
, call_info
->method
))) {
363 pa_assert_se(call_info
->expected_method_sig
= pa_hashmap_get(call_info
->iface_entry
->method_signatures
, call_info
->method
));
365 if (pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
368 return INVALID_METHOD_SIG
;
372 return NO_SUCH_METHOD
;
375 static enum find_result_t
find_handler_from_properties_call(struct call_info
*call_info
) {
376 pa_assert(call_info
);
378 if (pa_streq(call_info
->method
, "GetAll")) {
379 call_info
->expected_method_sig
= "s";
380 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
381 return INVALID_METHOD_SIG
;
383 pa_assert_se(dbus_message_get_args(call_info
->message
, NULL
,
384 DBUS_TYPE_STRING
, &call_info
->property_interface
,
387 if (*call_info
->property_interface
) {
388 if ((call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->property_interface
)))
389 return FOUND_GET_ALL
;
391 return NO_SUCH_PROPERTY_INTERFACE
;
394 pa_assert_se(call_info
->iface_entry
= pa_hashmap_first(call_info
->obj_entry
->interfaces
));
395 return FOUND_GET_ALL
;
398 } else if (pa_streq(call_info
->method
, "Get")) {
399 call_info
->expected_method_sig
= "ss";
400 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
401 return INVALID_METHOD_SIG
;
403 pa_assert_se(dbus_message_get_args(call_info
->message
, NULL
,
404 DBUS_TYPE_STRING
, &call_info
->property_interface
,
405 DBUS_TYPE_STRING
, &call_info
->property
,
408 if (*call_info
->property_interface
) {
409 if (!(call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->property_interface
)))
410 return NO_SUCH_PROPERTY_INTERFACE
;
411 else if ((call_info
->property_handler
=
412 pa_hashmap_get(call_info
->iface_entry
->property_handlers
, call_info
->property
)))
413 return call_info
->property_handler
->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
415 return NO_SUCH_PROPERTY
;
418 return find_handler_by_property(call_info
);
420 } else if (pa_streq(call_info
->method
, "Set")) {
421 DBusMessageIter msg_iter
;
423 call_info
->expected_method_sig
= "ssv";
424 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
425 return INVALID_METHOD_SIG
;
427 pa_assert_se(dbus_message_iter_init(call_info
->message
, &msg_iter
));
429 dbus_message_iter_get_basic(&msg_iter
, &call_info
->property_interface
);
430 pa_assert_se(dbus_message_iter_next(&msg_iter
));
431 dbus_message_iter_get_basic(&msg_iter
, &call_info
->property
);
432 pa_assert_se(dbus_message_iter_next(&msg_iter
));
434 dbus_message_iter_recurse(&msg_iter
, &call_info
->variant_iter
);
436 call_info
->property_sig
= dbus_message_iter_get_signature(&call_info
->variant_iter
);
438 if (*call_info
->property_interface
) {
439 if (!(call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->property_interface
)))
440 return NO_SUCH_PROPERTY_INTERFACE
;
442 else if ((call_info
->property_handler
=
443 pa_hashmap_get(call_info
->iface_entry
->property_handlers
, call_info
->property
))) {
444 call_info
->expected_property_sig
= call_info
->property_handler
->type
;
446 if (pa_streq(call_info
->property_sig
, call_info
->expected_property_sig
))
447 return call_info
->property_handler
->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
449 return INVALID_PROPERTY_SIG
;
452 return NO_SUCH_PROPERTY
;
455 return find_handler_by_property(call_info
);
458 pa_assert_not_reached();
461 static enum find_result_t
find_handler(struct call_info
*call_info
) {
462 pa_assert(call_info
);
464 if (call_info
->interface
) {
465 if (pa_streq(call_info
->interface
, DBUS_INTERFACE_PROPERTIES
))
466 return find_handler_from_properties_call(call_info
);
468 else if (!(call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->interface
)))
469 return NO_SUCH_INTERFACE
;
471 else if ((call_info
->method_handler
= pa_hashmap_get(call_info
->iface_entry
->method_handlers
, call_info
->method
))) {
472 pa_assert_se(call_info
->expected_method_sig
= pa_hashmap_get(call_info
->iface_entry
->method_signatures
, call_info
->method
));
474 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
475 return INVALID_METHOD_SIG
;
480 return NO_SUCH_METHOD
;
482 } else { /* The method call doesn't contain an interface. */
483 if (pa_streq(call_info
->method
, "Get") || pa_streq(call_info
->method
, "Set") || pa_streq(call_info
->method
, "GetAll")) {
484 if (find_handler_by_method(call_info
) == FOUND_METHOD
)
485 /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
488 /* Assume this is a .Properties call. */
489 return find_handler_from_properties_call(call_info
);
491 } else /* This is not a .Properties call. */
492 return find_handler_by_method(call_info
);
496 static DBusHandlerResult
handle_message_cb(DBusConnection
*connection
, DBusMessage
*message
, void *user_data
) {
497 pa_dbus_protocol
*p
= user_data
;
498 struct call_info call_info
;
500 pa_assert(connection
);
503 pa_assert(p
->objects
);
505 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
506 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
508 pa_log_debug("Received message: destination = %s, interface = %s, member = %s",
509 dbus_message_get_path(message
),
510 dbus_message_get_interface(message
),
511 dbus_message_get_member(message
));
513 call_info
.message
= message
;
514 pa_assert_se(call_info
.obj_entry
= pa_hashmap_get(p
->objects
, dbus_message_get_path(message
)));
515 call_info
.interface
= dbus_message_get_interface(message
);
516 pa_assert_se(call_info
.method
= dbus_message_get_member(message
));
517 pa_assert_se(call_info
.method_sig
= dbus_message_get_signature(message
));
519 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") ||
520 (!dbus_message_get_interface(message
) && dbus_message_has_member(message
, "Introspect"))) {
521 pa_dbus_send_basic_value_reply(connection
, message
, DBUS_TYPE_STRING
, &call_info
.obj_entry
->introspection
);
525 switch (find_handler(&call_info
)) {
526 case FOUND_GET_PROPERTY
:
527 call_info
.property_handler
->get_cb(connection
, message
, call_info
.iface_entry
->userdata
);
530 case FOUND_SET_PROPERTY
:
531 call_info
.property_handler
->set_cb(connection
, message
, &call_info
.variant_iter
, call_info
.iface_entry
->userdata
);
535 call_info
.method_handler
->receive_cb(connection
, message
, call_info
.iface_entry
->userdata
);
539 if (call_info
.iface_entry
->get_all_properties_cb
)
540 call_info
.iface_entry
->get_all_properties_cb(connection
, message
, call_info
.iface_entry
->userdata
);
542 DBusMessage
*dummy_reply
= NULL
;
543 DBusMessageIter msg_iter
;
544 DBusMessageIter dict_iter
;
546 pa_assert_se(dummy_reply
= dbus_message_new_method_return(message
));
547 dbus_message_iter_init_append(dummy_reply
, &msg_iter
);
548 pa_assert_se(dbus_message_iter_open_container(&msg_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
));
549 pa_assert_se(dbus_message_iter_close_container(&msg_iter
, &dict_iter
));
550 pa_assert_se(dbus_connection_send(connection
, dummy_reply
, NULL
));
551 dbus_message_unref(dummy_reply
);
555 case PROPERTY_ACCESS_DENIED
:
556 pa_dbus_send_error(connection
, message
, DBUS_ERROR_ACCESS_DENIED
,
557 "%s access denied for property %s", call_info
.method
, call_info
.property
);
561 pa_dbus_send_error(connection
, message
, DBUS_ERROR_UNKNOWN_METHOD
, "No such method: %s", call_info
.method
);
564 case NO_SUCH_INTERFACE
:
565 pa_dbus_send_error(connection
, message
, PA_DBUS_ERROR_NO_SUCH_INTERFACE
, "No such interface: %s", call_info
.interface
);
568 case NO_SUCH_PROPERTY
:
569 pa_dbus_send_error(connection
, message
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "No such property: %s", call_info
.property
);
572 case NO_SUCH_PROPERTY_INTERFACE
:
573 pa_dbus_send_error(connection
, message
, PA_DBUS_ERROR_NO_SUCH_INTERFACE
, "No such property interface: %s", call_info
.property_interface
);
576 case INVALID_METHOD_SIG
:
577 pa_dbus_send_error(connection
, message
, DBUS_ERROR_INVALID_ARGS
,
578 "Invalid signature for method %s: '%s'. Expected '%s'.",
579 call_info
.method
, call_info
.method_sig
, call_info
.expected_method_sig
);
582 case INVALID_PROPERTY_SIG
:
583 pa_dbus_send_error(connection
, message
, DBUS_ERROR_INVALID_ARGS
,
584 "Invalid signature for property %s: '%s'. Expected '%s'.",
585 call_info
.property
, call_info
.property_sig
, call_info
.expected_property_sig
);
589 pa_assert_not_reached();
593 return DBUS_HANDLER_RESULT_HANDLED
;
596 static DBusObjectPathVTable vtable
= {
597 .unregister_function
= NULL
,
598 .message_function
= handle_message_cb
,
599 .dbus_internal_pad1
= NULL
,
600 .dbus_internal_pad2
= NULL
,
601 .dbus_internal_pad3
= NULL
,
602 .dbus_internal_pad4
= NULL
605 static void register_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
606 struct connection_entry
*conn_entry
;
610 pa_assert(obj_entry
);
612 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
613 pa_assert_se(dbus_connection_register_object_path(conn_entry
->connection
, obj_entry
->path
, &vtable
, p
));
616 static pa_dbus_arg_info
*copy_args(const pa_dbus_arg_info
*src
, unsigned n
) {
617 pa_dbus_arg_info
*dst
;
625 dst
= pa_xnew0(pa_dbus_arg_info
, n
);
627 for (i
= 0; i
< n
; ++i
) {
628 dst
[i
].name
= pa_xstrdup(src
[i
].name
);
629 dst
[i
].type
= pa_xstrdup(src
[i
].type
);
630 dst
[i
].direction
= pa_xstrdup(src
[i
].direction
);
636 static pa_hashmap
*create_method_handlers(const pa_dbus_interface_info
*info
) {
637 pa_hashmap
*handlers
;
641 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
643 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
645 for (i
= 0; i
< info
->n_method_handlers
; ++i
) {
646 pa_dbus_method_handler
*h
= pa_xnew(pa_dbus_method_handler
, 1);
647 h
->method_name
= pa_xstrdup(info
->method_handlers
[i
].method_name
);
648 h
->arguments
= copy_args(info
->method_handlers
[i
].arguments
, info
->method_handlers
[i
].n_arguments
);
649 h
->n_arguments
= info
->method_handlers
[i
].n_arguments
;
650 h
->receive_cb
= info
->method_handlers
[i
].receive_cb
;
652 pa_hashmap_put(handlers
, h
->method_name
, h
);
658 static pa_hashmap
*extract_method_signatures(pa_hashmap
*method_handlers
) {
659 pa_hashmap
*signatures
= NULL
;
660 pa_dbus_method_handler
*handler
= NULL
;
662 pa_strbuf
*sig_buf
= NULL
;
665 pa_assert(method_handlers
);
667 signatures
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
669 PA_HASHMAP_FOREACH(handler
, method_handlers
, state
) {
670 sig_buf
= pa_strbuf_new();
672 for (i
= 0; i
< handler
->n_arguments
; ++i
) {
673 if (pa_streq(handler
->arguments
[i
].direction
, "in"))
674 pa_strbuf_puts(sig_buf
, handler
->arguments
[i
].type
);
677 pa_hashmap_put(signatures
, handler
->method_name
, pa_strbuf_tostring_free(sig_buf
));
683 static pa_hashmap
*create_property_handlers(const pa_dbus_interface_info
*info
) {
684 pa_hashmap
*handlers
;
688 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
690 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
692 for (i
= 0; i
< info
->n_property_handlers
; ++i
) {
693 pa_dbus_property_handler
*h
= pa_xnew(pa_dbus_property_handler
, 1);
694 h
->property_name
= pa_xstrdup(info
->property_handlers
[i
].property_name
);
695 h
->type
= pa_xstrdup(info
->property_handlers
[i
].type
);
696 h
->get_cb
= info
->property_handlers
[i
].get_cb
;
697 h
->set_cb
= info
->property_handlers
[i
].set_cb
;
699 pa_hashmap_put(handlers
, h
->property_name
, h
);
705 static pa_dbus_signal_info
*copy_signals(const pa_dbus_interface_info
*info
) {
706 pa_dbus_signal_info
*dst
;
711 if (info
->n_signals
== 0)
714 pa_assert(info
->signals
);
716 dst
= pa_xnew(pa_dbus_signal_info
, info
->n_signals
);
718 for (i
= 0; i
< info
->n_signals
; ++i
) {
719 dst
[i
].name
= pa_xstrdup(info
->signals
[i
].name
);
720 dst
[i
].arguments
= copy_args(info
->signals
[i
].arguments
, info
->signals
[i
].n_arguments
);
721 dst
[i
].n_arguments
= info
->signals
[i
].n_arguments
;
727 int pa_dbus_protocol_add_interface(pa_dbus_protocol
*p
,
729 const pa_dbus_interface_info
*info
,
731 struct object_entry
*obj_entry
;
732 struct interface_entry
*iface_entry
;
733 pa_bool_t obj_entry_created
= FALSE
;
738 pa_assert(info
->name
);
739 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
740 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
741 pa_assert(info
->get_all_properties_cb
|| info
->n_property_handlers
== 0);
742 pa_assert(info
->signals
|| info
->n_signals
== 0);
744 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
))) {
745 obj_entry
= pa_xnew(struct object_entry
, 1);
746 obj_entry
->path
= pa_xstrdup(path
);
747 obj_entry
->interfaces
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
748 obj_entry
->introspection
= NULL
;
750 pa_hashmap_put(p
->objects
, obj_entry
->path
, obj_entry
);
751 obj_entry_created
= TRUE
;
754 if (pa_hashmap_get(obj_entry
->interfaces
, info
->name
) != NULL
)
755 goto fail
; /* The interface was already registered. */
757 iface_entry
= pa_xnew(struct interface_entry
, 1);
758 iface_entry
->name
= pa_xstrdup(info
->name
);
759 iface_entry
->method_handlers
= create_method_handlers(info
);
760 iface_entry
->method_signatures
= extract_method_signatures(iface_entry
->method_handlers
);
761 iface_entry
->property_handlers
= create_property_handlers(info
);
762 iface_entry
->get_all_properties_cb
= info
->get_all_properties_cb
;
763 iface_entry
->signals
= copy_signals(info
);
764 iface_entry
->n_signals
= info
->n_signals
;
765 iface_entry
->userdata
= userdata
;
766 pa_hashmap_put(obj_entry
->interfaces
, iface_entry
->name
, iface_entry
);
768 update_introspection(obj_entry
);
770 if (obj_entry_created
)
771 register_object(p
, obj_entry
);
773 pa_log_debug("Interface %s added for object %s", iface_entry
->name
, obj_entry
->path
);
781 static void unregister_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
782 struct connection_entry
*conn_entry
;
786 pa_assert(obj_entry
);
788 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
789 pa_assert_se(dbus_connection_unregister_object_path(conn_entry
->connection
, obj_entry
->path
));
792 static void method_handler_free_cb(void *p
, void *userdata
) {
793 pa_dbus_method_handler
*h
= p
;
798 pa_xfree((char *) h
->method_name
);
800 for (i
= 0; i
< h
->n_arguments
; ++i
) {
801 pa_xfree((char *) h
->arguments
[i
].name
);
802 pa_xfree((char *) h
->arguments
[i
].type
);
803 pa_xfree((char *) h
->arguments
[i
].direction
);
806 pa_xfree((pa_dbus_arg_info
*) h
->arguments
);
810 static void method_signature_free_cb(void *p
, void *userdata
) {
816 static void property_handler_free_cb(void *p
, void *userdata
) {
817 pa_dbus_property_handler
*h
= p
;
821 pa_xfree((char *) h
->property_name
);
822 pa_xfree((char *) h
->type
);
827 int pa_dbus_protocol_remove_interface(pa_dbus_protocol
*p
, const char* path
, const char* interface
) {
828 struct object_entry
*obj_entry
;
829 struct interface_entry
*iface_entry
;
834 pa_assert(interface
);
836 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
)))
839 if (!(iface_entry
= pa_hashmap_remove(obj_entry
->interfaces
, interface
)))
842 update_introspection(obj_entry
);
844 pa_log_debug("Interface %s removed from object %s", iface_entry
->name
, obj_entry
->path
);
846 pa_xfree(iface_entry
->name
);
847 pa_hashmap_free(iface_entry
->method_signatures
, method_signature_free_cb
, NULL
);
848 pa_hashmap_free(iface_entry
->method_handlers
, method_handler_free_cb
, NULL
);
849 pa_hashmap_free(iface_entry
->property_handlers
, property_handler_free_cb
, NULL
);
851 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
854 pa_xfree((char *) iface_entry
->signals
[i
].name
);
856 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
) {
857 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].name
);
858 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].type
);
859 pa_assert(iface_entry
->signals
[i
].arguments
[j
].direction
== NULL
);
862 pa_xfree((pa_dbus_arg_info
*) iface_entry
->signals
[i
].arguments
);
865 pa_xfree(iface_entry
->signals
);
866 pa_xfree(iface_entry
);
868 if (pa_hashmap_isempty(obj_entry
->interfaces
)) {
869 unregister_object(p
, obj_entry
);
871 pa_hashmap_remove(p
->objects
, path
);
872 pa_xfree(obj_entry
->path
);
873 pa_hashmap_free(obj_entry
->interfaces
, NULL
, NULL
);
874 pa_xfree(obj_entry
->introspection
);
881 static void register_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
882 struct object_entry
*obj_entry
;
888 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
889 pa_assert_se(dbus_connection_register_object_path(conn
, obj_entry
->path
, &vtable
, p
));
892 int pa_dbus_protocol_register_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
, pa_client
*client
) {
893 struct connection_entry
*conn_entry
;
899 if (pa_hashmap_get(p
->connections
, conn
))
900 return -1; /* The connection was already registered. */
902 register_all_objects(p
, conn
);
904 conn_entry
= pa_xnew(struct connection_entry
, 1);
905 conn_entry
->connection
= dbus_connection_ref(conn
);
906 conn_entry
->client
= client
;
907 conn_entry
->listening_for_all_signals
= FALSE
;
908 conn_entry
->all_signals_objects
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
909 conn_entry
->listening_signals
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
911 pa_hashmap_put(p
->connections
, conn
, conn_entry
);
916 static void unregister_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
917 struct object_entry
*obj_entry
;
923 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
924 pa_assert_se(dbus_connection_unregister_object_path(conn
, obj_entry
->path
));
927 static struct signal_paths_entry
*signal_paths_entry_new(const char *signal_name
) {
928 struct signal_paths_entry
*e
= NULL
;
930 pa_assert(signal_name
);
932 e
= pa_xnew0(struct signal_paths_entry
, 1);
933 e
->signal
= pa_xstrdup(signal_name
);
934 e
->paths
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
939 static void signal_paths_entry_free(struct signal_paths_entry
*e
) {
946 while ((path
= pa_idxset_steal_first(e
->paths
, NULL
)))
949 pa_idxset_free(e
->paths
, NULL
, NULL
);
953 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
954 struct connection_entry
*conn_entry
= NULL
;
955 struct signal_paths_entry
*signal_paths_entry
= NULL
;
956 char *object_path
= NULL
;
961 if (!(conn_entry
= pa_hashmap_remove(p
->connections
, conn
)))
964 unregister_all_objects(p
, conn
);
966 dbus_connection_unref(conn_entry
->connection
);
968 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
969 pa_xfree(object_path
);
971 pa_idxset_free(conn_entry
->all_signals_objects
, NULL
, NULL
);
973 while ((signal_paths_entry
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
974 signal_paths_entry_free(signal_paths_entry
);
976 pa_hashmap_free(conn_entry
->listening_signals
, NULL
, NULL
);
977 pa_xfree(conn_entry
);
982 pa_client
*pa_dbus_protocol_get_client(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
983 struct connection_entry
*conn_entry
;
988 if (!(conn_entry
= pa_hashmap_get(p
->connections
, conn
)))
991 return conn_entry
->client
;
994 void pa_dbus_protocol_add_signal_listener(
996 DBusConnection
*conn
,
997 const char *signal_name
,
999 unsigned n_objects
) {
1000 struct connection_entry
*conn_entry
= NULL
;
1001 struct signal_paths_entry
*signal_paths_entry
= NULL
;
1002 char *object_path
= NULL
;
1007 pa_assert(objects
|| n_objects
== 0);
1009 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
1011 /* all_signals_objects will either be emptied or replaced with new objects,
1012 * so we empty it here unconditionally. If listening_for_all_signals is
1013 * currently FALSE, the idxset is empty already so this does nothing. */
1014 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
1015 pa_xfree(object_path
);
1018 conn_entry
->listening_for_all_signals
= FALSE
;
1020 /* Replace the old signal paths entry for this signal with a new
1022 if ((signal_paths_entry
= pa_hashmap_remove(conn_entry
->listening_signals
, signal_name
)))
1023 signal_paths_entry_free(signal_paths_entry
);
1024 signal_paths_entry
= signal_paths_entry_new(signal_name
);
1026 for (i
= 0; i
< n_objects
; ++i
)
1027 pa_idxset_put(signal_paths_entry
->paths
, pa_xstrdup(objects
[i
]), NULL
);
1029 pa_hashmap_put(conn_entry
->listening_signals
, signal_paths_entry
->signal
, signal_paths_entry
);
1032 conn_entry
->listening_for_all_signals
= TRUE
;
1034 /* We're not interested in individual signals anymore, so let's empty
1035 * listening_signals. */
1036 while ((signal_paths_entry
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
1037 signal_paths_entry_free(signal_paths_entry
);
1039 for (i
= 0; i
< n_objects
; ++i
)
1040 pa_idxset_put(conn_entry
->all_signals_objects
, pa_xstrdup(objects
[i
]), NULL
);
1044 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal_name
) {
1045 struct connection_entry
*conn_entry
= NULL
;
1046 struct signal_paths_entry
*signal_paths_entry
= NULL
;
1051 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
1054 if ((signal_paths_entry
= pa_hashmap_get(conn_entry
->listening_signals
, signal_name
)))
1055 signal_paths_entry_free(signal_paths_entry
);
1060 conn_entry
->listening_for_all_signals
= FALSE
;
1062 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
1063 pa_xfree(object_path
);
1065 while ((signal_paths_entry
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
1066 signal_paths_entry_free(signal_paths_entry
);
1070 void pa_dbus_protocol_send_signal(pa_dbus_protocol
*p
, DBusMessage
*signal_msg
) {
1071 struct connection_entry
*conn_entry
;
1072 struct signal_paths_entry
*signal_paths_entry
;
1074 DBusMessage
*signal_copy
;
1075 char *signal_string
;
1078 pa_assert(signal_msg
);
1079 pa_assert(dbus_message_get_type(signal_msg
) == DBUS_MESSAGE_TYPE_SIGNAL
);
1080 pa_assert(dbus_message_get_path(signal_msg
));
1081 pa_assert(dbus_message_get_interface(signal_msg
));
1082 pa_assert(dbus_message_get_member(signal_msg
));
1084 signal_string
= pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal_msg
), dbus_message_get_member(signal_msg
));
1086 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
) {
1087 if ((conn_entry
->listening_for_all_signals
/* Case 1: listening for all signals */
1088 && (pa_idxset_get_by_data(conn_entry
->all_signals_objects
, dbus_message_get_path(signal_msg
), NULL
)
1089 || pa_idxset_isempty(conn_entry
->all_signals_objects
)))
1091 || (!conn_entry
->listening_for_all_signals
/* Case 2: not listening for all signals */
1092 && (signal_paths_entry
= pa_hashmap_get(conn_entry
->listening_signals
, signal_string
))
1093 && (pa_idxset_get_by_data(signal_paths_entry
->paths
, dbus_message_get_path(signal_msg
), NULL
)
1094 || pa_idxset_isempty(signal_paths_entry
->paths
)))) {
1096 pa_assert_se(signal_copy
= dbus_message_copy(signal_msg
));
1097 pa_assert_se(dbus_connection_send(conn_entry
->connection
, signal_copy
, NULL
));
1098 dbus_message_unref(signal_copy
);
1102 pa_xfree(signal_string
);
1105 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol
*p
, unsigned *n
) {
1106 const char **extensions
;
1107 const char *ext_name
;
1114 *n
= pa_idxset_size(p
->extensions
);
1119 extensions
= pa_xnew(const char *, *n
);
1121 while ((ext_name
= pa_idxset_iterate(p
->extensions
, &state
, NULL
)))
1122 extensions
[i
++] = ext_name
;
1127 int pa_dbus_protocol_register_extension(pa_dbus_protocol
*p
, const char *name
) {
1128 char *internal_name
;
1133 internal_name
= pa_xstrdup(name
);
1135 if (pa_idxset_put(p
->extensions
, internal_name
, NULL
) < 0) {
1136 pa_xfree(internal_name
);
1140 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED
], internal_name
);
1145 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol
*p
, const char *name
) {
1146 char *internal_name
;
1151 if (!(internal_name
= pa_idxset_remove_by_data(p
->extensions
, name
, NULL
)))
1154 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED
], internal_name
);
1156 pa_xfree(internal_name
);
1161 pa_hook_slot
*pa_dbus_protocol_hook_connect(
1162 pa_dbus_protocol
*p
,
1163 pa_dbus_protocol_hook_t hook
,
1164 pa_hook_priority_t prio
,
1168 pa_assert(hook
< PA_DBUS_PROTOCOL_HOOK_MAX
);
1171 return pa_hook_connect(&p
->hooks
[hook
], prio
, cb
, data
);