[3353] | 1 | //plugins.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2014 |
---|
[3353] | 5 | * |
---|
| 6 | * This file is part of roard 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 | * RoarAudio 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[3353] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "roard.h" |
---|
| 27 | |
---|
[3355] | 28 | #define MAX_PLUGINS 8 |
---|
| 29 | |
---|
[3362] | 30 | static struct _roard_plugin { |
---|
| 31 | struct roar_dl_lhandle * lhandle; |
---|
| 32 | } g_plugins[MAX_PLUGINS]; |
---|
| 33 | static struct _roard_plugin * _pp = NULL; |
---|
[3355] | 34 | |
---|
[5567] | 35 | static int _plugins_inited = 0; |
---|
| 36 | |
---|
[5576] | 37 | static int plugin_callback(enum roar_dl_fnreg_action action, int fn, int subtype, const void * object, size_t objectlen, int version, int options, void * userdata, struct roar_dl_lhandle * lhandle); |
---|
| 38 | static const struct roar_dl_fnreg _plugin_callbacks = { |
---|
| 39 | .fn = -1, |
---|
| 40 | .subtype = -1, |
---|
| 41 | .version = -1, |
---|
| 42 | .callback = plugin_callback, |
---|
| 43 | .userdata = NULL |
---|
| 44 | }; |
---|
| 45 | |
---|
[3362] | 46 | static struct _roard_plugin * _find_free(void) { |
---|
[3355] | 47 | int i; |
---|
| 48 | |
---|
[5592] | 49 | ROAR_DBG("_find_free(void) = ?"); |
---|
| 50 | |
---|
[3355] | 51 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
[3362] | 52 | if ( g_plugins[i].lhandle == NULL ) { |
---|
| 53 | memset(&(g_plugins[i]), 0, sizeof(struct _roard_plugin)); |
---|
[5592] | 54 | ROAR_DBG("_find_free(void) = %p // i=%i", &(g_plugins[i]), i); |
---|
[3355] | 55 | return &(g_plugins[i]); |
---|
[3362] | 56 | } |
---|
[3355] | 57 | } |
---|
| 58 | |
---|
[5592] | 59 | ROAR_DBG("_find_free(void) = NULL"); |
---|
[3355] | 60 | return NULL; |
---|
| 61 | } |
---|
| 62 | |
---|
[3354] | 63 | int plugins_preinit (void) { |
---|
[5592] | 64 | ROAR_DBG("plugins_preinit(void) = ?"); |
---|
| 65 | |
---|
[3355] | 66 | memset(g_plugins, 0, sizeof(g_plugins)); |
---|
| 67 | |
---|
[5592] | 68 | #ifdef DEBUG |
---|
| 69 | print_pluginlist(FORMAT_NATIVE); |
---|
| 70 | #endif |
---|
| 71 | |
---|
| 72 | ROAR_DBG("plugins_preinit(void) = 0"); |
---|
[3354] | 73 | return 0; |
---|
| 74 | } |
---|
| 75 | |
---|
[5586] | 76 | static inline void plugins_delete(struct _roard_plugin * plugin) { |
---|
[5592] | 77 | ROAR_DBG("plugins_delete(plugin=%p) = ?", plugin); |
---|
| 78 | |
---|
[5319] | 79 | roar_dl_appsched_trigger(plugin->lhandle, ROAR_DL_APPSCHED_FREE); |
---|
| 80 | |
---|
[5275] | 81 | roar_dl_close(plugin->lhandle); |
---|
| 82 | memset(plugin, 0, sizeof(struct _roard_plugin)); |
---|
| 83 | plugin->lhandle = NULL; |
---|
| 84 | } |
---|
| 85 | |
---|
[5567] | 86 | static int plugins_init_one(struct _roard_plugin * plugin) { |
---|
| 87 | if ( plugin == NULL ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
[5592] | 90 | ROAR_DBG("plugins_init_one(plugin=%p) = ?", plugin); |
---|
| 91 | |
---|
[5567] | 92 | _pp = plugin; |
---|
| 93 | |
---|
| 94 | if ( roar_dl_ra_init(plugin->lhandle, NULL, NULL) == -1 ) { |
---|
| 95 | ROAR_WARN("plugins_init(void): Can not RA init lib at %p: %s", plugin->lhandle, roar_error2str(roar_error)); |
---|
| 96 | plugins_delete(plugin); |
---|
| 97 | return -1; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | roar_dl_appsched_trigger(plugin->lhandle, ROAR_DL_APPSCHED_INIT); |
---|
| 101 | |
---|
| 102 | _pp = NULL; |
---|
| 103 | |
---|
| 104 | return 0; |
---|
| 105 | } |
---|
| 106 | |
---|
[3354] | 107 | int plugins_init (void) { |
---|
[5567] | 108 | size_t i; |
---|
| 109 | |
---|
[5592] | 110 | ROAR_DBG("plugins_init(void) = ?"); |
---|
| 111 | |
---|
| 112 | #ifdef DEBUG |
---|
| 113 | print_pluginlist(FORMAT_NATIVE); |
---|
| 114 | #endif |
---|
| 115 | |
---|
[5567] | 116 | if ( _plugins_inited ) { |
---|
[5592] | 117 | ROAR_DBG("plugins_init(void) = -1 // error=BUSY"); |
---|
[5567] | 118 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 119 | return -1; |
---|
| 120 | } |
---|
[3355] | 121 | |
---|
[5582] | 122 | ROAR_DL_RFNREG(ROAR_DL_HANDLE_APPLICATION, _plugin_callbacks); |
---|
[5576] | 123 | |
---|
[3355] | 124 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
[3362] | 125 | if ( g_plugins[i].lhandle != NULL ) { |
---|
[5567] | 126 | plugins_init_one(&(g_plugins[i])); |
---|
[3355] | 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
[5567] | 130 | _plugins_inited = 1; |
---|
| 131 | |
---|
[5592] | 132 | #ifdef DEBUG |
---|
| 133 | print_pluginlist(FORMAT_NATIVE); |
---|
| 134 | #endif |
---|
| 135 | |
---|
| 136 | ROAR_DBG("plugins_init(void) = 0"); |
---|
[3354] | 137 | return 0; |
---|
| 138 | } |
---|
[3353] | 139 | |
---|
[3354] | 140 | int plugins_free (void) { |
---|
[3355] | 141 | int i; |
---|
| 142 | |
---|
[5592] | 143 | ROAR_DBG("plugins_free(void) = ?"); |
---|
| 144 | |
---|
[3355] | 145 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
[3362] | 146 | if ( g_plugins[i].lhandle != NULL ) { |
---|
[5275] | 147 | plugins_delete(&(g_plugins[i])); |
---|
[3355] | 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
[3357] | 151 | return plugins_preinit(); |
---|
[3354] | 152 | } |
---|
| 153 | |
---|
[3362] | 154 | int plugins_update (void) { |
---|
| 155 | int ret = 0; |
---|
| 156 | int i; |
---|
| 157 | |
---|
[5312] | 158 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 159 | if ( g_plugins[i].lhandle != NULL ) { |
---|
[5319] | 160 | if ( roar_dl_appsched_trigger(g_plugins[i].lhandle, ROAR_DL_APPSCHED_UPDATE) == -1 ) |
---|
| 161 | if ( roar_error != ROAR_ERROR_NOENT ) |
---|
| 162 | ret = -1; |
---|
[5312] | 163 | } |
---|
| 164 | } |
---|
[3362] | 165 | |
---|
| 166 | return ret; |
---|
| 167 | } |
---|
| 168 | |
---|
[5275] | 169 | int plugins_load (const char * filename, const char * args) { |
---|
[3362] | 170 | struct _roard_plugin * next = _find_free(); |
---|
[5275] | 171 | struct roar_dl_librarypara * para; |
---|
[3355] | 172 | |
---|
[5592] | 173 | ROAR_DBG("plugins_load(filename=\"%s\", args=\"%s\") = ?", filename, args); |
---|
| 174 | |
---|
[3355] | 175 | if ( next == NULL ) |
---|
| 176 | return -1; |
---|
| 177 | |
---|
[5275] | 178 | if ( (para = roar_dl_para_new(args, NULL, ROARD_DL_APPNAME, ROARD_DL_ABIVERSION)) == NULL ) { |
---|
| 179 | ROAR_WARN("Can not load plugin (allocate para set): %s: %s", filename, roar_error2str(roar_error)); |
---|
| 180 | return -1; |
---|
| 181 | } |
---|
| 182 | |
---|
[5427] | 183 | next->lhandle = roar_dl_open(filename, ROAR_DL_FLAG_PLUGIN, 0 /* we delay this until plugins_init() */, para); |
---|
[5275] | 184 | roar_dl_para_unref(para); |
---|
| 185 | |
---|
[3365] | 186 | if ( next->lhandle == NULL ) { |
---|
| 187 | ROAR_ERR("plugins_load(filename='%s'): can not load plugin: %s", filename, roar_dl_errstr(NULL)); |
---|
[3355] | 188 | return -1; |
---|
[3365] | 189 | } |
---|
[3355] | 190 | |
---|
[5567] | 191 | if ( _plugins_inited ) |
---|
| 192 | return plugins_init_one(next); |
---|
| 193 | |
---|
[3355] | 194 | return 0; |
---|
[3354] | 195 | } |
---|
[3353] | 196 | |
---|
[5592] | 197 | void print_pluginlist(enum output_format format) { |
---|
| 198 | const struct roar_dl_libraryname * libname; |
---|
| 199 | struct _roard_plugin * p; |
---|
| 200 | size_t i; |
---|
| 201 | |
---|
| 202 | switch (format) { |
---|
| 203 | case FORMAT_NATIVE: |
---|
| 204 | printf(" Name\n"); |
---|
| 205 | printf(" Attributes\n"); |
---|
| 206 | printf("-----------------------------------------------------\n"); |
---|
| 207 | break; |
---|
| 208 | case FORMAT_WIKI: |
---|
| 209 | case FORMAT_CSV: |
---|
| 210 | default: |
---|
| 211 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 212 | return; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 216 | p = &(g_plugins[i]); |
---|
| 217 | if ( p->lhandle == NULL ) |
---|
| 218 | continue; |
---|
| 219 | |
---|
| 220 | libname = roar_dl_getlibname(p->lhandle); |
---|
| 221 | if ( libname == NULL ) { |
---|
| 222 | } else { |
---|
| 223 | printf(" %s\n", libname->libname); |
---|
[5639] | 224 | printf(" Flags: %s\n", ""); |
---|
[5592] | 225 | if ( libname->description != NULL ) |
---|
| 226 | printf(" Description: %s\n", libname->description); |
---|
| 227 | if ( libname->contact != NULL ) |
---|
| 228 | printf(" Contact: %s\n", libname->contact); |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
[5576] | 233 | static int plugin_callback(enum roar_dl_fnreg_action action, int fn, int subtype, const void * object, size_t objectlen, int version, int options, void * userdata, struct roar_dl_lhandle * lhandle) { |
---|
| 234 | |
---|
[5586] | 235 | (void)options, (void)userdata; |
---|
| 236 | |
---|
[5576] | 237 | ROAR_DBG("plugin_callback(action=%i, fn=%i, subtype=%i, object=%p, objectlen=%llu, version=%i, options=0x%x, userdata=%p, lhandle=%p) = ?", (int)action, fn, subtype, object, (long long unsigned int)objectlen, version, options, userdata, lhandle); |
---|
| 238 | |
---|
| 239 | switch (fn) { |
---|
| 240 | case ROAR_DL_FN_PROTO: |
---|
| 241 | if ( subtype != ROAR_DL_PROTO_SUBTYPE ) { |
---|
| 242 | ROAR_DBG("plugin_callback(action=%i, fn=%i, subtype=%i, object=%p, objectlen=%llu, version=%i, options=0x%x, userdata=%p, lhandle=%p) = -1 // error=TYPEMM", (int)action, fn, subtype, object, (long long unsigned int)objectlen, version, options, userdata, lhandle); |
---|
| 243 | |
---|
| 244 | roar_err_set(ROAR_ERROR_TYPEMM); |
---|
| 245 | return -1; |
---|
| 246 | } |
---|
| 247 | if ( objectlen != ROAR_DL_PROTO_SIZE ) { |
---|
| 248 | ROAR_DBG("plugin_callback(action=%i, fn=%i, subtype=%i, object=%p, objectlen=%llu, version=%i, options=0x%x, userdata=%p, lhandle=%p) = -1 // error=BADLIB", (int)action, fn, subtype, object, (long long unsigned int)objectlen, version, options, userdata, lhandle); |
---|
| 249 | roar_err_set(ROAR_ERROR_BADLIB); |
---|
| 250 | return -1; |
---|
| 251 | } |
---|
| 252 | if ( version != ROAR_DL_PROTO_VERSION ) { |
---|
| 253 | ROAR_DBG("plugin_callback(action=%i, fn=%i, subtype=%i, object=%p, objectlen=%llu, version=%i, options=0x%x, userdata=%p, lhandle=%p) = -1 // error=BADVERSION", (int)action, fn, subtype, object, (long long unsigned int)objectlen, version, options, userdata, lhandle); |
---|
| 254 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 255 | return -1; |
---|
| 256 | } |
---|
| 257 | switch (action) { |
---|
| 258 | case ROAR_DL_FNREG: |
---|
| 259 | return clients_register_proto_common(object, lhandle); |
---|
| 260 | break; |
---|
| 261 | case ROAR_DL_FNUNREG: |
---|
| 262 | return clients_unregister_proto(((const struct roard_proto *)object)->proto); |
---|
| 263 | break; |
---|
| 264 | } |
---|
| 265 | break; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | ROAR_DBG("plugin_callback(action=%i, fn=%i, subtype=%i, object=%p, objectlen=%llu, version=%i, options=0x%x, userdata=%p, lhandle=%p) = -1 // error=NOTSUP", (int)action, fn, subtype, object, (long long unsigned int)objectlen, version, options, userdata, lhandle); |
---|
| 269 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 270 | return -1; |
---|
| 271 | } |
---|
| 272 | |
---|
[3353] | 273 | //ll |
---|