[3353] | 1 | //plugins.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
[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 | struct roard_plugins_sched * sched; |
---|
[5275] | 33 | int protocols[MAX_PROTOS]; |
---|
[3362] | 34 | } g_plugins[MAX_PLUGINS]; |
---|
| 35 | static struct _roard_plugin * _pp = NULL; |
---|
[3355] | 36 | |
---|
[3362] | 37 | static struct _roard_plugin * _find_free(void) { |
---|
[3355] | 38 | int i; |
---|
| 39 | |
---|
| 40 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
[3362] | 41 | if ( g_plugins[i].lhandle == NULL ) { |
---|
| 42 | memset(&(g_plugins[i]), 0, sizeof(struct _roard_plugin)); |
---|
[3355] | 43 | return &(g_plugins[i]); |
---|
[3362] | 44 | } |
---|
[3355] | 45 | } |
---|
| 46 | |
---|
| 47 | return NULL; |
---|
| 48 | } |
---|
| 49 | |
---|
[3354] | 50 | int plugins_preinit (void) { |
---|
[3355] | 51 | memset(g_plugins, 0, sizeof(g_plugins)); |
---|
| 52 | |
---|
[3354] | 53 | return 0; |
---|
| 54 | } |
---|
| 55 | |
---|
[5275] | 56 | static void inline plugins_delete(struct _roard_plugin * plugin) { |
---|
| 57 | int i; |
---|
| 58 | |
---|
[5312] | 59 | if ( plugin->sched != NULL ) { |
---|
| 60 | if ( plugin->sched->free != NULL ) { |
---|
| 61 | roar_dl_context_restore(plugin->lhandle); |
---|
[5275] | 62 | plugin->sched->free(); |
---|
[5312] | 63 | roar_dl_context_store(plugin->lhandle); |
---|
| 64 | } |
---|
| 65 | } |
---|
[5275] | 66 | |
---|
[5319] | 67 | roar_dl_appsched_trigger(plugin->lhandle, ROAR_DL_APPSCHED_FREE); |
---|
| 68 | |
---|
[5275] | 69 | for (i = 0; i < MAX_PROTOS; i++) { |
---|
| 70 | if ( plugin->protocols[i] != -1 ) { |
---|
| 71 | clients_unregister_proto(plugin->protocols[i]); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | roar_dl_close(plugin->lhandle); |
---|
| 76 | memset(plugin, 0, sizeof(struct _roard_plugin)); |
---|
| 77 | plugin->lhandle = NULL; |
---|
| 78 | } |
---|
| 79 | |
---|
[3354] | 80 | int plugins_init (void) { |
---|
[3355] | 81 | int i; |
---|
| 82 | |
---|
| 83 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
[3362] | 84 | if ( g_plugins[i].lhandle != NULL ) { |
---|
| 85 | _pp = &(g_plugins[i]); |
---|
| 86 | |
---|
[4682] | 87 | _pp->sched = NULL; |
---|
| 88 | |
---|
[5275] | 89 | if ( roar_dl_ra_init(g_plugins[i].lhandle, NULL, NULL) == -1 ) { |
---|
| 90 | ROAR_WARN("plugins_init(void): Can not RA init lib at %p: %s", g_plugins[i].lhandle, roar_error2str(roar_error)); |
---|
| 91 | plugins_delete(&(g_plugins[i])); |
---|
| 92 | continue; |
---|
| 93 | } |
---|
[3362] | 94 | |
---|
[5312] | 95 | if ( g_plugins[i].sched != NULL ) { |
---|
| 96 | if ( g_plugins[i].sched->init != NULL ) { |
---|
| 97 | roar_dl_context_restore(g_plugins[i].lhandle); |
---|
[4682] | 98 | g_plugins[i].sched->init(); |
---|
[5312] | 99 | roar_dl_context_store(g_plugins[i].lhandle); |
---|
| 100 | } |
---|
| 101 | } |
---|
[3362] | 102 | |
---|
[5319] | 103 | roar_dl_appsched_trigger(g_plugins[i].lhandle, ROAR_DL_APPSCHED_INIT); |
---|
| 104 | |
---|
[3362] | 105 | _pp = NULL; |
---|
[3355] | 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
[3354] | 109 | return 0; |
---|
| 110 | } |
---|
[3353] | 111 | |
---|
[3354] | 112 | int plugins_free (void) { |
---|
[3355] | 113 | int i; |
---|
| 114 | |
---|
| 115 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
[3362] | 116 | if ( g_plugins[i].lhandle != NULL ) { |
---|
[5275] | 117 | plugins_delete(&(g_plugins[i])); |
---|
[3355] | 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[3357] | 121 | return plugins_preinit(); |
---|
[3354] | 122 | } |
---|
| 123 | |
---|
[3362] | 124 | int plugins_update (void) { |
---|
| 125 | int ret = 0; |
---|
| 126 | int i; |
---|
| 127 | |
---|
[5312] | 128 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 129 | if ( g_plugins[i].lhandle != NULL ) { |
---|
| 130 | if ( g_plugins[i].sched != NULL ) { |
---|
| 131 | if ( g_plugins[i].sched->update != NULL ) { |
---|
| 132 | roar_dl_context_restore(g_plugins[i].lhandle); |
---|
[4682] | 133 | if ( g_plugins[i].sched->update() == -1 ) |
---|
| 134 | ret = -1; |
---|
[5312] | 135 | roar_dl_context_store(g_plugins[i].lhandle); |
---|
| 136 | } |
---|
| 137 | } |
---|
[5319] | 138 | if ( roar_dl_appsched_trigger(g_plugins[i].lhandle, ROAR_DL_APPSCHED_UPDATE) == -1 ) |
---|
| 139 | if ( roar_error != ROAR_ERROR_NOENT ) |
---|
| 140 | ret = -1; |
---|
[5312] | 141 | } |
---|
| 142 | } |
---|
[3362] | 143 | |
---|
| 144 | return ret; |
---|
| 145 | } |
---|
| 146 | |
---|
[5275] | 147 | int plugins_load (const char * filename, const char * args) { |
---|
[3362] | 148 | struct _roard_plugin * next = _find_free(); |
---|
[5275] | 149 | struct roar_dl_librarypara * para; |
---|
| 150 | int i; |
---|
[3355] | 151 | |
---|
| 152 | if ( next == NULL ) |
---|
| 153 | return -1; |
---|
| 154 | |
---|
[5275] | 155 | for (i = 0; i < MAX_PROTOS; i++) |
---|
| 156 | next->protocols[i] = -1; |
---|
| 157 | |
---|
| 158 | if ( (para = roar_dl_para_new(args, NULL, ROARD_DL_APPNAME, ROARD_DL_ABIVERSION)) == NULL ) { |
---|
| 159 | ROAR_WARN("Can not load plugin (allocate para set): %s: %s", filename, roar_error2str(roar_error)); |
---|
| 160 | return -1; |
---|
| 161 | } |
---|
| 162 | |
---|
[5427] | 163 | next->lhandle = roar_dl_open(filename, ROAR_DL_FLAG_PLUGIN, 0 /* we delay this until plugins_init() */, para); |
---|
[5275] | 164 | roar_dl_para_unref(para); |
---|
| 165 | |
---|
[3365] | 166 | if ( next->lhandle == NULL ) { |
---|
| 167 | ROAR_ERR("plugins_load(filename='%s'): can not load plugin: %s", filename, roar_dl_errstr(NULL)); |
---|
[3355] | 168 | return -1; |
---|
[3365] | 169 | } |
---|
[3355] | 170 | |
---|
| 171 | return 0; |
---|
[3354] | 172 | } |
---|
[3353] | 173 | |
---|
[3362] | 174 | int plugins_reg_sched(struct roard_plugins_sched * sched) { |
---|
| 175 | if ( _pp == NULL ) |
---|
| 176 | return -1; |
---|
| 177 | |
---|
| 178 | _pp->sched = sched; |
---|
| 179 | |
---|
| 180 | return 0; |
---|
| 181 | } |
---|
| 182 | |
---|
[4680] | 183 | int plugins_reg_proto(struct roard_proto * proto) { |
---|
[5275] | 184 | int i; |
---|
| 185 | |
---|
[4680] | 186 | if ( _pp == NULL ) |
---|
| 187 | return -1; |
---|
| 188 | |
---|
[5275] | 189 | for (i = 0; i < MAX_PROTOS; i++) { |
---|
| 190 | if ( _pp->protocols[i] == -1 ) { |
---|
| 191 | _pp->protocols[i] = proto->proto; |
---|
| 192 | break; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | if ( i == MAX_PROTOS ) { |
---|
| 197 | roar_err_set(ROAR_ERROR_NOMEM); |
---|
| 198 | return -1; |
---|
| 199 | } |
---|
| 200 | |
---|
[5312] | 201 | return clients_register_proto(proto, _pp->lhandle); |
---|
[4680] | 202 | } |
---|
| 203 | |
---|
[3353] | 204 | //ll |
---|