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"
23 static void gis_plugin_base_init(gpointer g_class)
25 static gboolean is_initialized = FALSE;
26 if (!is_initialized) {
27 /* add properties and signals to the interface here */
28 is_initialized = TRUE;
32 GType gis_plugin_get_type()
34 static GType type = 0;
36 static const GTypeInfo info = {
37 sizeof(GisPluginInterface),
41 type = g_type_register_static(G_TYPE_INTERFACE,
42 "GisPlugin", &info, 0);
47 void gis_plugin_expose(GisPlugin *self)
49 g_return_if_fail(GIS_IS_PLUGIN(self));
50 GIS_PLUGIN_GET_INTERFACE(self)->expose(self);
53 GtkWidget *gis_plugin_get_config(GisPlugin *self)
55 g_return_val_if_fail(GIS_IS_PLUGIN(self), NULL);
56 return GIS_PLUGIN_GET_INTERFACE(self)->get_config(self);
65 GisPlugins *gis_plugins_new()
67 return g_ptr_array_new();
70 void gis_plugins_free(GisPlugins *self)
72 for (int i = 0; i < self->len; i++) {
73 GisPluginStore *store = g_ptr_array_index(self, i);
74 g_object_unref(store->plugin);
77 g_ptr_array_remove_index(self, i);
79 g_ptr_array_free(self, TRUE);
82 GList *gis_plugins_available()
84 GDir *dir = g_dir_open(PLUGINDIR, 0, NULL);
89 while ((name = g_dir_read_name(dir))) {
90 if (g_pattern_match_simple("*.so", name)) {
91 gchar **parts = g_strsplit(name, ".", 2);
92 list = g_list_prepend(list, g_strdup(parts[0]));
99 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
100 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
102 gchar *path = g_strdup_printf("%s/%s.%s", PLUGINDIR, name, G_MODULE_SUFFIX);
103 GModule *module = g_module_open(path, 0);
105 if (module == NULL) {
106 g_warning("Unable to load module %s: %s", name, g_module_error());
110 GisPluginConstructor constructor;
111 gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
112 if (!g_module_symbol(module, constructor_str, (gpointer*)&constructor)) {
113 g_warning("Unable to load symbol %s from %s: %s",
114 constructor_str, name, g_module_error());
115 g_module_close(module);
116 g_free(constructor_str);
119 g_free(constructor_str);
121 GisPluginStore *store = g_malloc(sizeof(GisPluginStore));
122 store->name = g_strdup(name);
123 store->plugin = constructor(world, view, opengl, prefs);
124 g_ptr_array_add(self, store);
125 return store->plugin;
128 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
130 for (int i = 0; i < self->len; i++) {
131 GisPluginStore *store = g_ptr_array_index(self, i);
132 if (g_str_equal(store->name, name)) {
133 g_object_unref(store->plugin);
136 g_ptr_array_remove_index(self, i);
141 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
143 typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
144 CBFunc callback = (CBFunc)_callback;
145 for (int i = 0; i < self->len; i++) {
146 GisPluginStore *store = g_ptr_array_index(self, i);
147 callback(store->plugin, store->name, user_data);