2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
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.
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.
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
31 #include <sys/types.h>
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>
43 #include "module-gconf-symdef.h"
45 PA_MODULE_AUTHOR("Lennart Poettering");
46 PA_MODULE_DESCRIPTION("GConf Adapter");
47 PA_MODULE_VERSION(PACKAGE_VERSION);
48 PA_MODULE_LOAD_ONCE(true);
50 #define MAX_MODULES 10
62 struct userdata *userdata;
65 struct module_item items[MAX_MODULES];
73 pa_hashmap *module_infos;
79 pa_io_event *io_event;
85 static int fill_buf(struct userdata *u) {
89 if (u->buf_fill >= BUF_MAX) {
90 pa_log("read buffer overflow");
94 if ((r = pa_read(u->fd, u->buf + u->buf_fill, BUF_MAX - u->buf_fill, &u->fd_type)) <= 0)
97 u->buf_fill += (size_t) r;
101 static int read_byte(struct userdata *u) {
110 pa_assert(u->buf_fill > 0);
112 memmove(u->buf, u->buf+1, u->buf_fill);
116 static char *read_string(struct userdata *u) {
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);
134 static void unload_one_module(struct module_info *m, unsigned i) {
138 pa_assert(i < m->n_items);
142 if (m->items[i].index == PA_INVALID_INDEX)
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;
153 static void unload_all_modules(struct module_info *m) {
158 for (i = 0; i < m->n_items; i++)
159 unload_one_module(m, i);
164 static void load_module(
165 struct module_info *m,
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))
186 unload_one_module(m, i);
189 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name, args);
191 m->items[i].name = pa_xstrdup(name);
192 m->items[i].args = pa_xstrdup(args);
193 m->items[i].index = PA_INVALID_INDEX;
195 if (!(mod = pa_module_load(u->core, name, args))) {
196 pa_log("pa_module_load() failed");
200 m->items[i].index = mod->index;
203 static void module_info_free(void *p) {
204 struct module_info *m = p;
208 unload_all_modules(m);
213 static int handle_event(struct userdata *u) {
218 if ((opcode = read_byte(u)) < 0) {
219 if (errno == EINTR || errno == EAGAIN)
226 /* The helper tool is now initialized */
232 struct module_info *m;
235 if (!(name = read_string(u)))
238 if (!(m = pa_hashmap_get(u->module_infos, name))) {
239 m = pa_xnew(struct module_info, 1);
243 pa_hashmap_put(u->module_infos, m->name, m);
248 while (i < MAX_MODULES) {
251 if (!(module = read_string(u))) {
252 if (i > m->n_items) m->n_items = i;
261 if (!(args = read_string(u))) {
264 if (i > m->n_items) m->n_items = i;
268 load_module(m, i, module, args, i >= m->n_items);
276 /* Unload all removed modules */
277 for (j = i; j < m->n_items; j++)
278 unload_one_module(m, j);
287 struct module_info *m;
289 if (!(name = read_string(u)))
292 if ((m = pa_hashmap_get(u->module_infos, name))) {
293 pa_hashmap_remove(u->module_infos, name);
302 } while (u->buf_fill > 0 && ret == 0);
307 pa_log("Unable to read or parse data from client.");
311 static void io_event_cb(
315 pa_io_event_flags_t events,
318 struct userdata *u = userdata;
320 if (handle_event(u) < 0) {
323 u->core->mainloop->io_free(u->io_event);
327 pa_module_unload_request(u->module, true);
331 int pa__init(pa_module*m) {
335 u = pa_xnew(struct userdata, 1);
339 u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
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" :
350 PA_GCONF_HELPER, NULL, &u->pid)) < 0)
353 u->io_event = m->core->mainloop->io_new(
361 if ((r = handle_event(u)) < 0)
364 /* Read until the client signalled us that it is ready with
375 void pa__done(pa_module*m) {
380 if (!(u = m->userdata))
383 if (u->pid != (pid_t) -1) {
384 kill(u->pid, SIGTERM);
387 if (waitpid(u->pid, NULL, 0) >= 0)
390 if (errno != EINTR) {
391 pa_log("waitpid() failed: %s", pa_cstrerror(errno));
398 m->core->mainloop->io_free(u->io_event);
404 pa_hashmap_free(u->module_infos, (pa_free_cb_t) module_info_free);