[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 | |
---|
| 40 | struct roar_plugincontainer { |
---|
| 41 | size_t refc; |
---|
| 42 | struct roar_dl_librarypara * default_para; |
---|
| 43 | struct roar_dl_lhandle * handle[MAX_PLUGINS]; |
---|
[5320] | 44 | size_t deprefc[MAX_PLUGINS]; |
---|
| 45 | size_t numhandles; |
---|
[5317] | 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 | |
---|
[5389] | 60 | static struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) { |
---|
[5317] | 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 | |
---|
[5320] | 118 | while (cont->numhandles) { |
---|
| 119 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 120 | if ( cont->handle[i] == NULL ) |
---|
| 121 | continue; |
---|
[5317] | 122 | |
---|
[5320] | 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]); |
---|
[5353] | 128 | cont->handle[i] = NULL; |
---|
| 129 | cont->numhandles--; |
---|
[5320] | 130 | } |
---|
[5317] | 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: |
---|
[5320] | 142 | int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) { |
---|
[5432] | 143 | struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para); |
---|
[5429] | 144 | |
---|
| 145 | if ( ret == NULL ) |
---|
| 146 | return -1; |
---|
| 147 | |
---|
| 148 | roar_dl_unref(ret); |
---|
| 149 | |
---|
| 150 | return 0; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | struct roar_dl_lhandle * roar_plugincontainer_load_lhandle (struct roar_plugincontainer * cont, |
---|
| 154 | const char * name, |
---|
[5432] | 155 | int flags, |
---|
| 156 | int ra_init, |
---|
[5429] | 157 | struct roar_dl_librarypara * para) { |
---|
[5320] | 158 | ssize_t idx = -1; |
---|
| 159 | size_t i; |
---|
| 160 | |
---|
| 161 | if ( cont == NULL || name == NULL ) { |
---|
| 162 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[5429] | 163 | return NULL; |
---|
[5320] | 164 | } |
---|
| 165 | |
---|
| 166 | if ( para == NULL ) |
---|
| 167 | para = cont->default_para; |
---|
| 168 | // we do not need to _ref(para) here. This will be done by the roardl open function. |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | // search for a free index. |
---|
| 172 | if ( cont->numhandles == MAX_PLUGINS ) { |
---|
| 173 | // return early if we are full. |
---|
| 174 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 175 | return NULL; |
---|
[5320] | 176 | } |
---|
| 177 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 178 | if ( cont->handle[i] == NULL ) { |
---|
| 179 | idx = i; |
---|
| 180 | break; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | if ( idx == -1 ) { |
---|
| 185 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 186 | return NULL; |
---|
[5320] | 187 | } |
---|
| 188 | |
---|
[5432] | 189 | cont->handle[idx] = roar_dl_open(name, flags, ra_init, para); |
---|
[5423] | 190 | if ( cont->handle[idx] == NULL ) |
---|
[5429] | 191 | return NULL; |
---|
[5423] | 192 | |
---|
| 193 | cont->deprefc[idx] = 0; |
---|
| 194 | cont->numhandles++; |
---|
| 195 | |
---|
[5429] | 196 | roar_dl_ref(cont->handle[idx]); |
---|
| 197 | return cont->handle[idx]; |
---|
[5320] | 198 | } |
---|
| 199 | |
---|
[5317] | 200 | int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) { |
---|
[5429] | 201 | const struct roar_dl_libraryname * libname; |
---|
| 202 | size_t i; |
---|
| 203 | |
---|
| 204 | if ( cont == NULL || name == NULL ) { |
---|
| 205 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 210 | if ( cont->handle[i] == NULL ) |
---|
| 211 | continue; |
---|
| 212 | libname = roar_dl_getlibname(cont->handle[i]); |
---|
| 213 | if ( libname == NULL ) |
---|
| 214 | continue; |
---|
| 215 | |
---|
| 216 | if ( !strcmp(libname->name, name) ) |
---|
| 217 | return roar_plugincontainer_unload_lhandle(cont, cont->handle[i]); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | roar_err_set(ROAR_ERROR_NOENT); |
---|
[5317] | 221 | return -1; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) { |
---|
| 225 | size_t i; |
---|
| 226 | |
---|
| 227 | if ( cont == NULL ) { |
---|
| 228 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 229 | return -1; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | if ( lhandle == NULL ) { |
---|
| 233 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 234 | return -1; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 238 | if ( cont->handle[i] == lhandle ) { |
---|
[5320] | 239 | if ( cont->deprefc[i] ) { |
---|
| 240 | // still in use. |
---|
| 241 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 242 | return -1; |
---|
| 243 | } |
---|
[5317] | 244 | roar_dl_close(cont->handle[i]); |
---|
| 245 | cont->handle[i] = NULL; |
---|
[5320] | 246 | cont->numhandles--; |
---|
[5317] | 247 | return 0; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 252 | return -1; |
---|
| 253 | } |
---|
| 254 | |
---|
[5432] | 255 | int roar_plugincontainer_ra_init (struct roar_plugincontainer * cont) { |
---|
| 256 | size_t i; |
---|
| 257 | |
---|
| 258 | if ( cont == NULL ) { |
---|
| 259 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 260 | return -1; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 264 | if ( cont->handle[i] == NULL ) |
---|
| 265 | continue; |
---|
| 266 | roar_dl_ra_init(cont->handle[i], NULL, cont->default_para); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | return 0; |
---|
| 270 | } |
---|
| 271 | |
---|
[5317] | 272 | // appsched: |
---|
| 273 | int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) { |
---|
| 274 | size_t i; |
---|
| 275 | |
---|
| 276 | if ( cont == NULL ) { |
---|
| 277 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 278 | return -1; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 282 | if ( cont->handle[i] == NULL ) |
---|
| 283 | continue; |
---|
| 284 | |
---|
| 285 | roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | return 0; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | //ll |
---|