dbus: Do message argument type checking early, centrally.
[profile/ivi/pulseaudio.git] / src / modules / dbus / iface-core.c
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, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <ctype.h>
27
28 #include <dbus/dbus.h>
29
30 #include <pulse/utf8.h>
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/dbus-util.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/protocol-dbus.h>
38 #include <pulsecore/socket-util.h>
39 #include <pulsecore/strbuf.h>
40
41 #include "iface-card.h"
42 #include "iface-client.h"
43 #include "iface-device.h"
44 #include "iface-memstats.h"
45 #include "iface-module.h"
46 #include "iface-sample.h"
47 #include "iface-stream.h"
48
49 #include "iface-core.h"
50
51 #define INTERFACE_REVISION 0
52
53 static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata);
54 static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
55 static void handle_get_version(DBusConnection *conn, DBusMessage *msg, void *userdata);
56 static void handle_get_is_local(DBusConnection *conn, DBusMessage *msg, void *userdata);
57 static void handle_get_username(DBusConnection *conn, DBusMessage *msg, void *userdata);
58 static void handle_get_hostname(DBusConnection *conn, DBusMessage *msg, void *userdata);
59 static void handle_get_default_channels(DBusConnection *conn, DBusMessage *msg, void *userdata);
60 static void handle_set_default_channels(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
61 static void handle_get_default_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata);
62 static void handle_set_default_sample_format(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
63 static void handle_get_default_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata);
64 static void handle_set_default_sample_rate(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
65 static void handle_get_cards(DBusConnection *conn, DBusMessage *msg, void *userdata);
66 static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata);
67 static void handle_get_fallback_sink(DBusConnection *conn, DBusMessage *msg, void *userdata);
68 static void handle_set_fallback_sink(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
69 static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata);
70 static void handle_get_fallback_source(DBusConnection *conn, DBusMessage *msg, void *userdata);
71 static void handle_set_fallback_source(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
72 static void handle_get_playback_streams(DBusConnection *conn, DBusMessage *msg, void *userdata);
73 static void handle_get_record_streams(DBusConnection *conn, DBusMessage *msg, void *userdata);
74 static void handle_get_samples(DBusConnection *conn, DBusMessage *msg, void *userdata);
75 static void handle_get_modules(DBusConnection *conn, DBusMessage *msg, void *userdata);
76 static void handle_get_clients(DBusConnection *conn, DBusMessage *msg, void *userdata);
77 static void handle_get_my_client(DBusConnection *conn, DBusMessage *msg, void *userdata);
78 static void handle_get_extensions(DBusConnection *conn, DBusMessage *msg, void *userdata);
79
80 static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
81
82 static void handle_get_card_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
83 static void handle_get_sink_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
84 static void handle_get_source_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
85 static void handle_get_sample_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
86 static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *userdata);
87 static void handle_load_module(DBusConnection *conn, DBusMessage *msg, void *userdata);
88 static void handle_exit(DBusConnection *conn, DBusMessage *msg, void *userdata);
89 static void handle_listen_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata);
90 static void handle_stop_listening_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata);
91
92 struct pa_dbusiface_core {
93     pa_core *core;
94     pa_subscription *subscription;
95
96     pa_dbus_protocol *dbus_protocol;
97
98     pa_hashmap *cards;
99     pa_hashmap *sinks_by_index;
100     pa_hashmap *sinks_by_path;
101     pa_hashmap *sources_by_index;
102     pa_hashmap *sources_by_path;
103     pa_hashmap *playback_streams;
104     pa_hashmap *record_streams;
105     pa_hashmap *samples;
106     pa_hashmap *modules;
107     pa_hashmap *clients;
108
109     pa_sink *fallback_sink;
110     pa_source *fallback_source;
111
112     pa_hook_slot *extension_registered_slot;
113     pa_hook_slot *extension_unregistered_slot;
114
115     pa_dbusiface_memstats *memstats;
116 };
117
118 enum property_handler_index {
119     PROPERTY_HANDLER_INTERFACE_REVISION,
120     PROPERTY_HANDLER_NAME,
121     PROPERTY_HANDLER_VERSION,
122     PROPERTY_HANDLER_IS_LOCAL,
123     PROPERTY_HANDLER_USERNAME,
124     PROPERTY_HANDLER_HOSTNAME,
125     PROPERTY_HANDLER_DEFAULT_CHANNELS,
126     PROPERTY_HANDLER_DEFAULT_SAMPLE_FORMAT,
127     PROPERTY_HANDLER_DEFAULT_SAMPLE_RATE,
128     PROPERTY_HANDLER_CARDS,
129     PROPERTY_HANDLER_SINKS,
130     PROPERTY_HANDLER_FALLBACK_SINK,
131     PROPERTY_HANDLER_SOURCES,
132     PROPERTY_HANDLER_FALLBACK_SOURCE,
133     PROPERTY_HANDLER_PLAYBACK_STREAMS,
134     PROPERTY_HANDLER_RECORD_STREAMS,
135     PROPERTY_HANDLER_SAMPLES,
136     PROPERTY_HANDLER_MODULES,
137     PROPERTY_HANDLER_CLIENTS,
138     PROPERTY_HANDLER_MY_CLIENT,
139     PROPERTY_HANDLER_EXTENSIONS,
140     PROPERTY_HANDLER_MAX
141 };
142
143 static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
144     [PROPERTY_HANDLER_INTERFACE_REVISION]    = { .property_name = "InterfaceRevision",   .type = "u",  .get_cb = handle_get_interface_revision,    .set_cb = NULL },
145     [PROPERTY_HANDLER_NAME]                  = { .property_name = "Name",                .type = "s",  .get_cb = handle_get_name,                  .set_cb = NULL },
146     [PROPERTY_HANDLER_VERSION]               = { .property_name = "Version",             .type = "s",  .get_cb = handle_get_version,               .set_cb = NULL },
147     [PROPERTY_HANDLER_IS_LOCAL]              = { .property_name = "IsLocal",             .type = "b",  .get_cb = handle_get_is_local,              .set_cb = NULL },
148     [PROPERTY_HANDLER_USERNAME]              = { .property_name = "Username",            .type = "s",  .get_cb = handle_get_username,              .set_cb = NULL },
149     [PROPERTY_HANDLER_HOSTNAME]              = { .property_name = "Hostname",            .type = "s",  .get_cb = handle_get_hostname,              .set_cb = NULL },
150     [PROPERTY_HANDLER_DEFAULT_CHANNELS]      = { .property_name = "DefaultChannels",     .type = "au", .get_cb = handle_get_default_channels,      .set_cb = handle_set_default_channels },
151     [PROPERTY_HANDLER_DEFAULT_SAMPLE_FORMAT] = { .property_name = "DefaultSampleFormat", .type = "u",  .get_cb = handle_get_default_sample_format, .set_cb = handle_set_default_sample_format },
152     [PROPERTY_HANDLER_DEFAULT_SAMPLE_RATE]   = { .property_name = "DefaultSampleRate",   .type = "u",  .get_cb = handle_get_default_sample_rate,   .set_cb = handle_set_default_sample_rate },
153     [PROPERTY_HANDLER_CARDS]                 = { .property_name = "Cards",               .type = "ao", .get_cb = handle_get_cards,                 .set_cb = NULL },
154     [PROPERTY_HANDLER_SINKS]                 = { .property_name = "Sinks",               .type = "ao", .get_cb = handle_get_sinks,                 .set_cb = NULL },
155     [PROPERTY_HANDLER_FALLBACK_SINK]         = { .property_name = "FallbackSink",        .type = "o",  .get_cb = handle_get_fallback_sink,         .set_cb = handle_set_fallback_sink },
156     [PROPERTY_HANDLER_SOURCES]               = { .property_name = "Sources",             .type = "ao", .get_cb = handle_get_sources,               .set_cb = NULL },
157     [PROPERTY_HANDLER_FALLBACK_SOURCE]       = { .property_name = "FallbackSource",      .type = "o",  .get_cb = handle_get_fallback_source,       .set_cb = handle_set_fallback_source },
158     [PROPERTY_HANDLER_PLAYBACK_STREAMS]      = { .property_name = "PlaybackStreams",     .type = "ao", .get_cb = handle_get_playback_streams,      .set_cb = NULL },
159     [PROPERTY_HANDLER_RECORD_STREAMS]        = { .property_name = "RecordStreams",       .type = "ao", .get_cb = handle_get_record_streams,        .set_cb = NULL },
160     [PROPERTY_HANDLER_SAMPLES]               = { .property_name = "Samples",             .type = "ao", .get_cb = handle_get_samples,               .set_cb = NULL },
161     [PROPERTY_HANDLER_MODULES]               = { .property_name = "Modules",             .type = "ao", .get_cb = handle_get_modules,               .set_cb = NULL },
162     [PROPERTY_HANDLER_CLIENTS]               = { .property_name = "Clients",             .type = "ao", .get_cb = handle_get_clients,               .set_cb = NULL },
163     [PROPERTY_HANDLER_MY_CLIENT]             = { .property_name = "MyClient",            .type = "o",  .get_cb = handle_get_my_client,             .set_cb = NULL },
164     [PROPERTY_HANDLER_EXTENSIONS]            = { .property_name = "Extensions",          .type = "as", .get_cb = handle_get_extensions,            .set_cb = NULL }
165 };
166
167 enum method_handler_index {
168     METHOD_HANDLER_GET_CARD_BY_NAME,
169     METHOD_HANDLER_GET_SINK_BY_NAME,
170     METHOD_HANDLER_GET_SOURCE_BY_NAME,
171     METHOD_HANDLER_GET_SAMPLE_BY_NAME,
172     METHOD_HANDLER_UPLOAD_SAMPLE,
173     METHOD_HANDLER_LOAD_MODULE,
174     METHOD_HANDLER_EXIT,
175     METHOD_HANDLER_LISTEN_FOR_SIGNAL,
176     METHOD_HANDLER_STOP_LISTENING_FOR_SIGNAL,
177     METHOD_HANDLER_MAX
178 };
179
180 static pa_dbus_arg_info get_card_by_name_args[] = { { "name", "s", "in" }, { "card", "o", "out" } };
181 static pa_dbus_arg_info get_sink_by_name_args[] = { { "name", "s", "in" }, { "sink", "o", "out" } };
182 static pa_dbus_arg_info get_source_by_name_args[] = { { "name", "s", "in" }, { "source", "o", "out" } };
183 static pa_dbus_arg_info get_sample_by_name_args[] = { { "name", "s", "in" }, { "sample", "o", "out" } };
184 static pa_dbus_arg_info upload_sample_args[] = { { "name",           "s",      "in" },
185                                                  { "sample_format",  "u",      "in" },
186                                                  { "sample_rate",    "u",      "in" },
187                                                  { "channels",       "au",     "in" },
188                                                  { "default_volume", "au",     "in" },
189                                                  { "property_list",  "a{say}", "in" },
190                                                  { "data",           "ay",     "in" },
191                                                  { "sample",         "o",      "out" } };
192 static pa_dbus_arg_info load_module_args[] = { { "name", "s", "in" }, { "arguments", "a{ss}", "in" }, { "module", "o", "out" } };
193 static pa_dbus_arg_info listen_for_signal_args[] = { { "signal", "s", "in" }, { "objects", "ao", "in" } };
194 static pa_dbus_arg_info stop_listening_for_signal_args[] = { { "signal", "s", "in" } };
195
196 static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
197     [METHOD_HANDLER_GET_CARD_BY_NAME] = {
198         .method_name = "GetCardByName",
199         .arguments = get_card_by_name_args,
200         .n_arguments = sizeof(get_card_by_name_args) / sizeof(pa_dbus_arg_info),
201         .receive_cb = handle_get_card_by_name },
202     [METHOD_HANDLER_GET_SINK_BY_NAME] = {
203         .method_name = "GetSinkByName",
204         .arguments = get_sink_by_name_args,
205         .n_arguments = sizeof(get_sink_by_name_args) / sizeof(pa_dbus_arg_info),
206         .receive_cb = handle_get_sink_by_name },
207     [METHOD_HANDLER_GET_SOURCE_BY_NAME] = {
208         .method_name = "GetSourceByName",
209         .arguments = get_source_by_name_args,
210         .n_arguments = sizeof(get_source_by_name_args) / sizeof(pa_dbus_arg_info),
211         .receive_cb = handle_get_source_by_name },
212     [METHOD_HANDLER_GET_SAMPLE_BY_NAME] = {
213         .method_name = "GetSampleByName",
214         .arguments = get_sample_by_name_args,
215         .n_arguments = sizeof(get_sample_by_name_args) / sizeof(pa_dbus_arg_info),
216         .receive_cb = handle_get_sample_by_name },
217     [METHOD_HANDLER_UPLOAD_SAMPLE] = {
218         .method_name = "UploadSample",
219         .arguments = upload_sample_args,
220         .n_arguments = sizeof(upload_sample_args) / sizeof(pa_dbus_arg_info),
221         .receive_cb = handle_upload_sample },
222     [METHOD_HANDLER_LOAD_MODULE] = {
223         .method_name = "LoadModule",
224         .arguments = load_module_args,
225         .n_arguments = sizeof(load_module_args) / sizeof(pa_dbus_arg_info),
226         .receive_cb = handle_load_module },
227     [METHOD_HANDLER_EXIT] = {
228         .method_name = "Exit",
229         .arguments = NULL,
230         .n_arguments = 0,
231         .receive_cb = handle_exit },
232     [METHOD_HANDLER_LISTEN_FOR_SIGNAL] = {
233         .method_name = "ListenForSignal",
234         .arguments = listen_for_signal_args,
235         .n_arguments = sizeof(listen_for_signal_args) / sizeof(pa_dbus_arg_info),
236         .receive_cb = handle_listen_for_signal },
237     [METHOD_HANDLER_STOP_LISTENING_FOR_SIGNAL] = {
238         .method_name = "StopListeningForSignal",
239         .arguments = stop_listening_for_signal_args,
240         .n_arguments = sizeof(stop_listening_for_signal_args) / sizeof(pa_dbus_arg_info),
241         .receive_cb = handle_stop_listening_for_signal }
242 };
243
244 enum signal_index {
245     SIGNAL_NEW_CARD,
246     SIGNAL_CARD_REMOVED,
247     SIGNAL_NEW_SINK,
248     SIGNAL_SINK_REMOVED,
249     SIGNAL_FALLBACK_SINK_UPDATED,
250     SIGNAL_NEW_SOURCE,
251     SIGNAL_SOURCE_REMOVED,
252     SIGNAL_FALLBACK_SOURCE_UPDATED,
253     SIGNAL_NEW_PLAYBACK_STREAM,
254     SIGNAL_PLAYBACK_STREAM_REMOVED,
255     SIGNAL_NEW_RECORD_STREAM,
256     SIGNAL_RECORD_STREAM_REMOVED,
257     SIGNAL_NEW_SAMPLE,
258     SIGNAL_SAMPLE_REMOVED,
259     SIGNAL_NEW_MODULE,
260     SIGNAL_MODULE_REMOVED,
261     SIGNAL_NEW_CLIENT,
262     SIGNAL_CLIENT_REMOVED,
263     SIGNAL_NEW_EXTENSION,
264     SIGNAL_EXTENSION_REMOVED,
265     SIGNAL_MAX
266 };
267
268 static pa_dbus_arg_info new_card_args[] =                { { "card",            "o", NULL } };
269 static pa_dbus_arg_info card_removed_args[] =            { { "card",            "o", NULL } };
270 static pa_dbus_arg_info new_sink_args[] =                { { "sink",            "o", NULL } };
271 static pa_dbus_arg_info sink_removed_args[] =            { { "sink",            "o", NULL } };
272 static pa_dbus_arg_info fallback_sink_updated_args[] =   { { "sink",            "o", NULL } };
273 static pa_dbus_arg_info new_source_args[] =              { { "source",          "o", NULL } };
274 static pa_dbus_arg_info source_removed_args[] =          { { "source",          "o", NULL } };
275 static pa_dbus_arg_info fallback_source_updated_args[] = { { "source",          "o", NULL } };
276 static pa_dbus_arg_info new_playback_stream_args[] =     { { "playback_stream", "o", NULL } };
277 static pa_dbus_arg_info playback_stream_removed_args[] = { { "playback_stream", "o", NULL } };
278 static pa_dbus_arg_info new_record_stream_args[] =       { { "record_stream",   "o", NULL } };
279 static pa_dbus_arg_info record_stream_removed_args[] =   { { "record_stream",   "o", NULL } };
280 static pa_dbus_arg_info new_sample_args[] =              { { "sample",          "o", NULL } };
281 static pa_dbus_arg_info sample_removed_args[] =          { { "sample",          "o", NULL } };
282 static pa_dbus_arg_info new_module_args[] =              { { "module",          "o", NULL } };
283 static pa_dbus_arg_info module_removed_args[] =          { { "module",          "o", NULL } };
284 static pa_dbus_arg_info new_client_args[] =              { { "client",          "o", NULL } };
285 static pa_dbus_arg_info client_removed_args[] =          { { "client",          "o", NULL } };
286 static pa_dbus_arg_info new_extension_args[] =           { { "extension",       "s", NULL } };
287 static pa_dbus_arg_info extension_removed_args[] =       { { "extension",       "s", NULL } };
288
289 static pa_dbus_signal_info signals[SIGNAL_MAX] = {
290     [SIGNAL_NEW_CARD]                = { .name = "NewCard",               .arguments = new_card_args,                .n_arguments = 1 },
291     [SIGNAL_CARD_REMOVED]            = { .name = "CardRemoved",           .arguments = card_removed_args,            .n_arguments = 1 },
292     [SIGNAL_NEW_SINK]                = { .name = "NewSink",               .arguments = new_sink_args,                .n_arguments = 1 },
293     [SIGNAL_SINK_REMOVED]            = { .name = "SinkRemoved",           .arguments = sink_removed_args,            .n_arguments = 1 },
294     [SIGNAL_FALLBACK_SINK_UPDATED]   = { .name = "FallbackSinkUpdated",   .arguments = fallback_sink_updated_args,   .n_arguments = 1 },
295     [SIGNAL_NEW_SOURCE]              = { .name = "NewSource",             .arguments = new_source_args,              .n_arguments = 1 },
296     [SIGNAL_SOURCE_REMOVED]          = { .name = "SourceRemoved",         .arguments = source_removed_args,          .n_arguments = 1 },
297     [SIGNAL_FALLBACK_SOURCE_UPDATED] = { .name = "FallbackSourceUpdated", .arguments = fallback_source_updated_args, .n_arguments = 1 },
298     [SIGNAL_NEW_PLAYBACK_STREAM]     = { .name = "NewPlaybackStream",     .arguments = new_playback_stream_args,     .n_arguments = 1 },
299     [SIGNAL_PLAYBACK_STREAM_REMOVED] = { .name = "PlaybackStreamRemoved", .arguments = playback_stream_removed_args, .n_arguments = 1 },
300     [SIGNAL_NEW_RECORD_STREAM]       = { .name = "NewRecordStream",       .arguments = new_record_stream_args,       .n_arguments = 1 },
301     [SIGNAL_RECORD_STREAM_REMOVED]   = { .name = "RecordStreamRemoved",   .arguments = record_stream_removed_args,   .n_arguments = 1 },
302     [SIGNAL_NEW_SAMPLE]              = { .name = "NewSample",             .arguments = new_sample_args,              .n_arguments = 1 },
303     [SIGNAL_SAMPLE_REMOVED]          = { .name = "SampleRemoved",         .arguments = sample_removed_args,          .n_arguments = 1 },
304     [SIGNAL_NEW_MODULE]              = { .name = "NewModule",             .arguments = new_module_args,              .n_arguments = 1 },
305     [SIGNAL_MODULE_REMOVED]          = { .name = "ModuleRemoved",         .arguments = module_removed_args,          .n_arguments = 1 },
306     [SIGNAL_NEW_CLIENT]              = { .name = "NewClient",             .arguments = new_client_args,              .n_arguments = 1 },
307     [SIGNAL_CLIENT_REMOVED]          = { .name = "ClientRemoved",         .arguments = client_removed_args,          .n_arguments = 1 },
308     [SIGNAL_NEW_EXTENSION]           = { .name = "NewExtension",          .arguments = new_extension_args,           .n_arguments = 1 },
309     [SIGNAL_EXTENSION_REMOVED]       = { .name = "ExtensionRemoved",      .arguments = extension_removed_args,       .n_arguments = 1 }
310 };
311
312 static pa_dbus_interface_info core_interface_info = {
313     .name = PA_DBUS_CORE_INTERFACE,
314     .method_handlers = method_handlers,
315     .n_method_handlers = METHOD_HANDLER_MAX,
316     .property_handlers = property_handlers,
317     .n_property_handlers = PROPERTY_HANDLER_MAX,
318     .get_all_properties_cb = handle_get_all,
319     .signals = signals,
320     .n_signals = SIGNAL_MAX
321 };
322
323 static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata) {
324     dbus_uint32_t interface_revision = INTERFACE_REVISION;
325
326     pa_assert(conn);
327     pa_assert(msg);
328
329     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &interface_revision);
330 }
331
332 static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
333     const char *server_name = PACKAGE_NAME;
334
335     pa_assert(conn);
336     pa_assert(msg);
337
338     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &server_name);
339 }
340
341 static void handle_get_version(DBusConnection *conn, DBusMessage *msg, void *userdata) {
342     const char *version = PACKAGE_VERSION;
343
344     pa_assert(conn);
345     pa_assert(msg);
346
347     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &version);
348 }
349
350 static dbus_bool_t get_is_local(DBusConnection *conn) {
351     int conn_fd;
352
353     pa_assert(conn);
354
355     if (!dbus_connection_get_socket(conn, &conn_fd))
356         return FALSE;
357
358     return pa_socket_is_local(conn_fd);
359 }
360
361 static void handle_get_is_local(DBusConnection *conn, DBusMessage *msg, void *userdata) {
362     dbus_bool_t is_local;
363
364     pa_assert(conn);
365     pa_assert(msg);
366
367     is_local = get_is_local(conn);
368
369     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &is_local);
370 }
371
372 static void handle_get_username(DBusConnection *conn, DBusMessage *msg, void *userdata) {
373     char *username = NULL;
374
375     pa_assert(conn);
376     pa_assert(msg);
377
378     username = pa_get_user_name_malloc();
379
380     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &username);
381
382     pa_xfree(username);
383 }
384
385 static void handle_get_hostname(DBusConnection *conn, DBusMessage *msg, void *userdata) {
386     char *hostname = NULL;
387
388     pa_assert(conn);
389     pa_assert(msg);
390
391     hostname = pa_get_host_name_malloc();
392
393     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &hostname);
394
395     pa_xfree(hostname);
396 }
397
398 /* Caller frees the returned array. */
399 static dbus_uint32_t *get_default_channels(pa_dbusiface_core *c, unsigned *n) {
400     dbus_uint32_t *default_channels = NULL;
401     unsigned i;
402
403     pa_assert(c);
404     pa_assert(n);
405
406     *n = c->core->default_channel_map.channels;
407     default_channels = pa_xnew(dbus_uint32_t, *n);
408
409     for (i = 0; i < *n; ++i)
410         default_channels[i] = c->core->default_channel_map.map[i];
411
412     return default_channels;
413 }
414
415 static void handle_get_default_channels(DBusConnection *conn, DBusMessage *msg, void *userdata) {
416     pa_dbusiface_core *c = userdata;
417     dbus_uint32_t *default_channels = NULL;
418     unsigned n;
419
420     pa_assert(conn);
421     pa_assert(msg);
422     pa_assert(c);
423
424     default_channels = get_default_channels(c, &n);
425
426     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, default_channels, n);
427
428     pa_xfree(default_channels);
429 }
430
431 static void handle_set_default_channels(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
432     pa_dbusiface_core *c = userdata;
433     DBusMessageIter array_iter;
434     pa_channel_map new_channel_map;
435     const dbus_uint32_t *default_channels;
436     int n_channels;
437     unsigned i;
438
439     pa_assert(conn);
440     pa_assert(msg);
441     pa_assert(iter);
442     pa_assert(c);
443
444     pa_channel_map_init(&new_channel_map);
445
446     dbus_message_iter_recurse(iter, &array_iter);
447     dbus_message_iter_get_fixed_array(&array_iter, &default_channels, &n_channels);
448
449     if (n_channels <= 0) {
450         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Empty channel array.");
451         return;
452     }
453
454     if (n_channels > (int) PA_CHANNELS_MAX) {
455         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
456                            "Too many channels: %i. The maximum number of channels is %u.", n_channels, PA_CHANNELS_MAX);
457         return;
458     }
459
460     new_channel_map.channels = n_channels;
461
462     for (i = 0; i < new_channel_map.channels; ++i) {
463         if (default_channels[i] >= PA_CHANNEL_POSITION_MAX) {
464             pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position: %u.", default_channels[i]);
465             return;
466         }
467
468         new_channel_map.map[i] = default_channels[i];
469     }
470
471     c->core->default_channel_map = new_channel_map;
472     c->core->default_sample_spec.channels = n_channels;
473
474     pa_dbus_send_empty_reply(conn, msg);
475 };
476
477 static void handle_get_default_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata) {
478     pa_dbusiface_core *c = userdata;
479     dbus_uint32_t default_sample_format;
480
481     pa_assert(conn);
482     pa_assert(msg);
483     pa_assert(c);
484
485     default_sample_format = c->core->default_sample_spec.format;
486
487     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &default_sample_format);
488 }
489
490 static void handle_set_default_sample_format(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
491     pa_dbusiface_core *c = userdata;
492     dbus_uint32_t default_sample_format;
493
494     pa_assert(conn);
495     pa_assert(msg);
496     pa_assert(iter);
497     pa_assert(c);
498
499     dbus_message_iter_get_basic(iter, &default_sample_format);
500
501     if (default_sample_format >= PA_SAMPLE_MAX) {
502         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
503         return;
504     }
505
506     c->core->default_sample_spec.format = default_sample_format;
507
508     pa_dbus_send_empty_reply(conn, msg);
509 }
510
511 static void handle_get_default_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata) {
512     pa_dbusiface_core *c = userdata;
513     dbus_uint32_t default_sample_rate;
514
515     pa_assert(conn);
516     pa_assert(msg);
517     pa_assert(c);
518
519     default_sample_rate = c->core->default_sample_spec.rate;
520
521     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &default_sample_rate);
522 }
523
524 static void handle_set_default_sample_rate(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
525     pa_dbusiface_core *c = userdata;
526     dbus_uint32_t default_sample_rate;
527
528     pa_assert(conn);
529     pa_assert(msg);
530     pa_assert(iter);
531     pa_assert(c);
532
533     dbus_message_iter_get_basic(iter, &default_sample_rate);
534
535     if (default_sample_rate <= 0 || default_sample_rate > PA_RATE_MAX) {
536         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
537         return;
538     }
539
540     c->core->default_sample_spec.rate = default_sample_rate;
541
542     pa_dbus_send_empty_reply(conn, msg);
543 }
544
545 /* The caller frees the array, but not the strings. */
546 static const char **get_cards(pa_dbusiface_core *c, unsigned *n) {
547     const char **cards;
548     unsigned i = 0;
549     void *state = NULL;
550     pa_dbusiface_card *card;
551
552     pa_assert(c);
553     pa_assert(n);
554
555     *n = pa_hashmap_size(c->cards);
556
557     if (*n == 0)
558         return NULL;
559
560     cards = pa_xnew(const char *, *n);
561
562     PA_HASHMAP_FOREACH(card, c->cards, state)
563         cards[i++] = pa_dbusiface_card_get_path(card);
564
565     return cards;
566 }
567
568 static void handle_get_cards(DBusConnection *conn, DBusMessage *msg, void *userdata) {
569     pa_dbusiface_core *c = userdata;
570     const char **cards;
571     unsigned n;
572
573     pa_assert(conn);
574     pa_assert(msg);
575     pa_assert(c);
576
577     cards = get_cards(c, &n);
578
579     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, cards, n);
580
581     pa_xfree(cards);
582 }
583
584 /* The caller frees the array, but not the strings. */
585 static const char **get_sinks(pa_dbusiface_core *c, unsigned *n) {
586     const char **sinks;
587     unsigned i = 0;
588     void *state = NULL;
589     pa_dbusiface_device *sink;
590
591     pa_assert(c);
592     pa_assert(n);
593
594     *n = pa_hashmap_size(c->sinks_by_index);
595
596     if (*n == 0)
597         return NULL;
598
599     sinks = pa_xnew(const char *, *n);
600
601     PA_HASHMAP_FOREACH(sink, c->sinks_by_index, state)
602         sinks[i++] = pa_dbusiface_device_get_path(sink);
603
604     return sinks;
605 }
606
607 static void handle_get_sinks(DBusConnection *conn, DBusMessage *msg, void *userdata) {
608     pa_dbusiface_core *c = userdata;
609     const char **sinks;
610     unsigned n;
611
612     pa_assert(conn);
613     pa_assert(msg);
614     pa_assert(c);
615
616     sinks = get_sinks(c, &n);
617
618     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sinks, n);
619
620     pa_xfree(sinks);
621 }
622
623 static void handle_get_fallback_sink(DBusConnection *conn, DBusMessage *msg, void *userdata) {
624     pa_dbusiface_core *c = userdata;
625     pa_dbusiface_device *fallback_sink;
626     const char *object_path;
627
628     pa_assert(conn);
629     pa_assert(msg);
630     pa_assert(c);
631
632     if (!c->fallback_sink) {
633         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
634                            "There are no sinks, and therefore no fallback sink either.");
635         return;
636     }
637
638     pa_assert_se((fallback_sink = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(c->fallback_sink->index))));
639     object_path = pa_dbusiface_device_get_path(fallback_sink);
640
641     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
642 }
643
644 static void handle_set_fallback_sink(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
645     pa_dbusiface_core *c = userdata;
646     pa_dbusiface_device *fallback_sink;
647     const char *object_path;
648
649     pa_assert(conn);
650     pa_assert(msg);
651     pa_assert(iter);
652     pa_assert(c);
653
654     if (!c->fallback_sink) {
655         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
656                            "There are no sinks, and therefore no fallback sink either.");
657         return;
658     }
659
660     dbus_message_iter_get_basic(iter, &object_path);
661
662     if (!(fallback_sink = pa_hashmap_get(c->sinks_by_path, object_path))) {
663         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", object_path);
664         return;
665     }
666
667     pa_namereg_set_default_sink(c->core, pa_dbusiface_device_get_sink(fallback_sink));
668
669     pa_dbus_send_empty_reply(conn, msg);
670 }
671
672 /* The caller frees the array, but not the strings. */
673 static const char **get_sources(pa_dbusiface_core *c, unsigned *n) {
674     const char **sources;
675     unsigned i = 0;
676     void *state = NULL;
677     pa_dbusiface_device *source;
678
679     pa_assert(c);
680     pa_assert(n);
681
682     *n = pa_hashmap_size(c->sources_by_index);
683
684     if (*n == 0)
685         return NULL;
686
687     sources = pa_xnew(const char *, *n);
688
689     PA_HASHMAP_FOREACH(source, c->sources_by_index, state)
690         sources[i++] = pa_dbusiface_device_get_path(source);
691
692     return sources;
693 }
694
695 static void handle_get_sources(DBusConnection *conn, DBusMessage *msg, void *userdata) {
696     pa_dbusiface_core *c = userdata;
697     const char **sources;
698     unsigned n;
699
700     pa_assert(conn);
701     pa_assert(msg);
702     pa_assert(c);
703
704     sources = get_sources(c, &n);
705
706     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, sources, n);
707
708     pa_xfree(sources);
709 }
710
711 static void handle_get_fallback_source(DBusConnection *conn, DBusMessage *msg, void *userdata) {
712     pa_dbusiface_core *c = userdata;
713     pa_dbusiface_device *fallback_source;
714     const char *object_path;
715
716     pa_assert(conn);
717     pa_assert(msg);
718     pa_assert(c);
719
720     if (!c->fallback_source) {
721         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
722                            "There are no sources, and therefore no fallback source either.");
723         return;
724     }
725
726     pa_assert_se((fallback_source = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(c->fallback_source->index))));
727     object_path = pa_dbusiface_device_get_path(fallback_source);
728
729     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
730 }
731
732 static void handle_set_fallback_source(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
733     pa_dbusiface_core *c = userdata;
734     pa_dbusiface_device *fallback_source;
735     const char *object_path;
736
737     pa_assert(conn);
738     pa_assert(msg);
739     pa_assert(iter);
740     pa_assert(c);
741
742     if (!c->fallback_source) {
743         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
744                            "There are no sources, and therefore no fallback source either.");
745         return;
746     }
747
748     dbus_message_iter_get_basic(iter, &object_path);
749
750     if (!(fallback_source = pa_hashmap_get(c->sources_by_path, object_path))) {
751         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such source.", object_path);
752         return;
753     }
754
755     pa_namereg_set_default_source(c->core, pa_dbusiface_device_get_source(fallback_source));
756
757     pa_dbus_send_empty_reply(conn, msg);
758 }
759
760 /* The caller frees the array, but not the strings. */
761 static const char **get_playback_streams(pa_dbusiface_core *c, unsigned *n) {
762     const char **streams;
763     unsigned i = 0;
764     void *state = NULL;
765     pa_dbusiface_stream *stream;
766
767     pa_assert(c);
768     pa_assert(n);
769
770     *n = pa_hashmap_size(c->playback_streams);
771
772     if (*n == 0)
773         return NULL;
774
775     streams = pa_xnew(const char *, *n);
776
777     PA_HASHMAP_FOREACH(stream, c->playback_streams, state)
778         streams[i++] = pa_dbusiface_stream_get_path(stream);
779
780     return streams;
781 }
782
783 static void handle_get_playback_streams(DBusConnection *conn, DBusMessage *msg, void *userdata) {
784     pa_dbusiface_core *c = userdata;
785     const char **playback_streams;
786     unsigned n;
787
788     pa_assert(conn);
789     pa_assert(msg);
790     pa_assert(c);
791
792     playback_streams = get_playback_streams(c, &n);
793
794     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, playback_streams, n);
795
796     pa_xfree(playback_streams);
797 }
798
799 /* The caller frees the array, but not the strings. */
800 static const char **get_record_streams(pa_dbusiface_core *c, unsigned *n) {
801     const char **streams;
802     unsigned i = 0;
803     void *state = NULL;
804     pa_dbusiface_stream *stream;
805
806     pa_assert(c);
807     pa_assert(n);
808
809     *n = pa_hashmap_size(c->record_streams);
810
811     if (*n == 0)
812         return NULL;
813
814     streams = pa_xnew(const char *, *n);
815
816     PA_HASHMAP_FOREACH(stream, c->record_streams, state)
817         streams[i++] = pa_dbusiface_stream_get_path(stream);
818
819     return streams;
820 }
821
822 static void handle_get_record_streams(DBusConnection *conn, DBusMessage *msg, void *userdata) {
823     pa_dbusiface_core *c = userdata;
824     const char **record_streams;
825     unsigned n;
826
827     pa_assert(conn);
828     pa_assert(msg);
829     pa_assert(c);
830
831     record_streams = get_record_streams(c, &n);
832
833     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, record_streams, n);
834
835     pa_xfree(record_streams);
836 }
837
838 /* The caller frees the array, but not the strings. */
839 static const char **get_samples(pa_dbusiface_core *c, unsigned *n) {
840     const char **samples;
841     unsigned i = 0;
842     void *state = NULL;
843     pa_dbusiface_sample *sample;
844
845     pa_assert(c);
846     pa_assert(n);
847
848     *n = pa_hashmap_size(c->samples);
849
850     if (*n == 0)
851         return NULL;
852
853     samples = pa_xnew(const char *, *n);
854
855     PA_HASHMAP_FOREACH(sample, c->samples, state)
856         samples[i++] = pa_dbusiface_sample_get_path(sample);
857
858     return samples;
859 }
860
861 static void handle_get_samples(DBusConnection *conn, DBusMessage *msg, void *userdata) {
862     pa_dbusiface_core *c = userdata;
863     const char **samples;
864     unsigned n;
865
866     pa_assert(conn);
867     pa_assert(msg);
868     pa_assert(c);
869
870     samples = get_samples(c, &n);
871
872     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, samples, n);
873
874     pa_xfree(samples);
875 }
876
877 /* The caller frees the array, but not the strings. */
878 static const char **get_modules(pa_dbusiface_core *c, unsigned *n) {
879     const char **modules;
880     unsigned i = 0;
881     void *state = NULL;
882     pa_dbusiface_module *module;
883
884     pa_assert(c);
885     pa_assert(n);
886
887     *n = pa_hashmap_size(c->modules);
888
889     if (*n == 0)
890         return NULL;
891
892     modules = pa_xnew(const char *, *n);
893
894     PA_HASHMAP_FOREACH(module, c->modules, state)
895         modules[i++] = pa_dbusiface_module_get_path(module);
896
897     return modules;
898 }
899
900 static void handle_get_modules(DBusConnection *conn, DBusMessage *msg, void *userdata) {
901     pa_dbusiface_core *c = userdata;
902     const char **modules;
903     unsigned n;
904
905     pa_assert(conn);
906     pa_assert(msg);
907     pa_assert(c);
908
909     modules = get_modules(c, &n);
910
911     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, modules, n);
912
913     pa_xfree(modules);
914 }
915
916 /* The caller frees the array, but not the strings. */
917 static const char **get_clients(pa_dbusiface_core *c, unsigned *n) {
918     const char **clients;
919     unsigned i = 0;
920     void *state = NULL;
921     pa_dbusiface_client *client;
922
923     pa_assert(c);
924     pa_assert(n);
925
926     *n = pa_hashmap_size(c->clients);
927
928     if (*n == 0)
929         return NULL;
930
931     clients = pa_xnew(const char *, *n);
932
933     PA_HASHMAP_FOREACH(client, c->clients, state)
934         clients[i++] = pa_dbusiface_client_get_path(client);
935
936     return clients;
937 }
938
939 static void handle_get_clients(DBusConnection *conn, DBusMessage *msg, void *userdata) {
940     pa_dbusiface_core *c = userdata;
941     const char **clients;
942     unsigned n;
943
944     pa_assert(conn);
945     pa_assert(msg);
946     pa_assert(c);
947
948     clients = get_clients(c, &n);
949
950     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, clients, n);
951
952     pa_xfree(clients);
953 }
954
955 static const char *get_my_client(pa_dbusiface_core *c, DBusConnection *conn) {
956     pa_client *my_client;
957
958     pa_assert(c);
959     pa_assert(conn);
960
961     pa_assert_se((my_client = pa_dbus_protocol_get_client(c->dbus_protocol, conn)));
962
963     return pa_dbusiface_client_get_path(pa_hashmap_get(c->clients, PA_UINT32_TO_PTR(my_client->index)));
964 }
965
966 static void handle_get_my_client(DBusConnection *conn, DBusMessage *msg, void *userdata) {
967     pa_dbusiface_core *c = userdata;
968     const char *my_client;
969
970     pa_assert(conn);
971     pa_assert(msg);
972     pa_assert(c);
973
974     my_client = get_my_client(c, conn);
975
976     pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &my_client);
977 }
978
979 static void handle_get_extensions(DBusConnection *conn, DBusMessage *msg, void *userdata) {
980     pa_dbusiface_core *c = userdata;
981     const char **extensions;
982     unsigned n;
983
984     pa_assert(conn);
985     pa_assert(msg);
986     pa_assert(c);
987
988     extensions = pa_dbus_protocol_get_extensions(c->dbus_protocol, &n);
989
990     pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_STRING, extensions, n);
991
992     pa_xfree(extensions);
993 }
994
995 static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
996     pa_dbusiface_core *c = userdata;
997     DBusMessage *reply = NULL;
998     DBusMessageIter msg_iter;
999     DBusMessageIter dict_iter;
1000     dbus_uint32_t interface_revision;
1001     const char *server_name;
1002     const char *version;
1003     dbus_bool_t is_local;
1004     char *username;
1005     char *hostname;
1006     dbus_uint32_t *default_channels;
1007     unsigned n_default_channels;
1008     dbus_uint32_t default_sample_format;
1009     dbus_uint32_t default_sample_rate;
1010     const char **cards;
1011     unsigned n_cards;
1012     const char **sinks;
1013     unsigned n_sinks;
1014     const char *fallback_sink;
1015     const char **sources;
1016     unsigned n_sources;
1017     const char *fallback_source;
1018     const char **playback_streams;
1019     unsigned n_playback_streams;
1020     const char **record_streams;
1021     unsigned n_record_streams;
1022     const char **samples;
1023     unsigned n_samples;
1024     const char **modules;
1025     unsigned n_modules;
1026     const char **clients;
1027     unsigned n_clients;
1028     const char *my_client;
1029     const char **extensions;
1030     unsigned n_extensions;
1031
1032     pa_assert(conn);
1033     pa_assert(msg);
1034     pa_assert(c);
1035
1036     interface_revision = INTERFACE_REVISION;
1037     server_name = PACKAGE_NAME;
1038     version = PACKAGE_VERSION;
1039     is_local = get_is_local(conn);
1040     username = pa_get_user_name_malloc();
1041     hostname = pa_get_host_name_malloc();
1042     default_channels = get_default_channels(c, &n_default_channels);
1043     default_sample_format = c->core->default_sample_spec.format;
1044     default_sample_rate = c->core->default_sample_spec.rate;
1045     cards = get_cards(c, &n_cards);
1046     sinks = get_sinks(c, &n_sinks);
1047     fallback_sink = c->fallback_sink
1048                     ? pa_dbusiface_device_get_path(pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(c->fallback_sink->index)))
1049                     : NULL;
1050     sources = get_sources(c, &n_sources);
1051     fallback_source = c->fallback_source
1052                       ? pa_dbusiface_device_get_path(pa_hashmap_get(c->sources_by_index,
1053                                                                     PA_UINT32_TO_PTR(c->fallback_source->index)))
1054                       : NULL;
1055     playback_streams = get_playback_streams(c, &n_playback_streams);
1056     record_streams = get_record_streams(c, &n_record_streams);
1057     samples = get_samples(c, &n_samples);
1058     modules = get_modules(c, &n_modules);
1059     clients = get_clients(c, &n_clients);
1060     my_client = get_my_client(c, conn);
1061     extensions = pa_dbus_protocol_get_extensions(c->dbus_protocol, &n_extensions);
1062
1063     pa_assert_se((reply = dbus_message_new_method_return(msg)));
1064
1065     dbus_message_iter_init_append(reply, &msg_iter);
1066     pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
1067
1068     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INTERFACE_REVISION].property_name, DBUS_TYPE_UINT32, &interface_revision);
1069     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &server_name);
1070     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_VERSION].property_name, DBUS_TYPE_STRING, &version);
1071     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_IS_LOCAL].property_name, DBUS_TYPE_BOOLEAN, &is_local);
1072     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_USERNAME].property_name, DBUS_TYPE_STRING, &username);
1073     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_HOSTNAME].property_name, DBUS_TYPE_STRING, &hostname);
1074     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_CHANNELS].property_name, DBUS_TYPE_UINT32, default_channels, n_default_channels);
1075     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_SAMPLE_FORMAT].property_name, DBUS_TYPE_UINT32, &default_sample_format);
1076     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_SAMPLE_RATE].property_name, DBUS_TYPE_UINT32, &default_sample_rate);
1077     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CARDS].property_name, DBUS_TYPE_OBJECT_PATH, cards, n_cards);
1078     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SINKS].property_name, DBUS_TYPE_OBJECT_PATH, sinks, n_sinks);
1079
1080     if (fallback_sink)
1081         pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_FALLBACK_SINK].property_name, DBUS_TYPE_OBJECT_PATH, &fallback_sink);
1082
1083     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SOURCES].property_name, DBUS_TYPE_OBJECT_PATH, sources, n_sources);
1084
1085     if (fallback_source)
1086         pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_FALLBACK_SOURCE].property_name, DBUS_TYPE_OBJECT_PATH, &fallback_source);
1087
1088     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PLAYBACK_STREAMS].property_name, DBUS_TYPE_OBJECT_PATH, playback_streams, n_playback_streams);
1089     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_RECORD_STREAMS].property_name, DBUS_TYPE_OBJECT_PATH, record_streams, n_record_streams);
1090     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLES].property_name, DBUS_TYPE_OBJECT_PATH, samples, n_samples);
1091     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_MODULES].property_name, DBUS_TYPE_OBJECT_PATH, modules, n_modules);
1092     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CLIENTS].property_name, DBUS_TYPE_OBJECT_PATH, clients, n_clients);
1093     pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_MY_CLIENT].property_name, DBUS_TYPE_OBJECT_PATH, &my_client);
1094     pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_EXTENSIONS].property_name, DBUS_TYPE_STRING, extensions, n_extensions);
1095
1096     pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
1097
1098     pa_assert_se(dbus_connection_send(conn, reply, NULL));
1099
1100     dbus_message_unref(reply);
1101
1102     pa_xfree(username);
1103     pa_xfree(hostname);
1104     pa_xfree(default_channels);
1105     pa_xfree(cards);
1106     pa_xfree(sinks);
1107     pa_xfree(sources);
1108     pa_xfree(playback_streams);
1109     pa_xfree(record_streams);
1110     pa_xfree(samples);
1111     pa_xfree(modules);
1112     pa_xfree(clients);
1113     pa_xfree(extensions);
1114 }
1115
1116 static void handle_get_card_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1117     pa_dbusiface_core *c = userdata;
1118     char *card_name;
1119     pa_card *card;
1120     pa_dbusiface_card *dbus_card;
1121     const char *object_path;
1122
1123     pa_assert(conn);
1124     pa_assert(msg);
1125     pa_assert(c);
1126
1127     pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &card_name, DBUS_TYPE_INVALID));
1128
1129     if (!(card = pa_namereg_get(c->core, card_name, PA_NAMEREG_CARD))) {
1130         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such card.");
1131         return;
1132     }
1133
1134     pa_assert_se((dbus_card = pa_hashmap_get(c->cards, PA_UINT32_TO_PTR(card->index))));
1135
1136     object_path = pa_dbusiface_card_get_path(dbus_card);
1137
1138     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
1139 }
1140
1141 static void handle_get_sink_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1142     pa_dbusiface_core *c = userdata;
1143     char *sink_name;
1144     pa_sink *sink;
1145     pa_dbusiface_device *dbus_sink;
1146     const char *object_path;
1147
1148     pa_assert(conn);
1149     pa_assert(msg);
1150     pa_assert(c);
1151
1152     pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &sink_name, DBUS_TYPE_INVALID));
1153
1154     if (!(sink = pa_namereg_get(c->core, sink_name, PA_NAMEREG_SINK))) {
1155         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", sink_name);
1156         return;
1157     }
1158
1159     pa_assert_se((dbus_sink = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(sink->index))));
1160
1161     object_path = pa_dbusiface_device_get_path(dbus_sink);
1162
1163     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
1164 }
1165
1166 static void handle_get_source_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1167     pa_dbusiface_core *c = userdata;
1168     char *source_name;
1169     pa_source *source;
1170     pa_dbusiface_device *dbus_source;
1171     const char *object_path;
1172
1173     pa_assert(conn);
1174     pa_assert(msg);
1175     pa_assert(c);
1176
1177     pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &source_name, DBUS_TYPE_INVALID));
1178
1179     if (!(source = pa_namereg_get(c->core, source_name, PA_NAMEREG_SOURCE))) {
1180         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such source.", source_name);
1181         return;
1182     }
1183
1184     pa_assert_se((dbus_source = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(source->index))));
1185
1186     object_path = pa_dbusiface_device_get_path(dbus_source);
1187
1188     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
1189 }
1190
1191 static void handle_get_sample_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1192     pa_dbusiface_core *c = userdata;
1193     char *sample_name;
1194     pa_scache_entry *sample;
1195     pa_dbusiface_sample *dbus_sample;
1196     const char *object_path;
1197
1198     pa_assert(conn);
1199     pa_assert(msg);
1200     pa_assert(c);
1201
1202     pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &sample_name, DBUS_TYPE_INVALID));
1203
1204     if (!(sample = pa_namereg_get(c->core, sample_name, PA_NAMEREG_SAMPLE))) {
1205         pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such sample.");
1206         return;
1207     }
1208
1209     pa_assert_se((dbus_sample = pa_hashmap_get(c->samples, PA_UINT32_TO_PTR(sample->index))));
1210
1211     object_path = pa_dbusiface_sample_get_path(dbus_sample);
1212
1213     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
1214 }
1215
1216 static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1217     pa_dbusiface_core *c = userdata;
1218     DBusMessageIter msg_iter;
1219     DBusMessageIter array_iter;
1220     const char *name;
1221     dbus_uint32_t sample_format;
1222     dbus_uint32_t sample_rate;
1223     const dbus_uint32_t *channels;
1224     int n_channels;
1225     const dbus_uint32_t *default_volume;
1226     int n_volume_entries;
1227     pa_proplist *property_list;
1228     const uint8_t *data;
1229     int data_length;
1230     int i;
1231     pa_sample_spec ss;
1232     pa_channel_map map;
1233     pa_memchunk chunk;
1234     uint32_t idx;
1235     pa_dbusiface_sample *dbus_sample = NULL;
1236     pa_scache_entry *sample = NULL;
1237     const char *object_path;
1238
1239     pa_assert(conn);
1240     pa_assert(msg);
1241     pa_assert(c);
1242
1243     chunk.memblock = NULL;
1244
1245     pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
1246     dbus_message_iter_get_basic(&msg_iter, &name);
1247
1248     pa_assert_se(dbus_message_iter_next(&msg_iter));
1249     dbus_message_iter_get_basic(&msg_iter, &sample_format);
1250
1251     pa_assert_se(dbus_message_iter_next(&msg_iter));
1252     dbus_message_iter_get_basic(&msg_iter, &sample_rate);
1253
1254     pa_assert_se(dbus_message_iter_next(&msg_iter));
1255     dbus_message_iter_recurse(&msg_iter, &array_iter);
1256     dbus_message_iter_get_fixed_array(&array_iter, &channels, &n_channels);
1257
1258     pa_assert_se(dbus_message_iter_next(&msg_iter));
1259     dbus_message_iter_recurse(&msg_iter, &array_iter);
1260     dbus_message_iter_get_fixed_array(&array_iter, &default_volume, &n_volume_entries);
1261
1262     pa_assert_se(dbus_message_iter_next(&msg_iter));
1263     if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
1264         return;
1265
1266     dbus_message_iter_recurse(&msg_iter, &array_iter);
1267     dbus_message_iter_get_fixed_array(&array_iter, &data, &data_length);
1268
1269     if (sample_format >= PA_SAMPLE_MAX) {
1270         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
1271         goto finish;
1272     }
1273
1274     if (sample_rate <= 0 || sample_rate > PA_RATE_MAX) {
1275         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
1276         goto finish;
1277     }
1278
1279     if (n_channels <= 0) {
1280         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Empty channel map.");
1281         goto finish;
1282     }
1283
1284     if (n_channels > (int) PA_CHANNELS_MAX) {
1285         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
1286                            "Too many channels: %i. The maximum is %u.", n_channels, PA_CHANNELS_MAX);
1287         goto finish;
1288     }
1289
1290     for (i = 0; i < n_channels; ++i) {
1291         if (channels[i] >= PA_CHANNEL_POSITION_MAX) {
1292             pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position.");
1293             goto finish;
1294         }
1295     }
1296
1297     if (n_volume_entries != 0 && n_volume_entries != n_channels) {
1298         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
1299                            "The channels and default_volume arguments have different number of elements (%i and %i, resp).",
1300                            n_channels, n_volume_entries);
1301         goto finish;
1302     }
1303
1304     for (i = 0; i < n_volume_entries; ++i) {
1305         if (default_volume[i] > PA_VOLUME_MAX) {
1306             pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume: %u.", default_volume[i]);
1307             goto finish;
1308         }
1309     }
1310
1311     if (data_length == 0) {
1312         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Empty data.");
1313         goto finish;
1314     }
1315
1316     if (data_length > PA_SCACHE_ENTRY_SIZE_MAX) {
1317         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
1318                            "Too big sample: %i bytes. The maximum sample length is %u bytes.",
1319                            data_length, PA_SCACHE_ENTRY_SIZE_MAX);
1320         goto finish;
1321     }
1322
1323     ss.format = sample_format;
1324     ss.rate = sample_rate;
1325     ss.channels = n_channels;
1326
1327     pa_assert(pa_sample_spec_valid(&ss));
1328
1329     if (!pa_frame_aligned(data_length, &ss)) {
1330         char buf[PA_SAMPLE_SPEC_SNPRINT_MAX];
1331         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS,
1332                            "The sample length (%i bytes) doesn't align with the sample format and channels (%s).",
1333                            data_length, pa_sample_spec_snprint(buf, sizeof(buf), &ss));
1334         goto finish;
1335     }
1336
1337     map.channels = n_channels;
1338     for (i = 0; i < n_channels; ++i)
1339         map.map[i] = channels[i];
1340
1341     chunk.memblock = pa_memblock_new(c->core->mempool, data_length);
1342     chunk.index = 0;
1343     chunk.length = data_length;
1344
1345     memcpy(pa_memblock_acquire(chunk.memblock), data, data_length);
1346     pa_memblock_release(chunk.memblock);
1347
1348     if (pa_scache_add_item(c->core, name, &ss, &map, &chunk, property_list, &idx) < 0) {
1349         pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Adding the sample failed.");
1350         goto finish;
1351     }
1352
1353     sample = pa_idxset_get_by_index(c->core->scache, idx);
1354
1355     if (n_volume_entries > 0) {
1356         sample->volume.channels = n_channels;
1357         for (i = 0; i < n_volume_entries; ++i)
1358             sample->volume.values[i] = default_volume[i];
1359         sample->volume_is_set = TRUE;
1360     } else {
1361         sample->volume_is_set = FALSE;
1362     }
1363
1364     dbus_sample = pa_dbusiface_sample_new(c, sample);
1365     pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), dbus_sample);
1366
1367     object_path = pa_dbusiface_sample_get_path(dbus_sample);
1368
1369     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
1370
1371 finish:
1372     if (property_list)
1373         pa_proplist_free(property_list);
1374
1375     if (chunk.memblock)
1376         pa_memblock_unref(chunk.memblock);
1377 }
1378
1379 static pa_bool_t contains_space(const char *string) {
1380     const char *p;
1381
1382     pa_assert(string);
1383
1384     for (p = string; *p; ++p) {
1385         if (isspace(*p))
1386             return TRUE;
1387     }
1388
1389     return FALSE;
1390 }
1391
1392 static void handle_load_module(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1393     pa_dbusiface_core *c = userdata;
1394     DBusMessageIter msg_iter;
1395     DBusMessageIter dict_iter;
1396     DBusMessageIter dict_entry_iter;
1397     char *name = NULL;
1398     const char *key = NULL;
1399     const char *value = NULL;
1400     char *escaped_value = NULL;
1401     pa_strbuf *arg_buffer = NULL;
1402     char *arg_string = NULL;
1403     pa_module *module = NULL;
1404     pa_dbusiface_module *dbus_module = NULL;
1405     const char *object_path = NULL;
1406
1407     pa_assert(conn);
1408     pa_assert(msg);
1409     pa_assert(c);
1410
1411     if (c->core->disallow_module_loading) {
1412         pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "The server is configured to disallow module loading.");
1413         return;
1414     }
1415
1416     pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
1417     dbus_message_iter_get_basic(&msg_iter, &name);
1418
1419     arg_buffer = pa_strbuf_new();
1420
1421     pa_assert_se(dbus_message_iter_next(&msg_iter));
1422     dbus_message_iter_recurse(&msg_iter, &dict_iter);
1423
1424     while (dbus_message_iter_get_arg_type(&dict_iter) != DBUS_TYPE_INVALID) {
1425         if (!pa_strbuf_isempty(arg_buffer))
1426             pa_strbuf_putc(arg_buffer, ' ');
1427
1428         dbus_message_iter_recurse(&dict_iter, &dict_entry_iter);
1429
1430         dbus_message_iter_get_basic(&dict_entry_iter, &key);
1431
1432         if (strlen(key) <= 0 || !pa_ascii_valid(key) || contains_space(key)) {
1433             pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid module argument name: %s", key);
1434             goto finish;
1435         }
1436
1437         pa_assert_se(dbus_message_iter_next(&dict_entry_iter));
1438         dbus_message_iter_get_basic(&dict_entry_iter, &value);
1439
1440         escaped_value = pa_escape(value, "\"");
1441         pa_strbuf_printf(arg_buffer, "%s=\"%s\"", key, escaped_value);
1442         pa_xfree(escaped_value);
1443
1444         dbus_message_iter_next(&dict_iter);
1445     }
1446
1447     arg_string = pa_strbuf_tostring(arg_buffer);
1448
1449     if (!(module = pa_module_load(c->core, name, arg_string))) {
1450         pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Failed to load module.");
1451         goto finish;
1452     }
1453
1454     dbus_module = pa_dbusiface_module_new(module);
1455     pa_hashmap_put(c->modules, PA_UINT32_TO_PTR(module->index), dbus_module);
1456
1457     object_path = pa_dbusiface_module_get_path(dbus_module);
1458
1459     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &object_path);
1460
1461 finish:
1462     if (arg_buffer)
1463         pa_strbuf_free(arg_buffer);
1464
1465     pa_xfree(arg_string);
1466 }
1467
1468 static void handle_exit(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1469     pa_dbusiface_core *c = userdata;
1470
1471     pa_assert(conn);
1472     pa_assert(msg);
1473     pa_assert(c);
1474
1475     if (c->core->disallow_exit) {
1476         pa_dbus_send_error(conn, msg, DBUS_ERROR_ACCESS_DENIED, "The server is configured to disallow exiting.");
1477         return;
1478     }
1479
1480     pa_dbus_send_empty_reply(conn, msg);
1481
1482     pa_core_exit(c->core, FALSE, 0);
1483 }
1484
1485 static void handle_listen_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1486     pa_dbusiface_core *c = userdata;
1487     const char *signal;
1488     char **objects = NULL;
1489     int n_objects;
1490
1491     pa_assert(conn);
1492     pa_assert(msg);
1493     pa_assert(c);
1494
1495     pa_assert_se(dbus_message_get_args(msg, NULL,
1496                                        DBUS_TYPE_STRING, &signal,
1497                                        DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &objects, &n_objects,
1498                                        DBUS_TYPE_INVALID));
1499
1500     pa_dbus_protocol_add_signal_listener(c->dbus_protocol, conn, *signal ? signal : NULL, objects, n_objects);
1501
1502     pa_dbus_send_empty_reply(conn, msg);
1503
1504     dbus_free_string_array(objects);
1505 }
1506
1507 static void handle_stop_listening_for_signal(DBusConnection *conn, DBusMessage *msg, void *userdata) {
1508     pa_dbusiface_core *c = userdata;
1509     const char *signal;
1510
1511     pa_assert(conn);
1512     pa_assert(msg);
1513     pa_assert(c);
1514
1515     pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &signal, DBUS_TYPE_INVALID));
1516
1517     pa_dbus_protocol_remove_signal_listener(c->dbus_protocol, conn, *signal ? signal : NULL);
1518
1519     pa_dbus_send_empty_reply(conn, msg);
1520 }
1521
1522 static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1523     pa_dbusiface_core *c = userdata;
1524     pa_dbusiface_card *card_iface = NULL;
1525     pa_dbusiface_device *device_iface = NULL;
1526     pa_dbusiface_stream *stream_iface = NULL;
1527     pa_dbusiface_sample *sample_iface = NULL;
1528     pa_dbusiface_module *module_iface = NULL;
1529     pa_dbusiface_client *client_iface = NULL;
1530     DBusMessage *signal = NULL;
1531     const char *object_path = NULL;
1532     pa_sink *new_fallback_sink = NULL;
1533     pa_source *new_fallback_source = NULL;
1534
1535     pa_assert(c);
1536
1537     switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
1538         case PA_SUBSCRIPTION_EVENT_SERVER:
1539             new_fallback_sink = pa_namereg_get_default_sink(core);
1540             new_fallback_source = pa_namereg_get_default_source(core);
1541
1542             if (c->fallback_sink != new_fallback_sink) {
1543                 if (c->fallback_sink)
1544                     pa_sink_unref(c->fallback_sink);
1545                 c->fallback_sink = new_fallback_sink ? pa_sink_ref(new_fallback_sink) : NULL;
1546
1547                 if (new_fallback_sink
1548                     && (device_iface = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(new_fallback_sink->index)))) {
1549                     object_path = pa_dbusiface_device_get_path(device_iface);
1550
1551                     pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1552                                                                    PA_DBUS_CORE_INTERFACE,
1553                                                                    signals[SIGNAL_FALLBACK_SINK_UPDATED].name)));
1554                     pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1555                     pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1556                     dbus_message_unref(signal);
1557                     signal = NULL;
1558                 }
1559             }
1560
1561             if (c->fallback_source != new_fallback_source) {
1562                 if (c->fallback_source)
1563                     pa_source_unref(c->fallback_source);
1564                 c->fallback_source = new_fallback_source ? pa_source_ref(new_fallback_source) : NULL;
1565
1566                 if (new_fallback_source
1567                     && (device_iface = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(new_fallback_source->index)))) {
1568                     object_path = pa_dbusiface_device_get_path(device_iface);
1569
1570                     pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1571                                                                    PA_DBUS_CORE_INTERFACE,
1572                                                                    signals[SIGNAL_FALLBACK_SOURCE_UPDATED].name)));
1573                     pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1574                     pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1575                     dbus_message_unref(signal);
1576                     signal = NULL;
1577                 }
1578             }
1579             break;
1580
1581         case PA_SUBSCRIPTION_EVENT_CARD:
1582             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1583                 if (!(card_iface = pa_hashmap_get(c->cards, PA_UINT32_TO_PTR(idx)))) {
1584                     pa_card *card = NULL;
1585
1586                     if (!(card = pa_idxset_get_by_index(core->cards, idx)))
1587                         return; /* The card was removed immediately after creation. */
1588
1589                     card_iface = pa_dbusiface_card_new(c, card);
1590                     pa_hashmap_put(c->cards, PA_UINT32_TO_PTR(idx), card_iface);
1591                 }
1592
1593                 object_path = pa_dbusiface_card_get_path(card_iface);
1594
1595                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1596                                                                PA_DBUS_CORE_INTERFACE,
1597                                                                signals[SIGNAL_NEW_CARD].name)));
1598                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1599
1600             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1601                 if (!(card_iface = pa_hashmap_remove(c->cards, PA_UINT32_TO_PTR(idx))))
1602                     return;
1603
1604                 object_path = pa_dbusiface_card_get_path(card_iface);
1605
1606                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1607                                                                PA_DBUS_CORE_INTERFACE,
1608                                                                signals[SIGNAL_CARD_REMOVED].name)));
1609                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1610
1611                 pa_dbusiface_card_free(card_iface);
1612             }
1613             break;
1614
1615         case PA_SUBSCRIPTION_EVENT_SINK:
1616             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1617                 pa_sink *sink = NULL;
1618
1619                 if (!(sink = pa_idxset_get_by_index(core->sinks, idx)))
1620                     return; /* The sink was removed immediately after creation. */
1621
1622                 if (!(device_iface = pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(idx)))) {
1623                     device_iface = pa_dbusiface_device_new_sink(c, sink);
1624                     pa_hashmap_put(c->sinks_by_index, PA_UINT32_TO_PTR(idx), device_iface);
1625                     pa_hashmap_put(c->sinks_by_path, pa_dbusiface_device_get_path(device_iface), device_iface);
1626                 }
1627
1628                 object_path = pa_dbusiface_device_get_path(device_iface);
1629
1630                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1631                                                                PA_DBUS_CORE_INTERFACE,
1632                                                                signals[SIGNAL_NEW_SINK].name)));
1633                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1634
1635                 pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1636                 dbus_message_unref(signal);
1637                 signal = NULL;
1638
1639                 if (c->fallback_sink && pa_streq(c->fallback_sink->name, sink->name)) {
1640                     /* We have got default sink change event, but at that point
1641                      * the D-Bus sink object wasn't created yet. Now that the
1642                      * object is created, let's send the fallback sink change
1643                      * signal. */
1644                     pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1645                                                                    PA_DBUS_CORE_INTERFACE,
1646                                                                    signals[SIGNAL_FALLBACK_SINK_UPDATED].name)));
1647                     pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1648
1649                     pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1650                     dbus_message_unref(signal);
1651                     signal = NULL;
1652                 }
1653
1654             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1655                 if (!(device_iface = pa_hashmap_remove(c->sinks_by_index, PA_UINT32_TO_PTR(idx))))
1656                     return;
1657
1658                 object_path = pa_dbusiface_device_get_path(device_iface);
1659                 pa_assert_se(pa_hashmap_remove(c->sinks_by_path, object_path));
1660
1661                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1662                                                                PA_DBUS_CORE_INTERFACE,
1663                                                                signals[SIGNAL_SINK_REMOVED].name)));
1664                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1665
1666                 pa_dbusiface_device_free(device_iface);
1667             }
1668             break;
1669
1670         case PA_SUBSCRIPTION_EVENT_SOURCE:
1671             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1672                 pa_source *source = pa_idxset_get_by_index(core->sources, idx);
1673
1674                 if (!(source = pa_idxset_get_by_index(core->sources, idx)))
1675                     return; /* The source was removed immediately after creation. */
1676
1677                 if (!(device_iface = pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(idx)))) {
1678                     device_iface = pa_dbusiface_device_new_source(c, source);
1679                     pa_hashmap_put(c->sources_by_index, PA_UINT32_TO_PTR(idx), device_iface);
1680                     pa_hashmap_put(c->sources_by_path, pa_dbusiface_device_get_path(device_iface), device_iface);
1681                 }
1682
1683                 object_path = pa_dbusiface_device_get_path(device_iface);
1684
1685                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1686                                                                PA_DBUS_CORE_INTERFACE,
1687                                                                signals[SIGNAL_NEW_SOURCE].name)));
1688                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1689
1690                 pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1691                 dbus_message_unref(signal);
1692                 signal = NULL;
1693
1694                 if (c->fallback_source && pa_streq(c->fallback_source->name, source->name)) {
1695                     /* We have got default source change event, but at that
1696                      * point the D-Bus source object wasn't created yet. Now
1697                      * that the object is created, let's send the fallback
1698                      * source change signal. */
1699                     pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1700                                                                    PA_DBUS_CORE_INTERFACE,
1701                                                                    signals[SIGNAL_FALLBACK_SOURCE_UPDATED].name)));
1702                     pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1703
1704                     pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1705                     dbus_message_unref(signal);
1706                     signal = NULL;
1707                 }
1708
1709             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1710                 if (!(device_iface = pa_hashmap_remove(c->sources_by_index, PA_UINT32_TO_PTR(idx))))
1711                     return;
1712
1713                 object_path = pa_dbusiface_device_get_path(device_iface);
1714                 pa_assert_se(pa_hashmap_remove(c->sources_by_path, object_path));
1715
1716                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1717                                                                PA_DBUS_CORE_INTERFACE,
1718                                                                signals[SIGNAL_SOURCE_REMOVED].name)));
1719                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1720
1721                 pa_dbusiface_device_free(device_iface);
1722             }
1723             break;
1724
1725         case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
1726             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1727                 pa_sink_input *sink_input = NULL;
1728
1729                 if (!(sink_input = pa_idxset_get_by_index(core->sink_inputs, idx)))
1730                     return; /* The sink input was removed immediately after creation. */
1731
1732                 if (!(stream_iface = pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(idx)))) {
1733                     stream_iface = pa_dbusiface_stream_new_playback(c, sink_input);
1734                     pa_hashmap_put(c->playback_streams, PA_UINT32_TO_PTR(idx), stream_iface);
1735                 }
1736
1737                 object_path = pa_dbusiface_stream_get_path(stream_iface);
1738
1739                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1740                                                                PA_DBUS_CORE_INTERFACE,
1741                                                                signals[SIGNAL_NEW_PLAYBACK_STREAM].name)));
1742                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1743
1744             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1745                 if (!(stream_iface = pa_hashmap_remove(c->playback_streams, PA_UINT32_TO_PTR(idx))))
1746                     return;
1747
1748                 object_path = pa_dbusiface_stream_get_path(stream_iface);
1749
1750                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1751                                                                PA_DBUS_CORE_INTERFACE,
1752                                                                signals[SIGNAL_PLAYBACK_STREAM_REMOVED].name)));
1753                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1754
1755                 pa_dbusiface_stream_free(stream_iface);
1756             }
1757             break;
1758
1759         case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
1760             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1761                 pa_source_output *source_output = NULL;
1762
1763                 if (!(source_output = pa_idxset_get_by_index(core->source_outputs, idx)))
1764                     return; /* The source output was removed immediately after creation. */
1765
1766                 if (!(stream_iface = pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(idx)))) {
1767                     stream_iface = pa_dbusiface_stream_new_record(c, source_output);
1768                     pa_hashmap_put(c->record_streams, PA_UINT32_TO_PTR(idx), stream_iface);
1769                 }
1770
1771                 object_path = pa_dbusiface_stream_get_path(stream_iface);
1772
1773                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1774                                                                PA_DBUS_CORE_INTERFACE,
1775                                                                signals[SIGNAL_NEW_RECORD_STREAM].name)));
1776                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1777
1778             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1779                 if (!(stream_iface = pa_hashmap_remove(c->record_streams, PA_UINT32_TO_PTR(idx))))
1780                     return;
1781
1782                 object_path = pa_dbusiface_stream_get_path(stream_iface);
1783
1784                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1785                                                                PA_DBUS_CORE_INTERFACE,
1786                                                                signals[SIGNAL_RECORD_STREAM_REMOVED].name)));
1787                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1788
1789                 pa_dbusiface_stream_free(stream_iface);
1790             }
1791             break;
1792
1793         case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
1794             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1795                 pa_scache_entry *sample = NULL;
1796
1797                 if (!(sample = pa_idxset_get_by_index(core->scache, idx)))
1798                     return; /* The sample was removed immediately after creation. */
1799
1800                 if (!(sample_iface = pa_hashmap_get(c->samples, PA_UINT32_TO_PTR(idx)))) {
1801                     sample_iface = pa_dbusiface_sample_new(c, sample);
1802                     pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), sample_iface);
1803                 }
1804
1805                 object_path = pa_dbusiface_sample_get_path(sample_iface);
1806
1807                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1808                                                                PA_DBUS_CORE_INTERFACE,
1809                                                                signals[SIGNAL_NEW_SAMPLE].name)));
1810                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1811
1812             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1813                 if (!(sample_iface = pa_hashmap_remove(c->samples, PA_UINT32_TO_PTR(idx))))
1814                     return;
1815
1816                 object_path = pa_dbusiface_sample_get_path(sample_iface);
1817
1818                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1819                                                                PA_DBUS_CORE_INTERFACE,
1820                                                                signals[SIGNAL_SAMPLE_REMOVED].name)));
1821                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1822
1823                 pa_dbusiface_sample_free(sample_iface);
1824             }
1825             break;
1826
1827         case PA_SUBSCRIPTION_EVENT_MODULE:
1828             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1829                 pa_module *module = NULL;
1830
1831                 if (!(module = pa_idxset_get_by_index(core->modules, idx)))
1832                     return; /* The module was removed immediately after creation. */
1833
1834                 if (!(module_iface = pa_hashmap_get(c->modules, PA_UINT32_TO_PTR(idx)))) {
1835                     module_iface = pa_dbusiface_module_new(module);
1836                     pa_hashmap_put(c->modules, PA_UINT32_TO_PTR(idx), module_iface);
1837                 }
1838
1839                 object_path = pa_dbusiface_module_get_path(module_iface);
1840
1841                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1842                                                                PA_DBUS_CORE_INTERFACE,
1843                                                                signals[SIGNAL_NEW_MODULE].name)));
1844                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1845
1846             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1847                 if (!(module_iface = pa_hashmap_remove(c->modules, PA_UINT32_TO_PTR(idx))))
1848                     return;
1849
1850                 object_path = pa_dbusiface_module_get_path(module_iface);
1851
1852                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1853                                                                PA_DBUS_CORE_INTERFACE,
1854                                                                signals[SIGNAL_MODULE_REMOVED].name)));
1855                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1856
1857                 pa_dbusiface_module_free(module_iface);
1858             }
1859             break;
1860
1861         case PA_SUBSCRIPTION_EVENT_CLIENT:
1862             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
1863                 pa_client *client = NULL;
1864
1865                 if (!(client = pa_idxset_get_by_index(core->clients, idx)))
1866                     return; /* The client was removed immediately after creation. */
1867
1868                 if (!(client_iface = pa_hashmap_get(c->clients, PA_UINT32_TO_PTR(idx)))) {
1869                     client_iface = pa_dbusiface_client_new(c, client);
1870                     pa_hashmap_put(c->clients, PA_UINT32_TO_PTR(idx), client_iface);
1871                 }
1872
1873                 object_path = pa_dbusiface_client_get_path(client_iface);
1874
1875                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1876                                                                PA_DBUS_CORE_INTERFACE,
1877                                                                signals[SIGNAL_NEW_CLIENT].name)));
1878                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1879
1880             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
1881                 if (!(client_iface = pa_hashmap_remove(c->clients, PA_UINT32_TO_PTR(idx))))
1882                     return;
1883
1884                 object_path = pa_dbusiface_client_get_path(client_iface);
1885
1886                 pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1887                                                                PA_DBUS_CORE_INTERFACE,
1888                                                                signals[SIGNAL_CLIENT_REMOVED].name)));
1889                 pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
1890
1891                 pa_dbusiface_client_free(client_iface);
1892             }
1893             break;
1894     }
1895
1896     if (signal) {
1897         pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1898         dbus_message_unref(signal);
1899     }
1900 }
1901
1902 static pa_hook_result_t extension_registered_cb(void *hook_data, void *call_data, void *slot_data) {
1903     pa_dbusiface_core *c = slot_data;
1904     const char *ext_name = call_data;
1905     DBusMessage *signal = NULL;
1906
1907     pa_assert(c);
1908     pa_assert(ext_name);
1909
1910     pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1911                                                    PA_DBUS_CORE_INTERFACE,
1912                                                    signals[SIGNAL_NEW_EXTENSION].name)));
1913     pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_STRING, &ext_name, DBUS_TYPE_INVALID));
1914
1915     pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1916     dbus_message_unref(signal);
1917
1918     return PA_HOOK_OK;
1919 }
1920
1921 static pa_hook_result_t extension_unregistered_cb(void *hook_data, void *call_data, void *slot_data) {
1922     pa_dbusiface_core *c = slot_data;
1923     const char *ext_name = call_data;
1924     DBusMessage *signal = NULL;
1925
1926     pa_assert(c);
1927     pa_assert(ext_name);
1928
1929     pa_assert_se((signal = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
1930                                                    PA_DBUS_CORE_INTERFACE,
1931                                                    signals[SIGNAL_EXTENSION_REMOVED].name)));
1932     pa_assert_se(dbus_message_append_args(signal, DBUS_TYPE_STRING, &ext_name, DBUS_TYPE_INVALID));
1933
1934     pa_dbus_protocol_send_signal(c->dbus_protocol, signal);
1935     dbus_message_unref(signal);
1936
1937     return PA_HOOK_OK;
1938 }
1939
1940 pa_dbusiface_core *pa_dbusiface_core_new(pa_core *core) {
1941     pa_dbusiface_core *c;
1942     pa_card *card;
1943     pa_sink *sink;
1944     pa_source *source;
1945     pa_dbusiface_device *device;
1946     pa_sink_input *sink_input;
1947     pa_source_output *source_output;
1948     pa_scache_entry *sample;
1949     pa_module *module;
1950     pa_client *client;
1951     uint32_t idx;
1952
1953     pa_assert(core);
1954
1955     c = pa_xnew(pa_dbusiface_core, 1);
1956     c->core = pa_core_ref(core);
1957     c->subscription = pa_subscription_new(core, PA_SUBSCRIPTION_MASK_ALL, subscription_cb, c);
1958     c->dbus_protocol = pa_dbus_protocol_get(core);
1959     c->cards = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1960     c->sinks_by_index = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1961     c->sinks_by_path = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1962     c->sources_by_index = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1963     c->sources_by_path = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1964     c->playback_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1965     c->record_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1966     c->samples = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1967     c->modules = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1968     c->clients = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
1969     c->fallback_sink = pa_namereg_get_default_sink(core);
1970     c->fallback_source = pa_namereg_get_default_source(core);
1971     c->extension_registered_slot = pa_dbus_protocol_hook_connect(c->dbus_protocol,
1972                                                                  PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED,
1973                                                                  PA_HOOK_NORMAL,
1974                                                                  extension_registered_cb,
1975                                                                  c);
1976     c->extension_unregistered_slot = pa_dbus_protocol_hook_connect(c->dbus_protocol,
1977                                                                    PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED,
1978                                                                    PA_HOOK_NORMAL,
1979                                                                    extension_unregistered_cb,
1980                                                                    c);
1981     c->memstats = pa_dbusiface_memstats_new(c, core);
1982
1983     if (c->fallback_sink)
1984         pa_sink_ref(c->fallback_sink);
1985     if (c->fallback_source)
1986         pa_source_ref(c->fallback_source);
1987
1988     PA_IDXSET_FOREACH(card, core->cards, idx)
1989         pa_hashmap_put(c->cards, PA_UINT32_TO_PTR(idx), pa_dbusiface_card_new(c, card));
1990
1991     PA_IDXSET_FOREACH(sink, core->sinks, idx) {
1992         device = pa_dbusiface_device_new_sink(c, sink);
1993         pa_hashmap_put(c->sinks_by_index, PA_UINT32_TO_PTR(idx), device);
1994         pa_hashmap_put(c->sinks_by_path, pa_dbusiface_device_get_path(device), device);
1995     }
1996
1997     PA_IDXSET_FOREACH(source, core->sources, idx) {
1998         device = pa_dbusiface_device_new_source(c, source);
1999         pa_hashmap_put(c->sources_by_index, PA_UINT32_TO_PTR(idx), device);
2000         pa_hashmap_put(c->sources_by_path, pa_dbusiface_device_get_path(device), device);
2001     }
2002
2003     PA_IDXSET_FOREACH(sink_input, core->sink_inputs, idx)
2004         pa_hashmap_put(c->playback_streams, PA_UINT32_TO_PTR(idx), pa_dbusiface_stream_new_playback(c, sink_input));
2005
2006     PA_IDXSET_FOREACH(source_output, core->source_outputs, idx)
2007         pa_hashmap_put(c->record_streams, PA_UINT32_TO_PTR(idx), pa_dbusiface_stream_new_record(c, source_output));
2008
2009     PA_IDXSET_FOREACH(sample, core->scache, idx)
2010         pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), pa_dbusiface_sample_new(c, sample));
2011
2012     PA_IDXSET_FOREACH(module, core->modules, idx)
2013         pa_hashmap_put(c->modules, PA_UINT32_TO_PTR(idx), pa_dbusiface_module_new(module));
2014
2015     PA_IDXSET_FOREACH(client, core->clients, idx)
2016         pa_hashmap_put(c->clients, PA_UINT32_TO_PTR(idx), pa_dbusiface_client_new(c, client));
2017
2018     pa_assert_se(pa_dbus_protocol_add_interface(c->dbus_protocol, PA_DBUS_CORE_OBJECT_PATH, &core_interface_info, c) >= 0);
2019
2020     return c;
2021 }
2022
2023 static void free_card_cb(void *p, void *userdata) {
2024     pa_dbusiface_card *c = p;
2025
2026     pa_assert(c);
2027
2028     pa_dbusiface_card_free(c);
2029 }
2030
2031 static void free_device_cb(void *p, void *userdata) {
2032     pa_dbusiface_device *d = p;
2033
2034     pa_assert(d);
2035
2036     pa_dbusiface_device_free(d);
2037 }
2038
2039 static void free_stream_cb(void *p, void *userdata) {
2040     pa_dbusiface_stream *s = p;
2041
2042     pa_assert(s);
2043
2044     pa_dbusiface_stream_free(s);
2045 }
2046
2047 static void free_sample_cb(void *p, void *userdata) {
2048     pa_dbusiface_sample *s = p;
2049
2050     pa_assert(s);
2051
2052     pa_dbusiface_sample_free(s);
2053 }
2054
2055 static void free_module_cb(void *p, void *userdata) {
2056     pa_dbusiface_module *m = p;
2057
2058     pa_assert(m);
2059
2060     pa_dbusiface_module_free(m);
2061 }
2062
2063 static void free_client_cb(void *p, void *userdata) {
2064     pa_dbusiface_client *c = p;
2065
2066     pa_assert(c);
2067
2068     pa_dbusiface_client_free(c);
2069 }
2070
2071 void pa_dbusiface_core_free(pa_dbusiface_core *c) {
2072     pa_assert(c);
2073
2074     pa_assert_se(pa_dbus_protocol_remove_interface(c->dbus_protocol, PA_DBUS_CORE_OBJECT_PATH, core_interface_info.name) >= 0);
2075
2076     pa_subscription_free(c->subscription);
2077     pa_hashmap_free(c->cards, free_card_cb, NULL);
2078     pa_hashmap_free(c->sinks_by_index, free_device_cb, NULL);
2079     pa_hashmap_free(c->sinks_by_path, NULL, NULL);
2080     pa_hashmap_free(c->sources_by_index, free_device_cb, NULL);
2081     pa_hashmap_free(c->sources_by_path, NULL, NULL);
2082     pa_hashmap_free(c->playback_streams, free_stream_cb, NULL);
2083     pa_hashmap_free(c->record_streams, free_stream_cb, NULL);
2084     pa_hashmap_free(c->samples, free_sample_cb, NULL);
2085     pa_hashmap_free(c->modules, free_module_cb, NULL);
2086     pa_hashmap_free(c->clients, free_client_cb, NULL);
2087     pa_hook_slot_free(c->extension_registered_slot);
2088     pa_hook_slot_free(c->extension_unregistered_slot);
2089     pa_dbusiface_memstats_free(c->memstats);
2090
2091     if (c->fallback_sink)
2092         pa_sink_unref(c->fallback_sink);
2093     if (c->fallback_source)
2094         pa_source_unref(c->fallback_source);
2095
2096     pa_dbus_protocol_unref(c->dbus_protocol);
2097     pa_core_unref(c->core);
2098
2099     pa_xfree(c);
2100 }
2101
2102 const char *pa_dbusiface_core_get_card_path(pa_dbusiface_core *c, const pa_card *card) {
2103     pa_assert(c);
2104     pa_assert(card);
2105
2106     return pa_dbusiface_card_get_path(pa_hashmap_get(c->cards, PA_UINT32_TO_PTR(card->index)));
2107 }
2108
2109 const char *pa_dbusiface_core_get_sink_path(pa_dbusiface_core *c, const pa_sink *sink) {
2110     pa_assert(c);
2111     pa_assert(sink);
2112
2113     return pa_dbusiface_device_get_path(pa_hashmap_get(c->sinks_by_index, PA_UINT32_TO_PTR(sink->index)));
2114 }
2115
2116 const char *pa_dbusiface_core_get_source_path(pa_dbusiface_core *c, const pa_source *source) {
2117     pa_assert(c);
2118     pa_assert(source);
2119
2120     return pa_dbusiface_device_get_path(pa_hashmap_get(c->sources_by_index, PA_UINT32_TO_PTR(source->index)));
2121 }
2122
2123 const char *pa_dbusiface_core_get_playback_stream_path(pa_dbusiface_core *c, const pa_sink_input *sink_input) {
2124     pa_assert(c);
2125     pa_assert(sink_input);
2126
2127     return pa_dbusiface_stream_get_path(pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(sink_input->index)));
2128 }
2129
2130 const char *pa_dbusiface_core_get_record_stream_path(pa_dbusiface_core *c, const pa_source_output *source_output) {
2131     pa_assert(c);
2132     pa_assert(source_output);
2133
2134     return pa_dbusiface_stream_get_path(pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(source_output->index)));
2135 }
2136
2137 const char *pa_dbusiface_core_get_module_path(pa_dbusiface_core *c, const pa_module *module) {
2138     pa_assert(c);
2139     pa_assert(module);
2140
2141     return pa_dbusiface_module_get_path(pa_hashmap_get(c->modules, PA_UINT32_TO_PTR(module->index)));
2142 }
2143
2144 const char *pa_dbusiface_core_get_client_path(pa_dbusiface_core *c, const pa_client *client) {
2145     pa_assert(c);
2146     pa_assert(client);
2147
2148     return pa_dbusiface_client_get_path(pa_hashmap_get(c->clients, PA_UINT32_TO_PTR(client->index)));
2149 }
2150
2151 pa_sink *pa_dbusiface_core_get_sink(pa_dbusiface_core *c, const char *object_path) {
2152     pa_dbusiface_device *device = NULL;
2153
2154     pa_assert(c);
2155     pa_assert(object_path);
2156
2157     device = pa_hashmap_get(c->sinks_by_path, object_path);
2158
2159     if (device)
2160         return pa_dbusiface_device_get_sink(device);
2161     else
2162         return NULL;
2163 }
2164
2165 pa_source *pa_dbusiface_core_get_source(pa_dbusiface_core *c, const char *object_path) {
2166     pa_dbusiface_device *device = NULL;
2167
2168     pa_assert(c);
2169     pa_assert(object_path);
2170
2171     device = pa_hashmap_get(c->sources_by_path, object_path);
2172
2173     if (device)
2174         return pa_dbusiface_device_get_source(device);
2175     else
2176         return NULL;
2177 }