4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <sys/types.h>
37 #include <pulse/xmalloc.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/core.h>
40 #include <pulsecore/llist.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulse/mainloop-api.h>
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/start-child.h>
47 #include "module-gconf-symdef.h"
49 PA_MODULE_AUTHOR("Lennart Poettering");
50 PA_MODULE_DESCRIPTION("GConf Adapter");
51 PA_MODULE_VERSION(PACKAGE_VERSION
);
52 PA_MODULE_LOAD_ONCE(TRUE
);
54 #define MAX_MODULES 10
57 /* #undef PA_GCONF_HELPER */
58 /* #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper" */
69 struct module_item items
[MAX_MODULES
];
77 pa_hashmap
*module_infos
;
83 pa_io_event
*io_event
;
89 static int fill_buf(struct userdata
*u
) {
93 if (u
->buf_fill
>= BUF_MAX
) {
94 pa_log("read buffer overflow");
98 if ((r
= pa_read(u
->fd
, u
->buf
+ u
->buf_fill
, BUF_MAX
- u
->buf_fill
, &u
->fd_type
)) <= 0)
105 static int read_byte(struct userdata
*u
) {
114 pa_assert(u
->buf_fill
> 0);
116 memmove(u
->buf
, u
->buf
+1, u
->buf_fill
);
120 static char *read_string(struct userdata
*u
) {
126 if ((e
= memchr(u
->buf
, 0, u
->buf_fill
))) {
127 char *ret
= pa_xstrdup(u
->buf
);
128 u
->buf_fill
-= e
- u
->buf
+1;
129 memmove(u
->buf
, e
+1, u
->buf_fill
);
138 static void unload_one_module(struct userdata
*u
, struct module_info
*m
, unsigned i
) {
141 pa_assert(i
< m
->n_items
);
143 if (m
->items
[i
].index
== PA_INVALID_INDEX
)
146 pa_log_debug("Unloading module #%i", m
->items
[i
].index
);
147 pa_module_unload_by_index(u
->core
, m
->items
[i
].index
);
148 m
->items
[i
].index
= PA_INVALID_INDEX
;
149 pa_xfree(m
->items
[i
].name
);
150 pa_xfree(m
->items
[i
].args
);
151 m
->items
[i
].name
= m
->items
[i
].args
= NULL
;
154 static void unload_all_modules(struct userdata
*u
, struct module_info
*m
) {
160 for (i
= 0; i
< m
->n_items
; i
++)
161 unload_one_module(u
, m
, i
);
166 static void load_module(
168 struct module_info
*m
,
182 if (m
->items
[i
].index
!= PA_INVALID_INDEX
&&
183 strcmp(m
->items
[i
].name
, name
) == 0 &&
184 strcmp(m
->items
[i
].args
, args
) == 0)
187 unload_one_module(u
, m
, i
);
190 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name
, args
);
192 m
->items
[i
].name
= pa_xstrdup(name
);
193 m
->items
[i
].args
= pa_xstrdup(args
);
194 m
->items
[i
].index
= PA_INVALID_INDEX
;
196 if (!(mod
= pa_module_load(u
->core
, name
, args
))) {
197 pa_log("pa_module_load() failed");
201 m
->items
[i
].index
= mod
->index
;
204 static void module_info_free(void *p
, void *userdata
) {
205 struct module_info
*m
= p
;
206 struct userdata
*u
= userdata
;
211 unload_all_modules(u
, m
);
216 static int handle_event(struct userdata
*u
) {
221 if ((opcode
= read_byte(u
)) < 0){
222 if (errno
== EINTR
|| errno
== EAGAIN
)
229 /* The helper tool is now initialized */
235 struct module_info
*m
;
238 if (!(name
= read_string(u
)))
241 if (!(m
= pa_hashmap_get(u
->module_infos
, name
))) {
242 m
= pa_xnew(struct module_info
, 1);
245 pa_hashmap_put(u
->module_infos
, m
->name
, m
);
250 while (i
< MAX_MODULES
) {
253 if (!(module
= read_string(u
))) {
254 if (i
> m
->n_items
) m
->n_items
= i
;
263 if (!(args
= read_string(u
))) {
266 if (i
> m
->n_items
) m
->n_items
= i
;
270 load_module(u
, m
, i
, module
, args
, i
>= m
->n_items
);
278 /* Unload all removed modules */
279 for (j
= i
; j
< m
->n_items
; j
++)
280 unload_one_module(u
, m
, j
);
289 struct module_info
*m
;
291 if (!(name
= read_string(u
)))
294 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
295 pa_hashmap_remove(u
->module_infos
, name
);
296 module_info_free(m
, u
);
304 } while (u
->buf_fill
> 0 && ret
== 0);
309 pa_log("Unable to read or parse data from client.");
313 static void io_event_cb(
317 pa_io_event_flags_t events
,
320 struct userdata
*u
= userdata
;
322 if (handle_event(u
) < 0) {
325 u
->core
->mainloop
->io_free(u
->io_event
);
329 pa_module_unload_request(u
->module
);
333 int pa__init(pa_module
*m
) {
337 u
= pa_xnew(struct userdata
, 1);
341 u
->module_infos
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
348 if ((u
->fd
= pa_start_child_for_read(PA_GCONF_HELPER
, NULL
, &u
->pid
)) < 0)
351 u
->io_event
= m
->core
->mainloop
->io_new(
359 if ((r
= handle_event(u
)) < 0)
362 /* Read until the client signalled us that it is ready with
373 void pa__done(pa_module
*m
) {
378 if (!(u
= m
->userdata
))
381 if (u
->pid
!= (pid_t
) -1) {
382 kill(u
->pid
, SIGTERM
);
383 waitpid(u
->pid
, NULL
, 0);
387 m
->core
->mainloop
->io_free(u
->io_event
);
394 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);