add proper locking when accessing the file volume.table
[profile/ivi/pulseaudio-panda.git] / src / modules / module-volume-restore.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio 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   polypaudio 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 polypaudio; 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 <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #include <polypcore/module.h>
36 #include <polypcore/util.h>
37 #include <polypcore/modargs.h>
38 #include <polypcore/log.h>
39 #include <polypcore/core-subscribe.h>
40 #include <polypcore/xmalloc.h>
41 #include <polypcore/sink-input.h>
42 #include <polypcore/util.h>
43 #include <polyp/volume.h>
44
45 #include "module-volume-restore-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering")
48 PA_MODULE_DESCRIPTION("Playback stream automatic volume restore module")
49 PA_MODULE_USAGE("table=<filename>")
50 PA_MODULE_VERSION(PACKAGE_VERSION)
51
52 #define WHITESPACE "\n\r \t"
53
54 #define DEFAULT_VOLUME_TABLE_FILE ".polypaudio/volume.table"
55
56 static const char* const valid_modargs[] = {
57     "table",
58     NULL,
59 };
60
61 struct rule {
62     char* name;
63     pa_cvolume volume;
64 };
65
66 struct userdata {
67     pa_hashmap *hashmap;
68     pa_subscription *subscription;
69     int modified;
70     char *table_file;
71 };
72
73 static pa_cvolume* parse_volume(const char *s, pa_cvolume *v) {
74     char *p;
75     long k;
76     unsigned i;
77     
78     assert(s);
79     assert(v);
80
81     if (!isdigit(*s))
82         return NULL;
83
84     k = strtol(s, &p, 0);
85     if (k <= 0 || k > PA_CHANNELS_MAX)
86         return NULL;
87     
88     v->channels = (unsigned) k;
89
90     for (i = 0; i < v->channels; i++) {
91         p += strspn(p, WHITESPACE);
92
93         if (!isdigit(*p))
94             return NULL;
95
96         k = strtol(p, &p, 0);
97
98         if (k < PA_VOLUME_MUTED)
99             return NULL;
100         
101         v->values[i] = (pa_volume_t) k;
102     }
103
104     if (*p != 0)
105         return NULL;
106
107     return v;
108 }
109
110 static int load_rules(struct userdata *u) {
111     FILE *f;
112     int n = 0;
113     int ret = -1;
114     char buf_name[256], buf_volume[256];
115     char *ln = buf_name;
116
117     f = u->table_file ?
118         fopen(u->table_file, "r") :
119         pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "r");
120
121     if (!f) {
122         if (errno == ENOENT) {
123             pa_log_info(__FILE__": starting with empty ruleset.");
124             ret = 0;
125         } else
126             pa_log(__FILE__": failed to open file '%s': %s", u->table_file, strerror(errno));
127         
128         goto finish;
129     }
130
131     pa_lock_fd(fileno(f), 1);
132     
133     while (!feof(f)) {
134         struct rule *rule;
135         pa_cvolume v;
136         
137         if (!fgets(ln, sizeof(buf_name), f))
138             break;
139
140         n++;
141         
142         pa_strip_nl(ln);
143
144         if (ln[0] == '#' || !*ln )
145             continue;
146
147         if (ln == buf_name) {
148             ln = buf_volume;
149             continue;
150         }
151
152         assert(ln == buf_volume);
153
154         if (!parse_volume(buf_volume, &v)) {
155             pa_log(__FILE__": parse failure in %s:%u, stopping parsing", u->table_file, n);
156             goto finish;
157         }
158
159         ln = buf_name;
160         
161         if (pa_hashmap_get(u->hashmap, buf_name)) {
162             pa_log(__FILE__": double entry in %s:%u, ignoring", u->table_file, n);
163             goto finish;
164         }
165         
166         rule = pa_xnew(struct rule, 1);
167         rule->name = pa_xstrdup(buf_name);
168         rule->volume = v;
169
170         pa_hashmap_put(u->hashmap, rule->name, rule);
171     }
172
173     if (ln == buf_volume) {
174         pa_log(__FILE__": invalid number of lines in %s.", u->table_file);
175         goto finish;
176     }
177
178     ret = 0;
179     
180 finish:
181     if (f) {
182         pa_lock_fd(fileno(f), 0);
183         fclose(f);
184     }
185
186     return ret;
187 }
188
189 static int save_rules(struct userdata *u) {
190     FILE *f;
191     int ret = -1;
192     void *state = NULL;
193     struct rule *rule;
194     
195     f = u->table_file ?
196         fopen(u->table_file, "w") :
197         pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "w");
198
199     if (!f) {
200         pa_log(__FILE__": failed to open file '%s': %s", u->table_file, strerror(errno));
201         goto finish;
202     }
203
204     pa_lock_fd(fileno(f), 1);
205
206     while ((rule = pa_hashmap_iterate(u->hashmap, &state, NULL))) {
207         unsigned i;
208         
209         fprintf(f, "%s\n%u", rule->name, rule->volume.channels);
210         
211         for (i = 0; i < rule->volume.channels; i++)
212             fprintf(f, " %u", rule->volume.values[i]);
213
214         fprintf(f, "\n");
215     }
216     
217     ret = 0;
218     
219 finish:
220     if (f) {
221         pa_lock_fd(fileno(f), 0);
222         fclose(f);
223     }
224
225     return ret;
226 }
227
228 static char* client_name(pa_client *c) {
229     char *t, *e;
230     
231     if (!c->name || !c->driver)
232         return NULL;
233
234     t = pa_sprintf_malloc("%s$%s", c->driver, c->name);
235     t[strcspn(t, "\n\r#")] = 0;
236
237     if (!*t)
238         return NULL;
239
240     if ((e = strrchr(t, '('))) {
241         char *k = e + 1 + strspn(e + 1, "0123456789-");
242
243         /* Dirty trick: truncate all trailing parens with numbers in
244          * between, since they are usually used to identify multiple
245          * sessions of the same application, which is something we
246          * explicitly don't want. Besides other stuff this makes xmms
247          * with esound work properly for us. */
248         
249         if (*k == ')' && *(k+1) == 0)
250             *e = 0;
251     }
252     
253     return t;
254 }
255
256 static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
257     struct userdata *u =  userdata;
258     pa_sink_input *si;
259     struct rule *r;
260     char *name;
261     
262     assert(c);
263     assert(u);
264
265     if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
266         t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
267         return;
268         
269     if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
270         return;
271     
272     if (!si->client || !(name = client_name(si->client)))
273         return;
274
275     if ((r = pa_hashmap_get(u->hashmap, name))) {
276         pa_xfree(name);
277
278         if (((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) && si->sample_spec.channels == r->volume.channels) {
279             pa_log_info(__FILE__": Restoring volume for <%s>", r->name);
280             pa_sink_input_set_volume(si, &r->volume);
281         } else if (!pa_cvolume_equal(pa_sink_input_get_volume(si), &r->volume)) {
282             pa_log_info(__FILE__": Saving volume for <%s>", r->name);
283             r->volume = *pa_sink_input_get_volume(si);
284             u->modified = 1;
285         }
286         
287     } else {
288         pa_log_info(__FILE__": Creating new entry for <%s>", name);
289
290         r = pa_xnew(struct rule, 1);
291         r->name = name;
292         r->volume = *pa_sink_input_get_volume(si);
293         pa_hashmap_put(u->hashmap, r->name, r);
294
295         u->modified = 1;
296     }
297 }
298
299 int pa__init(pa_core *c, pa_module*m) {
300     pa_modargs *ma = NULL;
301     struct userdata *u;
302     
303     assert(c);
304     assert(m);
305
306     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
307         pa_log(__FILE__": Failed to parse module arguments");
308         goto fail;
309     }
310
311     u = pa_xnew(struct userdata, 1);
312     u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
313     u->subscription = NULL;
314     u->table_file = pa_xstrdup(pa_modargs_get_value(ma, "table", NULL));
315     u->modified = 0;
316     
317     m->userdata = u;
318     
319     if (load_rules(u) < 0)
320         goto fail;
321
322     u->subscription = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
323
324     pa_modargs_free(ma);
325     return 0;
326
327 fail:
328     pa__done(c, m);
329
330     if (ma)
331         pa_modargs_free(ma);
332     
333     return  -1;
334 }
335
336 static void free_func(void *p, void *userdata) {
337     struct rule *r = p;
338     assert(r);
339
340     pa_xfree(r->name);
341     pa_xfree(r);
342 }
343
344 void pa__done(pa_core *c, pa_module*m) {
345     struct userdata* u;
346     
347     assert(c);
348     assert(m);
349
350     if (!(u = m->userdata))
351         return;
352
353     if (u->subscription)
354         pa_subscription_free(u->subscription);
355
356     if (u->hashmap) {
357
358         if (u->modified)
359             save_rules(u);
360         
361         pa_hashmap_free(u->hashmap, free_func, NULL);
362     }
363
364     pa_xfree(u->table_file);
365     pa_xfree(u);
366 }
367
368