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");
272 DLOG("About to dump unknown container type=%d. This is a bug.\n", con->type);
277 /* provided for backwards compatibility only. */
279 if (!con_is_split(con))
282 if (con_orientation(con) == HORIZ)
288 ystr("scratchpad_state");
289 switch (con->scratchpad_state) {
290 case SCRATCHPAD_NONE:
293 case SCRATCHPAD_FRESH:
296 case SCRATCHPAD_CHANGED:
302 if (con->percent == 0.0)
305 y(double, con->percent);
308 y(bool, con->urgent);
310 if (!TAILQ_EMPTY(&(con->marks_head))) {
315 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
323 y(bool, (con == focused));
325 if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
327 ystr(con_get_output(con)->name);
331 switch (con->layout) {
333 DLOG("About to dump layout=default, this is a bug in the code.\n");
356 ystr("workspace_layout");
357 switch (con->workspace_layout) {
368 DLOG("About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
373 ystr("last_split_layout");
374 switch (con->layout) {
384 switch (con->border_style) {
396 ystr("current_border_width");
397 y(integer, con->current_border_width);
399 dump_rect(gen, "rect", con->rect);
400 dump_rect(gen, "deco_rect", con->deco_rect);
401 dump_rect(gen, "window_rect", con->window_rect);
402 dump_rect(gen, "geometry", con->geometry);
405 if (con->window && con->window->name)
406 ystr(i3string_as_utf8(con->window->name));
407 else if (con->name != NULL)
412 if (con->title_format != NULL) {
413 ystr("title_format");
414 ystr(con->title_format);
417 if (con->type == CT_WORKSPACE) {
419 y(integer, con->num);
424 y(integer, con->window->id);
428 if (con->window && !inplace_restart) {
429 /* Window properties are useless to preserve when restarting because
430 * they will be queried again anyway. However, for i3-save-tree(1),
431 * they are very useful and save i3-save-tree dealing with X11. */
432 ystr("window_properties");
435 #define DUMP_PROPERTY(key, prop_name) \
437 if (con->window->prop_name != NULL) { \
439 ystr(con->window->prop_name); \
443 DUMP_PROPERTY("class", class_class);
444 DUMP_PROPERTY("instance", class_instance);
445 DUMP_PROPERTY("window_role", role);
447 if (con->window->name != NULL) {
449 ystr(i3string_as_utf8(con->window->name));
452 ystr("transient_for");
453 if (con->window->transient_for == XCB_NONE)
456 y(integer, con->window->transient_for);
464 if (con->type != CT_DOCKAREA || !inplace_restart) {
465 TAILQ_FOREACH(node, &(con->nodes_head), nodes) {
466 dump_node(gen, node, inplace_restart);
471 ystr("floating_nodes");
473 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
474 dump_node(gen, node, inplace_restart);
480 TAILQ_FOREACH(node, &(con->focus_head), focused) {
481 y(integer, (uintptr_t)node);
485 ystr("fullscreen_mode");
486 y(integer, con->fullscreen_mode);
489 y(bool, con->sticky);
492 switch (con->floating) {
493 case FLOATING_AUTO_OFF:
496 case FLOATING_AUTO_ON:
499 case FLOATING_USER_OFF:
502 case FLOATING_USER_ON:
510 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
511 /* We will generate a new restart_mode match specification after this
512 * loop, so skip this one. */
513 if (match->restart_mode)
516 if (match->dock != M_DONTCHECK) {
518 y(integer, match->dock);
519 ystr("insert_where");
520 y(integer, match->insert_where);
523 #define DUMP_REGEX(re_name) \
525 if (match->re_name != NULL) { \
527 ystr(match->re_name->pattern); \
532 DUMP_REGEX(instance);
533 DUMP_REGEX(window_role);
540 if (inplace_restart) {
541 if (con->window != NULL) {
544 y(integer, con->window->id);
545 ystr("restart_mode");
552 if (inplace_restart && con->window != NULL) {
554 y(integer, con->depth);
560 static void dump_bar_bindings(yajl_gen gen, Barconfig *config) {
561 if (TAILQ_EMPTY(&(config->bar_bindings)))
567 struct Barbinding *current;
568 TAILQ_FOREACH(current, &(config->bar_bindings), bindings) {
572 y(integer, current->input_code);
574 ystr(current->command);
576 y(bool, current->release == B_UPON_KEYRELEASE);
584 static char *canonicalize_output_name(char *name) {
585 /* Do not canonicalize special output names. */
586 if (strcasecmp(name, "primary") == 0) {
589 Output *output = get_output_by_name(name, false);
590 return output ? output_primary_name(output) : name;
593 static void dump_bar_config(yajl_gen gen, Barconfig *config) {
599 if (config->num_outputs > 0) {
602 for (int c = 0; c < config->num_outputs; c++) {
603 /* Convert monitor names (RandR ≥ 1.5) or output names
604 * (RandR < 1.5) into monitor names. This way, existing
605 * configs which use output names transparently keep
607 ystr(canonicalize_output_name(config->outputs[c]));
612 if (!TAILQ_EMPTY(&(config->tray_outputs))) {
613 ystr("tray_outputs");
616 struct tray_output_t *tray_output;
617 TAILQ_FOREACH(tray_output, &(config->tray_outputs), tray_outputs) {
618 ystr(canonicalize_output_name(tray_output->output));
624 #define YSTR_IF_SET(name) \
626 if (config->name) { \
628 ystr(config->name); \
632 ystr("tray_padding");
633 y(integer, config->tray_padding);
635 YSTR_IF_SET(socket_path);
638 switch (config->mode) {
651 ystr("hidden_state");
652 switch (config->hidden_state) {
663 switch (config->modifier) {
690 dump_bar_bindings(gen, config);
693 if (config->position == P_BOTTOM)
698 YSTR_IF_SET(status_command);
701 if (config->separator_symbol) {
702 ystr("separator_symbol");
703 ystr(config->separator_symbol);
706 ystr("workspace_buttons");
707 y(bool, !config->hide_workspace_buttons);
709 ystr("strip_workspace_numbers");
710 y(bool, config->strip_workspace_numbers);
712 ystr("strip_workspace_name");
713 y(bool, config->strip_workspace_name);
715 ystr("binding_mode_indicator");
716 y(bool, !config->hide_binding_mode_indicator);
719 y(bool, config->verbose);
722 #define YSTR_IF_SET(name) \
724 if (config->colors.name) { \
726 ystr(config->colors.name); \
732 YSTR_IF_SET(background);
733 YSTR_IF_SET(statusline);
734 YSTR_IF_SET(separator);
735 YSTR_IF_SET(focused_background);
736 YSTR_IF_SET(focused_statusline);
737 YSTR_IF_SET(focused_separator);
738 YSTR_IF_SET(focused_workspace_border);
739 YSTR_IF_SET(focused_workspace_bg);
740 YSTR_IF_SET(focused_workspace_text);
741 YSTR_IF_SET(active_workspace_border);
742 YSTR_IF_SET(active_workspace_bg);
743 YSTR_IF_SET(active_workspace_text);
744 YSTR_IF_SET(inactive_workspace_border);
745 YSTR_IF_SET(inactive_workspace_bg);
746 YSTR_IF_SET(inactive_workspace_text);
747 YSTR_IF_SET(urgent_workspace_border);
748 YSTR_IF_SET(urgent_workspace_bg);
749 YSTR_IF_SET(urgent_workspace_text);
750 YSTR_IF_SET(binding_mode_border);
751 YSTR_IF_SET(binding_mode_bg);
752 YSTR_IF_SET(binding_mode_text);
760 setlocale(LC_NUMERIC, "C");
761 yajl_gen gen = ygenalloc();
762 dump_node(gen, croot, false);
763 setlocale(LC_NUMERIC, "");
765 const unsigned char *payload;
767 y(get_buf, &payload, &length);
769 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
774 * Formats the reply message for a GET_WORKSPACES request and sends it to the
778 IPC_HANDLER(get_workspaces) {
779 yajl_gen gen = ygenalloc();
782 Con *focused_ws = con_get_workspace(focused);
785 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
786 if (con_is_internal(output))
789 TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
790 assert(ws->type == CT_WORKSPACE);
800 y(bool, workspace_is_visible(ws));
803 y(bool, ws == focused_ws);
808 y(integer, ws->rect.x);
810 y(integer, ws->rect.y);
812 y(integer, ws->rect.width);
814 y(integer, ws->rect.height);
829 const unsigned char *payload;
831 y(get_buf, &payload, &length);
833 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
838 * Formats the reply message for a GET_OUTPUTS request and sends it to the
842 IPC_HANDLER(get_outputs) {
843 yajl_gen gen = ygenalloc();
847 TAILQ_FOREACH(output, &outputs, outputs) {
851 ystr(output_primary_name(output));
854 y(bool, output->active);
857 y(bool, output->primary);
862 y(integer, output->rect.x);
864 y(integer, output->rect.y);
866 y(integer, output->rect.width);
868 y(integer, output->rect.height);
871 ystr("current_workspace");
873 if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
883 const unsigned char *payload;
885 y(get_buf, &payload, &length);
887 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
892 * Formats the reply message for a GET_MARKS request and sends it to the
896 IPC_HANDLER(get_marks) {
897 yajl_gen gen = ygenalloc();
901 TAILQ_FOREACH(con, &all_cons, all_cons) {
903 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
910 const unsigned char *payload;
912 y(get_buf, &payload, &length);
914 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
919 * Returns the version of i3
922 IPC_HANDLER(get_version) {
923 yajl_gen gen = ygenalloc();
927 y(integer, MAJOR_VERSION);
930 y(integer, MINOR_VERSION);
933 y(integer, PATCH_VERSION);
935 ystr("human_readable");
938 ystr("loaded_config_file_name");
939 ystr(current_configpath);
943 const unsigned char *payload;
945 y(get_buf, &payload, &length);
947 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
952 * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
956 IPC_HANDLER(get_bar_config) {
957 yajl_gen gen = ygenalloc();
959 /* If no ID was passed, we return a JSON array with all IDs */
960 if (message_size == 0) {
963 TAILQ_FOREACH(current, &barconfigs, configs) {
968 const unsigned char *payload;
970 y(get_buf, &payload, &length);
972 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
977 /* To get a properly terminated buffer, we copy
978 * message_size bytes out of the buffer */
980 sasprintf(&bar_id, "%.*s", message_size, message);
981 LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
982 Barconfig *current, *config = NULL;
983 TAILQ_FOREACH(current, &barconfigs, configs) {
984 if (strcmp(current->id, bar_id) != 0)
993 /* If we did not find a config for the given ID, the reply will contain
994 * a null 'id' field. */
1002 dump_bar_config(gen, config);
1005 const unsigned char *payload;
1007 y(get_buf, &payload, &length);
1009 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
1014 * Returns a list of configured binding modes
1017 IPC_HANDLER(get_binding_modes) {
1018 yajl_gen gen = ygenalloc();
1022 SLIST_FOREACH(mode, &modes, modes) {
1027 const unsigned char *payload;
1029 y(get_buf, &payload, &length);
1031 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BINDING_MODES, payload);
1036 * Callback for the YAJL parser (will be called when a string is parsed).
1039 static int add_subscription(void *extra, const unsigned char *s,
1041 ipc_client *client = extra;
1043 DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
1044 int event = client->num_events;
1046 client->num_events++;
1047 client->events = srealloc(client->events, client->num_events * sizeof(char *));
1048 /* We copy the string because it is not null-terminated and strndup()
1049 * is missing on some BSD systems */
1050 client->events[event] = scalloc(len + 1, 1);
1051 memcpy(client->events[event], s, len);
1053 DLOG("client is now subscribed to:\n");
1054 for (int i = 0; i < client->num_events; i++) {
1055 DLOG("event %s\n", client->events[i]);
1063 * Subscribes this connection to the event types which were given as a JSON
1064 * serialized array in the payload field of the message.
1067 IPC_HANDLER(subscribe) {
1070 ipc_client *current, *client = NULL;
1072 /* Search the ipc_client structure for this connection */
1073 TAILQ_FOREACH(current, &all_clients, clients) {
1074 if (current->fd != fd)
1081 if (client == NULL) {
1082 ELOG("Could not find ipc_client data structure for fd %d\n", fd);
1086 /* Setup the JSON parser */
1087 static yajl_callbacks callbacks = {
1088 .yajl_string = add_subscription,
1091 p = yalloc(&callbacks, (void *)client);
1092 stat = yajl_parse(p, (const unsigned char *)message, message_size);
1093 if (stat != yajl_status_ok) {
1095 err = yajl_get_error(p, true, (const unsigned char *)message,
1097 ELOG("YAJL parse error: %s\n", err);
1098 yajl_free_error(p, err);
1100 const char *reply = "{\"success\":false}";
1101 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1106 const char *reply = "{\"success\":true}";
1107 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t *)reply);
1109 if (client->first_tick_sent) {
1113 bool is_tick = false;
1114 for (int i = 0; i < client->num_events; i++) {
1115 if (strcmp(client->events[i], "tick") == 0) {
1124 client->first_tick_sent = true;
1125 const char *payload = "{\"first\":true,\"payload\":\"\"}";
1126 ipc_send_message(client->fd, strlen(payload), I3_IPC_EVENT_TICK, (const uint8_t *)payload);
1130 * Returns the raw last loaded i3 configuration file contents.
1132 IPC_HANDLER(get_config) {
1133 yajl_gen gen = ygenalloc();
1138 ystr(current_config);
1142 const unsigned char *payload;
1144 y(get_buf, &payload, &length);
1146 ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_CONFIG, payload);
1151 * Sends the tick event from the message payload to subscribers. Establishes a
1152 * synchronization point in event-related tests.
1154 IPC_HANDLER(send_tick) {
1155 yajl_gen gen = ygenalloc();
1160 yajl_gen_string(gen, (unsigned char *)message, message_size);
1164 const unsigned char *payload;
1166 y(get_buf, &payload, &length);
1168 ipc_send_event("tick", I3_IPC_EVENT_TICK, (const char *)payload);
1171 const char *reply = "{\"success\":true}";
1172 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_TICK, (const uint8_t *)reply);
1173 DLOG("Sent tick event\n");
1176 /* The index of each callback function corresponds to the numeric
1177 * value of the message type (see include/i3/ipc.h) */
1178 handler_t handlers[11] = {
1180 handle_get_workspaces,
1185 handle_get_bar_config,
1187 handle_get_binding_modes,
1193 * Handler for activity on a client connection, receives a message from a
1196 * For now, the maximum message size is 2048. I’m not sure for what the
1197 * IPC interface will be used in the future, thus I’m not implementing a
1198 * mechanism for arbitrarily long messages, as it seems like overkill
1202 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
1203 uint32_t message_type;
1204 uint32_t message_length;
1205 uint8_t *message = NULL;
1207 int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
1208 /* EOF or other error */
1210 /* Was this a spurious read? See ev(3) */
1211 if (ret == -1 && errno == EAGAIN) {
1216 /* If not, there was some kind of error. We don’t bother
1217 * and close the connection */
1220 /* Delete the client from the list of clients */
1221 ipc_client *current;
1222 TAILQ_FOREACH(current, &all_clients, clients) {
1223 if (current->fd != w->fd)
1226 for (int i = 0; i < current->num_events; i++)
1227 free(current->events[i]);
1228 free(current->events);
1229 /* We can call TAILQ_REMOVE because we break out of the
1230 * TAILQ_FOREACH afterwards */
1231 TAILQ_REMOVE(&all_clients, current, clients);
1236 ev_io_stop(EV_A_ w);
1240 DLOG("IPC: client disconnected\n");
1244 if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
1245 DLOG("Unhandled message type: %d\n", message_type);
1247 handler_t h = handlers[message_type];
1248 h(w->fd, message, 0, message_length, message_type);
1255 * Handler for activity on the listening socket, meaning that a new client
1256 * has just connected and we should accept() him. Sets up the event handler
1257 * for activity on the new connection and inserts the file descriptor into
1258 * the list of clients.
1261 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
1262 struct sockaddr_un peer;
1263 socklen_t len = sizeof(struct sockaddr_un);
1265 if ((client = accept(w->fd, (struct sockaddr *)&peer, &len)) < 0) {
1273 /* Close this file descriptor on exec() */
1274 (void)fcntl(client, F_SETFD, FD_CLOEXEC);
1276 set_nonblock(client);
1278 struct ev_io *package = scalloc(1, sizeof(struct ev_io));
1279 ev_io_init(package, ipc_receive_message, client, EV_READ);
1280 ev_io_start(EV_A_ package);
1282 DLOG("IPC: new client connected on fd %d\n", w->fd);
1284 ipc_client *new = scalloc(1, sizeof(ipc_client));
1287 TAILQ_INSERT_TAIL(&all_clients, new, clients);
1291 * Creates the UNIX domain socket at the given path, sets it to non-blocking
1292 * mode, bind()s and listen()s on it.
1295 int ipc_create_socket(const char *filename) {
1298 FREE(current_socketpath);
1300 char *resolved = resolve_tilde(filename);
1301 DLOG("Creating IPC-socket at %s\n", resolved);
1302 char *copy = sstrdup(resolved);
1303 const char *dir = dirname(copy);
1304 if (!path_exists(dir))
1305 mkdirp(dir, DEFAULT_DIR_MODE);
1308 /* Unlink the unix domain socket before */
1311 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1317 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1319 struct sockaddr_un addr;
1320 memset(&addr, 0, sizeof(struct sockaddr_un));
1321 addr.sun_family = AF_LOCAL;
1322 strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
1323 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
1329 set_nonblock(sockfd);
1331 if (listen(sockfd, 5) < 0) {
1337 current_socketpath = resolved;
1342 * Generates a json workspace event. Returns a dynamically allocated yajl
1343 * generator. Free with yajl_gen_free().
1345 yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old) {
1346 setlocale(LC_NUMERIC, "C");
1347 yajl_gen gen = ygenalloc();
1355 if (current == NULL)
1358 dump_node(gen, current, false);
1364 dump_node(gen, old, false);
1368 setlocale(LC_NUMERIC, "");
1374 * For the workspace events we send, along with the usual "change" field, also
1375 * the workspace container in "current". For focus events, we send the
1376 * previously focused workspace in "old".
1378 void ipc_send_workspace_event(const char *change, Con *current, Con *old) {
1379 yajl_gen gen = ipc_marshal_workspace_event(change, current, old);
1381 const unsigned char *payload;
1383 y(get_buf, &payload, &length);
1385 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
1391 * For the window events we send, along the usual "change" field,
1392 * also the window container, in "container".
1394 void ipc_send_window_event(const char *property, Con *con) {
1395 DLOG("Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1396 property, con, (con->window ? con->window->id : XCB_WINDOW_NONE));
1398 setlocale(LC_NUMERIC, "C");
1399 yajl_gen gen = ygenalloc();
1407 dump_node(gen, con, false);
1411 const unsigned char *payload;
1413 y(get_buf, &payload, &length);
1415 ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload);
1417 setlocale(LC_NUMERIC, "");
1421 * For the barconfig update events, we send the serialized barconfig.
1423 void ipc_send_barconfig_update_event(Barconfig *barconfig) {
1424 DLOG("Issue barconfig_update event for id = %s\n", barconfig->id);
1425 setlocale(LC_NUMERIC, "C");
1426 yajl_gen gen = ygenalloc();
1428 dump_bar_config(gen, barconfig);
1430 const unsigned char *payload;
1432 y(get_buf, &payload, &length);
1434 ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (const char *)payload);
1436 setlocale(LC_NUMERIC, "");
1440 * For the binding events, we send the serialized binding struct.
1442 void ipc_send_binding_event(const char *event_type, Binding *bind) {
1443 DLOG("Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->symbol, bind->keycode);
1445 setlocale(LC_NUMERIC, "C");
1447 yajl_gen gen = ygenalloc();
1455 dump_binding(gen, bind);
1459 const unsigned char *payload;
1461 y(get_buf, &payload, &length);
1463 ipc_send_event("binding", I3_IPC_EVENT_BINDING, (const char *)payload);
1466 setlocale(LC_NUMERIC, "");