From 97f7d47413967e2b6f405c4fa303850b7c56f57a Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 10 Dec 2016 16:44:43 +0100 Subject: [PATCH 01/30] Added Awesome/Monad type "auto" layouts --- include/sway/container.h | 17 ++ include/sway/layout.h | 3 + sway/commands/layout.c | 20 ++ sway/container.c | 2 + sway/debug_log.c | 4 + sway/layout.c | 436 +++++++++++++++++++++++++++++---------- sway/sway.5.txt | 3 +- 7 files changed, 372 insertions(+), 113 deletions(-) diff --git a/include/sway/container.h b/include/sway/container.h index 2bedd136..1d0fb265 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -37,6 +37,13 @@ enum swayc_layouts { L_STACKED, L_TABBED, L_FLOATING, /**< A psuedo-container, removed from the tree, to hold floating windows */ + + /* Awesome/Monad style auto layouts */ + L_AUTO_LEFT, + L_AUTO_RIGHT, + L_AUTO_TOP, + L_AUTO_BOTTOM, + // Keep last L_LAYOUTS, }; @@ -144,6 +151,16 @@ struct sway_container { struct wlc_geometry title_bar_geometry; struct wlc_geometry actual_geometry; int border_thickness; + + /** + * Number of master views in auto layouts. + */ + uint32_t nb_master; + + /** + * Number of slave groups (e.g. columns) in auto layouts. + */ + uint32_t nb_slave_groups; }; enum visibility_mask { diff --git a/include/sway/layout.h b/include/sway/layout.h index b982365c..38096947 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -75,4 +75,7 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. */ enum swayc_layouts default_layout(swayc_t *output); +inline bool is_auto_layout(enum swayc_layouts layout) { + return (layout >= L_AUTO_LEFT) && (layout <= L_AUTO_BOTTOM); +} #endif diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 08336150..e6fa7ef1 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -54,6 +54,26 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else { swayc_change_layout(parent, L_HORIZ); } + } else if (strcasecmp(argv[0], "auto_left") == 0) { + if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ + parent = new_container(parent, L_AUTO_LEFT); + } + swayc_change_layout(parent, L_AUTO_LEFT); + } else if (strcasecmp(argv[0], "auto_right") == 0) { + if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ + parent = new_container(parent, L_AUTO_RIGHT); + } + swayc_change_layout(parent, L_AUTO_RIGHT); + } else if (strcasecmp(argv[0], "auto_top") == 0) { + if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ + parent = new_container(parent, L_AUTO_TOP); + } + swayc_change_layout(parent, L_AUTO_TOP); + } else if (strcasecmp(argv[0], "auto_bot") == 0) { + if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ + parent = new_container(parent, L_AUTO_BOTTOM); + } + swayc_change_layout(parent, L_AUTO_BOTTOM); } } diff --git a/sway/container.c b/sway/container.c index e557f450..d034115f 100644 --- a/sway/container.c +++ b/sway/container.c @@ -32,6 +32,8 @@ static swayc_t *new_swayc(enum swayc_types type) { c->layout = L_NONE; c->workspace_layout = L_NONE; c->type = type; + c->nb_master = 1; + c->nb_slave_groups = 1; if (type != C_VIEW) { c->children = create_list(); } diff --git a/sway/debug_log.c b/sway/debug_log.c index 8d891a44..d1eafae8 100644 --- a/sway/debug_log.c +++ b/sway/debug_log.c @@ -38,6 +38,10 @@ static void container_log(const swayc_t *c, int depth) { c->layout == L_STACKED ? "Stack": c->layout == L_TABBED ? "Tab": c->layout == L_FLOATING ? "Float": + c->layout == L_AUTO_LEFT ? "A_lft": + c->layout == L_AUTO_RIGHT ? "A_rgt": + c->layout == L_AUTO_TOP ? "A_top": + c->layout == L_AUTO_BOTTOM ? "A_bot": "Unknown"); fprintf(stderr, "w:%4.f|h:%4.f|", c->width, c->height); fprintf(stderr, "x:%4.f|y:%4.f|", c->x, c->y); diff --git a/sway/layout.c b/sway/layout.c index ea4a680d..52473d0d 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -118,7 +118,11 @@ swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { list_add(parent->floating, active); } else { int i = index_child(fixed); - list_insert(parent->children, i + 1, active); + if (is_auto_layout(parent->layout)) { + list_add(parent->children, active); + } else { + list_insert(parent->children, i + 1, active); + } } } active->parent = parent; @@ -769,6 +773,26 @@ void update_geometry(swayc_t *container) { } } +/** + * Layout application prototypes + */ +static void apply_horiz_layout(swayc_t *container, const double x, + const double y, const double width, + const double height, const int start, + const int end); +static void apply_vert_layout(swayc_t *container, const double x, + const double y, const double width, + const double height, const int start, + const int end); +static void apply_tabbed_or_stacked_layout(swayc_t *container, double x, + double y, double width, + double height); + +static void apply_auto_layout(swayc_t *container, const double x, const double y, + const double width, const double height, + enum swayc_layouts group_layout, + bool master_first); + static void arrange_windows_r(swayc_t *container, double width, double height) { int i; if (width == -1 || height == -1) { @@ -776,14 +800,15 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { width = container->width; height = container->height; } - // pixels are indivisable. if we don't round the pixels, then the view + // pixels are indivisible. if we don't round the pixels, then the view // calculations will be off (e.g. 50.5 + 50.5 = 101, but in reality it's // 50 + 50 = 100). doing it here cascades properly to all width/height/x/y. width = floor(width); height = floor(height); sway_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container, - container->name, container->width, container->height, container->x, container->y); + container->name, container->width, container->height, container->x, + container->y); double x = 0, y = 0; switch (container->type) { @@ -895,132 +920,46 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { break; } - double scale = 0; switch (container->layout) { case L_HORIZ: default: - // Calculate total width - for (i = 0; i < container->children->length; ++i) { - double *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; - } - } - scale += *old_width; - } - - // Resize windows - if (scale > 0.1) { - scale = width / scale; - sway_log(L_DEBUG, "Arranging %p horizontally", container); - swayc_t *focused = NULL; - 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 %f by %f)", child, child->type, width, scale); - child->x = x; - child->y = y; - - if (child == container->focused) { - focused = child; - } - - if (i == container->children->length - 1) { - double remaining_width = container->x + width - x; - arrange_windows_r(child, remaining_width, height); - } else { - arrange_windows_r(child, child->width * scale, height); - } - x += child->width; - } - - // update focused view border last because it may - // depend on the title bar geometry of its siblings. - if (focused && container->children->length > 1) { - update_container_border(focused); - } - } + apply_horiz_layout(container, x, y, width, height, 0, + container->children->length); break; case L_VERT: - // Calculate total height - for (i = 0; i < container->children->length; ++i) { - double *old_height = &((swayc_t *)container->children->items[i])->height; - if (*old_height <= 0) { - if (container->children->length > 1) { - *old_height = height / (container->children->length - 1); - } else { - *old_height = height; - } - } - scale += *old_height; - } - // Resize - if (scale > 0.1) { - scale = height / scale; - sway_log(L_DEBUG, "Arranging %p vertically", container); - swayc_t *focused = NULL; - 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 %f by %f)", child, child->type, height, scale); - child->x = x; - child->y = y; - - if (child == container->focused) { - focused = child; - } - - if (i == container->children->length - 1) { - double remaining_height = container->y + height - y; - arrange_windows_r(child, width, remaining_height); - } else { - arrange_windows_r(child, width, child->height * scale); - } - y += child->height; - } - - // update focused view border last because it may - // depend on the title bar geometry of its siblings. - if (focused && container->children->length > 1) { - update_container_border(focused); - } - } + apply_vert_layout(container, x, y, width, height, 0, + container->children->length); break; case L_TABBED: case L_STACKED: - { - swayc_t *focused = NULL; - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - child->x = x; - child->y = y; - if (child == container->focused) { - focused = child; - } else { - arrange_windows_r(child, width, height); - } - } - - if (focused) { - arrange_windows_r(focused, width, height); - } - break; - } + apply_tabbed_or_stacked_layout(container, x, y, width, height); + break; + case L_AUTO_LEFT: + apply_auto_layout(container, x, y, width, height, L_VERT, true); + break; + case L_AUTO_RIGHT: + apply_auto_layout(container, x, y, width, height, L_VERT, false); + break; + case L_AUTO_TOP: + apply_auto_layout(container, x, y, width, height, L_HORIZ, true); + break; + case L_AUTO_BOTTOM: + apply_auto_layout(container, x, y, width, height, L_HORIZ, false); + break; } // Arrage floating layouts for workspaces last if (container->type == C_WORKSPACE) { - for (i = 0; i < container->floating->length; ++i) { + for (int i = 0; i < container->floating->length; ++i) { swayc_t *view = container->floating->items[i]; if (view->type == C_VIEW) { update_geometry(view); - sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", view->width, - view->height, view->x, view->y); + sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", + view->width, view->height, view->x, view->y); if (swayc_is_fullscreen(view)) { wlc_view_bring_to_front(view->handle); - } else if (!container->focused - || !swayc_is_fullscreen(container->focused)) { + } else if (!container->focused || + !swayc_is_fullscreen(container->focused)) { wlc_view_bring_to_front(view->handle); } } @@ -1028,6 +967,279 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { } } +void apply_horiz_layout(swayc_t *container, const double x, const double y, + const double width, const double height, + const int start, const int end) { + double scale = 0; + // Calculate total width + for (int i = start; i < end; ++i) { + double *old_width = &((swayc_t *)container->children->items[i])->width; + if (*old_width <= 0) { + if (end - start > 1) { + *old_width = width / (end - start - 1); + } else { + *old_width = width; + } + } + scale += *old_width; + } + + // Resize windows + double child_x = x; + if (scale > 0.1) { + scale = width / scale; + sway_log(L_DEBUG, "Arranging %p horizontally", container); + swayc_t *focused = NULL; + for (int i = start; i < end; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, + "Calculating arrangement for %p:%d (will scale %f by %f)", child, + child->type, width, scale); + child->x = child_x; + child->y = y; + + if (child == container->focused) { + focused = child; + } + + if (i == end - 1) { + double remaining_width = x + width - child_x; + arrange_windows_r(child, remaining_width, height); + } else { + arrange_windows_r(child, child->width * scale, height); + } + child_x += child->width; + } + + // update focused view border last because it may + // depend on the title bar geometry of its siblings. + if (focused && container->children->length > 1) { + update_container_border(focused); + } + } +} + +void apply_vert_layout(swayc_t *container, const double x, const double y, + const double width, const double height, const int start, + const int end) { + int i; + double scale = 0; + // Calculate total height + for (i = start; i < end; ++i) { + double *old_height = &((swayc_t *)container->children->items[i])->height; + if (*old_height <= 0) { + if (end - start > 1) { + *old_height = height / (end - start - 1); + } else { + *old_height = height; + } + } + scale += *old_height; + } + // Resize + double child_y = y; + if (scale > 0.1) { + scale = height / scale; + sway_log(L_DEBUG, "Arranging %p vertically", container); + swayc_t *focused = NULL; + for (i = start; i < end; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, + "Calculating arrangement for %p:%d (will scale %f by %f)", child, + child->type, height, scale); + child->x = x; + child->y = child_y; + + if (child == container->focused) { + focused = child; + } + + if (i == end - 1) { + double remaining_height = y + height - child_y; + arrange_windows_r(child, width, remaining_height); + } else { + arrange_windows_r(child, width, child->height * scale); + } + child_y += child->height; + } + + // update focused view border last because it may + // depend on the title bar geometry of its siblings. + if (focused && container->children->length > 1) { + update_container_border(focused); + } + } +} + +void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, + double width, double height) { + int i; + swayc_t *focused = NULL; + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + child->x = x; + child->y = y; + if (child == container->focused) { + focused = child; + } else { + arrange_windows_r(child, width, height); + } + } + + if (focused) { + arrange_windows_r(focused, width, height); + } +} + +void apply_auto_layout(swayc_t *container, const double x, const double y, + const double width, const double height, + enum swayc_layouts group_layout, + bool master_first) { + // Auto layout "container" in width x height @ x, y + // using "group_layout" for each of the groups in the container. + // There is one "master" group, plus container->nb_slave_groups. + // Each group is layed out side by side following the "major" axis. + // The direction of the layout used for groups is the "minor" axis. + // Example: + // + // ---- major axis --> + // +---------+-----------+ + // | | | | + // | master | slave 1 | | + // | +-----------+ | minor axis (direction of group_layout) + // | | | | + // | | slave 2 | V + // +---------+-----------+ + // + // container with three children (one master and two slaves) and + // a single slave group (containing slave 1 and 2). The master + // group and slave group are layed out using L_VERT. + + uint_fast32_t nb_slaves = container->children->length - container->nb_master; + uint_fast32_t nb_groups = (container->nb_master > 0 ? 1 : 0) + + MIN(container->nb_slave_groups, nb_slaves); + + // the target dimension of the container along the "major" axis, each + // group in the container will be layed out using "group_layout" along + // the "minor" axis. + double dim_maj; + double pos_maj; + + // x and y coords for the next group to be laid out. + const double *group_x, *group_y; + + // pos of the next group to layout along the major axis + double pos; + + // size of the next group along the major axis. + double group_dim; + + // height and width of next group to be laid out. + const double *group_h, *group_w; + + switch(group_layout) { + default: + sway_log(L_ERROR, "Unknown layout type (%d) used in %s()", + group_layout, __func__); + /* fall through */ + case L_VERT: + dim_maj = width; + pos_maj = x; + + group_x = &pos; + group_y = &y; + group_w = &group_dim; + group_h = &height; + break; + case L_HORIZ: + dim_maj = height; + pos_maj = y; + + group_x = &x; + group_y = &pos; + group_w = &width; + group_h = &group_dim; + break; + } + + /* Determine the dimension of each of the groups in the layout. + * Dimension will be width for a VERT layout and height for a HORIZ + * layout. */ + double old_group_dim[nb_groups]; + double old_dim = 0; + uint_fast32_t group = 0; + for (int i = 0; i < container->children->length;) { + swayc_t *child = container->children->items[i]; + double *dim = group_layout == L_HORIZ ? &child->height : &child->width; + if (*dim <= 0) { + // New child with uninitialized dimension + *dim = dim_maj; + if (nb_groups > 1) { + // child gets a dimension proportional to existing groups, + // it will be later scaled based on to the available size + // in the major axis. + *dim /= (nb_groups - 1); + } + } + if (i == 0 && container->nb_master > 0) { + i += container->nb_master; + } else { + i += (nb_slaves - i + container->nb_master) / (nb_groups - group); + } + old_dim += *dim; + old_group_dim[group++] = *dim; + } + + double scale = dim_maj / old_dim; + + /* Apply layout to each group */ + pos = pos_maj; + + // first child in the current group + int start; + + // index immediately after the last child in the current group + int end = 0; + + for (group = 0; group < nb_groups; ++group) { + // column to include next by increasing position. + uint_fast32_t layout_group = master_first ? group : (group + 1) % nb_groups; + + // adjusted size of the group + group_dim = old_group_dim[layout_group] * scale; + if (container->nb_master > 0 && layout_group == 0) { + start = 0; + end = container->nb_master; + } else { + if (group == 0) { + start = container->nb_master; + } else { + start = end; + } + end = start + + (nb_slaves - start + container->nb_master) / (nb_groups - layout_group); + } + if (group == nb_groups - 1) { + group_dim = pos_maj + dim_maj - pos; // remaining width + } + sway_log(L_DEBUG, "Arranging container %p column %" PRIuFAST32 + ", children [%d,%d[ (%fx%f+%f,%f)", + container, group, start, end, *group_w, *group_h, *group_x, *group_y); + switch (group_layout) { + default: + case L_VERT: + apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; + case L_HORIZ: + apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; + } + + /* update position for next group */ + pos += group_dim; + } +} + void arrange_windows(swayc_t *container, double width, double height) { update_visibility(container); arrange_windows_r(container, width, height); diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 7980ba82..1f25ce14 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -81,7 +81,8 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( **layout** :: Sets the layout mode of the focused container. _mode_ can be one of _splith_, - _splitv_, _toggle split_, _stacking_ or _tabbed_. + _splitv_, _toggle split_, _stacking_, _tabbed_, _auto_left_, _auto_right_, + _auto_top, _auto_bottom_. **move** :: Moves the focused container _left_, _right_, _up_, or _down_. From 5425d0489fad8c6c00a7794ca18ac34041bd9308 Mon Sep 17 00:00:00 2001 From: wil Date: Mon, 19 Dec 2016 21:45:52 +0100 Subject: [PATCH 02/30] Handle resize in auto layouts --- sway/commands/resize.c | 409 ++++++++++++++++++++--------------------- 1 file changed, 200 insertions(+), 209 deletions(-) diff --git a/sway/commands/resize.c b/sway/commands/resize.c index 2c5b3f6b..c850575b 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -62,225 +62,216 @@ static bool resize_floating(int amount, bool use_width) { return false; } +/** + * returns the index of the container's child that is first in a group. + * This index is > to the argument. + * This makes the function usable to walk through the groups in a container. + */ +static int next_group_index(swayc_t *container, int after) { + if (after < 0) { + return 0; + } else if (is_auto_layout(container->layout)) { + if ((uint_fast32_t) after < container->nb_master) { + return container->nb_master; + } else { + uint_fast32_t grp_idx = 0; + for (int i = container->nb_master; i < container->children->length; ) { + uint_fast32_t grp_sz = (container->children->length - i) / + (container->nb_slave_groups - grp_idx); + if (after - i < (int) grp_sz) { + return i + grp_sz; + } + i += grp_sz; + } + return container->children->length; + } + } else { + // return after + 1; + return container->children->length; + } +} + +/** + * Return the number of children in the slave groups. This corresponds to the children + * that are not members of the master group. + */ +static inline uint_fast32_t slave_count(swayc_t *container) { + return container->children->length - container->nb_master; + +} + +/** + * given the index of a container's child, return the index of the first child of the group + * which index is a member of. + */ +static int group_start_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout) || (uint_fast32_t) index < container->nb_master) { + return 0; + } else { + uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; + uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; + if ((index - container->nb_master) / grp_sz < container->nb_slave_groups - remainder) { + return ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; + } else { + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + return idx2 + ((idx2 - index) / (grp_sz + 1)) * (grp_sz + 1); + } + } +} + +/** + * given the index of a container's child, return the index of the first child of the group + * that follows the one which index is a member of. + */ +static int group_end_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout)) { + return container->children->length; + } else { + uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; + uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; + if ((index - container->nb_master) / grp_sz < container->nb_slave_groups - remainder) { + return ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; + } else { + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + return idx2 + ((idx2 - index) / (grp_sz + 1) + 1) * (grp_sz + 1); + } + } +} + +/** + * Return the combined number of master and slave groups in the container. + */ +static inline uint_fast32_t group_count(swayc_t *container) { + return MIN(container->nb_slave_groups, slave_count(container)) + (container->nb_master ? 1 : 0); +} + +/** + * return the index of the Group containing th child of . + * The index is the order of the group along the container's major axis (starting at 0). + */ +static uint_fast32_t group_index(swayc_t *container, int index) { + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + int nb_slaves = slave_count(container); + if (index < (int) container->nb_master) { + if (master_first || nb_slaves <= 0) { + return 0; + } else { + return MIN(container->nb_slave_groups, nb_slaves); + } + } else { + uint_fast32_t grp_idx = 0; + for (int i = container->nb_master; i < container->children->length; ) { + uint_fast32_t grp_sz = (container->children->length - i) / + (container->nb_slave_groups - grp_idx); + if (index - i < (int) grp_sz) { + break; + } + } + return grp_idx + (master_first ? 1 : 0); + } +} + static bool resize_tiled(int amount, bool use_width) { - swayc_t *parent = get_focused_view(swayc_active_workspace()); - swayc_t *focused = parent; - swayc_t *sibling; - if (!parent) { + swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *parent = container->parent; + int idx_focused = 0; + bool use_major = false; + uint_fast32_t nb_before = 0; + uint_fast32_t nb_after = 0; + + // 1. Identify a container ancestor that will allow the focused child to grow in the requested + // direction. + while (container->parent) { + parent = container->parent; + if ((parent->children && parent->children->length > 1) && + (is_auto_layout(parent->layout) || (use_width ? parent->layout == L_HORIZ : + parent->layout == L_VERT))) { + // check if container has siblings that can provide/absorb the space needed for + // the resize operation. + use_major = use_width + ? parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT + : parent->layout == L_AUTO_TOP || parent->layout == L_AUTO_BOTTOM; + // Note: use_major will be false for L_HORIZ and L_VERT + + idx_focused = index_child(container); + if (idx_focused < 0) { + sway_log(L_ERROR, "Something weird is happening, child container not " + "present in its parent's children list."); + continue; + } + if (use_major) { + nb_before = group_index(parent, idx_focused); + nb_after = group_count(parent) - nb_before - 1; + } else { + nb_before = idx_focused - group_start_index(parent, idx_focused); + nb_after = next_group_index(parent, idx_focused) - idx_focused - 1; + } + if (nb_before || nb_after) { + break; + } + } + container = parent; /* continue up the tree to the next ancestor */ + } + if (parent == &root_container) { return true; } - // Find the closest parent container which has siblings of the proper layout. - // Then apply the resize to all of them. - int i; - if (use_width) { - int lnumber = 0; - int rnumber = 0; - while (parent->parent) { - if (parent->parent->layout == L_HORIZ && parent->parent->children) { - for (i = 0; i < parent->parent->children->length; i++) { - sibling = parent->parent->children->items[i]; - if (sibling->x != focused->x) { - if (sibling->x < parent->x) { - lnumber++; - } else if (sibling->x > parent->x) { - rnumber++; - } - } - } - if (rnumber || lnumber) { - break; - } + sway_log(L_DEBUG, "Found the proper parent: %p. It has %" PRIuFAST32 " before conts, and %" + PRIuFAST32 " after conts", parent, nb_before, nb_after); + // 2. Ensure that the resize operation will not make one of the resized containers drop + // below the "sane" size threshold. + bool valid = true; + swayc_t *focused = parent->children->items[idx_focused]; + int start = use_major ? 0 : group_start_index(parent, idx_focused); + int end = use_major ? parent->children->length : group_end_index(parent, idx_focused); + for (int i = start; i < end; ) { + swayc_t *sibling = parent->children->items[i]; + double pixels = amount; + bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; + bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; + if (is_before || is_after) { + pixels = -pixels; + pixels /= is_before ? nb_before : nb_after; + if (nb_after != 0 && nb_before != 0) { + pixels /= 2; } - parent = parent->parent; } - if (parent == &root_container) { - return true; + if (use_width ? + sibling->width + pixels < min_sane_w : + sibling->height + pixels < min_sane_h) { + valid = false; + break; } - sway_log(L_DEBUG, "Found the proper parent: %p. It has %d l conts, and %d r conts", parent->parent, lnumber, rnumber); - //TODO: Ensure rounding is done in such a way that there are NO pixel leaks - bool valid = true; - for (i = 0; i < parent->parent->children->length; i++) { - sibling = parent->parent->children->items[i]; - if (sibling->x != focused->x) { - if (sibling->x < parent->x) { - double pixels = -1 * amount; - pixels /= lnumber; - if (rnumber) { - if ((sibling->width + pixels/2) < min_sane_w) { - valid = false; - break; - } - } else { - if ((sibling->width + pixels) < min_sane_w) { - valid = false; - break; - } - } - } else if (sibling->x > parent->x) { - double pixels = -1 * amount; - pixels /= rnumber; - if (lnumber) { - if ((sibling->width + pixels/2) < min_sane_w) { - valid = false; - break; - } - } else { - if ((sibling->width + pixels) < min_sane_w) { - valid = false; - break; - } - } + i = use_major ? next_group_index(parent, i) : (i + 1); + } + // 3. Apply the size change + if (valid) { + for (int i = 0; i < parent->children->length; ++i) { + swayc_t *sibling = parent->children->items[i]; + double pixels = amount; + bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; + bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; + if (is_before || is_after) { + pixels = -pixels; + pixels /= is_before ? nb_before : nb_after; + if (nb_after != 0 && nb_before != 0) { + pixels /= 2; } + sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after"); + recursive_resize(sibling, pixels, + use_width ? + (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : + (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); } else { - double pixels = amount; - if (parent->width + pixels < min_sane_w) { - valid = false; - break; - } + sway_log(L_DEBUG, "%p: same pos", sibling); + recursive_resize(sibling, pixels, + use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); + recursive_resize(sibling, pixels, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); } } - if (valid) { - for (i = 0; i < parent->parent->children->length; i++) { - sibling = parent->parent->children->items[i]; - if (sibling->x != focused->x) { - if (sibling->x < parent->x) { - double pixels = -1 * amount; - pixels /= lnumber; - if (rnumber) { - recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_RIGHT); - } else { - recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_RIGHT); - } - } else if (sibling->x > parent->x) { - double pixels = -1 * amount; - pixels /= rnumber; - if (lnumber) { - recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_LEFT); - } else { - recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_LEFT); - } - } - } else { - if (rnumber != 0 && lnumber != 0) { - double pixels = amount; - pixels /= 2; - recursive_resize(parent, pixels, WLC_RESIZE_EDGE_LEFT); - recursive_resize(parent, pixels, WLC_RESIZE_EDGE_RIGHT); - } else if (rnumber) { - recursive_resize(parent, amount, WLC_RESIZE_EDGE_RIGHT); - } else if (lnumber) { - recursive_resize(parent, amount, WLC_RESIZE_EDGE_LEFT); - } - } - } - // Recursive resize does not handle positions, let arrange_windows - // take care of that. - arrange_windows(swayc_active_workspace(), -1, -1); - } - return true; - } else { - int tnumber = 0; - int bnumber = 0; - while (parent->parent) { - if (parent->parent->layout == L_VERT) { - for (i = 0; i < parent->parent->children->length; i++) { - sibling = parent->parent->children->items[i]; - if (sibling->y != focused->y) { - if (sibling->y < parent->y) { - bnumber++; - } else if (sibling->y > parent->y) { - tnumber++; - } - } - } - if (bnumber || tnumber) { - break; - } - } - parent = parent->parent; - } - if (parent->parent == NULL || parent->parent->children == NULL) { - return true; - } - sway_log(L_DEBUG, "Found the proper parent: %p. It has %d b conts, and %d t conts", parent->parent, bnumber, tnumber); - //TODO: Ensure rounding is done in such a way that there are NO pixel leaks - bool valid = true; - for (i = 0; i < parent->parent->children->length; i++) { - sibling = parent->parent->children->items[i]; - if (sibling->y != focused->y) { - if (sibling->y < parent->y) { - double pixels = -1 * amount; - pixels /= bnumber; - if (tnumber) { - if ((sibling->height + pixels/2) < min_sane_h) { - valid = false; - break; - } - } else { - if ((sibling->height + pixels) < min_sane_h) { - valid = false; - break; - } - } - } else if (sibling->y > parent->y) { - double pixels = -1 * amount; - pixels /= tnumber; - if (bnumber) { - if ((sibling->height + pixels/2) < min_sane_h) { - valid = false; - break; - } - } else { - if ((sibling->height + pixels) < min_sane_h) { - valid = false; - break; - } - } - } - } else { - double pixels = amount; - if (parent->height + pixels < min_sane_h) { - valid = false; - break; - } - } - } - if (valid) { - for (i = 0; i < parent->parent->children->length; i++) { - sibling = parent->parent->children->items[i]; - if (sibling->y != focused->y) { - if (sibling->y < parent->y) { - double pixels = -1 * amount; - pixels /= bnumber; - if (tnumber) { - recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_BOTTOM); - } else { - recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_BOTTOM); - } - } else if (sibling->x > parent->x) { - double pixels = -1 * amount; - pixels /= tnumber; - if (bnumber) { - recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_TOP); - } else { - recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_TOP); - } - } - } else { - if (bnumber != 0 && tnumber != 0) { - double pixels = amount/2; - recursive_resize(parent, pixels, WLC_RESIZE_EDGE_TOP); - recursive_resize(parent, pixels, WLC_RESIZE_EDGE_BOTTOM); - } else if (tnumber) { - recursive_resize(parent, amount, WLC_RESIZE_EDGE_TOP); - } else if (bnumber) { - recursive_resize(parent, amount, WLC_RESIZE_EDGE_BOTTOM); - } - } - } - arrange_windows(swayc_active_workspace(), -1, -1); - } - return true; + // Recursive resize does not handle positions, let arrange_windows + // take care of that. + arrange_windows(swayc_active_workspace(), -1, -1); } return true; } From 8b0073b1954b54792f74b9e024c51548143906ea Mon Sep 17 00:00:00 2001 From: wil Date: Wed, 21 Dec 2016 16:55:02 +0100 Subject: [PATCH 03/30] Added "layout incnmaster|incncol" commands --- sway/commands/layout.c | 30 ++++++++++++++++++++++++++++++ sway/sway.5.txt | 20 ++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/sway/commands/layout.c b/sway/commands/layout.c index e6fa7ef1..5e2e8efd 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -74,6 +74,36 @@ struct cmd_results *cmd_layout(int argc, char **argv) { parent = new_container(parent, L_AUTO_BOTTOM); } swayc_change_layout(parent, L_AUTO_BOTTOM); + } else if (strcasecmp(argv[0], "incnmaster") == 0) { + if ((error = checkarg(argc, "layout incnmaster", + EXPECTED_EQUAL_TO, 2))) { + return error; + } + int inc = (int) strtol(argv[1], NULL, 10); + swayc_t *container = get_focused_view(swayc_active_workspace()); + if (container && inc && + is_auto_layout(container->parent->layout) && + ((int)container->parent->nb_master + inc >= 0)) { + for (int i = container->parent->nb_master; + i >= 0 && i < container->parent->children->length && + i != (int) container->parent->nb_master + inc;) { + ((swayc_t *) container->parent->children->items[i])->height = -1; + ((swayc_t *) container->parent->children->items[i])->width = -1; + i += inc > 0 ? 1 : -1; + } + container->parent->nb_master += inc; + } + } else if ((strcasecmp(argv[0], "incncol") == 0) && argc ==2) { + if ((error = checkarg(argc, "layout incncol", + EXPECTED_EQUAL_TO, 2))) { + return error; + } + int inc = (int) strtol(argv[1], NULL, 10); + swayc_t *container = get_focused_view(swayc_active_workspace()); + if (container && inc && is_auto_layout(container->parent->layout) && + ((int)container->parent->nb_slave_groups + inc >= 1)) { + container->parent->nb_slave_groups += inc; + } } } diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 1f25ce14..2fa5f32e 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -39,7 +39,7 @@ The following commands may only be used in the configuration file. **set** :: Sets variable $name to _value_. You can use the new variable in the arguments of future commands. - + **orientation** :: Sets the default container layout for tiled containers. @@ -62,11 +62,14 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( Make focused view floating, non-floating, or the opposite of what it is now. **focus** :: - Direction may be one of _up_, _down_, _left_, _right_, _parent_, or _child_. - The directional focus commands will move the focus in that direction. The parent - focus command will change the focus to the parent of the currently focused - container, which is useful, for example, to open a sibling of the parent - container, or to move the entire container around. + Direction may be one of _up_, _down_, _left_, _right_, _next_, _prev_, + _parent_, or _child_. The directional focus commands will move the focus + in that direction. The auto_next and auto_prev will focus the next, + respectively previous, element in the current container if it is using + one of the _auto_ layouts. The parent focus command will change the + focus to the parent of the currently focused container, which is useful, + for example, to open a sibling of the parent container, or to move the + entire container around. **focus** output :: Direction may be one of _up_, _down_, _left_, _right_. The directional focus @@ -84,6 +87,11 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( _splitv_, _toggle split_, _stacking_, _tabbed_, _auto_left_, _auto_right_, _auto_top, _auto_bottom_. +**layout** :: + Modify the number of master elements, respectively slave columns, in the + focused container. can be a positive or negative integer. These commands + only have an effect if the focused container uses one of the "auto" layouts. + **move** :: Moves the focused container _left_, _right_, _up_, or _down_. From ed71e67d7e46746a36a4c2437d143f52aa17b666 Mon Sep 17 00:00:00 2001 From: wil Date: Wed, 21 Dec 2016 16:58:38 +0100 Subject: [PATCH 04/30] [fix] handle cases where nb_master > children->length in auto layout --- sway/layout.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index 52473d0d..0428ecf9 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1209,15 +1209,14 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, group_dim = old_group_dim[layout_group] * scale; if (container->nb_master > 0 && layout_group == 0) { start = 0; - end = container->nb_master; + end = MIN(container->nb_master, container->children->length); } else { if (group == 0) { start = container->nb_master; } else { start = end; } - end = start + - (nb_slaves - start + container->nb_master) / (nb_groups - layout_group); + end = start + (nb_slaves - start + container->nb_master) / (nb_groups - layout_group); } if (group == nb_groups - 1) { group_dim = pos_maj + dim_maj - pos; // remaining width From bc3dc970265dab6e0305975791b5a266bbc390ca Mon Sep 17 00:00:00 2001 From: wil Date: Wed, 21 Dec 2016 16:59:50 +0100 Subject: [PATCH 05/30] [fix] Handle auto layout resize with multiple slave groups --- sway/commands/resize.c | 132 +++++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 59 deletions(-) diff --git a/sway/commands/resize.c b/sway/commands/resize.c index c850575b..9a756e81 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -62,35 +62,6 @@ static bool resize_floating(int amount, bool use_width) { return false; } -/** - * returns the index of the container's child that is first in a group. - * This index is > to the argument. - * This makes the function usable to walk through the groups in a container. - */ -static int next_group_index(swayc_t *container, int after) { - if (after < 0) { - return 0; - } else if (is_auto_layout(container->layout)) { - if ((uint_fast32_t) after < container->nb_master) { - return container->nb_master; - } else { - uint_fast32_t grp_idx = 0; - for (int i = container->nb_master; i < container->children->length; ) { - uint_fast32_t grp_sz = (container->children->length - i) / - (container->nb_slave_groups - grp_idx); - if (after - i < (int) grp_sz) { - return i + grp_sz; - } - i += grp_sz; - } - return container->children->length; - } - } else { - // return after + 1; - return container->children->length; - } -} - /** * Return the number of children in the slave groups. This corresponds to the children * that are not members of the master group. @@ -105,36 +76,46 @@ static inline uint_fast32_t slave_count(swayc_t *container) { * which index is a member of. */ static int group_start_index(swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout) || (uint_fast32_t) index < container->nb_master) { + if ((index < 0) || (! is_auto_layout(container->layout)) || + ((uint_fast32_t) index < container->nb_master)) { return 0; } else { uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; - if ((index - container->nb_master) / grp_sz < container->nb_slave_groups - remainder) { - return ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; + int start_idx; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; } else { - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - return idx2 + ((idx2 - index) / (grp_sz + 1)) * (grp_sz + 1); + start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); } + return MIN(start_idx, container->children->length); } } /** * given the index of a container's child, return the index of the first child of the group * that follows the one which index is a member of. + * This makes the function usable to walk through the groups in a container. */ static int group_end_index(swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout)) { return container->children->length; } else { - uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; - uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; - if ((index - container->nb_master) / grp_sz < container->nb_slave_groups - remainder) { - return ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; + int nxt_idx; + if ((uint_fast32_t)index < container->nb_master) { + nxt_idx = container->nb_master; } else { + uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; + uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - return idx2 + ((idx2 - index) / (grp_sz + 1) + 1) * (grp_sz + 1); + if (index < idx2) { + nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; + } else { + nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); + } } + return MIN(nxt_idx, container->children->length); } } @@ -150,22 +131,26 @@ static inline uint_fast32_t group_count(swayc_t *container) { * The index is the order of the group along the container's major axis (starting at 0). */ static uint_fast32_t group_index(swayc_t *container, int index) { + if (index < 0) { + return 0; + } bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); int nb_slaves = slave_count(container); - if (index < (int) container->nb_master) { + if ((uint_fast32_t) index < container->nb_master) { if (master_first || nb_slaves <= 0) { return 0; } else { return MIN(container->nb_slave_groups, nb_slaves); } } else { - uint_fast32_t grp_idx = 0; - for (int i = container->nb_master; i < container->children->length; ) { - uint_fast32_t grp_sz = (container->children->length - i) / - (container->nb_slave_groups - grp_idx); - if (index - i < (int) grp_sz) { - break; - } + uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; + uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; + uint_fast32_t grp_idx; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + grp_idx = (index - container->nb_master) / grp_sz; + } else { + grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; } return grp_idx + (master_first ? 1 : 0); } @@ -204,7 +189,13 @@ static bool resize_tiled(int amount, bool use_width) { nb_after = group_count(parent) - nb_before - 1; } else { nb_before = idx_focused - group_start_index(parent, idx_focused); - nb_after = next_group_index(parent, idx_focused) - idx_focused - 1; + nb_after = group_end_index(parent, idx_focused) - idx_focused - 1; + sway_log(L_DEBUG, "+++ focused: %d, start: %d, end: %d, before: %d, after: %d", + idx_focused, + (int)group_start_index(parent, idx_focused), + (int)group_end_index(parent, idx_focused), + (int)nb_before, (int)nb_after); + } if (nb_before || nb_after) { break; @@ -223,6 +214,7 @@ static bool resize_tiled(int amount, bool use_width) { swayc_t *focused = parent->children->items[idx_focused]; int start = use_major ? 0 : group_start_index(parent, idx_focused); int end = use_major ? parent->children->length : group_end_index(parent, idx_focused); + sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end); for (int i = start; i < end; ) { swayc_t *sibling = parent->children->items[i]; double pixels = amount; @@ -235,17 +227,21 @@ static bool resize_tiled(int amount, bool use_width) { pixels /= 2; } } + sway_log(L_DEBUG, "Check container %p: width %g vs %d, height %g vs %d", sibling, sibling->width + pixels, min_sane_w, sibling->height + pixels, min_sane_h); if (use_width ? sibling->width + pixels < min_sane_w : sibling->height + pixels < min_sane_h) { valid = false; + sway_log(L_DEBUG, "Container size no longer sane"); break; } - i = use_major ? next_group_index(parent, i) : (i + 1); + i = use_major ? group_end_index(parent, i) : (i + 1); + sway_log(L_DEBUG, "+++++ check %i", i); } // 3. Apply the size change if (valid) { - for (int i = 0; i < parent->children->length; ++i) { + for (int i = start; i < end; ) { + int next_i = use_major ? group_end_index(parent, i) : (i + 1); swayc_t *sibling = parent->children->items[i]; double pixels = amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; @@ -257,17 +253,35 @@ static bool resize_tiled(int amount, bool use_width) { pixels /= 2; } sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after"); - recursive_resize(sibling, pixels, - use_width ? - (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : - (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); + if (use_major) { + for (int j = i; j < next_i; ++j) { + recursive_resize(parent->children->items[j], pixels, + use_width ? + (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : + (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); + } + } else { + recursive_resize(sibling, pixels, + use_width ? + (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : + (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); + } } else { - sway_log(L_DEBUG, "%p: same pos", sibling); - recursive_resize(sibling, pixels, - use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); - recursive_resize(sibling, pixels, - use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); + if (use_major) { + for (int j = i; j < next_i; ++j) { + recursive_resize(parent->children->items[j], pixels, + use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); + recursive_resize(parent->children->items[j], pixels, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); + } + } else { + recursive_resize(sibling, pixels, + use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); + recursive_resize(sibling, pixels, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); + } } + i = next_i; } // Recursive resize does not handle positions, let arrange_windows // take care of that. From 0ff9fe9a7a18a5130b7c5e979ba980409f91b5f1 Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 22 Dec 2016 18:46:00 +0100 Subject: [PATCH 06/30] introduce next/prev as a direction for focus/move commands. --- include/sway/focus.h | 5 ++-- sway/commands/focus.c | 4 +++ sway/commands/layout.c | 3 +- sway/commands/move.c | 6 +++- sway/layout.c | 65 ++++++++++++++++++++++++++++++++++++------ sway/sway.5.txt | 6 ++-- 6 files changed, 75 insertions(+), 14 deletions(-) diff --git a/include/sway/focus.h b/include/sway/focus.h index b532edc2..30c9e108 100644 --- a/include/sway/focus.h +++ b/include/sway/focus.h @@ -6,7 +6,9 @@ enum movement_direction { MOVE_UP, MOVE_DOWN, MOVE_PARENT, - MOVE_CHILD + MOVE_CHILD, + MOVE_NEXT, + MOVE_PREV }; #include "container.h" @@ -40,4 +42,3 @@ extern bool suspend_workspace_cleanup; bool move_focus(enum movement_direction direction); #endif - diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 8442305f..0be442ca 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -46,6 +46,10 @@ struct cmd_results *cmd_focus(int argc, char **argv) { move_focus(MOVE_PARENT); } else if (strcasecmp(argv[0], "child") == 0) { move_focus(MOVE_CHILD); + } else if (strcasecmp(argv[0], "next") == 0) { + move_focus(MOVE_NEXT); + } else if (strcasecmp(argv[0], "prev") == 0) { + move_focus(MOVE_PREV); } else if (strcasecmp(argv[0], "mode_toggle") == 0) { int i; swayc_t *workspace = swayc_active_workspace(); diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 5e2e8efd..9e468c21 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -49,7 +49,8 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else if (strcasecmp(argv[0], "splitv") == 0) { swayc_change_layout(parent, L_VERT); } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { - if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE || parent->workspace_layout == L_HORIZ)) { + if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE || + parent->workspace_layout == L_HORIZ)) { swayc_change_layout(parent, L_VERT); } else { swayc_change_layout(parent, L_HORIZ); diff --git a/sway/commands/move.c b/sway/commands/move.c index 4819d9ef..4f6bc76f 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -13,7 +13,7 @@ struct cmd_results *cmd_move(int argc, char **argv) { if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) { return error; } - const char* expected_syntax = "Expected 'move ' or " + const char* expected_syntax = "Expected 'move ' or " "'move to workspace ' or " "'move to output ' or " "'move position mouse'"; @@ -27,6 +27,10 @@ struct cmd_results *cmd_move(int argc, char **argv) { move_container(view, MOVE_UP); } else if (strcasecmp(argv[0], "down") == 0) { move_container(view, MOVE_DOWN); + } else if (strcasecmp(argv[0], "next") == 0) { + move_container(view, MOVE_NEXT); + } else if (strcasecmp(argv[0], "prev") == 0) { + move_container(view, MOVE_PREV); } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) { // "move container ... if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) { diff --git a/sway/layout.c b/sway/layout.c index 0428ecf9..a8ad7ed5 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -251,7 +251,7 @@ void swap_geometry(swayc_t *a, swayc_t *b) { } void move_container(swayc_t *container, enum movement_direction dir) { - enum swayc_layouts layout; + enum swayc_layouts layout = L_NONE; if (container->is_floating || (container->type != C_VIEW && container->type != C_CONTAINER)) { return; @@ -260,7 +260,7 @@ void move_container(swayc_t *container, enum movement_direction dir) { layout = L_VERT; } else if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { layout = L_HORIZ; - } else { + } else if (! (dir == MOVE_NEXT || dir == MOVE_PREV)) { return; } swayc_t *parent = container->parent; @@ -284,18 +284,51 @@ void move_container(swayc_t *container, enum movement_direction dir) { container,parent,child); if (parent->layout == layout || (parent->layout == L_TABBED && layout == L_HORIZ) - || (parent->layout == L_STACKED && layout == L_VERT)) { + || (parent->layout == L_STACKED && layout == L_VERT) + || is_auto_layout(parent->layout)) { int diff; // If it has ascended (parent has moved up), no container is removed // so insert it at index, or index+1. // if it has not, the moved container is removed, so it needs to be // inserted at index-1, or index+1 if (ascended) { - diff = dir == MOVE_LEFT || dir == MOVE_UP ? 0 : 1; + diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? 0 : 1; } else { - diff = dir == MOVE_LEFT || dir == MOVE_UP ? -1 : 1; + diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? -1 : 1; + } + int idx = index_child(child); + int desired = idx + diff; + if (dir == MOVE_NEXT || dir == MOVE_PREV) { + // Next/Prev always wrap. + if (desired < 0) { + desired += parent->children->length; + } else if (desired >= parent->children->length) { + desired = 0; + } + // if move command makes container change from master to slave + // (or the contrary), reset its geometry an the one of the replaced item. + if (parent->nb_master && + (uint_fast32_t) parent->children->length > parent->nb_master) { + swayc_t *swap_geom = NULL; + // if child is being promoted/demoted, it will swap geometry + // with the sibling being demoted/promoted. + if ((dir == MOVE_NEXT && desired == 0) + || (dir == MOVE_PREV && (uint_fast32_t) desired == parent->nb_master - 1)) { + swap_geom = parent->children->items[parent->nb_master - 1]; + } else if ((dir == MOVE_NEXT && (uint_fast32_t) desired == parent->nb_master) + || (dir == MOVE_PREV && desired == parent->children->length - 1)) { + swap_geom = parent->children->items[parent->nb_master]; + } + if (swap_geom) { + double h = child->height; + double w = child->width; + child->width = swap_geom->width; + child->height = swap_geom->height; + swap_geom->width = w; + swap_geom->height = h; + } + } } - int desired = index_child(child) + diff; // when it has ascended, legal insertion position is 0:len // when it has not, legal insertion position is 0:len-1 if (desired >= 0 && desired - ascended < parent->children->length) { @@ -308,7 +341,8 @@ void move_container(swayc_t *container, enum movement_direction dir) { // insert it next to focused container if (parent->layout == layout || (parent->layout == L_TABBED && layout == L_HORIZ) - || (parent->layout == L_STACKED && layout == L_VERT)) { + || (parent->layout == L_STACKED && layout == L_VERT) + || is_auto_layout(parent->layout)) { desired = (diff < 0) * parent->children->length; } else { desired = index_child(child->focused) + 1; @@ -325,7 +359,7 @@ void move_container(swayc_t *container, enum movement_direction dir) { } } // Change parent layout if we need to - if (parent->children->length == 1 && parent->layout != layout) { + if (parent->children->length == 1 && parent->layout != layout && layout != L_NONE) { /* swayc_change_layout(parent, layout); */ parent->layout = layout; continue; @@ -1310,6 +1344,21 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio return parent; } } + + if (dir == MOVE_PREV || dir == MOVE_NEXT) { + int focused_idx = index_child(container); + if (focused_idx == -1) { + return NULL; + } else { + int desired = (focused_idx + (dir == MOVE_NEXT ? 1 : -1)) % + parent->children->length; + if (desired < 0) { + desired += parent->children->length; + } + return parent->children->items[desired]; + } + } + // If moving to an adjacent output we need a starting position (since this // output might border to multiple outputs). struct wlc_point abs_pos; diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 2fa5f32e..8f8302f0 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -92,8 +92,10 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( focused container. can be a positive or negative integer. These commands only have an effect if the focused container uses one of the "auto" layouts. -**move** :: - Moves the focused container _left_, _right_, _up_, or _down_. +**move** :: + Moves the focused container _left_, _right_, _up_, or _down_. Moving + to _prev_ or _next_ swaps the container with its sibling in the same + container. **move** to workspace :: Moves the focused container to the workspace identified by _name_. From 2b0e3c212a6c269b68879ba6c2d84ebedd5938e1 Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 22 Dec 2016 21:18:15 +0100 Subject: [PATCH 07/30] [fix] move next/prev behavior for vert/horiz layout --- sway/layout.c | 1 + 1 file changed, 1 insertion(+) diff --git a/sway/layout.c b/sway/layout.c index a8ad7ed5..2de6da45 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -283,6 +283,7 @@ void move_container(swayc_t *container, enum movement_direction dir) { sway_log(L_DEBUG, "container:%p, parent:%p, child %p,", container,parent,child); if (parent->layout == layout + || layout == L_NONE /* accept any layout for next/prev direction */ || (parent->layout == L_TABBED && layout == L_HORIZ) || (parent->layout == L_STACKED && layout == L_VERT) || is_auto_layout(parent->layout)) { From a0aa8d9780c6c8b0138800e3b2c2c0053174a2c5 Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 29 Dec 2016 20:26:35 +0100 Subject: [PATCH 08/30] cleanup in auto layouts - added L_AUTO_FIRST/LAST instead of using explicit layouts. - when switching between auto layout that don't share the same major axis, invert the width/height of their child views to preserve their relative proportions. --- include/sway/container.h | 3 ++ include/sway/layout.h | 2 +- sway/commands/layout.c | 48 ++++++++++++++++++++++++-------- sway/commands/workspace_layout.c | 10 ++++++- sway/container.c | 23 +++++++++++++-- sway/sway.5.txt | 2 +- 6 files changed, 70 insertions(+), 18 deletions(-) diff --git a/include/sway/container.h b/include/sway/container.h index 1d0fb265..f0574b1b 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -44,6 +44,9 @@ enum swayc_layouts { L_AUTO_TOP, L_AUTO_BOTTOM, + L_AUTO_FIRST = L_AUTO_LEFT, + L_AUTO_LAST = L_AUTO_BOTTOM, + // Keep last L_LAYOUTS, }; diff --git a/include/sway/layout.h b/include/sway/layout.h index 38096947..a771a72e 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -76,6 +76,6 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. enum swayc_layouts default_layout(swayc_t *output); inline bool is_auto_layout(enum swayc_layouts layout) { - return (layout >= L_AUTO_LEFT) && (layout <= L_AUTO_BOTTOM); + return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); } #endif diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 9e468c21..5426186e 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -56,24 +56,12 @@ struct cmd_results *cmd_layout(int argc, char **argv) { swayc_change_layout(parent, L_HORIZ); } } else if (strcasecmp(argv[0], "auto_left") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_LEFT); - } swayc_change_layout(parent, L_AUTO_LEFT); } else if (strcasecmp(argv[0], "auto_right") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_RIGHT); - } swayc_change_layout(parent, L_AUTO_RIGHT); } else if (strcasecmp(argv[0], "auto_top") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_TOP); - } swayc_change_layout(parent, L_AUTO_TOP); } else if (strcasecmp(argv[0], "auto_bot") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_BOTTOM); - } swayc_change_layout(parent, L_AUTO_BOTTOM); } else if (strcasecmp(argv[0], "incnmaster") == 0) { if ((error = checkarg(argc, "layout incnmaster", @@ -105,6 +93,42 @@ struct cmd_results *cmd_layout(int argc, char **argv) { ((int)container->parent->nb_slave_groups + inc >= 1)) { container->parent->nb_slave_groups += inc; } + } else if (strcasecmp(argv[0], "auto") == 0) { + if ((error = checkarg(argc, "auto", EXPECTED_EQUAL_TO, 2))) { + return error; + } + swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *parent = container->parent; + enum swayc_layouts layout; + if (strcasecmp(argv[1], "next") == 0) { + if (is_auto_layout(parent->layout) && parent->layout < L_AUTO_LAST) { + layout = parent->layout + 1; + } else { + layout = L_AUTO_FIRST; + } + } else if (strcasecmp(argv[1], "prev") == 0) { + if (is_auto_layout(parent->layout) && parent->layout > L_AUTO_FIRST) { + layout = parent->layout - 1; + } else { + layout = L_AUTO_FIRST; + } + } else { + return cmd_results_new(CMD_FAILURE, "layout auto", + "Must be one of ."); + } + swayc_change_layout(parent, layout); + } else if (strcasecmp(argv[0], "promote") == 0) { + // swap first child in auto layout with currently focused child + swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *parent = container->parent; + if (is_auto_layout(parent->layout)) { + int focused_idx = index_child(container); + swayc_t *first = parent->children->items[0]; + if (focused_idx > 0) { + list_swap(parent->children, 0, focused_idx); + swap_geometry(first, container); + } + } } } diff --git a/sway/commands/workspace_layout.c b/sway/commands/workspace_layout.c index b7b4b033..3e0a12ce 100644 --- a/sway/commands/workspace_layout.c +++ b/sway/commands/workspace_layout.c @@ -13,8 +13,16 @@ struct cmd_results *cmd_workspace_layout(int argc, char **argv) { config->default_layout = L_STACKED; } else if (strcasecmp(argv[0], "tabbed") == 0) { config->default_layout = L_TABBED; + } else if (strcasecmp(argv[0], "auto_left") == 0) { + config->default_layout = L_AUTO_LEFT; + } else if (strcasecmp(argv[0], "auto_right") == 0) { + config->default_layout = L_AUTO_RIGHT; + } else if (strcasecmp(argv[0], "auto_top") == 0) { + config->default_layout = L_AUTO_TOP; + } else if (strcasecmp(argv[0], "auto_bottom") == 0) { + config->default_layout = L_AUTO_BOTTOM; } else { - return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); + return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/container.c b/sway/container.c index d034115f..3bdc8b6c 100644 --- a/sway/container.c +++ b/sway/container.c @@ -962,11 +962,28 @@ swayc_t *swayc_tabbed_stacked_parent(swayc_t *con) { } swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { + // if layout change modifies the auto layout's major axis, swap width and height + // to preserve current ratios. + if (is_auto_layout(layout) && is_auto_layout(container->layout)) { + enum swayc_layouts prev_major = (container->layout == L_AUTO_LEFT || + container->layout == L_AUTO_RIGHT) + ? L_HORIZ : L_VERT; + enum swayc_layouts new_major = (layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT) + ? L_HORIZ : L_VERT; + if (new_major != prev_major) { + for (int i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + double h = child->height; + child->height = child->width; + child->width = h; + } + } + } if (container->type == C_WORKSPACE) { container->workspace_layout = layout; - if (layout == L_HORIZ || layout == L_VERT) { - container->layout = layout; - } + if (layout == L_HORIZ || layout == L_VERT || is_auto_layout(layout)) { + container->layout = layout; + } } else { container->layout = layout; } diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 8f8302f0..c0c4bfb2 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -371,7 +371,7 @@ The default colors are: switch to workspace 2, then invoke the "workspace 2" command again, you will be returned to workspace 1. Defaults to _no_. -**workspace_layout** :: +**workspace_layout** :: Specifies the start layout for new workspaces. **include** :: From 1b87193c3d63c8d884b5a45451a000d9b930521e Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 29 Dec 2016 20:30:32 +0100 Subject: [PATCH 09/30] Added "layout promote" command. --- common/list.c | 2 +- include/list.h | 2 ++ sway/sway.5.txt | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/common/list.c b/common/list.c index dd864a9b..39cc10e1 100644 --- a/common/list.c +++ b/common/list.c @@ -76,7 +76,7 @@ int list_seq_find(list_t *list, int compare(const void *item, const void *data), return -1; } -static void list_swap(list_t *list, int src, int dest) { +void list_swap(list_t *list, int src, int dest) { void *tmp = list->items[src]; list->items[src] = list->items[dest]; list->items[dest] = tmp; diff --git a/include/list.h b/include/list.h index f478b6bb..7eead4ac 100644 --- a/include/list.h +++ b/include/list.h @@ -22,4 +22,6 @@ void list_qsort(list_t *list, int compare(const void *left, const void *right)); int list_seq_find(list_t *list, int compare(const void *item, const void *cmp_to), const void *cmp_to); // stable sort since qsort is not guaranteed to be stable void list_stable_sort(list_t *list, int compare(const void *a, const void *b)); +// swap two elements in a list +void list_swap(list_t *list, int src, int dest); #endif diff --git a/sway/sway.5.txt b/sway/sway.5.txt index c0c4bfb2..b58fbe55 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -92,6 +92,9 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( focused container. can be a positive or negative integer. These commands only have an effect if the focused container uses one of the "auto" layouts. +**layout** promote:: + Swap the focused element with the first in the one of the auto layouts. + **move** :: Moves the focused container _left_, _right_, _up_, or _down_. Moving to _prev_ or _next_ swaps the container with its sibling in the same From 4b1d9b058e41d89dd25e765aee2b8c987a534303 Mon Sep 17 00:00:00 2001 From: wil Date: Fri, 30 Dec 2016 18:03:29 +0100 Subject: [PATCH 10/30] Added a word in the Readme about the purpose of the fork. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 6b038045..6e714cb9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # sway [![](https://api.travis-ci.org/SirCmpwn/sway.svg)](https://travis-ci.org/SirCmpwn/sway) [![Donate with fosspay](https://drewdevault.com/donate/static/donate-with-fosspay.png)](https://drewdevault.com/donate?project=4) "**S**irCmpwn's **Way**land compositor" is a **work in progress** @@ -10,6 +11,10 @@ irc.freenode.net). [More screenshots](https://github.com/SirCmpwn/sway/wiki/Screenshots-of-Sway) +This fork attempts to add +[Awesome](http://awesomewm.org)/[Monad](http://xmonad.org) style auto layouts +to Sway. For us lazies that just want things to happen automatically! + ## Release Signatures Releases are signed with [B22DA89A](http://pgp.mit.edu/pks/lookup?op=vindex&search=0x52CB6609B22DA89A) From 15745abf0cb4948fcc750a9e78139d3c02e1c1f0 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 31 Dec 2016 18:41:13 +0100 Subject: [PATCH 11/30] [fix] cycle auto layouts backwards --- sway/commands/layout.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 5426186e..d908b95c 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -110,7 +110,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if (is_auto_layout(parent->layout) && parent->layout > L_AUTO_FIRST) { layout = parent->layout - 1; } else { - layout = L_AUTO_FIRST; + layout = L_AUTO_LAST; } } else { return cmd_results_new(CMD_FAILURE, "layout auto", From c01b89839874239bee7948fad004b19a19917432 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 1 Jan 2017 12:36:47 -0500 Subject: [PATCH 12/30] Fix inline is_auto_layout --- include/sway/layout.h | 5 ++--- sway/layout.c | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/sway/layout.h b/include/sway/layout.h index a771a72e..d7fe748d 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -75,7 +75,6 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. */ enum swayc_layouts default_layout(swayc_t *output); -inline bool is_auto_layout(enum swayc_layouts layout) { - return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); -} +bool is_auto_layout(enum swayc_layouts layout); + #endif diff --git a/sway/layout.c b/sway/layout.c index 2de6da45..2195863e 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1487,3 +1487,7 @@ enum swayc_layouts default_layout(swayc_t *output) { return L_VERT; } } + +bool is_auto_layout(enum swayc_layouts layout) { + return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); +} From 0412e95ba99581a32513eb241e6ac2c4e4ed03dc Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 1 Jan 2017 12:41:52 -0500 Subject: [PATCH 13/30] Document new layout command syntax And an old one that the docs overlooked --- sway/sway.5.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sway/sway.5.txt b/sway/sway.5.txt index b58fbe55..87b02f9f 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -87,11 +87,17 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( _splitv_, _toggle split_, _stacking_, _tabbed_, _auto_left_, _auto_right_, _auto_top, _auto_bottom_. +**layout** auto :: + Cycles between available auto layouts. + **layout** :: Modify the number of master elements, respectively slave columns, in the focused container. can be a positive or negative integer. These commands only have an effect if the focused container uses one of the "auto" layouts. +**layout** toggle split:: + Cycles between available split layouts. + **layout** promote:: Swap the focused element with the first in the one of the auto layouts. From a62048f15d9381e3040bd45965233e59a041eab7 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 1 Jan 2017 19:53:53 +0100 Subject: [PATCH 14/30] changed "layout promote" command to "move first" This is more consistent with other Sway semantics. --- include/sway/focus.h | 3 ++- sway/commands/layout.c | 12 ------------ sway/commands/move.c | 4 +++- sway/layout.c | 16 +++++++++++++++- sway/sway.5.txt | 12 +++++------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/include/sway/focus.h b/include/sway/focus.h index 30c9e108..652cdccc 100644 --- a/include/sway/focus.h +++ b/include/sway/focus.h @@ -8,7 +8,8 @@ enum movement_direction { MOVE_PARENT, MOVE_CHILD, MOVE_NEXT, - MOVE_PREV + MOVE_PREV, + MOVE_FIRST }; #include "container.h" diff --git a/sway/commands/layout.c b/sway/commands/layout.c index d908b95c..2da65765 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -117,18 +117,6 @@ struct cmd_results *cmd_layout(int argc, char **argv) { "Must be one of ."); } swayc_change_layout(parent, layout); - } else if (strcasecmp(argv[0], "promote") == 0) { - // swap first child in auto layout with currently focused child - swayc_t *container = get_focused_view(swayc_active_workspace()); - swayc_t *parent = container->parent; - if (is_auto_layout(parent->layout)) { - int focused_idx = index_child(container); - swayc_t *first = parent->children->items[0]; - if (focused_idx > 0) { - list_swap(parent->children, 0, focused_idx); - swap_geometry(first, container); - } - } } } diff --git a/sway/commands/move.c b/sway/commands/move.c index 4f6bc76f..0b134494 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -13,7 +13,7 @@ struct cmd_results *cmd_move(int argc, char **argv) { if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) { return error; } - const char* expected_syntax = "Expected 'move ' or " + const char* expected_syntax = "Expected 'move ' or " "'move to workspace ' or " "'move to output ' or " "'move position mouse'"; @@ -31,6 +31,8 @@ struct cmd_results *cmd_move(int argc, char **argv) { move_container(view, MOVE_NEXT); } else if (strcasecmp(argv[0], "prev") == 0) { move_container(view, MOVE_PREV); + } else if (strcasecmp(argv[0], "first") == 0) { + move_container(view, MOVE_FIRST); } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) { // "move container ... if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) { diff --git a/sway/layout.c b/sway/layout.c index 2de6da45..6212ec75 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -252,6 +252,7 @@ void swap_geometry(swayc_t *a, swayc_t *b) { void move_container(swayc_t *container, enum movement_direction dir) { enum swayc_layouts layout = L_NONE; + swayc_t *parent = container->parent; if (container->is_floating || (container->type != C_VIEW && container->type != C_CONTAINER)) { return; @@ -260,10 +261,23 @@ void move_container(swayc_t *container, enum movement_direction dir) { layout = L_VERT; } else if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { layout = L_HORIZ; + } else if (dir == MOVE_FIRST) { + // swap first child in auto layout with currently focused child + if (is_auto_layout(parent->layout)) { + int focused_idx = index_child(container); + swayc_t *first = parent->children->items[0]; + if (focused_idx > 0) { + list_swap(parent->children, 0, focused_idx); + swap_geometry(first, container); + } + arrange_windows(parent->parent, -1, -1); + ipc_event_window(container, "move"); + set_focused_container_for(parent->parent, container); + } + return; } else if (! (dir == MOVE_NEXT || dir == MOVE_PREV)) { return; } - swayc_t *parent = container->parent; swayc_t *child = container; bool ascended = false; diff --git a/sway/sway.5.txt b/sway/sway.5.txt index b58fbe55..eece4b5b 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -92,13 +92,11 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( focused container. can be a positive or negative integer. These commands only have an effect if the focused container uses one of the "auto" layouts. -**layout** promote:: - Swap the focused element with the first in the one of the auto layouts. - -**move** :: - Moves the focused container _left_, _right_, _up_, or _down_. Moving - to _prev_ or _next_ swaps the container with its sibling in the same - container. +**move** :: + Moves the focused container _left_, _right_, _up_, or _down_. Moving to _prev_ + or _next_ swaps the container with its sibling in the same container. Move + _first_ exchanges the focused element in an auto layout with the first + element, i.e. promotes the focused element to master position. **move** to workspace :: Moves the focused container to the workspace identified by _name_. From 97f70987d70315c683fd1e16c731b396679f6b96 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 1 Jan 2017 21:52:49 +0100 Subject: [PATCH 15/30] [fix] cleanups suggested by Sway community --- README.md | 5 --- include/sway/container.h | 4 +-- include/sway/layout.h | 4 +-- sway/commands/layout.c | 22 ++++++++++--- sway/commands/resize.c | 70 ++++++++++++++++++++-------------------- sway/layout.c | 44 +++++++++++++------------ 6 files changed, 79 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 6e714cb9..6b038045 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - # sway [![](https://api.travis-ci.org/SirCmpwn/sway.svg)](https://travis-ci.org/SirCmpwn/sway) [![Donate with fosspay](https://drewdevault.com/donate/static/donate-with-fosspay.png)](https://drewdevault.com/donate?project=4) "**S**irCmpwn's **Way**land compositor" is a **work in progress** @@ -11,10 +10,6 @@ irc.freenode.net). [More screenshots](https://github.com/SirCmpwn/sway/wiki/Screenshots-of-Sway) -This fork attempts to add -[Awesome](http://awesomewm.org)/[Monad](http://xmonad.org) style auto layouts -to Sway. For us lazies that just want things to happen automatically! - ## Release Signatures Releases are signed with [B22DA89A](http://pgp.mit.edu/pks/lookup?op=vindex&search=0x52CB6609B22DA89A) diff --git a/include/sway/container.h b/include/sway/container.h index f0574b1b..ff65628c 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -158,12 +158,12 @@ struct sway_container { /** * Number of master views in auto layouts. */ - uint32_t nb_master; + size_t nb_master; /** * Number of slave groups (e.g. columns) in auto layouts. */ - uint32_t nb_slave_groups; + size_t nb_slave_groups; }; enum visibility_mask { diff --git a/include/sway/layout.h b/include/sway/layout.h index a771a72e..8cc513d8 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -75,7 +75,5 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. */ enum swayc_layouts default_layout(swayc_t *output); -inline bool is_auto_layout(enum swayc_layouts layout) { - return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); -} +bool is_auto_layout(enum swayc_layouts layout); #endif diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 2da65765..0cdac1b4 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -64,11 +64,18 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else if (strcasecmp(argv[0], "auto_bot") == 0) { swayc_change_layout(parent, L_AUTO_BOTTOM); } else if (strcasecmp(argv[0], "incnmaster") == 0) { - if ((error = checkarg(argc, "layout incnmaster", + const char *name = "layout incnmaster"; + if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 2))) { return error; } - int inc = (int) strtol(argv[1], NULL, 10); + char *end; + int inc = (int) strtol(argv[1], &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(argument must be an integer)", name); + + } swayc_t *container = get_focused_view(swayc_active_workspace()); if (container && inc && is_auto_layout(container->parent->layout) && @@ -83,11 +90,18 @@ struct cmd_results *cmd_layout(int argc, char **argv) { container->parent->nb_master += inc; } } else if ((strcasecmp(argv[0], "incncol") == 0) && argc ==2) { - if ((error = checkarg(argc, "layout incncol", + const char *name = "layout incncol"; + if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 2))) { return error; } - int inc = (int) strtol(argv[1], NULL, 10); + char *end; + int inc = (int) strtol(argv[1], &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, name, "Invalid %s command " + "(argument must be an integer)", name); + + } swayc_t *container = get_focused_view(swayc_active_workspace()); if (container && inc && is_auto_layout(container->parent->layout) && ((int)container->parent->nb_slave_groups + inc >= 1)) { diff --git a/sway/commands/resize.c b/sway/commands/resize.c index 9a756e81..1c052286 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -66,7 +66,7 @@ static bool resize_floating(int amount, bool use_width) { * Return the number of children in the slave groups. This corresponds to the children * that are not members of the master group. */ -static inline uint_fast32_t slave_count(swayc_t *container) { +static inline size_t auto_slave_count(swayc_t *container) { return container->children->length - container->nb_master; } @@ -75,13 +75,13 @@ static inline uint_fast32_t slave_count(swayc_t *container) { * given the index of a container's child, return the index of the first child of the group * which index is a member of. */ -static int group_start_index(swayc_t *container, int index) { - if ((index < 0) || (! is_auto_layout(container->layout)) || - ((uint_fast32_t) index < container->nb_master)) { +static int auto_group_start_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout) + || (size_t) index < container->nb_master) { return 0; } else { - uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; - uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; int start_idx; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; if (index < idx2) { @@ -98,16 +98,16 @@ static int group_start_index(swayc_t *container, int index) { * that follows the one which index is a member of. * This makes the function usable to walk through the groups in a container. */ -static int group_end_index(swayc_t *container, int index) { +static int auto_group_end_index(swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout)) { return container->children->length; } else { int nxt_idx; - if ((uint_fast32_t)index < container->nb_master) { + if ((size_t)index < container->nb_master) { nxt_idx = container->nb_master; } else { - uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; - uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; if (index < idx2) { nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; @@ -122,30 +122,30 @@ static int group_end_index(swayc_t *container, int index) { /** * Return the combined number of master and slave groups in the container. */ -static inline uint_fast32_t group_count(swayc_t *container) { - return MIN(container->nb_slave_groups, slave_count(container)) + (container->nb_master ? 1 : 0); +static inline size_t auto_group_count(swayc_t *container) { + return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); } /** * return the index of the Group containing th child of . * The index is the order of the group along the container's major axis (starting at 0). */ -static uint_fast32_t group_index(swayc_t *container, int index) { +static size_t auto_group_index(swayc_t *container, int index) { if (index < 0) { return 0; } bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); - int nb_slaves = slave_count(container); - if ((uint_fast32_t) index < container->nb_master) { + int nb_slaves = auto_slave_count(container); + if ((size_t) index < container->nb_master) { if (master_first || nb_slaves <= 0) { return 0; } else { return MIN(container->nb_slave_groups, nb_slaves); } } else { - uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; - uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; - uint_fast32_t grp_idx; + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + size_t grp_idx; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; if (index < idx2) { grp_idx = (index - container->nb_master) / grp_sz; @@ -161,16 +161,16 @@ static bool resize_tiled(int amount, bool use_width) { swayc_t *parent = container->parent; int idx_focused = 0; bool use_major = false; - uint_fast32_t nb_before = 0; - uint_fast32_t nb_after = 0; + size_t nb_before = 0; + size_t nb_after = 0; // 1. Identify a container ancestor that will allow the focused child to grow in the requested // direction. while (container->parent) { parent = container->parent; - if ((parent->children && parent->children->length > 1) && - (is_auto_layout(parent->layout) || (use_width ? parent->layout == L_HORIZ : - parent->layout == L_VERT))) { + if ((parent->children && parent->children->length > 1) + && (is_auto_layout(parent->layout) + || (use_width ? parent->layout == L_HORIZ : parent->layout == L_VERT))) { // check if container has siblings that can provide/absorb the space needed for // the resize operation. use_major = use_width @@ -185,15 +185,15 @@ static bool resize_tiled(int amount, bool use_width) { continue; } if (use_major) { - nb_before = group_index(parent, idx_focused); - nb_after = group_count(parent) - nb_before - 1; + nb_before = auto_group_index(parent, idx_focused); + nb_after = auto_group_count(parent) - nb_before - 1; } else { - nb_before = idx_focused - group_start_index(parent, idx_focused); - nb_after = group_end_index(parent, idx_focused) - idx_focused - 1; + nb_before = idx_focused - auto_group_start_index(parent, idx_focused); + nb_after = auto_group_end_index(parent, idx_focused) - idx_focused - 1; sway_log(L_DEBUG, "+++ focused: %d, start: %d, end: %d, before: %d, after: %d", idx_focused, - (int)group_start_index(parent, idx_focused), - (int)group_end_index(parent, idx_focused), + (int)auto_group_start_index(parent, idx_focused), + (int)auto_group_end_index(parent, idx_focused), (int)nb_before, (int)nb_after); } @@ -206,14 +206,14 @@ static bool resize_tiled(int amount, bool use_width) { if (parent == &root_container) { return true; } - sway_log(L_DEBUG, "Found the proper parent: %p. It has %" PRIuFAST32 " before conts, and %" - PRIuFAST32 " after conts", parent, nb_before, nb_after); + sway_log(L_DEBUG, "Found the proper parent: %p. It has %zu before conts, " + "and %zu after conts", parent, nb_before, nb_after); // 2. Ensure that the resize operation will not make one of the resized containers drop // below the "sane" size threshold. bool valid = true; swayc_t *focused = parent->children->items[idx_focused]; - int start = use_major ? 0 : group_start_index(parent, idx_focused); - int end = use_major ? parent->children->length : group_end_index(parent, idx_focused); + int start = use_major ? 0 : auto_group_start_index(parent, idx_focused); + int end = use_major ? parent->children->length : auto_group_end_index(parent, idx_focused); sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end); for (int i = start; i < end; ) { swayc_t *sibling = parent->children->items[i]; @@ -235,13 +235,13 @@ static bool resize_tiled(int amount, bool use_width) { sway_log(L_DEBUG, "Container size no longer sane"); break; } - i = use_major ? group_end_index(parent, i) : (i + 1); + i = use_major ? auto_group_end_index(parent, i) : (i + 1); sway_log(L_DEBUG, "+++++ check %i", i); } // 3. Apply the size change if (valid) { for (int i = start; i < end; ) { - int next_i = use_major ? group_end_index(parent, i) : (i + 1); + int next_i = use_major ? auto_group_end_index(parent, i) : (i + 1); swayc_t *sibling = parent->children->items[i]; double pixels = amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; diff --git a/sway/layout.c b/sway/layout.c index 6212ec75..faab9196 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -253,8 +253,7 @@ void swap_geometry(swayc_t *a, swayc_t *b) { void move_container(swayc_t *container, enum movement_direction dir) { enum swayc_layouts layout = L_NONE; swayc_t *parent = container->parent; - if (container->is_floating - || (container->type != C_VIEW && container->type != C_CONTAINER)) { + if (container->is_floating || (container->type != C_VIEW && container->type != C_CONTAINER)) { return; } if (dir == MOVE_UP || dir == MOVE_DOWN) { @@ -323,14 +322,14 @@ void move_container(swayc_t *container, enum movement_direction dir) { // if move command makes container change from master to slave // (or the contrary), reset its geometry an the one of the replaced item. if (parent->nb_master && - (uint_fast32_t) parent->children->length > parent->nb_master) { + (size_t) parent->children->length > parent->nb_master) { swayc_t *swap_geom = NULL; // if child is being promoted/demoted, it will swap geometry // with the sibling being demoted/promoted. if ((dir == MOVE_NEXT && desired == 0) - || (dir == MOVE_PREV && (uint_fast32_t) desired == parent->nb_master - 1)) { + || (dir == MOVE_PREV && (size_t) desired == parent->nb_master - 1)) { swap_geom = parent->children->items[parent->nb_master - 1]; - } else if ((dir == MOVE_NEXT && (uint_fast32_t) desired == parent->nb_master) + } else if ((dir == MOVE_NEXT && (size_t) desired == parent->nb_master) || (dir == MOVE_PREV && desired == parent->children->length - 1)) { swap_geom = parent->children->items[parent->nb_master]; } @@ -822,20 +821,24 @@ void update_geometry(swayc_t *container) { } } +bool is_auto_layout(enum swayc_layouts layout) { + return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); +} + /** * Layout application prototypes */ static void apply_horiz_layout(swayc_t *container, const double x, - const double y, const double width, - const double height, const int start, - const int end); + const double y, const double width, + const double height, const int start, + const int end); static void apply_vert_layout(swayc_t *container, const double x, - const double y, const double width, - const double height, const int start, - const int end); + const double y, const double width, + const double height, const int start, + const int end); static void apply_tabbed_or_stacked_layout(swayc_t *container, double x, - double y, double width, - double height); + double y, double width, + double height); static void apply_auto_layout(swayc_t *container, const double x, const double y, const double width, const double height, @@ -1164,8 +1167,8 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, // a single slave group (containing slave 1 and 2). The master // group and slave group are layed out using L_VERT. - uint_fast32_t nb_slaves = container->children->length - container->nb_master; - uint_fast32_t nb_groups = (container->nb_master > 0 ? 1 : 0) + + size_t nb_slaves = container->children->length - container->nb_master; + size_t nb_groups = (container->nb_master > 0 ? 1 : 0) + MIN(container->nb_slave_groups, nb_slaves); // the target dimension of the container along the "major" axis, each @@ -1186,9 +1189,9 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, // height and width of next group to be laid out. const double *group_h, *group_w; - switch(group_layout) { + switch (group_layout) { default: - sway_log(L_ERROR, "Unknown layout type (%d) used in %s()", + sway_log(L_DEBUG, "Unknown layout type (%d) used in %s()", group_layout, __func__); /* fall through */ case L_VERT: @@ -1216,7 +1219,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, * layout. */ double old_group_dim[nb_groups]; double old_dim = 0; - uint_fast32_t group = 0; + size_t group = 0; for (int i = 0; i < container->children->length;) { swayc_t *child = container->children->items[i]; double *dim = group_layout == L_HORIZ ? &child->height : &child->width; @@ -1252,7 +1255,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, for (group = 0; group < nb_groups; ++group) { // column to include next by increasing position. - uint_fast32_t layout_group = master_first ? group : (group + 1) % nb_groups; + size_t layout_group = master_first ? group : (group + 1) % nb_groups; // adjusted size of the group group_dim = old_group_dim[layout_group] * scale; @@ -1270,8 +1273,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, if (group == nb_groups - 1) { group_dim = pos_maj + dim_maj - pos; // remaining width } - sway_log(L_DEBUG, "Arranging container %p column %" PRIuFAST32 - ", children [%d,%d[ (%fx%f+%f,%f)", + sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", container, group, start, end, *group_w, *group_h, *group_x, *group_y); switch (group_layout) { default: From bd415029ba72425c97647b55fce19213e7909cbc Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 17:41:15 +0100 Subject: [PATCH 16/30] Moved auto_* layout functions from resize.c to layout.c --- include/sway/layout.h | 4 ++ sway/commands/resize.c | 98 +----------------------------------------- sway/layout.c | 93 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 96 deletions(-) diff --git a/include/sway/layout.h b/include/sway/layout.h index d7fe748d..c51fece9 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -76,5 +76,9 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. enum swayc_layouts default_layout(swayc_t *output); bool is_auto_layout(enum swayc_layouts layout); +int auto_group_start_index(swayc_t *container, int index); +int auto_group_end_index(swayc_t *container, int index); +size_t auto_group_count(swayc_t *container); +size_t auto_group_index(swayc_t *container, int index); #endif diff --git a/sway/commands/resize.c b/sway/commands/resize.c index 1c052286..c391945f 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -62,100 +62,6 @@ static bool resize_floating(int amount, bool use_width) { return false; } -/** - * Return the number of children in the slave groups. This corresponds to the children - * that are not members of the master group. - */ -static inline size_t auto_slave_count(swayc_t *container) { - return container->children->length - container->nb_master; - -} - -/** - * given the index of a container's child, return the index of the first child of the group - * which index is a member of. - */ -static int auto_group_start_index(swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout) - || (size_t) index < container->nb_master) { - return 0; - } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - int start_idx; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; - } else { - start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); - } - return MIN(start_idx, container->children->length); - } -} - -/** - * given the index of a container's child, return the index of the first child of the group - * that follows the one which index is a member of. - * This makes the function usable to walk through the groups in a container. - */ -static int auto_group_end_index(swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout)) { - return container->children->length; - } else { - int nxt_idx; - if ((size_t)index < container->nb_master) { - nxt_idx = container->nb_master; - } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; - } else { - nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); - } - } - return MIN(nxt_idx, container->children->length); - } -} - -/** - * Return the combined number of master and slave groups in the container. - */ -static inline size_t auto_group_count(swayc_t *container) { - return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); -} - -/** - * return the index of the Group containing th child of . - * The index is the order of the group along the container's major axis (starting at 0). - */ -static size_t auto_group_index(swayc_t *container, int index) { - if (index < 0) { - return 0; - } - bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); - int nb_slaves = auto_slave_count(container); - if ((size_t) index < container->nb_master) { - if (master_first || nb_slaves <= 0) { - return 0; - } else { - return MIN(container->nb_slave_groups, nb_slaves); - } - } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - size_t grp_idx; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - grp_idx = (index - container->nb_master) / grp_sz; - } else { - grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; - } - return grp_idx + (master_first ? 1 : 0); - } -} - static bool resize_tiled(int amount, bool use_width) { swayc_t *container = get_focused_view(swayc_active_workspace()); swayc_t *parent = container->parent; @@ -229,8 +135,8 @@ static bool resize_tiled(int amount, bool use_width) { } sway_log(L_DEBUG, "Check container %p: width %g vs %d, height %g vs %d", sibling, sibling->width + pixels, min_sane_w, sibling->height + pixels, min_sane_h); if (use_width ? - sibling->width + pixels < min_sane_w : - sibling->height + pixels < min_sane_h) { + sibling->width + pixels < min_sane_w : + sibling->height + pixels < min_sane_h) { valid = false; sway_log(L_DEBUG, "Container size no longer sane"); break; diff --git a/sway/layout.c b/sway/layout.c index 377dad47..5f8da9e6 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1503,3 +1503,96 @@ enum swayc_layouts default_layout(swayc_t *output) { bool is_auto_layout(enum swayc_layouts layout) { return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); } + +/** + * Return the number of children in the slave groups. This corresponds to the children + * that are not members of the master group. + */ +static inline size_t auto_slave_count(swayc_t *container) { + return container->children->length - container->nb_master; +} + +/** + * given the index of a container's child, return the index of the first child of the group + * which index is a member of. + */ +int auto_group_start_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout) + || (size_t) index < container->nb_master) { + return 0; + } else { + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + int start_idx; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; + } else { + start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); + } + return MIN(start_idx, container->children->length); + } +} + +/** + * given the index of a container's child, return the index of the first child of the group + * that follows the one which index is a member of. + * This makes the function usable to walk through the groups in a container. + */ +int auto_group_end_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout)) { + return container->children->length; + } else { + int nxt_idx; + if ((size_t)index < container->nb_master) { + nxt_idx = container->nb_master; + } else { + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; + } else { + nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); + } + } + return MIN(nxt_idx, container->children->length); + } +} + +/** + * Return the combined number of master and slave groups in the container. + */ +size_t auto_group_count(swayc_t *container) { + return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); +} + +/** + * return the index of the Group containing th child of . + * The index is the order of the group along the container's major axis (starting at 0). + */ +size_t auto_group_index(swayc_t *container, int index) { + if (index < 0) { + return 0; + } + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + int nb_slaves = auto_slave_count(container); + if ((size_t) index < container->nb_master) { + if (master_first || nb_slaves <= 0) { + return 0; + } else { + return MIN(container->nb_slave_groups, nb_slaves); + } + } else { + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + size_t grp_idx; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + grp_idx = (index - container->nb_master) / grp_sz; + } else { + grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; + } + return grp_idx + (master_first ? 1 : 0); + } +} From d99efb5f6f37d9ffbb6794df8964d2537f75eeda Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 18:09:42 +0100 Subject: [PATCH 17/30] [fix] corner cases win nb_children < nb_master|nb_col --- sway/layout.c | 61 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index 5f8da9e6..9661f505 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1504,12 +1504,33 @@ bool is_auto_layout(enum swayc_layouts layout) { return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); } +/** + * Return the number of master elements in a container + */ +static inline size_t auto_master_count(swayc_t *container) { + return MIN(container->nb_master, container->children->length); +} + /** * Return the number of children in the slave groups. This corresponds to the children * that are not members of the master group. */ static inline size_t auto_slave_count(swayc_t *container) { - return container->children->length - container->nb_master; + return container->children->length - auto_master_count(container); +} + +/** + * Return the number of slave groups in the container. + */ +size_t auto_slave_group_count(swayc_t *container) { + return MIN(container->nb_slave_groups, auto_slave_count(container)); +} + +/** + * Return the combined number of master and slave groups in the container. + */ +size_t auto_group_count(swayc_t *container) { + return auto_slave_group_count(container) + (container->nb_master ? 1 : 0); } /** @@ -1521,10 +1542,12 @@ int auto_group_start_index(swayc_t *container, int index) { || (size_t) index < container->nb_master) { return 0; } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + size_t nb_slaves = auto_slave_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; int start_idx; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; if (index < idx2) { start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; } else { @@ -1545,11 +1568,13 @@ int auto_group_end_index(swayc_t *container, int index) { } else { int nxt_idx; if ((size_t)index < container->nb_master) { - nxt_idx = container->nb_master; + nxt_idx = auto_master_count(container); } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + size_t nb_slaves = auto_slave_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; if (index < idx2) { nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; } else { @@ -1560,13 +1585,6 @@ int auto_group_end_index(swayc_t *container, int index) { } } -/** - * Return the combined number of master and slave groups in the container. - */ -size_t auto_group_count(swayc_t *container) { - return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); -} - /** * return the index of the Group containing th child of . * The index is the order of the group along the container's major axis (starting at 0). @@ -1576,22 +1594,23 @@ size_t auto_group_index(swayc_t *container, int index) { return 0; } bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); - int nb_slaves = auto_slave_count(container); + size_t nb_slaves = auto_slave_count(container); if ((size_t) index < container->nb_master) { if (master_first || nb_slaves <= 0) { return 0; } else { - return MIN(container->nb_slave_groups, nb_slaves); + return auto_slave_group_count(container); } } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + size_t nb_slave_grp = auto_slave_group_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + int idx2 = (nb_slave_grp - remainder) * grp_sz + container->nb_master; size_t grp_idx; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; if (index < idx2) { grp_idx = (index - container->nb_master) / grp_sz; } else { - grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; + grp_idx = (nb_slave_grp - remainder) + (index - idx2) / (grp_sz + 1) ; } return grp_idx + (master_first ? 1 : 0); } From f7269684d018658c508f82529f5ca24c02dfd227 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 18:15:42 +0100 Subject: [PATCH 18/30] [fix] scale check to prevent un-necessary layouts was in the wrong place. --- sway/layout.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index 9661f505..78673cf9 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1031,11 +1031,11 @@ void apply_horiz_layout(swayc_t *container, const double x, const double y, } scale += *old_width; } + scale = width / scale; // Resize windows double child_x = x; if (scale > 0.1) { - scale = width / scale; sway_log(L_DEBUG, "Arranging %p horizontally", container); swayc_t *focused = NULL; for (int i = start; i < end; ++i) { @@ -1084,10 +1084,11 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, } scale += *old_height; } + scale = height / scale; + // Resize double child_y = y; if (scale > 0.1) { - scale = height / scale; sway_log(L_DEBUG, "Arranging %p vertically", container); swayc_t *focused = NULL; for (i = start; i < end; ++i) { From 3c84250be8a31ec08f3c8057d589b5b040673b36 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 18:20:13 +0100 Subject: [PATCH 19/30] [fix] resize should now preserve surrounding container's dimensions - prior to this modification, the requested pixels were added/removed to both edges of the modified container. To preserve sizes, only half the pixels should be added/removed to each edge. --- sway/commands/resize.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sway/commands/resize.c b/sway/commands/resize.c index c391945f..28b20dc4 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -175,15 +175,15 @@ static bool resize_tiled(int amount, bool use_width) { } else { if (use_major) { for (int j = i; j < next_i; ++j) { - recursive_resize(parent->children->items[j], pixels, + recursive_resize(parent->children->items[j], pixels / 2, use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); - recursive_resize(parent->children->items[j], pixels, + recursive_resize(parent->children->items[j], pixels / 2, use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); } } else { - recursive_resize(sibling, pixels, + recursive_resize(sibling, pixels / 2, use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); - recursive_resize(sibling, pixels, + recursive_resize(sibling, pixels / 2, use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); } } From 1f47c58d63130b8de59cb81422a4339bc0273273 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 20:26:46 +0100 Subject: [PATCH 20/30] simplification of apply_auto_layout Achieved by introducing auto_group_bounds function that produces the start/end indexes of a group inside an auto layot container. --- include/sway/layout.h | 9 +-- sway/layout.c | 158 ++++++++++++++++++++++++------------------ 2 files changed, 95 insertions(+), 72 deletions(-) diff --git a/include/sway/layout.h b/include/sway/layout.h index c51fece9..fbedcdb3 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -76,9 +76,10 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. enum swayc_layouts default_layout(swayc_t *output); bool is_auto_layout(enum swayc_layouts layout); -int auto_group_start_index(swayc_t *container, int index); -int auto_group_end_index(swayc_t *container, int index); -size_t auto_group_count(swayc_t *container); -size_t auto_group_index(swayc_t *container, int index); +int auto_group_start_index(const swayc_t *container, int index); +int auto_group_end_index(const swayc_t *container, int index); +size_t auto_group_count(const swayc_t *container); +size_t auto_group_index(const swayc_t *container, int index); +bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end); #endif diff --git a/sway/layout.c b/sway/layout.c index 78673cf9..6a2af2a6 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1164,9 +1164,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, // a single slave group (containing slave 1 and 2). The master // group and slave group are layed out using L_VERT. - size_t nb_slaves = container->children->length - container->nb_master; - size_t nb_groups = (container->nb_master > 0 ? 1 : 0) + - MIN(container->nb_slave_groups, nb_slaves); + size_t nb_groups = auto_group_count(container); // the target dimension of the container along the "major" axis, each // group in the container will be layed out using "group_layout" along @@ -1216,74 +1214,53 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, * layout. */ double old_group_dim[nb_groups]; double old_dim = 0; - size_t group = 0; - for (int i = 0; i < container->children->length;) { - swayc_t *child = container->children->items[i]; - double *dim = group_layout == L_HORIZ ? &child->height : &child->width; - if (*dim <= 0) { - // New child with uninitialized dimension - *dim = dim_maj; - if (nb_groups > 1) { - // child gets a dimension proportional to existing groups, - // it will be later scaled based on to the available size - // in the major axis. - *dim /= (nb_groups - 1); + for (size_t group = 0; group < nb_groups; ++group) { + int idx; + if (auto_group_bounds(container, group, &idx, NULL)) { + swayc_t *child = container->children->items[idx]; + double *dim = group_layout == L_HORIZ ? &child->height : &child->width; + if (*dim <= 0) { + // New child with uninitialized dimension + *dim = dim_maj; + if (nb_groups > 1) { + // child gets a dimension proportional to existing groups, + // it will be later scaled based on to the available size + // in the major axis. + *dim /= (nb_groups - 1); + } } + old_dim += *dim; + old_group_dim[group] = *dim; } - if (i == 0 && container->nb_master > 0) { - i += container->nb_master; - } else { - i += (nb_slaves - i + container->nb_master) / (nb_groups - group); - } - old_dim += *dim; - old_group_dim[group++] = *dim; } - double scale = dim_maj / old_dim; /* Apply layout to each group */ pos = pos_maj; - // first child in the current group - int start; - - // index immediately after the last child in the current group - int end = 0; - - for (group = 0; group < nb_groups; ++group) { - // column to include next by increasing position. - size_t layout_group = master_first ? group : (group + 1) % nb_groups; - - // adjusted size of the group - group_dim = old_group_dim[layout_group] * scale; - if (container->nb_master > 0 && layout_group == 0) { - start = 0; - end = MIN(container->nb_master, container->children->length); - } else { - if (group == 0) { - start = container->nb_master; - } else { - start = end; + for (size_t group = 0; group < nb_groups; ++group) { + int start, end; // index of first (inclusive) and last (exclusive) child in the group + if (auto_group_bounds(container, group, &start, &end)) { + // adjusted size of the group + group_dim = old_group_dim[group] * scale; + if (group == nb_groups - 1) { + group_dim = pos_maj + dim_maj - pos; // remaining width + } + sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", + container, group, start, end, *group_w, *group_h, *group_x, *group_y); + switch (group_layout) { + default: + case L_VERT: + apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; + case L_HORIZ: + apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; } - end = start + (nb_slaves - start + container->nb_master) / (nb_groups - layout_group); - } - if (group == nb_groups - 1) { - group_dim = pos_maj + dim_maj - pos; // remaining width - } - sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", - container, group, start, end, *group_w, *group_h, *group_x, *group_y); - switch (group_layout) { - default: - case L_VERT: - apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); - break; - case L_HORIZ: - apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); - break; - } - /* update position for next group */ - pos += group_dim; + /* update position for next group */ + pos += group_dim; + } } } @@ -1508,7 +1485,7 @@ bool is_auto_layout(enum swayc_layouts layout) { /** * Return the number of master elements in a container */ -static inline size_t auto_master_count(swayc_t *container) { +static inline size_t auto_master_count(const swayc_t *container) { return MIN(container->nb_master, container->children->length); } @@ -1516,21 +1493,21 @@ static inline size_t auto_master_count(swayc_t *container) { * Return the number of children in the slave groups. This corresponds to the children * that are not members of the master group. */ -static inline size_t auto_slave_count(swayc_t *container) { +static inline size_t auto_slave_count(const swayc_t *container) { return container->children->length - auto_master_count(container); } /** * Return the number of slave groups in the container. */ -size_t auto_slave_group_count(swayc_t *container) { +size_t auto_slave_group_count(const swayc_t *container) { return MIN(container->nb_slave_groups, auto_slave_count(container)); } /** * Return the combined number of master and slave groups in the container. */ -size_t auto_group_count(swayc_t *container) { +size_t auto_group_count(const swayc_t *container) { return auto_slave_group_count(container) + (container->nb_master ? 1 : 0); } @@ -1538,7 +1515,7 @@ size_t auto_group_count(swayc_t *container) { * given the index of a container's child, return the index of the first child of the group * which index is a member of. */ -int auto_group_start_index(swayc_t *container, int index) { +int auto_group_start_index(const swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout) || (size_t) index < container->nb_master) { return 0; @@ -1563,7 +1540,7 @@ int auto_group_start_index(swayc_t *container, int index) { * that follows the one which index is a member of. * This makes the function usable to walk through the groups in a container. */ -int auto_group_end_index(swayc_t *container, int index) { +int auto_group_end_index(const swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout)) { return container->children->length; } else { @@ -1590,7 +1567,7 @@ int auto_group_end_index(swayc_t *container, int index) { * return the index of the Group containing th child of . * The index is the order of the group along the container's major axis (starting at 0). */ -size_t auto_group_index(swayc_t *container, int index) { +size_t auto_group_index(const swayc_t *container, int index) { if (index < 0) { return 0; } @@ -1616,3 +1593,48 @@ size_t auto_group_index(swayc_t *container, int index) { return grp_idx + (master_first ? 1 : 0); } } + +/** + * Return the first index (inclusive) and last index (exclusive) of the elements of a group in + * an auto layout. + * If the bounds of the given group can be calculated, they are returned in the start/end + * parameters (int pointers) and the return value will be true. + * The indexes are passed by reference and can be NULL. + */ +bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end) { + size_t nb_grp = auto_group_count(container); + if (group_index >= nb_grp) { + return false; + } + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + size_t nb_master = auto_master_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + int g_start, g_end; + if (nb_master && (master_first ? group_index == 0 : group_index == nb_grp - 1)) { + g_start = 0; + g_end = nb_master; + } else { + size_t nb_slaves = auto_slave_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + size_t g0 = master_first && container->nb_master ? 1 : 0; + size_t g1 = g0 + nb_slave_grp - remainder; + if (group_index < g1) { + g_start = container->nb_master + (group_index - g0) * grp_sz; + g_end = g_start + grp_sz; + } else { + size_t g2 = group_index - g1; + g_start = container->nb_master + + (nb_slave_grp - remainder) * grp_sz + + g2 * (grp_sz + 1); + g_end = g_start + grp_sz + 1; + } + } + if (start) { + *start = g_start; + } + if (end) { + *end = g_end; + } + return true; +} From f24ebd75fa3ce35e844d5f6e71f3052025aa7995 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 21:24:43 +0100 Subject: [PATCH 21/30] Added mouse resize for auto layouts --- sway/layout.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index 6a2af2a6..e2f31848 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1372,7 +1372,8 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio while (true) { // Test if we can even make a difference here bool can_move = false; - int diff = 0; + int desired; + int idx = index_child(container); if (parent->type == C_ROOT) { swayc_t *output = swayc_adjacent_output(container, dir, &abs_pos, true); if (!output || output == container) { @@ -1381,21 +1382,36 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio sway_log(L_DEBUG, "Moving between outputs"); return get_swayc_in_output_direction(output, dir); } else { - if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { - if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { - can_move = true; - diff = dir == MOVE_LEFT ? -1 : 1; + if (is_auto_layout(parent->layout)) { + bool is_major = parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT + ? dir == MOVE_LEFT || dir == MOVE_RIGHT + : dir == MOVE_DOWN || dir == MOVE_UP; + size_t gidx = auto_group_index(parent, idx); + if (is_major) { + size_t desired_grp = gidx + (dir == MOVE_RIGHT || dir == MOVE_DOWN ? 1 : -1); + can_move = auto_group_bounds(parent, desired_grp, &desired, NULL); + } else { + desired = idx + (dir == MOVE_RIGHT || dir == MOVE_DOWN ? 1 : -1); + int start, end; + can_move = auto_group_bounds(parent, gidx, &start, &end) + && desired >= start && desired < end; } } else { - if (parent->layout == L_VERT || parent->layout == L_STACKED) { - can_move = true; - diff = dir == MOVE_UP ? -1 : 1; + if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { + if (parent->layout == L_HORIZ || parent->layout == L_TABBED) { + can_move = true; + desired = idx + (dir == MOVE_LEFT ? -1 : 1); + } + } else { + if (parent->layout == L_VERT || parent->layout == L_STACKED) { + can_move = true; + desired = idx + (dir == MOVE_UP ? -1 : 1); + } } } } if (can_move) { - int desired = index_child(container) + diff; if (container->is_floating) { if (desired < 0) { wrap_candidate = parent->floating->items[parent->floating->length-1]; @@ -1422,6 +1438,8 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio } } } else { + sway_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, + idx, container, dir, desired, parent->children->items[desired]); return parent->children->items[desired]; } } @@ -1590,7 +1608,7 @@ size_t auto_group_index(const swayc_t *container, int index) { } else { grp_idx = (nb_slave_grp - remainder) + (index - idx2) / (grp_sz + 1) ; } - return grp_idx + (master_first ? 1 : 0); + return grp_idx + (master_first && container-> nb_master ? 1 : 0); } } From d822150d8389a293d6df1ce177ee713ef3113d3f Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 8 Jan 2017 14:16:40 +0100 Subject: [PATCH 22/30] [fix] Keep Clang happy --- sway/layout.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sway/layout.c b/sway/layout.c index e2f31848..3e467169 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1504,7 +1504,9 @@ bool is_auto_layout(enum swayc_layouts layout) { * Return the number of master elements in a container */ static inline size_t auto_master_count(const swayc_t *container) { - return MIN(container->nb_master, container->children->length); + sway_assert(container->children->length >= 0, "Container %p has (negative) children %d", + container, container->children->length); + return MIN(container->nb_master, (size_t)container->children->length); } /** From 063c79874a0d55ffa69f48947381607978e128d7 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 8 Jan 2017 14:49:47 +0100 Subject: [PATCH 23/30] Indent cleanups --- sway/commands/layout.c | 24 ++++++++++++------------ sway/container.c | 7 ++++--- sway/layout.c | 36 ++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 0cdac1b4..c13a2ef7 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -49,8 +49,8 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else if (strcasecmp(argv[0], "splitv") == 0) { swayc_change_layout(parent, L_VERT); } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { - if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE || - parent->workspace_layout == L_HORIZ)) { + if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE + || parent->workspace_layout == L_HORIZ)) { swayc_change_layout(parent, L_VERT); } else { swayc_change_layout(parent, L_HORIZ); @@ -66,23 +66,23 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else if (strcasecmp(argv[0], "incnmaster") == 0) { const char *name = "layout incnmaster"; if ((error = checkarg(argc, name, - EXPECTED_EQUAL_TO, 2))) { + EXPECTED_EQUAL_TO, 2))) { return error; } char *end; int inc = (int) strtol(argv[1], &end, 10); if (*end) { return cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(argument must be an integer)", name); + "(argument must be an integer)", name); } swayc_t *container = get_focused_view(swayc_active_workspace()); if (container && inc && - is_auto_layout(container->parent->layout) && - ((int)container->parent->nb_master + inc >= 0)) { + is_auto_layout(container->parent->layout) && + ((int)container->parent->nb_master + inc >= 0)) { for (int i = container->parent->nb_master; - i >= 0 && i < container->parent->children->length && - i != (int) container->parent->nb_master + inc;) { + i >= 0 && i < container->parent->children->length + && i != (int) container->parent->nb_master + inc;) { ((swayc_t *) container->parent->children->items[i])->height = -1; ((swayc_t *) container->parent->children->items[i])->width = -1; i += inc > 0 ? 1 : -1; @@ -92,19 +92,19 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else if ((strcasecmp(argv[0], "incncol") == 0) && argc ==2) { const char *name = "layout incncol"; if ((error = checkarg(argc, name, - EXPECTED_EQUAL_TO, 2))) { + EXPECTED_EQUAL_TO, 2))) { return error; } char *end; int inc = (int) strtol(argv[1], &end, 10); if (*end) { return cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(argument must be an integer)", name); + "(argument must be an integer)", name); } swayc_t *container = get_focused_view(swayc_active_workspace()); if (container && inc && is_auto_layout(container->parent->layout) && - ((int)container->parent->nb_slave_groups + inc >= 1)) { + ((int)container->parent->nb_slave_groups + inc >= 1)) { container->parent->nb_slave_groups += inc; } } else if (strcasecmp(argv[0], "auto") == 0) { @@ -128,7 +128,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } } else { return cmd_results_new(CMD_FAILURE, "layout auto", - "Must be one of ."); + "Must be one of ."); } swayc_change_layout(parent, layout); } diff --git a/sway/container.c b/sway/container.c index 3bdc8b6c..cf7d7dda 100644 --- a/sway/container.c +++ b/sway/container.c @@ -965,10 +965,11 @@ swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { // if layout change modifies the auto layout's major axis, swap width and height // to preserve current ratios. if (is_auto_layout(layout) && is_auto_layout(container->layout)) { - enum swayc_layouts prev_major = (container->layout == L_AUTO_LEFT || - container->layout == L_AUTO_RIGHT) + enum swayc_layouts prev_major = + container->layout == L_AUTO_LEFT || container->layout == L_AUTO_RIGHT ? L_HORIZ : L_VERT; - enum swayc_layouts new_major = (layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT) + enum swayc_layouts new_major = + layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT ? L_HORIZ : L_VERT; if (new_major != prev_major) { for (int i = 0; i < container->children->length; ++i) { diff --git a/sway/layout.c b/sway/layout.c index 3e467169..869a3bcb 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -296,7 +296,7 @@ void move_container(swayc_t *container, enum movement_direction dir) { sway_log(L_DEBUG, "container:%p, parent:%p, child %p,", container,parent,child); if (parent->layout == layout - || layout == L_NONE /* accept any layout for next/prev direction */ + || (layout == L_NONE && parent->type == C_CONTAINER) /* accept any layout for next/prev direction */ || (parent->layout == L_TABBED && layout == L_HORIZ) || (parent->layout == L_STACKED && layout == L_VERT) || is_auto_layout(parent->layout)) { @@ -321,16 +321,16 @@ void move_container(swayc_t *container, enum movement_direction dir) { } // if move command makes container change from master to slave // (or the contrary), reset its geometry an the one of the replaced item. - if (parent->nb_master && - (size_t) parent->children->length > parent->nb_master) { + if (parent->nb_master + && (size_t) parent->children->length > parent->nb_master) { swayc_t *swap_geom = NULL; // if child is being promoted/demoted, it will swap geometry // with the sibling being demoted/promoted. if ((dir == MOVE_NEXT && desired == 0) - || (dir == MOVE_PREV && (size_t) desired == parent->nb_master - 1)) { + || (dir == MOVE_PREV && (size_t) desired == parent->nb_master - 1)) { swap_geom = parent->children->items[parent->nb_master - 1]; } else if ((dir == MOVE_NEXT && (size_t) desired == parent->nb_master) - || (dir == MOVE_PREV && desired == parent->children->length - 1)) { + || (dir == MOVE_PREV && desired == parent->children->length - 1)) { swap_geom = parent->children->items[parent->nb_master]; } if (swap_geom) { @@ -837,9 +837,9 @@ static void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double height); static void apply_auto_layout(swayc_t *container, const double x, const double y, - const double width, const double height, - enum swayc_layouts group_layout, - bool master_first); + const double width, const double height, + enum swayc_layouts group_layout, + bool master_first); static void arrange_windows_r(swayc_t *container, double width, double height) { int i; @@ -972,11 +972,11 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { case L_HORIZ: default: apply_horiz_layout(container, x, y, width, height, 0, - container->children->length); + container->children->length); break; case L_VERT: apply_vert_layout(container, x, y, width, height, 0, - container->children->length); + container->children->length); break; case L_TABBED: case L_STACKED: @@ -1007,7 +1007,7 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { if (swayc_is_fullscreen(view)) { wlc_view_bring_to_front(view->handle); } else if (!container->focused || - !swayc_is_fullscreen(container->focused)) { + !swayc_is_fullscreen(container->focused)) { wlc_view_bring_to_front(view->handle); } } @@ -1068,8 +1068,8 @@ void apply_horiz_layout(swayc_t *container, const double x, const double y, } void apply_vert_layout(swayc_t *container, const double x, const double y, - const double width, const double height, const int start, - const int end) { + const double width, const double height, const int start, + const int end) { int i; double scale = 0; // Calculate total height @@ -1121,7 +1121,7 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, } void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, - double width, double height) { + double width, double height) { int i; swayc_t *focused = NULL; for (i = 0; i < container->children->length; ++i) { @@ -1141,9 +1141,9 @@ void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, } void apply_auto_layout(swayc_t *container, const double x, const double y, - const double width, const double height, - enum swayc_layouts group_layout, - bool master_first) { + const double width, const double height, + enum swayc_layouts group_layout, + bool master_first) { // Auto layout "container" in width x height @ x, y // using "group_layout" for each of the groups in the container. // There is one "master" group, plus container->nb_slave_groups. @@ -1342,7 +1342,7 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio return NULL; } else { int desired = (focused_idx + (dir == MOVE_NEXT ? 1 : -1)) % - parent->children->length; + parent->children->length; if (desired < 0) { desired += parent->children->length; } From 07474a4fa73374469664ee5595f1223b21534b77 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 8 Jan 2017 17:57:38 +0100 Subject: [PATCH 24/30] reworked "layout auto*" star commands - "layout auto_left|auto_xxx" are now "layout auto xxx" - "layout incmaster " is now "layout auto master [set|inc] " - "layout incncol " is now "layout auto ncol [set|inc] " --- sway/commands/layout.c | 198 +++++++++++++++++++++++++---------------- sway/sway.5.txt | 12 +-- 2 files changed, 129 insertions(+), 81 deletions(-) diff --git a/sway/commands/layout.c b/sway/commands/layout.c index c13a2ef7..ff097fef 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -3,6 +3,11 @@ #include "sway/container.h" #include "sway/layout.h" +/** + * handle "layout auto" command group + */ +static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv); + struct cmd_results *cmd_layout(int argc, char **argv) { struct cmd_results *error = NULL; if (config->reading) return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file."); @@ -55,82 +60,8 @@ struct cmd_results *cmd_layout(int argc, char **argv) { } else { swayc_change_layout(parent, L_HORIZ); } - } else if (strcasecmp(argv[0], "auto_left") == 0) { - swayc_change_layout(parent, L_AUTO_LEFT); - } else if (strcasecmp(argv[0], "auto_right") == 0) { - swayc_change_layout(parent, L_AUTO_RIGHT); - } else if (strcasecmp(argv[0], "auto_top") == 0) { - swayc_change_layout(parent, L_AUTO_TOP); - } else if (strcasecmp(argv[0], "auto_bot") == 0) { - swayc_change_layout(parent, L_AUTO_BOTTOM); - } else if (strcasecmp(argv[0], "incnmaster") == 0) { - const char *name = "layout incnmaster"; - if ((error = checkarg(argc, name, - EXPECTED_EQUAL_TO, 2))) { - return error; - } - char *end; - int inc = (int) strtol(argv[1], &end, 10); - if (*end) { - return cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(argument must be an integer)", name); - - } - swayc_t *container = get_focused_view(swayc_active_workspace()); - if (container && inc && - is_auto_layout(container->parent->layout) && - ((int)container->parent->nb_master + inc >= 0)) { - for (int i = container->parent->nb_master; - i >= 0 && i < container->parent->children->length - && i != (int) container->parent->nb_master + inc;) { - ((swayc_t *) container->parent->children->items[i])->height = -1; - ((swayc_t *) container->parent->children->items[i])->width = -1; - i += inc > 0 ? 1 : -1; - } - container->parent->nb_master += inc; - } - } else if ((strcasecmp(argv[0], "incncol") == 0) && argc ==2) { - const char *name = "layout incncol"; - if ((error = checkarg(argc, name, - EXPECTED_EQUAL_TO, 2))) { - return error; - } - char *end; - int inc = (int) strtol(argv[1], &end, 10); - if (*end) { - return cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(argument must be an integer)", name); - - } - swayc_t *container = get_focused_view(swayc_active_workspace()); - if (container && inc && is_auto_layout(container->parent->layout) && - ((int)container->parent->nb_slave_groups + inc >= 1)) { - container->parent->nb_slave_groups += inc; - } } else if (strcasecmp(argv[0], "auto") == 0) { - if ((error = checkarg(argc, "auto", EXPECTED_EQUAL_TO, 2))) { - return error; - } - swayc_t *container = get_focused_view(swayc_active_workspace()); - swayc_t *parent = container->parent; - enum swayc_layouts layout; - if (strcasecmp(argv[1], "next") == 0) { - if (is_auto_layout(parent->layout) && parent->layout < L_AUTO_LAST) { - layout = parent->layout + 1; - } else { - layout = L_AUTO_FIRST; - } - } else if (strcasecmp(argv[1], "prev") == 0) { - if (is_auto_layout(parent->layout) && parent->layout > L_AUTO_FIRST) { - layout = parent->layout - 1; - } else { - layout = L_AUTO_LAST; - } - } else { - return cmd_results_new(CMD_FAILURE, "layout auto", - "Must be one of ."); - } - swayc_change_layout(parent, layout); + return cmd_layout_auto(parent, argc, argv); } } @@ -141,3 +72,120 @@ struct cmd_results *cmd_layout(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } + +static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv) { + // called after checking that argv[0] is auto, so just continue parsing from there + struct cmd_results *error = NULL; + const char *cmd_name = "layout auto"; + const char *set_inc_cmd_name = "layout auto [master|ncol] [set|inc]"; + const char *err_msg = "Allowed arguments are "; + + bool need_layout_update = false; + enum swayc_layouts old_layout = container->layout; + enum swayc_layouts layout = old_layout; + + if (strcasecmp(argv[1], "left") == 0) { + layout = L_AUTO_LEFT; + } else if (strcasecmp(argv[1], "right") == 0) { + layout = L_AUTO_RIGHT; + } else if (strcasecmp(argv[1], "top") == 0) { + layout = L_AUTO_TOP; + } else if (strcasecmp(argv[1], "bot") == 0) { + layout = L_AUTO_BOTTOM; + } else if (strcasecmp(argv[1], "next") == 0) { + if (is_auto_layout(container->layout) && container->layout < L_AUTO_LAST) { + layout = container->layout + 1; + } else { + layout = L_AUTO_FIRST; + } + } else if (strcasecmp(argv[1], "prev") == 0) { + if (is_auto_layout(container->layout) && container->layout > L_AUTO_FIRST) { + layout = container->layout - 1; + } else { + layout = L_AUTO_LAST; + } + } else { + bool is_nmaster; + bool is_set; + if (strcasecmp(argv[1], "master") == 0) { + is_nmaster = true; + } else if (strcasecmp(argv[1], "ncol") == 0) { + is_nmaster = false; + } else { + return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command. %s", + cmd_name, err_msg); + } + if ((error = checkarg(argc, "auto ", EXPECTED_EQUAL_TO, 4))) { + return error; + } + if (strcasecmp(argv[2], "set") == 0) { + is_set = true; + } else if (strcasecmp(argv[2], "inc") == 0) { + is_set = false; + } else { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command. %s, " + "Argument must be on of ", + set_inc_cmd_name); + } + char *end; + int n = (int)strtol(argv[3], &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " + "(argument must be an integer)", set_inc_cmd_name); + } + if (is_auto_layout(container->layout)) { + int inc = 0; /* difference between current master/ncol and requested value */ + if (is_nmaster) { + if (is_set) { + if (n < 0) { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " + "(master must be >= 0)", set_inc_cmd_name); + } + inc = n - (int)container->nb_master; + } else { /* inc command */ + if ((int)container->nb_master + n >= 0) { + inc = n; + } + } + if (inc) { + for (int i = container->nb_master; + i >= 0 && i < container->children->length + && i != (int)container->nb_master + inc;) { + ((swayc_t *)container->children->items[i])->height = -1; + ((swayc_t *)container->children->items[i])->width = -1; + i += inc > 0 ? 1 : -1; + } + container->nb_master += inc; + need_layout_update = true; + } + } else { /* ncol modification */ + if (is_set) { + if (n <= 0) { + return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command " + "(ncol must be > 0)", set_inc_cmd_name); + } + inc = n - (int)container->nb_slave_groups; + } else { /* inc command */ + if ((int)container->nb_slave_groups + n > 0) { + inc = n; + } + } + if (inc) { + container->nb_slave_groups += inc; + need_layout_update = true; + } + } + } + } + + if (layout != old_layout) { + swayc_change_layout(container, layout); + update_layout_geometry(container, old_layout); + need_layout_update = true; + } + if (need_layout_update) { + update_geometry(container); + arrange_windows(container, container->width, container->height); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/sway.5.txt b/sway/sway.5.txt index cbff6cef..5e0a07bd 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -84,13 +84,16 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( **layout** :: Sets the layout mode of the focused container. _mode_ can be one of _splith_, - _splitv_, _toggle split_, _stacking_, _tabbed_, _auto_left_, _auto_right_, - _auto_top, _auto_bottom_. + _splitv_, _toggle split_, _stacking_, _tabbed_. + +**layout** auto :: + Sets layout to one of the auto modes, i.e. one of _left_, right_, _top_, + or _bot_. **layout** auto :: Cycles between available auto layouts. -**layout** :: +**layout** auto [master|ncol] [inc|set] :: Modify the number of master elements, respectively slave columns, in the focused container. can be a positive or negative integer. These commands only have an effect if the focused container uses one of the "auto" layouts. @@ -98,9 +101,6 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( **layout** toggle split:: Cycles between available split layouts. -**layout** promote:: - Swap the focused element with the first in the one of the auto layouts. - **move** :: Moves the focused container _left_, _right_, _up_, or _down_. Moving to _prev_ or _next_ swaps the container with its sibling in the same container. Move From 52f3a8df511ca52079011db84aaf53464b81c562 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 8 Jan 2017 18:08:10 +0100 Subject: [PATCH 25/30] fixed up space-after-cast style issues --- sway/layout.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index 869a3bcb..fdd2fe6b 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -322,14 +322,14 @@ void move_container(swayc_t *container, enum movement_direction dir) { // if move command makes container change from master to slave // (or the contrary), reset its geometry an the one of the replaced item. if (parent->nb_master - && (size_t) parent->children->length > parent->nb_master) { + && (size_t)parent->children->length > parent->nb_master) { swayc_t *swap_geom = NULL; // if child is being promoted/demoted, it will swap geometry // with the sibling being demoted/promoted. if ((dir == MOVE_NEXT && desired == 0) - || (dir == MOVE_PREV && (size_t) desired == parent->nb_master - 1)) { + || (dir == MOVE_PREV && (size_t)desired == parent->nb_master - 1)) { swap_geom = parent->children->items[parent->nb_master - 1]; - } else if ((dir == MOVE_NEXT && (size_t) desired == parent->nb_master) + } else if ((dir == MOVE_NEXT && (size_t)desired == parent->nb_master) || (dir == MOVE_PREV && desired == parent->children->length - 1)) { swap_geom = parent->children->items[parent->nb_master]; } @@ -1537,7 +1537,7 @@ size_t auto_group_count(const swayc_t *container) { */ int auto_group_start_index(const swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout) - || (size_t) index < container->nb_master) { + || (size_t)index < container->nb_master) { return 0; } else { size_t nb_slaves = auto_slave_count(container); @@ -1593,7 +1593,7 @@ size_t auto_group_index(const swayc_t *container, int index) { } bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); size_t nb_slaves = auto_slave_count(container); - if ((size_t) index < container->nb_master) { + if ((size_t)index < container->nb_master) { if (master_first || nb_slaves <= 0) { return 0; } else { From b74870f51653872b36dd31fc76bdb738979e58a9 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 14 Jan 2017 17:42:55 +0100 Subject: [PATCH 26/30] Improved behavior of insert/remove child in auto layouts Previous implementation would not preserve dimension of groups along the major axis. This should avoid weird behavior when using container motion commands. --- sway/layout.c | 136 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 28 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index 4b30f729..e3de3354 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -71,6 +71,14 @@ void add_child(swayc_t *parent, swayc_t *child) { } } +static double *get_height(swayc_t *cont) { + return &cont->height; +} + +static double *get_width(swayc_t *cont) { + return &cont->width; +} + void insert_child(swayc_t *parent, swayc_t *child, int index) { if (index > parent->children->length) { index = parent->children->length; @@ -86,7 +94,44 @@ void insert_child(swayc_t *parent, swayc_t *child, int index) { if (parent->type == C_WORKSPACE && child->type == C_VIEW && (parent->workspace_layout == L_TABBED || parent->workspace_layout == L_STACKED)) { child = new_container(child, parent->workspace_layout); } - + if (is_auto_layout(parent->layout)) { + /* go through each group, adjust the size of the first child of each group */ + double *(*get_maj_dim)(swayc_t *cont); + double *(*get_min_dim)(swayc_t *cont); + if (parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT) { + get_maj_dim = get_width; + get_min_dim = get_height; + } else { + get_maj_dim = get_height; + get_min_dim = get_width; + } + for (int i = index; i < parent->children->length;) { + int start = auto_group_start_index(parent, i); + int end = auto_group_end_index(parent, i); + swayc_t *first = parent->children->items[start]; + if (start + 1 < parent->children->length) { + /* preserve the group's dimension along major axis */ + *get_maj_dim(first) = *get_maj_dim(parent->children->items[start + 1]); + } else { + /* new group, let the apply_layout handle it */ + first->height = first->width = 0; + break; + } + double remaining = *get_min_dim(parent); + for (int j = end - 1; j > start; --j) { + swayc_t *sibling = parent->children->items[j]; + if (sibling == child) { + /* the inserted child won't yet have its minor + dimension set */ + remaining -= *get_min_dim(parent) / (end - start); + } else { + remaining -= *get_min_dim(sibling); + } + } + *get_min_dim(first) = remaining; + i = end; + } + } } void add_floating(swayc_t *ws, swayc_t *child) { @@ -185,6 +230,42 @@ swayc_t *remove_child(swayc_t *child) { break; } } + if (is_auto_layout(parent->layout) && parent->children->length) { + /* go through each group, adjust the size of the last child of each group */ + double *(*get_maj_dim)(swayc_t *cont); + double *(*get_min_dim)(swayc_t *cont); + if (parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT) { + get_maj_dim = get_width; + get_min_dim = get_height; + } else { + get_maj_dim = get_height; + get_min_dim = get_width; + } + for (int j = parent->children->length - 1; j >= i;) { + int start = auto_group_start_index(parent, j); + int end = auto_group_end_index(parent, j); + swayc_t *first = parent->children->items[start]; + if (i == start) { + /* removed element was first child in the current group, + use its size along the major axis */ + *get_maj_dim(first) = *get_maj_dim(child); + } else if (start > i) { + /* preserve the group's dimension along major axis */ + *get_maj_dim(first) = *get_maj_dim(parent->children->items[start - 1]); + } + if (end != parent->children->length) { + double remaining = *get_min_dim(parent); + for (int k = start; k < end - 1; ++k) { + swayc_t *sibling = parent->children->items[k]; + remaining -= *get_min_dim(sibling); + } + /* last element of the group gets remaining size, elements + that don't change groups keep their ratio */ + *get_min_dim((swayc_t *) parent->children->items[end - 1]) = remaining; + } /* else last group, let apply_layout handle it */ + j = start - 1; + } + } } // Set focused to new container if (parent->focused == child) { @@ -250,6 +331,24 @@ void swap_geometry(swayc_t *a, swayc_t *b) { b->height = h; } +static void swap_children(swayc_t *container, int a, int b) { + if (a >= 0 && b >= 0 && a < container->children->length + && b < container->children->length + && a != b) { + swayc_t *pa = (swayc_t *)container->children->items[a]; + swayc_t *pb = (swayc_t *)container->children->items[b]; + container->children->items[a] = container->children->items[b]; + container->children->items[b] = pa; + if (is_auto_layout(container->layout)) { + size_t ga = auto_group_index(container, a); + size_t gb = auto_group_index(container, b); + if (ga != gb) { + swap_geometry(pa, pb); + } + } + } +} + void move_container(swayc_t *container, enum movement_direction dir) { enum swayc_layouts layout = L_NONE; swayc_t *parent = container->parent; @@ -319,29 +418,6 @@ void move_container(swayc_t *container, enum movement_direction dir) { } else if (desired >= parent->children->length) { desired = 0; } - // if move command makes container change from master to slave - // (or the contrary), reset its geometry an the one of the replaced item. - if (parent->nb_master - && (size_t)parent->children->length > parent->nb_master) { - swayc_t *swap_geom = NULL; - // if child is being promoted/demoted, it will swap geometry - // with the sibling being demoted/promoted. - if ((dir == MOVE_NEXT && desired == 0) - || (dir == MOVE_PREV && (size_t)desired == parent->nb_master - 1)) { - swap_geom = parent->children->items[parent->nb_master - 1]; - } else if ((dir == MOVE_NEXT && (size_t)desired == parent->nb_master) - || (dir == MOVE_PREV && desired == parent->children->length - 1)) { - swap_geom = parent->children->items[parent->nb_master]; - } - if (swap_geom) { - double h = child->height; - double w = child->width; - child->width = swap_geom->width; - child->height = swap_geom->height; - swap_geom->width = w; - swap_geom->height = h; - } - } } // when it has ascended, legal insertion position is 0:len // when it has not, legal insertion position is 0:len-1 @@ -365,10 +441,14 @@ void move_container(swayc_t *container, enum movement_direction dir) { container->width = container->height = 0; } } - swayc_t *old_parent = remove_child(container); - insert_child(parent, container, desired); - destroy_container(old_parent); - sway_log(L_DEBUG,"Moving to %p %d", parent, desired); + if (container->parent == parent) { + swap_children(parent, idx, desired); + } else { + swayc_t *old_parent = remove_child(container); + insert_child(parent, container, desired); + destroy_container(old_parent); + sway_log(L_DEBUG,"Moving to %p %d", parent, desired); + } break; } } From 71b386964afa553cb2386a06304bfe55cdc25aa1 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 14 Jan 2017 19:34:04 +0100 Subject: [PATCH 27/30] replaced "bot" with "bottom" in auto layout commands --- sway/commands/layout.c | 4 ++-- sway/commands/workspace_layout.c | 31 +++++++++++++++++++++---------- sway/sway.5.txt | 17 ++++++++--------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/sway/commands/layout.c b/sway/commands/layout.c index ff097fef..d04bb4dc 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -78,7 +78,7 @@ static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char ** struct cmd_results *error = NULL; const char *cmd_name = "layout auto"; const char *set_inc_cmd_name = "layout auto [master|ncol] [set|inc]"; - const char *err_msg = "Allowed arguments are "; + const char *err_msg = "Allowed arguments are "; bool need_layout_update = false; enum swayc_layouts old_layout = container->layout; @@ -90,7 +90,7 @@ static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char ** layout = L_AUTO_RIGHT; } else if (strcasecmp(argv[1], "top") == 0) { layout = L_AUTO_TOP; - } else if (strcasecmp(argv[1], "bot") == 0) { + } else if (strcasecmp(argv[1], "bottom") == 0) { layout = L_AUTO_BOTTOM; } else if (strcasecmp(argv[1], "next") == 0) { if (is_auto_layout(container->layout) && container->layout < L_AUTO_LAST) { diff --git a/sway/commands/workspace_layout.c b/sway/commands/workspace_layout.c index 3e0a12ce..c9305773 100644 --- a/sway/commands/workspace_layout.c +++ b/sway/commands/workspace_layout.c @@ -3,7 +3,7 @@ struct cmd_results *cmd_workspace_layout(int argc, char **argv) { struct cmd_results *error = NULL; - if ((error = checkarg(argc, "workspace_layout", EXPECTED_EQUAL_TO, 1))) { + if ((error = checkarg(argc, "workspace_layout", EXPECTED_AT_LEAST, 1))) { return error; } @@ -13,16 +13,27 @@ struct cmd_results *cmd_workspace_layout(int argc, char **argv) { config->default_layout = L_STACKED; } else if (strcasecmp(argv[0], "tabbed") == 0) { config->default_layout = L_TABBED; - } else if (strcasecmp(argv[0], "auto_left") == 0) { - config->default_layout = L_AUTO_LEFT; - } else if (strcasecmp(argv[0], "auto_right") == 0) { - config->default_layout = L_AUTO_RIGHT; - } else if (strcasecmp(argv[0], "auto_top") == 0) { - config->default_layout = L_AUTO_TOP; - } else if (strcasecmp(argv[0], "auto_bottom") == 0) { - config->default_layout = L_AUTO_BOTTOM; + } else if (strcasecmp(argv[0], "auto") == 0) { + if (argc == 1) { + config->default_layout = L_AUTO_FIRST; + } else { + if ((error = checkarg(argc, "workspace_layout auto", EXPECTED_EQUAL_TO, 2))) { + return error; + } + if (strcasecmp(argv[0], "left") == 0) { + config->default_layout = L_AUTO_LEFT; + } else if (strcasecmp(argv[0], "right") == 0) { + config->default_layout = L_AUTO_RIGHT; + } else if (strcasecmp(argv[0], "top") == 0) { + config->default_layout = L_AUTO_TOP; + } else if (strcasecmp(argv[0], "bottom") == 0) { + config->default_layout = L_AUTO_BOTTOM; + } else { + return cmd_results_new(CMD_INVALID, "workspace_layout auto", "Expected 'workspace_layout auto '"); + } + } } else { - return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); + return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 5e0a07bd..ee1cbddd 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -64,12 +64,11 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( **focus** :: Direction may be one of _up_, _down_, _left_, _right_, _next_, _prev_, _parent_, or _child_. The directional focus commands will move the focus - in that direction. The auto_next and auto_prev will focus the next, - respectively previous, element in the current container if it is using - one of the _auto_ layouts. The parent focus command will change the - focus to the parent of the currently focused container, which is useful, - for example, to open a sibling of the parent container, or to move the - entire container around. + in that direction. The _next_ and _prev_ directions will focus the next, + respectively previous, element in the current container. The parent + focus command will change the focus to the parent of the currently + focused container, which is useful, for example, to open a sibling of + the parent container, or to move the entire container around. **focus** output :: Direction may be one of _up_, _down_, _left_, _right_. The directional focus @@ -88,7 +87,7 @@ They are expected to be used with **bindsym** or at runtime through **swaymsg**( **layout** auto :: Sets layout to one of the auto modes, i.e. one of _left_, right_, _top_, - or _bot_. + or _bottom_. **layout** auto :: Cycles between available auto layouts. @@ -381,8 +380,8 @@ The default colors are: switch to workspace 2, then invoke the "workspace 2" command again, you will be returned to workspace 1. Defaults to _no_. -**workspace_layout** :: - Specifies the start layout for new workspaces. +**workspace_layout** :: Specifies the start layout for new workspaces. **include** :: Includes a sub config file by _path_. _path_ can be either a full path or a From 5c40cc46ac0160ac870ec926e2285824cad0dd9c Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 14 Jan 2017 19:37:35 +0100 Subject: [PATCH 28/30] Added a sample config for Awesome-like behavior --- samples/awesome-like.config | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 samples/awesome-like.config diff --git a/samples/awesome-like.config b/samples/awesome-like.config new file mode 100644 index 00000000..41e17c91 --- /dev/null +++ b/samples/awesome-like.config @@ -0,0 +1,63 @@ +# +# Replicate some of Awesome's default layout manipulation configuration for Sway +# +# Differences: +# - Layout switching doesn't use the spacebar (i.e. i3/Sway behavior to switch to/from floating windows) +# and uses the 'A' key instead (as in auto) +# - Resizing windows uses i3/Sway's more versatile Mod4+r +# - no tags +# - no Maximize/Minize, alternatives to Maximize would be to switch to Stacked/Tabbed layouts +# via Mod4+w or Mod4+s. +# - kill focused client is available on Mod4+Shift+q (instead of Mod4+Shift+c, which maps to Sway's +# config reload) +# - probably many more ... + +# Awesome-style container traversal using Vim-like binding +set $next j +set $prev k + +# +# Moving around: +# + # Move your focus around + bindsym $mod+$next focus next + bindsym $mod+$prev focus prev + + # _move_ the focused window with the same, but add Shift + bindsym $mod+Shift+$next move next + bindsym $mod+Shift+$prev move prev + +# +# Layout: +# + workspace_layout auto left + + # This is usually bound to $mod+space, but this works well in practice by keeping + # all the layout switching keys grouped together. + bindsym $mod+a layout auto next + bindsym $mod+Shift+a layout auto prev + + # Promote a child to master position in an auto layout + bindsym $mod+Control+Return move first + + # Increase/decrease number of master elements in auto layout + bindsym $mod+Shift+h layout auto master inc 1 + bindsym $mod+Shift+l layout auto master inc -1 + + # Increase/decrease number of slave element groups in auto layout + bindsym $mod+Control+h layout auto ncol inc 1 + bindsym $mod+Control+l layout auto ncol inc -1 + +# +# Resizing containers: +# Again, not really the way Awesome works well, but in spirit with i3/Sway and it works well. +# +mode "resize" { + bindsym Left resize shrink width 20 px + bindsym Down resize grow height 20 px + bindsym Up resize shrink height 20 px + bindsym Right resize grow width 20 px +} +bindsym $mod+r mode "resize" + +new_window pixel 1 From a90dddea4014707c2240e1c20a7b1a1500f4605d Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 14 Jan 2017 19:48:41 +0100 Subject: [PATCH 29/30] [fix] handle auto layout of empty container --- sway/layout.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sway/layout.c b/sway/layout.c index e3de3354..5e144cd8 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1615,7 +1615,8 @@ size_t auto_slave_group_count(const swayc_t *container) { * Return the combined number of master and slave groups in the container. */ size_t auto_group_count(const swayc_t *container) { - return auto_slave_group_count(container) + (container->nb_master ? 1 : 0); + return auto_slave_group_count(container) + + (container->children->length && container->nb_master ? 1 : 0); } /** From a2cf3be890241a27cbbfd38a9367181a75cd2277 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 14 Jan 2017 16:03:41 -0500 Subject: [PATCH 30/30] Move awesome config to contrib/ --- samples/awesome-like.config => contrib/awesome.config | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename samples/awesome-like.config => contrib/awesome.config (100%) diff --git a/samples/awesome-like.config b/contrib/awesome.config similarity index 100% rename from samples/awesome-like.config rename to contrib/awesome.config