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 module_item items
[MAX_MODULES
];
70 pa_hashmap
*module_infos
;
76 pa_io_event
*io_event
;
82 static int fill_buf(struct userdata
*u
) {
86 if (u
->buf_fill
>= BUF_MAX
) {
87 pa_log("read buffer overflow");
91 if ((r
= pa_read(u
->fd
, u
->buf
+ u
->buf_fill
, BUF_MAX
- u
->buf_fill
, &u
->fd_type
)) <= 0)
94 u
->buf_fill
+= (size_t) r
;
98 static int read_byte(struct userdata
*u
) {
107 pa_assert(u
->buf_fill
> 0);
109 memmove(u
->buf
, u
->buf
+1, u
->buf_fill
);
113 static char *read_string(struct userdata
*u
) {
119 if ((e
= memchr(u
->buf
, 0, u
->buf_fill
))) {
120 char *ret
= pa_xstrdup(u
->buf
);
121 u
->buf_fill
-= (size_t) (e
- u
->buf
+1);
122 memmove(u
->buf
, e
+1, u
->buf_fill
);
131 static void unload_one_module(struct userdata
*u
, struct module_info
*m
, unsigned i
) {
134 pa_assert(i
< m
->n_items
);
136 if (m
->items
[i
].index
== PA_INVALID_INDEX
)
139 pa_log_debug("Unloading module #%i", m
->items
[i
].index
);
140 pa_module_unload_by_index(u
->core
, m
->items
[i
].index
, TRUE
);
141 m
->items
[i
].index
= PA_INVALID_INDEX
;
142 pa_xfree(m
->items
[i
].name
);
143 pa_xfree(m
->items
[i
].args
);
144 m
->items
[i
].name
= m
->items
[i
].args
= NULL
;
147 static void unload_all_modules(struct userdata
*u
, struct module_info
*m
) {
153 for (i
= 0; i
< m
->n_items
; i
++)
154 unload_one_module(u
, m
, i
);
159 static void load_module(
161 struct module_info
*m
,
175 if (m
->items
[i
].index
!= PA_INVALID_INDEX
&&
176 pa_streq(m
->items
[i
].name
, name
) &&
177 pa_streq(m
->items
[i
].args
, args
))
180 unload_one_module(u
, m
, i
);
183 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name
, args
);
185 m
->items
[i
].name
= pa_xstrdup(name
);
186 m
->items
[i
].args
= pa_xstrdup(args
);
187 m
->items
[i
].index
= PA_INVALID_INDEX
;
189 if (!(mod
= pa_module_load(u
->core
, name
, args
))) {
190 pa_log("pa_module_load() failed");
194 m
->items
[i
].index
= mod
->index
;
197 static void module_info_free(void *p
, void *userdata
) {
198 struct module_info
*m
= p
;
199 struct userdata
*u
= userdata
;
204 unload_all_modules(u
, m
);
209 static int handle_event(struct userdata
*u
) {
214 if ((opcode
= read_byte(u
)) < 0){
215 if (errno
== EINTR
|| errno
== EAGAIN
)
222 /* The helper tool is now initialized */
228 struct module_info
*m
;
231 if (!(name
= read_string(u
)))
234 if (!(m
= pa_hashmap_get(u
->module_infos
, name
))) {
235 m
= pa_xnew(struct module_info
, 1);
238 pa_hashmap_put(u
->module_infos
, m
->name
, m
);
243 while (i
< MAX_MODULES
) {
246 if (!(module
= read_string(u
))) {
247 if (i
> m
->n_items
) m
->n_items
= i
;
256 if (!(args
= read_string(u
))) {
259 if (i
> m
->n_items
) m
->n_items
= i
;
263 load_module(u
, m
, i
, module
, args
, i
>= m
->n_items
);
271 /* Unload all removed modules */
272 for (j
= i
; j
< m
->n_items
; j
++)
273 unload_one_module(u
, m
, j
);
282 struct module_info
*m
;
284 if (!(name
= read_string(u
)))
287 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
288 pa_hashmap_remove(u
->module_infos
, name
);
289 module_info_free(m
, u
);
297 } while (u
->buf_fill
> 0 && ret
== 0);
302 pa_log("Unable to read or parse data from client.");
306 static void io_event_cb(
310 pa_io_event_flags_t events
,
313 struct userdata
*u
= userdata
;
315 if (handle_event(u
) < 0) {
318 u
->core
->mainloop
->io_free(u
->io_event
);
322 pa_module_unload_request(u
->module
, TRUE
);
326 int pa__init(pa_module
*m
) {
330 u
= pa_xnew(struct userdata
, 1);
334 u
->module_infos
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
341 if ((u
->fd
= pa_start_child_for_read(
342 #if defined(__linux__) && !defined(__OPTIMIZE__)
343 pa_run_from_build_tree() ? PA_BUILDDIR
"/gconf-helper" :
345 PA_GCONF_HELPER
, NULL
, &u
->pid
)) < 0)
348 u
->io_event
= m
->core
->mainloop
->io_new(
356 if ((r
= handle_event(u
)) < 0)
359 /* Read until the client signalled us that it is ready with
370 void pa__done(pa_module
*m
) {
375 if (!(u
= m
->userdata
))
378 if (u
->pid
!= (pid_t
) -1) {
379 kill(u
->pid
, SIGTERM
);
382 if (waitpid(u
->pid
, NULL
, 0) >= 0)
385 if (errno
!= EINTR
) {
386 pa_log("waitpid() failed: %s", pa_cstrerror(errno
));
393 m
->core
->mainloop
->io_free(u
->io_event
);
399 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);