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