]> code.delx.au - pulseaudio/blob - src/modules/module-device-restore.c
Yes, yet another evil all-in-one commit of intervowen changes. I suck.
[pulseaudio] / src / modules / module-device-restore.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <unistd.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <gdbm.h>
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/volume.h>
39 #include <pulse/timeval.h>
40 #include <pulse/util.h>
41
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/core-subscribe.h>
48 #include <pulsecore/sink-input.h>
49 #include <pulsecore/source-output.h>
50 #include <pulsecore/namereg.h>
51
52 #include "module-device-restore-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(TRUE);
58
59 #define SAVE_INTERVAL 10
60
61 static const char* const valid_modargs[] = {
62 NULL,
63 };
64
65 struct userdata {
66 pa_core *core;
67 pa_subscription *subscription;
68 pa_hook_slot *sink_fixate_hook_slot, *source_fixate_hook_slot;
69 pa_time_event *save_time_event;
70 GDBM_FILE gdbm_file;
71 };
72
73 struct entry {
74 pa_cvolume volume;
75 int muted;
76 };
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 gdbm_sync(u->gdbm_file);
91 pa_log_info("Synced.");
92 }
93
94 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
95 struct userdata *u = userdata;
96 struct entry entry;
97 char *name;
98 datum key, data;
99
100 pa_assert(c);
101 pa_assert(u);
102
103 if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
104 t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
105 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
106 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
107 return;
108
109 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
110 pa_sink *sink;
111
112 if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
113 return;
114
115 name = pa_sprintf_malloc("sink:%s", sink->name);
116 entry.volume = *pa_sink_get_volume(sink);
117 entry.muted = pa_sink_get_mute(sink);
118
119 } else {
120 pa_source *source;
121
122 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
123
124 if (!(source = pa_idxset_get_by_index(c->sources, idx)))
125 return;
126
127 name = pa_sprintf_malloc("source:%s", source->name);
128 entry.volume = *pa_source_get_volume(source);
129 entry.muted = pa_source_get_mute(source);
130 }
131
132 key.dptr = name;
133 key.dsize = strlen(name);
134
135 data = gdbm_fetch(u->gdbm_file, key);
136
137 if (data.dptr) {
138
139 if (data.dsize == sizeof(struct entry)) {
140 struct entry *old = (struct entry*) data.dptr;
141
142 if (pa_cvolume_valid(&old->volume)) {
143
144 if (pa_cvolume_equal(&old->volume, &entry.volume) &&
145 !old->muted == !entry.muted) {
146
147 pa_xfree(data.dptr);
148 pa_xfree(name);
149 return;
150 }
151 } else
152 pa_log_warn("Invalid volume stored in database for device %s", name);
153
154 } else
155 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
156
157 pa_xfree(data.dptr);
158 }
159
160 data.dptr = (void*) &entry;
161 data.dsize = sizeof(entry);
162
163 pa_log_info("Storing volume/mute for device %s.", name);
164
165 gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
166
167 if (!u->save_time_event) {
168 struct timeval tv;
169 pa_gettimeofday(&tv);
170 tv.tv_sec += SAVE_INTERVAL;
171 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
172 }
173
174 pa_xfree(name);
175 }
176
177 static struct entry* read_entry(struct userdata *u, char *name) {
178 datum key, data;
179 struct entry *e;
180
181 pa_assert(u);
182 pa_assert(name);
183
184 key.dptr = name;
185 key.dsize = strlen(name);
186
187 data = gdbm_fetch(u->gdbm_file, key);
188
189 if (!data.dptr)
190 goto fail;
191
192 if (data.dsize != sizeof(struct entry)) {
193 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
194 goto fail;
195 }
196
197 e = (struct entry*) data.dptr;
198
199 if (!(pa_cvolume_valid(&e->volume))) {
200 pa_log_warn("Invalid volume stored in database for device %s", name);
201 goto fail;
202 }
203
204 return e;
205
206 fail:
207
208 pa_xfree(data.dptr);
209 return NULL;
210 }
211
212
213 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
214 char *name;
215 struct entry *e;
216
217 pa_assert(new_data);
218
219 name = pa_sprintf_malloc("sink:%s", new_data->name);
220
221 if ((e = read_entry(u, name))) {
222
223 if (e->volume.channels == new_data->sample_spec.channels) {
224 pa_log_info("Restoring volume for sink %s.", new_data->name);
225 pa_sink_new_data_set_volume(new_data, &e->volume);
226 }
227
228 pa_log_info("Restoring mute state for sink %s.", new_data->name);
229 pa_sink_new_data_set_muted(new_data, e->muted);
230 pa_xfree(e);
231 }
232
233 pa_xfree(name);
234
235 return PA_HOOK_OK;
236 }
237
238 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
239 char *name;
240 struct entry *e;
241
242 pa_assert(new_data);
243
244 name = pa_sprintf_malloc("source:%s", new_data->name);
245
246 if ((e = read_entry(u, name))) {
247
248 if (e->volume.channels == new_data->sample_spec.channels) {
249 pa_log_info("Restoring volume for source %s.", new_data->name);
250 pa_source_new_data_set_volume(new_data, &e->volume);
251 }
252
253 pa_log_info("Restoring mute state for source %s.", new_data->name);
254 pa_source_new_data_set_muted(new_data, e->muted);
255 pa_xfree(e);
256 }
257
258 pa_xfree(name);
259
260 return PA_HOOK_OK;
261 }
262
263 int pa__init(pa_module*m) {
264 pa_modargs *ma = NULL;
265 struct userdata *u;
266 char *fname, *runtime_dir;
267 char hn[256];
268 pa_sink *sink;
269 pa_source *source;
270 uint32_t idx;
271
272 pa_assert(m);
273
274 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
275 pa_log("Failed to parse module arguments");
276 goto fail;
277 }
278
279 u = pa_xnew(struct userdata, 1);
280 u->core = m->core;
281 u->save_time_event = NULL;
282
283 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
284
285 u->sink_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_FIXATE], (pa_hook_cb_t) sink_fixate_hook_callback, u);
286 u->source_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], (pa_hook_cb_t) source_fixate_hook_callback, u);
287
288 m->userdata = u;
289
290 if (!pa_get_host_name(hn, sizeof(hn)))
291 goto fail;
292
293 if (!(runtime_dir = pa_get_runtime_dir()))
294 goto fail;
295
296 fname = pa_sprintf_malloc("%s/device-volumes.%s.gdbm", runtime_dir, hn);
297 pa_xfree(runtime_dir);
298
299 if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT, 0600, NULL))) {
300 pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
301 pa_xfree(fname);
302 goto fail;
303 }
304
305 pa_log_info("Sucessfully opened database file '%s'.", fname);
306 pa_xfree(fname);
307
308 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
309 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
310
311 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
312 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
313
314 pa_modargs_free(ma);
315 return 0;
316
317 fail:
318 pa__done(m);
319
320 if (ma)
321 pa_modargs_free(ma);
322
323 return -1;
324 }
325
326 void pa__done(pa_module*m) {
327 struct userdata* u;
328
329 pa_assert(m);
330
331 if (!(u = m->userdata))
332 return;
333
334 if (u->subscription)
335 pa_subscription_free(u->subscription);
336
337 if (u->sink_fixate_hook_slot)
338 pa_hook_slot_free(u->sink_fixate_hook_slot);
339 if (u->source_fixate_hook_slot)
340 pa_hook_slot_free(u->source_fixate_hook_slot);
341
342 if (u->save_time_event)
343 u->core->mainloop->time_free(u->save_time_event);
344
345 if (u->gdbm_file)
346 gdbm_close(u->gdbm_file);
347
348 pa_xfree(u);
349 }