4 This file is part of polypaudio.
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.
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.
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
30 #include <sys/types.h>
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>
45 #include "module-volume-restore-symdef.h"
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)
52 #define WHITESPACE "\n\r \t"
54 #define DEFAULT_VOLUME_TABLE_FILE ".polypaudio/volume.table"
56 static const char* const valid_modargs[] = {
68 pa_subscription *subscription;
73 static pa_cvolume* parse_volume(const char *s, pa_cvolume *v) {
85 if (k <= 0 || k > PA_CHANNELS_MAX)
88 v->channels = (unsigned) k;
90 for (i = 0; i < v->channels; i++) {
91 p += strspn(p, WHITESPACE);
98 if (k < PA_VOLUME_MUTED)
101 v->values[i] = (pa_volume_t) k;
110 static int load_rules(struct userdata *u) {
114 char buf_name[256], buf_volume[256];
118 fopen(u->table_file, "r") :
119 pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "r");
122 if (errno == ENOENT) {
123 pa_log_info(__FILE__": starting with empty ruleset.");
126 pa_log(__FILE__": failed to open file '%s': %s", u->table_file, strerror(errno));
131 pa_lock_fd(fileno(f), 1);
137 if (!fgets(ln, sizeof(buf_name), f))
144 if (ln[0] == '#' || !*ln )
147 if (ln == buf_name) {
152 assert(ln == buf_volume);
154 if (!parse_volume(buf_volume, &v)) {
155 pa_log(__FILE__": parse failure in %s:%u, stopping parsing", u->table_file, n);
161 if (pa_hashmap_get(u->hashmap, buf_name)) {
162 pa_log(__FILE__": double entry in %s:%u, ignoring", u->table_file, n);
166 rule = pa_xnew(struct rule, 1);
167 rule->name = pa_xstrdup(buf_name);
170 pa_hashmap_put(u->hashmap, rule->name, rule);
173 if (ln == buf_volume) {
174 pa_log(__FILE__": invalid number of lines in %s.", u->table_file);
182 pa_lock_fd(fileno(f), 0);
189 static int save_rules(struct userdata *u) {
196 fopen(u->table_file, "w") :
197 pa_open_config_file(NULL, DEFAULT_VOLUME_TABLE_FILE, NULL, &u->table_file, "w");
200 pa_log(__FILE__": failed to open file '%s': %s", u->table_file, strerror(errno));
204 pa_lock_fd(fileno(f), 1);
206 while ((rule = pa_hashmap_iterate(u->hashmap, &state, NULL))) {
209 fprintf(f, "%s\n%u", rule->name, rule->volume.channels);
211 for (i = 0; i < rule->volume.channels; i++)
212 fprintf(f, " %u", rule->volume.values[i]);
221 pa_lock_fd(fileno(f), 0);
228 static char* client_name(pa_client *c) {
231 if (!c->name || !c->driver)
234 t = pa_sprintf_malloc("%s$%s", c->driver, c->name);
235 t[strcspn(t, "\n\r#")] = 0;
240 if ((e = strrchr(t, '('))) {
241 char *k = e + 1 + strspn(e + 1, "0123456789-");
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. */
249 if (*k == ')' && *(k+1) == 0)
256 static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
257 struct userdata *u = userdata;
265 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
266 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
269 if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
272 if (!si->client || !(name = client_name(si->client)))
275 if ((r = pa_hashmap_get(u->hashmap, name))) {
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);
288 pa_log_info(__FILE__": Creating new entry for <%s>", name);
290 r = pa_xnew(struct rule, 1);
292 r->volume = *pa_sink_input_get_volume(si);
293 pa_hashmap_put(u->hashmap, r->name, r);
299 int pa__init(pa_core *c, pa_module*m) {
300 pa_modargs *ma = NULL;
306 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
307 pa_log(__FILE__": Failed to parse module arguments");
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));
319 if (load_rules(u) < 0)
322 u->subscription = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
336 static void free_func(void *p, void *userdata) {
344 void pa__done(pa_core *c, pa_module*m) {
350 if (!(u = m->userdata))
354 pa_subscription_free(u->subscription);
361 pa_hashmap_free(u->hashmap, free_func, NULL);
364 pa_xfree(u->table_file);