diff options
Diffstat (limited to 'sway/commands')
-rw-r--r-- | sway/commands/focus.c | 4 | ||||
-rw-r--r-- | sway/commands/layout.c | 79 | ||||
-rw-r--r-- | sway/commands/move.c | 8 | ||||
-rw-r--r-- | sway/commands/resize.c | 313 | ||||
-rw-r--r-- | sway/commands/workspace_layout.c | 10 |
5 files changed, 210 insertions, 204 deletions
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 08336150..0cdac1b4 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -49,11 +49,88 @@ 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); } + } 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 <prev|next>."); + } + swayc_change_layout(parent, layout); } } diff --git a/sway/commands/move.c b/sway/commands/move.c index 4819d9ef..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 <left|right|up|down>' or " + const char* expected_syntax = "Expected 'move <left|right|up|down|next|prev|first>' or " "'move <container|window> to workspace <name>' or " "'move <container|window|workspace> to output <name|direction>' or " "'move position mouse'"; @@ -27,6 +27,12 @@ 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], "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/commands/resize.c b/sway/commands/resize.c index 2c5b3f6b..28b20dc4 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -63,224 +63,135 @@ static bool resize_floating(int amount, bool use_width) { } 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; - } + swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *parent = container->parent; + int idx_focused = 0; + bool use_major = false; + 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))) { + // 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; } - parent = parent->parent; - } - if (parent == &root_container) { - return true; - } - 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; - } - } - } + if (use_major) { + nb_before = auto_group_index(parent, idx_focused); + nb_after = auto_group_count(parent) - nb_before - 1; } else { - double pixels = amount; - if (parent->width + pixels < min_sane_w) { - valid = false; - break; - } + 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)auto_group_start_index(parent, idx_focused), + (int)auto_group_end_index(parent, idx_focused), + (int)nb_before, (int)nb_after); + } - } - 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); - } - } + 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 %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 : 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]; + 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; + 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; } - 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 ? 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 ? 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; + 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"); + 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 { - 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); - } + if (use_major) { + for (int j = i; j < next_i; ++j) { + 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 / 2, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); } } 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); - } + recursive_resize(sibling, pixels / 2, + use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); + recursive_resize(sibling, pixels / 2, + use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); } } - arrange_windows(swayc_active_workspace(), -1, -1); + i = next_i; } - return true; + // Recursive resize does not handle positions, let arrange_windows + // take care of that. + arrange_windows(swayc_active_workspace(), -1, -1); } return true; } 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 <default|stacking|tabbed>'"); + return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout <default|stacking|tabbed|auto_left|auto_right|auto_top|auto_bottom>'"); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } |