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"
37 #define UNLOAD_POLL_TIME 10
39 static void timeout_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
40 struct pa_core
*c
= userdata
;
42 assert(c
&& c
->mainloop
== m
&& c
->auto_unload_event
== e
);
44 pa_module_unload_unused(c
);
46 gettimeofday(&ntv
, NULL
);
47 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
48 m
->time_restart(e
, &ntv
);
51 struct pa_module
* pa_module_load(struct pa_core
*c
, const char *name
, const char *argument
) {
52 struct pa_module
*m
= NULL
;
57 m
= pa_xmalloc(sizeof(struct pa_module
));
59 m
->name
= pa_xstrdup(name
);
60 m
->argument
= pa_xstrdup(argument
);
62 if (!(m
->dl
= lt_dlopenext(name
)))
65 if (!(m
->init
= lt_dlsym(m
->dl
, "pa_module_init")))
68 if (!(m
->done
= lt_dlsym(m
->dl
, "pa_module_done")))
77 if (m
->init(c
, m
) < 0)
81 c
->modules
= pa_idxset_new(NULL
, NULL
);
83 if (!c
->auto_unload_event
) {
85 gettimeofday(&ntv
, NULL
);
86 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
87 c
->auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
89 assert(c
->auto_unload_event
);
92 r
= pa_idxset_put(c
->modules
, m
, &m
->index
);
93 assert(r
>= 0 && m
->index
!= PA_IDXSET_INVALID
);
95 fprintf(stderr
, "module: loaded %u \"%s\" with argument \"%s\".\n", m
->index
, m
->name
, m
->argument
);
97 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
103 pa_xfree(m
->argument
);
115 static void pa_module_free(struct pa_module
*m
) {
116 assert(m
&& m
->done
&& m
->core
);
121 fprintf(stderr
, "module: unloaded %u \"%s\".\n", m
->index
, m
->name
);
123 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
126 pa_xfree(m
->argument
);
131 void pa_module_unload(struct pa_core
*c
, struct pa_module
*m
) {
135 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
141 void pa_module_unload_by_index(struct pa_core
*c
, uint32_t index
) {
143 assert(c
&& index
!= PA_IDXSET_INVALID
);
146 if (!(m
= pa_idxset_remove_by_index(c
->modules
, index
)))
152 static void free_callback(void *p
, void *userdata
) {
153 struct pa_module
*m
= p
;
158 void pa_module_unload_all(struct pa_core
*c
) {
164 pa_idxset_free(c
->modules
, free_callback
, NULL
);
167 if (c
->auto_unload_event
)
168 c
->mainloop
->time_free(c
->auto_unload_event
);
169 c
->auto_unload_event
= NULL
;
172 static int unused_callback(void *p
, uint32_t index
, int *del
, void *userdata
) {
173 struct pa_module
*m
= p
;
174 time_t *now
= userdata
;
175 assert(p
&& del
&& now
);
177 if (m
->n_used
== 0 && m
->auto_unload
&& m
->last_used_time
+m
->core
->auto_unload_time
<= *now
) {
185 void pa_module_unload_unused(struct pa_core
*c
) {
193 pa_idxset_foreach(c
->modules
, unused_callback
, &now
);
197 struct pa_core
*core
;
201 static void module_unload_once_callback(struct pa_mainloop_api
*m
, void *userdata
) {
202 struct once_info
*i
= userdata
;
204 pa_module_unload_by_index(i
->core
, i
->index
);
208 void pa_module_unload_request(struct pa_core
*c
, struct pa_module
*m
) {
212 i
= pa_xmalloc(sizeof(struct once_info
));
215 pa_mainloop_api_once(c
->mainloop
, module_unload_once_callback
, i
);
218 void pa_module_set_used(struct pa_module
*m
, int used
) {
221 if (m
->n_used
!= used
)
222 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);
224 if (m
->n_used
!= used
&& used
== 0)
225 time(&m
->last_used_time
);