]> Dogcows Code - chaz/openbox/blob - openbox/plugin.c
9b8e2d596c640b42cf6d74e7fcdd0b393c352ef8
[chaz/openbox] / openbox / plugin.c
1 #include "plugins/interface.h"
2 #include "parser/parse.h"
3
4 #include <glib.h>
5 #include <gmodule.h>
6
7 typedef struct {
8 GModule *module;
9 char *name;
10
11 PluginSetupConfig config;
12 PluginStartup startup;
13 PluginShutdown shutdown;
14 PluginCreate create;
15 PluginDestroy destroy;
16 } Plugin;
17
18 static gpointer load_sym(GModule *module, char *name, char *symbol,
19 gboolean allow_fail)
20 {
21 gpointer var;
22 if (!g_module_symbol(module, symbol, &var)) {
23 if (!allow_fail)
24 g_warning("Failed to load symbol '%s' from plugin '%s'",
25 symbol, name);
26 var = NULL;
27 }
28 return var;
29 }
30
31 static Plugin *plugin_new(char *name)
32 {
33 Plugin *p;
34 char *path;
35
36 p = g_new(Plugin, 1);
37
38 path = g_build_filename(g_get_home_dir(), ".openbox", "plugins", name,
39 NULL);
40 p->module = g_module_open(path, 0);
41 g_free(path);
42
43 if (p->module == NULL) {
44 path = g_build_filename(PLUGINDIR, name, NULL);
45 p->module = g_module_open(path, 0);
46 g_free(path);
47 }
48
49 if (p->module == NULL) {
50 g_warning(g_module_error());
51 g_free(p);
52 return NULL;
53 }
54
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",
58 FALSE);
59 p->shutdown = (PluginShutdown)load_sym(p->module, name, "plugin_shutdown",
60 FALSE);
61 p->create = (PluginCreate)load_sym(p->module, name, "plugin_create", TRUE);
62 p->destroy = (PluginDestroy)load_sym(p->module, name, "plugin_destroy",
63 TRUE);
64
65 if (p->config == NULL || p->startup == NULL || p->shutdown == NULL) {
66 g_module_close(p->module);
67 g_free(p);
68 return NULL;
69 }
70
71 p->name = g_strdup(name);
72 return p;
73 }
74
75 static void plugin_free(Plugin *p)
76 {
77 p->shutdown();
78
79 g_free(p->name);
80 g_module_close(p->module);
81 }
82
83
84 static GData *plugins = NULL;
85
86 void plugin_startup()
87 {
88 g_datalist_init(&plugins);
89 }
90
91 void plugin_shutdown()
92 {
93 g_datalist_clear(&plugins);
94 }
95
96 gboolean plugin_open_full(char *name, gboolean reopen, ObParseInst *i)
97 {
98 Plugin *p;
99
100 if (g_datalist_get_data(&plugins, name) != NULL) {
101 if (!reopen)
102 g_warning("plugin '%s' already loaded, can't load again", name);
103 return TRUE;
104 }
105
106 p = plugin_new(name);
107 if (p == NULL) {
108 g_warning("failed to load plugin '%s'", name);
109 return FALSE;
110 }
111 p->config(i);
112
113 g_datalist_set_data_full(&plugins, name, p, (GDestroyNotify) plugin_free);
114 return TRUE;
115 }
116
117 gboolean plugin_open(char *name, ObParseInst *i) {
118 return plugin_open_full(name, FALSE, i);
119 }
120
121 gboolean plugin_open_reopen(char *name, ObParseInst *i) {
122 return plugin_open_full(name, TRUE, i);
123 }
124
125 void plugin_close(char *name)
126 {
127 g_datalist_remove_data(&plugins, name);
128 }
129
130 static void foreach_start(GQuark key, Plugin *p, gpointer *foo)
131 {
132 p->startup();
133 }
134
135 void plugin_startall()
136 {
137 g_datalist_foreach(&plugins, (GDataForeachFunc)foreach_start, NULL);
138 }
139
140 void plugin_loadall(ObParseInst *i)
141 {
142 GIOChannel *io;
143 GError *err;
144 char *path, *name;
145
146 path = g_build_filename(g_get_home_dir(), ".openbox", "pluginrc", NULL);
147 err = NULL;
148 io = g_io_channel_new_file(path, "r", &err);
149 g_free(path);
150
151 if (io == NULL) {
152 path = g_build_filename(RCDIR, "pluginrc", NULL);
153 err = NULL;
154 io = g_io_channel_new_file(path, "r", &err);
155 g_free(path);
156 }
157
158 if (io == NULL) {
159 /* load the default plugins */
160 plugin_open("placement", i);
161 plugin_open("resistance", i);
162
163 /* XXX rm me when the parser loads me magically */
164 plugin_open("client_menu", i);
165 } else {
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) {
169 g_strstrip(name);
170 if (name[0] != '\0' && name[0] != '#')
171 plugin_open(name, i);
172 g_free(name);
173 }
174 g_io_channel_unref(io);
175 }
176 }
177
178 void *plugin_create(char *name, void *data)
179 {
180 Plugin *p = (Plugin *)g_datalist_get_data(&plugins, name);
181
182 if (p == NULL) {
183 g_warning("Unable to find plugin for create: %s", name);
184 return NULL;
185 }
186
187 if (p->create == NULL || p->destroy == NULL) {
188 g_critical("Unsupported create/destroy: %s", name);
189 return NULL;
190 }
191
192 return p->create(data);
193 }
194
195 void plugin_destroy(char *name, void *data)
196 {
197 Plugin *p = (Plugin *)g_datalist_get_data(&plugins, name);
198
199 if (p == NULL) {
200 g_critical("Unable to find plugin for destroy: %s", name);
201 /* really shouldn't happen, but attempt to free something anyway? */
202 g_free(data);
203 return;
204 }
205
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? */
209 g_free(data);
210 return;
211 }
212
213 p->destroy(data);
214 }
This page took 0.042707 seconds and 3 git commands to generate.