2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * ipc.c: UNIX domain socket IPC (initialization, client handling, protocol).
12 #include "yajl_utils.h"
15 #include <sys/socket.h>
20 #include <yajl/yajl_gen.h>
21 #include <yajl/yajl_parse.h>
23 char *current_socketpath = NULL;
25 TAILQ_HEAD(ipc_client_head, ipc_client)
26 all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
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.
34 static void set_nonblock(int sockfd) {
35 int flags = fcntl(sockfd, F_GETFL, 0);
37 if (fcntl(sockfd, F_SETFL, flags) < 0)
38 err(-1, "Could not set O_NONBLOCK");
42 * Sends the specified event to all IPC clients which are currently connected
43 * and subscribed to this kind of event.
46 void ipc_send_event(const char *event, uint32_t message_type, const char *payload) {
48 TAILQ_FOREACH(current, &all_clients, clients) {
49 /* see if this client is interested in this event */
50 bool interested = false;
51 for (int i = 0; i < current->num_events; i++) {
52 if (strcasecmp(current->events[i], event) != 0)
60 ipc_send_message(current->fd, strlen(payload), message_type, (const uint8_t *)payload);
65 * For shutdown events, we send the reason for the shutdown.
67 static void ipc_send_shutdown_event(shutdown_reason_t reason) {
68 yajl_gen gen = ygenalloc();
73 if (reason == SHUTDOWN_REASON_RESTART) {
75 } else if (reason == SHUTDOWN_REASON_EXIT) {
81 const unsigned char *payload;
84 y(get_buf, &payload, &length);
85 ipc_send_event("shutdown", I3_IPC_EVENT_SHUTDOWN, (const char *)payload);
91 * Calls shutdown() on each socket and closes it. This function is to be called
92 * when exiting or restarting only!
95 void ipc_shutdown(shutdown_reason_t reason) {
96 ipc_send_shutdown_event(reason);
99 while (!TAILQ_EMPTY(&all_clients)) {
100 current = TAILQ_FIRST(&all_clients);
101 shutdown(current->fd, SHUT_RDWR);
103 for (int i = 0; i < current->num_events; i++)
104 free(current->events[i]);
105 free(current->events);
106 TAILQ_REMOVE(&all_clients, current, clients);
112 * Executes the command and returns whether it could be successfully parsed
113 * or not (at the moment, always returns true).
116 IPC_HANDLER(run_command) {
117 /* To get a properly terminated buffer, we copy
118 * message_size bytes out of the buffer */
119 char *command = scalloc(message_size + 1, 1);
120 strncpy(command, (const char *)message, message_size);
121 LOG("IPC: received: *%s*\n", command);
122 yajl_gen gen = yajl_gen_alloc(NULL);
124 CommandResult *result = parse_command((const char *)command, gen);
127 if (result->needs_tree_render)
130 command_result_free(result);
132 const unsigned char *reply;
134 yajl_gen_get_buf(gen, &reply, &length);
136 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
137 (const uint8_t *)reply);
142 static void dump_rect(yajl_gen gen, const char *name, Rect r) {
152 y(integer, r.height);
156 static void dump_event_state_mask(yajl_gen gen, Binding *bind) {
158 for (int i = 0; i < 20; i++) {
159 if (bind->event_state_mask & (1 << i)) {
161 case XCB_KEY_BUT_MASK_SHIFT:
164 case XCB_KEY_BUT_MASK_LOCK:
167 case XCB_KEY_BUT_MASK_CONTROL:
170 case XCB_KEY_BUT_MASK_MOD_1:
173 case XCB_KEY_BUT_MASK_MOD_2:
176 case XCB_KEY_BUT_MASK_MOD_3:
179 case XCB_KEY_BUT_MASK_MOD_4:
182 case XCB_KEY_BUT_MASK_MOD_5:
185 case XCB_KEY_BUT_MASK_BUTTON_1:
188 case XCB_KEY_BUT_MASK_BUTTON_2:
191 case XCB_KEY_BUT_MASK_BUTTON_3:
194 case XCB_KEY_BUT_MASK_BUTTON_4:
197 case XCB_KEY_BUT_MASK_BUTTON_5:
200 case (I3_XKB_GROUP_MASK_1 << 16):
203 case (I3_XKB_GROUP_MASK_2 << 16):
206 case (I3_XKB_GROUP_MASK_3 << 16):
209 case (I3_XKB_GROUP_MASK_4 << 16):
218 static void dump_binding(yajl_gen gen, Binding *bind) {
221 y(integer, bind->keycode);
224 ystr((const char *)(bind->input_type == B_KEYBOARD ? "keyboard" : "mouse"));
227 if (bind->symbol == NULL)
235 // This key is only provided for compatibility, new programs should use
236 // event_state_mask instead.
238 dump_event_state_mask(gen, bind);
240 ystr("event_state_mask");
241 dump_event_state_mask(gen, bind);
246 void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
249 y(integer, (uintptr_t)con);
262 case CT_FLOATING_CON:
263 ystr("floating_con");
273 /* provided for backwards compatibility only. */
275 if (!con_is_split(con))
278 if (con_orientation(con) == HORIZ)
284 ystr("scratchpad_state");
285 switch (con->scratchpad_state) {
286 case SCRATCHPAD_NONE:
289 case SCRATCHPAD_FRESH:
292 case SCRATCHPAD_CHANGED:
298 if (con->percent == 0.0)
301 y(double, con->percent);
304 y(bool, con->urgent);
306 if (!TAILQ_EMPTY(&(con->marks_head))) {
311 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
319 y(bool, (con == focused));
321 if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
323 ystr(con_get_output(con)->name);
327 switch (con->layout) {
329 DLOG("About to dump layout=default, this is a bug in the code.\n");
352 ystr("workspace_layout");
353 switch (con->workspace_layout) {
364 DLOG("About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
369 ystr("last_split_layout");
370 switch (con->layout) {
380 switch (con->border_style) {
392 ystr("current_border_width");
393 y(integer, con->current_border_width);
395 dump_rect(gen, "rect", con->rect);
396 dump_rect(gen, "deco_rect", con->deco_rect);
397 dump_rect(gen, "window_rect", con->window_rect);
398 dump_rect(gen, "geometry", con->geometry);
401 if (con->window && con->window->name)
402 ystr(i3string_as_utf8(con->window->name));
403 else if (con->name != NULL)
408 if (con->title_format != NULL) {
409 ystr("title_format");
410 ystr(con->title_format);
413 if (con->type == CT_WORKSPACE) {
415 y(integer, con->num);
420 y(integer, con->window->id);
424 if (con->window && !inplace_restart) {
425 /* Window properties are useless to preserve when restarting because
426 * they will be queried again anyway. However, for i3-save-tree(1),
427 * they are very useful and save i3-save-tree dealing with X11. */
428 ystr("window_properties");
431 #define DUMP_PROPERTY(key, prop_name) \
433 if (con->window->prop_name != NULL) { \
435 ystr(con->window->prop_name); \
439 DUMP_PROPERTY("class", class_class);
440 DUMP_PROPERTY("instance", class_instance);
441 DUMP_PROPERTY("window_role", role);
443 if (con->window->name != NULL) {
445 ystr(i3string_as_utf8(con->window->name));
448 ystr("transient_for");
449 if (con->window->transient_for == XCB_NONE)
452 y(integer, con->window->transient_for);
460 if (con->type != CT_DOCKAREA || !inplace_restart) {
461 TAILQ_FOREACH(node, &(con->nodes_head), nodes) {
462 dump_node(gen, node, inplace_restart);
467 ystr("floating_nodes");
469 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
470 dump_node(gen, node, inplace_restart);
476 TAILQ_FOREACH(node, &(con->focus_head), focused) {
477 y(integer, (uintptr_t)node);
481 ystr("fullscreen_mode");
482 y(integer, con->fullscreen_mode);
485 y(bool, con->sticky);
488 switch (con->floating) {
489 case FLOATING_AUTO_OFF:
492 case FLOATING_AUTO_ON:
495 case FLOATING_USER_OFF:
498 case FLOATING_USER_ON:
506 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
507 /* We will generate a new restart_mode match specification after this
508 * loop, so skip this one. */
509 if (match->restart_mode)
512 if (match->dock != M_DONTCHECK) {
514 y(integer, match->dock);
515 ystr("insert_where");
516 y(integer, match->insert_where);
519 #define DUMP_REGEX(re_name) \
521 if (match->re_name != NULL) { \
523 ystr(match->re_name->pattern); \
528 DUMP_REGEX(instance);
529 DUMP_REGEX(window_role);
536 if (inplace_restart) {
537 if (con->window != NULL) {
540 y(integer, con->window->id);
541 ystr("restart_mode");
548 if (inplace_restart && con->window != NULL) {
550 y(integer, con->depth);
556 static void dump_bar_bindings(yajl_gen gen, Barconfig *config) {
557 if (TAILQ_EMPTY(&(config->bar_bindings)))
563 struct Barbinding *current;
564 TAILQ_FOREACH(current, &(config->bar_bindings), bindings) {
568 y(integer, current->input_code);
570 ystr(current->command);
572 y(bool, current->release == B_UPON_KEYRELEASE);
580 static char *canonicalize_output_name(char *name) {
581 /* Do not canonicalize special output names. */
582 if (strcasecmp(name, "primary") == 0) {
585 Output *output = get_output_by_name(name, false);
586 return output ? output_primary_name(output) : name;
589 static void dump_bar_config(yajl_gen gen, Barconfig *config) {
595 if (config->num_outputs > 0) {
598 for (int c = 0; c < config->num_outputs; c++) {
599 /* Convert monitor names (RandR ≥ 1.5) or output names
600 * (RandR < 1.5) into monitor names. This way, existing
601 * configs which use output names transparently keep
603 ystr(canonicalize_output_name(config->outputs[c]));
608 if (!TAILQ_EMPTY(&(config->tray_outputs))) {
609 ystr("tray_outputs");
612 struct tray_output_t *tray_output;
613 TAILQ_FOREACH(tray_output, &(config->tray_outputs), tray_outputs) {
614 ystr(canonicalize_output_name(tray_output->output));
620 #define YSTR_IF_SET(name) \
622 if (config->name) { \
624 ystr(config->name); \
628 ystr("tray_padding");
629 y(integer, config->tray_padding);
631 YSTR_IF_SET(socket_path);
634 switch (config->mode) {
647 ystr("hidden_state");
648 switch (config->hidden_state) {
659 y(integer, config->modifier);
661 dump_bar_bindings(gen, config);
664 if (config->position == P_BOTTOM)
669 YSTR_IF_SET(status_command);
672 if (config->separator_symbol) {
673 ystr("separator_symbol");
674 ystr(config->separator_symbol);
677 ystr("workspace_buttons");
678 y(bool, !config->hide_workspace_buttons);
680 ystr("strip_workspace_numbers");
681 y(bool, config->strip_workspace_numbers);
683 ystr("strip_workspace_name");
684 y(bool, config->strip_workspace_name);
686 ystr("binding_mode_indicator");
687 y(bool, !config->hide_binding_mode_indicator);
690 y(bool, config->verbose);
693 #define YSTR_IF_SET(name) \
695 if (config->colors.name) { \
697 ystr(config->colors.name); \
703 YSTR_IF_SET(background);
704 YSTR_IF_SET(statusline);
705 YSTR_IF_SET(separator);
706 YSTR_IF_SET(focused_background);
707 YSTR_IF_SET(focused_statusline);
708 YSTR_IF_SET(focused_separator);
709 YSTR_IF_SET(focused_workspace_border);
710 YSTR_IF_SET(focused_workspace_bg);
711 YSTR_IF_SET(focused_workspace_text);
712 YSTR_IF_SET(active_workspace_border);
713 YSTR_IF_SET(active_workspace_bg);
714 YSTR_IF_SET(active_workspace_text);
715 YSTR_IF_SET(inactive_workspace_border);
716 YSTR_IF_SET(inactive_workspace_bg);
717 YSTR_IF_SET(inactive_workspace_text);
718 YSTR_IF_SET(urgent_workspace_border);
719 YSTR_IF_SET(urgent_workspace_bg);
720 YSTR_IF_SET(urgent_workspace_text);
721 YSTR_IF_SET(binding_mode_border);
722 YSTR_IF_SET(binding_mode_bg);
723 YSTR_IF_SET(binding_mode_text);
731 setlocale(LC_NUMERIC, "C");
732 yajl_gen gen = ygenalloc();
733 dump_node(gen, croot, false);
734 setlocale(LC_NUMERIC, "");
736 const unsigned char *payload;
738 y(get_buf, &payload, &length);
740 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
745 * Formats the reply message for a GET_WORKSPACES request and sends it to the
749 IPC_HANDLER(get_workspaces) {
750 yajl_gen gen = ygenalloc();
753 Con *focused_ws = con_get_workspace(focused);
756 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
757 if (con_is_internal(output))
760 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
761 assert(ws->type == CT_WORKSPACE);
771 y(bool, workspace_is_visible(ws));
774 y(bool, ws == focused_ws);
779 y(integer, ws->rect.x);
781 y(integer, ws->rect.y);
783 y(integer, ws->rect.width);
785 y(integer, ws->rect.height);
800 const unsigned char *payload;
802 y(get_buf, &payload, &length);
804 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
809 * Formats the reply message for a GET_OUTPUTS request and sends it to the
813 IPC_HANDLER(get_outputs) {
814 yajl_gen gen = ygenalloc();
818 TAILQ_FOREACH(output, &outputs, outputs) {
822 ystr(output_primary_name(output));
825 y(bool, output->active);
828 y(bool, output->primary);
833 y(integer, output->rect.x);
835 y(integer, output->rect.y);
837 y(integer, output->rect.width);
839 y(integer, output->rect.height);
842 ystr("current_workspace");
844 if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
854 const unsigned char *payload;
856 y(get_buf, &payload, &length);
858 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
863 * Formats the reply message for a GET_MARKS request and sends it to the
867 IPC_HANDLER(get_marks) {
868 yajl_gen gen = ygenalloc();
872 TAILQ_FOREACH(con, &all_cons, all_cons) {
874 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
881 const unsigned char *payload;
883 y(get_buf, &payload, &length);
885 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
890 * Returns the version of i3
893 IPC_HANDLER(get_version) {
894 yajl_gen gen = ygenalloc();
898 y(integer, MAJOR_VERSION);
901 y(integer, MINOR_VERSION);
904 y(integer, PATCH_VERSION);
906 ystr("human_readable");
909 ystr("loaded_config_file_name");
910 ystr(current_configpath);
914 const unsigned char *payload;
916 y(get_buf, &payload, &length);
918 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
923 * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
927 IPC_HANDLER(get_bar_config) {
928 yajl_gen gen = ygenalloc();
930 /* If no ID was passed, we return a JSON array with all IDs */
931 if (message_size == 0) {
934 TAILQ_FOREACH(current, &barconfigs, configs) {
939 const unsigned char *payload;
941 y(get_buf, &payload, &length);
943 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
948 /* To get a properly terminated buffer, we copy
949 * message_size bytes out of the buffer */
951 sasprintf(&bar_id, "%.*s", message_size, message);
952 LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
953 Barconfig *current, *config = NULL;
954 TAILQ_FOREACH(current, &barconfigs, configs) {
955 if (strcmp(current->id, bar_id) != 0)
964 /* If we did not find a config for the given ID, the reply will contain
965 * a null 'id' field. */
973 dump_bar_config(gen, config);
976 const unsigned char *payload;
978 y(get_buf, &payload, &length);
980 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
985 * Returns a list of configured binding modes
988 IPC_HANDLER(get_binding_modes) {
989 yajl_gen gen = ygenalloc();
993 SLIST_FOREACH(mode, &modes, modes) {
998 const unsigned char *payload;
1000 y(get_buf, &payload, &length);
1002 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BINDING_MODES, payload);
1007 * Callback for the YAJL parser (will be called when a string is parsed).
1010 static int add_subscription(void *extra, const unsigned char *s,
1012 ipc_client *client = extra;
1014 DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
1015 int event = client->num_events;
1017 client->num_events++;
1018 client->events = srealloc(client->events, client->num_events * sizeof(char *));
1019 /* We copy the string because it is not null-terminated and strndup()
1020 * is missing on some BSD systems */
1021 client->events[event] = scalloc(len + 1, 1);
1022 memcpy(client->events[event], s, len);
1024 DLOG("client is now subscribed to:\n");
1025 for (int i = 0; i < client->num_events; i++) {
1026 DLOG("event %s\n", client->events[i]);
1034 * Subscribes this connection to the event types which were given as a JSON
1035 * serialized array in the payload field of the message.
1038 IPC_HANDLER(subscribe) {
1041 ipc_client *current, *client = NULL;
1043 /* Search the ipc_client structure for this connection */
1044 TAILQ_FOREACH(current, &all_clients, clients) {
1045 if (current->fd != fd)
1052 if (client == NULL) {
1053 ELOG("Could not find ipc_client data structure for fd %d\n", fd);
1057 /* Setup the JSON parser */
1058 static yajl_callbacks callbacks = {
1059 .yajl_string = add_subscription,
1062 p = yalloc(&callbacks, (void *)client);
1063 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1064 if (stat != yajl_status_ok) {
1066 err = yajl_get_error(p, true, (const unsigned char *)message,
1068 ELOG("YAJL parse error: %s\n", err);
1069 yajl_free_error(p, err);
1071 const char *reply = "{\"success\":false}";
1072 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1077 const char *reply = "{\"success\":true}";
1078 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1080 if (client->first_tick_sent) {
1084 bool is_tick = false;
1085 for (int i = 0; i < client->num_events; i++) {
1086 if (strcmp(client->events[i], "tick") == 0) {
1095 client->first_tick_sent = true;
1096 const char *payload = "{\"first\":true,\"payload\":\"\"}";
1097 ipc_send_message(client->fd, strlen(payload), I3_IPC_EVENT_TICK, (const uint8_t *)payload);
1101 * Returns the raw last loaded i3 configuration file contents.
1103 IPC_HANDLER(get_config) {
1104 yajl_gen gen = ygenalloc();
1109 ystr(current_config);
1113 const unsigned char *payload;
1115 y(get_buf, &payload, &length);
1117 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_CONFIG, payload);
1122 * Sends the tick event from the message payload to subscribers. Establishes a
1123 * synchronization point in event-related tests.
1125 IPC_HANDLER(send_tick) {
1126 yajl_gen gen = ygenalloc();
1134 yajl_gen_string(gen, (unsigned char *)message, message_size);
1138 const unsigned char *payload;
1140 y(get_buf, &payload, &length);
1142 ipc_send_event("tick", I3_IPC_EVENT_TICK, (const char *)payload);
1145 const char *reply = "{\"success\":true}";
1146 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_TICK, (const uint8_t *)reply);
1147 DLOG("Sent tick event\n");
1153 xcb_window_t window;
1156 static int _sync_json_key(void *extra, const unsigned char *val, size_t len) {
1157 struct sync_state *state = extra;
1158 FREE(state->last_key);
1159 state->last_key = scalloc(len + 1, 1);
1160 memcpy(state->last_key, val, len);
1164 static int _sync_json_int(void *extra, long long val) {
1165 struct sync_state *state = extra;
1166 if (strcasecmp(state->last_key, "rnd") == 0) {
1168 } else if (strcasecmp(state->last_key, "window") == 0) {
1169 state->window = (xcb_window_t)val;
1178 /* Setup the JSON parser */
1179 static yajl_callbacks callbacks = {
1180 .yajl_map_key = _sync_json_key,
1181 .yajl_integer = _sync_json_int,
1184 struct sync_state state;
1185 memset(&state, '\0', sizeof(struct sync_state));
1186 p = yalloc(&callbacks, (void *)&state);
1187 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1188 FREE(state.last_key);
1189 if (stat != yajl_status_ok) {
1191 err = yajl_get_error(p, true, (const unsigned char *)message,
1193 ELOG("YAJL parse error: %s\n", err);
1194 yajl_free_error(p, err);
1196 const char *reply = "{\"success\":false}";
1197 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1203 DLOG("received IPC sync request (rnd = %d, window = 0x%08x)\n", state.rnd, state.window);
1204 sync_respond(state.window, state.rnd);
1205 const char *reply = "{\"success\":true}";
1206 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1209 /* The index of each callback function corresponds to the numeric
1210 * value of the message type (see include/i3/ipc.h) */
1211 handler_t handlers[12] = {
1213 handle_get_workspaces,
1218 handle_get_bar_config,
1220 handle_get_binding_modes,
1227 * Handler for activity on a client connection, receives a message from a
1230 * For now, the maximum message size is 2048. I’m not sure for what the
1231 * IPC interface will be used in the future, thus I’m not implementing a
1232 * mechanism for arbitrarily long messages, as it seems like overkill
1236 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
1237 uint32_t message_type;
1238 uint32_t message_length;
1239 uint8_t *message = NULL;
1241 int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
1242 /* EOF or other error */
1244 /* Was this a spurious read? See ev(3) */
1245 if (ret == -1 && errno == EAGAIN) {
1250 /* If not, there was some kind of error. We don’t bother
1251 * and close the connection */
1254 /* Delete the client from the list of clients */
1255 ipc_client *current;
1256 TAILQ_FOREACH(current, &all_clients, clients) {
1257 if (current->fd != w->fd)
1260 for (int i = 0; i < current->num_events; i++)
1261 free(current->events[i]);
1262 free(current->events);
1263 /* We can call TAILQ_REMOVE because we break out of the
1264 * TAILQ_FOREACH afterwards */
1265 TAILQ_REMOVE(&all_clients, current, clients);
1270 ev_io_stop(EV_A_ w);
1274 DLOG("IPC: client disconnected\n");
1278 if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
1279 DLOG("Unhandled message type: %d\n", message_type);
1281 handler_t h = handlers[message_type];
1282 h(w->fd, message, 0, message_length, message_type);
1289 * Handler for activity on the listening socket, meaning that a new client
1290 * has just connected and we should accept() him. Sets up the event handler
1291 * for activity on the new connection and inserts the file descriptor into
1292 * the list of clients.
1295 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
1296 struct sockaddr_un peer;
1297 socklen_t len = sizeof(struct sockaddr_un);
1299 if ((client = accept(w->fd, (struct sockaddr *)&peer, &len)) < 0) {
1307 /* Close this file descriptor on exec() */
1308 (void)fcntl(client, F_SETFD, FD_CLOEXEC);
1310 set_nonblock(client);
1312 struct ev_io *package = scalloc(1, sizeof(struct ev_io));
1313 ev_io_init(package, ipc_receive_message, client, EV_READ);
1314 ev_io_start(EV_A_ package);
1316 DLOG("IPC: new client connected on fd %d\n", w->fd);
1318 ipc_client *new = scalloc(1, sizeof(ipc_client));
1321 TAILQ_INSERT_TAIL(&all_clients, new, clients);
1325 * Creates the UNIX domain socket at the given path, sets it to non-blocking
1326 * mode, bind()s and listen()s on it.
1329 int ipc_create_socket(const char *filename) {
1332 FREE(current_socketpath);
1334 char *resolved = resolve_tilde(filename);
1335 DLOG("Creating IPC-socket at %s\n", resolved);
1336 char *copy = sstrdup(resolved);
1337 const char *dir = dirname(copy);
1338 if (!path_exists(dir))
1339 mkdirp(dir, DEFAULT_DIR_MODE);
1342 /* Unlink the unix domain socket before */
1345 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1351 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1353 struct sockaddr_un addr;
1354 memset(&addr, 0, sizeof(struct sockaddr_un));
1355 addr.sun_family = AF_LOCAL;
1356 strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
1357 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
1363 set_nonblock(sockfd);
1365 if (listen(sockfd, 5) < 0) {
1371 current_socketpath = resolved;
1376 * Generates a json workspace event. Returns a dynamically allocated yajl
1377 * generator. Free with yajl_gen_free().
1379 yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old) {
1380 setlocale(LC_NUMERIC, "C");
1381 yajl_gen gen = ygenalloc();
1389 if (current == NULL)
1392 dump_node(gen, current, false);
1398 dump_node(gen, old, false);
1402 setlocale(LC_NUMERIC, "");
1408 * For the workspace events we send, along with the usual "change" field, also
1409 * the workspace container in "current". For focus events, we send the
1410 * previously focused workspace in "old".
1412 void ipc_send_workspace_event(const char *change, Con *current, Con *old) {
1413 yajl_gen gen = ipc_marshal_workspace_event(change, current, old);
1415 const unsigned char *payload;
1417 y(get_buf, &payload, &length);
1419 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
1425 * For the window events we send, along the usual "change" field,
1426 * also the window container, in "container".
1428 void ipc_send_window_event(const char *property, Con *con) {
1429 DLOG("Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1430 property, con, (con->window ? con->window->id : XCB_WINDOW_NONE));
1432 setlocale(LC_NUMERIC, "C");
1433 yajl_gen gen = ygenalloc();
1441 dump_node(gen, con, false);
1445 const unsigned char *payload;
1447 y(get_buf, &payload, &length);
1449 ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload);
1451 setlocale(LC_NUMERIC, "");
1455 * For the barconfig update events, we send the serialized barconfig.
1457 void ipc_send_barconfig_update_event(Barconfig *barconfig) {
1458 DLOG("Issue barconfig_update event for id = %s\n", barconfig->id);
1459 setlocale(LC_NUMERIC, "C");
1460 yajl_gen gen = ygenalloc();
1462 dump_bar_config(gen, barconfig);
1464 const unsigned char *payload;
1466 y(get_buf, &payload, &length);
1468 ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (const char *)payload);
1470 setlocale(LC_NUMERIC, "");
1474 * For the binding events, we send the serialized binding struct.
1476 void ipc_send_binding_event(const char *event_type, Binding *bind) {
1477 DLOG("Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->symbol, bind->keycode);
1479 setlocale(LC_NUMERIC, "C");
1481 yajl_gen gen = ygenalloc();
1489 dump_binding(gen, bind);
1493 const unsigned char *payload;
1495 y(get_buf, &payload, &length);
1497 ipc_send_event("binding", I3_IPC_EVENT_BINDING, (const char *)payload);
1500 setlocale(LC_NUMERIC, "");