diff options
author | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-08-30 21:00:10 +1000 |
---|---|---|
committer | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-09-05 18:01:43 +1000 |
commit | 7586f150c058997d9dde387ea7c091ffa7a3c3c7 (patch) | |
tree | 63d19027974c1db62ce3a74ca1d2314eb6d5049b /sway/commands/resize.c | |
parent | Merge pull request #2569 from RyanDwyer/deny-reload-repeat (diff) | |
download | sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.tar.gz sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.tar.zst sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.zip |
Implement type safe arguments and demote sway_container
This commit changes the meaning of sway_container so that it only refers
to layout containers and view containers. Workspaces, outputs and the
root are no longer known as containers. Instead, root, outputs,
workspaces and containers are all a type of node, and containers come in
two types: layout containers and view containers.
In addition to the above, this implements type safe variables. This
means we use specific types such as sway_output and sway_workspace
instead of generic containers or nodes. However, it's worth noting that
in a few places places (eg. seat focus and transactions) referring to
them in a generic way is unavoidable which is why we still use nodes in
some places.
If you want a TL;DR, look at node.h, as well as the struct definitions
for root, output, workspace and container. Note that sway_output now
contains a workspaces list, and workspaces now contain a tiling and
floating list, and containers now contain a pointer back to the
workspace.
There are now functions for seat_get_focused_workspace and
seat_get_focused_container. The latter will return NULL if a workspace
itself is focused. Most other seat functions like seat_get_focus and
seat_set_focus now accept and return nodes.
In the config->handler_context struct, current_container has been
replaced with three pointers: node, container and workspace. node is the
same as what current_container was, while workspace is the workspace
that the node resides on and container is the actual container, which
may be NULL if a workspace itself is focused.
The global root_container variable has been replaced with one simply
called root, which is a pointer to the sway_root instance.
The way outputs are created, enabled, disabled and destroyed has
changed. Previously we'd wrap the sway_output in a container when it is
enabled, but as we don't have containers any more it needs a different
approach. The output_create and output_destroy functions previously
created/destroyed the container, but now they create/destroy the
sway_output. There is a new function output_disable to disable an output
without destroying it.
Containers have a new view property. If this is populated then the
container is a view container, otherwise it's a layout container. Like
before, this property is immutable for the life of the container.
Containers have both a `sway_container *parent` and
`sway_workspace *workspace`. As we use specific types now, parent cannot
point to a workspace so it'll be NULL for containers which are direct
children of the workspace. The workspace property is set for all
containers, except those which are hidden in the scratchpad as they have
no workspace.
In some cases we need to refer to workspaces in a container-like way.
For example, workspaces have layout and children, but when using
specific types this makes it difficult. Likewise, it's difficult for a
container to get its parent's layout when the parent could be another
container or a workspace. To make it easier, some helper functions have
been created: container_parent_layout and container_get_siblings.
container_remove_child has been renamed to container_detach and
container_replace_child has been renamed to container_replace.
`container_handle_fullscreen_reparent(con, old_parent)` has had the
old_parent removed. We now unfullscreen the workspace when detaching the
container, so this function is simplified and only needs one argument
now.
container_notify_subtree_changed has been renamed to
container_update_representation. This is more descriptive of its
purpose. I also wanted to be able to call it with whatever container was
changed rather than the container's parent, which makes bubbling up to
the workspace easier.
There are now state structs per node thing. ie. sway_output_state,
sway_workspace_state and sway_container_state.
The focus, move and layout commands have been completely refactored to
work with the specific types. I considered making these a separate PR,
but I'd be backporting my changes only to replace them again, and it's
easier just to test everything at once.
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r-- | sway/commands/resize.c | 86 |
1 files changed, 43 insertions, 43 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c index ad659ef5..99e9dbda 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c | |||
@@ -10,6 +10,7 @@ | |||
10 | #include "sway/commands.h" | 10 | #include "sway/commands.h" |
11 | #include "sway/tree/arrange.h" | 11 | #include "sway/tree/arrange.h" |
12 | #include "sway/tree/view.h" | 12 | #include "sway/tree/view.h" |
13 | #include "sway/tree/workspace.h" | ||
13 | #include "log.h" | 14 | #include "log.h" |
14 | 15 | ||
15 | static const int MIN_SANE_W = 100, MIN_SANE_H = 60; | 16 | static const int MIN_SANE_W = 100, MIN_SANE_H = 60; |
@@ -75,7 +76,7 @@ static int parse_resize_amount(int argc, char **argv, | |||
75 | 76 | ||
76 | static void calculate_constraints(int *min_width, int *max_width, | 77 | static void calculate_constraints(int *min_width, int *max_width, |
77 | int *min_height, int *max_height) { | 78 | int *min_height, int *max_height) { |
78 | struct sway_container *con = config->handler_context.current_container; | 79 | struct sway_container *con = config->handler_context.container; |
79 | 80 | ||
80 | if (config->floating_minimum_width == -1) { // no minimum | 81 | if (config->floating_minimum_width == -1) { // no minimum |
81 | *min_width = 0; | 82 | *min_width = 0; |
@@ -96,8 +97,7 @@ static void calculate_constraints(int *min_width, int *max_width, | |||
96 | if (config->floating_maximum_width == -1) { // no maximum | 97 | if (config->floating_maximum_width == -1) { // no maximum |
97 | *max_width = INT_MAX; | 98 | *max_width = INT_MAX; |
98 | } else if (config->floating_maximum_width == 0) { // automatic | 99 | } else if (config->floating_maximum_width == 0) { // automatic |
99 | struct sway_container *ws = container_parent(con, C_WORKSPACE); | 100 | *max_width = con->workspace->width; |
100 | *max_width = ws->width; | ||
101 | } else { | 101 | } else { |
102 | *max_width = config->floating_maximum_width; | 102 | *max_width = config->floating_maximum_width; |
103 | } | 103 | } |
@@ -105,8 +105,7 @@ static void calculate_constraints(int *min_width, int *max_width, | |||
105 | if (config->floating_maximum_height == -1) { // no maximum | 105 | if (config->floating_maximum_height == -1) { // no maximum |
106 | *max_height = INT_MAX; | 106 | *max_height = INT_MAX; |
107 | } else if (config->floating_maximum_height == 0) { // automatic | 107 | } else if (config->floating_maximum_height == 0) { // automatic |
108 | struct sway_container *ws = container_parent(con, C_WORKSPACE); | 108 | *max_height = con->workspace->height; |
109 | *max_height = ws->height; | ||
110 | } else { | 109 | } else { |
111 | *max_height = config->floating_maximum_height; | 110 | *max_height = config->floating_maximum_height; |
112 | } | 111 | } |
@@ -191,11 +190,11 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
191 | normalize_axis(axis) == RESIZE_AXIS_HORIZONTAL ? L_HORIZ : L_VERT; | 190 | normalize_axis(axis) == RESIZE_AXIS_HORIZONTAL ? L_HORIZ : L_VERT; |
192 | int minor_weight = 0; | 191 | int minor_weight = 0; |
193 | int major_weight = 0; | 192 | int major_weight = 0; |
194 | while (parent->parent) { | 193 | while (parent) { |
195 | struct sway_container *next = parent->parent; | 194 | list_t *siblings = container_get_siblings(parent); |
196 | if (next->layout == parallel_layout) { | 195 | if (container_parent_layout(parent) == parallel_layout) { |
197 | for (int i = 0; i < next->children->length; i++) { | 196 | for (int i = 0; i < siblings->length; i++) { |
198 | struct sway_container *sibling = next->children->items[i]; | 197 | struct sway_container *sibling = siblings->items[i]; |
199 | 198 | ||
200 | int sibling_pos = parallel_coord(sibling, axis); | 199 | int sibling_pos = parallel_coord(sibling, axis); |
201 | int focused_pos = parallel_coord(focused, axis); | 200 | int focused_pos = parallel_coord(focused, axis); |
@@ -213,17 +212,13 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
213 | break; | 212 | break; |
214 | } | 213 | } |
215 | } | 214 | } |
216 | parent = next; | 215 | parent = parent->parent; |
217 | } | 216 | } |
218 | 217 | if (!parent) { | |
219 | if (parent->type == C_ROOT) { | 218 | // Can't resize in this direction |
220 | return; | 219 | return; |
221 | } | 220 | } |
222 | 221 | ||
223 | wlr_log(WLR_DEBUG, | ||
224 | "Found the proper parent: %p. It has %d l conts, and %d r conts", | ||
225 | parent->parent, minor_weight, major_weight); | ||
226 | |||
227 | // Implement up/down/left/right direction by zeroing one of the weights, | 222 | // Implement up/down/left/right direction by zeroing one of the weights, |
228 | // then setting the axis to be horizontal or vertical | 223 | // then setting the axis to be horizontal or vertical |
229 | if (axis == RESIZE_AXIS_UP || axis == RESIZE_AXIS_LEFT) { | 224 | if (axis == RESIZE_AXIS_UP || axis == RESIZE_AXIS_LEFT) { |
@@ -237,9 +232,10 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
237 | 232 | ||
238 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks | 233 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks |
239 | // ^ ????? | 234 | // ^ ????? |
235 | list_t *siblings = container_get_siblings(parent); | ||
240 | 236 | ||
241 | for (int i = 0; i < parent->parent->children->length; i++) { | 237 | for (int i = 0; i < siblings->length; i++) { |
242 | struct sway_container *sibling = parent->parent->children->items[i]; | 238 | struct sway_container *sibling = siblings->items[i]; |
243 | 239 | ||
244 | int sibling_pos = parallel_coord(sibling, axis); | 240 | int sibling_pos = parallel_coord(sibling, axis); |
245 | int focused_pos = parallel_coord(focused, axis); | 241 | int focused_pos = parallel_coord(focused, axis); |
@@ -277,8 +273,8 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
277 | enum wlr_edges major_edge = axis == RESIZE_AXIS_HORIZONTAL ? | 273 | enum wlr_edges major_edge = axis == RESIZE_AXIS_HORIZONTAL ? |
278 | WLR_EDGE_RIGHT : WLR_EDGE_BOTTOM; | 274 | WLR_EDGE_RIGHT : WLR_EDGE_BOTTOM; |
279 | 275 | ||
280 | for (int i = 0; i < parent->parent->children->length; i++) { | 276 | for (int i = 0; i < siblings->length; i++) { |
281 | struct sway_container *sibling = parent->parent->children->items[i]; | 277 | struct sway_container *sibling = siblings->items[i]; |
282 | 278 | ||
283 | int sibling_pos = parallel_coord(sibling, axis); | 279 | int sibling_pos = parallel_coord(sibling, axis); |
284 | int focused_pos = parallel_coord(focused, axis); | 280 | int focused_pos = parallel_coord(focused, axis); |
@@ -316,7 +312,11 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
316 | } | 312 | } |
317 | } | 313 | } |
318 | 314 | ||
319 | arrange_windows(parent->parent); | 315 | if (parent->parent) { |
316 | arrange_container(parent->parent); | ||
317 | } else { | ||
318 | arrange_workspace(parent->workspace); | ||
319 | } | ||
320 | } | 320 | } |
321 | 321 | ||
322 | void container_resize_tiled(struct sway_container *parent, | 322 | void container_resize_tiled(struct sway_container *parent, |
@@ -346,7 +346,7 @@ void container_resize_tiled(struct sway_container *parent, | |||
346 | */ | 346 | */ |
347 | static struct cmd_results *resize_adjust_floating(enum resize_axis axis, | 347 | static struct cmd_results *resize_adjust_floating(enum resize_axis axis, |
348 | struct resize_amount *amount) { | 348 | struct resize_amount *amount) { |
349 | struct sway_container *con = config->handler_context.current_container; | 349 | struct sway_container *con = config->handler_context.container; |
350 | int grow_width = 0, grow_height = 0; | 350 | int grow_width = 0, grow_height = 0; |
351 | switch (axis) { | 351 | switch (axis) { |
352 | case RESIZE_AXIS_HORIZONTAL: | 352 | case RESIZE_AXIS_HORIZONTAL: |
@@ -400,15 +400,15 @@ static struct cmd_results *resize_adjust_floating(enum resize_axis axis, | |||
400 | con->width += grow_width; | 400 | con->width += grow_width; |
401 | con->height += grow_height; | 401 | con->height += grow_height; |
402 | 402 | ||
403 | if (con->type == C_VIEW) { | 403 | if (con->view) { |
404 | struct sway_view *view = con->sway_view; | 404 | struct sway_view *view = con->view; |
405 | view->x += grow_x; | 405 | view->x += grow_x; |
406 | view->y += grow_y; | 406 | view->y += grow_y; |
407 | view->width += grow_width; | 407 | view->width += grow_width; |
408 | view->height += grow_height; | 408 | view->height += grow_height; |
409 | } | 409 | } |
410 | 410 | ||
411 | arrange_windows(con); | 411 | arrange_container(con); |
412 | 412 | ||
413 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | 413 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); |
414 | } | 414 | } |
@@ -418,7 +418,7 @@ static struct cmd_results *resize_adjust_floating(enum resize_axis axis, | |||
418 | */ | 418 | */ |
419 | static struct cmd_results *resize_adjust_tiled(enum resize_axis axis, | 419 | static struct cmd_results *resize_adjust_tiled(enum resize_axis axis, |
420 | struct resize_amount *amount) { | 420 | struct resize_amount *amount) { |
421 | struct sway_container *current = config->handler_context.current_container; | 421 | struct sway_container *current = config->handler_context.container; |
422 | 422 | ||
423 | if (amount->unit == RESIZE_UNIT_DEFAULT) { | 423 | if (amount->unit == RESIZE_UNIT_DEFAULT) { |
424 | amount->unit = RESIZE_UNIT_PPT; | 424 | amount->unit = RESIZE_UNIT_PPT; |
@@ -456,13 +456,15 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con, | |||
456 | width->unit == RESIZE_UNIT_DEFAULT) { | 456 | width->unit == RESIZE_UNIT_DEFAULT) { |
457 | // Convert to px | 457 | // Convert to px |
458 | struct sway_container *parent = con->parent; | 458 | struct sway_container *parent = con->parent; |
459 | while (parent->type >= C_WORKSPACE && parent->layout != L_HORIZ) { | 459 | while (parent && parent->layout != L_HORIZ) { |
460 | parent = parent->parent; | 460 | parent = parent->parent; |
461 | } | 461 | } |
462 | if (parent->type >= C_WORKSPACE) { | 462 | if (parent) { |
463 | width->amount = parent->width * width->amount / 100; | 463 | width->amount = parent->width * width->amount / 100; |
464 | width->unit = RESIZE_UNIT_PX; | 464 | } else { |
465 | width->amount = con->workspace->width * width->amount / 100; | ||
465 | } | 466 | } |
467 | width->unit = RESIZE_UNIT_PX; | ||
466 | } | 468 | } |
467 | if (width->unit == RESIZE_UNIT_PX) { | 469 | if (width->unit == RESIZE_UNIT_PX) { |
468 | resize_tiled(con, width->amount - con->width, | 470 | resize_tiled(con, width->amount - con->width, |
@@ -475,13 +477,15 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con, | |||
475 | height->unit == RESIZE_UNIT_DEFAULT) { | 477 | height->unit == RESIZE_UNIT_DEFAULT) { |
476 | // Convert to px | 478 | // Convert to px |
477 | struct sway_container *parent = con->parent; | 479 | struct sway_container *parent = con->parent; |
478 | while (parent->type >= C_WORKSPACE && parent->layout != L_VERT) { | 480 | while (parent && parent->layout != L_VERT) { |
479 | parent = parent->parent; | 481 | parent = parent->parent; |
480 | } | 482 | } |
481 | if (parent->type >= C_WORKSPACE) { | 483 | if (parent) { |
482 | height->amount = parent->height * height->amount / 100; | 484 | height->amount = parent->height * height->amount / 100; |
483 | height->unit = RESIZE_UNIT_PX; | 485 | } else { |
486 | height->amount = con->workspace->height * height->amount / 100; | ||
484 | } | 487 | } |
488 | height->unit = RESIZE_UNIT_PX; | ||
485 | } | 489 | } |
486 | if (height->unit == RESIZE_UNIT_PX) { | 490 | if (height->unit == RESIZE_UNIT_PX) { |
487 | resize_tiled(con, height->amount - con->height, | 491 | resize_tiled(con, height->amount - con->height, |
@@ -508,15 +512,15 @@ static struct cmd_results *resize_set_floating(struct sway_container *con, | |||
508 | con->width = width->amount; | 512 | con->width = width->amount; |
509 | con->height = height->amount; | 513 | con->height = height->amount; |
510 | 514 | ||
511 | if (con->type == C_VIEW) { | 515 | if (con->view) { |
512 | struct sway_view *view = con->sway_view; | 516 | struct sway_view *view = con->view; |
513 | view->x -= grow_width / 2; | 517 | view->x -= grow_width / 2; |
514 | view->y -= grow_height / 2; | 518 | view->y -= grow_height / 2; |
515 | view->width += grow_width; | 519 | view->width += grow_width; |
516 | view->height += grow_height; | 520 | view->height += grow_height; |
517 | } | 521 | } |
518 | 522 | ||
519 | arrange_windows(con); | 523 | arrange_container(con); |
520 | 524 | ||
521 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | 525 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); |
522 | } | 526 | } |
@@ -555,7 +559,7 @@ static struct cmd_results *cmd_resize_set(int argc, char **argv) { | |||
555 | } | 559 | } |
556 | 560 | ||
557 | // If 0, don't resize that dimension | 561 | // If 0, don't resize that dimension |
558 | struct sway_container *con = config->handler_context.current_container; | 562 | struct sway_container *con = config->handler_context.container; |
559 | if (width.amount <= 0) { | 563 | if (width.amount <= 0) { |
560 | width.amount = con->width; | 564 | width.amount = con->width; |
561 | } | 565 | } |
@@ -624,7 +628,7 @@ static struct cmd_results *cmd_resize_adjust(int argc, char **argv, | |||
624 | first_amount.amount *= multiplier; | 628 | first_amount.amount *= multiplier; |
625 | second_amount.amount *= multiplier; | 629 | second_amount.amount *= multiplier; |
626 | 630 | ||
627 | struct sway_container *con = config->handler_context.current_container; | 631 | struct sway_container *con = config->handler_context.container; |
628 | if (container_is_floating(con)) { | 632 | if (container_is_floating(con)) { |
629 | // Floating containers can only resize in px. Choose an amount which | 633 | // Floating containers can only resize in px. Choose an amount which |
630 | // uses px, with fallback to an amount that specified no unit. | 634 | // uses px, with fallback to an amount that specified no unit. |
@@ -657,14 +661,10 @@ static struct cmd_results *cmd_resize_adjust(int argc, char **argv, | |||
657 | } | 661 | } |
658 | 662 | ||
659 | struct cmd_results *cmd_resize(int argc, char **argv) { | 663 | struct cmd_results *cmd_resize(int argc, char **argv) { |
660 | struct sway_container *current = config->handler_context.current_container; | 664 | struct sway_container *current = config->handler_context.container; |
661 | if (!current) { | 665 | if (!current) { |
662 | return cmd_results_new(CMD_INVALID, "resize", "Cannot resize nothing"); | 666 | return cmd_results_new(CMD_INVALID, "resize", "Cannot resize nothing"); |
663 | } | 667 | } |
664 | if (current->type != C_VIEW && current->type != C_CONTAINER) { | ||
665 | return cmd_results_new(CMD_INVALID, "resize", | ||
666 | "Can only resize views/containers"); | ||
667 | } | ||
668 | 668 | ||
669 | struct cmd_results *error; | 669 | struct cmd_results *error; |
670 | if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { | 670 | if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { |