2 This file is part of PulseAudio.
4 Copyright 2009 Tanu Kaskinen
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <dbus/dbus.h>
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/dbus-util.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/shared.h>
35 #include <pulsecore/strbuf.h>
37 #include "protocol-dbus.h"
39 struct pa_dbus_protocol
{
43 pa_hashmap
*objects
; /* Object path -> struct object_entry */
44 pa_hashmap
*connections
; /* DBusConnection -> struct connection_entry */
45 pa_idxset
*extensions
; /* Strings */
47 pa_hook hooks
[PA_DBUS_PROTOCOL_HOOK_MAX
];
52 pa_hashmap
*interfaces
; /* Interface name -> struct interface_entry */
56 struct connection_entry
{
57 DBusConnection
*connection
;
60 pa_bool_t listening_for_all_signals
;
62 /* Contains object paths. If this is empty, then signals from all objects
63 * are accepted. Only used when listening_for_all_signals == TRUE. */
64 pa_idxset
*all_signals_objects
;
66 /* Signal name -> idxset. The idxsets contain object paths. If an idxset is
67 * empty, then that signal is accepted from all objects. Only used when
68 * listening_for_all_signals == FALSE. */
69 pa_hashmap
*listening_signals
;
72 struct interface_entry
{
74 pa_hashmap
*method_handlers
;
75 pa_hashmap
*property_handlers
;
76 pa_dbus_receive_cb_t get_all_properties_cb
;
77 pa_dbus_signal_info
*signals
;
82 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type
) {
84 char *runtime_path
= NULL
;
85 char *escaped_path
= NULL
;
87 switch (server_type
) {
88 case PA_SERVER_TYPE_USER
:
89 pa_assert_se((runtime_path
= pa_runtime_path(PA_DBUS_SOCKET_NAME
)));
90 pa_assert_se((escaped_path
= dbus_address_escape_value(runtime_path
)));
91 address
= pa_sprintf_malloc("unix:path=%s", escaped_path
);
94 case PA_SERVER_TYPE_SYSTEM
:
95 pa_assert_se((escaped_path
= dbus_address_escape_value(PA_DBUS_SYSTEM_SOCKET_PATH
)));
96 address
= pa_sprintf_malloc("unix:path=%s", escaped_path
);
99 case PA_SERVER_TYPE_NONE
:
100 address
= pa_xnew0(char, 1);
104 pa_assert_not_reached();
107 pa_xfree(runtime_path
);
108 pa_xfree(escaped_path
);
113 static pa_dbus_protocol
*dbus_protocol_new(pa_core
*c
) {
119 p
= pa_xnew(pa_dbus_protocol
, 1);
121 p
->core
= pa_core_ref(c
);
122 p
->objects
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
123 p
->connections
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
124 p
->extensions
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
126 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
127 pa_hook_init(&p
->hooks
[i
], p
);
129 pa_assert_se(pa_shared_set(c
, "dbus-protocol", p
) >= 0);
134 pa_dbus_protocol
* pa_dbus_protocol_get(pa_core
*c
) {
137 if ((p
= pa_shared_get(c
, "dbus-protocol")))
138 return pa_dbus_protocol_ref(p
);
140 return dbus_protocol_new(c
);
143 pa_dbus_protocol
* pa_dbus_protocol_ref(pa_dbus_protocol
*p
) {
145 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
152 void pa_dbus_protocol_unref(pa_dbus_protocol
*p
) {
156 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
158 if (PA_REFCNT_DEC(p
) > 0)
161 pa_assert(pa_hashmap_isempty(p
->objects
));
162 pa_assert(pa_hashmap_isempty(p
->connections
));
163 pa_assert(pa_idxset_isempty(p
->extensions
));
165 pa_hashmap_free(p
->objects
, NULL
, NULL
);
166 pa_hashmap_free(p
->connections
, NULL
, NULL
);
167 pa_idxset_free(p
->extensions
, NULL
, NULL
);
169 for (i
= 0; i
< PA_DBUS_PROTOCOL_HOOK_MAX
; ++i
)
170 pa_hook_done(&p
->hooks
[i
]);
172 pa_assert_se(pa_shared_remove(p
->core
, "dbus-protocol") >= 0);
174 pa_core_unref(p
->core
);
179 static void update_introspection(struct object_entry
*oe
) {
181 void *interfaces_state
= NULL
;
182 struct interface_entry
*iface_entry
= NULL
;
186 buf
= pa_strbuf_new();
187 pa_strbuf_puts(buf
, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
);
188 pa_strbuf_puts(buf
, "<node>\n");
190 PA_HASHMAP_FOREACH(iface_entry
, oe
->interfaces
, interfaces_state
) {
191 pa_dbus_method_handler
*method_handler
;
192 pa_dbus_property_handler
*property_handler
;
193 void *handlers_state
= NULL
;
197 pa_strbuf_printf(buf
, " <interface name=\"%s\">\n", iface_entry
->name
);
199 PA_HASHMAP_FOREACH(method_handler
, iface_entry
->method_handlers
, handlers_state
) {
200 pa_strbuf_printf(buf
, " <method name=\"%s\">\n", method_handler
->method_name
);
202 for (i
= 0; i
< method_handler
->n_arguments
; ++i
)
203 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n",
204 method_handler
->arguments
[i
].name
,
205 method_handler
->arguments
[i
].type
,
206 method_handler
->arguments
[i
].direction
);
208 pa_strbuf_puts(buf
, " </method>\n");
211 handlers_state
= NULL
;
213 PA_HASHMAP_FOREACH(property_handler
, iface_entry
->property_handlers
, handlers_state
)
214 pa_strbuf_printf(buf
, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n",
215 property_handler
->property_name
,
216 property_handler
->type
,
217 property_handler
->get_cb
? (property_handler
->set_cb
? "readwrite" : "read") : "write");
219 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
220 pa_strbuf_printf(buf
, " <signal name=\"%s\">\n", iface_entry
->signals
[i
].name
);
222 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
)
223 pa_strbuf_printf(buf
, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry
->signals
[i
].arguments
[j
].name
,
224 iface_entry
->signals
[i
].arguments
[j
].type
);
226 pa_strbuf_puts(buf
, " </signal>\n");
229 pa_strbuf_puts(buf
, " </interface>\n");
232 pa_strbuf_puts(buf
, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE
"\">\n"
233 " <method name=\"Introspect\">\n"
234 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
237 " <interface name=\"" DBUS_INTERFACE_PROPERTIES
"\">\n"
238 " <method name=\"Get\">\n"
239 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
240 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
241 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
243 " <method name=\"Set\">\n"
244 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
246 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
248 " <method name=\"GetAll\">\n"
249 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
250 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
254 pa_strbuf_puts(buf
, "</node>\n");
256 pa_xfree(oe
->introspection
);
257 oe
->introspection
= pa_strbuf_tostring_free(buf
);
260 /* Return value of find_handler() and its subfunctions. */
262 /* The received message is a valid .Get call. */
265 /* The received message is a valid .Set call. */
268 /* The received message is a valid .GetAll call. */
271 /* The received message is a valid method call. */
274 /* The interface of the received message hasn't been registered for the
275 * destination object. */
278 /* No property handler was found for the received .Get or .Set call. */
281 /* The interface argument of a property call didn't match any registered
283 NO_SUCH_PROPERTY_INTERFACE
,
285 /* The received message called .Get or .Set for a property whose access
286 * mode doesn't match the call. */
287 PROPERTY_ACCESS_DENIED
,
289 /* The new value signature of a .Set call didn't match the expexted
291 INVALID_PROPERTY_SIG
,
293 /* No method handler was found for the received message. */
296 /* The signature of the received message didn't match the expected
297 * signature. Despite the name, this can also be returned for a property
298 * call if its message signature is invalid. */
302 /* Data for resolving the correct reaction to a received message. */
304 DBusMessage
*message
; /* The received message. */
305 struct object_entry
*obj_entry
;
306 const char *interface
; /* Destination interface name (extracted from the message). */
307 struct interface_entry
*iface_entry
;
309 const char *property
; /* Property name (extracted from the message). */
310 const char *property_interface
; /* The interface argument of a property call is stored here. */
311 pa_dbus_property_handler
*property_handler
;
312 const char *expected_property_sig
; /* Property signature from the introspection data. */
313 const char *property_sig
; /* The signature of the new value in the received .Set message. */
314 DBusMessageIter variant_iter
; /* Iterator pointing to the beginning of the new value variant of a .Set call. */
316 const char *method
; /* Method name (extracted from the message). */
317 pa_dbus_method_handler
*method_handler
;
318 const char *expected_method_sig
; /* Method signature from the introspection data. */
319 const char *method_sig
; /* The signature of the received message. */
322 /* Called when call_info->property has been set and the property interface has
323 * not been given. In case of a Set call, call_info->property_sig is also set,
324 * which is checked against the expected value in this function. */
325 static enum find_result_t
find_handler_by_property(struct call_info
*call_info
) {
328 pa_assert(call_info
);
330 PA_HASHMAP_FOREACH(call_info
->iface_entry
, call_info
->obj_entry
->interfaces
, state
) {
331 if ((call_info
->property_handler
= pa_hashmap_get(call_info
->iface_entry
->property_handlers
, call_info
->property
))) {
332 if (pa_streq(call_info
->method
, "Get"))
333 return call_info
->property_handler
->get_cb
? FOUND_GET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
335 else if (pa_streq(call_info
->method
, "Set")) {
336 call_info
->expected_property_sig
= call_info
->property_handler
->type
;
338 if (pa_streq(call_info
->property_sig
, call_info
->expected_property_sig
))
339 return call_info
->property_handler
->set_cb
? FOUND_SET_PROPERTY
: PROPERTY_ACCESS_DENIED
;
341 return INVALID_PROPERTY_SIG
;
344 pa_assert_not_reached();
348 return NO_SUCH_PROPERTY
;
351 static enum find_result_t
find_handler_by_method(struct call_info
*call_info
) {
354 pa_assert(call_info
);
356 PA_HASHMAP_FOREACH(call_info
->iface_entry
, call_info
->obj_entry
->interfaces
, state
) {
357 if ((call_info
->method_handler
= pa_hashmap_get(call_info
->iface_entry
->method_handlers
, call_info
->method
)))
361 return NO_SUCH_METHOD
;
364 static enum find_result_t
find_handler_from_properties_call(struct call_info
*call_info
) {
365 pa_assert(call_info
);
367 if (pa_streq(call_info
->method
, "GetAll")) {
368 call_info
->expected_method_sig
= "s";
369 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
370 return INVALID_METHOD_SIG
;
372 pa_assert_se(dbus_message_get_args(call_info
->message
, NULL
,
373 DBUS_TYPE_STRING
, &call_info
->property_interface
,
376 if (*call_info
->property_interface
) {
377 if ((call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->property_interface
)))
378 return FOUND_GET_ALL
;
380 return NO_SUCH_PROPERTY_INTERFACE
;
383 pa_assert_se(call_info
->iface_entry
= pa_hashmap_first(call_info
->obj_entry
->interfaces
));
384 return FOUND_GET_ALL
;
387 } else if (pa_streq(call_info
->method
, "Get")) {
388 call_info
->expected_method_sig
= "ss";
389 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
390 return INVALID_METHOD_SIG
;
392 pa_assert_se(dbus_message_get_args(call_info
->message
, NULL
,
393 DBUS_TYPE_STRING
, &call_info
->property_interface
,
394 DBUS_TYPE_STRING
, &call_info
->property
,
397 if (*call_info
->property_interface
) {
398 if (!(call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->property_interface
)))
399 return NO_SUCH_PROPERTY_INTERFACE
;
400 else if ((call_info
->property_handler
=
401 pa_hashmap_get(call_info
->iface_entry
->property_handlers
, call_info
->property
)))
402 return FOUND_GET_PROPERTY
;
404 return NO_SUCH_PROPERTY
;
407 return find_handler_by_property(call_info
);
409 } else if (pa_streq(call_info
->method
, "Set")) {
410 DBusMessageIter msg_iter
;
412 call_info
->expected_method_sig
= "ssv";
413 if (!pa_streq(call_info
->method_sig
, call_info
->expected_method_sig
))
414 return INVALID_METHOD_SIG
;
416 pa_assert_se(dbus_message_iter_init(call_info
->message
, &msg_iter
));
418 dbus_message_iter_get_basic(&msg_iter
, &call_info
->property_interface
);
419 pa_assert_se(dbus_message_iter_next(&msg_iter
));
420 dbus_message_iter_get_basic(&msg_iter
, &call_info
->property
);
421 pa_assert_se(dbus_message_iter_next(&msg_iter
));
423 dbus_message_iter_recurse(&msg_iter
, &call_info
->variant_iter
);
425 call_info
->property_sig
= dbus_message_iter_get_signature(&call_info
->variant_iter
);
427 if (*call_info
->property_interface
) {
428 if (!(call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->property_interface
)))
429 return NO_SUCH_PROPERTY_INTERFACE
;
431 else if ((call_info
->property_handler
=
432 pa_hashmap_get(call_info
->iface_entry
->property_handlers
, call_info
->property
))) {
433 call_info
->expected_property_sig
= call_info
->property_handler
->type
;
435 if (pa_streq(call_info
->property_sig
, call_info
->expected_property_sig
))
436 return FOUND_SET_PROPERTY
;
438 return INVALID_PROPERTY_SIG
;
441 return NO_SUCH_PROPERTY
;
444 return find_handler_by_property(call_info
);
447 pa_assert_not_reached();
450 static enum find_result_t
find_handler(struct call_info
*call_info
) {
451 pa_assert(call_info
);
453 if (call_info
->interface
) {
454 if (pa_streq(call_info
->interface
, DBUS_INTERFACE_PROPERTIES
))
455 return find_handler_from_properties_call(call_info
);
457 else if (!(call_info
->iface_entry
= pa_hashmap_get(call_info
->obj_entry
->interfaces
, call_info
->interface
)))
458 return NO_SUCH_INTERFACE
;
460 else if ((call_info
->method_handler
= pa_hashmap_get(call_info
->iface_entry
->method_handlers
, call_info
->method
)))
464 return NO_SUCH_METHOD
;
466 } else { /* The method call doesn't contain an interface. */
467 if (pa_streq(call_info
->method
, "Get") || pa_streq(call_info
->method
, "Set") || pa_streq(call_info
->method
, "GetAll")) {
468 if (find_handler_by_method(call_info
) == FOUND_METHOD
)
469 /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
472 /* Assume this is a .Properties call. */
473 return find_handler_from_properties_call(call_info
);
475 } else /* This is not a .Properties call. */
476 return find_handler_by_method(call_info
);
480 static DBusHandlerResult
handle_message_cb(DBusConnection
*connection
, DBusMessage
*message
, void *user_data
) {
481 pa_dbus_protocol
*p
= user_data
;
482 struct call_info call_info
;
484 pa_assert(connection
);
487 pa_assert(p
->objects
);
489 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
490 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
492 pa_log_debug("Received message: destination = %s, interface = %s, member = %s",
493 dbus_message_get_path(message
),
494 dbus_message_get_interface(message
),
495 dbus_message_get_member(message
));
497 call_info
.message
= message
;
498 pa_assert_se(call_info
.obj_entry
= pa_hashmap_get(p
->objects
, dbus_message_get_path(message
)));
499 call_info
.interface
= dbus_message_get_interface(message
);
500 pa_assert_se(call_info
.method
= dbus_message_get_member(message
));
501 pa_assert_se(call_info
.method_sig
= dbus_message_get_signature(message
));
503 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") ||
504 (!dbus_message_get_interface(message
) && dbus_message_has_member(message
, "Introspect"))) {
505 pa_dbus_send_basic_value_reply(connection
, message
, DBUS_TYPE_STRING
, &call_info
.obj_entry
->introspection
);
509 switch (find_handler(&call_info
)) {
510 case FOUND_GET_PROPERTY
:
511 call_info
.property_handler
->get_cb(connection
, message
, call_info
.iface_entry
->userdata
);
514 case FOUND_SET_PROPERTY
:
515 call_info
.property_handler
->set_cb(connection
, message
, &call_info
.variant_iter
, call_info
.iface_entry
->userdata
);
519 call_info
.method_handler
->receive_cb(connection
, message
, call_info
.iface_entry
->userdata
);
523 if (call_info
.iface_entry
->get_all_properties_cb
)
524 call_info
.iface_entry
->get_all_properties_cb(connection
, message
, call_info
.iface_entry
->userdata
);
526 DBusMessage
*dummy_reply
= NULL
;
527 DBusMessageIter msg_iter
;
528 DBusMessageIter dict_iter
;
530 pa_assert_se(dummy_reply
= dbus_message_new_method_return(message
));
531 dbus_message_iter_init_append(dummy_reply
, &msg_iter
);
532 pa_assert_se(dbus_message_iter_open_container(&msg_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
));
533 pa_assert_se(dbus_message_iter_close_container(&msg_iter
, &dict_iter
));
534 pa_assert_se(dbus_connection_send(connection
, dummy_reply
, NULL
));
535 dbus_message_unref(dummy_reply
);
539 case PROPERTY_ACCESS_DENIED
:
540 pa_dbus_send_error(connection
, message
, DBUS_ERROR_ACCESS_DENIED
,
541 "%s access denied for property %s", call_info
.method
, call_info
.property
);
545 pa_dbus_send_error(connection
, message
, DBUS_ERROR_UNKNOWN_METHOD
, "No such method: %s", call_info
.method
);
548 case NO_SUCH_PROPERTY
:
549 pa_dbus_send_error(connection
, message
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "No such property: %s", call_info
.property
);
552 case INVALID_METHOD_SIG
:
553 pa_dbus_send_error(connection
, message
, DBUS_ERROR_INVALID_ARGS
,
554 "Invalid signature for method %s: '%s'. Expected '%s'.",
555 call_info
.method
, call_info
.method_sig
, call_info
.expected_method_sig
);
558 case INVALID_PROPERTY_SIG
:
559 pa_dbus_send_error(connection
, message
, DBUS_ERROR_INVALID_ARGS
,
560 "Invalid signature for property %s: '%s'. Expected '%s'.",
561 call_info
.property
, call_info
.property_sig
, call_info
.expected_property_sig
);
564 pa_assert_not_reached();
568 return DBUS_HANDLER_RESULT_HANDLED
;
571 static DBusObjectPathVTable vtable
= {
572 .unregister_function
= NULL
,
573 .message_function
= handle_message_cb
,
574 .dbus_internal_pad1
= NULL
,
575 .dbus_internal_pad2
= NULL
,
576 .dbus_internal_pad3
= NULL
,
577 .dbus_internal_pad4
= NULL
580 static void register_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
581 struct connection_entry
*conn_entry
;
585 pa_assert(obj_entry
);
587 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
588 pa_assert_se(dbus_connection_register_object_path(conn_entry
->connection
, obj_entry
->path
, &vtable
, p
));
591 static pa_dbus_arg_info
*copy_args(const pa_dbus_arg_info
*src
, unsigned n
) {
592 pa_dbus_arg_info
*dst
;
600 dst
= pa_xnew0(pa_dbus_arg_info
, n
);
602 for (i
= 0; i
< n
; ++i
) {
603 dst
[i
].name
= pa_xstrdup(src
[i
].name
);
604 dst
[i
].type
= pa_xstrdup(src
[i
].type
);
605 dst
[i
].direction
= pa_xstrdup(src
[i
].direction
);
611 static pa_hashmap
*create_method_handlers(const pa_dbus_interface_info
*info
) {
612 pa_hashmap
*handlers
;
616 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
618 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
620 for (i
= 0; i
< info
->n_method_handlers
; ++i
) {
621 pa_dbus_method_handler
*h
= pa_xnew(pa_dbus_method_handler
, 1);
622 h
->method_name
= pa_xstrdup(info
->method_handlers
[i
].method_name
);
623 h
->arguments
= copy_args(info
->method_handlers
[i
].arguments
, info
->method_handlers
[i
].n_arguments
);
624 h
->n_arguments
= info
->method_handlers
[i
].n_arguments
;
625 h
->receive_cb
= info
->method_handlers
[i
].receive_cb
;
627 pa_hashmap_put(handlers
, h
->method_name
, h
);
633 static pa_hashmap
*create_property_handlers(const pa_dbus_interface_info
*info
) {
634 pa_hashmap
*handlers
;
638 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
640 handlers
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
642 for (i
= 0; i
< info
->n_property_handlers
; ++i
) {
643 pa_dbus_property_handler
*h
= pa_xnew(pa_dbus_property_handler
, 1);
644 h
->property_name
= pa_xstrdup(info
->property_handlers
[i
].property_name
);
645 h
->type
= pa_xstrdup(info
->property_handlers
[i
].type
);
646 h
->get_cb
= info
->property_handlers
[i
].get_cb
;
647 h
->set_cb
= info
->property_handlers
[i
].set_cb
;
649 pa_hashmap_put(handlers
, h
->property_name
, h
);
655 static pa_dbus_signal_info
*copy_signals(const pa_dbus_interface_info
*info
) {
656 pa_dbus_signal_info
*dst
;
661 if (info
->n_signals
== 0)
664 pa_assert(info
->signals
);
666 dst
= pa_xnew(pa_dbus_signal_info
, info
->n_signals
);
668 for (i
= 0; i
< info
->n_signals
; ++i
) {
669 dst
[i
].name
= pa_xstrdup(info
->signals
[i
].name
);
670 dst
[i
].arguments
= copy_args(info
->signals
[i
].arguments
, info
->signals
[i
].n_arguments
);
671 dst
[i
].n_arguments
= info
->signals
[i
].n_arguments
;
677 int pa_dbus_protocol_add_interface(pa_dbus_protocol
*p
,
679 const pa_dbus_interface_info
*info
,
681 struct object_entry
*obj_entry
;
682 struct interface_entry
*iface_entry
;
683 pa_bool_t obj_entry_created
= FALSE
;
688 pa_assert(info
->name
);
689 pa_assert(info
->method_handlers
|| info
->n_method_handlers
== 0);
690 pa_assert(info
->property_handlers
|| info
->n_property_handlers
== 0);
691 pa_assert(info
->get_all_properties_cb
|| info
->n_property_handlers
== 0);
692 pa_assert(info
->signals
|| info
->n_signals
== 0);
694 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
))) {
695 obj_entry
= pa_xnew(struct object_entry
, 1);
696 obj_entry
->path
= pa_xstrdup(path
);
697 obj_entry
->interfaces
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
698 obj_entry
->introspection
= NULL
;
700 pa_hashmap_put(p
->objects
, path
, obj_entry
);
701 obj_entry_created
= TRUE
;
704 if (pa_hashmap_get(obj_entry
->interfaces
, info
->name
) != NULL
)
705 goto fail
; /* The interface was already registered. */
707 iface_entry
= pa_xnew(struct interface_entry
, 1);
708 iface_entry
->name
= pa_xstrdup(info
->name
);
709 iface_entry
->method_handlers
= create_method_handlers(info
);
710 iface_entry
->property_handlers
= create_property_handlers(info
);
711 iface_entry
->get_all_properties_cb
= info
->get_all_properties_cb
;
712 iface_entry
->signals
= copy_signals(info
);
713 iface_entry
->n_signals
= info
->n_signals
;
714 iface_entry
->userdata
= userdata
;
715 pa_hashmap_put(obj_entry
->interfaces
, iface_entry
->name
, iface_entry
);
717 update_introspection(obj_entry
);
719 if (obj_entry_created
)
720 register_object(p
, obj_entry
);
722 pa_log_debug("Interface %s added for object %s", iface_entry
->name
, obj_entry
->path
);
727 if (obj_entry_created
) {
728 pa_hashmap_remove(p
->objects
, path
);
735 static void unregister_object(pa_dbus_protocol
*p
, struct object_entry
*obj_entry
) {
736 struct connection_entry
*conn_entry
;
740 pa_assert(obj_entry
);
742 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
)
743 pa_assert_se(dbus_connection_unregister_object_path(conn_entry
->connection
, obj_entry
->path
));
746 static void method_handler_free_cb(void *p
, void *userdata
) {
747 pa_dbus_method_handler
*h
= p
;
752 pa_xfree((char *) h
->method_name
);
754 for (i
= 0; i
< h
->n_arguments
; ++i
) {
755 pa_xfree((char *) h
->arguments
[i
].name
);
756 pa_xfree((char *) h
->arguments
[i
].type
);
757 pa_xfree((char *) h
->arguments
[i
].direction
);
760 pa_xfree((pa_dbus_arg_info
*) h
->arguments
);
763 static void property_handler_free_cb(void *p
, void *userdata
) {
764 pa_dbus_property_handler
*h
= p
;
768 pa_xfree((char *) h
->property_name
);
769 pa_xfree((char *) h
->type
);
774 int pa_dbus_protocol_remove_interface(pa_dbus_protocol
*p
, const char* path
, const char* interface
) {
775 struct object_entry
*obj_entry
;
776 struct interface_entry
*iface_entry
;
781 pa_assert(interface
);
783 if (!(obj_entry
= pa_hashmap_get(p
->objects
, path
)))
786 if (!(iface_entry
= pa_hashmap_remove(obj_entry
->interfaces
, interface
)))
789 update_introspection(obj_entry
);
791 pa_log_debug("Interface %s removed from object %s", iface_entry
->name
, obj_entry
->path
);
793 pa_xfree(iface_entry
->name
);
794 pa_hashmap_free(iface_entry
->method_handlers
, method_handler_free_cb
, NULL
);
795 pa_hashmap_free(iface_entry
->property_handlers
, property_handler_free_cb
, NULL
);
797 for (i
= 0; i
< iface_entry
->n_signals
; ++i
) {
800 pa_xfree((char *) iface_entry
->signals
[i
].name
);
802 for (j
= 0; j
< iface_entry
->signals
[i
].n_arguments
; ++j
) {
803 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].name
);
804 pa_xfree((char *) iface_entry
->signals
[i
].arguments
[j
].type
);
805 pa_assert(iface_entry
->signals
[i
].arguments
[j
].direction
== NULL
);
808 pa_xfree((pa_dbus_arg_info
*) iface_entry
->signals
[i
].arguments
);
811 pa_xfree(iface_entry
->signals
);
812 pa_xfree(iface_entry
);
814 if (pa_hashmap_isempty(obj_entry
->interfaces
)) {
815 unregister_object(p
, obj_entry
);
817 pa_hashmap_remove(p
->objects
, path
);
818 pa_xfree(obj_entry
->path
);
819 pa_hashmap_free(obj_entry
->interfaces
, NULL
, NULL
);
820 pa_xfree(obj_entry
->introspection
);
827 static void register_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
828 struct object_entry
*obj_entry
;
834 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
835 pa_assert_se(dbus_connection_register_object_path(conn
, obj_entry
->path
, &vtable
, p
));
838 int pa_dbus_protocol_register_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
, pa_client
*client
) {
839 struct connection_entry
*conn_entry
;
845 if (pa_hashmap_get(p
->connections
, conn
))
846 return -1; /* The connection was already registered. */
848 register_all_objects(p
, conn
);
850 conn_entry
= pa_xnew(struct connection_entry
, 1);
851 conn_entry
->connection
= dbus_connection_ref(conn
);
852 conn_entry
->client
= client
;
853 conn_entry
->listening_for_all_signals
= FALSE
;
854 conn_entry
->all_signals_objects
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
855 conn_entry
->listening_signals
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
857 pa_hashmap_put(p
->connections
, conn
, conn_entry
);
862 static void unregister_all_objects(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
863 struct object_entry
*obj_entry
;
869 PA_HASHMAP_FOREACH(obj_entry
, p
->objects
, state
)
870 pa_assert_se(dbus_connection_unregister_object_path(conn
, obj_entry
->path
));
873 static void free_listened_object_name_cb(void *p
, void *userdata
) {
879 static void free_listening_signals_idxset_cb(void *p
, void *userdata
) {
884 pa_idxset_free(set
, free_listened_object_name_cb
, NULL
);
887 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
888 struct connection_entry
*conn_entry
;
893 if (!(conn_entry
= pa_hashmap_remove(p
->connections
, conn
)))
896 unregister_all_objects(p
, conn
);
898 dbus_connection_unref(conn_entry
->connection
);
899 pa_idxset_free(conn_entry
->all_signals_objects
, free_listened_object_name_cb
, NULL
);
900 pa_hashmap_free(conn_entry
->listening_signals
, free_listening_signals_idxset_cb
, NULL
);
901 pa_xfree(conn_entry
);
906 pa_client
*pa_dbus_protocol_get_client(pa_dbus_protocol
*p
, DBusConnection
*conn
) {
907 struct connection_entry
*conn_entry
;
912 if (!(conn_entry
= pa_hashmap_get(p
->connections
, conn
)))
915 return conn_entry
->client
;
918 void pa_dbus_protocol_add_signal_listener(
920 DBusConnection
*conn
,
923 unsigned n_objects
) {
924 struct connection_entry
*conn_entry
;
925 pa_idxset
*object_set
;
931 pa_assert(objects
|| n_objects
== 0);
933 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
935 /* all_signals_objects will either be emptied or replaced with new objects,
936 * so we empty it here unconditionally. If listening_for_all_signals is
937 * currently FALSE, the idxset is empty already. */
938 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
939 pa_xfree(object_path
);
942 conn_entry
->listening_for_all_signals
= FALSE
;
944 /* Replace the old object list with a new one. */
945 if ((object_set
= pa_hashmap_remove(conn_entry
->listening_signals
, signal
)))
946 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
947 object_set
= pa_idxset_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
949 for (i
= 0; i
< n_objects
; ++i
)
950 pa_idxset_put(object_set
, pa_xstrdup(objects
[i
]), NULL
);
952 pa_hashmap_put(conn_entry
->listening_signals
, signal
, object_set
);
955 conn_entry
->listening_for_all_signals
= TRUE
;
957 /* We're not interested in individual signals anymore, so let's empty
958 * listening_signals. */
959 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
960 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
962 for (i
= 0; i
< n_objects
; ++i
)
963 pa_idxset_put(conn_entry
->all_signals_objects
, pa_xstrdup(objects
[i
]), NULL
);
967 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol
*p
, DBusConnection
*conn
, const char *signal
) {
968 struct connection_entry
*conn_entry
;
969 pa_idxset
*object_set
;
974 pa_assert_se((conn_entry
= pa_hashmap_get(p
->connections
, conn
)));
977 if ((object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal
)))
978 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
983 conn_entry
->listening_for_all_signals
= FALSE
;
985 while ((object_path
= pa_idxset_steal_first(conn_entry
->all_signals_objects
, NULL
)))
986 pa_xfree(object_path
);
988 while ((object_set
= pa_hashmap_steal_first(conn_entry
->listening_signals
)))
989 pa_idxset_free(object_set
, free_listened_object_name_cb
, NULL
);
993 void pa_dbus_protocol_send_signal(pa_dbus_protocol
*p
, DBusMessage
*signal
) {
994 struct connection_entry
*conn_entry
;
996 pa_idxset
*object_set
;
997 DBusMessage
*signal_copy
;
1002 pa_assert(dbus_message_get_type(signal
) == DBUS_MESSAGE_TYPE_SIGNAL
);
1003 pa_assert_se(dbus_message_get_interface(signal
));
1004 pa_assert_se(dbus_message_get_member(signal
));
1006 signal_string
= pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal
), dbus_message_get_member(signal
));
1008 PA_HASHMAP_FOREACH(conn_entry
, p
->connections
, state
) {
1009 if ((conn_entry
->listening_for_all_signals
/* Case 1: listening for all signals */
1010 && (pa_idxset_get_by_data(conn_entry
->all_signals_objects
, dbus_message_get_path(signal
), NULL
)
1011 || pa_idxset_isempty(conn_entry
->all_signals_objects
)))
1013 || (!conn_entry
->listening_for_all_signals
/* Case 2: not listening for all signals */
1014 && (object_set
= pa_hashmap_get(conn_entry
->listening_signals
, signal_string
))
1015 && (pa_idxset_get_by_data(object_set
, dbus_message_get_path(signal
), NULL
)
1016 || pa_idxset_isempty(object_set
)))) {
1018 pa_assert_se(signal_copy
= dbus_message_copy(signal
));
1019 pa_assert_se(dbus_connection_send(conn_entry
->connection
, signal_copy
, NULL
));
1020 dbus_message_unref(signal_copy
);
1024 pa_xfree(signal_string
);
1027 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol
*p
, unsigned *n
) {
1028 const char **extensions
;
1029 const char *ext_name
;
1036 *n
= pa_idxset_size(p
->extensions
);
1041 extensions
= pa_xnew(const char *, *n
);
1043 while ((ext_name
= pa_idxset_iterate(p
->extensions
, &state
, NULL
)))
1044 extensions
[i
++] = ext_name
;
1049 int pa_dbus_protocol_register_extension(pa_dbus_protocol
*p
, const char *name
) {
1050 char *internal_name
;
1055 internal_name
= pa_xstrdup(name
);
1057 if (pa_idxset_put(p
->extensions
, internal_name
, NULL
) < 0) {
1058 pa_xfree(internal_name
);
1062 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED
], internal_name
);
1067 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol
*p
, const char *name
) {
1068 char *internal_name
;
1073 if (!(internal_name
= pa_idxset_remove_by_data(p
->extensions
, name
, NULL
)))
1076 pa_hook_fire(&p
->hooks
[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED
], internal_name
);
1078 pa_xfree(internal_name
);
1083 pa_hook_slot
*pa_dbus_protocol_hook_connect(
1084 pa_dbus_protocol
*p
,
1085 pa_dbus_protocol_hook_t hook
,
1086 pa_hook_priority_t prio
,
1090 pa_assert(hook
< PA_DBUS_PROTOCOL_HOOK_MAX
);
1093 return pa_hook_connect(&p
->hooks
[hook
], prio
, cb
, data
);