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