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 *addr=GetProcAddress ((HANDLE)handle, symbol);
84 last_error=GetLastError();
89 g_module_close(GModule *handle)
91 FreeLibrary((HANDLE)handle);
96 g_module_open(char *name, int flags)
99 (flags & G_MODULE_BIND_LAZY ? RTLD_LAZY : RTLD_NOW) |
100 (flags & G_MODULE_BIND_LOCAL ? RTLD_LOCAL : RTLD_GLOBAL));
110 g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
112 *addr=dlsym(handle, symbol);
113 return (*addr != NULL);
117 g_module_close(GModule *handle)
141 static struct plugin *
142 plugin_new_from_path(char *plugin)
146 if (! g_module_supported()) {
149 ret=g_new0(struct plugin, 1);
150 ret->name=g_strdup(plugin);
158 plugin_load(struct plugin *pl)
166 dbg(0,"can't load '%s', already loaded\n", pl->name);
169 mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
171 dbg(0,"can't load '%s', Error '%s'\n", pl->name, g_module_error());
174 if (!g_module_symbol(mod, "plugin_init", &init)) {
175 dbg(0,"can't load '%s', plugin_init not found\n", pl->name);
189 plugin_get_name(struct plugin *pl)
195 plugin_get_active(struct plugin *pl)
201 plugin_set_active(struct plugin *pl, int active)
207 plugin_set_lazy(struct plugin *pl, int lazy)
214 plugin_get_ondemand(struct plugin *pl)
221 plugin_set_ondemand(struct plugin *pl, int ondemand)
223 pl->ondemand=ondemand;
227 plugin_call_init(struct plugin *pl)
233 plugin_unload(struct plugin *pl)
236 g_module_close(pl->mod);
242 plugin_destroy(struct plugin *pl)
250 struct plugins *ret=g_new0(struct plugins, 1);
251 ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
257 plugin_new(struct attr *parent, struct attr **attrs) {
259 struct attr *path_attr, *attr;
260 struct file_wordexp *we;
261 int active=1; // default active
262 int lazy=0, ondemand=0;
266 struct plugin *pl=NULL;
267 struct plugins *pls=NULL;
270 pls=parent->u.plugins;
272 if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
273 dbg(0,"missing path\n");
276 if ( (attr=attr_search(attrs, NULL, attr_active))) {
279 if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
282 if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
283 ondemand=attr->u.num;
285 dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
287 we=file_wordexp_new(path_attr->u.str);
288 count=file_wordexp_get_count(we);
289 array=file_wordexp_get_array(we);
290 dbg(2,"expanded to %d words\n",count);
291 if (count != 1 || file_exists(array[0])) {
292 for (i = 0 ; i < count ; i++) {
294 dbg(2,"name[%d]='%s'\n", i, name);
295 if (! (pls && (pl=g_hash_table_lookup(pls->hash, name)))) {
296 pl=plugin_new_from_path(name);
298 dbg(0,"failed to create plugin '%s'\n", name);
302 g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
303 pls->list=g_list_append(pls->list, pl);
307 pls->list=g_list_remove(pls->list, pl);
308 pls->list=g_list_append(pls->list, pl);
311 plugin_set_active(pl, active);
312 plugin_set_lazy(pl, lazy);
313 plugin_set_ondemand(pl, ondemand);
314 if (!pls && active) {
315 if (!plugin_load(pl))
316 plugin_set_active(pl, 0);
318 plugin_call_init(pl);
321 file_wordexp_destroy(we);
328 plugins_init(struct plugins *pls)
337 if (! plugin_get_ondemand(pl)) {
338 if (plugin_get_active(pl))
339 if (!plugin_load(pl))
340 plugin_set_active(pl, 0);
341 if (plugin_get_active(pl))
342 plugin_call_init(pl);
350 plugins_destroy(struct plugins *pls)
361 g_list_free(pls->list);
362 g_hash_table_destroy(pls->hash);
367 plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
369 dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
373 char *mod_name, *filename=NULL, *corename=NULL;
374 l=plugin_types[type];
377 if (!g_ascii_strcasecmp(nv->name, name))
384 filename=g_strjoin("", "lib", type_name, "_", name, NULL);
385 corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
388 if ((mod_name=g_strrstr(pl->name, "/")))
392 dbg(2,"compare '%s' with '%s'\n", mod_name, filename);
393 if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(corename))) {
394 dbg(1, "Loading module \"%s\"\n",pl->name) ;
395 if (plugin_get_active(pl))
396 if (!plugin_load(pl))
397 plugin_set_active(pl, 0);
398 if (plugin_get_active(pl))
399 plugin_call_init(pl);
400 l=plugin_types[type];
403 if (!g_ascii_strcasecmp(nv->name, name)) {
411 lpls=g_list_next(lpls);