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