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