Fix svn properties and some minor indentation
[pulseaudio-mirror.git] / src / modules / module-match.c
blob769a6b59ef32140a94332323ed98bd39af3e393a
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 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 <regex.h>
31 #include <stdio.h>
32 #include <stdlib.h>
34 #include <pulse/xmalloc.h>
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>
45 #include "module-match-symdef.h"
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>");
53 #define WHITESPACE "\n\r \t"
55 #define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
56 #define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
58 static const char* const valid_modargs[] = {
59 "table",
60 NULL,
63 struct rule {
64 regex_t regex;
65 pa_volume_t volume;
66 struct rule *next;
69 struct userdata {
70 struct rule *rules;
71 pa_subscription *subscription;
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;
81 pa_assert(u);
83 if (filename)
84 f = fopen(fn = pa_xstrdup(filename), "r");
85 else
86 f = pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn);
88 if (!f) {
89 pa_xfree(fn);
90 pa_log("Failed to open file config file: %s", pa_cstrerror(errno));
91 goto finish;
94 pa_lock_fd(fileno(f), 1);
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;
104 if (!fgets(ln, sizeof(ln), f))
105 break;
107 n++;
109 pa_strip_nl(ln);
111 if (ln[0] == '#' || !*ln )
112 continue;
114 d = ln+strcspn(ln, WHITESPACE);
115 v = d+strspn(d, WHITESPACE);
118 if (!*v) {
119 pa_log(__FILE__ ": [%s:%u] failed to parse line - too few words", filename, n);
120 goto finish;
123 *d = 0;
124 if (pa_atou(v, &k) < 0) {
125 pa_log("[%s:%u] failed to parse volume", filename, n);
126 goto finish;
129 volume = (pa_volume_t) k;
132 if (regcomp(&regex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
133 pa_log("[%s:%u] invalid regular expression", filename, n);
134 goto finish;
137 rule = pa_xnew(struct rule, 1);
138 rule->regex = regex;
139 rule->volume = volume;
140 rule->next = NULL;
142 if (end)
143 end->next = rule;
144 else
145 u->rules = rule;
146 end = rule;
148 *d = 0;
151 ret = 0;
153 finish:
154 if (f) {
155 pa_lock_fd(fileno(f), 0);
156 fclose(f);
159 if (fn)
160 pa_xfree(fn);
162 return ret;
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 const char *n;
171 pa_assert(c);
172 pa_assert(u);
174 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW))
175 return;
177 if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
178 return;
180 if (!(n = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_NAME)))
181 return;
183 for (r = u->rules; r; r = r->next) {
184 if (!regexec(&r->regex, n, 0, NULL, 0)) {
185 pa_cvolume cv;
186 pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume);
187 pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
188 pa_sink_input_set_volume(si, &cv);
193 int pa__init(pa_module*m) {
194 pa_modargs *ma = NULL;
195 struct userdata *u;
197 pa_assert(m);
199 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
200 pa_log("Failed to parse module arguments");
201 goto fail;
204 u = pa_xnew(struct userdata, 1);
205 u->rules = NULL;
206 u->subscription = NULL;
207 m->userdata = u;
209 if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
210 goto fail;
212 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
214 pa_modargs_free(ma);
215 return 0;
217 fail:
218 pa__done(m);
220 if (ma)
221 pa_modargs_free(ma);
222 return -1;
225 void pa__done(pa_module*m) {
226 struct userdata* u;
227 struct rule *r, *n;
229 pa_assert(m);
231 if (!(u = m->userdata))
232 return;
234 if (u->subscription)
235 pa_subscription_free(u->subscription);
237 for (r = u->rules; r; r = n) {
238 n = r->next;
240 regfree(&r->regex);
241 pa_xfree(r);
244 pa_xfree(u);