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>
35 #include <pulse/xmalloc.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/core.h>
38 #include <pulsecore/llist.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/log.h>
41 #include <pulse/mainloop-api.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/start-child.h>
45 #include "module-gconf-symdef.h"
47 PA_MODULE_AUTHOR("Lennart Poettering");
48 PA_MODULE_DESCRIPTION("GConf Adapter");
49 PA_MODULE_VERSION(PACKAGE_VERSION
);
50 PA_MODULE_LOAD_ONCE(TRUE
);
52 #define MAX_MODULES 10
64 struct module_item items
[MAX_MODULES
];
72 pa_hashmap
*module_infos
;
78 pa_io_event
*io_event
;
84 static int fill_buf(struct userdata
*u
) {
88 if (u
->buf_fill
>= BUF_MAX
) {
89 pa_log("read buffer overflow");
93 if ((r
= pa_read(u
->fd
, u
->buf
+ u
->buf_fill
, BUF_MAX
- u
->buf_fill
, &u
->fd_type
)) <= 0)
96 u
->buf_fill
+= (size_t) r
;
100 static int read_byte(struct userdata
*u
) {
109 pa_assert(u
->buf_fill
> 0);
111 memmove(u
->buf
, u
->buf
+1, u
->buf_fill
);
115 static char *read_string(struct userdata
*u
) {
121 if ((e
= memchr(u
->buf
, 0, u
->buf_fill
))) {
122 char *ret
= pa_xstrdup(u
->buf
);
123 u
->buf_fill
-= (size_t) (e
- u
->buf
+1);
124 memmove(u
->buf
, e
+1, u
->buf_fill
);
133 static void unload_one_module(struct userdata
*u
, struct module_info
*m
, unsigned i
) {
136 pa_assert(i
< m
->n_items
);
138 if (m
->items
[i
].index
== PA_INVALID_INDEX
)
141 pa_log_debug("Unloading module #%i", m
->items
[i
].index
);
142 pa_module_unload_by_index(u
->core
, m
->items
[i
].index
, TRUE
);
143 m
->items
[i
].index
= PA_INVALID_INDEX
;
144 pa_xfree(m
->items
[i
].name
);
145 pa_xfree(m
->items
[i
].args
);
146 m
->items
[i
].name
= m
->items
[i
].args
= NULL
;
149 static void unload_all_modules(struct userdata
*u
, struct module_info
*m
) {
155 for (i
= 0; i
< m
->n_items
; i
++)
156 unload_one_module(u
, m
, i
);
161 static void load_module(
163 struct module_info
*m
,
177 if (m
->items
[i
].index
!= PA_INVALID_INDEX
&&
178 strcmp(m
->items
[i
].name
, name
) == 0 &&
179 strcmp(m
->items
[i
].args
, args
) == 0)
182 unload_one_module(u
, m
, i
);
185 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name
, args
);
187 m
->items
[i
].name
= pa_xstrdup(name
);
188 m
->items
[i
].args
= pa_xstrdup(args
);
189 m
->items
[i
].index
= PA_INVALID_INDEX
;
191 if (!(mod
= pa_module_load(u
->core
, name
, args
))) {
192 pa_log("pa_module_load() failed");
196 m
->items
[i
].index
= mod
->index
;
199 static void module_info_free(void *p
, void *userdata
) {
200 struct module_info
*m
= p
;
201 struct userdata
*u
= userdata
;
206 unload_all_modules(u
, m
);
211 static int handle_event(struct userdata
*u
) {
216 if ((opcode
= read_byte(u
)) < 0){
217 if (errno
== EINTR
|| errno
== EAGAIN
)
224 /* The helper tool is now initialized */
230 struct module_info
*m
;
233 if (!(name
= read_string(u
)))
236 if (!(m
= pa_hashmap_get(u
->module_infos
, name
))) {
237 m
= pa_xnew(struct module_info
, 1);
240 pa_hashmap_put(u
->module_infos
, m
->name
, m
);
245 while (i
< MAX_MODULES
) {
248 if (!(module
= read_string(u
))) {
249 if (i
> m
->n_items
) m
->n_items
= i
;
258 if (!(args
= read_string(u
))) {
261 if (i
> m
->n_items
) m
->n_items
= i
;
265 load_module(u
, m
, i
, module
, args
, i
>= m
->n_items
);
273 /* Unload all removed modules */
274 for (j
= i
; j
< m
->n_items
; j
++)
275 unload_one_module(u
, m
, j
);
284 struct module_info
*m
;
286 if (!(name
= read_string(u
)))
289 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
290 pa_hashmap_remove(u
->module_infos
, name
);
291 module_info_free(m
, u
);
299 } while (u
->buf_fill
> 0 && ret
== 0);
304 pa_log("Unable to read or parse data from client.");
308 static void io_event_cb(
312 pa_io_event_flags_t events
,
315 struct userdata
*u
= userdata
;
317 if (handle_event(u
) < 0) {
320 u
->core
->mainloop
->io_free(u
->io_event
);
324 pa_module_unload_request(u
->module
, TRUE
);
328 int pa__init(pa_module
*m
) {
332 u
= pa_xnew(struct userdata
, 1);
336 u
->module_infos
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
343 if ((u
->fd
= pa_start_child_for_read(
344 #if defined(__linux__) && !defined(__OPTIMIZE__)
345 pa_run_from_build_tree() ? PA_BUILDDIR
"/.libs/gconf-helper" :
347 PA_GCONF_HELPER
, NULL
, &u
->pid
)) < 0)
350 u
->io_event
= m
->core
->mainloop
->io_new(
358 if ((r
= handle_event(u
)) < 0)
361 /* Read until the client signalled us that it is ready with
372 void pa__done(pa_module
*m
) {
377 if (!(u
= m
->userdata
))
380 if (u
->pid
!= (pid_t
) -1) {
381 kill(u
->pid
, SIGTERM
);
384 if (waitpid(u
->pid
, NULL
, 0) >= 0)
387 if (errno
!= EINTR
) {
388 pa_log("waitpid() failed: %s", pa_cstrerror(errno
));
395 m
->core
->mainloop
->io_free(u
->io_event
);
401 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);