i3 - improved tiling WM


Add optional bidirectional interface to i3bar. (ver.6)

Patch status: superseded

Patch by enkore

Long description:

If the child specifies bidirectional:1 in the protocl header,
a JSON array will be streamed to it's stdin.
It consists of maps with at least one key (command).

Such a map is emitted if the user clicks on a status block. i.e.
{"command":"block_clicked","name":"some_block_name","instance":"optional_instance"}

The basic output format is like the rest of the i3bar protocol (i.e.
a new line after each element (map in this case))

The second version of this patch mainly introduced a better API for sending
commands.

The third version of this patch introduced the additional button parameter and thus
added support for right clicks. Now root window coordinates are passed instead of
window coordinates.

The fourth version of this patch (re-)added closing of unused pipe fd's.
Some superfluous whitespace was removed, too.

The fifth version of this patch changed some implementation details and
added handling of all mouse buttons by the child.

The sixth version of this patch changed the bidirectional-key in the
protocol header to a boolean.

To apply this patch, use:
curl http://cr.i3wm.org/patch/76/raw.patch | git am

b/docs/i3bar-protocol

43
@@ -51,7 +51,7 @@ consists of a single JSON hash:
44
 
45
 *All features example*:
46
 ------------------------------
47
-{ "version": 1, "stop_signal": 10, "cont_signal": 12 }
48
+{ "version": 1, "stop_signal": 10, "cont_signal": 12, "bidirectional": 1 }
49
 ------------------------------
50
 
51
 (Note that before i3 v4.3 the precise format had to be +{"version":1}+,
52
@@ -110,6 +110,9 @@ cont_signal::
53
 	Specify to i3bar the signal (as an integer)to send to continue your
54
 	processing.
55
 	The default value (if none is specified) is SIGCONT.
56
+bidirectional::
57
+	If specified and true i3bar will write a infinite array (same as above)
58
+	to your stdin.
59
 
60
 === Blocks in detail
61
 
62
@@ -183,3 +186,33 @@ An example of a block which uses all possible entries follows:
63
  "instance": "eth0"
64
 }
65
 ------------------------------------------
66
+
67
+=== Bidirectional communication
68
+
69
+If enabled i3bar will send you notifications about certain events, currently
70
+only one such notification is implemented: block_clicked.
71
+It is sent if the user clicks on a block and looks like this:
72
+
73
+command::
74
+	Always block_clicked at the moment, but more are maybe added later.
75
+name::
76
+	Name of the block, if set
77
+instance::
78
+	Instance of the block, if set
79
+x, y::
80
+	X11 root window coordinates where the click occured
81
+button:
82
+	X11 button ID (for example 1 to 3 for left/middle/right mouse button
83
+    or 4/5 for mouse wheel up respectively down)
84
+
85
+*Example*:
86
+------------------------------------------
87
+{
88
+ "command": "block_clicked",
89
+ "name": "ethernet",
90
+ "instance": "eth0",
91
+ "button": 1,
92
+ "x": 1320,
93
+ "y": 1400
94
+}
95
+------------------------------------------

b/i3bar/include/child.h

100
@@ -33,6 +33,12 @@ typedef struct {
101
      * The signal requested by the client to inform it of theun hidden state of i3bar
102
      */
103
     int cont_signal;
104
+
105
+    /**
106
+     * Enable bi-directional communication, i.e. on-click events
107
+     */
108
+    bool bidirectional;
109
+    bool bidirectional_init;
110
 } i3bar_child;
111
 
112
 /*
113
@@ -68,4 +74,10 @@ void stop_child(void);
114
  */
115
 void cont_child(void);
116
 
117
+/*
118
+ * ends the block_clicked command to the child
119
+ *
120
+ */
121
+void send_block_clicked(int button, const char *name, const char *instance, int x, int y);
122
+
123
 #endif

b/i3bar/include/common.h

128
@@ -50,6 +50,10 @@ struct status_block {
129
     uint32_t x_offset;
130
     uint32_t x_append;
131
 
132
+    /* Optional */
133
+    char *name;
134
+    char *instance;
135
+
136
     TAILQ_ENTRY(status_block) blocks;
137
 };
138
 

b/i3bar/src/child.c

143
@@ -21,6 +21,7 @@
144
 #include <yajl/yajl_common.h>
145
 #include <yajl/yajl_parse.h>
146
 #include <yajl/yajl_version.h>
147
+#include <yajl/yajl_gen.h>
148
 
149
 #include "common.h"
150
 
151
@@ -35,6 +36,9 @@ ev_child *child_sig;
152
 yajl_callbacks callbacks;
153
 yajl_handle parser;
154
 
155
+/* JSON generator for stdout */
156
+yajl_gen gen;
157
+
158
 typedef struct parser_ctx {
159
     /* True if one of the parsed blocks was urgent */
160
     bool has_urgent;
161
@@ -141,6 +145,18 @@ static int stdin_string(void *context, const unsigned char *val, unsigned int le
162
             ctx->block.align = ALIGN_CENTER;
163
         }
164
     }
