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 || strcasecmp(name, "nonprimary") == 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(workspace_command);
831 YSTR_IF_SET(font);
832
833 if (config->bar_height) {
834 ystr("bar_height");
835 y(integer, config->bar_height);
836 }
837
838 dump_rect(gen, "padding", config->padding);
839
840 if (config->separator_symbol) {
841 ystr("separator_symbol");
842 ystr(config->separator_symbol);
843 }
844
845 ystr("workspace_buttons");
846 y(bool, !config->hide_workspace_buttons);
847
848 ystr("workspace_min_width");
849 y(integer, config->workspace_min_width);
850
851 ystr("strip_workspace_numbers");
852 y(bool, config->strip_workspace_numbers);
853
854 ystr("strip_workspace_name");
855 y(bool, config->strip_workspace_name);
856
857 ystr("binding_mode_indicator");
858 y(bool, !config->hide_binding_mode_indicator);
859
860 ystr("verbose");
861 y(bool, config->verbose);
862
863#undef YSTR_IF_SET
864#define YSTR_IF_SET(name) \
865 do { \
866 if (config->colors.name) { \
867 ystr(#name); \
868 ystr(config->colors.name); \
869 } \
870 } while (0)
871
872 ystr("colors");
873 y(map_open);
874 YSTR_IF_SET(background);
875 YSTR_IF_SET(statusline);
876 YSTR_IF_SET(separator);
877 YSTR_IF_SET(focused_background);
878 YSTR_IF_SET(focused_statusline);
879 YSTR_IF_SET(focused_separator);
880 YSTR_IF_SET(focused_workspace_border);
881 YSTR_IF_SET(focused_workspace_bg);
882 YSTR_IF_SET(focused_workspace_text);
883 YSTR_IF_SET(active_workspace_border);
884 YSTR_IF_SET(active_workspace_bg);
885 YSTR_IF_SET(active_workspace_text);
886 YSTR_IF_SET(inactive_workspace_border);
887 YSTR_IF_SET(inactive_workspace_bg);
888 YSTR_IF_SET(inactive_workspace_text);
889 YSTR_IF_SET(urgent_workspace_border);
890 YSTR_IF_SET(urgent_workspace_bg);
891 YSTR_IF_SET(urgent_workspace_text);
892 YSTR_IF_SET(binding_mode_border);
893 YSTR_IF_SET(binding_mode_bg);
894 YSTR_IF_SET(binding_mode_text);
895 y(map_close);
896
897 y(map_close);
898#undef YSTR_IF_SET
899}
900
902 setlocale(LC_NUMERIC, "C");
903 yajl_gen gen = ygenalloc();
904 dump_node(gen, croot, false);
905 setlocale(LC_NUMERIC, "");
906
907 const unsigned char *payload;
908 ylength length;
909 y(get_buf, &payload, &length);
910
911 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_TREE, payload);
912 y(free);
913}
914
915/*
916 * Formats the reply message for a GET_WORKSPACES request and sends it to the
917 * client
918 *
919 */
920IPC_HANDLER(get_workspaces) {
921 yajl_gen gen = ygenalloc();
922 y(array_open);
923
924 Con *focused_ws = con_get_workspace(focused);
925
926 Con *output;
927 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
929 continue;
930 Con *ws;
931 TAILQ_FOREACH (ws, &(output_get_content(output)->nodes_head), nodes) {
932 assert(ws->type == CT_WORKSPACE);
933 y(map_open);
934
935 ystr("id");
936 y(integer, (uintptr_t)ws);
937
938 ystr("num");
939 y(integer, ws->num);
940
941 ystr("name");
942 ystr(ws->name);
943
944 ystr("visible");
945 y(bool, workspace_is_visible(ws));
946
947 ystr("focused");
948 y(bool, ws == focused_ws);
949
950 ystr("rect");
951 y(map_open);
952 ystr("x");
953 y(integer, ws->rect.x);
954 ystr("y");
955 y(integer, ws->rect.y);
956 ystr("width");
957 y(integer, ws->rect.width);
958 ystr("height");
959 y(integer, ws->rect.height);
960 y(map_close);
961
962 ystr("output");
963 ystr(output->name);
964
965 ystr("urgent");
966 y(bool, ws->urgent);
967
968 y(map_close);
969 }
970 }
971
972 y(array_close);
973
974 const unsigned char *payload;
975 ylength length;
976 y(get_buf, &payload, &length);
977
978 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
979 y(free);
980}
981
982/*
983 * Formats the reply message for a GET_OUTPUTS request and sends it to the
984 * client
985 *
986 */
987IPC_HANDLER(get_outputs) {
988 yajl_gen gen = ygenalloc();
989 y(array_open);
990
991 Output *output;
993 y(map_open);
994
995 ystr("name");
997
998 ystr("active");
999 y(bool, output->active);
1000
1001 ystr("primary");
1002 y(bool, output->primary);
1003
1004 ystr("rect");
1005 y(map_open);
1006 ystr("x");
1007 y(integer, output->rect.x);
1008 ystr("y");
1009 y(integer, output->rect.y);
1010 ystr("width");
1011 y(integer, output->rect.width);
1012 ystr("height");
1013 y(integer, output->rect.height);
1014 y(map_close);
1015
1016 ystr("current_workspace");
1017 Con *ws = NULL;
1018 if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
1019 ystr(ws->name);
1020 else
1021 y(null);
1022
1023 y(map_close);
1024 }
1025
1026 y(array_close);
1027
1028 const unsigned char *payload;
1029 ylength length;
1030 y(get_buf, &payload, &length);
1031
1032 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
1033 y(free);
1034}
1035
1036/*
1037 * Formats the reply message for a GET_MARKS request and sends it to the
1038 * client
1039 *
1040 */
1041IPC_HANDLER(get_marks) {
1042 yajl_gen gen = ygenalloc();
1043 y(array_open);
1044
1045 Con *con;
1047 mark_t *mark;
1048 TAILQ_FOREACH (mark, &(con->marks_head), marks) {
1049 ystr(mark->name);
1050 }
1051 }
1052
1053 y(array_close);
1054
1055 const unsigned char *payload;
1056 ylength length;
1057 y(get_buf, &payload, &length);
1058
1059 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_MARKS, payload);
1060 y(free);
1061}
1062
1063/*
1064 * Returns the version of i3
1065 *
1066 */
1067IPC_HANDLER(get_version) {
1068 yajl_gen gen = ygenalloc();
1069 y(map_open);
1070
1071 ystr("major");
1072 y(integer, MAJOR_VERSION);
1073
1074 ystr("minor");
1075 y(integer, MINOR_VERSION);
1076
1077 ystr("patch");
1078 y(integer, PATCH_VERSION);
1079
1080 ystr("human_readable");
1082
1083 ystr("loaded_config_file_name");
1085
1086 ystr("included_config_file_names");
1087 y(array_open);
1088 IncludedFile *file;
1089 TAILQ_FOREACH (file, &included_files, files) {
1090 if (file == TAILQ_FIRST(&included_files)) {
1091 /* Skip the first file, which is current_configpath. */
1092 continue;
1093 }
1094 ystr(file->path);
1095 }
1096 y(array_close);
1097 y(map_close);
1098
1099 const unsigned char *payload;
1100 ylength length;
1101 y(get_buf, &payload, &length);
1102
1103 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_VERSION, payload);
1104 y(free);
1105}
1106
1107/*
1108 * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
1109 * client.
1110 *
1111 */
1112IPC_HANDLER(get_bar_config) {
1113 yajl_gen gen = ygenalloc();
1114
1115 /* If no ID was passed, we return a JSON array with all IDs */
1116 if (message_size == 0) {
1117 y(array_open);
1118 Barconfig *current;
1119 TAILQ_FOREACH (current, &barconfigs, configs) {
1120 ystr(current->id);
1121 }
1122 y(array_close);
1123
1124 const unsigned char *payload;
1125 ylength length;
1126 y(get_buf, &payload, &length);
1127
1128 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
1129 y(free);
1130 return;
1131 }
1132
1133 /* To get a properly terminated buffer, we copy
1134 * message_size bytes out of the buffer */
1135 char *bar_id = NULL;
1136 sasprintf(&bar_id, "%.*s", message_size, message);
1137 LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
1138 Barconfig *current, *config = NULL;
1139 TAILQ_FOREACH (current, &barconfigs, configs) {
1140 if (strcmp(current->id, bar_id) != 0)
1141 continue;
1142
1143 config = current;
1144 break;
1145 }
1146 free(bar_id);
1147
1148 if (!config) {
1149 /* If we did not find a config for the given ID, the reply will contain
1150 * a null 'id' field. */
1151 y(map_open);
1152
1153 ystr("id");
1154 y(null);
1155
1156 y(map_close);
1157 } else {
1158 dump_bar_config(gen, config);
1159 }
1160
1161 const unsigned char *payload;
1162 ylength length;
1163 y(get_buf, &payload, &length);
1164
1165 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
1166 y(free);
1167}
1168
1169/*
1170 * Returns a list of configured binding modes
1171 *
1172 */
1173IPC_HANDLER(get_binding_modes) {
1174 yajl_gen gen = ygenalloc();
1175
1176 y(array_open);
1177 struct Mode *mode;
1178 SLIST_FOREACH (mode, &modes, modes) {
1179 ystr(mode->name);
1180 }
1181 y(array_close);
1182
1183 const unsigned char *payload;
1184 ylength length;
1185 y(get_buf, &payload, &length);
1186
1187 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_BINDING_MODES, payload);
1188 y(free);
1189}
1190
1191/*
1192 * Callback for the YAJL parser (will be called when a string is parsed).
1193 *
1194 */
1195static int add_subscription(void *extra, const unsigned char *s,
1196 ylength len) {
1197 ipc_client *client = extra;
1198
1199 DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
1200 int event = client->num_events;
1201
1202 client->num_events++;
1203 client->events = srealloc(client->events, client->num_events * sizeof(char *));
1204 /* We copy the string because it is not null-terminated and strndup()
1205 * is missing on some BSD systems */
1206 client->events[event] = scalloc(len + 1, 1);
1207 memcpy(client->events[event], s, len);
1208
1209 DLOG("client is now subscribed to:\n");
1210 for (int i = 0; i < client->num_events; i++) {
1211 DLOG("event %s\n", client->events[i]);
1212 }
1213 DLOG("(done)\n");
1214
1215 return 1;
1216}
1217
1218/*
1219 * Subscribes this connection to the event types which were given as a JSON
1220 * serialized array in the payload field of the message.
1221 *
1222 */
1223IPC_HANDLER(subscribe) {
1224 yajl_handle p;
1225 yajl_status stat;
1226
1227 /* Setup the JSON parser */
1228 static yajl_callbacks callbacks = {
1229 .yajl_string = add_subscription,
1230 };
1231
1232 p = yalloc(&callbacks, (void *)client);
1233 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1234 if (stat != yajl_status_ok) {
1235 unsigned char *err;
1236 err = yajl_get_error(p, true, (const unsigned char *)message,
1237 message_size);
1238 ELOG("YAJL parse error: %s\n", err);
1239 yajl_free_error(p, err);
1240
1241 const char *reply = "{\"success\":false}";
1242 ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1243 yajl_free(p);
1244 return;
1245 }
1246 yajl_free(p);
1247 const char *reply = "{\"success\":true}";
1248 ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1249
1250 if (client->first_tick_sent) {
1251 return;
1252 }
1253
1254 bool is_tick = false;
1255 for (int i = 0; i < client->num_events; i++) {
1256 if (strcmp(client->events[i], "tick") == 0) {
1257 is_tick = true;
1258 break;
1259 }
1260 }
1261 if (!is_tick) {
1262 return;
1263 }
1264
1265 client->first_tick_sent = true;
1266 const char *payload = "{\"first\":true,\"payload\":\"\"}";
1267 ipc_send_client_message(client, strlen(payload), I3_IPC_EVENT_TICK, (const uint8_t *)payload);
1268}
1269
1270/*
1271 * Returns the raw last loaded i3 configuration file contents.
1272 */
1273IPC_HANDLER(get_config) {
1274 yajl_gen gen = ygenalloc();
1275
1276 y(map_open);
1277
1278 ystr("config");
1280 ystr(file->raw_contents);
1281
1282 ystr("included_configs");
1283 y(array_open);
1284 TAILQ_FOREACH (file, &included_files, files) {
1285 y(map_open);
1286 ystr("path");
1287 ystr(file->path);
1288 ystr("raw_contents");
1289 ystr(file->raw_contents);
1290 ystr("variable_replaced_contents");
1292 y(map_close);
1293 }
1294 y(array_close);
1295
1296 y(map_close);
1297
1298 const unsigned char *payload;
1299 ylength length;
1300 y(get_buf, &payload, &length);
1301
1302 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_CONFIG, payload);
1303 y(free);
1304}
1305
1306/*
1307 * Sends the tick event from the message payload to subscribers. Establishes a
1308 * synchronization point in event-related tests.
1309 */
1310IPC_HANDLER(send_tick) {
1311 yajl_gen gen = ygenalloc();
1312
1313 y(map_open);
1314
1315 ystr("first");
1316 y(bool, false);
1317
1318 ystr("payload");
1319 yajl_gen_string(gen, (unsigned char *)message, message_size);
1320
1321 y(map_close);
1322
1323 const unsigned char *payload;
1324 ylength length;
1325 y(get_buf, &payload, &length);
1326
1327 ipc_send_event("tick", I3_IPC_EVENT_TICK, (const char *)payload);
1328 y(free);
1329
1330 const char *reply = "{\"success\":true}";
1331 ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_TICK, (const uint8_t *)reply);
1332 DLOG("Sent tick event\n");
1333}
1334
1337 uint32_t rnd;
1338 xcb_window_t window;
1339};
1340
1341static int _sync_json_key(void *extra, const unsigned char *val, size_t len) {
1342 struct sync_state *state = extra;
1343 FREE(state->last_key);
1344 state->last_key = scalloc(len + 1, 1);
1345 memcpy(state->last_key, val, len);
1346 return 1;
1347}
1348
1349static int _sync_json_int(void *extra, long long val) {
1350 struct sync_state *state = extra;
1351 if (strcasecmp(state->last_key, "rnd") == 0) {
1352 state->rnd = val;
1353 } else if (strcasecmp(state->last_key, "window") == 0) {
1354 state->window = (xcb_window_t)val;
1355 }
1356 return 1;
1357}
1358
1360 yajl_handle p;
1361 yajl_status stat;
1362
1363 /* Setup the JSON parser */
1364 static yajl_callbacks callbacks = {
1365 .yajl_map_key = _sync_json_key,
1366 .yajl_integer = _sync_json_int,
1367 };
1368
1369 struct sync_state state;
1370 memset(&state, '\0', sizeof(struct sync_state));
1371 p = yalloc(&callbacks, (void *)&state);
1372 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1373 FREE(state.last_key);
1374 if (stat != yajl_status_ok) {
1375 unsigned char *err;
1376 err = yajl_get_error(p, true, (const unsigned char *)message,
1377 message_size);
1378 ELOG("YAJL parse error: %s\n", err);
1379 yajl_free_error(p, err);
1380
1381 const char *reply = "{\"success\":false}";
1382 ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1383 yajl_free(p);
1384 return;
1385 }
1386 yajl_free(p);
1387
1388 DLOG("received IPC sync request (rnd = %d, window = 0x%08x)\n", state.rnd, state.window);
1389 sync_respond(state.window, state.rnd);
1390 const char *reply = "{\"success\":true}";
1391 ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1392}
1393
1394IPC_HANDLER(get_binding_state) {
1395 yajl_gen gen = ygenalloc();
1396
1397 y(map_open);
1398
1399 ystr("name");
1401
1402 y(map_close);
1403
1404 const unsigned char *payload;
1405 ylength length;
1406 y(get_buf, &payload, &length);
1407
1408 ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_GET_BINDING_STATE, payload);
1409 y(free);
1410}
1411
1412/* The index of each callback function corresponds to the numeric
1413 * value of the message type (see include/i3/ipc.h) */
1415 handle_run_command,
1416 handle_get_workspaces,
1417 handle_subscribe,
1418 handle_get_outputs,
1419 handle_tree,
1420 handle_get_marks,
1421 handle_get_bar_config,
1422 handle_get_version,
1423 handle_get_binding_modes,
1424 handle_get_config,
1425 handle_send_tick,
1426 handle_sync,
1427 handle_get_binding_state,
1428};
1429
1430/*
1431 * Handler for activity on a client connection, receives a message from a
1432 * client.
1433 *
1434 * For now, the maximum message size is 2048. I’m not sure for what the
1435 * IPC interface will be used in the future, thus I’m not implementing a
1436 * mechanism for arbitrarily long messages, as it seems like overkill
1437 * at the moment.
1438 *
1439 */
1440static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
1441 uint32_t message_type;
1442 uint32_t message_length;
1443 uint8_t *message = NULL;
1444 ipc_client *client = (ipc_client *)w->data;
1445 assert(client->fd == w->fd);
1446
1447 int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
1448 /* EOF or other error */
1449 if (ret < 0) {
1450 /* Was this a spurious read? See ev(3) */
1451 if (ret == -1 && errno == EAGAIN) {
1452 FREE(message);
1453 return;
1454 }
1455
1456 /* If not, there was some kind of error. We don’t bother and close the
1457 * connection. Delete the client from the list of clients. */
1458 free_ipc_client(client, -1);
1459 FREE(message);
1460 return;
1461 }
1462
1463 if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
1464 DLOG("Unhandled message type: %d\n", message_type);
1465 else {
1466 handler_t h = handlers[message_type];
1467 h(client, message, 0, message_length, message_type);
1468 }
1469
1470 FREE(message);
1471}
1472
1473static void ipc_client_timeout(EV_P_ ev_timer *w, int revents) {
1474 /* No need to be polite and check for writeability, the other callback would
1475 * have been called by now. */
1476 ipc_client *client = (ipc_client *)w->data;
1477
1478 char *cmdline = NULL;
1479#if defined(__linux__) && defined(SO_PEERCRED)
1480 struct ucred peercred;
1481 socklen_t so_len = sizeof(peercred);
1482 if (getsockopt(client->fd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) != 0) {
1483 goto end;
1484 }
1485 char *exepath;
1486 sasprintf(&exepath, "/proc/%d/cmdline", peercred.pid);
1487
1488 int fd = open(exepath, O_RDONLY);
1489 free(exepath);
1490 if (fd == -1) {
1491 goto end;
1492 }
1493 char buf[512] = {'\0'}; /* cut off cmdline for the error message. */
1494 const ssize_t n = read(fd, buf, sizeof(buf));
1495 close(fd);
1496 if (n < 0) {
1497 goto end;
1498 }
1499 for (char *walk = buf; walk < buf + n - 1; walk++) {
1500 if (*walk == '\0') {
1501 *walk = ' ';
1502 }
1503 }
1504 cmdline = buf;
1505
1506 if (cmdline) {
1507 ELOG("client %p with pid %d and cmdline '%s' on fd %d timed out, killing\n", client, peercred.pid, cmdline, client->fd);
1508 }
1509
1510end:
1511#endif
1512 if (!cmdline) {
1513 ELOG("client %p on fd %d timed out, killing\n", client, client->fd);
1514 }
1515
1516 free_ipc_client(client, -1);
1517}
1518
1519static void ipc_socket_writeable_cb(EV_P_ ev_io *w, int revents) {
1520 DLOG("fd %d writeable\n", w->fd);
1521 ipc_client *client = (ipc_client *)w->data;
1522
1523 /* If this callback is called then there should be a corresponding active
1524 * timer. */
1525 assert(client->timeout != NULL);
1526 ipc_push_pending(client);
1527}
1528
1529/*
1530 * Handler for activity on the listening socket, meaning that a new client
1531 * has just connected and we should accept() him. Sets up the event handler
1532 * for activity on the new connection and inserts the file descriptor into
1533 * the list of clients.
1534 *
1535 */
1536void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
1537 struct sockaddr_un peer;
1538 socklen_t len = sizeof(struct sockaddr_un);
1539 int fd;
1540 if ((fd = accept(w->fd, (struct sockaddr *)&peer, &len)) < 0) {
1541 if (errno != EINTR) {
1542 perror("accept()");
1543 }
1544 return;
1545 }
1546
1547 /* Close this file descriptor on exec() */
1548 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1549
1550 ipc_new_client_on_fd(EV_A_ fd);
1551}
1552
1553/*
1554 * ipc_new_client_on_fd() only sets up the event handler
1555 * for activity on the new connection and inserts the file descriptor into
1556 * the list of clients.
1557 *
1558 * This variant is useful for the inherited IPC connection when restarting.
1559 *
1560 */
1562 set_nonblock(fd);
1563
1564 ipc_client *client = scalloc(1, sizeof(ipc_client));
1565 client->fd = fd;
1566
1567 client->read_callback = scalloc(1, sizeof(struct ev_io));
1568 client->read_callback->data = client;
1569 ev_io_init(client->read_callback, ipc_receive_message, fd, EV_READ);
1570 ev_io_start(EV_A_ client->read_callback);
1571
1572 client->write_callback = scalloc(1, sizeof(struct ev_io));
1573 client->write_callback->data = client;
1574 ev_io_init(client->write_callback, ipc_socket_writeable_cb, fd, EV_WRITE);
1575
1576 DLOG("IPC: new client connected on fd %d\n", fd);
1577 TAILQ_INSERT_TAIL(&all_clients, client, clients);
1578 return client;
1579}
1580
1581/*
1582 * Generates a json workspace event. Returns a dynamically allocated yajl
1583 * generator. Free with yajl_gen_free().
1584 */
1585yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old) {
1586 setlocale(LC_NUMERIC, "C");
1587 yajl_gen gen = ygenalloc();
1588
1589 y(map_open);
1590
1591 ystr("change");
1592 ystr(change);
1593
1594 ystr("current");
1595 if (current == NULL)
1596 y(null);
1597 else
1598 dump_node(gen, current, false);
1599
1600 ystr("old");
1601 if (old == NULL)
1602 y(null);
1603 else
1604 dump_node(gen, old, false);
1605
1606 y(map_close);
1607
1608 setlocale(LC_NUMERIC, "");
1609
1610 return gen;
1611}
1612
1613/*
1614 * For the workspace events we send, along with the usual "change" field, also
1615 * the workspace container in "current". For focus events, we send the
1616 * previously focused workspace in "old".
1617 */
1618void ipc_send_workspace_event(const char *change, Con *current, Con *old) {
1619 yajl_gen gen = ipc_marshal_workspace_event(change, current, old);
1620
1621 const unsigned char *payload;
1622 ylength length;
1623 y(get_buf, &payload, &length);
1624
1625 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
1626
1627 y(free);
1628}
1629
1630/*
1631 * For the window events we send, along the usual "change" field,
1632 * also the window container, in "container".
1633 */
1634void ipc_send_window_event(const char *property, Con *con) {
1635 DLOG("Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1636 property, con, (con->window ? con->window->id : XCB_WINDOW_NONE));
1637
1638 setlocale(LC_NUMERIC, "C");
1639 yajl_gen gen = ygenalloc();
1640
1641 y(map_open);
1642
1643 ystr("change");
1644 ystr(property);
1645
1646 ystr("container");
1647 dump_node(gen, con, false);
1648
1649 y(map_close);
1650
1651 const unsigned char *payload;
1652 ylength length;
1653 y(get_buf, &payload, &length);
1654
1655 ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload);
1656 y(free);
1657 setlocale(LC_NUMERIC, "");
1658}
1659
1660/*
1661 * For the barconfig update events, we send the serialized barconfig.
1662 */
1664 DLOG("Issue barconfig_update event for id = %s\n", barconfig->id);
1665 setlocale(LC_NUMERIC, "C");
1666 yajl_gen gen = ygenalloc();
1667
1668 dump_bar_config(gen, barconfig);
1669
1670 const unsigned char *payload;
1671 ylength length;
1672 y(get_buf, &payload, &length);
1673
1674 ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (const char *)payload);
1675 y(free);
1676 setlocale(LC_NUMERIC, "");
1677}
1678
1679/*
1680 * For the binding events, we send the serialized binding struct.
1681 */
1682void ipc_send_binding_event(const char *event_type, Binding *bind, const char *modename) {
1683 DLOG("Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->symbol, bind->keycode);
1684
1685 setlocale(LC_NUMERIC, "C");
1686
1687 yajl_gen gen = ygenalloc();
1688
1689 y(map_open);
1690
1691 ystr("change");
1692 ystr(event_type);
1693
1694 ystr("mode");
1695 if (modename == NULL) {
1696 ystr("default");
1697 } else {
1698 ystr(modename);
1699 }
1700
1701 ystr("binding");
1702 dump_binding(gen, bind);
1703
1704 y(map_close);
1705
1706 const unsigned char *payload;
1707 ylength length;
1708 y(get_buf, &payload, &length);
1709
1710 ipc_send_event("binding", I3_IPC_EVENT_BINDING, (const char *)payload);
1711
1712 y(free);
1713 setlocale(LC_NUMERIC, "");
1714}
1715
1716/*
1717 * Sends a restart reply to the IPC client on the specified fd.
1718 */
1720 DLOG("ipc_confirm_restart(fd %d)\n", client->fd);
1721 static const char *reply = "[{\"success\":true}]";
1723 client, strlen(reply), I3_IPC_REPLY_TYPE_COMMAND,
1724 (const uint8_t *)reply);
1725 ipc_push_pending(client);
1726}
#define y(x,...)
Definition commands.c:18
#define ystr(str)
Definition commands.c:19
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
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
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
struct pending_marks * marks
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
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
void sync_respond(xcb_window_t window, uint32_t rnd)
Definition sync.c:12
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
const char * i3_version
Git commit identifier, from version.c.
Definition version.c:13
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
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:1349
static void dump_event_state_mask(yajl_gen gen, Binding *bind)
Definition ipc.c:270
handler_t handlers[13]
Definition ipc.c:1414
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:1341
static void ipc_client_timeout(EV_P_ ev_timer *w, int revents)
Definition ipc.c:1473
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:1719
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:1561
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:1682
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:1440
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:1618
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:1536
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
Definition ipc.c:1663
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:1585
static int add_subscription(void *extra, const unsigned char *s, ylength len)
Definition ipc.c:1195
static void ipc_socket_writeable_cb(EV_P_ ev_io *w, int revents)
Definition ipc.c:1519
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:1634
static void ipc_push_pending(ipc_client *client)
Definition ipc.c:45
#define DUMP_PROPERTY(key, prop_name)
static i3_shmlog_header * header
Definition log.c:53
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
@ 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
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 FREE(pointer)
Definition util.h:47
#define yalloc(callbacks, client)
Definition yajl_utils.h:23
size_t ylength
Definition yajl_utils.h:24
#define ygenalloc()
Definition yajl_utils.h:22
#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
char * last_key
Definition ipc.c:1336
xcb_window_t window
Definition ipc.c:1338
uint32_t rnd
Definition ipc.c:1337
A struct that contains useful information about the result of a command as a whole (e....
List entry struct for an included file.
char * variable_replaced_contents
char * raw_contents
The configuration file can contain multiple sets of bindings.
char * name
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
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