]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/plugin.c
change the menu plugin interface, no need for the create/destroy functions any more.
[mikachu/openbox.git] / 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 } Plugin;
15
16 static gpointer load_sym(GModule *module, char *name, char *symbol,
17                          gboolean allow_fail)
18 {
19     gpointer var;
20     if (!g_module_symbol(module, symbol, &var)) {
21         if (!allow_fail)
22             g_warning("Failed to load symbol '%s' from plugin '%s'",
23                       symbol, name);
24         var = NULL;
25     }
26     return var;
27 }
28
29 static Plugin *plugin_new(char *name)
30 {
31     Plugin *p;
32     char *path;
33    
34     p = g_new(Plugin, 1);
35
36     path = g_build_filename(g_get_home_dir(), ".openbox", "plugins", name,
37                             NULL);
38     p->module = g_module_open(path, 0);
39     g_free(path);
40
41     if (p->module == NULL) {
42         path = g_build_filename(PLUGINDIR, name, NULL);
43         p->module = g_module_open(path, 0);
44         g_free(path);
45     }
46
47     if (p->module == NULL) {
48         g_warning(g_module_error());
49         g_free(p);
50         return NULL;
51     }
52
53     p->config = (PluginSetupConfig)load_sym(p->module, name,
54                                             "plugin_setup_config", FALSE);
55     p->startup = (PluginStartup)load_sym(p->module, name, "plugin_startup",
56                                          FALSE);
57     p->shutdown = (PluginShutdown)load_sym(p->module, name, "plugin_shutdown",
58                                            FALSE);
59
60     if (p->config == NULL || p->startup == NULL || p->shutdown == NULL) {
61         g_module_close(p->module);
62         g_free(p);
63         return NULL;
64     }
65
66     p->name = g_strdup(name);
67     return p;
68 }
69
70 static void plugin_free(Plugin *p)
71 {
72     p->shutdown();
73
74     g_free(p->name);
75     g_module_close(p->module);
76 }
77
78
79 static GData *plugins = NULL;
80
81 void plugin_startup()
82 {
83     g_datalist_init(&plugins);
84 }
85
86 void plugin_shutdown()
87 {
88     g_datalist_clear(&plugins);
89 }
90
91 gboolean plugin_open_full(char *name, gboolean reopen, ObParseInst *i)
92 {
93     Plugin *p;
94
95     if (g_datalist_get_data(&plugins, name) != NULL) {
96         if (!reopen) 
97             g_warning("plugin '%s' already loaded, can't load again", name);
98         return TRUE;
99     }
100
101     p = plugin_new(name);
102     if (p == NULL) {
103         g_warning("failed to load plugin '%s'", name);
104         return FALSE;
105     }
106     p->config(i);
107
108     g_datalist_set_data_full(&plugins, name, p, (GDestroyNotify) plugin_free);
109     return TRUE;
110 }
111
112 gboolean plugin_open(char *name, ObParseInst *i) {
113     return plugin_open_full(name, FALSE, i);
114 }
115
116 gboolean plugin_open_reopen(char *name, ObParseInst *i) {
117     return plugin_open_full(name, TRUE, i);
118 }
119
120 void plugin_close(char *name)
121 {
122     g_datalist_remove_data(&plugins, name);
123 }
124
125 static void foreach_start(GQuark key, Plugin *p, gpointer *foo)
126 {
127     p->startup();
128 }
129
130 void plugin_startall()
131 {
132     g_datalist_foreach(&plugins, (GDataForeachFunc)foreach_start, NULL);
133 }
134
135 void plugin_loadall(ObParseInst *i)
136 {
137     GIOChannel *io;
138     GError *err;
139     char *path, *name;
140
141     path = g_build_filename(g_get_home_dir(), ".openbox", "pluginrc", NULL);
142     err = NULL;
143     io = g_io_channel_new_file(path, "r", &err);
144     g_free(path);
145
146     if (io == NULL) {
147         path = g_build_filename(RCDIR, "pluginrc", NULL);
148         err = NULL;
149         io = g_io_channel_new_file(path, "r", &err);
150         g_free(path);
151     }
152
153     if (io == NULL) {
154         /* load the default plugins */
155         plugin_open("placement", i);
156
157         /* XXX rm me when the parser loads me magically */
158         plugin_open("client_menu", i);
159         plugin_open("client_list_menu", i);
160     } else {
161         /* load the plugins in the rc file */
162         while (g_io_channel_read_line(io, &name, NULL, NULL, &err) ==
163                G_IO_STATUS_NORMAL) {
164             g_strstrip(name);
165             if (name[0] != '\0' && name[0] != '#')
166                 plugin_open(name, i);
167             g_free(name);
168         }
169         g_io_channel_unref(io);
170     }
171 }