]> code.delx.au - pulseaudio/blob - src/modules/module-card-restore.c
f1c193076b55448b1162baab4abf7f29d215db49
[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 #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/card.h>
47 #include <pulsecore/namereg.h>
48 #include <pulsecore/database.h>
49 #include <pulsecore/tagstruct.h>
50
51 #include "module-card-restore-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("Automatically restore profile of cards");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(TRUE);
57
58 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
59
60 static const char* const valid_modargs[] = {
61 NULL
62 };
63
64 struct userdata {
65 pa_core *core;
66 pa_module *module;
67 pa_subscription *subscription;
68 pa_hook_slot *card_new_hook_slot;
69 pa_time_event *save_time_event;
70 pa_database *database;
71 };
72
73 #define ENTRY_VERSION 1
74
75 struct entry {
76 uint8_t version;
77 char *profile;
78 };
79
80 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
81 struct userdata *u = userdata;
82
83 pa_assert(a);
84 pa_assert(e);
85 pa_assert(u);
86
87 pa_assert(e == u->save_time_event);
88 u->core->mainloop->time_free(u->save_time_event);
89 u->save_time_event = NULL;
90
91 pa_database_sync(u->database);
92 pa_log_info("Synced.");
93 }
94
95 static void trigger_save(struct userdata *u) {
96 if (u->save_time_event)
97 return;
98
99 u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
100 }
101
102 static struct entry* entry_new(void) {
103 struct entry *r = pa_xnew0(struct entry, 1);
104 r->version = ENTRY_VERSION;
105 return r;
106 }
107
108 static void entry_free(struct entry* e) {
109 pa_assert(e);
110
111 pa_xfree(e->profile);
112 pa_xfree(e);
113 }
114
115 static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
116 pa_tagstruct *t;
117 pa_datum key, data;
118 pa_bool_t r;
119
120 pa_assert(u);
121 pa_assert(name);
122 pa_assert(e);
123
124 t = pa_tagstruct_new(NULL, 0);
125 pa_tagstruct_putu8(t, e->version);
126 pa_tagstruct_puts(t, e->profile);
127
128 key.data = (char *) name;
129 key.size = strlen(name);
130
131 data.data = (void*)pa_tagstruct_data(t, &data.size);
132
133 r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
134
135 pa_tagstruct_free(t);
136
137 return r;
138 }
139
140 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
141
142 #define LEGACY_ENTRY_VERSION 1
143 static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
144 struct legacy_entry {
145 uint8_t version;
146 char profile[PA_NAME_MAX];
147 } PA_GCC_PACKED ;
148 struct legacy_entry *le;
149 struct entry *e;
150
151 pa_assert(u);
152 pa_assert(data);
153
154 if (data->size != sizeof(struct legacy_entry)) {
155 pa_log_debug("Size does not match.");
156 return NULL;
157 }
158
159 le = (struct legacy_entry*)data->data;
160
161 if (le->version != LEGACY_ENTRY_VERSION) {
162 pa_log_debug("Version mismatch.");
163 return NULL;
164 }
165
166 if (!memchr(le->profile, 0, sizeof(le->profile))) {
167 pa_log_warn("Profile has missing NUL byte.");
168 return NULL;
169 }
170
171 e = entry_new();
172 e->profile = pa_xstrdup(le->profile);
173 return e;
174 }
175 #endif
176
177 static struct entry* entry_read(struct userdata *u, const char *name) {
178 pa_datum key, data;
179 struct entry *e = NULL;
180 pa_tagstruct *t = NULL;
181 const char* profile;
182
183 pa_assert(u);
184 pa_assert(name);
185
186 key.data = (char*) name;
187 key.size = strlen(name);
188
189 pa_zero(data);
190
191 if (!pa_database_get(u->database, &key, &data))
192 goto fail;
193
194 t = pa_tagstruct_new(data.data, data.size);
195 e = entry_new();
196
197 if (pa_tagstruct_getu8(t, &e->version) < 0 ||
198 e->version > ENTRY_VERSION ||
199 pa_tagstruct_gets(t, &profile) < 0) {
200
201 goto fail;
202 }
203
204 e->profile = pa_xstrdup(profile);
205
206 if (!pa_tagstruct_eof(t))
207 goto fail;
208
209 pa_tagstruct_free(t);
210 pa_datum_free(&data);
211
212 return e;
213
214 fail:
215
216 pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
217
218 if (e)
219 entry_free(e);
220 if (t)
221 pa_tagstruct_free(t);
222
223 #ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
224 pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
225 if ((e = legacy_entry_read(u, &data))) {
226 pa_log_debug("Success. Saving new format for key: %s", name);
227 if (entry_write(u, name, e))
228 trigger_save(u);
229 pa_datum_free(&data);
230 return e;
231 } else
232 pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
233 #endif
234
235 pa_datum_free(&data);
236 return NULL;
237 }
238
239 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
240 struct userdata *u = userdata;
241 struct entry *entry, *old;
242 pa_card *card;
243
244 pa_assert(c);
245 pa_assert(u);
246
247 if (t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW) &&
248 t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
249 return;
250
251 entry = entry_new();
252
253 if (!(card = pa_idxset_get_by_index(c->cards, idx)))
254 return;
255
256 if (!card->save_profile)
257 return;
258
259 entry->profile = pa_xstrdup(card->active_profile ? card->active_profile->name : "");
260
261 if ((old = entry_read(u, card->name))) {
262
263 if (pa_streq(old->profile, entry->profile)) {
264 entry_free(old);
265 entry_free(entry);
266 return;
267 }
268
269 entry_free(old);
270 }
271
272 pa_log_info("Storing profile for card %s.", card->name);
273
274 if (entry_write(u, card->name, entry))
275 trigger_save(u);
276
277 entry_free(entry);
278 }
279
280 static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
281 struct entry *e;
282
283 pa_assert(new_data);
284
285 if ((e = entry_read(u, new_data->name)) && e->profile[0]) {
286
287 if (!new_data->active_profile) {
288 pa_log_info("Restoring profile for card %s.", new_data->name);
289 pa_card_new_data_set_profile(new_data, e->profile);
290 new_data->save_profile = TRUE;
291 } else
292 pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
293
294 entry_free(e);
295 }
296
297 return PA_HOOK_OK;
298 }
299
300 int pa__init(pa_module*m) {
301 pa_modargs *ma = NULL;
302 struct userdata *u;
303 char *fname;
304 pa_card *card;
305 uint32_t idx;
306
307 pa_assert(m);
308
309 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
310 pa_log("Failed to parse module arguments");
311 goto fail;
312 }
313
314 m->userdata = u = pa_xnew0(struct userdata, 1);
315 u->core = m->core;
316 u->module = m;
317
318 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u);
319
320 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);
321
322 if (!(fname = pa_state_path("card-database", TRUE)))
323 goto fail;
324
325 if (!(u->database = pa_database_open(fname, TRUE))) {
326 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
327 pa_xfree(fname);
328 goto fail;
329 }
330
331 pa_log_info("Successfully opened database file '%s'.", fname);
332 pa_xfree(fname);
333
334 for (card = pa_idxset_first(m->core->cards, &idx); card; card = pa_idxset_next(m->core->cards, &idx))
335 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, card->index, u);
336
337 pa_modargs_free(ma);
338 return 0;
339
340 fail:
341 pa__done(m);
342
343 if (ma)
344 pa_modargs_free(ma);
345
346 return -1;
347 }
348
349 void pa__done(pa_module*m) {
350 struct userdata* u;
351
352 pa_assert(m);
353
354 if (!(u = m->userdata))
355 return;
356
357 if (u->subscription)
358 pa_subscription_free(u->subscription);
359
360 if (u->card_new_hook_slot)
361 pa_hook_slot_free(u->card_new_hook_slot);
362
363 if (u->save_time_event)
364 u->core->mainloop->time_free(u->save_time_event);
365
366 if (u->database)
367 pa_database_close(u->database);
368
369 pa_xfree(u);
370 }