15 #include <sys/socket.h>
20 #include <yajl/yajl_gen.h>
21 #include <yajl/yajl_parse.h>
35 int flags = fcntl(sockfd, F_GETFL, 0);
36 if (flags & O_NONBLOCK) {
40 if (fcntl(sockfd, F_SETFL, flags) < 0)
41 err(-1,
"Could not set O_NONBLOCK");
83 struct ev_timer *timeout =
scalloc(1,
sizeof(
struct ev_timer));
85 timeout->data = client;
87 ev_set_priority(timeout, EV_MINPRI);
89 }
else if (result > 0) {
113 const i3_ipc_header_t
header = {
114 .magic = {
'i',
'3',
'-',
'i',
'p',
'c'},
116 .type = message_type};
117 const size_t header_size =
sizeof(i3_ipc_header_t);
118 const size_t message_size = header_size + size;
132 if (client->
fd != exempt_fd) {
133 DLOG(
"Disconnecting client on fd %d\n", client->
fd);
148 for (
int i = 0; i < client->
num_events; i++) {
161 void ipc_send_event(
const char *event, uint32_t message_type,
const char *payload) {
164 for (
int i = 0; i < current->
num_events; i++) {
165 if (strcasecmp(current->
events[i], event) == 0) {
190 const unsigned char *payload;
193 y(get_buf, &payload, &length);
194 ipc_send_event(
"shutdown", I3_IPC_EVENT_SHUTDOWN, (
const char *)payload);
212 if (current->
fd != exempt_fd) {
213 shutdown(current->
fd, SHUT_RDWR);
227 char *command =
sstrndup((
const char *)message, message_size);
228 LOG(
"IPC: received: *%s*\n", command);
229 yajl_gen gen = yajl_gen_alloc(NULL);
239 const unsigned char *reply;
241 yajl_gen_get_buf(gen, &reply, &length);
244 (
const uint8_t *)reply);
272 y(integer, gaps.
top);
275 y(integer, gaps.
top);
281 y(integer, gaps.
left);
287 for (
int i = 0; i < 20; i++) {
290 case XCB_KEY_BUT_MASK_SHIFT:
293 case XCB_KEY_BUT_MASK_LOCK:
296 case XCB_KEY_BUT_MASK_CONTROL:
299 case XCB_KEY_BUT_MASK_MOD_1:
302 case XCB_KEY_BUT_MASK_MOD_2:
305 case XCB_KEY_BUT_MASK_MOD_3:
308 case XCB_KEY_BUT_MASK_MOD_4:
311 case XCB_KEY_BUT_MASK_MOD_5:
314 case XCB_KEY_BUT_MASK_BUTTON_1:
317 case XCB_KEY_BUT_MASK_BUTTON_2:
320 case XCB_KEY_BUT_MASK_BUTTON_3:
323 case XCB_KEY_BUT_MASK_BUTTON_4:
326 case XCB_KEY_BUT_MASK_BUTTON_5:
369 ystr(
"event_state_mask");
378 y(integer, (uintptr_t)con);
391 case CT_FLOATING_CON:
392 ystr(
"floating_con");
413 ystr(
"scratchpad_state");
414 switch (con->scratchpad_state) {
415 case SCRATCHPAD_NONE:
418 case SCRATCHPAD_FRESH:
421 case SCRATCHPAD_CHANGED:
427 if (con->percent == 0.0)
430 y(
double, con->percent);
433 y(
bool, con->urgent);
450 if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
456 switch (con->layout) {
458 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
481 ystr(
"workspace_layout");
482 switch (con->workspace_layout) {
493 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
498 ystr(
"last_split_layout");
499 switch (con->layout) {
509 switch (con->border_style) {
521 ystr(
"current_border_width");
522 y(integer, con->current_border_width);
525 dump_rect(gen,
"deco_rect", con->deco_rect);
526 dump_rect(gen,
"window_rect", con->window_rect);
527 dump_rect(gen,
"geometry", con->geometry);
530 if (con->window && con->window->name)
532 else if (con->name != NULL)
537 if (con->title_format != NULL) {
538 ystr(
"title_format");
539 ystr(con->title_format);
542 if (con->type == CT_WORKSPACE) {
544 y(integer, con->num);
551 y(integer, con->window->id);
555 if (con->window && !inplace_restart) {
559 ystr(
"window_properties");
562 #define DUMP_PROPERTY(key, prop_name) \
564 if (con->window->prop_name != NULL) { \
566 ystr(con->window->prop_name); \
574 if (con->window->name != NULL) {
579 ystr(
"transient_for");
580 if (con->window->transient_for == XCB_NONE)
583 y(integer, con->window->transient_for);
591 if (con->type != CT_DOCKAREA || !inplace_restart) {
598 ystr(
"floating_nodes");
600 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
608 y(integer, (uintptr_t)node);
612 ystr(
"fullscreen_mode");
613 y(integer, con->fullscreen_mode);
616 y(
bool, con->sticky);
619 switch (con->floating) {
620 case FLOATING_AUTO_OFF:
623 case FLOATING_AUTO_ON:
626 case FLOATING_USER_OFF:
629 case FLOATING_USER_ON:
643 if (match->
dock != M_DONTCHECK) {
645 y(integer, match->
dock);
646 ystr(
"insert_where");
650 #define DUMP_REGEX(re_name) \
652 if (match->re_name != NULL) { \
654 ystr(match->re_name->pattern); \
667 if (inplace_restart) {
668 if (con->window != NULL) {
671 y(integer, con->window->id);
672 ystr(
"restart_mode");
679 if (inplace_restart && con->window != NULL) {
681 y(integer, con->depth);
685 ystr(
"previous_workspace_name");
708 y(
bool, current->
release == B_UPON_KEYRELEASE);
718 if (strcasecmp(name,
"primary") == 0) {
731 if (
config->num_outputs > 0) {
734 for (
int c = 0; c <
config->num_outputs; c++) {
745 ystr(
"tray_outputs");
756 #define YSTR_IF_SET(name) \
758 if (config->name) { \
760 ystr(config->name); \
764 ystr(
"tray_padding");
765 y(integer,
config->tray_padding);
783 ystr(
"hidden_state");
784 switch (
config->hidden_state) {
800 if (
config->position == P_BOTTOM)
810 y(integer,
config->bar_height);
813 if (
config->separator_symbol) {
814 ystr(
"separator_symbol");
818 ystr(
"workspace_buttons");
819 y(
bool, !
config->hide_workspace_buttons);
821 ystr(
"strip_workspace_numbers");
822 y(
bool,
config->strip_workspace_numbers);
824 ystr(
"strip_workspace_name");
825 y(
bool,
config->strip_workspace_name);
827 ystr(
"binding_mode_indicator");
828 y(
bool, !
config->hide_binding_mode_indicator);
834 #define YSTR_IF_SET(name) \
836 if (config->colors.name) { \
838 ystr(config->colors.name); \
872 setlocale(LC_NUMERIC,
"C");
875 setlocale(LC_NUMERIC,
"");
877 const unsigned char *payload;
879 y(get_buf, &payload, &length);
902 assert(ws->
type == CT_WORKSPACE);
915 y(
bool, ws == focused_ws);
941 const unsigned char *payload;
943 y(get_buf, &payload, &length);
978 y(integer,
output->rect.width);
980 y(integer,
output->rect.height);
983 ystr(
"current_workspace");
995 const unsigned char *payload;
997 y(get_buf, &payload, &length);
1022 const unsigned char *payload;
1024 y(get_buf, &payload, &length);
1039 y(integer, MAJOR_VERSION);
1042 y(integer, MINOR_VERSION);
1045 y(integer, PATCH_VERSION);
1047 ystr(
"human_readable");
1050 ystr(
"loaded_config_file_name");
1055 const unsigned char *payload;
1057 y(get_buf, &payload, &length);
1072 if (message_size == 0) {
1080 const unsigned char *payload;
1082 y(get_buf, &payload, &length);
1091 char *bar_id = NULL;
1092 sasprintf(&bar_id,
"%.*s", message_size, message);
1093 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
1096 if (strcmp(current->
id, bar_id) != 0)
1117 const unsigned char *payload;
1119 y(get_buf, &payload, &length);
1139 const unsigned char *payload;
1141 y(get_buf, &payload, &length);
1155 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
1163 memcpy(client->
events[event], s, len);
1165 DLOG(
"client is now subscribed to:\n");
1166 for (
int i = 0; i < client->
num_events; i++) {
1184 static yajl_callbacks callbacks = {
1188 p =
yalloc(&callbacks, (
void *)client);
1189 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1190 if (stat != yajl_status_ok) {
1192 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1194 ELOG(
"YAJL parse error: %s\n", err);
1195 yajl_free_error(p, err);
1197 const char *reply =
"{\"success\":false}";
1203 const char *reply =
"{\"success\":true}";
1206 if (client->first_tick_sent) {
1210 bool is_tick =
false;
1211 for (
int i = 0; i < client->num_events; i++) {
1212 if (strcmp(client->events[i],
"tick") == 0) {
1221 client->first_tick_sent =
true;
1222 const char *payload =
"{\"first\":true,\"payload\":\"\"}";
1239 const unsigned char *payload;
1241 y(get_buf, &payload, &length);
1260 yajl_gen_string(gen, (
unsigned char *)message, message_size);
1264 const unsigned char *payload;
1266 y(get_buf, &payload, &length);
1268 ipc_send_event(
"tick", I3_IPC_EVENT_TICK, (
const char *)payload);
1271 const char *reply =
"{\"success\":true}";
1273 DLOG(
"Sent tick event\n");
1286 memcpy(
state->last_key, val, len);
1292 if (strcasecmp(
state->last_key,
"rnd") == 0) {
1294 }
else if (strcasecmp(
state->last_key,
"window") == 0) {
1295 state->window = (xcb_window_t)val;
1305 static yajl_callbacks callbacks = {
1313 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1315 if (stat != yajl_status_ok) {
1317 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1319 ELOG(
"YAJL parse error: %s\n", err);
1320 yajl_free_error(p, err);
1322 const char *reply =
"{\"success\":false}";
1329 DLOG(
"received IPC sync request (rnd = %d, window = 0x%08x)\n",
state.rnd,
state.window);
1331 const char *reply =
"{\"success\":true}";
1339 handle_get_workspaces,
1344 handle_get_bar_config,
1346 handle_get_binding_modes,
1363 uint32_t message_type;
1364 uint32_t message_length;
1365 uint8_t *message = NULL;
1367 assert(client->
fd == w->fd);
1369 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
1373 if (ret == -1 && errno == EAGAIN) {
1386 DLOG(
"Unhandled message type: %d\n", message_type);
1389 h(client, message, 0, message_length, message_type);
1400 char *cmdline = NULL;
1401 #
if defined(__linux__) && defined(SO_PEERCRED)
1402 struct ucred peercred;
1403 socklen_t so_len =
sizeof(peercred);
1404 if (getsockopt(client->
fd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) != 0) {
1408 sasprintf(&exepath,
"/proc/%d/cmdline", peercred.pid);
1410 int fd = open(exepath, O_RDONLY);
1415 char buf[512] = {
'\0'};
1416 const ssize_t n = read(fd, buf,
sizeof(buf));
1421 for (
char *walk = buf; walk < buf + n - 1; walk++) {
1422 if (*walk ==
'\0') {
1429 ELOG(
"client %p with pid %d and cmdline '%s' on fd %d timed out, killing\n", client, peercred.pid, cmdline, client->
fd);
1435 ELOG(
"client %p on fd %d timed out, killing\n", client, client->
fd);
1442 DLOG(
"fd %d writeable\n", w->fd);
1447 assert(client->
timeout != NULL);
1459 struct sockaddr_un peer;
1460 socklen_t len =
sizeof(
struct sockaddr_un);
1462 if ((fd = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
1463 if (errno != EINTR) {
1470 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1498 DLOG(
"IPC: new client connected on fd %d\n", fd);
1514 DLOG(
"Creating IPC-socket at %s\n", resolved);
1515 char *copy =
sstrdup(resolved);
1516 const char *dir = dirname(copy);
1524 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1530 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1532 struct sockaddr_un addr;
1533 memset(&addr, 0,
sizeof(
struct sockaddr_un));
1534 addr.sun_family = AF_LOCAL;
1535 strncpy(addr.sun_path, resolved,
sizeof(addr.sun_path) - 1);
1536 if (bind(sockfd, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0) {
1544 if (listen(sockfd, 5) < 0) {
1559 setlocale(LC_NUMERIC,
"C");
1568 if (current == NULL)
1581 setlocale(LC_NUMERIC,
"");
1594 const unsigned char *payload;
1596 y(get_buf, &payload, &length);
1598 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1608 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1609 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1611 setlocale(LC_NUMERIC,
"C");
1624 const unsigned char *payload;
1626 y(get_buf, &payload, &length);
1628 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1630 setlocale(LC_NUMERIC,
"");
1637 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1638 setlocale(LC_NUMERIC,
"C");
1643 const unsigned char *payload;
1645 y(get_buf, &payload, &length);
1647 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1649 setlocale(LC_NUMERIC,
"");
1656 DLOG(
"Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->
symbol, bind->
keycode);
1658 setlocale(LC_NUMERIC,
"C");
1672 const unsigned char *payload;
1674 y(get_buf, &payload, &length);
1676 ipc_send_event(
"binding", I3_IPC_EVENT_BINDING, (
const char *)payload);
1679 setlocale(LC_NUMERIC,
"");
1686 DLOG(
"ipc_confirm_restart(fd %d)\n", client->
fd);
1687 static const char *reply =
"[{\"success\":true}]";
1689 client, strlen(reply), I3_IPC_REPLY_TYPE_COMMAND,
1690 (
const uint8_t *)reply);