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