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