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