]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/menu/timed_menu.c
Timed menu updates. Still needs work.
[mikachu/openbox.git] / plugins / menu / timed_menu.c
1 #include <glib.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 #include <sys/stat.h>
11
12 #include "kernel/menu.h"
13 #include "kernel/timer.h"
14 #include "timed_menu.h"
15 #include "kernel/action.h"
16 #include "kernel/event.h"
17
18 #define TIMED_MENU(m) ((ObMenu *)m)
19 #define TIMED_MENU_DATA(m) ((Timed_Menu_Data *)((ObMenu *)m)->plugin_data)
20 static char *PLUGIN_NAME = "timed_menu";
21
22 typedef enum {
23     TIMED_MENU_PIPE, /* read entries from a child process' output */
24     TIMED_MENU_STAT  /* reread entries from file when timestamp changes */
25 } Timed_Menu_Type;
26
27 typedef struct {
28     Timed_Menu_Type type;
29     ObTimer *timer;
30     char *command; /* for the PIPE */
31     char *buf; /* buffer to hold partially read menu */
32     unsigned long buflen; /* how many bytes are in the buffer */
33     int fd; /* file descriptor to read menu from */
34     pid_t pid; /* pid of child process in PIPE */
35     time_t mtime; /* time of last modification */
36 } Timed_Menu_Data;
37
38
39 void plugin_setup_config() { }
40 void plugin_startup()
41 { }
42 void plugin_shutdown() { }
43
44 void timed_menu_clean_up(ObMenu *m) {
45     if (TIMED_MENU_DATA(m)->buf != NULL) {
46         fprintf(stderr, "%s", TIMED_MENU_DATA(m)->buf);
47         g_free(TIMED_MENU_DATA(m)->buf);
48         TIMED_MENU_DATA(m)->buf = NULL;
49     }
50
51     TIMED_MENU_DATA(m)->buflen = 0;
52
53     if (TIMED_MENU_DATA(m)->fd != -1) {
54         event_remove_fd(TIMED_MENU_DATA(m)->fd);
55         close(TIMED_MENU_DATA(m)->fd);
56         TIMED_MENU_DATA(m)->fd = -1;
57     }
58
59     if (TIMED_MENU_DATA(m)->pid != -1) {
60         waitpid(TIMED_MENU_DATA(m)->pid, NULL, 0);
61         TIMED_MENU_DATA(m)->pid = -1;
62     }
63
64     TIMED_MENU_DATA(m)->mtime = 0;
65 }
66
67 void timed_menu_read_pipe(int fd, void *d)
68 {
69     ObMenu *menu = d;
70     char *tmpbuf = NULL;
71     unsigned long num_read;
72 #ifdef DEBUG
73     /* because gdb is dumb */
74 #if 0
75     Timed_Menu_Data *d = TIMED_MENU_DATA(menu);
76 #endif
77 #endif
78
79     unsigned long num_realloc;
80     /* if we have less than a quarter BUFSIZ left, allocate more */
81     num_realloc = (BUFSIZ - (TIMED_MENU_DATA(menu)->buflen % BUFSIZ) <
82                    BUFSIZ >> 2) ?
83                   0 : BUFSIZ;
84     
85     tmpbuf = g_try_realloc(TIMED_MENU_DATA(menu)->buf,             
86                            TIMED_MENU_DATA(menu)->buflen + num_realloc);
87
88     if (tmpbuf == NULL) {
89         g_warning("Unable to allocate memory for read()");
90         return;
91     }
92     
93     TIMED_MENU_DATA(menu)->buf = tmpbuf;
94     
95     num_read = read(fd,
96                     TIMED_MENU_DATA(menu)->buf + TIMED_MENU_DATA(menu)->buflen,
97                     num_realloc);
98     if (num_read == 0) {
99          menu->invalid = TRUE;
100         menu_clear(menu);
101
102         TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
103
104         xmlDocPtr doc = xmlParseMemory(TIMED_MENU_DATA(menu)->buf,
105                                        TIMED_MENU_DATA(menu)->buflen);
106
107         xmlNodePtr node = xmlDocGetRootElement(doc);
108
109         if (!xmlStrcasecmp(node->name, (const xmlChar*) "timed_menu")) {
110             if ((node = parse_find_node("item", node->xmlChildrenNode)))
111                 parse_menu_full(doc, node, menu, FALSE);
112         }
113
114         timed_menu_clean_up(menu);
115     } else if (num_read > 0) {
116         TIMED_MENU_DATA(menu)->buflen += num_read;
117         TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
118     } else { /* num_read < 1 */
119         g_warning("Error on read %s", strerror(errno));
120         timed_menu_clean_up(menu);
121     }
122 }
123
124 void timed_menu_timeout_handler(void *d)
125 {
126     ObMenu *data = d;
127     if (!data->shown && TIMED_MENU_DATA(data)->fd == -1) {
128         switch (TIMED_MENU_DATA(data)->type) {
129             case (TIMED_MENU_PIPE): {
130                 /* if the menu is not shown, run a process and use its output
131                    as menu */
132
133                 /* I hate you glib in all your hideous forms */
134                 char *args[4];
135                 int child_stdout;
136                 int child_pid;
137                 args[0] = "/bin/sh";
138                 args[1] = "-c";
139                 args[2] = TIMED_MENU_DATA(data)->command;
140                 args[3] = NULL;
141                 if (g_spawn_async_with_pipes(
142                         NULL,
143                         args,
144                         NULL,
145                         G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
146                         NULL,
147                         NULL,
148                         &child_pid,
149                         NULL,
150                         &child_stdout,
151                         NULL,
152                         NULL)) {
153                     event_fd_handler *h = g_new(event_fd_handler, 1);
154                     TIMED_MENU_DATA(data)->fd = h->fd = child_stdout;
155                     TIMED_MENU_DATA(data)->pid = child_pid;
156                     h->handler = timed_menu_read_pipe;
157                     h->data = data;
158                     event_add_fd_handler(h);
159                 } else {
160                     g_warning("unable to spawn child");
161                 }
162                 break;
163             }
164
165             case (TIMED_MENU_STAT): {
166                 struct stat stat_buf;
167
168                 if (stat(TIMED_MENU_DATA(data)->command, &stat_buf) == -1) {
169                     g_warning("Unable to stat %s: %s",
170                               TIMED_MENU_DATA(data)->command,
171                               strerror(errno));
172                     break;
173                 }
174
175                 if (stat_buf.st_mtime > TIMED_MENU_DATA(data)->mtime) {
176                     g_warning("file changed");
177                     TIMED_MENU_DATA(data)->mtime = stat_buf.st_mtime;
178                     /* TODO: parse */
179                 }
180             }
181         }
182     }
183 }
184
185 void *plugin_create(PluginMenuCreateData *data)
186 {
187     char *id;
188     char *label;
189     char *timeout;
190     Timed_Menu_Data *d;
191     ObMenu *m;
192     
193     parse_attr_string("id", data->node, &id);
194     parse_attr_string("label", data->node, &label);
195     
196     d = g_new(Timed_Menu_Data, 1);
197         
198     m = menu_new( (label != NULL ? label : ""),
199                   (id != NULL ? id : PLUGIN_NAME),
200                   data->parent);
201
202     m->plugin = PLUGIN_NAME;
203
204     if (data->parent)
205         menu_add_entry(data->parent, menu_entry_new_submenu(
206                            (label != NULL ? label : ""),
207                            m));
208
209     if (!parse_attr_string("command", data->node, &d->command)) {
210         d->command = g_strdup("");
211     }
212
213     if (parse_attr_string("timeout", data->node, &timeout)) {
214         char *endptr;
215         gdouble timeout_val = g_strtod(timeout, &endptr);
216         g_free(timeout);
217         d->timer = timer_start(timeout_val * 1000000,
218                                &timed_menu_timeout_handler, m);
219     } else
220         d->timer = timer_start(600 * 1000000, &timed_menu_timeout_handler, m);
221
222     d->type = TIMED_MENU_PIPE;
223     d->buf = NULL;
224     d->buflen = 0;
225     d->fd = -1;
226     d->pid = -1;
227     d->mtime = 0;
228     
229     m->plugin_data = (void *)d;
230
231     timed_menu_timeout_handler(m);
232     return (void *)m;
233 }
234
235 void plugin_destroy (void *m)
236 {
237     timed_menu_clean_up(m);
238     /* this will be freed by timer_* */
239     timer_stop( ((Timed_Menu_Data *)TIMED_MENU(m)->plugin_data)->timer);
240     
241     g_free( TIMED_MENU(m)->plugin_data );
242 }