]> Pileus Git - grits/blob - src/gis/gis-plugin.c
Lots of work on libGIS
[grits] / src / gis / 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()
72 {
73         return g_ptr_array_new();
74 }
75
76 void gis_plugins_free(GisPlugins *self)
77 {
78         for (int i = 0; i < self->len; i++) {
79                 GisPluginStore *store = g_ptr_array_index(self, i);
80                 g_object_unref(store->plugin);
81                 g_free(store->name);
82                 g_free(store);
83                 g_ptr_array_remove_index(self, i);
84         }
85         g_ptr_array_free(self, TRUE);
86 }
87
88 GList *gis_plugins_available()
89 {
90         GDir *dir = g_dir_open(PLUGINDIR, 0, NULL);
91         if (dir == NULL)
92                 return NULL;
93         GList *list = NULL;
94         const gchar *name;
95         while ((name = g_dir_read_name(dir))) {
96                 if (g_pattern_match_simple("*.so", name)) {
97                         gchar **parts = g_strsplit(name, ".", 2);
98                         list = g_list_prepend(list, g_strdup(parts[0]));
99                         g_strfreev(parts);
100                 }
101         }
102         return list;
103 }
104
105 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
106                 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
107 {
108         gchar *path = g_strdup_printf("%s/%s.%s", PLUGINDIR, name, G_MODULE_SUFFIX);
109         GModule *module = g_module_open(path, 0);
110         g_free(path);
111         if (module == NULL) {
112                 g_warning("Unable to load module %s: %s", name, g_module_error());
113                 return NULL;
114         }
115
116         gpointer constructor_ptr; // GCC 4.1 fix?
117         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
118         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
119                 g_warning("Unable to load symbol %s from %s: %s",
120                                 constructor_str, name, g_module_error());
121                 g_module_close(module);
122                 g_free(constructor_str);
123                 return NULL;
124         }
125         g_free(constructor_str);
126         GisPluginConstructor constructor = constructor_ptr;
127
128         GisPluginStore *store = g_malloc(sizeof(GisPluginStore));
129         store->name = g_strdup(name);
130         store->plugin = constructor(world, view, opengl, prefs);
131         g_ptr_array_add(self, store);
132         return store->plugin;
133 }
134
135 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
136 {
137         for (int i = 0; i < self->len; i++) {
138                 GisPluginStore *store = g_ptr_array_index(self, i);
139                 if (g_str_equal(store->name, name)) {
140                         g_object_unref(store->plugin);
141                         g_free(store->name);
142                         g_free(store);
143                         g_ptr_array_remove_index(self, i);
144                 }
145         }
146         return FALSE;
147 }
148 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
149 {
150         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
151         CBFunc callback = (CBFunc)_callback;
152         for (int i = 0; i < self->len; i++) {
153                 GisPluginStore *store = g_ptr_array_index(self, i);
154                 callback(store->plugin, store->name, user_data);
155         }
156 }