aboutsummaryrefslogtreecommitdiffstats
path: root/sway/desktop/transaction.c
blob: 69f97e3d7af8062d6b9a37f310627a2d0fce47b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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);
	}
}