10 #include "kernel/menu.h"
11 #include "kernel/event.h"
13 static char *PLUGIN_NAME = "fifo_menu";
15 typedef struct Fifo_Menu_Data{
17 char *buf; /* buffer to hold partially read menu */
18 unsigned long buflen; /* how many bytes are in the buffer */
19 int fd; /* file descriptor to read menu from */
20 event_fd_handler *handler;
23 #define FIFO_MENU(m) ((ObMenu *)m)
24 #define FIFO_MENU_DATA(m) ((Fifo_Menu_Data *)((ObMenu *)m)->plugin_data)
27 void fifo_menu_clean_up(ObMenu *m) {
28 if (FIFO_MENU_DATA(m)->buf != NULL) {
29 g_free(FIFO_MENU_DATA(m)->buf);
30 FIFO_MENU_DATA(m)->buf = NULL;
31 FIFO_MENU_DATA(m)->buflen = 0;
34 if (FIFO_MENU_DATA(m)->fd != -1) {
35 close(FIFO_MENU_DATA(m)->fd);
36 FIFO_MENU_DATA(m)->fd = -1;
40 void plugin_setup_config() { }
43 void plugin_shutdown() { }
45 void fifo_menu_handler(int fd, void *d) {
48 unsigned long num_read;
50 /* because gdb is dumb */
52 Fifo_Menu_Data *d = FIFO_MENU_DATA(menu);
56 /* if the menu is shown this will go into busy loop :(
59 unsigned long num_realloc;
60 /* if we have less than a quarter BUFSIZ left, allocate more */
61 num_realloc = (BUFSIZ - (FIFO_MENU_DATA(menu)->buflen % BUFSIZ) <
65 tmpbuf = g_try_realloc(FIFO_MENU_DATA(menu)->buf,
66 FIFO_MENU_DATA(menu)->buflen + num_realloc);
69 g_warning("Unable to allocate memory for read()");
73 FIFO_MENU_DATA(menu)->buf = tmpbuf;
76 FIFO_MENU_DATA(menu)->buf + FIFO_MENU_DATA(menu)->buflen,
79 if (num_read == 0) { /* eof */
80 unsigned long count = 0;
88 (found = strchr(&FIFO_MENU_DATA(menu)->buf[count], '\n'))) {
89 FIFO_MENU_DATA(menu)->buf
90 [found - FIFO_MENU_DATA(menu)->buf] = '\0';
92 menu_entry_new_separator
93 (&FIFO_MENU_DATA(menu)->buf[count]));
94 count = found - FIFO_MENU_DATA(menu)->buf + 1;
97 FIFO_MENU_DATA(menu)->buf[FIFO_MENU_DATA(menu)->buflen] = '\0';
98 fifo_menu_clean_up(menu);
100 event_remove_fd(FIFO_MENU_DATA(menu)->handler->fd);
102 if ((FIFO_MENU_DATA(menu)->fd =
103 open(FIFO_MENU_DATA(menu)->fifo, O_NONBLOCK | O_RDONLY)) == -1) {
104 g_warning("Can't reopen FIFO");
105 fifo_menu_clean_up(menu);
109 FIFO_MENU_DATA(menu)->handler->fd = FIFO_MENU_DATA(menu)->fd;
111 event_add_fd_handler(FIFO_MENU_DATA(menu)->handler);
112 } else if (num_read > 0) {
113 FIFO_MENU_DATA(menu)->buflen += num_read;
114 FIFO_MENU_DATA(menu)->buf[FIFO_MENU_DATA(menu)->buflen] = '\0';
119 void plugin_destroy (ObMenu *m)
121 fifo_menu_clean_up(m);
122 if (FIFO_MENU_DATA(m)->handler != NULL) {
123 g_free(FIFO_MENU_DATA(m)->handler);
124 FIFO_MENU_DATA(m)->handler = NULL;
127 if (FIFO_MENU_DATA(m)->fifo != NULL) {
128 g_free(FIFO_MENU_DATA(m)->fifo);
129 FIFO_MENU_DATA(m)->fifo = NULL;
132 g_free(m->plugin_data);
137 void *plugin_create() /* TODO: need config */
143 Fifo_Menu_Data *d = g_new(Fifo_Menu_Data, 1);
144 ObMenu *m = menu_new("", PLUGIN_NAME, NULL);
151 m->plugin = PLUGIN_NAME;
155 m->plugin_data = (void *)d;
157 dir = g_build_filename(g_get_home_dir(), ".openbox", PLUGIN_NAME, NULL);
159 if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1 && errno != EEXIST) {
160 /* technically, if ~/.openbox/fifo_menu exists and isn't a directory
161 this will fail, but we don't care because mkfifo will fail and warn
163 g_warning("Can't create %s: %s", dir, strerror(errno));
169 fifo = g_build_filename(g_get_home_dir(), ".openbox", PLUGIN_NAME,
171 if (mkfifo(fifo, S_IRUSR | S_IWUSR |
172 S_IRGRP | S_IWGRP | /* let umask do its thing */
173 S_IROTH | S_IWOTH) == -1 && errno != EEXIST) {
174 g_warning("Can't create FIFO %s: %s", fifo, strerror(errno));
181 /* open in non-blocking mode so we don't wait for a process to open FIFO
183 if ((d->fd = open(fifo, O_NONBLOCK | O_RDONLY)) == -1) {
184 g_warning("Can't open FIFO %s: %s", fifo, strerror(errno));
193 h = g_new(event_fd_handler, 1);
196 g_warning("Out of memory");
206 h->handler = fifo_menu_handler;
209 event_add_fd_handler(h);