]> code.delx.au - pulseaudio/blob - src/modules/module-device-restore.c
Merge branch 'master' of git://git.0pointer.de/pulseaudio
[pulseaudio] / src / modules / module-device-restore.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 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 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 #include <gdbm.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.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/sink-input.h>
47 #include <pulsecore/source-output.h>
48 #include <pulsecore/namereg.h>
49
50 #include "module-device-restore-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(TRUE);
56
57 #define SAVE_INTERVAL 10
58
59 static const char* const valid_modargs[] = {
60 "restore_volume",
61 "restore_muted",
62 NULL
63 };
64
65 struct userdata {
66 pa_core *core;
67 pa_subscription *subscription;
68 pa_hook_slot
69 *sink_fixate_hook_slot,
70 *source_fixate_hook_slot;
71 pa_time_event *save_time_event;
72 GDBM_FILE gdbm_file;
73
74 pa_bool_t restore_volume:1;
75 pa_bool_t restore_muted:1;
76 };
77
78 struct entry {
79 pa_cvolume volume;
80 pa_bool_t muted:1;
81 };
82
83 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
84 struct userdata *u = userdata;
85
86 pa_assert(a);
87 pa_assert(e);
88 pa_assert(tv);
89 pa_assert(u);
90
91 pa_assert(e == u->save_time_event);
92 u->core->mainloop->time_free(u->save_time_event);
93 u->save_time_event = NULL;
94
95 gdbm_sync(u->gdbm_file);
96 pa_log_info("Synced.");
97 }
98
99 static struct entry* read_entry(struct userdata *u, char *name) {
100 datum key, data;
101 struct entry *e;
102
103 pa_assert(u);
104 pa_assert(name);
105
106 key.dptr = name;
107 key.dsize = strlen(name);
108
109 data = gdbm_fetch(u->gdbm_file, key);
110
111 if (!data.dptr)
112 goto fail;
113
114 if (data.dsize != sizeof(struct entry)) {
115 pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
116 goto fail;
117 }
118
119 e = (struct entry*) data.dptr;
120
121 if (!(pa_cvolume_valid(&e->volume))) {
122 pa_log_warn("Invalid volume stored in database for device %s", name);
123 goto fail;
124 }
125
126 return e;
127
128 fail:
129
130 pa_xfree(data.dptr);
131 return NULL;
132 }
133
134 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
135 struct userdata *u = userdata;
136 struct entry entry, *old;
137 char *name;
138 datum key, data;
139
140 pa_assert(c);
141 pa_assert(u);
142
143 if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
144 t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
145 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
146 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
147 return;
148
149 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
150 pa_sink *sink;
151
152 if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
153 return;
154
155 name = pa_sprintf_malloc("sink:%s", sink->name);
156 entry.volume = *pa_sink_get_volume(sink);
157 entry.muted = pa_sink_get_mute(sink);
158
159 } else {
160 pa_source *source;
161
162 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
163
164 if (!(source = pa_idxset_get_by_index(c->sources, idx)))
165 return;
166
167 name = pa_sprintf_malloc("source:%s", source->name);
168 entry.volume = *pa_source_get_volume(source);
169 entry.muted = pa_source_get_mute(source);
170 }
171
172 if ((old = read_entry(u, name))) {
173
174 if (pa_cvolume_equal(&old->volume, &entry.volume) &&
175 !old->muted == !entry.muted) {
176
177 pa_xfree(old);
178 pa_xfree(name);
179 return;
180 }
181
182 pa_xfree(old);
183 }
184
185 key.dptr = name;
186 key.dsize = strlen(name);
187
188 data.dptr = (void*) &entry;
189 data.dsize = sizeof(entry);
190
191 pa_log_info("Storing volume/mute for device %s.", name);
192
193 gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
194
195 if (!u->save_time_event) {
196 struct timeval tv;
197 pa_gettimeofday(&tv);
198 tv.tv_sec += SAVE_INTERVAL;
199 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
200 }
201
202 pa_xfree(name);
203 }
204
205 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
206 char *name;
207 struct entry *e;
208
209 pa_assert(new_data);
210
211 name = pa_sprintf_malloc("sink:%s", new_data->name);
212
213 if ((e = read_entry(u, name))) {
214
215 if (u->restore_volume &&
216 e->volume.channels == new_data->sample_spec.channels) {
217
218 pa_log_info("Restoring volume for sink %s.", new_data->name);
219 pa_sink_new_data_set_volume(new_data, &e->volume);
220 }
221
222 if (u->restore_muted) {
223 pa_log_info("Restoring mute state for sink %s.", new_data->name);
224 pa_sink_new_data_set_muted(new_data, e->muted);
225 }
226
227 pa_xfree(e);
228 }
229
230 pa_xfree(name);
231
232 return PA_HOOK_OK;
233 }
234
235 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
236 char *name;
237 struct entry *e;
238
239 pa_assert(new_data);
240
241 name = pa_sprintf_malloc("source:%s", new_data->name);
242
243 if ((e = read_entry(u, name))) {
244
245 if (u->restore_volume &&
246 e->volume.channels == new_data->sample_spec.channels) {
247
248 pa_log_info("Restoring volume for source %s.", new_data->name);
249 pa_source_new_data_set_volume(new_data, &e->volume);
250 }
251
252 if (u->restore_muted) {
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 }
256
257 pa_xfree(e);
258 }
259
260 pa_xfree(name);
261
262 return PA_HOOK_OK;
263 }
264
265 int pa__init(pa_module*m) {
266 pa_modargs *ma = NULL;
267 struct userdata *u;
268 char *fname, *fn;
269 char hn[256];
270 pa_sink *sink;
271 pa_source *source;
272 uint32_t idx;
273 pa_bool_t restore_volume = TRUE, restore_muted = TRUE;
274
275 pa_assert(m);
276
277 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
278 pa_log("Failed to parse module arguments");
279 goto fail;
280 }
281
282 if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
283 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
284 pa_log("restore_volume= and restore_muted= expect boolean arguments");
285 goto fail;
286 }
287
288 if (!restore_muted && !restore_volume)
289 pa_log_warn("Neither restoring volume nor restoring muted enabled!");
290
291 m->userdata = u = pa_xnew(struct userdata, 1);
292 u->core = m->core;
293 u->save_time_event = NULL;
294 u->restore_volume = restore_volume;
295 u->restore_muted = restore_muted;
296
297 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
298
299 if (restore_muted || restore_volume) {
300 u->sink_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_fixate_hook_callback, u);
301 u->source_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) source_fixate_hook_callback, u);
302 }
303
304 if (!pa_get_host_name(hn, sizeof(hn)))
305 goto fail;
306
307 fn = pa_sprintf_malloc("device-volumes.%s."CANONICAL_HOST".gdbm", hn);
308 fname = pa_state_path(fn);
309 pa_xfree(fn);
310
311 if (!fname)
312 goto fail;
313
314 if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT, 0600, NULL))) {
315 pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
316 pa_xfree(fname);
317 goto fail;
318 }
319
320 pa_log_info("Sucessfully opened database file '%s'.", fname);
321 pa_xfree(fname);
322
323 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
324 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
325
326 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
327 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
328
329 pa_modargs_free(ma);
330 return 0;
331
332 fail:
333 pa__done(m);
334
335 if (ma)
336 pa_modargs_free(ma);
337
338 return -1;
339 }
340
341 void pa__done(pa_module*m) {
342 struct userdata* u;
343
344 pa_assert(m);
345
346 if (!(u = m->userdata))
347 return;
348
349 if (u->subscription)
350 pa_subscription_free(u->subscription);
351
352 if (u->sink_fixate_hook_slot)
353 pa_hook_slot_free(u->sink_fixate_hook_slot);
354 if (u->source_fixate_hook_slot)
355 pa_hook_slot_free(u->source_fixate_hook_slot);
356
357 if (u->save_time_event)
358 u->core->mainloop->time_free(u->save_time_event);
359
360 if (u->gdbm_file)
361 gdbm_close(u->gdbm_file);
362
363 pa_xfree(u);
364 }