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