all repos — openbox @ e7ae71db7008c2418090608e9860da024f1c1464

openbox fork - make it a bit more like ryudo

plugins/menu/fifo_menu.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * $Header$
 *
 * FFIO menu plugin
 * Provides a menu from a FIFO located in ~/.openbox/fifo_menu/id
 * Example:
 * rc3:
 *   <menu id="fonk" label="fonk" plugin="fifo_menu"></menu>
 * Menu format
 * <fifo_menu>
 *    <item label="GLOVE.png">
 *       <action name="execute">
 *          <execute>
 *             bsetbg "/home/woodblock/.openbox/backgrounds/GLOVE.png"
 *          </execute>
 *       </action>
 *    </item>
 *  </fifo_menu>
 *
 * If the attribute pid="true" is in the <menu>
 */

#include <glib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "kernel/menu.h"
#include "kernel/event.h"

static char *PLUGIN_NAME = "fifo_menu";

typedef struct Fifo_Menu_Data {
    char *fifo;
    char *buf; /* buffer to hold partially read menu */
    unsigned long buflen; /* how many bytes are in the buffer */
    int fd; /* file descriptor to read menu from */
    gboolean use_pid;
    event_fd_handler *handler;
} Fifo_Menu_Data;

#define FIFO_MENU(m) ((ObMenu *)m)
#define FIFO_MENU_DATA(m) ((Fifo_Menu_Data *)((ObMenu *)m)->plugin_data)


void fifo_menu_clean_up(ObMenu *m) {
    if (FIFO_MENU_DATA(m)->buf != NULL) {
        g_free(FIFO_MENU_DATA(m)->buf);
        FIFO_MENU_DATA(m)->buf = NULL;
        FIFO_MENU_DATA(m)->buflen = 0;
    }

    if (FIFO_MENU_DATA(m)->fd != -1) {
        close(FIFO_MENU_DATA(m)->fd);
        FIFO_MENU_DATA(m)->fd = -1;
    }
}

void plugin_setup_config() { }
void plugin_startup()
{ }
void plugin_shutdown() { }

void fifo_menu_handler(int fd, void *d) {
    ObMenu *menu = d;
    char *tmpbuf = NULL;
    unsigned long num_read;
#ifdef DEBUG
    /* because gdb is dumb */
#if 0
    Fifo_Menu_Data *d = FIFO_MENU_DATA(menu);
#endif
#endif
    
    /* if the menu is shown this will go into busy loop :(
     fix me*/
    if (!menu->shown) { 
        unsigned long num_realloc;
        /* if we have less than a quarter BUFSIZ left, allocate more */
        num_realloc = (BUFSIZ - (FIFO_MENU_DATA(menu)->buflen % BUFSIZ) <
                       BUFSIZ >> 2) ?
            0 : BUFSIZ;
    
        tmpbuf = g_try_realloc(FIFO_MENU_DATA(menu)->buf,             
                               FIFO_MENU_DATA(menu)->buflen + num_realloc);

        if (tmpbuf == NULL) {
            g_warning("Unable to allocate memory for read()");
            return;
        }
    
        FIFO_MENU_DATA(menu)->buf = tmpbuf;
    
        num_read = read(fd,
                        FIFO_MENU_DATA(menu)->buf +
                        FIFO_MENU_DATA(menu)->buflen,
                        num_realloc);

        if (num_read == 0) { /* eof */
            xmlDocPtr doc;
            xmlNodePtr node;

            menu->invalid = TRUE;
            menu_clear(menu);

            FIFO_MENU_DATA(menu)->buf[FIFO_MENU_DATA(menu)->buflen] = '\0';

            doc = xmlParseMemory(FIFO_MENU_DATA(menu)->buf,
                                 FIFO_MENU_DATA(menu)->buflen);

            node = xmlDocGetRootElement(doc);
            
            if (node &&
                !xmlStrcasecmp(node->name, (const xmlChar*) "fifo_menu")) {
                parse_menu_full(doc, node, menu, FALSE);
            }
            
            fifo_menu_clean_up(menu);
            
            event_remove_fd(FIFO_MENU_DATA(menu)->handler->fd);
        
            if ((FIFO_MENU_DATA(menu)->fd =
                 open(FIFO_MENU_DATA(menu)->fifo,
                      O_NONBLOCK | O_RDONLY)) == -1) {
                g_warning("Can't reopen FIFO");
                fifo_menu_clean_up(menu);
                return;
            }

            FIFO_MENU_DATA(menu)->handler->fd = FIFO_MENU_DATA(menu)->fd;
        
            event_add_fd_handler(FIFO_MENU_DATA(menu)->handler);
        } else if (num_read > 0) {
            FIFO_MENU_DATA(menu)->buflen += num_read;
            FIFO_MENU_DATA(menu)->buf[FIFO_MENU_DATA(menu)->buflen] = '\0';
        }
    }
}

