• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   Copyright 2009 Tanu Kaskinen
5 
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.
10 
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.
15 
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <dbus/dbus.h>
25 
26 #include <pulsecore/core-util.h>
27 #include <pulsecore/dbus-util.h>
28 #include <pulsecore/protocol-dbus.h>
29 
30 #include "iface-card-profile.h"
31 
32 #include "iface-card.h"
33 
34 #define OBJECT_NAME "card"
35 
36 static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
37 static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
38 static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata);
39 static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
40 static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata);
41 static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata);
42 static void handle_get_profiles(DBusConnection *conn, DBusMessage *msg, void *userdata);
43 static void handle_get_active_profile(DBusConnection *conn, DBusMessage *msg, void *userdata);
44 static void handle_set_active_profile(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
45 static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
46 
47 static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
48 
49 static void handle_get_profile_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
50 
51 struct pa_dbusiface_card {
52     pa_dbusiface_core *core;
53 
54     pa_card *card;
55     char *path;
56     pa_hashmap *profiles;
57     uint32_t next_profile_index;
58     pa_card_profile *active_profile;
59     pa_proplist *proplist;
60 
61     pa_hook_slot *card_profile_added_slot;
62     pa_hook_slot *card_profile_changed_slot;
63     pa_hook_slot *card_profile_available_slot;
64 
65     pa_dbus_protocol *dbus_protocol;
66 };
67 
68 enum property_handler_index {
69     PROPERTY_HANDLER_INDEX,
70     PROPERTY_HANDLER_NAME,
71     PROPERTY_HANDLER_DRIVER,
72     PROPERTY_HANDLER_OWNER_MODULE,
73     PROPERTY_HANDLER_SINKS,
74     PROPERTY_HANDLER_SOURCES,
75     PROPERTY_HANDLER_PROFILES,
76     PROPERTY_HANDLER_ACTIVE_PROFILE,
77     PROPERTY_HANDLER_PROPERTY_LIST,
78     PROPERTY_HANDLER_MAX
79 };
80 
81 static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
82     [PROPERTY_HANDLER_INDEX]          = { .property_name = "Index",         .type = "u",      .get_cb = handle_get_index,          .set_cb = NULL },
83     [PROPERTY_HANDLER_NAME]           = { .property_name = "Name",          .type = "s",      .get_cb = handle_get_name,           .set_cb = NULL },
84     [PROPERTY_HANDLER_DRIVER]         = { .property_name = "Driver",        .type = "s",      .get_cb = handle_get_driver,         .set_cb = NULL },
85     [PROPERTY_HANDLER_OWNER_MODULE]   = { .property_name = "OwnerModule",   .type = "o",      .get_cb = handle_get_owner_module,   .set_cb = NULL },
86     [PROPERTY_HANDLER_SINKS]          = { .property_name = "Sinks",         .type = "ao",     .get_cb = handle_get_sinks,          .set_cb = NULL },
87     [PROPERTY_HANDLER_SOURCES]        = { .property_name = "Sources",       .type = "ao",     .get_cb = handle_get_sources,        .set_cb = NULL },
88     [PROPERTY_HANDLER_PROFILES]       = { .property_name = "Profiles",      .type = "ao",     .get_cb = handle_get_profiles,       .set_cb = NULL },
89     [PROPERTY_HANDLER_ACTIVE_PROFILE] = { .property_name = "ActiveProfile", .type = "o",      .get_cb = handle_get_active_profile, .set_cb = handle_set_active_profile },
90     [PROPERTY_HANDLER_PROPERTY_LIST]  = { .property_name = "PropertyList",  .type = "a{say}", .get_cb = handle_get_property_list,  .set_cb = NULL }
91 };
92 
93 enum method_handler_index {
94     METHOD_HANDLER_GET_PROFILE_BY_NAME,
95     METHOD_HANDLER_MAX
96 };
97 
98 static pa_dbus_arg_info get_profile_by_name_args[] = { { "name", "s", "in" }, { "profile", "o", "out" } };
99 
100 static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
101     [METHOD_HANDLER_GET_PROFILE_BY_NAME] = {
102         .method_name = "GetProfileByName",
103         .arguments = get_profile_by_name_args,
104         .n_arguments = sizeof(get_profile_by_name_args) / sizeof(pa_dbus_arg_info),
105         .receive_cb = handle_get_profile_by_name }
106 };
107 
108 enum signal_index {
109     SIGNAL_ACTIVE_PROFILE_UPDATED,
110     SIGNAL_NEW_PROFILE,
111     SIGNAL_PROFILE_AVAILABLE_CHANGED,
112     SIGNAL_PROPERTY_LIST_UPDATED,
113     SIGNAL_MAX
114 };
115 
116 static pa_dbus_arg_info active_profile_updated_args[]    = { { "profile",       "o",      NULL } };
117 static pa_dbus_arg_info new_profile_args[]               = { { "profile",       "o",      NULL } };
118 static pa_dbus_arg_info profile_available_changed_args[] = { { "profile",       "o",      NULL },
119                                                              { "available",     "b",      NULL } };
120 static pa_dbus_arg_info property_list_updated_args[]     = { { "property_list", "a{say}", NULL } };
121 
122 static pa_dbus_signal_info signals[SIGNAL_MAX] = {
123     [SIGNAL_ACTIVE_PROFILE_UPDATED]     = { .name = "ActiveProfileUpdated",     .arguments = active_profile_updated_args,    .n_arguments = 1 },
124     [SIGNAL_NEW_PROFILE]                = { .name = "NewProfile",               .arguments = new_profile_args,               .n_arguments = 1 },
125     [SIGNAL_PROFILE_AVAILABLE_CHANGED]  = { .name = "ProfileAvailableChanged",  .arguments = profile_available_changed_args, .n_arguments = 2 },
126     [SIGNAL_PROPERTY_LIST_UPDATED]      = { .name = "PropertyListUpdated",      .arguments = property_list_updated_args,     .n_arguments = 1 }
127 };
128 
129 static pa_dbus_interface_info card_interface_info = {
130     .name = PA_DBUSIFACE_CARD_INTERFACE,
131     .method_handlers = method_handlers,
132     .n_method_handlers = METHOD_HANDLER_MAX,
133     .property_handlers = property_handlers,
134     .n_property_handlers = PROPERTY_HANDLER_MAX,
135     .get_all_properties_cb = handle_get_all,
136     .signals = signals,
137     .n_signals = SIGNAL_MAX
138 };
139 
handle_get_index(DBusConnection * conn,DBusMessage * msg,void * userdata)140 static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
141     pa_dbusiface_card *c = userdata;
142     dbus_uint32_t idx;
143 
144     pa_assert(conn);
145     pa_assert(msg);
146     pa_assert(c);
147 
148     idx = c->card->index;
149 
150     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
151 }
152 
handle_get_name(DBusConnection * conn,DBusMessage * msg,void * userdata)153 static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
154     pa_dbusiface_card *c = userdata;
155 
156     pa_assert(conn);
157     pa_assert(msg);
158     pa_assert(c);
159 
160     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &c->card->name);
161 }
162 
handle_get_driver(DBusConnection * conn,DBusMessage * msg,void * userdata)163 static void handle_get_driver(DBusConnection *conn, DBusMessage *msg, void *userdata) {
164     pa_dbusiface_card *c = userdata;
165 
166     pa_assert(conn);
167     pa_assert(msg);
168     pa_assert(c);
169 
170     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &c->card->driver);
171 }
172 
handle_get_owner_module(DBusConnection * conn,DBusMessage * msg,void * userdata)173 static void handle_get_owner_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
174     pa_dbusiface_card *c = userdata;
175     const char *owner_module;
176 
177     pa_assert(conn);
178     pa_assert(msg);
179     pa_assert(c);
180 
181     if (!c->card->module) {
182         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "Card %s doesn't have an owner module.", c->card->name);
183         return;
184     }
185 
186     owner_module = pa_dbusiface_core_get_module_path(c->core, c->card->module);
187 
188     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &owner_module);
189 }
190 
191 /* The caller frees the array, but not the strings. */
get_sinks(pa_dbusiface_card * c,unsigned * n)192 static const char **get_sinks(pa_dbusiface_card *c, unsigned *n) {
193     const char **sinks = NULL;
194     unsigned i = 0;
195     uint32_t idx = 0;
196     pa_sink *sink = NULL;
197 
198     pa_assert(c);
199     pa_assert(n);
200 
201     *n = pa_idxset_size(c->card->sinks);
202 
203     if (*n == 0)
204         return NULL;
205 
206     sinks = pa_xnew(const char *, *n);
207 
208     PA_IDXSET_FOREACH(sink, c->card->sinks, idx) {
209         sinks[i] = pa_dbusiface_core_get_sink_path(c->core, sink);
210         ++i;
211     }
212 
213     return sinks;
214 }
215 
handle_get_sinks(DBusConnection * conn,DBusMessage * msg,void * userdata)216 static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
217     pa_dbusiface_card *c = userdata;
218     const char **sinks;
219     unsigned n_sinks;
220 
221     pa_assert(conn);
222     pa_assert(msg);
223     pa_assert(c);
224 
225     sinks = get_sinks(c, &n_sinks);
226 
227     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sinks, n_sinks);
228 
229     pa_xfree(sinks);
230 }
231 
232 /* The caller frees the array, but not the strings. */
get_sources(pa_dbusiface_card * c,unsigned * n)233 static const char **get_sources(pa_dbusiface_card *c, unsigned *n) {
234     const char **sources = NULL;
235     unsigned i = 0;
236     uint32_t idx = 0;
237     pa_source *source = NULL;
238 
239     pa_assert(c);
240     pa_assert(n);
241 
242     *n = pa_idxset_size(c->card->sources);
243 
244     if (*n == 0)
245         return NULL;
246 
247     sources = pa_xnew(const char *, *n);
248 
249     PA_IDXSET_FOREACH(source, c->card->sources, idx) {
250         sources[i] = pa_dbusiface_core_get_source_path(c->core, source);
251         ++i;
252     }
253 
254     return sources;
255 }
256 
handle_get_sources(DBusConnection * conn,DBusMessage * msg,void * userdata)257 static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata) {
258     pa_dbusiface_card *c = userdata;
259     const char **sources;
260     unsigned n_sources;
261 
262     pa_assert(conn);
263     pa_assert(msg);
264     pa_assert(c);
265 
266     sources = get_sources(c, &n_sources);
267 
268     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sources, n_sources);
269 
270     pa_xfree(sources);
271 }
272 
273 /* The caller frees the array, but not the strings. */
get_profiles(pa_dbusiface_card * c,unsigned * n)274 static const char **get_profiles(pa_dbusiface_card *c, unsigned *n) {
275     const char **profiles;
276     unsigned i = 0;
277     void *state = NULL;
278     pa_dbusiface_card_profile *profile;
279 
280     pa_assert(c);
281     pa_assert(n);
282 
283     *n = pa_hashmap_size(c->profiles);
284 
285     if (*n == 0)
286         return NULL;
287 
288     profiles = pa_xnew(const char *, *n);
289 
290     PA_HASHMAP_FOREACH(profile, c->profiles, state)
291         profiles[i++] = pa_dbusiface_card_profile_get_path(profile);
292 
293     return profiles;
294 }
295 
handle_get_profiles(DBusConnection * conn,DBusMessage * msg,void * userdata)296 static void handle_get_profiles(DBusConnection *conn, DBusMessage *msg, void *userdata) {
297     pa_dbusiface_card *c = userdata;
298     const char **profiles;
299     unsigned n_profiles;
300 
301     pa_assert(conn);
302     pa_assert(msg);
303     pa_assert(c);
304 
305     profiles = get_profiles(c, &n_profiles);
306 
307     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, profiles, n_profiles);
308 
309     pa_xfree(profiles);
310 }
311 
handle_get_active_profile(DBusConnection * conn,DBusMessage * msg,void * userdata)312 static void handle_get_active_profile(DBusConnection *conn, DBusMessage *msg, void *userdata) {
313     pa_dbusiface_card *c = userdata;
314     const char *active_profile;
315 
316     pa_assert(conn);
317     pa_assert(msg);
318     pa_assert(c);
319 
320     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);
322 }
323 
handle_set_active_profile(DBusConnection * conn,DBusMessage * msg,DBusMessageIter * iter,void * userdata)324 static void handle_set_active_profile(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
325     pa_dbusiface_card *c = userdata;
326     const char *new_active_path;
327     pa_dbusiface_card_profile *profile;
328     void *state;
329     pa_dbusiface_card_profile *new_active = NULL;
330     int r;
331 
332     pa_assert(conn);
333     pa_assert(msg);
334     pa_assert(iter);
335     pa_assert(c);
336 
337     dbus_message_iter_get_basic(iter, &new_active_path);
338 
339     PA_HASHMAP_FOREACH(profile, c->profiles, state) {
340         if (pa_streq(pa_dbusiface_card_profile_get_path(profile), new_active_path)) {
341             new_active = profile;
342             break;
343         }
344     }
345 
346     if (!new_active) {
347         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such profile.", new_active_path);
348         return;
349     }
350 
351     if ((r = pa_card_set_profile(c->card, pa_dbusiface_card_profile_get_profile(new_active), true)) < 0) {
352         pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
353                            "Internal error in PulseAudio: pa_card_set_profile() failed with error code %i.", r);
354         return;
355     }
356 
357     pa_dbus_send_empty_reply(conn, msg);
358 }
359 
handle_get_property_list(DBusConnection * conn,DBusMessage * msg,void * userdata)360 static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
361     pa_dbusiface_card *c = userdata;
362 
363     pa_assert(conn);
364     pa_assert(msg);
365     pa_assert(c);
366 
367     pa_dbus_send_proplist_variant_reply(conn, msg, c->proplist);
368 }
369 
handle_get_all(DBusConnection * conn,DBusMessage * msg,void * userdata)370 static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
371     pa_dbusiface_card *c = userdata;
372     DBusMessage *reply = NULL;
373     DBusMessageIter msg_iter;
374     DBusMessageIter dict_iter;
375     dbus_uint32_t idx;
376     const char *owner_module = NULL;
377     const char **sinks = NULL;
378     unsigned n_sinks = 0;
379     const char **sources = NULL;
380     unsigned n_sources = 0;
381     const char **profiles = NULL;
382     unsigned n_profiles = 0;
383     const char *active_profile = NULL;
384 
385     pa_assert(conn);
386     pa_assert(msg);
387     pa_assert(c);
388 
389     idx = c->card->index;
390     if (c->card->module)
391         owner_module = pa_dbusiface_core_get_module_path(c->core, c->card->module);
392     sinks = get_sinks(c, &n_sinks);
393     sources = get_sources(c, &n_sources);
394     profiles = get_profiles(c, &n_profiles);
395     active_profile = pa_dbusiface_card_profile_get_path(pa_hashmap_get(c->profiles, c->active_profile->name));
396 
397     pa_assert_se((reply = dbus_message_new_method_return(msg)));
398 
399     dbus_message_iter_init_append(reply, &msg_iter);
400     pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
401 
402     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
403     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &c->card->name);
404     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DRIVER].property_name, DBUS_TYPE_STRING, &c->card->driver);
405 
406     if (owner_module)
407         pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_OWNER_MODULE].property_name, DBUS_TYPE_OBJECT_PATH, &owner_module);
408 
409     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SINKS].property_name, DBUS_TYPE_OBJECT_PATH, sinks, n_sinks);
410     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SOURCES].property_name, DBUS_TYPE_OBJECT_PATH, sources, n_sources);
411     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROFILES].property_name, DBUS_TYPE_OBJECT_PATH, profiles, n_profiles);
412     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ACTIVE_PROFILE].property_name, DBUS_TYPE_OBJECT_PATH, &active_profile);
413 
414     pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, c->proplist);
415 
416     pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
417 
418     pa_assert_se(dbus_connection_send(conn, reply, NULL));
419 
420     dbus_message_unref(reply);
421 
422     pa_xfree(sinks);
423     pa_xfree(sources);
424     pa_xfree(profiles);
425 }
426 
handle_get_profile_by_name(DBusConnection * conn,DBusMessage * msg,void * userdata)427 static void handle_get_profile_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
428     pa_dbusiface_card *c = userdata;
429     const char *profile_name = NULL;
430     pa_dbusiface_card_profile *profile = NULL;
431     const char *profile_path = NULL;
432 
433     pa_assert(conn);
434     pa_assert(msg);
435     pa_assert(c);
436 
437     pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &profile_name, DBUS_TYPE_INVALID));
438 
439     if (!(profile = pa_hashmap_get(c->profiles, profile_name))) {
440         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such profile on card %s.", profile_name, c->card->name);
441         return;
442     }
443 
444     profile_path = pa_dbusiface_card_profile_get_path(profile);
445 
446     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &profile_path);
447 }
448 
check_card_proplist(pa_dbusiface_card * c)449 static void check_card_proplist(pa_dbusiface_card *c) {
450     DBusMessage *signal_msg;
451 
452     if (!pa_proplist_equal(c->proplist, c->card->proplist)) {
453         DBusMessageIter msg_iter;
454 
455         pa_proplist_update(c->proplist, PA_UPDATE_SET, c->card->proplist);
456 
457         pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
458                                                           PA_DBUSIFACE_CARD_INTERFACE,
459                                                           signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
460         dbus_message_iter_init_append(signal_msg, &msg_iter);
461         pa_dbus_append_proplist(&msg_iter, c->proplist);
462 
463         pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
464         dbus_message_unref(signal_msg);
465     }
466 }
467 
card_profile_changed_cb(void * hook_data,void * call_data,void * slot_data)468 static pa_hook_result_t card_profile_changed_cb(void *hook_data, void *call_data, void *slot_data) {
469     pa_dbusiface_card *dbus_card = slot_data;
470     pa_card *core_card = call_data;
471     const char *object_path;
472     DBusMessage *signal_msg;
473 
474     if (dbus_card->card != core_card)
475         return PA_HOOK_OK;
476 
477     dbus_card->active_profile = dbus_card->card->active_profile;
478 
479     object_path = pa_dbusiface_card_profile_get_path(pa_hashmap_get(dbus_card->profiles, dbus_card->active_profile->name));
480 
481     pa_assert_se(signal_msg = dbus_message_new_signal(dbus_card->path,
482                                                       PA_DBUSIFACE_CARD_INTERFACE,
483                                                       signals[SIGNAL_ACTIVE_PROFILE_UPDATED].name));
484     pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
485 
486     pa_dbus_protocol_send_signal(dbus_card->dbus_protocol, signal_msg);
487     dbus_message_unref(signal_msg);
488 
489     check_card_proplist(dbus_card);
490 
491     return PA_HOOK_OK;
492 }
493 
card_profile_added_cb(void * hook_data,void * call_data,void * slot_data)494 static pa_hook_result_t card_profile_added_cb(void *hook_data, void *call_data, void *slot_data) {
495     pa_core *core = hook_data;
496     pa_dbusiface_card *c = slot_data;
497     pa_card_profile *profile = call_data;
498     pa_dbusiface_card_profile *p;
499     const char *object_path;
500     DBusMessage *signal_msg;
501 
502     if (profile->card != c->card)
503         return PA_HOOK_OK;
504 
505     p = pa_dbusiface_card_profile_new(c, core, profile, c->next_profile_index++);
506     pa_assert_se(pa_hashmap_put(c->profiles, (char *) pa_dbusiface_card_profile_get_name(p), p) >= 0);
507 
508     /* Send D-Bus signal */
509     object_path = pa_dbusiface_card_profile_get_path(p);
510 
511     pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
512                                                       PA_DBUSIFACE_CARD_INTERFACE,
513                                                       signals[SIGNAL_NEW_PROFILE].name));
514     pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
515 
516     pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
517     dbus_message_unref(signal_msg);
518 
519     check_card_proplist(c);
520 
521     return PA_HOOK_OK;
522 }
523 
card_profile_available_changed_cb(void * hook_data,void * call_data,void * slot_data)524 static pa_hook_result_t card_profile_available_changed_cb(void *hook_data, void *call_data, void *slot_data) {
525     pa_dbusiface_card *c = slot_data;
526     pa_card_profile *profile = call_data;
527     pa_dbusiface_card_profile *p;
528     const char *object_path;
529     dbus_bool_t available;
530     DBusMessage *signal_msg;
531 
532     if (profile->card != c->card)
533         return PA_HOOK_OK;
534 
535     pa_assert_se((p = pa_hashmap_get(c->profiles, profile->name)));
536 
537     object_path = pa_dbusiface_card_profile_get_path(p);
538     available = profile->available != PA_AVAILABLE_NO;
539 
540     pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
541                                                       PA_DBUSIFACE_CARD_INTERFACE,
542                                                       signals[SIGNAL_PROFILE_AVAILABLE_CHANGED].name));
543     pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path,
544                                                       DBUS_TYPE_BOOLEAN, &available,
545                                                       DBUS_TYPE_INVALID));
546 
547     pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
548     dbus_message_unref(signal_msg);
549 
550     check_card_proplist(c);
551 
552     return PA_HOOK_OK;
553 }
554 
pa_dbusiface_card_new(pa_dbusiface_core * core,pa_card * card)555 pa_dbusiface_card *pa_dbusiface_card_new(pa_dbusiface_core *core, pa_card *card) {
556     pa_dbusiface_card *c = NULL;
557     pa_card_profile *profile;
558     void *state;
559 
560     pa_assert(core);
561     pa_assert(card);
562 
563     c = pa_xnew0(pa_dbusiface_card, 1);
564     c->core = core;
565     c->card = card;
566     c->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME, card->index);
567     c->profiles = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL,
568                                       (pa_free_cb_t) pa_dbusiface_card_profile_free);
569     c->next_profile_index = 0;
570     c->active_profile = card->active_profile;
571     c->proplist = pa_proplist_copy(card->proplist);
572     c->dbus_protocol = pa_dbus_protocol_get(card->core);
573 
574     PA_HASHMAP_FOREACH(profile, card->profiles, state) {
575         pa_dbusiface_card_profile *p = pa_dbusiface_card_profile_new(c, card->core, profile, c->next_profile_index++);
576         pa_hashmap_put(c->profiles, (char *) pa_dbusiface_card_profile_get_name(p), p);
577     }
578 
579     pa_assert_se(pa_dbus_protocol_add_interface(c->dbus_protocol, c->path, &card_interface_info, c) >= 0);
580 
581     c->card_profile_changed_slot = pa_hook_connect(&card->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], PA_HOOK_NORMAL,
582                                                    card_profile_changed_cb, c);
583     c->card_profile_added_slot = pa_hook_connect(&card->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], PA_HOOK_NORMAL,
584                                                  card_profile_added_cb, c);
585     c->card_profile_available_slot = pa_hook_connect(&card->core->hooks[PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED], PA_HOOK_NORMAL,
586                                                      card_profile_available_changed_cb, c);
587 
588     return c;
589 }
590 
pa_dbusiface_card_free(pa_dbusiface_card * c)591 void pa_dbusiface_card_free(pa_dbusiface_card *c) {
592     pa_assert(c);
593 
594     pa_assert_se(pa_dbus_protocol_remove_interface(c->dbus_protocol, c->path, card_interface_info.name) >= 0);
595 
596     pa_hook_slot_free(c->card_profile_added_slot);
597     pa_hook_slot_free(c->card_profile_changed_slot);
598     pa_hook_slot_free(c->card_profile_available_slot);
599 
600     pa_hashmap_free(c->profiles);
601     pa_proplist_free(c->proplist);
602     pa_dbus_protocol_unref(c->dbus_protocol);
603 
604     pa_xfree(c->path);
605     pa_xfree(c);
606 }
607 
pa_dbusiface_card_get_path(pa_dbusiface_card * c)608 const char *pa_dbusiface_card_get_path(pa_dbusiface_card *c) {
609     pa_assert(c);
610 
611     return c->path;
612 }
613