]> code.delx.au - pulseaudio/blob - src/modules/module-card-restore.c
Merge branch 'master-tx'
[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
50 #include "module-card-restore-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Automatically restore profile of cards");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(TRUE);
56
57 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
58
59 static const char* const valid_modargs[] = {
60 NULL
61 };
62
63 struct userdata {
64 pa_core *core;
65 pa_module *module;
66 pa_subscription *subscription;
67 pa_hook_slot *card_new_hook_slot;
68 pa_time_event *save_time_event;
69 pa_database *database;
70 };
71
72 #define ENTRY_VERSION 1
73
74 struct entry {
75 uint8_t version;
76 char profile[PA_NAME_MAX];
77 } PA_GCC_PACKED ;
78
79 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
80 struct userdata *u = userdata;
81
82 pa_assert(a);
83 pa_assert(e);
84 pa_assert(u);
85
86 pa_assert(e == u->save_time_event);
87 u->core->mainloop->time_free(u->save_time_event);
88 u->save_time_event = NULL;
89
90 pa_database_sync(u->database);
91 pa_log_info("Synced.");
92 }
93
94 static struct entry* read_entry(struct userdata *u, const char *name) {
95 pa_datum key, data;
96 struct entry *e;
97
98 pa_assert(u);
99 pa_assert(name);
100
101 key.data = (char*) name;
102 key.size = strlen(name);
103
104 pa_zero(data);
105
106 if (!pa_database_get(u->database, &key, &data))
107 goto fail;
108
109 if (data.size != sizeof(struct entry)) {
110 pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
111 goto fail;
112 }
113
114 e = (struct entry*) data.data;
115
116 if (e->version != ENTRY_VERSION) {
117 pa_log_debug("Version of database entry for card %s doesn't match our version. Probably due to upgrade, ignoring.", name);
118 goto fail;
119 }
120
121 if (!memchr(e->profile, 0, sizeof(e->profile))) {
122 pa_log_warn("Database contains entry for card %s with missing NUL byte in profile name", name);
123 goto fail;
124 }
125
126 return e;
127
128 fail:
129
130 pa_datum_free(&data);
131 return NULL;
132 }
133
134 static void trigger_save(struct userdata *u) {
135 if (u->save_time_event)
136 return;
137
138 u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
139 }
140
141 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
142 struct userdata *u = userdata;
143 struct entry entry, *old;
144 pa_datum key, data;
145 pa_card *card;
146
147 pa_assert(c);
148 pa_assert(u);
149
150 if (t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW) &&
151 t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
152 return;
153
154 pa_zero(entry);
155 entry.version = ENTRY_VERSION;
156
157 if (!(card = pa_idxset_get_by_index(c->cards, idx)))
158 return;
159
160 if (!card->save_profile)
161 return;
162
163 pa_strlcpy(entry.profile, card->active_profile ? card->active_profile->name : "", sizeof(entry.profile));
164
165 if ((old = read_entry(u, card->name))) {
166
167 if (strncmp(old->profile, entry.profile, sizeof(entry.profile)) == 0) {
168 pa_xfree(old);
169 return;
170 }
171
172 pa_xfree(old);
173 }
174
175 key.data = card->name;
176 key.size = strlen(card->name);
177
178 data.data = &entry;
179 data.size = sizeof(entry);
180
181 pa_log_info("Storing profile for card %s.", card->name);
182
183 pa_database_set(u->database, &key, &data, TRUE);
184
185 trigger_save(u);
186 }
187
188 static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
189 struct entry *e;
190
191 pa_assert(new_data);
192
193 if ((e = read_entry(u, new_data->name)) && e->profile[0]) {
194
195 if (!new_data->active_profile) {
196 pa_log_info("Restoring profile for card %s.", new_data->name);
197 pa_card_new_data_set_profile(new_data, e->profile);
198 new_data->save_profile = TRUE;
199 } else
200 pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
201
202 pa_xfree(e);
203 }
204
205 return PA_HOOK_OK;
206 }
207
208 int pa__init(pa_module*m) {
209 pa_modargs *ma = NULL;
210 struct userdata *u;
211 char *fname;
212 pa_card *card;
213 uint32_t idx;
214
215 pa_assert(m);
216
217 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
218 pa_log("Failed to parse module arguments");
219 goto fail;
220 }
221
222 m->userdata = u = pa_xnew0(struct userdata, 1);
223 u->core = m->core;
224 u->module = m;
225
226 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u);
227
228 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);
229
230 if (!(fname = pa_state_path("card-database", TRUE)))
231 goto fail;
232
233 if (!(u->database = pa_database_open(fname, TRUE))) {
234 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
235 pa_xfree(fname);
236 goto fail;
237 }
238
239 pa_log_info("Sucessfully opened database file '%s'.", fname);
240 pa_xfree(fname);
241
242 for (card = pa_idxset_first(m->core->cards, &idx); card; card = pa_idxset_next(m->core->cards, &idx))
243 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, card->index, u);
244
245 pa_modargs_free(ma);
246 return 0;
247
248 fail:
249 pa__done(m);
250
251 if (ma)
252 pa_modargs_free(ma);
253
254 return -1;
255 }
256
257 void pa__done(pa_module*m) {
258 struct userdata* u;
259
260 pa_assert(m);
261
262 if (!(u = m->userdata))
263 return;
264
265 if (u->subscription)
266 pa_subscription_free(u->subscription);
267
268 if (u->card_new_hook_slot)
269 pa_hook_slot_free(u->card_new_hook_slot);
270
271 if (u->save_time_event)
272 u->core->mainloop->time_free(u->save_time_event);
273
274 if (u->database)
275 pa_database_close(u->database);
276
277 pa_xfree(u);
278 }