parent
0b798ed954
commit
59c9488701
@ -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
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue