]> Dogcows Code - chaz/openbox/blob - plugins/menu/timed_menu.c
Clean up on destroy.
[chaz/openbox] / 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) ((Menu *)m)
19 #define TIMED_MENU_DATA(m) ((Timed_Menu_Data *)((Menu *)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 Timer *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(Menu *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, Menu *menu)
68 {
69 char *tmpbuf = NULL;
70 unsigned long num_read;
71 #ifdef DEBUG
72 /* because gdb is dumb */
73 Timed_Menu_Data *d = TIMED_MENU_DATA(menu);
74 #endif
75
76 unsigned long num_realloc;
77 /* if we have less than a quarter BUFSIZ left, allocate more */
78 num_realloc = (BUFSIZ - (TIMED_MENU_DATA(menu)->buflen % BUFSIZ) <
79 BUFSIZ >> 2) ?
80 0 : BUFSIZ;
81
82 tmpbuf = g_try_realloc(TIMED_MENU_DATA(menu)->buf,
83 TIMED_MENU_DATA(menu)->buflen + num_realloc);
84
85 if (tmpbuf == NULL) {
86 g_warning("Unable to allocate memory for read()");
87 return;
88 }
89
90 TIMED_MENU_DATA(menu)->buf = tmpbuf;
91
92 num_read = read(fd,
93 TIMED_MENU_DATA(menu)->buf + TIMED_MENU_DATA(menu)->buflen,
94 num_realloc);
95 if (num_read == 0) {
96 unsigned long count = 0;
97 char *found = NULL;
98 menu->invalid = TRUE;
99 menu_clear(menu);
100
101 /* TEMP: list them */
102 while (NULL !=
103 (found = strchr(&TIMED_MENU_DATA(menu)->buf[count], '\n'))) {
104 TIMED_MENU_DATA(menu)->buf
105 [found - TIMED_MENU_DATA(menu)->buf] = '\0';
106 menu_add_entry(menu,
107 menu_entry_new_separator
108 (&TIMED_MENU_DATA(menu)->buf[count]));
109 count = found - TIMED_MENU_DATA(menu)->buf + 1;
110 }
111
112 TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
113 timed_menu_clean_up(menu);
114 } else if (num_read > 0) {
115 TIMED_MENU_DATA(menu)->buflen += num_read;
116 TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
117 } else { /* num_read < 1 */
118 g_warning("Error on read %s", strerror(errno));
119 timed_menu_clean_up(menu);
120 }
121 }
122
123 void timed_menu_timeout_handler(Menu *data)
124 {
125 Action *a;
126
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[] = {"/bin/sh", "-c", TIMED_MENU_DATA(data)->command,
135 NULL};
136 int child_stdout;
137 int child_pid;
138 if (g_spawn_async_with_pipes(
139 NULL,
140 args,
141 NULL,
142 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
143 NULL,
144 NULL,
145 &child_pid,
146 NULL,
147 &child_stdout,
148 NULL,
149 NULL)) {
150 event_fd_handler *h = g_new(event_fd_handler, 1);
151 TIMED_MENU_DATA(data)->fd = h->fd = child_stdout;
152 TIMED_MENU_DATA(data)->pid = child_pid;
153 h->handler = timed_menu_read_pipe;
154 h->data = data;
155 event_add_fd_handler(h);
156 } else {
157 g_warning("unable to spawn child");
158 }
159 break;
160 }
161
162 case (TIMED_MENU_STAT): {
163 struct stat stat_buf;
164
165 if (stat(TIMED_MENU_DATA(data)->command, &stat_buf) == -1) {
166 g_warning("Unable to stat %s: %s",
167 TIMED_MENU_DATA(data)->command,
168 strerror(errno));
169 break;
170 }
171
172 if (stat_buf.st_mtime > TIMED_MENU_DATA(data)->mtime) {
173 g_warning("file changed");
174 TIMED_MENU_DATA(data)->mtime = stat_buf.st_mtime;
175 /* TODO: parse */
176 }
177 }
178 }
179 }
180 }
181
182 void *plugin_create()
183 {
184 Timed_Menu_Data *d = g_new(Timed_Menu_Data, 1);
185 Menu *m = menu_new("", PLUGIN_NAME, NULL);
186
187 m->plugin = PLUGIN_NAME;
188
189 d->type = TIMED_MENU_STAT;
190 d->timer = timer_start(6000000, &timed_menu_timeout_handler, m);
191 d->command = "/home/woodblock/timed_menu_stat";
192 d->buf = NULL;
193 d->buflen = 0;
194 d->fd = -1;
195 d->pid = -1;
196 d->mtime = 0;
197
198 m->plugin_data = (void *)d;
199
200 timed_menu_timeout_handler(m);
201 return (void *)m;
202 }
203
204 void plugin_destroy (void *m)
205 {
206 timed_menu_clean_up(m);
207 /* this will be freed by timer_* */
208 timer_stop( ((Timed_Menu_Data *)TIMED_MENU(m)->plugin_data)->timer);
209
210 g_free( TIMED_MENU(m)->plugin_data );
211 }
This page took 0.042455 seconds and 5 git commands to generate.