Core:Fix:Cleanup of plugin_get_type
[navit-package] / navit / plugin.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21 #include <glib.h>
22 #include <gmodule.h>
23 #include "config.h"
24 #include "plugin.h"
25 #include "file.h"
26 #define PLUGIN_C
27 #include "plugin.h"
28 #include "item.h"
29 #include "debug.h"
30
31 struct plugin {
32         int active;
33         int lazy;
34         int ondemand;
35         char *name;
36         GModule *mod;
37         void (*init)(void);
38 };
39
40 struct plugins {
41         GHashTable *hash;
42         GList *list;
43 } *pls;
44
45 struct plugin *
46 plugin_new(char *plugin)
47 {
48         struct plugin *ret;
49         if (! g_module_supported()) {
50                 return NULL;
51         }
52         ret=g_new0(struct plugin, 1);
53         ret->name=g_strdup(plugin);
54         return ret;
55         
56 }
57
58 int
59 plugin_load(struct plugin *pl)
60 {
61         gpointer init;
62
63         GModule *mod;
64
65         if (pl->mod) {
66                 g_warning("can't load '%s', already loaded\n", pl->name);
67                 return 0;
68         }
69         mod=g_module_open(pl->name, G_MODULE_BIND_LOCAL | (pl->lazy ? G_MODULE_BIND_LAZY : 0));
70         if (! mod) {
71                 g_warning("can't load '%s', Error '%s'\n", pl->name, g_module_error());
72                 return 0;
73         }
74         if (!g_module_symbol(mod, "plugin_init", &init)) {
75                 g_warning("can't load '%s', plugin_init not found\n", pl->name);
76                 g_module_close(mod);
77                 return 0;
78         } else {
79                 pl->mod=mod;
80                 pl->init=init;
81         }
82         return 1;
83 }
84
85 char *
86 plugin_get_name(struct plugin *pl)
87 {
88         return pl->name;
89 }
90
91 int
92 plugin_get_active(struct plugin *pl)
93 {
94         return pl->active;
95 }
96
97 void
98 plugin_set_active(struct plugin *pl, int active)
99 {
100         pl->active=active;
101 }
102
103 void
104 plugin_set_lazy(struct plugin *pl, int lazy)
105 {
106         pl->lazy=lazy;
107 }
108
109 static int
110 plugin_get_ondemand(struct plugin *pl)
111 {
112         return pl->ondemand;
113 }
114
115 static void
116 plugin_set_ondemand(struct plugin *pl, int ondemand)
117 {
118         pl->ondemand=ondemand;
119 }
120
121 void
122 plugin_call_init(struct plugin *pl)
123 {
124         pl->init();
125 }
126
127 void
128 plugin_unload(struct plugin *pl)
129 {
130         g_module_close(pl->mod);
131         pl->mod=NULL;
132 }
133
134 void
135 plugin_destroy(struct plugin *pl)
136 {
137         g_free(pl);
138 }
139
140 struct plugins *
141 plugins_new(void)
142 {
143         struct plugins *ret=g_new0(struct plugins, 1);
144         ret->hash=g_hash_table_new(g_str_hash, g_str_equal);
145         pls=ret;
146         return ret;
147 }
148
149 void
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;
155         int i, count;
156         char **array;
157         char *name;
158         struct plugin *pl;
159
160         if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
161                 dbg(0,"missing path\n");
162                 return;
163         }
164         if ( (attr=attr_search(attrs, NULL, attr_active))) {
165                 active=attr->u.num;
166         }
167         if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
168                 lazy=attr->u.num;
169         }
170         if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
171                 ondemand=attr->u.num;
172         }
173         dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
174
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++) {
179                 name=array[i];
180                 if (! (pl=g_hash_table_lookup(pls->hash, name))) {
181                         pl=plugin_new(name);
182                         if (! pl) {
183                                 g_warning("failed to create plugin '%s'\n", name);
184                                 continue;
185                         }
186                         g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
187                         pls->list=g_list_append(pls->list, pl);
188                 } else {
189                         pls->list=g_list_remove(pls->list, pl);
190                         pls->list=g_list_append(pls->list, pl);
191                 }
192                 plugin_set_active(pl, active);
193                 plugin_set_lazy(pl, lazy);
194                 plugin_set_ondemand(pl, ondemand);
195         }
196         file_wordexp_destroy(we);
197 }
198
199 void
200 plugins_init(struct plugins *pls)
201 {
202 #ifdef USE_PLUGINS
203         struct plugin *pl;
204         GList *l;
205
206         l=pls->list;
207         while (l) {
208                 pl=l->data;
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);
215                 }
216                 l=g_list_next(l);
217         }
218 #endif
219 }
220
221 void
222 plugins_destroy(struct plugins *pls)
223 {
224         GList *l;
225         struct plugin *pl;
226
227         l=pls->list;
228         while (l) {
229                 pl=l->data;
230                 plugin_unload(pl);
231                 plugin_destroy(pl);
232         }
233         g_list_free(pls->list);
234         g_hash_table_destroy(pls->hash);
235         g_free(pls);
236 }
237
238         void *
239 plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
240 {
241         dbg(1, "type=\"%s\", name=\"%s\"\n", type_name, name);
242         GList *l,*lpls;
243         struct name_val *nv;
244         struct plugin *pl;
245         char *mod_name, *filename=NULL, *corename=NULL;
246         l=plugin_types[type];
247         while (l) {
248                 nv=l->data;
249                 if (!g_ascii_strcasecmp(nv->name, name))
250                         return nv->val;
251                 l=g_list_next(l);
252         }
253         lpls=pls->list;
254         if(!g_ascii_strcasecmp(type_name, "map"))
255                 type_name="data";
256         filename=g_strjoin("", "lib", type_name, "_", name, NULL);
257         corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
258         while (lpls) {
259                 pl=lpls->data;
260                 if ((mod_name=g_strrstr(pl->name, "/")))
261                         mod_name++;
262                 else
263                         mod_name=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];
272                         while (l) {
273                                 nv=l->data;
274                                 if (!g_ascii_strcasecmp(nv->name, name)) {
275                                         g_free(filename);
276                                         g_free(corename);
277                                         return nv->val;
278                                 }
279                                 l=g_list_next(l);
280                         }
281                 }
282                 lpls=g_list_next(lpls);
283         }
284         g_free(filename);
285         g_free(corename);
286         return NULL;
287 }