]> code.delx.au - pulseaudio/blob - src/modules/gconf/module-gconf.c
Merge dead branch 'lockfree'
[pulseaudio] / src / modules / gconf / module-gconf.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <string.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <fcntl.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/core.h>
38 #include <pulsecore/llist.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/log.h>
41 #include <pulse/mainloop-api.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/start-child.h>
44
45 #include "module-gconf-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering");
48 PA_MODULE_DESCRIPTION("GConf Adapter");
49 PA_MODULE_VERSION(PACKAGE_VERSION);
50 PA_MODULE_LOAD_ONCE(TRUE);
51
52 #define MAX_MODULES 10
53 #define BUF_MAX 2048
54
55 /* #undef PA_GCONF_HELPER */
56 /* #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper" */
57
58 struct module_item {
59 char *name;
60 char *args;
61 uint32_t index;
62 };
63
64 struct module_info {
65 char *name;
66
67 struct module_item items[MAX_MODULES];
68 unsigned n_items;
69 };
70
71 struct userdata {
72 pa_core *core;
73 pa_module *module;
74
75 pa_hashmap *module_infos;
76
77 pid_t pid;
78
79 int fd;
80 int fd_type;
81 pa_io_event *io_event;
82
83 char buf[BUF_MAX];
84 size_t buf_fill;
85 };
86
87 static int fill_buf(struct userdata *u) {
88 ssize_t r;
89 pa_assert(u);
90
91 if (u->buf_fill >= BUF_MAX) {
92 pa_log("read buffer overflow");
93 return -1;
94 }
95
96 if ((r = pa_read(u->fd, u->buf + u->buf_fill, BUF_MAX - u->buf_fill, &u->fd_type)) <= 0)
97 return -1;
98
99 u->buf_fill += r;
100 return 0;
101 }
102
103 static int read_byte(struct userdata *u) {
104 int ret;
105 pa_assert(u);
106
107 if (u->buf_fill < 1)
108 if (fill_buf(u) < 0)
109 return -1;
110
111 ret = u->buf[0];
112 pa_assert(u->buf_fill > 0);
113 u->buf_fill--;
114 memmove(u->buf, u->buf+1, u->buf_fill);
115 return ret;
116 }
117
118 static char *read_string(struct userdata *u) {
119 pa_assert(u);
120
121 for (;;) {
122 char *e;
123
124 if ((e = memchr(u->buf, 0, u->buf_fill))) {
125 char *ret = pa_xstrdup(u->buf);
126 u->buf_fill -= e - u->buf +1;
127 memmove(u->buf, e+1, u->buf_fill);
128 return ret;
129 }
130
131 if (fill_buf(u) < 0)
132 return NULL;
133 }
134 }
135
136 static void unload_one_module(struct userdata *u, struct module_info*m, unsigned i) {
137 pa_assert(u);
138 pa_assert(m);
139 pa_assert(i < m->n_items);
140
141 if (m->items[i].index == PA_INVALID_INDEX)
142 return;
143
144 pa_log_debug("Unloading module #%i", m->items[i].index);
145 pa_module_unload_by_index(u->core, m->items[i].index);
146 m->items[i].index = PA_INVALID_INDEX;
147 pa_xfree(m->items[i].name);
148 pa_xfree(m->items[i].args);
149 m->items[i].name = m->items[i].args = NULL;
150 }
151
152 static void unload_all_modules(struct userdata *u, struct module_info*m) {
153 unsigned i;
154
155 pa_assert(u);
156 pa_assert(m);
157
158 for (i = 0; i < m->n_items; i++)
159 unload_one_module(u, m, i);
160
161 m->n_items = 0;
162 }
163
164 static void load_module(
165 struct userdata *u,
166 struct module_info *m,
167 int i,
168 const char *name,
169 const char *args,
170 int is_new) {
171
172 pa_module *mod;
173
174 pa_assert(u);
175 pa_assert(m);
176 pa_assert(name);
177 pa_assert(args);
178
179 if (!is_new) {
180 if (m->items[i].index != PA_INVALID_INDEX &&
181 strcmp(m->items[i].name, name) == 0 &&
182 strcmp(m->items[i].args, args) == 0)
183 return;
184
185 unload_one_module(u, m, i);
186 }
187
188 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name, args);
189
190 m->items[i].name = pa_xstrdup(name);
191 m->items[i].args = pa_xstrdup(args);
192 m->items[i].index = PA_INVALID_INDEX;
193
194 if (!(mod = pa_module_load(u->core, name, args))) {
195 pa_log("pa_module_load() failed");
196 return;
197 }
198
199 m->items[i].index = mod->index;
200 }
201
202 static void module_info_free(void *p, void *userdata) {
203 struct module_info *m = p;
204 struct userdata *u = userdata;
205
206 pa_assert(m);
207 pa_assert(u);
208
209 unload_all_modules(u, m);
210 pa_xfree(m->name);
211 pa_xfree(m);
212 }
213
214 static int handle_event(struct userdata *u) {
215 int opcode;
216 int ret = 0;
217
218 do {
219 if ((opcode = read_byte(u)) < 0){
220 if (errno == EINTR || errno == EAGAIN)
221 break;
222 goto fail;
223 }
224
225 switch (opcode) {
226 case '!':
227 /* The helper tool is now initialized */
228 ret = 1;
229 break;
230
231 case '+': {
232 char *name;
233 struct module_info *m;
234 unsigned i, j;
235
236 if (!(name = read_string(u)))
237 goto fail;
238
239 if (!(m = pa_hashmap_get(u->module_infos, name))) {
240 m = pa_xnew(struct module_info, 1);
241 m->name = name;
242 m->n_items = 0;
243 pa_hashmap_put(u->module_infos, m->name, m);
244 } else
245 pa_xfree(name);
246
247 i = 0;
248 while (i < MAX_MODULES) {
249 char *module, *args;
250
251 if (!(module = read_string(u))) {
252 if (i > m->n_items) m->n_items = i;
253 goto fail;
254 }
255
256 if (!*module) {
257 pa_xfree(module);
258 break;
259 }
260
261 if (!(args = read_string(u))) {
262 pa_xfree(module);
263
264 if (i > m->n_items) m->n_items = i;
265 goto fail;
266 }
267
268 load_module(u, m, i, module, args, i >= m->n_items);
269
270 i++;
271
272 pa_xfree(module);
273 pa_xfree(args);
274 }
275
276 /* Unload all removed modules */
277 for (j = i; j < m->n_items; j++)
278 unload_one_module(u, m, j);
279
280 m->n_items = i;
281
282 break;
283 }
284
285 case '-': {
286 char *name;
287 struct module_info *m;
288
289 if (!(name = read_string(u)))
290 goto fail;
291
292 if ((m = pa_hashmap_get(u->module_infos, name))) {
293 pa_hashmap_remove(u->module_infos, name);
294 module_info_free(m, u);
295 }
296
297 pa_xfree(name);
298
299 break;
300 }
301 }
302 } while (u->buf_fill > 0 && ret == 0);
303
304 return ret;
305
306 fail:
307 pa_log("Unable to read or parse data from client.");
308 return -1;
309 }
310
311 static void io_event_cb(
312 pa_mainloop_api*a,
313 pa_io_event* e,
314 int fd,
315 pa_io_event_flags_t events,
316 void *userdata) {
317
318 struct userdata *u = userdata;
319
320 if (handle_event(u) < 0) {
321
322 if (u->io_event) {
323 u->core->mainloop->io_free(u->io_event);
324 u->io_event = NULL;
325 }
326
327 pa_module_unload_request(u->module);
328 }
329 }
330
331 int pa__init(pa_module*m) {
332 struct userdata *u;
333 int r;
334
335 u = pa_xnew(struct userdata, 1);
336 u->core = m->core;
337 u->module = m;
338 m->userdata = u;
339 u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
340 u->pid = (pid_t) -1;
341 u->fd = -1;
342 u->fd_type = 0;
343 u->io_event = NULL;
344 u->buf_fill = 0;
345
346 if ((u->fd = pa_start_child_for_read(PA_GCONF_HELPER, NULL, &u->pid)) < 0)
347 goto fail;
348
349 u->io_event = m->core->mainloop->io_new(
350 m->core->mainloop,
351 u->fd,
352 PA_IO_EVENT_INPUT,
353 io_event_cb,
354 u);
355
356 do {
357 if ((r = handle_event(u)) < 0)
358 goto fail;
359
360 /* Read until the client signalled us that it is ready with
361 * initialization */
362 } while (r != 1);
363
364 return 0;
365
366 fail:
367 pa__done(m);
368 return -1;
369 }
370
371 void pa__done(pa_module*m) {
372 struct userdata *u;
373
374 pa_assert(m);
375
376 if (!(u = m->userdata))
377 return;
378
379 if (u->pid != (pid_t) -1) {
380 kill(u->pid, SIGTERM);
381 waitpid(u->pid, NULL, 0);
382 }
383
384 if (u->io_event)
385 m->core->mainloop->io_free(u->io_event);
386
387 if (u->fd >= 0)
388 pa_close(u->fd);
389
390
391 if (u->module_infos)
392 pa_hashmap_free(u->module_infos, module_info_free, u);
393
394 pa_xfree(u);
395 }