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);
87 char *command =
scalloc(message_size + 1);
88 strncpy(command, (
const char *)message, message_size);
89 LOG(
"IPC: received: *%s*\n", command);
90 yajl_gen gen = yajl_gen_alloc(NULL);
100 const unsigned char *reply;
102 yajl_gen_get_buf(gen, &reply, &length);
105 (
const uint8_t *)reply);
143 for (
int i = 0; i < 8; i++) {
144 if (bind->
mods & (1 << i)) {
146 case XCB_MOD_MASK_SHIFT:
149 case XCB_MOD_MASK_LOCK:
152 case XCB_MOD_MASK_CONTROL:
181 y(integer, (
long int)con);
194 case CT_FLOATING_CON:
195 ystr(
"floating_con");
204 DLOG(
"About to dump unknown container type=%d. This is a bug.\n", con->type);
220 ystr(
"scratchpad_state");
221 switch (con->scratchpad_state) {
222 case SCRATCHPAD_NONE:
225 case SCRATCHPAD_FRESH:
228 case SCRATCHPAD_CHANGED:
234 if (con->percent == 0.0)
237 y(
double, con->percent);
240 y(
bool, con->urgent);
242 if (con->mark != NULL) {
251 switch (con->layout) {
253 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
276 ystr(
"workspace_layout");
277 switch (con->workspace_layout) {
288 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
293 ystr(
"last_split_layout");
294 switch (con->layout) {
304 switch (con->border_style) {
316 ystr(
"current_border_width");
317 y(integer, con->current_border_width);
320 dump_rect(gen,
"deco_rect", con->deco_rect);
321 dump_rect(gen,
"window_rect", con->window_rect);
322 dump_rect(gen,
"geometry", con->geometry);
325 if (con->window && con->window->name)
327 else if (con->name != NULL)
332 if (con->type == CT_WORKSPACE) {
334 y(integer, con->num);
339 y(integer, con->window->id);
343 if (con->window && !inplace_restart) {
347 ystr(
"window_properties");
350 #define DUMP_PROPERTY(key, prop_name) \
352 if (con->window->prop_name != NULL) { \
354 ystr(con->window->prop_name); \
362 if (con->window->name != NULL) {
367 ystr(
"transient_for");
368 if (con->window->transient_for == XCB_NONE)
371 y(integer, con->window->transient_for);
379 if (con->type != CT_DOCKAREA || !inplace_restart) {
386 ystr(
"floating_nodes");
388 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
396 y(integer, (
long int)node);
400 ystr(
"fullscreen_mode");
401 y(integer, con->fullscreen_mode);
404 switch (con->floating) {
405 case FLOATING_AUTO_OFF:
408 case FLOATING_AUTO_ON:
411 case FLOATING_USER_OFF:
414 case FLOATING_USER_ON:
428 if (match->
dock != -1) {
430 y(integer, match->
dock);
431 ystr(
"insert_where");
435 #define DUMP_REGEX(re_name) \
437 if (match->re_name != NULL) { \
439 ystr(match->re_name->pattern); \
452 if (inplace_restart) {
453 if (con->window != NULL) {
456 y(integer, con->window->id);
457 ystr(
"restart_mode");
464 if (inplace_restart && con->window != NULL) {
466 y(integer, con->depth);
486 #define YSTR_IF_SET(name) \
488 if (config->name) { \
490 ystr(config->name); \
498 switch (config->
mode) {
511 ystr(
"hidden_state");
553 ystr(
"wheel_up_cmd");
558 ystr(
"wheel_down_cmd");
572 ystr(
"separator_symbol");
576 ystr(
"workspace_buttons");
579 ystr(
"strip_workspace_numbers");
582 ystr(
"binding_mode_indicator");
589 #define YSTR_IF_SET(name) \
591 if (config->colors.name) { \
593 ystr(config->colors.name); \
621 setlocale(LC_NUMERIC,
"C");
624 setlocale(LC_NUMERIC,
"");
626 const unsigned char *payload;
628 y(get_buf, &payload, &length);
651 assert(ws->
type == CT_WORKSPACE);
664 y(
bool, ws == focused_ws);
690 const unsigned char *payload;
692 y(get_buf, &payload, &length);
723 y(integer, output->
rect.
x);
725 y(integer, output->
rect.
y);
732 ystr(
"current_workspace");
744 const unsigned char *payload;
746 y(get_buf, &payload, &length);
763 if (con->
mark != NULL)
768 const unsigned char *payload;
770 y(get_buf, &payload, &length);
785 y(integer, MAJOR_VERSION);
788 y(integer, MINOR_VERSION);
791 y(integer, PATCH_VERSION);
793 ystr(
"human_readable");
798 const unsigned char *payload;
800 y(get_buf, &payload, &length);
815 if (message_size == 0) {
823 const unsigned char *payload;
825 y(get_buf, &payload, &length);
834 char *bar_id =
scalloc(message_size + 1);
835 strncpy(bar_id, (
const char *)message, message_size);
836 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
839 if (strcmp(current->
id, bar_id) != 0)
859 const unsigned char *payload;
861 y(get_buf, &payload, &length);
875 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
883 memcpy(client->
events[event], s, len);
885 DLOG(
"client is now subscribed to:\n");
905 if (current->
fd != fd)
912 if (client == NULL) {
913 ELOG(
"Could not find ipc_client data structure for fd %d\n", fd);
918 static yajl_callbacks callbacks = {
922 p =
yalloc(&callbacks, (
void *)client);
923 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
924 if (stat != yajl_status_ok) {
926 err = yajl_get_error(p,
true, (
const unsigned char *)message,
928 ELOG(
"YAJL parse error: %s\n", err);
929 yajl_free_error(p, err);
931 const char *reply =
"{\"success\":false}";
932 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (
const uint8_t *)reply);
937 const char *reply =
"{\"success\":true}";
938 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (
const uint8_t *)reply);
945 handle_get_workspaces,
950 handle_get_bar_config,
965 uint32_t message_type;
966 uint32_t message_length;
967 uint8_t *message = NULL;
969 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
973 if (ret == -1 && errno == EAGAIN) {
985 if (current->
fd != w->fd)
1001 DLOG(
"IPC: client disconnected\n");
1006 DLOG(
"Unhandled message type: %d\n", message_type);
1009 h(w->fd, message, 0, message_length, message_type);
1023 struct sockaddr_un peer;
1024 socklen_t len =
sizeof(
struct sockaddr_un);
1026 if ((client = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
1035 (void)fcntl(client, F_SETFD, FD_CLOEXEC);
1037 set_nonblock(client);
1039 struct ev_io *
package = scalloc(sizeof(struct ev_io));
1041 ev_io_start(EV_A_ package);
1043 DLOG(
"IPC: new client connected on fd %d\n", w->fd);
1062 DLOG(
"Creating IPC-socket at %s\n", resolved);
1063 char *copy =
sstrdup(resolved);
1064 const char *dir = dirname(copy);
1072 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1078 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1080 struct sockaddr_un addr;
1081 memset(&addr, 0,
sizeof(
struct sockaddr_un));
1082 addr.sun_family = AF_LOCAL;
1083 strncpy(addr.sun_path, resolved,
sizeof(addr.sun_path) - 1);
1084 if (bind(sockfd, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0) {
1090 set_nonblock(sockfd);
1092 if (listen(sockfd, 5) < 0) {
1107 setlocale(LC_NUMERIC,
"C");
1116 if (current == NULL)
1129 setlocale(LC_NUMERIC,
"");
1142 const unsigned char *payload;
1144 y(get_buf, &payload, &length);
1146 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1156 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1157 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1159 setlocale(LC_NUMERIC,
"C");
1172 const unsigned char *payload;
1174 y(get_buf, &payload, &length);
1176 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1178 setlocale(LC_NUMERIC,
"");
1185 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1186 setlocale(LC_NUMERIC,
"C");
1191 const unsigned char *payload;
1193 y(get_buf, &payload, &length);
1195 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1197 setlocale(LC_NUMERIC,
"");
1204 DLOG(
"Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->
symbol, bind->
keycode);
1206 setlocale(LC_NUMERIC,
"C");
1220 const unsigned char *payload;
1222 y(get_buf, &payload, &length);
1224 ipc_send_event(
"binding", I3_IPC_EVENT_BINDING, (
const char *)payload);
1227 setlocale(LC_NUMERIC,
"");
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
void ipc_shutdown(void)
Calls shutdown() on each socket and closes it.
char * name
Name of the output.
char * symbol
Symbol the user specified in configfile, if any.
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
#define TAILQ_REMOVE(head, elm, field)
bool hide_binding_mode_indicator
Hide mode button? Configuration option is 'binding_mode_indicator no' but we invert the bool for the ...
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
static void dump_bar_config(yajl_gen gen, Barconfig *config)
Stores a rectangle, for example the size of a window, the child window etc.
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
enum Barconfig::@5 mode
Bar display mode (hide unless modifier is pressed or show in dock mode or always hide in invisible mo...
enum Barconfig::@8 position
Bar position (bottom by default).
struct all_cons_head all_cons
Rect rect
x, y, width, height
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
bool verbose
Enable verbose mode? Useful for debugging purposes.
#define TAILQ_FIRST(head)
TAILQ_HEAD(ipc_client_head, ipc_client)
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
An Output is a physical output on your graphics driver.
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...
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
void command_result_free(CommandResult *result)
Frees a CommandResult.
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
static void ipc_receive_message(EV_P_ struct ev_io *w, int revents)
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...
static void dump_binding(yajl_gen gen, Binding *bind)
A struct that contains useful information about the result of a command as a whole (e...
char * command
Command, like in command mode.
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...
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
char * wheel_up_cmd
Command that should be run when mouse wheel up button is pressed over i3bar to override the default b...
struct outputs_head outputs
char * wheel_down_cmd
Command that should be run when mouse wheel down button is pressed over i3bar to override the default...
Con * con
Pointer to the Con which represents this output.
char * id
Automatically generated ID for this bar config.
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
enum Barconfig::@7 modifier
Bar modifier (to show bar when in hide mode).
static void dump_rect(yajl_gen gen, const char *name, Rect r)
#define TAILQ_EMPTY(head)
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...
#define yalloc(callbacks, client)
#define YSTR_IF_SET(name)
bool strip_workspace_numbers
Strip workspace numbers? Configuration option is 'strip_workspace_numbers yes'.
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
char * separator_symbol
A custom separator to use instead of a vertical line.
bool con_is_split(Con *con)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
static int add_subscription(void *extra, const unsigned char *s, ylength len)
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
A 'Con' represents everything from the X11 root window down to a single X11 window.
enum Barconfig::@6 hidden_state
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
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...
uint32_t keycode
Keycode to bind.
enum Match::@15 insert_where
bool mkdirp(const char *path)
Emulates mkdir -p (creates any missing folders)
#define TAILQ_INSERT_TAIL(head, elm, field)
uint32_t mods
Bitmask consisting of BIND_MOD_1, BIND_MODE_SWITCH, …
bool hide_workspace_buttons
Hide workspace buttons? Configuration option is 'workspace_buttons no' but we invert the bool to get ...
#define TAILQ_HEAD_INITIALIZER(head)
yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old)
Generates a json workspace event.
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
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...
struct barconfig_head barconfigs
char ** outputs
Outputs on which this bar should show up on.
Con * output_get_content(Con *output)
Returns the output container below the given output container.
#define TAILQ_FOREACH(var, head, field)
void(* handler_t)(int, uint8_t *, int, uint32_t, uint32_t)
#define DUMP_PROPERTY(key, prop_name)
Holds the status bar configuration (i3bar).
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".
#define DUMP_REGEX(re_name)
void ipc_send_binding_event(const char *event_type, Binding *bind)
For the binding events, we send the serialized binding struct.
int num_outputs
Number of outputs in the outputs array.
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.