source: roaraudio/libroar/plugincontainer.c @ 5661:efd1ca5963ee

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

Improved kv API: added roar_keyval_copy()

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