From 59c94887018bdfa578c4371c4275061ca6e71b3e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 3 Jun 2018 16:35:06 +1000 Subject: [PATCH] WIP: Atomic layout updates ground work --- include/sway/desktop/transaction.h | 56 +++++ include/sway/output.h | 2 + include/sway/tree/arrange.h | 33 ++- include/sway/tree/container.h | 29 +++ include/sway/tree/view.h | 19 +- sway/desktop/output.c | 70 ++++++- sway/desktop/transaction.c | 214 +++++++++++++++++++ sway/desktop/xdg_shell.c | 29 +-- sway/desktop/xdg_shell_v6.c | 30 +-- sway/desktop/xwayland.c | 33 +-- sway/meson.build | 1 + 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 + 16 files changed, 690 insertions(+), 266 deletions(-) create mode 100644 include/sway/desktop/transaction.h create mode 100644 sway/desktop/transaction.c diff --git a/include/sway/desktop/transaction.h b/include/sway/desktop/transaction.h new file mode 100644 index 00000000..575d28c8 --- /dev/null +++ b/include/sway/desktop/transaction.h @@ -0,0 +1,56 @@ +#ifndef _SWAY_TRANSACTION_H +#define _SWAY_TRANSACTION_H +#include "sway/tree/container.h" + +/** + * Transactions enable us to perform atomic layout updates. + * + * When we want to make adjustments to the layout, we create a transaction. + * A transaction contains a list of affected containers and their new state. + * A state might contain a new size, or new border settings, or new parent/child + * relationships. + * + * Calling transaction_commit() makes sway notify of all the affected clients + * with their new sizes. We then wait for all the views to respond with their + * new surface sizes. When all are ready, or when a timeout has passed, we apply + * the updates all at the same time. + */ + +struct sway_transaction { + struct wl_event_source *timer; + list_t *instructions; // struct sway_transaction_instruction * + list_t *damage; // struct wlr_box * + size_t num_waiting; +}; + +/** + * Create a new transaction. + */ +struct sway_transaction *transaction_create(void); + +/** + * Add a container's pending state to the transaction. + */ +void transaction_add_container(struct sway_transaction *transaction, + struct sway_container *container); + +/** + * Add a box to be damaged when the transaction is applied. + * The box should be in layout coordinates. + */ +void transaction_add_damage(struct sway_transaction *transaction, + struct wlr_box *box); + +/** + * Submit a transaction to the client views for configuration. + */ +void transaction_commit(struct sway_transaction *transaction); + +/** + * Notify the transaction system that a view is ready for the new layout. + * + * When all views in the transaction are ready, the layout will be applied. + */ +void transaction_notify_view_ready(struct sway_view *view, uint32_t serial); + +#endif diff --git a/include/sway/output.h b/include/sway/output.h index 70f746dc..e6ca0d02 100644 --- a/include/sway/output.h +++ b/include/sway/output.h @@ -42,6 +42,8 @@ void output_damage_surface(struct sway_output *output, double ox, double oy, void output_damage_from_view(struct sway_output *output, struct sway_view *view); +void output_damage_box(struct sway_output *output, struct wlr_box *box); + void output_damage_whole_container(struct sway_output *output, struct sway_container *con); diff --git a/include/sway/tree/arrange.h b/include/sway/tree/arrange.h index ce95cfe9..23cd66dc 100644 --- a/include/sway/tree/arrange.h +++ b/include/sway/tree/arrange.h @@ -1,18 +1,33 @@ #ifndef _SWAY_ARRANGE_H #define _SWAY_ARRANGE_H +#include "sway/desktop/transaction.h" struct sway_container; -// Determine the root container's geometry, then iterate to everything below -void arrange_root(void); - -// Determine the output's geometry, then iterate to everything below -void arrange_output(struct sway_container *output); +/** + * Arrange layout for all the children of the given container, and add them to + * the given transaction. + * + * Use this function if you need to arrange multiple sections of the tree in one + * transaction. + */ +void arrange_windows(struct sway_container *container, + struct sway_transaction *transaction); -// Determine the workspace's geometry, then iterate to everything below -void arrange_workspace(struct sway_container *workspace); +/** + * Arrange layout for the given container and commit the transaction. + * + * This function is a wrapper around arrange_windows, and handles creating and + * committing the transaction for you. Use this function if you're only doing + * one arrange operation. + */ +void arrange_and_commit(struct sway_container *container); -// Arrange layout for all the children of the given workspace/container -void arrange_children_of(struct sway_container *parent); +// These functions are temporary and are only here to make everything compile. +// They are wrappers around arrange_and_commit. +void arrange_root(void); +void arrange_output(struct sway_container *container); +void arrange_workspace(struct sway_container *container); +void arrange_children_of(struct sway_container *container); #endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 7ed6aab1..dd5bd47c 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -54,6 +54,28 @@ struct sway_output; struct sway_workspace; struct sway_view; +struct sway_container_state { + // Container/swayc properties + enum sway_container_layout layout; + double swayc_x, swayc_y; + double swayc_width, swayc_height; + + //struct sway_container *parent; + //list_t *children; + + // View properties + double view_x, view_y; + double view_width, view_height; + bool is_fullscreen; + + enum sway_container_border border; + int border_thickness; + bool border_top; + bool border_bottom; + bool border_left; + bool border_right; +}; + struct sway_container { union { // TODO: Encapsulate state for other node types as well like C_CONTAINER @@ -69,6 +91,8 @@ struct sway_container { */ size_t id; + struct sway_container_state pending; + char *name; // The view's title (unformatted) char *formatted_title; // The title displayed in the title bar @@ -246,4 +270,9 @@ void container_set_geometry_from_floating_view(struct sway_container *con); */ bool container_is_floating(struct sway_container *container); +/** + * Get a container's box in layout coordinates. + */ +struct wlr_box *container_get_box(struct sway_container *container); + #endif diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 3df38e2d..f47db567 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -29,8 +29,8 @@ struct sway_view_impl { const char *(*get_string_prop)(struct sway_view *view, enum sway_view_prop prop); uint32_t (*get_int_prop)(struct sway_view *view, enum sway_view_prop prop); - void (*configure)(struct sway_view *view, double lx, double ly, int width, - int height); + uint32_t (*configure)(struct sway_view *view, double lx, double ly, + int width, int height); void (*set_activated)(struct sway_view *view, bool activated); void (*set_fullscreen)(struct sway_view *view, bool fullscreen); bool (*wants_floating)(struct sway_view *view); @@ -70,6 +70,12 @@ struct sway_view { list_t *executed_criteria; // struct criteria * list_t *marks; // char * + list_t *instructions; // struct sway_transaction_instruction * + + // If saved_texture is set, the main surface of the view will render this + // texture instead of its own. This is used while waiting for transactions + // to complete. + struct wlr_texture *saved_texture; struct wlr_texture *marks_focused; struct wlr_texture *marks_focused_inactive; @@ -103,8 +109,6 @@ struct sway_xdg_shell_v6_view { struct wl_listener map; struct wl_listener unmap; struct wl_listener destroy; - - int pending_width, pending_height; }; struct sway_xdg_shell_view { @@ -119,8 +123,6 @@ struct sway_xdg_shell_view { struct wl_listener map; struct wl_listener unmap; struct wl_listener destroy; - - int pending_width, pending_height; }; struct sway_xwayland_view { @@ -138,9 +140,6 @@ struct sway_xwayland_view { struct wl_listener map; struct wl_listener unmap; struct wl_listener destroy; - - int pending_lx, pending_ly; - int pending_width, pending_height; }; struct sway_xwayland_unmanaged { @@ -212,7 +211,7 @@ uint32_t view_get_window_type(struct sway_view *view); const char *view_get_shell(struct sway_view *view); -void view_configure(struct sway_view *view, double ox, double oy, int width, +uint32_t view_configure(struct sway_view *view, double lx, double ly, int width, int height); /** diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 3142bdb4..c5d445a6 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -69,6 +69,7 @@ struct render_data { struct root_geometry root_geo; struct sway_output *output; pixman_region32_t *damage; + struct sway_view *view; float alpha; }; @@ -108,6 +109,38 @@ static bool get_surface_box(struct root_geometry *geo, return wlr_box_intersection(&output_box, &rotated_box, &intersection); } +static bool get_view_box(struct root_geometry *geo, + struct sway_output *output, struct sway_view *view, int sx, int sy, + struct wlr_box *surface_box) { + int sw = view->width; + int sh = view->height; + + double _sx = sx, _sy = sy; + rotate_child_position(&_sx, &_sy, sw, sh, geo->width, geo->height, + geo->rotation); + + struct wlr_box box = { + .x = geo->x + _sx, + .y = geo->y + _sy, + .width = sw, + .height = sh, + }; + if (surface_box != NULL) { + memcpy(surface_box, &box, sizeof(struct wlr_box)); + } + + struct wlr_box rotated_box; + wlr_box_rotated_bounds(&box, geo->rotation, &rotated_box); + + struct wlr_box output_box = { + .width = output->swayc->width, + .height = output->swayc->height, + }; + + struct wlr_box intersection; + return wlr_box_intersection(&output_box, &rotated_box, &intersection); +} + static void surface_for_each_surface(struct wlr_surface *surface, double ox, double oy, struct root_geometry *geo, wlr_surface_iterator_func_t iterator, void *user_data) { @@ -225,13 +258,26 @@ static void render_surface_iterator(struct wlr_surface *surface, int sx, int sy, pixman_region32_t *output_damage = data->damage; float alpha = data->alpha; - if (!wlr_surface_has_buffer(surface)) { - return; + struct wlr_texture *texture = NULL; + struct wlr_box box; + bool intersects; + + // If this is the main surface of a view, render the saved_texture instead + // if it exists. It exists when we are mid-transaction. + if (data->view && data->view->saved_texture && + data->view->surface == surface) { + texture = data->view->saved_texture; + intersects = get_view_box(&data->root_geo, data->output, data->view, + sx, sy, &box); + } else { + if (!wlr_surface_has_buffer(surface)) { + return; + } + texture = surface->texture; + intersects = get_surface_box(&data->root_geo, data->output, surface, + sx, sy, &box); } - struct wlr_box box; - bool intersects = get_surface_box(&data->root_geo, data->output, surface, - sx, sy, &box); if (!intersects) { return; } @@ -244,8 +290,7 @@ static void render_surface_iterator(struct wlr_surface *surface, int sx, int sy, wlr_matrix_project_box(matrix, &box, transform, rotation, wlr_output->transform_matrix); - render_texture(wlr_output, output_damage, surface->texture, &box, matrix, - alpha); + render_texture(wlr_output, output_damage, texture, &box, matrix, alpha); } static void render_layer(struct sway_output *output, @@ -315,6 +360,7 @@ static void render_view_surfaces(struct sway_view *view, struct render_data data = { .output = output, .damage = damage, + .view = view, .alpha = alpha, }; output_view_for_each_surface( @@ -1134,6 +1180,16 @@ void output_damage_from_view(struct sway_output *output, output_damage_view(output, view, false); } +// Expecting an unscaled box in layout coordinates +void output_damage_box(struct sway_output *output, struct wlr_box *_box) { + struct wlr_box box; + memcpy(&box, _box, sizeof(struct wlr_box)); + box.x -= output->swayc->x; + box.y -= output->swayc->y; + scale_box(&box, output->wlr_output->scale); + wlr_output_damage_add_box(output->damage, &box); +} + static void output_damage_whole_container_iterator(struct sway_container *con, void *data) { struct sway_output *output = data; diff --git a/sway/desktop/transaction.c b/sway/desktop/transaction.c new file mode 100644 index 00000000..69f97e3d --- /dev/null +++ b/sway/desktop/transaction.c @@ -0,0 +1,214 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include "sway/debug.h" +#include "sway/desktop/transaction.h" +#include "sway/output.h" +#include "sway/tree/container.h" +#include "sway/tree/view.h" +#include "list.h" +#include "log.h" + +/** + * How long we should wait for views to respond to the configure before giving + * up and applying the transaction anyway. + */ +#define TIMEOUT_MS 200 + +struct sway_transaction_instruction { + struct sway_transaction *transaction; + struct sway_container *container; + struct sway_container_state state; + uint32_t serial; +}; + +struct sway_transaction *transaction_create() { + struct sway_transaction *transaction = + calloc(1, sizeof(struct sway_transaction)); + transaction->instructions = create_list(); + transaction->damage = create_list(); + return transaction; +} + +static void transaction_destroy(struct sway_transaction *transaction) { + int i; + // Free instructions + for (i = 0; i < transaction->instructions->length; ++i) { + struct sway_transaction_instruction *instruction = + transaction->instructions->items[i]; + if (instruction->container->type == C_VIEW) { + struct sway_view *view = instruction->container->sway_view; + for (int j = 0; j < view->instructions->length; ++j) { + if (view->instructions->items[j] == instruction) { + list_del(view->instructions, j); + break; + } + } + } + free(instruction); + } + list_free(transaction->instructions); + + // Free damage + for (i = 0; i < transaction->damage->length; ++i) { + struct wlr_box *box = transaction->damage->items[i]; + free(box); + } + list_free(transaction->damage); + + free(transaction); +} + +void transaction_add_container(struct sway_transaction *transaction, + struct sway_container *container) { + struct sway_transaction_instruction *instruction = + calloc(1, sizeof(struct sway_transaction_instruction)); + instruction->transaction = transaction; + instruction->container = container; + memcpy(&instruction->state, &container->pending, + sizeof(struct sway_container_state)); + list_add(transaction->instructions, instruction); +} + +void transaction_add_damage(struct sway_transaction *transaction, + struct wlr_box *_box) { + struct wlr_box *box = calloc(1, sizeof(struct wlr_box)); + memcpy(box, _box, sizeof(struct wlr_box)); + list_add(transaction->damage, box); +} + +static void save_view_texture(struct sway_view *view) { + wlr_texture_destroy(view->saved_texture); + view->saved_texture = NULL; + + // TODO: Copy the texture and store it in view->saved_texture. +} + +static void remove_saved_view_texture(struct sway_view *view) { + wlr_texture_destroy(view->saved_texture); + view->saved_texture = NULL; +} + +/** + * Apply a transaction to the "current" state of the tree. + * + * This is mostly copying stuff from the pending state into the main swayc + * properties, but also includes reparenting and deleting containers. + */ +static void transaction_apply(struct sway_transaction *transaction) { + int i; + for (i = 0; i < transaction->instructions->length; ++i) { + struct sway_transaction_instruction *instruction = + transaction->instructions->items[i]; + struct sway_container_state *state = &instruction->state; + struct sway_container *container = instruction->container; + + container->layout = state->layout; + container->x = state->swayc_x; + container->y = state->swayc_y; + container->width = state->swayc_width; + container->height = state->swayc_height; + + if (container->type == C_VIEW) { + struct sway_view *view = container->sway_view; + view->x = state->view_x; + view->y = state->view_y; + view->width = state->view_width; + view->height = state->view_height; + view->is_fullscreen = state->is_fullscreen; + view->border = state->border; + view->border_thickness = state->border_thickness; + view->border_top = state->border_top; + view->border_left = state->border_left; + view->border_right = state->border_right; + view->border_bottom = state->border_bottom; + + remove_saved_view_texture(view); + } + } + + // Damage + for (i = 0; i < transaction->damage->length; ++i) { + struct wlr_box *box = transaction->damage->items[i]; + for (int j = 0; j < root_container.children->length; ++j) { + struct sway_container *output = root_container.children->items[j]; + output_damage_box(output->sway_output, box); + } + } + + update_debug_tree(); +} + +static int handle_timeout(void *data) { + struct sway_transaction *transaction = data; + wlr_log(L_DEBUG, "Transaction %p timed out (%li waiting), applying anyway", + transaction, transaction->num_waiting); + transaction_apply(transaction); + transaction_destroy(transaction); + return 0; +} + +void transaction_commit(struct sway_transaction *transaction) { + wlr_log(L_DEBUG, "Transaction %p committing with %i instructions", + transaction, transaction->instructions->length); + transaction->num_waiting = 0; + for (int i = 0; i < transaction->instructions->length; ++i) { + struct sway_transaction_instruction *instruction = + transaction->instructions->items[i]; + if (instruction->container->type == C_VIEW) { + struct sway_view *view = instruction->container->sway_view; + instruction->serial = view_configure(view, + instruction->state.view_x, + instruction->state.view_y, + instruction->state.view_width, + instruction->state.view_height); + if (instruction->serial) { + save_view_texture(view); + list_add(view->instructions, instruction); + ++transaction->num_waiting; + } + } + } + if (!transaction->num_waiting) { + // This can happen if the transaction only contains xwayland views + wlr_log(L_DEBUG, "Transaction %p has nothing to wait for, applying", + transaction); + transaction_apply(transaction); + transaction_destroy(transaction); + return; + } + + // Set up a timer which the views must respond within + transaction->timer = wl_event_loop_add_timer(server.wl_event_loop, + handle_timeout, transaction); + wl_event_source_timer_update(transaction->timer, TIMEOUT_MS); +} + +void transaction_notify_view_ready(struct sway_view *view, uint32_t serial) { + // Find the instruction + struct sway_transaction_instruction *instruction = NULL; + for (int i = 0; i < view->instructions->length; ++i) { + struct sway_transaction_instruction *tmp_instruction = + view->instructions->items[i]; + if (tmp_instruction->serial == serial) { + instruction = tmp_instruction; + list_del(view->instructions, i); + break; + } + } + if (!instruction) { + // This can happen if the view acknowledges the configure after the + // transaction has timed out and applied. + return; + } + // If all views are ready, apply the transaction + struct sway_transaction *transaction = instruction->transaction; + if (--transaction->num_waiting == 0) { + wlr_log(L_DEBUG, "Transaction %p is ready, applying", transaction); + wl_event_source_timer_update(transaction->timer, 0); + transaction_apply(transaction); + transaction_destroy(transaction); + } +} diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index d2b8822c..f43a0a1b 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -5,6 +5,7 @@ #include #include #include "log.h" +#include "sway/desktop/transaction.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/server.h" @@ -87,18 +88,14 @@ static const char *get_string_prop(struct sway_view *view, enum sway_view_prop p } } -static void configure(struct sway_view *view, double lx, double ly, int width, - int height) { +static uint32_t configure(struct sway_view *view, double lx, double ly, + int width, int height) { struct sway_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); if (xdg_shell_view == NULL) { - return; + return 0; } - - xdg_shell_view->pending_width = width; - xdg_shell_view->pending_height = height; - wlr_xdg_toplevel_set_size(view->wlr_xdg_surface, width, height); - view_update_position(view, lx, ly); + return wlr_xdg_toplevel_set_size(view->wlr_xdg_surface, width, height); } static void set_activated(struct sway_view *view, bool activated) { @@ -174,18 +171,12 @@ static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, commit); struct sway_view *view = &xdg_shell_view->view; - if (view->swayc && container_is_floating(view->swayc)) { - int width = view->wlr_xdg_surface->geometry.width; - int height = view->wlr_xdg_surface->geometry.height; - if (!width && !height) { - width = view->wlr_xdg_surface->surface->current->width; - height = view->wlr_xdg_surface->surface->current->height; - } - view_update_size(view, width, height); - } else { - view_update_size(view, xdg_shell_view->pending_width, - xdg_shell_view->pending_height); + struct wlr_xdg_surface *xdg_surface = view->wlr_xdg_surface; + + if (view->instructions->length) { + transaction_notify_view_ready(view, xdg_surface->configure_serial); } + view_update_title(view, false); view_damage_from(view); } diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 6ffe334a..bce59174 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -3,6 +3,7 @@ #include #include #include +#include "sway/desktop/transaction.h" #include "sway/tree/container.h" #include "sway/tree/layout.h" #include "sway/server.h" @@ -86,18 +87,15 @@ static const char *get_string_prop(struct sway_view *view, enum sway_view_prop p } } -static void configure(struct sway_view *view, double lx, double ly, int width, - int height) { +static uint32_t configure(struct sway_view *view, double lx, double ly, + int width, int height) { struct sway_xdg_shell_v6_view *xdg_shell_v6_view = xdg_shell_v6_view_from_view(view); if (xdg_shell_v6_view == NULL) { - return; + return 0; } - - xdg_shell_v6_view->pending_width = width; - xdg_shell_v6_view->pending_height = height; - wlr_xdg_toplevel_v6_set_size(view->wlr_xdg_surface_v6, width, height); - view_update_position(view, lx, ly); + return wlr_xdg_toplevel_v6_set_size( + view->wlr_xdg_surface_v6, width, height); } static void set_activated(struct sway_view *view, bool activated) { @@ -173,18 +171,12 @@ static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xdg_shell_v6_view *xdg_shell_v6_view = wl_container_of(listener, xdg_shell_v6_view, commit); struct sway_view *view = &xdg_shell_v6_view->view; - if (view->swayc && container_is_floating(view->swayc)) { - int width = view->wlr_xdg_surface_v6->geometry.width; - int height = view->wlr_xdg_surface_v6->geometry.height; - if (!width && !height) { - width = view->wlr_xdg_surface_v6->surface->current->width; - height = view->wlr_xdg_surface_v6->surface->current->height; - } - view_update_size(view, width, height); - } else { - view_update_size(view, xdg_shell_v6_view->pending_width, - xdg_shell_v6_view->pending_height); + struct wlr_xdg_surface_v6 *xdg_surface_v6 = view->wlr_xdg_surface_v6; + + if (view->instructions->length) { + transaction_notify_view_ready(view, xdg_surface_v6->configure_serial); } + view_update_title(view, false); view_damage_from(view); } diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 6447b711..6a3c1b66 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -167,19 +167,18 @@ static uint32_t get_int_prop(struct sway_view *view, enum sway_view_prop prop) { } } -static void configure(struct sway_view *view, double lx, double ly, int width, +static uint32_t configure(struct sway_view *view, double lx, double ly, int width, int height) { struct sway_xwayland_view *xwayland_view = xwayland_view_from_view(view); if (xwayland_view == NULL) { - return; + return 0; } struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; - xwayland_view->pending_lx = lx; - xwayland_view->pending_ly = ly; - xwayland_view->pending_width = width; - xwayland_view->pending_height = height; wlr_xwayland_surface_configure(xsurface, lx, ly, width, height); + + // xwayland doesn't give us a serial for the configure + return 0; } static void set_activated(struct sway_view *view, bool activated) { @@ -250,15 +249,21 @@ static void handle_commit(struct wl_listener *listener, void *data) { wl_container_of(listener, xwayland_view, commit); struct sway_view *view = &xwayland_view->view; struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; - if (view->swayc && container_is_floating(view->swayc)) { - view_update_size(view, xsurface->width, xsurface->height); - } else { - view_update_size(view, xwayland_view->pending_width, - xwayland_view->pending_height); + + // Don't allow xwayland views to do resize or reposition themselves if + // they're involved in a transaction. Once the transaction has finished + // they'll apply the next time a commit happens. + if (view->instructions->length) { + if (view->swayc && container_is_floating(view->swayc)) { + view_update_size(view, xsurface->width, xsurface->height); + } else { + view_update_size(view, view->swayc->pending.swayc_width, + view->swayc->pending.swayc_height); + } + view_update_position(view, + view->swayc->pending.view_x, view->swayc->pending.view_y); + view_damage_from(view); } - view_update_position(view, - xwayland_view->pending_lx, xwayland_view->pending_ly); - view_damage_from(view); } static void handle_unmap(struct wl_listener *listener, void *data) { diff --git a/sway/meson.build b/sway/meson.build index b6bb02a7..5ff62490 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -12,6 +12,7 @@ sway_sources = files( 'desktop/desktop.c', 'desktop/layer_shell.c', 'desktop/output.c', + 'desktop/transaction.c', 'desktop/xdg_shell_v6.c', 'desktop/xdg_shell.c', 'desktop/xwayland.c', 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;