i3 - improved tiling WM


Add optional bidirectional interface to i3bar (v11)

Patch status: needinfo

Patch by enkore

Long description:

If the child specifies bidirectional:true 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.

The seventh version of this patch fixed a memory leak.

The 8th version of this patch fixed switching workspaces by scrolling

The 9th version is a one-line fix.

The 10th version doesn't send click commands anymore if the empty area left
of the last block is clicked.

The 11th version of this patch made some small changes and fixed
some inconsitencies.

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

b/docs/i3bar-protocol

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

b/i3bar/include/child.h

111
@@ -33,6 +33,12 @@ typedef struct {
112
      * The signal requested by the client to inform it of theun hidden state of i3bar
113
      */
114
     int cont_signal;
115
+
116
+    /**
117
+     * Enable bi-directional communication, i.e. on-click events
118
+     */
119
+    bool bidirectional;
120
+    bool bidirectional_init;
121
 } i3bar_child;
122
 
123
 /*
124
@@ -68,4 +74,10 @@ void stop_child(void);
125
  */
126
 void cont_child(void);
127
 
128
+/*
129
+ * ends the block_clicked command to the child
130
+ *
131
+ */
132
+void send_block_clicked(int button, const char *name, const char *instance, int x, int y);
133
+
134
 #endif

b/i3bar/include/common.h

139
@@ -50,6 +50,10 @@ struct status_block {
140
     uint32_t x_offset;
141
     uint32_t x_append;
142
 
143
+    /* Optional */
144
+    char *name;
145
+    char *instance;
146
+
147
     TAILQ_ENTRY(status_block) blocks;
148
 };
149
 

b/i3bar/src/child.c

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

b/i3bar/src/parse_json_header.c

347
@@ -31,6 +31,7 @@ static enum {
348
     KEY_VERSION,
349
     KEY_STOP_SIGNAL,
350
     KEY_CONT_SIGNAL,
351
+    KEY_BIDIRECTIONAL,
352
     NO_KEY
353
 } current_key;
354
 
355
@@ -54,6 +55,21 @@ static int header_integer(void *ctx, long val) {
356
         default:
357
             break;
358
     }
359
+
360
+    return 1;
361
+}
362
+
363
+static int header_boolean(void *ctx, int val) {
364
+    i3bar_child *child = ctx;
365
+
366
+    switch (current_key) {
367
+        case KEY_BIDIRECTIONAL:
368
+            child->bidirectional = val;
369
+            break;
370
+        default:
371
+            break;
372
+    }
373
+
374
     return 1;
375
 }
376
 
377
@@ -71,13 +87,15 @@ static int header_map_key(void *ctx, const unsigned char *stringval, unsigned in
378
         current_key = KEY_STOP_SIGNAL;
379
     } else if (CHECK_KEY("cont_signal")) {
380
         current_key = KEY_CONT_SIGNAL;
381
+    } else if (CHECK_KEY("bidirectional")) {
382
+        current_key = KEY_BIDIRECTIONAL;
383
     }
384
     return 1;
385
 }
386
 
387
 static yajl_callbacks version_callbacks = {
388
     NULL, /* null */
389
-    NULL, /* boolean */
390
+    &header_boolean, /* boolean */
391
     &header_integer,
392
     NULL, /* double */
393
     NULL, /* number */

b/i3bar/src/xcb.c

398
@@ -306,24 +306,11 @@ void handle_button(xcb_button_press_event_t *event) {
399
     }
400
 
401
     int32_t x = event->event_x >= 0 ? event->event_x : 0;
402
+    int32_t original_x = x;
403
 
404
     DLOG("Got Button %d\n", event->detail);
405
 
406
     switch (event->detail) {
407
-        case 1:
408
-            /* Left Mousbutton. We determine, which button was clicked
409
-             * and set cur_ws accordingly */
410
-            TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
411
-                DLOG("x = %d\n", x);
412
-                if (x >= 0 && x < cur_ws->name_width + 10) {
413
-                    break;
414
-                }
415
-                x -= cur_ws->name_width + 11;
416
-            }
417
-            if (cur_ws == NULL) {
418
-                return;
419
-            }
420
-            break;
421
         case 4:
422
             /* Mouse wheel up. We select the previous ws, if any.
423
              * If there is no more workspace, don’t even send the workspace
424
@@ -344,6 +331,52 @@ void handle_button(xcb_button_press_event_t *event) {
425
 
426
             cur_ws = TAILQ_NEXT(cur_ws, tailq);
427
             break;
428
+        default:
429
+            /* Check if this event regards a workspace button */
430
+            TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
431
+                DLOG("x = %d\n", x);
432
+                if (x >= 0 && x < cur_ws->name_width + 10) {
433
+                    break;
434
+                }
435
+                x -= cur_ws->name_width + 11;
436
+            }
437
+            if (cur_ws == NULL) {
438
+                /* No workspace button was pressed.
439
+                 * Check if a status block has been clicked.
440
+                 * This of course only has an effect,
441
+                 * if the child reported bidirectional protocol usage. */
442
+
443
+                /* First calculate width of tray area */
444
+                trayclient *trayclient;
445
+                int tray_width = 0;
446
+                TAILQ_FOREACH_REVERSE(trayclient, walk->trayclients, tc_head, tailq) {
447
+                    if (!trayclient->mapped)
448
+                        continue;
449
+                    tray_width += (font.height + 2);
450
+                }
451
+
452
+                int block_x = 0, last_block_x;
453
+                int offset = (walk->rect.w - (statusline_width + tray_width)) - 10;
454
+
455
+                x = original_x - offset;
456
+                if(x < 0)
457
+                    return;
458
+
459
+                struct status_block *block;
460
+
461
+                TAILQ_FOREACH(block, &statusline_head, blocks) {
462
+                    last_block_x = block_x;
463
+                    block_x += block->width + block->x_offset + block->x_append;
464
+
465
+                    if(x <= block_x && x >= last_block_x) {
466
+                        send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
467
+                        return;
468
+                    }
469
+                }
470
+                return;
471
+            }
472
+            if (event->detail != 1)
473
+                return;
474
     }
475
 
476
     /* To properly handle workspace names with double quotes in them, we need