]> code.delx.au - pulseaudio/blob - src/modules/module-card-restore.c
Merge commit 'origin/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
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/core-subscribe.h>
45 #include <pulsecore/card.h>
46 #include <pulsecore/namereg.h>
47 #include <pulsecore/database.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
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[PA_NAME_MAX];
76 } PA_GCC_PACKED ;
77
78 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
79 struct userdata *u = userdata;
80
81 pa_assert(a);
82 pa_assert(e);
83 pa_assert(tv);
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 struct timeval tv;
136
137 if (u->save_time_event)
138 return;
139
140 pa_gettimeofday(&tv);
141 tv.tv_sec += SAVE_INTERVAL;
142 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
143 }
144
145 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
146 struct userdata *u = userdata;
147 struct entry entry, *old;
148 pa_datum key, data;
149 pa_card *card;
150
151 pa_assert(c);
152 pa_assert(u);
153
154 if (t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW) &&
155 t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
156 return;
157
158 pa_zero(entry);
159 entry.version = ENTRY_VERSION;
160
161 if (!(card = pa_idxset_get_by_index(c->cards, idx)))
162 return;
163
164 if (!card->save_profile)
165 return;
166
167 pa_strlcpy(entry.profile, card->active_profile ? card->active_profile->name : "", sizeof(entry.profile));
168
169 if ((old = read_entry(u, card->name))) {
170
171 if (strncmp(old->profile, entry.profile, sizeof(entry.profile)) == 0) {
172 pa_xfree(old);
173 return;
174 }
175
176 pa_xfree(old);
177 }
178
179 key.data = card->name;
180 key.size = strlen(card->name);
181
182 data.data = &entry;
183 data.size = sizeof(entry);
184
185 pa_log_info("Storing profile for card %s.", card->name);
186
187 pa_database_set(u->database, &key, &data, TRUE);
188
189 trigger_save(u);
190 }
191
192 static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
193 struct entry *e;
194
195 pa_assert(new_data);
196
197 if ((e = read_entry(u, new_data->name)) && e->profile[0]) {
198
199 if (!new_data->active_profile) {
200 pa_card_new_data_set_profile(new_data, e->profile);
201 pa_log_info("Restoring profile for card %s.", new_data->name);
202 } else
203 pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
204
205 pa_xfree(e);
206 }
207
208 return PA_HOOK_OK;
209 }
210
211 int pa__init(pa_module*m) {
212 pa_modargs *ma = NULL;
213 struct userdata *u;
214 char *fname;
215 pa_card *card;
216 uint32_t idx;
217
218 pa_assert(m);
219
220 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
221 pa_log("Failed to parse module arguments");
222 goto fail;
223 }
224
225 m->userdata = u = pa_xnew(struct userdata, 1);
226 u->core = m->core;
227 u->module = m;
228 u->save_time_event = NULL;
229 u->database = NULL;
230
231 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u);
232
233 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);
234
235 if (!(fname = pa_state_path("card-database", TRUE)))
236 goto fail;
237
238 if (!(u->database = pa_database_open(fname, TRUE))) {
239 pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
240 pa_xfree(fname);
241 goto fail;
242 }
243
244 pa_log_info("Sucessfully opened database file '%s'.", fname);
245 pa_xfree(fname);
246
247 for (card = pa_idxset_first(m->core->cards, &idx); card; card = pa_idxset_next(m->core->cards, &idx))
248 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, card->index, u);
249
250 pa_modargs_free(ma);
251 return 0;
252
253 fail:
254 pa__done(m);
255
256 if (ma)
257 pa_modargs_free(ma);
258
259 return -1;
260 }
261
262 void pa__done(pa_module*m) {
263 struct userdata* u;
264
265 pa_assert(m);
266
267 if (!(u = m->userdata))
268 return;
269
270 if (u->subscription)
271 pa_subscription_free(u->subscription);
272
273 if (u->card_new_hook_slot)
274 pa_hook_slot_free(u->card_new_hook_slot);
275
276 if (u->save_time_event)
277 u->core->mainloop->time_free(u->save_time_event);
278
279 if (u->database)
280 pa_database_close(u->database);
281
282 pa_xfree(u);
283 }