X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fipc.c;h=0d2c92b87ecfe9fec062fa7804dcb862bc12e29d;hb=617afc67a25c9bb7bf91a7659319e07fbe32d758;hp=84ef2c36289adc63736c74583d4b0bd49e6cbcaf;hpb=0aa306890b66047aacb4863df0cfdfd6f46361e6;p=i3%2Fi3 diff --git a/src/ipc.c b/src/ipc.c index 84ef2c36..0d2c92b8 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -10,6 +10,7 @@ * */ #include "all.h" +#include "yajl_utils.h" #include #include @@ -18,14 +19,9 @@ #include #include #include -#include char *current_socketpath = NULL; -/* Shorter names for all those yajl_gen_* functions */ -#define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__) -#define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str)) - TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients); /* @@ -121,24 +117,24 @@ IPC_HANDLER(command) { char *command = scalloc(message_size + 1); strncpy(command, (const char*)message, message_size); LOG("IPC: received: *%s*\n", command); - struct CommandResult *command_output = parse_command((const char*)command); + yajl_gen gen = yajl_gen_alloc(NULL); + + CommandResult *result = parse_command((const char*)command, gen); free(command); - if (command_output->needs_tree_render) + if (result->needs_tree_render) tree_render(); + command_result_free(result); + const unsigned char *reply; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif - yajl_gen_get_buf(command_output->json_gen, &reply, &length); + ylength length; + yajl_gen_get_buf(gen, &reply, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND, (const uint8_t*)reply); - yajl_gen_free(command_output->json_gen); + yajl_gen_free(gen); } static void dump_rect(yajl_gen gen, const char *name, Rect r) { @@ -161,11 +157,34 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) { y(integer, (long int)con); ystr("type"); - y(integer, con->type); + switch (con->type) { + case CT_ROOT: + ystr("root"); + break; + case CT_OUTPUT: + ystr("output"); + break; + case CT_CON: + ystr("con"); + break; + case CT_FLOATING_CON: + ystr("floating_con"); + break; + case CT_WORKSPACE: + ystr("workspace"); + break; + case CT_DOCKAREA: + ystr("dockarea"); + break; + default: + DLOG("About to dump unknown container type=%d. This is a bug.\n", con->type); + assert(false); + break; + } /* provided for backwards compatibility only. */ ystr("orientation"); - if (!con->split) + if (!con_is_split(con)) ystr("none"); else { if (con_orientation(con) == HORIZ) @@ -202,9 +221,6 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) { ystr("focused"); y(bool, (con == focused)); - ystr("split"); - y(bool, con->split); - ystr("layout"); switch (con->layout) { case L_DEFAULT: @@ -294,6 +310,32 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) { y(integer, con->window->id); else y(null); + if (con->window && !inplace_restart) { + /* Window properties are useless to preserve when restarting because + * they will be queried again anyway. However, for i3-save-tree(1), + * they are very useful and save i3-save-tree dealing with X11. */ + ystr("window_properties"); + y(map_open); + +#define DUMP_PROPERTY(key, prop_name) do { \ + if (con->window->prop_name != NULL) { \ + ystr(key); \ + ystr(con->window->prop_name); \ + } \ +} while (0) + + DUMP_PROPERTY("class", class_class); + DUMP_PROPERTY("instance", class_instance); + DUMP_PROPERTY("window_role", role); + + if (con->window->name != NULL) { + ystr("title"); + ystr(i3string_as_utf8(con->window->name)); + } + + y(map_close); + } + ystr("nodes"); y(array_open); Con *node; @@ -341,16 +383,28 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) { y(array_open); Match *match; TAILQ_FOREACH(match, &(con->swallow_head), matches) { + y(map_open); if (match->dock != -1) { - y(map_open); ystr("dock"); y(integer, match->dock); ystr("insert_where"); y(integer, match->insert_where); - y(map_close); } - /* TODO: the other swallow keys */ +#define DUMP_REGEX(re_name) do { \ + if (match->re_name != NULL) { \ + ystr(# re_name); \ + ystr(match->re_name->pattern); \ + } \ +} while (0) + + DUMP_REGEX(class); + DUMP_REGEX(instance); + DUMP_REGEX(window_role); + DUMP_REGEX(title); + +#undef DUMP_REGEX + y(map_close); } if (inplace_restart) { @@ -365,25 +419,154 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) { } y(array_close); + if (inplace_restart && con->window != NULL) { + ystr("depth"); + y(integer, con->depth); + } + + y(map_close); +} + +static void dump_bar_config(yajl_gen gen, Barconfig *config) { + y(map_open); + + ystr("id"); + ystr(config->id); + + if (config->num_outputs > 0) { + ystr("outputs"); + y(array_open); + for (int c = 0; c < config->num_outputs; c++) + ystr(config->outputs[c]); + y(array_close); + } + +#define YSTR_IF_SET(name) \ + do { \ + if (config->name) { \ + ystr( # name); \ + ystr(config->name); \ + } \ + } while (0) + + YSTR_IF_SET(tray_output); + YSTR_IF_SET(socket_path); + + ystr("mode"); + switch (config->mode) { + case M_HIDE: + ystr("hide"); + break; + case M_INVISIBLE: + ystr("invisible"); + break; + case M_DOCK: + default: + ystr("dock"); + break; + } + + ystr("hidden_state"); + switch (config->hidden_state) { + case S_SHOW: + ystr("show"); + break; + case S_HIDE: + default: + ystr("hide"); + break; + } + + ystr("modifier"); + switch (config->modifier) { + case M_CONTROL: + ystr("ctrl"); + break; + case M_SHIFT: + ystr("shift"); + break; + case M_MOD1: + ystr("Mod1"); + break; + case M_MOD2: + ystr("Mod2"); + break; + case M_MOD3: + ystr("Mod3"); + break; + /* + case M_MOD4: + ystr("Mod4"); + break; + */ + case M_MOD5: + ystr("Mod5"); + break; + default: + ystr("Mod4"); + break; + } + + ystr("position"); + if (config->position == P_BOTTOM) + ystr("bottom"); + else ystr("top"); + + YSTR_IF_SET(status_command); + YSTR_IF_SET(font); + + ystr("workspace_buttons"); + y(bool, !config->hide_workspace_buttons); + + ystr("strip_workspace_numbers"); + y(bool, config->strip_workspace_numbers); + + ystr("binding_mode_indicator"); + y(bool, !config->hide_binding_mode_indicator); + + ystr("verbose"); + y(bool, config->verbose); + +#undef YSTR_IF_SET +#define YSTR_IF_SET(name) \ + do { \ + if (config->colors.name) { \ + ystr( # name); \ + ystr(config->colors.name); \ + } \ + } while (0) + + ystr("colors"); + y(map_open); + YSTR_IF_SET(background); + YSTR_IF_SET(statusline); + YSTR_IF_SET(separator); + YSTR_IF_SET(focused_workspace_border); + YSTR_IF_SET(focused_workspace_bg); + YSTR_IF_SET(focused_workspace_text); + YSTR_IF_SET(active_workspace_border); + YSTR_IF_SET(active_workspace_bg); + YSTR_IF_SET(active_workspace_text); + YSTR_IF_SET(inactive_workspace_border); + YSTR_IF_SET(inactive_workspace_bg); + YSTR_IF_SET(inactive_workspace_text); + YSTR_IF_SET(urgent_workspace_border); + YSTR_IF_SET(urgent_workspace_bg); + YSTR_IF_SET(urgent_workspace_text); + y(map_close); + y(map_close); +#undef YSTR_IF_SET } IPC_HANDLER(tree) { setlocale(LC_NUMERIC, "C"); -#if YAJL_MAJOR >= 2 - yajl_gen gen = yajl_gen_alloc(NULL); -#else - yajl_gen gen = yajl_gen_alloc(NULL, NULL); -#endif + yajl_gen gen = ygenalloc(); dump_node(gen, croot, false); setlocale(LC_NUMERIC, ""); const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload); @@ -397,11 +580,7 @@ IPC_HANDLER(tree) { * */ IPC_HANDLER(get_workspaces) { -#if YAJL_MAJOR >= 2 - yajl_gen gen = yajl_gen_alloc(NULL); -#else - yajl_gen gen = yajl_gen_alloc(NULL, NULL); -#endif + yajl_gen gen = ygenalloc(); y(array_open); Con *focused_ws = con_get_workspace(focused); @@ -454,11 +633,7 @@ IPC_HANDLER(get_workspaces) { y(array_close); const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload); @@ -471,11 +646,7 @@ IPC_HANDLER(get_workspaces) { * */ IPC_HANDLER(get_outputs) { -#if YAJL_MAJOR >= 2 - yajl_gen gen = yajl_gen_alloc(NULL); -#else - yajl_gen gen = yajl_gen_alloc(NULL, NULL); -#endif + yajl_gen gen = ygenalloc(); y(array_open); Output *output; @@ -515,11 +686,7 @@ IPC_HANDLER(get_outputs) { y(array_close); const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload); @@ -532,11 +699,7 @@ IPC_HANDLER(get_outputs) { * */ IPC_HANDLER(get_marks) { -#if YAJL_MAJOR >= 2 - yajl_gen gen = yajl_gen_alloc(NULL); -#else - yajl_gen gen = yajl_gen_alloc(NULL, NULL); -#endif + yajl_gen gen = ygenalloc(); y(array_open); Con *con; @@ -547,11 +710,7 @@ IPC_HANDLER(get_marks) { y(array_close); const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload); @@ -563,11 +722,7 @@ IPC_HANDLER(get_marks) { * */ IPC_HANDLER(get_version) { -#if YAJL_MAJOR >= 2 - yajl_gen gen = yajl_gen_alloc(NULL); -#else - yajl_gen gen = yajl_gen_alloc(NULL, NULL); -#endif + yajl_gen gen = ygenalloc(); y(map_open); ystr("major"); @@ -585,11 +740,7 @@ IPC_HANDLER(get_version) { y(map_close); const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload); @@ -602,11 +753,7 @@ IPC_HANDLER(get_version) { * */ IPC_HANDLER(get_bar_config) { -#if YAJL_MAJOR >= 2 - yajl_gen gen = yajl_gen_alloc(NULL); -#else - yajl_gen gen = yajl_gen_alloc(NULL, NULL); -#endif + yajl_gen gen = ygenalloc(); /* If no ID was passed, we return a JSON array with all IDs */ if (message_size == 0) { @@ -618,11 +765,7 @@ IPC_HANDLER(get_bar_config) { y(array_close); const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload); @@ -644,123 +787,21 @@ IPC_HANDLER(get_bar_config) { break; } - y(map_open); - if (!config) { /* If we did not find a config for the given ID, the reply will contain * a null 'id' field. */ + y(map_open); + ystr("id"); y(null); - } else { - ystr("id"); - ystr(config->id); - - if (config->num_outputs > 0) { - ystr("outputs"); - y(array_open); - for (int c = 0; c < config->num_outputs; c++) - ystr(config->outputs[c]); - y(array_close); - } - -#define YSTR_IF_SET(name) \ - do { \ - if (config->name) { \ - ystr( # name); \ - ystr(config->name); \ - } \ - } while (0) - - YSTR_IF_SET(tray_output); - YSTR_IF_SET(socket_path); - - ystr("mode"); - if (config->mode == M_HIDE) - ystr("hide"); - else ystr("dock"); - - ystr("modifier"); - switch (config->modifier) { - case M_CONTROL: - ystr("ctrl"); - break; - case M_SHIFT: - ystr("shift"); - break; - case M_MOD1: - ystr("Mod1"); - break; - case M_MOD2: - ystr("Mod2"); - break; - case M_MOD3: - ystr("Mod3"); - break; - /* - case M_MOD4: - ystr("Mod4"); - break; - */ - case M_MOD5: - ystr("Mod5"); - break; - default: - ystr("Mod4"); - break; - } - - ystr("position"); - if (config->position == P_BOTTOM) - ystr("bottom"); - else ystr("top"); - YSTR_IF_SET(status_command); - YSTR_IF_SET(font); - - ystr("workspace_buttons"); - y(bool, !config->hide_workspace_buttons); - - ystr("verbose"); - y(bool, config->verbose); - -#undef YSTR_IF_SET -#define YSTR_IF_SET(name) \ - do { \ - if (config->colors.name) { \ - ystr( # name); \ - ystr(config->colors.name); \ - } \ - } while (0) - - ystr("colors"); - y(map_open); - YSTR_IF_SET(background); - YSTR_IF_SET(statusline); - YSTR_IF_SET(focused_workspace_border); - YSTR_IF_SET(focused_workspace_bg); - YSTR_IF_SET(focused_workspace_text); - YSTR_IF_SET(active_workspace_border); - YSTR_IF_SET(active_workspace_bg); - YSTR_IF_SET(active_workspace_text); - YSTR_IF_SET(inactive_workspace_border); - YSTR_IF_SET(inactive_workspace_bg); - YSTR_IF_SET(inactive_workspace_text); - YSTR_IF_SET(urgent_workspace_border); - YSTR_IF_SET(urgent_workspace_bg); - YSTR_IF_SET(urgent_workspace_text); y(map_close); - -#undef YSTR_IF_SET + } else { + dump_bar_config(gen, config); } - y(map_close); - const unsigned char *payload; -#if YAJL_MAJOR >= 2 - size_t length; -#else - unsigned int length; -#endif + ylength length; y(get_buf, &payload, &length); ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload); @@ -771,13 +812,8 @@ IPC_HANDLER(get_bar_config) { * Callback for the YAJL parser (will be called when a string is parsed). * */ -#if YAJL_MAJOR < 2 static int add_subscription(void *extra, const unsigned char *s, - unsigned int len) { -#else -static int add_subscription(void *extra, const unsigned char *s, - size_t len) { -#endif + ylength len) { ipc_client *client = extra; DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s); @@ -805,7 +841,6 @@ static int add_subscription(void *extra, const unsigned char *s, */ IPC_HANDLER(subscribe) { yajl_handle p; - yajl_callbacks callbacks; yajl_status stat; ipc_client *current, *client = NULL; @@ -824,14 +859,11 @@ IPC_HANDLER(subscribe) { } /* Setup the JSON parser */ - memset(&callbacks, 0, sizeof(yajl_callbacks)); - callbacks.yajl_string = add_subscription; - -#if YAJL_MAJOR >= 2 - p = yajl_alloc(&callbacks, NULL, (void*)client); -#else - p = yajl_alloc(&callbacks, NULL, NULL, (void*)client); -#endif + static yajl_callbacks callbacks = { + .yajl_string = add_subscription, + }; + + p = yalloc(&callbacks, (void*)client); stat = yajl_parse(p, (const unsigned char*)message, message_size); if (stat != yajl_status_ok) { unsigned char *err; @@ -874,18 +906,18 @@ handler_t handlers[8] = { * */ static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) { - char buf[2048]; - int n = read(w->fd, buf, sizeof(buf)); - - /* On error or an empty message, we close the connection */ - if (n <= 0) { -#if 0 - /* FIXME: I get these when closing a client socket, - * therefore we just treat them as an error. Is this - * correct? */ - if (errno == EAGAIN || errno == EWOULDBLOCK) - return; -#endif + uint32_t message_type; + uint32_t message_length; + uint8_t *message = NULL; + + int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message); + /* EOF or other error */ + if (ret < 0) { + /* Was this a spurious read? See ev(3) */ + if (ret == -1 && errno == EAGAIN) { + FREE(message); + return; + } /* If not, there was some kind of error. We don’t bother * and close the connection */ @@ -908,57 +940,20 @@ static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) { ev_io_stop(EV_A_ w); free(w); + FREE(message); DLOG("IPC: client disconnected\n"); return; } - /* Terminate the message correctly */ - buf[n] = '\0'; - - /* Check if the message starts with the i3 IPC magic code */ - if (n < strlen(I3_IPC_MAGIC)) { - DLOG("IPC: message too short, ignoring\n"); - return; - } - - if (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) { - DLOG("IPC: message does not start with the IPC magic\n"); - return; + if (message_type >= (sizeof(handlers) / sizeof(handler_t))) + DLOG("Unhandled message type: %d\n", message_type); + else { + handler_t h = handlers[message_type]; + h(w->fd, message, 0, message_length, message_type); } - uint8_t *message = (uint8_t*)buf; - while (n > 0) { - DLOG("IPC: n = %d\n", n); - message += strlen(I3_IPC_MAGIC); - n -= strlen(I3_IPC_MAGIC); - - /* The next 32 bit after the magic are the message size */ - uint32_t message_size; - memcpy(&message_size, (uint32_t*)message, sizeof(uint32_t)); - message += sizeof(uint32_t); - n -= sizeof(uint32_t); - - if (message_size > n) { - DLOG("IPC: Either the message size was wrong or the message was not read completely, dropping\n"); - return; - } - - /* The last 32 bits of the header are the message type */ - uint32_t message_type; - memcpy(&message_type, (uint32_t*)message, sizeof(uint32_t)); - message += sizeof(uint32_t); - n -= sizeof(uint32_t); - - if (message_type >= (sizeof(handlers) / sizeof(handler_t))) - DLOG("Unhandled message type: %d\n", message_type); - else { - handler_t h = handlers[message_type]; - h(w->fd, message, n, message_size, message_type); - } - n -= message_size; - message += message_size; - } + FREE(message); } /* @@ -1046,3 +1041,86 @@ int ipc_create_socket(const char *filename) { current_socketpath = resolved; return sockfd; } + +/* + * For the workspace "focus" event we send, along the usual "change" field, + * also the current and previous workspace, in "current" and "old" + * respectively. + */ +void ipc_send_workspace_focus_event(Con *current, Con *old) { + setlocale(LC_NUMERIC, "C"); + yajl_gen gen = ygenalloc(); + + y(map_open); + + ystr("change"); + ystr("focus"); + + ystr("current"); + dump_node(gen, current, false); + + ystr("old"); + if (old == NULL) + y(null); + else + dump_node(gen, old, false); + + y(map_close); + + const unsigned char *payload; + ylength length; + y(get_buf, &payload, &length); + + ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload); + y(free); + setlocale(LC_NUMERIC, ""); +} + +/** + * For the window events we send, along the usual "change" field, + * also the window container, in "container". + */ +void ipc_send_window_event(const char *property, Con *con) { + DLOG("Issue IPC window %s event (con = %p, window = 0x%08x)\n", + property, con, (con->window ? con->window->id : XCB_WINDOW_NONE)); + + setlocale(LC_NUMERIC, "C"); + yajl_gen gen = ygenalloc(); + + y(map_open); + + ystr("change"); + ystr(property); + + ystr("container"); + dump_node(gen, con, false); + + y(map_close); + + const unsigned char *payload; + ylength length; + y(get_buf, &payload, &length); + + ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload); + y(free); + setlocale(LC_NUMERIC, ""); +} + +/** + * For the barconfig update events, we send the serialized barconfig. + */ +void ipc_send_barconfig_update_event(Barconfig *barconfig) { + DLOG("Issue barconfig_update event for id = %s\n", barconfig->id); + setlocale(LC_NUMERIC, "C"); + yajl_gen gen = ygenalloc(); + + dump_bar_config(gen, barconfig); + + const unsigned char *payload; + ylength length; + y(get_buf, &payload, &length); + + ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, (const char *)payload); + y(free); + setlocale(LC_NUMERIC, ""); +}