2 This file is part of PulseAudio.
4 Copyright 2008 Lennart Poettering
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.
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.
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
29 #include <sys/types.h>
34 #include <pulse/xmalloc.h>
35 #include <pulse/volume.h>
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/core-subscribe.h>
45 #include <pulsecore/sink-input.h>
46 #include <pulsecore/source-output.h>
47 #include <pulsecore/namereg.h>
48 #include <pulsecore/protocol-native.h>
49 #include <pulsecore/pstream.h>
50 #include <pulsecore/pstream-util.h>
51 #include <pulsecore/database.h>
53 #include "module-stream-restore-symdef.h"
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
);
60 "restore_device=<Save/restore sinks/sources?> "
61 "restore_volume=<Save/restore volumes?> "
62 "restore_muted=<Save/restore muted states?> "
63 "on_hotplug=<When new device becomes available, recheck streams?> "
64 "on_rescue=<When device becomes unavailable, recheck streams?>");
66 #define SAVE_INTERVAL 10
67 #define IDENTIFICATION_PROPERTY "module-stream-restore.id"
69 static const char* const valid_modargs
[] = {
81 pa_subscription
*subscription
;
83 *sink_input_new_hook_slot
,
84 *sink_input_fixate_hook_slot
,
85 *source_output_new_hook_slot
,
87 *source_put_hook_slot
,
88 *sink_unlink_hook_slot
,
89 *source_unlink_hook_slot
,
90 *connection_unlink_hook_slot
;
91 pa_time_event
*save_time_event
;
92 pa_database
* database
;
94 pa_bool_t restore_device
:1;
95 pa_bool_t restore_volume
:1;
96 pa_bool_t restore_muted
:1;
97 pa_bool_t on_hotplug
:1;
98 pa_bool_t on_rescue
:1;
100 pa_native_protocol
*protocol
;
101 pa_idxset
*subscribed
;
104 #define ENTRY_VERSION 2
108 pa_bool_t muted_valid
:1, volume_valid
:1, device_valid
:1;
110 pa_channel_map channel_map
;
112 char device
[PA_NAME_MAX
];
120 SUBCOMMAND_SUBSCRIBE
,
124 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
125 struct userdata
*u
= userdata
;
132 pa_assert(e
== u
->save_time_event
);
133 u
->core
->mainloop
->time_free(u
->save_time_event
);
134 u
->save_time_event
= NULL
;
136 pa_database_sync(u
->database
);
137 pa_log_info("Synced.");
140 static char *get_name(pa_proplist
*p
, const char *prefix
) {
147 if ((r
= pa_proplist_gets(p
, IDENTIFICATION_PROPERTY
)))
148 return pa_xstrdup(r
);
150 if ((r
= pa_proplist_gets(p
, PA_PROP_MEDIA_ROLE
)))
151 t
= pa_sprintf_malloc("%s-by-media-role:%s", prefix
, r
);
152 else if ((r
= pa_proplist_gets(p
, PA_PROP_APPLICATION_ID
)))
153 t
= pa_sprintf_malloc("%s-by-application-id:%s", prefix
, r
);
154 else if ((r
= pa_proplist_gets(p
, PA_PROP_APPLICATION_NAME
)))
155 t
= pa_sprintf_malloc("%s-by-application-name:%s", prefix
, r
);
156 else if ((r
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
)))
157 t
= pa_sprintf_malloc("%s-by-media-name:%s", prefix
, r
);
159 t
= pa_sprintf_malloc("%s-fallback:%s", prefix
, r
);
161 pa_proplist_sets(p
, IDENTIFICATION_PROPERTY
, t
);
165 static struct entry
* read_entry(struct userdata
*u
, const char *name
) {
172 key
.data
= (char*) name
;
173 key
.size
= strlen(name
);
177 if (!pa_database_get(u
->database
, &key
, &data
))
180 if (data
.size
!= sizeof(struct entry
)) {
181 /* This is probably just a database upgrade, hence let's not
182 * consider this more than a debug message */
183 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
));
187 e
= (struct entry
*) data
.data
;
189 if (e
->version
!= ENTRY_VERSION
) {
190 pa_log_debug("Version of database entry for stream %s doesn't match our version. Probably due to upgrade, ignoring.", name
);
194 if (!memchr(e
->device
, 0, sizeof(e
->device
))) {
195 pa_log_warn("Database contains entry for stream %s with missing NUL byte in device name", name
);
199 if (e
->device_valid
&& !pa_namereg_is_valid_name(e
->device
)) {
200 pa_log_warn("Invalid device name stored in database for stream %s", name
);
204 if (e
->volume_valid
&& !pa_channel_map_valid(&e
->channel_map
)) {
205 pa_log_warn("Invalid channel map stored in database for stream %s", name
);
209 if (e
->volume_valid
&& (!pa_cvolume_valid(&e
->volume
) || !pa_cvolume_compatible_with_channel_map(&e
->volume
, &e
->channel_map
))) {
210 pa_log_warn("Invalid volume stored in database for stream %s", name
);
218 pa_datum_free(&data
);
222 static void trigger_save(struct userdata
*u
) {
224 pa_native_connection
*c
;
227 for (c
= pa_idxset_first(u
->subscribed
, &idx
); c
; c
= pa_idxset_next(u
->subscribed
, &idx
)) {
230 t
= pa_tagstruct_new(NULL
, 0);
231 pa_tagstruct_putu32(t
, PA_COMMAND_EXTENSION
);
232 pa_tagstruct_putu32(t
, 0);
233 pa_tagstruct_putu32(t
, u
->module
->index
);
234 pa_tagstruct_puts(t
, u
->module
->name
);
235 pa_tagstruct_putu32(t
, SUBCOMMAND_EVENT
);
237 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c
), t
);
240 if (u
->save_time_event
)
243 pa_gettimeofday(&tv
);
244 tv
.tv_sec
+= SAVE_INTERVAL
;
245 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
248 static pa_bool_t
entries_equal(const struct entry
*a
, const struct entry
*b
) {
254 if (a
->device_valid
!= b
->device_valid
||
255 (a
->device_valid
&& strncmp(a
->device
, b
->device
, sizeof(a
->device
))))
258 if (a
->muted_valid
!= b
->muted_valid
||
259 (a
->muted_valid
&& (a
->muted
!= b
->muted
)))
263 if (a
->volume_valid
!= b
->volume_valid
||
264 (a
->volume_valid
&& !pa_cvolume_equal(pa_cvolume_remap(&t
, &b
->channel_map
, &a
->channel_map
), &a
->volume
)))
270 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
271 struct userdata
*u
= userdata
;
272 struct entry entry
, *old
;
279 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
280 t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
281 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
282 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
))
286 entry
.version
= ENTRY_VERSION
;
288 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK_INPUT
) {
289 pa_sink_input
*sink_input
;
291 if (!(sink_input
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
294 if (!(name
= get_name(sink_input
->proplist
, "sink-input")))
297 if ((old
= read_entry(u
, name
)))
300 if (sink_input
->save_volume
) {
301 entry
.channel_map
= sink_input
->channel_map
;
302 pa_sink_input_get_volume(sink_input
, &entry
.volume
, FALSE
);
303 entry
.volume_valid
= TRUE
;
306 if (sink_input
->save_muted
) {
307 entry
.muted
= pa_sink_input_get_mute(sink_input
);
308 entry
.muted_valid
= TRUE
;
311 if (sink_input
->save_sink
) {
312 pa_strlcpy(entry
.device
, sink_input
->sink
->name
, sizeof(entry
.device
));
313 entry
.device_valid
= TRUE
;
317 pa_source_output
*source_output
;
319 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
);
321 if (!(source_output
= pa_idxset_get_by_index(c
->source_outputs
, idx
)))
324 if (!(name
= get_name(source_output
->proplist
, "source-output")))
327 if ((old
= read_entry(u
, name
)))
330 if (source_output
->save_source
) {
331 pa_strlcpy(entry
.device
, source_output
->source
->name
, sizeof(entry
.device
));
332 entry
.device_valid
= source_output
->save_source
;
338 if (entries_equal(old
, &entry
)) {
348 key
.size
= strlen(name
);
351 data
.size
= sizeof(entry
);
353 pa_log_info("Storing volume/mute/device for stream %s.", name
);
355 pa_database_set(u
->database
, &key
, &data
, TRUE
);
362 static pa_hook_result_t
sink_input_new_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
369 pa_assert(u
->restore_device
);
371 if (!(name
= get_name(new_data
->proplist
, "sink-input")))
374 if ((e
= read_entry(u
, name
))) {
376 if (e
->device_valid
) {
379 if ((s
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SINK
))) {
380 if (!new_data
->sink
) {
381 pa_log_info("Restoring device for stream %s.", name
);
383 new_data
->save_sink
= TRUE
;
385 pa_log_debug("Not restoring device for stream %s, because already set.", name
);
397 static pa_hook_result_t
sink_input_fixate_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
404 pa_assert(u
->restore_volume
|| u
->restore_muted
);
406 if (!(name
= get_name(new_data
->proplist
, "sink-input")))
409 if ((e
= read_entry(u
, name
))) {
411 if (u
->restore_volume
&& e
->volume_valid
) {
413 if (!new_data
->volume_is_set
) {
416 pa_log_info("Restoring volume for sink input %s.", name
);
419 pa_cvolume_remap(&v
, &e
->channel_map
, &new_data
->channel_map
);
420 pa_sink_input_new_data_set_volume(new_data
, &v
);
422 new_data
->volume_is_absolute
= FALSE
;
423 new_data
->save_volume
= TRUE
;
425 pa_log_debug("Not restoring volume for sink input %s, because already set.", name
);
428 if (u
->restore_muted
&& e
->muted_valid
) {
430 if (!new_data
->muted_is_set
) {
431 pa_log_info("Restoring mute state for sink input %s.", name
);
432 pa_sink_input_new_data_set_muted(new_data
, e
->muted
);
433 new_data
->save_muted
= TRUE
;
435 pa_log_debug("Not restoring mute state for sink input %s, because already set.", name
);
446 static pa_hook_result_t
source_output_new_hook_callback(pa_core
*c
, pa_source_output_new_data
*new_data
, struct userdata
*u
) {
453 pa_assert(u
->restore_device
);
455 if (new_data
->direct_on_input
)
458 if (!(name
= get_name(new_data
->proplist
, "source-output")))
461 if ((e
= read_entry(u
, name
))) {
464 if (e
->device_valid
) {
465 if ((s
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SOURCE
))) {
466 if (!new_data
->source
) {
467 pa_log_info("Restoring device for stream %s.", name
);
468 new_data
->source
= s
;
469 new_data
->save_source
= TRUE
;
471 pa_log_debug("Not restoring device for stream %s, because already set", name
);
483 static pa_hook_result_t
sink_put_hook_callback(pa_core
*c
, pa_sink
*sink
, struct userdata
*u
) {
490 pa_assert(u
->on_hotplug
&& u
->restore_device
);
492 PA_IDXSET_FOREACH(si
, c
->sink_inputs
, idx
) {
496 if (si
->sink
== sink
)
502 if (!(name
= get_name(si
->proplist
, "sink-input")))
505 if ((e
= read_entry(u
, name
))) {
506 if (e
->device_valid
&& pa_streq(e
->device
, sink
->name
))
507 pa_sink_input_move_to(si
, sink
, TRUE
);
518 static pa_hook_result_t
source_put_hook_callback(pa_core
*c
, pa_source
*source
, struct userdata
*u
) {
519 pa_source_output
*so
;
525 pa_assert(u
->on_hotplug
&& u
->restore_device
);
527 PA_IDXSET_FOREACH(so
, c
->source_outputs
, idx
) {
531 if (so
->source
== source
)
537 if (so
->direct_on_input
)
540 if (!(name
= get_name(so
->proplist
, "source-input")))
543 if ((e
= read_entry(u
, name
))) {
544 if (e
->device_valid
&& pa_streq(e
->device
, source
->name
))
545 pa_source_output_move_to(so
, source
, TRUE
);
556 static pa_hook_result_t
sink_unlink_hook_callback(pa_core
*c
, pa_sink
*sink
, struct userdata
*u
) {
563 pa_assert(u
->on_rescue
&& u
->restore_device
);
565 /* There's no point in doing anything if the core is shut down anyway */
566 if (c
->state
== PA_CORE_SHUTDOWN
)
569 PA_IDXSET_FOREACH(si
, sink
->inputs
, idx
) {
573 if (!(name
= get_name(si
->proplist
, "sink-input")))
576 if ((e
= read_entry(u
, name
))) {
578 if (e
->device_valid
) {
581 if ((d
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SINK
)) && d
!= sink
)
582 pa_sink_input_move_to(si
, d
, TRUE
);
594 static pa_hook_result_t
source_unlink_hook_callback(pa_core
*c
, pa_source
*source
, struct userdata
*u
) {
595 pa_source_output
*so
;
601 pa_assert(u
->on_rescue
&& u
->restore_device
);
603 /* There's no point in doing anything if the core is shut down anyway */
604 if (c
->state
== PA_CORE_SHUTDOWN
)
607 PA_IDXSET_FOREACH(so
, source
->outputs
, idx
) {
611 if (!(name
= get_name(so
->proplist
, "source-output")))
614 if ((e
= read_entry(u
, name
))) {
616 if (e
->device_valid
) {
619 if ((d
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SOURCE
)) && d
!= source
)
620 pa_source_output_move_to(so
, d
, TRUE
);
632 #define EXT_VERSION 1
634 static void apply_entry(struct userdata
*u
, const char *name
, struct entry
*e
) {
636 pa_source_output
*so
;
643 for (si
= pa_idxset_first(u
->core
->sink_inputs
, &idx
); si
; si
= pa_idxset_next(u
->core
->sink_inputs
, &idx
)) {
647 if (!(n
= get_name(si
->proplist
, "sink-input")))
650 if (!pa_streq(name
, n
)) {
656 if (u
->restore_volume
&& e
->volume_valid
) {
660 pa_log_info("Restoring volume for sink input %s.", name
);
661 pa_sink_input_set_volume(si
, pa_cvolume_remap(&v
, &e
->channel_map
, &si
->channel_map
), FALSE
, FALSE
);
664 if (u
->restore_muted
&& e
->muted_valid
) {
665 pa_log_info("Restoring mute state for sink input %s.", name
);
666 pa_sink_input_set_mute(si
, e
->muted
, FALSE
);
669 if (u
->restore_device
&&
671 (s
= pa_namereg_get(u
->core
, e
->device
, PA_NAMEREG_SINK
))) {
673 pa_log_info("Restoring device for stream %s.", name
);
674 pa_sink_input_move_to(si
, s
, FALSE
);
678 for (so
= pa_idxset_first(u
->core
->source_outputs
, &idx
); so
; so
= pa_idxset_next(u
->core
->source_outputs
, &idx
)) {
682 if (!(n
= get_name(so
->proplist
, "source-output")))
685 if (!pa_streq(name
, n
)) {
691 if (u
->restore_device
&&
693 (s
= pa_namereg_get(u
->core
, e
->device
, PA_NAMEREG_SOURCE
))) {
695 pa_log_info("Restoring device for stream %s.", name
);
696 pa_source_output_move_to(so
, s
, FALSE
);
702 static void dump_database(struct userdata
*u
) {
706 done
= !pa_database_first(u
->database
, &key
, NULL
);
713 done
= !pa_database_next(u
->database
, &key
, &next_key
, NULL
);
715 name
= pa_xstrndup(key
.data
, key
.size
);
718 if ((e
= read_entry(u
, name
))) {
720 pa_log("name=%s", name
);
721 pa_log("device=%s %s", e
->device
, pa_yes_no(e
->device_valid
));
722 pa_log("channel_map=%s", pa_channel_map_snprint(t
, sizeof(t
), &e
->channel_map
));
723 pa_log("volume=%s %s", pa_cvolume_snprint(t
, sizeof(t
), &e
->volume
), pa_yes_no(e
->volume_valid
));
724 pa_log("mute=%s %s", pa_yes_no(e
->muted
), pa_yes_no(e
->volume_valid
));
735 static int extension_cb(pa_native_protocol
*p
, pa_module
*m
, pa_native_connection
*c
, uint32_t tag
, pa_tagstruct
*t
) {
738 pa_tagstruct
*reply
= NULL
;
747 if (pa_tagstruct_getu32(t
, &command
) < 0)
750 reply
= pa_tagstruct_new(NULL
, 0);
751 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
752 pa_tagstruct_putu32(reply
, tag
);
755 case SUBCOMMAND_TEST
: {
756 if (!pa_tagstruct_eof(t
))
759 pa_tagstruct_putu32(reply
, EXT_VERSION
);
763 case SUBCOMMAND_READ
: {
767 if (!pa_tagstruct_eof(t
))
770 done
= !pa_database_first(u
->database
, &key
, NULL
);
777 done
= !pa_database_next(u
->database
, &key
, &next_key
, NULL
);
779 name
= pa_xstrndup(key
.data
, key
.size
);
782 if ((e
= read_entry(u
, name
))) {
786 pa_tagstruct_puts(reply
, name
);
787 pa_tagstruct_put_channel_map(reply
, e
->volume_valid
? &e
->channel_map
: pa_channel_map_init(&cm
));
788 pa_tagstruct_put_cvolume(reply
, e
->volume_valid
? &e
->volume
: pa_cvolume_init(&r
));
789 pa_tagstruct_puts(reply
, e
->device_valid
? e
->device
: NULL
);
790 pa_tagstruct_put_boolean(reply
, e
->muted_valid
? e
->muted
: FALSE
);
803 case SUBCOMMAND_WRITE
: {
805 pa_bool_t apply_immediately
= FALSE
;
807 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
808 pa_tagstruct_get_boolean(t
, &apply_immediately
) < 0)
811 if (mode
!= PA_UPDATE_MERGE
&&
812 mode
!= PA_UPDATE_REPLACE
&&
813 mode
!= PA_UPDATE_SET
)
816 if (mode
== PA_UPDATE_SET
)
817 pa_database_clear(u
->database
);
819 while (!pa_tagstruct_eof(t
)) {
820 const char *name
, *device
;
826 entry
.version
= ENTRY_VERSION
;
828 if (pa_tagstruct_gets(t
, &name
) < 0 ||
829 pa_tagstruct_get_channel_map(t
, &entry
.channel_map
) ||
830 pa_tagstruct_get_cvolume(t
, &entry
.volume
) < 0 ||
831 pa_tagstruct_gets(t
, &device
) < 0 ||
832 pa_tagstruct_get_boolean(t
, &muted
) < 0)
838 entry
.volume_valid
= entry
.volume
.channels
> 0;
840 if (entry
.volume_valid
)
841 if (!pa_cvolume_compatible_with_channel_map(&entry
.volume
, &entry
.channel_map
))
845 entry
.muted_valid
= TRUE
;
848 pa_strlcpy(entry
.device
, device
, sizeof(entry
.device
));
849 entry
.device_valid
= !!entry
.device
[0];
851 if (entry
.device_valid
&&
852 !pa_namereg_is_valid_name(entry
.device
))
855 key
.data
= (char*) name
;
856 key
.size
= strlen(name
);
859 data
.size
= sizeof(entry
);
861 if (pa_database_set(u
->database
, &key
, &data
, mode
== PA_UPDATE_REPLACE
) == 0)
862 if (apply_immediately
)
863 apply_entry(u
, name
, &entry
);
871 case SUBCOMMAND_DELETE
:
873 while (!pa_tagstruct_eof(t
)) {
877 if (pa_tagstruct_gets(t
, &name
) < 0)
880 key
.data
= (char*) name
;
881 key
.size
= strlen(name
);
883 pa_database_unset(u
->database
, &key
);
890 case SUBCOMMAND_SUBSCRIBE
: {
894 if (pa_tagstruct_get_boolean(t
, &enabled
) < 0 ||
895 !pa_tagstruct_eof(t
))
899 pa_idxset_put(u
->subscribed
, c
, NULL
);
901 pa_idxset_remove_by_data(u
->subscribed
, c
, NULL
);
910 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c
), reply
);
916 pa_tagstruct_free(reply
);
921 static pa_hook_result_t
connection_unlink_hook_cb(pa_native_protocol
*p
, pa_native_connection
*c
, struct userdata
*u
) {
926 pa_idxset_remove_by_data(u
->subscribed
, c
, NULL
);
930 int pa__init(pa_module
*m
) {
931 pa_modargs
*ma
= NULL
;
935 pa_source_output
*so
;
937 pa_bool_t restore_device
= TRUE
, restore_volume
= TRUE
, restore_muted
= TRUE
, on_hotplug
= TRUE
, on_rescue
= TRUE
;
941 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
942 pa_log("Failed to parse module arguments");
946 if (pa_modargs_get_value_boolean(ma
, "restore_device", &restore_device
) < 0 ||
947 pa_modargs_get_value_boolean(ma
, "restore_volume", &restore_volume
) < 0 ||
948 pa_modargs_get_value_boolean(ma
, "restore_muted", &restore_muted
) < 0 ||
949 pa_modargs_get_value_boolean(ma
, "on_hotplug", &on_hotplug
) < 0 ||
950 pa_modargs_get_value_boolean(ma
, "on_rescue", &on_rescue
) < 0) {
951 pa_log("restore_device=, restore_volume=, restore_muted=, on_hotplug= and on_rescue= expect boolean arguments");
955 if (!restore_muted
&& !restore_volume
&& !restore_device
)
956 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
958 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
961 u
->restore_device
= restore_device
;
962 u
->restore_volume
= restore_volume
;
963 u
->restore_muted
= restore_muted
;
964 u
->on_hotplug
= on_hotplug
;
965 u
->on_rescue
= on_rescue
;
966 u
->subscribed
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
968 u
->protocol
= pa_native_protocol_get(m
->core
);
969 pa_native_protocol_install_ext(u
->protocol
, m
, extension_cb
);
971 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
);
973 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK_INPUT
|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT
, subscribe_callback
, u
);
975 if (restore_device
) {
976 /* A little bit earlier than module-intended-roles ... */
977 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
);
978 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
);
981 if (restore_device
&& on_hotplug
) {
982 /* A little bit earlier than module-intended-roles ... */
983 u
->sink_put_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_put_hook_callback
, u
);
984 u
->source_put_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_put_hook_callback
, u
);
987 if (restore_device
&& on_rescue
) {
988 /* A little bit earlier than module-intended-roles, module-rescue-streams, ... */
989 u
->sink_unlink_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_unlink_hook_callback
, NULL
);
990 u
->source_unlink_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_unlink_hook_callback
, NULL
);
993 if (restore_volume
|| restore_muted
)
994 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
);
996 if (!(fname
= pa_state_path("stream-volumes", TRUE
)))
999 if (!(u
->database
= pa_database_open(fname
, TRUE
))) {
1000 pa_log("Failed to open volume database '%s': %s", fname
, pa_cstrerror(errno
));
1005 pa_log_info("Sucessfully opened database file '%s'.", fname
);
1008 PA_IDXSET_FOREACH(si
, m
->core
->sink_inputs
, idx
)
1009 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, si
->index
, u
);
1011 PA_IDXSET_FOREACH(so
, m
->core
->source_outputs
, idx
)
1012 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, so
->index
, u
);
1014 pa_modargs_free(ma
);
1021 pa_modargs_free(ma
);
1026 void pa__done(pa_module
*m
) {
1031 if (!(u
= m
->userdata
))
1034 if (u
->subscription
)
1035 pa_subscription_free(u
->subscription
);
1037 if (u
->sink_input_new_hook_slot
)
1038 pa_hook_slot_free(u
->sink_input_new_hook_slot
);
1039 if (u
->sink_input_fixate_hook_slot
)
1040 pa_hook_slot_free(u
->sink_input_fixate_hook_slot
);
1041 if (u
->source_output_new_hook_slot
)
1042 pa_hook_slot_free(u
->source_output_new_hook_slot
);
1044 if (u
->sink_put_hook_slot
)
1045 pa_hook_slot_free(u
->sink_put_hook_slot
);
1046 if (u
->source_put_hook_slot
)
1047 pa_hook_slot_free(u
->source_put_hook_slot
);
1049 if (u
->sink_unlink_hook_slot
)
1050 pa_hook_slot_free(u
->sink_unlink_hook_slot
);
1051 if (u
->source_unlink_hook_slot
)
1052 pa_hook_slot_free(u
->source_unlink_hook_slot
);
1054 if (u
->connection_unlink_hook_slot
)
1055 pa_hook_slot_free(u
->connection_unlink_hook_slot
);
1057 if (u
->save_time_event
)
1058 u
->core
->mainloop
->time_free(u
->save_time_event
);
1061 pa_database_close(u
->database
);
1064 pa_native_protocol_remove_ext(u
->protocol
, m
);
1065 pa_native_protocol_unref(u
->protocol
);
1069 pa_idxset_free(u
->subscribed
, NULL
, NULL
);