[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) { |
---|
[5429] | 143 | struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, para); |
---|
| 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, |
---|
| 155 | struct roar_dl_librarypara * para) { |
---|
[5320] | 156 | ssize_t idx = -1; |
---|
| 157 | size_t i; |
---|
| 158 | |
---|
| 159 | if ( cont == NULL || name == NULL ) { |
---|
| 160 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[5429] | 161 | return NULL; |
---|
[5320] | 162 | } |
---|
| 163 | |
---|
| 164 | if ( para == NULL ) |
---|
| 165 | para = cont->default_para; |
---|
| 166 | // we do not need to _ref(para) here. This will be done by the roardl open function. |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | // search for a free index. |
---|
| 170 | if ( cont->numhandles == MAX_PLUGINS ) { |
---|
| 171 | // return early if we are full. |
---|
| 172 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 173 | return NULL; |
---|
[5320] | 174 | } |
---|
| 175 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 176 | if ( cont->handle[i] == NULL ) { |
---|
| 177 | idx = i; |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if ( idx == -1 ) { |
---|
| 183 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 184 | return NULL; |
---|
[5320] | 185 | } |
---|
| 186 | |
---|
[5427] | 187 | cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_PLUGIN, 1, para); |
---|
[5423] | 188 | if ( cont->handle[idx] == NULL ) |
---|
[5429] | 189 | return NULL; |
---|
[5423] | 190 | |
---|
| 191 | cont->deprefc[idx] = 0; |
---|
| 192 | cont->numhandles++; |
---|
| 193 | |
---|
[5429] | 194 | roar_dl_ref(cont->handle[idx]); |
---|
| 195 | return cont->handle[idx]; |
---|
[5320] | 196 | } |
---|
| 197 | |
---|
[5317] | 198 | int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) { |
---|
[5429] | 199 | const struct roar_dl_libraryname * libname; |
---|
| 200 | size_t i; |
---|
| 201 | |
---|
| 202 | if ( cont == NULL || name == NULL ) { |
---|
| 203 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 204 | return -1; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 208 | if ( cont->handle[i] == NULL ) |
---|
| 209 | continue; |
---|
| 210 | libname = roar_dl_getlibname(cont->handle[i]); |
---|
| 211 | if ( libname == NULL ) |
---|
| 212 | continue; |
---|
| 213 | |
---|
| 214 | if ( !strcmp(libname->name, name) ) |
---|
| 215 | return roar_plugincontainer_unload_lhandle(cont, cont->handle[i]); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | roar_err_set(ROAR_ERROR_NOENT); |
---|
[5317] | 219 | return -1; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) { |
---|
| 223 | size_t i; |
---|
| 224 | |
---|
| 225 | if ( cont == NULL ) { |
---|
| 226 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 227 | return -1; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | if ( lhandle == NULL ) { |
---|
| 231 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 232 | return -1; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 236 | if ( cont->handle[i] == lhandle ) { |
---|
[5320] | 237 | if ( cont->deprefc[i] ) { |
---|
| 238 | // still in use. |
---|
| 239 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 240 | return -1; |
---|
| 241 | } |
---|
[5317] | 242 | roar_dl_close(cont->handle[i]); |
---|
| 243 | cont->handle[i] = NULL; |
---|
[5320] | 244 | cont->numhandles--; |
---|
[5317] | 245 | return 0; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 250 | return -1; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | // appsched: |
---|
| 254 | int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) { |
---|
| 255 | size_t i; |
---|
| 256 | |
---|
| 257 | if ( cont == NULL ) { |
---|
| 258 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 259 | return -1; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 263 | if ( cont->handle[i] == NULL ) |
---|
| 264 | continue; |
---|
| 265 | |
---|
| 266 | roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | return 0; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | //ll |
---|