database: port restore modules to new database API
[profile/ivi/pulseaudio-panda.git] / src / modules / module-device-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/sink-input.h>
46 #include <pulsecore/source-output.h>
47 #include <pulsecore/namereg.h>
48 #include <pulsecore/database.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 PA_MODULE_USAGE(
57         "restore_volume=<Save/restore volumes?> "
58         "restore_muted=<Save/restore muted states?>");
59
60 #define SAVE_INTERVAL 10
61
62 static const char* const valid_modargs[] = {
63     "restore_volume",
64     "restore_muted",
65     NULL
66 };
67
68 struct userdata {
69     pa_core *core;
70     pa_module *module;
71     pa_subscription *subscription;
72     pa_hook_slot
73         *sink_fixate_hook_slot,
74         *source_fixate_hook_slot;
75     pa_time_event *save_time_event;
76     pa_database *database;
77
78     pa_bool_t restore_volume:1;
79     pa_bool_t restore_muted:1;
80 };
81
82 #define ENTRY_VERSION 1
83
84 struct entry {
85     uint8_t version;
86     pa_bool_t muted:1;
87     pa_channel_map channel_map;
88     pa_cvolume volume;
89 } PA_GCC_PACKED;
90
91 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
92     struct userdata *u = userdata;
93
94     pa_assert(a);
95     pa_assert(e);
96     pa_assert(tv);
97     pa_assert(u);
98
99     pa_assert(e == u->save_time_event);
100     u->core->mainloop->time_free(u->save_time_event);
101     u->save_time_event = NULL;
102
103     pa_database_sync(u->database);
104     pa_log_info("Synced.");
105 }
106
107 static struct entry* read_entry(struct userdata *u, const char *name) {
108     pa_datum key, data;
109     struct entry *e;
110
111     pa_assert(u);
112     pa_assert(name);
113
114     key.data = (char*) name;
115     key.size = strlen(name);
116
117     pa_zero(data);
118
119     if (!pa_database_get(u->database, &key, &data))
120         goto fail;
121
122     if (data.size != sizeof(struct entry)) {
123         pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
124         goto fail;
125     }
126
127     e = (struct entry*) data.data;
128
129     if (e->version != ENTRY_VERSION) {
130         pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name);
131         goto fail;
132     }
133
134     if (!(pa_cvolume_valid(&e->volume))) {
135         pa_log_warn("Invalid volume stored in database for device %s", name);
136         goto fail;
137     }
138
139     if (!(pa_channel_map_valid(&e->channel_map))) {
140         pa_log_warn("Invalid channel map stored in database for device %s", name);
141         goto fail;
142     }
143
144     if (e->volume.channels != e->channel_map.channels) {
145         pa_log_warn("Volume and channel map don't match in database entry for device %s", name);
146         goto fail;
147     }
148
149     return e;
150
151 fail:
152
153     pa_datum_free(&data);
154     return NULL;
155 }
156
157 static void trigger_save(struct userdata *u) {
158     struct timeval tv;
159
160     if (u->save_time_event)
161         return;
162
163     pa_gettimeofday(&tv);
164     tv.tv_sec += SAVE_INTERVAL;
165     u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
166 }
167
168 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
169     struct userdata *u = userdata;
170     struct entry entry, *old;
171     char *name;
172     pa_datum key, data;
173
174     pa_assert(c);
175     pa_assert(u);
176
177     if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
178         t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
179         t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
180         t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
181         return;
182
183     pa_zero(entry);
184     entry.version = ENTRY_VERSION;
185
186     if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
187         pa_sink *sink;
188
189         if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
190             return;
191
192         name = pa_sprintf_malloc("sink:%s", sink->name);
193         entry.channel_map = sink->channel_map;
194         entry.volume = *pa_sink_get_volume(sink, FALSE, TRUE);
195         entry.muted = pa_sink_get_mute(sink, FALSE);
196
197     } else {
198         pa_source *source;
199
200         pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
201
202         if (!(source = pa_idxset_get_by_index(c->sources, idx)))
203             return;
204
205         name = pa_sprintf_malloc("source:%s", source->name);
206         entry.channel_map = source->channel_map;
207         entry.volume = *pa_source_get_volume(source, FALSE);
208         entry.muted = pa_source_get_mute(source, FALSE);
209     }
210
211     if ((old = read_entry(u, name))) {
212
213         if (pa_cvolume_equal(pa_cvolume_remap(&old->volume, &old->channel_map, &entry.channel_map), &entry.volume) &&
214             !old->muted == !entry.muted) {
215
216             pa_xfree(old);
217             pa_xfree(name);
218             return;
219         }
220
221         pa_xfree(old);
222     }
223
224     key.data = name;
225     key.size = strlen(name);
226
227     data.data = &entry;
228     data.size = sizeof(entry);
229
230     pa_log_info("Storing volume/mute for device %s.", name);
231
232     pa_database_set(u->database, &key, &data, TRUE);
233
234     pa_xfree(name);
235
236     trigger_save(u);
237 }
238
239 static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
240     char *name;
241     struct entry *e;
242
243     pa_assert(new_data);
244
245     name = pa_sprintf_malloc("sink:%s", new_data->name);
246
247     if ((e = read_entry(u, name))) {
248
249         if (u->restore_volume) {
250
251             if (!new_data->volume_is_set) {
252                 pa_log_info("Restoring volume for sink %s.", new_data->name);
253                 pa_sink_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
254             } else
255                 pa_log_debug("Not restoring volume for sink %s, because already set.", new_data->name);
256
257         }
258
259         if (u->restore_muted) {
260
261             if (!new_data->muted_is_set) {
262                 pa_log_info("Restoring mute state for sink %s.", new_data->name);
263                 pa_sink_new_data_set_muted(new_data, e->muted);
264             } else
265                 pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name);
266         }
267
268         pa_xfree(e);
269     }
270
271     pa_xfree(name);
272
273     return PA_HOOK_OK;
274 }
275
276 static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
277     char *name;
278     struct entry *e;
279
280     pa_assert(new_data);
281
282     name = pa_sprintf_malloc("source:%s", new_data->name);
283
284     if ((e = read_entry(u, name))) {
285
286         if (u->restore_volume) {
287
288             if (!new_data->volume_is_set) {
289                 pa_log_info("Restoring volume for source %s.", new_data->name);
290                 pa_source_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
291             } else
292                 pa_log_debug("Not restoring volume for source %s, because already set.", new_data->name);
293         }
294
295         if (u->restore_muted) {
296
297             if (!new_data->muted_is_set) {
298                 pa_log_info("Restoring mute state for source %s.", new_data->name);
299                 pa_source_new_data_set_muted(new_data, e->muted);
300             } else
301                 pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name);
302         }
303
304         pa_xfree(e);
305     }
306
307     pa_xfree(name);
308
309     return PA_HOOK_OK;
310 }
311
312 int pa__init(pa_module*m) {
313     pa_modargs *ma = NULL;
314     struct userdata *u;
315     char *fname;
316     pa_sink *sink;
317     pa_source *source;
318     uint32_t idx;
319     pa_bool_t restore_volume = TRUE, restore_muted = TRUE;
320
321     pa_assert(m);
322
323     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
324         pa_log("Failed to parse module arguments");
325         goto fail;
326     }
327
328     if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
329         pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
330         pa_log("restore_volume= and restore_muted= expect boolean arguments");
331         goto fail;
332     }
333
334     if (!restore_muted && !restore_volume)
335         pa_log_warn("Neither restoring volume nor restoring muted enabled!");
336
337     m->userdata = u = pa_xnew(struct userdata, 1);
338     u->core = m->core;
339     u->module = m;
340     u->save_time_event = NULL;
341     u->restore_volume = restore_volume;
342     u->restore_muted = restore_muted;
343     u->database = NULL;
344
345     u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
346
347     if (restore_muted || restore_volume) {
348         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);
349         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);
350     }
351
352     if (!(fname = pa_state_path("device-volumes", TRUE)))
353         goto fail;
354
355     if (!(u->database = pa_database_open(fname, TRUE))) {
356         pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
357         pa_xfree(fname);
358         goto fail;
359     }
360
361     pa_log_info("Sucessfully opened database file '%s'.", fname);
362     pa_xfree(fname);
363
364     for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
365         subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
366
367     for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
368         subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
369
370     pa_modargs_free(ma);
371     return 0;
372
373 fail:
374     pa__done(m);
375
376     if (ma)
377         pa_modargs_free(ma);
378
379     return  -1;
380 }
381
382 void pa__done(pa_module*m) {
383     struct userdata* u;
384
385     pa_assert(m);
386
387     if (!(u = m->userdata))
388         return;
389
390     if (u->subscription)
391         pa_subscription_free(u->subscription);
392
393     if (u->sink_fixate_hook_slot)
394         pa_hook_slot_free(u->sink_fixate_hook_slot);
395     if (u->source_fixate_hook_slot)
396         pa_hook_slot_free(u->source_fixate_hook_slot);
397
398     if (u->save_time_event)
399         u->core->mainloop->time_free(u->save_time_event);
400
401     if (u->database)
402         pa_database_close(u->database);
403
404     pa_xfree(u);
405 }