4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include "subscribe.h"
38 #define PA_SYMBOL_INIT "pa__init"
39 #define PA_SYMBOL_DONE "pa__done"
41 #define UNLOAD_POLL_TIME 2
43 static void timeout_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
44 struct pa_core
*c
= userdata
;
46 assert(c
&& c
->mainloop
== m
&& c
->module_auto_unload_event
== e
);
48 pa_module_unload_unused(c
);
50 gettimeofday(&ntv
, NULL
);
51 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
52 m
->time_restart(e
, &ntv
);
55 struct pa_module
* pa_module_load(struct pa_core
*c
, const char *name
, const char *argument
) {
56 struct pa_module
*m
= NULL
;
61 if (c
->disallow_module_loading
)
64 m
= pa_xmalloc(sizeof(struct pa_module
));
66 m
->name
= pa_xstrdup(name
);
67 m
->argument
= pa_xstrdup(argument
);
69 if (!(m
->dl
= lt_dlopenext(name
))) {
70 pa_log(__FILE__
": Failed to open module \"%s\": %s\n", name
, lt_dlerror());
74 if (!(m
->init
= (int (*)(struct pa_core
*c
, struct pa_module
*m
)) lt_dlsym(m
->dl
, PA_SYMBOL_INIT
))) {
75 pa_log(__FILE__
": Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT
"\" not found.\n", name
);
79 if (!(m
->done
= (void (*)(struct pa_core
*c
, struct pa_module
*m
)) lt_dlsym(m
->dl
, PA_SYMBOL_DONE
))) {
80 pa_log(__FILE__
": Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE
"\" not found.\n", name
);
88 m
->unload_requested
= 0;
91 if (m
->init(c
, m
) < 0) {
92 pa_log(__FILE__
": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.\n", name
, argument
? argument
: "");
97 c
->modules
= pa_idxset_new(NULL
, NULL
);
99 if (!c
->module_auto_unload_event
) {
101 gettimeofday(&ntv
, NULL
);
102 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
103 c
->module_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
105 assert(c
->module_auto_unload_event
);
108 r
= pa_idxset_put(c
->modules
, m
, &m
->index
);
109 assert(r
>= 0 && m
->index
!= PA_IDXSET_INVALID
);
111 pa_log(__FILE__
": Loaded \"%s\" (index: #%u; argument: \"%s\").\n", m
->name
, m
->index
, m
->argument
? m
->argument
: "");
113 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
120 pa_xfree(m
->argument
);
132 static void pa_module_free(struct pa_module
*m
) {
133 assert(m
&& m
->done
&& m
->core
);
135 if (m
->core
->disallow_module_loading
)
138 pa_log(__FILE__
": Unloading \"%s\" (index: #%u).\n", m
->name
, m
->index
);
144 pa_log(__FILE__
": Unloaded \"%s\" (index: #%u).\n", m
->name
, m
->index
);
146 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
149 pa_xfree(m
->argument
);
153 void pa_module_unload(struct pa_core
*c
, struct pa_module
*m
) {
157 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
163 void pa_module_unload_by_index(struct pa_core
*c
, uint32_t index
) {
165 assert(c
&& index
!= PA_IDXSET_INVALID
);
168 if (!(m
= pa_idxset_remove_by_index(c
->modules
, index
)))
174 static void free_callback(void *p
, void *userdata
) {
175 struct pa_module
*m
= p
;
180 void pa_module_unload_all(struct pa_core
*c
) {
186 pa_idxset_free(c
->modules
, free_callback
, NULL
);
189 if (c
->module_auto_unload_event
) {
190 c
->mainloop
->time_free(c
->module_auto_unload_event
);
191 c
->module_auto_unload_event
= NULL
;
194 if (c
->module_defer_unload_event
) {
195 c
->mainloop
->defer_free(c
->module_defer_unload_event
);
196 c
->module_defer_unload_event
= NULL
;
200 static int unused_callback(void *p
, uint32_t index
, int *del
, void *userdata
) {
201 struct pa_module
*m
= p
;
202 time_t *now
= userdata
;
203 assert(p
&& del
&& now
);
205 if (m
->n_used
== 0 && m
->auto_unload
&& m
->last_used_time
+m
->core
->module_idle_time
<= *now
) {
213 void pa_module_unload_unused(struct pa_core
*c
) {
221 pa_idxset_foreach(c
->modules
, unused_callback
, &now
);
224 static int unload_callback(void *p
, uint32_t index
, int *del
, void *userdata
) {
225 struct pa_module
*m
= p
;
228 if (m
->unload_requested
) {
236 static void defer_cb(struct pa_mainloop_api
*api
, struct pa_defer_event
*e
, void *userdata
) {
237 struct pa_core
*core
= userdata
;
238 api
->defer_enable(e
, 0);
243 pa_idxset_foreach(core
->modules
, unload_callback
, NULL
);
247 void pa_module_unload_request(struct pa_module
*m
) {
250 m
->unload_requested
= 1;
252 if (!m
->core
->module_defer_unload_event
)
253 m
->core
->module_defer_unload_event
= m
->core
->mainloop
->defer_new(m
->core
->mainloop
, defer_cb
, m
->core
);
255 m
->core
->mainloop
->defer_enable(m
->core
->module_defer_unload_event
, 1);
258 void pa_module_set_used(struct pa_module
*m
, int used
) {
261 if (m
->n_used
!= used
)
262 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);
264 if (m
->n_used
!= used
&& used
== 0)
265 time(&m
->last_used_time
);
270 struct pa_modinfo
*pa_module_get_info(struct pa_module
*m
) {
273 return pa_modinfo_get_by_handle(m
->dl
);