]> Dogcows Code - chaz/openbox/blob - plugins/menu/timed_menu.c
Some fixes and new bugs. Someone else can do the menus because it is
[chaz/openbox] / plugins / menu / timed_menu.c
1 /*
2 * $Header$
3 *
4 * Timed menu plugin
5 * Provides a menu from either a periodically executing process or by
6 * periodically checking the timestamp of a file and loading if it has changed.
7 *
8 * Examples:
9 * Piped timed menu:
10 * rc3:
11 * <menu id="name" label="Piped menu" plugin="timed_menu" timeout="120"
12 * command="obcommand.py -c bsetbg -d ~/.openbox/backgrounds/" />
13 * timeout is in seconds
14 *
15 * Output of command:
16 * <timed_menu>
17 * <item label="GLOVE.png">
18 * <action name="execute">
19 * <execute>
20 * bsetbg "/home/woodblock/.openbox/backgrounds/GLOVE.png"
21 * </execute>
22 * </action>
23 * </item>
24 * </fifo_menu>
25 *
26 * stat() menu:
27 * <menu id="name" label="Stat menu" plugin="timed_menu" type="stat"
28 * timeout="120" file="~/.openbox/stat_menu" />
29 * stat_menu contents: same as timed menu
30 *
31 */
32
33 #include <glib.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42 #include <sys/stat.h>
43
44 #include "kernel/menu.h"
45 #include "kernel/timer.h"
46 #include "timed_menu.h"
47 #include "kernel/action.h"
48 #include "kernel/event.h"
49
50 #define TIMED_MENU(m) ((ObMenu *)m)
51 #define TIMED_MENU_DATA(m) ((Timed_Menu_Data *)((ObMenu *)m)->plugin_data)
52 static char *PLUGIN_NAME = "timed_menu";
53
54 typedef enum {
55 TIMED_MENU_PIPE, /* read entries from a child process' output */
56 TIMED_MENU_STAT /* reread entries from file when timestamp changes */
57 } Timed_Menu_Type;
58
59 typedef struct {
60 Timed_Menu_Type type;
61 ObTimer *timer;
62 char *command; /* command to run or file to stat() */
63 char *buf; /* buffer to hold partially read menu */
64 unsigned long buflen; /* how many bytes are in the buffer */
65 int fd; /* file descriptor to read menu from */
66 pid_t pid; /* pid of child process in PIPE */
67 time_t mtime; /* time of last modification */
68 } Timed_Menu_Data;
69
70
71 void plugin_setup_config() { }
72 void plugin_startup()
73 { }
74 void plugin_shutdown() { }
75
76 void timed_menu_clean_up(ObMenu *m) {
77 if (TIMED_MENU_DATA(m)->buf != NULL) {
78 g_free(TIMED_MENU_DATA(m)->buf);
79 TIMED_MENU_DATA(m)->buf = NULL;
80 }
81
82 TIMED_MENU_DATA(m)->buflen = 0;
83
84 if (TIMED_MENU_DATA(m)->fd != -1) {
85 event_remove_fd(TIMED_MENU_DATA(m)->fd);
86 close(TIMED_MENU_DATA(m)->fd);
87 TIMED_MENU_DATA(m)->fd = -1;
88 }
89
90 if (TIMED_MENU_DATA(m)->pid != -1) {
91 waitpid(TIMED_MENU_DATA(m)->pid, NULL, 0);
92 TIMED_MENU_DATA(m)->pid = -1;
93 }
94 }
95
96 void timed_menu_read_pipe(int fd, void *d)
97 {
98 ObMenu *menu = d;
99 char *tmpbuf = NULL;
100 unsigned long num_read;
101 #ifdef DEBUG
102 /* because gdb is dumb */
103 #if 0
104 Timed_Menu_Data *d = TIMED_MENU_DATA(menu);
105 #endif
106 #endif
107
108 unsigned long num_realloc;
109 /* if we have less than a quarter BUFSIZ left, allocate more */
110 num_realloc = (BUFSIZ - (TIMED_MENU_DATA(menu)->buflen % BUFSIZ) <
111 BUFSIZ >> 2) ?
112 0 : BUFSIZ;
113
114 tmpbuf = g_try_realloc(TIMED_MENU_DATA(menu)->buf,
115 TIMED_MENU_DATA(menu)->buflen + num_realloc);
116
117 if (tmpbuf == NULL) {
118 g_warning("Unable to allocate memory for read()");
119 return;
120 }
121
122 TIMED_MENU_DATA(menu)->buf = tmpbuf;
123
124 num_read = read(fd,
125 TIMED_MENU_DATA(menu)->buf + TIMED_MENU_DATA(menu)->buflen,
126 num_realloc);
127 if (num_read == 0) {
128 xmlDocPtr doc;
129 xmlNodePtr node;
130
131 menu->invalid = TRUE;
132 menu_clear(menu);
133
134 TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
135
136 doc = xmlParseMemory(TIMED_MENU_DATA(menu)->buf,
137 TIMED_MENU_DATA(menu)->buflen);
138
139 node = xmlDocGetRootElement(doc);
140
141 if (node &&
142 !xmlStrcasecmp(node->name, (const xmlChar*) "timed_menu")) {
143 parse_menu_full(doc, node, menu, FALSE);
144 }
145
146 timed_menu_clean_up(menu);
147 } else if (num_read > 0) {
148 TIMED_MENU_DATA(menu)->buflen += num_read;
149 TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
150 } else { /* num_read < 1 */
151 g_warning("Error on read %s", strerror(errno));
152 timed_menu_clean_up(menu);
153 }
154 }
155
156 void timed_menu_timeout_handler(ObTimer *t, void *d)
157 {
158 ObMenu *data = d;
159 if (!data->shown && TIMED_MENU_DATA(data)->fd == -1) {
160 switch (TIMED_MENU_DATA(data)->type) {
161 case (TIMED_MENU_PIPE): {
162 /* if the menu is not shown, run a process and use its output
163 as menu */
164
165 /* I hate you glib in all your hideous forms */
166 char *args[4];
167 int child_stdout;
168 int child_pid;
169 args[0] = "/bin/sh";
170 args[1] = "-c";
171 args[2] = TIMED_MENU_DATA(data)->command;
172 args[3] = NULL;
173 if (g_spawn_async_with_pipes(
174 NULL,
175 args,
176 NULL,
177 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
178 NULL,
179 NULL,
180 &child_pid,
181 NULL,
182 &child_stdout,
183 NULL,
184 NULL)) {
185 event_fd_handler *h = g_new(event_fd_handler, 1);
186 TIMED_MENU_DATA(data)->fd = h->fd = child_stdout;
187 TIMED_MENU_DATA(data)->pid = child_pid;
188 h->handler = timed_menu_read_pipe;
189 h->data = data;
190 event_add_fd_handler(h);
191 } else {
192 g_warning("unable to spawn child");
193 }
194 break;
195 }
196
197 case (TIMED_MENU_STAT): {
198 struct stat stat_buf;
199
200 if (stat(TIMED_MENU_DATA(data)->command, &stat_buf) == -1) {
201 g_warning("Unable to stat %s: %s",
202 TIMED_MENU_DATA(data)->command,
203 strerror(errno));
204 break;
205 }
206
207 if (stat_buf.st_mtime > TIMED_MENU_DATA(data)->mtime) {
208 xmlDocPtr doc;
209 xmlNodePtr node;
210
211 g_warning("file changed");
212 TIMED_MENU_DATA(data)->mtime = stat_buf.st_mtime;
213
214 data->invalid = TRUE;
215 menu_clear(data);
216
217 doc = xmlParseFile(TIMED_MENU_DATA(data)->command);
218
219 node = xmlDocGetRootElement(doc);
220
221 if (node &&
222 !xmlStrcasecmp(node->name, (const xmlChar*) "timed_menu")) {
223 parse_menu_full(doc, node, data, FALSE);
224 }
225
226 timed_menu_clean_up(data);
227 }
228 }
229 }
230 }
231 }
232
233 void *plugin_create(PluginMenuCreateData *data)
234 {
235 char *id;
236 char *label;
237 char *timeout;
238 char *type;
239
240 Timed_Menu_Data *d;
241 ObMenu *m;
242
243 parse_attr_string("id", data->node, &id);
244 parse_attr_string("label", data->node, &label);
245
246 d = g_new(Timed_Menu_Data, 1);
247
248 m = menu_new( (label != NULL ? label : ""),
249 (id != NULL ? id : PLUGIN_NAME),
250 data->parent);
251
252 m->plugin = PLUGIN_NAME;
253
254 if (data->parent)
255 menu_add_entry(data->parent, menu_entry_new_submenu(
256 (label != NULL ? label : ""),
257 m));
258
259 d->type = TIMED_MENU_PIPE;
260
261 if (parse_attr_string("type", data->node, &type) &&
262 !g_strcasecmp(type, "stat")) {
263 d->type = TIMED_MENU_STAT;
264
265 if (!parse_attr_string("file", data->node, &d->command)) {
266 d->command = g_strdup("");
267 }
268 } else
269 if (!parse_attr_string("command", data->node, &d->command)) {
270 d->command = g_strdup("");
271 }
272
273 if (parse_attr_string("timeout", data->node, &timeout)) {
274 char *endptr;
275 gdouble timeout_val = g_strtod(timeout, &endptr);
276 g_free(timeout);
277 d->timer = timer_start(timeout_val * 1000000,
278 &timed_menu_timeout_handler, m);
279 } else
280 d->timer = timer_start(600 * 1000000, &timed_menu_timeout_handler, m);
281
282 d->buf = NULL;
283 d->buflen = 0;
284 d->fd = -1;
285 d->pid = -1;
286 d->mtime = 0;
287
288 m->plugin_data = (void *)d;
289
290 timed_menu_timeout_handler(NULL, m);
291 return (void *)m;
292 }
293
294 void plugin_destroy (void *m)
295 {
296 timed_menu_clean_up(m);
297 /* this will be freed by timer_* */
298 timer_stop( ((Timed_Menu_Data *)TIMED_MENU(m)->plugin_data)->timer);
299
300 g_free( TIMED_MENU(m)->plugin_data );
301 }
This page took 0.047439 seconds and 4 git commands to generate.