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