aboutsummaryrefslogtreecommitdiff
path: root/sway/desktop
diff options
context:
space:
mode:
authorRyan Dwyer <ryandwyer1@gmail.com>2018-06-03 16:35:06 +1000
committerRyan Dwyer <ryandwyer1@gmail.com>2018-06-09 10:08:43 +1000
commit59c94887018bdfa578c4371c4275061ca6e71b3e (patch)
tree62bdaa6ac4777d1fcb292013bddd2043dad7765a /sway/desktop
parent0b798ed9543d55bd39782c3a4a4bc1789acd40d3 (diff)
downloadsway-59c94887018bdfa578c4371c4275061ca6e71b3e.tar.xz
WIP: Atomic layout updates ground work
Diffstat (limited to 'sway/desktop')
-rw-r--r--sway/desktop/output.c70
-rw-r--r--sway/desktop/transaction.c214
-rw-r--r--sway/desktop/xdg_shell.c29
-rw-r--r--sway/desktop/xdg_shell_v6.c30
-rw-r--r--sway/desktop/xwayland.c33
5 files changed, 317 insertions, 59 deletions
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 <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wlr/types/wlr_linux_dmabuf.h>
+#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 <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/edges.h>
#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 <stdlib.h>
#include <wayland-server.h>
#include <wlr/types/wlr_xdg_shell_v6.h>
+#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) {