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