1 #include "plugins/interface.h"
2 #include "parser/parse.h"
11 PluginSetupConfig config;
12 PluginStartup startup;
13 PluginShutdown shutdown;
15 PluginDestroy destroy;
18 static gpointer load_sym(GModule *module, char *name, char *symbol,
22 if (!g_module_symbol(module, symbol, &var)) {
24 g_warning("Failed to load symbol '%s' from plugin '%s'",
31 static Plugin *plugin_new(char *name)
38 path = g_build_filename(g_get_home_dir(), ".openbox", "plugins", name,
40 p->module = g_module_open(path, 0);
43 if (p->module == NULL) {
44 path = g_build_filename(PLUGINDIR, name, NULL);
45 p->module = g_module_open(path, 0);
49 if (p->module == NULL) {
50 g_warning(g_module_error());
55 p->config = (PluginSetupConfig)load_sym(p->module, name,
56 "plugin_setup_config", FALSE);
57 p->startup = (PluginStartup)load_sym(p->module, name, "plugin_startup",
59 p->shutdown = (PluginShutdown)load_sym(p->module, name, "plugin_shutdown",
61 p->create = (PluginCreate)load_sym(p->module, name, "plugin_create", TRUE);
62 p->destroy = (PluginDestroy)load_sym(p->module, name, "plugin_destroy",
65 if (p->config == NULL || p->startup == NULL || p->shutdown == NULL) {
66 g_module_close(p->module);
71 p->name = g_strdup(name);
75 static void plugin_free(Plugin *p)
80 g_module_close(p->module);
84 static GData *plugins = NULL;
88 g_datalist_init(&plugins);
91 void plugin_shutdown()
93 g_datalist_clear(&plugins);
96 gboolean plugin_open_full(char *name, gboolean reopen, ObParseInst *i)
100 if (g_datalist_get_data(&plugins, name) != NULL) {
102 g_warning("plugin '%s' already loaded, can't load again", name);
106 p = plugin_new(name);
108 g_warning("failed to load plugin '%s'", name);
113 g_datalist_set_data_full(&plugins, name, p, (GDestroyNotify) plugin_free);
117 gboolean plugin_open(char *name, ObParseInst *i) {
118 return plugin_open_full(name, FALSE, i);
121 gboolean plugin_open_reopen(char *name, ObParseInst *i) {
122 return plugin_open_full(name, TRUE, i);
125 void plugin_close(char *name)
127 g_datalist_remove_data(&plugins, name);
130 static void foreach_start(GQuark key, Plugin *p, gpointer *foo)
135 void plugin_startall()
137 g_datalist_foreach(&plugins, (GDataForeachFunc)foreach_start, NULL);
140 void plugin_loadall(ObParseInst *i)
146 path = g_build_filename(g_get_home_dir(), ".openbox", "pluginrc", NULL);
148 io = g_io_channel_new_file(path, "r", &err);
152 path = g_build_filename(RCDIR, "pluginrc", NULL);
154 io = g_io_channel_new_file(path, "r", &err);
159 /* load the default plugins */
160 plugin_open("placement", i);
162 /* XXX rm me when the parser loads me magically */
163 plugin_open("client_menu", i);
164 plugin_open("client_list_menu", i);
166 /* load the plugins in the rc file */
167 while (g_io_channel_read_line(io, &name, NULL, NULL, &err) ==
168 G_IO_STATUS_NORMAL) {
170 if (name[0] != '\0' && name[0] != '#')
171 plugin_open(name, i);
174 g_io_channel_unref(io);
178 void *plugin_create(char *name, void *data)
180 Plugin *p = (Plugin *)g_datalist_get_data(&plugins, name);
183 g_warning("Unable to find plugin for create: %s", name);
187 if (p->create == NULL || p->destroy == NULL) {
188 g_critical("Unsupported create/destroy: %s", name);
192 return p->create(data);
195 void plugin_destroy(char *name, void *data)
197 Plugin *p = (Plugin *)g_datalist_get_data(&plugins, name);
200 g_critical("Unable to find plugin for destroy: %s", name);
201 /* really shouldn't happen, but attempt to free something anyway? */
206 if (p->destroy == NULL || p->create == NULL) {
207 g_critical("Unsupported create/destroy: %s", name);
208 /* really, really shouldn't happen, but attempt to free anyway? */