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