From 083d1eed1f61f8cc7397031f1ab987022ba6868e Mon Sep 17 00:00:00 2001 From: taiyu Date: Sat, 15 Aug 2015 21:21:20 -0700 Subject: fixed 2 small memory leaks & adds format attribute to log. --- sway/stringop.c | 1 + 1 file changed, 1 insertion(+) (limited to 'sway/stringop.c') 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; -- cgit v1.2.3 From e9e09d123ce1e82cb1680e499a6c349241ada9e6 Mon Sep 17 00:00:00 2001 From: taiyu Date: Sat, 15 Aug 2015 22:11:19 -0700 Subject: reduced code duplication --- sway/stringop.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'sway/stringop.c') 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; -- cgit v1.2.3 From eefc76b39e61ce04fa8ac0e732a22c10d9c98e1f Mon Sep 17 00:00:00 2001 From: taiyu Date: Sun, 16 Aug 2015 00:46:58 -0700 Subject: 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(-) (limited to 'sway/stringop.c') 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; -- cgit v1.2.3