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