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
[] = {
69 pa_subscription
*subscription
;
71 *sink_input_new_hook_slot
,
72 *sink_input_fixate_hook_slot
,
73 *source_output_new_hook_slot
;
74 pa_time_event
*save_time_event
;
77 pa_bool_t restore_device
:1;
78 pa_bool_t restore_volume
:1;
79 pa_bool_t restore_muted
:1;
83 char device
[PA_NAME_MAX
];
84 pa_channel_map channel_map
;
89 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
90 struct userdata
*u
= userdata
;
97 pa_assert(e
== u
->save_time_event
);
98 u
->core
->mainloop
->time_free(u
->save_time_event
);
99 u
->save_time_event
= NULL
;
101 gdbm_sync(u
->gdbm_file
);
102 pa_log_info("Synced.");
105 static char *get_name(pa_proplist
*p
, const char *prefix
) {
111 if ((r
= pa_proplist_gets(p
, PA_PROP_MEDIA_ROLE
)))
112 return pa_sprintf_malloc("%s-by-media-role:%s", prefix
, r
);
113 else if ((r
= pa_proplist_gets(p
, PA_PROP_APPLICATION_ID
)))
114 return pa_sprintf_malloc("%s-by-application-id:%s", prefix
, r
);
115 else if ((r
= pa_proplist_gets(p
, PA_PROP_APPLICATION_NAME
)))
116 return pa_sprintf_malloc("%s-by-application-name:%s", prefix
, r
);
117 else if ((r
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
)))
118 return pa_sprintf_malloc("%s-by-media-name:%s", prefix
, r
);
123 static struct entry
* read_entry(struct userdata
*u
, char *name
) {
131 key
.dsize
= strlen(name
);
133 data
= gdbm_fetch(u
->gdbm_file
, key
);
138 if (data
.dsize
!= sizeof(struct entry
)) {
139 pa_log_warn("Database contains entry for stream %s of wrong size %lu != %lu", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
143 e
= (struct entry
*) data
.dptr
;
145 if (!memchr(e
->device
, 0, sizeof(e
->device
))) {
146 pa_log_warn("Database contains entry for stream %s with missing NUL byte in device name", name
);
150 if (!(pa_cvolume_valid(&e
->volume
))) {
151 pa_log_warn("Invalid volume stored in database for stream %s", name
);
155 if (!(pa_channel_map_valid(&e
->channel_map
))) {
156 pa_log_warn("Invalid channel map stored in database for stream %s", name
);
160 if (e
->volume
.channels
!= e
->channel_map
.channels
) {
161 pa_log_warn("Volume and channel map don't match in database entry for stream %s", name
);
173 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
174 struct userdata
*u
= userdata
;
175 struct entry entry
, *old
;
182 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
183 t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
184 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
185 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
))
188 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK_INPUT
) {
189 pa_sink_input
*sink_input
;
191 if (!(sink_input
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
194 if (!(name
= get_name(sink_input
->proplist
, "sink-input")))
197 entry
.channel_map
= sink_input
->channel_map
;
198 entry
.volume
= *pa_sink_input_get_volume(sink_input
);
199 entry
.muted
= pa_sink_input_get_mute(sink_input
);
200 pa_strlcpy(entry
.device
, sink_input
->sink
->name
, sizeof(entry
.device
));
203 pa_source_output
*source_output
;
205 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
);
207 if (!(source_output
= pa_idxset_get_by_index(c
->source_outputs
, idx
)))
210 if (!(name
= get_name(source_output
->proplist
, "source-output")))
213 /* The following fields are filled in to make the entry valid
214 * according to read_entry(). They are otherwise useless */
215 entry
.channel_map
= source_output
->channel_map
;
216 pa_cvolume_reset(&entry
.volume
, entry
.channel_map
.channels
);
217 pa_strlcpy(entry
.device
, source_output
->source
->name
, sizeof(entry
.device
));
220 if ((old
= read_entry(u
, name
))) {
222 if (pa_cvolume_equal(pa_cvolume_remap(&old
->volume
, &old
->channel_map
, &entry
.channel_map
), &entry
.volume
) &&
223 !old
->muted
== !entry
.muted
&&
224 strcmp(old
->device
, entry
.device
) == 0) {
235 key
.dsize
= strlen(name
);
237 data
.dptr
= (void*) &entry
;
238 data
.dsize
= sizeof(entry
);
240 pa_log_info("Storing volume/mute/device for stream %s.", name
);
242 gdbm_store(u
->gdbm_file
, key
, data
, GDBM_REPLACE
);
244 if (!u
->save_time_event
) {
246 pa_gettimeofday(&tv
);
247 tv
.tv_sec
+= SAVE_INTERVAL
;
248 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
254 static pa_hook_result_t
sink_input_new_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
260 if (!(name
= get_name(new_data
->proplist
, "sink-input")))
263 if ((e
= read_entry(u
, name
))) {
266 if (u
->restore_device
&&
268 (s
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SINK
, TRUE
))) {
270 pa_log_info("Restoring device for stream %s.", name
);
282 static pa_hook_result_t
sink_input_fixate_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
288 if (!(name
= get_name(new_data
->proplist
, "sink-input")))
291 if ((e
= read_entry(u
, name
))) {
293 if (u
->restore_volume
) {
294 pa_log_info("Restoring volume for sink input %s.", name
);
295 pa_sink_input_new_data_set_volume(new_data
, pa_cvolume_remap(&e
->volume
, &e
->channel_map
, &new_data
->channel_map
));
298 if (u
->restore_muted
) {
299 pa_log_info("Restoring mute state for sink input %s.", name
);
300 pa_sink_input_new_data_set_muted(new_data
, e
->muted
);
311 static pa_hook_result_t
source_output_new_hook_callback(pa_core
*c
, pa_source_output_new_data
*new_data
, struct userdata
*u
) {
317 if (!(name
= get_name(new_data
->proplist
, "source-output")))
320 if ((e
= read_entry(u
, name
))) {
323 if (u
->restore_device
&&
325 (s
= pa_namereg_get(c
, e
->device
, PA_NAMEREG_SOURCE
, TRUE
))) {
327 pa_log_info("Restoring device for stream %s.", name
);
328 new_data
->source
= s
;
339 int pa__init(pa_module
*m
) {
340 pa_modargs
*ma
= NULL
;
345 pa_source_output
*so
;
347 pa_bool_t restore_device
= TRUE
, restore_volume
= TRUE
, restore_muted
= TRUE
;
351 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
352 pa_log("Failed to parse module arguments");
356 if (pa_modargs_get_value_boolean(ma
, "restore_device", &restore_device
) < 0 ||
357 pa_modargs_get_value_boolean(ma
, "restore_volume", &restore_volume
) < 0 ||
358 pa_modargs_get_value_boolean(ma
, "restore_muted", &restore_muted
) < 0) {
359 pa_log("restore_device=, restore_volume= and restore_muted= expect boolean arguments");
363 if (!restore_muted
&& !restore_volume
&& !restore_device
)
364 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
366 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
369 u
->save_time_event
= NULL
;
370 u
->restore_device
= restore_device
;
371 u
->restore_volume
= restore_volume
;
372 u
->restore_muted
= restore_muted
;
374 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK_INPUT
|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT
, subscribe_callback
, u
);
376 if (restore_device
) {
377 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
);
378 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
);
381 if (restore_volume
|| restore_muted
)
382 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
);
384 if (!pa_get_host_name(hn
, sizeof(hn
)))
387 fn
= pa_sprintf_malloc("stream-volumes.%s."CANONICAL_HOST
".gdbm", hn
);
388 fname
= pa_state_path(fn
);
394 if (!(u
->gdbm_file
= gdbm_open(fname
, 0, GDBM_WRCREAT
, 0600, NULL
))) {
395 pa_log("Failed to open volume database '%s': %s", fname
, gdbm_strerror(gdbm_errno
));
400 pa_log_info("Sucessfully opened database file '%s'.", fname
);
403 for (si
= pa_idxset_first(m
->core
->sink_inputs
, &idx
); si
; si
= pa_idxset_next(m
->core
->sink_inputs
, &idx
))
404 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, si
->index
, u
);
406 for (so
= pa_idxset_first(m
->core
->source_outputs
, &idx
); so
; so
= pa_idxset_next(m
->core
->source_outputs
, &idx
))
407 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, so
->index
, u
);
421 void pa__done(pa_module
*m
) {
426 if (!(u
= m
->userdata
))
430 pa_subscription_free(u
->subscription
);
432 if (u
->sink_input_new_hook_slot
)
433 pa_hook_slot_free(u
->sink_input_new_hook_slot
);
434 if (u
->sink_input_fixate_hook_slot
)
435 pa_hook_slot_free(u
->sink_input_fixate_hook_slot
);
436 if (u
->source_output_new_hook_slot
)
437 pa_hook_slot_free(u
->source_output_new_hook_slot
);
439 if (u
->save_time_event
)
440 u
->core
->mainloop
->time_free(u
->save_time_event
);
443 gdbm_close(u
->gdbm_file
);