aboutsummaryrefslogtreecommitdiffstats
path: root/sway/layout.c
blob: affcb83e94525aef7f7daf34cb4be32a8f6d251c (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
337
338
339
340
#include <stdlib.h>
#include <stdbool.h>
#include <wlc/wlc.h>
#include "list.h"
#include "log.h"
#include "layout.h"

swayc_t root_container;

swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
	if (!container->children) {
		return NULL;
	}
	int i;
	for (i = 0; i < container->children->length; ++i) {
		swayc_t *child = container->children->items[i];
		if (test(child, data)) {
			return child;
		} else {
			swayc_t *_ = find_container(child, test, data);
			if (_) {
				return _;
			}
		}
	}
	return NULL;
}

void arrange_windows(swayc_t *container, int width, int height) {
	int i;
	if (width == -1 || height == -1) {
		sway_log(L_DEBUG, "Arranging layout for %p", container);
		width = container->width;
		height = container->height;
	}

	switch (container->type) {
	case C_ROOT:
		for (i = 0; i < container->children->length; ++i) {
			arrange_windows(container->children->items[i], -1, -1);
		}
		return;
	case C_VIEW:
		// If the view is fullscreen, we need to tell wlc to draw it as such
		if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) {
			swayc_t *parent = container;
			do {
				parent = parent->parent;
			} while(parent->type != C_OUTPUT);

			sway_log(L_DEBUG, "res %d %d", parent->width, parent->height);
			struct wlc_geometry geometry = {
				.origin = {
					.x = 0,
					.y = 0
				},
				.size = {
					.w = parent->width,
					.h = parent->height
				}
			};

			wlc_view_set_geometry(container->handle, &geometry);
			return;
		}

		sway_log(L_DEBUG, "Setting view to %d x %d @ %d, %d", width, height, container->x, container->y);
		struct wlc_geometry geometry = {
			.origin = {
				.x = container->x,
				.y = container->y
			},
			.size = {
				.w = width,
				.h = height
			}
		};
		wlc_view_set_geometry(container->handle, &geometry);
		container->width = width;
		container->height = height;
		return;
	default:
		container->width = width;
		container->height = height;
		break;
	}

	double total_weight = 0;
	for (i = 0; i < container->children->length; ++i) {
		swayc_t *child = container->children->items[i];
		total_weight += child->weight;
	}

	int x = 0, y = 0;
	switch (container->layout) {
	case L_HORIZ:
	default:
		sway_log(L_DEBUG, "Arranging %p horizontally", container);
		for (i = 0; i < container->children->length; ++i) {
			swayc_t *child = container->children->items[i];
			double percent = child->weight / total_weight;
			sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width);
			child->x = x + container->x;
			child->y = y + container->y;
			int w = width * percent;
			int h = height;
			arrange_windows(child, w, h);
			x += w;
		}
		break;
	case L_VERT:
		sway_log(L_DEBUG, "Arranging %p vertically", container);
		for (i = 0; i < container->children->length; ++i) {
			swayc_t *child = container->children->items[i];
			double percent = child->weight / total_weight;
			sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width);
			child->x = x + container->x;
			child->y = y + container->y;
			int w = width;
			int h = height * percent;
			arrange_windows(child, w, h);
			y += h;
		}
		break;
	}
}

void init_layout() {
	root_container.type = C_ROOT;
	root_container.layout = L_NONE;
	root_container.children = create_list();
	root_container.handle = -1;
}

void free_swayc(swayc_t *container) {
	// NOTE: Does not handle moving children into a different container
	list_free(container->children);
	if (container->name) {
		free(container->name);
	}
	free(container);
}

swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
	if (parent->children == NULL) {
		return NULL;
	}
	int i;
	for (i = 0; i < parent->children->length; ++i) {
		swayc_t *child = parent->children->items[i];
		if (child->handle == handle) {
			return child;
		} else {
			swayc_t *res;
			if ((res = get_swayc_for_handle(handle, child))) {
				return res;
			}
		}
	}
	return NULL;
}

swayc_t *get_focused_container(swayc_t *parent) {
	if (parent->focused == NULL) {
		return parent;
	}
	return get_focused_container(parent->focused);
}

