2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
33 #include <pulse/proplist.h>
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/ltdl-helper.h>
40 #include <pulsecore/modinfo.h>
44 #define PA_SYMBOL_INIT "pa__init"
45 #define PA_SYMBOL_DONE "pa__done"
46 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
47 #define PA_SYMBOL_GET_N_USED "pa__get_n_used"
48 #define PA_SYMBOL_GET_DEPRECATE "pa__get_deprecated"
50 pa_module
* pa_module_load(pa_core
*c
, const char *name
, const char *argument
) {
52 pa_bool_t (*load_once
)(void);
53 const char* (*get_deprecated
)(void);
59 if (c
->disallow_module_loading
)
62 m
= pa_xnew(pa_module
, 1);
63 m
->name
= pa_xstrdup(name
);
64 m
->argument
= pa_xstrdup(argument
);
66 m
->proplist
= pa_proplist_new();
68 if (!(m
->dl
= lt_dlopenext(name
))) {
69 pa_log("Failed to open module \"%s\": %s", name
, lt_dlerror());
73 if ((load_once
= (pa_bool_t (*)(void)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_LOAD_ONCE
))) {
75 m
->load_once
= load_once();
80 /* OK, the module only wants to be loaded once, let's make sure it is */
82 PA_IDXSET_FOREACH(i
, c
->modules
, idx
) {
83 if (pa_streq(name
, i
->name
)) {
84 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name
);
91 if ((get_deprecated
= (const char* (*) (void)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_GET_DEPRECATE
))) {
94 if ((t
= get_deprecated()))
95 pa_log_warn("%s is deprecated: %s", name
, t
);
98 if (!(m
->init
= (int (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_INIT
))) {
99 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT
"\" not found.", name
);
103 m
->done
= (void (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_DONE
);
104 m
->get_n_used
= (int (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_GET_N_USED
);
107 m
->unload_requested
= FALSE
;
109 if (m
->init(m
) < 0) {
110 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name
, argument
? argument
: "");
114 pa_assert_se(pa_idxset_put(c
->modules
, m
, &m
->index
) >= 0);
115 pa_assert(m
->index
!= PA_IDXSET_INVALID
);
117 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m
->name
, m
->index
, m
->argument
? m
->argument
: "");
119 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
121 if ((mi
= pa_modinfo_get_by_handle(m
->dl
, name
))) {
123 if (mi
->author
&& !pa_proplist_contains(m
->proplist
, PA_PROP_MODULE_AUTHOR
))
124 pa_proplist_sets(m
->proplist
, PA_PROP_MODULE_AUTHOR
, mi
->author
);
126 if (mi
->description
&& !pa_proplist_contains(m
->proplist
, PA_PROP_MODULE_DESCRIPTION
))
127 pa_proplist_sets(m
->proplist
, PA_PROP_MODULE_DESCRIPTION
, mi
->description
);
129 if (mi
->version
&& !pa_proplist_contains(m
->proplist
, PA_PROP_MODULE_VERSION
))
130 pa_proplist_sets(m
->proplist
, PA_PROP_MODULE_VERSION
, mi
->version
);
141 pa_proplist_free(m
->proplist
);
143 pa_xfree(m
->argument
);
155 static void pa_module_free(pa_module
*m
) {
159 pa_log_info("Unloading \"%s\" (index: #%u).", m
->name
, m
->index
);
165 pa_proplist_free(m
->proplist
);
169 pa_log_info("Unloaded \"%s\" (index: #%u).", m
->name
, m
->index
);
171 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
174 pa_xfree(m
->argument
);
178 void pa_module_unload(pa_core
*c
, pa_module
*m
, pa_bool_t force
) {
182 if (m
->core
->disallow_module_loading
&& !force
)
185 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
191 void pa_module_unload_by_index(pa_core
*c
, uint32_t idx
, pa_bool_t force
) {
194 pa_assert(idx
!= PA_IDXSET_INVALID
);
196 if (c
->disallow_module_loading
&& !force
)
199 if (!(m
= pa_idxset_remove_by_index(c
->modules
, idx
)))
205 void pa_module_unload_all(pa_core
*c
) {
208 pa_idxset_remove_all(c
->modules
, (pa_free_cb_t
) pa_module_free
);
210 if (c
->module_defer_unload_event
) {
211 c
->mainloop
->defer_free(c
->module_defer_unload_event
);
212 c
->module_defer_unload_event
= NULL
;
216 static void defer_cb(pa_mainloop_api
*api
, pa_defer_event
*e
, void *userdata
) {
218 pa_core
*c
= PA_CORE(userdata
);
221 pa_core_assert_ref(c
);
222 api
->defer_enable(e
, 0);
224 while ((m
= pa_idxset_iterate(c
->modules
, &state
, NULL
)))
225 if (m
->unload_requested
)
226 pa_module_unload(c
, m
, TRUE
);
229 void pa_module_unload_request(pa_module
*m
, pa_bool_t force
) {
232 if (m
->core
->disallow_module_loading
&& !force
)
235 m
->unload_requested
= TRUE
;
237 if (!m
->core
->module_defer_unload_event
)
238 m
->core
->module_defer_unload_event
= m
->core
->mainloop
->defer_new(m
->core
->mainloop
, defer_cb
, m
->core
);
240 m
->core
->mainloop
->defer_enable(m
->core
->module_defer_unload_event
, 1);
243 void pa_module_unload_request_by_index(pa_core
*c
, uint32_t idx
, pa_bool_t force
) {
247 if (!(m
= pa_idxset_get_by_index(c
->modules
, idx
)))
250 pa_module_unload_request(m
, force
);
253 int pa_module_get_n_used(pa_module
*m
) {
259 return m
->get_n_used(m
);
262 void pa_module_update_proplist(pa_module
*m
, pa_update_mode_t mode
, pa_proplist
*p
) {
266 pa_proplist_update(m
->proplist
, mode
, p
);
268 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);