Eanble plugin support.
[lxde/lxappearance.git] / src / plugin.c
1 // plugin.c
2 //
3 // Copyright 2010 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "plugin.h"
25 #include "lxappearance.h"
26 #include <gmodule.h>
27
28 #define PLUGIN_DIR PACKAGE_LIB_DIR"/lxappearance/plugins"
29
30 typedef gboolean (*PluginLoadFunc)(LXAppearance*, GtkBuilder*);
31 typedef void (*PluginUnloadFunc)(LXAppearance*);
32
33 typedef struct _Plugin Plugin;
34 struct _Plugin
35 {
36 GModule* module;
37 PluginLoadFunc load;
38 PluginUnloadFunc unload;
39 };
40
41 static GSList* plugins = NULL;
42
43 void plugins_init(GtkBuilder* builder)
44 {
45 GDir* dir = g_dir_open(PLUGIN_DIR, 0, NULL);
46 char* name;
47 if(!dir)
48 return;
49 while(name = g_dir_read_name(dir))
50 {
51 char* file = g_build_filename(PLUGIN_DIR, name, NULL);
52 GModule* mod = g_module_open(file, G_MODULE_BIND_LOCAL);
53 if(mod)
54 {
55 PluginLoadFunc load;
56 gboolean loaded = FALSE;
57 if(g_module_symbol(mod, "plugin_load", &load))
58 loaded = load(&app, builder);
59 if(loaded)
60 {
61 Plugin* plugin = g_slice_new0(Plugin);
62 plugin->module = mod;
63 plugin->load = load;
64 g_module_symbol(mod, "plugin_unload", &plugin->unload);
65 plugins = g_slist_prepend(plugins, plugin);
66 g_debug("plugin: %s loadad", file);
67 }
68 else
69 g_module_close(mod);
70 }
71 g_free(file);
72 }
73 g_dir_close(dir);
74 }
75
76 void plugins_finalize()
77 {
78 GSList* l;
79 for(l = plugins; l; l=l->next)
80 {
81 Plugin* plugin = (Plugin*)l->data;
82 g_module_close(plugin->module);
83 g_slice_free(Plugin, plugin);
84 }
85 g_slist_free(plugins);
86 }