]> code.delx.au - pulseaudio/blob - src/modules/module-match.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-match.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-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 <regex.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-subscribe.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/core-util.h>
46
47 #include "module-match-symdef.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("Playback stream expression matching module")
51 PA_MODULE_USAGE("table=<filename>")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53
54 #define WHITESPACE "\n\r \t"
55
56 #define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
57 #define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
58
59 static const char* const valid_modargs[] = {
60 "table",
61 NULL,
62 };
63
64 struct rule {
65 regex_t regex;
66 pa_volume_t volume;
67 struct rule *next;
68 };
69
70 struct userdata {
71 struct rule *rules;
72 pa_subscription *subscription;
73 };
74
75 static int load_rules(struct userdata *u, const char *filename) {
76 FILE *f;
77 int n = 0;
78 int ret = -1;
79 struct rule *end = NULL;
80 char *fn = NULL;
81
82 pa_assert(u);
83
84 f = filename ?
85 fopen(fn = pa_xstrdup(filename), "r") :
86 pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn, "r");
87
88 if (!f) {
89 pa_log("failed to open file '%s': %s", fn, pa_cstrerror(errno));
90 goto finish;
91 }
92
93 pa_lock_fd(fileno(f), 1);
94
95 while (!feof(f)) {
96 char *d, *v;
97 pa_volume_t volume;
98 uint32_t k;
99 regex_t regex;
100 char ln[256];
101 struct rule *rule;
102
103 if (!fgets(ln, sizeof(ln), f))
104 break;
105
106 n++;
107
108 pa_strip_nl(ln);
109
110 if (ln[0] == '#' || !*ln )
111 continue;
112
113 d = ln+strcspn(ln, WHITESPACE);
114 v = d+strspn(d, WHITESPACE);
115
116
117 if (!*v) {
118 pa_log(__FILE__ ": [%s:%u] failed to parse line - too few words", filename, n);
119 goto finish;
120 }
121
122 *d = 0;
123 if (pa_atou(v, &k) < 0) {
124 pa_log("[%s:%u] failed to parse volume", filename, n);
125 goto finish;
126 }
127
128 volume = (pa_volume_t) k;
129
130
131 if (regcomp(&regex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
132 pa_log("[%s:%u] invalid regular expression", filename, n);
133 goto finish;
134 }
135
136 rule = pa_xnew(struct rule, 1);
137 rule->regex = regex;
138 rule->volume = volume;
139 rule->next = NULL;
140
141 if (end)
142 end->next = rule;
143 else
144 u->rules = rule;
145 end = rule;
146
147 *d = 0;
148 }
149
150 ret = 0;
151
152 finish:
153 if (f) {
154 pa_lock_fd(fileno(f), 0);
155 fclose(f);
156 }
157
158 if (fn)
159 pa_xfree(fn);
160
161 return ret;
162 }
163
164 static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
165 struct userdata *u = userdata;
166 pa_sink_input *si;
167 struct rule *r;
168
169 pa_assert(c);
170 pa_assert(u);
171
172 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW))
173 return;
174
175 if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
176 return;
177
178 if (!si->name)
179 return;
180
181 for (r = u->rules; r; r = r->next) {
182 if (!regexec(&r->regex, si->name, 0, NULL, 0)) {
183 pa_cvolume cv;
184 pa_log_debug("changing volume of sink input '%s' to 0x%03x", si->name, r->volume);
185 pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
186 pa_sink_input_set_volume(si, &cv);
187 }
188 }
189 }
190
191 int pa__init(pa_module*m) {
192 pa_modargs *ma = NULL;
193 struct userdata *u;
194
195 pa_assert(m);
196
197 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
198 pa_log("Failed to parse module arguments");
199 goto fail;
200 }
201
202 u = pa_xnew(struct userdata, 1);
203 u->rules = NULL;
204 u->subscription = NULL;
205 m->userdata = u;
206
207 if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
208 goto fail;
209
210 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
211
212 pa_modargs_free(ma);
213 return 0;
214
215 fail:
216 pa__done(m);
217
218 if (ma)
219 pa_modargs_free(ma);
220 return -1;
221 }
222
223 void pa__done(pa_module*m) {
224 struct userdata* u;
225 struct rule *r, *n;
226
227 pa_assert(m);
228
229 if (!(u = m->userdata))
230 return;
231
232 if (u->subscription)
233 pa_subscription_free(u->subscription);
234
235 for (r = u->rules; r; r = n) {
236 n = r->next;
237
238 regfree(&r->regex);
239 pa_xfree(r);
240 }
241
242 pa_xfree(u);
243 }
244
245