]> Pileus Git - grits/blob - src/gis-plugin.c
c874095bd41795ac4381ca4a2ac97e728fd819a3
[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         for (int i = 0; i<2; i++) {
103                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
104                 if (dir == NULL)
105                         continue;
106                 g_debug("            checking %s", dirs[i]);
107                 const gchar *name;
108                 while ((name = g_dir_read_name(dir))) {
109                         if (g_pattern_match_simple("*.so", name)) {
110                                 gchar **parts = g_strsplit(name, ".", 2);
111                                 list = g_list_prepend(list, g_strdup(parts[0]));
112                                 g_strfreev(parts);
113                         }
114                 }
115         }
116         return list;
117 }
118
119 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
120                 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
121 {
122         g_debug("GisPlugins: load %s", name);
123         gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
124         if (!g_file_test(path, G_FILE_TEST_EXISTS))
125                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
126         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
127                 g_warning("Module %s not found", name);
128                 return NULL;
129         }
130         GModule *module = g_module_open(path, 0);
131         g_free(path);
132         if (module == NULL) {
133                 g_warning("Unable to load module %s: %s", name, g_module_error());
134                 return NULL;
135         }
136
137         gpointer constructor_ptr; // GCC 4.1 fix?
138         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
139         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
140                 g_warning("Unable to load symbol %s from %s: %s",
141                                 constructor_str, name, g_module_error());
142                 g_module_close(module);
143                 g_free(constructor_str);
144                 return NULL;
145         }
146         g_free(constructor_str);
147         GisPluginConstructor constructor = constructor_ptr;
148
149         GisPluginStore *store = g_malloc(sizeof(GisPluginStore));
150         store->name = g_strdup(name);
151         store->plugin = constructor(world, view, opengl, prefs);
152         g_ptr_array_add(self->plugins, store);
153         return store->plugin;
154 }
155
156 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
157 {
158         g_debug("GisPlugins: unload %s", name);
159         for (int i = 0; i < self->plugins->len; i++) {
160                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
161                 if (g_str_equal(store->name, name)) {
162                         g_object_unref(store->plugin);
163                         g_free(store->name);
164                         g_free(store);
165                         g_ptr_array_remove_index(self->plugins, i);
166                 }
167         }
168         return FALSE;
169 }
170 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
171 {
172         g_debug("GisPlugins: foreach");
173         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
174         CBFunc callback = (CBFunc)_callback;
175         for (int i = 0; i < self->plugins->len; i++) {
176                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
177                 callback(store->plugin, store->name, user_data);
178         }
179 }