]> code.delx.au - pulseaudio/blob - src/modules/module-match.c
sink, source: Add hooks for mute changes
[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 <stdio.h>
31 #include <stdlib.h>
32
33 #if defined(HAVE_REGEX_H)
34 #include <regex.h>
35 #elif defined(HAVE_PCREPOSIX_H)
36 #include <pcreposix.h>
37 #endif
38
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/sink-input.h>
47 #include <pulsecore/core-util.h>
48
49 #include "module-match-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering");
52 PA_MODULE_DESCRIPTION("Playback stream expression matching module");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(true);
55 PA_MODULE_USAGE("table=<filename> "
56 "key=<property_key>");
57
58 #define WHITESPACE "\n\r \t"
59
60 #define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
61 #define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
62
63 #define UPDATE_REPLACE "replace"
64 #define UPDATE_MERGE "merge"
65
66 static const char* const valid_modargs[] = {
67 "table",
68 "key",
69 NULL,
70 };
71
72 struct rule {
73 regex_t regex;
74 pa_volume_t volume;
75 pa_proplist *proplist;
76 pa_update_mode_t mode;
77 struct rule *next;
78 };
79
80 struct userdata {
81 struct rule *rules;
82 char *property_key;
83 pa_hook_slot *sink_input_fixate_hook_slot;
84 };
85
86 static int load_rules(struct userdata *u, const char *filename) {
87 FILE *f;
88 int n = 0;
89 int ret = -1;
90 struct rule *end = NULL;
91 char *fn = NULL;
92
93 pa_assert(u);
94
95 if (filename)
96 f = pa_fopen_cloexec(fn = pa_xstrdup(filename), "r");
97 else
98 f = pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn);
99
100 if (!f) {
101 pa_log("Failed to open file config file: %s", pa_cstrerror(errno));
102 goto finish;
103 }
104
105 pa_lock_fd(fileno(f), 1);
106
107 while (!feof(f)) {
108 char *token_end, *value_str;
109 pa_volume_t volume = PA_VOLUME_NORM;
110 uint32_t k;
111 regex_t regex;
112 char ln[256];
113 struct rule *rule;
114 pa_proplist *proplist = NULL;
115 pa_update_mode_t mode = (pa_update_mode_t) -1;
116
117 if (!fgets(ln, sizeof(ln), f))
118 break;
119
120 n++;
121
122 pa_strip_nl(ln);
123
124 if (ln[0] == '#' || !*ln )
125 continue;
126
127 token_end = ln + strcspn(ln, WHITESPACE);
128 value_str = token_end + strspn(token_end, WHITESPACE);
129 *token_end = 0;
130
131 if (!*ln) {
132 pa_log("[%s:%u] failed to parse line - missing regexp", fn, n);
133 goto finish;
134 }
135
136 if (!*value_str) {
137 pa_log("[%s:%u] failed to parse line - too few words", fn, n);
138 goto finish;
139 }
140
141 if (pa_atou(value_str, &k) >= 0)
142 volume = (pa_volume_t) PA_CLAMP_VOLUME(k);
143 else {
144 size_t len;
145
146 token_end = value_str + strcspn(value_str, WHITESPACE);
147
148 len = token_end - value_str;
149 if (len == (sizeof(UPDATE_REPLACE) - 1) && !strncmp(value_str, UPDATE_REPLACE, len))
150 mode = PA_UPDATE_REPLACE;
151 else if (len == (sizeof(UPDATE_MERGE) - 1) && !strncmp(value_str, UPDATE_MERGE, len))
152 mode = PA_UPDATE_MERGE;
153
154 if (mode != (pa_update_mode_t) -1) {
155 value_str = token_end + strspn(token_end, WHITESPACE);
156
157 if (!*value_str) {
158 pa_log("[%s:%u] failed to parse line - too few words", fn, n);
159 goto finish;
160 }
161 } else
162 mode = PA_UPDATE_MERGE;
163
164 if (*value_str == '"') {
165 value_str++;
166
167 token_end = strchr(value_str, '"');
168 if (!token_end) {
169 pa_log("[%s:%u] failed to parse line - missing role closing quote", fn, n);
170 goto finish;
171 }
172 } else
173 token_end = value_str + strcspn(value_str, WHITESPACE);
174
175 *token_end = 0;
176
177 value_str = pa_sprintf_malloc("media.role=\"%s\"", value_str);
178 proplist = pa_proplist_from_string(value_str);
179 pa_xfree(value_str);
180 }
181
182 if (regcomp(&regex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
183 pa_log("[%s:%u] invalid regular expression", fn, n);
184 if (proplist)
185 pa_proplist_free(proplist);
186 goto finish;
187 }
188
189 rule = pa_xnew(struct rule, 1);
190 rule->regex = regex;
191 rule->proplist = proplist;
192 rule->mode = mode;
193 rule->volume = volume;
194 rule->next = NULL;
195
196 if (end)
197 end->next = rule;
198 else
199 u->rules = rule;
200 end = rule;
201 }
202
203 ret = 0;
204
205 finish:
206 if (f) {
207 pa_lock_fd(fileno(f), 0);
208 fclose(f);
209 }
210
211 pa_xfree(fn);
212
213 return ret;
214 }
215
216 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_input_new_data *si, struct userdata *u) {
217 struct rule *r;
218 const char *n;
219
220 pa_assert(c);
221 pa_assert(u);
222
223 if (!(n = pa_proplist_gets(si->proplist, u->property_key)))
224 return PA_HOOK_OK;
225
226 pa_log_debug("Matching with %s", n);
227
228 for (r = u->rules; r; r = r->next) {
229 if (!regexec(&r->regex, n, 0, NULL, 0)) {
230 if (r->proplist) {
231 pa_log_debug("updating proplist of sink input '%s'", n);
232 pa_proplist_update(si->proplist, r->mode, r->proplist);
233 } else if (si->volume_writable) {
234 pa_cvolume cv;
235 pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume);
236 pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
237 pa_sink_input_new_data_set_volume(si, &cv);
238 } else
239 pa_log_debug("the volume of sink input '%s' is not writable, can't change it", n);
240 }
241 }
242
243 return PA_HOOK_OK;
244 }
245
246 int pa__init(pa_module*m) {
247 pa_modargs *ma = NULL;
248 struct userdata *u;
249
250 pa_assert(m);
251
252 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
253 pa_log("Failed to parse module arguments");
254 goto fail;
255 }
256
257 u = pa_xnew0(struct userdata, 1);
258 u->rules = NULL;
259 m->userdata = u;
260
261 u->property_key = pa_xstrdup(pa_modargs_get_value(ma, "key", PA_PROP_MEDIA_NAME));
262
263 if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
264 goto fail;
265
266 /* hook EARLY - 1, to match before stream-restore */
267 u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY - 1, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
268
269 pa_modargs_free(ma);
270 return 0;
271
272 fail:
273 pa__done(m);
274
275 if (ma)
276 pa_modargs_free(ma);
277 return -1;
278 }
279
280 void pa__done(pa_module*m) {
281 struct userdata* u;
282 struct rule *r, *n;
283
284 pa_assert(m);
285
286 if (!(u = m->userdata))
287 return;
288
289 if (u->sink_input_fixate_hook_slot)
290 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
291
292 if (u->property_key)
293 pa_xfree(u->property_key);
294
295 for (r = u->rules; r; r = n) {
296 n = r->next;
297
298 regfree(&r->regex);
299 if (r->proplist)
300 pa_proplist_free(r->proplist);
301 pa_xfree(r);
302 }
303
304 pa_xfree(u);
305 }