4 This file is part of PulseAudio.
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 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
32 #include <sys/types.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/core.h>
37 #include <pulsecore/llist.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/log.h>
40 #include <pulse/mainloop-api.h>
41 #include <pulse/xmalloc.h>
42 #include <pulsecore/core-error.h>
44 #include "module-gconf-symdef.h"
46 PA_MODULE_AUTHOR("Lennart Poettering")
47 PA_MODULE_DESCRIPTION("GConf Adapter")
48 PA_MODULE_VERSION(PACKAGE_VERSION
)
51 #define MAX_MODULES 10
54 #undef PA_GCONF_HELPER
55 #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper"
61 uint32_t indexes
[MAX_MODULES
];
68 pa_hashmap
*module_infos
;
74 pa_io_event
*io_event
;
80 static int fill_buf(struct userdata
*u
) {
84 if (u
->buf_fill
>= BUF_MAX
) {
85 pa_log(__FILE__
": read buffer overflow");
89 if ((r
= pa_read(u
->fd
, u
->buf
+ u
->buf_fill
, BUF_MAX
- u
->buf_fill
, &u
->fd_type
)) <= 0)
96 static int read_byte(struct userdata
*u
) {
105 assert(u
->buf_fill
> 0);
107 memmove(u
->buf
, u
->buf
+1, u
->buf_fill
);
111 static char *read_string(struct userdata
*u
) {
117 if ((e
= memchr(u
->buf
, 0, u
->buf_fill
))) {
118 char *ret
= pa_xstrdup(u
->buf
);
119 u
->buf_fill
-= e
- u
->buf
+1;
120 memmove(u
->buf
, e
+1, u
->buf_fill
);
129 static void unload_modules(struct userdata
*u
, struct module_info
*m
) {
135 for (i
= 0; i
< m
->n_indexes
; i
++) {
136 pa_log_debug(__FILE__
": Unloading module #%i", m
->indexes
[i
]);
137 pa_module_unload_by_index(u
->core
, m
->indexes
[i
]);
143 static void load_module(
145 struct module_info
*m
,
155 assert(m
->n_indexes
< MAX_MODULES
);
157 pa_log_debug(__FILE__
": Loading module '%s' with args '%s' due to GConf configuration.", module
, args
);
159 if (!(mod
= pa_module_load(u
->core
, module
, args
))) {
160 pa_log(__FILE__
": pa_module_load() failed");
164 m
->indexes
[m
->n_indexes
++] = mod
->index
;
167 static void module_info_free(void *p
, void *userdata
) {
168 struct module_info
*m
= p
;
169 struct userdata
*u
= userdata
;
174 unload_modules(u
, m
);
179 static int handle_event(struct userdata
*u
) {
184 if ((opcode
= read_byte(u
)) < 0)
189 /* The helper tool is now initialized */
195 struct module_info
*m
;
197 if (!(name
= read_string(u
)))
200 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
201 unload_modules(u
, m
);
203 m
= pa_xnew(struct module_info
, 1);
204 m
->name
= pa_xstrdup(name
);
206 pa_hashmap_put(u
->module_infos
, m
->name
, m
);
209 while (m
->n_indexes
< MAX_MODULES
) {
212 if (!(module
= read_string(u
))) {
222 if (!(args
= read_string(u
))) {
228 load_module(u
, m
, module
, args
);
241 struct module_info
*m
;
243 if (!(name
= read_string(u
)))
246 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
247 pa_hashmap_remove(u
->module_infos
, name
);
248 module_info_free(m
, u
);
256 } while (u
->buf_fill
> 0 && ret
== 0);
261 pa_log(__FILE__
": Unable to read or parse data from client.");
265 static void io_event_cb(
269 pa_io_event_flags_t events
,
272 struct userdata
*u
= userdata
;
277 static int start_client(const char *n
, pid_t
*pid
) {
279 int pipe_fds
[2] = { -1, -1 };
281 if (pipe(pipe_fds
) < 0) {
282 pa_log(__FILE__
": pipe() failed: %s", pa_cstrerror(errno
));
286 if ((child
= fork()) == (pid_t
) -1) {
287 pa_log(__FILE__
": fork() failed: %s", pa_cstrerror(errno
));
289 } else if (child
!= 0) {
303 dup2(pipe_fds
[1], 1);
305 if (pipe_fds
[1] != 1)
313 if (pipe_fds
[0] >= 0)
316 if (pipe_fds
[1] >= 0)
322 int pa__init(pa_core
*c
, pa_module
*m
) {
326 u
= pa_xnew(struct userdata
, 1);
329 u
->module_infos
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
336 if ((u
->fd
= start_client(PA_GCONF_HELPER
, &u
->pid
)) < 0)
339 u
->io_event
= c
->mainloop
->io_new(
347 if ((r
= handle_event(u
)) < 0)
350 /* Read until the client signalled us that it is ready with
361 void pa__done(pa_core
*c
, pa_module
*m
) {
367 if (!(u
= m
->userdata
))
371 c
->mainloop
->io_free(u
->io_event
);
376 if (u
->pid
!= (pid_t
) -1) {
377 kill(u
->pid
, SIGTERM
);
378 waitpid(u
->pid
, NULL
, 0);
382 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);