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