]> code.delx.au - pulseaudio/blob - src/modules/module-device-restore.c
database: Convert our use of database files to save in tagstruct format.
[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 #include <pulse/rtclock.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 #include <pulsecore/database.h>
50 #include <pulsecore/tagstruct.h>
51
52 #include "module-device-restore-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(TRUE);
58 PA_MODULE_USAGE(
59 "restore_port=<Save/restore port?> "
60 "restore_volume=<Save/restore volumes?> "
61 "restore_muted=<Save/restore muted states?>");
62
63 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
64
65 static const char* const valid_modargs[] = {
66 "restore_volume",
67 "restore_muted",
68 "restore_port",
69 NULL
70 };
71
72 struct userdata {
73 pa_core *core;
74 pa_module *module;
75 pa_subscription *subscription;
76 pa_hook_slot
77 *sink_new_hook_slot,
78 *sink_fixate_hook_slot,
79 *source_new_hook_slot,
80 *source_fixate_hook_slot;
81 pa_time_event *save_time_event;
82 pa_database *database;
83
84 pa_bool_t restore_volume:1;
85 pa_bool_t restore_muted:1;
86 pa_bool_t restore_port:1;
87 };
88
89 #define ENTRY_VERSION 3
90
91 struct entry {
92 uint8_t version;
93 pa_bool_t muted_valid, volume_valid, port_valid;
94 pa_bool_t muted;
95 pa_channel_map channel_map;
96 pa_cvolume volume;
97 char *port;
98 };
99
100 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
101 struct userdata *u = userdata;
102
103 pa_assert(a);
104 pa_assert(e);
105 pa_assert(u);
106
107 pa_assert(e == u->save_time_event);
108 u->core->mainloop->time_free(u->save_time_event);
109 u->save_time_event = NULL;
110
111 pa_database_sync(u->database);
112 pa_log_info("Synced.");
113 }
114
115 static struct entry* entry_new(void) {
116 struct entry *r = pa_xnew0(struct entry, 1);
117 r->version = ENTRY_VERSION;
118 return r;
119 }
120
121 static void entry_free(struct entry* e) {
122 pa_assert(e);
123
124 pa_xfree(e->port);
125 pa_xfree(e);
126 }
127
128 static struct entry* entry_read(struct userdata *u, const char *name) {
129 pa_datum key, data;
130 struct entry *e = NULL;
131 pa_tagstruct *t = NULL;
132 const char* port;
133
134 pa_assert(u);
135 pa_assert(name);
136
137 key.data = (char*) name;
138 key.size = strlen(name);
139
140 pa_zero(data);
141
142 if (!pa_database_get(u->database, &key, &data))
143 goto fail;
144
145 t = pa_tagstruct_new(data.data, data.size);
146 e = entry_new();
147
148 if (pa_tagstruct_getu8(t, &e->version) < 0 ||
149 e->version > ENTRY_VERSION ||
150 pa_tagstruct_get_boolean(t, &e->volume_valid) < 0 ||
151 pa_tagstruct_get_channel_map(t, &e->channel_map) < 0 ||
152 pa_tagstruct_get_cvolume(t, &e->volume) < 0 ||
153 pa_tagstruct_get_boolean(t, &e->muted_valid) < 0 ||
154 pa_tagstruct_get_boolean(t, &e->muted) < 0 ||
155 pa_tagstruct_get_boolean(t, &e->port_valid) < 0 ||
156 pa_tagstruct_gets(t, &port) < 0) {
157
158 goto fail;
159 }
160
161 e->port = pa_xstrdup(port);
162
163 if (!pa_tagstruct_eof(t))
164 goto fail;
165
166 if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
167 pa_log_warn("Invalid channel map stored in database for device %s", name);
168 goto fail;
169 }
170
171 if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) {
172 pa_log_warn("Volume and channel map don't match in database entry for device %s", name);
173 goto fail;
174 }
175
176 pa_tagstruct_free(t);
177 pa_datum_free(&data);
178
179 return e;
180
181 fail:
182
183 pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
184
185 if (e)
186 entry_free(e);
187 if (t)
188 pa_tagstruct_free(t);
189 pa_datum_free(&data);
190 return NULL;
191 }
192
193 static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
194 pa_tagstruct *t;
195 pa_datum key, data;
196 pa_bool_t r;
197
198 pa_assert(u);
199 pa_assert(name);
200 pa_assert(e);
201
202 t = pa_tagstruct_new(NULL, 0);
203 pa_tagstruct_putu8(t, e->version);
204 pa_tagstruct_put_boolean(t, e->volume_valid);
205 pa_tagstruct_put_channel_map(t, &e->channel_map);
206 pa_tagstruct_put_cvolume(t, &e->volume);
207 pa_tagstruct_put_boolean(t, e->muted_valid);
208 pa_tagstruct_put_boolean(t, e->muted);
209 pa_tagstruct_put_boolean(t, e->port_valid);
210 pa_tagstruct_puts(t, e->port);
211
212 key.data = (char *) name;
213 key.size = strlen(name);
214
215 data.data = (void*)pa_tagstruct_data(t, &data.size);
216
217 r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
218
219 pa_tagstruct_free(t);
220
221 return r;
222 }
223
224 static struct entry* entry_copy(const struct entry *e) {
225 struct entry* r;
226
227 pa_assert(e);
228 r = entry_new();
229 *r = *e;
230 r->port = pa_xstrdup(e->port);
231 return r;
232 }
233
234 static void trigger_save(struct userdata *u) {
235 if (u->save_time_event)
236 return;
237
238 u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
239 }
240
241 static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
242 pa_cvolume t;
243
244 if (a->port_valid != b->port_valid ||
245 (a->port_valid && !pa_streq(a->port, b->port)))
246 return FALSE;
247
248 if (a->muted_valid != b->muted_valid ||
249 (a->muted_valid && (a->muted != b->muted)))
250 return FALSE;
251
252 t = b->volume;
253 if (a->volume_valid != b->volume_valid ||
254 (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume)))
255 return FALSE;
256
257 return TRUE;
258 }
259
260 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
261 struct userdata *u = userdata;
262 struct entry *entry, *old;
263 char *name;
264
265 pa_assert(c);
266 pa_assert(u);
267
268 if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
269 t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
270 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
271 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
272 return;
273
274 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
275 pa_sink *sink;
276
277 if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
278 return;
279
280 name = pa_sprintf_malloc("sink:%s", sink->name);
281
282 if ((old = entry_read(u, name)))
283 entry = entry_copy(old);
284 else
285 entry = entry_new();
286
287 if (sink->save_volume) {
288 entry->channel_map = sink->channel_map;
289 entry->volume = *pa_sink_get_volume(sink, FALSE);
290 entry->volume_valid = TRUE;
291 }
292
293 if (sink->save_muted) {
294 entry->muted = pa_sink_get_mute(sink, FALSE);
295 entry->muted_valid = TRUE;
296 }
297
298 if (sink->save_port) {
299 pa_xfree(entry->port);
300 entry->port = pa_xstrdup(sink->active_port ? sink->active_port->name : "");
301 entry->port_valid = TRUE;
302 }
303
304 } else {
305 pa_source *source;
306
307 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
308
309 if (!(source = pa_idxset_get_by_index(c->sources, idx)))
310 return;
311
312 name = pa_sprintf_malloc("source:%s", source->name);
313
314 if ((old = entry_read(u, name)))
315 entry = entry_copy(old);
316 else
317 entry = entry_new();
318
319 if (source->save_volume) {
320 entry->channel_map = source->channel_map;
321 entry->volume = *pa_source_get_volume(source, FALSE);
322 entry->volume_valid = TRUE;
323 }
324
325 if (source->save_muted) {
326 entry->muted = pa_source_get_mute(source, FALSE);
327 entry->muted_valid = TRUE;
328 }
329
330 if (source->save_port) {
331 pa_xfree(entry->port);
332 entry->port = pa_xstrdup(source->active_port ? source->active_port->name : "");
333 entry->port_valid = TRUE;
334 }
335 }
336
337 pa_assert(entry);
338
339 if (old) {
340
341 if (entries_equal(old, entry)) {
342 entry_free(old);
343 entry_free(entry);
344 pa_xfree(name);
345 return;
346 }
347
348 entry_free(old);
349 }
350
351 pa_log_info("Storing volume/mute/port for device %s.", name);
352
353 if (entry_write(u, name, entry))
354 trigger_save(u);
355
356 entry_free(entry);
357 pa_xfree(name);
358 }
359
360 static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
361 char *name;
362 struct entry *e;
363
364 pa_assert(c);
365 pa_assert(new_data);
366 pa_assert(u);
367 pa_assert(u->restore_port);
368
369 name = pa_sprintf_malloc("sink:%s", new_data->name);
370
371 if ((e = entry_read(u, name))) {
372
373 if (e->port_valid) {
374 if (!new_data->active_port) {
375 pa_log_info("Restoring port for sink %s.", name);
376 pa_sink_new_data_set_port(new_data, e->port);
377 new_data->save_port = TRUE;
378 } else
379 pa_log_debug("Not restoring port for sink %s, because already set.", name);
380 }
381
382 entry_free(e);
383 }
384
385 pa_xfree(name);
386
387 return PA_HOOK_OK;
388 }
389
390 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
391 char *name;
392 struct entry *e;
393
394 pa_assert(c);
395 pa_assert(new_data);
396 pa_assert(u);
397 pa_assert(u->restore_volume || u->restore_muted);
398
399 name = pa_sprintf_malloc("sink:%s", new_data->name);
400
401 if ((e = entry_read(u, name))) {
402
403 if (u->restore_volume && e->volume_valid) {
404
405 if (!new_data->volume_is_set) {
406 pa_cvolume v;
407
408 pa_log_info("Restoring volume for sink %s.", new_data->name);
409
410 v = e->volume;
411 pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
412 pa_sink_new_data_set_volume(new_data, &v);
413
414 new_data->save_volume = TRUE;
415 } else
416 pa_log_debug("Not restoring volume for sink %s, because already set.", new_data->name);
417 }
418
419 if (u->restore_muted && e->muted_valid) {
420
421 if (!new_data->muted_is_set) {
422 pa_log_info("Restoring mute state for sink %s.", new_data->name);
423 pa_sink_new_data_set_muted(new_data, e->muted);
424 new_data->save_muted = TRUE;
425 } else
426 pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name);
427 }
428
429 entry_free(e);
430 }
431
432 pa_xfree(name);
433
434 return PA_HOOK_OK;
435 }
436
437 static pa_hook_result_t source_new_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
438 char *name;
439 struct entry *e;
440
441 pa_assert(c);
442 pa_assert(new_data);
443 pa_assert(u);
444 pa_assert(u->restore_port);
445
446 name = pa_sprintf_malloc("source:%s", new_data->name);
447
448 if ((e = entry_read(u, name))) {
449
450 if (e->port_valid) {
451 if (!new_data->active_port) {
452 pa_log_info("Restoring port for source %s.", name);
453 pa_source_new_data_set_port(new_data, e->port);
454 new_data->save_port = TRUE;
455 } else
456 pa_log_debug("Not restoring port for source %s, because already set.", name);
457 }
458
459 entry_free(e);
460 }
461
462 pa_xfree(name);
463
464 return PA_HOOK_OK;
465 }
466
467 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
468 char *name;
469 struct entry *e;
470
471 pa_assert(c);
472 pa_assert(new_data);
473 pa_assert(u);
474 pa_assert(u->restore_volume || u->restore_muted);
475
476 name = pa_sprintf_malloc("source:%s", new_data->name);
477
478 if ((e = entry_read(u, name))) {
479
480 if (u->restore_volume && e->volume_valid) {
481
482 if (!new_data->volume_is_set) {
483 pa_cvolume v;
484
485 pa_log_info("Restoring volume for source %s.", new_data->name);
486
487 v = e->volume;
488 pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map);
489 pa_source_new_data_set_volume(new_data, &v);
490
491 new_data->save_volume = TRUE;
492 } else
493 pa_log_debug("Not restoring volume for source %s, because already set.", new_data->name);
494 }
495
496 if (u->restore_muted && e->muted_valid) {
497
498 if (!new_data->muted_is_set) {
499 pa_log_info("Restoring mute state for source %s.", new_data->name);
500 pa_source_new_data_set_muted(new_data, e->muted);
501 new_data->save_muted = TRUE;
502 } else
503 pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name);
504 }
505
506 entry_free(e);
507 }
508
509 pa_xfree(name);
510
511 return PA_HOOK_OK;
512 }
513
514 int pa__init(pa_module*m) {
515 pa_modargs *ma = NULL;
516 struct userdata *u;
517 char *fname;
518 pa_sink *sink;
519 pa_source *source;
520 uint32_t idx;
521 pa_bool_t restore_volume = TRUE, restore_muted = TRUE, restore_port = TRUE;
522
523 pa_assert(m);
524
525 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
526 pa_log("Failed to parse module arguments");
527 goto fail;
528 }
529
530 if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
531 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 ||
532 pa_modargs_get_value_boolean(ma, "restore_port", &restore_port) < 0) {
533 pa_log("restore_port=, restore_volume= and restore_muted= expect boolean arguments");
534 goto fail;
535 }
536
537 if (!restore_muted && !restore_volume && !restore_port)
538 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring port enabled!");
539
540 m->userdata = u = pa_xnew0(struct userdata, 1);
541 u->core = m->core;
542 u->module = m;
543 u->restore_volume = restore_volume;
544 u->restore_muted = restore_muted;
545 u->restore_port = restore_port;
546
547 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
548
549 if (restore_port) {
550 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);
551 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);
552 }
553
554 if (restore_muted || restore_volume) {
555 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);
556 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);
557 }
558
559 if (!(fname = pa_state_path("device-volumes", TRUE)))
560 goto fail;
561
562 if (!(u->database = pa_database_open(fname, TRUE))) {
563 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
564 pa_xfree(fname);
565 goto fail;
566 }
567
568 pa_log_info("Successfully opened database file '%s'.", fname);
569 pa_xfree(fname);
570
571 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
572 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
573
574 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
575 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
576
577 pa_modargs_free(ma);
578 return 0;
579
580 fail:
581 pa__done(m);
582
583 if (ma)
584 pa_modargs_free(ma);
585
586 return -1;
587 }
588
589 void pa__done(pa_module*m) {
590 struct userdata* u;
591
592 pa_assert(m);
593
594 if (!(u = m->userdata))
595 return;
596
597 if (u->subscription)
598 pa_subscription_free(u->subscription);
599
600 if (u->sink_fixate_hook_slot)
601 pa_hook_slot_free(u->sink_fixate_hook_slot);
602 if (u->source_fixate_hook_slot)
603 pa_hook_slot_free(u->source_fixate_hook_slot);
604 if (u->sink_new_hook_slot)
605 pa_hook_slot_free(u->sink_new_hook_slot);
606 if (u->source_new_hook_slot)
607 pa_hook_slot_free(u->source_new_hook_slot);
608
609 if (u->save_time_event)
610 u->core->mainloop->time_free(u->save_time_event);
611
612 if (u->database)
613 pa_database_close(u->database);
614
615 pa_xfree(u);
616 }