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