]> Pileus Git - grits/blob - src/gis-plugin.c
Fix misc bugs and bump version to 0.3
[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         GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
60         return iface->get_config ? iface->get_config (self) : NULL;
61 }
62
63
64 /***************
65  * Plugins API *
66  ***************/
67 typedef struct {
68         gchar *name;
69         GisPlugin *plugin;
70 } GisPluginStore;
71
72 GisPlugins *gis_plugins_new(gchar *dir)
73 {
74         g_debug("GisPlugins: new - dir=%s", dir);
75         GisPlugins *plugins = g_new0(GisPlugins, 1);
76         if (dir)
77                 plugins->dir = g_strdup(dir);
78         plugins->plugins = g_ptr_array_new();
79         return plugins;
80 }
81
82 void gis_plugins_free(GisPlugins *self)
83 {
84         g_debug("GisPlugins: free");
85         for (int i = 0; i < self->plugins->len; i++) {
86                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
87                 g_object_unref(store->plugin);
88                 g_free(store->name);
89                 g_free(store);
90                 g_ptr_array_remove_index(self->plugins, i);
91         }
92         g_ptr_array_free(self->plugins, TRUE);
93         if (self->dir)
94                 g_free(self->dir);
95         g_free(self);
96 }
97
98 GList *gis_plugins_available(GisPlugins *self)
99 {
100         g_debug("GisPlugins: available");
101         GList *list = NULL;
102         gchar *dirs[] = {self->dir, PLUGINSDIR};
103         g_debug("pluginsdir=%s", PLUGINSDIR);
104         for (int i = 0; i<2; i++) {
105                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
106                 if (dir == NULL)
107                         continue;
108                 g_debug("            checking %s", dirs[i]);
109                 const gchar *name;
110                 while ((name = g_dir_read_name(dir))) {
111                         if (g_pattern_match_simple("*.so", name)) {
112                                 gchar **parts = g_strsplit(name, ".", 2);
113                                 list = g_list_prepend(list, g_strdup(parts[0]));
114                                 g_strfreev(parts);
115                         }
116                 }
117         }
118         return list;
119 }
120
121 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
122                 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
123 {
124         g_debug("GisPlugins: load %s", name);
125         gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
126         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
127                 g_free(path);
128                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
129         }
130         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
131                 g_warning("Module %s not found", name);
132                 g_free(path);
133                 return NULL;
134         }
135         GModule *module = g_module_open(path, 0);
136         g_free(path);
137         if (module == NULL) {
138                 g_warning("Unable to load module %s: %s", name, g_module_error());
139                 return NULL;
140         }
141
142         gpointer constructor_ptr; // GCC 4.1 fix?
143         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
144         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
145                 g_warning("Unable to load symbol %s from %s: %s",
146                                 constructor_str, name, g_module_error());
147                 g_module_close(module);
148                 g_free(constructor_str);
149                 return NULL;
150         }
151         g_free(constructor_str);
152         GisPluginConstructor constructor = constructor_ptr;
153
154         GisPluginStore *store = g_new0(GisPluginStore, 1);
155         store->name = g_strdup(name);
156         store->plugin = constructor(world, view, opengl, prefs);
157         g_ptr_array_add(self->plugins, store);
158         return store->plugin;
159 }
160
161 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
162 {
163         g_debug("GisPlugins: unload %s", name);
164         for (int i = 0; i < self->plugins->len; i++) {
165                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
166                 if (g_str_equal(store->name, name)) {
167                         g_object_unref(store->plugin);
168                         g_free(store->name);
169                         g_free(store);
170                         g_ptr_array_remove_index(self->plugins, i);
171                 }
172         }
173         return FALSE;
174 }
175 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
176 {
177         g_debug("GisPlugins: foreach");
178         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
179         CBFunc callback = (CBFunc)_callback;
180         for (int i = 0; i < self->plugins->len; i++) {
181                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
182                 callback(store->plugin, store->name, user_data);
183         }
184 }