diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-06-30 06:27:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-30 06:27:39 -0700 |
commit | d8c61c976372eedf271f505ffd82c461b6503f6f (patch) | |
tree | 323739dc80680dd774acfdcf43eb76cfe93aa64c /sway/tree | |
parent | 9ba72433b6c87086f2772405e09e8ac8c0136a01 (diff) | |
parent | e396af853b01438f7e5ef34bfa6fd2507d11ce5a (diff) |
Merge pull request #2072 from RyanDwyer/atomic
Atomic layout updates
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/arrange.c | 338 | ||||
-rw-r--r-- | sway/tree/container.c | 214 | ||||
-rw-r--r-- | sway/tree/layout.c | 76 | ||||
-rw-r--r-- | sway/tree/output.c | 3 | ||||
-rw-r--r-- | sway/tree/view.c | 178 | ||||
-rw-r--r-- | sway/tree/workspace.c | 5 |
6 files changed, 407 insertions, 407 deletions
diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index 53c95820..582b2891 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -5,7 +5,6 @@ #include <string.h> #include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output_layout.h> -#include "sway/debug.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/layout.h" @@ -17,116 +16,6 @@ struct sway_container root_container; -void arrange_root() { - if (config->reloading) { - return; - } - struct wlr_output_layout *output_layout = - root_container.sway_root->output_layout; - const struct wlr_box *layout_box = - wlr_output_layout_get_box(output_layout, NULL); - root_container.x = layout_box->x; - root_container.y = layout_box->y; - root_container.width = layout_box->width; - root_container.height = layout_box->height; - for (int i = 0; i < root_container.children->length; ++i) { - struct sway_container *output = root_container.children->items[i]; - arrange_output(output); - } -} - -void arrange_output(struct sway_container *output) { - if (config->reloading) { - return; - } - if (!sway_assert(output->type == C_OUTPUT, - "called arrange_output() on non-output container")) { - return; - } - - const struct wlr_box *output_box = wlr_output_layout_get_box( - root_container.sway_root->output_layout, - output->sway_output->wlr_output); - output->x = output_box->x; - output->y = output_box->y; - output->width = output_box->width; - output->height = output_box->height; - wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", - output->name, output->x, output->y); - - for (int i = 0; i < output->children->length; ++i) { - struct sway_container *workspace = output->children->items[i]; - arrange_workspace(workspace); - } - container_damage_whole(output); -} - -void arrange_workspace(struct sway_container *workspace) { - if (config->reloading) { - return; - } - if (!sway_assert(workspace->type == C_WORKSPACE, - "called arrange_workspace() on non-workspace container")) { - return; - } - - struct sway_container *output = workspace->parent; - struct wlr_box *area = &output->sway_output->usable_area; - wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", - area->width, area->height, area->x, area->y); - - remove_gaps(workspace); - - workspace->width = area->width; - workspace->height = area->height; - workspace->x = output->x + area->x; - workspace->y = output->y + area->y; - - add_gaps(workspace); - - wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", - workspace->name, workspace->x, workspace->y); - arrange_children_of(workspace); -} - -void remove_gaps(struct sway_container *c) { - if (c->current_gaps == 0) { - wlr_log(L_DEBUG, "Removing gaps: not gapped: %p", c); - return; - } - - c->width += c->current_gaps * 2; - c->height += c->current_gaps * 2; - c->x -= c->current_gaps; - c->y -= c->current_gaps; - - c->current_gaps = 0; - - wlr_log(L_DEBUG, "Removing gaps %p", c); -} - -void add_gaps(struct sway_container *c) { - if (c->current_gaps > 0 || c->type == C_CONTAINER) { - wlr_log(L_DEBUG, "Not adding gaps: %p", c); - return; - } - - if (c->type == C_WORKSPACE && - !(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) { - return; - } - - double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner; - - c->x += gaps; - c->y += gaps; - c->width -= 2 * gaps; - c->height -= 2 * gaps; - c->current_gaps = gaps; - - wlr_log(L_DEBUG, "Adding gaps: %p", c); -} - static void apply_horiz_layout(struct sway_container *parent) { size_t num_children = parent->children->length; if (!num_children) { @@ -136,8 +25,8 @@ static void apply_horiz_layout(struct sway_container *parent) { if (parent->parent->layout == L_TABBED) { parent_offset = container_titlebar_height(); } else if (parent->parent->layout == L_STACKED) { - parent_offset = - container_titlebar_height() * parent->parent->children->length; + parent_offset = container_titlebar_height() * + parent->parent->children->length; } size_t parent_height = parent->height - parent_offset; @@ -145,7 +34,6 @@ static void apply_horiz_layout(struct sway_container *parent) { double total_width = 0; for (size_t i = 0; i < num_children; ++i) { struct sway_container *child = parent->children->items[i]; - if (child->width <= 0) { if (num_children > 1) { child->width = parent->width / (num_children - 1); @@ -161,25 +49,21 @@ static void apply_horiz_layout(struct sway_container *parent) { // Resize windows wlr_log(L_DEBUG, "Arranging %p horizontally", parent); double child_x = parent->x; - struct sway_container *child; for (size_t i = 0; i < num_children; ++i) { - child = parent->children->items[i]; + struct sway_container *child = parent->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, child->width, scale); child->x = child_x; child->y = parent->y + parent_offset; + child->width = floor(child->width * scale); child->height = parent_height; + child_x += child->width; + // Make last child use remaining width of parent if (i == num_children - 1) { - // Make last child use remaining width of parent child->width = parent->x + parent->width - child->x; - } else { - child->width = floor(child->width * scale); } - - child_x += child->width; - add_gaps(child); } } @@ -202,7 +86,6 @@ static void apply_vert_layout(struct sway_container *parent) { double total_height = 0; for (size_t i = 0; i < num_children; ++i) { struct sway_container *child = parent->children->items[i]; - if (child->height <= 0) { if (num_children > 1) { child->height = parent_height / (num_children - 1); @@ -218,25 +101,22 @@ static void apply_vert_layout(struct sway_container *parent) { // Resize wlr_log(L_DEBUG, "Arranging %p vertically", parent); double child_y = parent->y + parent_offset; - struct sway_container *child; for (size_t i = 0; i < num_children; ++i) { - child = parent->children->items[i]; + struct sway_container *child = parent->children->items[i]; wlr_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, child->height, scale); child->x = parent->x; child->y = child_y; child->width = parent->width; + child->height = floor(child->height * scale); + child_y += child->height; + // Make last child use remaining height of parent if (i == num_children - 1) { - // Make last child use remaining height of parent - child->height = parent->y + parent_offset + parent_height - child->y; - } else { - child->height = floor(child->height * scale); + child->height = + parent->y + parent_offset + parent_height - child->y; } - - child_y += child->height; - add_gaps(child); } } @@ -264,26 +144,41 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { } } -void arrange_children_of(struct sway_container *parent) { - if (config->reloading) { - return; - } - if (!sway_assert(parent->type == C_WORKSPACE || parent->type == C_CONTAINER, - "container is a %s", container_type_to_str(parent->type))) { - return; +/** + * If a container has been deleted from the pending tree state, we must add it + * to the transaction so it can be freed afterwards. To do this, we iterate the + * server's destroying_containers list and add all of them. We may add more than + * what we need to, but this is easy and has no negative consequences. + */ +static void add_deleted_containers(struct sway_transaction *transaction) { + for (int i = 0; i < server.destroying_containers->length; ++i) { + struct sway_container *child = server.destroying_containers->items[i]; + transaction_add_container(transaction, child); } +} - struct sway_container *workspace = parent; - if (workspace->type != C_WORKSPACE) { - workspace = container_parent(workspace, C_WORKSPACE); +static void arrange_children_of(struct sway_container *parent, + struct sway_transaction *transaction); + +static void arrange_floating(struct sway_container *floating, + struct sway_transaction *transaction) { + for (int i = 0; i < floating->children->length; ++i) { + struct sway_container *floater = floating->children->items[i]; + if (floater->type == C_VIEW) { + view_autoconfigure(floater->sway_view); + } else { + arrange_children_of(floater, transaction); + } + transaction_add_container(transaction, floater); } + transaction_add_container(transaction, floating); +} - if (workspace->sway_workspace->fullscreen) { - // Just arrange the fullscreen view and jump out - view_autoconfigure(workspace->sway_workspace->fullscreen); +static void arrange_children_of(struct sway_container *parent, + struct sway_transaction *transaction) { + if (config->reloading) { return; } - wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent, parent->name, parent->width, parent->height, parent->x, parent->y); @@ -299,13 +194,15 @@ void arrange_children_of(struct sway_container *parent) { case L_STACKED: apply_tabbed_or_stacked_layout(parent); break; - default: - wlr_log(L_DEBUG, "TODO: arrange layout type %d", parent->layout); + case L_NONE: apply_horiz_layout(parent); break; + case L_FLOATING: + arrange_floating(parent, transaction); + break; } - // Apply x, y, width and height to children and recurse if needed + // Recurse into child containers for (int i = 0; i < parent->children->length; ++i) { struct sway_container *child = parent->children->items[i]; if (parent->has_gaps && !child->has_gaps) { @@ -316,21 +213,140 @@ void arrange_children_of(struct sway_container *parent) { if (child->type == C_VIEW) { view_autoconfigure(child->sway_view); } else { - arrange_children_of(child); + arrange_children_of(child, transaction); } + transaction_add_container(transaction, child); } +} - // If container is a workspace, process floating containers too - if (parent->type == C_WORKSPACE) { - struct sway_workspace *ws = workspace->sway_workspace; - for (int i = 0; i < ws->floating->children->length; ++i) { - struct sway_container *child = ws->floating->children->items[i]; - if (child->type != C_VIEW) { - arrange_children_of(child); - } - } +static void arrange_workspace(struct sway_container *workspace, + struct sway_transaction *transaction) { + if (config->reloading) { + return; + } + struct sway_container *output = workspace->parent; + struct wlr_box *area = &output->sway_output->usable_area; + wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", + area->width, area->height, area->x, area->y); + remove_gaps(workspace); + workspace->width = area->width; + workspace->height = area->height; + workspace->x = output->x + area->x; + workspace->y = output->y + area->y; + add_gaps(workspace); + transaction_add_container(transaction, workspace); + wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, + workspace->x, workspace->y); + arrange_floating(workspace->sway_workspace->floating, transaction); + arrange_children_of(workspace, transaction); +} + +static void arrange_output(struct sway_container *output, + struct sway_transaction *transaction) { + if (config->reloading) { + return; + } + const struct wlr_box *output_box = wlr_output_layout_get_box( + root_container.sway_root->output_layout, + output->sway_output->wlr_output); + output->x = output_box->x; + output->y = output_box->y; + output->width = output_box->width; + output->height = output_box->height; + transaction_add_container(transaction, output); + wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", + output->name, output->x, output->y); + for (int i = 0; i < output->children->length; ++i) { + struct sway_container *workspace = output->children->items[i]; + arrange_workspace(workspace, transaction); + } +} + +static void arrange_root(struct sway_transaction *transaction) { + if (config->reloading) { + return; + } + struct wlr_output_layout *output_layout = + root_container.sway_root->output_layout; + const struct wlr_box *layout_box = + wlr_output_layout_get_box(output_layout, NULL); + root_container.x = layout_box->x; + root_container.y = layout_box->y; + root_container.width = layout_box->width; + root_container.height = layout_box->height; + transaction_add_container(transaction, &root_container); + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + arrange_output(output, transaction); + } +} + +void arrange_windows(struct sway_container *container, + struct sway_transaction *transaction) { + switch (container->type) { + case C_ROOT: + arrange_root(transaction); + break; + case C_OUTPUT: + arrange_output(container, transaction); + break; + case C_WORKSPACE: + arrange_workspace(container, transaction); + break; + case C_CONTAINER: + arrange_children_of(container, transaction); + transaction_add_container(transaction, container); + break; + case C_VIEW: + view_autoconfigure(container->sway_view); + transaction_add_container(transaction, container); + break; + case C_TYPES: + break; + } + add_deleted_containers(transaction); +} + +void arrange_and_commit(struct sway_container *container) { + struct sway_transaction *transaction = transaction_create(); + arrange_windows(container, transaction); + transaction_commit(transaction); +} + +void remove_gaps(struct sway_container *c) { + if (c->current_gaps == 0) { + wlr_log(L_DEBUG, "Removing gaps: not gapped: %p", c); + return; } - container_damage_whole(parent); - update_debug_tree(); + c->width += c->current_gaps * 2; + c->height += c->current_gaps * 2; + c->x -= c->current_gaps; + c->y -= c->current_gaps; + + c->current_gaps = 0; + + wlr_log(L_DEBUG, "Removing gaps %p", c); +} + +void add_gaps(struct sway_container *c) { + if (c->current_gaps > 0 || c->type == C_CONTAINER) { + wlr_log(L_DEBUG, "Not adding gaps: %p", c); + return; + } + + if (c->type == C_WORKSPACE && + !(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) { + return; + } + + double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner; + + c->x += gaps; + c->y += gaps; + c->width -= 2 * gaps; + c->height -= 2 * gaps; + c->current_gaps = gaps; + + wlr_log(L_DEBUG, "Adding gaps: %p", c); } diff --git a/sway/tree/container.c b/sway/tree/container.c index 2de0c7a8..6f6137c4 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -16,7 +16,6 @@ #include "sway/ipc-server.h" #include "sway/output.h" #include "sway/server.h" -#include "sway/tree/arrange.h" #include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" @@ -113,9 +112,11 @@ struct sway_container *container_create(enum sway_container_type type) { c->layout = L_NONE; c->type = type; c->alpha = 1.0f; + c->instructions = create_list(); if (type != C_VIEW) { c->children = create_list(); + c->current.children = create_list(); } wl_signal_init(&c->events.destroy); @@ -132,42 +133,67 @@ struct sway_container *container_create(enum sway_container_type type) { return c; } -static void _container_destroy(struct sway_container *cont) { - if (cont == NULL) { - return; - } - - wl_signal_emit(&cont->events.destroy, cont); +static void container_workspace_free(struct sway_workspace *ws) { + list_foreach(ws->output_priority, free); + list_free(ws->output_priority); + ws->floating->destroying = true; + container_free(ws->floating); + free(ws); +} - struct sway_container *parent = cont->parent; - if (cont->children != NULL && cont->children->length) { - // remove children until there are no more, container_destroy calls - // container_remove_child, which removes child from this container - while (cont->children != NULL && cont->children->length > 0) { - struct sway_container *child = cont->children->items[0]; - ipc_event_window(child, "close"); - container_remove_child(child); - _container_destroy(child); - } - } - if (cont->marks) { - list_foreach(cont->marks, free); - list_free(cont->marks); - } - if (parent) { - parent = container_remove_child(cont); +void container_free(struct sway_container *cont) { + if (!sway_assert(cont->destroying, + "Tried to free container which wasn't marked as destroying")) { + return; } - if (cont->name) { - free(cont->name); + if (!sway_assert(cont->instructions->length == 0, + "Tried to free container with pending instructions")) { + return; } - + free(cont->name); wlr_texture_destroy(cont->title_focused); wlr_texture_destroy(cont->title_focused_inactive); wlr_texture_destroy(cont->title_unfocused); wlr_texture_destroy(cont->title_urgent); + for (int i = 0; i < server.destroying_containers->length; ++i) { + if (server.destroying_containers->items[i] == cont) { + list_del(server.destroying_containers, i); + break; + } + } + + list_free(cont->instructions); list_free(cont->children); - cont->children = NULL; + list_free(cont->current.children); + + switch (cont->type) { + case C_ROOT: + break; + case C_OUTPUT: + break; + case C_WORKSPACE: + container_workspace_free(cont->sway_workspace); + break; + case C_CONTAINER: + break; + case C_VIEW: + { + struct sway_view *view = cont->sway_view; + view->swayc = NULL; + free(view->title_format); + view->title_format = NULL; + + if (view->destroying) { + view_free(view); + } + } + break; + case C_TYPES: + sway_assert(false, "Didn't expect to see C_TYPES here"); + break; + } + free(cont); } @@ -184,7 +210,6 @@ static struct sway_container *container_workspace_destroy( } wlr_log(L_DEBUG, "destroying workspace '%s'", workspace->name); - ipc_event_window(workspace, "close"); struct sway_container *parent = workspace->parent; if (!workspace_is_empty(workspace) && output) { @@ -209,24 +234,6 @@ static struct sway_container *container_workspace_destroy( } } - struct sway_workspace *sway_workspace = workspace->sway_workspace; - - // This emits the destroy event and also destroys the swayc. - _container_destroy(workspace); - - // Clean up the floating container - sway_workspace->floating->parent = NULL; - _container_destroy(sway_workspace->floating); - - list_foreach(sway_workspace->output_priority, free); - list_free(sway_workspace->output_priority); - - free(sway_workspace); - - if (output) { - output_damage_whole(output->sway_output); - } - return parent; } @@ -263,11 +270,10 @@ static struct sway_container *container_output_destroy( container_add_child(new_output, workspace); ipc_event_workspace(workspace, NULL, "move"); } else { - container_workspace_destroy(workspace); + container_destroy(workspace); } container_sort_workspaces(new_output); - arrange_output(new_output); } } } @@ -280,14 +286,48 @@ static struct sway_container *container_output_destroy( wl_list_remove(&output->sway_output->damage_frame.link); output->sway_output->swayc = NULL; + output->sway_output = NULL; wlr_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); - _container_destroy(output); + return &root_container; } -static void container_root_finish(struct sway_container *con) { - wlr_log(L_ERROR, "TODO: destroy the root container"); +/** + * Implement the actual destroy logic, without reaping. + */ +static struct sway_container *container_destroy_noreaping( + struct sway_container *con) { + if (con == NULL) { + return NULL; + } + if (con->destroying) { + return NULL; + } + + wl_signal_emit(&con->events.destroy, con); + ipc_event_window(con, "close"); + + // The below functions move their children to somewhere else. + if (con->type == C_OUTPUT) { + container_output_destroy(con); + } else if (con->type == C_WORKSPACE) { + // Workspaces will refuse to be destroyed if they're the last workspace + // on their output. + if (!container_workspace_destroy(con)) { + wlr_log(L_ERROR, "workspace doesn't want to destroy"); + return NULL; + } + } + + con->destroying = true; + list_add(server.destroying_containers, con); + + if (!con->parent) { + return NULL; + } + + return container_remove_child(con); } bool container_reap_empty(struct sway_container *con) { @@ -303,13 +343,13 @@ bool container_reap_empty(struct sway_container *con) { case C_WORKSPACE: if (!workspace_is_visible(con) && workspace_is_empty(con)) { wlr_log(L_DEBUG, "Destroying workspace via reaper"); - container_workspace_destroy(con); + container_destroy_noreaping(con); return true; } break; case C_CONTAINER: if (con->children->length == 0) { - _container_destroy(con); + container_destroy_noreaping(con); return true; } case C_VIEW: @@ -340,50 +380,25 @@ struct sway_container *container_flatten(struct sway_container *container) { struct sway_container *child = container->children->items[0]; struct sway_container *parent = container->parent; container_replace_child(container, child); - container_destroy(container); + container_destroy_noreaping(container); container = parent; } return container; } +/** + * container_destroy() is the first step in destroying a container. We'll emit + * events, detach it from the tree and mark it as destroying. The container will + * remain in memory until it's no longer used by a transaction, then it will be + * freed via container_free(). + * + * This function just wraps container_destroy_noreaping(), then does reaping. + */ struct sway_container *container_destroy(struct sway_container *con) { - if (con == NULL) { - return NULL; - } - - struct sway_container *parent = con->parent; + struct sway_container *parent = container_destroy_noreaping(con); - switch (con->type) { - case C_ROOT: - container_root_finish(con); - break; - case C_OUTPUT: - // dont try to reap the root after this - container_output_destroy(con); - break; - case C_WORKSPACE: - // dont try to reap the output after this - container_workspace_destroy(con); - break; - case C_CONTAINER: - if (con->children->length) { - for (int i = 0; i < con->children->length; ++i) { - struct sway_container *child = con->children->items[0]; - ipc_event_window(child, "close"); - container_remove_child(child); - container_add_child(parent, child); - } - } - ipc_event_window(con, "close"); - _container_destroy(con); - break; - case C_VIEW: - _container_destroy(con); - break; - case C_TYPES: - wlr_log(L_ERROR, "container_destroy called on an invalid " - "container"); - break; + if (!parent) { + return NULL; } return container_reap_empty_recursive(parent); @@ -753,9 +768,6 @@ static void update_title_texture(struct sway_container *con, "Unexpected type %s", container_type_to_str(con->type))) { return; } - if (!con->width) { - return; - } struct sway_container *output = container_parent(con, C_OUTPUT); if (!output) { return; @@ -917,13 +929,12 @@ void container_set_floating(struct sway_container *container, bool enable) { struct sway_container *workspace = container_parent(container, C_WORKSPACE); struct sway_seat *seat = input_manager_current_seat(input_manager); - container_damage_whole(container); if (enable) { container_remove_child(container); container_add_child(workspace->sway_workspace->floating, container); if (container->type == C_VIEW) { - view_autoconfigure(container->sway_view); + view_init_floating(container->sway_view); } seat_set_focus(seat, seat_get_focus_inactive(seat, container)); container_reap_empty_recursive(workspace); @@ -939,8 +950,8 @@ void container_set_floating(struct sway_container *container, bool enable) { container->is_sticky = false; container_reap_empty_recursive(workspace->sway_workspace->floating); } - arrange_workspace(workspace); - container_damage_whole(container); + + ipc_event_window(container, "floating"); } void container_set_geometry_from_floating_view(struct sway_container *con) { @@ -969,3 +980,10 @@ bool container_is_floating(struct sway_container *container) { } return container->parent == workspace->sway_workspace->floating; } + +void container_get_box(struct sway_container *container, struct wlr_box *box) { + box->x = container->x; + box->y = container->y; + box->width = container->width; + box->height = container->height; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index d1ad044d..14631ad4 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -22,7 +22,7 @@ struct sway_container root_container; static void output_layout_handle_change(struct wl_listener *listener, void *data) { - arrange_root(); + arrange_and_commit(&root_container); } void layout_init(void) { @@ -30,7 +30,9 @@ void layout_init(void) { root_container.type = C_ROOT; root_container.layout = L_NONE; root_container.name = strdup("root"); + root_container.instructions = create_list(); root_container.children = create_list(); + root_container.current.children = create_list(); wl_signal_init(&root_container.events.destroy); root_container.sway_root = calloc(1, sizeof(*root_container.sway_root)); @@ -57,18 +59,17 @@ static int index_child(const struct sway_container *child) { return -1; } -static void container_handle_fullscreen_reparent(struct sway_container *viewcon, +static void container_handle_fullscreen_reparent(struct sway_container *con, struct sway_container *old_parent) { - if (viewcon->type != C_VIEW || !viewcon->sway_view->is_fullscreen) { + if (con->type != C_VIEW || !con->sway_view->is_fullscreen) { return; } - struct sway_view *view = viewcon->sway_view; + struct sway_view *view = con->sway_view; struct sway_container *old_workspace = old_parent; if (old_workspace && old_workspace->type != C_WORKSPACE) { old_workspace = container_parent(old_workspace, C_WORKSPACE); } - struct sway_container *new_workspace = container_parent(view->swayc, - C_WORKSPACE); + struct sway_container *new_workspace = container_parent(con, C_WORKSPACE); if (old_workspace == new_workspace) { return; } @@ -79,15 +80,19 @@ static void container_handle_fullscreen_reparent(struct sway_container *viewcon, // Mark the new workspace as fullscreen if (new_workspace->sway_workspace->fullscreen) { - view_set_fullscreen_raw( - new_workspace->sway_workspace->fullscreen, false); + view_set_fullscreen(new_workspace->sway_workspace->fullscreen, false); } new_workspace->sway_workspace->fullscreen = view; // Resize view to new output dimensions struct sway_container *output = new_workspace->parent; - view_configure(view, 0, 0, output->width, output->height); - view->swayc->width = output->width; - view->swayc->height = output->height; + view->x = output->x; + view->y = output->y; + view->width = output->width; + view->height = output->height; + con->x = output->x; + con->y = output->y; + con->width = output->width; + con->height = output->height; } void container_insert_child(struct sway_container *parent, @@ -189,18 +194,7 @@ void container_move_to(struct sway_container *container, } container_notify_subtree_changed(old_parent); container_notify_subtree_changed(new_parent); - if (old_parent) { - if (old_parent->type == C_OUTPUT) { - arrange_output(old_parent); - } else { - arrange_children_of(old_parent); - } - } - if (new_parent->type == C_OUTPUT) { - arrange_output(new_parent); - } else { - arrange_children_of(new_parent); - } + // If view was moved to a fullscreen workspace, refocus the fullscreen view struct sway_container *new_workspace = container; if (new_workspace->type != C_WORKSPACE) { @@ -215,7 +209,8 @@ void container_move_to(struct sway_container *container, if (focus_ws->type != C_WORKSPACE) { focus_ws = container_parent(focus_ws, C_WORKSPACE); } - seat_set_focus(seat, new_workspace->sway_workspace->fullscreen->swayc); + seat_set_focus(seat, + new_workspace->sway_workspace->fullscreen->swayc); if (focus_ws != new_workspace) { seat_set_focus(seat, focus); } @@ -309,7 +304,6 @@ static void workspace_rejigger(struct sway_container *ws, container_reap_empty_recursive(original_parent); wl_signal_emit(&child->events.reparent, original_parent); container_create_notify(new_parent); - arrange_workspace(ws); } static void move_out_of_tabs_stacks(struct sway_container *container, @@ -320,11 +314,6 @@ static void move_out_of_tabs_stacks(struct sway_container *container, wlr_log(L_DEBUG, "Changing layout of %zd", current->parent->id); current->parent->layout = move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; - if (current->parent->type == C_WORKSPACE) { - arrange_workspace(current->parent); - } else { - arrange_children_of(current->parent); - } return; } @@ -340,11 +329,6 @@ static void move_out_of_tabs_stacks(struct sway_container *container, container_flatten(new_parent->parent); } container_create_notify(new_parent); - if (is_workspace) { - arrange_workspace(new_parent->parent); - } else { - arrange_children_of(new_parent); - } container_notify_subtree_changed(new_parent); } @@ -368,10 +352,7 @@ void container_move(struct sway_container *container, struct sway_container *new_parent = container_flatten(parent); if (new_parent != parent) { - // Special case: we were the last one in this container, so flatten it - // and leave - arrange_children_of(new_parent); - update_debug_tree(); + // Special case: we were the last one in this container, so leave return; } @@ -453,12 +434,9 @@ void container_move(struct sway_container *container, wlr_log(L_DEBUG, "Hit limit, " "promoting descendant to sibling"); // Special case - struct sway_container *old_parent = container->parent; container_insert_child(current->parent, container, index + (offs < 0 ? 0 : 1)); container->width = container->height = 0; - arrange_children_of(current->parent); - arrange_children_of(old_parent); return; } } else { @@ -492,14 +470,11 @@ void container_move(struct sway_container *container, wlr_log(L_DEBUG, "Swapping siblings"); sibling->parent->children->items[index + offs] = container; sibling->parent->children->items[index] = sibling; - arrange_children_of(sibling->parent); } else { wlr_log(L_DEBUG, "Promoting to sibling of cousin"); container_insert_child(sibling->parent, container, index_child(sibling) + (offs > 0 ? 0 : 1)); container->width = container->height = 0; - arrange_children_of(sibling->parent); - arrange_children_of(old_parent); } sibling = NULL; break; @@ -513,8 +488,6 @@ void container_move(struct sway_container *container, "(move dir: %d)", limit, move_dir); container_insert_child(sibling, container, limit); container->width = container->height = 0; - arrange_children_of(sibling); - arrange_children_of(old_parent); sibling = NULL; } else { wlr_log(L_DEBUG, "Reparenting container (perpendicular)"); @@ -538,8 +511,6 @@ void container_move(struct sway_container *container, container_add_child(sibling, container); } container->width = container->height = 0; - arrange_children_of(sibling); - arrange_children_of(old_parent); sibling = NULL; } break; @@ -864,7 +835,6 @@ struct sway_container *container_split(struct sway_container *child, // Special case: this just behaves like splitt child->prev_layout = child->layout; child->layout = layout; - arrange_children_of(child); return child; } @@ -882,7 +852,7 @@ struct sway_container *container_split(struct sway_container *child, struct sway_seat *seat = input_manager_get_default_seat(input_manager); bool set_focus = (seat_get_focus(seat) == child); - + add_gaps(cont); if (child->type == C_WORKSPACE) { @@ -912,7 +882,6 @@ struct sway_container *container_split(struct sway_container *child, } container_notify_subtree_changed(cont); - arrange_children_of(cont); return cont; } @@ -1050,9 +1019,6 @@ void container_swap(struct sway_container *con1, struct sway_container *con2) { prev_workspace_name = stored_prev_name; } - arrange_children_of(con1->parent); - arrange_children_of(con2->parent); - if (fs1 && con2->type == C_VIEW) { view_set_fullscreen(con2->sway_view, true); } diff --git a/sway/tree/output.c b/sway/tree/output.c index ed7e941e..e2927cdb 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -26,12 +26,9 @@ static void restore_workspaces(struct sway_container *output) { j--; } } - - arrange_output(other); } container_sort_workspaces(output); - arrange_output(output); } struct sway_container *output_create( diff --git a/sway/tree/view.c b/sway/tree/view.c index 98637c33..6263bfb0 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -28,20 +28,22 @@ void view_init(struct sway_view *view, enum sway_view_type type, wl_signal_init(&view->events.unmap); } -void view_destroy(struct sway_view *view) { - if (view == NULL) { +void view_free(struct sway_view *view) { + if (!sway_assert(view->surface == NULL, "Tried to free mapped view")) { return; } - - if (view->surface != NULL) { - view_unmap(view); + if (!sway_assert(view->destroying, + "Tried to free view which wasn't marked as destroying")) { + return; + } + if (!sway_assert(view->swayc == NULL, + "Tried to free view which still has a swayc " + "(might have a pending transaction?)")) { + return; } - list_free(view->executed_criteria); - for (int i = 0; i < view->marks->length; ++i) { - free(view->marks->items[i]); - } + list_foreach(view->marks, free); list_free(view->marks); wlr_texture_destroy(view->marks_focused); @@ -49,8 +51,6 @@ void view_destroy(struct sway_view *view) { wlr_texture_destroy(view->marks_unfocused); wlr_texture_destroy(view->marks_urgent); - container_destroy(view->swayc); - if (view->impl->destroy) { view->impl->destroy(view); } else { @@ -58,6 +58,27 @@ void view_destroy(struct sway_view *view) { } } +/** + * The view may or may not be involved in a transaction. For example, a view may + * unmap then attempt to destroy itself before we've applied the new layout. If + * an unmapping view is still involved in a transaction then it'll still have a + * swayc. + * + * If there's no transaction we can simply free the view. Otherwise the + * destroying flag will make the view get freed when the transaction is + * finished. + */ +void view_destroy(struct sway_view *view) { + if (!sway_assert(view->surface == NULL, "Tried to destroy a mapped view")) { + return; + } + view->destroying = true; + + if (!view->swayc) { + view_free(view); + } +} + const char *view_get_title(struct sway_view *view) { if (view->impl->get_string_prop) { return view->impl->get_string_prop(view, VIEW_PROP_TITLE); @@ -119,32 +140,33 @@ const char *view_get_shell(struct sway_view *view) { return "unknown"; } -void view_configure(struct sway_view *view, double lx, double ly, int width, +uint32_t view_configure(struct sway_view *view, double lx, double ly, int width, int height) { if (view->impl->configure) { - view->impl->configure(view, lx, ly, width, height); + return view->impl->configure(view, lx, ly, width, height); } + return 0; } -static void view_autoconfigure_floating(struct sway_view *view) { +void view_init_floating(struct sway_view *view) { struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); int max_width = ws->width * 0.6666; int max_height = ws->height * 0.6666; - int width = + view->width = view->natural_width > max_width ? max_width : view->natural_width; - int height = + view->height = view->natural_height > max_height ? max_height : view->natural_height; - int lx = ws->x + (ws->width - width) / 2; - int ly = ws->y + (ws->height - height) / 2; + view->x = ws->x + (ws->width - view->width) / 2; + view->y = ws->y + (ws->height - view->height) / 2; // If the view's border is B_NONE then these properties are ignored. view->border_top = view->border_bottom = true; view->border_left = view->border_right = true; + container_set_geometry_from_floating_view(view->swayc); + // Don't maximize floating windows view_set_tiled(view, false); - - view_configure(view, lx, ly, width, height); } void view_autoconfigure(struct sway_view *view) { @@ -156,12 +178,14 @@ void view_autoconfigure(struct sway_view *view) { struct sway_container *output = container_parent(view->swayc, C_OUTPUT); if (view->is_fullscreen) { - view_configure(view, output->x, output->y, output->width, output->height); + view->x = output->x; + view->y = output->y; + view->width = output->width; + view->height = output->height; return; } if (container_is_floating(view->swayc)) { - view_autoconfigure_floating(view); return; } @@ -181,20 +205,22 @@ void view_autoconfigure(struct sway_view *view) { } } + struct sway_container *con = view->swayc; + view->border_top = view->border_bottom = true; view->border_left = view->border_right = true; if (config->hide_edge_borders == E_BOTH || config->hide_edge_borders == E_VERTICAL || (config->hide_edge_borders == E_SMART && !other_views)) { - view->border_left = view->swayc->x != ws->x; - int right_x = view->swayc->x + view->swayc->width; + view->border_left = con->x != ws->x; + int right_x = con->x + con->width; view->border_right = right_x != ws->x + ws->width; } if (config->hide_edge_borders == E_BOTH || config->hide_edge_borders == E_HORIZONTAL || (config->hide_edge_borders == E_SMART && !other_views)) { - view->border_top = view->swayc->y != ws->y; - int bottom_y = view->swayc->y + view->swayc->height; + view->border_top = con->y != ws->y; + int bottom_y = con->y + con->height; view->border_bottom = bottom_y != ws->y + ws->height; } @@ -205,45 +231,44 @@ void view_autoconfigure(struct sway_view *view) { // In a tabbed or stacked container, the swayc's y is the top of the title // area. We have to offset the surface y by the height of the title bar, and // disable any top border because we'll always have the title bar. - if (view->swayc->parent->layout == L_TABBED) { + if (con->parent->layout == L_TABBED) { y_offset = container_titlebar_height(); view->border_top = false; - } else if (view->swayc->parent->layout == L_STACKED) { - y_offset = container_titlebar_height() - * view->swayc->parent->children->length; + } else if (con->parent->layout == L_STACKED) { + y_offset = container_titlebar_height() * con->parent->children->length; view->border_top = false; } switch (view->border) { case B_NONE: - x = view->swayc->x; - y = view->swayc->y + y_offset; - width = view->swayc->width; - height = view->swayc->height - y_offset; + x = con->x; + y = con->y + y_offset; + width = con->width; + height = con->height - y_offset; break; case B_PIXEL: - x = view->swayc->x + view->border_thickness * view->border_left; - y = view->swayc->y + view->border_thickness * view->border_top + y_offset; - width = view->swayc->width + x = con->x + view->border_thickness * view->border_left; + y = con->y + view->border_thickness * view->border_top + y_offset; + width = con->width - view->border_thickness * view->border_left - view->border_thickness * view->border_right; - height = view->swayc->height - y_offset + height = con->height - y_offset - view->border_thickness * view->border_top - view->border_thickness * view->border_bottom; break; case B_NORMAL: // Height is: 1px border + 3px pad + title height + 3px pad + 1px border - x = view->swayc->x + view->border_thickness * view->border_left; - width = view->swayc->width + x = con->x + view->border_thickness * view->border_left; + width = con->width - view->border_thickness * view->border_left - view->border_thickness * view->border_right; if (y_offset) { - y = view->swayc->y + y_offset; - height = view->swayc->height - y_offset + y = con->y + y_offset; + height = con->height - y_offset - view->border_thickness * view->border_bottom; } else { - y = view->swayc->y + container_titlebar_height(); - height = view->swayc->height - container_titlebar_height() + y = con->y + container_titlebar_height(); + height = con->height - container_titlebar_height() - view->border_thickness * view->border_bottom; } break; @@ -251,8 +276,9 @@ void view_autoconfigure(struct sway_view *view) { view->x = x; view->y = y; + view->width = width; + view->height = height; view_set_tiled(view, true); - view_configure(view, x, y, width, height); } void view_set_activated(struct sway_view *view, bool activated) { @@ -268,8 +294,7 @@ void view_set_tiled(struct sway_view *view, bool tiled) { } } -// Set fullscreen, but without IPC events or arranging windows. -void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) { +void view_set_fullscreen(struct sway_view *view, bool fullscreen) { if (view->is_fullscreen == fullscreen) { return; } @@ -315,27 +340,17 @@ void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) { } else { workspace->sway_workspace->fullscreen = NULL; if (container_is_floating(view->swayc)) { - view_configure(view, view->saved_x, view->saved_y, - view->saved_width, view->saved_height); + view->x = view->saved_x; + view->y = view->saved_y; + view->width = view->saved_width; + view->height = view->saved_height; + container_set_geometry_from_floating_view(view->swayc); } else { view->swayc->width = view->swayc->saved_width; view->swayc->height = view->swayc->saved_height; - view_autoconfigure(view); } } -} - -void view_set_fullscreen(struct sway_view *view, bool fullscreen) { - if (view->is_fullscreen == fullscreen) { - return; - } - view_set_fullscreen_raw(view, fullscreen); - - struct sway_container *workspace = - container_parent(view->swayc, C_WORKSPACE); - arrange_workspace(workspace); - output_damage_whole(workspace->parent->sway_output); ipc_event_window(view->swayc, "fullscreen_mode"); } @@ -365,6 +380,9 @@ static void view_get_layout_box(struct sway_view *view, struct wlr_box *box) { void view_for_each_surface(struct sway_view *view, wlr_surface_iterator_func_t iterator, void *user_data) { + if (!view->surface) { + return; + } if (view->impl->for_each_surface) { view->impl->for_each_surface(view, iterator, user_data); } else { @@ -518,8 +536,6 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { if (view->impl->wants_floating && view->impl->wants_floating(view)) { container_set_floating(view->swayc, true); - } else { - arrange_children_of(cont->parent); } input_manager_set_focus(input_manager, cont); @@ -531,42 +547,26 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { container_notify_subtree_changed(view->swayc->parent); view_execute_criteria(view); - container_damage_whole(cont); view_handle_container_reparent(&view->container_reparent, NULL); } void view_unmap(struct sway_view *view) { - if (!sway_assert(view->surface != NULL, "cannot unmap unmapped view")) { - return; - } - wl_signal_emit(&view->events.unmap, view); - if (view->is_fullscreen) { - struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); - ws->sway_workspace->fullscreen = NULL; - } - - container_damage_whole(view->swayc); - wl_list_remove(&view->surface_new_subsurface.link); wl_list_remove(&view->container_reparent.link); - struct sway_container *parent = container_destroy(view->swayc); - - view->swayc = NULL; - view->surface = NULL; - - if (view->title_format) { - free(view->title_format); - view->title_format = NULL; - } + if (view->is_fullscreen) { + struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); + ws->sway_workspace->fullscreen = NULL; + container_destroy(view->swayc); - if (parent->type == C_OUTPUT) { - arrange_output(parent); + arrange_and_commit(ws->parent); } else { - arrange_children_of(parent); + struct sway_container *parent = container_destroy(view->swayc); + arrange_and_commit(parent); } + view->surface = NULL; } void view_update_position(struct sway_view *view, double lx, double ly) { @@ -940,7 +940,7 @@ void view_update_marks_textures(struct sway_view *view) { } bool view_is_visible(struct sway_view *view) { - if (!view->swayc) { + if (!view->swayc || view->swayc->destroying) { return false; } struct sway_container *workspace = diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 9ba210fd..5eb4be0f 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -425,11 +425,14 @@ bool workspace_switch(struct sway_container *workspace) { } seat_set_focus(seat, next); struct sway_container *output = container_parent(workspace, C_OUTPUT); - arrange_output(output); + arrange_and_commit(output); return true; } bool workspace_is_visible(struct sway_container *ws) { + if (ws->destroying) { + return false; + } struct sway_container *output = container_parent(ws, C_OUTPUT); struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *focus = seat_get_focus_inactive(seat, output); |