]> code.delx.au - pulseaudio/blob - src/modules/module-device-restore.c
Merge commit 'origin/master-tx'
[pulseaudio] / src / modules / module-device-restore.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006-2008 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 PA_MODULE_USAGE(
57 "restore_volume=<Save/restore volumes?> "
58 "restore_muted=<Save/restore muted states?>");
59
60 #define SAVE_INTERVAL 10
61
62 static const char* const valid_modargs[] = {
63 "restore_volume",
64 "restore_muted",
65 NULL
66 };
67
68 struct userdata {
69 pa_core *core;
70 pa_module *module;
71 pa_subscription *subscription;
72 pa_hook_slot
73 *sink_fixate_hook_slot,
74 *source_fixate_hook_slot;
75 pa_time_event *save_time_event;
76 GDBM_FILE gdbm_file;
77
78 pa_bool_t restore_volume:1;
79 pa_bool_t restore_muted:1;
80 };
81
82 struct entry {
83 pa_channel_map channel_map;
84 pa_cvolume volume;
85 pa_bool_t muted:1;
86 };
87
88 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
89 struct userdata *u = userdata;
90
91 pa_assert(a);
92 pa_assert(e);
93 pa_assert(tv);
94 pa_assert(u);
95
96 pa_assert(e == u->save_time_event);
97 u->core->mainloop->time_free(u->save_time_event);
98 u->save_time_event = NULL;
99
100 gdbm_sync(u->gdbm_file);
101 pa_log_info("Synced.");
102 }
103
104 static struct entry* read_entry(struct userdata *u, const char *name) {
105 datum key, data;
106 struct entry *e;
107
108 pa_assert(u);
109 pa_assert(name);
110
111 key.dptr = (char*) name;
112 key.dsize = (int) strlen(name);
113
114 data = gdbm_fetch(u->gdbm_file, key);
115
116 if (!data.dptr)
117 goto fail;
118
119 if (data.dsize != sizeof(struct entry)) {
120 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
121 goto fail;
122 }
123
124 e = (struct entry*) data.dptr;
125
126 if (!(pa_cvolume_valid(&e->volume))) {
127 pa_log_warn("Invalid volume stored in database for device %s", name);
128 goto fail;
129 }
130
131 if (!(pa_channel_map_valid(&e->channel_map))) {
132 pa_log_warn("Invalid channel map stored in database for device %s", name);
133 goto fail;
134 }
135
136 if (e->volume.channels != e->channel_map.channels) {
137 pa_log_warn("Volume and channel map don't match in database entry for device %s", name);
138 goto fail;
139 }
140
141 return e;
142
143 fail:
144
145 pa_xfree(data.dptr);
146 return NULL;
147 }
148
149 static void trigger_save(struct userdata *u) {
150 struct timeval tv;
151
152 if (u->save_time_event)
153 return;
154
155 pa_gettimeofday(&tv);
156 tv.tv_sec += SAVE_INTERVAL;
157 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
158 }
159
160 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
161 struct userdata *u = userdata;
162 struct entry entry, *old;
163 char *name;
164 datum key, data;
165
166 pa_assert(c);
167 pa_assert(u);
168
169 if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
170 t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
171 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
172 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
173 return;
174
175 memset(&entry, 0, sizeof(entry));
176
177 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
178 pa_sink *sink;
179
180 if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
181 return;
182
183 name = pa_sprintf_malloc("sink:%s", sink->name);
184 entry.channel_map = sink->channel_map;
185 entry.volume = *pa_sink_get_volume(sink, FALSE);
186 entry.muted = pa_sink_get_mute(sink, FALSE);
187
188 } else {
189 pa_source *source;
190
191 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
192
193 if (!(source = pa_idxset_get_by_index(c->sources, idx)))
194 return;
195
196 name = pa_sprintf_malloc("source:%s", source->name);
197 entry.channel_map = source->channel_map;
198 entry.volume = *pa_source_get_volume(source, FALSE);
199 entry.muted = pa_source_get_mute(source, FALSE);
200 }
201
202 if ((old = read_entry(u, name))) {
203
204 if (pa_cvolume_equal(pa_cvolume_remap(&old->volume, &old->channel_map, &entry.channel_map), &entry.volume) &&
205 !old->muted == !entry.muted) {
206
207 pa_xfree(old);
208 pa_xfree(name);
209 return;
210 }
211
212 pa_xfree(old);
213 }
214
215 key.dptr = name;
216 key.dsize = (int) strlen(name);
217
218 data.dptr = (void*) &entry;
219 data.dsize = sizeof(entry);
220
221 pa_log_info("Storing volume/mute for device %s.", name);
222
223 gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
224
225 pa_xfree(name);
226
227 trigger_save(u);
228 }
229
230 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
231 char *name;
232 struct entry *e;
233
234 pa_assert(new_data);
235
236 name = pa_sprintf_malloc("sink:%s", new_data->name);
237
238 if ((e = read_entry(u, name))) {
239
240 if (u->restore_volume) {
241
242 if (!new_data->volume_is_set) {
243 pa_log_info("Restoring volume for sink %s.", new_data->name);
244 pa_sink_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
245 } else
246 pa_log_debug("Not restoring volume for sink %s, because already set.", new_data->name);
247
248 }
249
250 if (u->restore_muted) {
251
252 if (!new_data->muted_is_set) {
253 pa_log_info("Restoring mute state for sink %s.", new_data->name);
254 pa_sink_new_data_set_muted(new_data, e->muted);
255 } else
256 pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name);
257 }
258
259 pa_xfree(e);
260 }
261
262 pa_xfree(name);
263
264 return PA_HOOK_OK;
265 }
266
267 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
268 char *name;
269 struct entry *e;
270
271 pa_assert(new_data);
272
273 name = pa_sprintf_malloc("source:%s", new_data->name);
274
275 if ((e = read_entry(u, name))) {
276
277 if (u->restore_volume) {
278
279 if (!new_data->volume_is_set) {
280 pa_log_info("Restoring volume for source %s.", new_data->name);
281 pa_source_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
282 } else
283 pa_log_debug("Not restoring volume for source %s, because already set.", new_data->name);
284 }
285
286 if (u->restore_muted) {
287
288 if (!new_data->muted_is_set) {
289 pa_log_info("Restoring mute state for source %s.", new_data->name);
290 pa_source_new_data_set_muted(new_data, e->muted);
291 } else
292 pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name);
293 }
294
295 pa_xfree(e);
296 }
297
298 pa_xfree(name);
299
300 return PA_HOOK_OK;
301 }
302
303 int pa__init(pa_module*m) {
304 pa_modargs *ma = NULL;
305 struct userdata *u;
306 char *fname, *fn;
307 pa_sink *sink;
308 pa_source *source;
309 uint32_t idx;
310 pa_bool_t restore_volume = TRUE, restore_muted = TRUE;
311 int gdbm_cache_size;
312
313 pa_assert(m);
314
315 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
316 pa_log("Failed to parse module arguments");
317 goto fail;
318 }
319
320 if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
321 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
322 pa_log("restore_volume= and restore_muted= expect boolean arguments");
323 goto fail;
324 }
325
326 if (!restore_muted && !restore_volume)
327 pa_log_warn("Neither restoring volume nor restoring muted enabled!");
328
329 m->userdata = u = pa_xnew(struct userdata, 1);
330 u->core = m->core;
331 u->module = m;
332 u->save_time_event = NULL;
333 u->restore_volume = restore_volume;
334 u->restore_muted = restore_muted;
335 u->gdbm_file = NULL;
336
337 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
338
339 if (restore_muted || restore_volume) {
340 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);
341 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);
342 }
343
344 /* We include the host identifier in the file name because gdbm
345 * files are CPU dependant, and we don't want things to go wrong
346 * if we are on a multiarch system. */
347
348 fn = pa_sprintf_malloc("device-volumes."CANONICAL_HOST".gdbm");
349 fname = pa_state_path(fn, TRUE);
350 pa_xfree(fn);
351
352 if (!fname)
353 goto fail;
354
355 if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) {
356 pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
357 pa_xfree(fname);
358 goto fail;
359 }
360
361 /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
362 gdbm_cache_size = 10;
363 gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
364
365 pa_log_info("Sucessfully opened database file '%s'.", fname);
366 pa_xfree(fname);
367
368 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
369 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
370
371 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
372 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
373
374 pa_modargs_free(ma);
375 return 0;
376
377 fail:
378 pa__done(m);
379
380 if (ma)
381 pa_modargs_free(ma);
382
383 return -1;
384 }
385
386 void pa__done(pa_module*m) {
387 struct userdata* u;
388
389 pa_assert(m);
390
391 if (!(u = m->userdata))
392 return;
393
394 if (u->subscription)
395 pa_subscription_free(u->subscription);
396
397 if (u->sink_fixate_hook_slot)
398 pa_hook_slot_free(u->sink_fixate_hook_slot);
399 if (u->source_fixate_hook_slot)
400 pa_hook_slot_free(u->source_fixate_hook_slot);
401
402 if (u->save_time_event)
403 u->core->mainloop->time_free(u->save_time_event);
404
405 if (u->gdbm_file)
406 gdbm_close(u->gdbm_file);
407
408 pa_xfree(u);
409 }