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