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