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