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.
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
17 * <item label="GLOVE.png">
18 * <action name="execute">
20 * bsetbg "/home/woodblock/.openbox/backgrounds/GLOVE.png"
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
39 #include <sys/types.h>
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"
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";
55 TIMED_MENU_PIPE
, /* read entries from a child process' output */
56 TIMED_MENU_STAT
/* reread entries from file when timestamp changes */
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 */
71 void plugin_setup_config() { }
74 void plugin_shutdown() { }
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
;
82 TIMED_MENU_DATA(m
)->buflen
= 0;
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;
90 if (TIMED_MENU_DATA(m
)->pid
!= -1) {
91 waitpid(TIMED_MENU_DATA(m
)->pid
, NULL
, 0);
92 TIMED_MENU_DATA(m
)->pid
= -1;
96 void timed_menu_read_pipe(int fd
, void *d
)
100 unsigned long num_read
;
102 /* because gdb is dumb */
104 Timed_Menu_Data
*d
= TIMED_MENU_DATA(menu
);
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
) <
114 tmpbuf
= g_try_realloc(TIMED_MENU_DATA(menu
)->buf
,
115 TIMED_MENU_DATA(menu
)->buflen
+ num_realloc
);
117 if (tmpbuf
== NULL
) {
118 g_warning("Unable to allocate memory for read()");
122 TIMED_MENU_DATA(menu
)->buf
= tmpbuf
;
125 TIMED_MENU_DATA(menu
)->buf
+ TIMED_MENU_DATA(menu
)->buflen
,
132 menu
->invalid
= TRUE
;
135 TIMED_MENU_DATA(menu
)->buf
[TIMED_MENU_DATA(menu
)->buflen
] = '\0';
139 if (parse_load_mem(TIMED_MENU_DATA(menu
)->buf
,
140 TIMED_MENU_DATA(menu
)->buflen
,
141 "timed_menu", &doc
, &node
))
142 parse_menu_full(i
, doc
, node
, menu
, FALSE
);
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
);
156 void timed_menu_timeout_handler(ObTimer
*t
, void *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
165 /* I hate you glib in all your hideous forms */
171 args
[2] = TIMED_MENU_DATA(data
)->command
;
173 if (g_spawn_async_with_pipes(
177 G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
,
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
;
190 event_add_fd_handler(h
);
192 g_warning("unable to spawn child");
197 case (TIMED_MENU_STAT
): {
198 struct stat stat_buf
;
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
,
207 if (stat_buf
.st_mtime
> TIMED_MENU_DATA(data
)->mtime
) {
212 g_warning("file changed");
213 TIMED_MENU_DATA(data
)->mtime
= stat_buf
.st_mtime
;
215 data
->invalid
= TRUE
;
220 if (parse_load(TIMED_MENU_DATA(data
)->command
,
221 "timed_menu", &doc
, &node
))
222 parse_menu_full(i
, doc
, node
, data
, FALSE
);
226 timed_menu_clean_up(data
);
233 void *plugin_create(PluginMenuCreateData
*data
)
243 parse_attr_string("id", data
->node
, &id
);
244 parse_attr_string("label", data
->node
, &label
);
246 d
= g_new(Timed_Menu_Data
, 1);
248 m
= menu_new( (label
!= NULL
? label
: ""),
249 (id
!= NULL
? id
: PLUGIN_NAME
),
252 m
->plugin
= PLUGIN_NAME
;
255 menu_add_entry(data
->parent
, menu_entry_new_submenu(
256 (label
!= NULL
? label
: ""),
259 d
->type
= TIMED_MENU_PIPE
;
261 if (parse_attr_string("type", data
->node
, &type
) &&
262 !g_strcasecmp(type
, "stat")) {
263 d
->type
= TIMED_MENU_STAT
;
265 if (!parse_attr_string("file", data
->node
, &d
->command
)) {
266 d
->command
= g_strdup("");
269 if (!parse_attr_string("command", data
->node
, &d
->command
)) {
270 d
->command
= g_strdup("");
273 if (parse_attr_string("timeout", data
->node
, &timeout
)) {
275 gdouble timeout_val
= g_strtod(timeout
, &endptr
);
277 d
->timer
= timer_start(timeout_val
* 1000000,
278 &timed_menu_timeout_handler
, m
);
280 d
->timer
= timer_start(600 * 1000000, &timed_menu_timeout_handler
, m
);
288 m
->plugin_data
= (void *)d
;
290 timed_menu_timeout_handler(NULL
, m
);
294 void plugin_destroy (void *m
)
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
);
300 g_free( TIMED_MENU(m
)->plugin_data
);