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);
37 if (fcntl(sockfd, F_SETFL, flags) < 0)
38 err(-1,
"Could not set O_NONBLOCK");
80 struct ev_timer *timeout =
scalloc(1,
sizeof(
struct ev_timer));
82 timeout->data = client;
84 ev_set_priority(timeout, EV_MINPRI);
86 }
else if (result > 0) {
110 const i3_ipc_header_t
header = {
111 .magic = {
'i',
'3',
'-',
'i',
'p',
'c'},
113 .type = message_type};
114 const size_t header_size =
sizeof(i3_ipc_header_t);
115 const size_t message_size = header_size + size;
129 DLOG(
"Disconnecting client on fd %d\n", client->
fd);
143 for (
int i = 0; i < client->
num_events; i++) {
156 void ipc_send_event(
const char *event, uint32_t message_type,
const char *payload) {
159 for (
int i = 0; i < current->
num_events; i++) {
160 if (strcasecmp(current->
events[i], event) == 0) {
185 const unsigned char *payload;
188 y(get_buf, &payload, &length);
189 ipc_send_event(
"shutdown", I3_IPC_EVENT_SHUTDOWN, (
const char *)payload);
205 shutdown(current->
fd, SHUT_RDWR);
218 char *command =
scalloc(message_size + 1, 1);
219 strncpy(command, (
const char *)message, message_size);
220 LOG(
"IPC: received: *%s*\n", command);
221 yajl_gen gen = yajl_gen_alloc(NULL);
231 const unsigned char *reply;
233 yajl_gen_get_buf(gen, &reply, &length);
236 (
const uint8_t *)reply);
257 for (
int i = 0; i < 20; i++) {
260 case XCB_KEY_BUT_MASK_SHIFT:
263 case XCB_KEY_BUT_MASK_LOCK:
266 case XCB_KEY_BUT_MASK_CONTROL:
269 case XCB_KEY_BUT_MASK_MOD_1:
272 case XCB_KEY_BUT_MASK_MOD_2:
275 case XCB_KEY_BUT_MASK_MOD_3:
278 case XCB_KEY_BUT_MASK_MOD_4:
281 case XCB_KEY_BUT_MASK_MOD_5:
284 case XCB_KEY_BUT_MASK_BUTTON_1:
287 case XCB_KEY_BUT_MASK_BUTTON_2:
290 case XCB_KEY_BUT_MASK_BUTTON_3:
293 case XCB_KEY_BUT_MASK_BUTTON_4:
296 case XCB_KEY_BUT_MASK_BUTTON_5:
339 ystr(
"event_state_mask");
348 y(integer, (uintptr_t)con);
361 case CT_FLOATING_CON:
362 ystr(
"floating_con");
383 ystr(
"scratchpad_state");
384 switch (con->scratchpad_state) {
385 case SCRATCHPAD_NONE:
388 case SCRATCHPAD_FRESH:
391 case SCRATCHPAD_CHANGED:
397 if (con->percent == 0.0)
400 y(
double, con->percent);
403 y(
bool, con->urgent);
420 if (con->type != CT_ROOT && con->type != CT_OUTPUT) {
426 switch (con->layout) {
428 DLOG(
"About to dump layout=default, this is a bug in the code.\n");
451 ystr(
"workspace_layout");
452 switch (con->workspace_layout) {
463 DLOG(
"About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
468 ystr(
"last_split_layout");
469 switch (con->layout) {
479 switch (con->border_style) {
491 ystr(
"current_border_width");
492 y(integer, con->current_border_width);
495 dump_rect(gen,
"deco_rect", con->deco_rect);
496 dump_rect(gen,
"window_rect", con->window_rect);
497 dump_rect(gen,
"geometry", con->geometry);
500 if (con->window && con->window->name)
502 else if (con->name != NULL)
507 if (con->title_format != NULL) {
508 ystr(
"title_format");
509 ystr(con->title_format);
512 if (con->type == CT_WORKSPACE) {
514 y(integer, con->num);
519 y(integer, con->window->id);
523 if (con->window && !inplace_restart) {
527 ystr(
"window_properties");
530 #define DUMP_PROPERTY(key, prop_name) \ 532 if (con->window->prop_name != NULL) { \ 534 ystr(con->window->prop_name); \ 542 if (con->window->name != NULL) {
547 ystr(
"transient_for");
548 if (con->window->transient_for == XCB_NONE)
551 y(integer, con->window->transient_for);
559 if (con->type != CT_DOCKAREA || !inplace_restart) {
566 ystr(
"floating_nodes");
568 TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
576 y(integer, (uintptr_t)node);
580 ystr(
"fullscreen_mode");
581 y(integer, con->fullscreen_mode);
584 y(
bool, con->sticky);
587 switch (con->floating) {
588 case FLOATING_AUTO_OFF:
591 case FLOATING_AUTO_ON:
594 case FLOATING_USER_OFF:
597 case FLOATING_USER_ON:
611 if (match->
dock != M_DONTCHECK) {
613 y(integer, match->
dock);
614 ystr(
"insert_where");
618 #define DUMP_REGEX(re_name) \ 620 if (match->re_name != NULL) { \ 622 ystr(match->re_name->pattern); \ 635 if (inplace_restart) {
636 if (con->window != NULL) {
639 y(integer, con->window->id);
640 ystr(
"restart_mode");
647 if (inplace_restart && con->window != NULL) {
649 y(integer, con->depth);
671 y(
bool, current->
release == B_UPON_KEYRELEASE);
681 if (strcasecmp(name,
"primary") == 0) {
694 if (
config->num_outputs > 0) {
697 for (
int c = 0; c <
config->num_outputs; c++) {
708 ystr(
"tray_outputs");
719 #define YSTR_IF_SET(name) \ 721 if (config->name) { \ 723 ystr(config->name); \ 727 ystr(
"tray_padding");
728 y(integer,
config->tray_padding);
746 ystr(
"hidden_state");
747 switch (
config->hidden_state) {
763 if (
config->position == P_BOTTOM)
771 if (
config->separator_symbol) {
772 ystr(
"separator_symbol");
776 ystr(
"workspace_buttons");
777 y(
bool, !
config->hide_workspace_buttons);
779 ystr(
"strip_workspace_numbers");
780 y(
bool,
config->strip_workspace_numbers);
782 ystr(
"strip_workspace_name");
783 y(
bool,
config->strip_workspace_name);
785 ystr(
"binding_mode_indicator");
786 y(
bool, !
config->hide_binding_mode_indicator);
792 #define YSTR_IF_SET(name) \ 794 if (config->colors.name) { \ 796 ystr(config->colors.name); \ 830 setlocale(LC_NUMERIC,
"C");
833 setlocale(LC_NUMERIC,
"");
835 const unsigned char *payload;
837 y(get_buf, &payload, &length);
860 assert(ws->
type == CT_WORKSPACE);
873 y(
bool, ws == focused_ws);
899 const unsigned char *payload;
901 y(get_buf, &payload, &length);
936 y(integer,
output->rect.width);
938 y(integer,
output->rect.height);
941 ystr(
"current_workspace");
953 const unsigned char *payload;
955 y(get_buf, &payload, &length);
980 const unsigned char *payload;
982 y(get_buf, &payload, &length);
997 y(integer, MAJOR_VERSION);
1000 y(integer, MINOR_VERSION);
1003 y(integer, PATCH_VERSION);
1005 ystr(
"human_readable");
1008 ystr(
"loaded_config_file_name");
1013 const unsigned char *payload;
1015 y(get_buf, &payload, &length);
1030 if (message_size == 0) {
1038 const unsigned char *payload;
1040 y(get_buf, &payload, &length);
1049 char *bar_id = NULL;
1050 sasprintf(&bar_id,
"%.*s", message_size, message);
1051 LOG(
"IPC: looking for config for bar ID \"%s\"\n", bar_id);
1054 if (strcmp(current->
id, bar_id) != 0)
1075 const unsigned char *payload;
1077 y(get_buf, &payload, &length);
1097 const unsigned char *payload;
1099 y(get_buf, &payload, &length);
1113 DLOG(
"should add subscription to extra %p, sub %.*s\n", client, (
int)len, s);
1121 memcpy(client->
events[event], s, len);
1123 DLOG(
"client is now subscribed to:\n");
1124 for (
int i = 0; i < client->
num_events; i++) {
1142 static yajl_callbacks callbacks = {
1146 p =
yalloc(&callbacks, (
void *)client);
1147 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1148 if (stat != yajl_status_ok) {
1150 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1152 ELOG(
"YAJL parse error: %s\n", err);
1153 yajl_free_error(p, err);
1155 const char *reply =
"{\"success\":false}";
1161 const char *reply =
"{\"success\":true}";
1164 if (client->first_tick_sent) {
1168 bool is_tick =
false;
1169 for (
int i = 0; i < client->num_events; i++) {
1170 if (strcmp(client->events[i],
"tick") == 0) {
1179 client->first_tick_sent =
true;
1180 const char *payload =
"{\"first\":true,\"payload\":\"\"}";
1197 const unsigned char *payload;
1199 y(get_buf, &payload, &length);
1218 yajl_gen_string(gen, (
unsigned char *)message, message_size);
1222 const unsigned char *payload;
1224 y(get_buf, &payload, &length);
1226 ipc_send_event(
"tick", I3_IPC_EVENT_TICK, (
const char *)payload);
1229 const char *reply =
"{\"success\":true}";
1231 DLOG(
"Sent tick event\n");
1244 memcpy(
state->last_key, val, len);
1250 if (strcasecmp(
state->last_key,
"rnd") == 0) {
1252 }
else if (strcasecmp(
state->last_key,
"window") == 0) {
1253 state->window = (xcb_window_t)val;
1263 static yajl_callbacks callbacks = {
1271 stat = yajl_parse(p, (
const unsigned char *)message, message_size);
1273 if (stat != yajl_status_ok) {
1275 err = yajl_get_error(p,
true, (
const unsigned char *)message,
1277 ELOG(
"YAJL parse error: %s\n", err);
1278 yajl_free_error(p, err);
1280 const char *reply =
"{\"success\":false}";
1287 DLOG(
"received IPC sync request (rnd = %d, window = 0x%08x)\n",
state.rnd,
state.window);
1289 const char *reply =
"{\"success\":true}";
1297 handle_get_workspaces,
1302 handle_get_bar_config,
1304 handle_get_binding_modes,
1321 uint32_t message_type;
1322 uint32_t message_length;
1323 uint8_t *message = NULL;
1325 assert(client->
fd == w->fd);
1327 int ret =
ipc_recv_message(w->fd, &message_type, &message_length, &message);
1331 if (ret == -1 && errno == EAGAIN) {
1344 DLOG(
"Unhandled message type: %d\n", message_type);
1347 h(client, message, 0, message_length, message_type);
1358 char *cmdline = NULL;
1359 #
if defined(__linux__) && defined(SO_PEERCRED)
1360 struct ucred peercred;
1361 socklen_t so_len =
sizeof(peercred);
1362 if (getsockopt(client->
fd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) != 0) {
1366 sasprintf(&exepath,
"/proc/%d/cmdline", peercred.pid);
1368 int fd = open(exepath, O_RDONLY);
1373 char buf[512] = {
'\0'};
1374 const ssize_t n = read(fd, buf,
sizeof(buf));
1379 for (
char *walk = buf; walk < buf + n - 1; walk++) {
1380 if (*walk ==
'\0') {
1387 ELOG(
"client %p with pid %d and cmdline '%s' on fd %d timed out, killing\n", client, peercred.pid, cmdline, client->
fd);
1393 ELOG(
"client %p on fd %d timed out, killing\n", client, client->
fd);
1400 DLOG(
"fd %d writeable\n", w->fd);
1405 assert(client->
timeout != NULL);
1417 struct sockaddr_un peer;
1418 socklen_t len =
sizeof(
struct sockaddr_un);
1420 if ((fd = accept(w->fd, (
struct sockaddr *)&peer, &len)) < 0) {
1421 if (errno != EINTR) {
1428 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1444 DLOG(
"IPC: new client connected on fd %d\n", w->fd);
1459 DLOG(
"Creating IPC-socket at %s\n", resolved);
1460 char *copy =
sstrdup(resolved);
1461 const char *dir = dirname(copy);
1469 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
1475 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1477 struct sockaddr_un addr;
1478 memset(&addr, 0,
sizeof(
struct sockaddr_un));
1479 addr.sun_family = AF_LOCAL;
1480 strncpy(addr.sun_path, resolved,
sizeof(addr.sun_path) - 1);
1481 if (bind(sockfd, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0) {
1489 if (listen(sockfd, 5) < 0) {
1504 setlocale(LC_NUMERIC,
"C");
1513 if (current == NULL)
1526 setlocale(LC_NUMERIC,
"");
1539 const unsigned char *payload;
1541 y(get_buf, &payload, &length);
1543 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE, (
const char *)payload);
1553 DLOG(
"Issue IPC window %s event (con = %p, window = 0x%08x)\n",
1554 property, con, (con->
window ? con->
window->
id : XCB_WINDOW_NONE));
1556 setlocale(LC_NUMERIC,
"C");
1569 const unsigned char *payload;
1571 y(get_buf, &payload, &length);
1573 ipc_send_event(
"window", I3_IPC_EVENT_WINDOW, (
const char *)payload);
1575 setlocale(LC_NUMERIC,
"");
1582 DLOG(
"Issue barconfig_update event for id = %s\n", barconfig->
id);
1583 setlocale(LC_NUMERIC,
"C");
1588 const unsigned char *payload;
1590 y(get_buf, &payload, &length);
1592 ipc_send_event(
"barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (
const char *)payload);
1594 setlocale(LC_NUMERIC,
"");
1601 DLOG(
"Issue IPC binding %s event (sym = %s, code = %d)\n", event_type, bind->
symbol, bind->
keycode);
1603 setlocale(LC_NUMERIC,
"C");
1617 const unsigned char *payload;
1619 y(get_buf, &payload, &length);
1621 ipc_send_event(
"binding", I3_IPC_EVENT_BINDING, (
const char *)payload);
1624 setlocale(LC_NUMERIC,
"");
struct outputs_head outputs
#define SLIST_FOREACH(var, head, field)
uint32_t keycode
Keycode to bind.
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
struct ev_io * write_callback
#define YSTR_IF_SET(name)
static void ipc_socket_writeable_cb(EV_P_ struct ev_io *w, int revents)
char * current_socketpath
static int _sync_json_int(void *extra, long long val)
struct barconfig_head barconfigs
char * symbol
Symbol the user specified in configfile, if any.
char * command
Command, like in command mode.
#define TAILQ_HEAD(name, type)
static void dump_bar_config(yajl_gen gen, Barconfig *config)
#define TAILQ_EMPTY(head)
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...
An Output is a physical output on your graphics driver.
A struct that contains useful information about the result of a command as a whole (e....
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
void ipc_send_binding_event(const char *event_type, Binding *bind)
For the binding events, we send the serialized binding struct.
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
static void dump_event_state_mask(yajl_gen gen, Binding *bind)
static void dump_bar_bindings(yajl_gen gen, Barconfig *config)
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
static void ipc_receive_message(EV_P_ struct ev_io *w, int revents)
struct bindings_head * bindings
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,...
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 yalloc(callbacks, client)
The configuration file can contain multiple sets of bindings.
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
enum Match::@17 insert_where
Output * get_output_by_name(const char *name, const bool require_active)
Returns the output with the given name or NULL.
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
#define TAILQ_REMOVE(head, elm, field)
static void set_nonblock(int sockfd)
#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...
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
static int add_subscription(void *extra, const unsigned char *s, ylength len)
Stores a rectangle, for example the size of a window, the child window etc.
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 ...
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
bool release
If true, the command will be executed after the button is released.
#define DUMP_PROPERTY(key, prop_name)
char * current_configpath
static void dump_binding(yajl_gen gen, Binding *bind)
#define DUMP_REGEX(re_name)
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
bool con_is_split(Con *con)
Returns true if a container should be considered split.
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Con * output_get_content(Con *output)
Returns the output container below the given output container.
struct ev_timer * timeout
void ipc_set_kill_timeout(ev_tstamp new)
Set the maximum duration that we allow for a connection with an unwriteable socket.
static ev_tstamp kill_timeout
void ipc_shutdown(shutdown_reason_t reason)
Calls shutdown() on each socket and closes it.
static void ipc_push_pending(ipc_client *client)
static void ipc_send_shutdown_event(shutdown_reason_t reason)
shutdown_reason_t
Calls to ipc_shutdown() should provide a reason for the shutdown.
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
struct all_cons_head all_cons
char * command
The command which is to be executed for this button.
Holds the status bar configuration (i3bar).
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
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.
A 'Con' represents everything from the X11 root window down to a single X11 window.
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),...
static int _sync_json_key(void *extra, const unsigned char *val, size_t len)
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
void sync_respond(xcb_window_t window, uint32_t rnd)
const char * i3_version
Git commit identifier, from version.c.
static void ipc_send_client_message(ipc_client *client, size_t size, const uint32_t message_type, const uint8_t *payload)
static void dump_rect(yajl_gen gen, const char *name, Rect r)
static char * canonicalize_output_name(char *name)
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
struct ev_io * read_callback
struct ev_loop * main_loop
int input_code
The button to be used (e.g., 1 for "button1").
void(* handler_t)(ipc_client *, uint8_t *, int, uint32_t, uint32_t)
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
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_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...
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 void ipc_client_timeout(EV_P_ ev_timer *w, int revents)
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
#define TAILQ_HEAD_INITIALIZER(head)
#define TAILQ_INSERT_TAIL(head, elm, field)
#define TAILQ_FOREACH(var, head, field)
static i3_shmlog_header * header
static void free_ipc_client(ipc_client *client)
i3_event_state_mask_t event_state_mask
Bitmask which is applied against event->state for KeyPress and KeyRelease events to determine whether...
char * id
Automatically generated ID for this bar config.