1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
#include <string.h>
|
21 |
#include <glib.h>
|
22 |
#include "config.h"
|
23 |
#ifdef USE_PLUGINS
|
24 |
#ifdef HAVE_GMODULE
|
25 |
#include <gmodule.h>
|
26 |
#else
|
27 |
#ifdef HAVE_API_WIN32_BASE
|
28 |
#include <windows.h>
|
29 |
#else
|
30 |
#include <dlfcn.h>
|
31 |
#endif
|
32 |
#endif
|
33 |
#endif
|
34 |
#include "plugin.h"
|
35 |
#include "file.h"
|
36 |
#define PLUGIN_C
|
37 |
#include "plugin.h"
|
38 |
#include "item.h"
|
39 |
#include "debug.h"
|
40 |
|
41 |
#ifdef USE_PLUGINS
|
42 |
#ifndef HAVE_GMODULE
|
43 |
typedef void * GModule;
|
44 |
#define G_MODULE_BIND_LOCAL 1
|
45 |
#define G_MODULE_BIND_LAZY 2
|
46 |
static int
|
47 |
g_module_supported(void)
|
48 |
{
|
49 |
return 1;
|
50 |
}
|
51 |
|
52 |
#ifdef HAVE_API_WIN32_BASE
|
53 |
|
54 |
static DWORD last_error;
|
55 |
static char errormsg[64];
|
56 |
|
57 |
static void *
|
58 |
g_module_open(char *name, int flags)
|
59 |
{
|
60 |
HINSTANCE handle;
|
61 |
int len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, 0, 0);
|
62 |
wchar_t filename[len];
|
63 |
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, filename, len) ;
|
64 |
|
65 |
handle = LoadLibraryW (filename);
|
66 |
if (!handle)
|
67 |
last_error=GetLastError();
|
68 |
return handle;
|
69 |
}
|
70 |
|
71 |
static char *
|
72 |
g_module_error(void)
|
73 |
{
|
74 |
sprintf(errormsg,"dll error %d",(int)last_error);
|
75 |
return errormsg;
|
76 |
}
|
77 |
|
78 |
static int
|
79 |
g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
|
80 |
{
|
81 |
#ifdef HAVE_API_WIN32_CE
|
82 |
int len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, symbol, -1, 0, 0);
|
83 |
wchar_t wsymbol[len+1];
|
84 |
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, symbol, -1, wsymbol, len) ;
|
85 |
*addr=GetProcAddress ((HANDLE)handle, wsymbol);
|
86 |
#else
|
87 |
*addr=GetProcAddress ((HANDLE)handle, symbol);
|
88 |
#endif
|
89 |
if (*addr)
|
90 |
return 1;
|
91 |
last_error=GetLastError();
|
92 |
return 0;
|
93 |
}
|
94 |
|
95 |
static void
|
96 |
g_module_close(GModule *handle)
|
97 |
{
|
98 |
FreeLibrary((HANDLE)handle);
|
99 |
}
|
100 |
|
101 |
#else
|
102 |
static void *
|
103 |
g_module_open(char *name, int flags)
|
104 |
{
|
105 |
return dlopen(name,
|
106 |
(flags & G_MODULE_BIND_LAZY ? RTLD_LAZY : RTLD_NOW) |
|
107 |
(flags & G_MODULE_BIND_LOCAL ? RTLD_LOCAL : RTLD_GLOBAL));
|
108 |
}
|
109 |
|
110 |
static char *
|
111 |
g_module_error(void)
|
112 |
{
|
113 |
return dlerror();
|
114 |
}
|
115 |
|
116 |
static int
|
117 |
g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
|
118 |
{
|
119 |
*addr=dlsym(handle, symbol);
|
120 |
return (*addr != NULL);
|
121 |
}
|
122 |
|
123 |
static void
|
124 |
g_module_close(GModule *handle)
|
125 |
{
|
126 |
dlclose(handle);
|
127 |
}
|
128 |
#endif
|
129 |
#endif
|
130 |
#endif
|
131 |
|
132 |
struct plugin {
|
133 |
int active;
|
134 |
int lazy;
|
135 |
int ondemand;
|
136 |
char *name;
|
137 |
#ifdef USE_PLUGINS
|
138 |
GModule *mod;
|
139 |
#endif
|
140 |
void (*init)(void);
|
141 |
};
|
142 |
|
143 |
struct plugins {
|
144 |
GHashTable *hash;
|
145 |
GList *list;
|
146 |
} *pls;
|
147 |
|
148 |
static struct plugin *
|
149 |
plugin_new_from_path(char *plugin)
|
150 |
{
|
151 |
#ifdef USE_PLUGINS
|
152 |
struct plugin *ret;
|
153 |
if (! g_module_supported()) {
|
154 |
return NULL;
|
155 |
}
|
156 |
ret=g_new0(struct plugin, 1);
|
157 |
ret->name=g_strdup(plugin);
|
158 |
return ret;
|
159 |
#else
|
160 |
return NULL;
|
161 |
#endif
|
162 |
}
|
163 |
|
164 |
int
|
165 |
plugin_load(struct plugin *pl)
|
166 |
{
|
167 |
#ifdef USE_PLUGINS
|
168 |
gpointer init;
|
169 |
|
170 |
GModule *mod;
|
171 |
|
172 |
if (pl->mod) {
|
173 |
dbg(0,"can't load '%s', already loaded\n", pl->name);
|
174 |
return 0;
|
175 |
}
|
176 |
mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
|
177 |
if (! mod) {
|
178 |
dbg(0,"can't load '%s', Error '%s'\n", pl->name, g_module_error());
|
179 |
return 0;
|
180 |
}
|
181 |
if (!g_module_symbol(mod, "plugin_init", &init)) {
|
182 |
dbg(0,"can't load '%s', plugin_init not found\n", pl->name);
|
183 |
g_module_close(mod);
|
184 |
return 0;
|
185 |
} else {
|
186 |
pl->mod=mod;
|
187 |
pl->init=init;
|
188 |
}
|
189 |
return 1;
|
190 |
#else
|
191 |
return 0;
|
192 |
#endif
|
193 |
}
|
194 |
|
195 |
char *
|
196 |
plugin_get_name(struct plugin *pl)
|
197 |
{
|
198 |
return pl->name;
|
199 |
}
|
200 |
|
201 |
int
|
202 |
plugin_get_active(struct plugin *pl)
|
203 |
{
|
204 |
return pl->active;
|
205 |
}
|
206 |
|
207 |
void
|
208 |
plugin_set_active(struct plugin *pl, int active)
|
209 |
{
|
210 |
pl->active=active;
|
211 |
}
|
212 |
|
213 |
void
|
214 |
plugin_set_lazy(struct plugin *pl, int lazy)
|
215 |
{
|
216 |
pl->lazy=lazy;
|
217 |
}
|
218 |
|
219 |
#ifdef USE_PLUGINS
|
220 |
static int
|
221 |
plugin_get_ondemand(struct plugin *pl)
|
222 |
{
|
223 |
return pl->ondemand;
|
224 |
}
|
225 |
#endif
|
226 |
|
227 |
static void
|
228 |
plugin_set_ondemand(struct plugin *pl, int ondemand)
|
229 |
{
|
230 |
pl->ondemand=ondemand;
|
231 |
}
|
232 |
|
233 |
void
|
234 |
plugin_call_init(struct plugin *pl)
|
235 |
{
|
236 |
pl->init();
|
237 |
}
|
238 |
|
239 |
void
|
240 |
plugin_unload(struct plugin *pl)
|
241 |
{
|
242 |
#ifdef USE_PLUGINS
|
243 |
g_module_close(pl->mod);
|
244 |
pl->mod=NULL;
|
245 |
#endif
|
246 |
}
|
247 |
|
248 |
void
|
249 |
plugin_destroy(struct plugin *pl)
|
250 |
{
|
251 |
g_free(pl);
|
252 |
}
|
253 |
|
254 |
struct plugins *
|
255 |
plugins_new(void)
|
256 |
{
|
257 |
struct plugins *ret=g_new0(struct plugins, 1);
|
258 |
ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
|
259 |
pls=ret;
|
260 |
return ret;
|
261 |
}
|
262 |
|
263 |
struct plugin *
|
264 |
plugin_new(struct attr *parent, struct attr **attrs) {
|
265 |
#ifdef USE_PLUGINS
|
266 |
struct attr *path_attr, *attr;
|
267 |
struct file_wordexp *we;
|
268 |
int active=1; // default active
|
269 |
int lazy=0, ondemand=0;
|
270 |
int i, count;
|
271 |
char **array;
|
272 |
char *name;
|
273 |
struct plugin *pl=NULL;
|
274 |
struct plugins *pls=NULL;
|
275 |
|
276 |
if (parent)
|
277 |
pls=parent->u.plugins;
|
278 |
|
279 |
if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
|
280 |
dbg(0,"missing path\n");
|
281 |
return NULL;
|
282 |
}
|
283 |
if ( (attr=attr_search(attrs, NULL, attr_active))) {
|
284 |
active=attr->u.num;
|
285 |
}
|
286 |
if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
|
287 |
lazy=attr->u.num;
|
288 |
}
|
289 |
if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
|
290 |
ondemand=attr->u.num;
|
291 |
}
|
292 |
dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
|
293 |
|
294 |
we=file_wordexp_new(path_attr->u.str);
|
295 |
count=file_wordexp_get_count(we);
|
296 |
array=file_wordexp_get_array(we);
|
297 |
dbg(2,"expanded to %d words\n",count);
|
298 |
if (count != 1 || file_exists(array[0])) {
|
299 |
for (i = 0 ; i < count ; i++) {
|
300 |
name=array[i];
|
301 |
dbg(2,"name[%d]='%s'\n", i, name);
|
302 |
if (! (pls && (pl=g_hash_table_lookup(pls->hash, name)))) {
|
303 |
pl=plugin_new_from_path(name);
|
304 |
if (! pl) {
|
305 |
dbg(0,"failed to create plugin '%s'\n", name);
|
306 |
continue;
|
307 |
}
|
308 |
if (pls) {
|
309 |
g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
|
310 |
pls->list=g_list_append(pls->list, pl);
|
311 |
}
|
312 |
} else {
|
313 |
if (pls) {
|
314 |
pls->list=g_list_remove(pls->list, pl);
|
315 |
pls->list=g_list_append(pls->list, pl);
|
316 |
}
|
317 |
}
|
318 |
plugin_set_active(pl, active);
|
319 |
plugin_set_lazy(pl, lazy);
|
320 |
plugin_set_ondemand(pl, ondemand);
|
321 |
if (!pls && active) {
|
322 |
if (!plugin_load(pl))
|
323 |
plugin_set_active(pl, 0);
|
324 |
else
|
325 |
plugin_call_init(pl);
|
326 |
}
|
327 |
}
|
328 |
}
|
329 |
file_wordexp_destroy(we);
|
330 |
return pl;
|
331 |
#else
|
332 |
return 0;
|
333 |
#endif
|
334 |
}
|
335 |
|
336 |
void
|
337 |
plugins_init(struct plugins *pls)
|
338 |
{
|
339 |
#ifdef USE_PLUGINS
|
340 |
struct plugin *pl;
|
341 |
GList *l;
|
342 |
|
343 |
l=pls->list;
|
344 |
while (l) {
|
345 |
pl=l->data;
|
346 |
if (! plugin_get_ondemand(pl)) {
|
347 |
if (plugin_get_active(pl))
|
348 |
if (!plugin_load(pl))
|
349 |
plugin_set_active(pl, 0);
|
350 |
if (plugin_get_active(pl))
|
351 |
plugin_call_init(pl);
|
352 |
}
|
353 |
l=g_list_next(l);
|
354 |
}
|
355 |
#endif
|
356 |
}
|
357 |
|
358 |
void
|
359 |
plugins_destroy(struct plugins *pls)
|
360 |
{
|
361 |
GList *l;
|
362 |
struct plugin *pl;
|
363 |
|
364 |
l=pls->list;
|
365 |
while (l) {
|
366 |
pl=l->data;
|
367 |
plugin_unload(pl);
|
368 |
plugin_destroy(pl);
|
369 |
}
|
370 |
g_list_free(pls->list);
|
371 |
g_hash_table_destroy(pls->hash);
|
372 |
g_free(pls);
|
373 |
}
|
374 |
|
375 |
void *
|
376 |
plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
|
377 |
{
|
378 |
GList *l,*lpls;
|
379 |
struct name_val *nv;
|
380 |
struct plugin *pl;
|
381 |
char *mod_name, *filename=NULL, *corename=NULL;
|
382 |
|
383 |
dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
|
384 |
|
385 |
l=plugin_types[type];
|
386 |
while (l) {
|
387 |
nv=l->data;
|
388 |
if (!g_ascii_strcasecmp(nv->name, name))
|
389 |
return nv->val;
|
390 |
l=g_list_next(l);
|
391 |
}
|
392 |
if (!pls)
|
393 |
return NULL;
|
394 |
lpls=pls->list;
|
395 |
filename=g_strjoin("", "lib", type_name, "_", name, NULL);
|
396 |
corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
|
397 |
while (lpls) {
|
398 |
pl=lpls->data;
|
399 |
if ((mod_name=g_strrstr(pl->name, "/")))
|
400 |
mod_name++;
|
401 |
else
|
402 |
mod_name=pl->name;
|
403 |
dbg(2,"compare '%s' with '%s'\n", mod_name, filename);
|
404 |
if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(corename))) {
|
405 |
dbg(1, "Loading module \"%s\"\n",pl->name) ;
|
406 |
if (plugin_get_active(pl))
|
407 |
if (!plugin_load(pl))
|
408 |
plugin_set_active(pl, 0);
|
409 |
if (plugin_get_active(pl))
|
410 |
plugin_call_init(pl);
|
411 |
l=plugin_types[type];
|
412 |
while (l) {
|
413 |
nv=l->data;
|
414 |
if (!g_ascii_strcasecmp(nv->name, name)) {
|
415 |
g_free(filename);
|
416 |
g_free(corename);
|
417 |
return nv->val;
|
418 |
}
|
419 |
l=g_list_next(l);
|
420 |
}
|
421 |
}
|
422 |
lpls=g_list_next(lpls);
|
423 |
}
|
424 |
g_free(filename);
|
425 |
g_free(corename);
|
426 |
return NULL;
|
427 |
}
|