4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/types.h>
37 #include <pulse/xmalloc.h>
38 #include <pulse/volume.h>
39 #include <pulse/timeval.h>
40 #include <pulse/util.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/core-subscribe.h>
48 #include <pulsecore/sink-input.h>
49 #include <pulsecore/source-output.h>
50 #include <pulsecore/namereg.h>
52 #include "module-device-restore-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices");
56 PA_MODULE_VERSION(PACKAGE_VERSION
);
57 PA_MODULE_LOAD_ONCE(TRUE
);
59 #define SAVE_INTERVAL 10
61 static const char* const valid_modargs
[] = {
67 pa_subscription
*subscription
;
68 pa_hook_slot
*sink_fixate_hook_slot
, *source_fixate_hook_slot
;
69 pa_time_event
*save_time_event
;
78 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
79 struct userdata
*u
= userdata
;
86 pa_assert(e
== u
->save_time_event
);
87 u
->core
->mainloop
->time_free(u
->save_time_event
);
88 u
->save_time_event
= NULL
;
90 gdbm_sync(u
->gdbm_file
);
91 pa_log_info("Synced.");
94 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
95 struct userdata
*u
= userdata
;
103 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
) &&
104 t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
105 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
) &&
106 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
))
109 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK
) {
112 if (!(sink
= pa_idxset_get_by_index(c
->sinks
, idx
)))
115 name
= pa_sprintf_malloc("sink:%s", sink
->name
);
116 entry
.volume
= *pa_sink_get_volume(sink
);
117 entry
.muted
= pa_sink_get_mute(sink
);
122 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE
);
124 if (!(source
= pa_idxset_get_by_index(c
->sources
, idx
)))
127 name
= pa_sprintf_malloc("source:%s", source
->name
);
128 entry
.volume
= *pa_source_get_volume(source
);
129 entry
.muted
= pa_source_get_mute(source
);
133 key
.dsize
= strlen(name
);
135 data
= gdbm_fetch(u
->gdbm_file
, key
);
139 if (data
.dsize
== sizeof(struct entry
)) {
140 struct entry
*old
= (struct entry
*) data
.dptr
;
142 if (pa_cvolume_valid(&old
->volume
)) {
144 if (pa_cvolume_equal(&old
->volume
, &entry
.volume
) &&
145 !old
->muted
== !entry
.muted
) {
152 pa_log_warn("Invalid volume stored in database for device %s", name
);
155 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
160 data
.dptr
= (void*) &entry
;
161 data
.dsize
= sizeof(entry
);
163 pa_log_info("Storing volume/mute for device %s.", name
);
165 gdbm_store(u
->gdbm_file
, key
, data
, GDBM_REPLACE
);
167 if (!u
->save_time_event
) {
169 pa_gettimeofday(&tv
);
170 tv
.tv_sec
+= SAVE_INTERVAL
;
171 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
177 static struct entry
* read_entry(struct userdata
*u
, char *name
) {
185 key
.dsize
= strlen(name
);
187 data
= gdbm_fetch(u
->gdbm_file
, key
);
192 if (data
.dsize
!= sizeof(struct entry
)) {
193 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
197 e
= (struct entry
*) data
.dptr
;
199 if (!(pa_cvolume_valid(&e
->volume
))) {
200 pa_log_warn("Invalid volume stored in database for device %s", name
);
213 static pa_hook_result_t
sink_fixate_hook_callback(pa_core
*c
, pa_sink_new_data
*new_data
, struct userdata
*u
) {
219 name
= pa_sprintf_malloc("sink:%s", new_data
->name
);
221 if ((e
= read_entry(u
, name
))) {
223 if (e
->volume
.channels
== new_data
->sample_spec
.channels
) {
224 pa_log_info("Restoring volume for sink %s.", new_data
->name
);
225 pa_sink_new_data_set_volume(new_data
, &e
->volume
);
228 pa_log_info("Restoring mute state for sink %s.", new_data
->name
);
229 pa_sink_new_data_set_muted(new_data
, e
->muted
);
238 static pa_hook_result_t
source_fixate_hook_callback(pa_core
*c
, pa_source_new_data
*new_data
, struct userdata
*u
) {
244 name
= pa_sprintf_malloc("source:%s", new_data
->name
);
246 if ((e
= read_entry(u
, name
))) {
248 if (e
->volume
.channels
== new_data
->sample_spec
.channels
) {
249 pa_log_info("Restoring volume for source %s.", new_data
->name
);
250 pa_source_new_data_set_volume(new_data
, &e
->volume
);
253 pa_log_info("Restoring mute state for source %s.", new_data
->name
);
254 pa_source_new_data_set_muted(new_data
, e
->muted
);
263 int pa__init(pa_module
*m
) {
264 pa_modargs
*ma
= NULL
;
266 char *fname
, *runtime_dir
;
274 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
275 pa_log("Failed to parse module arguments");
279 u
= pa_xnew(struct userdata
, 1);
281 u
->save_time_event
= NULL
;
283 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK
|PA_SUBSCRIPTION_MASK_SOURCE
, subscribe_callback
, u
);
285 u
->sink_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], (pa_hook_cb_t
) sink_fixate_hook_callback
, u
);
286 u
->source_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_FIXATE
], (pa_hook_cb_t
) source_fixate_hook_callback
, u
);
290 if (!pa_get_host_name(hn
, sizeof(hn
)))
293 if (!(runtime_dir
= pa_get_runtime_dir()))
296 fname
= pa_sprintf_malloc("%s/device-volumes.%s.gdbm", runtime_dir
, hn
);
297 pa_xfree(runtime_dir
);
299 if (!(u
->gdbm_file
= gdbm_open(fname
, 0, GDBM_WRCREAT
, 0600, NULL
))) {
300 pa_log("Failed to open volume database '%s': %s", fname
, gdbm_strerror(gdbm_errno
));
305 pa_log_info("Sucessfully opened database file '%s'.", fname
);
308 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
309 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
, sink
->index
, u
);
311 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
312 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
, source
->index
, u
);
326 void pa__done(pa_module
*m
) {
331 if (!(u
= m
->userdata
))
335 pa_subscription_free(u
->subscription
);
337 if (u
->sink_fixate_hook_slot
)
338 pa_hook_slot_free(u
->sink_fixate_hook_slot
);
339 if (u
->source_fixate_hook_slot
)
340 pa_hook_slot_free(u
->source_fixate_hook_slot
);
342 if (u
->save_time_event
)
343 u
->core
->mainloop
->time_free(u
->save_time_event
);
346 gdbm_close(u
->gdbm_file
);