]> code.delx.au - pulseaudio/blob - src/modules/module-stream-restore.c
Merge commit 'coling/lgpl21'
[pulseaudio] / src / modules / module-stream-restore.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
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 <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <gdbm.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.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
53 #include "module-stream-restore-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute/device state of streams");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(TRUE);
59 PA_MODULE_USAGE(
60 "restore_device=<Save/restore sinks/sources?> "
61 "restore_volume=<Save/restore volumes?> "
62 "restore_muted=<Save/restore muted states?>");
63
64 #define SAVE_INTERVAL 10
65 #define IDENTIFICATION_PROPERTY "module-stream-restore.id"
66
67 static const char* const valid_modargs[] = {
68 "restore_device",
69 "restore_volume",
70 "restore_muted",
71 NULL
72 };
73
74 struct userdata {
75 pa_core *core;
76 pa_module *module;
77 pa_subscription *subscription;
78 pa_hook_slot
79 *sink_input_new_hook_slot,
80 *sink_input_fixate_hook_slot,
81 *source_output_new_hook_slot,
82 *connection_unlink_hook_slot;
83 pa_time_event *save_time_event;
84 GDBM_FILE gdbm_file;
85
86 pa_bool_t restore_device:1;
87 pa_bool_t restore_volume:1;
88 pa_bool_t restore_muted:1;
89
90 pa_native_protocol *protocol;
91 pa_idxset *subscribed;
92 };
93
94 #define ENTRY_VERSION 1
95
96 struct entry {
97 uint8_t version;
98 pa_bool_t muted_valid:1, relative_volume_valid:1, absolute_volume_valid:1, device_valid:1;
99 pa_bool_t muted:1;
100 pa_channel_map channel_map;
101 pa_cvolume relative_volume;
102 pa_cvolume absolute_volume;
103 char device[PA_NAME_MAX];
104 } PA_GCC_PACKED;
105
106 enum {
107 SUBCOMMAND_TEST,
108 SUBCOMMAND_READ,
109 SUBCOMMAND_WRITE,
110 SUBCOMMAND_DELETE,
111 SUBCOMMAND_SUBSCRIBE,
112 SUBCOMMAND_EVENT
113 };
114
115 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
116 struct userdata *u = userdata;
117
118 pa_assert(a);
119 pa_assert(e);
120 pa_assert(tv);
121 pa_assert(u);
122
123 pa_assert(e == u->save_time_event);
124 u->core->mainloop->time_free(u->save_time_event);
125 u->save_time_event = NULL;
126
127 gdbm_sync(u->gdbm_file);
128 pa_log_info("Synced.");
129 }
130
131 static char *get_name(pa_proplist *p, const char *prefix) {
132 const char *r;
133 char *t;
134
135 if (!p)
136 return NULL;
137
138 if ((r = pa_proplist_gets(p, IDENTIFICATION_PROPERTY)))
139 return pa_xstrdup(r);
140
141 if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_ROLE)))
142 t = pa_sprintf_malloc("%s-by-media-role:%s", prefix, r);
143 else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_ID)))
144 t = pa_sprintf_malloc("%s-by-application-id:%s", prefix, r);
145 else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME)))
146 t = pa_sprintf_malloc("%s-by-application-name:%s", prefix, r);
147 else if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_NAME)))
148 t = pa_sprintf_malloc("%s-by-media-name:%s", prefix, r);
149 else
150 t = pa_sprintf_malloc("%s-fallback:%s", prefix, r);
151
152 pa_proplist_sets(p, IDENTIFICATION_PROPERTY, t);
153 return t;
154 }
155
156 static struct entry* read_entry(struct userdata *u, const char *name) {
157 datum key, data;
158 struct entry *e;
159
160 pa_assert(u);
161 pa_assert(name);
162
163 key.dptr = (char*) name;
164 key.dsize = (int) strlen(name);
165
166 data = gdbm_fetch(u->gdbm_file, key);
167
168 if (!data.dptr)
169 goto fail;
170
171 if (data.dsize != sizeof(struct entry)) {
172 /* This is probably just a database upgrade, hence let's not
173 * consider this more than a debug message */
174 pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
175 goto fail;
176 }
177
178 e = (struct entry*) data.dptr;
179
180 if (e->version != ENTRY_VERSION) {
181 pa_log_debug("Version of database entry for stream %s doesn't match our version. Probably due to upgrade, ignoring.", name);
182 goto fail;
183 }
184
185 if (!memchr(e->device, 0, sizeof(e->device))) {
186 pa_log_warn("Database contains entry for stream %s with missing NUL byte in device name", name);
187 goto fail;
188 }
189
190 if (e->device_valid && !pa_namereg_is_valid_name(e->device)) {
191 pa_log_warn("Invalid device name stored in database for stream %s", name);
192 goto fail;
193 }
194
195 if ((e->relative_volume_valid || e->absolute_volume_valid) && !(pa_channel_map_valid(&e->channel_map))) {
196 pa_log_warn("Invalid channel map stored in database for stream %s", name);
197 goto fail;
198 }
199
200 if ((e->relative_volume_valid && (!pa_cvolume_valid(&e->relative_volume) || e->relative_volume.channels != e->channel_map.channels)) ||
201 (e->absolute_volume_valid && (!pa_cvolume_valid(&e->absolute_volume) || e->absolute_volume.channels != e->channel_map.channels))) {
202 pa_log_warn("Invalid volume stored in database for stream %s", name);
203 goto fail;
204 }
205
206 return e;
207
208 fail:
209
210 pa_xfree(data.dptr);
211 return NULL;
212 }
213
214 static void trigger_save(struct userdata *u) {
215 struct timeval tv;
216 pa_native_connection *c;
217 uint32_t idx;
218
219 for (c = pa_idxset_first(u->subscribed, &idx); c; c = pa_idxset_next(u->subscribed, &idx)) {
220 pa_tagstruct *t;
221
222 t = pa_tagstruct_new(NULL, 0);
223 pa_tagstruct_putu32(t, PA_COMMAND_EXTENSION);
224 pa_tagstruct_putu32(t, 0);
225 pa_tagstruct_putu32(t, u->module->index);
226 pa_tagstruct_puts(t, u->module->name);
227 pa_tagstruct_putu32(t, SUBCOMMAND_EVENT);
228
229 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), t);
230 }
231
232 if (u->save_time_event)
233 return;
234
235 pa_gettimeofday(&tv);
236 tv.tv_sec += SAVE_INTERVAL;
237 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
238 }
239
240 static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
241 pa_cvolume t;
242
243 pa_assert(a);
244 pa_assert(b);
245
246 if (a->device_valid != b->device_valid ||
247 (a->device_valid && strncmp(a->device, b->device, sizeof(a->device))))
248 return FALSE;
249
250 if (a->muted_valid != b->muted_valid ||
251 (a->muted_valid && (a->muted != b->muted)))
252 return FALSE;
253
254 t = b->relative_volume;
255 if (a->relative_volume_valid != b->relative_volume_valid ||
256 (a->relative_volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->relative_volume)))
257 return FALSE;
258
259 t = b->absolute_volume;
260 if (a->absolute_volume_valid != b->absolute_volume_valid ||
261 (a->absolute_volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->absolute_volume)))
262 return FALSE;
263
264 return TRUE;
265 }
266
267 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
268 struct userdata *u = userdata;
269 struct entry entry, *old;
270 char *name;
271 datum key, data;
272
273 pa_assert(c);
274 pa_assert(u);
275
276 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
277 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
278 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
279 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
280 return;
281
282 memset(&entry, 0, sizeof(entry));
283 entry.version = ENTRY_VERSION;
284
285 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
286 pa_sink_input *sink_input;
287
288 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx)))
289 return;
290
291 if (!(name = get_name(sink_input->proplist, "sink-input")))
292 return;
293
294 entry.channel_map = sink_input->channel_map;
295
296 if (sink_input->sink->flags & PA_SINK_FLAT_VOLUME) {
297 entry.absolute_volume = *pa_sink_input_get_volume(sink_input);
298 entry.absolute_volume_valid = sink_input->save_volume;
299
300 pa_sw_cvolume_divide(&entry.relative_volume, &entry.absolute_volume, pa_sink_get_volume(sink_input->sink, FALSE));
301 entry.relative_volume_valid = sink_input->save_volume;
302 } else {
303 entry.relative_volume = *pa_sink_input_get_volume(sink_input);
304 entry.relative_volume_valid = sink_input->save_volume;
305 }
306
307 entry.muted = pa_sink_input_get_mute(sink_input);
308 entry.muted_valid = sink_input->save_muted;
309
310 pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device));
311 entry.device_valid = sink_input->save_sink;
312
313 } else {
314 pa_source_output *source_output;
315
316 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT);
317
318 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx)))
319 return;
320
321 if (!(name = get_name(source_output->proplist, "source-output")))
322 return;
323
324 entry.channel_map = source_output->channel_map;
325
326 pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device));
327 entry.device_valid = source_output->save_source;
328 }
329
330 if ((old = read_entry(u, name))) {
331
332 if (entries_equal(old, &entry)) {
333 pa_xfree(old);
334 pa_xfree(name);
335 return;
336 }
337
338 pa_xfree(old);
339 }
340
341 key.dptr = name;
342 key.dsize = (int) strlen(name);
343
344 data.dptr = (void*) &entry;
345 data.dsize = sizeof(entry);
346
347 pa_log_info("Storing volume/mute/device for stream %s.", name);
348
349 gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
350
351 pa_xfree(name);
352
353 trigger_save(u);
354 }
355
356 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
357 char *name;
358 struct entry *e;
359
360 pa_assert(new_data);
361
362 if (!u->restore_device)
363 return PA_HOOK_OK;
364
365 if (!(name = get_name(new_data->proplist, "sink-input")))
366 return PA_HOOK_OK;
367
368 if ((e = read_entry(u, name))) {
369 pa_sink *s;
370
371 if (e->device_valid) {
372
373 if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK))) {
374 if (!new_data->sink) {
375 pa_log_info("Restoring device for stream %s.", name);
376 new_data->sink = s;
377 new_data->save_sink = TRUE;
378 } else
379 pa_log_info("Not restore device for stream %s, because already set.", name);
380 }
381 }
382
383 pa_xfree(e);
384 }
385
386 pa_xfree(name);
387
388 return PA_HOOK_OK;
389 }
390
391 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
392 char *name;
393 struct entry *e;
394
395 pa_assert(new_data);
396
397 if (!u->restore_volume && !u->restore_muted)
398 return PA_HOOK_OK;
399
400 if (!(name = get_name(new_data->proplist, "sink-input")))
401 return PA_HOOK_OK;
402
403 if ((e = read_entry(u, name))) {
404
405 if (u->restore_volume) {
406
407 if (!new_data->volume_is_set) {
408 pa_cvolume v;
409 pa_cvolume_init(&v);
410
411 if (new_data->sink->flags & PA_SINK_FLAT_VOLUME) {
412
413 /* We don't check for e->device_valid here because
414 that bit marks whether it is a good choice for
415 restoring, not just if the data is filled in. */
416 if (e->absolute_volume_valid &&
417 (e->device[0] == 0 || pa_streq(new_data->sink->name, e->device))) {
418
419 v = e->absolute_volume;
420 new_data->volume_is_absolute = TRUE;
421 } else if (e->relative_volume_valid) {
422 v = e->relative_volume;
423 new_data->volume_is_absolute = FALSE;
424 }
425
426 } else if (e->relative_volume_valid) {
427 v = e->relative_volume;
428 new_data->volume_is_absolute = FALSE;
429 }
430
431 if (v.channels > 0) {
432 pa_log_info("Restoring volume for sink input %s.", name);
433 pa_sink_input_new_data_set_volume(new_data, pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map));
434 new_data->save_volume = TRUE;
435 }
436 } else
437 pa_log_debug("Not restoring volume for sink input %s, because already set.", name);
438 }
439
440 if (u->restore_muted && e->muted_valid) {
441 if (!new_data->muted_is_set) {
442 pa_log_info("Restoring mute state for sink input %s.", name);
443 pa_sink_input_new_data_set_muted(new_data, e->muted);
444 new_data->save_muted = TRUE;
445 } else
446 pa_log_debug("Not restoring mute state for sink input %s, because already set.", name);
447 }
448
449 pa_xfree(e);
450 }
451
452 pa_xfree(name);
453
454 return PA_HOOK_OK;
455 }
456
457 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
458 char *name;
459 struct entry *e;
460
461 pa_assert(new_data);
462
463 if (!u->restore_device)
464 return PA_HOOK_OK;
465
466 if (new_data->direct_on_input)
467 return PA_HOOK_OK;
468
469 if (!(name = get_name(new_data->proplist, "source-output")))
470 return PA_HOOK_OK;
471
472 if ((e = read_entry(u, name))) {
473 pa_source *s;
474
475 if (e->device_valid) {
476 if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE))) {
477 if (!new_data->source) {
478 pa_log_info("Restoring device for stream %s.", name);
479 new_data->source = s;
480 new_data->save_source = TRUE;
481 } else
482 pa_log_info("Not restoring device for stream %s, because already set", name);
483 }
484 }
485
486 pa_xfree(e);
487 }
488
489 pa_xfree(name);
490
491 return PA_HOOK_OK;
492 }
493
494 #define EXT_VERSION 1
495
496 static void clear_db(struct userdata *u) {
497 datum key;
498
499 pa_assert(u);
500
501 key = gdbm_firstkey(u->gdbm_file);
502 while (key.dptr) {
503 datum next_key;
504 next_key = gdbm_nextkey(u->gdbm_file, key);
505
506 gdbm_delete(u->gdbm_file, key);
507 pa_xfree(key.dptr);
508
509 key = next_key;
510 }
511
512 gdbm_reorganize(u->gdbm_file);
513 }
514
515 static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
516 pa_sink_input *si;
517 pa_source_output *so;
518 uint32_t idx;
519
520 pa_assert(u);
521 pa_assert(name);
522 pa_assert(e);
523
524 for (si = pa_idxset_first(u->core->sink_inputs, &idx); si; si = pa_idxset_next(u->core->sink_inputs, &idx)) {
525 char *n;
526 pa_sink *s;
527
528 if (!(n = get_name(si->proplist, "sink-input")))
529 continue;
530
531 if (strcmp(name, n)) {
532 pa_xfree(n);
533 continue;
534 }
535 pa_xfree(n);
536
537 if (u->restore_volume) {
538 pa_cvolume v;
539 pa_cvolume_init(&v);
540
541 if (si->sink->flags & PA_SINK_FLAT_VOLUME) {
542
543 if (e->absolute_volume_valid &&
544 (e->device[0] == 0 || pa_streq(e->device, si->sink->name)))
545 v = e->absolute_volume;
546 else if (e->relative_volume_valid) {
547 pa_cvolume t = *pa_sink_get_volume(si->sink, FALSE);
548 pa_sw_cvolume_multiply(&v, &e->relative_volume, pa_cvolume_remap(&t, &si->sink->channel_map, &e->channel_map));
549 }
550 } else if (e->relative_volume_valid)
551 v = e->relative_volume;
552
553 if (v.channels > 0) {
554 pa_log_info("Restoring volume for sink input %s.", name);
555 pa_sink_input_set_volume(si, pa_cvolume_remap(&v, &e->channel_map, &si->channel_map), TRUE);
556 }
557 }
558
559 if (u->restore_muted &&
560 e->muted_valid) {
561 pa_log_info("Restoring mute state for sink input %s.", name);
562 pa_sink_input_set_mute(si, e->muted, TRUE);
563 }
564
565 if (u->restore_device &&
566 e->device_valid &&
567 (s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SINK))) {
568
569 pa_log_info("Restoring device for stream %s.", name);
570 pa_sink_input_move_to(si, s, TRUE);
571 }
572 }
573
574 for (so = pa_idxset_first(u->core->source_outputs, &idx); so; so = pa_idxset_next(u->core->source_outputs, &idx)) {
575 char *n;
576 pa_source *s;
577
578 if (!(n = get_name(so->proplist, "source-output")))
579 continue;
580
581 if (strcmp(name, n)) {
582 pa_xfree(n);
583 continue;
584 }
585 pa_xfree(n);
586
587 if (u->restore_device &&
588 e->device_valid &&
589 (s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SOURCE))) {
590
591 pa_log_info("Restoring device for stream %s.", name);
592 pa_source_output_move_to(so, s, TRUE);
593 }
594 }
595 }
596
597 #if 0
598 static void dump_database(struct userdata *u) {
599 datum key;
600
601 key = gdbm_firstkey(u->gdbm_file);
602 while (key.dptr) {
603 datum next_key;
604 struct entry *e;
605 char *name;
606
607 next_key = gdbm_nextkey(u->gdbm_file, key);
608
609 name = pa_xstrndup(key.dptr, key.dsize);
610 pa_xfree(key.dptr);
611
612 if ((e = read_entry(u, name))) {
613 char t[256];
614 pa_log("name=%s", name);
615 pa_log("device=%s", e->device);
616 pa_log("channel_map=%s", pa_channel_map_snprint(t, sizeof(t), &e->channel_map));
617 pa_log("volume=%s", pa_cvolume_snprint(t, sizeof(t), &e->volume));
618 pa_log("mute=%s", pa_yes_no(e->muted));
619 pa_xfree(e);
620 }
621
622 pa_xfree(name);
623
624 key = next_key;
625 }
626 }
627 #endif
628
629 static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
630 struct userdata *u;
631 uint32_t command;
632 pa_tagstruct *reply = NULL;
633
634 pa_assert(p);
635 pa_assert(m);
636 pa_assert(c);
637 pa_assert(t);
638
639 u = m->userdata;
640
641 if (pa_tagstruct_getu32(t, &command) < 0)
642 goto fail;
643
644 reply = pa_tagstruct_new(NULL, 0);
645 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
646 pa_tagstruct_putu32(reply, tag);
647
648 switch (command) {
649 case SUBCOMMAND_TEST: {
650 if (!pa_tagstruct_eof(t))
651 goto fail;
652
653 pa_tagstruct_putu32(reply, EXT_VERSION);
654 break;
655 }
656
657 case SUBCOMMAND_READ: {
658 datum key;
659
660 if (!pa_tagstruct_eof(t))
661 goto fail;
662
663 key = gdbm_firstkey(u->gdbm_file);
664 while (key.dptr) {
665 datum next_key;
666 struct entry *e;
667 char *name;
668
669 next_key = gdbm_nextkey(u->gdbm_file, key);
670
671 name = pa_xstrndup(key.dptr, (size_t) key.dsize);
672 pa_xfree(key.dptr);
673
674 if ((e = read_entry(u, name))) {
675 pa_cvolume r;
676 pa_channel_map cm;
677
678 pa_tagstruct_puts(reply, name);
679 pa_tagstruct_put_channel_map(reply, (e->relative_volume_valid || e->absolute_volume_valid) ? &e->channel_map : pa_channel_map_init(&cm));
680 pa_tagstruct_put_cvolume(reply, e->absolute_volume_valid ? &e->absolute_volume : (e->relative_volume_valid ? &e->relative_volume : pa_cvolume_init(&r)));
681 pa_tagstruct_puts(reply, e->device_valid ? e->device : NULL);
682 pa_tagstruct_put_boolean(reply, e->muted_valid ? e->muted : FALSE);
683
684 pa_xfree(e);
685 }
686
687 pa_xfree(name);
688
689 key = next_key;
690 }
691
692 break;
693 }
694
695 case SUBCOMMAND_WRITE: {
696 uint32_t mode;
697 pa_bool_t apply_immediately = FALSE;
698
699 if (pa_tagstruct_getu32(t, &mode) < 0 ||
700 pa_tagstruct_get_boolean(t, &apply_immediately) < 0)
701 goto fail;
702
703 if (mode != PA_UPDATE_MERGE &&
704 mode != PA_UPDATE_REPLACE &&
705 mode != PA_UPDATE_SET)
706 goto fail;
707
708 if (mode == PA_UPDATE_SET)
709 clear_db(u);
710
711 while (!pa_tagstruct_eof(t)) {
712 const char *name, *device;
713 pa_bool_t muted;
714 struct entry entry;
715 datum key, data;
716 int k;
717
718 memset(&entry, 0, sizeof(entry));
719 entry.version = ENTRY_VERSION;
720
721 if (pa_tagstruct_gets(t, &name) < 0 ||
722 pa_tagstruct_get_channel_map(t, &entry.channel_map) ||
723 pa_tagstruct_get_cvolume(t, &entry.absolute_volume) < 0 ||
724 pa_tagstruct_gets(t, &device) < 0 ||
725 pa_tagstruct_get_boolean(t, &muted) < 0)
726 goto fail;
727
728 if (!name || !*name)
729 goto fail;
730
731 entry.relative_volume = entry.absolute_volume;
732 entry.absolute_volume_valid = entry.relative_volume_valid = entry.relative_volume.channels > 0;
733
734 if (entry.relative_volume_valid)
735 if (!pa_cvolume_compatible_with_channel_map(&entry.relative_volume, &entry.channel_map))
736 goto fail;
737
738 entry.muted = muted;
739 entry.muted_valid = TRUE;
740
741 if (device)
742 pa_strlcpy(entry.device, device, sizeof(entry.device));
743 entry.device_valid = !!entry.device[0];
744
745 if (entry.device_valid &&
746 !pa_namereg_is_valid_name(entry.device))
747 goto fail;
748
749 key.dptr = (void*) name;
750 key.dsize = (int) strlen(name);
751
752 data.dptr = (void*) &entry;
753 data.dsize = sizeof(entry);
754
755 if ((k = gdbm_store(u->gdbm_file, key, data, mode == PA_UPDATE_REPLACE ? GDBM_REPLACE : GDBM_INSERT)) == 0)
756 if (apply_immediately)
757 apply_entry(u, name, &entry);
758 }
759
760 trigger_save(u);
761
762 break;
763 }
764
765 case SUBCOMMAND_DELETE:
766
767 while (!pa_tagstruct_eof(t)) {
768 const char *name;
769 datum key;
770
771 if (pa_tagstruct_gets(t, &name) < 0)
772 goto fail;
773
774 key.dptr = (void*) name;
775 key.dsize = (int) strlen(name);
776
777 gdbm_delete(u->gdbm_file, key);
778 }
779
780 trigger_save(u);
781
782 break;
783
784 case SUBCOMMAND_SUBSCRIBE: {
785
786 pa_bool_t enabled;
787
788 if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
789 !pa_tagstruct_eof(t))
790 goto fail;
791
792 if (enabled)
793 pa_idxset_put(u->subscribed, c, NULL);
794 else
795 pa_idxset_remove_by_data(u->subscribed, c, NULL);
796
797 break;
798 }
799
800 default:
801 goto fail;
802 }
803
804 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
805 return 0;
806
807 fail:
808
809 if (reply)
810 pa_tagstruct_free(reply);
811
812 return -1;
813 }
814
815 static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
816 pa_assert(p);
817 pa_assert(c);
818 pa_assert(u);
819
820 pa_idxset_remove_by_data(u->subscribed, c, NULL);
821 return PA_HOOK_OK;
822 }
823
824 int pa__init(pa_module*m) {
825 pa_modargs *ma = NULL;
826 struct userdata *u;
827 char *fname, *fn;
828 pa_sink_input *si;
829 pa_source_output *so;
830 uint32_t idx;
831 pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE;
832 int gdbm_cache_size;
833
834 pa_assert(m);
835
836 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
837 pa_log("Failed to parse module arguments");
838 goto fail;
839 }
840
841 if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 ||
842 pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
843 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
844 pa_log("restore_device=, restore_volume= and restore_muted= expect boolean arguments");
845 goto fail;
846 }
847
848 if (!restore_muted && !restore_volume && !restore_device)
849 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
850
851 m->userdata = u = pa_xnew(struct userdata, 1);
852 u->core = m->core;
853 u->module = m;
854 u->save_time_event = NULL;
855 u->restore_device = restore_device;
856 u->restore_volume = restore_volume;
857 u->restore_muted = restore_muted;
858 u->gdbm_file = NULL;
859 u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
860
861 u->protocol = pa_native_protocol_get(m->core);
862 pa_native_protocol_install_ext(u->protocol, m, extension_cb);
863
864 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);
865
866 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
867
868 if (restore_device) {
869 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);
870 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);
871 }
872
873 if (restore_volume || restore_muted)
874 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);
875
876 /* We include the host identifier in the file name because gdbm
877 * files are CPU dependant, and we don't want things to go wrong
878 * if we are on a multiarch system. */
879
880 fn = pa_sprintf_malloc("stream-volumes."CANONICAL_HOST".gdbm");
881 fname = pa_state_path(fn, TRUE);
882 pa_xfree(fn);
883
884 if (!fname)
885 goto fail;
886
887 if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) {
888 pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
889 pa_xfree(fname);
890 goto fail;
891 }
892
893 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
894 gdbm_cache_size = 10;
895 gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
896
897 pa_log_info("Sucessfully opened database file '%s'.", fname);
898 pa_xfree(fname);
899
900 for (si = pa_idxset_first(m->core->sink_inputs, &idx); si; si = pa_idxset_next(m->core->sink_inputs, &idx))
901 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, si->index, u);
902
903 for (so = pa_idxset_first(m->core->source_outputs, &idx); so; so = pa_idxset_next(m->core->source_outputs, &idx))
904 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, so->index, u);
905
906 pa_modargs_free(ma);
907 return 0;
908
909 fail:
910 pa__done(m);
911
912 if (ma)
913 pa_modargs_free(ma);
914
915 return -1;
916 }
917
918 void pa__done(pa_module*m) {
919 struct userdata* u;
920
921 pa_assert(m);
922
923 if (!(u = m->userdata))
924 return;
925
926 if (u->subscription)
927 pa_subscription_free(u->subscription);
928
929 if (u->sink_input_new_hook_slot)
930 pa_hook_slot_free(u->sink_input_new_hook_slot);
931 if (u->sink_input_fixate_hook_slot)
932 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
933 if (u->source_output_new_hook_slot)
934 pa_hook_slot_free(u->source_output_new_hook_slot);
935
936 if (u->connection_unlink_hook_slot)
937 pa_hook_slot_free(u->connection_unlink_hook_slot);
938
939 if (u->save_time_event)
940 u->core->mainloop->time_free(u->save_time_event);
941
942 if (u->gdbm_file)
943 gdbm_close(u->gdbm_file);
944
945 if (u->protocol) {
946 pa_native_protocol_remove_ext(u->protocol, m);
947 pa_native_protocol_unref(u->protocol);
948 }
949
950 if (u->subscribed)
951 pa_idxset_free(u->subscribed, NULL, NULL);
952
953 pa_xfree(u);
954 }