]> Dogcows Code - chaz/openbox/blob - openbox/plugin.c
clarify using stacking_lower for restoring the stacking order
[chaz/openbox] / openbox / plugin.c
1 #include <glib.h>
2 #include <gmodule.h>
3
4 typedef void (*PluginSetupConfig)();
5 typedef void (*PluginStartup)();
6 typedef void (*PluginShutdown)();
7
8 typedef struct {
9 GModule *module;
10 char *name;
11
12 PluginSetupConfig config;
13 PluginStartup startup;
14 PluginShutdown shutdown;
15 } Plugin;
16
17 static gpointer load_sym(GModule *module, char *name, char *symbol)
18 {
19 gpointer var;
20 if (!g_module_symbol(module, symbol, &var)) {
21 g_warning("Failed to load symbol '%s' from plugin '%s'",
22 symbol, name);
23 var = NULL;
24 }
25 return var;
26 }
27
28 static Plugin *plugin_new(char *name)
29 {
30 Plugin *p;
31 char *path;
32
33 p = g_new(Plugin, 1);
34
35 path = g_build_filename(g_get_home_dir(), ".openbox", "plugins", name,
36 NULL);
37 p->module = g_module_open(path, 0);
38 g_free(path);
39
40 if (p->module == NULL) {
41 path = g_build_filename(PLUGINDIR, name, NULL);
42 p->module = g_module_open(path, 0);
43 g_free(path);
44 }
45
46 if (p->module == NULL) {
47 g_free(p);
48 return NULL;
49 }
50
51 p->config = (PluginSetupConfig)load_sym(p->module, name,
52 "plugin_setup_config");
53 p->startup = (PluginStartup)load_sym(p->module, name, "plugin_startup");
54 p->shutdown = (PluginShutdown)load_sym(p->module, name, "plugin_shutdown");
55
56 if (p->config == NULL || p->startup == NULL || p->shutdown == NULL) {
57 g_module_close(p->module);
58 g_free(p);
59 return NULL;
60 }
61
62 p->name = g_strdup(name);
63 return p;
64 }
65
66 static void plugin_free(Plugin *p)
67 {
68 p->shutdown();
69
70 g_free(p->name);
71 g_module_close(p->module);
72 }
73
74
75 static GData *plugins = NULL;
76
77 void plugin_startup()
78 {
79 g_datalist_init(&plugins);
80 }
81
82 void plugin_shutdown()
83 {
84 g_datalist_clear(&plugins);
85 }
86
87 gboolean plugin_open(char *name)
88 {
89 Plugin *p;
90
91 if (g_datalist_get_data(&plugins, name) != NULL) {
92 g_warning("plugin '%s' already loaded, can't load again", name);
93 return TRUE;
94 }
95
96 p = plugin_new(name);
97 if (p == NULL) {
98 g_warning("failed to load plugin '%s'", name);
99 return FALSE;
100 }
101 p->config();
102
103 g_datalist_set_data_full(&plugins, name, p, (GDestroyNotify) plugin_free);
104 return TRUE;
105 }
106
107 void plugin_close(char *name)
108 {
109 g_datalist_remove_data(&plugins, name);
110 }
111
112 static void foreach_start(GQuark key, Plugin *p, gpointer *foo)
113 {
114 p->startup();
115 }
116
117 void plugin_startall()
118 {
119 g_datalist_foreach(&plugins, (GDataForeachFunc)foreach_start, NULL);
120 }
121
122 void plugin_loadall()
123 {
124 GIOChannel *io;
125 GError *err;
126 char *path, *name;
127
128 path = g_build_filename(g_get_home_dir(), ".openbox", "pluginrc", NULL);
129 err = NULL;
130 io = g_io_channel_new_file(path, "r", &err);
131 g_free(path);
132
133 if (io == NULL) {
134 path = g_build_filename(RCDIR, "pluginrc", NULL);
135 err = NULL;
136 io = g_io_channel_new_file(path, "r", &err);
137 g_free(path);
138 }
139
140 if (io == NULL) {
141 /* load the default plugins */
142 plugin_open("focus");
143 plugin_open("keyboard");
144 plugin_open("mouse");
145 plugin_open("placement");
146 plugin_open("resistance");
147 } else {
148 /* load the plugins in the rc file */
149 while (g_io_channel_read_line(io, &name, NULL, NULL, &err) ==
150 G_IO_STATUS_NORMAL) {
151 g_strstrip(name);
152 plugin_open(name);
153 g_free(name);
154 }
155 g_io_channel_unref(io);
156 }
157 }
This page took 0.039057 seconds and 4 git commands to generate.