From 4c688cba4e3528921656d63a09f7015cae13cd0c Mon Sep 17 00:00:00 2001 From: KoffeinFlummi Date: Tue, 18 Aug 2015 23:53:57 +0200 Subject: Add support for gaps option --- include/container.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/container.h') diff --git a/include/container.h b/include/container.h index 186ee8b6..6d64b490 100644 --- a/include/container.h +++ b/include/container.h @@ -48,6 +48,8 @@ struct sway_container { char *name; + int gaps; + list_t *children; list_t *floating; -- cgit v1.2.3-54-g00ecf From 3c430688a8ffd35dc88ff23c4cb7a066a89fbd3f Mon Sep 17 00:00:00 2001 From: Luminarys Date: Tue, 18 Aug 2015 21:06:41 -0500 Subject: header update --- include/container.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include/container.h') diff --git a/include/container.h b/include/container.h index 6d64b490..76ddec7e 100644 --- a/include/container.h +++ b/include/container.h @@ -79,5 +79,6 @@ void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *); // Mappings void set_view_visibility(swayc_t *view, void *data); +void reset_gaps(swayc_t *view, void *data); #endif -- cgit v1.2.3-54-g00ecf From fbaa9111a8525daeef8a5534784da2f793917a36 Mon Sep 17 00:00:00 2001 From: taiyu Date: Thu, 20 Aug 2015 04:47:36 -0700 Subject: setup for resizable windows, drop weight --- include/container.h | 2 -- sway/commands.c | 1 + sway/container.c | 9 ++++++- sway/layout.c | 75 ++++++++++++++++++++++++++++++++++------------------- sway/log.c | 1 - 5 files changed, 58 insertions(+), 30 deletions(-) (limited to 'include/container.h') diff --git a/include/container.h b/include/container.h index 76ddec7e..4f1877e3 100644 --- a/include/container.h +++ b/include/container.h @@ -44,8 +44,6 @@ struct sway_container { bool is_floating; bool is_focused; - int weight; - char *name; int gaps; diff --git a/sway/commands.c b/sway/commands.c index c4cf96a2..babefd02 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -242,6 +242,7 @@ 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 + view->width = view->height = 0; arrange_windows(active_workspace, -1, -1); } set_focused_container(view); diff --git a/sway/container.c b/sway/container.c index 9c6b78e9..c9163784 100644 --- a/sway/container.c +++ b/sway/container.c @@ -14,7 +14,6 @@ static swayc_t *new_swayc(enum swayc_types type) { c->handle = -1; c->layout = L_NONE; c->type = type; - c->weight = 1; if (type != C_VIEW) { c->children = create_list(); } @@ -172,6 +171,14 @@ 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 + view->width = sibling->parent->width; + view->height = sibling->parent->height; + if (sibling->parent->layout == L_HORIZ) { + view->width /= sibling->parent->children->length; + } else { + view->height /= sibling->parent->children->length; + } view->gaps = config->gaps_inner; diff --git a/sway/layout.c b/sway/layout.c index f600cf49..50a25442 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -186,40 +186,63 @@ void arrange_windows(swayc_t *container, int width, int height) { break; } - double total_weight = 0; - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - total_weight += child->weight; - } - + x = y = 0; + double scale = 0; switch (container->layout) { case L_HORIZ: default: - sway_log(L_DEBUG, "Arranging %p horizontally", container); + //Calculate total width for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - double percent = child->weight / total_weight; - sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width); - child->x = x + container->x; - child->y = y + container->y; - int w = width * percent; - int h = height; - arrange_windows(child, w, h); - x += w; + int *old_width = &((swayc_t *)container->children->items[i])->width; + if (*old_width == 0) { + if (container->children->length > 1) { + *old_width = width / (container->children->length - 1); + } else { + *old_width = width; + } + sway_log(L_DEBUG,"setting width as %d",*old_width); + } + scale += *old_width; + } + //Resize windows + if (scale > 0.1) { + scale = width / scale; + sway_log(L_DEBUG, "Arranging %p horizontally", container); + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %d by %f)", child, child->type, width, scale); + child->x = x + container->x; + child->y = y + container->y; + arrange_windows(child, child->width * scale, height); + x += child->width; + } } break; case L_VERT: - sway_log(L_DEBUG, "Arranging %p vertically", container); + //Calculate total height for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - double percent = child->weight / total_weight; - sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width); - child->x = x + container->x; - child->y = y + container->y; - int w = width; - int h = height * percent; - arrange_windows(child, w, h); - y += h; + int *old_height = &((swayc_t *)container->children->items[i])->height; + if (container->children->length > 1) { + *old_height = height / (container->children->length - 1); + } else { + *old_height = height; + } + if (*old_height == 0) { + } + scale += *old_height; + } + //Resize + if (scale > 0.1) { + scale = height / scale; + sway_log(L_DEBUG, "Arranging %p vertically", container); + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %d by %f)", child, child->type, height, scale); + child->x = x + container->x; + child->y = y + container->y; + arrange_windows(child, width, child->height * scale); + y += child->height; + } } break; } diff --git a/sway/log.c b/sway/log.c index 44f6e366..49a74e02 100644 --- a/sway/log.c +++ b/sway/log.c @@ -114,7 +114,6 @@ static void container_log(const swayc_t *c) { 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); } -- cgit v1.2.3-54-g00ecf 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 'include/container.h') 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-54-g00ecf 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 'include/container.h') 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-54-g00ecf