From 97f7d47413967e2b6f405c4fa303850b7c56f57a Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 10 Dec 2016 16:44:43 +0100 Subject: Added Awesome/Monad type "auto" layouts --- sway/commands/layout.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'sway/commands') 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); } } -- cgit v1.2.3 From 5425d0489fad8c6c00a7794ca18ac34041bd9308 Mon Sep 17 00:00:00 2001 From: wil Date: Mon, 19 Dec 2016 21:45:52 +0100 Subject: Handle resize in auto layouts --- sway/commands/resize.c | 397 ++++++++++++++++++++++++------------------------- 1 file changed, 194 insertions(+), 203 deletions(-) (limited to 'sway/commands') 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; } -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) { - 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; +/** + * 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; } - parent = parent->parent; + 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); } - if (parent == &root_container) { - return true; + } +} + +/** + * 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); } - 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; - } - } - } - } else { - double pixels = amount; - if (parent->width + pixels < min_sane_w) { - valid = false; - break; - } + } +} + +/** + * 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; } } - 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); - } - } + 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; + 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; } - // Recursive resize does not handle positions, let arrange_windows - // take care of that. - arrange_windows(swayc_active_workspace(), -1, -1); } + container = parent; /* continue up the tree to the next ancestor */ + } + if (parent == &root_container) { 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; - } + } + 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->parent == NULL || parent->parent->children == NULL) { - 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 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; - } - } + 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->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); - } - } + 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); } - 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; } -- cgit v1.2.3 From 8b0073b1954b54792f74b9e024c51548143906ea Mon Sep 17 00:00:00 2001 From: wil Date: Wed, 21 Dec 2016 16:55:02 +0100 Subject: Added "layout incnmaster|incncol" commands --- sway/commands/layout.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'sway/commands') 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; + } } } -- cgit v1.2.3 From bc3dc970265dab6e0305975791b5a266bbc390ca Mon Sep 17 00:00:00 2001 From: wil Date: Wed, 21 Dec 2016 16:59:50 +0100 Subject: [fix] Handle auto layout resize with multiple slave groups --- sway/commands/resize.c | 132 +++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 59 deletions(-) (limited to 'sway/commands') 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. -- cgit v1.2.3 From 0ff9fe9a7a18a5130b7c5e979ba980409f91b5f1 Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 22 Dec 2016 18:46:00 +0100 Subject: introduce next/prev as a direction for focus/move commands. --- sway/commands/focus.c | 4 ++++ sway/commands/layout.c | 3 ++- sway/commands/move.c | 6 +++++- 3 files changed, 11 insertions(+), 2 deletions(-) (limited to 'sway/commands') 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))) { -- cgit v1.2.3 From a0aa8d9780c6c8b0138800e3b2c2c0053174a2c5 Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 29 Dec 2016 20:26:35 +0100 Subject: 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. --- sway/commands/layout.c | 48 ++++++++++++++++++++++++++++++---------- sway/commands/workspace_layout.c | 10 ++++++++- 2 files changed, 45 insertions(+), 13 deletions(-) (limited to 'sway/commands') 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); } -- cgit v1.2.3 From 15745abf0cb4948fcc750a9e78139d3c02e1c1f0 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 31 Dec 2016 18:41:13 +0100 Subject: [fix] cycle auto layouts backwards --- sway/commands/layout.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sway/commands') 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", -- cgit v1.2.3 From a62048f15d9381e3040bd45965233e59a041eab7 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 1 Jan 2017 19:53:53 +0100 Subject: changed "layout promote" command to "move first" This is more consistent with other Sway semantics. --- sway/commands/layout.c | 12 ------------ sway/commands/move.c | 4 +++- 2 files changed, 3 insertions(+), 13 deletions(-) (limited to 'sway/commands') 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))) { -- cgit v1.2.3 From 97f70987d70315c683fd1e16c731b396679f6b96 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 1 Jan 2017 21:52:49 +0100 Subject: [fix] cleanups suggested by Sway community --- sway/commands/layout.c | 22 +++++++++++++--- sway/commands/resize.c | 70 +++++++++++++++++++++++++------------------------- 2 files changed, 53 insertions(+), 39 deletions(-) (limited to 'sway/commands') 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; -- cgit v1.2.3 From bd415029ba72425c97647b55fce19213e7909cbc Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 17:41:15 +0100 Subject: Moved auto_* layout functions from resize.c to layout.c --- sway/commands/resize.c | 98 ++------------------------------------------------ 1 file changed, 2 insertions(+), 96 deletions(-) (limited to 'sway/commands') 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; -- cgit v1.2.3 From 3c84250be8a31ec08f3c8057d589b5b040673b36 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 18:20:13 +0100 Subject: [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(-) (limited to 'sway/commands') 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); } } -- cgit v1.2.3 From 063c79874a0d55ffa69f48947381607978e128d7 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 8 Jan 2017 14:49:47 +0100 Subject: Indent cleanups --- sway/commands/layout.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'sway/commands') 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); } -- cgit v1.2.3 From 07474a4fa73374469664ee5595f1223b21534b77 Mon Sep 17 00:00:00 2001 From: wil Date: Sun, 8 Jan 2017 17:57:38 +0100 Subject: 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 ++++++++++++++++++++++++++++++------------------- 1 file changed, 123 insertions(+), 75 deletions(-) (limited to 'sway/commands') 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); +} -- cgit v1.2.3 From 71b386964afa553cb2386a06304bfe55cdc25aa1 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 14 Jan 2017 19:34:04 +0100 Subject: replaced "bot" with "bottom" in auto layout commands --- sway/commands/layout.c | 4 ++-- sway/commands/workspace_layout.c | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) (limited to 'sway/commands') 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); } -- cgit v1.2.3