165
+    if (strcasecmp(ctx->last_map_key, "name") == 0) {
166
+        char *copy = (char*)malloc(len+1);
167
+        strncpy(copy, (const char *)val, len);
168
+        copy[len] = 0;
169
+        ctx->block.name = copy;
170
+    }
171
+    if (strcasecmp(ctx->last_map_key, "instance") == 0) {
172
+        char *copy = (char*)malloc(len+1);
173
+        strncpy(copy, (const char *)val, len);
174
+        copy[len] = 0;
175
+        ctx->block.instance = copy;
176
+    }
177
     return 1;
178
 }
179
 
180
@@ -322,6 +338,18 @@ void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
181
     cleanup();
182
 }
183
 
184
+void child_write_output(void) {
185
+    if(child.bidirectional) {
186
+        const unsigned char *output;
187
+        size_t size;
188
+        yajl_gen_get_buf(gen, &output, &size);
189
+        fwrite(output, 1, size, stdout);
190
+        fwrite("\n", 1, 1, stdout);
191
+        fflush(stdout);
192
+        yajl_gen_clear(gen);
193
+    }
194
+}
195
+
196
 /*
197
  * Start a child-process with the specified command and reroute stdin.
198
  * We actually start a $SHELL to execute the command so we don't have to care
199
@@ -347,10 +375,16 @@ void start_child(char *command) {
200
     parser = yajl_alloc(&callbacks, NULL, &parser_context);
201
 #endif
202
 
203
+    gen = yajl_gen_alloc(NULL);
204
+
205
     if (command != NULL) {
206
-        int fd[2];
207
-        if (pipe(fd) == -1)
208
-            err(EXIT_FAILURE, "pipe(fd)");
209
+        int pipe_in[2]; /* pipe we read from */
210
+        int pipe_out[2]; /* pipe we write to */
211
+
212
+        if (pipe(pipe_in) == -1)
213
+            err(EXIT_FAILURE, "pipe(pipe_in)");
214
+        if (pipe(pipe_out) == -1)
215
+            err(EXIT_FAILURE, "pipe(pipe_out)");
216
 
217
         child.pid = fork();
218
         switch (child.pid) {
219
@@ -358,10 +392,13 @@ void start_child(char *command) {
220
                 ELOG("Couldn't fork(): %s\n", strerror(errno));
221
                 exit(EXIT_FAILURE);
222
             case 0:
223
-                /* Child-process. Reroute stdout and start shell */
224
-                close(fd[0]);
225
+                /* Child-process. Reroute streams and start shell */
226
 
227
-                dup2(fd[1], STDOUT_FILENO);
228
+                close(pipe_in[0]);
229
+                close(pipe_out[1]);
230
+
231
+                dup2(pipe_in[1], STDOUT_FILENO);
232
+                dup2(pipe_out[0], STDIN_FILENO);
233
 
234
                 static const char *shell = NULL;
235
 
236
@@ -371,10 +408,13 @@ void start_child(char *command) {
237
                 execl(shell, shell, "-c", command, (char*) NULL);
238
                 return;
239
             default:
240
-                /* Parent-process. Rerout stdin */
241
-                close(fd[1]);
242
+                /* Parent-process. Reroute streams */
243
+
244
+                close(pipe_in[1]);
245
+                close(pipe_out[0]);
246
 
247
-                dup2(fd[0], STDIN_FILENO);
248
+                dup2(pipe_in[0], STDIN_FILENO);
249
+                dup2(pipe_out[1], STDOUT_FILENO);
250
 
251
                 break;
252
         }
253
@@ -396,6 +436,69 @@ void start_child(char *command) {
254
 }
255
 
256
 /*
257
+ * Internal helper functions for bidirectional comms
258
+ *
259
+ */
260
+void child_bidi_initialize(void) {
261
+    if(!child.bidirectional_init) {
262
+        yajl_gen_array_open(gen);
263
+        child_write_output();
264
+        child.bidirectional_init = true;
265
+    }
266
+}
267
+
268
+void child_bidi_key(const char *key) {
269
+    yajl_gen_string(gen, (const unsigned char *)key, strlen(key));
270
+}
271
+
272
+void child_bidi_open(const char *command) {
273
+    child_bidi_initialize();
274
+
275
+    yajl_gen_map_open(gen);
276
+
277
+    child_bidi_key("command");
278
+    yajl_gen_string(gen, (const unsigned char *)command, strlen(command));
279
+}
280
+
281
+void child_bidi_close(void) {
282
+    yajl_gen_map_close(gen);
283
+    child_write_output();
284
+}
285
+
286
+/*
287
+ * sends the block_clicked command to the child
288
+ *
289
+ */
290
+void send_block_clicked(int button, const char *name, const char *instance, int x, int y) {
291
+    if(child.bidirectional) {
292
+        child_bidi_open("block_clicked");
293
+
294
+        if(name) {
295
+            child_bidi_key("name");
296
+            yajl_gen_string(gen, (const unsigned char *)name, strlen(name));
297
+        }
298
+
299
+        if(instance) {
300
+            child_bidi_key("instance");
301
+            yajl_gen_string(gen, (const unsigned char *)instance, strlen(instance));
302
+        }
303
+
304
+        child_bidi_key("button");
305
+        yajl_gen_integer(gen, button);
306
+
307
+        child_bidi_key("x");
308
+        yajl_gen_integer(gen, x);
309
+
310
+        child_bidi_key("y");
311
+        yajl_gen_integer(gen, y);
312
+
313
+        yajl_gen_map_close(gen);
314
+
315
+        child_write_output();
316
+    }
317
+}
318
+
319
+/*
320
  * kill()s the child-process (if any). Called when exit()ing.
321
  *
322
  */

b/i3bar/src/parse_json_header.c

327
@@ -31,6 +31,7 @@ static enum {
328
     KEY_VERSION,
329
     KEY_STOP_SIGNAL,
330
     KEY_CONT_SIGNAL,
331
+    KEY_BIDIRECTIONAL,
332
     NO_KEY
333
 } current_key;
334
 
335
@@ -54,6 +55,21 @@ static int header_integer(void *ctx, long val) {
336
         default:
337
             break;
338
     }
339
+
340
+    return 1;
341
+}
342
+
343
+static int header_boolean(void *ctx, int val) {
344
+    i3bar_child *child = ctx;
345
+
346
+    switch (current_key) {
347
+        case KEY_BIDIRECTIONAL:
348
+            child->bidirectional = val ? true : false;
349
+            break;
350
+        default:
351
+            break;
352
+    }
353
+
354
     return 1;
355
 }
356
 
357
@@ -71,13 +87,15 @@ static int header_map_key(void *ctx, const unsigned char *stringval, unsigned in
358
         current_key = KEY_STOP_SIGNAL;
359
     } else if (CHECK_KEY("cont_signal")) {
360
         current_key = KEY_CONT_SIGNAL;
361
+    } else if (CHECK_KEY("bidirectional")) {
362
+        current_key = KEY_BIDIRECTIONAL;
363
     }
364
     return 1;
365
 }
