]> code.delx.au - pulseaudio/blob - src/modules/module-device-restore.c
Merge branch 'master' of ssh://rootserver/home/lennart/git/public/pulseaudio
[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.1 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
34 #include <pulse/xmalloc.h>
35 #include <pulse/volume.h>
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/core-subscribe.h>
45 #include <pulsecore/sink-input.h>
46 #include <pulsecore/source-output.h>
47 #include <pulsecore/namereg.h>
48 #include <pulsecore/database.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_port=<Save/restore port?> "
58 "restore_volume=<Save/restore volumes?> "
59 "restore_muted=<Save/restore muted states?>");
60
61 #define SAVE_INTERVAL 10
62
63 static const char* const valid_modargs[] = {
64 "restore_volume",
65 "restore_muted",
66 "restore_port",
67 NULL
68 };
69
70 struct userdata {
71 pa_core *core;
72 pa_module *module;
73 pa_subscription *subscription;
74 pa_hook_slot
75 *sink_new_hook_slot,
76 *sink_fixate_hook_slot,
77 *source_new_hook_slot,
78 *source_fixate_hook_slot;
79 pa_time_event *save_time_event;
80 pa_database *database;
81
82 pa_bool_t restore_volume:1;
83 pa_bool_t restore_muted:1;
84 pa_bool_t restore_port:1;
85 };
86
87 #define ENTRY_VERSION 2
88
89 struct entry {
90 uint8_t version;
91 pa_bool_t muted_valid:1, volume_valid:1, port_valid:1;
92 pa_bool_t muted:1;
93 pa_channel_map channel_map;
94 pa_cvolume volume;
95 char port[PA_NAME_MAX];
96 } PA_GCC_PACKED;
97
98 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
99 struct userdata *u = userdata;
100
101 pa_assert(a);
102 pa_assert(e);
103 pa_assert(tv);
104 pa_assert(u);
105
106 pa_assert(e == u->save_time_event);
107 u->core->mainloop->time_free(u->save_time_event);
108 u->save_time_event = NULL;
109
110 pa_database_sync(u->database);
111 pa_log_info("Synced.");
112 }
113
114 static struct entry* read_entry(struct userdata *u, const char *name) {
115 pa_datum key, data;
116 struct entry *e;
117
118 pa_assert(u);
119 pa_assert(name);
120
121 key.data = (char*) name;
122 key.size = strlen(name);
123
124 pa_zero(data);
125
126 if (!pa_database_get(u->database, &key, &data))
127 goto fail;
128
129 if (data.size != sizeof(struct entry)) {
130 pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
131 goto fail;
132 }
133
134 e = (struct entry*) data.data;
135
136 if (e->version != ENTRY_VERSION) {
137 pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name);
138 goto fail;
139 }
140
141 if (!memchr(e->port, 0, sizeof(e->port))) {
142 pa_log_warn("Database contains entry for device %s with missing NUL byte in port name", name);
143 goto fail;
144 }
145
146 if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
147 pa_log_warn("Invalid channel map stored in database for device %s", name);
148 goto fail;
149 }
150
151 if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) {
152 pa_log_warn("Volume and channel map don't match in database entry for device %s", name);
153 goto fail;
154 }
155
156 return e;
157
158 fail:
159
160 pa_datum_free(&data);
161 return NULL;
162 }
163
164 static void trigger_save(struct userdata *u) {
165 struct timeval tv;
166
167 if (u->save_time_event)
168 return;
169
170 pa_gettimeofday(&tv);
171 tv.tv_sec += SAVE_INTERVAL;
172 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
173 }
174
175 static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
176 pa_cvolume t;
177
178 if (a->port_valid != b->port_valid ||
179 (a->port_valid && strncmp(a->port, b->port, sizeof(a->port))))
180 return FALSE;
181
182 if (a->muted_valid != b->muted_valid ||
183 (a->muted_valid && (a->muted != b->muted)))
184 return FALSE;
185
186 t = b->volume;
187 if (a->volume_valid != b->volume_valid ||
188 (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume)))
189 return FALSE;
190
191 return TRUE;
192 }
193
194 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
195 struct userdata *u = userdata;
196 struct entry entry, *old;
197 char *name;
198 pa_datum key, data;
199
200 pa_assert(c);
201 pa_assert(u);
202
203 if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
204 t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
205 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
206 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
207 return;
208
209 pa_zero(entry);
210 entry.version = ENTRY_VERSION;
211
212 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
213 pa_sink *sink;
214
215 if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
216 return;
217
218 name = pa_sprintf_malloc("sink:%s", sink->name);
219
220 if ((old = read_entry(u, name)))
221 entry = *old;
222
223 if (sink->save_volume) {
224 entry.channel_map = sink->channel_map;
225 entry.volume = *pa_sink_get_volume(sink, FALSE, TRUE);
226 entry.volume_valid = TRUE;
227 }
228
229 if (sink->save_muted) {
230 entry.muted = pa_sink_get_mute(sink, FALSE);
231 entry.muted_valid = TRUE;
232 }
233
234 if (sink->save_port) {
235 pa_strlcpy(entry.port, sink->active_port ? sink->active_port->name : "", sizeof(entry.port));
236 entry.port_valid = TRUE;
237 }
238
239 } else {
240 pa_source *source;
241
242 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
243
244 if (!(source = pa_idxset_get_by_index(c->sources, idx)))
245 return;
246
247 name = pa_sprintf_malloc("source:%s", source->name);
248
249 if ((old = read_entry(u, name)))
250 entry = *old;
251
252 if (source->save_volume) {
253 entry.channel_map = source->channel_map;
254 entry.volume = *pa_source_get_volume(source, FALSE);
255 entry.volume_valid = TRUE;
256 }
257
258 if (source->save_muted) {
259 entry.muted = pa_source_get_mute(source, FALSE);
260 entry.muted_valid = TRUE;
261 }
262
263 if (source->save_port) {
264 pa_strlcpy(entry.port, source->active_port ? source->active_port->name : "", sizeof(entry.port));
265 entry.port_valid = TRUE;
266 }
267 }
268
269 if (old) {
270
271 if (entries_equal(old, &entry)) {
272 pa_xfree(old);
273 pa_xfree(name);
274 return;
275 }
276
277 pa_xfree(old);
278 }
279
280 key.data = name;
281 key.size = strlen(name);
282
283 data.data = &entry;
284 data.size = sizeof(entry);
285
286 pa_log_info("Storing volume/mute/port for device %s.", name);
287
288 pa_database_set(u->database, &key, &data, TRUE);
289
290 pa_xfree(name);
291
292 trigger_save(u);
293 }
294
295 static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
296 char *name;
297 struct entry *e;
298
299 pa_assert(c);
300 pa_assert(new_data);
301 pa_assert(u);
302 pa_assert(u->restore_port);
303
304 name = pa_sprintf_malloc("sink:%s", new_data->name);
305
306 if ((e = read_entry(u, name))) {
307
308 if (e->port_valid) {
309 if (!new_data->active_port) {
310 pa_log_info("Restoring port for sink %s.", name);
311 pa_sink_new_data_set_port(new_data, e->port);
312 new_data->save_port = TRUE;
313 } else
314 pa_log_debug("Not restoring port for sink %s, because already set.", name);
315 }
316
317 pa_xfree(e);
318 }
319
320 pa_xfree(name);
321
322 return PA_HOOK_OK;
323 }
324
325 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
326 char *name;
327 struct entry *e;
328
329 pa_assert(c);
330 pa_assert(new_data);
331 pa_assert(u);
332 pa_assert(u->restore_volume || u->restore_muted);
333
334 name = pa_sprintf_malloc("sink:%s", new_data->name);
335
336 if ((e = read_entry(u, name))) {
337
338 if (u->restore_volume && e->volume_valid) {
339
340 if (!new_data->volume_is_set) {
341 pa_cvolume v;
342
343 pa_log_info("Restoring volume for sink %s.", new_data->name);
344
345 v = e->volume;
346 pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
347 pa_sink_new_data_set_volume(new_data, &v);
348
349 new_data->save_volume = TRUE;
350 } else
351 pa_log_debug("Not restoring volume for sink %s, because already set.", new_data->name);
352 }
353
354 if (u->restore_muted && e->muted_valid) {
355
356 if (!new_data->muted_is_set) {
357 pa_log_info("Restoring mute state for sink %s.", new_data->name);
358 pa_sink_new_data_set_muted(new_data, e->muted);
359 new_data->save_muted = TRUE;
360 } else
361 pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name);
362 }
363
364 pa_xfree(e);
365 }
366
367 pa_xfree(name);
368
369 return PA_HOOK_OK;
370 }
371
372 static pa_hook_result_t source_new_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
373 char *name;
374 struct entry *e;
375
376 pa_assert(c);
377 pa_assert(new_data);
378 pa_assert(u);
379 pa_assert(u->restore_port);
380
381 name = pa_sprintf_malloc("source:%s", new_data->name);
382
383 if ((e = read_entry(u, name))) {
384
385 if (e->port_valid) {
386 if (!new_data->active_port) {
387 pa_log_info("Restoring port for source %s.", name);
388 pa_source_new_data_set_port(new_data, e->port);
389 new_data->save_port = TRUE;
390 } else
391 pa_log_debug("Not restoring port for source %s, because already set.", name);
392 }
393
394 pa_xfree(e);
395 }
396
397 pa_xfree(name);
398
399 return PA_HOOK_OK;
400 }
401
402 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
403 char *name;
404 struct entry *e;
405
406 pa_assert(c);
407 pa_assert(new_data);
408 pa_assert(u);
409 pa_assert(u->restore_volume || u->restore_muted);
410
411 name = pa_sprintf_malloc("source:%s", new_data->name);
412
413 if ((e = read_entry(u, name))) {
414
415 if (u->restore_volume && e->volume_valid) {
416
417 if (!new_data->volume_is_set) {
418 pa_cvolume v;
419
420 pa_log_info("Restoring volume for source %s.", new_data->name);
421
422 v = e->volume;
423 pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
424 pa_source_new_data_set_volume(new_data, &v);
425
426 new_data->save_volume = TRUE;
427 } else
428 pa_log_debug("Not restoring volume for source %s, because already set.", new_data->name);
429 }
430
431 if (u->restore_muted && e->muted_valid) {
432
433 if (!new_data->muted_is_set) {
434 pa_log_info("Restoring mute state for source %s.", new_data->name);
435 pa_source_new_data_set_muted(new_data, e->muted);
436 new_data->save_muted = TRUE;
437 } else
438 pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name);
439 }
440
441 pa_xfree(e);
442 }
443
444 pa_xfree(name);
445
446 return PA_HOOK_OK;
447 }
448
449 int pa__init(pa_module*m) {
450 pa_modargs *ma = NULL;
451 struct userdata *u;
452 char *fname;
453 pa_sink *sink;
454 pa_source *source;
455 uint32_t idx;
456 pa_bool_t restore_volume = TRUE, restore_muted = TRUE, restore_port = TRUE;
457
458 pa_assert(m);
459
460 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
461 pa_log("Failed to parse module arguments");
462 goto fail;
463 }
464
465 if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
466 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 ||
467 pa_modargs_get_value_boolean(ma, "restore_port", &restore_port) < 0) {
468 pa_log("restore_port=, restore_volume= and restore_muted= expect boolean arguments");
469 goto fail;
470 }
471
472 if (!restore_muted && !restore_volume && !restore_port)
473 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring port enabled!");
474
475 m->userdata = u = pa_xnew0(struct userdata, 1);
476 u->core = m->core;
477 u->module = m;
478 u->restore_volume = restore_volume;
479 u->restore_muted = restore_muted;
480 u->restore_port = restore_port;
481
482 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
483
484 if (restore_port) {
485 u->sink_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_new_hook_callback, u);
486 u->source_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_new_hook_callback, u);
487 }
488
489 if (restore_muted || restore_volume) {
490 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);
491 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);
492 }
493
494 if (!(fname = pa_state_path("device-volumes", TRUE)))
495 goto fail;
496
497 if (!(u->database = pa_database_open(fname, TRUE))) {
498 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
499 pa_xfree(fname);
500 goto fail;
501 }
502
503 pa_log_info("Sucessfully opened database file '%s'.", fname);
504 pa_xfree(fname);
505
506 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
507 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
508
509 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
510 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
511
512 pa_modargs_free(ma);
513 return 0;
514
515 fail:
516 pa__done(m);
517
518 if (ma)
519 pa_modargs_free(ma);
520
521 return -1;
522 }
523
524 void pa__done(pa_module*m) {
525 struct userdata* u;
526
527 pa_assert(m);
528
529 if (!(u = m->userdata))
530 return;
531
532 if (u->subscription)
533 pa_subscription_free(u->subscription);
534
535 if (u->sink_fixate_hook_slot)
536 pa_hook_slot_free(u->sink_fixate_hook_slot);
537 if (u->source_fixate_hook_slot)
538 pa_hook_slot_free(u->source_fixate_hook_slot);
539 if (u->sink_new_hook_slot)
540 pa_hook_slot_free(u->sink_new_hook_slot);
541 if (u->source_new_hook_slot)
542 pa_hook_slot_free(u->source_new_hook_slot);
543
544 if (u->save_time_event)
545 u->core->mainloop->time_free(u->save_time_event);
546
547 if (u->database)
548 pa_database_close(u->database);
549
550 pa_xfree(u);
551 }