source: roaraudio/libroar/plugincontainer.c @ 5504:ff89becc47ab

Last change on this file since 5504:ff89becc47ab was 5504:ff89becc47ab, checked in by phi, 12 years ago

Added support for AutoAppSched? to the plugin container.

File size: 14.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
40#define OPT_AUTOAPPSCHED 0x0001
41
42struct roar_plugincontainer {
43 size_t refc;
44 const struct roar_plugincontainer_callbacks * callbacks;
45 void * userdata;
46 unsigned int options;
47 struct roar_dl_librarypara * default_para;
48 struct roar_dl_lhandle * handle[MAX_PLUGINS];
49 size_t deprefc[MAX_PLUGINS];
50 void * context[MAX_PLUGINS];
51 size_t numhandles;
52};
53
54static inline void _unload(struct roar_plugincontainer * cont, size_t index) {
55 int err = roar_error;
56
57 if ( cont->options & OPT_AUTOAPPSCHED )
58  roar_dl_appsched_trigger(cont->handle[index], ROAR_DL_APPSCHED_FREE);
59
60 if ( cont->callbacks != NULL && cont->callbacks->preunload != NULL )
61  cont->callbacks->preunload(cont, &(cont->context[index]), cont->handle[index]);
62
63 roar_err_set(err);
64 roar_dl_unref(cont->handle[index]);
65 err = roar_error;
66 cont->handle[index] = NULL;
67 cont->numhandles--;
68
69 if ( cont->callbacks != NULL && cont->callbacks->postunload != NULL )
70  cont->callbacks->postunload(cont, &(cont->context[index]));
71
72 if ( cont->context[index] != NULL )
73  if ( cont->callbacks != NULL && cont->callbacks->freecontext != NULL )
74  cont->callbacks->freecontext(cont, &(cont->context[index]));
75
76 if ( cont->context[index] != NULL ) {
77  roar_mm_free(cont->context[index]);
78  cont->context[index] = NULL;
79 }
80
81 roar_err_set(err);
82}
83
84static int _loader(struct roar_dl_librarypara * lhandle, void * loader_userdata, enum roar_dl_loadercmd cmd, void * argp) {
85 roar_err_set(ROAR_ERROR_NOSYS);
86 return -1;
87}
88
89static inline void _copy_str(char ** dst, const char * src) {
90 for (; *src; src++, (*dst)++) **dst = *src;
91 *((*dst)++) = 0;
92}
93
94static void * _copy_kv(struct roar_keyval ** copy, struct roar_keyval * src, size_t len) {
95 size_t buflen = 0;
96 size_t i;
97 void * ret;
98 char * p;
99 struct roar_keyval * c;
100
101 for (i = 0; i < len; i++) {
102  if ( src[i].key != NULL )
103   buflen += roar_mm_strlen(src[i].key) + 1;
104  if ( src[i].value != NULL )
105   buflen += roar_mm_strlen(src[i].value) + 1;
106 }
107
108 c   = roar_mm_malloc(len * sizeof(struct roar_keyval));
109 if ( c == NULL )
110  return NULL;
111
112 memset(c, 0, len * sizeof(struct roar_keyval));
113
114 ret = roar_mm_malloc(buflen);
115 if ( ret == NULL )
116  return NULL;
117
118 memset(ret, 0, buflen);
119
120 p = ret;
121
122 for (i = 0; i < len; i++) {
123  if ( src[i].key == NULL ) {
124   c[i].key = NULL;
125  } else {
126   c[i].key = p;
127   _copy_str(&p, src[i].key);
128  }
129
130  if ( src[i].value == NULL ) {
131   c[i].value = NULL;
132  } else {
133   c[i].value = p;
134   _copy_str(&p, src[i].value);
135  }
136 }
137
138 *copy = c;
139 return ret;
140}
141
142static struct roar_dl_librarypara * _copy_para(struct roar_dl_librarypara * para, struct roar_plugincontainer * cont) {
143 struct roar_dl_librarypara * ret = NULL;
144 int err;
145
146 if ( para == NULL || cont == NULL ) {
147  roar_err_set(ROAR_ERROR_FAULT);
148  return NULL;
149 }
150
151 ret = roar_dl_para_new(NULL, para->binargv, para->appname, para->abiversion);
152
153 if ( para->argc && para->argv != NULL ) {
154  ret->argc       = para->argc;
155  ret->args_store = _copy_kv(&(ret->argv), para->argv, para->argc);
156  if ( ret->args_store == NULL ) {
157   err = roar_error;
158   roar_dl_para_unref(ret);
159   roar_error = err;
160   return NULL;
161  }
162 }
163
164 ret->notifycore      = para->notifycore;
165 ret->container       = cont;
166 ret->loader          = _loader;
167 ret->loader_userdata = NULL;
168
169 return ret;
170}
171
172static struct roar_plugincontainer * _new_init(void) {
173 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
174 if ( ret == NULL )
175  return NULL;
176
177 memset(ret, 0, sizeof(struct roar_plugincontainer));
178
179 ret->refc = 1;
180
181 return ret;
182}
183
184struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
185 struct roar_plugincontainer * ret = _new_init();
186 int err;
187
188 if ( ret == NULL )
189  return NULL;
190
191 if ( default_para != NULL ) {
192  ret->default_para = _copy_para(default_para, ret);
193  if ( ret->default_para == NULL ) {
194   err = roar_error;
195   roar_plugincontainer_unref(ret);
196   roar_err_set(err);
197   return NULL;
198  }
199 }
200
201 return ret;
202}
203
204struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
205 struct roar_plugincontainer * ret;
206 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
207 int err;
208
209 ret = roar_plugincontainer_new(para);
210 err = roar_error;
211
212 roar_dl_para_unref(para);
213
214 roar_err_set(err);
215 return ret;
216}
217
218int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
219 if ( cont == NULL ) {
220  ROAR_DBG("roar_plugincontainer_ref(cont=%p) = -1 // error=FAULT", cont);
221  roar_err_set(ROAR_ERROR_FAULT);
222  return -1;
223 }
224
225 cont->refc++;
226
227 ROAR_DBG("roar_plugincontainer_ref(cont=%p) = 0", cont);
228 return 0;
229}
230
231int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
232 size_t i;
233
234 if ( cont == NULL ) {
235  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = -1 // error=FAULT", cont);
236  roar_err_set(ROAR_ERROR_FAULT);
237  return -1;
238 }
239
240 cont->refc--;
241
242 if ( cont->refc ) {
243  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont);
244  return 0;
245 }
246
247 if ( cont->callbacks != NULL && cont->callbacks->prefree != NULL )
248  cont->callbacks->prefree(cont, &(cont->userdata));
249
250 while (cont->numhandles) {
251  for (i = 0; i < MAX_PLUGINS; i++) {
252   if ( cont->handle[i] == NULL )
253    continue;
254
255   // skip plugins in use by others. We will unload them in a later loop.
256   if ( cont->deprefc[i] )
257    continue;
258
259   _unload(cont, i);
260  }
261 }
262
263 // try to free user data...
264 if ( cont->userdata != NULL ) {
265  if ( cont->callbacks != NULL && cont->callbacks->freeuserdata != NULL ) {
266   cont->callbacks->freeuserdata(cont, &(cont->userdata));
267  }
268 }
269
270 // if still not freed by caller, free it using memmgr.
271 if ( cont->userdata != NULL )
272  roar_mm_free(cont->userdata);
273
274 if ( cont->default_para != NULL )
275  roar_dl_para_unref(cont->default_para);
276
277 roar_mm_free(cont);
278
279 ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont);
280 return 0;
281}
282
283int roar_plugincontainer_set_autoappsched(struct roar_plugincontainer * cont, int val) {
284 if ( cont == NULL ) {
285  roar_err_set(ROAR_ERROR_FAULT);
286  return -1;
287 }
288
289 if ( val ) {
290  cont->options |= OPT_AUTOAPPSCHED;
291  return 0;
292 } else {
293  if ( cont->options & OPT_AUTOAPPSCHED ) {
294   roar_err_set(ROAR_ERROR_BUSY);
295   return -1;
296  } else {
297   return 0;
298  }
299 }
300}
301
302int roar_plugincontainer_set_callbacks(struct roar_plugincontainer * cont,
303                                       const struct roar_plugincontainer_callbacks * callbacks) {
304 if ( cont == NULL ) {
305  roar_err_set(ROAR_ERROR_FAULT);
306  return -1;
307 }
308
309 cont->callbacks = callbacks;
310
311 return 0;
312}
313
314int roar_plugincontainer_set_userdata(struct roar_plugincontainer * cont, void * userdata) {
315 if ( cont == NULL ) {
316  roar_err_set(ROAR_ERROR_FAULT);
317  return -1;
318 }
319
320 cont->userdata = userdata;
321
322 return 0;
323}
324
325void * roar_plugincontainer_get_userdata(struct roar_plugincontainer * cont) {
326 if ( cont == NULL ) {
327  roar_err_set(ROAR_ERROR_FAULT);
328  return NULL;
329 }
330
331 if ( cont->userdata == NULL ) {
332  roar_err_set(ROAR_ERROR_NOENT);
333  return NULL;
334 }
335
336 return cont->userdata;
337}
338
339struct roar_plugincontainer_plugininfo roar_plugincontainer_get_info_by_name (struct roar_plugincontainer * cont,
340                                                                              const char * name) {
341 struct roar_plugincontainer_plugininfo ret;
342 const struct roar_dl_libraryname * libname;
343 size_t i;
344
345 memset(&ret, 0, sizeof(ret));
346
347 if ( cont == NULL || name == NULL ) {
348  roar_err_set(ROAR_ERROR_FAULT);
349  return ret;
350 }
351
352 for (i = 0; i < MAX_PLUGINS; i++) {
353  if ( cont->handle[i] == NULL )
354   continue;
355  libname = roar_dl_getlibname(cont->handle[i]);
356  if ( libname == NULL )
357   continue;
358
359  if ( !strcmp(libname->name, name) ) {
360   if ( roar_dl_ref(cont->handle[i]) == -1 )
361    return ret;
362
363   ret.libname  = libname->name;
364   ret.handle   = cont->handle[i];
365   ret.rdepends = cont->deprefc[i];
366   ret.context  = &(cont->context[i]);
367   return ret;
368  }
369 }
370
371 roar_err_set(ROAR_ERROR_NOENT);
372 return ret;
373}
374
375struct roar_dl_lhandle * roar_plugincontainer_get_lhandle_by_name (struct roar_plugincontainer * cont,
376                                                                   const char * name) {
377 struct roar_plugincontainer_plugininfo info = roar_plugincontainer_get_info_by_name(cont, name);
378
379 if ( info.libname == NULL )
380  return NULL;
381
382 return info.handle;
383}
384
385// plugin loading and unloading:
386int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
387 struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para);
388
389 if ( ret == NULL )
390  return -1;
391
392 roar_dl_unref(ret);
393
394 return 0;
395}
396
397struct roar_dl_lhandle * roar_plugincontainer_load_lhandle    (struct roar_plugincontainer * cont,
398                                                               const char * name,
399                                                               int flags,
400                                                               int ra_init,
401                                                               struct roar_dl_librarypara * para) {
402 ssize_t idx = -1;
403 size_t i;
404 int err;
405
406 if ( cont == NULL || name == NULL ) {
407  roar_err_set(ROAR_ERROR_FAULT);
408  return NULL;
409 }
410
411 if ( para == NULL ) {
412  para = cont->default_para;
413  roar_dl_para_ref(para);
414 } else {
415  para = _copy_para(para, cont);
416 }
417
418
419 // search for a free index.
420 if ( cont->numhandles == MAX_PLUGINS ) {
421  // return early if we are full.
422  roar_dl_para_unref(para);
423  roar_err_set(ROAR_ERROR_NOSPC);
424  return NULL;
425 }
426 for (i = 0; i < MAX_PLUGINS; i++) {
427  if ( cont->handle[i] == NULL ) {
428   idx = i;
429   break;
430  }
431 }
432
433 if ( idx == -1 ) {
434  roar_dl_para_unref(para);
435  roar_err_set(ROAR_ERROR_NOSPC);
436  return NULL;
437 }
438
439 cont->context[idx] = NULL; // clear context.
440 cont->deprefc[idx] = 0;
441
442 if ( cont->callbacks != NULL && cont->callbacks->preload != NULL )
443  cont->callbacks->preload(cont, &(cont->context[idx]), name, flags, para);
444
445 cont->handle[idx] = roar_dl_open(name, flags, 0, para);
446 if ( cont->handle[idx] == NULL ) {
447  err = roar_error;
448  roar_dl_para_unref(para);
449  roar_error = err;
450  return NULL;
451 }
452
453 cont->numhandles++;
454
455 if ( cont->callbacks != NULL && cont->callbacks->postload != NULL )
456  cont->callbacks->postload(cont, &(cont->context[idx]), cont->handle[idx], name, flags, para);
457
458 if ( ra_init ) {
459  if ( cont->callbacks != NULL && cont->callbacks->prera_init != NULL )
460   cont->callbacks->prera_init(cont, &(cont->context[idx]), cont->handle[idx], para);
461  if ( roar_dl_ra_init(cont->handle[idx], NULL, para) == -1 ) {
462   err = roar_error;
463
464   if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL )
465    cont->callbacks->postra_init(cont, &(cont->context[idx]), cont->handle[idx], para);
466
467   _unload(cont, idx);
468
469   roar_dl_para_unref(para);
470   cont->handle[idx] = NULL;
471   roar_error = err;
472   return NULL;
473  }
474
475  if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL )
476   cont->callbacks->postra_init(cont, &(cont->context[idx]), cont->handle[idx], para);
477
478  if ( cont->options & OPT_AUTOAPPSCHED )
479   roar_dl_appsched_trigger(cont->handle[idx], ROAR_DL_APPSCHED_INIT);
480 }
481
482 roar_dl_para_unref(para);
483 roar_dl_ref(cont->handle[idx]);
484 return cont->handle[idx];
485}
486
487int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
488 struct roar_dl_lhandle * lhandle;
489
490 if ( cont == NULL || name == NULL ) {
491  roar_err_set(ROAR_ERROR_FAULT);
492  return -1;
493 }
494
495 lhandle = roar_plugincontainer_get_lhandle_by_name(cont, name);
496 if ( lhandle == NULL )
497  return -1;
498
499 roar_dl_unref(lhandle); // we can do this early here as the handle stay valid in the container's array.
500
501 return roar_plugincontainer_unload_lhandle(cont, lhandle);
502}
503
504int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
505 size_t i;
506
507 if ( cont == NULL ) {
508  roar_err_set(ROAR_ERROR_FAULT);
509  return -1;
510 }
511
512 if ( lhandle == NULL ) {
513  roar_err_set(ROAR_ERROR_INVAL);
514  return -1;
515 }
516
517 for (i = 0; i < MAX_PLUGINS; i++) {
518  if ( cont->handle[i] == lhandle ) {
519   if ( cont->deprefc[i] ) {
520    // still in use.
521    roar_err_set(ROAR_ERROR_BUSY);
522    return -1;
523   }
524   _unload(cont, i);
525   return 0;
526  }
527 }
528
529 roar_err_set(ROAR_ERROR_NOENT);
530 return -1;
531}
532
533int                      roar_plugincontainer_ra_init         (struct roar_plugincontainer * cont) {
534 size_t i;
535
536 if ( cont == NULL ) {
537  roar_err_set(ROAR_ERROR_FAULT);
538  return -1;
539 }
540
541 for (i = 0; i < MAX_PLUGINS; i++) {
542  if ( cont->handle[i] == NULL )
543   continue;
544
545  if ( cont->callbacks != NULL && cont->callbacks->prera_init != NULL )
546   cont->callbacks->prera_init(cont, &(cont->context[i]), cont->handle[i], cont->default_para);
547
548  roar_dl_ra_init(cont->handle[i], NULL, cont->default_para);
549
550  if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL )
551   cont->callbacks->postra_init(cont, &(cont->context[i]), cont->handle[i], cont->default_para);
552
553  if ( cont->options & OPT_AUTOAPPSCHED )
554   roar_dl_appsched_trigger(cont->handle[i], ROAR_DL_APPSCHED_INIT);
555 }
556
557 return 0;
558}
559
560// appsched:
561int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
562 size_t i;
563 int ret = -1;
564 int rv;
565
566 if ( cont == NULL ) {
567  roar_err_set(ROAR_ERROR_FAULT);
568  return -1;
569 }
570
571 if ( cont->numhandles == 1 ) {
572  for (i = 0; i < MAX_PLUGINS; i++) {
573   if ( cont->handle[i] == NULL )
574    continue;
575   return roar_dl_appsched_trigger(cont->handle[i], trigger);
576  }
577  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
578  roar_err_set(ROAR_ERROR_FAULT);
579  return -1;
580 }
581
582 for (i = 0; i < MAX_PLUGINS; i++) {
583  if ( cont->handle[i] == NULL )
584   continue;
585
586  rv = roar_dl_appsched_trigger(cont->handle[i], trigger);
587  if ( rv == 0 )
588   ret = 0;
589 }
590
591 return ret;
592}
593
594//ll
Note: See TracBrowser for help on using the repository browser.