]> code.delx.au - pulseaudio/blob - polyp/module.c
8c5e318f1939c83dddf9299783664d676b4e5322
[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
36 #define UNLOAD_POLL_TIME 10
37
38 static void timeout_callback(struct pa_mainloop_api *m, void *id, const struct timeval *tv, void *userdata) {
39 struct pa_core *c = userdata;
40 struct timeval ntv;
41 assert(c && c->mainloop == m && c->auto_unload_mainloop_source == id);
42
43 pa_module_unload_unused(c);
44
45 gettimeofday(&ntv, NULL);
46 ntv.tv_sec += UNLOAD_POLL_TIME;
47 m->enable_time(m, id, &ntv);
48 }
49
50 struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
51 struct pa_module *m = NULL;
52 int r;
53
54 assert(c && name);
55
56 m = pa_xmalloc(sizeof(struct pa_module));
57
58 m->name = pa_xstrdup(name);
59 m->argument = pa_xstrdup(argument);
60
61 if (!(m->dl = lt_dlopenext(name)))
62 goto fail;
63
64 if (!(m->init = lt_dlsym(m->dl, "pa_module_init")))
65 goto fail;
66
67 if (!(m->done = lt_dlsym(m->dl, "pa_module_done")))
68 goto fail;
69
70 m->userdata = NULL;
71 m->core = c;
72 m->n_used = -1;
73 m->auto_unload = 0;
74
75 assert(m->init);
76 if (m->init(c, m) < 0)
77 goto fail;
78
79 if (!c->modules)
80 c->modules = pa_idxset_new(NULL, NULL);
81
82 if (!c->auto_unload_mainloop_source) {
83 struct timeval ntv;
84 gettimeofday(&ntv, NULL);
85 ntv.tv_sec += UNLOAD_POLL_TIME;
86 c->auto_unload_mainloop_source = c->mainloop->source_time(c->mainloop, &ntv, timeout_callback, c);
87 }
88 assert(c->auto_unload_mainloop_source);
89
90 assert(c->modules);
91 r = pa_idxset_put(c->modules, m, &m->index);
92 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
93
94 fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
95
96 return m;
97
98 fail:
99 if (m) {
100 pa_xfree(m->argument);
101 pa_xfree(m->name);
102
103 if (m->dl)
104 lt_dlclose(m->dl);
105
106 pa_xfree(m);
107 }
108
109 return NULL;
110 }
111
112 static void pa_module_free(struct pa_module *m) {
113 assert(m && m->done && m->core);
114 m->done(m->core, m);
115
116 lt_dlclose(m->dl);
117
118 fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
119
120 pa_xfree(m->name);
121 pa_xfree(m->argument);
122 pa_xfree(m);
123 }
124
125
126 void pa_module_unload(struct pa_core *c, struct pa_module *m) {
127 assert(c && m);
128
129 assert(c->modules);
130 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
131 return;
132
133 pa_module_free(m);
134 }
135
136 void pa_module_unload_by_index(struct pa_core *c, uint32_t index) {
137 struct pa_module *m;
138 assert(c && index != PA_IDXSET_INVALID);
139
140 assert(c->modules);
141 if (!(m = pa_idxset_remove_by_index(c->modules, index)))
142 return;
143
144 pa_module_free(m);
145 }
146
147 static void free_callback(void *p, void *userdata) {
148 struct pa_module *m = p;
149 assert(m);
150 pa_module_free(m);
151 }
152
153 void pa_module_unload_all(struct pa_core *c) {
154 assert(c);
155
156 if (!c->modules)
157 return;
158
159 pa_idxset_free(c->modules, free_callback, NULL);
160 c->modules = NULL;
161
162 if (c->auto_unload_mainloop_source)
163 c->mainloop->cancel_time(c->mainloop, c->auto_unload_mainloop_source);
164 c->auto_unload_mainloop_source = NULL;
165 }
166
167 static int unused_callback(void *p, uint32_t index, int *del, void *userdata) {
168 struct pa_module *m = p;
169 time_t *now = userdata;
170 assert(p && del && now);
171
172 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->auto_unload_time <= *now) {
173 pa_module_free(m);
174 *del = 1;
175 }
176
177 return 0;
178 }
179
180 void pa_module_unload_unused(struct pa_core *c) {
181 time_t now;
182 assert(c);
183
184 if (!c->modules)
185 return;
186
187 time(&now);
188 pa_idxset_foreach(c->modules, unused_callback, &now);
189 }
190
191 struct once_info {
192 struct pa_core *core;
193 uint32_t index;
194 };
195
196 static void module_unload_once_callback(void *userdata) {
197 struct once_info *i = userdata;
198 assert(i);
199 pa_module_unload_by_index(i->core, i->index);
200 pa_xfree(i);
201 }
202
203 void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
204 struct once_info *i;
205 assert(c && m);
206
207 i = pa_xmalloc(sizeof(struct once_info));
208 i->core = c;
209 i->index = m->index;
210 pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
211 }
212
213 void pa_module_set_used(struct pa_module*m, int used) {
214 assert(m);
215
216 if (m->n_used != used && used == 0)
217 time(&m->last_used_time);
218
219 m->n_used = used;
220 }
221