#define _POSIX_C_SOURCE 200809L #include #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 { struct wl_event_source *timer; list_t *instructions; // struct sway_transaction_instruction * list_t *damage; // struct wlr_box * size_t num_waiting; }; 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; // Copy the container's main (pending) properties into the instruction state struct sway_container_state *state = &instruction->state; state->layout = container->layout; state->swayc_x = container->x; state->swayc_y = container->y; state->swayc_width = container->width; state->swayc_height = container->height; state->has_gaps = container->has_gaps; state->current_gaps = container->current_gaps; state->gaps_inner = container->gaps_inner; state->gaps_outer = container->gaps_outer; if (container->type == C_VIEW) { struct sway_view *view = container->sway_view; state->view_x = view->x; state->view_y = view->y; state->view_width = view->width; state->view_height = view->height; state->is_fullscreen = view->is_fullscreen; state->border = view->border; state->border_thickness = view->border_thickness; state->border_top = view->border_top; state->border_left = view->border_left; state->border_right = view->border_right; state->border_bottom = view->border_bottom; } 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_buffer(struct sway_view *view) { if (view->saved_buffer) { wlr_buffer_unref(view->saved_buffer); } wlr_buffer_ref(view->surface->buffer); view->saved_buffer = view->surface->buffer; view->saved_surface_width = view->surface->current->width; view->saved_surface_height = view->surface->current->height; } static void remove_saved_view_buffer(struct sway_view *view) { if (view->saved_buffer) { wlr_buffer_unref(view->saved_buffer); view->saved_buffer = NULL; view->saved_surface_width = 0; view->saved_surface_height = 0; } } /** * 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 *container = instruction->container; memcpy(&instruction->container->current, &instruction->state, sizeof(struct sway_container_state)); if (container->type == C_VIEW) { remove_saved_view_buffer(container->sway_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]; struct sway_container *con = instruction->container; if (con->type == C_VIEW && (con->current.view_width != instruction->state.view_width || con->current.view_height != instruction->state.view_height)) { instruction->serial = view_configure(con->sway_view, instruction->state.view_x, instruction->state.view_y, instruction->state.view_width, instruction->state.view_height); if (instruction->serial) { save_view_buffer(con->sway_view); list_add(con->sway_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); } }