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 | struct 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 | |
---|
48 | static 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 | |
---|
60 | static 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 | |
---|
80 | struct 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 | |
---|
94 | int 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 | |
---|
105 | int 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: |
---|
142 | int 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 | if ( strstr(name, "/") != NULL ) { |
---|
175 | cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para); |
---|
176 | if ( cont->handle[idx] == NULL ) |
---|
177 | return -1; |
---|
178 | |
---|
179 | cont->deprefc[idx] = 0; |
---|
180 | cont->numhandles++; |
---|
181 | |
---|
182 | return 0; |
---|
183 | } else { |
---|
184 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
185 | return -1; |
---|
186 | } |
---|
187 | |
---|
188 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
189 | return -1; |
---|
190 | } |
---|
191 | |
---|
192 | #if 0 |
---|
193 | int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) { |
---|
194 | // get b name, then call roar_plugincontainer_unload_lhandle(). |
---|
195 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
196 | return -1; |
---|
197 | } |
---|
198 | |
---|
199 | int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) { |
---|
200 | size_t i; |
---|
201 | |
---|
202 | if ( cont == NULL ) { |
---|
203 | roar_err_set(ROAR_ERROR_FAULT); |
---|
204 | return -1; |
---|
205 | } |
---|
206 | |
---|
207 | if ( lhandle == NULL ) { |
---|
208 | roar_err_set(ROAR_ERROR_INVAL); |
---|
209 | return -1; |
---|
210 | } |
---|
211 | |
---|
212 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
213 | if ( cont->handle[i] == lhandle ) { |
---|
214 | if ( cont->deprefc[i] ) { |
---|
215 | // still in use. |
---|
216 | roar_err_set(ROAR_ERROR_BUSY); |
---|
217 | return -1; |
---|
218 | } |
---|
219 | roar_dl_close(cont->handle[i]); |
---|
220 | cont->handle[i] = NULL; |
---|
221 | cont->numhandles--; |
---|
222 | return 0; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | roar_err_set(ROAR_ERROR_NOENT); |
---|
227 | return -1; |
---|
228 | } |
---|
229 | #endif |
---|
230 | |
---|
231 | // appsched: |
---|
232 | int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) { |
---|
233 | size_t i; |
---|
234 | |
---|
235 | if ( cont == NULL ) { |
---|
236 | roar_err_set(ROAR_ERROR_FAULT); |
---|
237 | return -1; |
---|
238 | } |
---|
239 | |
---|
240 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
241 | if ( cont->handle[i] == NULL ) |
---|
242 | continue; |
---|
243 | |
---|
244 | roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
245 | } |
---|
246 | |
---|
247 | return 0; |
---|
248 | } |
---|
249 | |
---|
250 | //ll |
---|