2 This file is part of PulseAudio.
4 Copyright 2006 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-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 #define SAVE_INTERVAL 10
59 static const char* const valid_modargs
[] = {
65 pa_subscription
*subscription
;
66 pa_hook_slot
*sink_fixate_hook_slot
, *source_fixate_hook_slot
;
67 pa_time_event
*save_time_event
;
76 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
77 struct userdata
*u
= userdata
;
84 pa_assert(e
== u
->save_time_event
);
85 u
->core
->mainloop
->time_free(u
->save_time_event
);
86 u
->save_time_event
= NULL
;
88 gdbm_sync(u
->gdbm_file
);
89 pa_log_info("Synced.");
92 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
93 struct userdata
*u
= userdata
;
101 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
) &&
102 t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
103 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
) &&
104 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
))
107 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK
) {
110 if (!(sink
= pa_idxset_get_by_index(c
->sinks
, idx
)))
113 name
= pa_sprintf_malloc("sink:%s", sink
->name
);
114 entry
.volume
= *pa_sink_get_volume(sink
);
115 entry
.muted
= pa_sink_get_mute(sink
);
120 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE
);
122 if (!(source
= pa_idxset_get_by_index(c
->sources
, idx
)))
125 name
= pa_sprintf_malloc("source:%s", source
->name
);
126 entry
.volume
= *pa_source_get_volume(source
);
127 entry
.muted
= pa_source_get_mute(source
);
131 key
.dsize
= strlen(name
);
133 data
= gdbm_fetch(u
->gdbm_file
, key
);
137 if (data
.dsize
== sizeof(struct entry
)) {
138 struct entry
*old
= (struct entry
*) data
.dptr
;
140 if (pa_cvolume_valid(&old
->volume
)) {
142 if (pa_cvolume_equal(&old
->volume
, &entry
.volume
) &&
143 !old
->muted
== !entry
.muted
) {
150 pa_log_warn("Invalid volume stored in database for device %s", name
);
153 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
158 data
.dptr
= (void*) &entry
;
159 data
.dsize
= sizeof(entry
);
161 pa_log_info("Storing volume/mute for device %s.", name
);
163 gdbm_store(u
->gdbm_file
, key
, data
, GDBM_REPLACE
);
165 if (!u
->save_time_event
) {
167 pa_gettimeofday(&tv
);
168 tv
.tv_sec
+= SAVE_INTERVAL
;
169 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
175 static struct entry
* read_entry(struct userdata
*u
, char *name
) {
183 key
.dsize
= strlen(name
);
185 data
= gdbm_fetch(u
->gdbm_file
, key
);
190 if (data
.dsize
!= sizeof(struct entry
)) {
191 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name
, (unsigned long) data
.dsize
, (unsigned long) sizeof(struct entry
));
195 e
= (struct entry
*) data
.dptr
;
197 if (!(pa_cvolume_valid(&e
->volume
))) {
198 pa_log_warn("Invalid volume stored in database for device %s", name
);
211 static pa_hook_result_t
sink_fixate_hook_callback(pa_core
*c
, pa_sink_new_data
*new_data
, struct userdata
*u
) {
217 name
= pa_sprintf_malloc("sink:%s", new_data
->name
);
219 if ((e
= read_entry(u
, name
))) {
221 if (e
->volume
.channels
== new_data
->sample_spec
.channels
) {
222 pa_log_info("Restoring volume for sink %s.", new_data
->name
);
223 pa_sink_new_data_set_volume(new_data
, &e
->volume
);
226 pa_log_info("Restoring mute state for sink %s.", new_data
->name
);
227 pa_sink_new_data_set_muted(new_data
, e
->muted
);
236 static pa_hook_result_t
source_fixate_hook_callback(pa_core
*c
, pa_source_new_data
*new_data
, struct userdata
*u
) {
242 name
= pa_sprintf_malloc("source:%s", new_data
->name
);
244 if ((e
= read_entry(u
, name
))) {
246 if (e
->volume
.channels
== new_data
->sample_spec
.channels
) {
247 pa_log_info("Restoring volume for source %s.", new_data
->name
);
248 pa_source_new_data_set_volume(new_data
, &e
->volume
);
251 pa_log_info("Restoring mute state for source %s.", new_data
->name
);
252 pa_source_new_data_set_muted(new_data
, e
->muted
);
261 int pa__init(pa_module
*m
) {
262 pa_modargs
*ma
= NULL
;
272 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
273 pa_log("Failed to parse module arguments");
277 u
= pa_xnew(struct userdata
, 1);
279 u
->save_time_event
= NULL
;
281 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK
|PA_SUBSCRIPTION_MASK_SOURCE
, subscribe_callback
, u
);
283 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
);
284 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
);
288 if (!pa_get_host_name(hn
, sizeof(hn
)))
291 fn
= pa_sprintf_malloc("device-volumes.%s.gdbm", hn
);
292 fname
= pa_state_path(fn
);
298 if (!(u
->gdbm_file
= gdbm_open(fname
, 0, GDBM_WRCREAT
, 0600, NULL
))) {
299 pa_log("Failed to open volume database '%s': %s", fname
, gdbm_strerror(gdbm_errno
));
304 pa_log_info("Sucessfully opened database file '%s'.", fname
);
307 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
308 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
, sink
->index
, u
);
310 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
311 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
, source
->index
, u
);
325 void pa__done(pa_module
*m
) {
330 if (!(u
= m
->userdata
))
334 pa_subscription_free(u
->subscription
);
336 if (u
->sink_fixate_hook_slot
)
337 pa_hook_slot_free(u
->sink_fixate_hook_slot
);
338 if (u
->source_fixate_hook_slot
)
339 pa_hook_slot_free(u
->source_fixate_hook_slot
);
341 if (u
->save_time_event
)
342 u
->core
->mainloop
->time_free(u
->save_time_event
);
345 gdbm_close(u
->gdbm_file
);