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