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
55 /* #undef PA_GCONF_HELPER */
56 /* #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper" */
67 struct module_item items
[MAX_MODULES
];
75 pa_hashmap
*module_infos
;
81 pa_io_event
*io_event
;
87 static int fill_buf(struct userdata
*u
) {
91 if (u
->buf_fill
>= BUF_MAX
) {
92 pa_log("read buffer overflow");
96 if ((r
= pa_read(u
->fd
, u
->buf
+ u
->buf_fill
, BUF_MAX
- u
->buf_fill
, &u
->fd_type
)) <= 0)
99 u
->buf_fill
+= (size_t) r
;
103 static int read_byte(struct userdata
*u
) {
112 pa_assert(u
->buf_fill
> 0);
114 memmove(u
->buf
, u
->buf
+1, u
->buf_fill
);
118 static char *read_string(struct userdata
*u
) {
124 if ((e
= memchr(u
->buf
, 0, u
->buf_fill
))) {
125 char *ret
= pa_xstrdup(u
->buf
);
126 u
->buf_fill
-= (size_t) (e
- u
->buf
+1);
127 memmove(u
->buf
, e
+1, u
->buf_fill
);
136 static void unload_one_module(struct userdata
*u
, struct module_info
*m
, unsigned i
) {
139 pa_assert(i
< m
->n_items
);
141 if (m
->items
[i
].index
== PA_INVALID_INDEX
)
144 pa_log_debug("Unloading module #%i", m
->items
[i
].index
);
145 pa_module_unload_by_index(u
->core
, m
->items
[i
].index
, TRUE
);
146 m
->items
[i
].index
= PA_INVALID_INDEX
;
147 pa_xfree(m
->items
[i
].name
);
148 pa_xfree(m
->items
[i
].args
);
149 m
->items
[i
].name
= m
->items
[i
].args
= NULL
;
152 static void unload_all_modules(struct userdata
*u
, struct module_info
*m
) {
158 for (i
= 0; i
< m
->n_items
; i
++)
159 unload_one_module(u
, m
, i
);
164 static void load_module(
166 struct module_info
*m
,
180 if (m
->items
[i
].index
!= PA_INVALID_INDEX
&&
181 strcmp(m
->items
[i
].name
, name
) == 0 &&
182 strcmp(m
->items
[i
].args
, args
) == 0)
185 unload_one_module(u
, m
, i
);
188 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name
, args
);
190 m
->items
[i
].name
= pa_xstrdup(name
);
191 m
->items
[i
].args
= pa_xstrdup(args
);
192 m
->items
[i
].index
= PA_INVALID_INDEX
;
194 if (!(mod
= pa_module_load(u
->core
, name
, args
))) {
195 pa_log("pa_module_load() failed");
199 m
->items
[i
].index
= mod
->index
;
202 static void module_info_free(void *p
, void *userdata
) {
203 struct module_info
*m
= p
;
204 struct userdata
*u
= userdata
;
209 unload_all_modules(u
, m
);
214 static int handle_event(struct userdata
*u
) {
219 if ((opcode
= read_byte(u
)) < 0){
220 if (errno
== EINTR
|| errno
== EAGAIN
)
227 /* The helper tool is now initialized */
233 struct module_info
*m
;
236 if (!(name
= read_string(u
)))
239 if (!(m
= pa_hashmap_get(u
->module_infos
, name
))) {
240 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(u
, 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(u
, 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
);
294 module_info_free(m
, u
);
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(PA_GCONF_HELPER
, NULL
, &u
->pid
)) < 0)
349 u
->io_event
= m
->core
->mainloop
->io_new(
357 if ((r
= handle_event(u
)) < 0)
360 /* Read until the client signalled us that it is ready with
371 void pa__done(pa_module
*m
) {
376 if (!(u
= m
->userdata
))
379 if (u
->pid
!= (pid_t
) -1) {
380 kill(u
->pid
, SIGTERM
);
383 if (waitpid(u
->pid
, NULL
, 0) >= 0)
386 if (errno
!= EINTR
) {
387 pa_log("waitpid() failed: %s", pa_cstrerror(errno
));
394 m
->core
->mainloop
->io_free(u
->io_event
);
400 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);