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