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 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>
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
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>
50 #include "module-stream-restore-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute/device state of streams");
54 PA_MODULE_VERSION(PACKAGE_VERSION
);
55 PA_MODULE_LOAD_ONCE(TRUE
);
57 #define SAVE_INTERVAL 10
59 static const char* const valid_modargs
[] = {
68 pa_subscription
*subscription
;
70 *sink_input_new_hook_slot
,
71 *sink_input_fixate_hook_slot
,
72 *source_output_new_hook_slot
;
73 pa_time_event
*save_time_event
;
76 pa_bool_t restore_device
:1;
77 pa_bool_t restore_volume
:1;
78 pa_bool_t restore_muted
:1;
83 char device
[PA_NAME_MAX
];
87 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
88 struct userdata
*u
= userdata
;
95 pa_assert(e
== u
->save_time_event
);
96 u
->core
->mainloop
->time_free(u
->save_time_event
);
97 u
->save_time_event
= NULL
;
99 gdbm_sync(u
->gdbm_file
);
100 pa_log_info("Synced.");
103 static char *get_name(pa_proplist
*p
, const char *prefix
) {
109 if ((r
= pa_proplist_gets(p
, PA_PROP_MEDIA_ROLE
)))
110 return pa_sprintf_malloc("%s-by-media-role:%s", prefix
, r
);
111 else if ((r
= pa_proplist_gets(p
, PA_PROP_APPLICATION_ID
)))
112 return pa_sprintf_malloc("%s-by-application-id:%s", prefix
, r
);
113 else if ((r
= pa_proplist_gets(p
, PA_PROP_APPLICATION_NAME
)))
114 return pa_sprintf_malloc("%s-by-application-name:%s", prefix
, r
);
115 else if ((r
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
)))
116 return pa_sprintf_malloc("%s-by-media-name:%s", prefix
, r
);
121 static struct entry
* read_entry(struct userdata
*u
, char *name
) {
129 key
.dsize
= strlen(name
);
131 data
= gdbm_fetch(u
->gdbm_file
, key
);
136 if (data
.dsize
!= sizeof(struct entry
)) {
137 pa_log_warn("Database contains entry for stream %s of wrong size %lu != %lu", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
141 e
= (struct entry
*) data
.dptr
;
143 if (!memchr(e
->device
, 0, sizeof(e
->device
))) {
144 pa_log_warn("Database contains entry for stream %s with missing NUL byte in device name", name
);
148 if (!(pa_cvolume_valid(&e
->volume
))) {
149 pa_log_warn("Invalid volume stored in database for device %s", name
);
161 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
162 struct userdata
*u
= userdata
;
163 struct entry entry
, *old
;
170 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
171 t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
172 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
173 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
))
176 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK_INPUT
) {
177 pa_sink_input
*sink_input
;
179 if (!(sink_input
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
182 if (!(name
= get_name(sink_input
->proplist
, "sink-input")))
185 entry
.volume
= *pa_sink_input_get_volume(sink_input
);
186 entry
.muted
= pa_sink_input_get_mute(sink_input
);
187 pa_strlcpy(entry
.device
, sink_input
->sink
->name
, sizeof(entry
.device
));
190 pa_source_output
*source_output
;
192 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
);
194 if (!(source_output
= pa_idxset_get_by_index(c
->source_outputs
, idx
)))
197 if (!(name
= get_name(source_output
->proplist
, "source-output")))
200 memset(&entry
.volume
, 0, sizeof(entry
.volume
));
203 pa_strlcpy(entry
.device
, source_output
->source
->name
, sizeof(entry
.device
));
206 if ((old
= read_entry(u
, name
))) {
208 if (pa_cvolume_equal(&old
->volume
, &entry
.volume
) &&
209 !old
->muted
== !entry
.muted
&&
210 strcmp(old
->device
, entry
.device
) == 0) {
221 key
.dsize
= strlen(name
);
223 data
.dptr
= (void*) &entry
;
224 data
.dsize
= sizeof(entry
);
226 pa_log_info("Storing volume/mute/device for stream %s.", name
);
228 gdbm_store(u
->gdbm_file
, key
, data
, GDBM_REPLACE
);
230 if (!u
->save_time_event
) {
232 pa_gettimeofday(&tv
);
233 tv
.tv_sec
+= SAVE_INTERVAL
;
234 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
240 static pa_hook_result_t
sink_input_new_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
246 if (!(name
= get_name(new_data
->proplist
, "sink-input")))
249 if ((e
= read_entry(u
, name
))) {
252 if (u
->restore_device
&&
254 (s
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SINK
, TRUE
))) {
256 pa_log_info("Restoring device for stream %s.", name
);
268 static pa_hook_result_t
sink_input_fixate_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
274 if (!(name
= get_name(new_data
->proplist
, "sink-input")))
277 if ((e
= read_entry(u
, name
))) {
279 if (u
->restore_volume
&&
280 e
->volume
.channels
== new_data
->sample_spec
.channels
) {
282 pa_log_info("Restoring volume for sink input %s.", name
);
283 pa_sink_input_new_data_set_volume(new_data
, &e
->volume
);
286 if (u
->restore_muted
) {
287 pa_log_info("Restoring mute state for sink input %s.", name
);
288 pa_sink_input_new_data_set_muted(new_data
, e
->muted
);
299 static pa_hook_result_t
source_output_new_hook_callback(pa_core
*c
, pa_source_output_new_data
*new_data
, struct userdata
*u
) {
305 if (!(name
= get_name(new_data
->proplist
, "source-output")))
308 if ((e
= read_entry(u
, name
))) {
311 if (u
->restore_device
&&
313 (s
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SOURCE
, TRUE
))) {
315 pa_log_info("Restoring device for stream %s.", name
);
316 new_data
->source
= s
;
327 int pa__init(pa_module
*m
) {
328 pa_modargs
*ma
= NULL
;
333 pa_source_output
*so
;
335 pa_bool_t restore_device
= TRUE
, restore_volume
= TRUE
, restore_muted
= TRUE
;
339 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
340 pa_log("Failed to parse module arguments");
344 if (pa_modargs_get_value_boolean(ma
, "restore_device", &restore_device
) < 0 ||
345 pa_modargs_get_value_boolean(ma
, "restore_volume", &restore_volume
) < 0 ||
346 pa_modargs_get_value_boolean(ma
, "restore_muted", &restore_muted
) < 0) {
347 pa_log("restore_device=, restore_volume= and restore_muted= expect boolean arguments");
351 if (!restore_muted
&& !restore_volume
&& !restore_device
)
352 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
354 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
356 u
->save_time_event
= NULL
;
357 u
->restore_device
= restore_device
;
358 u
->restore_volume
= restore_volume
;
359 u
->restore_muted
= restore_muted
;
361 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK_INPUT
|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT
, subscribe_callback
, u
);
363 if (restore_device
) {
364 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
);
365 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
);
368 if (restore_volume
|| restore_muted
)
369 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
);
371 if (!pa_get_host_name(hn
, sizeof(hn
)))
374 fn
= pa_sprintf_malloc("stream-volumes.%s."CANONICAL_HOST
".gdbm", hn
);
375 fname
= pa_state_path(fn
);
381 if (!(u
->gdbm_file
= gdbm_open(fname
, 0, GDBM_WRCREAT
, 0600, NULL
))) {
382 pa_log("Failed to open volume database '%s': %s", fname
, gdbm_strerror(gdbm_errno
));
387 pa_log_info("Sucessfully opened database file '%s'.", fname
);
390 for (si
= pa_idxset_first(m
->core
->sink_inputs
, &idx
); si
; si
= pa_idxset_next(m
->core
->sink_inputs
, &idx
))
391 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, si
->index
, u
);
393 for (so
= pa_idxset_first(m
->core
->source_outputs
, &idx
); so
; so
= pa_idxset_next(m
->core
->source_outputs
, &idx
))
394 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, so
->index
, u
);
408 void pa__done(pa_module
*m
) {
413 if (!(u
= m
->userdata
))
417 pa_subscription_free(u
->subscription
);
419 if (u
->sink_input_new_hook_slot
)
420 pa_hook_slot_free(u
->sink_input_new_hook_slot
);
421 if (u
->sink_input_fixate_hook_slot
)
422 pa_hook_slot_free(u
->sink_input_fixate_hook_slot
);
423 if (u
->source_output_new_hook_slot
)
424 pa_hook_slot_free(u
->source_output_new_hook_slot
);
426 if (u
->save_time_event
)
427 u
->core
->mainloop
->time_free(u
->save_time_event
);
430 gdbm_close(u
->gdbm_file
);