void add_view(wlc_handle view_handle) {
	const uint32_t type = wlc_view_get_type(view_handle);
	const char *title = wlc_view_get_title(view_handle);
	if ((type & WLC_BIT_UNMANAGED) || (type & WLC_BIT_POPUP) || (type & WLC_BIT_MODAL) || (type & WLC_BIT_SPLASH)) {
		sway_log(L_DEBUG, "Leaving view %d:%s alone (unmanaged)", view_handle, title);
		return;
	}

	swayc_t *parent = get_focused_container(&root_container);
	sway_log(L_DEBUG, "Adding new view %d:%s under container %p %d", view_handle, title, parent, parent->type);

	while (parent->type == C_VIEW) {
		parent = parent->parent;
	}

	swayc_t *view = calloc(1, sizeof(swayc_t));
	view->weight = 1;
	view->layout = L_NONE;
	view->handle = view_handle;
	view->parent = parent;
	view->type = C_VIEW;
	view->name = malloc(strlen(title) + 1);
	strcpy(view->name, title);
	add_child(parent, view);

	unfocus_all(&root_container);
	focus_view(view);

	arrange_windows(parent, -1, -1);
}

int remove_container_from_parent(swayc_t *parent, swayc_t *container) {
	int i;
	for (i = 0; i < parent->children->length; ++i) {
		if (parent->children->items[i] == container) {
			list_del(parent->children, i);
			break;
		}
	}

	if (parent->focused == container) {
		parent->focused = NULL;
	}

	return i;
}

void destroy_view(swayc_t *view) {
	if (view == NULL) {
		sway_log(L_DEBUG, "Warning: NULL passed into destroy_view");
		return;
	}
	sway_log(L_DEBUG, "Destroying container %p", view);
	swayc_t *parent = view->parent;
	if (!parent) {
		return;
	}

	int i;
	for (i = 0; i < parent->children->length; ++i) {
		if (parent->children->items[i] == view) {
			list_del(parent->children, i);
			break;
		}
	}

	free_swayc(view);

	if (parent->focused == view) {
		parent->focused = NULL;
	}

	unfocus_all(&root_container);
	if (parent->children->length != 0) {
		focus_view(parent->children->items[0]);
	} else {
		focus_view(parent);
	}

	arrange_windows(parent, -1, -1);

	if (parent->children->length == 0 && parent->type == C_CONTAINER) {
		destroy_view(parent);
	}
}

void unfocus_all(swayc_t *container) {
	if (container->children == NULL) {
		return;
	}
	int i;
	for (i = 0; i < container->children->length; ++i) {
		swayc_t *view = container->children->items[i];
		if (view->type == C_VIEW) {
			wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, false);
		} else {
			unfocus_all(view);
		}
	}
}

void focus_view(swayc_t *view) {
	sway_log(L_DEBUG, "Setting focus for %p", view);
	if (view == &root_container) {
		// Propegate wayland focus down
		swayc_t *child = view->focused;
		while (child && child->type != C_VIEW) {
			child = child->focused;
		}
		if (child) {
			wlc_view_set_state(child->handle, WLC_BIT_ACTIVATED, true);
			wlc_view_focus(child->handle);
		}
		return;
	}
	view->parent->focused = view;
	focus_view(view->parent);
}

void add_child(swayc_t *parent, swayc_t *child) {
	sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type,
			child->width, child->height, parent, parent->type, parent->width, parent->height);
	list_add(parent->children, child);
}

swayc_t *create_container(swayc_t *parent, wlc_handle handle) {
	swayc_t *c = calloc(1, sizeof(swayc_t));
	c->weight = 1;
	c->handle = handle;
	c->parent = parent;
	c->layout = L_NONE;
	c->type = C_CONTAINER;
	c->children = create_list();
	return c;
}

void add_output(wlc_handle output) {
	sway_log(L_DEBUG, "Adding output %d", output);
	const struct wlc_size* size = wlc_output_get_resolution(output);

	swayc_t *container = create_container(&root_container, output);
	container->type = C_OUTPUT;
	container->width = size->w;
	container->height = size->h;
	add_child(&root_container, container);

	swayc_t *workspace = create_container(container, -1);
	workspace->type = C_WORKSPACE;
	workspace->width = size->w; // TODO: gaps
	workspace->height = size->h;
	workspace->layout = L_HORIZ; // TODO: Get default layout from config
	add_child(container, workspace);

	if (root_container.focused == NULL) {
		unfocus_all(&root_container);
		focus_view(workspace);
	}
}

void destroy_output(wlc_handle output) {
	sway_log(L_DEBUG, "Destroying output %d", output);
	int i;
	for (i = 0; i < root_container.children->length; ++i) {
		swayc_t *c = root_container.children->items[i];
		if (c->handle == output) {
			list_del(root_container.children, i);
			free_swayc(c);
			return;
		}
	}
}