source: roaraudio/libroar/plugincontainer.c @ 5432:956f6af25715

Last change on this file since 5432:956f6af25715 was 5432:956f6af25715, checked in by phi, 12 years ago

support post ra_initing of plugins loaded by roar_plugincontainer_load_lhandle() but not ra_inited.

File size: 6.9 KB
Line 
1//plugincontainer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#define MAX_PLUGINS 64
39
40struct roar_plugincontainer {
41 size_t refc;
42 struct roar_dl_librarypara * default_para;
43 struct roar_dl_lhandle * handle[MAX_PLUGINS];
44 size_t deprefc[MAX_PLUGINS];
45 size_t numhandles;
46};
47
48static struct roar_plugincontainer * _new_init(void) {
49 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
50 if ( ret == NULL )
51  return NULL;
52
53 memset(ret, 0, sizeof(struct roar_plugincontainer));
54
55 ret->refc = 1;
56
57 return ret;
58}
59
60static struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
61 struct roar_plugincontainer * ret = _new_init();
62 int err;
63
64 if ( ret == NULL )
65  return NULL;
66
67 if ( default_para != NULL ) {
68  if ( roar_dl_para_ref(default_para) == -1 ) {
69   err = roar_error;
70   roar_plugincontainer_unref(ret);
71   roar_err_set(err);
72   return NULL;
73  }
74  ret->default_para = default_para;
75 }
76
77 return ret;
78}
79
80struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
81 struct roar_plugincontainer * ret;
82 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
83 int err;
84
85 ret = roar_plugincontainer_new(para);
86 err = roar_error;
87
88 roar_dl_para_unref(para);
89
90 roar_err_set(err);
91 return ret;
92}
93
94int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
95 if ( cont == NULL ) {
96  roar_err_set(ROAR_ERROR_FAULT);
97  return -1;
98 }
99
100 cont->refc++;
101
102 return 0;
103}
104
105int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
106 size_t i;
107
108 if ( cont == NULL ) {
109  roar_err_set(ROAR_ERROR_FAULT);
110  return -1;
111 }
112
113 cont->refc--;
114
115 if ( cont->refc )
116  return 0;
117
118 while (cont->numhandles) {
119  for (i = 0; i < MAX_PLUGINS; i++) {
120   if ( cont->handle[i] == NULL )
121    continue;
122
123   // skip plugins in use by others. We will unload them in a later loop.
124   if ( cont->deprefc[i] )
125    continue;
126
127   roar_dl_close(cont->handle[i]);
128   cont->handle[i] = NULL;
129   cont->numhandles--;
130  }
131 }
132
133 if ( cont->default_para != NULL )
134  roar_dl_para_unref(cont->default_para);
135
136 roar_mm_free(cont);
137
138 return 0;
139}
140
141// plugin loading and unloading:
142int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
143 struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para);
144
145 if ( ret == NULL )
146  return -1;
147
148 roar_dl_unref(ret);
149
150 return 0;
151}
152
153struct roar_dl_lhandle * roar_plugincontainer_load_lhandle    (struct roar_plugincontainer * cont,
154                                                               const char * name,
155                                                               int flags,
156                                                               int ra_init,
157                                                               struct roar_dl_librarypara * para) {
158 ssize_t idx = -1;
159 size_t i;
160
161 if ( cont == NULL || name == NULL ) {
162  roar_err_set(ROAR_ERROR_FAULT);
163  return NULL;
164 }
165
166 if ( para == NULL )
167  para = cont->default_para;
168 // we do not need to _ref(para) here. This will be done by the roardl open function.
169
170
171 // search for a free index.
172 if ( cont->numhandles == MAX_PLUGINS ) {
173  // return early if we are full.
174  roar_err_set(ROAR_ERROR_NOSPC);
175  return NULL;
176 }
177 for (i = 0; i < MAX_PLUGINS; i++) {
178  if ( cont->handle[i] == NULL ) {
179   idx = i;
180   break;
181  }
182 }
183
184 if ( idx == -1 ) {
185  roar_err_set(ROAR_ERROR_NOSPC);
186  return NULL;
187 }
188
189 cont->handle[idx] = roar_dl_open(name, flags, ra_init, para);
190 if ( cont->handle[idx] == NULL )
191  return NULL;
192
193 cont->deprefc[idx] = 0;
194 cont->numhandles++;
195
196 roar_dl_ref(cont->handle[idx]);
197 return cont->handle[idx];
198}
199
200int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
201 const struct roar_dl_libraryname * libname;
202 size_t i;
203
204 if ( cont == NULL || name == NULL ) {
205  roar_err_set(ROAR_ERROR_FAULT);
206  return -1;
207 }
208
209 for (i = 0; i < MAX_PLUGINS; i++) {
210  if ( cont->handle[i] == NULL )
211   continue;
212  libname = roar_dl_getlibname(cont->handle[i]);
213  if ( libname == NULL )
214   continue;
215
216  if ( !strcmp(libname->name, name) )
217   return roar_plugincontainer_unload_lhandle(cont, cont->handle[i]);
218 }
219
220 roar_err_set(ROAR_ERROR_NOENT);
221 return -1;
222}
223
224int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
225 size_t i;
226
227 if ( cont == NULL ) {
228  roar_err_set(ROAR_ERROR_FAULT);
229  return -1;
230 }
231
232 if ( lhandle == NULL ) {
233  roar_err_set(ROAR_ERROR_INVAL);
234  return -1;
235 }
236
237 for (i = 0; i < MAX_PLUGINS; i++) {
238  if ( cont->handle[i] == lhandle ) {
239   if ( cont->deprefc[i] ) {
240    // still in use.
241    roar_err_set(ROAR_ERROR_BUSY);
242    return -1;
243   }
244   roar_dl_close(cont->handle[i]);
245   cont->handle[i] = NULL;
246   cont->numhandles--;
247   return 0;
248  }
249 }
250
251 roar_err_set(ROAR_ERROR_NOENT);
252 return -1;
253}
254
255int                      roar_plugincontainer_ra_init         (struct roar_plugincontainer * cont) {
256 size_t i;
257
258 if ( cont == NULL ) {
259  roar_err_set(ROAR_ERROR_FAULT);
260  return -1;
261 }
262
263 for (i = 0; i < MAX_PLUGINS; i++) {
264  if ( cont->handle[i] == NULL )
265   continue;
266  roar_dl_ra_init(cont->handle[i], NULL, cont->default_para);
267 }
268
269 return 0;
270}
271
272// appsched:
273int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
274 size_t i;
275
276 if ( cont == NULL ) {
277  roar_err_set(ROAR_ERROR_FAULT);
278  return -1;
279 }
280
281 for (i = 0; i < MAX_PLUGINS; i++) {
282  if ( cont->handle[i] == NULL )
283   continue;
284
285  roar_dl_appsched_trigger(cont->handle[i], trigger);
286 }
287
288 return 0;
289}
290
291//ll
Note: See TracBrowser for help on using the repository browser.