Fix up some double spaces
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / modules / module-match.c
blobc8f27062eeb193f8da629a29c275a6ba3190ba6f
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
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>
33 #if defined(HAVE_REGEX_H)
34 #include <regex.h>
35 #elif defined(HAVE_PCREPOSIX_H)
36 #include <pcreposix.h>
37 #endif
39 #include <pulse/xmalloc.h>
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/core-subscribe.h>
47 #include <pulsecore/sink-input.h>
48 #include <pulsecore/core-util.h>
50 #include "module-match-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Playback stream expression matching module");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(TRUE);
56 PA_MODULE_USAGE("table=<filename> "
57 "key=<property_key>");
59 #define WHITESPACE "\n\r \t"
61 #define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
62 #define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
64 static const char* const valid_modargs[] = {
65 "table",
66 "key",
67 NULL,
70 struct rule {
71 regex_t regex;
72 pa_volume_t volume;
73 pa_proplist *proplist;
74 struct rule *next;
77 struct userdata {
78 struct rule *rules;
79 char *property_key;
80 pa_subscription *subscription;
83 static int load_rules(struct userdata *u, const char *filename) {
84 FILE *f;
85 int n = 0;
86 int ret = -1;
87 struct rule *end = NULL;
88 char *fn = NULL;
90 pa_assert(u);
92 if (filename)
93 f = pa_fopen_cloexec(fn = pa_xstrdup(filename), "r");
94 else
95 f = pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn);
97 if (!f) {
98 pa_xfree(fn);
99 pa_log("Failed to open file config file: %s", pa_cstrerror(errno));
100 goto finish;
103 pa_lock_fd(fileno(f), 1);
105 while (!feof(f)) {
106 char *d, *v;
107 pa_volume_t volume = PA_VOLUME_NORM;
108 uint32_t k;
109 regex_t regex;
110 char ln[256];
111 struct rule *rule;
112 pa_proplist *proplist = NULL;
114 if (!fgets(ln, sizeof(ln), f))
115 break;
117 n++;
119 pa_strip_nl(ln);
121 if (ln[0] == '#' || !*ln )
122 continue;
124 d = ln+strcspn(ln, WHITESPACE);
125 v = d+strspn(d, WHITESPACE);
128 if (!*v) {
129 pa_log(__FILE__ ": [%s:%u] failed to parse line - too few words", filename, n);
130 goto finish;
133 *d = 0;
134 if (pa_atou(v, &k) >= 0) {
135 volume = (pa_volume_t) PA_CLAMP_VOLUME(k);
136 } else if (*v == '"') {
137 char *e;
139 e = strchr(v+1, '"');
140 if (!e) {
141 pa_log(__FILE__ ": [%s:%u] failed to parse line - missing role closing quote", filename, n);
142 goto finish;
145 *e = '\0';
146 e = pa_sprintf_malloc("media.role=\"%s\"", v+1);
147 proplist = pa_proplist_from_string(e);
148 pa_xfree(e);
149 } else {
150 char *e;
152 e = v+strspn(v, WHITESPACE);
153 if (!*e) {
154 pa_log(__FILE__ ": [%s:%u] failed to parse line - missing end of property list", filename, n);
155 goto finish;
157 *e = '\0';
158 proplist = pa_proplist_from_string(v);
161 if (regcomp(&regex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
162 pa_log("[%s:%u] invalid regular expression", filename, n);
163 goto finish;
166 rule = pa_xnew(struct rule, 1);
167 rule->regex = regex;
168 rule->proplist = proplist;
169 rule->volume = volume;
170 rule->next = NULL;
172 if (end)
173 end->next = rule;
174 else
175 u->rules = rule;
176 end = rule;
178 *d = 0;
181 ret = 0;
183 finish:
184 if (f) {
185 pa_lock_fd(fileno(f), 0);
186 fclose(f);
189 if (fn)
190 pa_xfree(fn);
192 return ret;
195 static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
196 struct userdata *u = userdata;
197 pa_sink_input *si;
198 struct rule *r;
199 const char *n;
201 pa_assert(c);
202 pa_assert(u);
204 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW))
205 return;
207 if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
208 return;
210 if (!(n = pa_proplist_gets(si->proplist, u->property_key)))
211 return;
213 pa_log_debug("Matching with %s", n);
215 for (r = u->rules; r; r = r->next) {
216 if (!regexec(&r->regex, n, 0, NULL, 0)) {
217 if (r->proplist) {
218 pa_log_debug("updating proplist of sink input '%s'", n);
219 pa_proplist_update(si->proplist, PA_UPDATE_MERGE, r->proplist);
220 } else {
221 pa_cvolume cv;
222 pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume);
223 pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
224 pa_sink_input_set_volume(si, &cv, TRUE, FALSE);
230 int pa__init(pa_module*m) {
231 pa_modargs *ma = NULL;
232 struct userdata *u;
234 pa_assert(m);
236 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
237 pa_log("Failed to parse module arguments");
238 goto fail;
241 u = pa_xnew(struct userdata, 1);
242 u->rules = NULL;
243 u->subscription = NULL;
244 m->userdata = u;
246 u->property_key = pa_xstrdup(pa_modargs_get_value(ma, "key", PA_PROP_MEDIA_NAME));
248 if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
249 goto fail;
251 /* FIXME: Doing this asynchronously is just broken. This needs to
252 * use a hook! */
254 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
256 pa_modargs_free(ma);
257 return 0;
259 fail:
260 pa__done(m);
262 if (ma)
263 pa_modargs_free(ma);
264 return -1;
267 void pa__done(pa_module*m) {
268 struct userdata* u;
269 struct rule *r, *n;
271 pa_assert(m);
273 if (!(u = m->userdata))
274 return;
276 if (u->subscription)
277 pa_subscription_free(u->subscription);
279 if (u->property_key)
280 pa_xfree(u->property_key);
282 for (r = u->rules; r; r = n) {
283 n = r->next;
285 regfree(&r->regex);
286 if (r->proplist)
287 pa_proplist_free(r->proplist);
288 pa_xfree(r);
291 pa_xfree(u);