source: roaraudio/libroar/plugincontainer.c @ 5427:543c052527b2

Last change on this file since 5427:543c052527b2 was 5427:543c052527b2, checked in by phi, 10 years ago

support usage of plugin path also outside plugin containers

File size: 5.6 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 struct roar_plugincontainer * _new_init(void) {
49 struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer));
50 if ( ret == NULL )
51  return NULL;
52
53 memset(ret, 0, sizeof(struct roar_plugincontainer));
54
55 ret->refc = 1;
56
57 return ret;
58}
59
60static struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) {
61 struct roar_plugincontainer * ret = _new_init();
62 int err;
63
64 if ( ret == NULL )
65  return NULL;
66
67 if ( default_para != NULL ) {
68  if ( roar_dl_para_ref(default_para) == -1 ) {
69   err = roar_error;
70   roar_plugincontainer_unref(ret);
71   roar_err_set(err);
72   return NULL;
73  }
74  ret->default_para = default_para;
75 }
76
77 return ret;
78}
79
80struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) {
81 struct roar_plugincontainer * ret;
82 struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion);
83 int err;
84
85 ret = roar_plugincontainer_new(para);
86 err = roar_error;
87
88 roar_dl_para_unref(para);
89
90 roar_err_set(err);
91 return ret;
92}
93
94int roar_plugincontainer_ref(struct roar_plugincontainer * cont) {
95 if ( cont == NULL ) {
96  roar_err_set(ROAR_ERROR_FAULT);
97  return -1;
98 }
99
100 cont->refc++;
101
102 return 0;
103}
104
105int roar_plugincontainer_unref(struct roar_plugincontainer * cont) {
106 size_t i;
107
108 if ( cont == NULL ) {
109  roar_err_set(ROAR_ERROR_FAULT);
110  return -1;
111 }
112
113 cont->refc--;
114
115 if ( cont->refc )
116  return 0;
117
118 while (cont->numhandles) {
119  for (i = 0; i < MAX_PLUGINS; i++) {
120   if ( cont->handle[i] == NULL )
121    continue;
122
123   // skip plugins in use by others. We will unload them in a later loop.
124   if ( cont->deprefc[i] )
125    continue;
126
127   roar_dl_close(cont->handle[i]);
128   cont->handle[i] = NULL;
129   cont->numhandles--;
130  }
131 }
132
133 if ( cont->default_para != NULL )
134  roar_dl_para_unref(cont->default_para);
135
136 roar_mm_free(cont);
137
138 return 0;
139}
140
141// plugin loading and unloading:
142int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) {
143 ssize_t idx = -1;
144 size_t i;
145
146 if ( cont == NULL || name == NULL ) {
147  roar_err_set(ROAR_ERROR_FAULT);
148  return -1;
149 }
150
151 if ( para == NULL )
152  para = cont->default_para;
153 // we do not need to _ref(para) here. This will be done by the roardl open function.
154
155
156 // search for a free index.
157 if ( cont->numhandles == MAX_PLUGINS ) {
158  // return early if we are full.
159  roar_err_set(ROAR_ERROR_NOSPC);
160  return -1;
161 }
162 for (i = 0; i < MAX_PLUGINS; i++) {
163  if ( cont->handle[i] == NULL ) {
164   idx = i;
165   break;
166  }
167 }
168
169 if ( idx == -1 ) {
170  roar_err_set(ROAR_ERROR_NOSPC);
171  return -1;
172 }
173
174 cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_PLUGIN, 1, para);
175 if ( cont->handle[idx] == NULL )
176  return -1;
177
178 cont->deprefc[idx] = 0;
179 cont->numhandles++;
180
181 return 0;
182}
183
184#if 0
185int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) {
186 // get b name, then call roar_plugincontainer_unload_lhandle().
187 roar_err_set(ROAR_ERROR_NOSYS);
188 return -1;
189}
190
191int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) {
192 size_t i;
193
194 if ( cont == NULL ) {
195  roar_err_set(ROAR_ERROR_FAULT);
196  return -1;
197 }
198
199 if ( lhandle == NULL ) {
200  roar_err_set(ROAR_ERROR_INVAL);
201  return -1;
202 }
203
204 for (i = 0; i < MAX_PLUGINS; i++) {
205  if ( cont->handle[i] == lhandle ) {
206   if ( cont->deprefc[i] ) {
207    // still in use.
208    roar_err_set(ROAR_ERROR_BUSY);
209    return -1;
210   }
211   roar_dl_close(cont->handle[i]);
212   cont->handle[i] = NULL;
213   cont->numhandles--;
214   return 0;
215  }
216 }
217
218 roar_err_set(ROAR_ERROR_NOENT);
219 return -1;
220}
221#endif
222
223// appsched:
224int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) {
225 size_t i;
226
227 if ( cont == NULL ) {
228  roar_err_set(ROAR_ERROR_FAULT);
229  return -1;
230 }
231
232 for (i = 0; i < MAX_PLUGINS; i++) {
233  if ( cont->handle[i] == NULL )
234   continue;
235
236  roar_dl_appsched_trigger(cont->handle[i], trigger);
237 }
238
239 return 0;
240}
241
242//ll
Note: See TracBrowser for help on using the repository browser.