2 #define I3__FILE__ "ipc.c"
15 #include <sys/socket.h>
20 #include <yajl/yajl_gen.h>
21 #include <yajl/yajl_parse.h>
33 static
void set_nonblock(
int sockfd) {
34 int flags = fcntl(sockfd, F_GETFL, 0);
36 if (fcntl(sockfd, F_SETFL, flags) < 0)
37 err(-1,
"Could not set O_NONBLOCK");
45 void ipc_send_event(
const char *event, uint32_t message_type,
const char *payload) {
49 bool interested =
false;
50 for (
int i = 0; i < current->
num_events; i++) {
51 if (strcasecmp(current->
events[i], event) != 0)
59 ipc_send_message(current->
fd, strlen(payload), message_type, (
const uint8_t *)payload);
72 shutdown(current->
fd, SHUT_RDWR);
90 char *command =
scalloc(message_size + 1, 1);
91 strncpy(command, (
const char *)message, message_size);
92 LOG(
"IPC: received: *%s*\n", command);
93 yajl_gen gen = yajl_gen_alloc(NULL);
103 const unsigned char *reply;
105 yajl_gen_get_buf(gen, &reply, &length);
108 (
const uint8_t *)reply);
129 for (
int i = 0; i < 20; i++) {
132 case XCB_KEY_BUT_MASK_SHIFT:
135 case XCB_KEY_BUT_MASK_LOCK:
138 case XCB_KEY_BUT_MASK_CONTROL:
141 case XCB_KEY_BUT_MASK_MOD_1:
144 case XCB_KEY_BUT_MASK_MOD_2:
147 case XCB_KEY_BUT_MASK_MOD_3:
150 case XCB_KEY_BUT_MASK_MOD_4:
153 case XCB_KEY_BUT_MASK_MOD_5:
156 case XCB_KEY_BUT_MASK_BUTTON_1:
159 case XCB_KEY_BUT_MASK_BUTTON_2:
162 case XCB_KEY_BUT_MASK_BUTTON_3:
165 case XCB_KEY_BUT_MASK_BUTTON_4:
168 case XCB_KEY_BUT_MASK_BUTTON_5:
211 ystr(
"event_state_mask");
220 y(integer, (
long int)con);
233 case CT_FLOATING_CON:
234 ystr(
"floating_con");
243 DLOG(
"About to dump unknown container type=%d. This is a bug.\n", con->type);
259 ystr(
"scratchpad_state");
260 switch (con->scratchpad_state) {
261 case SCRATCHPAD_NONE:
264 case SCRATCHPAD_FRESH:
267 case SCRATCHPAD_CHANGED:
273 if (con->percent == 0.0)
276 y(
double, con->percent);
279 y(
bool, con->urgent);
297 switch (con->layout) {
299 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
322 ystr(
"workspace_layout");
323 switch (con->workspace_layout) {
334 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
339 ystr(
"last_split_layout");
340 switch (con->layout) {
350 switch (con->border_style) {
362 ystr(
"current_border_width");
363 y(integer, con->current_border_width);
366 dump_rect(gen,
"deco_rect", con->deco_rect);
367 dump_rect(gen,
"window_rect", con->window_rect);
368 dump_rect(gen,
"geometry", con->geometry);
371 if (con->window && con->window->name)
373 else if (con->name != NULL)
378 if (con->title_format != NULL) {
379 ystr(
"title_format");
380 ystr(con->title_format);
383 if (con->type == CT_WORKSPACE) {
385 y(integer, con->num);
390 y(integer, con->window->id);
394 if (con->window && !inplace_restart) {
398 ystr(
"window_properties");
401 #define DUMP_PROPERTY(key, prop_name) \
403 if (con->window->prop_name != NULL) { \
405 ystr(con->window->prop_name); \
413 if (con->window->name != NULL) {
418 ystr(
"transient_for");
419 if (con->window->transient_for == XCB_NONE)
422 y(integer, con->window->transient_for);
430 if (con->type != CT_DOCKAREA || !inplace_restart) {
437 ystr(
"floating_nodes");
439 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
447 y(integer, (
long int)node);
451 ystr(
"fullscreen_mode");
452 y(integer, con->fullscreen_mode);
455 y(
bool, con->sticky);
458 switch (con->floating) {
459 case FLOATING_AUTO_OFF:
462 case FLOATING_AUTO_ON:
465 case FLOATING_USER_OFF:
468 case FLOATING_USER_ON:
482 if (match->
dock != -1) {
484 y(integer, match->
dock);
485 ystr(
"insert_where");
489 #define DUMP_REGEX(re_name) \
491 if (match->re_name != NULL) { \
493 ystr(match->re_name->pattern); \
506 if (inplace_restart) {
507 if (con->window != NULL) {
510 y(integer, con->window->id);
511 ystr(
"restart_mode");
518 if (inplace_restart && con->window != NULL) {
520 y(integer, con->depth);
563 ystr(
"tray_outputs");
567 TAILQ_FOREACH(tray_output, &(config->tray_outputs), tray_outputs) {
574 #define YSTR_IF_SET(name) \
576 if (config->name) { \
578 ystr(config->name); \
582 ystr(
"tray_padding");
588 switch (config->
mode) {
601 ystr(
"hidden_state");
652 ystr(
"separator_symbol");
656 ystr(
"workspace_buttons");
659 ystr(
"strip_workspace_numbers");
662 ystr(
"binding_mode_indicator");
669 #define YSTR_IF_SET(name) \
671 if (config->colors.name) { \
673 ystr(config->colors.name); \
707 setlocale(LC_NUMERIC,
"C");
710 setlocale(LC_NUMERIC,
"");
712 const unsigned char *payload;
714 y(get_buf, &payload, &length);
737 assert(ws->
type == CT_WORKSPACE);
750 y(
bool, ws == focused_ws);
776 const unsigned char *payload;
778 y(get_buf, &payload, &length);
809 y(integer, output->
rect.
x);
811 y(integer, output->
rect.
y);
818 ystr(
"current_workspace");
830 const unsigned char *payload;
832 y(get_buf, &payload, &length);
857 const unsigned char *payload;
859 y(get_buf, &payload, &length);
874 y(integer, MAJOR_VERSION);
877 y(integer, MINOR_VERSION);
880 y(integer, PATCH_VERSION);
882 ystr(
"human_readable");
885 ystr(
"loaded_config_file_name");
890 const unsigned char *payload;
892 y(get_buf, &payload, &length);
907 if (message_size == 0) {
915 const unsigned char *payload;
917 y(get_buf, &payload, &length);
927 sasprintf(&bar_id,
"%.*s", message_size, message);
928 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
931 if (strcmp(current->
id, bar_id) != 0)
952 const unsigned char *payload;
954 y(get_buf, &payload, &length);
968 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
976 memcpy(client->
events[event], s, len);
978 DLOG(
"client is now subscribed to:\n");
998 if (current->
fd != fd)
1005 if (client == NULL) {
1006 ELOG(
"Could not find ipc_client data structure for fd %d\n", fd);
1011 static yajl_callbacks callbacks = {
1015 p =
yalloc(&callbacks, (
void *)client);
1016 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1017 if (stat != yajl_status_ok) {
1019 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1021 ELOG(
"YAJL parse error: %s\n", err);
1022 yajl_free_error(p, err);
1024 const char *reply =
"{\"success\":false}";
1025 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (
const uint8_t *)reply);
1030 const char *reply =
"{\"success\":true}";
1031 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (
const uint8_t *)reply);
1038 handle_get_workspaces,
1043 handle_get_bar_config,
1058 uint32_t message_type;
1059 uint32_t message_length;
1060 uint8_t *message = NULL;
1062 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
1066 if (ret == -1 && errno == EAGAIN) {
1078 if (current->
fd != w->fd)
1081 for (
int i = 0; i < current->
num_events; i++)
1082 free(current->
events[i]);
1091 ev_io_stop(EV_A_ w);
1095 DLOG(
"IPC: client disconnected\n");
1100 DLOG(
"Unhandled message type: %d\n", message_type);
1103 h(w->fd, message, 0, message_length, message_type);
1117 struct sockaddr_un peer;
1118 socklen_t len =
sizeof(
struct sockaddr_un);
1120 if ((client = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
1129 (void)fcntl(client, F_SETFD, FD_CLOEXEC);
1131 set_nonblock(client);
1133 struct ev_io *
package = scalloc(1, sizeof(struct ev_io));
1135 ev_io_start(EV_A_ package);
1137 DLOG(
"IPC: new client connected on fd %d\n", w->fd);
1156 DLOG(
"Creating IPC-socket at %s\n", resolved);
1157 char *copy =
sstrdup(resolved);
1158 const char *dir = dirname(copy);
1166 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1172 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1174 struct sockaddr_un addr;
1175 memset(&addr, 0,
sizeof(
struct sockaddr_un));
1176 addr.sun_family = AF_LOCAL;
1177 strncpy(addr.sun_path, resolved,
sizeof(addr.sun_path) - 1);
1178 if (bind(sockfd, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0) {
1184 set_nonblock(sockfd);
1186 if (listen(sockfd, 5) < 0) {
1201 setlocale(LC_NUMERIC,
"C");
1210 if (current == NULL)
1223 setlocale(LC_NUMERIC,
"");
1236 const unsigned char *payload;
1238 y(get_buf, &payload, &length);
1240 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1250 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1251 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1253 setlocale(LC_NUMERIC,
"C");
1266 const unsigned char *payload;
1268 y(get_buf, &payload, &length);
1270 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1272 setlocale(LC_NUMERIC,
"");
1279 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1280 setlocale(LC_NUMERIC,
"C");
1285 const unsigned char *payload;
1287 y(get_buf, &payload, &length);
1289 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1291 setlocale(LC_NUMERIC,
"");
1298 DLOG(
"Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->
symbol, bind->
keycode);
1300 setlocale(LC_NUMERIC,
"C");
1314 const unsigned char *payload;
1316 y(get_buf, &payload, &length);
1318 ipc_send_event(
"binding", I3_IPC_EVENT_BINDING, (
const char *)payload);
1321 setlocale(LC_NUMERIC,
"");
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...
#define IPC_HANDLER(name)
enum Barconfig::@10 modifier
Bar modifier (to show bar when in hide mode).
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
char * command
The command which is to be executed for this button.
char * symbol
Symbol the user specified in configfile, if any.
char ** outputs
Outputs on which this bar should show up on.
#define TAILQ_EMPTY(head)
Con * output_get_content(Con *output)
Returns the output container below the given output container.
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
enum Barconfig::@8 mode
Bar display mode (hide unless modifier is pressed or show in dock mode or always hide in invisible mo...
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
struct bindings_head * bindings
static void ipc_receive_message(EV_P_ struct ev_io *w, int revents)
#define TAILQ_FOREACH(var, head, field)
enum Barconfig::@9 hidden_state
struct barconfig_head barconfigs
void ipc_send_binding_event(const char *event_type, Binding *bind)
For the binding events, we send the serialized binding struct.
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...
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
bool strip_workspace_numbers
Strip workspace numbers? Configuration option is 'strip_workspace_numbers yes'.
static int add_subscription(void *extra, const unsigned char *s, ylength len)
#define DUMP_PROPERTY(key, prop_name)
char * command
Command, like in command mode.
static void dump_bar_bindings(yajl_gen gen, Barconfig *config)
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
bool hide_binding_mode_indicator
Hide mode button? Configuration option is 'binding_mode_indicator no' but we invert the bool for the ...
Con * con
Pointer to the Con which represents this output.
#define TAILQ_REMOVE(head, elm, field)
bool hide_workspace_buttons
Hide workspace buttons? Configuration option is 'workspace_buttons no' but we invert the bool to get ...
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
#define TAILQ_HEAD(name, type)
bool verbose
Enable verbose mode? Useful for debugging purposes.
static void dump_bar_config(yajl_gen gen, Barconfig *config)
uint32_t keycode
Keycode to bind.
void command_result_free(CommandResult *result)
Frees a CommandResult.
static void dump_binding(yajl_gen gen, Binding *bind)
Stores a rectangle, for example the size of a window, the child window etc.
Holds the status bar configuration (i3bar).
void ipc_shutdown(void)
Calls shutdown() on each socket and closes it.
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, in "container".
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
void(* handler_t)(int, uint8_t *, int, uint32_t, uint32_t)
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
#define DUMP_REGEX(re_name)
int input_code
The button to be used (e.g., 1 for "button1").
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
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...
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
enum Match::@17 insert_where
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
i3_event_state_mask_t event_state_mask
Bitmask which is applied against event->state for KeyPress and KeyRelease events to determine whether...
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...
#define yalloc(callbacks, client)
A 'Con' represents everything from the X11 root window down to a single X11 window.
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
struct outputs_head outputs
A struct that contains useful information about the result of a command as a whole (e...
#define TAILQ_FIRST(head)
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...
bool con_is_split(Con *con)
Returns true if a container should be considered split.
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
static void dump_rect(yajl_gen gen, const char *name, Rect r)
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_INSERT_TAIL(head, elm, field)
Rect rect
x, y, width, height
#define YSTR_IF_SET(name)
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
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...
int num_outputs
Number of outputs in the outputs array.
char * current_configpath
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
struct all_cons_head all_cons
int ipc_send_message(int sockfd, const uint32_t message_size, const uint32_t message_type, const uint8_t *payload)
Formats a message (payload) of the given size and type and sends it to i3 via the given socket file d...
An Output is a physical output on your graphics driver.
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
enum Barconfig::@11 position
Bar position (bottom by default).
const char * i3_version
Git commit identifier, from version.c.
char * name
Name of the output.
char * separator_symbol
A custom separator to use instead of a vertical line.
char * id
Automatically generated ID for this bar config.
char * current_socketpath
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...
#define TAILQ_HEAD_INITIALIZER(head)
yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old)
Generates a json workspace event.
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
static void dump_event_state_mask(yajl_gen gen, Binding *bind)