aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/arrange.c
blob: 53c95820cd9435208d11210f76f6c5caa8570431 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include "sway/debug.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "sway/tree/layout.h"
#include "sway/output.h"
#include "sway/tree/workspace.h"
#include "sway/tree/view.h"
#include "list.h"
#include "log.h"

struct sway_container root_container;

void arrange_root() {
	if (config->reloading) {
		return;
	}
	struct wlr_output_layout *output_layout =
		root_container.sway_root->output_layout;
	const struct wlr_box *layout_box =
		wlr_output_layout_get_box(output_layout, NULL);
	root_container.x = layout_box->x;
	root_container.y = layout_box->y;
	root_container.width = layout_box->width;
	root_container.height = layout_box->height;
	for (int i = 0; i < root_container.children->length; ++i) {
		struct sway_container *output = root_container.children->items[i];
		arrange_output(output);
	}
}

void arrange_output(struct sway_container *output) {
	if (config->reloading) {
		return;
	}
	if (!sway_assert(output->type == C_OUTPUT,
			"called arrange_output() on non-output container")) {
		return;
	}

	const struct wlr_box *output_box = wlr_output_layout_get_box(
			root_container.sway_root->output_layout,
			output->sway_output->wlr_output);
	output->x = output_box->x;
	output->y = output_box->y;
	output->width = output_box->width;
	output->height = output_box->height;
	wlr_log(L_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);
	}
	container_damage_whole(output);
}

void arrange_workspace(struct sway_container *workspace) {
	if (config->reloading) {
		return;
	}
	if (!sway_assert(workspace->type == C_WORKSPACE,
			"called arrange_workspace() on non-workspace container")) {
		return;
	}

	struct sway_container *output = workspace->parent;
	struct wlr_box *area = &output->sway_output->usable_area;
	wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d",
			area->width, area->height, area->x, area->y);

	remove_gaps(workspace);

	workspace->width = area->width;
	workspace->height = area->height;
	workspace->x = output->x + area->x;
	workspace->y = output->y + area->y;

	add_gaps(workspace);

	wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f",
			workspace->name, workspace->x, workspace->y);
	arrange_children_of(workspace);
}

void remove_gaps(struct sway_container *c) {
	if (c->current_gaps == 0) {
		wlr_log(L_DEBUG, "Removing gaps: not gapped: %p", c);
		return;
	}

	c->width += c->current_gaps * 2;
	c->height += c->current_gaps * 2;
	c->x -= c->current_gaps;
	c->y -= c->current_gaps;

	c->current_gaps = 0;

	wlr_log(L_DEBUG, "Removing gaps %p", c);
}

