]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/menu/timed_menu.c
Timed menu that reads output from a process while the window is hidden
[mikachu/openbox.git] / plugins / menu / timed_menu.c
1 #include <glib.h>
2
3 #include "kernel/menu.h"
4 #include "kernel/timer.h"
5 #include "timed_menu.h"
6 #include "kernel/action.h"
7
8 #define TIMED_MENU(m) ((Menu *)m)
9 #define TIMED_MENU_DATA(m) ((Timed_Menu_Data *)((Menu *)m)->plugin_data)
10 static char *PLUGIN_NAME = "timed_menu";
11
12 typedef enum {
13     TIMED_MENU_PIPE
14 } Timed_Menu_Type;
15
16 /* we can use various GIO channels to support reading menus (can we add them to
17    the event loop? )
18    stat() based update
19    exec() based read
20 */
21 typedef struct {
22     Timed_Menu_Type type;
23     Timer *timer; /* timer code handles free */
24     char *command; /* for the PIPE */
25 } Timed_Menu_Data;
26
27
28 void plugin_setup_config() { }
29 void plugin_startup()
30 { }
31 void plugin_shutdown() { }
32
33 void timed_menu_timeout_handler(void *data)
34 {
35     Action *a;
36     ((Menu *)data)->invalid = TRUE;
37
38     if (!TIMED_MENU(data)->shown) {
39         switch (TIMED_MENU_DATA(data)->type) {
40             case (TIMED_MENU_PIPE):
41             {
42                 /* if the menu is not shown, run a process and use its output
43                    as menu */
44                 char *args[] = {"/bin/sh", "-c", "ls", NULL};
45                 GIOChannel *io;
46                 char *line;
47                 gint child, c_stdout, line_len, terminator_pos;
48                 GIOStatus status;
49                 /* this blocks for now, until the event stuff can handle it */
50                 if (!g_spawn_async_with_pipes(NULL,
51                         args,
52                         NULL,
53                         G_SPAWN_DO_NOT_REAP_CHILD,
54                         NULL, NULL,
55                         &child, NULL, &c_stdout, NULL,
56                         NULL)) {
57                     g_warning("%s: Unable to run timed_menu program",
58                         __FUNCTION__);
59                     break;
60                 }
61                 
62                 io = g_io_channel_unix_new(c_stdout);
63                 if (io == NULL) {
64                     g_error("%s: Unable to get IO channel", __FUNCTION__);
65                     break;
66                 }
67
68                 menu_clear(TIMED_MENU(data));
69                 
70                 while ( G_IO_STATUS_NORMAL == (status =
71                             g_io_channel_read_line
72                             (io, &line, &line_len, &terminator_pos, NULL))
73                     ) {
74                     /* the \n looks ugly */
75                     line[terminator_pos] = '\0';
76                     menu_add_entry(TIMED_MENU(data),
77                         menu_entry_new_separator(line));
78                     g_message("%s", line);
79                     g_free(line);
80                 }
81                 break;
82             }
83         }
84     }
85 }
86
87 void *plugin_create()
88 {
89     Timed_Menu_Data *d = g_new(Timed_Menu_Data, 1);
90     Menu *m = menu_new("", PLUGIN_NAME, NULL);
91     
92     m->plugin = PLUGIN_NAME;
93
94     d->type = TIMED_MENU_PIPE;
95     d->timer = timer_start(1000000, &timed_menu_timeout_handler, m);
96     
97     m->plugin_data = (void *)d;
98   
99     return (void *)m;
100 }
101
102 void plugin_destroy (void *m)
103 {
104     /* this will be freed by timer_* */
105     timer_stop( ((Timed_Menu_Data *)TIMED_MENU(m)->plugin_data)->timer);
106     
107     g_free( TIMED_MENU(m)->plugin_data );
108 }