19 #include <sys/socket.h>
23 #include <yajl/yajl_gen.h>
24 #include <yajl/yajl_parse.h>
36 static void set_nonblock(
int sockfd) {
37 int flags = fcntl(sockfd, F_GETFL, 0);
38 if (flags & O_NONBLOCK) {
42 if (fcntl(sockfd, F_SETFL, flags) < 0)
43 err(-1,
"Could not set O_NONBLOCK");
85 struct ev_timer *timeout =
scalloc(1,
sizeof(
struct ev_timer));
87 timeout->data = client;
89 ev_set_priority(timeout, EV_MINPRI);
91 }
else if (result > 0) {
115 const i3_ipc_header_t
header = {
116 .magic = {
'i',
'3',
'-',
'i',
'p',
'c'},
118 .type = message_type};
119 const size_t header_size =
sizeof(i3_ipc_header_t);
120 const size_t message_size = header_size + size;
134 if (client->
fd != exempt_fd) {
135 DLOG(
"Disconnecting client on fd %d\n", client->
fd);
150 for (
int i = 0; i < client->
num_events; i++) {
163 void ipc_send_event(
const char *event, uint32_t message_type,
const char *payload) {
166 for (
int i = 0; i < current->
num_events; i++) {
167 if (strcasecmp(current->
events[i], event) == 0) {
192 const unsigned char *payload;
195 y(get_buf, &payload, &length);
196 ipc_send_event(
"shutdown", I3_IPC_EVENT_SHUTDOWN, (
const char *)payload);
214 if (current->
fd != exempt_fd) {
215 shutdown(current->
fd, SHUT_RDWR);
228 char *command =
sstrndup((
const char *)message, message_size);
229 LOG(
"IPC: received: *%.4000s*\n", command);
230 yajl_gen gen = yajl_gen_alloc(NULL);
240 const unsigned char *reply;
242 yajl_gen_get_buf(gen, &reply, &length);
245 (
const uint8_t *)reply);
266 for (
int i = 0; i < 20; i++) {
269 case XCB_KEY_BUT_MASK_SHIFT:
272 case XCB_KEY_BUT_MASK_LOCK:
275 case XCB_KEY_BUT_MASK_CONTROL:
278 case XCB_KEY_BUT_MASK_MOD_1:
281 case XCB_KEY_BUT_MASK_MOD_2:
284 case XCB_KEY_BUT_MASK_MOD_3:
287 case XCB_KEY_BUT_MASK_MOD_4:
290 case XCB_KEY_BUT_MASK_MOD_5:
293 case XCB_KEY_BUT_MASK_BUTTON_1:
296 case XCB_KEY_BUT_MASK_BUTTON_2:
299 case XCB_KEY_BUT_MASK_BUTTON_3:
302 case XCB_KEY_BUT_MASK_BUTTON_4:
305 case XCB_KEY_BUT_MASK_BUTTON_5:
348 ystr(
"event_state_mask");
357 y(integer, (uintptr_t)con);
370 case CT_FLOATING_CON:
371 ystr(
"floating_con");
392 ystr(
"scratchpad_state");
394 case SCRATCHPAD_NONE:
397 case SCRATCHPAD_FRESH:
400 case SCRATCHPAD_CHANGED:
425 if (con->
type != CT_ROOT && con->
type != CT_OUTPUT) {
433 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
456 ystr(
"workspace_layout");
468 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->
workspace_layout);
473 ystr(
"last_split_layout");
496 ystr(
"current_border_width");
507 else if (con->
name != NULL)
513 ystr(
"title_format");
517 if (con->
type == CT_WORKSPACE) {
519 y(integer, con->
num);
545 ystr(
"dropdown_menu");
551 ystr(
"notification");
558 if (con->
window && !inplace_restart) {
562 ystr(
"window_properties");
565 #define DUMP_PROPERTY(key, prop_name) \
567 if (con->window->prop_name != NULL) { \
569 ystr(con->window->prop_name); \
582 ystr(
"transient_for");
594 if (con->
type != CT_DOCKAREA || !inplace_restart) {
601 ystr(
"floating_nodes");
603 TAILQ_FOREACH (node, &(con->floating_head), floating_windows) {
611 y(integer, (uintptr_t)node);
615 ystr(
"fullscreen_mode");
623 case FLOATING_AUTO_OFF:
626 case FLOATING_AUTO_ON:
629 case FLOATING_USER_OFF:
632 case FLOATING_USER_ON:
646 if (match->
dock != M_DONTCHECK) {
648 y(integer, match->
dock);
649 ystr(
"insert_where");
653 #define DUMP_REGEX(re_name) \
655 if (match->re_name != NULL) { \
657 ystr(match->re_name->pattern); \
670 if (inplace_restart) {
671 if (con->
window != NULL) {
675 ystr(
"restart_mode");
682 if (inplace_restart && con->
window != NULL) {
688 ystr(
"previous_workspace_name");
711 y(
bool, current->
release == B_UPON_KEYRELEASE);
721 if (strcasecmp(name,
"primary") == 0) {
734 if (
config->num_outputs > 0) {
737 for (
int c = 0; c <
config->num_outputs; c++) {
748 ystr(
"tray_outputs");
759 #define YSTR_IF_SET(name) \
761 if (config->name) { \
763 ystr(config->name); \
767 ystr(
"tray_padding");
768 y(integer,
config->tray_padding);
786 ystr(
"hidden_state");
787 switch (
config->hidden_state) {
803 if (
config->position == P_BOTTOM)
811 if (
config->separator_symbol) {
812 ystr(
"separator_symbol");
816 ystr(
"workspace_buttons");
817 y(
bool, !
config->hide_workspace_buttons);
819 ystr(
"workspace_min_width");
820 y(integer,
config->workspace_min_width);
822 ystr(
"strip_workspace_numbers");
823 y(
bool,
config->strip_workspace_numbers);
825 ystr(
"strip_workspace_name");
826 y(
bool,
config->strip_workspace_name);
828 ystr(
"binding_mode_indicator");
829 y(
bool, !
config->hide_binding_mode_indicator);
835 #define YSTR_IF_SET(name) \
837 if (config->colors.name) { \
839 ystr(config->colors.name); \
873 setlocale(LC_NUMERIC,
"C");
876 setlocale(LC_NUMERIC,
"");
878 const unsigned char *payload;
880 y(get_buf, &payload, &length);
903 assert(ws->
type == CT_WORKSPACE);
907 y(integer, (uintptr_t)ws);
919 y(
bool, ws == focused_ws);
924 y(integer, ws->rect.x);
926 y(integer, ws->rect.y);
928 y(integer, ws->rect.width);
930 y(integer, ws->rect.height);
945 const unsigned char *payload;
947 y(get_buf, &payload, &length);
982 y(integer,
output->rect.width);
984 y(integer,
output->rect.height);
987 ystr(
"current_workspace");
999 const unsigned char *payload;
1001 y(get_buf, &payload, &length);
1026 const unsigned char *payload;
1028 y(get_buf, &payload, &length);
1043 y(integer, MAJOR_VERSION);
1046 y(integer, MINOR_VERSION);
1049 y(integer, PATCH_VERSION);
1051 ystr(
"human_readable");
1054 ystr(
"loaded_config_file_name");
1059 const unsigned char *payload;
1061 y(get_buf, &payload, &length);
1076 if (message_size == 0) {
1084 const unsigned char *payload;
1086 y(get_buf, &payload, &length);
1095 char *bar_id = NULL;
1096 sasprintf(&bar_id,
"%.*s", message_size, message);
1097 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
1100 if (strcmp(current->
id, bar_id) != 0)
1121 const unsigned char *payload;
1123 y(get_buf, &payload, &length);
1143 const unsigned char *payload;
1145 y(get_buf, &payload, &length);
1159 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
1167 memcpy(client->
events[event], s, len);
1169 DLOG(
"client is now subscribed to:\n");
1170 for (
int i = 0; i < client->
num_events; i++) {
1188 static yajl_callbacks callbacks = {
1192 p =
yalloc(&callbacks, (
void *)client);
1193 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
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}";
1207 const char *reply =
"{\"success\":true}";
1210 if (client->first_tick_sent) {
1214 bool is_tick =
false;
1215 for (
int i = 0; i < client->num_events; i++) {
1216 if (strcmp(client->events[i],
"tick") == 0) {
1225 client->first_tick_sent =
true;
1226 const char *payload =
"{\"first\":true,\"payload\":\"\"}";
1243 const unsigned char *payload;
1245 y(get_buf, &payload, &length);
1264 yajl_gen_string(gen, (
unsigned char *)message, message_size);
1268 const unsigned char *payload;
1270 y(get_buf, &payload, &length);
1272 ipc_send_event(
"tick", I3_IPC_EVENT_TICK, (
const char *)payload);
1275 const char *reply =
"{\"success\":true}";
1277 DLOG(
"Sent tick event\n");
1290 memcpy(
state->last_key, val, len);
1296 if (strcasecmp(
state->last_key,
"rnd") == 0) {
1298 }
else if (strcasecmp(
state->last_key,
"window") == 0) {
1299 state->window = (xcb_window_t)val;
1309 static yajl_callbacks callbacks = {
1317 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1319 if (stat != yajl_status_ok) {
1321 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1323 ELOG(
"YAJL parse error: %s\n", err);
1324 yajl_free_error(p, err);
1326 const char *reply =
"{\"success\":false}";
1333 DLOG(
"received IPC sync request (rnd = %d, window = 0x%08x)\n",
state.rnd,
state.window);
1335 const char *reply =
"{\"success\":true}";
1349 const unsigned char *payload;
1351 y(get_buf, &payload, &length);
1361 handle_get_workspaces,
1366 handle_get_bar_config,
1368 handle_get_binding_modes,
1372 handle_get_binding_state,
1386 uint32_t message_type;
1387 uint32_t message_length;
1388 uint8_t *message = NULL;
1390 assert(client->
fd == w->fd);
1392 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
1396 if (ret == -1 && errno == EAGAIN) {
1409 DLOG(
"Unhandled message type: %d\n", message_type);
1412 h(client, message, 0, message_length, message_type);
1423 char *cmdline = NULL;
1424 #
if defined(__linux__) && defined(SO_PEERCRED)
1425 struct ucred peercred;
1426 socklen_t so_len =
sizeof(peercred);
1427 if (getsockopt(client->
fd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) != 0) {
1431 sasprintf(&exepath,
"/proc/%d/cmdline", peercred.pid);
1433 int fd = open(exepath, O_RDONLY);
1438 char buf[512] = {
'\0'};
1439 const ssize_t n = read(fd, buf,
sizeof(buf));
1444 for (
char *walk = buf; walk < buf + n - 1; walk++) {
1445 if (*walk ==
'\0') {
1452 ELOG(
"client %p with pid %d and cmdline '%s' on fd %d timed out, killing\n", client, peercred.pid, cmdline, client->
fd);
1458 ELOG(
"client %p on fd %d timed out, killing\n", client, client->
fd);
1465 DLOG(
"fd %d writeable\n", w->fd);
1470 assert(client->
timeout != NULL);
1482 struct sockaddr_un peer;
1483 socklen_t len =
sizeof(
struct sockaddr_un);
1485 if ((fd = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
1486 if (errno != EINTR) {
1493 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1521 DLOG(
"IPC: new client connected on fd %d\n", fd);
1537 DLOG(
"Creating IPC-socket at %s\n", resolved);
1538 char *copy =
sstrdup(resolved);
1539 const char *dir = dirname(copy);
1547 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1553 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1555 struct sockaddr_un addr;
1556 memset(&addr, 0,
sizeof(
struct sockaddr_un));
1557 addr.sun_family = AF_LOCAL;
1558 strncpy(addr.sun_path, resolved,
sizeof(addr.sun_path) - 1);
1559 if (bind(sockfd, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0) {
1565 set_nonblock(sockfd);
1567 if (listen(sockfd, 5) < 0) {
1582 setlocale(LC_NUMERIC,
"C");
1591 if (current == NULL)
1604 setlocale(LC_NUMERIC,
"");
1617 const unsigned char *payload;
1619 y(get_buf, &payload, &length);
1621 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1631 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1632 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1634 setlocale(LC_NUMERIC,
"C");
1647 const unsigned char *payload;
1649 y(get_buf, &payload, &length);
1651 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1653 setlocale(LC_NUMERIC,
"");
1660 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1661 setlocale(LC_NUMERIC,
"C");
1666 const unsigned char *payload;
1668 y(get_buf, &payload, &length);
1670 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1672 setlocale(LC_NUMERIC,
"");
1679 DLOG(
"Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->
symbol, bind->
keycode);
1681 setlocale(LC_NUMERIC,
"C");
1695 const unsigned char *payload;
1697 y(get_buf, &payload, &length);
1699 ipc_send_event(
"binding", I3_IPC_EVENT_BINDING, (
const char *)payload);
1702 setlocale(LC_NUMERIC,
"");
1709 DLOG(
"ipc_confirm_restart(fd %d)\n", client->
fd);
1710 static const char *reply =
"[{\"success\":true}]";
1712 client, strlen(reply), I3_IPC_REPLY_TYPE_COMMAND,
1713 (
const uint8_t *)reply);
CommandResult * parse_command(const char *input, yajl_gen gen, ipc_client *client)
Parses and executes the given command.
void command_result_free(CommandResult *result)
Frees a CommandResult.
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
bool con_is_split(Con *con)
Returns true if a container should be considered split.
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
struct barconfig_head barconfigs
char * current_configpath
static void free_ipc_client(ipc_client *client, int exempt_fd)
static int _sync_json_int(void *extra, long long val)
void ipc_set_kill_timeout(ev_tstamp new)
Set the maximum duration that we allow for a connection with an unwriteable socket.
static void dump_event_state_mask(yajl_gen gen, Binding *bind)
int ipc_create_socket(const char *filename)
Creates the UNIX domain socket at the given path, sets it to non-blocking mode, bind()s and listen()s...
static char * canonicalize_output_name(char *name)
static void ipc_send_shutdown_event(shutdown_reason_t reason)
static void dump_bar_config(yajl_gen gen, Barconfig *config)
#define DUMP_REGEX(re_name)
static void dump_rect(yajl_gen gen, const char *name, Rect r)
static int _sync_json_key(void *extra, const unsigned char *val, size_t len)
static void ipc_client_timeout(EV_P_ ev_timer *w, int revents)
static void dump_binding(yajl_gen gen, Binding *bind)
void ipc_confirm_restart(ipc_client *client)
Sends a restart reply to the IPC client on the specified fd.
TAILQ_HEAD(ipc_client_head, ipc_client)
static void ipc_socket_writeable_cb(EV_P_ struct ev_io *w, int revents)
ipc_client * ipc_new_client_on_fd(EV_P_ int fd)
ipc_new_client_on_fd() only sets up the event handler for activity on the new connection and inserts ...
char * current_socketpath
static void dump_bar_bindings(yajl_gen gen, Barconfig *config)
static void ipc_receive_message(EV_P_ struct ev_io *w, int revents)
void ipc_shutdown(shutdown_reason_t reason, int exempt_fd)
Calls shutdown() on each socket and closes it.
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
static void ipc_send_client_message(ipc_client *client, size_t size, const uint32_t message_type, const uint8_t *payload)
#define YSTR_IF_SET(name)
void ipc_send_workspace_event(const char *change, Con *current, Con *old)
For the workspace events we send, along with the usual "change" field, also the workspace container i...
static ev_tstamp kill_timeout
void ipc_send_binding_event(const char *event_type, Binding *bind)
For the binding events, we send the serialized binding struct.
void ipc_new_client(EV_P_ struct ev_io *w, int revents)
Handler for activity on the listening socket, meaning that a new client has just connected and we sho...
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
void ipc_send_event(const char *event, uint32_t message_type, const char *payload)
Sends the specified event to all IPC clients which are currently connected and subscribed to this kin...
yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old)
Generates a json workspace event.
static int add_subscription(void *extra, const unsigned char *s, ylength len)
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container,...
static void ipc_push_pending(ipc_client *client)
#define DUMP_PROPERTY(key, prop_name)
struct pending_marks * marks
static i3_shmlog_header * header
const char * current_binding_mode
struct ev_loop * main_loop
struct bindings_head * bindings
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Output * get_output_by_name(const char *name, const bool require_active)
Returns the output with the given name or NULL.
struct outputs_head outputs
void sync_respond(xcb_window_t window, uint32_t rnd)
struct all_cons_head all_cons
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
const char * i3_version
Git commit identifier, from version.c.
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
char * previous_workspace_name
Stores a copy of the name of the last used workspace for the workspace back-and-forth switching.
void(* handler_t)(ipc_client *, uint8_t *, int, uint32_t, uint32_t)
shutdown_reason_t
Calls to ipc_shutdown() should provide a reason for the shutdown.
@ SHUTDOWN_REASON_RESTART
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
ssize_t writeall_nonblock(int fd, const void *buf, size_t count)
Like writeall, but instead of retrying upon EAGAIN (returned when a write would block),...
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
int ipc_recv_message(int sockfd, uint32_t *message_type, uint32_t *reply_length, uint8_t **reply)
Reads a message from the given socket file descriptor and stores its length (reply_length) as well as...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
char * sstrndup(const char *str, size_t size)
Safe-wrapper around strndup which exits if strndup returns NULL (meaning that there is no more memory...
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define SLIST_FOREACH(var, head, field)
#define TAILQ_FOREACH(var, head, field)
#define TAILQ_INSERT_TAIL(head, elm, field)
#define TAILQ_FIRST(head)
#define TAILQ_REMOVE(head, elm, field)
#define TAILQ_HEAD_INITIALIZER(head)
#define TAILQ_EMPTY(head)
#define yalloc(callbacks, client)
A struct that contains useful information about the result of a command as a whole (e....
The configuration file can contain multiple sets of bindings.
Holds the status bar configuration (i3bar).
char * id
Automatically generated ID for this bar config.
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
bool release
If true, the command will be executed after the button is released.
int input_code
The button to be used (e.g., 1 for "button1").
char * command
The command which is to be executed for this button.
Stores a rectangle, for example the size of a window, the child window etc.
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
char * command
Command, like in command mode.
uint32_t keycode
Keycode to bind.
char * symbol
Symbol the user specified in configfile, if any.
i3_event_state_mask_t event_state_mask
Bitmask which is applied against event->state for KeyPress and KeyRelease events to determine whether...
An Output is a physical output on your graphics driver.
i3String * name
The name of the window.
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
xcb_window_t transient_for
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
enum Match::@17 insert_where
A 'Con' represents everything from the X11 root window down to a single X11 window.
layout_t workspace_layout
enum Con::@21 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
enum Con::@22 scratchpad_state
char * title_format
The format with which the window's name should be displayed.
border_style_t border_style
struct Rect geometry
the geometry this window requested when getting mapped
fullscreen_mode_t fullscreen_mode
struct ev_io * read_callback
struct ev_timer * timeout
struct ev_io * write_callback