From bc3babf566f1f8ad9db4d1f7b70a859f54ba6c48 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Thu, 20 Aug 2015 11:30:32 -0500 Subject: Added in basic resize command --- sway/commands.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) (limited to 'sway/commands.c') diff --git a/sway/commands.c b/sway/commands.c index 644b8005..cdc80a0b 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -422,6 +422,150 @@ static bool cmd_reload(struct sway_config *config, int argc, char **argv) { return true; } +static bool cmd_resize(struct sway_config *config, int argc, char **argv) { + if (!checkarg(argc, "resize", EXPECTED_AT_LEAST, 3)) { + return false; + } + char *end; + int amount = (int)strtol(argv[2], &end, 10); + if (errno == ERANGE || amount == 0) { + errno = 0; + return false; + } + if (strcmp(argv[0], "shrink") != 0 && strcmp(argv[0], "grow") != 0) { + return false; + } + if (strcmp(argv[0], "shrink") == 0) { + amount *= -1; + } + + swayc_t *parent = get_focused_view(active_workspace); + swayc_t *focused = parent; + swayc_t *sibling; + if (!parent) { + return true; + } + // Find the closest possible sibling and resize using that edge + int i; + if (strcmp(argv[1], "width") == 0) { + int lnumber = 0; + int rnumber = 0; + while (parent->parent) { + if (parent->parent->layout == L_HORIZ) { + 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; + } + } + 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 + 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/lnumber); + if (lnumber) { + recursive_resize(sibling, pixels/2, MOVE_RIGHT); + } else { + recursive_resize(sibling, pixels, MOVE_RIGHT); + } + } else if (sibling->x > parent->x) { + double pixels = -1 * (amount/rnumber); + if (rnumber) { + recursive_resize(sibling, pixels/2, MOVE_LEFT); + } else { + recursive_resize(sibling, pixels, MOVE_LEFT); + } + } + } else { + if (rnumber != 0 && lnumber != 0) { + recursive_resize(parent, amount/2, MOVE_LEFT); + recursive_resize(parent, amount/2, MOVE_RIGHT); + } else if (rnumber) { + recursive_resize(parent, amount, MOVE_RIGHT); + } else if (lnumber) { + recursive_resize(parent, amount, MOVE_LEFT); + } + } + } + arrange_windows(active_workspace, -1, -1); + return true; + } else if (strcmp(argv[1], "height") == 0) { + 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 == &root_container) { + 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 + 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/bnumber); + if (tnumber) { + recursive_resize(sibling, pixels/2, MOVE_UP); + } else { + recursive_resize(sibling, pixels, MOVE_UP); + } + } else if (sibling->x > parent->x) { + double pixels = -1 * (amount/tnumber); + if (bnumber) { + recursive_resize(sibling, pixels/2, MOVE_DOWN); + } else { + recursive_resize(sibling, pixels, MOVE_DOWN); + } + } + } else { + if (bnumber != 0 && tnumber != 0) { + recursive_resize(parent, amount/2, MOVE_UP); + recursive_resize(parent, amount/2, MOVE_DOWN); + } else if (tnumber) { + recursive_resize(parent, amount, MOVE_UP); + } else if (bnumber) { + recursive_resize(parent, amount, MOVE_DOWN); + } + } + } + arrange_windows(active_workspace, -1, -1); + return true; + } + sway_log(L_INFO, "Done with resize"); + return true; +} + static bool cmd_set(struct sway_config *config, int argc, char **argv) { if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) { return false; @@ -587,6 +731,7 @@ static struct cmd_handler handlers[] = { { "layout", cmd_layout }, { "log_colors", cmd_log_colors }, { "reload", cmd_reload }, + { "resize", cmd_resize }, { "set", cmd_set }, { "split", cmd_split }, { "splith", cmd_splith }, -- cgit v1.2.3 From d06732e1a8c2df4d1523c1c0efeff5534156e387 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Thu, 20 Aug 2015 11:35:01 -0500 Subject: Altered incorrect comment --- sway/commands.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sway/commands.c') diff --git a/sway/commands.c b/sway/commands.c index cdc80a0b..be6bc8cf 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -445,7 +445,8 @@ static bool cmd_resize(struct sway_config *config, int argc, char **argv) { if (!parent) { return true; } - // Find the closest possible sibling and resize using that edge + // Find the closest parent container which has siblings of the proper layout. + // Then apply the resize to all of them. int i; if (strcmp(argv[1], "width") == 0) { int lnumber = 0; -- cgit v1.2.3 From c9935507f2bc8e2471f6642e99a2fb52ab1310f0 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Thu, 20 Aug 2015 11:42:01 -0500 Subject: Style fixes --- sway/commands.c | 1 - sway/layout.c | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'sway/commands.c') diff --git a/sway/commands.c b/sway/commands.c index be6bc8cf..27dbb44b 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -563,7 +563,6 @@ static bool cmd_resize(struct sway_config *config, int argc, char **argv) { arrange_windows(active_workspace, -1, -1); return true; } - sway_log(L_INFO, "Done with resize"); return true; } diff --git a/sway/layout.c b/sway/layout.c index b70e1041..c9c8fa7f 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -378,14 +378,14 @@ void recursive_resize(swayc_t *container, double amount, enum movement_direction container->x += (int) amount; container->width += (int) amount; layout_match = container->layout == L_HORIZ; - } else if(dir == MOVE_RIGHT) { + } else if (dir == MOVE_RIGHT) { container->width += (int) amount; layout_match = container->layout == L_HORIZ; - } else if(dir == MOVE_UP) { + } else if (dir == MOVE_UP) { container->y += (int) amount; container->height += (int) amount; layout_match = container->layout == L_VERT; - } else if(dir == MOVE_DOWN) { + } else if (dir == MOVE_DOWN) { container->height += (int) amount; layout_match = container->layout == L_VERT; } -- cgit v1.2.3 From f589731f2912660bab6fdffc14ddcdbac3edd41c Mon Sep 17 00:00:00 2001 From: Luminarys Date: Thu, 20 Aug 2015 21:37:59 -0500 Subject: Rewrite of resize command to make it more sane --- include/container.h | 4 ++-- include/layout.h | 2 +- sway/commands.c | 53 +++++++++++++++++++++++++++++++---------------------- sway/layout.c | 44 ++++++++++++++++++++++++-------------------- sway/log.c | 4 ++-- 5 files changed, 60 insertions(+), 47 deletions(-) (limited to 'sway/commands.c') diff --git a/include/container.h b/include/container.h index bd92058d..d3026011 100644 --- a/include/container.h +++ b/include/container.h @@ -33,12 +33,12 @@ struct sway_container { enum swayc_layouts layout; // Not including borders or margins - int width, height; + double width, height; // Used for setting floating geometry int desired_width, desired_height; - int x, y; + double x, y; bool visible; bool is_floating; diff --git a/include/layout.h b/include/layout.h index 4dff81b7..566e3246 100644 --- a/include/layout.h +++ b/include/layout.h @@ -29,6 +29,6 @@ swayc_t *get_focused_container(swayc_t *parent); swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir); -void recursive_resize(swayc_t *container, double amount, enum movement_direction dir); +void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge); #endif diff --git a/sway/commands.c b/sway/commands.c index 27dbb44b..0f743b4e 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -478,31 +478,37 @@ static bool cmd_resize(struct sway_config *config, int argc, char **argv) { sibling = parent->parent->children->items[i]; if (sibling->x != focused->x) { if (sibling->x < parent->x) { - double pixels = -1 * (amount/lnumber); - if (lnumber) { - recursive_resize(sibling, pixels/2, MOVE_RIGHT); + double pixels = -1 * amount; + pixels /= lnumber; + if (rnumber) { + recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_RIGHT); } else { - recursive_resize(sibling, pixels, MOVE_RIGHT); + recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_RIGHT); } } else if (sibling->x > parent->x) { - double pixels = -1 * (amount/rnumber); - if (rnumber) { - recursive_resize(sibling, pixels/2, MOVE_LEFT); + double pixels = -1 * amount; + pixels /= rnumber; + if (lnumber) { + recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_LEFT); } else { - recursive_resize(sibling, pixels, MOVE_LEFT); + recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_LEFT); } } } else { if (rnumber != 0 && lnumber != 0) { - recursive_resize(parent, amount/2, MOVE_LEFT); - recursive_resize(parent, amount/2, MOVE_RIGHT); + 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, MOVE_RIGHT); + recursive_resize(parent, amount, WLC_RESIZE_EDGE_RIGHT); } else if (lnumber) { - recursive_resize(parent, amount, MOVE_LEFT); + recursive_resize(parent, amount, WLC_RESIZE_EDGE_LEFT); } } } + // Recursive resize does not handle positions, let arrange_windows + // take care of that. arrange_windows(active_workspace, -1, -1); return true; } else if (strcmp(argv[1], "height") == 0) { @@ -535,28 +541,31 @@ static bool cmd_resize(struct sway_config *config, int argc, char **argv) { sibling = parent->parent->children->items[i]; if (sibling->y != focused->y) { if (sibling->y < parent->y) { - double pixels = -1 * (amount/bnumber); + double pixels = -1 * amount; + pixels /= bnumber; if (tnumber) { - recursive_resize(sibling, pixels/2, MOVE_UP); + recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_BOTTOM); } else { - recursive_resize(sibling, pixels, MOVE_UP); + recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_BOTTOM); } } else if (sibling->x > parent->x) { - double pixels = -1 * (amount/tnumber); + double pixels = -1 * amount; + pixels /= tnumber; if (bnumber) { - recursive_resize(sibling, pixels/2, MOVE_DOWN); + recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_TOP); } else { - recursive_resize(sibling, pixels, MOVE_DOWN); + recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_TOP); } } } else { if (bnumber != 0 && tnumber != 0) { - recursive_resize(parent, amount/2, MOVE_UP); - recursive_resize(parent, amount/2, MOVE_DOWN); + 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, MOVE_UP); + recursive_resize(parent, amount, WLC_RESIZE_EDGE_TOP); } else if (bnumber) { - recursive_resize(parent, amount, MOVE_DOWN); + recursive_resize(parent, amount, WLC_RESIZE_EDGE_BOTTOM); } } } diff --git a/sway/layout.c b/sway/layout.c index c9c8fa7f..50e7b592 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -29,7 +29,7 @@ static int index_child(swayc_t *parent, swayc_t *child) { } void add_child(swayc_t *parent, swayc_t *child) { - sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type, + sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); child->parent = parent; @@ -40,7 +40,7 @@ void add_child(swayc_t *parent, swayc_t *child) { } void add_floating(swayc_t *ws, swayc_t *child) { - sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type, + sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, ws, ws->type, ws->width, ws->height); list_add(ws->floating, child); child->parent = ws; @@ -144,7 +144,7 @@ void arrange_windows(swayc_t *container, int width, int height) { child->y = y + container->gaps; child->width = width - container->gaps * 2; child->height = height - container->gaps * 2; - sway_log(L_DEBUG, "Arranging workspace #%d at %d, %d", i, child->x, child->y); + sway_log(L_DEBUG, "Arranging workspace #%d at %f, %f", i, child->x, child->y); arrange_windows(child, -1, -1); } return; @@ -193,7 +193,7 @@ void arrange_windows(swayc_t *container, int width, int height) { default: // Calculate total width for (i = 0; i < container->children->length; ++i) { - int *old_width = &((swayc_t *)container->children->items[i])->width; + 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); @@ -220,7 +220,7 @@ void arrange_windows(swayc_t *container, int width, int height) { case L_VERT: // Calculate total height for (i = 0; i < container->children->length; ++i) { - int *old_height = &((swayc_t *)container->children->items[i])->height; + 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); @@ -371,34 +371,38 @@ swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) } } -void recursive_resize(swayc_t *container, double amount, enum movement_direction dir) { +void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge) { int i; bool layout_match = true; - if (dir == MOVE_LEFT) { - container->x += (int) amount; - container->width += (int) amount; + sway_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); + if (edge == WLC_RESIZE_EDGE_LEFT || edge == WLC_RESIZE_EDGE_RIGHT) { + container->width += amount; layout_match = container->layout == L_HORIZ; - } else if (dir == MOVE_RIGHT) { - container->width += (int) amount; - layout_match = container->layout == L_HORIZ; - } else if (dir == MOVE_UP) { - container->y += (int) amount; - container->height += (int) amount; - layout_match = container->layout == L_VERT; - } else if (dir == MOVE_DOWN) { - container->height += (int) amount; + } else if (edge == WLC_RESIZE_EDGE_TOP || edge == WLC_RESIZE_EDGE_BOTTOM) { + container->height += amount; layout_match = container->layout == L_VERT; } if (container->type == C_VIEW) { + struct wlc_geometry geometry = { + .origin = { + .x = container->x + container->gaps / 2, + .y = container->y + container->gaps / 2 + }, + .size = { + .w = container->width - container->gaps, + .h = container->height - container->gaps + } + }; + wlc_view_set_geometry(container->handle, edge, &geometry); return; } if (layout_match) { for (i = 0; i < container->children->length; i++) { - recursive_resize(container->children->items[i], amount/container->children->length, dir); + recursive_resize(container->children->items[i], amount/container->children->length, edge); } } else { for (i = 0; i < container->children->length; i++) { - recursive_resize(container->children->items[i], amount, dir); + recursive_resize(container->children->items[i], amount, edge); } } } diff --git a/sway/log.c b/sway/log.c index 6e01421b..eda0c88e 100644 --- a/sway/log.c +++ b/sway/log.c @@ -142,8 +142,8 @@ static void container_log(const swayc_t *c) { c->layout == L_STACKED ? "Stacked|": c->layout == L_FLOATING ? "Floating|": "Unknown|"); - fprintf(stderr, "w:%d|h:%d|", c->width, c->height); - fprintf(stderr, "x:%d|y:%d|", c->x, c->y); + fprintf(stderr, "w:%f|h:%f|", c->width, c->height); + fprintf(stderr, "x:%f|y:%f|", c->x, c->y); fprintf(stderr, "vis:%c|", c->visible?'t':'f'); fprintf(stderr, "name:%.16s|", c->name); fprintf(stderr, "children:%d\n",c->children?c->children->length:0); -- cgit v1.2.3