2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
27 #ifdef HAVE_API_WIN32_BASE
43 typedef void * GModule;
44 #define G_MODULE_BIND_LOCAL 1
45 #define G_MODULE_BIND_LAZY 2
47 g_module_supported(void)
52 #ifdef HAVE_API_WIN32_BASE
54 static DWORD last_error;
55 static char errormsg[64];
58 g_module_open(char *name, int flags)
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) ;
65 handle = LoadLibraryW (filename);
67 last_error=GetLastError();
74 sprintf(errormsg,"dll error %d",(int)last_error);
79 g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
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);
87 *addr=GetProcAddress ((HANDLE)handle, symbol);
91 last_error=GetLastError();
96 g_module_close(GModule *handle)
98 FreeLibrary((HANDLE)handle);
103 g_module_open(char *name, int flags)
106 (flags & G_MODULE_BIND_LAZY ? RTLD_LAZY : RTLD_NOW) |
107 (flags & G_MODULE_BIND_LOCAL ? RTLD_LOCAL : RTLD_GLOBAL));
117 g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
119 *addr=dlsym(handle, symbol);
120 return (*addr != NULL);
124 g_module_close(GModule *handle)
148 static struct plugin *
149 plugin_new_from_path(char *plugin)
153 if (! g_module_supported()) {
156 ret=g_new0(struct plugin, 1);
157 ret->name=g_strdup(plugin);
165 plugin_load(struct plugin *pl)
173 dbg(0,"can't load '%s', already loaded\n", pl->name);
176 mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
178 dbg(0,"can't load '%s', Error '%s'\n", pl->name, g_module_error());
181 if (!g_module_symbol(mod, "plugin_init", &init)) {
182 dbg(0,"can't load '%s', plugin_init not found\n", pl->name);
186 dbg(1, "loaded module %s\n", pl->name);
197 plugin_get_name(struct plugin *pl)
203 plugin_get_active(struct plugin *pl)
209 plugin_set_active(struct plugin *pl, int active)
215 plugin_set_lazy(struct plugin *pl, int lazy)
222 plugin_get_ondemand(struct plugin *pl)
229 plugin_set_ondemand(struct plugin *pl, int ondemand)
231 pl->ondemand=ondemand;
235 plugin_call_init(struct plugin *pl)
241 plugin_unload(struct plugin *pl)
244 g_module_close(pl->mod);
250 plugin_destroy(struct plugin *pl)
258 struct plugins *ret=g_new0(struct plugins, 1);
259 ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
265 plugin_new(struct attr *parent, struct attr **attrs) {
267 struct attr *path_attr, *attr;
268 struct file_wordexp *we;
269 int active=1; // default active
270 int lazy=0, ondemand=0;
274 struct plugin *pl=NULL;
275 struct plugins *pls=NULL;
278 pls=parent->u.plugins;
280 if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
281 dbg(0,"missing path\n");
284 if ( (attr=attr_search(attrs, NULL, attr_active))) {
287 if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
290 if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
291 ondemand=attr->u.num;
293 dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
295 we=file_wordexp_new(path_attr->u.str);
296 count=file_wordexp_get_count(we);
297 array=file_wordexp_get_array(we);
298 dbg(2,"expanded to %d words\n",count);
299 if (count != 1 || file_exists(array[0])) {
300 for (i = 0 ; i < count ; i++) {
302 dbg(2,"found plugin module file [%d]: '%s'\n", i, name);
303 if (! (pls && (pl=g_hash_table_lookup(pls->hash, name)))) {
304 pl=plugin_new_from_path(name);
306 dbg(0,"failed to create plugin from file '%s'\n", name);
310 g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
311 pls->list=g_list_append(pls->list, pl);
315 pls->list=g_list_remove(pls->list, pl);
316 pls->list=g_list_append(pls->list, pl);
319 plugin_set_active(pl, active);
320 plugin_set_lazy(pl, lazy);
321 plugin_set_ondemand(pl, ondemand);
322 if (!pls && active) {
323 if (!plugin_load(pl))
324 plugin_set_active(pl, 0);
326 plugin_call_init(pl);
330 file_wordexp_destroy(we);
338 plugins_init(struct plugins *pls)
348 if (! plugin_get_ondemand(pl)) {
349 if (plugin_get_active(pl))
350 if (!plugin_load(pl))
351 plugin_set_active(pl, 0);
352 if (plugin_get_active(pl))
353 plugin_call_init(pl);
358 dbg(0, "Warning: No plugins found. Is Navit installed correctly?\n");
364 plugins_destroy(struct plugins *pls)
375 g_list_free(pls->list);
376 g_hash_table_destroy(pls->hash);
381 plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
386 char *mod_name, *filename=NULL, *corename=NULL;
388 dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
390 l=plugin_types[type];
393 if (!g_ascii_strcasecmp(nv->name, name))
400 filename=g_strjoin("", "lib", type_name, "_", name, NULL);
401 corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
404 if ((mod_name=g_strrstr(pl->name, "/")))
408 dbg(2,"compare '%s' with '%s'\n", mod_name, filename);
409 if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(corename))) {
410 dbg(1, "Loading module \"%s\"\n",pl->name) ;
411 if (plugin_get_active(pl))
412 if (!plugin_load(pl))
413 plugin_set_active(pl, 0);
414 if (plugin_get_active(pl))
415 plugin_call_init(pl);
416 l=plugin_types[type];
419 if (!g_ascii_strcasecmp(nv->name, name)) {
427 lpls=g_list_next(lpls);