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 <pulsecore/core-util.h>
29 #include <pulsecore/dbus-util.h>
30 #include <pulsecore/protocol-dbus.h>
32 #include "iface-card-profile.h"
34 #include "iface-card.h"
36 #define OBJECT_NAME "card"
38 static void handle_get_index(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
39 static void handle_get_name(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
40 static void handle_get_driver(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
41 static void handle_get_owner_module(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
42 static void handle_get_sinks(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
43 static void handle_get_sources(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
44 static void handle_get_profiles(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
45 static void handle_get_active_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
46 static void handle_set_active_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
47 static void handle_get_property_list(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
49 static void handle_get_all(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
51 static void handle_get_profile_by_name(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
);
53 struct pa_dbusiface_card
{
54 pa_dbusiface_core
*core
;
59 uint32_t next_profile_index
;
60 pa_card_profile
*active_profile
;
61 pa_proplist
*proplist
;
63 pa_dbus_protocol
*dbus_protocol
;
64 pa_subscription
*subscription
;
67 enum property_handler_index
{
68 PROPERTY_HANDLER_INDEX
,
69 PROPERTY_HANDLER_NAME
,
70 PROPERTY_HANDLER_DRIVER
,
71 PROPERTY_HANDLER_OWNER_MODULE
,
72 PROPERTY_HANDLER_SINKS
,
73 PROPERTY_HANDLER_SOURCES
,
74 PROPERTY_HANDLER_PROFILES
,
75 PROPERTY_HANDLER_ACTIVE_PROFILE
,
76 PROPERTY_HANDLER_PROPERTY_LIST
,
80 static pa_dbus_property_handler property_handlers
[PROPERTY_HANDLER_MAX
] = {
81 [PROPERTY_HANDLER_INDEX
] = { .property_name
= "Index", .type
= "u", .get_cb
= handle_get_index
, .set_cb
= NULL
},
82 [PROPERTY_HANDLER_NAME
] = { .property_name
= "Name", .type
= "s", .get_cb
= handle_get_name
, .set_cb
= NULL
},
83 [PROPERTY_HANDLER_DRIVER
] = { .property_name
= "Driver", .type
= "s", .get_cb
= handle_get_driver
, .set_cb
= NULL
},
84 [PROPERTY_HANDLER_OWNER_MODULE
] = { .property_name
= "OwnerModule", .type
= "o", .get_cb
= handle_get_owner_module
, .set_cb
= NULL
},
85 [PROPERTY_HANDLER_SINKS
] = { .property_name
= "Sinks", .type
= "ao", .get_cb
= handle_get_sinks
, .set_cb
= NULL
},
86 [PROPERTY_HANDLER_SOURCES
] = { .property_name
= "Sources", .type
= "ao", .get_cb
= handle_get_sources
, .set_cb
= NULL
},
87 [PROPERTY_HANDLER_PROFILES
] = { .property_name
= "Profiles", .type
= "ao", .get_cb
= handle_get_profiles
, .set_cb
= NULL
},
88 [PROPERTY_HANDLER_ACTIVE_PROFILE
] = { .property_name
= "ActiveProfile", .type
= "o", .get_cb
= handle_get_active_profile
, .set_cb
= handle_set_active_profile
},
89 [PROPERTY_HANDLER_PROPERTY_LIST
] = { .property_name
= "PropertyList", .type
= "a{say}", .get_cb
= handle_get_property_list
, .set_cb
= NULL
}
92 enum method_handler_index
{
93 METHOD_HANDLER_GET_PROFILE_BY_NAME
,
97 static pa_dbus_arg_info get_profile_by_name_args
[] = { { "name", "s", "in" }, { "profile", "o", "out" } };
99 static pa_dbus_method_handler method_handlers
[METHOD_HANDLER_MAX
] = {
100 [METHOD_HANDLER_GET_PROFILE_BY_NAME
] = {
101 .method_name
= "GetProfileByName",
102 .arguments
= get_profile_by_name_args
,
103 .n_arguments
= sizeof(get_profile_by_name_args
) / sizeof(pa_dbus_arg_info
),
104 .receive_cb
= handle_get_profile_by_name
}
108 SIGNAL_ACTIVE_PROFILE_UPDATED
,
109 SIGNAL_PROPERTY_LIST_UPDATED
,
113 static pa_dbus_arg_info active_profile_updated_args
[] = { { "profile", "o", NULL
} };
114 static pa_dbus_arg_info property_list_updated_args
[] = { { "property_list", "a{say}", NULL
} };
116 static pa_dbus_signal_info signals
[SIGNAL_MAX
] = {
117 [SIGNAL_ACTIVE_PROFILE_UPDATED
] = { .name
= "ActiveProfileUpdated", .arguments
= active_profile_updated_args
, .n_arguments
= 1 },
118 [SIGNAL_PROPERTY_LIST_UPDATED
] = { .name
= "PropertyListUpdated", .arguments
= property_list_updated_args
, .n_arguments
= 1 }
121 static pa_dbus_interface_info card_interface_info
= {
122 .name
= PA_DBUSIFACE_CARD_INTERFACE
,
123 .method_handlers
= method_handlers
,
124 .n_method_handlers
= METHOD_HANDLER_MAX
,
125 .property_handlers
= property_handlers
,
126 .n_property_handlers
= PROPERTY_HANDLER_MAX
,
127 .get_all_properties_cb
= handle_get_all
,
129 .n_signals
= SIGNAL_MAX
132 static void handle_get_index(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
133 pa_dbusiface_card
*c
= userdata
;
140 idx
= c
->card
->index
;
142 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_UINT32
, &idx
);
145 static void handle_get_name(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
146 pa_dbusiface_card
*c
= userdata
;
152 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_STRING
, &c
->card
->name
);
155 static void handle_get_driver(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
156 pa_dbusiface_card
*c
= userdata
;
162 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_STRING
, &c
->card
->driver
);
165 static void handle_get_owner_module(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
166 pa_dbusiface_card
*c
= userdata
;
167 const char *owner_module
;
173 if (!c
->card
->module
) {
174 pa_dbus_send_error(conn
, msg
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "Card %s doesn't have an owner module.", c
->card
->name
);
178 owner_module
= pa_dbusiface_core_get_module_path(c
->core
, c
->card
->module
);
180 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, &owner_module
);
183 /* The caller frees the array, but not the strings. */
184 static const char **get_sinks(pa_dbusiface_card
*c
, unsigned *n
) {
185 const char **sinks
= NULL
;
188 pa_sink
*sink
= NULL
;
193 *n
= pa_idxset_size(c
->card
->sinks
);
198 sinks
= pa_xnew(const char *, *n
);
200 PA_IDXSET_FOREACH(sink
, c
->card
->sinks
, idx
) {
201 sinks
[i
] = pa_dbusiface_core_get_sink_path(c
->core
, sink
);
208 static void handle_get_sinks(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
209 pa_dbusiface_card
*c
= userdata
;
217 sinks
= get_sinks(c
, &n_sinks
);
219 pa_dbus_send_basic_array_variant_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, sinks
, n_sinks
);
224 /* The caller frees the array, but not the strings. */
225 static const char **get_sources(pa_dbusiface_card
*c
, unsigned *n
) {
226 const char **sources
= NULL
;
229 pa_source
*source
= NULL
;
234 *n
= pa_idxset_size(c
->card
->sources
);
239 sources
= pa_xnew(const char *, *n
);
241 PA_IDXSET_FOREACH(source
, c
->card
->sinks
, idx
) {
242 sources
[i
] = pa_dbusiface_core_get_source_path(c
->core
, source
);
249 static void handle_get_sources(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
250 pa_dbusiface_card
*c
= userdata
;
251 const char **sources
;
258 sources
= get_sources(c
, &n_sources
);
260 pa_dbus_send_basic_array_variant_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, sources
, n_sources
);
265 /* The caller frees the array, but not the strings. */
266 static const char **get_profiles(pa_dbusiface_card
*c
, unsigned *n
) {
267 const char **profiles
;
270 pa_dbusiface_card_profile
*profile
;
275 *n
= pa_hashmap_size(c
->profiles
);
280 profiles
= pa_xnew(const char *, *n
);
282 PA_HASHMAP_FOREACH(profile
, c
->profiles
, state
) {
283 profiles
[i
] = pa_dbusiface_card_profile_get_path(profile
);
290 static void handle_get_profiles(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
291 pa_dbusiface_card
*c
= userdata
;
292 const char **profiles
;
299 profiles
= get_profiles(c
, &n_profiles
);
301 pa_dbus_send_basic_array_variant_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, profiles
, n_profiles
);
306 static void handle_get_active_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
307 pa_dbusiface_card
*c
= userdata
;
308 const char *active_profile
;
314 if (!c
->active_profile
) {
315 pa_dbus_send_error(conn
, msg
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
, "The card %s has no profiles, and therefore there's no active profile either.", c
->card
->name
);
319 active_profile
= pa_dbusiface_card_profile_get_path(pa_hashmap_get(c
->profiles
, c
->active_profile
->name
));
321 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, &active_profile
);
324 static void handle_set_active_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
325 pa_dbusiface_card
*c
= userdata
;
326 const char *new_active_path
;
327 pa_dbusiface_card_profile
*new_active
;
334 if (pa_dbus_get_basic_set_property_arg(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, &new_active_path
) < 0)
337 if (!c
->active_profile
) {
338 pa_dbus_send_error(conn
, msg
, PA_DBUS_ERROR_NO_SUCH_PROPERTY
,
339 "The card %s has no profiles, and therefore there's no active profile either.",
344 if (!(new_active
= pa_hashmap_get(c
->profiles
, new_active_path
))) {
345 pa_dbus_send_error(conn
, msg
, PA_DBUS_ERROR_NOT_FOUND
, "%s: No such profile.", new_active_path
);
349 if ((r
= pa_card_set_profile(c
->card
, pa_dbusiface_card_profile_get_name(new_active
), TRUE
)) < 0) {
350 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_FAILED
,
351 "Internal error in PulseAudio: pa_card_set_profile() failed with error code %i.", r
);
355 pa_dbus_send_empty_reply(conn
, msg
);
358 static void handle_get_property_list(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
359 pa_dbusiface_card
*c
= userdata
;
365 pa_dbus_send_proplist_variant_reply(conn
, msg
, c
->proplist
);
368 static void handle_get_all(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
369 pa_dbusiface_card
*c
= userdata
;
370 DBusMessage
*reply
= NULL
;
371 DBusMessageIter msg_iter
;
372 DBusMessageIter dict_iter
;
374 const char *owner_module
= NULL
;
375 const char **sinks
= NULL
;
376 unsigned n_sinks
= 0;
377 const char **sources
= NULL
;
378 unsigned n_sources
= 0;
379 const char **profiles
= NULL
;
380 unsigned n_profiles
= 0;
381 const char *active_profile
= NULL
;
387 idx
= c
->card
->index
;
389 owner_module
= pa_dbusiface_core_get_module_path(c
->core
, c
->card
->module
);
390 sinks
= get_sinks(c
, &n_sinks
);
391 sources
= get_sources(c
, &n_sources
);
392 profiles
= get_profiles(c
, &n_profiles
);
393 if (c
->active_profile
)
394 active_profile
= pa_dbusiface_card_profile_get_path(pa_hashmap_get(c
->profiles
, c
->active_profile
->name
));
396 pa_assert_se((reply
= dbus_message_new_method_return(msg
)));
398 dbus_message_iter_init_append(reply
, &msg_iter
);
399 pa_assert_se(dbus_message_iter_open_container(&msg_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
));
401 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_INDEX
].property_name
, DBUS_TYPE_UINT32
, &idx
);
402 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_NAME
].property_name
, DBUS_TYPE_STRING
, &c
->card
->name
);
403 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_DRIVER
].property_name
, DBUS_TYPE_STRING
, &c
->card
->driver
);
406 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_OWNER_MODULE
].property_name
, DBUS_TYPE_STRING
, &owner_module
);
408 pa_dbus_append_basic_array_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_SINKS
].property_name
, DBUS_TYPE_OBJECT_PATH
, sinks
, n_sinks
);
409 pa_dbus_append_basic_array_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_SOURCES
].property_name
, DBUS_TYPE_OBJECT_PATH
, sources
, n_sources
);
410 pa_dbus_append_basic_array_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_PROFILES
].property_name
, DBUS_TYPE_OBJECT_PATH
, profiles
, n_profiles
);
413 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_ACTIVE_PROFILE
].property_name
, DBUS_TYPE_OBJECT_PATH
, &active_profile
);
415 pa_dbus_append_proplist_variant_dict_entry(&dict_iter
, property_handlers
[PROPERTY_HANDLER_PROPERTY_LIST
].property_name
, c
->proplist
);
417 pa_assert_se(dbus_message_iter_close_container(&msg_iter
, &dict_iter
));
419 pa_assert_se(dbus_connection_send(conn
, reply
, NULL
));
421 dbus_message_unref(reply
);
428 static void handle_get_profile_by_name(DBusConnection
*conn
, DBusMessage
*msg
, void *userdata
) {
429 pa_dbusiface_card
*c
= userdata
;
431 const char *profile_name
= NULL
;
432 pa_dbusiface_card_profile
*profile
= NULL
;
433 const char *profile_path
= NULL
;
439 dbus_error_init(&error
);
441 if (!dbus_message_get_args(msg
, &error
, DBUS_TYPE_STRING
, &profile_name
, DBUS_TYPE_INVALID
)) {
442 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
443 dbus_error_free(&error
);
447 if (!(profile
= pa_hashmap_get(c
->profiles
, profile_name
))) {
448 pa_dbus_send_error(conn
, msg
, PA_DBUS_ERROR_NOT_FOUND
, "%s: No such profile on card %s.", profile_name
, c
->card
->name
);
452 profile_path
= pa_dbusiface_card_profile_get_path(profile
);
454 pa_dbus_send_basic_value_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, &profile_path
);
457 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
458 pa_dbusiface_card
*c
= userdata
;
461 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_CARD
);
464 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_CHANGE
) {
465 DBusMessage
*signal
= NULL
;
467 if (c
->active_profile
!= c
->card
->active_profile
) {
468 const char *object_path
;
470 c
->active_profile
= c
->card
->active_profile
;
471 object_path
= pa_dbusiface_card_profile_get_path(pa_hashmap_get(c
->profiles
, c
->active_profile
->name
));
473 pa_assert_se(signal
= dbus_message_new_signal(c
->path
, PA_DBUSIFACE_CARD_INTERFACE
, signals
[SIGNAL_ACTIVE_PROFILE_UPDATED
].name
));
474 pa_assert_se(dbus_message_append_args(signal
, DBUS_TYPE_OBJECT_PATH
, &object_path
, DBUS_TYPE_INVALID
));
476 pa_dbus_protocol_send_signal(c
->dbus_protocol
, signal
);
477 dbus_message_unref(signal
);
481 if (!pa_proplist_equal(c
->proplist
, c
->card
->proplist
)) {
482 DBusMessageIter msg_iter
;
484 pa_proplist_update(c
->proplist
, PA_UPDATE_SET
, c
->card
->proplist
);
486 pa_assert_se(signal
= dbus_message_new_signal(c
->path
, PA_DBUSIFACE_CARD_INTERFACE
, signals
[SIGNAL_PROPERTY_LIST_UPDATED
].name
));
487 dbus_message_iter_init_append(signal
, &msg_iter
);
488 pa_dbus_append_proplist(&msg_iter
, c
->proplist
);
490 pa_dbus_protocol_send_signal(c
->dbus_protocol
, signal
);
491 dbus_message_unref(signal
);
497 pa_dbusiface_card
*pa_dbusiface_card_new(pa_dbusiface_core
*core
, pa_card
*card
) {
498 pa_dbusiface_card
*c
= NULL
;
503 c
= pa_xnew0(pa_dbusiface_card
, 1);
506 c
->path
= pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH
, OBJECT_NAME
, card
->index
);
507 c
->profiles
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
508 c
->next_profile_index
= 0;
509 c
->active_profile
= NULL
;
510 c
->proplist
= pa_proplist_copy(card
->proplist
);
511 c
->dbus_protocol
= pa_dbus_protocol_get(card
->core
);
512 c
->subscription
= pa_subscription_new(card
->core
, PA_SUBSCRIPTION_MASK_CARD
, subscription_cb
, c
);
514 if (card
->profiles
) {
515 pa_card_profile
*profile
;
518 PA_HASHMAP_FOREACH(profile
, card
->profiles
, state
) {
519 pa_dbusiface_card_profile
*p
= pa_dbusiface_card_profile_new(c
, card
->core
, profile
, c
->next_profile_index
++);
520 pa_hashmap_put(c
->profiles
, pa_dbusiface_card_profile_get_name(p
), p
);
522 pa_assert_se(c
->active_profile
= card
->active_profile
);
525 pa_assert_se(pa_dbus_protocol_add_interface(c
->dbus_protocol
, c
->path
, &card_interface_info
, c
) >= 0);
530 static void profile_free_cb(void *p
, void *userdata
) {
531 pa_dbusiface_card_profile
*profile
= p
;
535 pa_dbusiface_card_profile_free(profile
);
538 void pa_dbusiface_card_free(pa_dbusiface_card
*c
) {
541 pa_assert_se(pa_dbus_protocol_remove_interface(c
->dbus_protocol
, c
->path
, card_interface_info
.name
) >= 0);
543 pa_hashmap_free(c
->profiles
, profile_free_cb
, NULL
);
544 pa_proplist_free(c
->proplist
);
545 pa_dbus_protocol_unref(c
->dbus_protocol
);
546 pa_subscription_free(c
->subscription
);
552 const char *pa_dbusiface_card_get_path(pa_dbusiface_card
*c
) {