source: roaraudio/libroar/plugincontainer.c @ 5878:3b92b0d6ef9b

Last change on this file since 5878:3b92b0d6ef9b was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 13.9 KB
Line 
1//plugincontainer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2013
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 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;
102  ret->args_store = roar_keyval_copy(&(ret->argv), para->argv, para->argc);
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
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
131struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
132 struct roar_plugincontainer * ret = _new_init();
133 int err;
134
135 if ( ret == NULL )
136  return NULL;
137
138 if ( default_para != NULL ) {
139  ret->default_para = _copy_para(default_para, ret);
140  if ( ret->default_para == NULL ) {
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 ) {
167  ROAR_DBG("roar_plugincontainer_ref(cont=%p) = -1 // error=FAULT", cont);
168  roar_err_set(ROAR_ERROR_FAULT);
169  return -1;
170 }
171
172 cont->refc++;
173
174 ROAR_DBG("roar_plugincontainer_ref(cont=%p) = 0", cont);
175 return 0;
176}
177
178int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
179 size_t i;
180
181 if ( cont == NULL ) {
182  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = -1 // error=FAULT", cont);
183  roar_err_set(ROAR_ERROR_FAULT);
184  return -1;
185 }
186
187 cont->refc--;
188
189 if ( cont->refc ) {
190  ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont);
191  return 0;
192 }
193
194 if ( cont->callbacks != NULL && cont->callbacks->prefree != NULL )
195  cont->callbacks->prefree(cont, &(cont->userdata));
196
197 while (cont->numhandles) {
198  for (i = 0; i < MAX_PLUGINS; i++) {
199   if ( cont->handle[i] == NULL )
200    continue;
201
202   // skip plugins in use by others. We will unload them in a later loop.
203   if ( cont->deprefc[i] )
204    continue;
205
206   _unload(cont, i);
207  }
208 }
209
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
221 if ( cont->default_para != NULL )
222  roar_dl_para_unref(cont->default_para);
223
224 roar_mm_free(cont);
225
226 ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont);
227 return 0;
228}
229
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
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;
289 const struct roar_dl_libraryname * libname;
290 size_t i;
291
292 memset(&ret, 0, sizeof(ret));
293
294 if ( cont == NULL || name == NULL ) {
295  roar_err_set(ROAR_ERROR_FAULT);
296  return ret;
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 )
308    return ret;
309
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;
315  }
316 }
317
318 roar_err_set(ROAR_ERROR_NOENT);
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;
330}
331
332// plugin loading and unloading:
333int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
334 struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para);
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,
346                                                               int flags,
347                                                               int ra_init,
348                                                               struct roar_dl_librarypara * para) {
349 ssize_t idx = -1;
350 size_t i;
351 int err;
352
353 if ( cont == NULL || name == NULL ) {
354  roar_err_set(ROAR_ERROR_FAULT);
355  return NULL;
356 }
357
358 if ( para == NULL ) {
359  para = cont->default_para;
360  roar_dl_para_ref(para);
361 } else {
362  para = _copy_para(para, cont);
363 }
364
365
366 // search for a free index.
367 if ( cont->numhandles == MAX_PLUGINS ) {
368  // return early if we are full.
369  roar_dl_para_unref(para);
370  roar_err_set(ROAR_ERROR_NOSPC);
371  return NULL;
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 ) {
381  roar_dl_para_unref(para);
382  roar_err_set(ROAR_ERROR_NOSPC);
383  return NULL;
384 }
385
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);
393 if ( cont->handle[idx] == NULL ) {
394  err = roar_error;
395  roar_dl_para_unref(para);
396  roar_error = err;
397  return NULL;
398 }
399
400 cont->numhandles++;
401
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);
424
425  if ( cont->options & OPT_AUTOAPPSCHED )
426   roar_dl_appsched_trigger(cont->handle[idx], ROAR_DL_APPSCHED_INIT);
427 }
428
429 roar_dl_para_unref(para);
430 roar_dl_ref(cont->handle[idx]);
431 return cont->handle[idx];
432}
433
434int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
435 struct roar_dl_lhandle * lhandle;
436
437 if ( cont == NULL || name == NULL ) {
438  roar_err_set(ROAR_ERROR_FAULT);
439  return -1;
440 }
441
442 lhandle = roar_plugincontainer_get_lhandle_by_name(cont, name);
443 if ( lhandle == NULL )
444  return -1;
445
446 roar_dl_unref(lhandle); // we can do this early here as the handle stay valid in the container's array.
447
448 return roar_plugincontainer_unload_lhandle(cont, lhandle);
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 ) {
466   if ( cont->deprefc[i] ) {
467    // still in use.
468    roar_err_set(ROAR_ERROR_BUSY);
469    return -1;
470   }
471   _unload(cont, i);
472   return 0;
473  }
474 }
475
476 roar_err_set(ROAR_ERROR_NOENT);
477 return -1;
478}
479
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;
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
495  roar_dl_ra_init(cont->handle[i], NULL, cont->default_para);
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);
499
500  if ( cont->options & OPT_AUTOAPPSCHED )
501   roar_dl_appsched_trigger(cont->handle[i], ROAR_DL_APPSCHED_INIT);
502 }
503
504 return 0;
505}
506
507// appsched:
508int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
509 size_t i;
510 int ret = -1;
511 int rv;
512
513 if ( cont == NULL ) {
514  roar_err_set(ROAR_ERROR_FAULT);
515  return -1;
516 }
517
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
529 for (i = 0; i < MAX_PLUGINS; i++) {
530  if ( cont->handle[i] == NULL )
531   continue;
532
533  rv = roar_dl_appsched_trigger(cont->handle[i], trigger);
534  if ( rv == 0 )
535   ret = 0;
536 }
537
538 return ret;
539}
540
541//ll
Note: See TracBrowser for help on using the repository browser.