2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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.
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.
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/>.
21 #include "gis-plugin.h"
26 static void gis_plugin_base_init(gpointer g_class)
28 static gboolean is_initialized = FALSE;
29 if (!is_initialized) {
30 /* add properties and signals to the interface here */
31 is_initialized = TRUE;
35 GType gis_plugin_get_type()
37 static GType type = 0;
39 static const GTypeInfo info = {
40 sizeof(GisPluginInterface),
44 type = g_type_register_static(G_TYPE_INTERFACE,
45 "GisPlugin", &info, 0);
50 void gis_plugin_expose(GisPlugin *self)
52 g_return_if_fail(GIS_IS_PLUGIN(self));
53 GIS_PLUGIN_GET_INTERFACE(self)->expose(self);
56 GtkWidget *gis_plugin_get_config(GisPlugin *self)
58 g_return_val_if_fail(GIS_IS_PLUGIN(self), NULL);
59 return GIS_PLUGIN_GET_INTERFACE(self)->get_config(self);
71 GisPlugins *gis_plugins_new()
73 return g_ptr_array_new();
76 void gis_plugins_free(GisPlugins *self)
78 for (int i = 0; i < self->len; i++) {
79 GisPluginStore *store = g_ptr_array_index(self, i);
80 g_object_unref(store->plugin);
83 g_ptr_array_remove_index(self, i);
85 g_ptr_array_free(self, TRUE);
88 GList *gis_plugins_available()
90 GDir *dir = g_dir_open(PLUGINDIR, 0, NULL);
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]));
105 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
106 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
108 gchar *path = g_strdup_printf("%s/%s.%s", PLUGINDIR, name, G_MODULE_SUFFIX);
109 GModule *module = g_module_open(path, 0);
111 if (module == NULL) {
112 g_warning("Unable to load module %s: %s", name, g_module_error());
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);
125 g_free(constructor_str);
126 GisPluginConstructor constructor = constructor_ptr;
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;
135 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
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);
143 g_ptr_array_remove_index(self, i);
148 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
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);