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