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.
46 plugin_new(char *plugin)
49 if (! g_module_supported()) {
52 ret=g_new0(struct plugin, 1);
53 ret->name=g_strdup(plugin);
59 plugin_load(struct plugin *pl)
66 g_warning("can't load '%s', already loaded\n", pl->name);
69 mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
71 g_warning("can't load '%s', Error '%s'\n", pl->name, g_module_error());
74 if (!g_module_symbol(mod, "plugin_init", &init)) {
75 g_warning("can't load '%s', plugin_init not found\n", pl->name);
86 plugin_get_name(struct plugin *pl)
92 plugin_get_active(struct plugin *pl)
98 plugin_set_active(struct plugin *pl, int active)
104 plugin_set_lazy(struct plugin *pl, int lazy)
110 plugin_get_ondemand(struct plugin *pl)
116 plugin_set_ondemand(struct plugin *pl, int ondemand)
118 pl->ondemand=ondemand;
122 plugin_call_init(struct plugin *pl)
128 plugin_unload(struct plugin *pl)
130 g_module_close(pl->mod);
135 plugin_destroy(struct plugin *pl)
143 struct plugins *ret=g_new0(struct plugins, 1);
144 ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
150 plugins_add_path(struct plugins *pls, struct attr **attrs) {
151 struct attr *path_attr, *attr;
152 struct file_wordexp *we;
153 int active=1; // default active
154 int lazy=0, ondemand=0;
160 if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
161 dbg(0,"missing path\n");
164 if ( (attr=attr_search(attrs, NULL, attr_active))) {
167 if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
170 if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
171 ondemand=attr->u.num;
173 dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
175 we=file_wordexp_new(path_attr->u.str);
176 count=file_wordexp_get_count(we);
177 array=file_wordexp_get_array(we);
178 for (i = 0 ; i < count ; i++) {
180 if (! (pl=g_hash_table_lookup(pls->hash, name))) {
183 g_warning("failed to create plugin '%s'\n", name);
186 g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
187 pls->list=g_list_append(pls->list, pl);
189 pls->list=g_list_remove(pls->list, pl);
190 pls->list=g_list_append(pls->list, pl);
192 plugin_set_active(pl, active);
193 plugin_set_lazy(pl, lazy);
194 plugin_set_ondemand(pl, ondemand);
196 file_wordexp_destroy(we);
200 plugins_init(struct plugins *pls)
209 if (! plugin_get_ondemand(pl)) {
210 if (plugin_get_active(pl))
211 if (!plugin_load(pl))
212 plugin_set_active(pl, 0);
213 if (plugin_get_active(pl))
214 plugin_call_init(pl);
222 plugins_destroy(struct plugins *pls)
233 g_list_free(pls->list);
234 g_hash_table_destroy(pls->hash);
239 plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
241 dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
245 char *mod_name, *filename=NULL, *corename=NULL;
246 l=plugin_types[type];
249 if (!g_ascii_strcasecmp(nv->name, name))
254 if(!g_ascii_strcasecmp(type_name, "map"))
256 filename=g_strjoin("", "lib", type_name, "_", name, NULL);
257 corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
260 if ((mod_name=g_strrstr(pl->name, "/")))
264 if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(filename))) {
265 dbg(0, "Loading module \"%s\"\n",pl->name) ;
266 if (plugin_get_active(pl))
267 if (!plugin_load(pl))
268 plugin_set_active(pl, 0);
269 if (plugin_get_active(pl))
270 plugin_call_init(pl);
271 l=plugin_types[type];
274 if (!g_ascii_strcasecmp(nv->name, name)) {
282 lpls=g_list_next(lpls);