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