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 GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
60 return iface->get_config ? iface->get_config (self) : NULL;
72 GisPlugins *gis_plugins_new(gchar *dir)
74 g_debug("GisPlugins: new - dir=%s", dir);
75 GisPlugins *plugins = g_new0(GisPlugins, 1);
77 plugins->dir = g_strdup(dir);
78 plugins->plugins = g_ptr_array_new();
82 void gis_plugins_free(GisPlugins *self)
84 g_debug("GisPlugins: free");
85 for (int i = 0; i < self->plugins->len; i++) {
86 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
87 g_object_unref(store->plugin);
90 g_ptr_array_remove_index(self->plugins, i);
92 g_ptr_array_free(self->plugins, TRUE);
98 GList *gis_plugins_available(GisPlugins *self)
100 g_debug("GisPlugins: available");
102 gchar *dirs[] = {self->dir, PLUGINSDIR};
103 g_debug("pluginsdir=%s", PLUGINSDIR);
104 for (int i = 0; i<2; i++) {
105 GDir *dir = g_dir_open(dirs[i], 0, NULL);
108 g_debug(" checking %s", dirs[i]);
110 while ((name = g_dir_read_name(dir))) {
111 if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
112 gchar **parts = g_strsplit(name, ".", 2);
113 list = g_list_prepend(list, g_strdup(parts[0]));
121 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
122 GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
124 g_debug("GisPlugins: load %s", name);
125 gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
126 g_debug("GisPlugins: load - trying %s", path);
127 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
129 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
131 g_debug("GisPlugins: load - trying %s", path);
132 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
133 g_warning("Module %s not found", name);
137 GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
139 if (module == NULL) {
140 g_warning("Unable to load module %s: %s", name, g_module_error());
144 gpointer constructor_ptr; // GCC 4.1 fix?
145 gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
146 if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
147 g_warning("Unable to load symbol %s from %s: %s",
148 constructor_str, name, g_module_error());
149 g_module_close(module);
150 g_free(constructor_str);
153 g_free(constructor_str);
154 GisPluginConstructor constructor = constructor_ptr;
156 GisPluginStore *store = g_new0(GisPluginStore, 1);
157 store->name = g_strdup(name);
158 store->plugin = constructor(world, view, opengl, prefs);
159 g_ptr_array_add(self->plugins, store);
160 return store->plugin;
163 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
165 g_debug("GisPlugins: unload %s", name);
166 for (int i = 0; i < self->plugins->len; i++) {
167 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
168 if (g_str_equal(store->name, name)) {
169 g_object_unref(store->plugin);
172 g_ptr_array_remove_index(self->plugins, i);
177 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
179 g_debug("GisPlugins: foreach");
180 typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
181 CBFunc callback = (CBFunc)_callback;
182 for (int i = 0; i < self->plugins->len; i++) {
183 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
184 callback(store->plugin, store->name, user_data);