366
 
367
 static yajl_callbacks version_callbacks = {
368
     NULL, /* null */
369
-    NULL, /* boolean */
370
+    &header_boolean, /* boolean */
371
     &header_integer,
372
     NULL, /* double */
373
     NULL, /* number */

b/i3bar/src/xcb.c

378
@@ -306,23 +306,56 @@ void handle_button(xcb_button_press_event_t *event) {
379
     }
380
 
381
     int32_t x = event->event_x >= 0 ? event->event_x : 0;
382
+    int32_t original_x = x;
383
 
384
     DLOG("Got Button %d\n", event->detail);
385
 
386
-    switch (event->detail) {
387
-        case 1:
388
-            /* Left Mousbutton. We determine, which button was clicked
389
-             * and set cur_ws accordingly */
390
-            TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
391
-                DLOG("x = %d\n", x);
392
-                if (x >= 0 && x < cur_ws->name_width + 10) {
393
-                    break;
394
-                }
395
-                x -= cur_ws->name_width + 11;
396
-            }
397
-            if (cur_ws == NULL) {
398
+
399
+    /* Check if this event regards a workspace button */
400
+    TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
401
+        DLOG("x = %d\n", x);
402
+        if (x >= 0 && x < cur_ws->name_width + 10) {
403
+            break;
404
+        }
405
+        x -= cur_ws->name_width + 11;
406
+    }
407
+
408
+    if (cur_ws == NULL) {
409
+        /* No workspace button was pressed.
410
+         * Check if a status block has been clicked.
411
+         * This of course only has an effect,
412
+         * if the child reported bidirectional protocol usage. */
413
+
414
+        /* First calculate width of tray area */
415
+        trayclient *trayclient;
416
+        int tray_width = 0;
417
+        TAILQ_FOREACH_REVERSE(trayclient, walk->trayclients, tc_head, tailq) {
418
+            if (!trayclient->mapped)
419
+                continue;
420
+            tray_width += (font.height + 2);
421
+        }
422
+
423
+        int block_x = 0, last_block_x;
424
+        int offset = (walk->rect.w - (statusline_width + tray_width)) - 10;
425
+
426
+        x = original_x - offset > 0 ? original_x - offset : 0;
427
+
428
+        struct status_block *block;
429
+
430
+        TAILQ_FOREACH(block, &statusline_head, blocks) {
431
+            last_block_x = block_x;
432
+            block_x += block->width + block->x_offset + block->x_append;
433
+
434
+            if(x <= block_x && x >= last_block_x) {
435
+                send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
436
                 return;
437
             }
438
+        }
439
+        return;
440
+    }
441
+
442
+    switch (event->detail) {
443
+        case 1:
444
             break;
445
         case 4:
446
             /* Mouse wheel up. We select the previous ws, if any.
447
@@ -344,6 +377,9 @@ void handle_button(xcb_button_press_event_t *event) {
448
 
449
             cur_ws = TAILQ_NEXT(cur_ws, tailq);
450
             break;
451
+        default:
452
+            /* Return if other mouse button */
453
+            return;
454
     }
455
 
456
     /* To properly handle workspace names with double quotes in them, we need