Core:Fix:Use attr_list in plugins_add_path
[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 };
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         return ret;
146 }
147
148 void
149 plugins_add_path(struct plugins *pls, struct attr **attrs) {
150         struct attr *path_attr, *attr;
151         struct file_wordexp *we;
152         int active=1; // default active
153         int lazy=0, ondemand=0;
154         int i, count;
155         char **array;
156         char *name;
157         struct plugin *pl;
158
159         if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
160                 dbg(0,"missing path\n");
161                 return;
162         }
163         if ( (attr=attr_search(attrs, NULL, attr_active))) {
164                 active=attr->u.num;
165         }
166         if ( (attr=attr_search(attrs, NULL, attr_lazy))) {
167                 lazy=attr->u.num;
168         }
169         if ( (attr=attr_search(attrs, NULL, attr_ondemand))) {
170                 ondemand=attr->u.num;
171         }
172         dbg(1, "path=\"%s\", active=%d, lazy=%d, ondemand=%d\n",path_attr->u.str, active, lazy, ondemand);
173
174         we=file_wordexp_new(path_attr->u.str);
175         count=file_wordexp_get_count(we);
176         array=file_wordexp_get_array(we);       
177         for (i = 0 ; i < count ; i++) {
178                 name=array[i];
179                 if (! (pl=g_hash_table_lookup(pls->hash, name))) {
180                         pl=plugin_new(name);
181                         if (! pl) {
182                                 g_warning("failed to create plugin '%s'\n", name);
183                                 continue;
184                         }
185                         g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
186                         pls->list=g_list_append(pls->list, pl);
187                 } else {
188                         pls->list=g_list_remove(pls->list, pl);
189                         pls->list=g_list_append(pls->list, pl);
190                 }
191                 plugin_set_active(pl, active);
192                 plugin_set_lazy(pl, lazy);
193                 plugin_set_ondemand(pl, ondemand);
194         }
195         file_wordexp_destroy(we);
196 }
197
198 void
199 plugins_init(struct plugins *pls)
200 {
201 #ifdef USE_PLUGINS
202         struct plugin *pl;
203         GList *l;
204
205         l=pls->list;
206         while (l) {
207                 pl=l->data;
208                 if (! plugin_get_ondemand(pl)) {
209                         if (plugin_get_active(pl)) 
210                                 if (!plugin_load(pl)) 
211                                         plugin_set_active(pl, 0);
212                         if (plugin_get_active(pl)) 
213                                 plugin_call_init(pl);
214                 }
215                 l=g_list_next(l);
216         }
217 #endif
218 }
219
220 void
221 plugins_destroy(struct plugins *pls)
222 {
223         GList *l;
224         struct plugin *pl;
225
226         l=pls->list;
227         while (l) {
228                 pl=l->data;
229                 plugin_unload(pl);
230                 plugin_destroy(pl);
231         }
232         g_list_free(pls->list);
233         g_hash_table_destroy(pls->hash);
234         g_free(pls);
235 }
236
237 void *
238 plugin_get_type(enum plugin_type type, const char *name)
239 {
240         GList *l;
241         struct name_val *nv;
242         l=plugin_types[type];
243         while (l) {
244                 nv=l->data;
245                 if (!g_ascii_strcasecmp(nv->name, name))
246                         return nv->val;
247                 l=g_list_next(l);
248         }
249         return NULL;
250 }