From 083d1eed1f61f8cc7397031f1ab987022ba6868e Mon Sep 17 00:00:00 2001 From: taiyu Date: Sat, 15 Aug 2015 21:21:20 -0700 Subject: [PATCH 1/3] fixed 2 small memory leaks & adds format attribute to log. --- sway/container.c | 3 +++ sway/handlers.c | 22 +++++++++++----------- sway/layout.c | 6 +++--- sway/log.h | 9 +++++++-- sway/stringop.c | 1 + sway/workspace.c | 2 +- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/sway/container.c b/sway/container.c index 98941b2c..d1c8a7de 100644 --- a/sway/container.c +++ b/sway/container.c @@ -31,6 +31,9 @@ static void free_swayc(swayc_t *c) { } remove_child(c->parent, c); } + if (c->name) { + free(c->name); + } free(c); } diff --git a/sway/handlers.c b/sway/handlers.c index 9944be33..6f4cb477 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -88,22 +88,22 @@ static void handle_output_focused(wlc_handle output, bool focus) { static bool handle_view_created(wlc_handle handle) { swayc_t *focused = get_focused_container(&root_container); swayc_t *view = new_view(focused, handle); + //Leave unamanaged windows alone if (view) { - //Set maximize flag for windows. - //TODO: floating windows have this unset - wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); - unfocus_all(&root_container); - focus_view(view); arrange_windows(view->parent, -1, -1); - } else { //Unmanaged view + wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); + if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) { + unfocus_all(&root_container); + focus_view(view); + } + else { + wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); + wlc_view_focus(handle); + } + } else { wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); wlc_view_focus(handle); } - if (wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) { - unfocus_all(&root_container); - focus_view(focused); - arrange_windows(focused, -1, -1); - } return true; } diff --git a/sway/layout.c b/sway/layout.c index 918da9f0..8cf88be3 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -215,10 +215,10 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) { } swayc_t *get_focused_container(swayc_t *parent) { - if (parent->focused == NULL) { - return parent; + while (parent->focused) { + parent = parent->focused; } - return get_focused_container(parent->focused); + return parent; } void unfocus_all(swayc_t *container) { diff --git a/sway/log.h b/sway/log.h index e5075a39..3e8c55f0 100644 --- a/sway/log.h +++ b/sway/log.h @@ -1,6 +1,10 @@ #ifndef _SWAY_LOG_H #define _SWAY_LOG_H +#ifndef __GNUC__ +# define __attribute__(x) +#endif + typedef enum { L_SILENT = 0, L_ERROR = 1, @@ -8,9 +12,10 @@ typedef enum { L_DEBUG = 3, } log_importance_t; + void init_log(int verbosity); void sway_log_colors(int mode); -void sway_log(int verbosity, char* format, ...); -void sway_abort(char* format, ...); +void sway_log(int verbosity, char* format, ...)__attribute__((format (printf,2,3))); +void sway_abort(char* format, ...) __attribute__((format (printf,1,2))); #endif diff --git a/sway/stringop.c b/sway/stringop.c index bbc0bcdf..b944a43d 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -67,6 +67,7 @@ list_t *split_string(const char *str, const char *delims) { j++; i++; } + free(left); } } return res; diff --git a/sway/workspace.c b/sway/workspace.c index 49a1224f..a08877be 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -26,7 +26,7 @@ char *workspace_next_name(void) { list_t *args = split_string(command, " "); if (strcmp("workspace", args->items[0]) == 0 && args->length > 1) { - sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", args->items[1]); + sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", (char *)args->items[1]); char* target = malloc(strlen(args->items[1]) + 1); strcpy(target, args->items[1]); while (*target == ' ' || *target == '\t') From e9e09d123ce1e82cb1680e499a6c349241ada9e6 Mon Sep 17 00:00:00 2001 From: taiyu Date: Sat, 15 Aug 2015 22:11:19 -0700 Subject: [PATCH 2/3] reduced code duplication --- sway/stringop.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/sway/stringop.c b/sway/stringop.c index b944a43d..0f8577ae 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -53,8 +53,9 @@ char *strip_comments(char *str) { list_t *split_string(const char *str, const char *delims) { list_t *res = create_list(); int i, j; - for (i = 0, j = 0; i < strlen(str) + 1; ++i) { - if (strchr(delims, str[i]) || i == strlen(str)) { + int len = strlen(str); + for (i = 0, j = 0; i < len + 1; ++i) { + if (strchr(delims, str[i]) || i == len) { if (i - j == 0) { continue; } @@ -63,7 +64,7 @@ list_t *split_string(const char *str, const char *delims) { left[i - j] = 0; list_add(res, left); j = i + 1; - while (j <= strlen(str) && str[j] && strchr(delims, str[j])) { + while (j <= len && str[j] && strchr(delims, str[j])) { j++; i++; } @@ -111,40 +112,44 @@ int unescape_string(char *string) { for (i = 0; string[i]; ++i) { if (string[i] == '\\') { --len; + int shift = 0; switch (string[++i]) { case '0': string[i - 1] = '\0'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 'a': string[i - 1] = '\a'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 'b': string[i - 1] = '\b'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 't': string[i - 1] = '\t'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 'n': string[i - 1] = '\n'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 'v': string[i - 1] = '\v'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 'f': string[i - 1] = '\f'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; case 'r': string[i - 1] = '\r'; - memmove(string + i, string + i + 1, len - i); + shift = 1; break; } + if (shift) { + memmove(string + i, string + i + shift, len - i); + } } } return len; From eefc76b39e61ce04fa8ac0e732a22c10d9c98e1f Mon Sep 17 00:00:00 2001 From: taiyu Date: Sun, 16 Aug 2015 00:46:58 -0700 Subject: [PATCH 3/3] set userdata for handler to swayc_t container --- sway/container.c | 6 +++++- sway/handlers.c | 6 +++--- sway/stringop.c | 1 - 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/sway/container.c b/sway/container.c index d1c8a7de..ff9c983a 100644 --- a/sway/container.c +++ b/sway/container.c @@ -54,8 +54,10 @@ swayc_t *new_output(wlc_handle handle) { output->height = size->h; output->handle = handle; - add_child(&root_container, output); + //link this to handler + wlc_handle_set_user_data(handle, output); + add_child(&root_container, output); //TODO something with this int total_width = 0; container_map(&root_container, add_output_widths, &total_width); @@ -138,6 +140,8 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { view->name = strdup(title); view->visible = true; + //Link view to handle + wlc_handle_set_user_data(handle, view); //Case of focused workspace, just create as child of it if (sibling->type == C_WORKSPACE) { add_child(sibling, view); diff --git a/sway/handlers.c b/sway/handlers.c index 6f4cb477..af1fc98c 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -69,7 +69,7 @@ static void handle_output_destroyed(wlc_handle output) { static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { sway_log(L_DEBUG, "Output %d resolution changed to %d x %d", output, to->w, to->h); - swayc_t *c = get_swayc_for_handle(output, &root_container); + swayc_t *c = wlc_handle_get_user_data(output); if (!c) return; c->width = to->w; c->height = to->h; @@ -77,7 +77,7 @@ static void handle_output_resolution_change(wlc_handle output, const struct wlc_ } static void handle_output_focused(wlc_handle output, bool focus) { - swayc_t *c = get_swayc_for_handle(output, &root_container); + swayc_t *c = wlc_handle_get_user_data(output); if (!c) return; if (focus) { unfocus_all(&root_container); @@ -109,7 +109,7 @@ static bool handle_view_created(wlc_handle handle) { static void handle_view_destroyed(wlc_handle handle) { sway_log(L_DEBUG, "Destroying window %d", handle); - swayc_t *view = get_swayc_for_handle(handle, &root_container); + swayc_t *view = wlc_handle_get_user_data(handle); swayc_t *parent; swayc_t *focused = get_focused_container(&root_container); diff --git a/sway/stringop.c b/sway/stringop.c index 0f8577ae..450d5cd0 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -68,7 +68,6 @@ list_t *split_string(const char *str, const char *delims) { j++; i++; } - free(left); } } return res;