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");
44 static bool mkdirp(
const char *path) {
45 if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
47 if (errno != ENOENT) {
48 ELOG(
"mkdir(%s) failed: %s\n", path, strerror(errno));
53 while (copy[strlen(copy) - 1] ==
'/')
54 copy[strlen(copy) - 1] =
'\0';
56 char *sep = strrchr(copy,
'/');
75 void ipc_send_event(
const char *event, uint32_t message_type,
const char *payload) {
79 bool interested =
false;
80 for (
int i = 0; i < current->
num_events; i++) {
81 if (strcasecmp(current->
events[i], event) != 0)
89 ipc_send_message(current->
fd, strlen(payload), message_type, (
const uint8_t *)payload);
102 shutdown(current->
fd, SHUT_RDWR);
117 char *command =
scalloc(message_size + 1);
118 strncpy(command, (
const char *)message, message_size);
119 LOG(
"IPC: received: *%s*\n", command);
120 yajl_gen gen = yajl_gen_alloc(NULL);
130 const unsigned char *reply;
132 yajl_gen_get_buf(gen, &reply, &length);
135 (
const uint8_t *)reply);
157 y(integer, (
long int)con);
170 case CT_FLOATING_CON:
171 ystr(
"floating_con");
180 DLOG(
"About to dump unknown container type=%d. This is a bug.\n", con->type);
196 ystr(
"scratchpad_state");
197 switch (con->scratchpad_state) {
198 case SCRATCHPAD_NONE:
201 case SCRATCHPAD_FRESH:
204 case SCRATCHPAD_CHANGED:
210 if (con->percent == 0.0)
213 y(
double, con->percent);
216 y(
bool, con->urgent);
218 if (con->mark != NULL) {
227 switch (con->layout) {
229 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
252 ystr(
"workspace_layout");
253 switch (con->workspace_layout) {
264 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
269 ystr(
"last_split_layout");
270 switch (con->layout) {
280 switch (con->border_style) {
292 ystr(
"current_border_width");
293 y(integer, con->current_border_width);
296 dump_rect(gen,
"window_rect", con->window_rect);
297 dump_rect(gen,
"geometry", con->geometry);
300 if (con->window && con->window->name)
305 if (con->type == CT_WORKSPACE) {
307 y(integer, con->num);
312 y(integer, con->window->id);
316 if (con->window && !inplace_restart) {
320 ystr(
"window_properties");
323 #define DUMP_PROPERTY(key, prop_name) \
325 if (con->window->prop_name != NULL) { \
327 ystr(con->window->prop_name); \
335 if (con->window->name != NULL) {
346 if (con->type != CT_DOCKAREA || !inplace_restart) {
353 ystr(
"floating_nodes");
355 TAILQ_FOREACH (node, &(con->floating_head), floating_windows) {
363 y(integer, (
long int)node);
367 ystr(
"fullscreen_mode");
368 y(integer, con->fullscreen_mode);
371 switch (con->floating) {
372 case FLOATING_AUTO_OFF:
375 case FLOATING_AUTO_ON:
378 case FLOATING_USER_OFF:
381 case FLOATING_USER_ON:
391 if (match->
dock != -1) {
393 y(integer, match->
dock);
394 ystr(
"insert_where");
398 #define DUMP_REGEX(re_name) \
400 if (match->re_name != NULL) { \
402 ystr(match->re_name->pattern); \
415 if (inplace_restart) {
416 if (con->window != NULL) {
419 y(integer, con->window->id);
420 ystr(
"restart_mode");
427 if (inplace_restart && con->window != NULL) {
429 y(integer, con->depth);
449 #define YSTR_IF_SET(name) \
451 if (config->name) { \
453 ystr(config->name); \
461 switch (config->
mode) {
474 ystr(
"hidden_state");
524 ystr(
"workspace_buttons");
527 ystr(
"strip_workspace_numbers");
530 ystr(
"binding_mode_indicator");
537 #define YSTR_IF_SET(name) \
539 if (config->colors.name) { \
541 ystr(config->colors.name); \
569 setlocale(LC_NUMERIC,
"C");
572 setlocale(LC_NUMERIC,
"");
574 const unsigned char *payload;
576 y(get_buf, &payload, &length);
599 assert(ws->
type == CT_WORKSPACE);
615 y(
bool, ws == focused_ws);
641 const unsigned char *payload;
643 y(get_buf, &payload, &length);
674 y(integer, output->
rect.
x);
676 y(integer, output->
rect.
y);
683 ystr(
"current_workspace");
695 const unsigned char *payload;
697 y(get_buf, &payload, &length);
714 if (con->
mark != NULL)
719 const unsigned char *payload;
721 y(get_buf, &payload, &length);
736 y(integer, MAJOR_VERSION);
739 y(integer, MINOR_VERSION);
742 y(integer, PATCH_VERSION);
744 ystr(
"human_readable");
749 const unsigned char *payload;
751 y(get_buf, &payload, &length);
766 if (message_size == 0) {
774 const unsigned char *payload;
776 y(get_buf, &payload, &length);
785 char *bar_id =
scalloc(message_size + 1);
786 strncpy(bar_id, (
const char *)message, message_size);
787 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
790 if (strcmp(current->
id, bar_id) != 0)
810 const unsigned char *payload;
812 y(get_buf, &payload, &length);
826 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
834 memcpy(client->
events[event], s, len);
836 DLOG(
"client is now subscribed to:\n");
856 if (current->
fd != fd)
863 if (client == NULL) {
864 ELOG(
"Could not find ipc_client data structure for fd %d\n", fd);
869 static yajl_callbacks callbacks = {
873 p =
yalloc(&callbacks, (
void *)client);
874 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
875 if (stat != yajl_status_ok) {
877 err = yajl_get_error(p,
true, (
const unsigned char *)message,
879 ELOG(
"YAJL parse error: %s\n", err);
880 yajl_free_error(p, err);
882 const char *reply =
"{\"success\":false}";
883 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (
const uint8_t *)reply);
888 const char *reply =
"{\"success\":true}";
889 ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (
const uint8_t *)reply);
896 handle_get_workspaces,
901 handle_get_bar_config,
916 uint32_t message_type;
917 uint32_t message_length;
918 uint8_t *message = NULL;
920 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
924 if (ret == -1 && errno == EAGAIN) {
936 if (current->
fd != w->fd)
952 DLOG(
"IPC: client disconnected\n");
957 DLOG(
"Unhandled message type: %d\n", message_type);
960 h(w->fd, message, 0, message_length, message_type);
974 struct sockaddr_un peer;
975 socklen_t len =
sizeof(
struct sockaddr_un);
977 if ((client = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
986 (void)fcntl(client, F_SETFD, FD_CLOEXEC);
988 set_nonblock(client);
990 struct ev_io *
package = scalloc(sizeof(struct ev_io));
992 ev_io_start(EV_A_ package);
994 DLOG(
"IPC: new client connected on fd %d\n", w->fd);
1013 DLOG(
"Creating IPC-socket at %s\n", resolved);
1014 char *copy =
sstrdup(resolved);
1015 const char *dir = dirname(copy);
1023 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1029 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1031 struct sockaddr_un addr;
1032 memset(&addr, 0,
sizeof(
struct sockaddr_un));
1033 addr.sun_family = AF_LOCAL;
1034 strncpy(addr.sun_path, resolved,
sizeof(addr.sun_path) - 1);
1035 if (bind(sockfd, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0) {
1041 set_nonblock(sockfd);
1043 if (listen(sockfd, 5) < 0) {
1059 setlocale(LC_NUMERIC,
"C");
1078 const unsigned char *payload;
1080 y(get_buf, &payload, &length);
1082 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1084 setlocale(LC_NUMERIC,
"");
1092 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1093 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1095 setlocale(LC_NUMERIC,
"C");
1108 const unsigned char *payload;
1110 y(get_buf, &payload, &length);
1112 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1114 setlocale(LC_NUMERIC,
"");
1121 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1122 setlocale(LC_NUMERIC,
"C");
1127 const unsigned char *payload;
1129 y(get_buf, &payload, &length);
1131 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1133 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.
void ipc_send_workspace_focus_event(Con *current, Con *old)
For the workspace "focus" event we send, along the usual "change" field, also the current and previou...
char * name
Name of the output.
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.
static bool mkdirp(const char *path)
#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.
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...
A struct that contains useful information about the result of a command as a whole (e...
void(* handler_t)(int, uint8_t *, int, uint32_t, uint32_t)
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.
struct outputs_head outputs
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.
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
enum Match::@15 insert_where
#define TAILQ_INSERT_TAIL(head, elm, field)
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)
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)
#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)
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.