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