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;
219 struct plugin *pl=NULL;
220 struct plugins *pls=NULL;
223 pls=parent->u.plugins;
225 if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
226 dbg(0,"missing path\n");
229 if ( (attr=attr_search(attrs, NULL, attr_active))) {
232 if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
235 if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
236 ondemand=attr->u.num;
238 dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
240 we=file_wordexp_new(path_attr->u.str);
241 count=file_wordexp_get_count(we);
242 array=file_wordexp_get_array(we);
243 dbg(2,"expanded to %d words\n",count);
244 if (count != 1 || file_exists(array[0])) {
245 for (i = 0 ; i < count ; i++) {
247 dbg(2,"name[%d]='%s'\n", i, name);
248 if (! (pls && (pl=g_hash_table_lookup(pls->hash, name)))) {
249 pl=plugin_new_from_path(name);
251 dbg(0,"failed to create plugin '%s'\n", name);
255 g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
256 pls->list=g_list_append(pls->list, pl);
260 pls->list=g_list_remove(pls->list, pl);
261 pls->list=g_list_append(pls->list, pl);
264 plugin_set_active(pl, active);
265 plugin_set_lazy(pl, lazy);
266 plugin_set_ondemand(pl, ondemand);
267 if (!pls && active) {
268 if (!plugin_load(pl))
269 plugin_set_active(pl, 0);
271 plugin_call_init(pl);
274 file_wordexp_destroy(we);
281 plugins_init(struct plugins *pls)
290 if (! plugin_get_ondemand(pl)) {
291 if (plugin_get_active(pl))
292 if (!plugin_load(pl))
293 plugin_set_active(pl, 0);
294 if (plugin_get_active(pl))
295 plugin_call_init(pl);
303 plugins_destroy(struct plugins *pls)
314 g_list_free(pls->list);
315 g_hash_table_destroy(pls->hash);
320 plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
322 dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
326 char *mod_name, *filename=NULL, *corename=NULL;
327 l=plugin_types[type];
330 if (!g_ascii_strcasecmp(nv->name, name))
337 filename=g_strjoin("", "lib", type_name, "_", name, NULL);
338 corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
341 if ((mod_name=g_strrstr(pl->name, "/")))
345 dbg(2,"compare '%s' with '%s'\n", mod_name, filename);
346 if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(corename))) {
347 dbg(1, "Loading module \"%s\"\n",pl->name) ;
348 if (plugin_get_active(pl))
349 if (!plugin_load(pl))
350 plugin_set_active(pl, 0);
351 if (plugin_get_active(pl))
352 plugin_call_init(pl);
353 l=plugin_types[type];
356 if (!g_ascii_strcasecmp(nv->name, name)) {
364 lpls=g_list_next(lpls);