]> code.delx.au - pulseaudio/blob - src/modules/module-volume-restore.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-volume-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
36 #include <pulse/xmalloc.h>
37 #include <pulse/volume.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
49 #include "module-volume-restore-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("Automatically restore the volume and the devices of streams")
53 PA_MODULE_USAGE("table=<filename>")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
55
56 #define WHITESPACE "\n\r \t"
57
58 #define DEFAULT_VOLUME_TABLE_FILE "volume-restore.table"
59
60 static const char* const valid_modargs[] = {
61 "table",
62 NULL,
63 };
64
65 struct rule {
66 char* name;
67 int volume_is_set;
68 pa_cvolume volume;
69 char *sink;
70 char *source;
71 };
72
73 struct userdata {
74 pa_hashmap *hashmap;
75 pa_subscription *subscription;
76 pa_hook_slot *sink_input_hook_slot, *source_output_hook_slot;
77 int modified;
78 char *table_file;
79 };
80
81 static pa_cvolume* parse_volume(const char *s, pa_cvolume *v) {
82 char *p;
83 long k;
84 unsigned i;
85
86 pa_assert(s);
87 pa_assert(v);
88
89 if (!isdigit(*s))
90 return NULL;
91
92 k = strtol(s, &p, 0);
93 if (k <= 0 || k > PA_CHANNELS_MAX)
94 return NULL;
95
96 v->channels = (unsigned) k;
97
98 for (i = 0; i < v->channels; i++) {
99 p += strspn(p, WHITESPACE);
100
101 if (!isdigit(*p))
102 return NULL;
103
104 k = strtol(p, &p, 0);
105
106 if (k < PA_VOLUME_MUTED)
107 return NULL;
108
109 v->values[i] = (pa_volume_t) k;
110 }
111
112 if (*p != 0)
113 return NULL;
114
115 return v;
116 }
117
118 static int load_rules(struct userdata *u) {
119 FILE *f;
120 int n = 0;
121 int ret = -1;
122 char buf_name[256], buf_volume[256], buf_sink[256], buf_source[256];
123 char *ln = buf_name;
124
125 f = u->table_file ?
126 fopen(u->table_file, "r") :
127 pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "r");
128
129 if (!f) {
130 if (errno == ENOENT) {
131 pa_log_info("starting with empty ruleset.");
132 ret = 0;
133 } else
134 pa_log("failed to open file '%s': %s", u->table_file, pa_cstrerror(errno));
135
136 goto finish;
137 }
138
139 pa_lock_fd(fileno(f), 1);
140
141 while (!feof(f)) {
142 struct rule *rule;
143 pa_cvolume v;
144 int v_is_set;
145
146 if (!fgets(ln, sizeof(buf_name), f))
147 break;
148
149 n++;
150
151 pa_strip_nl(ln);
152
153 if (ln[0] == '#')
154 continue;
155
156 if (ln == buf_name) {
157 ln = buf_volume;
158 continue;
159 }
160
161 if (ln == buf_volume) {
162 ln = buf_sink;
163 continue;
164 }
165
166 if (ln == buf_sink) {
167 ln = buf_source;
168 continue;
169 }
170
171 pa_assert(ln == buf_source);
172
173 if (buf_volume[0]) {
174 if (!parse_volume(buf_volume, &v)) {
175 pa_log("parse failure in %s:%u, stopping parsing", u->table_file, n);
176 goto finish;
177 }
178
179 v_is_set = 1;
180 } else
181 v_is_set = 0;
182
183 ln = buf_name;
184
185 if (pa_hashmap_get(u->hashmap, buf_name)) {
186 pa_log("double entry in %s:%u, ignoring", u->table_file, n);
187 continue;
188 }
189
190 rule = pa_xnew(struct rule, 1);
191 rule->name = pa_xstrdup(buf_name);
192 if ((rule->volume_is_set = v_is_set))
193 rule->volume = v;
194 rule->sink = buf_sink[0] ? pa_xstrdup(buf_sink) : NULL;
195 rule->source = buf_source[0] ? pa_xstrdup(buf_source) : NULL;
196
197 pa_hashmap_put(u->hashmap, rule->name, rule);
198 }
199
200 if (ln != buf_name) {
201 pa_log("invalid number of lines in %s.", u->table_file);
202 goto finish;
203 }
204
205 ret = 0;
206
207 finish:
208 if (f) {
209 pa_lock_fd(fileno(f), 0);
210 fclose(f);
211 }
212
213 return ret;
214 }
215
216 static int save_rules(struct userdata *u) {
217 FILE *f;
218 int ret = -1;
219 void *state = NULL;
220 struct rule *rule;
221
222 f = u->table_file ?
223 fopen(u->table_file, "w") :
224 pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "w");
225
226 if (!f) {
227 pa_log("failed to open file '%s': %s", u->table_file, pa_cstrerror(errno));
228 goto finish;
229 }
230
231 pa_lock_fd(fileno(f), 1);
232
233 while ((rule = pa_hashmap_iterate(u->hashmap, &state, NULL))) {
234 unsigned i;
235
236 fprintf(f, "%s\n", rule->name);
237
238 if (rule->volume_is_set) {
239 fprintf(f, "%u", rule->volume.channels);
240
241 for (i = 0; i < rule->volume.channels; i++)
242 fprintf(f, " %u", rule->volume.values[i]);
243 }
244
245 fprintf(f, "\n%s\n%s\n",
246 rule->sink ? rule->sink : "",
247 rule->source ? rule->source : "");
248 }
249
250 ret = 0;
251
252 finish:
253 if (f) {
254 pa_lock_fd(fileno(f), 0);
255 fclose(f);
256 }
257
258 return ret;
259 }
260
261 static char* client_name(pa_client *c) {
262 char *t, *e;
263
264 if (!c->name || !c->driver)
265 return NULL;
266
267 t = pa_sprintf_malloc("%s$%s", c->driver, c->name);
268 t[strcspn(t, "\n\r#")] = 0;
269
270 if (!*t) {
271 pa_xfree(t);
272 return NULL;
273 }
274
275 if ((e = strrchr(t, '('))) {
276 char *k = e + 1 + strspn(e + 1, "0123456789-");
277
278 /* Dirty trick: truncate all trailing parens with numbers in
279 * between, since they are usually used to identify multiple
280 * sessions of the same application, which is something we
281 * explicitly don't want. Besides other stuff this makes xmms
282 * with esound work properly for us. */
283
284 if (*k == ')' && *(k+1) == 0)
285 *e = 0;
286 }
287
288 return t;
289 }
290
291 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
292 struct userdata *u = userdata;
293 pa_sink_input *si = NULL;
294 pa_source_output *so = NULL;
295 struct rule *r;
296 char *name;
297
298 pa_assert(c);
299 pa_assert(u);
300
301 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
302 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
303 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
304 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
305 return;
306
307 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
308 if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
309 return;
310
311 if (!si->client || !(name = client_name(si->client)))
312 return;
313 } else {
314 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT);
315
316 if (!(so = pa_idxset_get_by_index(c->source_outputs, idx)))
317 return;
318
319 if (!so->client || !(name = client_name(so->client)))
320 return;
321 }
322
323 if ((r = pa_hashmap_get(u->hashmap, name))) {
324 pa_xfree(name);
325
326 if (si) {
327
328 if (!r->volume_is_set || !pa_cvolume_equal(pa_sink_input_get_volume(si), &r->volume)) {
329 pa_log_info("Saving volume for <%s>", r->name);
330 r->volume = *pa_sink_input_get_volume(si);
331 r->volume_is_set = 1;
332 u->modified = 1;
333 }
334
335 if (!r->sink || strcmp(si->sink->name, r->sink) != 0) {
336 pa_log_info("Saving sink for <%s>", r->name);
337 pa_xfree(r->sink);
338 r->sink = pa_xstrdup(si->sink->name);
339 u->modified = 1;
340 }
341 } else {
342 pa_assert(so);
343
344 if (!r->source || strcmp(so->source->name, r->source) != 0) {
345 pa_log_info("Saving source for <%s>", r->name);
346 pa_xfree(r->source);
347 r->source = pa_xstrdup(so->source->name);
348 u->modified = 1;
349 }
350 }
351
352 } else {
353 pa_log_info("Creating new entry for <%s>", name);
354
355 r = pa_xnew(struct rule, 1);
356 r->name = name;
357
358 if (si) {
359 r->volume = *pa_sink_input_get_volume(si);
360 r->volume_is_set = 1;
361 r->sink = pa_xstrdup(si->sink->name);
362 r->source = NULL;
363 } else {
364 pa_assert(so);
365 r->volume_is_set = 0;
366 r->sink = NULL;
367 r->source = pa_xstrdup(so->source->name);
368 }
369
370 pa_hashmap_put(u->hashmap, r->name, r);
371 u->modified = 1;
372 }
373 }
374
375 static pa_hook_result_t sink_input_hook_callback(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
376 struct rule *r;
377 char *name;
378
379 pa_assert(data);
380
381 if (!data->client || !(name = client_name(data->client)))
382 return PA_HOOK_OK;
383
384 if ((r = pa_hashmap_get(u->hashmap, name))) {
385
386 if (r->volume_is_set && data->sample_spec.channels == r->volume.channels) {
387 pa_log_info("Restoring volume for <%s>", r->name);
388 pa_sink_input_new_data_set_volume(data, &r->volume);
389 }
390
391 if (!data->sink && r->sink) {
392 if ((data->sink = pa_namereg_get(c, r->sink, PA_NAMEREG_SINK, 1)))
393 pa_log_info("Restoring sink for <%s>", r->name);
394 }
395 }
396
397 pa_xfree(name);
398
399 return PA_HOOK_OK;
400 }
401
402 static pa_hook_result_t source_output_hook_callback(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
403 struct rule *r;
404 char *name;
405
406 pa_assert(data);
407
408 if (!data->client || !(name = client_name(data->client)))
409 return PA_HOOK_OK;
410
411 if ((r = pa_hashmap_get(u->hashmap, name))) {
412 if (!data->source && r->source) {
413 if ((data->source = pa_namereg_get(c, r->source, PA_NAMEREG_SOURCE, 1)))
414 pa_log_info("Restoring source for <%s>", r->name);
415 }
416 }
417
418 return PA_HOOK_OK;
419 }
420
421 int pa__init(pa_module*m) {
422 pa_modargs *ma = NULL;
423 struct userdata *u;
424
425 pa_assert(m);
426
427 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
428 pa_log("Failed to parse module arguments");
429 goto fail;
430 }
431
432 u = pa_xnew(struct userdata, 1);
433 u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
434 u->subscription = NULL;
435 u->table_file = pa_xstrdup(pa_modargs_get_value(ma, "table", NULL));
436 u->modified = 0;
437 u->sink_input_hook_slot = u->source_output_hook_slot = NULL;
438
439 m->userdata = u;
440
441 if (load_rules(u) < 0)
442 goto fail;
443
444 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
445 u->sink_input_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], (pa_hook_cb_t) sink_input_hook_callback, u);
446 u->source_output_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], (pa_hook_cb_t) source_output_hook_callback, u);
447
448 pa_modargs_free(ma);
449 return 0;
450
451 fail:
452 pa__done(m);
453 if (ma)
454 pa_modargs_free(ma);
455
456 return -1;
457 }
458
459 static void free_func(void *p, void *userdata) {
460 struct rule *r = p;
461 pa_assert(r);
462
463 pa_xfree(r->name);
464 pa_xfree(r->sink);
465 pa_xfree(r->source);
466 pa_xfree(r);
467 }
468
469 void pa__done(pa_module*m) {
470 struct userdata* u;
471
472 pa_assert(m);
473
474 if (!(u = m->userdata))
475 return;
476
477 if (u->subscription)
478 pa_subscription_free(u->subscription);
479
480 if (u->sink_input_hook_slot)
481 pa_hook_slot_free(u->sink_input_hook_slot);
482 if (u->source_output_hook_slot)
483 pa_hook_slot_free(u->source_output_hook_slot);
484
485 if (u->hashmap) {
486
487 if (u->modified)
488 save_rules(u);
489
490 pa_hashmap_free(u->hashmap, free_func, NULL);
491 }
492
493 pa_xfree(u->table_file);
494 pa_xfree(u);
495 }
496
497