]> code.delx.au - pulseaudio/blob - src/modules/module-match.c
Merge commit 'origin/master-tx'
[pulseaudio] / src / modules / module-match.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <regex.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/core-util.h>
44
45 #include "module-match-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering");
48 PA_MODULE_DESCRIPTION("Playback stream expression matching module");
49 PA_MODULE_VERSION(PACKAGE_VERSION);
50 PA_MODULE_LOAD_ONCE(TRUE);
51 PA_MODULE_USAGE("table=<filename> "
52 "key=<property_key>");
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 "key",
62 NULL,
63 };
64
65 struct rule {
66 regex_t regex;
67 pa_volume_t volume;
68 pa_proplist *proplist;
69 struct rule *next;
70 };
71
72 struct userdata {
73 struct rule *rules;
74 char *property_key;
75 pa_subscription *subscription;
76 };
77
78 static int load_rules(struct userdata *u, const char *filename) {
79 FILE *f;
80 int n = 0;
81 int ret = -1;
82 struct rule *end = NULL;
83 char *fn = NULL;
84
85 pa_assert(u);
86
87 if (filename)
88 f = fopen(fn = pa_xstrdup(filename), "r");
89 else
90 f = pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn);
91
92 if (!f) {
93 pa_xfree(fn);
94 pa_log("Failed to open file config file: %s", pa_cstrerror(errno));
95 goto finish;
96 }
97
98 pa_lock_fd(fileno(f), 1);
99
100 while (!feof(f)) {
101 char *d, *v;
102 pa_volume_t volume = PA_VOLUME_NORM;
103 uint32_t k;
104 regex_t regex;
105 char ln[256];
106 struct rule *rule;
107 pa_proplist *proplist = NULL;
108
109 if (!fgets(ln, sizeof(ln), f))
110 break;
111
112 n++;
113
114 pa_strip_nl(ln);
115
116 if (ln[0] == '#' || !*ln )
117 continue;
118
119 d = ln+strcspn(ln, WHITESPACE);
120 v = d+strspn(d, WHITESPACE);
121
122
123 if (!*v) {
124 pa_log(__FILE__ ": [%s:%u] failed to parse line - too few words", filename, n);
125 goto finish;
126 }
127
128 *d = 0;
129 if (pa_atou(v, &k) >= 0) {
130 volume = (pa_volume_t) k;
131 } else if (*v == '"') {
132 char *e;
133
134 e = strchr(v+1, '"');
135 if (!e) {
136 pa_log(__FILE__ ": [%s:%u] failed to parse line - missing role closing quote", filename, n);
137 goto finish;
138 }
139
140 *e = '\0';
141 e = pa_sprintf_malloc("media.role=\"%s\"", v+1);
142 proplist = pa_proplist_from_string(e);
143 pa_xfree(e);
144 } else {
145 char *e;
146
147 e = v+strspn(v, WHITESPACE);
148 if (!*e) {
149 pa_log(__FILE__ ": [%s:%u] failed to parse line - missing end of property list", filename, n);
150 goto finish;
151 }
152 *e = '\0';
153 proplist = pa_proplist_from_string(v);
154 }
155
156 if (regcomp(&regex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
157 pa_log("[%s:%u] invalid regular expression", filename, n);
158 goto finish;
159 }
160
161 rule = pa_xnew(struct rule, 1);
162 rule->regex = regex;
163 rule->proplist = proplist;
164 rule->volume = volume;
165 rule->next = NULL;
166
167 if (end)
168 end->next = rule;
169 else
170 u->rules = rule;
171 end = rule;
172
173 *d = 0;
174 }
175
176 ret = 0;
177
178 finish:
179 if (f) {
180 pa_lock_fd(fileno(f), 0);
181 fclose(f);
182 }
183
184 if (fn)
185 pa_xfree(fn);
186
187 return ret;
188 }
189
190 static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
191 struct userdata *u = userdata;
192 pa_sink_input *si;
193 struct rule *r;
194 const char *n;
195
196 pa_assert(c);
197 pa_assert(u);
198
199 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW))
200 return;
201
202 if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
203 return;
204
205 if (!(n = pa_proplist_gets(si->proplist, u->property_key)))
206 return;
207
208 pa_log_debug("Matching with %s", n);
209
210 for (r = u->rules; r; r = r->next) {
211 if (!regexec(&r->regex, n, 0, NULL, 0)) {
212 if (r->proplist) {
213 pa_log_debug("updating proplist of sink input '%s'", n);
214 pa_proplist_update(si->proplist, PA_UPDATE_MERGE, r->proplist);
215 } else {
216 pa_cvolume cv;
217 pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume);
218 pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
219 pa_sink_input_set_volume(si, &cv, TRUE);
220 }
221 }
222 }
223 }
224
225 int pa__init(pa_module*m) {
226 pa_modargs *ma = NULL;
227 struct userdata *u;
228
229 pa_assert(m);
230
231 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
232 pa_log("Failed to parse module arguments");
233 goto fail;
234 }
235
236 u = pa_xnew(struct userdata, 1);
237 u->rules = NULL;
238 u->subscription = NULL;
239 m->userdata = u;
240
241 u->property_key = pa_xstrdup(pa_modargs_get_value(ma, "key", PA_PROP_MEDIA_NAME));
242
243 if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
244 goto fail;
245
246 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
247
248 pa_modargs_free(ma);
249 return 0;
250
251 fail:
252 pa__done(m);
253
254 if (ma)
255 pa_modargs_free(ma);
256 return -1;
257 }
258
259 void pa__done(pa_module*m) {
260 struct userdata* u;
261 struct rule *r, *n;
262
263 pa_assert(m);
264
265 if (!(u = m->userdata))
266 return;
267
268 if (u->subscription)
269 pa_subscription_free(u->subscription);
270
271 if (u->property_key)
272 pa_xfree(u->property_key);
273
274 for (r = u->rules; r; r = n) {
275 n = r->next;
276
277 regfree(&r->regex);
278 if (r->proplist)
279 pa_proplist_free(r->proplist);
280 pa_xfree(r);
281 }
282
283 pa_xfree(u);
284 }