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");
41 static void free_ipc_client(ipc_client *client) {
43 for (int i = 0; i < client->num_events; i++){
44 free(client->events[i]);
47 TAILQ_REMOVE(&all_clients, client, clients);
52 * Sends the specified event to all IPC clients which are currently connected
53 * and subscribed to this kind of event.
56 void ipc_send_event(const char *event, uint32_t message_type, const char *payload) {
58 TAILQ_FOREACH(current, &all_clients, clients) {
59 /* see if this client is interested in this event */
60 bool interested = false;
61 for (int i = 0; i < current->num_events; i++) {
62 if (strcasecmp(current->events[i], event) != 0)
70 ipc_send_message(current->fd, strlen(payload), message_type, (const uint8_t *)payload);
75 * For shutdown events, we send the reason for the shutdown.
77 static void ipc_send_shutdown_event(shutdown_reason_t reason) {
78 yajl_gen gen = ygenalloc();
83 if (reason == SHUTDOWN_REASON_RESTART) {
85 } else if (reason == SHUTDOWN_REASON_EXIT) {
91 const unsigned char *payload;
94 y(get_buf, &payload, &length);
95 ipc_send_event("shutdown", I3_IPC_EVENT_SHUTDOWN, (const char *)payload);
101 * Calls shutdown() on each socket and closes it. This function is to be called
102 * when exiting or restarting only!
105 void ipc_shutdown(shutdown_reason_t reason) {
106 ipc_send_shutdown_event(reason);
109 while (!TAILQ_EMPTY(&all_clients)) {
110 current = TAILQ_FIRST(&all_clients);
111 shutdown(current->fd, SHUT_RDWR);
112 free_ipc_client(current);
117 * Executes the command and returns whether it could be successfully parsed
118 * or not (at the moment, always returns true).
121 IPC_HANDLER(run_command) {
122 /* To get a properly terminated buffer, we copy
123 * message_size bytes out of the buffer */
124 char *command = scalloc(message_size + 1, 1);
125 strncpy(command, (const char *)message, message_size);
126 LOG("IPC: received: *%s*\n", command);
127 yajl_gen gen = yajl_gen_alloc(NULL);
129 CommandResult *result = parse_command((const char *)command, gen);
132 if (result->needs_tree_render)
135 command_result_free(result);
137 const unsigned char *reply;
139 yajl_gen_get_buf(gen, &reply, &length);
141 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
142 (const uint8_t *)reply);
147 static void dump_rect(yajl_gen gen, const char *name, Rect r) {
157 y(integer, r.height);
161 static void dump_event_state_mask(yajl_gen gen, Binding *bind) {
163 for (int i = 0; i < 20; i++) {
164 if (bind->event_state_mask & (1 << i)) {
166 case XCB_KEY_BUT_MASK_SHIFT:
169 case XCB_KEY_BUT_MASK_LOCK:
172 case XCB_KEY_BUT_MASK_CONTROL:
175 case XCB_KEY_BUT_MASK_MOD_1:
178 case XCB_KEY_BUT_MASK_MOD_2:
181 case XCB_KEY_BUT_MASK_MOD_3:
184 case XCB_KEY_BUT_MASK_MOD_4:
187 case XCB_KEY_BUT_MASK_MOD_5:
190 case XCB_KEY_BUT_MASK_BUTTON_1:
193 case XCB_KEY_BUT_MASK_BUTTON_2:
196 case XCB_KEY_BUT_MASK_BUTTON_3:
199 case XCB_KEY_BUT_MASK_BUTTON_4:
202 case XCB_KEY_BUT_MASK_BUTTON_5:
205 case (I3_XKB_GROUP_MASK_1 << 16):
208 case (I3_XKB_GROUP_MASK_2 << 16):
211 case (I3_XKB_GROUP_MASK_3 << 16):
214 case (I3_XKB_GROUP_MASK_4 << 16):
223 static void dump_binding(yajl_gen gen, Binding *bind) {
226 y(integer, bind->keycode);
229 ystr((const char *)(bind->input_type == B_KEYBOARD ? "keyboard" : "mouse"));
232 if (bind->symbol == NULL)
240 // This key is only provided for compatibility, new programs should use
241 // event_state_mask instead.
243 dump_event_state_mask(gen, bind);
245 ystr("event_state_mask");
246 dump_event_state_mask(gen, bind);
251 void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
254 y(integer, (uintptr_t)con);
267 case CT_FLOATING_CON:
268 ystr("floating_con");
278 /* provided for backwards compatibility only. */
280 if (!con_is_split(con))
283 if (con_orientation(con) == HORIZ)
289 ystr("scratchpad_state");
290 switch (con->scratchpad_state) {
291 case SCRATCHPAD_NONE:
294 case SCRATCHPAD_FRESH:
297 case SCRATCHPAD_CHANGED:
303 if (con->percent == 0.0)
306 y(double, con->percent);
309 y(bool, con->urgent);
311 if (!TAILQ_EMPTY(&(con->marks_head))) {
316 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
324 y(bool, (con == focused));
326 if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
328 ystr(con_get_output(con)->name);
332 switch (con->layout) {
334 DLOG("About to dump layout=default, this is a bug in the code.\n");
357 ystr("workspace_layout");
358 switch (con->workspace_layout) {
369 DLOG("About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
374 ystr("last_split_layout");
375 switch (con->layout) {
385 switch (con->border_style) {
397 ystr("current_border_width");
398 y(integer, con->current_border_width);
400 dump_rect(gen, "rect", con->rect);
401 dump_rect(gen, "deco_rect", con->deco_rect);
402 dump_rect(gen, "window_rect", con->window_rect);
403 dump_rect(gen, "geometry", con->geometry);
406 if (con->window && con->window->name)
407 ystr(i3string_as_utf8(con->window->name));
408 else if (con->name != NULL)
413 if (con->title_format != NULL) {
414 ystr("title_format");
415 ystr(con->title_format);
418 if (con->type == CT_WORKSPACE) {
420 y(integer, con->num);
425 y(integer, con->window->id);
429 if (con->window && !inplace_restart) {
430 /* Window properties are useless to preserve when restarting because
431 * they will be queried again anyway. However, for i3-save-tree(1),
432 * they are very useful and save i3-save-tree dealing with X11. */
433 ystr("window_properties");
436 #define DUMP_PROPERTY(key, prop_name) \
438 if (con->window->prop_name != NULL) { \
440 ystr(con->window->prop_name); \
444 DUMP_PROPERTY("class", class_class);
445 DUMP_PROPERTY("instance", class_instance);
446 DUMP_PROPERTY("window_role", role);
448 if (con->window->name != NULL) {
450 ystr(i3string_as_utf8(con->window->name));
453 ystr("transient_for");
454 if (con->window->transient_for == XCB_NONE)
457 y(integer, con->window->transient_for);
465 if (con->type != CT_DOCKAREA || !inplace_restart) {
466 TAILQ_FOREACH(node, &(con->nodes_head), nodes) {
467 dump_node(gen, node, inplace_restart);
472 ystr("floating_nodes");
474 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
475 dump_node(gen, node, inplace_restart);
481 TAILQ_FOREACH(node, &(con->focus_head), focused) {
482 y(integer, (uintptr_t)node);
486 ystr("fullscreen_mode");
487 y(integer, con->fullscreen_mode);
490 y(bool, con->sticky);
493 switch (con->floating) {
494 case FLOATING_AUTO_OFF:
497 case FLOATING_AUTO_ON:
500 case FLOATING_USER_OFF:
503 case FLOATING_USER_ON:
511 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
512 /* We will generate a new restart_mode match specification after this
513 * loop, so skip this one. */
514 if (match->restart_mode)
517 if (match->dock != M_DONTCHECK) {
519 y(integer, match->dock);
520 ystr("insert_where");
521 y(integer, match->insert_where);
524 #define DUMP_REGEX(re_name) \
526 if (match->re_name != NULL) { \
528 ystr(match->re_name->pattern); \
533 DUMP_REGEX(instance);
534 DUMP_REGEX(window_role);
541 if (inplace_restart) {
542 if (con->window != NULL) {
545 y(integer, con->window->id);
546 ystr("restart_mode");
553 if (inplace_restart && con->window != NULL) {
555 y(integer, con->depth);
561 static void dump_bar_bindings(yajl_gen gen, Barconfig *config) {
562 if (TAILQ_EMPTY(&(config->bar_bindings)))
568 struct Barbinding *current;
569 TAILQ_FOREACH(current, &(config->bar_bindings), bindings) {
573 y(integer, current->input_code);
575 ystr(current->command);
577 y(bool, current->release == B_UPON_KEYRELEASE);
585 static char *canonicalize_output_name(char *name) {
586 /* Do not canonicalize special output names. */
587 if (strcasecmp(name, "primary") == 0) {
590 Output *output = get_output_by_name(name, false);
591 return output ? output_primary_name(output) : name;
594 static void dump_bar_config(yajl_gen gen, Barconfig *config) {
600 if (config->num_outputs > 0) {
603 for (int c = 0; c < config->num_outputs; c++) {
604 /* Convert monitor names (RandR ≥ 1.5) or output names
605 * (RandR < 1.5) into monitor names. This way, existing
606 * configs which use output names transparently keep
608 ystr(canonicalize_output_name(config->outputs[c]));
613 if (!TAILQ_EMPTY(&(config->tray_outputs))) {
614 ystr("tray_outputs");
617 struct tray_output_t *tray_output;
618 TAILQ_FOREACH(tray_output, &(config->tray_outputs), tray_outputs) {
619 ystr(canonicalize_output_name(tray_output->output));
625 #define YSTR_IF_SET(name) \
627 if (config->name) { \
629 ystr(config->name); \
633 ystr("tray_padding");
634 y(integer, config->tray_padding);
636 YSTR_IF_SET(socket_path);
639 switch (config->mode) {
652 ystr("hidden_state");
653 switch (config->hidden_state) {
664 y(integer, config->modifier);
666 dump_bar_bindings(gen, config);
669 if (config->position == P_BOTTOM)
674 YSTR_IF_SET(status_command);
677 if (config->separator_symbol) {
678 ystr("separator_symbol");
679 ystr(config->separator_symbol);
682 ystr("workspace_buttons");
683 y(bool, !config->hide_workspace_buttons);
685 ystr("strip_workspace_numbers");
686 y(bool, config->strip_workspace_numbers);
688 ystr("strip_workspace_name");
689 y(bool, config->strip_workspace_name);
691 ystr("binding_mode_indicator");
692 y(bool, !config->hide_binding_mode_indicator);
695 y(bool, config->verbose);
698 #define YSTR_IF_SET(name) \
700 if (config->colors.name) { \
702 ystr(config->colors.name); \
708 YSTR_IF_SET(background);
709 YSTR_IF_SET(statusline);
710 YSTR_IF_SET(separator);
711 YSTR_IF_SET(focused_background);
712 YSTR_IF_SET(focused_statusline);
713 YSTR_IF_SET(focused_separator);
714 YSTR_IF_SET(focused_workspace_border);
715 YSTR_IF_SET(focused_workspace_bg);
716 YSTR_IF_SET(focused_workspace_text);
717 YSTR_IF_SET(active_workspace_border);
718 YSTR_IF_SET(active_workspace_bg);
719 YSTR_IF_SET(active_workspace_text);
720 YSTR_IF_SET(inactive_workspace_border);
721 YSTR_IF_SET(inactive_workspace_bg);
722 YSTR_IF_SET(inactive_workspace_text);
723 YSTR_IF_SET(urgent_workspace_border);
724 YSTR_IF_SET(urgent_workspace_bg);
725 YSTR_IF_SET(urgent_workspace_text);
726 YSTR_IF_SET(binding_mode_border);
727 YSTR_IF_SET(binding_mode_bg);
728 YSTR_IF_SET(binding_mode_text);
736 setlocale(LC_NUMERIC, "C");
737 yajl_gen gen = ygenalloc();
738 dump_node(gen, croot, false);
739 setlocale(LC_NUMERIC, "");
741 const unsigned char *payload;
743 y(get_buf, &payload, &length);
745 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
750 * Formats the reply message for a GET_WORKSPACES request and sends it to the
754 IPC_HANDLER(get_workspaces) {
755 yajl_gen gen = ygenalloc();
758 Con *focused_ws = con_get_workspace(focused);
761 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
762 if (con_is_internal(output))
765 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
766 assert(ws->type == CT_WORKSPACE);
776 y(bool, workspace_is_visible(ws));
779 y(bool, ws == focused_ws);
784 y(integer, ws->rect.x);
786 y(integer, ws->rect.y);
788 y(integer, ws->rect.width);
790 y(integer, ws->rect.height);
805 const unsigned char *payload;
807 y(get_buf, &payload, &length);
809 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
814 * Formats the reply message for a GET_OUTPUTS request and sends it to the
818 IPC_HANDLER(get_outputs) {
819 yajl_gen gen = ygenalloc();
823 TAILQ_FOREACH(output, &outputs, outputs) {
827 ystr(output_primary_name(output));
830 y(bool, output->active);
833 y(bool, output->primary);
838 y(integer, output->rect.x);
840 y(integer, output->rect.y);
842 y(integer, output->rect.width);
844 y(integer, output->rect.height);
847 ystr("current_workspace");
849 if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
859 const unsigned char *payload;
861 y(get_buf, &payload, &length);
863 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
868 * Formats the reply message for a GET_MARKS request and sends it to the
872 IPC_HANDLER(get_marks) {
873 yajl_gen gen = ygenalloc();
877 TAILQ_FOREACH(con, &all_cons, all_cons) {
879 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
886 const unsigned char *payload;
888 y(get_buf, &payload, &length);
890 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
895 * Returns the version of i3
898 IPC_HANDLER(get_version) {
899 yajl_gen gen = ygenalloc();
903 y(integer, MAJOR_VERSION);
906 y(integer, MINOR_VERSION);
909 y(integer, PATCH_VERSION);
911 ystr("human_readable");
914 ystr("loaded_config_file_name");
915 ystr(current_configpath);
919 const unsigned char *payload;
921 y(get_buf, &payload, &length);
923 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
928 * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
932 IPC_HANDLER(get_bar_config) {
933 yajl_gen gen = ygenalloc();
935 /* If no ID was passed, we return a JSON array with all IDs */
936 if (message_size == 0) {
939 TAILQ_FOREACH(current, &barconfigs, configs) {
944 const unsigned char *payload;
946 y(get_buf, &payload, &length);
948 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
953 /* To get a properly terminated buffer, we copy
954 * message_size bytes out of the buffer */
956 sasprintf(&bar_id, "%.*s", message_size, message);
957 LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
958 Barconfig *current, *config = NULL;
959 TAILQ_FOREACH(current, &barconfigs, configs) {
960 if (strcmp(current->id, bar_id) != 0)
969 /* If we did not find a config for the given ID, the reply will contain
970 * a null 'id' field. */
978 dump_bar_config(gen, config);
981 const unsigned char *payload;
983 y(get_buf, &payload, &length);
985 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
990 * Returns a list of configured binding modes
993 IPC_HANDLER(get_binding_modes) {
994 yajl_gen gen = ygenalloc();
998 SLIST_FOREACH(mode, &modes, modes) {
1003 const unsigned char *payload;
1005 y(get_buf, &payload, &length);
1007 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BINDING_MODES, payload);
1012 * Callback for the YAJL parser (will be called when a string is parsed).
1015 static int add_subscription(void *extra, const unsigned char *s,
1017 ipc_client *client = extra;
1019 DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
1020 int event = client->num_events;
1022 client->num_events++;
1023 client->events = srealloc(client->events, client->num_events * sizeof(char *));
1024 /* We copy the string because it is not null-terminated and strndup()
1025 * is missing on some BSD systems */
1026 client->events[event] = scalloc(len + 1, 1);
1027 memcpy(client->events[event], s, len);
1029 DLOG("client is now subscribed to:\n");
1030 for (int i = 0; i < client->num_events; i++) {
1031 DLOG("event %s\n", client->events[i]);
1039 * Subscribes this connection to the event types which were given as a JSON
1040 * serialized array in the payload field of the message.
1043 IPC_HANDLER(subscribe) {
1046 ipc_client *current, *client = NULL;
1048 /* Search the ipc_client structure for this connection */
1049 TAILQ_FOREACH(current, &all_clients, clients) {
1050 if (current->fd != fd)
1057 if (client == NULL) {
1058 ELOG("Could not find ipc_client data structure for fd %d\n", fd);
1062 /* Setup the JSON parser */
1063 static yajl_callbacks callbacks = {
1064 .yajl_string = add_subscription,
1067 p = yalloc(&callbacks, (void *)client);
1068 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1069 if (stat != yajl_status_ok) {
1071 err = yajl_get_error(p, true, (const unsigned char *)message,
1073 ELOG("YAJL parse error: %s\n", err);
1074 yajl_free_error(p, err);
1076 const char *reply = "{\"success\":false}";
1077 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1082 const char *reply = "{\"success\":true}";
1083 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1085 if (client->first_tick_sent) {
1089 bool is_tick = false;
1090 for (int i = 0; i < client->num_events; i++) {
1091 if (strcmp(client->events[i], "tick") == 0) {
1100 client->first_tick_sent = true;
1101 const char *payload = "{\"first\":true,\"payload\":\"\"}";
1102 ipc_send_message(client->fd, strlen(payload), I3_IPC_EVENT_TICK, (const uint8_t *)payload);
1106 * Returns the raw last loaded i3 configuration file contents.
1108 IPC_HANDLER(get_config) {
1109 yajl_gen gen = ygenalloc();
1114 ystr(current_config);
1118 const unsigned char *payload;
1120 y(get_buf, &payload, &length);
1122 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_CONFIG, payload);
1127 * Sends the tick event from the message payload to subscribers. Establishes a
1128 * synchronization point in event-related tests.
1130 IPC_HANDLER(send_tick) {
1131 yajl_gen gen = ygenalloc();
1139 yajl_gen_string(gen, (unsigned char *)message, message_size);
1143 const unsigned char *payload;
1145 y(get_buf, &payload, &length);
1147 ipc_send_event("tick", I3_IPC_EVENT_TICK, (const char *)payload);
1150 const char *reply = "{\"success\":true}";
1151 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_TICK, (const uint8_t *)reply);
1152 DLOG("Sent tick event\n");
1158 xcb_window_t window;
1161 static int _sync_json_key(void *extra, const unsigned char *val, size_t len) {
1162 struct sync_state *state = extra;
1163 FREE(state->last_key);
1164 state->last_key = scalloc(len + 1, 1);
1165 memcpy(state->last_key, val, len);
1169 static int _sync_json_int(void *extra, long long val) {
1170 struct sync_state *state = extra;
1171 if (strcasecmp(state->last_key, "rnd") == 0) {
1173 } else if (strcasecmp(state->last_key, "window") == 0) {
1174 state->window = (xcb_window_t)val;
1183 /* Setup the JSON parser */
1184 static yajl_callbacks callbacks = {
1185 .yajl_map_key = _sync_json_key,
1186 .yajl_integer = _sync_json_int,
1189 struct sync_state state;
1190 memset(&state, '\0', sizeof(struct sync_state));
1191 p = yalloc(&callbacks, (void *)&state);
1192 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1193 FREE(state.last_key);
1194 if (stat != yajl_status_ok) {
1196 err = yajl_get_error(p, true, (const unsigned char *)message,
1198 ELOG("YAJL parse error: %s\n", err);
1199 yajl_free_error(p, err);
1201 const char *reply = "{\"success\":false}";
1202 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1208 DLOG("received IPC sync request (rnd = %d, window = 0x%08x)\n", state.rnd, state.window);
1209 sync_respond(state.window, state.rnd);
1210 const char *reply = "{\"success\":true}";
1211 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
1214 /* The index of each callback function corresponds to the numeric
1215 * value of the message type (see include/i3/ipc.h) */
1216 handler_t handlers[12] = {
1218 handle_get_workspaces,
1223 handle_get_bar_config,
1225 handle_get_binding_modes,
1232 * Handler for activity on a client connection, receives a message from a
1235 * For now, the maximum message size is 2048. I’m not sure for what the
1236 * IPC interface will be used in the future, thus I’m not implementing a
1237 * mechanism for arbitrarily long messages, as it seems like overkill
1241 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
1242 uint32_t message_type;
1243 uint32_t message_length;
1244 uint8_t *message = NULL;
1246 int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
1247 /* EOF or other error */
1249 /* Was this a spurious read? See ev(3) */
1250 if (ret == -1 && errno == EAGAIN) {
1255 /* If not, there was some kind of error. We don’t bother and close the
1256 * connection. Delete the client from the list of clients. */
1257 bool closed = false;
1258 ipc_client *current;
1259 TAILQ_FOREACH(current, &all_clients, clients) {
1260 if (current->fd != w->fd)
1263 free_ipc_client(current);
1271 ev_io_stop(EV_A_ w);
1275 DLOG("IPC: client disconnected\n");
1279 if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
1280 DLOG("Unhandled message type: %d\n", message_type);
1282 handler_t h = handlers[message_type];
1283 h(w->fd, message, 0, message_length, message_type);
1290 * Handler for activity on the listening socket, meaning that a new client
1291 * has just connected and we should accept() him. Sets up the event handler
1292 * for activity on the new connection and inserts the file descriptor into
1293 * the list of clients.
1296 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
1297 struct sockaddr_un peer;
1298 socklen_t len = sizeof(struct sockaddr_un);
1300 if ((client = accept(w->fd, (struct sockaddr *)&peer, &len)) < 0) {
1308 /* Close this file descriptor on exec() */
1309 (void)fcntl(client, F_SETFD, FD_CLOEXEC);
1311 set_nonblock(client);
1313 struct ev_io *package = scalloc(1, sizeof(struct ev_io));
1314 ev_io_init(package, ipc_receive_message, client, EV_READ);
1315 ev_io_start(EV_A_ package);
1317 DLOG("IPC: new client connected on fd %d\n", w->fd);
1319 ipc_client *new = scalloc(1, sizeof(ipc_client));
1322 TAILQ_INSERT_TAIL(&all_clients, new, clients);
1326 * Creates the UNIX domain socket at the given path, sets it to non-blocking
1327 * mode, bind()s and listen()s on it.
1330 int ipc_create_socket(const char *filename) {
1333 FREE(current_socketpath);
1335 char *resolved = resolve_tilde(filename);
1336 DLOG("Creating IPC-socket at %s\n", resolved);
1337 char *copy = sstrdup(resolved);
1338 const char *dir = dirname(copy);
1339 if (!path_exists(dir))
1340 mkdirp(dir, DEFAULT_DIR_MODE);
1343 /* Unlink the unix domain socket before */
1346 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1352 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1354 struct sockaddr_un addr;
1355 memset(&addr, 0, sizeof(struct sockaddr_un));
1356 addr.sun_family = AF_LOCAL;
1357 strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
1358 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
1364 set_nonblock(sockfd);
1366 if (listen(sockfd, 5) < 0) {
1372 current_socketpath = resolved;
1377 * Generates a json workspace event. Returns a dynamically allocated yajl
1378 * generator. Free with yajl_gen_free().
1380 yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old) {
1381 setlocale(LC_NUMERIC, "C");
1382 yajl_gen gen = ygenalloc();
1390 if (current == NULL)
1393 dump_node(gen, current, false);
1399 dump_node(gen, old, false);
1403 setlocale(LC_NUMERIC, "");
1409 * For the workspace events we send, along with the usual "change" field, also
1410 * the workspace container in "current". For focus events, we send the
1411 * previously focused workspace in "old".
1413 void ipc_send_workspace_event(const char *change, Con *current, Con *old) {
1414 yajl_gen gen = ipc_marshal_workspace_event(change, current, old);
1416 const unsigned char *payload;
1418 y(get_buf, &payload, &length);
1420 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
1426 * For the window events we send, along the usual "change" field,
1427 * also the window container, in "container".
1429 void ipc_send_window_event(const char *property, Con *con) {
1430 DLOG("Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1431 property, con, (con->window ? con->window->id : XCB_WINDOW_NONE));
1433 setlocale(LC_NUMERIC, "C");
1434 yajl_gen gen = ygenalloc();
1442 dump_node(gen, con, false);
1446 const unsigned char *payload;
1448 y(get_buf, &payload, &length);
1450 ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload);
1452 setlocale(LC_NUMERIC, "");
1456 * For the barconfig update events, we send the serialized barconfig.
1458 void ipc_send_barconfig_update_event(Barconfig *barconfig) {
1459 DLOG("Issue barconfig_update event for id = %s\n", barconfig->id);
1460 setlocale(LC_NUMERIC, "C");
1461 yajl_gen gen = ygenalloc();
1463 dump_bar_config(gen, barconfig);
1465 const unsigned char *payload;
1467 y(get_buf, &payload, &length);
1469 ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (const char *)payload);
1471 setlocale(LC_NUMERIC, "");
1475 * For the binding events, we send the serialized binding struct.
1477 void ipc_send_binding_event(const char *event_type, Binding *bind) {
1478 DLOG("Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->symbol, bind->keycode);
1480 setlocale(LC_NUMERIC, "C");
1482 yajl_gen gen = ygenalloc();
1490 dump_binding(gen, bind);
1494 const unsigned char *payload;
1496 y(get_buf, &payload, &length);
1498 ipc_send_event("binding", I3_IPC_EVENT_BINDING, (const char *)payload);
1501 setlocale(LC_NUMERIC, "");