From faccaf6112d923533512e1dd868ec4bf0d30e1b5 Mon Sep 17 00:00:00 2001 From: minus Date: Tue, 18 Aug 2015 23:38:34 +0200 Subject: added sway_assert function returns false on a failed assertion in release mode and raises SIGABRT in debug mode --- include/log.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'include/log.h') diff --git a/include/log.h b/include/log.h index d35b2a54..44f84940 100644 --- a/include/log.h +++ b/include/log.h @@ -1,5 +1,6 @@ #ifndef _SWAY_LOG_H #define _SWAY_LOG_H +#include typedef enum { L_SILENT = 0, @@ -10,7 +11,8 @@ typedef enum { void init_log(int verbosity); void sway_log_colors(int mode); -void sway_log(int verbosity, char* format, ...) __attribute__((format(printf,2,3))); -void sway_abort(char* format, ...)__attribute__((format(printf,1,2))); +void sway_log(int verbosity, const char* format, ...) __attribute__((format(printf,2,3))); +void sway_abort(const char* format, ...) __attribute__((format(printf,1,2))); +bool sway_assert(bool condition, const char* format, ...) __attribute__((format(printf,2,3))); #endif -- cgit v1.2.3-70-g09d2 From c5a69828934bf07db9062bd5f24bb2ff94b45b4a Mon Sep 17 00:00:00 2001 From: taiyu Date: Wed, 19 Aug 2015 01:06:15 -0700 Subject: fixed some more bugs, moved layout_log into log.ch, restored focus_parent --- include/log.h | 2 ++ include/workspace.h | 1 - sway/commands.c | 10 +++++---- sway/container.c | 1 - sway/focus.c | 3 +++ sway/log.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sway/workspace.c | 57 --------------------------------------------------- 7 files changed, 70 insertions(+), 63 deletions(-) (limited to 'include/log.h') diff --git a/include/log.h b/include/log.h index 44f84940..7aea2ded 100644 --- a/include/log.h +++ b/include/log.h @@ -1,6 +1,7 @@ #ifndef _SWAY_LOG_H #define _SWAY_LOG_H #include +#include "container.h" typedef enum { L_SILENT = 0, @@ -15,4 +16,5 @@ void sway_log(int verbosity, const char* format, ...) __attribute__((format(prin void sway_abort(const char* format, ...) __attribute__((format(printf,1,2))); bool sway_assert(bool condition, const char* format, ...) __attribute__((format(printf,2,3))); +void layout_log(const swayc_t *c, int depth); #endif diff --git a/include/workspace.h b/include/workspace.h index 8ce39bbd..042a15d9 100644 --- a/include/workspace.h +++ b/include/workspace.h @@ -15,6 +15,5 @@ void workspace_output_next(); void workspace_next(); void workspace_output_prev(); void workspace_prev(); -void layout_log(const swayc_t *c, int depth); #endif diff --git a/sway/commands.c b/sway/commands.c index 6e1f1848..7dde78bd 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -182,20 +182,22 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { if (view->type != C_VIEW) { return true; } - int i; // Change from nonfloating to floating if (!view->is_floating) { - remove_child(view); + //Remove view from its current location + destroy_container(remove_child(view)); + + //and move it into workspace floating add_floating(active_workspace,view); view->x = (active_workspace->width - view->width)/2; view->y = (active_workspace->height - view->height)/2; - arrange_windows(active_workspace, -1, -1); if (view->desired_width != -1) { view->width = view->desired_width; } if (view->desired_height != -1) { view->height = view->desired_height; } + arrange_windows(active_workspace, -1, -1); } else { // Delete the view from the floating list and unset its is_floating flag // Using length-1 as the index is safe because the view must be the currently @@ -221,10 +223,10 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { add_sibling(focused, view); } // Refocus on the view once its been put back into the layout - set_focused_container(view); arrange_windows(active_workspace, -1, -1); return true; } + set_focused_container(view); } return true; diff --git a/sway/container.c b/sway/container.c index 9763f381..0a89f634 100644 --- a/sway/container.c +++ b/sway/container.c @@ -261,7 +261,6 @@ swayc_t *destroy_container(swayc_t *container) { sway_log(L_DEBUG, "Container: Destroying container '%p'", container); swayc_t *parent = container->parent; free_swayc(container); - container = parent; } return container; diff --git a/sway/focus.c b/sway/focus.c index 0ee10694..7023d37d 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -146,6 +146,9 @@ void set_focused_container(swayc_t *c) { // update container focus from here to root, making necessary changes along // the way swayc_t *p = c; + if (p->type != C_OUTPUT && p->type != C_ROOT) { + p->is_focused = true; + } while (p != &root_container) { update_focus(p); p = p->parent; diff --git a/sway/log.c b/sway/log.c index 5bd3c8dc..9b9a9dc0 100644 --- a/sway/log.c +++ b/sway/log.c @@ -82,3 +82,62 @@ bool sway_assert(bool condition, const char* format, ...) { return false; } + +#include "workspace.h" + +/* XXX:DEBUG:XXX */ +static void container_log(const swayc_t *c) { + fprintf(stderr, "focus:%c|", + c->is_focused ? 'F' : //Focused + c == active_workspace ? 'W' : //active workspace + c == &root_container ? 'R' : //root + 'X');//not any others + fprintf(stderr,"(%p)",c); + fprintf(stderr,"(p:%p)",c->parent); + fprintf(stderr,"(f:%p)",c->focused); + fprintf(stderr,"(h:%ld)",c->handle); + fprintf(stderr,"Type:"); + fprintf(stderr, + c->type == C_ROOT ? "Root|" : + c->type == C_OUTPUT ? "Output|" : + c->type == C_WORKSPACE ? "Workspace|" : + c->type == C_CONTAINER ? "Container|" : + c->type == C_VIEW ? "View|" : "Unknown|"); + fprintf(stderr,"layout:"); + fprintf(stderr, + c->layout == L_NONE ? "NONE|" : + c->layout == L_HORIZ ? "Horiz|": + c->layout == L_VERT ? "Vert|": + c->layout == L_STACKED ? "Stacked|": + c->layout == L_FLOATING ? "Floating|": + "Unknown|"); + fprintf(stderr, "w:%d|h:%d|", c->width, c->height); + fprintf(stderr, "x:%d|y:%d|", c->x, c->y); + fprintf(stderr, "vis:%c|", c->visible?'t':'f'); + fprintf(stderr, "wgt:%d|", c->weight); + fprintf(stderr, "name:%.16s|", c->name); + fprintf(stderr, "children:%d\n",c->children?c->children->length:0); +} +void layout_log(const swayc_t *c, int depth) { + int i, d; + int e = c->children ? c->children->length : 0; + container_log(c); + if (e) { + for (i = 0; i < e; ++i) { + fputc('|',stderr); + for (d = 0; d < depth; ++d) fputc('-', stderr); + layout_log(c->children->items[i], depth + 1); + } + } + if (c->type == C_WORKSPACE) { + e = c->floating?c->floating->length:0; + if (e) { + for (i = 0; i < e; ++i) { + fputc('|',stderr); + for (d = 0; d < depth; ++d) fputc('=', stderr); + layout_log(c->floating->items[i], depth + 1); + } + } + } +} +/* XXX:DEBUG:XXX */ diff --git a/sway/workspace.c b/sway/workspace.c index 60108752..9b407c6a 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -182,60 +182,3 @@ void workspace_switch(swayc_t *workspace) { set_focused_container(get_focused_view(workspace)); arrange_windows(workspace, -1, -1); } - -/* XXX:DEBUG:XXX */ -static void container_log(const swayc_t *c) { - fprintf(stderr, "focus:%c|", - c->is_focused ? 'F' : //Focused - c == active_workspace ? 'W' : //active workspace - c == &root_container ? 'R' : //root - 'X');//not any others - fprintf(stderr,"(%p)",c); - fprintf(stderr,"(p:%p)",c->parent); - fprintf(stderr,"(f:%p)",c->focused); - fprintf(stderr,"(h:%ld)",c->handle); - fprintf(stderr,"Type:"); - fprintf(stderr, - c->type == C_ROOT ? "Root|" : - c->type == C_OUTPUT ? "Output|" : - c->type == C_WORKSPACE ? "Workspace|" : - c->type == C_CONTAINER ? "Container|" : - c->type == C_VIEW ? "View|" : "Unknown|"); - fprintf(stderr,"layout:"); - fprintf(stderr, - c->layout == L_NONE ? "NONE|" : - c->layout == L_HORIZ ? "Horiz|": - c->layout == L_VERT ? "Vert|": - c->layout == L_STACKED ? "Stacked|": - c->layout == L_FLOATING ? "Floating|": - "Unknown|"); - fprintf(stderr, "w:%d|h:%d|", c->width, c->height); - fprintf(stderr, "x:%d|y:%d|", c->x, c->y); - fprintf(stderr, "vis:%c|", c->visible?'t':'f'); - fprintf(stderr, "wgt:%d|", c->weight); - fprintf(stderr, "name:%.16s|", c->name); - fprintf(stderr, "children:%d\n",c->children?c->children->length:0); -} -void layout_log(const swayc_t *c, int depth) { - int i, d; - int e = c->children ? c->children->length : 0; - container_log(c); - if (e) { - for (i = 0; i < e; ++i) { - fputc('|',stderr); - for (d = 0; d < depth; ++d) fputc('-', stderr); - layout_log(c->children->items[i], depth + 1); - } - } - if (c->type == C_WORKSPACE) { - e = c->floating?c->floating->length:0; - if (e) { - for (i = 0; i < e; ++i) { - fputc('|',stderr); - for (d = 0; d < depth; ++d) fputc('-', stderr); - layout_log(c->floating->items[i], depth + 1); - } - } - } -} -/* XXX:DEBUG:XXX */ -- cgit v1.2.3-70-g09d2 From 773e85c681ee4faecf353de7066b536f1a50ff61 Mon Sep 17 00:00:00 2001 From: minus Date: Wed, 19 Aug 2015 01:35:01 +0200 Subject: properly handle IPC clients --- include/log.h | 1 + sway/ipc.c | 191 ++++++++++++++++++++++++++++++++++++++++------------------ sway/log.c | 30 +++++++++ 3 files changed, 162 insertions(+), 60 deletions(-) (limited to 'include/log.h') diff --git a/include/log.h b/include/log.h index 7aea2ded..47a83321 100644 --- a/include/log.h +++ b/include/log.h @@ -13,6 +13,7 @@ typedef enum { void init_log(int verbosity); void sway_log_colors(int mode); void sway_log(int verbosity, const char* format, ...) __attribute__((format(printf,2,3))); +void sway_log_errno(int verbosity, char* format, ...) __attribute__((format(printf,2,3))); void sway_abort(const char* format, ...) __attribute__((format(printf,1,2))); bool sway_assert(bool condition, const char* format, ...) __attribute__((format(printf,2,3))); diff --git a/sway/ipc.c b/sway/ipc.c index 1a074788..292d9593 100644 --- a/sway/ipc.c +++ b/sway/ipc.c @@ -1,3 +1,5 @@ +// See https://i3wm.org/docs/ipc.html for protocol information + #include #include #include @@ -6,6 +8,8 @@ #include #include #include +#include +#include #include "ipc.h" #include "log.h" #include "config.h" @@ -15,10 +19,18 @@ static int ipc_socket = -1; static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; -int ipc_handle_connection(int fd, uint32_t mask, void *data); -size_t ipc_handle_command(char **reply_data, char *data, ssize_t length); -size_t ipc_format_reply(char **data, enum ipc_command_type command_type, const char *payload, uint32_t payload_length); +struct ipc_client { + struct wlc_event_source *event_source; + int fd; + uint32_t payload_length; + enum ipc_command_type current_command; +}; +int ipc_handle_connection(int fd, uint32_t mask, void *data); +int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); +void ipc_client_disconnect(struct ipc_client *client); +void ipc_client_handle_command(struct ipc_client *client); +bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); void init_ipc() { ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); @@ -46,91 +58,150 @@ void init_ipc() { int ipc_handle_connection(int fd, uint32_t mask, void *data) { sway_log(L_DEBUG, "Event on IPC listening socket"); - int client_socket = accept(ipc_socket, NULL, NULL); - if (client_socket == -1) { - char error[256]; - strerror_r(errno, error, sizeof(error)); - sway_log(L_INFO, "Unable to accept IPC client connection: %s", error); - return 0; - } + assert(mask == WLC_EVENT_READABLE); - char buf[1024]; - // Leave one byte of space at the end of the buffer for NULL terminator - ssize_t received = recv(client_socket, buf, sizeof(buf) - 1, 0); - if (received == -1) { - char error[256]; - strerror_r(errno, error, sizeof(error)); - sway_log(L_INFO, "Unable to receive from IPC client: %s", error); - close(client_socket); + int client_fd = accept(ipc_socket, NULL, NULL); + if (client_fd == -1) { + sway_log_errno(L_INFO, "Unable to accept IPC client connection"); return 0; } - char *reply_buf; - size_t reply_length = ipc_handle_command(&reply_buf, buf, received); - sway_log(L_DEBUG, "IPC reply: %s", reply_buf); + struct ipc_client* client = malloc(sizeof(struct ipc_client)); + client->payload_length = 0; + client->fd = client_fd; + client->event_source = wlc_event_loop_add_fd(client_fd, WLC_EVENT_READABLE, ipc_client_handle_readable, client); - if (send(client_socket, reply_buf, reply_length, 0) == -1) { - char error[256]; - strerror_r(errno, error, sizeof(error)); - sway_log(L_INFO, "Unable to send to IPC client: %s", error); - } - - free(reply_buf); - close(client_socket); return 0; } static const int ipc_header_size = sizeof(ipc_magic)+8; -size_t ipc_handle_command(char **reply_data, char *data, ssize_t length) { - // See https://i3wm.org/docs/ipc.html for protocol details +int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { + struct ipc_client *client = data; + sway_log(L_DEBUG, "Event on IPC client socket %d", client_fd); - if (length < ipc_header_size) { - sway_log(L_DEBUG, "IPC data too short"); - return false; + if (mask & WLC_EVENT_ERROR) { + sway_log(L_INFO, "IPC Client socket error, removing client"); + ipc_client_disconnect(client); + return 0; + } + + if (mask & WLC_EVENT_HANGUP) { + ipc_client_disconnect(client); + return 0; } - if (memcmp(data, ipc_magic, sizeof(ipc_magic)) != 0) { + int read_available; + ioctl(client_fd, FIONREAD, &read_available); + + // Wait for the rest of the command payload in case the header has already been read + if (client->payload_length > 0) { + if (read_available >= client->payload_length) { + ipc_client_handle_command(client); + } + else { + sway_log(L_DEBUG, "Too little data to read payload on IPC Client socket, waiting for more (%d < %d)", read_available, client->payload_length); + } + return 0; + } + + if (read_available < ipc_header_size) { + sway_log(L_DEBUG, "Too little data to read header on IPC Client socket, waiting for more (%d < %d)", read_available, ipc_header_size); + return 0; + } + + char buf[ipc_header_size]; + ssize_t received = recv(client_fd, buf, ipc_header_size, 0); + if (received == -1) { + sway_log_errno(L_INFO, "Unable to receive header from IPC client"); + ipc_client_disconnect(client); + return 0; + } + + if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { sway_log(L_DEBUG, "IPC header check failed"); - return false; + ipc_client_disconnect(client); + return 0; } - uint32_t payload_length = *(uint32_t *)&data[sizeof(ipc_magic)]; - uint32_t command_type = *(uint32_t *)&data[sizeof(ipc_magic)+4]; + client->payload_length = *(uint32_t *)&buf[sizeof(ipc_magic)]; + client->current_command = (enum ipc_command_type) *(uint32_t *)&buf[sizeof(ipc_magic)+4]; - if (length != payload_length + ipc_header_size) { - // TODO: try to read enough data - sway_log(L_DEBUG, "IPC payload size mismatch"); - return false; + if (read_available - received >= client->payload_length) { + ipc_client_handle_command(client); + } + + return 0; +} + +void ipc_client_disconnect(struct ipc_client *client) +{ + if (!sway_assert(client != NULL, "client != NULL")) { + return; + } + + sway_log(L_INFO, "IPC Client %d disconnected", client->fd); + wlc_event_source_remove(client->event_source); + close(client->fd); + free(client); +} + +void ipc_client_handle_command(struct ipc_client *client) { + if (!sway_assert(client != NULL, "client != NULL")) { + return; + } + + char buf[client->payload_length + 1]; + if (client->payload_length > 0) + { + ssize_t received = recv(client->fd, buf, client->payload_length, 0); + if (received == -1) + { + sway_log_errno(L_INFO, "Unable to receive payload from IPC client"); + ipc_client_disconnect(client); + return; + } } - switch (command_type) { + switch (client->current_command) { case IPC_COMMAND: { - char *cmd = &data[ipc_header_size]; - data[ipc_header_size + payload_length] = '\0'; - bool success = handle_command(config, cmd); - char buf[64]; - int length = snprintf(buf, sizeof(buf), "{\"success\":%s}", success ? "true" : "false"); - return ipc_format_reply(reply_data, IPC_COMMAND, buf, (uint32_t) length); + buf[client->payload_length] = '\0'; + bool success = handle_command(config, buf); + char reply[64]; + int length = snprintf(reply, sizeof(reply), "{\"success\":%s}", success ? "true" : "false"); + ipc_send_reply(client, reply, (uint32_t) length); + break; } default: - sway_log(L_INFO, "Unknown IPC command type %i", command_type); - return false; + sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); + ipc_client_disconnect(client); + break; } + + client->payload_length = 0; } -size_t ipc_format_reply(char **data, enum ipc_command_type command_type, const char *payload, uint32_t payload_length) { - assert(data); +bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { assert(payload); - size_t length = ipc_header_size + payload_length; - *data = malloc(length); + char data[ipc_header_size]; - memcpy(*data, ipc_magic, sizeof(ipc_magic)); - *(uint32_t *)&((*data)[sizeof(ipc_magic)]) = payload_length; - *(uint32_t *)&((*data)[sizeof(ipc_magic)+4]) = command_type; - memcpy(&(*data)[ipc_header_size], payload, payload_length); + memcpy(data, ipc_magic, sizeof(ipc_magic)); + *(uint32_t *)&(data[sizeof(ipc_magic)]) = payload_length; + *(uint32_t *)&(data[sizeof(ipc_magic)+4]) = client->current_command; + + if (write(client->fd, data, ipc_header_size) == -1) { + sway_log_errno(L_INFO, "Unable to send header to IPC client"); + ipc_client_disconnect(client); + return false; + } + + if (write(client->fd, payload, payload_length) == -1) { + sway_log_errno(L_INFO, "Unable to send payload to IPC client"); + ipc_client_disconnect(client); + return false; + } - return length; + return true; } diff --git a/sway/log.c b/sway/log.c index 2d633abb..e8c1b78f 100644 --- a/sway/log.c +++ b/sway/log.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include int colored = 1; int v = 0; @@ -66,6 +68,34 @@ void sway_log(int verbosity, const char* format, ...) { } } +void sway_log_errno(int verbosity, char* format, ...) { + if (verbosity <= v) { + int c = verbosity; + if (c > sizeof(verbosity_colors) / sizeof(char *)) { + c = sizeof(verbosity_colors) / sizeof(char *) - 1; + } + + if (colored) { + fprintf(stderr, verbosity_colors[c]); + } + + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + + fprintf(stderr, ": "); + char error[256]; + strerror_r(errno, error, sizeof(error)); + fprintf(stderr, error); + + if (colored) { + fprintf(stderr, "\x1B[0m"); + } + fprintf(stderr, "\n"); + } +} + bool sway_assert(bool condition, const char* format, ...) { if (condition) { return true; -- cgit v1.2.3-70-g09d2