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