2 This file is part of PulseAudio.
4 Copyright 2006-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>
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-device-restore-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices");
54 PA_MODULE_VERSION(PACKAGE_VERSION
);
55 PA_MODULE_LOAD_ONCE(TRUE
);
57 "restore_volume=<Save/restore volumes?> "
58 "restore_muted=<Save/restore muted states?>");
60 #define SAVE_INTERVAL 10
62 static const char* const valid_modargs
[] = {
71 pa_subscription
*subscription
;
73 *sink_fixate_hook_slot
,
74 *source_fixate_hook_slot
;
75 pa_time_event
*save_time_event
;
78 pa_bool_t restore_volume
:1;
79 pa_bool_t restore_muted
:1;
82 #define ENTRY_VERSION 1
87 pa_channel_map channel_map
;
91 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
92 struct userdata
*u
= userdata
;
99 pa_assert(e
== u
->save_time_event
);
100 u
->core
->mainloop
->time_free(u
->save_time_event
);
101 u
->save_time_event
= NULL
;
103 gdbm_sync(u
->gdbm_file
);
104 pa_log_info("Synced.");
107 static struct entry
* read_entry(struct userdata
*u
, const char *name
) {
114 key
.dptr
= (char*) name
;
115 key
.dsize
= (int) strlen(name
);
117 data
= gdbm_fetch(u
->gdbm_file
, key
);
122 if (data
.dsize
!= sizeof(struct entry
)) {
123 pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
127 e
= (struct entry
*) data
.dptr
;
129 if (e
->version
!= ENTRY_VERSION
) {
130 pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name
);
134 if (!(pa_cvolume_valid(&e
->volume
))) {
135 pa_log_warn("Invalid volume stored in database for device %s", name
);
139 if (!(pa_channel_map_valid(&e
->channel_map
))) {
140 pa_log_warn("Invalid channel map stored in database for device %s", name
);
144 if (e
->volume
.channels
!= e
->channel_map
.channels
) {
145 pa_log_warn("Volume and channel map don't match in database entry for device %s", name
);
157 static void trigger_save(struct userdata
*u
) {
160 if (u
->save_time_event
)
163 pa_gettimeofday(&tv
);
164 tv
.tv_sec
+= SAVE_INTERVAL
;
165 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
168 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
169 struct userdata
*u
= userdata
;
170 struct entry entry
, *old
;
177 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
) &&
178 t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
179 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
) &&
180 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
))
183 memset(&entry
, 0, sizeof(entry
));
184 entry
.version
= ENTRY_VERSION
;
186 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK
) {
189 if (!(sink
= pa_idxset_get_by_index(c
->sinks
, idx
)))
192 name
= pa_sprintf_malloc("sink:%s", sink
->name
);
193 entry
.channel_map
= sink
->channel_map
;
194 entry
.volume
= *pa_sink_get_volume(sink
, FALSE
);
195 entry
.muted
= pa_sink_get_mute(sink
, FALSE
);
200 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE
);
202 if (!(source
= pa_idxset_get_by_index(c
->sources
, idx
)))
205 name
= pa_sprintf_malloc("source:%s", source
->name
);
206 entry
.channel_map
= source
->channel_map
;
207 entry
.volume
= *pa_source_get_volume(source
, FALSE
);
208 entry
.muted
= pa_source_get_mute(source
, FALSE
);
211 if ((old
= read_entry(u
, name
))) {
213 if (pa_cvolume_equal(pa_cvolume_remap(&old
->volume
, &old
->channel_map
, &entry
.channel_map
), &entry
.volume
) &&
214 !old
->muted
== !entry
.muted
) {
225 key
.dsize
= (int) strlen(name
);
227 data
.dptr
= (void*) &entry
;
228 data
.dsize
= sizeof(entry
);
230 pa_log_info("Storing volume/mute for device %s.", name
);
232 gdbm_store(u
->gdbm_file
, key
, data
, GDBM_REPLACE
);
239 static pa_hook_result_t
sink_fixate_hook_callback(pa_core
*c
, pa_sink_new_data
*new_data
, struct userdata
*u
) {
245 name
= pa_sprintf_malloc("sink:%s", new_data
->name
);
247 if ((e
= read_entry(u
, name
))) {
249 if (u
->restore_volume
) {
251 if (!new_data
->volume_is_set
) {
252 pa_log_info("Restoring volume for sink %s.", new_data
->name
);
253 pa_sink_new_data_set_volume(new_data
, pa_cvolume_remap(&e
->volume
, &e
->channel_map
, &new_data
->channel_map
));
255 pa_log_debug("Not restoring volume for sink %s, because already set.", new_data
->name
);
259 if (u
->restore_muted
) {
261 if (!new_data
->muted_is_set
) {
262 pa_log_info("Restoring mute state for sink %s.", new_data
->name
);
263 pa_sink_new_data_set_muted(new_data
, e
->muted
);
265 pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data
->name
);
276 static pa_hook_result_t
source_fixate_hook_callback(pa_core
*c
, pa_source_new_data
*new_data
, struct userdata
*u
) {
282 name
= pa_sprintf_malloc("source:%s", new_data
->name
);
284 if ((e
= read_entry(u
, name
))) {
286 if (u
->restore_volume
) {
288 if (!new_data
->volume_is_set
) {
289 pa_log_info("Restoring volume for source %s.", new_data
->name
);
290 pa_source_new_data_set_volume(new_data
, pa_cvolume_remap(&e
->volume
, &e
->channel_map
, &new_data
->channel_map
));
292 pa_log_debug("Not restoring volume for source %s, because already set.", new_data
->name
);
295 if (u
->restore_muted
) {
297 if (!new_data
->muted_is_set
) {
298 pa_log_info("Restoring mute state for source %s.", new_data
->name
);
299 pa_source_new_data_set_muted(new_data
, e
->muted
);
301 pa_log_debug("Not restoring mute state for source %s, because already set.", new_data
->name
);
312 int pa__init(pa_module
*m
) {
313 pa_modargs
*ma
= NULL
;
319 pa_bool_t restore_volume
= TRUE
, restore_muted
= TRUE
;
324 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
325 pa_log("Failed to parse module arguments");
329 if (pa_modargs_get_value_boolean(ma
, "restore_volume", &restore_volume
) < 0 ||
330 pa_modargs_get_value_boolean(ma
, "restore_muted", &restore_muted
) < 0) {
331 pa_log("restore_volume= and restore_muted= expect boolean arguments");
335 if (!restore_muted
&& !restore_volume
)
336 pa_log_warn("Neither restoring volume nor restoring muted enabled!");
338 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
341 u
->save_time_event
= NULL
;
342 u
->restore_volume
= restore_volume
;
343 u
->restore_muted
= restore_muted
;
346 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK
|PA_SUBSCRIPTION_MASK_SOURCE
, subscribe_callback
, u
);
348 if (restore_muted
|| restore_volume
) {
349 u
->sink_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], PA_HOOK_EARLY
, (pa_hook_cb_t
) sink_fixate_hook_callback
, u
);
350 u
->source_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_FIXATE
], PA_HOOK_EARLY
, (pa_hook_cb_t
) source_fixate_hook_callback
, u
);
353 /* We include the host identifier in the file name because gdbm
354 * files are CPU dependant, and we don't want things to go wrong
355 * if we are on a multiarch system. */
357 fn
= pa_sprintf_malloc("device-volumes."CANONICAL_HOST
".gdbm");
358 fname
= pa_state_path(fn
, TRUE
);
364 if (!(u
->gdbm_file
= gdbm_open(fname
, 0, GDBM_WRCREAT
|GDBM_NOLOCK
, 0600, NULL
))) {
365 pa_log("Failed to open volume database '%s': %s", fname
, gdbm_strerror(gdbm_errno
));
370 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
371 gdbm_cache_size
= 10;
372 gdbm_setopt(u
->gdbm_file
, GDBM_CACHESIZE
, &gdbm_cache_size
, sizeof(gdbm_cache_size
));
374 pa_log_info("Sucessfully opened database file '%s'.", fname
);
377 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
378 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
, sink
->index
, u
);
380 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
381 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
, source
->index
, u
);
395 void pa__done(pa_module
*m
) {
400 if (!(u
= m
->userdata
))
404 pa_subscription_free(u
->subscription
);
406 if (u
->sink_fixate_hook_slot
)
407 pa_hook_slot_free(u
->sink_fixate_hook_slot
);
408 if (u
->source_fixate_hook_slot
)
409 pa_hook_slot_free(u
->source_fixate_hook_slot
);
411 if (u
->save_time_event
)
412 u
->core
->mainloop
->time_free(u
->save_time_event
);
415 gdbm_close(u
->gdbm_file
);