void add_gaps(struct sway_container *c) {
	if (c->current_gaps > 0 || c->type == C_CONTAINER) {
		wlr_log(L_DEBUG, "Not adding gaps: %p", c);
		return;
	}

	if (c->type == C_WORKSPACE &&
		!(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) {
		return;
	}

	double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner;

	c->x += gaps;
	c->y += gaps;
	c->width -= 2 * gaps;
	c->height -= 2 * gaps;
	c->current_gaps = gaps;

	wlr_log(L_DEBUG, "Adding gaps: %p", c);
}

static void apply_horiz_layout(struct sway_container *parent) {
	size_t num_children = parent->children->length;
	if (!num_children) {
		return;
	}
	size_t parent_offset = 0;
	if (parent->parent->layout == L_TABBED) {
		parent_offset = container_titlebar_height();
	} else if (parent->parent->layout == L_STACKED) {
		parent_offset =
			container_titlebar_height() * parent->parent->children->length;
	}
	size_t parent_height = parent->height - parent_offset;

	// Calculate total width of children
	double total_width = 0;
	for (size_t i = 0; i < num_children; ++i) {
		struct sway_container *child = parent->children->items[i];

		if (child->width <= 0) {
			if (num_children > 1) {
				child->width = parent->width / (num_children - 1);
			} else {
				child->width = parent->width;
			}
		}
		remove_gaps(child);
		total_width += child->width;
	}
	double scale = parent->width / total_width;

	// Resize windows
	wlr_log(L_DEBUG, "Arranging %p horizontally", parent);
	double child_x = parent->x;
	struct sway_container *child;
	for (size_t i = 0; i < num_children; ++i) {
		child = parent->children->items[i];
		wlr_log(L_DEBUG,
				"Calculating arrangement for %p:%d (will scale %f by %f)",
				child, child->type, child->width, scale);
		child->x = child_x;
		child->y = parent->y + parent_offset;
		child->height = parent_height;

		if (i == num_children - 1) {
			// Make last child use remaining width of parent
			child->width = parent->x + parent->width - child->x;
		} else {
			child->width = floor(child->width * scale);
		}

		child_x += child->width;

		add_gaps(child);
	}
}

static void apply_vert_layout(struct sway_container *parent) {
	size_t num_children = parent->children->length;
	if (!num_children) {
		return;
	}
	size_t parent_offset = 0;
	if (parent->parent->layout == L_TABBED) {
		parent_offset = container_titlebar_height();
	} else if (parent->parent->layout == L_STACKED) {
		parent_offset =
			container_titlebar_height() * parent->parent->children->length;
	}
	size_t parent_height = parent->height + parent_offset;

	// Calculate total height of children
	double total_height = 0;
	for (size_t i = 0; i < num_children; ++i) {
		struct sway_container *child = parent->children->items[i];

		if (child->height <= 0) {
			if (num_children > 1) {
				child->height = parent_height / (num_children - 1);
			} else {
				child->height = parent_height;
			}
		}
		remove_gaps(child);
		total_height += child->height;
	}
	double scale = parent_height / total_height;

	// Resize
	wlr_log(L_DEBUG, "Arranging %p vertically", parent);
	double child_y = parent->y + parent_offset;
	struct sway_container *child;
	for (size_t i = 0; i < num_children; ++i) {
		child = parent->children->items[i];
		wlr_log(L_DEBUG,
				"Calculating arrangement for %p:%d (will scale %f by %f)",
				child, child->type, child->height, scale);
		child->x = parent->x;
		child->y = child_y;
		child->width = parent->width;

		if (i == num_children - 1) {
			// Make last child use remaining height of parent
			child->height = parent->y + parent_offset + parent_height - child->y;
		} else {
			child->height = floor(child->height * scale);
		}

		child_y += child->height;

		add_gaps(child);
	}
}

static void apply_tabbed_or_stacked_layout(struct sway_container *parent) {
	if (!parent->children->length) {
		return;
	}
	size_t parent_offset = 0;
	if (parent->parent->layout == L_TABBED) {
		parent_offset = container_titlebar_height();
	} else if (parent->parent->layout == L_STACKED) {
		parent_offset =
			container_titlebar_height() * parent->parent->children->length;
	}
	size_t parent_height = parent->height - parent_offset;
	for (int i = 0; i < parent->children->length; ++i) {
		struct sway_container *child = parent->children->items[i];
		remove_gaps(child);
		child->x = parent->x;
		child->y = parent->y + parent_offset;
		child->width = parent->width;
		child->height = parent_height;
		add_gaps(child);
	}
}

void arrange_children_of(struct sway_container *parent) {
	if (config->reloading) {
		return;
	}
	if (!sway_assert(parent->type == C_WORKSPACE || parent->type == C_CONTAINER,
			"container is a %s", container_type_to_str(parent->type))) {
		return;
	}

	struct sway_container *workspace = parent;
	if (workspace->type != C_WORKSPACE) {
		workspace = container_parent(workspace, C_WORKSPACE);
	}

	if (workspace->sway_workspace->fullscreen) {
		// Just arrange the fullscreen view and jump out
		view_autoconfigure(workspace->sway_workspace->fullscreen);
		return;
	}

	wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent,
		parent->name, parent->width, parent->height, parent->x, parent->y);

	// Calculate x, y, width and height of children
	switch (parent->layout) {
	case L_HORIZ:
		apply_horiz_layout(parent);
		break;
	case L_VERT:
		apply_vert_layout(parent);
		break;
	case L_TABBED:
	case L_STACKED:
		apply_tabbed_or_stacked_layout(parent);
		break;
	default:
		wlr_log(L_DEBUG, "TODO: arrange layout type %d", parent->layout);
		apply_horiz_layout(parent);
		break;
	}

	// Apply x, y, width and height to children and recurse if needed
	for (int i = 0; i < parent->children->length; ++i) {
		struct sway_container *child = parent->children->items[i];
		if (parent->has_gaps && !child->has_gaps) {
			child->has_gaps = true;
			child->gaps_inner = parent->gaps_inner;
			child->gaps_outer = parent->gaps_outer;
		}
		if (child->type == C_VIEW) {
			view_autoconfigure(child->sway_view);
		} else {
			arrange_children_of(child);
		}
	}

	// If container is a workspace, process floating containers too
	if (parent->type == C_WORKSPACE) {
		struct sway_workspace *ws = workspace->sway_workspace;
		for (int i = 0; i < ws->floating->children->length; ++i) {
			struct sway_container *child = ws->floating->children->items[i];
			if (child->type != C_VIEW) {
				arrange_children_of(child);
			}
		}
	}

	container_damage_whole(parent);
	update_debug_tree();
}