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