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