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.
39 typedef void * GModule;
40 #define G_MODULE_BIND_LOCAL 1
41 #define G_MODULE_BIND_LAZY 2
43 g_module_supported(void)
49 g_module_open(char *name, int flags)
52 (flags & G_MODULE_BIND_LAZY ? RTLD_LAZY : RTLD_NOW) |
53 (flags & G_MODULE_BIND_LOCAL ? RTLD_LOCAL : RTLD_GLOBAL));
63 g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
65 *addr=dlsym(handle, symbol);
66 return (*addr != NULL);
70 g_module_close(GModule *handle)
94 static struct plugin *
95 plugin_new_from_path(char *plugin)
99 if (! g_module_supported()) {
102 ret=g_new0(struct plugin, 1);
103 ret->name=g_strdup(plugin);
111 plugin_load(struct plugin *pl)
119 dbg(0,"can't load '%s', already loaded\n", pl->name);
122 mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
124 dbg(0,"can't load '%s', Error '%s'\n", pl->name, g_module_error());
127 if (!g_module_symbol(mod, "plugin_init", &init)) {
128 dbg(0,"can't load '%s', plugin_init not found\n", pl->name);
142 plugin_get_name(struct plugin *pl)
148 plugin_get_active(struct plugin *pl)
154 plugin_set_active(struct plugin *pl, int active)
160 plugin_set_lazy(struct plugin *pl, int lazy)
167 plugin_get_ondemand(struct plugin *pl)
174 plugin_set_ondemand(struct plugin *pl, int ondemand)
176 pl->ondemand=ondemand;
180 plugin_call_init(struct plugin *pl)
186 plugin_unload(struct plugin *pl)
189 g_module_close(pl->mod);
195 plugin_destroy(struct plugin *pl)
203 struct plugins *ret=g_new0(struct plugins, 1);
204 ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
210 plugin_new(struct attr *parent, struct attr **attrs) {
212 struct attr *path_attr, *attr;
213 struct file_wordexp *we;
214 int active=1; // default active
215 int lazy=0, ondemand=0;
222 pls=parent->u.plugins;
224 if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
225 dbg(0,"missing path\n");
228 if ( (attr=attr_search(attrs, NULL, attr_active))) {
231 if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
234 if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
235 ondemand=attr->u.num;
237 dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
239 we=file_wordexp_new(path_attr->u.str);
240 count=file_wordexp_get_count(we);
241 array=file_wordexp_get_array(we);
242 for (i = 0 ; i < count ; i++) {
244 if (! (pl=g_hash_table_lookup(pls->hash, name))) {
245 pl=plugin_new_from_path(name);
247 dbg(0,"failed to create plugin '%s'\n", name);
250 g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
251 pls->list=g_list_append(pls->list, pl);
253 pls->list=g_list_remove(pls->list, pl);
254 pls->list=g_list_append(pls->list, pl);
256 plugin_set_active(pl, active);
257 plugin_set_lazy(pl, lazy);
258 plugin_set_ondemand(pl, ondemand);
260 file_wordexp_destroy(we);
266 plugins_init(struct plugins *pls)
275 if (! plugin_get_ondemand(pl)) {
276 if (plugin_get_active(pl))
277 if (!plugin_load(pl))
278 plugin_set_active(pl, 0);
279 if (plugin_get_active(pl))
280 plugin_call_init(pl);
288 plugins_destroy(struct plugins *pls)
299 g_list_free(pls->list);
300 g_hash_table_destroy(pls->hash);
305 plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
307 dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
311 char *mod_name, *filename=NULL, *corename=NULL;
312 l=plugin_types[type];
315 if (!g_ascii_strcasecmp(nv->name, name))
322 if(!g_ascii_strcasecmp(type_name, "map"))
324 filename=g_strjoin("", "lib", type_name, "_", name, NULL);
325 corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
328 if ((mod_name=g_strrstr(pl->name, "/")))
332 if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(filename))) {
333 dbg(1, "Loading module \"%s\"\n",pl->name) ;
334 if (plugin_get_active(pl))
335 if (!plugin_load(pl))
336 plugin_set_active(pl, 0);
337 if (plugin_get_active(pl))
338 plugin_call_init(pl);
339 l=plugin_types[type];
342 if (!g_ascii_strcasecmp(nv->name, name)) {
350 lpls=g_list_next(lpls);