Simplify transactions by utilising a dirty flag on containers

This PR changes the way we handle transactions to a more simple method.
The new method is to mark containers as dirty from low level code
(eg. arranging, or container_destroy, and eventually seat_set_focus),
then call transaction_commit_dirty which picks up those containers and
runs them through a transaction. The old methods of using transactions
(arrange_and_commit, or creating one manually) are now no longer
possible.

The highest-level code (execute_command and view implementation
handlers) will call transaction_commit_dirty, so most other code just
needs to set containers as dirty. This is done by arranging, but can
also be done by calling container_set_dirty.
This commit is contained in:
Ryan Dwyer 2018-07-14 23:14:55 +10:00
parent 63f28bcf1e
commit 2032f85d94
29 changed files with 139 additions and 190 deletions

View file

@ -144,38 +144,22 @@ 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);
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) {
static void arrange_floating(struct sway_container *floating) {
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);
arrange_children_of(floater);
}
transaction_add_container(transaction, floater);
container_set_dirty(floater);
}
transaction_add_container(transaction, floating);
container_set_dirty(floating);
}
static void arrange_children_of(struct sway_container *parent,
struct sway_transaction *transaction) {
static void arrange_children_of(struct sway_container *parent) {
if (config->reloading) {
return;
}
@ -198,7 +182,7 @@ static void arrange_children_of(struct sway_container *parent,
apply_horiz_layout(parent);
break;
case L_FLOATING:
arrange_floating(parent, transaction);
arrange_floating(parent);
break;
}
@ -213,14 +197,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_add_container(transaction, child);
container_set_dirty(child);
}
}
static void arrange_workspace(struct sway_container *workspace,
struct sway_transaction *transaction) {
static void arrange_workspace(struct sway_container *workspace) {
if (config->reloading) {
return;
}
@ -234,15 +217,14 @@ static void arrange_workspace(struct sway_container *workspace,
workspace->x = output->x + area->x;
workspace->y = output->y + area->y;
add_gaps(workspace);
transaction_add_container(transaction, workspace);
container_set_dirty(workspace);
wlr_log(WLR_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name,
workspace->x, workspace->y);
arrange_floating(workspace->sway_workspace->floating, transaction);
arrange_children_of(workspace, transaction);
arrange_floating(workspace->sway_workspace->floating);
arrange_children_of(workspace);
}
static void arrange_output(struct sway_container *output,
struct sway_transaction *transaction) {
static void arrange_output(struct sway_container *output) {
if (config->reloading) {
return;
}
@ -253,16 +235,16 @@ static void arrange_output(struct sway_container *output,
output->y = output_box->y;
output->width = output_box->width;
output->height = output_box->height;
transaction_add_container(transaction, output);
container_set_dirty(output);
wlr_log(WLR_DEBUG, "Arranging output '%s' at %f,%f",
output->name, output->x, output->y);
for (int i = 0; i < output->children->length; ++i) {
struct sway_container *workspace = output->children->items[i];
arrange_workspace(workspace, transaction);
arrange_workspace(workspace);
}
}
static void arrange_root(struct sway_transaction *transaction) {
static void arrange_root() {
if (config->reloading) {
return;
}
@ -274,43 +256,35 @@ 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;
transaction_add_container(transaction, &root_container);
container_set_dirty(&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);
}
}
void arrange_windows(struct sway_container *container,
struct sway_transaction *transaction) {
void arrange_windows(struct sway_container *container) {
switch (container->type) {
case C_ROOT:
arrange_root(transaction);
arrange_root();
break;
case C_OUTPUT:
arrange_output(container, transaction);
arrange_output(container);
break;
case C_WORKSPACE:
arrange_workspace(container, transaction);
arrange_workspace(container);
break;
case C_CONTAINER:
arrange_children_of(container, transaction);
transaction_add_container(transaction, container);
arrange_children_of(container);
container_set_dirty(container);
break;
case C_VIEW:
view_autoconfigure(container->sway_view);
transaction_add_container(transaction, container);
container_set_dirty(container);
break;
case C_TYPES:
break;
}
add_deleted_containers(transaction);
}
void arrange_and_commit(struct sway_container *container) {
struct sway_transaction *transaction = transaction_create();
arrange_windows(container, transaction);
transaction_commit(transaction);
}
void remove_gaps(struct sway_container *c) {

View file

@ -159,14 +159,6 @@ void container_free(struct sway_container *cont) {
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->current.children);
@ -325,7 +317,7 @@ static struct sway_container *container_destroy_noreaping(
}
con->destroying = true;
list_add(server.destroying_containers, con);
container_set_dirty(con);
if (!con->parent) {
return NULL;
@ -1069,9 +1061,15 @@ void container_floating_move_to(struct sway_container *con,
if (old_workspace != new_workspace) {
container_remove_child(con);
container_add_child(new_workspace->sway_workspace->floating, con);
struct sway_transaction *transaction = transaction_create();
arrange_windows(old_workspace, transaction);
arrange_windows(new_workspace, transaction);
transaction_commit(transaction);
arrange_windows(old_workspace);
arrange_windows(new_workspace);
}
}
void container_set_dirty(struct sway_container *container) {
if (container->dirty) {
return;
}
container->dirty = true;
list_add(server.dirty_containers, container);
}

View file

@ -22,7 +22,8 @@ struct sway_container root_container;
static void output_layout_handle_change(struct wl_listener *listener,
void *data) {
arrange_and_commit(&root_container);
arrange_windows(&root_container);
transaction_commit_dirty();
}
void layout_init(void) {

View file

@ -594,11 +594,12 @@ void view_unmap(struct sway_view *view) {
ws->sway_workspace->fullscreen = NULL;
container_destroy(view->swayc);
arrange_and_commit(ws->parent);
arrange_windows(ws->parent);
} else {
struct sway_container *parent = container_destroy(view->swayc);
arrange_and_commit(parent);
arrange_windows(parent);
}
transaction_commit_dirty();
view->surface = NULL;
}

View file

@ -427,7 +427,7 @@ bool workspace_switch(struct sway_container *workspace) {
}
seat_set_focus(seat, next);
struct sway_container *output = container_parent(workspace, C_OUTPUT);
arrange_and_commit(output);
arrange_windows(output);
return true;
}