]> Pileus Git - grits/blob - src/gis/gis-plugin.c
Splitting GIS into a shared library, and a lot more
[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 static void gis_plugin_base_init(gpointer g_class)
24 {
25         static gboolean is_initialized = FALSE;
26         if (!is_initialized) {
27                 /* add properties and signals to the interface here */
28                 is_initialized = TRUE;
29         }
30 }
31
32 GType gis_plugin_get_type()
33 {
34         static GType type = 0;
35         if (type == 0) {
36                 static const GTypeInfo info = {
37                         sizeof(GisPluginInterface),
38                         gis_plugin_base_init,
39                         NULL,
40                 };
41                 type = g_type_register_static(G_TYPE_INTERFACE,
42                                 "GisPlugin", &info, 0);
43         }
44         return type;
45 }
46
47 void gis_plugin_expose(GisPlugin *self)
48 {
49         g_return_if_fail(GIS_IS_PLUGIN(self));
50         GIS_PLUGIN_GET_INTERFACE(self)->expose(self);
51 }
52
53 GtkWidget *gis_plugin_get_config(GisPlugin *self)
54 {
55         g_return_val_if_fail(GIS_IS_PLUGIN(self), NULL);
56         return GIS_PLUGIN_GET_INTERFACE(self)->get_config(self);
57 }
58
59 /* Plugins API */
60 typedef struct {
61         gchar *name;
62         GisPlugin *plugin;
63 } GisPluginStore;
64
65 GisPlugins *gis_plugins_new()
66 {
67         return g_ptr_array_new();
68 }
69
70 void gis_plugins_free(GisPlugins *self)
71 {
72         for (int i = 0; i < self->len; i++) {
73                 GisPluginStore *store = g_ptr_array_index(self, i);
74                 g_object_unref(store->plugin);
75                 g_free(store->name);
76                 g_free(store);
77                 g_ptr_array_remove_index(self, i);
78         }
79         g_ptr_array_free(self, TRUE);
80 }
81
82 GList *gis_plugins_available()
83 {
84         GDir *dir = g_dir_open(PLUGINDIR, 0, NULL);
85         GList *list = NULL;
86         const gchar *name;
87         while ((name = g_dir_read_name(dir))) {
88                 if (g_pattern_match_simple("*.so", name)) {
89                         gchar **parts = g_strsplit(name, ".", 2);
90                         list = g_list_prepend(list, g_strdup(parts[0]));
91                         g_strfreev(parts);
92                 }
93         }
94         return list;
95 }
96
97 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
98                 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
99 {
100         gchar *path = g_strdup_printf("%s/%s.%s", PLUGINDIR, name, G_MODULE_SUFFIX);
101         GModule *module = g_module_open(path, 0);
102         g_free(path);
103         if (module == NULL) {
104                 g_warning("Unable to load module %s: %s", name, g_module_error());
105                 return NULL;
106         }
107
108         GisPluginConstructor constructor;
109         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
110         if (!g_module_symbol(module, constructor_str, (gpointer*)&constructor)) {
111                 g_warning("Unable to load symbol %s from %s: %s",
112                                 constructor_str, name, g_module_error());
113                 g_module_close(module);
114                 g_free(constructor_str);
115                 return NULL;
116         }
117         g_free(constructor_str);
118
119         GisPluginStore *store = g_malloc(sizeof(GisPluginStore));
120         store->name = g_strdup(name);
121         store->plugin = constructor(world, view, opengl, prefs);
122         g_ptr_array_add(self, store);
123         return store->plugin;
124 }
125
126 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
127 {
128         for (int i = 0; i < self->len; i++) {
129                 GisPluginStore *store = g_ptr_array_index(self, i);
130                 if (g_str_equal(store->name, name)) {
131                         g_object_unref(store->plugin);
132                         g_free(store->name);
133                         g_free(store);
134                         g_ptr_array_remove_index(self, i);
135                 }
136         }
137         return FALSE;
138 }
139 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
140 {
141         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
142         CBFunc callback = (CBFunc)_callback;
143         for (int i = 0; i < self->len; i++) {
144                 GisPluginStore *store = g_ptr_array_index(self, i);
145                 callback(store->plugin, store->name, user_data);
146         }
147 }