]> code.delx.au - pulseaudio/blob - src/modules/module-stream-restore.c
rtclock: fix issues found by Lennart
[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
34 #include <pulse/xmalloc.h>
35 #include <pulse/volume.h>
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
38 #include <pulse/rtclock.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 #include <pulsecore/database.h>
53
54 #include "module-stream-restore-symdef.h"
55
56 PA_MODULE_AUTHOR("Lennart Poettering");
57 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute/device state of streams");
58 PA_MODULE_VERSION(PACKAGE_VERSION);
59 PA_MODULE_LOAD_ONCE(TRUE);
60 PA_MODULE_USAGE(
61 "restore_device=<Save/restore sinks/sources?> "
62 "restore_volume=<Save/restore volumes?> "
63 "restore_muted=<Save/restore muted states?>");
64
65 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
66 #define IDENTIFICATION_PROPERTY "module-stream-restore.id"
67
68 static const char* const valid_modargs[] = {
69 "restore_device",
70 "restore_volume",
71 "restore_muted",
72 NULL
73 };
74
75 struct userdata {
76 pa_core *core;
77 pa_module *module;
78 pa_subscription *subscription;
79 pa_hook_slot
80 *sink_input_new_hook_slot,
81 *sink_input_fixate_hook_slot,
82 *source_output_new_hook_slot,
83 *connection_unlink_hook_slot;
84 pa_time_event *save_time_event;
85 pa_database* database;
86
87 pa_bool_t restore_device:1;
88 pa_bool_t restore_volume:1;
89 pa_bool_t restore_muted:1;
90
91 pa_native_protocol *protocol;
92 pa_idxset *subscribed;
93 };
94
95 #define ENTRY_VERSION 2
96
97 struct entry {
98 uint8_t version;
99 pa_bool_t muted_valid:1, volume_valid:1, device_valid:1;
100 pa_bool_t muted:1;
101 pa_channel_map channel_map;
102 pa_cvolume volume;
103 char device[PA_NAME_MAX];
104 } PA_GCC_PACKED;
105
106 enum {
107 SUBCOMMAND_TEST,
108 SUBCOMMAND_READ,
109 SUBCOMMAND_WRITE,
110 SUBCOMMAND_DELETE,
111 SUBCOMMAND_SUBSCRIBE,
112 SUBCOMMAND_EVENT
113 };
114
115 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
116 struct userdata *u = userdata;
117
118 pa_assert(a);
119 pa_assert(e);
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 pa_database_sync(u->database);
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 pa_datum key, data;
157 struct entry *e;
158
159 pa_assert(u);
160 pa_assert(name);
161
162 key.data = (char*) name;
163 key.size = strlen(name);
164
165 pa_zero(data);
166
167 if (!pa_database_get(u->database, &key, &data))
168 goto fail;
169
170 if (data.size != 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.size, (unsigned long) sizeof(struct entry));
174 goto fail;
175 }
176
177 e = (struct entry*) data.data;
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_datum_free(&data);
209 return NULL;
210 }
211
212 static void trigger_save(struct userdata *u) {
213 pa_native_connection *c;
214 uint32_t idx;
215
216 for (c = pa_idxset_first(u->subscribed, &idx); c; c = pa_idxset_next(u->subscribed, &idx)) {
217 pa_tagstruct *t;
218
219 t = pa_tagstruct_new(NULL, 0);
220 pa_tagstruct_putu32(t, PA_COMMAND_EXTENSION);
221 pa_tagstruct_putu32(t, 0);
222 pa_tagstruct_putu32(t, u->module->index);
223 pa_tagstruct_puts(t, u->module->name);
224 pa_tagstruct_putu32(t, SUBCOMMAND_EVENT);
225
226 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), t);
227 }
228
229 if (u->save_time_event)
230 return;
231
232 u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
233 }
234
235 static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
236 pa_cvolume t;
237
238 pa_assert(a);
239 pa_assert(b);
240
241 if (a->device_valid != b->device_valid ||
242 (a->device_valid && strncmp(a->device, b->device, sizeof(a->device))))
243 return FALSE;
244
245 if (a->muted_valid != b->muted_valid ||
246 (a->muted_valid && (a->muted != b->muted)))
247 return FALSE;
248
249 t = b->volume;
250 if (a->volume_valid != b->volume_valid ||
251 (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume)))
252 return FALSE;
253
254 return TRUE;
255 }
256
257 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
258 struct userdata *u = userdata;
259 struct entry entry, *old;
260 char *name;
261 pa_datum key, data;
262
263 pa_assert(c);
264 pa_assert(u);
265
266 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
267 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
268 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
269 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
270 return;
271
272 pa_zero(entry);
273 entry.version = ENTRY_VERSION;
274
275 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
276 pa_sink_input *sink_input;
277
278 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx)))
279 return;
280
281 if (!(name = get_name(sink_input->proplist, "sink-input")))
282 return;
283
284 if ((old = read_entry(u, name)))
285 entry = *old;
286
287 if (sink_input->save_volume) {
288 entry.channel_map = sink_input->channel_map;
289 pa_sink_input_get_volume(sink_input, &entry.volume, FALSE);
290 entry.volume_valid = TRUE;
291 }
292
293 if (sink_input->save_muted) {
294 entry.muted = pa_sink_input_get_mute(sink_input);
295 entry.muted_valid = TRUE;
296 }
297
298 if (sink_input->save_sink) {
299 pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device));
300 entry.device_valid = TRUE;
301 }
302
303 } else {
304 pa_source_output *source_output;
305
306 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT);
307
308 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx)))
309 return;
310
311 if (!(name = get_name(source_output->proplist, "source-output")))
312 return;
313
314 if ((old = read_entry(u, name)))
315 entry = *old;
316
317 if (source_output->save_source) {
318 pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device));
319 entry.device_valid = source_output->save_source;
320 }
321 }
322
323 if (old) {
324
325 if (entries_equal(old, &entry)) {
326 pa_xfree(old);
327 pa_xfree(name);
328 return;
329 }
330
331 pa_xfree(old);
332 }
333
334 key.data = name;
335 key.size = strlen(name);
336
337 data.data = &entry;
338 data.size = sizeof(entry);
339
340 pa_log_info("Storing volume/mute/device for stream %s.", name);
341
342 pa_database_set(u->database, &key, &data, TRUE);
343
344 pa_xfree(name);
345
346 trigger_save(u);
347 }
348
349 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
350 char *name;
351 struct entry *e;
352
353 pa_assert(new_data);
354
355 if (!u->restore_device)
356 return PA_HOOK_OK;
357
358 if (!(name = get_name(new_data->proplist, "sink-input")))
359 return PA_HOOK_OK;
360
361 if ((e = read_entry(u, name))) {
362
363 if (e->device_valid) {
364 pa_sink *s;
365
366 if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK))) {
367 if (!new_data->sink) {
368 pa_log_info("Restoring device for stream %s.", name);
369 new_data->sink = s;
370 new_data->save_sink = FALSE;
371 } else
372 pa_log_info("Not restoring device for stream %s, because already set.", name);
373 }
374 }
375
376 pa_xfree(e);
377 }
378
379 pa_xfree(name);
380
381 return PA_HOOK_OK;
382 }
383
384 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
385 char *name;
386 struct entry *e;
387
388 pa_assert(new_data);
389
390 if (!u->restore_volume && !u->restore_muted)
391 return PA_HOOK_OK;
392
393 if (!(name = get_name(new_data->proplist, "sink-input")))
394 return PA_HOOK_OK;
395
396 if ((e = read_entry(u, name))) {
397
398 if (u->restore_volume && e->volume_valid) {
399
400 if (!new_data->volume_is_set) {
401 pa_cvolume v;
402
403 pa_log_info("Restoring volume for sink input %s.", name);
404
405 v = e->volume;
406 pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
407 pa_sink_input_new_data_set_volume(new_data, &v);
408
409 new_data->volume_is_absolute = FALSE;
410 new_data->save_volume = FALSE;
411 } else
412 pa_log_debug("Not restoring volume for sink input %s, because already set.", name);
413 }
414
415 if (u->restore_muted && e->muted_valid) {
416
417 if (!new_data->muted_is_set) {
418 pa_log_info("Restoring mute state for sink input %s.", name);
419 pa_sink_input_new_data_set_muted(new_data, e->muted);
420 new_data->save_muted = FALSE;
421 } else
422 pa_log_debug("Not restoring mute state for sink input %s, because already set.", name);
423 }
424
425 pa_xfree(e);
426 }
427
428 pa_xfree(name);
429
430 return PA_HOOK_OK;
431 }
432
433 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
434 char *name;
435 struct entry *e;
436
437 pa_assert(new_data);
438
439 if (!u->restore_device)
440 return PA_HOOK_OK;
441
442 if (new_data->direct_on_input)
443 return PA_HOOK_OK;
444
445 if (!(name = get_name(new_data->proplist, "source-output")))
446 return PA_HOOK_OK;
447
448 if ((e = read_entry(u, name))) {
449 pa_source *s;
450
451 if (e->device_valid) {
452 if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE))) {
453 if (!new_data->source) {
454 pa_log_info("Restoring device for stream %s.", name);
455 new_data->source = s;
456 new_data->save_source = FALSE;
457 } else
458 pa_log_info("Not restoring device for stream %s, because already set", name);
459 }
460 }
461
462 pa_xfree(e);
463 }
464
465 pa_xfree(name);
466
467 return PA_HOOK_OK;
468 }
469
470 #define EXT_VERSION 1
471
472 static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
473 pa_sink_input *si;
474 pa_source_output *so;
475 uint32_t idx;
476
477 pa_assert(u);
478 pa_assert(name);
479 pa_assert(e);
480
481 for (si = pa_idxset_first(u->core->sink_inputs, &idx); si; si = pa_idxset_next(u->core->sink_inputs, &idx)) {
482 char *n;
483 pa_sink *s;
484
485 if (!(n = get_name(si->proplist, "sink-input")))
486 continue;
487
488 if (!pa_streq(name, n)) {
489 pa_xfree(n);
490 continue;
491 }
492 pa_xfree(n);
493
494 if (u->restore_volume && e->volume_valid) {
495 pa_cvolume v;
496
497 v = e->volume;
498 pa_log_info("Restoring volume for sink input %s.", name);
499 pa_sink_input_set_volume(si, pa_cvolume_remap(&v, &e->channel_map, &si->channel_map), FALSE, FALSE);
500 }
501
502 if (u->restore_muted && e->muted_valid) {
503 pa_log_info("Restoring mute state for sink input %s.", name);
504 pa_sink_input_set_mute(si, e->muted, FALSE);
505 }
506
507 if (u->restore_device &&
508 e->device_valid &&
509 (s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SINK))) {
510
511 pa_log_info("Restoring device for stream %s.", name);
512 pa_sink_input_move_to(si, s, FALSE);
513 }
514 }
515
516 for (so = pa_idxset_first(u->core->source_outputs, &idx); so; so = pa_idxset_next(u->core->source_outputs, &idx)) {
517 char *n;
518 pa_source *s;
519
520 if (!(n = get_name(so->proplist, "source-output")))
521 continue;
522
523 if (!pa_streq(name, n)) {
524 pa_xfree(n);
525 continue;
526 }
527 pa_xfree(n);
528
529 if (u->restore_device &&
530 e->device_valid &&
531 (s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SOURCE))) {
532
533 pa_log_info("Restoring device for stream %s.", name);
534 pa_source_output_move_to(so, s, FALSE);
535 }
536 }
537 }
538
539 #if 0
540 static void dump_database(struct userdata *u) {
541 pa_datum key;
542 pa_bool_t done;
543
544 done = !pa_database_first(u->database, &key, NULL);
545
546 while (!done) {
547 pa_datum next_key;
548 struct entry *e;
549 char *name;
550
551 done = !pa_database_next(u->database, &key, &next_key, NULL);
552
553 name = pa_xstrndup(key.data, key.size);
554 pa_datum_free(&key);
555
556 if ((e = read_entry(u, name))) {
557 char t[256];
558 pa_log("name=%s", name);
559 pa_log("device=%s %s", e->device, pa_yes_no(e->device_valid));
560 pa_log("channel_map=%s", pa_channel_map_snprint(t, sizeof(t), &e->channel_map));
561 pa_log("volume=%s %s", pa_cvolume_snprint(t, sizeof(t), &e->volume), pa_yes_no(e->volume_valid));
562 pa_log("mute=%s %s", pa_yes_no(e->muted), pa_yes_no(e->volume_valid));
563 pa_xfree(e);
564 }
565
566 pa_xfree(name);
567
568 key = next_key;
569 }
570 }
571 #endif
572
573 static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
574 struct userdata *u;
575 uint32_t command;
576 pa_tagstruct *reply = NULL;
577
578 pa_assert(p);
579 pa_assert(m);
580 pa_assert(c);
581 pa_assert(t);
582
583 u = m->userdata;
584
585 if (pa_tagstruct_getu32(t, &command) < 0)
586 goto fail;
587
588 reply = pa_tagstruct_new(NULL, 0);
589 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
590 pa_tagstruct_putu32(reply, tag);
591
592 switch (command) {
593 case SUBCOMMAND_TEST: {
594 if (!pa_tagstruct_eof(t))
595 goto fail;
596
597 pa_tagstruct_putu32(reply, EXT_VERSION);
598 break;
599 }
600
601 case SUBCOMMAND_READ: {
602 pa_datum key;
603 pa_bool_t done;
604
605 if (!pa_tagstruct_eof(t))
606 goto fail;
607
608 done = !pa_database_first(u->database, &key, NULL);
609
610 while (!done) {
611 pa_datum next_key;
612 struct entry *e;
613 char *name;
614
615 done = !pa_database_next(u->database, &key, &next_key, NULL);
616
617 name = pa_xstrndup(key.data, key.size);
618 pa_datum_free(&key);
619
620 if ((e = read_entry(u, name))) {
621 pa_cvolume r;
622 pa_channel_map cm;
623
624 pa_tagstruct_puts(reply, name);
625 pa_tagstruct_put_channel_map(reply, e->volume_valid ? &e->channel_map : pa_channel_map_init(&cm));
626 pa_tagstruct_put_cvolume(reply, e->volume_valid ? &e->volume : pa_cvolume_init(&r));
627 pa_tagstruct_puts(reply, e->device_valid ? e->device : NULL);
628 pa_tagstruct_put_boolean(reply, e->muted_valid ? e->muted : FALSE);
629
630 pa_xfree(e);
631 }
632
633 pa_xfree(name);
634
635 key = next_key;
636 }
637
638 break;
639 }
640
641 case SUBCOMMAND_WRITE: {
642 uint32_t mode;
643 pa_bool_t apply_immediately = FALSE;
644
645 if (pa_tagstruct_getu32(t, &mode) < 0 ||
646 pa_tagstruct_get_boolean(t, &apply_immediately) < 0)
647 goto fail;
648
649 if (mode != PA_UPDATE_MERGE &&
650 mode != PA_UPDATE_REPLACE &&
651 mode != PA_UPDATE_SET)
652 goto fail;
653
654 if (mode == PA_UPDATE_SET)
655 pa_database_clear(u->database);
656
657 while (!pa_tagstruct_eof(t)) {
658 const char *name, *device;
659 pa_bool_t muted;
660 struct entry entry;
661 pa_datum key, data;
662
663 pa_zero(entry);
664 entry.version = ENTRY_VERSION;
665
666 if (pa_tagstruct_gets(t, &name) < 0 ||
667 pa_tagstruct_get_channel_map(t, &entry.channel_map) ||
668 pa_tagstruct_get_cvolume(t, &entry.volume) < 0 ||
669 pa_tagstruct_gets(t, &device) < 0 ||
670 pa_tagstruct_get_boolean(t, &muted) < 0)
671 goto fail;
672
673 if (!name || !*name)
674 goto fail;
675
676 entry.volume_valid = entry.volume.channels > 0;
677
678 if (entry.volume_valid)
679 if (!pa_cvolume_compatible_with_channel_map(&entry.volume, &entry.channel_map))
680 goto fail;
681
682 entry.muted = muted;
683 entry.muted_valid = TRUE;
684
685 if (device)
686 pa_strlcpy(entry.device, device, sizeof(entry.device));
687 entry.device_valid = !!entry.device[0];
688
689 if (entry.device_valid &&
690 !pa_namereg_is_valid_name(entry.device))
691 goto fail;
692
693 key.data = (char*) name;
694 key.size = strlen(name);
695
696 data.data = &entry;
697 data.size = sizeof(entry);
698
699 if (pa_database_set(u->database, &key, &data, mode == PA_UPDATE_REPLACE) == 0)
700 if (apply_immediately)
701 apply_entry(u, name, &entry);
702 }
703
704 trigger_save(u);
705
706 break;
707 }
708
709 case SUBCOMMAND_DELETE:
710
711 while (!pa_tagstruct_eof(t)) {
712 const char *name;
713 pa_datum key;
714
715 if (pa_tagstruct_gets(t, &name) < 0)
716 goto fail;
717
718 key.data = (char*) name;
719 key.size = strlen(name);
720
721 pa_database_unset(u->database, &key);
722 }
723
724 trigger_save(u);
725
726 break;
727
728 case SUBCOMMAND_SUBSCRIBE: {
729
730 pa_bool_t enabled;
731
732 if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
733 !pa_tagstruct_eof(t))
734 goto fail;
735
736 if (enabled)
737 pa_idxset_put(u->subscribed, c, NULL);
738 else
739 pa_idxset_remove_by_data(u->subscribed, c, NULL);
740
741 break;
742 }
743
744 default:
745 goto fail;
746 }
747
748 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
749 return 0;
750
751 fail:
752
753 if (reply)
754 pa_tagstruct_free(reply);
755
756 return -1;
757 }
758
759 static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
760 pa_assert(p);
761 pa_assert(c);
762 pa_assert(u);
763
764 pa_idxset_remove_by_data(u->subscribed, c, NULL);
765 return PA_HOOK_OK;
766 }
767
768 int pa__init(pa_module*m) {
769 pa_modargs *ma = NULL;
770 struct userdata *u;
771 char *fname;
772 pa_sink_input *si;
773 pa_source_output *so;
774 uint32_t idx;
775 pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE;
776
777 pa_assert(m);
778
779 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
780 pa_log("Failed to parse module arguments");
781 goto fail;
782 }
783
784 if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 ||
785 pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
786 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
787 pa_log("restore_device=, restore_volume= and restore_muted= expect boolean arguments");
788 goto fail;
789 }
790
791 if (!restore_muted && !restore_volume && !restore_device)
792 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
793
794 m->userdata = u = pa_xnew0(struct userdata, 1);
795 u->core = m->core;
796 u->module = m;
797 u->restore_device = restore_device;
798 u->restore_volume = restore_volume;
799 u->restore_muted = restore_muted;
800 u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
801
802 u->protocol = pa_native_protocol_get(m->core);
803 pa_native_protocol_install_ext(u->protocol, m, extension_cb);
804
805 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);
806
807 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
808
809 if (restore_device) {
810 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);
811 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);
812 }
813
814 if (restore_volume || restore_muted)
815 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);
816
817 if (!(fname = pa_state_path("stream-volumes", TRUE)))
818 goto fail;
819
820 if (!(u->database = pa_database_open(fname, TRUE))) {
821 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
822 pa_xfree(fname);
823 goto fail;
824 }
825
826 pa_log_info("Sucessfully opened database file '%s'.", fname);
827 pa_xfree(fname);
828
829 for (si = pa_idxset_first(m->core->sink_inputs, &idx); si; si = pa_idxset_next(m->core->sink_inputs, &idx))
830 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, si->index, u);
831
832 for (so = pa_idxset_first(m->core->source_outputs, &idx); so; so = pa_idxset_next(m->core->source_outputs, &idx))
833 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, so->index, u);
834
835 pa_modargs_free(ma);
836 return 0;
837
838 fail:
839 pa__done(m);
840
841 if (ma)
842 pa_modargs_free(ma);
843
844 return -1;
845 }
846
847 void pa__done(pa_module*m) {
848 struct userdata* u;
849
850 pa_assert(m);
851
852 if (!(u = m->userdata))
853 return;
854
855 if (u->subscription)
856 pa_subscription_free(u->subscription);
857
858 if (u->sink_input_new_hook_slot)
859 pa_hook_slot_free(u->sink_input_new_hook_slot);
860 if (u->sink_input_fixate_hook_slot)
861 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
862 if (u->source_output_new_hook_slot)
863 pa_hook_slot_free(u->source_output_new_hook_slot);
864
865 if (u->connection_unlink_hook_slot)
866 pa_hook_slot_free(u->connection_unlink_hook_slot);
867
868 if (u->save_time_event)
869 u->core->mainloop->time_free(u->save_time_event);
870
871 if (u->database)
872 pa_database_close(u->database);
873
874 if (u->protocol) {
875 pa_native_protocol_remove_ext(u->protocol, m);
876 pa_native_protocol_unref(u->protocol);
877 }
878
879 if (u->subscribed)
880 pa_idxset_free(u->subscribed, NULL, NULL);
881
882 pa_xfree(u);
883 }