]> code.delx.au - pulseaudio/blob - src/modules/module-stream-restore.c
Remove unnecessary #includes
[pulseaudio] / src / modules / module-stream-restore.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5 Copyright 2009 Tanu Kaskinen
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <pulse/gccmacro.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/rtclock.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/core-subscribe.h>
46 #include <pulsecore/sink-input.h>
47 #include <pulsecore/source-output.h>
48 #include <pulsecore/namereg.h>
49 #include <pulsecore/protocol-native.h>
50 #include <pulsecore/pstream.h>
51 #include <pulsecore/pstream-util.h>
52 #include <pulsecore/database.h>
53 #include <pulsecore/tagstruct.h>
54
55 #ifdef HAVE_DBUS
56 #include <pulsecore/dbus-util.h>
57 #include <pulsecore/protocol-dbus.h>
58 #endif
59
60 #include "module-stream-restore-symdef.h"
61
62 PA_MODULE_AUTHOR("Lennart Poettering");
63 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute/device state of streams");
64 PA_MODULE_VERSION(PACKAGE_VERSION);
65 PA_MODULE_LOAD_ONCE(TRUE);
66 PA_MODULE_USAGE(
67 "restore_device=<Save/restore sinks/sources?> "
68 "restore_volume=<Save/restore volumes?> "
69 "restore_muted=<Save/restore muted states?> "
70 "on_hotplug=<When new device becomes available, recheck streams?> "
71 "on_rescue=<When device becomes unavailable, recheck streams?>");
72
73 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
74 #define IDENTIFICATION_PROPERTY "module-stream-restore.id"
75
76 static const char* const valid_modargs[] = {
77 "restore_device",
78 "restore_volume",
79 "restore_muted",
80 "on_hotplug",
81 "on_rescue",
82 NULL
83 };
84
85 struct userdata {
86 pa_core *core;
87 pa_module *module;
88 pa_subscription *subscription;
89 pa_hook_slot
90 *sink_input_new_hook_slot,
91 *sink_input_fixate_hook_slot,
92 *source_output_new_hook_slot,
93 *sink_put_hook_slot,
94 *source_put_hook_slot,
95 *sink_unlink_hook_slot,
96 *source_unlink_hook_slot,
97 *connection_unlink_hook_slot;
98 pa_time_event *save_time_event;
99 pa_database* database;
100
101 pa_bool_t restore_device:1;
102 pa_bool_t restore_volume:1;
103 pa_bool_t restore_muted:1;
104 pa_bool_t on_hotplug:1;
105 pa_bool_t on_rescue:1;
106
107 pa_native_protocol *protocol;
108 pa_idxset *subscribed;
109
110 #ifdef HAVE_DBUS
111 pa_dbus_protocol *dbus_protocol;
112 pa_hashmap *dbus_entries;
113 uint32_t next_index; /* For generating object paths for entries. */
114 #endif
115 };
116
117 #define ENTRY_VERSION 1
118
119 struct entry {
120 uint8_t version;
121 pa_bool_t muted_valid, volume_valid, device_valid, card_valid;
122 pa_bool_t muted;
123 pa_channel_map channel_map;
124 pa_cvolume volume;
125 char* device;
126 char* card;
127 };
128
129 enum {
130 SUBCOMMAND_TEST,
131 SUBCOMMAND_READ,
132 SUBCOMMAND_WRITE,
133 SUBCOMMAND_DELETE,
134 SUBCOMMAND_SUBSCRIBE,
135 SUBCOMMAND_EVENT
136 };
137
138
139 static struct entry* entry_new(void);
140 static void entry_free(struct entry *e);
141 static struct entry *entry_read(struct userdata *u, const char *name);
142 static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e, pa_bool_t replace);
143 static struct entry* entry_copy(const struct entry *e);
144 static void entry_apply(struct userdata *u, const char *name, struct entry *e);
145 static void trigger_save(struct userdata *u);
146
147 #ifdef HAVE_DBUS
148
149 #define OBJECT_PATH "/org/pulseaudio/stream_restore1"
150 #define ENTRY_OBJECT_NAME "entry"
151 #define INTERFACE_STREAM_RESTORE "org.PulseAudio.Ext.StreamRestore1"
152 #define INTERFACE_ENTRY INTERFACE_STREAM_RESTORE ".RestoreEntry"
153
154 #define DBUS_INTERFACE_REVISION 0
155
156 struct dbus_entry {
157 struct userdata *userdata;
158
159 char *entry_name;
160 uint32_t index;
161 char *object_path;
162 };
163
164 static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata);
165 static void handle_get_entries(DBusConnection *conn, DBusMessage *msg, void *userdata);
166
167 static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
168
169 static void handle_add_entry(DBusConnection *conn, DBusMessage *msg, void *userdata);
170 static void handle_get_entry_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
171
172 static void handle_entry_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
173 static void handle_entry_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
174 static void handle_entry_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
175 static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
176 static void handle_entry_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
177 static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
178 static void handle_entry_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata);
179 static void handle_entry_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
180
181 static void handle_entry_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
182
183 static void handle_entry_remove(DBusConnection *conn, DBusMessage *msg, void *userdata);
184
185 enum property_handler_index {
186 PROPERTY_HANDLER_INTERFACE_REVISION,
187 PROPERTY_HANDLER_ENTRIES,
188 PROPERTY_HANDLER_MAX
189 };
190
191 enum entry_property_handler_index {
192 ENTRY_PROPERTY_HANDLER_INDEX,
193 ENTRY_PROPERTY_HANDLER_NAME,
194 ENTRY_PROPERTY_HANDLER_DEVICE,
195 ENTRY_PROPERTY_HANDLER_VOLUME,
196 ENTRY_PROPERTY_HANDLER_MUTE,
197 ENTRY_PROPERTY_HANDLER_MAX
198 };
199
200 static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
201 [PROPERTY_HANDLER_INTERFACE_REVISION] = { .property_name = "InterfaceRevision", .type = "u", .get_cb = handle_get_interface_revision, .set_cb = NULL },
202 [PROPERTY_HANDLER_ENTRIES] = { .property_name = "Entries", .type = "ao", .get_cb = handle_get_entries, .set_cb = NULL }
203 };
204
205 static pa_dbus_property_handler entry_property_handlers[ENTRY_PROPERTY_HANDLER_MAX] = {
206 [ENTRY_PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_entry_get_index, .set_cb = NULL },
207 [ENTRY_PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_entry_get_name, .set_cb = NULL },
208 [ENTRY_PROPERTY_HANDLER_DEVICE] = { .property_name = "Device", .type = "s", .get_cb = handle_entry_get_device, .set_cb = handle_entry_set_device },
209 [ENTRY_PROPERTY_HANDLER_VOLUME] = { .property_name = "Volume", .type = "a(uu)", .get_cb = handle_entry_get_volume, .set_cb = handle_entry_set_volume },
210 [ENTRY_PROPERTY_HANDLER_MUTE] = { .property_name = "Mute", .type = "b", .get_cb = handle_entry_get_mute, .set_cb = handle_entry_set_mute }
211 };
212
213 enum method_handler_index {
214 METHOD_HANDLER_ADD_ENTRY,
215 METHOD_HANDLER_GET_ENTRY_BY_NAME,
216 METHOD_HANDLER_MAX
217 };
218
219 enum entry_method_handler_index {
220 ENTRY_METHOD_HANDLER_REMOVE,
221 ENTRY_METHOD_HANDLER_MAX
222 };
223
224 static pa_dbus_arg_info add_entry_args[] = { { "name", "s", "in" },
225 { "device", "s", "in" },
226 { "volume", "a(uu)", "in" },
227 { "mute", "b", "in" },
228 { "entry", "o", "out" } };
229 static pa_dbus_arg_info get_entry_by_name_args[] = { { "name", "s", "in" }, { "entry", "o", "out" } };
230
231 static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
232 [METHOD_HANDLER_ADD_ENTRY] = {
233 .method_name = "AddEntry",
234 .arguments = add_entry_args,
235 .n_arguments = sizeof(add_entry_args) / sizeof(pa_dbus_arg_info),
236 .receive_cb = handle_add_entry },
237 [METHOD_HANDLER_GET_ENTRY_BY_NAME] = {
238 .method_name = "GetEntryByName",
239 .arguments = get_entry_by_name_args,
240 .n_arguments = sizeof(get_entry_by_name_args) / sizeof(pa_dbus_arg_info),
241 .receive_cb = handle_get_entry_by_name }
242 };
243
244 static pa_dbus_method_handler entry_method_handlers[ENTRY_METHOD_HANDLER_MAX] = {
245 [ENTRY_METHOD_HANDLER_REMOVE] = {
246 .method_name = "Remove",
247 .arguments = NULL,
248 .n_arguments = 0,
249 .receive_cb = handle_entry_remove }
250 };
251
252 enum signal_index {
253 SIGNAL_NEW_ENTRY,
254 SIGNAL_ENTRY_REMOVED,
255 SIGNAL_MAX
256 };
257
258 enum entry_signal_index {
259 ENTRY_SIGNAL_DEVICE_UPDATED,
260 ENTRY_SIGNAL_VOLUME_UPDATED,
261 ENTRY_SIGNAL_MUTE_UPDATED,
262 ENTRY_SIGNAL_MAX
263 };
264
265 static pa_dbus_arg_info new_entry_args[] = { { "entry", "o", NULL } };
266 static pa_dbus_arg_info entry_removed_args[] = { { "entry", "o", NULL } };
267
268 static pa_dbus_arg_info entry_device_updated_args[] = { { "device", "s", NULL } };
269 static pa_dbus_arg_info entry_volume_updated_args[] = { { "volume", "a(uu)", NULL } };
270 static pa_dbus_arg_info entry_mute_updated_args[] = { { "muted", "b", NULL } };
271
272 static pa_dbus_signal_info signals[SIGNAL_MAX] = {
273 [SIGNAL_NEW_ENTRY] = { .name = "NewEntry", .arguments = new_entry_args, .n_arguments = 1 },
274 [SIGNAL_ENTRY_REMOVED] = { .name = "EntryRemoved", .arguments = entry_removed_args, .n_arguments = 1 }
275 };
276
277 static pa_dbus_signal_info entry_signals[ENTRY_SIGNAL_MAX] = {
278 [ENTRY_SIGNAL_DEVICE_UPDATED] = { .name = "DeviceUpdated", .arguments = entry_device_updated_args, .n_arguments = 1 },
279 [ENTRY_SIGNAL_VOLUME_UPDATED] = { .name = "VolumeUpdated", .arguments = entry_volume_updated_args, .n_arguments = 1 },
280 [ENTRY_SIGNAL_MUTE_UPDATED] = { .name = "MuteUpdated", .arguments = entry_mute_updated_args, .n_arguments = 1 }
281 };
282
283 static pa_dbus_interface_info stream_restore_interface_info = {
284 .name = INTERFACE_STREAM_RESTORE,
285 .method_handlers = method_handlers,
286 .n_method_handlers = METHOD_HANDLER_MAX,
287 .property_handlers = property_handlers,
288 .n_property_handlers = PROPERTY_HANDLER_MAX,
289 .get_all_properties_cb = handle_get_all,
290 .signals = signals,
291 .n_signals = SIGNAL_MAX
292 };
293
294 static pa_dbus_interface_info entry_interface_info = {
295 .name = INTERFACE_ENTRY,
296 .method_handlers = entry_method_handlers,
297 .n_method_handlers = ENTRY_METHOD_HANDLER_MAX,
298 .property_handlers = entry_property_handlers,
299 .n_property_handlers = ENTRY_PROPERTY_HANDLER_MAX,
300 .get_all_properties_cb = handle_entry_get_all,
301 .signals = entry_signals,
302 .n_signals = ENTRY_SIGNAL_MAX
303 };
304
305 static struct dbus_entry *dbus_entry_new(struct userdata *u, const char *entry_name) {
306 struct dbus_entry *de;
307
308 pa_assert(u);
309 pa_assert(entry_name);
310 pa_assert(*entry_name);
311
312 de = pa_xnew(struct dbus_entry, 1);
313 de->userdata = u;
314 de->entry_name = pa_xstrdup(entry_name);
315 de->index = u->next_index++;
316 de->object_path = pa_sprintf_malloc("%s/%s%u", OBJECT_PATH, ENTRY_OBJECT_NAME, de->index);
317
318 pa_assert_se(pa_dbus_protocol_add_interface(u->dbus_protocol, de->object_path, &entry_interface_info, de) >= 0);
319
320 return de;
321 }
322
323 static void dbus_entry_free(struct dbus_entry *de) {
324 pa_assert(de);
325
326 pa_assert_se(pa_dbus_protocol_remove_interface(de->userdata->dbus_protocol, de->object_path, entry_interface_info.name) >= 0);
327
328 pa_xfree(de->entry_name);
329 pa_xfree(de->object_path);
330 }
331
332 /* Reads an array [(UInt32, UInt32)] from the iterator. The struct items are
333 * are a channel position and a volume value, respectively. The result is
334 * stored in the map and vol arguments. The iterator must point to a "a(uu)"
335 * element. If the data is invalid, an error reply is sent and a negative
336 * number is returned. In case of a failure we make no guarantees about the
337 * state of map and vol. In case of an empty array the channels field of both
338 * map and vol are set to 0. This function calls dbus_message_iter_next(iter)
339 * before returning. */
340 static int get_volume_arg(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, pa_channel_map *map, pa_cvolume *vol) {
341 DBusMessageIter array_iter;
342 DBusMessageIter struct_iter;
343
344 pa_assert(conn);
345 pa_assert(msg);
346 pa_assert(iter);
347 pa_assert(pa_streq(dbus_message_iter_get_signature(iter), "a(uu)"));
348 pa_assert(map);
349 pa_assert(vol);
350
351 pa_channel_map_init(map);
352 pa_cvolume_init(vol);
353
354 map->channels = 0;
355 vol->channels = 0;
356
357 dbus_message_iter_recurse(iter, &array_iter);
358
359 while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) {
360 dbus_uint32_t chan_pos;
361 dbus_uint32_t chan_vol;
362
363 dbus_message_iter_recurse(&array_iter, &struct_iter);
364
365 dbus_message_iter_get_basic(&struct_iter, &chan_pos);
366
367 if (chan_pos >= PA_CHANNEL_POSITION_MAX) {
368 pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position: %u", chan_pos);
369 return -1;
370 }
371
372 pa_assert_se(dbus_message_iter_next(&struct_iter));
373 dbus_message_iter_get_basic(&struct_iter, &chan_vol);
374
375 if (!PA_VOLUME_IS_VALID(chan_vol)) {
376 pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume: %u", chan_vol);
377 return -1;
378 }
379
380 if (map->channels < PA_CHANNELS_MAX) {
381 map->map[map->channels] = chan_pos;
382 vol->values[map->channels] = chan_vol;
383 }
384 ++map->channels;
385 ++vol->channels;
386
387 dbus_message_iter_next(&array_iter);
388 }
389
390 if (map->channels > PA_CHANNELS_MAX) {
391 pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Too many channels: %u. The maximum is %u.", map->channels, PA_CHANNELS_MAX);
392 return -1;
393 }
394
395 dbus_message_iter_next(iter);
396
397 return 0;
398 }
399
400 static void append_volume(DBusMessageIter *iter, struct entry *e) {
401 DBusMessageIter array_iter;
402 DBusMessageIter struct_iter;
403 unsigned i;
404
405 pa_assert(iter);
406 pa_assert(e);
407
408 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "(uu)", &array_iter));
409
410 if (!e->volume_valid) {
411 pa_assert_se(dbus_message_iter_close_container(iter, &array_iter));
412 return;
413 }
414
415 for (i = 0; i < e->channel_map.channels; ++i) {
416 pa_assert_se(dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter));
417
418 pa_assert_se(dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT32, &e->channel_map.map[i]));
419 pa_assert_se(dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT32, &e->volume.values[i]));
420
421 pa_assert_se(dbus_message_iter_close_container(&array_iter, &struct_iter));
422 }
423
424 pa_assert_se(dbus_message_iter_close_container(iter, &array_iter));
425 }
426
427 static void append_volume_variant(DBusMessageIter *iter, struct entry *e) {
428 DBusMessageIter variant_iter;
429
430 pa_assert(iter);
431 pa_assert(e);
432
433 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a(uu)", &variant_iter));
434
435 append_volume(&variant_iter, e);
436
437 pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
438 }
439
440 static void send_new_entry_signal(struct dbus_entry *entry) {
441 DBusMessage *signal_msg;
442
443 pa_assert(entry);
444
445 pa_assert_se(signal_msg = dbus_message_new_signal(OBJECT_PATH, INTERFACE_STREAM_RESTORE, signals[SIGNAL_NEW_ENTRY].name));
446 pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &entry->object_path, DBUS_TYPE_INVALID));
447 pa_dbus_protocol_send_signal(entry->userdata->dbus_protocol, signal_msg);
448 dbus_message_unref(signal_msg);
449 }
450
451 static void send_entry_removed_signal(struct dbus_entry *entry) {
452 DBusMessage *signal_msg;
453
454 pa_assert(entry);
455
456 pa_assert_se(signal_msg = dbus_message_new_signal(OBJECT_PATH, INTERFACE_STREAM_RESTORE, signals[SIGNAL_ENTRY_REMOVED].name));
457 pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &entry->object_path, DBUS_TYPE_INVALID));
458 pa_dbus_protocol_send_signal(entry->userdata->dbus_protocol, signal_msg);
459 dbus_message_unref(signal_msg);
460 }
461
462 static void send_device_updated_signal(struct dbus_entry *de, struct entry *e) {
463 DBusMessage *signal_msg;
464 const char *device;
465
466 pa_assert(de);
467 pa_assert(e);
468
469 device = e->device_valid ? e->device : "";
470
471 pa_assert_se(signal_msg = dbus_message_new_signal(de->object_path, INTERFACE_ENTRY, entry_signals[ENTRY_SIGNAL_DEVICE_UPDATED].name));
472 pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_STRING, &device, DBUS_TYPE_INVALID));
473 pa_dbus_protocol_send_signal(de->userdata->dbus_protocol, signal_msg);
474 dbus_message_unref(signal_msg);
475 }
476
477 static void send_volume_updated_signal(struct dbus_entry *de, struct entry *e) {
478 DBusMessage *signal_msg;
479 DBusMessageIter msg_iter;
480
481 pa_assert(de);
482 pa_assert(e);
483
484 pa_assert_se(signal_msg = dbus_message_new_signal(de->object_path, INTERFACE_ENTRY, entry_signals[ENTRY_SIGNAL_VOLUME_UPDATED].name));
485 dbus_message_iter_init_append(signal_msg, &msg_iter);
486 append_volume(&msg_iter, e);
487 pa_dbus_protocol_send_signal(de->userdata->dbus_protocol, signal_msg);
488 dbus_message_unref(signal_msg);
489 }
490
491 static void send_mute_updated_signal(struct dbus_entry *de, struct entry *e) {
492 DBusMessage *signal_msg;
493 dbus_bool_t muted;
494
495 pa_assert(de);
496 pa_assert(e);
497
498 pa_assert(e->muted_valid);
499
500 muted = e->muted;
501
502 pa_assert_se(signal_msg = dbus_message_new_signal(de->object_path, INTERFACE_ENTRY, entry_signals[ENTRY_SIGNAL_MUTE_UPDATED].name));
503 pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_BOOLEAN, &muted, DBUS_TYPE_INVALID));
504 pa_dbus_protocol_send_signal(de->userdata->dbus_protocol, signal_msg);
505 dbus_message_unref(signal_msg);
506 }
507
508 static void handle_get_interface_revision(DBusConnection *conn, DBusMessage *msg, void *userdata) {
509 dbus_uint32_t interface_revision = DBUS_INTERFACE_REVISION;
510
511 pa_assert(conn);
512 pa_assert(msg);
513
514 pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &interface_revision);
515 }
516
517 /* The caller frees the array, but not the strings. */
518 static const char **get_entries(struct userdata *u, unsigned *n) {
519 const char **entries;
520 unsigned i = 0;
521 void *state = NULL;
522 struct dbus_entry *de;
523
524 pa_assert(u);
525 pa_assert(n);
526
527 *n = pa_hashmap_size(u->dbus_entries);
528
529 if (*n == 0)
530 return NULL;
531
532 entries = pa_xnew(const char *, *n);
533
534 PA_HASHMAP_FOREACH(de, u->dbus_entries, state)
535 entries[i++] = de->object_path;
536
537 return entries;
538 }
539
540 static void handle_get_entries(DBusConnection *conn, DBusMessage *msg, void *userdata) {
541 struct userdata *u = userdata;
542 const char **entries;
543 unsigned n;
544
545 pa_assert(conn);
546 pa_assert(msg);
547 pa_assert(u);
548
549 entries = get_entries(u, &n);
550
551 pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, entries, n);
552
553 pa_xfree(entries);
554 }
555
556 static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
557 struct userdata *u = userdata;
558 DBusMessage *reply = NULL;
559 DBusMessageIter msg_iter;
560 DBusMessageIter dict_iter;
561 dbus_uint32_t interface_revision;
562 const char **entries;
563 unsigned n_entries;
564
565 pa_assert(conn);
566 pa_assert(msg);
567 pa_assert(u);
568
569 interface_revision = DBUS_INTERFACE_REVISION;
570 entries = get_entries(u, &n_entries);
571
572 pa_assert_se((reply = dbus_message_new_method_return(msg)));
573
574 dbus_message_iter_init_append(reply, &msg_iter);
575 pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
576
577 pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INTERFACE_REVISION].property_name, DBUS_TYPE_UINT32, &interface_revision);
578 pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_ENTRIES].property_name, DBUS_TYPE_OBJECT_PATH, entries, n_entries);
579
580 pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
581
582 pa_assert_se(dbus_connection_send(conn, reply, NULL));
583
584 dbus_message_unref(reply);
585
586 pa_xfree(entries);
587 }
588
589 static void handle_add_entry(DBusConnection *conn, DBusMessage *msg, void *userdata) {
590 struct userdata *u = userdata;
591 DBusMessageIter msg_iter;
592 const char *name = NULL;
593 const char *device = NULL;
594 pa_channel_map map;
595 pa_cvolume vol;
596 dbus_bool_t muted = FALSE;
597 dbus_bool_t apply_immediately = FALSE;
598 struct dbus_entry *dbus_entry = NULL;
599 struct entry *e = NULL;
600
601 pa_assert(conn);
602 pa_assert(msg);
603 pa_assert(u);
604
605 pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
606 dbus_message_iter_get_basic(&msg_iter, &name);
607
608 pa_assert_se(dbus_message_iter_next(&msg_iter));
609 dbus_message_iter_get_basic(&msg_iter, &device);
610
611 pa_assert_se(dbus_message_iter_next(&msg_iter));
612 if (get_volume_arg(conn, msg, &msg_iter, &map, &vol) < 0)
613 return;
614
615 dbus_message_iter_get_basic(&msg_iter, &muted);
616
617 pa_assert_se(dbus_message_iter_next(&msg_iter));
618 dbus_message_iter_get_basic(&msg_iter, &apply_immediately);
619
620 if (!*name) {
621 pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "An empty string was given as the entry name.");
622 return;
623 }
624
625 if ((dbus_entry = pa_hashmap_get(u->dbus_entries, name))) {
626 pa_bool_t mute_updated = FALSE;
627 pa_bool_t volume_updated = FALSE;
628 pa_bool_t device_updated = FALSE;
629
630 pa_assert_se(e = entry_read(u, name));
631 mute_updated = e->muted != muted;
632 e->muted = muted;
633 e->muted_valid = TRUE;
634
635 volume_updated = (e->volume_valid != !!map.channels) || !pa_cvolume_equal(&e->volume, &vol);
636 e->volume = vol;
637 e->channel_map = map;
638 e->volume_valid = !!map.channels;
639
640 device_updated = (e->device_valid != !!device[0]) || !pa_streq(e->device, device);
641 pa_xfree(e->device);
642 e->device = pa_xstrdup(device);
643 e->device_valid = !!device[0];
644
645 if (mute_updated)
646 send_mute_updated_signal(dbus_entry, e);
647 if (volume_updated)
648 send_volume_updated_signal(dbus_entry, e);
649 if (device_updated)
650 send_device_updated_signal(dbus_entry, e);
651
652 } else {
653 dbus_entry = dbus_entry_new(u, name);
654 pa_assert_se(pa_hashmap_put(u->dbus_entries, dbus_entry->entry_name, dbus_entry) == 0);
655
656 e = entry_new();
657 e->muted_valid = TRUE;
658 e->volume_valid = !!map.channels;
659 e->device_valid = !!device[0];
660 e->muted = muted;
661 e->volume = vol;
662 e->channel_map = map;
663 e->device = pa_xstrdup(device);
664
665 send_new_entry_signal(dbus_entry);
666 }
667
668 pa_assert_se(entry_write(u, name, e, TRUE));
669
670 if (apply_immediately)
671 entry_apply(u, name, e);
672
673 trigger_save(u);
674
675 pa_dbus_send_empty_reply(conn, msg);
676
677 entry_free(e);
678 }
679
680 static void handle_get_entry_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
681 struct userdata *u = userdata;
682 const char *name;
683 struct dbus_entry *de;
684
685 pa_assert(conn);
686 pa_assert(msg);
687 pa_assert(u);
688
689 pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
690
691 if (!(de = pa_hashmap_get(u->dbus_entries, name))) {
692 pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such stream restore entry.");
693 return;
694 }
695
696 pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &de->object_path);
697 }
698
699 static void handle_entry_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
700 struct dbus_entry *de = userdata;
701
702 pa_assert(conn);
703 pa_assert(msg);
704 pa_assert(de);
705
706 pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &de->index);
707 }
708
709 static void handle_entry_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
710 struct dbus_entry *de = userdata;
711
712 pa_assert(conn);
713 pa_assert(msg);
714 pa_assert(de);
715
716 pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &de->entry_name);
717 }
718
719 static void handle_entry_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
720 struct dbus_entry *de = userdata;
721 struct entry *e;
722 const char *device;
723
724 pa_assert(conn);
725 pa_assert(msg);
726 pa_assert(de);
727
728 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
729
730 device = e->device_valid ? e->device : "";
731
732 pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &device);
733
734 entry_free(e);
735 }
736
737 static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
738 struct dbus_entry *de = userdata;
739 const char *device;
740 struct entry *e;
741 pa_bool_t updated;
742
743 pa_assert(conn);
744 pa_assert(msg);
745 pa_assert(iter);
746 pa_assert(de);
747
748 dbus_message_iter_get_basic(iter, &device);
749
750 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
751
752 updated = (e->device_valid != !!device[0]) || !pa_streq(e->device, device);
753
754 if (updated) {
755 pa_xfree(e->device);
756 e->device = pa_xstrdup(device);
757 e->device_valid = !!device[0];
758
759 pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
760
761 entry_apply(de->userdata, de->entry_name, e);
762 send_device_updated_signal(de, e);
763 trigger_save(de->userdata);
764 }
765
766 pa_dbus_send_empty_reply(conn, msg);
767
768 entry_free(e);
769 }
770
771 static void handle_entry_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
772 struct dbus_entry *de = userdata;
773 DBusMessage *reply;
774 DBusMessageIter msg_iter;
775 struct entry *e;
776
777 pa_assert(conn);
778 pa_assert(msg);
779 pa_assert(de);
780
781 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
782
783 pa_assert_se(reply = dbus_message_new_method_return(msg));
784
785 dbus_message_iter_init_append(reply, &msg_iter);
786 append_volume_variant(&msg_iter, e);
787
788 pa_assert_se(dbus_connection_send(conn, reply, NULL));
789
790 entry_free(e);
791 }
792
793 static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
794 struct dbus_entry *de = userdata;
795 pa_channel_map map;
796 pa_cvolume vol;
797 struct entry *e = NULL;
798 pa_bool_t updated = FALSE;
799
800 pa_assert(conn);
801 pa_assert(msg);
802 pa_assert(iter);
803 pa_assert(de);
804
805 if (get_volume_arg(conn, msg, iter, &map, &vol) < 0)
806 return;
807
808 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
809
810 updated = (e->volume_valid != !!map.channels) || !pa_cvolume_equal(&e->volume, &vol);
811
812 if (updated) {
813 e->volume = vol;
814 e->channel_map = map;
815 e->volume_valid = !!map.channels;
816
817 pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
818
819 entry_apply(de->userdata, de->entry_name, e);
820 send_volume_updated_signal(de, e);
821 trigger_save(de->userdata);
822 }
823
824 pa_dbus_send_empty_reply(conn, msg);
825
826 entry_free(e);
827 }
828
829 static void handle_entry_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata) {
830 struct dbus_entry *de = userdata;
831 struct entry *e;
832 dbus_bool_t mute;
833
834 pa_assert(conn);
835 pa_assert(msg);
836 pa_assert(de);
837
838 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
839
840 mute = e->muted_valid ? e->muted : FALSE;
841
842 pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &mute);
843
844 entry_free(e);
845 }
846
847 static void handle_entry_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
848 struct dbus_entry *de = userdata;
849 dbus_bool_t mute;
850 struct entry *e;
851 pa_bool_t updated;
852
853 pa_assert(conn);
854 pa_assert(msg);
855 pa_assert(iter);
856 pa_assert(de);
857
858 dbus_message_iter_get_basic(iter, &mute);
859
860 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
861
862 updated = !e->muted_valid || e->muted != mute;
863
864 if (updated) {
865 e->muted = mute;
866 e->muted_valid = TRUE;
867
868 pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
869
870 entry_apply(de->userdata, de->entry_name, e);
871 send_mute_updated_signal(de, e);
872 trigger_save(de->userdata);
873 }
874
875 pa_dbus_send_empty_reply(conn, msg);
876
877 entry_free(e);
878 }
879
880 static void handle_entry_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
881 struct dbus_entry *de = userdata;
882 struct entry *e;
883 DBusMessage *reply = NULL;
884 DBusMessageIter msg_iter;
885 DBusMessageIter dict_iter;
886 DBusMessageIter dict_entry_iter;
887 const char *device;
888 dbus_bool_t mute;
889
890 pa_assert(conn);
891 pa_assert(msg);
892 pa_assert(de);
893
894 pa_assert_se(e = entry_read(de->userdata, de->entry_name));
895
896 device = e->device_valid ? e->device : "";
897 mute = e->muted_valid ? e->muted : FALSE;
898
899 pa_assert_se((reply = dbus_message_new_method_return(msg)));
900
901 dbus_message_iter_init_append(reply, &msg_iter);
902 pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
903
904 pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &de->index);
905 pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &de->entry_name);
906 pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_DEVICE].property_name, DBUS_TYPE_STRING, &device);
907
908 pa_assert_se(dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
909
910 pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &entry_property_handlers[ENTRY_PROPERTY_HANDLER_VOLUME].property_name));
911 append_volume_variant(&dict_entry_iter, e);
912
913 pa_assert_se(dbus_message_iter_close_container(&dict_iter, &dict_entry_iter));
914
915 pa_dbus_append_basic_variant_dict_entry(&dict_iter, entry_property_handlers[ENTRY_PROPERTY_HANDLER_MUTE].property_name, DBUS_TYPE_BOOLEAN, &mute);
916
917 pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
918
919 pa_assert_se(dbus_connection_send(conn, reply, NULL));
920
921 dbus_message_unref(reply);
922
923 entry_free(e);
924 }
925
926 static void handle_entry_remove(DBusConnection *conn, DBusMessage *msg, void *userdata) {
927 struct dbus_entry *de = userdata;
928 pa_datum key;
929
930 pa_assert(conn);
931 pa_assert(msg);
932 pa_assert(de);
933
934 key.data = de->entry_name;
935 key.size = strlen(de->entry_name);
936
937 pa_assert_se(pa_database_unset(de->userdata->database, &key) == 0);
938
939 send_entry_removed_signal(de);
940 trigger_save(de->userdata);
941
942 pa_assert_se(pa_hashmap_remove(de->userdata->dbus_entries, de->entry_name));
943 dbus_entry_free(de);
944
945 pa_dbus_send_empty_reply(conn, msg);
946 }
947
948 #endif /* HAVE_DBUS */
949
950 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
951 struct userdata *u = userdata;
952
953 pa_assert(a);
954 pa_assert(e);
955 pa_assert(u);
956
957 pa_assert(e == u->save_time_event);
958 u->core->mainloop->time_free(u->save_time_event);
959 u->save_time_event = NULL;
960
961 pa_database_sync(u->database);
962 pa_log_info("Synced.");
963 }
964
965 static char *get_name(pa_proplist *p, const char *prefix) {
966 const char *r;
967 char *t;
968
969 if (!p)
970 return NULL;
971
972 if ((r = pa_proplist_gets(p, IDENTIFICATION_PROPERTY)))
973 return pa_xstrdup(r);
974
975 if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_ROLE)))
976 t = pa_sprintf_malloc("%s-by-media-role:%s", prefix, r);
977 else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_ID)))
978 t = pa_sprintf_malloc("%s-by-application-id:%s", prefix, r);
979 else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME)))
980 t = pa_sprintf_malloc("%s-by-application-name:%s", prefix, r);
981 else if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_NAME)))
982 t = pa_sprintf_malloc("%s-by-media-name:%s", prefix, r);
983 else
984 t = pa_sprintf_malloc("%s-fallback:%s", prefix, r);
985
986 pa_proplist_sets(p, IDENTIFICATION_PROPERTY, t);
987 return t;
988 }
989
990 static struct entry* entry_new(void) {
991 struct entry *r = pa_xnew0(struct entry, 1);
992 r->version = ENTRY_VERSION;
993 return r;
994 }
995
996 static void entry_free(struct entry* e) {
997 pa_assert(e);
998
999 pa_xfree(e->device);
1000 pa_xfree(e->card);
1001 pa_xfree(e);
1002 }
1003
1004 static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e, pa_bool_t replace) {
1005 pa_tagstruct *t;
1006 pa_datum key, data;
1007 pa_bool_t r;
1008
1009 pa_assert(u);
1010 pa_assert(name);
1011 pa_assert(e);
1012
1013 t = pa_tagstruct_new(NULL, 0);
1014 pa_tagstruct_putu8(t, e->version);
1015 pa_tagstruct_put_boolean(t, e->volume_valid);
1016 pa_tagstruct_put_channel_map(t, &e->channel_map);
1017 pa_tagstruct_put_cvolume(t, &e->volume);
1018 pa_tagstruct_put_boolean(t, e->muted_valid);
1019 pa_tagstruct_put_boolean(t, e->muted);
1020 pa_tagstruct_put_boolean(t, e->device_valid);
1021 pa_tagstruct_puts(t, e->device);
1022 pa_tagstruct_put_boolean(t, e->card_valid);
1023 pa_tagstruct_puts(t, e->card);
1024
1025 key.data = (char *) name;
1026 key.size = strlen(name);
1027
1028 data.data = (void*)pa_tagstruct_data(t, &data.size);
1029
1030 r = (pa_database_set(u->database, &key, &data, replace) == 0);
1031
1032 pa_tagstruct_free(t);
1033
1034 return r;
1035 }
1036
1037 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
1038
1039 #define LEGACY_ENTRY_VERSION 3
1040 static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
1041 struct legacy_entry {
1042 uint8_t version;
1043 pa_bool_t muted_valid:1, volume_valid:1, device_valid:1, card_valid:1;
1044 pa_bool_t muted:1;
1045 pa_channel_map channel_map;
1046 pa_cvolume volume;
1047 char device[PA_NAME_MAX];
1048 char card[PA_NAME_MAX];
1049 } PA_GCC_PACKED;
1050 struct legacy_entry *le;
1051 struct entry *e;
1052
1053 pa_assert(u);
1054 pa_assert(data);
1055
1056 if (data->size != sizeof(struct legacy_entry)) {
1057 pa_log_debug("Size does not match.");
1058 return NULL;
1059 }
1060
1061 le = (struct legacy_entry*)data->data;
1062
1063 if (le->version != LEGACY_ENTRY_VERSION) {
1064 pa_log_debug("Version mismatch.");
1065 return NULL;
1066 }
1067
1068 if (!memchr(le->device, 0, sizeof(le->device))) {
1069 pa_log_warn("Device has missing NUL byte.");
1070 return NULL;
1071 }
1072
1073 if (!memchr(le->card, 0, sizeof(le->card))) {
1074 pa_log_warn("Card has missing NUL byte.");
1075 return NULL;
1076 }
1077
1078 e = entry_new();
1079 e->card = pa_xstrdup(le->card);
1080 return e;
1081 }
1082 #endif
1083
1084 static struct entry *entry_read(struct userdata *u, const char *name) {
1085 pa_datum key, data;
1086 struct entry *e = NULL;
1087 pa_tagstruct *t = NULL;
1088 const char *device, *card;
1089
1090 pa_assert(u);
1091 pa_assert(name);
1092
1093 key.data = (char*) name;
1094 key.size = strlen(name);
1095
1096 pa_zero(data);
1097
1098 if (!pa_database_get(u->database, &key, &data))
1099 goto fail;
1100
1101 t = pa_tagstruct_new(data.data, data.size);
1102 e = entry_new();
1103
1104 if (pa_tagstruct_getu8(t, &e->version) < 0 ||
1105 e->version > ENTRY_VERSION ||
1106 pa_tagstruct_get_boolean(t, &e->volume_valid) < 0 ||
1107 pa_tagstruct_get_channel_map(t, &e->channel_map) < 0 ||
1108 pa_tagstruct_get_cvolume(t, &e->volume) < 0 ||
1109 pa_tagstruct_get_boolean(t, &e->muted_valid) < 0 ||
1110 pa_tagstruct_get_boolean(t, &e->muted) < 0 ||
1111 pa_tagstruct_get_boolean(t, &e->device_valid) < 0 ||
1112 pa_tagstruct_gets(t, &device) < 0 ||
1113 pa_tagstruct_get_boolean(t, &e->card_valid) < 0 ||
1114 pa_tagstruct_gets(t, &card) < 0) {
1115
1116 goto fail;
1117 }
1118
1119 e->device = pa_xstrdup(device);
1120 e->card = pa_xstrdup(card);
1121
1122 if (!pa_tagstruct_eof(t))
1123 goto fail;
1124
1125 if (e->device_valid && !pa_namereg_is_valid_name(e->device)) {
1126 pa_log_warn("Invalid device name stored in database for stream %s", name);
1127 goto fail;
1128 }
1129
1130 if (e->card_valid && !pa_namereg_is_valid_name(e->card)) {
1131 pa_log_warn("Invalid card name stored in database for stream %s", name);
1132 goto fail;
1133 }
1134
1135 if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
1136 pa_log_warn("Invalid channel map stored in database for stream %s", name);
1137 goto fail;
1138 }
1139
1140 if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) {
1141 pa_log_warn("Invalid volume stored in database for stream %s", name);
1142 goto fail;
1143 }
1144
1145 pa_tagstruct_free(t);
1146 pa_datum_free(&data);
1147
1148 return e;
1149
1150 fail:
1151
1152 pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
1153
1154 if (e)
1155 entry_free(e);
1156 if (t)
1157 pa_tagstruct_free(t);
1158
1159 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
1160 pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
1161 if ((e = legacy_entry_read(u, &data))) {
1162 pa_log_debug("Success. Saving new format for key: %s", name);
1163 if (entry_write(u, name, e, TRUE))
1164 trigger_save(u);
1165 pa_datum_free(&data);
1166 return e;
1167 } else
1168 pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
1169 #endif
1170
1171 pa_datum_free(&data);
1172 return NULL;
1173 }
1174
1175 static struct entry* entry_copy(const struct entry *e) {
1176 struct entry* r;
1177
1178 pa_assert(e);
1179 r = entry_new();
1180 *r = *e;
1181 r->device = pa_xstrdup(e->device);
1182 r->card = pa_xstrdup(e->card);
1183 return r;
1184 }
1185
1186 static void trigger_save(struct userdata *u) {
1187 pa_native_connection *c;
1188 uint32_t idx;
1189
1190 for (c = pa_idxset_first(u->subscribed, &idx); c; c = pa_idxset_next(u->subscribed, &idx)) {
1191 pa_tagstruct *t;
1192
1193 t = pa_tagstruct_new(NULL, 0);
1194 pa_tagstruct_putu32(t, PA_COMMAND_EXTENSION);
1195 pa_tagstruct_putu32(t, 0);
1196 pa_tagstruct_putu32(t, u->module->index);
1197 pa_tagstruct_puts(t, u->module->name);
1198 pa_tagstruct_putu32(t, SUBCOMMAND_EVENT);
1199
1200 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), t);
1201 }
1202
1203 if (u->save_time_event)
1204 return;
1205
1206 u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
1207 }
1208
1209 static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
1210 pa_cvolume t;
1211
1212 pa_assert(a);
1213 pa_assert(b);
1214
1215 if (a->device_valid != b->device_valid ||
1216 (a->device_valid && !pa_streq(a->device, b->device)))
1217 return FALSE;
1218
1219 if (a->card_valid != b->card_valid ||
1220 (a->card_valid && !pa_streq(a->card, b->card)))
1221 return FALSE;
1222
1223 if (a->muted_valid != b->muted_valid ||
1224 (a->muted_valid && (a->muted != b->muted)))
1225 return FALSE;
1226
1227 t = b->volume;
1228 if (a->volume_valid != b->volume_valid ||
1229 (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume)))
1230 return FALSE;
1231
1232 return TRUE;
1233 }
1234
1235 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1236 struct userdata *u = userdata;
1237 struct entry *entry, *old = NULL;
1238 char *name = NULL;
1239
1240 /* These are only used when D-Bus is enabled, but in order to reduce ifdef
1241 * clutter these are defined here unconditionally. */
1242 pa_bool_t created_new_entry = TRUE;
1243 pa_bool_t device_updated = FALSE;
1244 pa_bool_t volume_updated = FALSE;
1245 pa_bool_t mute_updated = FALSE;
1246
1247 #ifdef HAVE_DBUS
1248 struct dbus_entry *de = NULL;
1249 #endif
1250
1251 pa_assert(c);
1252 pa_assert(u);
1253
1254 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
1255 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
1256 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
1257 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
1258 return;
1259
1260 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
1261 pa_sink_input *sink_input;
1262
1263 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx)))
1264 return;
1265
1266 if (!(name = get_name(sink_input->proplist, "sink-input")))
1267 return;
1268
1269 if ((old = entry_read(u, name))) {
1270 entry = entry_copy(old);
1271 created_new_entry = FALSE;
1272 } else
1273 entry = entry_new();
1274
1275 if (sink_input->save_volume && pa_sink_input_is_volume_readable(sink_input)) {
1276 pa_assert(sink_input->volume_writable);
1277
1278 entry->channel_map = sink_input->channel_map;
1279 pa_sink_input_get_volume(sink_input, &entry->volume, FALSE);
1280 entry->volume_valid = TRUE;
1281
1282 volume_updated = !created_new_entry
1283 && (!old->volume_valid
1284 || !pa_channel_map_equal(&entry->channel_map, &old->channel_map)
1285 || !pa_cvolume_equal(&entry->volume, &old->volume));
1286 }
1287
1288 if (sink_input->save_muted) {
1289 entry->muted = pa_sink_input_get_mute(sink_input);
1290 entry->muted_valid = TRUE;
1291
1292 mute_updated = !created_new_entry && (!old->muted_valid || entry->muted != old->muted);
1293 }
1294
1295 if (sink_input->save_sink) {
1296 pa_xfree(entry->device);
1297 entry->device = pa_xstrdup(sink_input->sink->name);
1298 entry->device_valid = TRUE;
1299
1300 device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry->device, old->device));
1301 if (sink_input->sink->card) {
1302 pa_xfree(entry->card);
1303 entry->card = pa_xstrdup(sink_input->sink->card->name);
1304 entry->card_valid = TRUE;
1305 }
1306 }
1307
1308 } else {
1309 pa_source_output *source_output;
1310
1311 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT);
1312
1313 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx)))
1314 return;
1315
1316 if (!(name = get_name(source_output->proplist, "source-output")))
1317 return;
1318
1319 if ((old = entry_read(u, name))) {
1320 entry = entry_copy(old);
1321 created_new_entry = FALSE;
1322 } else
1323 entry = entry_new();
1324
1325 if (source_output->save_source) {
1326 pa_xfree(entry->device);
1327 entry->device = pa_xstrdup(source_output->source->name);
1328 entry->device_valid = TRUE;
1329
1330 device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry->device, old->device));
1331
1332 if (source_output->source->card) {
1333 pa_xfree(entry->card);
1334 entry->card = pa_xstrdup(source_output->source->card->name);
1335 entry->card_valid = TRUE;
1336 }
1337 }
1338 }
1339
1340 pa_assert(entry);
1341
1342 if (old) {
1343
1344 if (entries_equal(old, entry)) {
1345 entry_free(old);
1346 entry_free(entry);
1347 pa_xfree(name);
1348 return;
1349 }
1350
1351 entry_free(old);
1352 }
1353
1354 pa_log_info("Storing volume/mute/device for stream %s.", name);
1355
1356 if (entry_write(u, name, entry, TRUE))
1357 trigger_save(u);
1358
1359 #ifdef HAVE_DBUS
1360 if (created_new_entry) {
1361 de = dbus_entry_new(u, name);
1362 pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
1363 send_new_entry_signal(de);
1364 } else {
1365 pa_assert_se(de = pa_hashmap_get(u->dbus_entries, name));
1366
1367 if (device_updated)
1368 send_device_updated_signal(de, entry);
1369 if (volume_updated)
1370 send_volume_updated_signal(de, entry);
1371 if (mute_updated)
1372 send_mute_updated_signal(de, entry);
1373 }
1374 #endif
1375
1376 entry_free(entry);
1377 pa_xfree(name);
1378 }
1379
1380 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
1381 char *name;
1382 struct entry *e;
1383
1384 pa_assert(c);
1385 pa_assert(new_data);
1386 pa_assert(u);
1387 pa_assert(u->restore_device);
1388
1389 if (!(name = get_name(new_data->proplist, "sink-input")))
1390 return PA_HOOK_OK;
1391
1392 if (new_data->sink)
1393 pa_log_debug("Not restoring device for stream %s, because already set to '%s'.", name, new_data->sink->name);
1394 else if ((e = entry_read(u, name))) {
1395 pa_sink *s = NULL;
1396
1397 if (e->device_valid)
1398 s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK);
1399
1400 if (!s && e->card_valid) {
1401 pa_card *card;
1402
1403 if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
1404 s = pa_idxset_first(card->sinks, NULL);
1405 }
1406
1407 /* It might happen that a stream and a sink are set up at the
1408 same time, in which case we want to make sure we don't
1409 interfere with that */
1410 if (s && PA_SINK_IS_LINKED(pa_sink_get_state(s)))
1411 if (pa_sink_input_new_data_set_sink(new_data, s, TRUE))
1412 pa_log_info("Restoring device for stream %s.", name);
1413
1414 entry_free(e);
1415 }
1416
1417 pa_xfree(name);
1418
1419 return PA_HOOK_OK;
1420 }
1421
1422 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
1423 char *name;
1424 struct entry *e;
1425
1426 pa_assert(c);
1427 pa_assert(new_data);
1428 pa_assert(u);
1429 pa_assert(u->restore_volume || u->restore_muted);
1430
1431 if (!(name = get_name(new_data->proplist, "sink-input")))
1432 return PA_HOOK_OK;
1433
1434 if ((e = entry_read(u, name))) {
1435
1436 if (u->restore_volume && e->volume_valid) {
1437 if (!new_data->volume_writable)
1438 pa_log_debug("Not restoring volume for sink input %s, because its volume can't be changed.", name);
1439 else if (new_data->volume_is_set)
1440 pa_log_debug("Not restoring volume for sink input %s, because already set.", name);
1441 else {
1442 pa_cvolume v;
1443
1444 pa_log_info("Restoring volume for sink input %s.", name);
1445
1446 v = e->volume;
1447 pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
1448 pa_sink_input_new_data_set_volume(new_data, &v);
1449
1450 new_data->volume_is_absolute = FALSE;
1451 new_data->save_volume = TRUE;
1452 }
1453 }
1454
1455 if (u->restore_muted && e->muted_valid) {
1456
1457 if (!new_data->muted_is_set) {
1458 pa_log_info("Restoring mute state for sink input %s.", name);
1459 pa_sink_input_new_data_set_muted(new_data, e->muted);
1460 new_data->save_muted = TRUE;
1461 } else
1462 pa_log_debug("Not restoring mute state for sink input %s, because already set.", name);
1463 }
1464
1465 entry_free(e);
1466 }
1467
1468 pa_xfree(name);
1469
1470 return PA_HOOK_OK;
1471 }
1472
1473 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
1474 char *name;
1475 struct entry *e;
1476
1477 pa_assert(c);
1478 pa_assert(new_data);
1479 pa_assert(u);
1480 pa_assert(u->restore_device);
1481
1482 if (new_data->direct_on_input)
1483 return PA_HOOK_OK;
1484
1485 if (!(name = get_name(new_data->proplist, "source-output")))
1486 return PA_HOOK_OK;
1487
1488 if (new_data->source)
1489 pa_log_debug("Not restoring device for stream %s, because already set", name);
1490 else if ((e = entry_read(u, name))) {
1491 pa_source *s = NULL;
1492
1493 if (e->device_valid)
1494 s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE);
1495
1496 if (!s && e->card_valid) {
1497 pa_card *card;
1498
1499 if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
1500 s = pa_idxset_first(card->sources, NULL);
1501 }
1502
1503 /* It might happen that a stream and a sink are set up at the
1504 same time, in which case we want to make sure we don't
1505 interfere with that */
1506 if (s && PA_SOURCE_IS_LINKED(pa_source_get_state(s))) {
1507 pa_log_info("Restoring device for stream %s.", name);
1508 pa_source_output_new_data_set_source(new_data, s, TRUE);
1509 }
1510
1511 entry_free(e);
1512 }
1513
1514 pa_xfree(name);
1515
1516 return PA_HOOK_OK;
1517 }
1518
1519 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
1520 pa_sink_input *si;
1521 uint32_t idx;
1522
1523 pa_assert(c);
1524 pa_assert(sink);
1525 pa_assert(u);
1526 pa_assert(u->on_hotplug && u->restore_device);
1527
1528 PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
1529 char *name;
1530 struct entry *e;
1531
1532 if (si->sink == sink)
1533 continue;
1534
1535 if (si->save_sink)
1536 continue;
1537
1538 /* Skip this if it is already in the process of being moved
1539 * anyway */
1540 if (!si->sink)
1541 continue;
1542
1543 /* It might happen that a stream and a sink are set up at the
1544 same time, in which case we want to make sure we don't
1545 interfere with that */
1546 if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
1547 continue;
1548
1549 if (!(name = get_name(si->proplist, "sink-input")))
1550 continue;
1551
1552 if ((e = entry_read(u, name))) {
1553 if (e->device_valid && pa_streq(e->device, sink->name))
1554 pa_sink_input_move_to(si, sink, TRUE);
1555
1556 entry_free(e);
1557 }
1558
1559 pa_xfree(name);
1560 }
1561
1562 return PA_HOOK_OK;
1563 }
1564
1565 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
1566 pa_source_output *so;
1567 uint32_t idx;
1568
1569 pa_assert(c);
1570 pa_assert(source);
1571 pa_assert(u);
1572 pa_assert(u->on_hotplug && u->restore_device);
1573
1574 PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
1575 char *name;
1576 struct entry *e;
1577
1578 if (so->source == source)
1579 continue;
1580
1581 if (so->save_source)
1582 continue;
1583
1584 if (so->direct_on_input)
1585 continue;
1586
1587 /* Skip this if it is already in the process of being moved anyway */
1588 if (!so->source)
1589 continue;
1590
1591 /* It might happen that a stream and a source are set up at the
1592 same time, in which case we want to make sure we don't
1593 interfere with that */
1594 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
1595 continue;
1596
1597 if (!(name = get_name(so->proplist, "source-output")))
1598 continue;
1599
1600 if ((e = entry_read(u, name))) {
1601 if (e->device_valid && pa_streq(e->device, source->name))
1602 pa_source_output_move_to(so, source, TRUE);
1603
1604 entry_free(e);
1605 }
1606
1607 pa_xfree(name);
1608 }
1609
1610 return PA_HOOK_OK;
1611 }
1612
1613 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
1614 pa_sink_input *si;
1615 uint32_t idx;
1616
1617 pa_assert(c);
1618 pa_assert(sink);
1619 pa_assert(u);
1620 pa_assert(u->on_rescue && u->restore_device);
1621
1622 /* There's no point in doing anything if the core is shut down anyway */
1623 if (c->state == PA_CORE_SHUTDOWN)
1624 return PA_HOOK_OK;
1625
1626 PA_IDXSET_FOREACH(si, sink->inputs, idx) {
1627 char *name;
1628 struct entry *e;
1629
1630 if (!si->sink)
1631 continue;
1632
1633 if (!(name = get_name(si->proplist, "sink-input")))
1634 continue;
1635
1636 if ((e = entry_read(u, name))) {
1637
1638 if (e->device_valid) {
1639 pa_sink *d;
1640
1641 if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) &&
1642 d != sink &&
1643 PA_SINK_IS_LINKED(pa_sink_get_state(d)))
1644 pa_sink_input_move_to(si, d, TRUE);
1645 }
1646
1647 entry_free(e);
1648 }
1649
1650 pa_xfree(name);
1651 }
1652
1653 return PA_HOOK_OK;
1654 }
1655
1656 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
1657 pa_source_output *so;
1658 uint32_t idx;
1659
1660 pa_assert(c);
1661 pa_assert(source);
1662 pa_assert(u);
1663 pa_assert(u->on_rescue && u->restore_device);
1664
1665 /* There's no point in doing anything if the core is shut down anyway */
1666 if (c->state == PA_CORE_SHUTDOWN)
1667 return PA_HOOK_OK;
1668
1669 PA_IDXSET_FOREACH(so, source->outputs, idx) {
1670 char *name;
1671 struct entry *e;
1672
1673 if (so->direct_on_input)
1674 continue;
1675
1676 if (!so->source)
1677 continue;
1678
1679 if (!(name = get_name(so->proplist, "source-output")))
1680 continue;
1681
1682 if ((e = entry_read(u, name))) {
1683
1684 if (e->device_valid) {
1685 pa_source *d;
1686
1687 if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) &&
1688 d != source &&
1689 PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
1690 pa_source_output_move_to(so, d, TRUE);
1691 }
1692
1693 entry_free(e);
1694 }
1695
1696 pa_xfree(name);
1697 }
1698
1699 return PA_HOOK_OK;
1700 }
1701
1702 #define EXT_VERSION 1
1703
1704 static void entry_apply(struct userdata *u, const char *name, struct entry *e) {
1705 pa_sink_input *si;
1706 pa_source_output *so;
1707 uint32_t idx;
1708
1709 pa_assert(u);
1710 pa_assert(name);
1711 pa_assert(e);
1712
1713 PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
1714 char *n;
1715 pa_sink *s;
1716
1717 if (!(n = get_name(si->proplist, "sink-input")))
1718 continue;
1719
1720 if (!pa_streq(name, n)) {
1721 pa_xfree(n);
1722 continue;
1723 }
1724 pa_xfree(n);
1725
1726 if (u->restore_volume && e->volume_valid && si->volume_writable) {
1727 pa_cvolume v;
1728
1729 v = e->volume;
1730 pa_log_info("Restoring volume for sink input %s.", name);
1731 pa_cvolume_remap(&v, &e->channel_map, &si->channel_map);
1732 pa_sink_input_set_volume(si, &v, TRUE, FALSE);
1733 }
1734
1735 if (u->restore_muted && e->muted_valid) {
1736 pa_log_info("Restoring mute state for sink input %s.", name);
1737 pa_sink_input_set_mute(si, e->muted, TRUE);
1738 }
1739
1740 if (u->restore_device) {
1741 if (!e->device_valid) {
1742 if (si->save_sink) {
1743 pa_log_info("Ensuring device is not saved for stream %s.", name);
1744 /* If the device is not valid we should make sure the
1745 save flag is cleared as the user may have specifically
1746 removed the sink element from the rule. */
1747 si->save_sink = FALSE;
1748 /* This is cheating a bit. The sink input itself has not changed
1749 but the rules governing it's routing have, so we fire this event
1750 such that other routing modules (e.g. module-device-manager)
1751 will pick up the change and reapply their routing */
1752 pa_subscription_post(si->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, si->index);
1753 }
1754 } else if ((s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SINK))) {
1755 pa_log_info("Restoring device for stream %s.", name);
1756 pa_sink_input_move_to(si, s, TRUE);
1757 }
1758 }
1759 }
1760
1761 PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
1762 char *n;
1763 pa_source *s;
1764
1765 if (!(n = get_name(so->proplist, "source-output")))
1766 continue;
1767
1768 if (!pa_streq(name, n)) {
1769 pa_xfree(n);
1770 continue;
1771 }
1772 pa_xfree(n);
1773
1774 if (u->restore_device) {
1775 if (!e->device_valid) {
1776 if (so->save_source) {
1777 pa_log_info("Ensuring device is not saved for stream %s.", name);
1778 /* If the device is not valid we should make sure the
1779 save flag is cleared as the user may have specifically
1780 removed the source element from the rule. */
1781 so->save_source = FALSE;
1782 /* This is cheating a bit. The source output itself has not changed
1783 but the rules governing it's routing have, so we fire this event
1784 such that other routing modules (e.g. module-device-manager)
1785 will pick up the change and reapply their routing */
1786 pa_subscription_post(so->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, so->index);
1787 }
1788 } else if ((s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SOURCE))) {
1789 pa_log_info("Restoring device for stream %s.", name);
1790 pa_source_output_move_to(so, s, TRUE);
1791 }
1792 }
1793 }
1794 }
1795
1796 #ifdef DEBUG_VOLUME
1797 PA_GCC_UNUSED static void stream_restore_dump_database(struct userdata *u) {
1798 pa_datum key;
1799 pa_bool_t done;
1800
1801 done = !pa_database_first(u->database, &key, NULL);
1802
1803 while (!done) {
1804 pa_datum next_key;
1805 struct entry *e;
1806 char *name;
1807
1808 done = !pa_database_next(u->database, &key, &next_key, NULL);
1809
1810 name = pa_xstrndup(key.data, key.size);
1811 pa_datum_free(&key);
1812
1813 if ((e = entry_read(u, name))) {
1814 char t[256];
1815 pa_log("name=%s", name);
1816 pa_log("device=%s %s", e->device, pa_yes_no(e->device_valid));
1817 pa_log("channel_map=%s", pa_channel_map_snprint(t, sizeof(t), &e->channel_map));
1818 pa_log("volume=%s %s", pa_cvolume_snprint(t, sizeof(t), &e->volume), pa_yes_no(e->volume_valid));
1819 pa_log("mute=%s %s", pa_yes_no(e->muted), pa_yes_no(e->volume_valid));
1820 entry_free(e);
1821 }
1822
1823 pa_xfree(name);
1824
1825 key = next_key;
1826 }
1827 }
1828 #endif
1829
1830 static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
1831 struct userdata *u;
1832 uint32_t command;
1833 pa_tagstruct *reply = NULL;
1834
1835 pa_assert(p);
1836 pa_assert(m);
1837 pa_assert(c);
1838 pa_assert(t);
1839
1840 u = m->userdata;
1841
1842 if (pa_tagstruct_getu32(t, &command) < 0)
1843 goto fail;
1844
1845 reply = pa_tagstruct_new(NULL, 0);
1846 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1847 pa_tagstruct_putu32(reply, tag);
1848
1849 switch (command) {
1850 case SUBCOMMAND_TEST: {
1851 if (!pa_tagstruct_eof(t))
1852 goto fail;
1853
1854 pa_tagstruct_putu32(reply, EXT_VERSION);
1855 break;
1856 }
1857
1858 case SUBCOMMAND_READ: {
1859 pa_datum key;
1860 pa_bool_t done;
1861
1862 if (!pa_tagstruct_eof(t))
1863 goto fail;
1864
1865 done = !pa_database_first(u->database, &key, NULL);
1866
1867 while (!done) {
1868 pa_datum next_key;
1869 struct entry *e;
1870 char *name;
1871
1872 done = !pa_database_next(u->database, &key, &next_key, NULL);
1873
1874 name = pa_xstrndup(key.data, key.size);
1875 pa_datum_free(&key);
1876
1877 if ((e = entry_read(u, name))) {
1878 pa_cvolume r;
1879 pa_channel_map cm;
1880
1881 pa_tagstruct_puts(reply, name);
1882 pa_tagstruct_put_channel_map(reply, e->volume_valid ? &e->channel_map : pa_channel_map_init(&cm));
1883 pa_tagstruct_put_cvolume(reply, e->volume_valid ? &e->volume : pa_cvolume_init(&r));
1884 pa_tagstruct_puts(reply, e->device_valid ? e->device : NULL);
1885 pa_tagstruct_put_boolean(reply, e->muted_valid ? e->muted : FALSE);
1886
1887 entry_free(e);
1888 }
1889
1890 pa_xfree(name);
1891
1892 key = next_key;
1893 }
1894
1895 break;
1896 }
1897
1898 case SUBCOMMAND_WRITE: {
1899 uint32_t mode;
1900 pa_bool_t apply_immediately = FALSE;
1901
1902 if (pa_tagstruct_getu32(t, &mode) < 0 ||
1903 pa_tagstruct_get_boolean(t, &apply_immediately) < 0)
1904 goto fail;
1905
1906 if (mode != PA_UPDATE_MERGE &&
1907 mode != PA_UPDATE_REPLACE &&
1908 mode != PA_UPDATE_SET)
1909 goto fail;
1910
1911 if (mode == PA_UPDATE_SET) {
1912 #ifdef HAVE_DBUS
1913 struct dbus_entry *de;
1914 void *state = NULL;
1915
1916 PA_HASHMAP_FOREACH(de, u->dbus_entries, state) {
1917 send_entry_removed_signal(de);
1918 dbus_entry_free(pa_hashmap_remove(u->dbus_entries, de->entry_name));
1919 }
1920 #endif
1921 pa_database_clear(u->database);
1922 }
1923
1924 while (!pa_tagstruct_eof(t)) {
1925 const char *name, *device;
1926 pa_bool_t muted;
1927 struct entry *entry;
1928 #ifdef HAVE_DBUS
1929 struct entry *old;
1930 #endif
1931
1932 entry = entry_new();
1933
1934 if (pa_tagstruct_gets(t, &name) < 0 ||
1935 pa_tagstruct_get_channel_map(t, &entry->channel_map) ||
1936 pa_tagstruct_get_cvolume(t, &entry->volume) < 0 ||
1937 pa_tagstruct_gets(t, &device) < 0 ||
1938 pa_tagstruct_get_boolean(t, &muted) < 0)
1939 goto fail;
1940
1941 if (!name || !*name) {
1942 entry_free(entry);
1943 goto fail;
1944 }
1945
1946 entry->volume_valid = entry->volume.channels > 0;
1947
1948 if (entry->volume_valid)
1949 if (!pa_cvolume_compatible_with_channel_map(&entry->volume, &entry->channel_map)) {
1950 entry_free(entry);
1951 goto fail;
1952 }
1953
1954 entry->muted = muted;
1955 entry->muted_valid = TRUE;
1956
1957 entry->device = pa_xstrdup(device);
1958 entry->device_valid = device && !!entry->device[0];
1959
1960 if (entry->device_valid && !pa_namereg_is_valid_name(entry->device)) {
1961 entry_free(entry);
1962 goto fail;
1963 }
1964
1965 #ifdef HAVE_DBUS
1966 old = entry_read(u, name);
1967 #endif
1968
1969 pa_log_debug("Client %s changes entry %s.",
1970 pa_strnull(pa_proplist_gets(pa_native_connection_get_client(c)->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)),
1971 name);
1972
1973 if (entry_write(u, name, entry, mode == PA_UPDATE_REPLACE)) {
1974 #ifdef HAVE_DBUS
1975 struct dbus_entry *de;
1976
1977 if (old) {
1978 pa_assert_se((de = pa_hashmap_get(u->dbus_entries, name)));
1979
1980 if ((old->device_valid != entry->device_valid)
1981 || (entry->device_valid && !pa_streq(entry->device, old->device)))
1982 send_device_updated_signal(de, entry);
1983
1984 if ((old->volume_valid != entry->volume_valid)
1985 || (entry->volume_valid && (!pa_cvolume_equal(&entry->volume, &old->volume)
1986 || !pa_channel_map_equal(&entry->channel_map, &old->channel_map))))
1987 send_volume_updated_signal(de, entry);
1988
1989 if (!old->muted_valid || (entry->muted != old->muted))
1990 send_mute_updated_signal(de, entry);
1991
1992 } else {
1993 de = dbus_entry_new(u, name);
1994 pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
1995 send_new_entry_signal(de);
1996 }
1997 #endif
1998
1999 if (apply_immediately)
2000 entry_apply(u, name, entry);
2001 }
2002
2003 #ifdef HAVE_DBUS
2004 if (old)
2005 entry_free(old);
2006 #endif
2007 entry_free(entry);
2008 }
2009
2010 trigger_save(u);
2011
2012 break;
2013 }
2014
2015 case SUBCOMMAND_DELETE:
2016
2017 while (!pa_tagstruct_eof(t)) {
2018 const char *name;
2019 pa_datum key;
2020 #ifdef HAVE_DBUS
2021 struct dbus_entry *de;
2022 #endif
2023
2024 if (pa_tagstruct_gets(t, &name) < 0)
2025 goto fail;
2026
2027 #ifdef HAVE_DBUS
2028 if ((de = pa_hashmap_get(u->dbus_entries, name))) {
2029 send_entry_removed_signal(de);
2030 dbus_entry_free(pa_hashmap_remove(u->dbus_entries, name));
2031 }
2032 #endif
2033
2034 key.data = (char*) name;
2035 key.size = strlen(name);
2036
2037 pa_database_unset(u->database, &key);
2038 }
2039
2040 trigger_save(u);
2041
2042 break;
2043
2044 case SUBCOMMAND_SUBSCRIBE: {
2045
2046 pa_bool_t enabled;
2047
2048 if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
2049 !pa_tagstruct_eof(t))
2050 goto fail;
2051
2052 if (enabled)
2053 pa_idxset_put(u->subscribed, c, NULL);
2054 else
2055 pa_idxset_remove_by_data(u->subscribed, c, NULL);
2056
2057 break;
2058 }
2059
2060 default:
2061 goto fail;
2062 }
2063
2064 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
2065 return 0;
2066
2067 fail:
2068
2069 if (reply)
2070 pa_tagstruct_free(reply);
2071
2072 return -1;
2073 }
2074
2075 static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
2076 pa_assert(p);
2077 pa_assert(c);
2078 pa_assert(u);
2079
2080 pa_idxset_remove_by_data(u->subscribed, c, NULL);
2081 return PA_HOOK_OK;
2082 }
2083
2084 int pa__init(pa_module*m) {
2085 pa_modargs *ma = NULL;
2086 struct userdata *u;
2087 char *fname;
2088 pa_sink_input *si;
2089 pa_source_output *so;
2090 uint32_t idx;
2091 pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE, on_hotplug = TRUE, on_rescue = TRUE;
2092 #ifdef HAVE_DBUS
2093 pa_datum key;
2094 pa_bool_t done;
2095 #endif
2096
2097 pa_assert(m);
2098
2099 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
2100 pa_log("Failed to parse module arguments");
2101 goto fail;
2102 }
2103
2104 if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 ||
2105 pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
2106 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 ||
2107 pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
2108 pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
2109 pa_log("restore_device=, restore_volume=, restore_muted=, on_hotplug= and on_rescue= expect boolean arguments");
2110 goto fail;
2111 }
2112
2113 if (!restore_muted && !restore_volume && !restore_device)
2114 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
2115
2116 m->userdata = u = pa_xnew0(struct userdata, 1);
2117 u->core = m->core;
2118 u->module = m;
2119 u->restore_device = restore_device;
2120 u->restore_volume = restore_volume;
2121 u->restore_muted = restore_muted;
2122 u->on_hotplug = on_hotplug;
2123 u->on_rescue = on_rescue;
2124 u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
2125
2126 u->protocol = pa_native_protocol_get(m->core);
2127 pa_native_protocol_install_ext(u->protocol, m, extension_cb);
2128
2129 u->connection_unlink_hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_CONNECTION_UNLINK], PA_HOOK_NORMAL, (pa_hook_cb_t) connection_unlink_hook_cb, u);
2130
2131 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
2132
2133 if (restore_device) {
2134 /* A little bit earlier than module-intended-roles ... */
2135 u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_new_hook_callback, u);
2136 u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_output_new_hook_callback, u);
2137 }
2138
2139 if (restore_device && on_hotplug) {
2140 /* A little bit earlier than module-intended-roles ... */
2141 u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_callback, u);
2142 u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_put_hook_callback, u);
2143 }
2144
2145 if (restore_device && on_rescue) {
2146 /* A little bit earlier than module-intended-roles, module-rescue-streams, ... */
2147 u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_unlink_hook_callback, u);
2148 u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_unlink_hook_callback, u);
2149 }
2150
2151 if (restore_volume || restore_muted)
2152 u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
2153
2154 if (!(fname = pa_state_path("stream-volumes", TRUE)))
2155 goto fail;
2156
2157 if (!(u->database = pa_database_open(fname, TRUE))) {
2158 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
2159 pa_xfree(fname);
2160 goto fail;
2161 }
2162
2163 pa_log_info("Successfully opened database file '%s'.", fname);
2164 pa_xfree(fname);
2165
2166 #ifdef HAVE_DBUS
2167 u->dbus_protocol = pa_dbus_protocol_get(u->core);
2168 u->dbus_entries = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
2169
2170 pa_assert_se(pa_dbus_protocol_add_interface(u->dbus_protocol, OBJECT_PATH, &stream_restore_interface_info, u) >= 0);
2171 pa_assert_se(pa_dbus_protocol_register_extension(u->dbus_protocol, INTERFACE_STREAM_RESTORE) >= 0);
2172
2173 /* Create the initial dbus entries. */
2174 done = !pa_database_first(u->database, &key, NULL);
2175 while (!done) {
2176 pa_datum next_key;
2177 char *name;
2178 struct dbus_entry *de;
2179 struct entry *e;
2180
2181 done = !pa_database_next(u->database, &key, &next_key, NULL);
2182
2183 name = pa_xstrndup(key.data, key.size);
2184 pa_datum_free(&key);
2185
2186 /* Use entry_read() for checking that the entry is valid. */
2187 if ((e = entry_read(u, name))) {
2188 de = dbus_entry_new(u, name);
2189 pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
2190 entry_free(e);
2191 }
2192
2193 pa_xfree(name);
2194
2195 key = next_key;
2196 }
2197 #endif
2198
2199 PA_IDXSET_FOREACH(si, m->core->sink_inputs, idx)
2200 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, si->index, u);
2201
2202 PA_IDXSET_FOREACH(so, m->core->source_outputs, idx)
2203 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, so->index, u);
2204
2205 pa_modargs_free(ma);
2206 return 0;
2207
2208 fail:
2209 pa__done(m);
2210
2211 if (ma)
2212 pa_modargs_free(ma);
2213
2214 return -1;
2215 }
2216
2217 #ifdef HAVE_DBUS
2218 static void free_dbus_entry_cb(void *p, void *userdata) {
2219 struct dbus_entry *de = p;
2220
2221 pa_assert(de);
2222
2223 dbus_entry_free(de);
2224 }
2225 #endif
2226
2227 void pa__done(pa_module*m) {
2228 struct userdata* u;
2229
2230 pa_assert(m);
2231
2232 if (!(u = m->userdata))
2233 return;
2234
2235 #ifdef HAVE_DBUS
2236 if (u->dbus_protocol) {
2237 pa_assert(u->dbus_entries);
2238
2239 pa_assert_se(pa_dbus_protocol_unregister_extension(u->dbus_protocol, INTERFACE_STREAM_RESTORE) >= 0);
2240 pa_assert_se(pa_dbus_protocol_remove_interface(u->dbus_protocol, OBJECT_PATH, stream_restore_interface_info.name) >= 0);
2241
2242 pa_hashmap_free(u->dbus_entries, free_dbus_entry_cb, NULL);
2243
2244 pa_dbus_protocol_unref(u->dbus_protocol);
2245 }
2246 #endif
2247
2248 if (u->subscription)
2249 pa_subscription_free(u->subscription);
2250
2251 if (u->sink_input_new_hook_slot)
2252 pa_hook_slot_free(u->sink_input_new_hook_slot);
2253 if (u->sink_input_fixate_hook_slot)
2254 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
2255 if (u->source_output_new_hook_slot)
2256 pa_hook_slot_free(u->source_output_new_hook_slot);
2257
2258 if (u->sink_put_hook_slot)
2259 pa_hook_slot_free(u->sink_put_hook_slot);
2260 if (u->source_put_hook_slot)
2261 pa_hook_slot_free(u->source_put_hook_slot);
2262
2263 if (u->sink_unlink_hook_slot)
2264 pa_hook_slot_free(u->sink_unlink_hook_slot);
2265 if (u->source_unlink_hook_slot)
2266 pa_hook_slot_free(u->source_unlink_hook_slot);
2267
2268 if (u->connection_unlink_hook_slot)
2269 pa_hook_slot_free(u->connection_unlink_hook_slot);
2270
2271 if (u->save_time_event)
2272 u->core->mainloop->time_free(u->save_time_event);
2273
2274 if (u->database)
2275 pa_database_close(u->database);
2276
2277 if (u->protocol) {
2278 pa_native_protocol_remove_ext(u->protocol, m);
2279 pa_native_protocol_unref(u->protocol);
2280 }
2281
2282 if (u->subscribed)
2283 pa_idxset_free(u->subscribed, NULL, NULL);
2284
2285 pa_xfree(u);
2286 }