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 userdata
*u
, struct module_info
*m
, unsigned i
) {
137 pa_assert(i
< m
->n_items
);
139 if (m
->items
[i
].index
== PA_INVALID_INDEX
)
142 pa_log_debug("Unloading module #%i", m
->items
[i
].index
);
143 pa_module_unload_by_index(u
->core
, m
->items
[i
].index
, TRUE
);
144 m
->items
[i
].index
= PA_INVALID_INDEX
;
145 pa_xfree(m
->items
[i
].name
);
146 pa_xfree(m
->items
[i
].args
);
147 m
->items
[i
].name
= m
->items
[i
].args
= NULL
;
150 static void unload_all_modules(struct userdata
*u
, struct module_info
*m
) {
156 for (i
= 0; i
< m
->n_items
; i
++)
157 unload_one_module(u
, m
, i
);
162 static void load_module(
164 struct module_info
*m
,
178 if (m
->items
[i
].index
!= PA_INVALID_INDEX
&&
179 pa_streq(m
->items
[i
].name
, name
) &&
180 pa_streq(m
->items
[i
].args
, args
))
183 unload_one_module(u
, m
, i
);
186 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name
, args
);
188 m
->items
[i
].name
= pa_xstrdup(name
);
189 m
->items
[i
].args
= pa_xstrdup(args
);
190 m
->items
[i
].index
= PA_INVALID_INDEX
;
192 if (!(mod
= pa_module_load(u
->core
, name
, args
))) {
193 pa_log("pa_module_load() failed");
197 m
->items
[i
].index
= mod
->index
;
200 static void module_info_free(void *p
, void *userdata
) {
201 struct module_info
*m
= p
;
202 struct userdata
*u
= userdata
;
207 unload_all_modules(u
, m
);
212 static int handle_event(struct userdata
*u
) {
217 if ((opcode
= read_byte(u
)) < 0){
218 if (errno
== EINTR
|| errno
== EAGAIN
)
225 /* The helper tool is now initialized */
231 struct module_info
*m
;
234 if (!(name
= read_string(u
)))
237 if (!(m
= pa_hashmap_get(u
->module_infos
, name
))) {
238 m
= pa_xnew(struct module_info
, 1);
242 pa_hashmap_put(u
->module_infos
, m
->name
, m
);
247 while (i
< MAX_MODULES
) {
250 if (!(module
= read_string(u
))) {
251 if (i
> m
->n_items
) m
->n_items
= i
;
260 if (!(args
= read_string(u
))) {
263 if (i
> m
->n_items
) m
->n_items
= i
;
267 load_module(u
, m
, i
, module
, args
, i
>= m
->n_items
);
275 /* Unload all removed modules */
276 for (j
= i
; j
< m
->n_items
; j
++)
277 unload_one_module(u
, m
, j
);
286 struct module_info
*m
;
288 if (!(name
= read_string(u
)))
291 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
292 pa_hashmap_remove(u
->module_infos
, name
);
293 module_info_free(m
, u
);
301 } while (u
->buf_fill
> 0 && ret
== 0);
306 pa_log("Unable to read or parse data from client.");
310 static void io_event_cb(
314 pa_io_event_flags_t events
,
317 struct userdata
*u
= userdata
;
319 if (handle_event(u
) < 0) {
322 u
->core
->mainloop
->io_free(u
->io_event
);
326 pa_module_unload_request(u
->module
, TRUE
);
330 int pa__init(pa_module
*m
) {
334 u
= pa_xnew(struct userdata
, 1);
338 u
->module_infos
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
345 if ((u
->fd
= pa_start_child_for_read(
346 #if defined(__linux__) && !defined(__OPTIMIZE__)
347 pa_run_from_build_tree() ? PA_BUILDDIR
"/gconf-helper" :
349 PA_GCONF_HELPER
, NULL
, &u
->pid
)) < 0)
352 u
->io_event
= m
->core
->mainloop
->io_new(
360 if ((r
= handle_event(u
)) < 0)
363 /* Read until the client signalled us that it is ready with
374 void pa__done(pa_module
*m
) {
379 if (!(u
= m
->userdata
))
382 if (u
->pid
!= (pid_t
) -1) {
383 kill(u
->pid
, SIGTERM
);
386 if (waitpid(u
->pid
, NULL
, 0) >= 0)
389 if (errno
!= EINTR
) {
390 pa_log("waitpid() failed: %s", pa_cstrerror(errno
));
397 m
->core
->mainloop
->io_free(u
->io_event
);
403 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);