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