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