From 59c94887018bdfa578c4371c4275061ca6e71b3e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 3 Jun 2018 16:35:06 +1000 Subject: WIP: Atomic layout updates ground work --- sway/tree/arrange.c | 321 ++++++++++++++++++++++++++++---------------------- sway/tree/container.c | 11 ++ sway/tree/layout.c | 4 +- sway/tree/view.c | 98 ++++++++------- sway/tree/workspace.c | 6 + 5 files changed, 252 insertions(+), 188 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index 721b557e..d8b3aec1 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -5,7 +5,6 @@ #include #include #include -#include "sway/debug.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/layout.h" @@ -17,116 +16,56 @@ 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); - workspace->width = area->width; - workspace->height = area->height; - workspace->x = output->x + area->x; - workspace->y = output->y + area->y; - wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", - workspace->name, workspace->x, workspace->y); - arrange_children_of(workspace); - container_damage_whole(workspace); -} - static void apply_horiz_layout(struct sway_container *parent) { size_t num_children = parent->children->length; if (!num_children) { return; } size_t parent_offset = 0; - if (parent->parent->layout == L_TABBED) { + if (parent->parent->pending.layout == L_TABBED) { parent_offset = container_titlebar_height(); - } else if (parent->parent->layout == L_STACKED) { - parent_offset = - container_titlebar_height() * parent->parent->children->length; + } else if (parent->parent->pending.layout == L_STACKED) { + parent_offset = container_titlebar_height() * + parent->parent->children->length; } - size_t parent_height = parent->height - parent_offset; + size_t parent_height = parent->pending.swayc_height - parent_offset; // Calculate total width of children 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 (child->pending.swayc_width <= 0) { if (num_children > 1) { - child->width = parent->width / (num_children - 1); + child->pending.swayc_width = + parent->pending.swayc_width / (num_children - 1); } else { - child->width = parent->width; + child->pending.swayc_width = parent->pending.swayc_width; } } - total_width += child->width; + total_width += child->pending.swayc_width; } - double scale = parent->width / total_width; + double scale = parent->pending.swayc_width / total_width; // Resize windows wlr_log(L_DEBUG, "Arranging %p horizontally", parent); - double child_x = parent->x; - struct sway_container *child; + double child_x = parent->pending.swayc_x; 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; + child, child->type, child->pending.swayc_width, scale); + child->pending.swayc_x = child_x; + child->pending.swayc_y = parent->pending.swayc_y + parent_offset; + child->pending.swayc_width = floor(child->pending.swayc_width * scale); + child->pending.swayc_height = parent_height; + child_x += child->pending.swayc_width; + + // Make last child use remaining width of parent + if (i == num_children - 1) { + child->pending.swayc_width = parent->pending.swayc_x + + parent->pending.swayc_width - child->pending.swayc_x; + } } - // Make last child use remaining width of parent - child->width = parent->x + parent->width - child->x; } static void apply_vert_layout(struct sway_container *parent) { @@ -135,46 +74,51 @@ static void apply_vert_layout(struct sway_container *parent) { return; } size_t parent_offset = 0; - if (parent->parent->layout == L_TABBED) { + if (parent->parent->pending.layout == L_TABBED) { parent_offset = container_titlebar_height(); - } else if (parent->parent->layout == L_STACKED) { + } else if (parent->parent->pending.layout == L_STACKED) { parent_offset = container_titlebar_height() * parent->parent->children->length; } - size_t parent_height = parent->height - parent_offset; + size_t parent_height = parent->pending.swayc_height - parent_offset; // Calculate total height of children 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 (child->pending.swayc_height <= 0) { if (num_children > 1) { - child->height = parent_height / (num_children - 1); + child->pending.swayc_height = + parent_height / (num_children - 1); } else { - child->height = parent_height; + child->pending.swayc_height = parent_height; } } - total_height += child->height; + total_height += child->pending.swayc_height; } double scale = parent_height / total_height; // Resize wlr_log(L_DEBUG, "Arranging %p vertically", parent); - double child_y = parent->y + parent_offset; - struct sway_container *child; + double child_y = parent->pending.swayc_y + parent_offset; 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; + child, child->type, child->pending.swayc_height, scale); + child->pending.swayc_x = parent->pending.swayc_x; + child->pending.swayc_y = child_y; + child->pending.swayc_width = parent->pending.swayc_width; + child->pending.swayc_height = + floor(child->pending.swayc_height * scale); + child_y += child->pending.swayc_height; + + // Make last child use remaining height of parent + if (i == num_children - 1) { + child->pending.swayc_height = parent->pending.swayc_y + + parent_offset + parent_height - child->pending.swayc_y; + } } - // Make last child use remaining height of parent - child->height = parent->y + parent_offset + parent_height - child->y; } static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { @@ -182,46 +126,33 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { return; } size_t parent_offset = 0; - if (parent->parent->layout == L_TABBED) { + if (parent->parent->pending.layout == L_TABBED) { parent_offset = container_titlebar_height(); - } else if (parent->parent->layout == L_STACKED) { + } else if (parent->parent->pending.layout == L_STACKED) { parent_offset = container_titlebar_height() * parent->parent->children->length; } - size_t parent_height = parent->height - parent_offset; + size_t parent_height = parent->pending.swayc_height - parent_offset; for (int i = 0; i < parent->children->length; ++i) { struct sway_container *child = parent->children->items[i]; - child->x = parent->x; - child->y = parent->y + parent_offset; - child->width = parent->width; - child->height = parent_height; + child->pending.swayc_x = parent->pending.swayc_x; + child->pending.swayc_y = parent->pending.swayc_y + parent_offset; + child->pending.swayc_width = parent->pending.swayc_width; + child->pending.swayc_height = parent_height; } } -void arrange_children_of(struct sway_container *parent) { +static void _arrange_children_of(struct sway_container *parent, + struct sway_transaction *transaction) { 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; - } - - struct sway_container *workspace = parent; - if (workspace->type != C_WORKSPACE) { - workspace = container_parent(workspace, C_WORKSPACE); - } - if (workspace->sway_workspace->fullscreen) { - // Just arrange the fullscreen view and jump out - view_autoconfigure(workspace->sway_workspace->fullscreen); - 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); + parent->name, parent->pending.swayc_width, parent->pending.swayc_height, + parent->pending.swayc_x, parent->pending.swayc_y); // Calculate x, y, width and height of children - switch (parent->layout) { + switch (parent->pending.layout) { case L_HORIZ: apply_horiz_layout(parent); break; @@ -232,33 +163,135 @@ 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: + sway_assert(false, "Didn't expect to see floating here"); } - // 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 (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); + workspace->pending.swayc_width = area->width; + workspace->pending.swayc_height = area->height; + workspace->pending.swayc_x = output->x + area->x; + workspace->pending.swayc_y = output->y + area->y; + transaction_add_container(transaction, workspace); + wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, + workspace->pending.swayc_x, workspace->pending.swayc_y); + _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; + output->pending.swayc_x = output_box->x; + output->pending.swayc_y = output_box->y; + output->pending.swayc_width = output_box->width; + output->pending.swayc_height = output_box->height; + transaction_add_container(transaction, output); + wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", + output->name, output->pending.swayc_x, output->pending.swayc_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; + root_container.pending.swayc_x = layout_box->x; + root_container.pending.swayc_y = layout_box->y; + root_container.pending.swayc_width = layout_box->width; + root_container.pending.swayc_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: + break; + case C_TYPES: + break; + } + transaction_add_damage(transaction, container_get_box(container)); +} + +void arrange_and_commit(struct sway_container *container) { + struct sway_transaction *transaction = transaction_create(); + arrange_windows(container, transaction); + transaction_commit(transaction); +} + +// These functions are only temporary +void arrange_root() { + arrange_and_commit(&root_container); +} + +void arrange_output(struct sway_container *container) { + arrange_and_commit(container); +} + +void arrange_workspace(struct sway_container *container) { + arrange_and_commit(container); +} - container_damage_whole(parent); - update_debug_tree(); +void arrange_children_of(struct sway_container *container) { + arrange_and_commit(container); } diff --git a/sway/tree/container.c b/sway/tree/container.c index cd2c083c..e6956f5c 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -116,6 +116,7 @@ struct sway_container *container_create(enum sway_container_type type) { if (type != C_VIEW) { c->children = create_list(); + //c->pending.children = create_list(); } wl_signal_init(&c->events.destroy); @@ -162,6 +163,7 @@ static void _container_destroy(struct sway_container *cont) { wlr_texture_destroy(cont->title_urgent); list_free(cont->children); + //list_free(cont->pending.children); cont->children = NULL; free(cont); } @@ -971,3 +973,12 @@ bool container_is_floating(struct sway_container *container) { } return container->parent == workspace->sway_workspace->floating; } + +struct wlr_box *container_get_box(struct sway_container *container) { + struct wlr_box *box = calloc(1, sizeof(struct wlr_box)); + box->x = container->x; + box->y = container->y; + box->width = container->width; + box->height = container->height; + return box; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 6d4cd088..3bba049a 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -915,10 +915,10 @@ void container_recursive_resize(struct sway_container *container, bool layout_match = true; wlr_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); if (edge == RESIZE_EDGE_LEFT || edge == RESIZE_EDGE_RIGHT) { - container->width += amount; + container->pending.swayc_width += amount; layout_match = container->layout == L_HORIZ; } else if (edge == RESIZE_EDGE_TOP || edge == RESIZE_EDGE_BOTTOM) { - container->height += amount; + container->pending.swayc_height += amount; layout_match = container->layout == L_VERT; } if (container->children) { diff --git a/sway/tree/view.c b/sway/tree/view.c index c9c82405..40fe2740 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -25,6 +25,7 @@ void view_init(struct sway_view *view, enum sway_view_type type, view->impl = impl; view->executed_criteria = create_list(); view->marks = create_list(); + view->instructions = create_list(); wl_signal_init(&view->events.unmap); } @@ -37,6 +38,11 @@ void view_destroy(struct sway_view *view) { view_unmap(view); } + if (!sway_assert(view->instructions->length == 0, + "Tried to destroy view with pending instructions")) { + return; + } + list_free(view->executed_criteria); for (int i = 0; i < view->marks->length; ++i) { @@ -44,6 +50,8 @@ void view_destroy(struct sway_view *view) { } list_free(view->marks); + list_free(view->instructions); + wlr_texture_destroy(view->marks_focused); wlr_texture_destroy(view->marks_focused_inactive); wlr_texture_destroy(view->marks_unfocused); @@ -119,11 +127,12 @@ 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) { @@ -178,21 +187,23 @@ void view_autoconfigure(struct sway_view *view) { } } - view->border_top = view->border_bottom = true; - view->border_left = view->border_right = true; + struct sway_container_state *state = &view->swayc->pending; + + state->border_top = state->border_bottom = true; + state->border_left = state->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_right = right_x != ws->x + ws->width; + state->border_left = state->swayc_x != ws->x; + int right_x = state->swayc_x + state->swayc_width; + state->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_bottom = bottom_y != ws->y + ws->height; + state->border_top = state->swayc_y != ws->y; + int bottom_y = state->swayc_y + state->swayc_height; + state->border_bottom = bottom_y != ws->y + ws->height; } double x, y, width, height; @@ -202,53 +213,54 @@ 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 (view->swayc->parent->pending.layout == L_TABBED) { y_offset = container_titlebar_height(); - view->border_top = false; - } else if (view->swayc->parent->layout == L_STACKED) { + state->border_top = false; + } else if (view->swayc->parent->pending.layout == L_STACKED) { y_offset = container_titlebar_height() * view->swayc->parent->children->length; view->border_top = false; } - switch (view->border) { + switch (state->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 = state->swayc_x; + y = state->swayc_y + y_offset; + width = state->swayc_width; + height = state->swayc_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 - - view->border_thickness * view->border_left - - view->border_thickness * view->border_right; - height = view->swayc->height - y_offset - - view->border_thickness * view->border_top - - view->border_thickness * view->border_bottom; + x = state->swayc_x + state->border_thickness * state->border_left; + y = state->swayc_y + state->border_thickness * state->border_top + y_offset; + width = state->swayc_width + - state->border_thickness * state->border_left + - state->border_thickness * state->border_right; + height = state->swayc_height - y_offset + - state->border_thickness * state->border_top + - state->border_thickness * state->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 - - view->border_thickness * view->border_left - - view->border_thickness * view->border_right; + x = state->swayc_x + state->border_thickness * state->border_left; + width = state->swayc_width + - state->border_thickness * state->border_left + - state->border_thickness * state->border_right; if (y_offset) { - y = view->swayc->y + y_offset; - height = view->swayc->height - y_offset - - view->border_thickness * view->border_bottom; + y = state->swayc_y + y_offset; + height = state->swayc_height - y_offset + - state->border_thickness * state->border_bottom; } else { - y = view->swayc->y + container_titlebar_height(); - height = view->swayc->height - container_titlebar_height() - - view->border_thickness * view->border_bottom; + y = state->swayc_y + container_titlebar_height(); + height = state->swayc_height - container_titlebar_height() + - state->border_thickness * state->border_bottom; } break; } - view->x = x; - view->y = y; - view_configure(view, x, y, width, height); + state->view_x = x; + state->view_y = y; + state->view_width = width; + state->view_height = height; } void view_set_activated(struct sway_view *view, bool activated) { @@ -307,8 +319,8 @@ void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) { view_configure(view, view->saved_x, view->saved_y, view->saved_width, view->saved_height); } else { - view->swayc->width = view->swayc->saved_width; - view->swayc->height = view->swayc->saved_height; + view->swayc->width = view->swayc->saved_width; + view->swayc->height = view->swayc->saved_height; view_autoconfigure(view); } } @@ -496,6 +508,8 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { view->swayc = cont; view->border = config->border; view->border_thickness = config->border_thickness; + view->swayc->pending.border = config->border; + view->swayc->pending.border_thickness = config->border_thickness; view_init_subsurfaces(view, wlr_surface); wl_signal_add(&wlr_surface->events.new_subsurface, @@ -963,7 +977,7 @@ bool view_is_visible(struct sway_view *view) { } // Check the workspace is visible if (!is_sticky) { - return workspace_is_visible(workspace); + return workspace_is_visible(workspace); } return true; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 9ba210fd..ad581a96 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -60,6 +60,12 @@ struct sway_container *workspace_create(struct sway_container *output, workspace->prev_layout = L_NONE; workspace->layout = container_get_default_layout(output); + workspace->pending.swayc_x = workspace->x; + workspace->pending.swayc_y = workspace->y; + workspace->pending.swayc_width = workspace->width; + workspace->pending.swayc_height = workspace->height; + workspace->pending.layout = workspace->layout; + struct sway_workspace *swayws = calloc(1, sizeof(struct sway_workspace)); if (!swayws) { return NULL; -- cgit v1.2.3 From f9e6d703d298dbdee0770fd9e0c64ab2d7ac7deb Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 6 Jun 2018 19:19:30 +1000 Subject: Make main properties be the pending state --- sway/tree/arrange.c | 107 ++++++++++++++++++++++---------------------------- sway/tree/layout.c | 4 +- sway/tree/view.c | 90 ++++++++++++++++++++---------------------- sway/tree/workspace.c | 6 --- 4 files changed, 92 insertions(+), 115 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index d8b3aec1..cf7ce61c 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -22,48 +22,46 @@ static void apply_horiz_layout(struct sway_container *parent) { return; } size_t parent_offset = 0; - if (parent->parent->pending.layout == L_TABBED) { + if (parent->parent->layout == L_TABBED) { parent_offset = container_titlebar_height(); - } else if (parent->parent->pending.layout == L_STACKED) { + } else if (parent->parent->layout == L_STACKED) { parent_offset = container_titlebar_height() * parent->parent->children->length; } - size_t parent_height = parent->pending.swayc_height - parent_offset; + size_t parent_height = parent->height - parent_offset; // Calculate total width of children double total_width = 0; for (size_t i = 0; i < num_children; ++i) { struct sway_container *child = parent->children->items[i]; - if (child->pending.swayc_width <= 0) { + if (child->width <= 0) { if (num_children > 1) { - child->pending.swayc_width = - parent->pending.swayc_width / (num_children - 1); + child->width = parent->width / (num_children - 1); } else { - child->pending.swayc_width = parent->pending.swayc_width; + child->width = parent->width; } } - total_width += child->pending.swayc_width; + total_width += child->width; } - double scale = parent->pending.swayc_width / total_width; + double scale = parent->width / total_width; // Resize windows wlr_log(L_DEBUG, "Arranging %p horizontally", parent); - double child_x = parent->pending.swayc_x; + double child_x = parent->x; for (size_t i = 0; i < num_children; ++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->pending.swayc_width, scale); - child->pending.swayc_x = child_x; - child->pending.swayc_y = parent->pending.swayc_y + parent_offset; - child->pending.swayc_width = floor(child->pending.swayc_width * scale); - child->pending.swayc_height = parent_height; - child_x += child->pending.swayc_width; + 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) { - child->pending.swayc_width = parent->pending.swayc_x + - parent->pending.swayc_width - child->pending.swayc_x; + child->width = parent->x + parent->width - child->x; } } } @@ -74,49 +72,47 @@ static void apply_vert_layout(struct sway_container *parent) { return; } size_t parent_offset = 0; - if (parent->parent->pending.layout == L_TABBED) { + if (parent->parent->layout == L_TABBED) { parent_offset = container_titlebar_height(); - } else if (parent->parent->pending.layout == L_STACKED) { + } else if (parent->parent->layout == L_STACKED) { parent_offset = container_titlebar_height() * parent->parent->children->length; } - size_t parent_height = parent->pending.swayc_height - parent_offset; + size_t parent_height = parent->height - parent_offset; // Calculate total height of children double total_height = 0; for (size_t i = 0; i < num_children; ++i) { struct sway_container *child = parent->children->items[i]; - if (child->pending.swayc_height <= 0) { + if (child->height <= 0) { if (num_children > 1) { - child->pending.swayc_height = - parent_height / (num_children - 1); + child->height = parent_height / (num_children - 1); } else { - child->pending.swayc_height = parent_height; + child->height = parent_height; } } - total_height += child->pending.swayc_height; + total_height += child->height; } double scale = parent_height / total_height; // Resize wlr_log(L_DEBUG, "Arranging %p vertically", parent); - double child_y = parent->pending.swayc_y + parent_offset; + double child_y = parent->y + parent_offset; for (size_t i = 0; i < num_children; ++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->pending.swayc_height, scale); - child->pending.swayc_x = parent->pending.swayc_x; - child->pending.swayc_y = child_y; - child->pending.swayc_width = parent->pending.swayc_width; - child->pending.swayc_height = - floor(child->pending.swayc_height * scale); - child_y += child->pending.swayc_height; + 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) { - child->pending.swayc_height = parent->pending.swayc_y + - parent_offset + parent_height - child->pending.swayc_y; + child->height = + parent->y + parent_offset + parent_height - child->y; } } } @@ -126,19 +122,19 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { return; } size_t parent_offset = 0; - if (parent->parent->pending.layout == L_TABBED) { + if (parent->parent->layout == L_TABBED) { parent_offset = container_titlebar_height(); - } else if (parent->parent->pending.layout == L_STACKED) { + } else if (parent->parent->layout == L_STACKED) { parent_offset = container_titlebar_height() * parent->parent->children->length; } - size_t parent_height = parent->pending.swayc_height - parent_offset; + size_t parent_height = parent->height - parent_offset; for (int i = 0; i < parent->children->length; ++i) { struct sway_container *child = parent->children->items[i]; - child->pending.swayc_x = parent->pending.swayc_x; - child->pending.swayc_y = parent->pending.swayc_y + parent_offset; - child->pending.swayc_width = parent->pending.swayc_width; - child->pending.swayc_height = parent_height; + child->x = parent->x; + child->y = parent->y + parent_offset; + child->width = parent->width; + child->height = parent_height; } } @@ -148,11 +144,10 @@ static void _arrange_children_of(struct sway_container *parent, return; } wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent, - parent->name, parent->pending.swayc_width, parent->pending.swayc_height, - parent->pending.swayc_x, parent->pending.swayc_y); + parent->name, parent->width, parent->height, parent->x, parent->y); // Calculate x, y, width and height of children - switch (parent->pending.layout) { + switch (parent->layout) { case L_HORIZ: apply_horiz_layout(parent); break; @@ -191,13 +186,13 @@ static void _arrange_workspace(struct sway_container *workspace, 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); - workspace->pending.swayc_width = area->width; - workspace->pending.swayc_height = area->height; - workspace->pending.swayc_x = output->x + area->x; - workspace->pending.swayc_y = output->y + area->y; + workspace->width = area->width; + workspace->height = area->height; + workspace->x = output->x + area->x; + workspace->y = output->y + area->y; transaction_add_container(transaction, workspace); wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, - workspace->pending.swayc_x, workspace->pending.swayc_y); + workspace->x, workspace->y); _arrange_children_of(workspace, transaction); } @@ -213,13 +208,9 @@ static void _arrange_output(struct sway_container *output, output->y = output_box->y; output->width = output_box->width; output->height = output_box->height; - output->pending.swayc_x = output_box->x; - output->pending.swayc_y = output_box->y; - output->pending.swayc_width = output_box->width; - output->pending.swayc_height = output_box->height; transaction_add_container(transaction, output); wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", - output->name, output->pending.swayc_x, output->pending.swayc_y); + 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); @@ -238,10 +229,6 @@ static void _arrange_root(struct sway_transaction *transaction) { root_container.y = layout_box->y; root_container.width = layout_box->width; root_container.height = layout_box->height; - root_container.pending.swayc_x = layout_box->x; - root_container.pending.swayc_y = layout_box->y; - root_container.pending.swayc_width = layout_box->width; - root_container.pending.swayc_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]; diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 3bba049a..6d4cd088 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -915,10 +915,10 @@ void container_recursive_resize(struct sway_container *container, bool layout_match = true; wlr_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); if (edge == RESIZE_EDGE_LEFT || edge == RESIZE_EDGE_RIGHT) { - container->pending.swayc_width += amount; + container->width += amount; layout_match = container->layout == L_HORIZ; } else if (edge == RESIZE_EDGE_TOP || edge == RESIZE_EDGE_BOTTOM) { - container->pending.swayc_height += amount; + container->height += amount; layout_match = container->layout == L_VERT; } if (container->children) { diff --git a/sway/tree/view.c b/sway/tree/view.c index 40fe2740..dbf803c6 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -187,23 +187,23 @@ void view_autoconfigure(struct sway_view *view) { } } - struct sway_container_state *state = &view->swayc->pending; + struct sway_container *con = view->swayc; - state->border_top = state->border_bottom = true; - state->border_left = state->border_right = true; + 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)) { - state->border_left = state->swayc_x != ws->x; - int right_x = state->swayc_x + state->swayc_width; - state->border_right = right_x != ws->x + ws->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)) { - state->border_top = state->swayc_y != ws->y; - int bottom_y = state->swayc_y + state->swayc_height; - state->border_bottom = bottom_y != ws->y + ws->height; + view->border_top = con->y != ws->y; + int bottom_y = con->y + con->height; + view->border_bottom = bottom_y != ws->y + ws->height; } double x, y, width, height; @@ -213,54 +213,53 @@ 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->pending.layout == L_TABBED) { + if (con->parent->layout == L_TABBED) { y_offset = container_titlebar_height(); - state->border_top = false; - } else if (view->swayc->parent->pending.layout == L_STACKED) { - y_offset = container_titlebar_height() - * view->swayc->parent->children->length; + view->border_top = false; + } else if (con->parent->layout == L_STACKED) { + y_offset = container_titlebar_height() * con->parent->children->length; view->border_top = false; } - switch (state->border) { + switch (view->border) { case B_NONE: - x = state->swayc_x; - y = state->swayc_y + y_offset; - width = state->swayc_width; - height = state->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 = state->swayc_x + state->border_thickness * state->border_left; - y = state->swayc_y + state->border_thickness * state->border_top + y_offset; - width = state->swayc_width - - state->border_thickness * state->border_left - - state->border_thickness * state->border_right; - height = state->swayc_height - y_offset - - state->border_thickness * state->border_top - - state->border_thickness * state->border_bottom; + 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 = 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 = state->swayc_x + state->border_thickness * state->border_left; - width = state->swayc_width - - state->border_thickness * state->border_left - - state->border_thickness * state->border_right; + 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 = state->swayc_y + y_offset; - height = state->swayc_height - y_offset - - state->border_thickness * state->border_bottom; + y = con->y + y_offset; + height = con->height - y_offset + - view->border_thickness * view->border_bottom; } else { - y = state->swayc_y + container_titlebar_height(); - height = state->swayc_height - container_titlebar_height() - - state->border_thickness * state->border_bottom; + y = con->y + container_titlebar_height(); + height = con->height - container_titlebar_height() + - view->border_thickness * view->border_bottom; } break; } - state->view_x = x; - state->view_y = y; - state->view_width = width; - state->view_height = height; + view->x = x; + view->y = y; + view->width = width; + view->height = height; } void view_set_activated(struct sway_view *view, bool activated) { @@ -319,9 +318,8 @@ void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) { view_configure(view, view->saved_x, view->saved_y, view->saved_width, view->saved_height); } else { - view->swayc->width = view->swayc->saved_width; - view->swayc->height = view->swayc->saved_height; - view_autoconfigure(view); + view->swayc->width = view->swayc->saved_width; + view->swayc->height = view->swayc->saved_height; } } } @@ -508,8 +506,6 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { view->swayc = cont; view->border = config->border; view->border_thickness = config->border_thickness; - view->swayc->pending.border = config->border; - view->swayc->pending.border_thickness = config->border_thickness; view_init_subsurfaces(view, wlr_surface); wl_signal_add(&wlr_surface->events.new_subsurface, @@ -977,7 +973,7 @@ bool view_is_visible(struct sway_view *view) { } // Check the workspace is visible if (!is_sticky) { - return workspace_is_visible(workspace); + return workspace_is_visible(workspace); } return true; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index ad581a96..9ba210fd 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -60,12 +60,6 @@ struct sway_container *workspace_create(struct sway_container *output, workspace->prev_layout = L_NONE; workspace->layout = container_get_default_layout(output); - workspace->pending.swayc_x = workspace->x; - workspace->pending.swayc_y = workspace->y; - workspace->pending.swayc_width = workspace->width; - workspace->pending.swayc_height = workspace->height; - workspace->pending.layout = workspace->layout; - struct sway_workspace *swayws = calloc(1, sizeof(struct sway_workspace)); if (!swayws) { return NULL; -- cgit v1.2.3 From bb66e6d578fdc68fb33d0fde921390d74f20bb31 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 6 Jun 2018 22:57:34 +1000 Subject: Refactor everything that needs to arrange windows * The arrange_foo functions are now replaced with arrange_and_commit, or with manually created transactions and arrange_windows x2. * The arrange functions are now only called from the highest level functions rather than from both high level and low level functions. * Due to the previous point, view_set_fullscreen_raw and view_set_fullscreen are both merged into one function again. * Floating and fullscreen are now working with transactions. --- sway/tree/arrange.c | 63 +++++++++++++++++++++++---------------------- sway/tree/container.c | 10 ++++---- sway/tree/layout.c | 71 +++++++++++++-------------------------------------- sway/tree/view.c | 49 ++++++++++++----------------------- sway/tree/workspace.c | 2 +- 5 files changed, 74 insertions(+), 121 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index cf7ce61c..e138410d 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -138,7 +138,23 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { } } -static void _arrange_children_of(struct sway_container *parent, +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); + } +} + +static void arrange_children_of(struct sway_container *parent, struct sway_transaction *transaction) { if (config->reloading) { return; @@ -162,7 +178,8 @@ static void _arrange_children_of(struct sway_container *parent, apply_horiz_layout(parent); break; case L_FLOATING: - sway_assert(false, "Didn't expect to see floating here"); + arrange_floating(parent, transaction); + break; } // Recurse into child containers @@ -171,13 +188,13 @@ static void _arrange_children_of(struct sway_container *parent, if (child->type == C_VIEW) { view_autoconfigure(child->sway_view); } else { - _arrange_children_of(child, transaction); + arrange_children_of(child, transaction); } transaction_add_container(transaction, child); } } -static void _arrange_workspace(struct sway_container *workspace, +static void arrange_workspace(struct sway_container *workspace, struct sway_transaction *transaction) { if (config->reloading) { return; @@ -193,10 +210,11 @@ static void _arrange_workspace(struct sway_container *workspace, transaction_add_container(transaction, workspace); wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name, workspace->x, workspace->y); - _arrange_children_of(workspace, transaction); + arrange_floating(workspace->sway_workspace->floating, transaction); + arrange_children_of(workspace, transaction); } -static void _arrange_output(struct sway_container *output, +static void arrange_output(struct sway_container *output, struct sway_transaction *transaction) { if (config->reloading) { return; @@ -213,11 +231,11 @@ static void _arrange_output(struct sway_container *output, 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); + arrange_workspace(workspace, transaction); } } -static void _arrange_root(struct sway_transaction *transaction) { +static void arrange_root(struct sway_transaction *transaction) { if (config->reloading) { return; } @@ -232,7 +250,7 @@ static void _arrange_root(struct sway_transaction *transaction) { 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); + arrange_output(output, transaction); } } @@ -240,19 +258,21 @@ void arrange_windows(struct sway_container *container, struct sway_transaction *transaction) { switch (container->type) { case C_ROOT: - _arrange_root(transaction); + arrange_root(transaction); break; case C_OUTPUT: - _arrange_output(container, transaction); + arrange_output(container, transaction); break; case C_WORKSPACE: - _arrange_workspace(container, transaction); + arrange_workspace(container, transaction); break; case C_CONTAINER: - _arrange_children_of(container, transaction); + 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; @@ -265,20 +285,3 @@ void arrange_and_commit(struct sway_container *container) { arrange_windows(container, transaction); transaction_commit(transaction); } - -// These functions are only temporary -void arrange_root() { - arrange_and_commit(&root_container); -} - -void arrange_output(struct sway_container *container) { - arrange_and_commit(container); -} - -void arrange_workspace(struct sway_container *container) { - arrange_and_commit(container); -} - -void arrange_children_of(struct sway_container *container) { - arrange_and_commit(container); -} diff --git a/sway/tree/container.c b/sway/tree/container.c index e6956f5c..d312eb60 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -204,6 +204,7 @@ static struct sway_container *container_workspace_destroy( container_move_to(floating->children->items[i], new_workspace->sway_workspace->floating); } + arrange_and_commit(new_workspace); } struct sway_workspace *sway_workspace = workspace->sway_workspace; @@ -264,10 +265,10 @@ static struct sway_container *container_output_destroy( } container_sort_workspaces(new_output); - arrange_output(new_output); } } } + arrange_and_commit(&root_container); wl_list_remove(&output->sway_output->mode.link); wl_list_remove(&output->sway_output->transform.link); @@ -924,13 +925,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); @@ -943,8 +943,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) { diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 6d4cd088..65b61495 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) { @@ -56,18 +56,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; } @@ -78,15 +77,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, @@ -188,18 +191,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) { @@ -214,7 +206,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); } @@ -308,7 +301,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, @@ -319,11 +311,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; } @@ -339,11 +326,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); } @@ -367,10 +349,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; } @@ -452,12 +431,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 { @@ -491,14 +467,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; @@ -512,8 +485,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)"); @@ -537,8 +508,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; @@ -863,7 +832,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; } @@ -1044,9 +1012,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/view.c b/sway/tree/view.c index dbf803c6..658a94e8 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -135,22 +135,22 @@ uint32_t view_configure(struct sway_view *view, double lx, double ly, int width, 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; - view_configure(view, lx, ly, width, height); + container_set_geometry_from_floating_view(view->swayc); } void view_autoconfigure(struct sway_view *view) { @@ -162,12 +162,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; } @@ -268,8 +270,7 @@ void view_set_activated(struct sway_view *view, bool activated) { } } -// 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,26 +316,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; } } -} - -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"); } @@ -517,8 +509,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); @@ -530,7 +520,6 @@ 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); } @@ -561,11 +550,7 @@ void view_unmap(struct sway_view *view) { view->title_format = NULL; } - if (parent->type == C_OUTPUT) { - arrange_output(parent); - } else { - arrange_children_of(parent); - } + arrange_and_commit(parent); } void view_update_position(struct sway_view *view, double lx, double ly) { diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 9ba210fd..ead752ad 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -425,7 +425,7 @@ 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; } -- cgit v1.2.3 From 1c89f32533534f6e78c81c95578f40df45bb9016 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Mon, 18 Jun 2018 20:42:12 +1000 Subject: Preserve buffers during transactions * Also fix parts of the rendering where it was rendering the pending state instead of current. --- sway/tree/container.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index f8620b72..b071f394 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -766,9 +766,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; -- cgit v1.2.3 From 38398e2d77d57dc06b67ec88a54091c897915602 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 23 Jun 2018 16:24:11 +1000 Subject: Implement atomic layout updates for tree operations This implements atomic layout updates for when views map, reparent or unmap. --- sway/tree/arrange.c | 25 ++++++- sway/tree/container.c | 184 +++++++++++++++++++++++++------------------------- sway/tree/layout.c | 2 + sway/tree/output.c | 2 - sway/tree/view.c | 76 ++++++++++----------- sway/tree/workspace.c | 3 + 6 files changed, 160 insertions(+), 132 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index ac99c5df..cb3f8ba2 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -144,6 +144,19 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) { } } +/** + * 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); + } +} + static void arrange_children_of(struct sway_container *parent, struct sway_transaction *transaction); @@ -158,6 +171,7 @@ static void arrange_floating(struct sway_container *floating, } transaction_add_container(transaction, floater); } + transaction_add_container(transaction, floating); } static void arrange_children_of(struct sway_container *parent, @@ -290,7 +304,16 @@ void arrange_windows(struct sway_container *container, case C_TYPES: break; } - transaction_add_damage(transaction, container_get_box(container)); + // Add damage for whatever container arrange_windows() was called with, + // unless it was called with the special floating container, in which case + // we'll damage the entire output. + if (container->type == C_CONTAINER && container->layout == L_FLOATING) { + struct sway_container *output = container_parent(container, C_OUTPUT); + transaction_add_damage(transaction, container_get_box(output)); + } else { + transaction_add_damage(transaction, container_get_box(container)); + } + add_deleted_containers(transaction); } void arrange_and_commit(struct sway_container *container) { diff --git a/sway/tree/container.c b/sway/tree/container.c index b071f394..484d26a5 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,10 +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->pending.children = create_list(); + c->current.children = create_list(); } wl_signal_init(&c->events.destroy); @@ -133,43 +133,68 @@ 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); - //list_free(cont->pending.children); - cont->children = NULL; + list_free(cont->current.children); + + switch (cont->type) { + case C_ROOT: + break; + case C_OUTPUT: + cont->sway_output->swayc = NULL; + 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); } @@ -186,7 +211,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,25 +233,6 @@ static struct sway_container *container_workspace_destroy( container_move_to(floating->children->items[i], new_workspace->sway_workspace->floating); } - arrange_and_commit(new_workspace); - } - - 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; @@ -266,14 +271,13 @@ 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_and_commit(&root_container); wl_list_remove(&output->sway_output->mode.link); wl_list_remove(&output->sway_output->transform.link); @@ -285,12 +289,8 @@ static struct sway_container *container_output_destroy( output->sway_output->swayc = 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"); + return &root_container; } bool container_reap_empty(struct sway_container *con) { @@ -306,13 +306,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(con); return true; } break; case C_CONTAINER: if (con->children->length == 0) { - _container_destroy(con); + container_destroy(con); return true; } case C_VIEW: @@ -349,46 +349,48 @@ struct sway_container *container_flatten(struct sway_container *container) { 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(). + */ struct sway_container *container_destroy(struct sway_container *con) { if (con == NULL) { return NULL; } + if (con->destroying) { + return NULL; + } - struct sway_container *parent = con->parent; + // 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)) { + return NULL; + } + } - 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; + // At this point the container being destroyed shouldn't have any children + // unless sway is terminating. + if (!server.terminating) { + if (!sway_assert(!con->children || con->children->length == 0, + "Didn't expect to see children here")) { + return NULL; + } } + wl_signal_emit(&con->events.destroy, con); + ipc_event_window(con, "close"); + + struct sway_container *parent = container_remove_child(con); + + con->destroying = true; + list_add(server.destroying_containers, con); + return container_reap_empty_recursive(parent); } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 3724361d..14631ad4 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -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)); diff --git a/sway/tree/output.c b/sway/tree/output.c index 8af319d5..e2927cdb 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -29,7 +29,6 @@ static void restore_workspaces(struct sway_container *output) { } container_sort_workspaces(output); - arrange_and_commit(&root_container); } struct sway_container *output_create( @@ -66,7 +65,6 @@ struct sway_container *output_create( struct sway_container *output = container_create(C_OUTPUT); output->sway_output = sway_output; - sway_output->swayc = output; output->name = strdup(name); if (output->name == NULL) { container_destroy(output); diff --git a/sway/tree/view.c b/sway/tree/view.c index 658a94e8..cb36f123 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -25,47 +25,60 @@ void view_init(struct sway_view *view, enum sway_view_type type, view->impl = impl; view->executed_criteria = create_list(); view->marks = create_list(); - view->instructions = create_list(); 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->instructions->length == 0, - "Tried to destroy view with pending instructions")) { + 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); - list_free(view->instructions); - wlr_texture_destroy(view->marks_focused); wlr_texture_destroy(view->marks_focused_inactive); wlr_texture_destroy(view->marks_unfocused); wlr_texture_destroy(view->marks_urgent); - container_destroy(view->swayc); - - if (view->impl->destroy) { - view->impl->destroy(view); + if (view->impl->free) { + view->impl->free(view); } else { free(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); @@ -356,6 +369,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 { @@ -523,11 +539,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { 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; - } - +struct sway_container *view_unmap(struct sway_view *view) { wl_signal_emit(&view->events.unmap, view); if (view->is_fullscreen) { @@ -535,22 +547,10 @@ void view_unmap(struct sway_view *view) { 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; - } - - arrange_and_commit(parent); + return container_destroy(view->swayc); } void view_update_position(struct sway_view *view, double lx, double ly) { @@ -924,7 +924,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 ead752ad..5eb4be0f 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -430,6 +430,9 @@ bool workspace_switch(struct sway_container *workspace) { } 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); -- cgit v1.2.3 From b864ac0149212adf753824366e20badfa971b29f Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 24 Jun 2018 15:50:53 +1000 Subject: Fix crash when unmapping a view with reapable parents container_destroy was calling container_reap_empty, which calls container_destroy and so on. Eventually the original container_destroy would return a NULL pointer to the caller which caused a crash. This also fixes an arrange on the wrong container when moving views in and out of stacks. --- sway/tree/container.c | 82 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 34 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index 484d26a5..075c508c 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -293,6 +293,47 @@ static struct sway_container *container_output_destroy( return &root_container; } +/** + * Implement the actual destroy logic, without reaping. + */ +struct sway_container *container_destroy_noreaping(struct sway_container *con) { + if (con == NULL) { + return NULL; + } + if (con->destroying) { + return NULL; + } + + // 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; + } + } + + // At this point the container being destroyed shouldn't have any children + // unless sway is terminating. + if (!server.terminating) { + if (!sway_assert(!con->children || con->children->length == 0, + "Didn't expect to see children here")) { + return NULL; + } + } + + wl_signal_emit(&con->events.destroy, con); + ipc_event_window(con, "close"); + + con->destroying = true; + list_add(server.destroying_containers, con); + + return container_remove_child(con); +} + bool container_reap_empty(struct sway_container *con) { if (con->layout == L_FLOATING) { // Don't reap the magical floating container that each workspace has @@ -306,13 +347,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_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: @@ -354,43 +395,16 @@ struct sway_container *container_flatten(struct sway_container *container) { * 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; - } - if (con->destroying) { - return NULL; - } - - // 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)) { - return NULL; - } - } + struct sway_container *parent = container_destroy_noreaping(con); - // At this point the container being destroyed shouldn't have any children - // unless sway is terminating. - if (!server.terminating) { - if (!sway_assert(!con->children || con->children->length == 0, - "Didn't expect to see children here")) { - return NULL; - } + if (!parent) { + return NULL; } - wl_signal_emit(&con->events.destroy, con); - ipc_event_window(con, "close"); - - struct sway_container *parent = container_remove_child(con); - - con->destroying = true; - list_add(server.destroying_containers, con); - return container_reap_empty_recursive(parent); } -- cgit v1.2.3 From a3976e2659ec3a90ba606ca5a93cfa8e78c410e1 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 24 Jun 2018 23:07:52 +1000 Subject: Fix another crash when moving out of stacks or tabs --- sway/tree/container.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index 075c508c..e30c7839 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -331,6 +331,10 @@ struct sway_container *container_destroy_noreaping(struct sway_container *con) { con->destroying = true; list_add(server.destroying_containers, con); + if (!con->parent) { + return NULL; + } + return container_remove_child(con); } @@ -384,7 +388,7 @@ 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; -- cgit v1.2.3 From 9b15e81cff62eb214c89f62bc9e499c7f21d86cf Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Mon, 25 Jun 2018 16:41:31 +1000 Subject: Fix potential crash when fullscreen view unmaps It happened when a view is a grandchild or deeper of the workspace, is fullscreen, and unmaps. The workspace would not be included in the transaction and its pointer to the fullscreen view was left dangling. --- sway/tree/view.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/view.c b/sway/tree/view.c index cb36f123..2ca0dbbb 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -542,14 +542,16 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { struct sway_container *view_unmap(struct sway_view *view) { wl_signal_emit(&view->events.unmap, view); + wl_list_remove(&view->surface_new_subsurface.link); + wl_list_remove(&view->container_reparent.link); + if (view->is_fullscreen) { struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); ws->sway_workspace->fullscreen = NULL; + container_destroy(view->swayc); + return ws; } - wl_list_remove(&view->surface_new_subsurface.link); - wl_list_remove(&view->container_reparent.link); - return container_destroy(view->swayc); } -- cgit v1.2.3 From 7a922c65aab27c5f4282cf15de52d240e5ac8052 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 26 Jun 2018 13:15:45 +1000 Subject: Damage output when a fullscreen view unmaps Also moved the arranging into view_unmap to avoid excessive code duplication. --- sway/tree/view.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/view.c b/sway/tree/view.c index 2ca0dbbb..5a78112a 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -539,7 +539,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { view_handle_container_reparent(&view->container_reparent, NULL); } -struct sway_container *view_unmap(struct sway_view *view) { +void view_unmap(struct sway_view *view) { wl_signal_emit(&view->events.unmap, view); wl_list_remove(&view->surface_new_subsurface.link); @@ -549,10 +549,16 @@ struct sway_container *view_unmap(struct sway_view *view) { struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); ws->sway_workspace->fullscreen = NULL; container_destroy(view->swayc); - return ws; - } - return container_destroy(view->swayc); + struct sway_container *output = ws->parent; + struct sway_transaction *transaction = transaction_create(); + arrange_windows(output, transaction); + transaction_add_damage(transaction, container_get_box(output)); + transaction_commit(transaction); + } else { + struct sway_container *parent = container_destroy(view->swayc); + arrange_and_commit(parent); + } } void view_update_position(struct sway_view *view, double lx, double ly) { -- cgit v1.2.3 From 50190bc7609d981c45d26cd0b7d6d0fbf66feb05 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 26 Jun 2018 13:18:33 +1000 Subject: Rename view's free callback to destroy --- sway/tree/view.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/view.c b/sway/tree/view.c index 5a78112a..a616af03 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -51,8 +51,8 @@ void view_free(struct sway_view *view) { wlr_texture_destroy(view->marks_unfocused); wlr_texture_destroy(view->marks_urgent); - if (view->impl->free) { - view->impl->free(view); + if (view->impl->destroy) { + view->impl->destroy(view); } else { free(view); } -- cgit v1.2.3 From 834805f5e260bcc77d714323d4a7f4bfd1dbfb17 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 26 Jun 2018 19:40:42 +1000 Subject: Fix crash when disconnecting output We were freeing the sway_output immediately upon disconnect which left a dangling pointer in the output's container. It then tried to use the pointer when the container is freed. We don't need to store the sway_output in an output's container which is destroying, so the fix is to set the pointer to NULL and remove the use in container_free. Also added an arrange when the output is disconnected for good measure. --- sway/tree/container.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index e30c7839..2f041a36 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -171,7 +171,6 @@ void container_free(struct sway_container *cont) { case C_ROOT: break; case C_OUTPUT: - cont->sway_output->swayc = NULL; break; case C_WORKSPACE: container_workspace_free(cont->sway_workspace); @@ -287,6 +286,7 @@ 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); -- cgit v1.2.3 From 93696b78ecbc31ec34be97ec26836efb74d359f0 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 26 Jun 2018 20:14:58 +1000 Subject: Fix crash when closing output window from outer session Emitting the close event needs to happen before container_output_destroy, because container_output_destroy sets the sway_output to NULL and sway_output is used in IPC. --- sway/tree/container.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index 2f041a36..0c860405 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -304,6 +304,9 @@ struct sway_container *container_destroy_noreaping(struct sway_container *con) { 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); @@ -325,9 +328,6 @@ struct sway_container *container_destroy_noreaping(struct sway_container *con) { } } - wl_signal_emit(&con->events.destroy, con); - ipc_event_window(con, "close"); - con->destroying = true; list_add(server.destroying_containers, con); -- cgit v1.2.3 From a7b3f29292cad029f010aa8b5fafb56b08ba4ed7 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 26 Jun 2018 20:18:57 +1000 Subject: Remove incorrect assertion and supporting code Children can exist when destroying a container, such as when destroying the last output. Sway is not terminating in that case. --- sway/tree/container.c | 9 --------- 1 file changed, 9 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index 0c860405..2b9eb636 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -319,15 +319,6 @@ struct sway_container *container_destroy_noreaping(struct sway_container *con) { } } - // At this point the container being destroyed shouldn't have any children - // unless sway is terminating. - if (!server.terminating) { - if (!sway_assert(!con->children || con->children->length == 0, - "Didn't expect to see children here")) { - return NULL; - } - } - con->destroying = true; list_add(server.destroying_containers, con); -- cgit v1.2.3 From 61c118768564eec07ac16494d90f567e75ea60cf Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 27 Jun 2018 17:23:44 +1000 Subject: Fix nitpicks --- sway/tree/container.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index 2b9eb636..8446c457 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -296,7 +296,8 @@ static struct sway_container *container_output_destroy( /** * Implement the actual destroy logic, without reaping. */ -struct sway_container *container_destroy_noreaping(struct sway_container *con) { +static struct sway_container *container_destroy_noreaping( + struct sway_container *con) { if (con == NULL) { return NULL; } -- cgit v1.2.3 From be86d3aba602fef7b51fafa8a6e7a39d1e49817f Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 27 Jun 2018 17:46:03 +1000 Subject: Remove transaction_add_damage Instead, damage each container when applying the transaction. --- sway/tree/arrange.c | 9 --------- sway/tree/view.c | 6 +----- 2 files changed, 1 insertion(+), 14 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index cb3f8ba2..582b2891 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -304,15 +304,6 @@ void arrange_windows(struct sway_container *container, case C_TYPES: break; } - // Add damage for whatever container arrange_windows() was called with, - // unless it was called with the special floating container, in which case - // we'll damage the entire output. - if (container->type == C_CONTAINER && container->layout == L_FLOATING) { - struct sway_container *output = container_parent(container, C_OUTPUT); - transaction_add_damage(transaction, container_get_box(output)); - } else { - transaction_add_damage(transaction, container_get_box(container)); - } add_deleted_containers(transaction); } diff --git a/sway/tree/view.c b/sway/tree/view.c index a616af03..68d2a029 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -550,11 +550,7 @@ void view_unmap(struct sway_view *view) { ws->sway_workspace->fullscreen = NULL; container_destroy(view->swayc); - struct sway_container *output = ws->parent; - struct sway_transaction *transaction = transaction_create(); - arrange_windows(output, transaction); - transaction_add_damage(transaction, container_get_box(output)); - transaction_commit(transaction); + arrange_and_commit(ws->parent); } else { struct sway_container *parent = container_destroy(view->swayc); arrange_and_commit(parent); -- cgit v1.2.3 From 8773ed39701748ba5500b4698d028795aa6e812e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 27 Jun 2018 17:47:41 +1000 Subject: Fix memleak in container_get_box Rather than allocate a structure and expect callers to free it, take a pointer to an existing struct as an argument. This function is no longer called anywhere though. --- sway/tree/container.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/container.c b/sway/tree/container.c index 8446c457..ab3d9dbd 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -988,11 +988,9 @@ bool container_is_floating(struct sway_container *container) { return container->parent == workspace->sway_workspace->floating; } -struct wlr_box *container_get_box(struct sway_container *container) { - struct wlr_box *box = calloc(1, sizeof(struct wlr_box)); +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; - return box; } -- cgit v1.2.3 From e6829c5991cac1bd164f800c14fccd522d702783 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 27 Jun 2018 17:54:57 +1000 Subject: Move unsetting of view->surface into view_unmap --- sway/tree/view.c | 1 + 1 file changed, 1 insertion(+) (limited to 'sway/tree') diff --git a/sway/tree/view.c b/sway/tree/view.c index 68d2a029..9f85bac0 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -555,6 +555,7 @@ void view_unmap(struct sway_view *view) { 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) { -- cgit v1.2.3