]> code.delx.au - pulseaudio/blob - polyp/module.c
add modinfo support
[pulseaudio] / polyp / module.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "module.h"
34 #include "xmalloc.h"
35 #include "subscribe.h"
36 #include "log.h"
37
38 #define PA_SYMBOL_INIT "pa__init"
39 #define PA_SYMBOL_DONE "pa__done"
40
41 #define UNLOAD_POLL_TIME 10
42
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;
45 struct timeval ntv;
46 assert(c && c->mainloop == m && c->auto_unload_event == e);
47
48 pa_module_unload_unused(c);
49
50 gettimeofday(&ntv, NULL);
51 ntv.tv_sec += UNLOAD_POLL_TIME;
52 m->time_restart(e, &ntv);
53 }
54
55 struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
56 struct pa_module *m = NULL;
57 int r;
58
59 assert(c && name);
60
61 if (c->disallow_module_loading)
62 goto fail;
63
64 m = pa_xmalloc(sizeof(struct pa_module));
65
66 m->name = pa_xstrdup(name);
67 m->argument = pa_xstrdup(argument);
68
69 if (!(m->dl = lt_dlopenext(name))) {
70 pa_log(__FILE__": Failed to open module \"%s\": %s\n", name, lt_dlerror());
71 goto fail;
72 }
73
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);
76 goto fail;
77 }
78
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);
81 goto fail;
82 }
83
84 m->userdata = NULL;
85 m->core = c;
86 m->n_used = -1;
87 m->auto_unload = 0;
88
89 assert(m->init);
90 if (m->init(c, m) < 0) {
91 pa_log(__FILE__": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.\n", name, argument ? argument : "");
92 goto fail;
93 }
94
95 if (!c->modules)
96 c->modules = pa_idxset_new(NULL, NULL);
97
98 if (!c->auto_unload_event) {
99 struct timeval ntv;
100 gettimeofday(&ntv, NULL);
101 ntv.tv_sec += UNLOAD_POLL_TIME;
102 c->auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
103 }
104 assert(c->auto_unload_event);
105
106 assert(c->modules);
107 r = pa_idxset_put(c->modules, m, &m->index);
108 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
109
110 pa_log(__FILE__": Loaded \"%s\" (index: #%u; argument: \"%s\").\n", m->name, m->index, m->argument ? m->argument : "");
111
112 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
113
114 return m;
115
116 fail:
117
118 if (m) {
119 pa_xfree(m->argument);
120 pa_xfree(m->name);
121
122 if (m->dl)
123 lt_dlclose(m->dl);
124
125 pa_xfree(m);
126 }
127
128 return NULL;
129 }
130
131 static void pa_module_free(struct pa_module *m) {
132 assert(m && m->done && m->core);
133
134 if (m->core->disallow_module_loading)
135 return;
136
137 m->done(m->core, m);
138
139 lt_dlclose(m->dl);
140
141 pa_log(__FILE__": Unloaded \"%s\" (index: #%u).\n", m->name, m->index);
142
143 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
144
145 pa_xfree(m->name);
146 pa_xfree(m->argument);
147 pa_xfree(m);
148 }
149
150 void pa_module_unload(struct pa_core *c, struct pa_module *m) {
151 assert(c && m);
152
153 assert(c->modules);
154 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
155 return;
156
157 pa_module_free(m);
158 }
159
160 void pa_module_unload_by_index(struct pa_core *c, uint32_t index) {
161 struct pa_module *m;
162 assert(c && index != PA_IDXSET_INVALID);
163
164 assert(c->modules);
165 if (!(m = pa_idxset_remove_by_index(c->modules, index)))
166 return;
167
168 pa_module_free(m);
169 }
170
171 static void free_callback(void *p, void *userdata) {
172 struct pa_module *m = p;
173 assert(m);
174 pa_module_free(m);
175 }
176
177 void pa_module_unload_all(struct pa_core *c) {
178 assert(c);
179
180 if (!c->modules)
181 return;
182
183 pa_idxset_free(c->modules, free_callback, NULL);
184 c->modules = NULL;
185
186 if (c->auto_unload_event)
187 c->mainloop->time_free(c->auto_unload_event);
188 c->auto_unload_event = NULL;
189 }
190
191 static int unused_callback(void *p, uint32_t index, int *del, void *userdata) {
192 struct pa_module *m = p;
193 time_t *now = userdata;
194 assert(p && del && now);
195
196 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->auto_unload_time <= *now) {
197 pa_module_free(m);
198 *del = 1;
199 }
200
201 return 0;
202 }
203
204 void pa_module_unload_unused(struct pa_core *c) {
205 time_t now;
206 assert(c);
207
208 if (!c->modules)
209 return;
210
211 time(&now);
212 pa_idxset_foreach(c->modules, unused_callback, &now);
213 }
214
215 struct once_info {
216 struct pa_core *core;
217 uint32_t index;
218 };
219
220 static void module_unload_once_callback(struct pa_mainloop_api *m, void *userdata) {
221 struct once_info *i = userdata;
222 assert(i);
223 pa_module_unload_by_index(i->core, i->index);
224 pa_xfree(i);
225 }
226
227 void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
228 struct once_info *i;
229 assert(c && m);
230
231 i = pa_xmalloc(sizeof(struct once_info));
232 i->core = c;
233 i->index = m->index;
234 pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
235 }
236
237 void pa_module_set_used(struct pa_module*m, int used) {
238 assert(m);
239
240 if (m->n_used != used)
241 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
242
243 if (m->n_used != used && used == 0)
244 time(&m->last_used_time);
245
246 m->n_used = used;
247 }
248
249 struct pa_modinfo *pa_module_get_info(struct pa_module *m) {
250 assert(m);
251
252 return pa_modinfo_get_by_handle(m->dl);
253 }