void plugin_destroy (ObMenu *m)
{
    fifo_menu_clean_up(m);
    if (FIFO_MENU_DATA(m)->handler != NULL) {
        g_free(FIFO_MENU_DATA(m)->handler);
        FIFO_MENU_DATA(m)->handler = NULL;
    }

    if (FIFO_MENU_DATA(m)->fifo != NULL) {
        g_free(FIFO_MENU_DATA(m)->fifo);
        FIFO_MENU_DATA(m)->fifo = NULL;
    }

    if (FIFO_MENU_DATA(m)->buf != NULL) {
        g_free(FIFO_MENU_DATA(m)->buf);
        FIFO_MENU_DATA(m)->buf = NULL;
    }
 
    g_free(m->plugin_data);

    menu_free(m->name);
}

void *plugin_create(PluginMenuCreateData *data)
{
    char *fifo;
    char *dir;
    event_fd_handler *h;
    Fifo_Menu_Data *d;
    ObMenu *m;
    char *label = NULL, *id = NULL;
    char *attr_pid = NULL;
        
    d = g_new(Fifo_Menu_Data, 1);

    parse_attr_string("id", data->node, &id);
    parse_attr_string("label", data->node, &label);
    
    if (parse_attr_string("pid", data->node, &attr_pid) &&
        g_strcasecmp(attr_pid, "true") == 0) {
        d->use_pid = TRUE;
    } else
        d->use_pid = FALSE;
    
    m = menu_new( (label != NULL ? label : ""),
                  (id != NULL ? id : PLUGIN_NAME),
                  data->parent);

    if (data->parent)
        menu_add_entry(data->parent, menu_entry_new_submenu(
                           (label != NULL ? label : ""),
                           m));

    g_free(label);
    g_free(id);
    d->fd = -1;
    d->buf = NULL;
    d->buflen = 0;
    d->handler = NULL;
    
    m->plugin = PLUGIN_NAME;

    d->fd = -1;
    
    m->plugin_data = (void *)d;

        
    dir = g_build_filename(g_get_home_dir(), ".openbox",
                          PLUGIN_NAME, NULL);

    if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1 && errno != EEXIST) {
/* technically, if ~/.openbox/fifo_menu exists and isn't a directory
   this will fail, but we don't care because mkfifo will fail and warn
   anyway */
        g_warning("Can't create %s: %s", dir, strerror(errno));
        g_free(dir);
        plugin_destroy(m);
        return NULL;
    }

    if (d->use_pid)
    {
        char *pid = g_strdup_printf("%s.%d", m->name, getpid());
        fifo = g_build_filename(g_get_home_dir(), ".openbox",
                                PLUGIN_NAME,
                                pid, NULL);
        g_free(pid);
    } else {
        fifo = g_build_filename(g_get_home_dir(), ".openbox",
                                PLUGIN_NAME,
                                m->name, NULL);
    }
    
    if (mkfifo(fifo, S_IRUSR | S_IWUSR |
               S_IRGRP | S_IWGRP | /* let umask do its thing */
               S_IROTH | S_IWOTH) == -1 && errno != EEXIST) {
        g_warning("Can't create FIFO %s: %s", fifo, strerror(errno));
        g_free(fifo);
        g_free(d);
        menu_free(m->name);
        return NULL;
    }

/* open in non-blocking mode so we don't wait for a process to open FIFO
   for writing */
    if ((d->fd = open(fifo, O_NONBLOCK | O_RDONLY)) == -1) { 
        g_warning("Can't open FIFO %s: %s", fifo, strerror(errno));
        g_free(fifo);
        g_free(d);
        menu_free(m->name);
        return NULL;
    }

    d->fifo = fifo;
    
    h = g_new(event_fd_handler, 1);

    if (h == NULL) {
        g_warning("Out of memory");
        close(d->fd);
        g_free(fifo);
        g_free(d);
        menu_free(m->name);
        return NULL;
    }
    
    h->fd = d->fd;
    h->data = m;
    h->handler = fifo_menu_handler;
    d->handler = h;
    
    event_add_fd_handler(h);
    
    g_free(dir);
    return (void *)m;
}