From 774e594714579699a5f73fd110007ebb5a60cec6 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Tue, 18 Aug 2015 21:41:16 -0500 Subject: Fixed full screen workspace switch focusing --- sway/workspace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sway/workspace.c') diff --git a/sway/workspace.c b/sway/workspace.c index 05a669fe..60108752 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -178,9 +178,9 @@ void workspace_switch(swayc_t *workspace) { return; } sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); + active_workspace = workspace; set_focused_container(get_focused_view(workspace)); arrange_windows(workspace, -1, -1); - active_workspace = workspace; } /* XXX:DEBUG:XXX */ -- cgit v1.2.3 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 'sway/workspace.c') 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 From 95517ac77ebedb6e07011affd9520159acf38376 Mon Sep 17 00:00:00 2001 From: Syed Amer Gilani Date: Wed, 19 Aug 2015 11:27:48 +0200 Subject: fix a few possible memory leaks --- sway/commands.c | 4 ++++ sway/config.c | 6 ++++++ sway/workspace.c | 15 ++++++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) (limited to 'sway/workspace.c') diff --git a/sway/commands.c b/sway/commands.c index 42d6b173..ab24f6ae 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -105,6 +105,10 @@ static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE); if (!sym) { sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]); + list_free(binding->keys); + free(binding->command); + free(binding); + list_free(split); return false; } xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); diff --git a/sway/config.c b/sway/config.c index be9f70c1..3a7f3904 100644 --- a/sway/config.c +++ b/sway/config.c @@ -28,6 +28,7 @@ static char *get_config_path() { if (exists(temp)) { return temp; } + free(temp); // Check XDG_CONFIG_HOME with fallback to ~/.config/ sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config"); @@ -54,6 +55,7 @@ static char *get_config_path() { if (exists(temp)) { return temp; } + free(temp); // Check XDG_CONFIG_DIRS sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); @@ -70,6 +72,7 @@ static char *get_config_path() { free_flat_list(paths); return temp; } + free(temp); } free_flat_list(paths); } @@ -83,6 +86,7 @@ static char *get_config_path() { if (exists(temp)) { return temp; } + free(temp); sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config"); if (xdg_config_home == NULL) { @@ -106,6 +110,7 @@ static char *get_config_path() { if (exists(temp)) { return temp; } + free(temp); sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); if (xdg_config_dirs != NULL) { @@ -120,6 +125,7 @@ static char *get_config_path() { free_flat_list(paths); return temp; } + free(temp); } free_flat_list(paths); } diff --git a/sway/workspace.c b/sway/workspace.c index 60108752..180c8a66 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -31,7 +31,7 @@ char *workspace_next_name(void) { char* target = malloc(strlen(args->items[1]) + 1); strcpy(target, args->items[1]); while (*target == ' ' || *target == '\t') - target++; + target++; // Make sure that the command references an actual workspace // not a command about workspaces @@ -42,11 +42,15 @@ char *workspace_next_name(void) { strcmp(target, "number") == 0 || strcmp(target, "back_and_forth") == 0 || strcmp(target, "current") == 0) + { + list_free(args); continue; - - //Make sure that the workspace doesn't already exist + } + + //Make sure that the workspace doesn't already exist if (workspace_find_by_name(target)) { - continue; + list_free(args); + continue; } list_free(args); @@ -54,6 +58,7 @@ char *workspace_next_name(void) { sway_log(L_DEBUG, "Workspace: Found free name %s", target); return target; } + list_free(args); } // As a fall back, get the current number of active workspaces // and return that + 1 for the next workspace's name @@ -77,7 +82,7 @@ swayc_t *workspace_create(const char* name) { } bool workspace_by_name(swayc_t *view, void *data) { - return (view->type == C_WORKSPACE) && + return (view->type == C_WORKSPACE) && (strcasecmp(view->name, (char *) data) == 0); } -- cgit v1.2.3 From a31f23f90c7259f48dcd02eaef08449a226460cd Mon Sep 17 00:00:00 2001 From: taiyu Date: Wed, 19 Aug 2015 15:22:55 -0700 Subject: fixed active_workspace update on focus change --- sway/focus.c | 2 ++ sway/handlers.c | 11 +++++++++-- sway/workspace.c | 1 - 3 files changed, 11 insertions(+), 3 deletions(-) (limited to 'sway/workspace.c') diff --git a/sway/focus.c b/sway/focus.c index 7023d37d..f76b2d9a 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -20,6 +20,8 @@ static void update_focus(swayc_t *c) { // Case where output changes case C_OUTPUT: wlc_output_focus(c->handle); + //Set new workspace to the outputs focused workspace + active_workspace = c->focused; break; // Case where workspace changes diff --git a/sway/handlers.c b/sway/handlers.c index 344edd07..24189003 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -179,8 +179,14 @@ static bool handle_view_created(wlc_handle handle) { if (!focused || focused->type == C_OUTPUT) { focused = get_focused_container(&root_container); } - sway_log(L_DEBUG, "creating view %ld with type %x, state %x, with parent %ld", - handle, wlc_view_get_type(handle), wlc_view_get_state(handle), parent); + sway_log(L_DEBUG, "handle:%ld type:%x state:%x parent:%ld " + "mask:%d (x:%d y:%d w:%d h:%d) title:%s " + "class:%s appid:%s", + handle, wlc_view_get_type(handle), wlc_view_get_state(handle), parent, + wlc_view_get_mask(handle), wlc_view_get_geometry(handle)->origin.x, + wlc_view_get_geometry(handle)->origin.y,wlc_view_get_geometry(handle)->size.w, + wlc_view_get_geometry(handle)->size.h, wlc_view_get_title(handle), + wlc_view_get_class(handle), wlc_view_get_app_id(handle)); // TODO properly figure out how each window should be handled. switch (wlc_view_get_type(handle)) { @@ -319,6 +325,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier sway_log(L_DEBUG, "modifier %x: state %d: key %d, sym: %d", modifiers->mods, state, key, sym); + //Revert floating container back to original position on keypress if (state == WLC_KEY_STATE_PRESSED && (dragging || resizing)) { reset_floating(get_focused_view(&root_container)); } diff --git a/sway/workspace.c b/sway/workspace.c index a690e3ae..ec60c8e0 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -183,7 +183,6 @@ void workspace_switch(swayc_t *workspace) { return; } sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); - active_workspace = workspace; set_focused_container(get_focused_view(workspace)); arrange_windows(workspace, -1, -1); } -- cgit v1.2.3 From f5fde7c45c04b02406eabc34cbb4248189c6a26e Mon Sep 17 00:00:00 2001 From: taiyu Date: Thu, 20 Aug 2015 05:06:22 -0700 Subject: style --- include/container.h | 4 ++-- sway/commands.c | 20 ++++++++++---------- sway/container.c | 4 ++-- sway/focus.c | 4 ++-- sway/handlers.c | 14 +++++++------- sway/input_state.c | 2 +- sway/layout.c | 12 ++++++------ sway/log.c | 8 ++++---- sway/workspace.c | 2 +- 9 files changed, 35 insertions(+), 35 deletions(-) (limited to 'sway/workspace.c') diff --git a/include/container.h b/include/container.h index 4f1877e3..bd92058d 100644 --- a/include/container.h +++ b/include/container.h @@ -11,7 +11,7 @@ enum swayc_types{ C_WORKSPACE, C_CONTAINER, C_VIEW, - //Keep last + // Keep last C_TYPES, }; @@ -22,7 +22,7 @@ enum swayc_layouts{ L_STACKED, L_TABBED, L_FLOATING, - //Keep last + // Keep last L_LAYOUTS, }; diff --git a/sway/commands.c b/sway/commands.c index babefd02..a3f74747 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -81,7 +81,7 @@ static int bindsym_sort(const void *_lbind, const void *_rbind) { const struct sway_binding *rbind = *(void **)_rbind; unsigned int lmod = 0, rmod = 0, i; - //Count how any modifiers are pressed + // Count how any modifiers are pressed for (i = 0; i < 8 * sizeof(lbind->modifiers); ++i) { lmod += lbind->modifiers & 1 << i; rmod += rbind->modifiers & 1 << i; @@ -203,10 +203,10 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { } // Change from nonfloating to floating if (!view->is_floating) { - //Remove view from its current location + // Remove view from its current location destroy_container(remove_child(view)); - //and move it into workspace floating + // 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; @@ -233,11 +233,11 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { sway_log(L_DEBUG, "Non-floating focused container is %p", focused); - //Case of focused workspace, just create as child of it + // Case of focused workspace, just create as child of it if (focused->type == C_WORKSPACE) { add_child(focused, view); } - //Regular case, create as sibling of current container + // Regular case, create as sibling of current container else { add_sibling(focused, view); } @@ -258,7 +258,7 @@ static bool cmd_floating_mod(struct sway_config *config, int argc, char **argv) list_t *split = split_string(argv[0], "+"); config->floating_mod = 0; - //set modifer keys + // set modifer keys for (i = 0; i < split->length; ++i) { for (j = 0; j < sizeof(modifiers) / sizeof(struct modifier_key); ++j) { if (strcasecmp(modifiers[j].name, split->items[i]) == 0) { @@ -508,14 +508,14 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { swayc_t *container = get_focused_view(&root_container); bool current = (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) > 0; wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current); - //Resize workspace if going from fullscreen -> notfullscreen - //otherwise just resize container + // Resize workspace if going from fullscreen -> notfullscreen + // otherwise just resize container if (current) { while (container->type != C_WORKSPACE) { container = container->parent; } } - //Only resize container when going into fullscreen + // Only resize container when going into fullscreen arrange_windows(container, -1, -1); return true; @@ -679,7 +679,7 @@ bool handle_command(struct sway_config *config, char *exec) { char **argv = split_directive(exec + strlen(handler->command), &argc); int i; - //Perform var subs on all parts of the command + // Perform var subs on all parts of the command for (i = 0; i < argc; ++i) { argv[i] = do_var_replacement(config, argv[i]); } diff --git a/sway/container.c b/sway/container.c index 59dc9e62..7da33b48 100644 --- a/sway/container.c +++ b/sway/container.c @@ -171,7 +171,7 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { view->name = title ? strdup(title) : NULL; view->visible = true; view->is_focused = true; - //Setup geometry + // Setup geometry view->width = 0; view->height = 0; @@ -205,7 +205,7 @@ swayc_t *new_floating_view(wlc_handle handle) { // Set the geometry of the floating view const struct wlc_geometry* geometry = wlc_view_get_geometry(handle); - //give it requested geometry, but place in center + // give it requested geometry, but place in center view->x = (active_workspace->width - geometry->size.w) / 2; view->y = (active_workspace->height- geometry->size.h) / 2; view->width = geometry->size.w; diff --git a/sway/focus.c b/sway/focus.c index a6ffe73f..5008dbbf 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -21,7 +21,7 @@ static void update_focus(swayc_t *c) { // Case where output changes case C_OUTPUT: wlc_output_focus(c->handle); - //Set new workspace to the outputs focused workspace + // Set new workspace to the outputs focused workspace active_workspace = c->focused; break; @@ -118,7 +118,7 @@ void set_focused_container(swayc_t *c) { // activate current focus if (p->type == C_VIEW) { wlc_view_set_state(p->handle, WLC_BIT_ACTIVATED, true); - //set focus if view_focus is unlocked + // set focus if view_focus is unlocked if (!locked_view_focus) { wlc_view_focus(p->handle); } diff --git a/sway/handlers.c b/sway/handlers.c index 03a32835..2f062911 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -87,7 +87,7 @@ swayc_t *container_under_pointer(void) { static bool handle_output_created(wlc_handle output) { swayc_t *op = new_output(output); - //Switch to workspace if we need to + // Switch to workspace if we need to if (active_workspace == NULL) { swayc_t *ws = op->children->items[0]; workspace_switch(ws); @@ -109,7 +109,7 @@ static void handle_output_destroyed(wlc_handle output) { if (list->length == 0) { active_workspace = NULL; } else { - //switch to other outputs active workspace + // switch to other outputs active workspace workspace_switch(((swayc_t *)root_container.children->items[0])->focused); } } @@ -167,7 +167,7 @@ static bool handle_view_created(wlc_handle handle) { // Dmenu keeps viewfocus, but others with this flag dont, for now simulate // dmenu case WLC_BIT_OVERRIDE_REDIRECT: -// locked_view_focus = true; +// locked_view_focus = true; wlc_view_focus(handle); wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); wlc_view_bring_to_front(handle); @@ -217,7 +217,7 @@ static void handle_view_destroyed(wlc_handle handle) { // DMENU has this flag, and takes view_focus, but other things with this // flag dont case WLC_BIT_OVERRIDE_REDIRECT: -// locked_view_focus = false; +// locked_view_focus = false; break; case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED: locked_container_focus = false; @@ -444,7 +444,7 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct } } if (config->focus_follows_mouse && prev_handle != handle) { - //Dont change focus if fullscreen + // Dont change focus if fullscreen swayc_t *focused = get_focused_view(view); if (!(focused->type == C_VIEW && wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) && !(pointer_state.l_held || pointer_state.r_held)) { @@ -474,7 +474,7 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, uint32_t button, enum wlc_button_state state, const struct wlc_origin *origin) { swayc_t *focused = get_focused_container(&root_container); - //dont change focus if fullscreen + // dont change focus if fullscreen if (focused->type == C_VIEW && wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) { return false; } @@ -510,7 +510,7 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w pointer_state.lock.left = !pointer_state.lock.right; start_floating(pointer); } - //Dont want pointer sent to window while dragging or resizing + // Dont want pointer sent to window while dragging or resizing return (pointer_state.floating.drag || pointer_state.floating.resize); } return (pointer && pointer != focused); diff --git a/sway/input_state.c b/sway/input_state.c index 51213b19..a7f88d4a 100644 --- a/sway/input_state.c +++ b/sway/input_state.c @@ -36,7 +36,7 @@ void press_key(keycode key) { void release_key(keycode key) { uint8_t index = find_key(key); if (index < KEY_STATE_MAX_LENGTH) { - //shift it over and remove key + // shift it over and remove key key_state_array[index] = 0; } } diff --git a/sway/layout.c b/sway/layout.c index 192f09cf..4a9aa907 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -33,7 +33,7 @@ void add_child(swayc_t *parent, swayc_t *child) { child->width, child->height, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); child->parent = parent; - //set focus for this container + // set focus for this container if (parent->children->length == 1) { set_focused_container_for(parent, child); } @@ -97,7 +97,7 @@ swayc_t *remove_child(swayc_t *child) { } } } - //Set focused to new container + // Set focused to new container if (parent->focused == child) { if (parent->children->length > 0) { set_focused_container_for(parent, parent->children->items[i?i-1:0]); @@ -191,7 +191,7 @@ void arrange_windows(swayc_t *container, int width, int height) { switch (container->layout) { case L_HORIZ: default: - //Calculate total width + // Calculate total width for (i = 0; i < container->children->length; ++i) { int *old_width = &((swayc_t *)container->children->items[i])->width; if (*old_width <= 0) { @@ -203,7 +203,7 @@ void arrange_windows(swayc_t *container, int width, int height) { } scale += *old_width; } - //Resize windows + // Resize windows if (scale > 0.1) { scale = width / scale; sway_log(L_DEBUG, "Arranging %p horizontally", container); @@ -218,7 +218,7 @@ void arrange_windows(swayc_t *container, int width, int height) { } break; case L_VERT: - //Calculate total height + // Calculate total height for (i = 0; i < container->children->length; ++i) { int *old_height = &((swayc_t *)container->children->items[i])->height; if (*old_height <= 0) { @@ -230,7 +230,7 @@ void arrange_windows(swayc_t *container, int width, int height) { } scale += *old_height; } - //Resize + // Resize if (scale > 0.1) { scale = height / scale; sway_log(L_DEBUG, "Arranging %p vertically", container); diff --git a/sway/log.c b/sway/log.c index 49a74e02..2d633abb 100644 --- a/sway/log.c +++ b/sway/log.c @@ -88,10 +88,10 @@ bool sway_assert(bool condition, const char* format, ...) { /* 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 + 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); diff --git a/sway/workspace.c b/sway/workspace.c index ec60c8e0..0f44d3b0 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -47,7 +47,7 @@ char *workspace_next_name(void) { continue; } - //Make sure that the workspace doesn't already exist + // Make sure that the workspace doesn't already exist if (workspace_find_by_name(target)) { list_free(args); continue; -- cgit v1.2.3 From 36e07e9ebc55b3fc8a8b8cd76ee743202691ad56 Mon Sep 17 00:00:00 2001 From: taiyu Date: Thu, 20 Aug 2015 09:52:54 -0700 Subject: find_parent_by_type --- include/container.h | 11 +++++ sway/commands.c | 5 +- sway/container.c | 138 +++++++++++++++++++++++++++++++++++----------------- sway/handlers.c | 15 ++---- sway/layout.c | 10 +--- sway/workspace.c | 4 +- 6 files changed, 112 insertions(+), 71 deletions(-) (limited to 'sway/workspace.c') diff --git a/include/container.h b/include/container.h index bd92058d..fd621490 100644 --- a/include/container.h +++ b/include/container.h @@ -55,6 +55,7 @@ struct sway_container { struct sway_container *focused; }; +// Container Creation swayc_t *new_output(wlc_handle handle); swayc_t *new_workspace(swayc_t *output, const char *name); @@ -65,13 +66,23 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle); // Creates view as a new floating view which is in the active workspace swayc_t *new_floating_view(wlc_handle handle); +// Container Destroying swayc_t *destroy_output(swayc_t *output); // Destroys workspace if empty and returns parent pointer, else returns NULL swayc_t *destroy_workspace(swayc_t *workspace); +// Destroyes container and all parent container if they are empty, returns +// topmost non-empty parent. returns NULL otherwise swayc_t *destroy_container(swayc_t *container); +// Destroys view and all empty parent containers. return topmost non-empty +// parent swayc_t *destroy_view(swayc_t *view); +// Container Lookup + +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types); +swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts); + swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *); diff --git a/sway/commands.c b/sway/commands.c index 644b8005..3ac7f4dd 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -390,7 +390,6 @@ static bool cmd_layout(struct sway_config *config, int argc, char **argv) { return false; } swayc_t *parent = get_focused_container(&root_container); - while (parent->type == C_VIEW) { parent = parent->parent; } @@ -512,9 +511,7 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { // Resize workspace if going from fullscreen -> notfullscreen // otherwise just resize container if (current) { - while (container->type != C_WORKSPACE) { - container = container->parent; - } + container = swayc_parent_by_type(container, C_WORKSPACE); } // Only resize container when going into fullscreen arrange_windows(container, -1, -1); diff --git a/sway/container.c b/sway/container.c index 4559a5f5..7ccc2e09 100644 --- a/sway/container.c +++ b/sway/container.c @@ -8,6 +8,8 @@ #include "layout.h" #include "log.h" +#define ASSERT_NONNULL(PTR) \ + sway_assert (PTR, "%s: " #PTR "must be non-null", __func__) static swayc_t *new_swayc(enum swayc_types type) { swayc_t *c = calloc(1, sizeof(swayc_t)); @@ -20,37 +22,40 @@ static swayc_t *new_swayc(enum swayc_types type) { return c; } -static void free_swayc(swayc_t *c) { +static void free_swayc(swayc_t *cont) { + if (!ASSERT_NONNULL(cont)) { + return; + } // TODO does not properly handle containers with children, // TODO but functions that call this usually check for that - if (c->children) { - if (c->children->length) { + if (cont->children) { + if (cont->children->length) { int i; - for (i = 0; i < c->children->length; ++i) { - free_swayc(c->children->items[i]); + for (i = 0; i < cont->children->length; ++i) { + free_swayc(cont->children->items[i]); } } - list_free(c->children); + list_free(cont->children); } - if (c->floating) { - if (c->floating->length) { + if (cont->floating) { + if (cont->floating->length) { int i; - for (i = 0; i < c->floating->length; ++i) { - free_swayc(c->floating->items[i]); + for (i = 0; i < cont->floating->length; ++i) { + free_swayc(cont->floating->items[i]); } } - list_free(c->floating); + list_free(cont->floating); } - if (c->parent) { - remove_child(c); + if (cont->parent) { + remove_child(cont); } - if (c->name) { - free(c->name); + if (cont->name) { + free(cont->name); } - free(c); + free(cont); } -/* New containers */ +// New containers static bool workspace_test(swayc_t *view, void *name) { return strcasecmp(view->name, (char *)name) == 0; @@ -103,6 +108,9 @@ swayc_t *new_output(wlc_handle handle) { } swayc_t *new_workspace(swayc_t *output, const char *name) { + if (!ASSERT_NONNULL(output)) { + return NULL; + } sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle); swayc_t *workspace = new_swayc(C_WORKSPACE); @@ -120,6 +128,9 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { } swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { + if (!ASSERT_NONNULL(child)) { + return NULL; + } swayc_t *cont = new_swayc(C_CONTAINER); sway_log(L_DEBUG, "creating container %p around %p", cont, child); @@ -162,6 +173,9 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { } swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { + if (!ASSERT_NONNULL(sibling)) { + return NULL; + } const char *title = wlc_view_get_title(handle); swayc_t *view = new_swayc(C_VIEW); sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d", @@ -172,14 +186,14 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { view->visible = true; view->is_focused = true; // Setup geometry + const struct wlc_geometry* geometry = wlc_view_get_geometry(handle); view->width = 0; view->height = 0; + view->desired_width = geometry->size.w; + view->desired_height = geometry->size.h; view->gaps = config->gaps_inner; - view->desired_width = -1; - view->desired_height = -1; - view->is_floating = false; if (sibling->type == C_WORKSPACE) { @@ -225,9 +239,12 @@ swayc_t *new_floating_view(wlc_handle handle) { return view; } -/* Destroy container */ +// Destroy container swayc_t *destroy_output(swayc_t *output) { + if (!ASSERT_NONNULL(output)) { + return NULL; + } if (output->children->length == 0) { // TODO move workspaces to other outputs } @@ -237,23 +254,21 @@ swayc_t *destroy_output(swayc_t *output) { } swayc_t *destroy_workspace(swayc_t *workspace) { + if (!ASSERT_NONNULL(workspace)) { + return NULL; + } // NOTE: This is called from elsewhere without checking children length // TODO move containers to other workspaces? // for now just dont delete // Do not destroy this if it's the last workspace on this output - swayc_t *output = workspace->parent; - while (output && output->type != C_OUTPUT) { - output = output->parent; - } - if (output) { - if (output->children->length == 1) { - return NULL; - } + swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT); + if (output && output->children->length == 1) { + return NULL; } if (workspace->children->length == 0) { - sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name); + sway_log(L_DEBUG, "%s: '%s'", __func__, workspace->name); swayc_t *parent = workspace->parent; free_swayc(workspace); return parent; @@ -262,6 +277,9 @@ swayc_t *destroy_workspace(swayc_t *workspace) { } swayc_t *destroy_container(swayc_t *container) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } while (container->children->length == 0 && container->type == C_CONTAINER) { sway_log(L_DEBUG, "Container: Destroying container '%p'", container); swayc_t *parent = container->parent; @@ -272,8 +290,7 @@ swayc_t *destroy_container(swayc_t *container) { } swayc_t *destroy_view(swayc_t *view) { - if (view == NULL) { - sway_log(L_DEBUG, "Warning: NULL passed into destroy_view"); + if (!ASSERT_NONNULL(view)) { return NULL; } sway_log(L_DEBUG, "Destroying view '%p'", view); @@ -287,6 +304,34 @@ swayc_t *destroy_view(swayc_t *view) { return parent; } +// Container lookup + +swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(type < C_TYPES && type >= C_ROOT, "%s: invalid type", __func__)) { + return NULL; + } + do { + container = container->parent; + } while(container && container->type != type); + return container; +} + +swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "%s: invalid layout", __func__)) { + return NULL; + } + do { + container = container->parent; + } while (container && container->layout != layout); + return container; +} + swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { if (!container->children) { return NULL; @@ -316,25 +361,27 @@ swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *da } void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { - if (!container || !container->children || !container->children->length) { - return; - } - int i; - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - f(child, data); - container_map(child, f, data); - } - if (container->type == C_WORKSPACE) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; + if (container && container->children && container->children->length) { + int i; + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; f(child, data); container_map(child, f, data); } + if (container->type == C_WORKSPACE) { + for (i = 0; i < container->floating->length; ++i) { + swayc_t *child = container->floating->items[i]; + f(child, data); + container_map(child, f, data); + } + } } } void set_view_visibility(swayc_t *view, void *data) { + if (!ASSERT_NONNULL(view)) { + return; + } uint32_t *p = data; if (view->type == C_VIEW) { wlc_view_set_mask(view->handle, *p); @@ -348,6 +395,9 @@ void set_view_visibility(swayc_t *view, void *data) { } void reset_gaps(swayc_t *view, void *data) { + if (!ASSERT_NONNULL(view)) { + return; + } if (view->type == C_OUTPUT) { view->gaps = config->gaps_outer; } diff --git a/sway/handlers.c b/sway/handlers.c index 5993223d..79628fe5 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -20,10 +20,7 @@ static struct wlc_origin mouse_origin; static bool pointer_test(swayc_t *view, void *_origin) { const struct wlc_origin *origin = _origin; // Determine the output that the view is under - swayc_t *parent = view; - while (parent->type != C_OUTPUT) { - parent = parent->parent; - } + swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT); if (origin->x >= view->x && origin->y >= view->y && origin->x < view->x + view->width && origin->y < view->y + view->height && view->visible && parent == root_container.focused) { @@ -191,10 +188,7 @@ static bool handle_view_created(wlc_handle handle) { if (newview) { set_focused_container(newview); - swayc_t *output = newview->parent; - while (output && output->type != C_OUTPUT) { - output = output->parent; - } + swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT); arrange_windows(output, -1, -1); } return true; @@ -262,10 +256,7 @@ static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit s arrange_windows(c->parent, -1, -1); // Set it as focused window for that workspace if its going fullscreen if (toggle) { - swayc_t *ws = c; - while (ws->type != C_WORKSPACE) { - ws = ws->parent; - } + swayc_t *ws = swayc_parent_by_type(c, C_WORKSPACE); // Set ws focus to c set_focused_container_for(ws, c); } diff --git a/sway/layout.c b/sway/layout.c index 78b3dd27..8c011fdb 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -161,10 +161,7 @@ void arrange_windows(swayc_t *container, int width, int height) { } }; if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) { - swayc_t *parent = container; - while (parent->type != C_OUTPUT) { - parent = parent->parent; - } + swayc_t *parent = swayc_parent_by_type(container, C_OUTPUT); geometry.origin.x = 0; geometry.origin.y = 0; geometry.size.w = parent->width; @@ -263,10 +260,7 @@ void arrange_windows(swayc_t *container, int width, int height) { } }; if (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN) { - swayc_t *parent = view; - while (parent->type != C_OUTPUT) { - parent = parent->parent; - } + swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT); geometry.origin.x = 0; geometry.origin.y = 0; geometry.size.w = parent->width; diff --git a/sway/workspace.c b/sway/workspace.c index 0f44d3b0..d436da8e 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -75,9 +75,7 @@ char *workspace_next_name(void) { swayc_t *workspace_create(const char* name) { swayc_t *parent = get_focused_container(&root_container); - while (parent->type != C_OUTPUT) { - parent = parent->parent; - } + parent = swayc_parent_by_type(parent, C_OUTPUT); return new_workspace(parent, name); } -- cgit v1.2.3