source: roaraudio/libroar/plugincontainer.c @ 5436:a29ea4926470

Last change on this file since 5436:a29ea4926470 was 5436:a29ea4926470, checked in by phi, 10 years ago

allows the use of costume para parameters

File size: 8.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 int _loader(struct roar_dl_librarypara * lhandle, void * loader_userdata, enum roar_dl_loadercmd cmd, void * argp) {
49 roar_err_set(ROAR_ERROR_NOSYS);
50 return -1;
51}
52
53static inline void _copy_str(char ** dst, const char * src) {
54 for (; *src; src++, (*dst)++) **dst = *src;
55 *((*dst)++) = 0;
56}
57
58static void * _copy_kv(struct roar_keyval ** copy, struct roar_keyval * src, size_t len) {
59 size_t buflen, i;
60 void * ret;
61 char * p;
62 struct roar_keyval * c;
63
64 buflen = len * (sizeof(struct roar_keyval) + 2 /* terminating \0s */);
65
66 for (i = 0; i < len; i++) {
67  if ( src[i].key != NULL )
68   buflen += roar_mm_strlen(src[i].key);
69  if ( src[i].value != NULL )
70   buflen += roar_mm_strlen(src[i].value);
71 }
72
73 ret = roar_mm_malloc(buflen);
74 if ( ret == NULL )
75  return NULL;
76
77 memset(ret, 0, buflen);
78
79 c = ret;
80 p = ret + len*sizeof(struct roar_keyval);
81
82 for (i = 0; i < len; i++) {
83  if ( src[i].key == NULL ) {
84   c[i].key = NULL;
85  } else {
86   c[i].key = p;
87   _copy_str(&p, src[i].key);
88  }
89
90  if ( src[i].value == NULL ) {
91   c[i].value = NULL;
92  } else {
93   c[i].value = p;
94   _copy_str(&p, src[i].value);
95  }
96 }
97
98 *copy = c;
99 return ret;
100}
101
102static struct roar_dl_librarypara * _copy_para(struct roar_dl_librarypara * para, struct roar_plugincontainer * cont) {
103 struct roar_dl_librarypara * ret = NULL;
104 int err;
105
106 if ( para == NULL || cont == NULL ) {
107  roar_err_set(ROAR_ERROR_FAULT);
108  return NULL;
109 }
110
111 ret = roar_dl_para_new(NULL, para->binargv, para->appname, para->abiversion);
112
113 if ( para->argc && para->argv != NULL ) {
114  ret->argc       = para->argc;
115  ret->args_store = _copy_kv(&(ret->argv), para->argv, para->argc);
116  if ( ret->args_store == NULL ) {
117   err = roar_error;
118   roar_dl_para_unref(ret);
119   roar_error = err;
120   return NULL;
121  }
122 }
123
124 ret->notifycore      = para->notifycore;
125 ret->container       = cont;
126 ret->loader          = _loader;
127 ret->loader_userdata = NULL;
128
129 return ret;
130}
131
132static struct roar_plugincontainer * _new_init(void) {
133 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
134 if ( ret == NULL )
135  return NULL;
136
137 memset(ret, 0, sizeof(struct roar_plugincontainer));
138
139 ret->refc = 1;
140
141 return ret;
142}
143
144struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
145 struct roar_plugincontainer * ret = _new_init();
146 int err;
147
148 if ( ret == NULL )
149  return NULL;
150
151 if ( default_para != NULL ) {
152  ret->default_para = _copy_para(default_para, ret);
153  if ( ret->default_para == NULL ) {
154   err = roar_error;
155   roar_plugincontainer_unref(ret);
156   roar_err_set(err);
157   return NULL;
158  }
159 }
160
161 return ret;
162}
163
164struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
165 struct roar_plugincontainer * ret;
166 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
167 int err;
168
169 ret = roar_plugincontainer_new(para);
170 err = roar_error;
171
172 roar_dl_para_unref(para);
173
174 roar_err_set(err);
175 return ret;
176}
177
178int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
179 if ( cont == NULL ) {
180  roar_err_set(ROAR_ERROR_FAULT);
181  return -1;
182 }
183
184 cont->refc++;
185
186 return 0;
187}
188
189int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
190 size_t i;
191
192 if ( cont == NULL ) {
193  roar_err_set(ROAR_ERROR_FAULT);
194  return -1;
195 }
196
197 cont->refc--;
198
199 if ( cont->refc )
200  return 0;
201
202 while (cont->numhandles) {
203  for (i = 0; i < MAX_PLUGINS; i++) {
204   if ( cont->handle[i] == NULL )
205    continue;
206
207   // skip plugins in use by others. We will unload them in a later loop.
208   if ( cont->deprefc[i] )
209    continue;
210
211   roar_dl_close(cont->handle[i]);
212   cont->handle[i] = NULL;
213   cont->numhandles--;
214  }
215 }
216
217 if ( cont->default_para != NULL )
218  roar_dl_para_unref(cont->default_para);
219
220 roar_mm_free(cont);
221
222 return 0;
223}
224
225// plugin loading and unloading:
226int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
227 struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para);
228
229 if ( ret == NULL )
230  return -1;
231
232 roar_dl_unref(ret);
233
234 return 0;
235}
236
237struct roar_dl_lhandle * roar_plugincontainer_load_lhandle    (struct roar_plugincontainer * cont,
238                                                               const char * name,
239                                                               int flags,
240                                                               int ra_init,
241                                                               struct roar_dl_librarypara * para) {
242 ssize_t idx = -1;
243 size_t i;
244 int err;
245
246 if ( cont == NULL || name == NULL ) {
247  roar_err_set(ROAR_ERROR_FAULT);
248  return NULL;
249 }
250
251 if ( para == NULL ) {
252  para = cont->default_para;
253  roar_dl_para_ref(para);
254 } else {
255  para = _copy_para(para, cont);
256 }
257
258
259 // search for a free index.
260 if ( cont->numhandles == MAX_PLUGINS ) {
261  // return early if we are full.
262  roar_dl_para_unref(para);
263  roar_err_set(ROAR_ERROR_NOSPC);
264  return NULL;
265 }
266 for (i = 0; i < MAX_PLUGINS; i++) {
267  if ( cont->handle[i] == NULL ) {
268   idx = i;
269   break;
270  }
271 }
272
273 if ( idx == -1 ) {
274  roar_dl_para_unref(para);
275  roar_err_set(ROAR_ERROR_NOSPC);
276  return NULL;
277 }
278
279 cont->handle[idx] = roar_dl_open(name, flags, ra_init, para);
280 if ( cont->handle[idx] == NULL ) {
281  err = roar_error;
282  roar_dl_para_unref(para);
283  roar_error = err;
284  return NULL;
285 }
286
287 cont->deprefc[idx] = 0;
288 cont->numhandles++;
289
290 roar_dl_para_unref(para);
291 roar_dl_ref(cont->handle[idx]);
292 return cont->handle[idx];
293}
294
295int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
296 const struct roar_dl_libraryname * libname;
297 size_t i;
298
299 if ( cont == NULL || name == NULL ) {
300  roar_err_set(ROAR_ERROR_FAULT);
301  return -1;
302 }
303
304 for (i = 0; i < MAX_PLUGINS; i++) {
305  if ( cont->handle[i] == NULL )
306   continue;
307  libname = roar_dl_getlibname(cont->handle[i]);
308  if ( libname == NULL )
309   continue;
310
311  if ( !strcmp(libname->name, name) )
312   return roar_plugincontainer_unload_lhandle(cont, cont->handle[i]);
313 }
314
315 roar_err_set(ROAR_ERROR_NOENT);
316 return -1;
317}
318
319int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
320 size_t i;
321
322 if ( cont == NULL ) {
323  roar_err_set(ROAR_ERROR_FAULT);
324  return -1;
325 }
326
327 if ( lhandle == NULL ) {
328  roar_err_set(ROAR_ERROR_INVAL);
329  return -1;
330 }
331
332 for (i = 0; i < MAX_PLUGINS; i++) {
333  if ( cont->handle[i] == lhandle ) {
334   if ( cont->deprefc[i] ) {
335    // still in use.
336    roar_err_set(ROAR_ERROR_BUSY);
337    return -1;
338   }
339   roar_dl_close(cont->handle[i]);
340   cont->handle[i] = NULL;
341   cont->numhandles--;
342   return 0;
343  }
344 }
345
346 roar_err_set(ROAR_ERROR_NOENT);
347 return -1;
348}
349
350int                      roar_plugincontainer_ra_init         (struct roar_plugincontainer * cont) {
351 size_t i;
352
353 if ( cont == NULL ) {
354  roar_err_set(ROAR_ERROR_FAULT);
355  return -1;
356 }
357
358 for (i = 0; i < MAX_PLUGINS; i++) {
359  if ( cont->handle[i] == NULL )
360   continue;
361  roar_dl_ra_init(cont->handle[i], NULL, cont->default_para);
362 }
363
364 return 0;
365}
366
367// appsched:
368int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
369 size_t i;
370
371 if ( cont == NULL ) {
372  roar_err_set(ROAR_ERROR_FAULT);
373  return -1;
374 }
375
376 for (i = 0; i < MAX_PLUGINS; i++) {
377  if ( cont->handle[i] == NULL )
378   continue;
379
380  roar_dl_appsched_trigger(cont->handle[i], trigger);
381 }
382
383 return 0;
384}
385
386//ll
Note: See TracBrowser for help on using the repository browser.