19 #include <sys/socket.h>
23 #include <yajl/yajl_gen.h>
24 #include <yajl/yajl_parse.h>
33 static ev_tstamp kill_timeout = 10.0;
69 struct ev_timer *timeout =
scalloc(1,
sizeof(
struct ev_timer));
71 timeout->data = client;
73 ev_set_priority(timeout, EV_MINPRI);
75 }
else if (result > 0) {
79 ev_timer_set(client->
timeout, kill_timeout, 0.0);
99 const i3_ipc_header_t
header = {
100 .magic = {
'i',
'3',
'-',
'i',
'p',
'c'},
102 .type = message_type};
103 const size_t header_size =
sizeof(i3_ipc_header_t);
104 const size_t message_size = header_size + size;
118 if (client->
fd != exempt_fd) {
119 DLOG(
"Disconnecting client on fd %d\n", client->
fd);
134 for (
int i = 0; i < client->
num_events; i++) {
147 void ipc_send_event(
const char *event, uint32_t message_type,
const char *payload) {
150 for (
int i = 0; i < current->
num_events; i++) {
151 if (strcasecmp(current->
events[i], event) == 0) {
176 const unsigned char *payload;
179 y(get_buf, &payload, &length);
180 ipc_send_event(
"shutdown", I3_IPC_EVENT_SHUTDOWN, (
const char *)payload);
198 if (current->
fd != exempt_fd) {
199 shutdown(current->
fd, SHUT_RDWR);
212 char *command =
sstrndup((
const char *)message, message_size);
213 LOG(
"IPC: received: *%.4000s*\n", command);
214 yajl_gen gen = yajl_gen_alloc(NULL);
224 const unsigned char *reply;
226 yajl_gen_get_buf(gen, &reply, &length);
229 (
const uint8_t *)reply);
238 y(integer, (int32_t)r.
x);
240 y(integer, (int32_t)r.
y);
257 y(integer, gaps.
top);
260 y(integer, gaps.
top);
266 y(integer, gaps.
left);
272 for (
int i = 0; i < 20; i++) {
275 case XCB_KEY_BUT_MASK_SHIFT:
278 case XCB_KEY_BUT_MASK_LOCK:
281 case XCB_KEY_BUT_MASK_CONTROL:
284 case XCB_KEY_BUT_MASK_MOD_1:
287 case XCB_KEY_BUT_MASK_MOD_2:
290 case XCB_KEY_BUT_MASK_MOD_3:
293 case XCB_KEY_BUT_MASK_MOD_4:
296 case XCB_KEY_BUT_MASK_MOD_5:
299 case XCB_KEY_BUT_MASK_BUTTON_1:
302 case XCB_KEY_BUT_MASK_BUTTON_2:
305 case XCB_KEY_BUT_MASK_BUTTON_3:
308 case XCB_KEY_BUT_MASK_BUTTON_4:
311 case XCB_KEY_BUT_MASK_BUTTON_5:
354 ystr(
"event_state_mask");
363 y(integer, (uintptr_t)con);
376 case CT_FLOATING_CON:
377 ystr(
"floating_con");
398 ystr(
"scratchpad_state");
399 switch (con->scratchpad_state) {
400 case SCRATCHPAD_NONE:
403 case SCRATCHPAD_FRESH:
406 case SCRATCHPAD_CHANGED:
412 if (con->percent == 0.0)
415 y(
double, con->percent);
418 y(
bool, con->urgent);
431 if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
437 switch (con->layout) {
439 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
462 ystr(
"workspace_layout");
463 switch (con->workspace_layout) {
474 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
479 ystr(
"last_split_layout");
480 switch (con->layout) {
490 switch (con->border_style) {
502 ystr(
"current_border_width");
503 y(integer, con->current_border_width);
507 Rect simulated_deco_rect = con->deco_rect;
508 simulated_deco_rect.
x = con->rect.x - con->parent->rect.x;
509 simulated_deco_rect.
y = con->rect.y - con->parent->rect.y;
510 dump_rect(gen,
"deco_rect", simulated_deco_rect);
511 dump_rect(gen,
"actual_deco_rect", con->deco_rect);
513 dump_rect(gen,
"deco_rect", con->deco_rect);
515 dump_rect(gen,
"window_rect", con->window_rect);
516 dump_rect(gen,
"geometry", con->geometry);
519 if (con->window && con->window->name)
521 else if (con->name != NULL)
526 if (con->title_format != NULL) {
527 ystr(
"title_format");
528 ystr(con->title_format);
531 ystr(
"window_icon_padding");
532 y(integer, con->window_icon_padding);
534 if (con->type == CT_WORKSPACE) {
536 y(integer, con->num);
543 y(integer, con->window->id);
549 if (con->window->window_type == A__NET_WM_WINDOW_TYPE_NORMAL) {
551 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_DOCK) {
553 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_DIALOG) {
555 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_UTILITY) {
557 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_TOOLBAR) {
559 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_SPLASH) {
561 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_MENU) {
563 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU) {
564 ystr(
"dropdown_menu");
565 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_POPUP_MENU) {
567 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_TOOLTIP) {
569 }
else if (con->window->window_type == A__NET_WM_WINDOW_TYPE_NOTIFICATION) {
570 ystr(
"notification");
577 if (con->window && !inplace_restart) {
581 ystr(
"window_properties");
584 #define DUMP_PROPERTY(key, prop_name) \
586 if (con->window->prop_name != NULL) { \
588 ystr(con->window->prop_name); \
597 if (con->window->name != NULL) {
602 ystr(
"transient_for");
603 if (con->window->transient_for == XCB_NONE)
606 y(integer, con->window->transient_for);
614 if (con->type != CT_DOCKAREA || !inplace_restart) {
621 ystr(
"floating_nodes");
623 TAILQ_FOREACH (node, &(con->floating_head), floating_windows) {
631 y(integer, (uintptr_t)node);
635 ystr(
"fullscreen_mode");
636 y(integer, con->fullscreen_mode);
639 y(
bool, con->sticky);
642 switch (con->floating) {
643 case FLOATING_AUTO_OFF:
646 case FLOATING_AUTO_ON:
649 case FLOATING_USER_OFF:
652 case FLOATING_USER_ON:
666 if (match->
dock != M_DONTCHECK) {
668 y(integer, match->
dock);
669 ystr(
"insert_where");
673 #define DUMP_REGEX(re_name) \
675 if (match->re_name != NULL) { \
677 ystr(match->re_name->pattern); \
691 if (inplace_restart) {
692 if (con->window != NULL) {
695 y(integer, con->window->id);
696 ystr(
"restart_mode");
703 if (inplace_restart && con->window != NULL) {
705 y(integer, con->depth);
709 ystr(
"previous_workspace_name");
732 y(
bool, current->
release == B_UPON_KEYRELEASE);
742 if (strcasecmp(name,
"primary") == 0 || strcasecmp(name,
"nonprimary") == 0) {
755 if (
config->num_outputs > 0) {
758 for (
int c = 0; c <
config->num_outputs; c++) {
769 ystr(
"tray_outputs");
780 #define YSTR_IF_SET(name) \
782 if (config->name) { \
784 ystr(config->name); \
788 ystr(
"tray_padding");
789 y(integer,
config->tray_padding);
807 ystr(
"hidden_state");
808 switch (
config->hidden_state) {
824 if (
config->position == P_BOTTOM)
835 y(integer,
config->bar_height);
840 if (
config->separator_symbol) {
841 ystr(
"separator_symbol");
845 ystr(
"workspace_buttons");
846 y(
bool, !
config->hide_workspace_buttons);
848 ystr(
"workspace_min_width");
849 y(integer,
config->workspace_min_width);
851 ystr(
"strip_workspace_numbers");
852 y(
bool,
config->strip_workspace_numbers);
854 ystr(
"strip_workspace_name");
855 y(
bool,
config->strip_workspace_name);
857 ystr(
"binding_mode_indicator");
858 y(
bool, !
config->hide_binding_mode_indicator);
864 #define YSTR_IF_SET(name) \
866 if (config->colors.name) { \
868 ystr(config->colors.name); \
902 setlocale(LC_NUMERIC,
"C");
905 setlocale(LC_NUMERIC,
"");
907 const unsigned char *payload;
909 y(get_buf, &payload, &length);
932 assert(ws->
type == CT_WORKSPACE);
936 y(integer, (uintptr_t)ws);
948 y(
bool, ws == focused_ws);
953 y(integer, ws->rect.x);
955 y(integer, ws->rect.y);
957 y(integer, ws->rect.width);
959 y(integer, ws->rect.height);
974 const unsigned char *payload;
976 y(get_buf, &payload, &length);
1011 y(integer,
output->rect.width);
1013 y(integer,
output->rect.height);
1016 ystr(
"current_workspace");
1028 const unsigned char *payload;
1030 y(get_buf, &payload, &length);
1055 const unsigned char *payload;
1057 y(get_buf, &payload, &length);
1072 y(integer, MAJOR_VERSION);
1075 y(integer, MINOR_VERSION);
1078 y(integer, PATCH_VERSION);
1080 ystr(
"human_readable");
1083 ystr(
"loaded_config_file_name");
1086 ystr(
"included_config_file_names");
1099 const unsigned char *payload;
1101 y(get_buf, &payload, &length);
1116 if (message_size == 0) {
1124 const unsigned char *payload;
1126 y(get_buf, &payload, &length);
1135 char *bar_id = NULL;
1136 sasprintf(&bar_id,
"%.*s", message_size, message);
1137 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
1140 if (strcmp(current->
id, bar_id) != 0)
1161 const unsigned char *payload;
1163 y(get_buf, &payload, &length);
1183 const unsigned char *payload;
1185 y(get_buf, &payload, &length);
1199 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
1207 memcpy(client->
events[event], s, len);
1209 DLOG(
"client is now subscribed to:\n");
1210 for (
int i = 0; i < client->
num_events; i++) {
1228 static yajl_callbacks callbacks = {
1232 p =
yalloc(&callbacks, (
void *)client);
1233 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1234 if (stat != yajl_status_ok) {
1236 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1238 ELOG(
"YAJL parse error: %s\n", err);
1239 yajl_free_error(p, err);
1241 const char *reply =
"{\"success\":false}";
1247 const char *reply =
"{\"success\":true}";
1250 if (client->first_tick_sent) {
1254 bool is_tick =
false;
1255 for (
int i = 0; i < client->num_events; i++) {
1256 if (strcmp(client->events[i],
"tick") == 0) {
1265 client->first_tick_sent =
true;
1266 const char *payload =
"{\"first\":true,\"payload\":\"\"}";
1282 ystr(
"included_configs");
1288 ystr(
"raw_contents");
1290 ystr(
"variable_replaced_contents");
1298 const unsigned char *payload;
1300 y(get_buf, &payload, &length);
1319 yajl_gen_string(gen, (
unsigned char *)message, message_size);
1323 const unsigned char *payload;
1325 y(get_buf, &payload, &length);
1327 ipc_send_event(
"tick", I3_IPC_EVENT_TICK, (
const char *)payload);
1330 const char *reply =
"{\"success\":true}";
1332 DLOG(
"Sent tick event\n");
1345 memcpy(
state->last_key, val, len);
1351 if (strcasecmp(
state->last_key,
"rnd") == 0) {
1353 }
else if (strcasecmp(
state->last_key,
"window") == 0) {
1354 state->window = (xcb_window_t)val;
1364 static yajl_callbacks callbacks = {
1372 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1374 if (stat != yajl_status_ok) {
1376 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1378 ELOG(
"YAJL parse error: %s\n", err);
1379 yajl_free_error(p, err);
1381 const char *reply =
"{\"success\":false}";
1388 DLOG(
"received IPC sync request (rnd = %d, window = 0x%08x)\n",
state.rnd,
state.window);
1390 const char *reply =
"{\"success\":true}";
1404 const unsigned char *payload;
1406 y(get_buf, &payload, &length);
1416 handle_get_workspaces,
1421 handle_get_bar_config,
1423 handle_get_binding_modes,
1427 handle_get_binding_state,
1441 uint32_t message_type;
1442 uint32_t message_length;
1443 uint8_t *message = NULL;
1445 assert(client->
fd == w->fd);
1447 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
1451 if (ret == -1 && errno == EAGAIN) {
1464 DLOG(
"Unhandled message type: %d\n", message_type);
1467 h(client, message, 0, message_length, message_type);
1478 char *cmdline = NULL;
1479 #
if defined(__linux__) && defined(SO_PEERCRED)
1480 struct ucred peercred;
1481 socklen_t so_len =
sizeof(peercred);
1482 if (getsockopt(client->
fd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) != 0) {
1486 sasprintf(&exepath,
"/proc/%d/cmdline", peercred.pid);
1488 int fd = open(exepath, O_RDONLY);
1493 char buf[512] = {
'\0'};
1494 const ssize_t n = read(fd, buf,
sizeof(buf));
1499 for (
char *walk = buf; walk < buf + n - 1; walk++) {
1500 if (*walk ==
'\0') {
1507 ELOG(
"client %p with pid %d and cmdline '%s' on fd %d timed out, killing\n", client, peercred.pid, cmdline, client->
fd);
1513 ELOG(
"client %p on fd %d timed out, killing\n", client, client->
fd);
1520 DLOG(
"fd %d writeable\n", w->fd);
1525 assert(client->
timeout != NULL);
1537 struct sockaddr_un peer;
1538 socklen_t len =
sizeof(
struct sockaddr_un);
1540 if ((fd = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
1541 if (errno != EINTR) {
1548 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1576 DLOG(
"IPC: new client connected on fd %d\n", fd);
1586 setlocale(LC_NUMERIC,
"C");
1595 if (current == NULL)
1608 setlocale(LC_NUMERIC,
"");
1621 const unsigned char *payload;
1623 y(get_buf, &payload, &length);
1625 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1635 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1636 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1638 setlocale(LC_NUMERIC,
"C");
1651 const unsigned char *payload;
1653 y(get_buf, &payload, &length);
1655 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1657 setlocale(LC_NUMERIC,
"");
1664 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1665 setlocale(LC_NUMERIC,
"C");
1670 const unsigned char *payload;
1672 y(get_buf, &payload, &length);
1674 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1676 setlocale(LC_NUMERIC,
"");
1683 DLOG(
"Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->
symbol, bind->
keycode);
1685 setlocale(LC_NUMERIC,
"C");
1695 if (modename == NULL) {
1706 const unsigned char *payload;
1708 y(get_buf, &payload, &length);
1710 ipc_send_event(
"binding", I3_IPC_EVENT_BINDING, (
const char *)payload);
1713 setlocale(LC_NUMERIC,
"");
1720 DLOG(
"ipc_confirm_restart(fd %d)\n", client->
fd);
1721 static const char *reply =
"[{\"success\":true}]";
1723 client, strlen(reply), I3_IPC_REPLY_TYPE_COMMAND,
1724 (
const uint8_t *)reply);