]> git.sur5r.net Git - i3/i3/blob - src/ipc.c
2432d7a543451a2059521ca4e7456c330325f3de
[i3/i3] / src / ipc.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * ipc.c: UNIX domain socket IPC (initialization, client handling, protocol).
8  *
9  */
10 #include "all.h"
11
12 #include "yajl_utils.h"
13
14 #include <stdint.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <fcntl.h>
18 #include <libgen.h>
19 #include <ev.h>
20 #include <yajl/yajl_gen.h>
21 #include <yajl/yajl_parse.h>
22
23 char *current_socketpath = NULL;
24
25 TAILQ_HEAD(ipc_client_head, ipc_client)
26 all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
27
28 /*
29  * Puts the given socket file descriptor into non-blocking mode or dies if
30  * setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our
31  * IPC model because we should by no means block the window manager.
32  *
33  */
34 static void set_nonblock(int sockfd) {
35     int flags = fcntl(sockfd, F_GETFL, 0);
36     flags |= O_NONBLOCK;
37     if (fcntl(sockfd, F_SETFL, flags) < 0)
38         err(-1, "Could not set O_NONBLOCK");
39 }
40
41 static void ipc_client_timeout(EV_P_ ev_timer *w, int revents);
42 static void ipc_socket_writeable_cb(EV_P_ struct ev_io *w, int revents);
43
44 static ev_tstamp kill_timeout = 10.0;
45
46 void ipc_set_kill_timeout(ev_tstamp new) {
47     kill_timeout = new;
48 }
49
50 /*
51  * Try to write the contents of the pending buffer to the client's subscription
52  * socket. Will set, reset or clear the timeout and io write callbacks depending
53  * on the result of the write operation.
54  *
55  */
56 static void ipc_push_pending(ipc_client *client) {
57     const ssize_t result = writeall_nonblock(client->fd, client->buffer, client->buffer_size);
58     if (result < 0) {
59         return;
60     }
61
62     if ((size_t)result == client->buffer_size) {
63         /* Everything was written successfully: clear the timer and stop the io
64          * callback. */
65         FREE(client->buffer);
66         client->buffer_size = 0;
67         if (client->timeout) {
68             ev_timer_stop(main_loop, client->timeout);
69             FREE(client->timeout);
70         }
71         ev_io_stop(main_loop, client->write_callback);
72         return;
73     }
74
75     /* Otherwise, make sure that the io callback is enabled and create a new
76      * timer if needed. */
77     ev_io_start(main_loop, client->write_callback);
78
79     if (!client->timeout) {
80         struct ev_timer *timeout = scalloc(1, sizeof(struct ev_timer));
81         ev_timer_init(timeout, ipc_client_timeout, kill_timeout, 0.);
82         timeout->data = client;
83         client->timeout = timeout;
84         ev_set_priority(timeout, EV_MINPRI);
85         ev_timer_start(main_loop, client->timeout);
86     } else if (result > 0) {
87         /* Keep the old timeout when nothing is written. Otherwise, we would
88          * keep a dead connection by continuously renewing its timeouts. */
89         ev_timer_stop(main_loop, client->timeout);
90         ev_timer_set(client->timeout, kill_timeout, 0.0);
91         ev_timer_start(main_loop, client->timeout);
92     }
93     if (result == 0) {
94         return;
95     }
96
97     /* Shift the buffer to the left and reduce the allocated space. */
98     client->buffer_size -= (size_t)result;
99     memmove(client->buffer, client->buffer + result, client->buffer_size);
100     client->buffer = srealloc(client->buffer, client->buffer_size);
101 }
102
103 /*
104  * Given a message and a message type, create the corresponding header, merge it
105  * with the message and append it to the given client's output buffer. Also,
106  * send the message if the client's buffer was empty.
107  *
108  */
109 static void ipc_send_client_message(ipc_client *client, size_t size, const uint32_t message_type, const uint8_t *payload) {
110     const i3_ipc_header_t header = {
111         .magic = {'i', '3', '-', 'i', 'p', 'c'},
112         .size = size,
113         .type = message_type};
114     const size_t header_size = sizeof(i3_ipc_header_t);
115     const size_t message_size = header_size + size;
116
117     const bool push_now = (client->buffer_size == 0);
118     client->buffer = srealloc(client->buffer, client->buffer_size + message_size);
119     memcpy(client->buffer + client->buffer_size, ((void *)&header), header_size);
120     memcpy(client->buffer + client->buffer_size + header_size, payload, size);
121     client->buffer_size += message_size;
122
123     if (push_now) {
124         ipc_push_pending(client);
125     }
126 }
127
128 static void free_ipc_client(ipc_client *client) {
129     DLOG("Disconnecting client on fd %d\n", client->fd);
130     close(client->fd);
131
132     ev_io_stop(main_loop, client->read_callback);
133     FREE(client->read_callback);
134     ev_io_stop(main_loop, client->write_callback);
135     FREE(client->write_callback);
136     if (client->timeout) {
137         ev_timer_stop(main_loop, client->timeout);
138         FREE(client->timeout);
139     }
140
141     free(client->buffer);
142
143     for (int i = 0; i < client->num_events; i++) {
144         free(client->events[i]);
145     }
146     free(client->events);
147     TAILQ_REMOVE(&all_clients, client, clients);
148     free(client);
149 }
150
151 /*
152  * Sends the specified event to all IPC clients which are currently connected
153  * and subscribed to this kind of event.
154  *
155  */
156 void ipc_send_event(const char *event, uint32_t message_type, const char *payload) {
157     ipc_client *current;
158     TAILQ_FOREACH(current, &all_clients, clients) {
159         for (int i = 0; i < current->num_events; i++) {
160             if (strcasecmp(current->events[i], event) == 0) {
161                 ipc_send_client_message(current, strlen(payload), message_type, (uint8_t *)payload);
162                 break;
163             }
164         }
165     }
166 }
167
168 /*
169  * For shutdown events, we send the reason for the shutdown.
170  */
171 static void ipc_send_shutdown_event(shutdown_reason_t reason) {
172     yajl_gen gen = ygenalloc();
173     y(map_open);
174
175     ystr("change");
176
177     if (reason == SHUTDOWN_REASON_RESTART) {
178         ystr("restart");
179     } else if (reason == SHUTDOWN_REASON_EXIT) {
180         ystr("exit");
181     }
182
183     y(map_close);
184
185     const unsigned char *payload;
186     ylength length;
187
188     y(get_buf, &payload, &length);
189     ipc_send_event("shutdown", I3_IPC_EVENT_SHUTDOWN, (const char *)payload);
190
191     y(free);
192 }
193
194 /*
195  * Calls shutdown() on each socket and closes it. This function is to be called
196  * when exiting or restarting only!
197  *
198  */
199 void ipc_shutdown(shutdown_reason_t reason) {
200     ipc_send_shutdown_event(reason);
201
202     ipc_client *current;
203     while (!TAILQ_EMPTY(&all_clients)) {
204         current = TAILQ_FIRST(&all_clients);
205         shutdown(current->fd, SHUT_RDWR);
206         free_ipc_client(current);
207     }
208 }
209
210 /*
211  * Executes the command and returns whether it could be successfully parsed
212  * or not (at the moment, always returns true).
213  *
214  */
215 IPC_HANDLER(run_command) {
216     /* To get a properly terminated buffer, we copy
217      * message_size bytes out of the buffer */
218     char *command = scalloc(message_size + 1, 1);
219     strncpy(command, (const char *)message, message_size);
220     LOG("IPC: received: *%s*\n", command);
221     yajl_gen gen = yajl_gen_alloc(NULL);
222
223     CommandResult *result = parse_command((const char *)command, gen);
224     free(command);
225
226     if (result->needs_tree_render)
227         tree_render();
228
229     command_result_free(result);
230
231     const unsigned char *reply;
232     ylength length;
233     yajl_gen_get_buf(gen, &reply, &length);
234
235     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_COMMAND,
236                             (const uint8_t *)reply);
237
238     yajl_gen_free(gen);
239 }
240
241 static void dump_rect(yajl_gen gen, const char *name, Rect r) {
242     ystr(name);
243     y(map_open);
244     ystr("x");
245     y(integer, r.x);
246     ystr("y");
247     y(integer, r.y);
248     ystr("width");
249     y(integer, r.width);
250     ystr("height");
251     y(integer, r.height);
252     y(map_close);
253 }
254
255 static void dump_event_state_mask(yajl_gen gen, Binding *bind) {
256     y(array_open);
257     for (int i = 0; i < 20; i++) {
258         if (bind->event_state_mask & (1 << i)) {
259             switch (1 << i) {
260                 case XCB_KEY_BUT_MASK_SHIFT:
261                     ystr("shift");
262                     break;
263                 case XCB_KEY_BUT_MASK_LOCK:
264                     ystr("lock");
265                     break;
266                 case XCB_KEY_BUT_MASK_CONTROL:
267                     ystr("ctrl");
268                     break;
269                 case XCB_KEY_BUT_MASK_MOD_1:
270                     ystr("Mod1");
271                     break;
272                 case XCB_KEY_BUT_MASK_MOD_2:
273                     ystr("Mod2");
274                     break;
275                 case XCB_KEY_BUT_MASK_MOD_3:
276                     ystr("Mod3");
277                     break;
278                 case XCB_KEY_BUT_MASK_MOD_4:
279                     ystr("Mod4");
280                     break;
281                 case XCB_KEY_BUT_MASK_MOD_5:
282                     ystr("Mod5");
283                     break;
284                 case XCB_KEY_BUT_MASK_BUTTON_1:
285                     ystr("Button1");
286                     break;
287                 case XCB_KEY_BUT_MASK_BUTTON_2:
288                     ystr("Button2");
289                     break;
290                 case XCB_KEY_BUT_MASK_BUTTON_3:
291                     ystr("Button3");
292                     break;
293                 case XCB_KEY_BUT_MASK_BUTTON_4:
294                     ystr("Button4");
295                     break;
296                 case XCB_KEY_BUT_MASK_BUTTON_5:
297                     ystr("Button5");
298                     break;
299                 case (I3_XKB_GROUP_MASK_1 << 16):
300                     ystr("Group1");
301                     break;
302                 case (I3_XKB_GROUP_MASK_2 << 16):
303                     ystr("Group2");
304                     break;
305                 case (I3_XKB_GROUP_MASK_3 << 16):
306                     ystr("Group3");
307                     break;
308                 case (I3_XKB_GROUP_MASK_4 << 16):
309                     ystr("Group4");
310                     break;
311             }
312         }
313     }
314     y(array_close);
315 }
316
317 static void dump_binding(yajl_gen gen, Binding *bind) {
318     y(map_open);
319     ystr("input_code");
320     y(integer, bind->keycode);
321
322     ystr("input_type");
323     ystr((const char *)(bind->input_type == B_KEYBOARD ? "keyboard" : "mouse"));
324
325     ystr("symbol");
326     if (bind->symbol == NULL)
327         y(null);
328     else
329         ystr(bind->symbol);
330
331     ystr("command");
332     ystr(bind->command);
333
334     // This key is only provided for compatibility, new programs should use
335     // event_state_mask instead.
336     ystr("mods");
337     dump_event_state_mask(gen, bind);
338
339     ystr("event_state_mask");
340     dump_event_state_mask(gen, bind);
341
342     y(map_close);
343 }
344
345 void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
346     y(map_open);
347     ystr("id");
348     y(integer, (uintptr_t)con);
349
350     ystr("type");
351     switch (con->type) {
352         case CT_ROOT:
353             ystr("root");
354             break;
355         case CT_OUTPUT:
356             ystr("output");
357             break;
358         case CT_CON:
359             ystr("con");
360             break;
361         case CT_FLOATING_CON:
362             ystr("floating_con");
363             break;
364         case CT_WORKSPACE:
365             ystr("workspace");
366             break;
367         case CT_DOCKAREA:
368             ystr("dockarea");
369             break;
370     }
371
372     /* provided for backwards compatibility only. */
373     ystr("orientation");
374     if (!con_is_split(con))
375         ystr("none");
376     else {
377         if (con_orientation(con) == HORIZ)
378             ystr("horizontal");
379         else
380             ystr("vertical");
381     }
382
383     ystr("scratchpad_state");
384     switch (con->scratchpad_state) {
385         case SCRATCHPAD_NONE:
386             ystr("none");
387             break;
388         case SCRATCHPAD_FRESH:
389             ystr("fresh");
390             break;
391         case SCRATCHPAD_CHANGED:
392             ystr("changed");
393             break;
394     }
395
396     ystr("percent");
397     if (con->percent == 0.0)
398         y(null);
399     else
400         y(double, con->percent);
401
402     ystr("urgent");
403     y(bool, con->urgent);
404
405     if (!TAILQ_EMPTY(&(con->marks_head))) {
406         ystr("marks");
407         y(array_open);
408
409         mark_t *mark;
410         TAILQ_FOREACH(mark, &(con->marks_head), marks) {
411             ystr(mark->name);
412         }
413
414         y(array_close);
415     }
416
417     ystr("focused");
418     y(bool, (con == focused));
419
420     if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
421         ystr("output");
422         ystr(con_get_output(con)->name);
423     }
424
425     ystr("layout");
426     switch (con->layout) {
427         case L_DEFAULT:
428             DLOG("About to dump layout=default, this is a bug in the code.\n");
429             assert(false);
430             break;
431         case L_SPLITV:
432             ystr("splitv");
433             break;
434         case L_SPLITH:
435             ystr("splith");
436             break;
437         case L_STACKED:
438             ystr("stacked");
439             break;
440         case L_TABBED:
441             ystr("tabbed");
442             break;
443         case L_DOCKAREA:
444             ystr("dockarea");
445             break;
446         case L_OUTPUT:
447             ystr("output");
448             break;
449     }
450
451     ystr("workspace_layout");
452     switch (con->workspace_layout) {
453         case L_DEFAULT:
454             ystr("default");
455             break;
456         case L_STACKED:
457             ystr("stacked");
458             break;
459         case L_TABBED:
460             ystr("tabbed");
461             break;
462         default:
463             DLOG("About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
464             assert(false);
465             break;
466     }
467
468     ystr("last_split_layout");
469     switch (con->layout) {
470         case L_SPLITV:
471             ystr("splitv");
472             break;
473         default:
474             ystr("splith");
475             break;
476     }
477
478     ystr("border");
479     switch (con->border_style) {
480         case BS_NORMAL:
481             ystr("normal");
482             break;
483         case BS_NONE:
484             ystr("none");
485             break;
486         case BS_PIXEL:
487             ystr("pixel");
488             break;
489     }
490
491     ystr("current_border_width");
492     y(integer, con->current_border_width);
493
494     dump_rect(gen, "rect", con->rect);
495     dump_rect(gen, "deco_rect", con->deco_rect);
496     dump_rect(gen, "window_rect", con->window_rect);
497     dump_rect(gen, "geometry", con->geometry);
498
499     ystr("name");
500     if (con->window && con->window->name)
501         ystr(i3string_as_utf8(con->window->name));
502     else if (con->name != NULL)
503         ystr(con->name);
504     else
505         y(null);
506
507     if (con->title_format != NULL) {
508         ystr("title_format");
509         ystr(con->title_format);
510     }
511
512     if (con->type == CT_WORKSPACE) {
513         ystr("num");
514         y(integer, con->num);
515     }
516
517     ystr("window");
518     if (con->window)
519         y(integer, con->window->id);
520     else
521         y(null);
522
523     if (con->window && !inplace_restart) {
524         /* Window properties are useless to preserve when restarting because
525          * they will be queried again anyway. However, for i3-save-tree(1),
526          * they are very useful and save i3-save-tree dealing with X11. */
527         ystr("window_properties");
528         y(map_open);
529
530 #define DUMP_PROPERTY(key, prop_name)         \
531     do {                                      \
532         if (con->window->prop_name != NULL) { \
533             ystr(key);                        \
534             ystr(con->window->prop_name);     \
535         }                                     \
536     } while (0)
537
538         DUMP_PROPERTY("class", class_class);
539         DUMP_PROPERTY("instance", class_instance);
540         DUMP_PROPERTY("window_role", role);
541
542         if (con->window->name != NULL) {
543             ystr("title");
544             ystr(i3string_as_utf8(con->window->name));
545         }
546
547         ystr("transient_for");
548         if (con->window->transient_for == XCB_NONE)
549             y(null);
550         else
551             y(integer, con->window->transient_for);
552
553         y(map_close);
554     }
555
556     ystr("nodes");
557     y(array_open);
558     Con *node;
559     if (con->type != CT_DOCKAREA || !inplace_restart) {
560         TAILQ_FOREACH(node, &(con->nodes_head), nodes) {
561             dump_node(gen, node, inplace_restart);
562         }
563     }
564     y(array_close);
565
566     ystr("floating_nodes");
567     y(array_open);
568     TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
569         dump_node(gen, node, inplace_restart);
570     }
571     y(array_close);
572
573     ystr("focus");
574     y(array_open);
575     TAILQ_FOREACH(node, &(con->focus_head), focused) {
576         y(integer, (uintptr_t)node);
577     }
578     y(array_close);
579
580     ystr("fullscreen_mode");
581     y(integer, con->fullscreen_mode);
582
583     ystr("sticky");
584     y(bool, con->sticky);
585
586     ystr("floating");
587     switch (con->floating) {
588         case FLOATING_AUTO_OFF:
589             ystr("auto_off");
590             break;
591         case FLOATING_AUTO_ON:
592             ystr("auto_on");
593             break;
594         case FLOATING_USER_OFF:
595             ystr("user_off");
596             break;
597         case FLOATING_USER_ON:
598             ystr("user_on");
599             break;
600     }
601
602     ystr("swallows");
603     y(array_open);
604     Match *match;
605     TAILQ_FOREACH(match, &(con->swallow_head), matches) {
606         /* We will generate a new restart_mode match specification after this
607          * loop, so skip this one. */
608         if (match->restart_mode)
609             continue;
610         y(map_open);
611         if (match->dock != M_DONTCHECK) {
612             ystr("dock");
613             y(integer, match->dock);
614             ystr("insert_where");
615             y(integer, match->insert_where);
616         }
617
618 #define DUMP_REGEX(re_name)                \
619     do {                                   \
620         if (match->re_name != NULL) {      \
621             ystr(#re_name);                \
622             ystr(match->re_name->pattern); \
623         }                                  \
624     } while (0)
625
626         DUMP_REGEX(class);
627         DUMP_REGEX(instance);
628         DUMP_REGEX(window_role);
629         DUMP_REGEX(title);
630
631 #undef DUMP_REGEX
632         y(map_close);
633     }
634
635     if (inplace_restart) {
636         if (con->window != NULL) {
637             y(map_open);
638             ystr("id");
639             y(integer, con->window->id);
640             ystr("restart_mode");
641             y(bool, true);
642             y(map_close);
643         }
644     }
645     y(array_close);
646
647     if (inplace_restart && con->window != NULL) {
648         ystr("depth");
649         y(integer, con->depth);
650     }
651
652     if (inplace_restart && con->type == CT_ROOT && previous_workspace_name) {
653         ystr("previous_workspace_name");
654         ystr(previous_workspace_name);
655     }
656
657     y(map_close);
658 }
659
660 static void dump_bar_bindings(yajl_gen gen, Barconfig *config) {
661     if (TAILQ_EMPTY(&(config->bar_bindings)))
662         return;
663
664     ystr("bindings");
665     y(array_open);
666
667     struct Barbinding *current;
668     TAILQ_FOREACH(current, &(config->bar_bindings), bindings) {
669         y(map_open);
670
671         ystr("input_code");
672         y(integer, current->input_code);
673         ystr("command");
674         ystr(current->command);
675         ystr("release");
676         y(bool, current->release == B_UPON_KEYRELEASE);
677
678         y(map_close);
679     }
680
681     y(array_close);
682 }
683
684 static char *canonicalize_output_name(char *name) {
685     /* Do not canonicalize special output names. */
686     if (strcasecmp(name, "primary") == 0) {
687         return name;
688     }
689     Output *output = get_output_by_name(name, false);
690     return output ? output_primary_name(output) : name;
691 }
692
693 static void dump_bar_config(yajl_gen gen, Barconfig *config) {
694     y(map_open);
695
696     ystr("id");
697     ystr(config->id);
698
699     if (config->num_outputs > 0) {
700         ystr("outputs");
701         y(array_open);
702         for (int c = 0; c < config->num_outputs; c++) {
703             /* Convert monitor names (RandR ≥ 1.5) or output names
704              * (RandR < 1.5) into monitor names. This way, existing
705              * configs which use output names transparently keep
706              * working. */
707             ystr(canonicalize_output_name(config->outputs[c]));
708         }
709         y(array_close);
710     }
711
712     if (!TAILQ_EMPTY(&(config->tray_outputs))) {
713         ystr("tray_outputs");
714         y(array_open);
715
716         struct tray_output_t *tray_output;
717         TAILQ_FOREACH(tray_output, &(config->tray_outputs), tray_outputs) {
718             ystr(canonicalize_output_name(tray_output->output));
719         }
720
721         y(array_close);
722     }
723
724 #define YSTR_IF_SET(name)       \
725     do {                        \
726         if (config->name) {     \
727             ystr(#name);        \
728             ystr(config->name); \
729         }                       \
730     } while (0)
731
732     ystr("tray_padding");
733     y(integer, config->tray_padding);
734
735     YSTR_IF_SET(socket_path);
736
737     ystr("mode");
738     switch (config->mode) {
739         case M_HIDE:
740             ystr("hide");
741             break;
742         case M_INVISIBLE:
743             ystr("invisible");
744             break;
745         case M_DOCK:
746         default:
747             ystr("dock");
748             break;
749     }
750
751     ystr("hidden_state");
752     switch (config->hidden_state) {
753         case S_SHOW:
754             ystr("show");
755             break;
756         case S_HIDE:
757         default:
758             ystr("hide");
759             break;
760     }
761
762     ystr("modifier");
763     y(integer, config->modifier);
764
765     dump_bar_bindings(gen, config);
766
767     ystr("position");
768     if (config->position == P_BOTTOM)
769         ystr("bottom");
770     else
771         ystr("top");
772
773     YSTR_IF_SET(status_command);
774     YSTR_IF_SET(font);
775
776     if (config->separator_symbol) {
777         ystr("separator_symbol");
778         ystr(config->separator_symbol);
779     }
780
781     ystr("workspace_buttons");
782     y(bool, !config->hide_workspace_buttons);
783
784     ystr("strip_workspace_numbers");
785     y(bool, config->strip_workspace_numbers);
786
787     ystr("strip_workspace_name");
788     y(bool, config->strip_workspace_name);
789
790     ystr("binding_mode_indicator");
791     y(bool, !config->hide_binding_mode_indicator);
792
793     ystr("verbose");
794     y(bool, config->verbose);
795
796 #undef YSTR_IF_SET
797 #define YSTR_IF_SET(name)              \
798     do {                               \
799         if (config->colors.name) {     \
800             ystr(#name);               \
801             ystr(config->colors.name); \
802         }                              \
803     } while (0)
804
805     ystr("colors");
806     y(map_open);
807     YSTR_IF_SET(background);
808     YSTR_IF_SET(statusline);
809     YSTR_IF_SET(separator);
810     YSTR_IF_SET(focused_background);
811     YSTR_IF_SET(focused_statusline);
812     YSTR_IF_SET(focused_separator);
813     YSTR_IF_SET(focused_workspace_border);
814     YSTR_IF_SET(focused_workspace_bg);
815     YSTR_IF_SET(focused_workspace_text);
816     YSTR_IF_SET(active_workspace_border);
817     YSTR_IF_SET(active_workspace_bg);
818     YSTR_IF_SET(active_workspace_text);
819     YSTR_IF_SET(inactive_workspace_border);
820     YSTR_IF_SET(inactive_workspace_bg);
821     YSTR_IF_SET(inactive_workspace_text);
822     YSTR_IF_SET(urgent_workspace_border);
823     YSTR_IF_SET(urgent_workspace_bg);
824     YSTR_IF_SET(urgent_workspace_text);
825     YSTR_IF_SET(binding_mode_border);
826     YSTR_IF_SET(binding_mode_bg);
827     YSTR_IF_SET(binding_mode_text);
828     y(map_close);
829
830     y(map_close);
831 #undef YSTR_IF_SET
832 }
833
834 IPC_HANDLER(tree) {
835     setlocale(LC_NUMERIC, "C");
836     yajl_gen gen = ygenalloc();
837     dump_node(gen, croot, false);
838     setlocale(LC_NUMERIC, "");
839
840     const unsigned char *payload;
841     ylength length;
842     y(get_buf, &payload, &length);
843
844     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_TREE, payload);
845     y(free);
846 }
847
848 /*
849  * Formats the reply message for a GET_WORKSPACES request and sends it to the
850  * client
851  *
852  */
853 IPC_HANDLER(get_workspaces) {
854     yajl_gen gen = ygenalloc();
855     y(array_open);
856
857     Con *focused_ws = con_get_workspace(focused);
858
859     Con *output;
860     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
861         if (con_is_internal(output))
862             continue;
863         Con *ws;
864         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
865             assert(ws->type == CT_WORKSPACE);
866             y(map_open);
867
868             ystr("num");
869             y(integer, ws->num);
870
871             ystr("name");
872             ystr(ws->name);
873
874             ystr("visible");
875             y(bool, workspace_is_visible(ws));
876
877             ystr("focused");
878             y(bool, ws == focused_ws);
879
880             ystr("rect");
881             y(map_open);
882             ystr("x");
883             y(integer, ws->rect.x);
884             ystr("y");
885             y(integer, ws->rect.y);
886             ystr("width");
887             y(integer, ws->rect.width);
888             ystr("height");
889             y(integer, ws->rect.height);
890             y(map_close);
891
892             ystr("output");
893             ystr(output->name);
894
895             ystr("urgent");
896             y(bool, ws->urgent);
897
898             y(map_close);
899         }
900     }
901
902     y(array_close);
903
904     const unsigned char *payload;
905     ylength length;
906     y(get_buf, &payload, &length);
907
908     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
909     y(free);
910 }
911
912 /*
913  * Formats the reply message for a GET_OUTPUTS request and sends it to the
914  * client
915  *
916  */
917 IPC_HANDLER(get_outputs) {
918     yajl_gen gen = ygenalloc();
919     y(array_open);
920
921     Output *output;
922     TAILQ_FOREACH(output, &outputs, outputs) {
923         y(map_open);
924
925         ystr("name");
926         ystr(output_primary_name(output));
927
928         ystr("active");
929         y(bool, output->active);
930
931         ystr("primary");
932         y(bool, output->primary);
933
934         ystr("rect");
935         y(map_open);
936         ystr("x");
937         y(integer, output->rect.x);
938         ystr("y");
939         y(integer, output->rect.y);
940         ystr("width");
941         y(integer, output->rect.width);
942         ystr("height");
943         y(integer, output->rect.height);
944         y(map_close);
945
946         ystr("current_workspace");
947         Con *ws = NULL;
948         if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
949             ystr(ws->name);
950         else
951             y(null);
952
953         y(map_close);
954     }
955
956     y(array_close);
957
958     const unsigned char *payload;
959     ylength length;
960     y(get_buf, &payload, &length);
961
962     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
963     y(free);
964 }
965
966 /*
967  * Formats the reply message for a GET_MARKS request and sends it to the
968  * client
969  *
970  */
971 IPC_HANDLER(get_marks) {
972     yajl_gen gen = ygenalloc();
973     y(array_open);
974
975     Con *con;
976     TAILQ_FOREACH(con, &all_cons, all_cons) {
977         mark_t *mark;
978         TAILQ_FOREACH(mark, &(con->marks_head), marks) {
979             ystr(mark->name);
980         }
981     }
982
983     y(array_close);
984
985     const unsigned char *payload;
986     ylength length;
987     y(get_buf, &payload, &length);
988
989     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_MARKS, payload);
990     y(free);
991 }
992
993 /*
994  * Returns the version of i3
995  *
996  */
997 IPC_HANDLER(get_version) {
998     yajl_gen gen = ygenalloc();
999     y(map_open);
1000
1001     ystr("major");
1002     y(integer, MAJOR_VERSION);
1003
1004     ystr("minor");
1005     y(integer, MINOR_VERSION);
1006
1007     ystr("patch");
1008     y(integer, PATCH_VERSION);
1009
1010     ystr("human_readable");
1011     ystr(i3_version);
1012
1013     ystr("loaded_config_file_name");
1014     ystr(current_configpath);
1015
1016     y(map_close);
1017
1018     const unsigned char *payload;
1019     ylength length;
1020     y(get_buf, &payload, &length);
1021
1022     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_VERSION, payload);
1023     y(free);
1024 }
1025
1026 /*
1027  * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
1028  * client.
1029  *
1030  */
1031 IPC_HANDLER(get_bar_config) {
1032     yajl_gen gen = ygenalloc();
1033
1034     /* If no ID was passed, we return a JSON array with all IDs */
1035     if (message_size == 0) {
1036         y(array_open);
1037         Barconfig *current;
1038         TAILQ_FOREACH(current, &barconfigs, configs) {
1039             ystr(current->id);
1040         }
1041         y(array_close);
1042
1043         const unsigned char *payload;
1044         ylength length;
1045         y(get_buf, &payload, &length);
1046
1047         ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
1048         y(free);
1049         return;
1050     }
1051
1052     /* To get a properly terminated buffer, we copy
1053      * message_size bytes out of the buffer */
1054     char *bar_id = NULL;
1055     sasprintf(&bar_id, "%.*s", message_size, message);
1056     LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
1057     Barconfig *current, *config = NULL;
1058     TAILQ_FOREACH(current, &barconfigs, configs) {
1059         if (strcmp(current->id, bar_id) != 0)
1060             continue;
1061
1062         config = current;
1063         break;
1064     }
1065     free(bar_id);
1066
1067     if (!config) {
1068         /* If we did not find a config for the given ID, the reply will contain
1069          * a null 'id' field. */
1070         y(map_open);
1071
1072         ystr("id");
1073         y(null);
1074
1075         y(map_close);
1076     } else {
1077         dump_bar_config(gen, config);
1078     }
1079
1080     const unsigned char *payload;
1081     ylength length;
1082     y(get_buf, &payload, &length);
1083
1084     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
1085     y(free);
1086 }
1087
1088 /*
1089  * Returns a list of configured binding modes
1090  *
1091  */
1092 IPC_HANDLER(get_binding_modes) {
1093     yajl_gen gen = ygenalloc();
1094
1095     y(array_open);
1096     struct Mode *mode;
1097     SLIST_FOREACH(mode, &modes, modes) {
1098         ystr(mode->name);
1099     }
1100     y(array_close);
1101
1102     const unsigned char *payload;
1103     ylength length;
1104     y(get_buf, &payload, &length);
1105
1106     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_BINDING_MODES, payload);
1107     y(free);
1108 }
1109
1110 /*
1111  * Callback for the YAJL parser (will be called when a string is parsed).
1112  *
1113  */
1114 static int add_subscription(void *extra, const unsigned char *s,
1115                             ylength len) {
1116     ipc_client *client = extra;
1117
1118     DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
1119     int event = client->num_events;
1120
1121     client->num_events++;
1122     client->events = srealloc(client->events, client->num_events * sizeof(char *));
1123     /* We copy the string because it is not null-terminated and strndup()
1124      * is missing on some BSD systems */
1125     client->events[event] = scalloc(len + 1, 1);
1126     memcpy(client->events[event], s, len);
1127
1128     DLOG("client is now subscribed to:\n");
1129     for (int i = 0; i < client->num_events; i++) {
1130         DLOG("event %s\n", client->events[i]);
1131     }
1132     DLOG("(done)\n");
1133
1134     return 1;
1135 }
1136
1137 /*
1138  * Subscribes this connection to the event types which were given as a JSON
1139  * serialized array in the payload field of the message.
1140  *
1141  */
1142 IPC_HANDLER(subscribe) {
1143     yajl_handle p;
1144     yajl_status stat;
1145
1146     /* Setup the JSON parser */
1147     static yajl_callbacks callbacks = {
1148         .yajl_string = add_subscription,
1149     };
1150
1151     p = yalloc(&callbacks, (void *)client);
1152     stat = yajl_parse(p, (const unsigned char *)message, message_size);
1153     if (stat != yajl_status_ok) {
1154         unsigned char *err;
1155         err = yajl_get_error(p, true, (const unsigned char *)message,
1156                              message_size);
1157         ELOG("YAJL parse error: %s\n", err);
1158         yajl_free_error(p, err);
1159
1160         const char *reply = "{\"success\":false}";
1161         ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1162         yajl_free(p);
1163         return;
1164     }
1165     yajl_free(p);
1166     const char *reply = "{\"success\":true}";
1167     ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1168
1169     if (client->first_tick_sent) {
1170         return;
1171     }
1172
1173     bool is_tick = false;
1174     for (int i = 0; i < client->num_events; i++) {
1175         if (strcmp(client->events[i], "tick") == 0) {
1176             is_tick = true;
1177             break;
1178         }
1179     }
1180     if (!is_tick) {
1181         return;
1182     }
1183
1184     client->first_tick_sent = true;
1185     const char *payload = "{\"first\":true,\"payload\":\"\"}";
1186     ipc_send_client_message(client, strlen(payload), I3_IPC_EVENT_TICK, (const uint8_t *)payload);
1187 }
1188
1189 /*
1190  * Returns the raw last loaded i3 configuration file contents.
1191  */
1192 IPC_HANDLER(get_config) {
1193     yajl_gen gen = ygenalloc();
1194
1195     y(map_open);
1196
1197     ystr("config");
1198     ystr(current_config);
1199
1200     y(map_close);
1201
1202     const unsigned char *payload;
1203     ylength length;
1204     y(get_buf, &payload, &length);
1205
1206     ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_CONFIG, payload);
1207     y(free);
1208 }
1209
1210 /*
1211  * Sends the tick event from the message payload to subscribers. Establishes a
1212  * synchronization point in event-related tests.
1213  */
1214 IPC_HANDLER(send_tick) {
1215     yajl_gen gen = ygenalloc();
1216
1217     y(map_open);
1218
1219     ystr("first");
1220     y(bool, false);
1221
1222     ystr("payload");
1223     yajl_gen_string(gen, (unsigned char *)message, message_size);
1224
1225     y(map_close);
1226
1227     const unsigned char *payload;
1228     ylength length;
1229     y(get_buf, &payload, &length);
1230
1231     ipc_send_event("tick", I3_IPC_EVENT_TICK, (const char *)payload);
1232     y(free);
1233
1234     const char *reply = "{\"success\":true}";
1235     ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_TICK, (const uint8_t *)reply);
1236     DLOG("Sent tick event\n");
1237 }
1238
1239 struct sync_state {
1240     char *last_key;
1241     uint32_t rnd;
1242     xcb_window_t window;
1243 };
1244
1245 static int _sync_json_key(void *extra, const unsigned char *val, size_t len) {
1246     struct sync_state *state = extra;
1247     FREE(state->last_key);
1248     state->last_key = scalloc(len + 1, 1);
1249     memcpy(state->last_key, val, len);
1250     return 1;
1251 }
1252
1253 static int _sync_json_int(void *extra, long long val) {
1254     struct sync_state *state = extra;
1255     if (strcasecmp(state->last_key, "rnd") == 0) {
1256         state->rnd = val;
1257     } else if (strcasecmp(state->last_key, "window") == 0) {
1258         state->window = (xcb_window_t)val;
1259     }
1260     return 1;
1261 }
1262
1263 IPC_HANDLER(sync) {
1264     yajl_handle p;
1265     yajl_status stat;
1266
1267     /* Setup the JSON parser */
1268     static yajl_callbacks callbacks = {
1269         .yajl_map_key = _sync_json_key,
1270         .yajl_integer = _sync_json_int,
1271     };
1272
1273     struct sync_state state;
1274     memset(&state, '\0', sizeof(struct sync_state));
1275     p = yalloc(&callbacks, (void *)&state);
1276     stat = yajl_parse(p, (const unsigned char *)message, message_size);
1277     FREE(state.last_key);
1278     if (stat != yajl_status_ok) {
1279         unsigned char *err;
1280         err = yajl_get_error(p, true, (const unsigned char *)message,
1281                              message_size);
1282         ELOG("YAJL parse error: %s\n", err);
1283         yajl_free_error(p, err);
1284
1285         const char *reply = "{\"success\":false}";
1286         ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1287         yajl_free(p);
1288         return;
1289     }
1290     yajl_free(p);
1291
1292     DLOG("received IPC sync request (rnd = %d, window = 0x%08x)\n", state.rnd, state.window);
1293     sync_respond(state.window, state.rnd);
1294     const char *reply = "{\"success\":true}";
1295     ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1296 }
1297
1298 /* The index of each callback function corresponds to the numeric
1299  * value of the message type (see include/i3/ipc.h) */
1300 handler_t handlers[12] = {
1301     handle_run_command,
1302     handle_get_workspaces,
1303     handle_subscribe,
1304     handle_get_outputs,
1305     handle_tree,
1306     handle_get_marks,
1307     handle_get_bar_config,
1308     handle_get_version,
1309     handle_get_binding_modes,
1310     handle_get_config,
1311     handle_send_tick,
1312     handle_sync,
1313 };
1314
1315 /*
1316  * Handler for activity on a client connection, receives a message from a
1317  * client.
1318  *
1319  * For now, the maximum message size is 2048. I’m not sure for what the
1320  * IPC interface will be used in the future, thus I’m not implementing a
1321  * mechanism for arbitrarily long messages, as it seems like overkill
1322  * at the moment.
1323  *
1324  */
1325 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
1326     uint32_t message_type;
1327     uint32_t message_length;
1328     uint8_t *message = NULL;
1329     ipc_client *client = (ipc_client *)w->data;
1330     assert(client->fd == w->fd);
1331
1332     int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
1333     /* EOF or other error */
1334     if (ret < 0) {
1335         /* Was this a spurious read? See ev(3) */
1336         if (ret == -1 && errno == EAGAIN) {
1337             FREE(message);
1338             return;
1339         }
1340
1341         /* If not, there was some kind of error. We don’t bother and close the
1342          * connection. Delete the client from the list of clients. */
1343         free_ipc_client(client);
1344         FREE(message);
1345         return;
1346     }
1347
1348     if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
1349         DLOG("Unhandled message type: %d\n", message_type);
1350     else {
1351         handler_t h = handlers[message_type];
1352         h(client, message, 0, message_length, message_type);
1353     }
1354
1355     FREE(message);
1356 }
1357
1358 static void ipc_client_timeout(EV_P_ ev_timer *w, int revents) {
1359     /* No need to be polite and check for writeability, the other callback would
1360      * have been called by now. */
1361     ipc_client *client = (ipc_client *)w->data;
1362
1363     char *cmdline = NULL;
1364 #if defined(__linux__) && defined(SO_PEERCRED)
1365     struct ucred peercred;
1366     socklen_t so_len = sizeof(peercred);
1367     if (getsockopt(client->fd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) != 0) {
1368         goto end;
1369     }
1370     char *exepath;
1371     sasprintf(&exepath, "/proc/%d/cmdline", peercred.pid);
1372
1373     int fd = open(exepath, O_RDONLY);
1374     free(exepath);
1375     if (fd == -1) {
1376         goto end;
1377     }
1378     char buf[512] = {'\0'}; /* cut off cmdline for the error message. */
1379     const ssize_t n = read(fd, buf, sizeof(buf));
1380     close(fd);
1381     if (n < 0) {
1382         goto end;
1383     }
1384     for (char *walk = buf; walk < buf + n - 1; walk++) {
1385         if (*walk == '\0') {
1386             *walk = ' ';
1387         }
1388     }
1389     cmdline = buf;
1390
1391     if (cmdline) {
1392         ELOG("client %p with pid %d and cmdline '%s' on fd %d timed out, killing\n", client, peercred.pid, cmdline, client->fd);
1393     }
1394
1395 end:
1396 #endif
1397     if (!cmdline) {
1398         ELOG("client %p on fd %d timed out, killing\n", client, client->fd);
1399     }
1400
1401     free_ipc_client(client);
1402 }
1403
1404 static void ipc_socket_writeable_cb(EV_P_ ev_io *w, int revents) {
1405     DLOG("fd %d writeable\n", w->fd);
1406     ipc_client *client = (ipc_client *)w->data;
1407
1408     /* If this callback is called then there should be a corresponding active
1409      * timer. */
1410     assert(client->timeout != NULL);
1411     ipc_push_pending(client);
1412 }
1413
1414 /*
1415  * Handler for activity on the listening socket, meaning that a new client
1416  * has just connected and we should accept() him. Sets up the event handler
1417  * for activity on the new connection and inserts the file descriptor into
1418  * the list of clients.
1419  *
1420  */
1421 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
1422     struct sockaddr_un peer;
1423     socklen_t len = sizeof(struct sockaddr_un);
1424     int fd;
1425     if ((fd = accept(w->fd, (struct sockaddr *)&peer, &len)) < 0) {
1426         if (errno != EINTR) {
1427             perror("accept()");
1428         }
1429         return;
1430     }
1431
1432     /* Close this file descriptor on exec() */
1433     (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1434
1435     set_nonblock(fd);
1436
1437     ipc_client *client = scalloc(1, sizeof(ipc_client));
1438     client->fd = fd;
1439
1440     client->read_callback = scalloc(1, sizeof(struct ev_io));
1441     client->read_callback->data = client;
1442     ev_io_init(client->read_callback, ipc_receive_message, fd, EV_READ);
1443     ev_io_start(EV_A_ client->read_callback);
1444
1445     client->write_callback = scalloc(1, sizeof(struct ev_io));
1446     client->write_callback->data = client;
1447     ev_io_init(client->write_callback, ipc_socket_writeable_cb, fd, EV_WRITE);
1448
1449     DLOG("IPC: new client connected on fd %d\n", w->fd);
1450     TAILQ_INSERT_TAIL(&all_clients, client, clients);
1451 }
1452
1453 /*
1454  * Creates the UNIX domain socket at the given path, sets it to non-blocking
1455  * mode, bind()s and listen()s on it.
1456  *
1457  */
1458 int ipc_create_socket(const char *filename) {
1459     int sockfd;
1460
1461     FREE(current_socketpath);
1462
1463     char *resolved = resolve_tilde(filename);
1464     DLOG("Creating IPC-socket at %s\n", resolved);
1465     char *copy = sstrdup(resolved);
1466     const char *dir = dirname(copy);
1467     if (!path_exists(dir))
1468         mkdirp(dir, DEFAULT_DIR_MODE);
1469     free(copy);
1470
1471     /* Unlink the unix domain socket before */
1472     unlink(resolved);
1473
1474     if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1475         perror("socket()");
1476         free(resolved);
1477         return -1;
1478     }
1479
1480     (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1481
1482     struct sockaddr_un addr;
1483     memset(&addr, 0, sizeof(struct sockaddr_un));
1484     addr.sun_family = AF_LOCAL;
1485     strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
1486     if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
1487         perror("bind()");
1488         free(resolved);
1489         return -1;
1490     }
1491
1492     set_nonblock(sockfd);
1493
1494     if (listen(sockfd, 5) < 0) {
1495         perror("listen()");
1496         free(resolved);
1497         return -1;
1498     }
1499
1500     current_socketpath = resolved;
1501     return sockfd;
1502 }
1503
1504 /*
1505  * Generates a json workspace event. Returns a dynamically allocated yajl
1506  * generator. Free with yajl_gen_free().
1507  */
1508 yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old) {
1509     setlocale(LC_NUMERIC, "C");
1510     yajl_gen gen = ygenalloc();
1511
1512     y(map_open);
1513
1514     ystr("change");
1515     ystr(change);
1516
1517     ystr("current");
1518     if (current == NULL)
1519         y(null);
1520     else
1521         dump_node(gen, current, false);
1522
1523     ystr("old");
1524     if (old == NULL)
1525         y(null);
1526     else
1527         dump_node(gen, old, false);
1528
1529     y(map_close);
1530
1531     setlocale(LC_NUMERIC, "");
1532
1533     return gen;
1534 }
1535
1536 /*
1537  * For the workspace events we send, along with the usual "change" field, also
1538  * the workspace container in "current". For focus events, we send the
1539  * previously focused workspace in "old".
1540  */
1541 void ipc_send_workspace_event(const char *change, Con *current, Con *old) {
1542     yajl_gen gen = ipc_marshal_workspace_event(change, current, old);
1543
1544     const unsigned char *payload;
1545     ylength length;
1546     y(get_buf, &payload, &length);
1547
1548     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
1549
1550     y(free);
1551 }
1552
1553 /*
1554  * For the window events we send, along the usual "change" field,
1555  * also the window container, in "container".
1556  */
1557 void ipc_send_window_event(const char *property, Con *con) {
1558     DLOG("Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1559          property, con, (con->window ? con->window->id : XCB_WINDOW_NONE));
1560
1561     setlocale(LC_NUMERIC, "C");
1562     yajl_gen gen = ygenalloc();
1563
1564     y(map_open);
1565
1566     ystr("change");
1567     ystr(property);
1568
1569     ystr("container");
1570     dump_node(gen, con, false);
1571
1572     y(map_close);
1573
1574     const unsigned char *payload;
1575     ylength length;
1576     y(get_buf, &payload, &length);
1577
1578     ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload);
1579     y(free);
1580     setlocale(LC_NUMERIC, "");
1581 }
1582
1583 /*
1584  * For the barconfig update events, we send the serialized barconfig.
1585  */
1586 void ipc_send_barconfig_update_event(Barconfig *barconfig) {
1587     DLOG("Issue barconfig_update event for id = %s\n", barconfig->id);
1588     setlocale(LC_NUMERIC, "C");
1589     yajl_gen gen = ygenalloc();
1590
1591     dump_bar_config(gen, barconfig);
1592
1593     const unsigned char *payload;
1594     ylength length;
1595     y(get_buf, &payload, &length);
1596
1597     ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (const char *)payload);
1598     y(free);
1599     setlocale(LC_NUMERIC, "");
1600 }
1601
1602 /*
1603  * For the binding events, we send the serialized binding struct.
1604  */
1605 void ipc_send_binding_event(const char *event_type, Binding *bind) {
1606     DLOG("Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->symbol, bind->keycode);
1607
1608     setlocale(LC_NUMERIC, "C");
1609
1610     yajl_gen gen = ygenalloc();
1611
1612     y(map_open);
1613
1614     ystr("change");
1615     ystr(event_type);
1616
1617     ystr("binding");
1618     dump_binding(gen, bind);
1619
1620     y(map_close);
1621
1622     const unsigned char *payload;
1623     ylength length;
1624     y(get_buf, &payload, &length);
1625
1626     ipc_send_event("binding", I3_IPC_EVENT_BINDING, (const char *)payload);
1627
1628     y(free);
1629     setlocale(LC_NUMERIC, "");
1630 }