source: roaraudio/libroar/plugincontainer.c @ 5425:2c3f247e8f9a

Last change on this file since 5425:2c3f247e8f9a was 5425:2c3f247e8f9a, checked in by phi, 10 years ago

use SHARED_SUFFIX from configure script, not hardcoded list

File size: 8.8 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#ifdef ROAR_HAVE_H_DIRENT
39#include <dirent.h>
40#endif
41
42#define MAX_PLUGINS 64
43
44struct roar_plugincontainer {
45 size_t refc;
46 struct roar_dl_librarypara * default_para;
47 struct roar_dl_lhandle * handle[MAX_PLUGINS];
48 size_t deprefc[MAX_PLUGINS];
49 size_t numhandles;
50};
51
52static struct roar_plugincontainer * _new_init(void) {
53 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
54 if ( ret == NULL )
55  return NULL;
56
57 memset(ret, 0, sizeof(struct roar_plugincontainer));
58
59 ret->refc = 1;
60
61 return ret;
62}
63
64static struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
65 struct roar_plugincontainer * ret = _new_init();
66 int err;
67
68 if ( ret == NULL )
69  return NULL;
70
71 if ( default_para != NULL ) {
72  if ( roar_dl_para_ref(default_para) == -1 ) {
73   err = roar_error;
74   roar_plugincontainer_unref(ret);
75   roar_err_set(err);
76   return NULL;
77  }
78  ret->default_para = default_para;
79 }
80
81 return ret;
82}
83
84struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
85 struct roar_plugincontainer * ret;
86 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
87 int err;
88
89 ret = roar_plugincontainer_new(para);
90 err = roar_error;
91
92 roar_dl_para_unref(para);
93
94 roar_err_set(err);
95 return ret;
96}
97
98int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
99 if ( cont == NULL ) {
100  roar_err_set(ROAR_ERROR_FAULT);
101  return -1;
102 }
103
104 cont->refc++;
105
106 return 0;
107}
108
109int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
110 size_t i;
111
112 if ( cont == NULL ) {
113  roar_err_set(ROAR_ERROR_FAULT);
114  return -1;
115 }
116
117 cont->refc--;
118
119 if ( cont->refc )
120  return 0;
121
122 while (cont->numhandles) {
123  for (i = 0; i < MAX_PLUGINS; i++) {
124   if ( cont->handle[i] == NULL )
125    continue;
126
127   // skip plugins in use by others. We will unload them in a later loop.
128   if ( cont->deprefc[i] )
129    continue;
130
131   roar_dl_close(cont->handle[i]);
132   cont->handle[i] = NULL;
133   cont->numhandles--;
134  }
135 }
136
137 if ( cont->default_para != NULL )
138  roar_dl_para_unref(cont->default_para);
139
140 roar_mm_free(cont);
141
142 return 0;
143}
144
145// plugin loading and unloading:
146#ifdef ROAR_HAVE_H_DIRENT
147// pvn = prefix, host vendor, host name
148static struct roar_dl_lhandle * _load_from_path_pvn(const char * name,
149                                                       struct roar_dl_librarypara * para,
150                                                       const char * prefix,
151                                                       const char * hostvendor,
152                                                       const char * hostname) {
153 struct roar_dl_lhandle * ret = NULL;
154 int i, j;
155 char path[1024];
156 const char * path_format[] = {"%s/%s/%s", "%s/%s/universal", "%s/universal/universal"};
157 DIR * dir;
158 struct dirent * dirent;
159 char file[1024];
160 const char * file_format[] = {"%s/%s/%s", "%s/%s/%s" ROAR_SHARED_SUFFIX, "%s/%s/lib%s" ROAR_SHARED_SUFFIX};
161//  cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para);
162//#vars: $PREFIX_PLUGINS, $hostvendor, $hostname, $name
163//#search order: $PREFIX_PLUGINS/{$hostvendor/{$hostname,universal},universal/universal}/*/{,lib}$name.so
164
165 for (i = 0; i < 3; i++) {
166  snprintf(path, sizeof(path), path_format[i], prefix, hostvendor, hostname);
167  dir = opendir(path);
168  if ( dir == NULL )
169   continue;
170
171  while ((dirent = readdir(dir)) != NULL) {
172   if ( !strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..") )
173    continue;
174
175   for (j = 0; j < 3; j++) {
176    snprintf(file, sizeof(file), file_format[j], path, dirent->d_name, name);
177    ret = roar_dl_open(file, ROAR_DL_FLAG_DEFAULTS, 1, para);
178    if ( ret != NULL ) {
179     closedir(dir);
180     return ret;
181    }
182   }
183  }
184
185  closedir(dir);
186 }
187
188 return NULL;
189}
190
191static struct roar_dl_lhandle * _load_from_path(const char * name, struct roar_dl_librarypara * para) {
192 struct roar_dl_lhandle * ret = NULL;
193 char * host = NULL;
194 char * hostvendor_buffer = NULL;
195 char * hostvendor = NULL;
196 char * hostname = NULL;
197 char * c, * d;
198
199 if ( para->appname != NULL && para->appname[0] != 0 ) {
200  host = roar_mm_strdup(para->appname);
201
202  // if we are out of memory we will likely not pass the rest, so just return in error.
203  if ( host == NULL )
204   return NULL;
205
206  hostname = host;
207  c = strstr(host, "<");
208  if ( c != NULL ) {
209   while (c[-1] == ' ') c--;
210   *c = 0;
211   c++;
212   while (*c == ' ' || *c == '<') c++;
213   if ( *c ) {
214    d = strstr(c, ">");
215    if ( d != NULL )
216     *d = 0;
217
218    d = strstr(c, "/");
219    if ( d != NULL ) {
220     *d = '-';
221     hostvendor = c;
222    } else {
223     hostvendor_buffer = roar_mm_malloc(roar_mm_strlen(c) + 1 /* tailing \0 */ + 6 /* "unreg-" */);
224
225     // see above
226     if ( hostvendor_buffer == NULL ) {
227      roar_mm_free(host);
228      return NULL;
229     }
230
231     strcpy(hostvendor_buffer, "unreg-");
232     strcat(hostvendor_buffer, c);
233     hostvendor = hostvendor_buffer;
234    }
235   }
236  }
237 }
238
239 if ( hostvendor == NULL )
240  hostvendor = "universal";
241
242 if ( hostname == NULL )
243  hostname = "universal";
244
245 ret = _load_from_path_pvn(name, para, ROAR_PREFIX_PLUGINS, hostvendor, hostname);
246
247 if ( host != NULL )
248  roar_mm_free(host);
249 if ( hostvendor_buffer != NULL )
250  roar_mm_free(hostvendor_buffer);
251 return ret;
252}
253#endif
254
255int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
256 ssize_t idx = -1;
257 size_t i;
258
259 if ( cont == NULL || name == NULL ) {
260  roar_err_set(ROAR_ERROR_FAULT);
261  return -1;
262 }
263
264 if ( para == NULL )
265  para = cont->default_para;
266 // we do not need to _ref(para) here. This will be done by the roardl open function.
267
268
269 // search for a free index.
270 if ( cont->numhandles == MAX_PLUGINS ) {
271  // return early if we are full.
272  roar_err_set(ROAR_ERROR_NOSPC);
273  return -1;
274 }
275 for (i = 0; i < MAX_PLUGINS; i++) {
276  if ( cont->handle[i] == NULL ) {
277   idx = i;
278   break;
279  }
280 }
281
282 if ( idx == -1 ) {
283  roar_err_set(ROAR_ERROR_NOSPC);
284  return -1;
285 }
286
287 if ( strstr(name, "/") != NULL ) {
288  cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para);
289 } else {
290#ifdef ROAR_HAVE_H_DIRENT
291  cont->handle[idx] = _load_from_path(name, para);
292#else
293  roar_err_set(ROAR_ERROR_NOSYS);
294  return -1;
295#endif
296 }
297
298 if ( cont->handle[idx] == NULL )
299  return -1;
300
301 cont->deprefc[idx] = 0;
302 cont->numhandles++;
303
304 return 0;
305}
306
307#if 0
308int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
309 // get b name, then call roar_plugincontainer_unload_lhandle().
310 roar_err_set(ROAR_ERROR_NOSYS);
311 return -1;
312}
313
314int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
315 size_t i;
316
317 if ( cont == NULL ) {
318  roar_err_set(ROAR_ERROR_FAULT);
319  return -1;
320 }
321
322 if ( lhandle == NULL ) {
323  roar_err_set(ROAR_ERROR_INVAL);
324  return -1;
325 }
326
327 for (i = 0; i < MAX_PLUGINS; i++) {
328  if ( cont->handle[i] == lhandle ) {
329   if ( cont->deprefc[i] ) {
330    // still in use.
331    roar_err_set(ROAR_ERROR_BUSY);
332    return -1;
333   }
334   roar_dl_close(cont->handle[i]);
335   cont->handle[i] = NULL;
336   cont->numhandles--;
337   return 0;
338  }
339 }
340
341 roar_err_set(ROAR_ERROR_NOENT);
342 return -1;
343}
344#endif
345
346// appsched:
347int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
348 size_t i;
349
350 if ( cont == NULL ) {
351  roar_err_set(ROAR_ERROR_FAULT);
352  return -1;
353 }
354
355 for (i = 0; i < MAX_PLUGINS; i++) {
356  if ( cont->handle[i] == NULL )
357   continue;
358
359  roar_dl_appsched_trigger(cont->handle[i], trigger);
360 }
361
362 return 0;
363}
364
365//ll
Note: See TracBrowser for help on using the repository browser.