summaryrefslogtreecommitdiff
path: root/navit/plugin.c
blob: 84d1aa1df78fb01c591ce8d341da04d6bbad1948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <string.h>
#include <glib.h>
#include "config.h"
#ifdef USE_PLUGINS
#ifdef HAVE_GMODULE
#include <gmodule.h>
#else
#include <dlfcn.h>
#endif
#endif
#include "plugin.h"
#include "file.h"
#define PLUGIN_C
#include "plugin.h"
#include "item.h"
#include "debug.h"

#ifdef USE_PLUGINS
#ifndef HAVE_GMODULE
typedef void * GModule;
#define G_MODULE_BIND_LOCAL 1
#define G_MODULE_BIND_LAZY 2
static int
g_module_supported(void)
{
	return 1;
}

static void *
g_module_open(char *name, int flags)
{
	return dlopen(name,
		(flags & G_MODULE_BIND_LAZY ? RTLD_LAZY : RTLD_NOW) |
		(flags & G_MODULE_BIND_LOCAL ? RTLD_LOCAL : RTLD_GLOBAL));
}

static char *
g_module_error(void)
{
	return dlerror();
}

static int
g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
{
	*addr=dlsym(handle, symbol);
	return (*addr != NULL);
}

static void
g_module_close(GModule *handle)
{
	dlclose(handle);
}

#endif
#endif

struct plugin {
	int active;
	int lazy;
	int ondemand;
	char *name;
#ifdef USE_PLUGINS
	GModule *mod;
#endif
	void (*init)(void);
};

struct plugins {
	GHashTable *hash;
	GList *list;
} *pls;

static struct plugin *
plugin_new_from_path(char *plugin)
{
#ifdef USE_PLUGINS
	struct plugin *ret;
	if (! g_module_supported()) {
	       return NULL;
	}
	ret=g_new0(struct plugin, 1);
	ret->name=g_strdup(plugin);
	return ret;
#else
	return NULL;
#endif
}

int
plugin_load(struct plugin *pl)
{
#ifdef USE_PLUGINS
	gpointer init;

	GModule *mod;

	if (pl->mod) {
		dbg(0,"can't load '%s', already loaded\n", pl->name);
		return 0;
	}
	mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
	if (! mod) {
		dbg(0,"can't load '%s', Error '%s'\n", pl->name, g_module_error());
		return 0;
	}
	if (!g_module_symbol(mod, "plugin_init", &init)) {
		dbg(0,"can't load '%s', plugin_init not found\n", pl->name);
		g_module_close(mod);
		return 0;
	} else {
		pl->mod=mod;
		pl->init=init;
	}
	return 1;
#else
	return 0;
#endif
}

char *
plugin_get_name(struct plugin *pl)
{
	return pl->name;
}

int
plugin_get_active(struct plugin *pl)
{
	return pl->active;
}

void
plugin_set_active(struct plugin *pl, int active)
{
	pl->active=active;
}

void
plugin_set_lazy(struct plugin *pl, int lazy)
{
	pl->lazy=lazy;
}

#ifdef USE_PLUGINS
static int
plugin_get_ondemand(struct plugin *pl)
{
	return pl->ondemand;
}
#endif

static void
plugin_set_ondemand(struct plugin *pl, int ondemand)
{
	pl->ondemand=ondemand;
}

void
plugin_call_init(struct plugin *pl)
{
	pl->init();
}

void
plugin_unload(struct plugin *pl)
{
#ifdef USE_PLUGINS
	g_module_close(pl->mod);
	pl->mod=NULL;
#endif
}

void
plugin_destroy(struct plugin *pl)
{
	g_free(pl);
}

struct plugins *
plugins_new(void)
{
	struct plugins *ret=g_new0(struct plugins, 1);
	ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
	pls=ret;
	return ret;
}

struct plugin *
plugin_new(struct attr *parent, struct attr **attrs) {
#ifdef USE_PLUGINS
	struct attr *path_attr, *attr;
	struct file_wordexp *we;
	int active=1; // default active
	int lazy=0, ondemand=0;
	int i, count;
	char **array;
	char *name;
	struct plugin *pl;
	struct plugins *pls;

	pls=parent->u.plugins;

	if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
		dbg(0,"missing path\n");
		return NULL;
	}
	if ( (attr=attr_search(attrs, NULL, attr_active))) {
		active=attr->u.num;
	}
	if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
		lazy=attr->u.num;
	}
	if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
		ondemand=attr->u.num;
	}
	dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);

	we=file_wordexp_new(path_attr->u.str);
	count=file_wordexp_get_count(we);
	array=file_wordexp_get_array(we);	
	for (i = 0 ; i < count ; i++) {
		name=array[i];
		if (! (pl=g_hash_table_lookup(pls->hash, name))) {
			pl=plugin_new_from_path(name);
			if (! pl) {
				dbg(0,"failed to create plugin '%s'\n", name);
				continue;
			}
			g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
			pls->list=g_list_append(pls->list, pl);
		} else {
			pls->list=g_list_remove(pls->list, pl);
			pls->list=g_list_append(pls->list, pl);
		}
		plugin_set_active(pl, active);
		plugin_set_lazy(pl, lazy);
		plugin_set_ondemand(pl, ondemand);
	}
	file_wordexp_destroy(we);
	return pl;
#endif
}

void
plugins_init(struct plugins *pls)
{
#ifdef USE_PLUGINS
	struct plugin *pl;
	GList *l;

	l=pls->list;
	while (l) {
		pl=l->data;
		if (! plugin_get_ondemand(pl)) {
			if (plugin_get_active(pl)) 
				if (!plugin_load(pl)) 
					plugin_set_active(pl, 0);
			if (plugin_get_active(pl)) 
				plugin_call_init(pl);
		}
		l=g_list_next(l);
	}
#endif
}

void
plugins_destroy(struct plugins *pls)
{
	GList *l;
	struct plugin *pl;

	l=pls->list;
	while (l) {
		pl=l->data;
		plugin_unload(pl);
		plugin_destroy(pl);
	}
	g_list_free(pls->list);
	g_hash_table_destroy(pls->hash);
	g_free(pls);
}

	void *
plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
{
	dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
	GList *l,*lpls;
	struct name_val *nv;
	struct plugin *pl;
	char *mod_name, *filename=NULL, *corename=NULL;
	l=plugin_types[type];
	while (l) {
		nv=l->data;
		if (!g_ascii_strcasecmp(nv->name, name))
			return nv->val;
		l=g_list_next(l);
	}
	if (!pls)
		return NULL;
	lpls=pls->list;
	if(!g_ascii_strcasecmp(type_name, "map"))
		type_name="data";
	filename=g_strjoin("", "lib", type_name, "_", name, NULL);
	corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
	while (lpls) {
		pl=lpls->data;
		if ((mod_name=g_strrstr(pl->name, "/")))
			mod_name++;
		else
			mod_name=pl->name;
		if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(corename))) {
			dbg(1, "Loading module \"%s\"\n",pl->name) ;
			if (plugin_get_active(pl)) 
				if (!plugin_load(pl)) 
					plugin_set_active(pl, 0);
			if (plugin_get_active(pl)) 
				plugin_call_init(pl);
			l=plugin_types[type];
			while (l) {
				nv=l->data;
				if (!g_ascii_strcasecmp(nv->name, name)) {
					g_free(filename);
					g_free(corename);
					return nv->val;
				}
				l=g_list_next(l);
			}
		}
		lpls=g_list_next(lpls);
	}
	g_free(filename);
	g_free(corename);
	return NULL;
}