]> Pileus Git - grits/blob - src/grits-plugin.c
Fix bugs in GritsPlugins
[grits] / src / grits-plugin.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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 /**
19  * SECTION:grits-plugin
20  * @short_description: Plugin support
21  *
22  * A plugin in grits is a GObject which implements the GritsPlugin interface.
23  * Additionally, each plugin is compiled to a separate shared object and loaded
24  * conditionally at runtime when the plugin is enabled. Each such shared object
25  * should define a GritsPluginConstructor() function named
26  * grits_plugin_NAME_new which will be called when loading the plugin.
27  *
28  * Almost all grits functionality is provided by a set of plugins. Each plugin
29  * can how however much it likes. The interface between plugins and the rest of
30  * grits is intentionally very thin. Since grits is the library, plugins must
31  * manually do everything. For instance, to draw something in the world, the
32  * plugin must add an object to the viewer. Likewise, plugins need to
33  * register callbacks on the viewer in order to receive updates, very little
34  * happens automagically.
35  *
36  * That being said, one thing that plugins do do automagically, is provide a
37  * configuration area.  Since the plugin doesn't know what application is is
38  * being loaded form, it is better for the application to ask the plugin for
39  * it's confirmation area, not the other way around.
40  */
41
42 #include <glib.h>
43 #include <gmodule.h>
44
45 #include <string.h>
46
47 #include "grits-plugin.h"
48
49 /********************
50  * Plugin interface *
51  ********************/
52 static void grits_plugin_base_init(gpointer g_class)
53 {
54         static gboolean is_initialized = FALSE;
55         if (!is_initialized) {
56                 /* add properties and signals to the interface here */
57                 is_initialized = TRUE;
58         }
59 }
60
61 GType grits_plugin_get_type()
62 {
63         static GType type = 0;
64         if (type == 0) {
65                 static const GTypeInfo info = {
66                         sizeof(GritsPluginInterface),
67                         grits_plugin_base_init,
68                         NULL,
69                 };
70                 type = g_type_register_static(G_TYPE_INTERFACE,
71                                 "GritsPlugin", &info, 0);
72         }
73         return type;
74 }
75
76 /**
77  * grits_plugin_get_name:
78  * @plugin: the plugin
79  *
80  * Get a short human readable name for a plugin, this is not necessarily the
81  * same as the name of the shared object.
82  *
83  * Returns: a short name for the plugin
84  */
85 const gchar *grits_plugin_get_name(GritsPlugin *plugin)
86 {
87         if (!GRITS_IS_PLUGIN(plugin))
88                 return NULL;
89         return GRITS_PLUGIN_GET_INTERFACE(plugin)->name;
90 }
91
92 /**
93  * grits_plugin_get_description:
94  * @plugin: the plugin
95  *
96  * Get a description of a plugin
97  *
98  * Returns: a description of the plugin
99  */
100 const gchar *grits_plugin_get_description(GritsPlugin *plugin)
101 {
102         if (!GRITS_IS_PLUGIN(plugin))
103                 return NULL;
104         return GRITS_PLUGIN_GET_INTERFACE(plugin)->description;
105 }
106
107 /**
108  * grits_plugin_get_config:
109  * @plugin: the plugin
110  *
111  * Each plugin can provide a configuration area. Applications using grits
112  * should display this configuration area to the user so they can modify the
113  * behavior of the plugin.
114  *
115  * Returns: a configuration widget for the plugin
116  */
117 GtkWidget *grits_plugin_get_config(GritsPlugin *plugin)
118 {
119         if (!GRITS_IS_PLUGIN(plugin))
120                 return NULL;
121         GritsPluginInterface *iface = GRITS_PLUGIN_GET_INTERFACE(plugin);
122         return iface->get_config ? iface->get_config(plugin) : NULL;
123 }
124
125
126 /***************
127  * Plugins API *
128  ***************/
129 typedef struct {
130         gchar       *name;
131         GritsPlugin *plugin;
132         GModule     *module;
133 } GritsPluginStore;
134
135 /**
136  * grits_plugins_new:
137  * @dir:   the directory to search for plugins in
138  * @prefs: a #GritsPrefs to save the state of plugins, or NULL
139  *
140  * Create a new plugin source. If @prefs is not %NULL, the state of the plugins
141  * will be saved when they are either enabled or disabled.
142  *
143  * Returns: the new plugin source
144  */
145 GritsPlugins *grits_plugins_new(const gchar *dir, GritsPrefs *prefs)
146 {
147         g_debug("GritsPlugins: new - dir=%s", dir);
148         GritsPlugins *plugins = g_new0(GritsPlugins, 1);
149         plugins->prefs = prefs;
150         if (dir)
151                 plugins->dir = g_strdup(dir);
152         return plugins;
153 }
154
155 static void grits_plugins_free_store(GritsPluginStore *store)
156 {
157         g_object_unref(store->plugin);
158         /* Flush any possible callbacks before
159          * unloading the plugin code */
160         while (gtk_events_pending())
161                   gtk_main_iteration();
162         g_module_close(store->module);
163         g_free(store->name);
164         g_free(store);
165 }
166
167 /**
168  * grits_plugins_free:
169  * @plugins: the #GritsPlugins to free
170  *
171  * Free data used by a plugin source
172  */
173 void grits_plugins_free(GritsPlugins *plugins)
174 {
175         g_debug("GritsPlugins: free");
176         for (GList *cur = plugins->plugins; cur; cur = cur->next) {
177                 GritsPluginStore *store = cur->data;
178                 g_debug("GritsPlugin: freeing %s refs=%d->%d", store->name,
179                         G_OBJECT(store->plugin)->ref_count,
180                         G_OBJECT(store->plugin)->ref_count-1);
181                 grits_plugins_free_store(store);
182         }
183         g_list_free(plugins->plugins);
184         if (plugins->dir)
185                 g_free(plugins->dir);
186         g_free(plugins);
187 }
188
189 /**
190  * grits_plugins_available:
191  * @plugins: the plugin source
192  *
193  * Search the plugin directory for shared objects which can be loaded as
194  * plugins.
195  *
196  * Returns: the list of available plugins
197  */
198 GList *grits_plugins_available(GritsPlugins *plugins)
199 {
200         g_debug("GritsPlugins: available");
201         GList *list = NULL;
202         gchar *dirs[] = {plugins->dir, PLUGINSDIR};
203         g_debug("pluginsdir=%s", PLUGINSDIR);
204         for (int i = 0; i<2; i++) {
205                 if (dirs[i] == NULL)
206                         continue;
207                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
208                 if (dir == NULL)
209                         continue;
210                 g_debug("            checking %s", dirs[i]);
211                 const gchar *name;
212                 while ((name = g_dir_read_name(dir))) {
213                         if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
214                                 gchar **parts = g_strsplit(name, ".", 2);
215                                 list = g_list_prepend(list, g_strdup(parts[0]));
216                                 g_strfreev(parts);
217                         }
218                 }
219                 g_dir_close(dir);
220         }
221         list = g_list_sort(list, (GCompareFunc)strcmp);
222         for (GList *cur = list; cur; cur = cur->next)
223                 while (cur->next && g_str_equal(cur->data,cur->next->data)) {
224                         GList *tmp = cur->next;
225                         list = g_list_remove_link(list, cur);
226                         cur = tmp;
227                 }
228         return list;
229 }
230
231 /**
232  * grits_plugins_load:
233  * @plugins: the plugins source
234  * @name:    the name of the plugin to load
235  * @viewer:  a #GritsViewer to pass to the plugins constructor
236  * @prefs:   a #GritsPrefs to pass to the plugins constructor
237  *
238  * @name should be the name of the shared object without the file extension.
239  * This is the same as what is returned by grits_plugins_available().
240  *
241  * When loading plugins, the @prefs argument is used, not the #GritsPrefs stored
242  * in @plugins.
243  *
244  * Returns: the new plugin
245  */
246 GritsPlugin *grits_plugins_load(GritsPlugins *plugins, const char *name,
247                 GritsViewer *viewer, GritsPrefs *prefs)
248 {
249         g_debug("GritsPlugins: load %s", name);
250         gchar *path = g_strdup_printf("%s/%s.%s", plugins->dir, name, G_MODULE_SUFFIX);
251         g_debug("GritsPlugins: load - trying %s", path);
252         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
253                 g_free(path);
254                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
255         }
256         g_debug("GritsPlugins: load - trying %s", path);
257         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
258                 g_warning("Module %s not found", name);
259                 g_free(path);
260                 return NULL;
261         }
262         GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
263         g_free(path);
264         if (module == NULL) {
265                 g_warning("Unable to load module %s: %s", name, g_module_error());
266                 return NULL;
267         }
268
269         gpointer constructor_ptr; // GCC 4.1 fix?
270         gchar *constructor_str = g_strconcat("grits_plugin_", name, "_new", NULL);
271         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
272                 g_warning("Unable to load symbol %s from %s: %s",
273                                 constructor_str, name, g_module_error());
274                 g_module_close(module);
275                 g_free(constructor_str);
276                 return NULL;
277         }
278         g_free(constructor_str);
279         GritsPluginConstructor constructor = constructor_ptr;
280
281         GritsPluginStore *store = g_new0(GritsPluginStore, 1);
282         store->name = g_strdup(name);
283         store->plugin = constructor(viewer, prefs);
284         store->module = module;
285         plugins->plugins = g_list_prepend(plugins->plugins, store);
286         return store->plugin;
287 }
288
289 /**
290  * grits_plugins_enable:
291  * @plugins: the plugins source
292  * @name:    the name of the plugin to load
293  * @viewer:  a #GritsViewer to pass to the plugins constructor
294  * @prefs:   a #GritsPrefs to pass to the plugins constructor
295  *
296  * Load a plugin and save it's loaded/unloaded state in the #GritsPrefs stored in
297  * #plugins.
298  *
299  * See also: grits_plugins_load()
300  *
301  * Returns: the new plugin
302  */
303 GritsPlugin *grits_plugins_enable(GritsPlugins *plugins, const char *name,
304                 GritsViewer *viewer, GritsPrefs *prefs)
305 {
306         GritsPlugin *plugin = grits_plugins_load(plugins, name, viewer, prefs);
307         grits_prefs_set_boolean_v(plugins->prefs, "plugins", name, TRUE);
308         return plugin;
309 }
310
311 /**
312  * grits_plugins_load_enabled:
313  * @plugins: the plugins source
314  * @viewer:  a #GritsViewer to pass to the plugins constructor
315  * @prefs:   a #GritsPrefs to pass to the plugins constructor
316  *
317  * Load all enabled which have previously been enabled.
318  *
319  * See also: grits_plugins_load()
320  *
321  * Returns: a list of all loaded plugins
322  */
323 GList *grits_plugins_load_enabled(GritsPlugins *plugins,
324                 GritsViewer *viewer, GritsPrefs *prefs)
325 {
326         GList *loaded = NULL;
327         for (GList *cur = grits_plugins_available(plugins); cur; cur = cur->next) {
328                 gchar *name = cur->data;
329                 if (grits_prefs_get_boolean_v(plugins->prefs, "plugins", name, NULL)) {
330                         GritsPlugin *plugin = grits_plugins_load(plugins, name, viewer, prefs);
331                         loaded = g_list_prepend(loaded, plugin);
332                 }
333         }
334         return loaded;
335 }
336
337 /**
338  * grits_plugins_unload:
339  * @plugins: the plugins source
340  * @name:    the name of the plugin to unload
341  *
342  * Unload a plugin and free any associated data.
343  *
344  * Returns: %FALSE
345  */
346 gboolean grits_plugins_unload(GritsPlugins *plugins, const char *name)
347 {
348         g_debug("GritsPlugins: unload %s", name);
349         for (GList *cur = plugins->plugins; cur; cur = cur->next) {
350                 GritsPluginStore *store = cur->data;
351                 if (g_str_equal(store->name, name)) {
352                         grits_plugins_free_store(store);
353                         plugins->plugins = g_list_delete_link(plugins->plugins, cur);
354                 }
355         }
356         return FALSE;
357 }
358
359 /**
360  * grits_plugins_disable:
361  * @plugins: the plugins source
362  * @name:    the name of the plugin to unload
363  *
364  * Unload a plugin and save it's loaded/unloaded state in the #GritsPrefs stored
365  * in #plugins.
366  *
367  * See also: grits_plugins_unload()
368  *
369  * Returns: %FALSE
370  */
371 gboolean grits_plugins_disable(GritsPlugins *plugins, const char *name)
372 {
373         grits_prefs_set_boolean_v(plugins->prefs, "plugins", name, FALSE);
374         grits_plugins_unload(plugins, name);
375         return FALSE;
376 }
377
378 /**
379  * grits_plugins_foreach:
380  * @plugins:   the plugins source
381  * @callback:  a function to call on each plugin
382  * @user_data: user data to pass to the function
383  *
384  * Iterate over all plugins loaded by the plugins source
385  */
386 void grits_plugins_foreach(GritsPlugins *plugins, GCallback _callback, gpointer user_data)
387 {
388         g_debug("GritsPlugins: foreach");
389         if (plugins == NULL)
390                 return;
391         typedef void (*CBFunc)(GritsPlugin *, const gchar *, gpointer);
392         CBFunc callback = (CBFunc)_callback;
393         for (GList *cur = plugins->plugins; cur; cur = cur->next) {
394                 GritsPluginStore *store = cur->data;
395                 callback(store->plugin, store->name, user_data);
396         }
397 }