i18n: remove unneeded files from POTFILES.in
[platform/upstream/pulseaudio.git] / 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 module_info *m, unsigned i) {
135     struct userdata *u;
136
137     pa_assert(m);
138     pa_assert(i < m->n_items);
139
140     u = m->userdata;
141
142     if (m->items[i].index == PA_INVALID_INDEX)
143         return;
144
145     pa_log_debug("Unloading module #%i", m->items[i].index);
146     pa_module_unload_by_index(u->core, m->items[i].index, true);
147     m->items[i].index = PA_INVALID_INDEX;
148     pa_xfree(m->items[i].name);
149     pa_xfree(m->items[i].args);
150     m->items[i].name = m->items[i].args = NULL;
151 }
152
153 static void unload_all_modules(struct module_info *m) {
154     unsigned i;
155
156     pa_assert(m);
157
158     for (i = 0; i < m->n_items; i++)
159         unload_one_module(m, i);
160
161     m->n_items = 0;
162 }
163
164 static void load_module(
165         struct module_info *m,
166         unsigned i,
167         const char *name,
168         const char *args,
169         bool is_new) {
170
171     struct userdata *u;
172     pa_module *mod;
173
174     pa_assert(m);
175     pa_assert(name);
176     pa_assert(args);
177
178     u = m->userdata;
179
180     if (!is_new) {
181         if (m->items[i].index != PA_INVALID_INDEX &&
182             pa_streq(m->items[i].name, name) &&
183             pa_streq(m->items[i].args, args))
184             return;
185
186         unload_one_module(m, i);
187     }
188
189     pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name, args);
190
191     m->items[i].name = pa_xstrdup(name);
192     m->items[i].args = pa_xstrdup(args);
193     m->items[i].index = PA_INVALID_INDEX;
194
195     if (!(mod = pa_module_load(u->core, name, args))) {
196         pa_log("pa_module_load() failed");
197         return;
198     }
199
200     m->items[i].index = mod->index;
201 }
202
203 static void module_info_free(void *p) {
204     struct module_info *m = p;
205
206     pa_assert(m);
207
208     unload_all_modules(m);
209     pa_xfree(m->name);
210     pa_xfree(m);
211 }
212
213 static int handle_event(struct userdata *u) {
214     int opcode;
215     int ret = 0;
216
217     do {
218         if ((opcode = read_byte(u)) < 0) {
219             if (errno == EINTR || errno == EAGAIN)
220                 break;
221             goto fail;
222         }
223
224         switch (opcode) {
225             case '!':
226                 /* The helper tool is now initialized */
227                 ret = 1;
228                 break;
229
230             case '+': {
231                 char *name;
232                 struct module_info *m;
233                 unsigned i, j;
234
235                 if (!(name = read_string(u)))
236                     goto fail;
237
238                 if (!(m = pa_hashmap_get(u->module_infos, name))) {
239                     m = pa_xnew(struct module_info, 1);
240                     m->userdata = u;
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(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(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);
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, true);
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(
347 #if defined(__linux__) && !defined(__OPTIMIZE__)
348                               pa_run_from_build_tree() ? PA_BUILDDIR "/gconf-helper" :
349 #endif
350                  PA_GCONF_HELPER, NULL, &u->pid)) < 0)
351         goto fail;
352
353     u->io_event = m->core->mainloop->io_new(
354             m->core->mainloop,
355             u->fd,
356             PA_IO_EVENT_INPUT,
357             io_event_cb,
358             u);
359
360     do {
361         if ((r = handle_event(u)) < 0)
362             goto fail;
363
364         /* Read until the client signalled us that it is ready with
365          * initialization */
366     } while (r != 1);
367
368     return 0;
369
370 fail:
371     pa__done(m);
372     return -1;
373 }
374
375 void pa__done(pa_module*m) {
376     struct userdata *u;
377
378     pa_assert(m);
379
380     if (!(u = m->userdata))
381         return;
382
383     if (u->pid != (pid_t) -1) {
384         kill(u->pid, SIGTERM);
385
386         for (;;) {
387             if (waitpid(u->pid, NULL, 0) >= 0)
388                 break;
389
390             if (errno != EINTR) {
391                 pa_log("waitpid() failed: %s", pa_cstrerror(errno));
392                 break;
393             }
394         }
395     }
396
397     if (u->io_event)
398         m->core->mainloop->io_free(u->io_event);
399
400     if (u->fd >= 0)
401         pa_close(u->fd);
402
403     if (u->module_infos)
404         pa_hashmap_free(u->module_infos, (pa_free_cb_t) module_info_free);
405
406     pa_xfree(u);
407 }