2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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/>.
19 * SECTION:grits-plugin
20 * @short_description: Plugin support
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.
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.
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.
47 #include "grits-plugin.h"
52 static void grits_plugin_base_init(gpointer g_class)
54 static gboolean is_initialized = FALSE;
55 if (!is_initialized) {
56 /* add properties and signals to the interface here */
57 is_initialized = TRUE;
61 GType grits_plugin_get_type()
63 static GType type = 0;
65 static const GTypeInfo info = {
66 sizeof(GritsPluginInterface),
67 grits_plugin_base_init,
70 type = g_type_register_static(G_TYPE_INTERFACE,
71 "GritsPlugin", &info, 0);
77 * grits_plugin_get_name:
80 * Get a short human readable name for a plugin, this is not necessarily the
81 * same as the name of the shared object.
83 * Returns: a short name for the plugin
85 const gchar *grits_plugin_get_name(GritsPlugin *plugin)
87 if (!GRITS_IS_PLUGIN(plugin))
89 return GRITS_PLUGIN_GET_INTERFACE(plugin)->name;
93 * grits_plugin_get_description:
96 * Get a description of a plugin
98 * Returns: a description of the plugin
100 const gchar *grits_plugin_get_description(GritsPlugin *plugin)
102 if (!GRITS_IS_PLUGIN(plugin))
104 return GRITS_PLUGIN_GET_INTERFACE(plugin)->description;
108 * grits_plugin_get_config:
109 * @plugin: the plugin
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.
115 * Returns: a configuration widget for the plugin
117 GtkWidget *grits_plugin_get_config(GritsPlugin *plugin)
119 if (!GRITS_IS_PLUGIN(plugin))
121 GritsPluginInterface *iface = GRITS_PLUGIN_GET_INTERFACE(plugin);
122 return iface->get_config ? iface->get_config(plugin) : NULL;
136 * @dir: the directory to search for plugins in
137 * @prefs: a #GritsPrefs to save the state of plugins, or NULL
139 * Create a new plugin source. If @prefs is not %NULL, the state of the plugins
140 * will be saved when they are either enabled or disabled.
142 * Returns: the new plugin source
144 GritsPlugins *grits_plugins_new(const gchar *dir, GritsPrefs *prefs)
146 g_debug("GritsPlugins: new - dir=%s", dir);
147 GritsPlugins *plugins = g_new0(GritsPlugins, 1);
148 plugins->prefs = prefs;
150 plugins->dir = g_strdup(dir);
155 * grits_plugins_free:
156 * @plugins: the #GritsPlugins to free
158 * Free data used by a plugin source
160 void grits_plugins_free(GritsPlugins *plugins)
162 g_debug("GritsPlugins: free");
163 for (GList *cur = plugins->plugins; cur; cur = cur->next) {
164 GritsPluginStore *store = cur->data;
165 g_debug("GritsPlugin: freeing %s refs=%d->%d", store->name,
166 G_OBJECT(store->plugin)->ref_count,
167 G_OBJECT(store->plugin)->ref_count-1);
168 g_object_unref(store->plugin);
172 g_list_free(plugins->plugins);
174 g_free(plugins->dir);
179 * grits_plugins_available:
180 * @plugins: the plugin source
182 * Search the plugin directory for shared objects which can be loaded as
185 * Returns: the list of available plugins
187 GList *grits_plugins_available(GritsPlugins *plugins)
189 g_debug("GritsPlugins: available");
191 gchar *dirs[] = {plugins->dir, PLUGINSDIR};
192 g_debug("pluginsdir=%s", PLUGINSDIR);
193 for (int i = 0; i<2; i++) {
196 GDir *dir = g_dir_open(dirs[i], 0, NULL);
199 g_debug(" checking %s", dirs[i]);
201 while ((name = g_dir_read_name(dir))) {
202 if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
203 gchar **parts = g_strsplit(name, ".", 2);
204 list = g_list_prepend(list, g_strdup(parts[0]));
210 list = g_list_sort(list, (GCompareFunc)strcmp);
211 for (GList *cur = list; cur; cur = cur->next)
212 while (cur->next && g_str_equal(cur->data,cur->next->data)) {
213 GList *tmp = cur->next;
214 list = g_list_remove_link(list, cur);
221 * grits_plugins_load:
222 * @plugins: the plugins source
223 * @name: the name of the plugin to load
224 * @viewer: a #GritsViewer to pass to the plugins constructor
225 * @prefs: a #GritsPrefs to pass to the plugins constructor
227 * @name should be the name of the shared object without the file extension.
228 * This is the same as what is returned by grits_plugins_available().
230 * When loading plugins, the @prefs argument is used, not the #GritsPrefs stored
233 * Returns: the new plugin
235 GritsPlugin *grits_plugins_load(GritsPlugins *plugins, const char *name,
236 GritsViewer *viewer, GritsPrefs *prefs)
238 g_debug("GritsPlugins: load %s", name);
239 gchar *path = g_strdup_printf("%s/%s.%s", plugins->dir, name, G_MODULE_SUFFIX);
240 g_debug("GritsPlugins: load - trying %s", path);
241 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
243 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
245 g_debug("GritsPlugins: load - trying %s", path);
246 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
247 g_warning("Module %s not found", name);
251 GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
253 if (module == NULL) {
254 g_warning("Unable to load module %s: %s", name, g_module_error());
258 gpointer constructor_ptr; // GCC 4.1 fix?
259 gchar *constructor_str = g_strconcat("grits_plugin_", name, "_new", NULL);
260 if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
261 g_warning("Unable to load symbol %s from %s: %s",
262 constructor_str, name, g_module_error());
263 g_module_close(module);
264 g_free(constructor_str);
267 g_free(constructor_str);
268 GritsPluginConstructor constructor = constructor_ptr;
270 GritsPluginStore *store = g_new0(GritsPluginStore, 1);
271 store->name = g_strdup(name);
272 store->plugin = constructor(viewer, prefs);
273 plugins->plugins = g_list_prepend(plugins->plugins, store);
274 return store->plugin;
278 * grits_plugins_enable:
279 * @plugins: the plugins source
280 * @name: the name of the plugin to load
281 * @viewer: a #GritsViewer to pass to the plugins constructor
282 * @prefs: a #GritsPrefs to pass to the plugins constructor
284 * Load a plugin and save it's loaded/unloaded state in the #GritsPrefs stored in
287 * See also: grits_plugins_load()
289 * Returns: the new plugin
291 GritsPlugin *grits_plugins_enable(GritsPlugins *plugins, const char *name,
292 GritsViewer *viewer, GritsPrefs *prefs)
294 GritsPlugin *plugin = grits_plugins_load(plugins, name, viewer, prefs);
295 grits_prefs_set_boolean_v(plugins->prefs, "plugins", name, TRUE);
300 * grits_plugins_load_enabled:
301 * @plugins: the plugins source
302 * @viewer: a #GritsViewer to pass to the plugins constructor
303 * @prefs: a #GritsPrefs to pass to the plugins constructor
305 * Load all enabled which have previously been enabled.
307 * See also: grits_plugins_load()
309 * Returns: a list of all loaded plugins
311 GList *grits_plugins_load_enabled(GritsPlugins *plugins,
312 GritsViewer *viewer, GritsPrefs *prefs)
314 GList *loaded = NULL;
315 for (GList *cur = grits_plugins_available(plugins); cur; cur = cur->next) {
316 gchar *name = cur->data;
317 if (grits_prefs_get_boolean_v(plugins->prefs, "plugins", name, NULL)) {
318 GritsPlugin *plugin = grits_plugins_load(plugins, name, viewer, prefs);
319 loaded = g_list_prepend(loaded, plugin);
326 * grits_plugins_unload:
327 * @plugins: the plugins source
328 * @name: the name of the plugin to unload
330 * Unload a plugin and free any associated data.
334 gboolean grits_plugins_unload(GritsPlugins *plugins, const char *name)
336 g_debug("GritsPlugins: unload %s", name);
337 for (GList *cur = plugins->plugins; cur; cur = cur->next) {
338 GritsPluginStore *store = cur->data;
339 if (g_str_equal(store->name, name)) {
340 g_object_unref(store->plugin);
343 plugins->plugins = g_list_delete_link(plugins->plugins, cur);
350 * grits_plugins_disable:
351 * @plugins: the plugins source
352 * @name: the name of the plugin to unload
354 * Unload a plugin and save it's loaded/unloaded state in the #GritsPrefs stored
357 * See also: grits_plugins_unload()
361 gboolean grits_plugins_disable(GritsPlugins *plugins, const char *name)
363 grits_prefs_set_boolean_v(plugins->prefs, "plugins", name, FALSE);
364 grits_plugins_unload(plugins, name);
369 * grits_plugins_foreach:
370 * @plugins: the plugins source
371 * @callback: a function to call on each plugin
372 * @user_data: user data to pass to the function
374 * Iterate over all plugins loaded by the plugins source
376 void grits_plugins_foreach(GritsPlugins *plugins, GCallback _callback, gpointer user_data)
378 g_debug("GritsPlugins: foreach");
381 typedef void (*CBFunc)(GritsPlugin *, const gchar *, gpointer);
382 CBFunc callback = (CBFunc)_callback;
383 for (GList *cur = plugins->plugins; cur; cur = cur->next) {
384 GritsPluginStore *store = cur->data;
385 callback(store->plugin, store->name, user_data);