]> code.delx.au - pulseaudio/blob - src/modules/module-card-restore.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / modules / module-card-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
33 #include <pulse/gccmacro.h>
34 #include <pulse/xmalloc.h>
35 #include <pulse/timeval.h>
36 #include <pulse/rtclock.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-subscribe.h>
44 #include <pulsecore/card.h>
45 #include <pulsecore/namereg.h>
46 #include <pulsecore/database.h>
47 #include <pulsecore/tagstruct.h>
48
49 #include "module-card-restore-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering");
52 PA_MODULE_DESCRIPTION("Automatically restore profile of cards");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(true);
55
56 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
57
58 static const char* const valid_modargs[] = {
59 NULL
60 };
61
62 struct userdata {
63 pa_core *core;
64 pa_module *module;
65 pa_hook_slot *card_new_hook_slot;
66 pa_hook_slot *card_put_hook_slot;
67 pa_hook_slot *card_profile_hook_slot;
68 pa_hook_slot *port_offset_hook_slot;
69 pa_time_event *save_time_event;
70 pa_database *database;
71 bool hooks_connected;
72 };
73
74 #define ENTRY_VERSION 2
75
76 struct port_info {
77 char *name;
78 int64_t offset;
79 };
80
81 struct entry {
82 uint8_t version;
83 char *profile;
84 pa_hashmap *ports; /* Port name -> struct port_info */
85 };
86
87 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
88 struct userdata *u = userdata;
89
90 pa_assert(a);
91 pa_assert(e);
92 pa_assert(u);
93
94 pa_assert(e == u->save_time_event);
95 u->core->mainloop->time_free(u->save_time_event);
96 u->save_time_event = NULL;
97
98 pa_database_sync(u->database);
99 pa_log_info("Synced.");
100 }
101
102 static void trigger_save(struct userdata *u) {
103 if (u->save_time_event)
104 return;
105
106 u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
107 }
108
109 static struct entry* entry_new(void) {
110 struct entry *r = pa_xnew0(struct entry, 1);
111 r->version = ENTRY_VERSION;
112 r->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
113 return r;
114 }
115
116 static struct port_info *port_info_new(pa_device_port *port) {
117 struct port_info *p_info;
118
119 if (port) {
120 p_info = pa_xnew(struct port_info, 1);
121 p_info->name = pa_xstrdup(port->name);
122 p_info->offset = port->latency_offset;
123 } else
124 p_info = pa_xnew0(struct port_info, 1);
125
126 return p_info;
127 }
128
129 static void port_info_free(struct port_info *p_info) {
130 pa_assert(p_info);
131
132 pa_xfree(p_info->name);
133 pa_xfree(p_info);
134 }
135
136 static void entry_free(struct entry* e) {
137 pa_assert(e);
138
139 pa_xfree(e->profile);
140 pa_hashmap_free(e->ports, (pa_free_cb_t) port_info_free);
141
142 pa_xfree(e);
143 }
144
145 static struct entry *entry_from_card(pa_card *card) {
146 struct port_info *p_info;
147 struct entry *entry;
148 pa_device_port *port;
149 void *state;
150
151 pa_assert(card);
152
153 entry = entry_new();
154 if (card->save_profile)
155 entry->profile = pa_xstrdup(card->active_profile->name);
156
157 PA_HASHMAP_FOREACH(port, card->ports, state) {
158 p_info = port_info_new(port);
159 pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
160 }
161
162 return entry;
163 }
164
165 static bool entrys_equal(struct entry *a, struct entry *b) {
166 struct port_info *Ap_info, *Bp_info;
167 void *state;
168
169 pa_assert(a);
170 pa_assert(b);
171
172 if (!pa_streq(a->profile, b->profile) ||
173 pa_hashmap_size(a->ports) != pa_hashmap_size(b->ports))
174 return false;
175
176 PA_HASHMAP_FOREACH(Ap_info, a->ports, state) {
177 if ((Bp_info = pa_hashmap_get(b->ports, Ap_info->name))) {
178 if (Ap_info->offset != Bp_info->offset)
179 return false;
180 } else
181 return false;
182 }
183
184 return true;
185 }
186
187 static bool entry_write(struct userdata *u, const char *name, const struct entry *e) {
188 pa_tagstruct *t;
189 pa_datum key, data;
190 bool r;
191 void *state;
192 struct port_info *p_info;
193
194 pa_assert(u);
195 pa_assert(name);
196 pa_assert(e);
197
198 t = pa_tagstruct_new(NULL, 0);
199 pa_tagstruct_putu8(t, e->version);
200 pa_tagstruct_puts(t, e->profile);
201 pa_tagstruct_putu32(t, pa_hashmap_size(e->ports));
202
203 PA_HASHMAP_FOREACH(p_info, e->ports, state) {
204 pa_tagstruct_puts(t, p_info->name);
205 pa_tagstruct_puts64(t, p_info->offset);
206 }
207
208 key.data = (char *) name;
209 key.size = strlen(name);
210
211 data.data = (void*)pa_tagstruct_data(t, &data.size);
212
213 r = (pa_database_set(u->database, &key, &data, true) == 0);
214
215 pa_tagstruct_free(t);
216
217 return r;
218 }
219
220 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
221
222 #define LEGACY_ENTRY_VERSION 1
223 static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
224 struct legacy_entry {
225 uint8_t version;
226 char profile[PA_NAME_MAX];
227 } PA_GCC_PACKED ;
228 struct legacy_entry *le;
229 struct entry *e;
230
231 pa_assert(u);
232 pa_assert(data);
233
234 if (data->size != sizeof(struct legacy_entry)) {
235 pa_log_debug("Size does not match.");
236 return NULL;
237 }
238
239 le = (struct legacy_entry*)data->data;
240
241 if (le->version != LEGACY_ENTRY_VERSION) {
242 pa_log_debug("Version mismatch.");
243 return NULL;
244 }
245
246 if (!memchr(le->profile, 0, sizeof(le->profile))) {
247 pa_log_warn("Profile has missing NUL byte.");
248 return NULL;
249 }
250
251 e = entry_new();
252 e->profile = pa_xstrdup(le->profile);
253 return e;
254 }
255 #endif
256
257 static struct entry* entry_read(struct userdata *u, const char *name) {
258 pa_datum key, data;
259 struct entry *e = NULL;
260 pa_tagstruct *t = NULL;
261 const char* profile;
262
263 pa_assert(u);
264 pa_assert(name);
265
266 key.data = (char*) name;
267 key.size = strlen(name);
268
269 pa_zero(data);
270
271 if (!pa_database_get(u->database, &key, &data))
272 goto fail;
273
274 t = pa_tagstruct_new(data.data, data.size);
275 e = entry_new();
276
277 if (pa_tagstruct_getu8(t, &e->version) < 0 ||
278 e->version > ENTRY_VERSION ||
279 pa_tagstruct_gets(t, &profile) < 0) {
280
281 goto fail;
282 }
283
284 if (!profile)
285 profile = "";
286
287 e->profile = pa_xstrdup(profile);
288
289 if (e->version >= 2) {
290 uint32_t port_count = 0;
291 const char *port_name = NULL;
292 int64_t port_offset = 0;
293 struct port_info *p_info;
294 unsigned i;
295
296 if (pa_tagstruct_getu32(t, &port_count) < 0)
297 goto fail;
298
299 for (i = 0; i < port_count; i++) {
300 if (pa_tagstruct_gets(t, &port_name) < 0 ||
301 !port_name ||
302 pa_hashmap_get(e->ports, port_name) ||
303 pa_tagstruct_gets64(t, &port_offset) < 0)
304 goto fail;
305
306 p_info = port_info_new(NULL);
307 p_info->name = pa_xstrdup(port_name);
308 p_info->offset = port_offset;
309
310 pa_assert_se(pa_hashmap_put(e->ports, p_info->name, p_info) >= 0);
311 }
312 }
313
314 if (!pa_tagstruct_eof(t))
315 goto fail;
316
317 pa_tagstruct_free(t);
318 pa_datum_free(&data);
319
320 return e;
321
322 fail:
323
324 pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
325
326 if (e)
327 entry_free(e);
328 if (t)
329 pa_tagstruct_free(t);
330
331 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
332 pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
333 if ((e = legacy_entry_read(u, &data))) {
334 pa_log_debug("Success. Saving new format for key: %s", name);
335 if (entry_write(u, name, e))
336 trigger_save(u);
337 pa_datum_free(&data);
338 return e;
339 } else
340 pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
341 #endif
342
343 pa_datum_free(&data);
344 return NULL;
345 }
346
347 static void show_full_info(pa_card *card) {
348 pa_assert(card);
349
350 if (card->save_profile)
351 pa_log_info("Storing profile and port latency offsets for card %s.", card->name);
352 else
353 pa_log_info("Storing port latency offsets for card %s.", card->name);
354 }
355
356 static pa_hook_result_t card_put_hook_callback(pa_core *c, pa_card *card, struct userdata *u) {
357 struct entry *entry, *old;
358
359 pa_assert(card);
360
361 entry = entry_from_card(card);
362
363 if ((old = entry_read(u, card->name))) {
364 if (!card->save_profile)
365 entry->profile = pa_xstrdup(old->profile);
366 if (entrys_equal(entry, old))
367 goto finish;
368 }
369
370 show_full_info(card);
371
372 if (entry_write(u, card->name, entry))
373 trigger_save(u);
374
375 finish:
376 entry_free(entry);
377 if (old)
378 entry_free(old);
379
380 return PA_HOOK_OK;
381 }
382
383 static pa_hook_result_t card_profile_change_callback(pa_core *c, pa_card *card, struct userdata *u) {
384 struct entry *entry;
385
386 pa_assert(card);
387
388 if (!card->save_profile)
389 return PA_HOOK_OK;
390
391 if ((entry = entry_read(u, card->name))) {
392 entry->profile = pa_xstrdup(card->active_profile->name);
393 pa_log_info("Storing card profile for card %s.", card->name);
394 } else {
395 entry = entry_from_card(card);
396 show_full_info(card);
397 }
398
399 if (entry_write(u, card->name, entry))
400 trigger_save(u);
401
402 entry_free(entry);
403 return PA_HOOK_OK;
404 }
405
406 static pa_hook_result_t port_offset_change_callback(pa_core *c, pa_device_port *port, struct userdata *u) {
407 struct entry *entry;
408 pa_card *card;
409
410 pa_assert(port);
411 card = port->card;
412
413 if ((entry = entry_read(u, card->name))) {
414 struct port_info *p_info;
415
416 if ((p_info = pa_hashmap_get(entry->ports, port->name)))
417 p_info->offset = port->latency_offset;
418 else {
419 p_info = port_info_new(port);
420 pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
421 }
422
423 pa_log_info("Storing latency offset for port %s on card %s.", port->name, card->name);
424
425 } else {
426 entry_from_card(card);
427 show_full_info(card);
428 }
429
430 if (entry_write(u, card->name, entry))
431 trigger_save(u);
432
433 entry_free(entry);
434 return PA_HOOK_OK;
435 }
436
437 static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
438 struct entry *e;
439 void *state;
440 pa_device_port *p;
441 struct port_info *p_info;
442
443 pa_assert(new_data);
444
445 if (!(e = entry_read(u, new_data->name)))
446 return PA_HOOK_OK;
447
448 if (e->profile[0]) {
449 if (!new_data->active_profile) {
450 pa_card_new_data_set_profile(new_data, e->profile);
451 pa_log_info("Restored profile '%s' for card %s.", new_data->active_profile, new_data->name);
452 new_data->save_profile = true;
453
454 } else
455 pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
456 }
457
458 /* Always restore the latency offsets because their
459 * initial value is always 0 */
460
461 pa_log_info("Restoring port latency offsets for card %s.", new_data->name);
462
463 PA_HASHMAP_FOREACH(p_info, e->ports, state)
464 if ((p = pa_hashmap_get(new_data->ports, p_info->name)))
465 p->latency_offset = p_info->offset;
466
467 entry_free(e);
468
469 return PA_HOOK_OK;
470 }
471
472 int pa__init(pa_module*m) {
473 pa_modargs *ma = NULL;
474 struct userdata *u;
475 char *fname;
476
477 pa_assert(m);
478
479 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
480 pa_log("Failed to parse module arguments");
481 goto fail;
482 }
483
484 m->userdata = u = pa_xnew0(struct userdata, 1);
485 u->core = m->core;
486 u->module = m;
487
488 u->card_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u);
489 u->card_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) card_put_hook_callback, u);
490 u->card_profile_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) card_profile_change_callback, u);
491 u->port_offset_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) port_offset_change_callback, u);
492 u->hooks_connected = true;
493
494 if (!(fname = pa_state_path("card-database", 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("Successfully opened database file '%s'.", fname);
504 pa_xfree(fname);
505
506 pa_modargs_free(ma);
507 return 0;
508
509 fail:
510 pa__done(m);
511
512 if (ma)
513 pa_modargs_free(ma);
514
515 return -1;
516 }
517
518 void pa__done(pa_module*m) {
519 struct userdata* u;
520
521 pa_assert(m);
522
523 if (!(u = m->userdata))
524 return;
525
526 if (u->hooks_connected) {
527 pa_hook_slot_free(u->card_new_hook_slot);
528 pa_hook_slot_free(u->card_put_hook_slot);
529 pa_hook_slot_free(u->card_profile_hook_slot);
530 pa_hook_slot_free(u->port_offset_hook_slot);
531 }
532
533 if (u->save_time_event)
534 u->core->mainloop->time_free(u->save_time_event);
535
536 if (u->database)
537 pa_database_close(u->database);
538
539 pa_xfree(u);
540 }