]> Dogcows Code - chaz/openbox/blob - openbox/plugin.c
add functions to the engine interface for rendering and sizing a text label
[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_warning(g_module_error());
48 g_free(p);
49 return NULL;
50 }
51
52 p->config = (PluginSetupConfig)load_sym(p->module, name,
53 "plugin_setup_config");
54 p->startup = (PluginStartup)load_sym(p->module, name, "plugin_startup");
55 p->shutdown = (PluginShutdown)load_sym(p->module, name, "plugin_shutdown");
56
57 if (p->config == NULL || p->startup == NULL || p->shutdown == NULL) {
58 g_module_close(p->module);
59 g_free(p);
60 return NULL;
61 }
62
63 p->name = g_strdup(name);
64 return p;
65 }
66
67 static void plugin_free(Plugin *p)
68 {
69 p->shutdown();
70
71 g_free(p->name);
72 g_module_close(p->module);
73 }
74
75
76 static GData *plugins = NULL;
77
78 void plugin_startup()
79 {
80 g_datalist_init(&plugins);
81 }
82
83 void plugin_shutdown()
84 {
85 g_datalist_clear(&plugins);
86 }
87
88 gboolean plugin_open(char *name)
89 {
90 Plugin *p;
91
92 if (g_datalist_get_data(&plugins, name) != NULL) {
93 g_warning("plugin '%s' already loaded, can't load again", name);
94 return TRUE;
95 }
96
97 p = plugin_new(name);
98 if (p == NULL) {
99 g_warning("failed to load plugin '%s'", name);
100 return FALSE;
101 }
102 p->config();
103
104 g_datalist_set_data_full(&plugins, name, p, (GDestroyNotify) plugin_free);
105 return TRUE;
106 }
107
108 void plugin_close(char *name)
109 {
110 g_datalist_remove_data(&plugins, name);
111 }
112
113 static void foreach_start(GQuark key, Plugin *p, gpointer *foo)
114 {
115 p->startup();
116 }
117
118 void plugin_startall()
119 {
120 g_datalist_foreach(&plugins, (GDataForeachFunc)foreach_start, NULL);
121 }
122
123 void plugin_loadall()
124 {
125 GIOChannel *io;
126 GError *err;
127 char *path, *name;
128
129 path = g_build_filename(g_get_home_dir(), ".openbox", "pluginrc", NULL);
130 err = NULL;
131 io = g_io_channel_new_file(path, "r", &err);
132 g_free(path);
133
134 if (io == NULL) {
135 path = g_build_filename(RCDIR, "pluginrc", NULL);
136 err = NULL;
137 io = g_io_channel_new_file(path, "r", &err);
138 g_free(path);
139 }
140
141 if (io == NULL) {
142 /* load the default plugins */
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 if (name[0] != '\0' && name[0] != '#')
153 plugin_open(name);
154 g_free(name);
155 }
156 g_io_channel_unref(io);
157 }
158 }
This page took 0.041363 seconds and 4 git commands to generate.