]> Dogcows Code - chaz/openbox/blob - openbox/plugin.c
move the resistance plugin into the kernel. dont resist when move/resizing with the...
[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
162 /* XXX rm me when the parser loads me magically */
163 plugin_open("client_menu", i);
164 } else {
165 /* load the plugins in the rc file */
166 while (g_io_channel_read_line(io, &name, NULL, NULL, &err) ==
167 G_IO_STATUS_NORMAL) {
168 g_strstrip(name);
169 if (name[0] != '\0' && name[0] != '#')
170 plugin_open(name, i);
171 g_free(name);
172 }
173 g_io_channel_unref(io);
174 }
175 }
176
177 void *plugin_create(char *name, void *data)
178 {
179 Plugin *p = (Plugin *)g_datalist_get_data(&plugins, name);
180
181 if (p == NULL) {
182 g_warning("Unable to find plugin for create: %s", name);
183 return NULL;
184 }
185
186 if (p->create == NULL || p->destroy == NULL) {
187 g_critical("Unsupported create/destroy: %s", name);
188 return NULL;
189 }
190
191 return p->create(data);
192 }
193
194 void plugin_destroy(char *name, void *data)
195 {
196 Plugin *p = (Plugin *)g_datalist_get_data(&plugins, name);
197
198 if (p == NULL) {
199 g_critical("Unable to find plugin for destroy: %s", name);
200 /* really shouldn't happen, but attempt to free something anyway? */
201 g_free(data);
202 return;
203 }
204
205 if (p->destroy == NULL || p->create == NULL) {
206 g_critical("Unsupported create/destroy: %s", name);
207 /* really, really shouldn't happen, but attempt to free anyway? */
208 g_free(data);
209 return;
210 }
211
212 p->destroy(data);
213 }
This page took 0.041151 seconds and 4 git commands to generate.