]> Pileus Git - grits/blob - src/gis-plugin.c
b0619f9b3e2fe16db971bccf26d95af54448055f
[grits] / src / gis-plugin.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <glib.h>
19 #include <gmodule.h>
20
21 #include "gis-plugin.h"
22
23 /********************
24  * Plugin interface *
25  ********************/
26 static void gis_plugin_base_init(gpointer g_class)
27 {
28         static gboolean is_initialized = FALSE;
29         if (!is_initialized) {
30                 /* add properties and signals to the interface here */
31                 is_initialized = TRUE;
32         }
33 }
34
35 GType gis_plugin_get_type()
36 {
37         static GType type = 0;
38         if (type == 0) {
39                 static const GTypeInfo info = {
40                         sizeof(GisPluginInterface),
41                         gis_plugin_base_init,
42                         NULL,
43                 };
44                 type = g_type_register_static(G_TYPE_INTERFACE,
45                                 "GisPlugin", &info, 0);
46         }
47         return type;
48 }
49
50 void gis_plugin_expose(GisPlugin *self)
51 {
52         g_return_if_fail(GIS_IS_PLUGIN(self));
53         GIS_PLUGIN_GET_INTERFACE(self)->expose(self);
54 }
55
56 GtkWidget *gis_plugin_get_config(GisPlugin *self)
57 {
58         g_return_val_if_fail(GIS_IS_PLUGIN(self), NULL);
59         return GIS_PLUGIN_GET_INTERFACE(self)->get_config(self);
60 }
61
62
63 /***************
64  * Plugins API *
65  ***************/
66 typedef struct {
67         gchar *name;
68         GisPlugin *plugin;
69 } GisPluginStore;
70
71 GisPlugins *gis_plugins_new(gchar *dir)
72 {
73         g_debug("GisPlugins: new - dir=%s", dir);
74         GisPlugins *plugins = g_new0(GisPlugins, 1);
75         if (dir)
76                 plugins->dir = g_strdup(dir);
77         plugins->plugins = g_ptr_array_new();
78         return plugins;
79 }
80
81 void gis_plugins_free(GisPlugins *self)
82 {
83         g_debug("GisPlugins: free");
84         for (int i = 0; i < self->plugins->len; i++) {
85                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
86                 g_object_unref(store->plugin);
87                 g_free(store->name);
88                 g_free(store);
89                 g_ptr_array_remove_index(self->plugins, i);
90         }
91         g_ptr_array_free(self->plugins, TRUE);
92         if (self->dir)
93                 g_free(self->dir);
94         g_free(self);
95 }
96
97 GList *gis_plugins_available(GisPlugins *self)
98 {
99         g_debug("GisPlugins: available");
100         GList *list = NULL;
101         gchar *dirs[] = {self->dir, PLUGINSDIR};
102         g_debug("pluginsdir=%s", PLUGINSDIR);
103         for (int i = 0; i<2; i++) {
104                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
105                 if (dir == NULL)
106                         continue;
107                 g_debug("            checking %s", dirs[i]);
108                 const gchar *name;
109                 while ((name = g_dir_read_name(dir))) {
110                         if (g_pattern_match_simple("*.so", name)) {
111                                 gchar **parts = g_strsplit(name, ".", 2);
112                                 list = g_list_prepend(list, g_strdup(parts[0]));
113                                 g_strfreev(parts);
114                         }
115                 }
116         }
117         return list;
118 }
119
120 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
121                 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
122 {
123         g_debug("GisPlugins: load %s", name);
124         gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
125         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
126                 g_free(path);
127                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
128         }
129         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
130                 g_warning("Module %s not found", name);
131                 g_free(path);
132                 return NULL;
133         }
134         GModule *module = g_module_open(path, 0);
135         g_free(path);
136         if (module == NULL) {
137                 g_warning("Unable to load module %s: %s", name, g_module_error());
138                 return NULL;
139         }
140
141         gpointer constructor_ptr; // GCC 4.1 fix?
142         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
143         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
144                 g_warning("Unable to load symbol %s from %s: %s",
145                                 constructor_str, name, g_module_error());
146                 g_module_close(module);
147                 g_free(constructor_str);
148                 return NULL;
149         }
150         g_free(constructor_str);
151         GisPluginConstructor constructor = constructor_ptr;
152
153         GisPluginStore *store = g_new0(GisPluginStore, 1);
154         store->name = g_strdup(name);
155         store->plugin = constructor(world, view, opengl, prefs);
156         g_ptr_array_add(self->plugins, store);
157         return store->plugin;
158 }
159
160 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
161 {
162         g_debug("GisPlugins: unload %s", name);
163         for (int i = 0; i < self->plugins->len; i++) {
164                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
165                 if (g_str_equal(store->name, name)) {
166                         g_object_unref(store->plugin);
167                         g_free(store->name);
168                         g_free(store);
169                         g_ptr_array_remove_index(self->plugins, i);
170                 }
171         }
172         return FALSE;
173 }
174 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
175 {
176         g_debug("GisPlugins: foreach");
177         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
178         CBFunc callback = (CBFunc)_callback;
179         for (int i = 0; i < self->plugins->len; i++) {
180                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
181                 callback(store->plugin, store->name, user_data);
182         }
183 }