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