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