]> code.delx.au - pulseaudio/blob - src/modules/module-device-restore.c
Merge dead branch 'glitch-free'
[pulseaudio] / src / modules / module-device-restore.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <gdbm.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
39
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>
49
50 #include "module-device-restore-symdef.h"
51
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);
56
57 #define SAVE_INTERVAL 10
58
59 static const char* const valid_modargs[] = {
60 NULL,
61 };
62
63 struct userdata {
64 pa_core *core;
65 pa_subscription *subscription;
66 pa_hook_slot *sink_fixate_hook_slot, *source_fixate_hook_slot;
67 pa_time_event *save_time_event;
68 GDBM_FILE gdbm_file;
69 };
70
71 struct entry {
72 pa_cvolume volume;
73 int muted;
74 };
75
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;
78
79 pa_assert(a);
80 pa_assert(e);
81 pa_assert(tv);
82 pa_assert(u);
83
84 pa_assert(e == u->save_time_event);
85 u->core->mainloop->time_free(u->save_time_event);
86 u->save_time_event = NULL;
87
88 gdbm_sync(u->gdbm_file);
89 pa_log_info("Synced.");
90 }
91
92 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
93 struct userdata *u = userdata;
94 struct entry entry;
95 char *name;
96 datum key, data;
97
98 pa_assert(c);
99 pa_assert(u);
100
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))
105 return;
106
107 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
108 pa_sink *sink;
109
110 if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
111 return;
112
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);
116
117 } else {
118 pa_source *source;
119
120 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
121
122 if (!(source = pa_idxset_get_by_index(c->sources, idx)))
123 return;
124
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);
128 }
129
130 key.dptr = name;
131 key.dsize = strlen(name);
132
133 data = gdbm_fetch(u->gdbm_file, key);
134
135 if (data.dptr) {
136
137 if (data.dsize == sizeof(struct entry)) {
138 struct entry *old = (struct entry*) data.dptr;
139
140 if (pa_cvolume_valid(&old->volume)) {
141
142 if (pa_cvolume_equal(&old->volume, &entry.volume) &&
143 !old->muted == !entry.muted) {
144
145 pa_xfree(data.dptr);
146 pa_xfree(name);
147 return;
148 }
149 } else
150 pa_log_warn("Invalid volume stored in database for device %s", name);
151
152 } else
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));
154
155 pa_xfree(data.dptr);
156 }
157
158 data.dptr = (void*) &entry;
159 data.dsize = sizeof(entry);
160
161 pa_log_info("Storing volume/mute for device %s.", name);
162
163 gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
164
165 if (!u->save_time_event) {
166 struct timeval tv;
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);
170 }
171
172 pa_xfree(name);
173 }
174
175 static struct entry* read_entry(struct userdata *u, char *name) {
176 datum key, data;
177 struct entry *e;
178
179 pa_assert(u);
180 pa_assert(name);
181
182 key.dptr = name;
183 key.dsize = strlen(name);
184
185 data = gdbm_fetch(u->gdbm_file, key);
186
187 if (!data.dptr)
188 goto fail;
189
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));
192 goto fail;
193 }
194
195 e = (struct entry*) data.dptr;
196
197 if (!(pa_cvolume_valid(&e->volume))) {
198 pa_log_warn("Invalid volume stored in database for device %s", name);
199 goto fail;
200 }
201
202 return e;
203
204 fail:
205
206 pa_xfree(data.dptr);
207 return NULL;
208 }
209
210
211 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
212 char *name;
213 struct entry *e;
214
215 pa_assert(new_data);
216
217 name = pa_sprintf_malloc("sink:%s", new_data->name);
218
219 if ((e = read_entry(u, name))) {
220
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);
224 }
225
226 pa_log_info("Restoring mute state for sink %s.", new_data->name);
227 pa_sink_new_data_set_muted(new_data, e->muted);
228 pa_xfree(e);
229 }
230
231 pa_xfree(name);
232
233 return PA_HOOK_OK;
234 }
235
236 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
237 char *name;
238 struct entry *e;
239
240 pa_assert(new_data);
241
242 name = pa_sprintf_malloc("source:%s", new_data->name);
243
244 if ((e = read_entry(u, name))) {
245
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);
249 }
250
251 pa_log_info("Restoring mute state for source %s.", new_data->name);
252 pa_source_new_data_set_muted(new_data, e->muted);
253 pa_xfree(e);
254 }
255
256 pa_xfree(name);
257
258 return PA_HOOK_OK;
259 }
260
261 int pa__init(pa_module*m) {
262 pa_modargs *ma = NULL;
263 struct userdata *u;
264 char *fname, *fn;
265 char hn[256];
266 pa_sink *sink;
267 pa_source *source;
268 uint32_t idx;
269
270 pa_assert(m);
271
272 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
273 pa_log("Failed to parse module arguments");
274 goto fail;
275 }
276
277 u = pa_xnew(struct userdata, 1);
278 u->core = m->core;
279 u->save_time_event = NULL;
280
281 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
282
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);
285
286 m->userdata = u;
287
288 if (!pa_get_host_name(hn, sizeof(hn)))
289 goto fail;
290
291 fn = pa_sprintf_malloc("device-volumes.%s.gdbm", hn);
292 fname = pa_state_path(fn);
293 pa_xfree(fn);
294
295 if (!fname)
296 goto fail;
297
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));
300 pa_xfree(fname);
301 goto fail;
302 }
303
304 pa_log_info("Sucessfully opened database file '%s'.", fname);
305 pa_xfree(fname);
306
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);
309
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);
312
313 pa_modargs_free(ma);
314 return 0;
315
316 fail:
317 pa__done(m);
318
319 if (ma)
320 pa_modargs_free(ma);
321
322 return -1;
323 }
324
325 void pa__done(pa_module*m) {
326 struct userdata* u;
327
328 pa_assert(m);
329
330 if (!(u = m->userdata))
331 return;
332
333 if (u->subscription)
334 pa_subscription_free(u->subscription);
335
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);
340
341 if (u->save_time_event)
342 u->core->mainloop->time_free(u->save_time_event);
343
344 if (u->gdbm_file)
345 gdbm_close(u->gdbm_file);
346
347 pa_xfree(u);
348 }