1 #define _POSIX_C_SOURCE 200809L
2 #include <ctype.h>
3 #include <stdbool.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <wlr/types/wlr_output.h>
7 #include <wlr/types/wlr_output_layout.h>
8 #include "sway/tree/arrange.h"
9 #include "sway/tree/container.h"
10 #include "sway/output.h"
11 #include "sway/tree/workspace.h"
12 #include "sway/tree/view.h"
13 #include "list.h"
14 #include "log.h"
15 
apply_horiz_layout(list_t * children,struct wlr_box * parent)16 static void apply_horiz_layout(list_t *children, struct wlr_box *parent) {
17 	if (!children->length) {
18 		return;
19 	}
20 
21 	// Count the number of new windows we are resizing, and how much space
22 	// is currently occupied
23 	int new_children = 0;
24 	double current_width_fraction = 0;
25 	for (int i = 0; i < children->length; ++i) {
26 		struct sway_container *child = children->items[i];
27 		current_width_fraction += child->width_fraction;
28 		if (child->width_fraction <= 0) {
29 			new_children += 1;
30 		}
31 	}
32 
33 	// Calculate each height fraction
34 	double total_width_fraction = 0;
35 	for (int i = 0; i < children->length; ++i) {
36 		struct sway_container *child = children->items[i];
37 		if (child->width_fraction <= 0) {
38 			if (current_width_fraction <= 0) {
39 				child->width_fraction = 1.0;
40 			} else if (children->length > new_children) {
41 				child->width_fraction = current_width_fraction /
42 					(children->length - new_children);
43 			} else {
44 				child->width_fraction = current_width_fraction;
45 			}
46 		}
47 		total_width_fraction += child->width_fraction;
48 	}
49 	// Normalize width fractions so the sum is 1.0
50 	for (int i = 0; i < children->length; ++i) {
51 		struct sway_container *child = children->items[i];
52 		child->width_fraction /= total_width_fraction;
53 	}
54 
55 	// Calculate gap size
56 	double inner_gap = 0;
57 	struct sway_container *child = children->items[0];
58 	struct sway_workspace *ws = child->workspace;
59 	if (ws) {
60 		inner_gap = ws->gaps_inner;
61 	}
62 	// Descendants of tabbed/stacked containers don't have gaps
63 	struct sway_container *temp = child;
64 	while (temp) {
65 		enum sway_container_layout layout = container_parent_layout(temp);
66 		if (layout == L_TABBED || layout == L_STACKED) {
67 			inner_gap = 0;
68 		}
69 		temp = temp->parent;
70 	}
71 	double total_gap = fmin(inner_gap * (children->length - 1),
72 		fmax(0, parent->width - MIN_SANE_W * children->length));
73 	double child_total_width = parent->width - total_gap;
74 	inner_gap = floor(total_gap / (children->length - 1));
75 
76 	// Resize windows
77 	sway_log(SWAY_DEBUG, "Arranging %p horizontally", parent);
78 	double child_x = parent->x;
79 	for (int i = 0; i < children->length; ++i) {
80 		struct sway_container *child = children->items[i];
81 		child->child_total_width = child_total_width;
82 		child->x = child_x;
83 		child->y = parent->y;
84 		child->width = round(child->width_fraction * child_total_width);
85 		child->height = parent->height;
86 		child_x += child->width + inner_gap;
87 
88 		// Make last child use remaining width of parent
89 		if (i == children->length - 1) {
90 			child->width = parent->x + parent->width - child->x;
91 		}
92 	}
93 }
94 
apply_vert_layout(list_t * children,struct wlr_box * parent)95 static void apply_vert_layout(list_t *children, struct wlr_box *parent) {
96 	if (!children->length) {
97 		return;
98 	}
99 
100 	// Count the number of new windows we are resizing, and how much space
101 	// is currently occupied
102 	int new_children = 0;
103 	double current_height_fraction = 0;
104 	for (int i = 0; i < children->length; ++i) {
105 		struct sway_container *child = children->items[i];
106 		current_height_fraction += child->height_fraction;
107 		if (child->height_fraction <= 0) {
108 			new_children += 1;
109 		}
110 	}
111 
112 	// Calculate each height fraction
113 	double total_height_fraction = 0;
114 	for (int i = 0; i < children->length; ++i) {
115 		struct sway_container *child = children->items[i];
116 		if (child->height_fraction <= 0) {
117 			if (current_height_fraction <= 0) {
118 				child->height_fraction = 1.0;
119 			} else if (children->length > new_children) {
120 				child->height_fraction = current_height_fraction /
121 					(children->length - new_children);
122 			} else {
123 				child->height_fraction = current_height_fraction;
124 			}
125 		}
126 		total_height_fraction += child->height_fraction;
127 	}
128 	// Normalize height fractions so the sum is 1.0
129 	for (int i = 0; i < children->length; ++i) {
130 		struct sway_container *child = children->items[i];
131 		child->height_fraction /= total_height_fraction;
132 	}
133 
134 	// Calculate gap size
135 	double inner_gap = 0;
136 	struct sway_container *child = children->items[0];
137 	struct sway_workspace *ws = child->workspace;
138 	if (ws) {
139 		inner_gap = ws->gaps_inner;
140 	}
141 	// Descendants of tabbed/stacked containers don't have gaps
142 	struct sway_container *temp = child;
143 	while (temp) {
144 		enum sway_container_layout layout = container_parent_layout(temp);
145 		if (layout == L_TABBED || layout == L_STACKED) {
146 			inner_gap = 0;
147 		}
148 		temp = temp->parent;
149 	}
150 	double total_gap = fmin(inner_gap * (children->length - 1),
151 		fmax(0, parent->height - MIN_SANE_H * children->length));
152 	double child_total_height = parent->height - total_gap;
153 	inner_gap = floor(total_gap / (children->length - 1));
154 
155 	// Resize windows
156 	sway_log(SWAY_DEBUG, "Arranging %p vertically", parent);
157 	double child_y = parent->y;
158 	for (int i = 0; i < children->length; ++i) {
159 		struct sway_container *child = children->items[i];
160 		child->child_total_height = child_total_height;
161 		child->x = parent->x;
162 		child->y = child_y;
163 		child->width = parent->width;
164 		child->height = round(child->height_fraction * child_total_height);
165 		child_y += child->height + inner_gap;
166 
167 		// Make last child use remaining height of parent
168 		if (i == children->length - 1) {
169 			child->height = parent->y + parent->height - child->y;
170 		}
171 	}
172 }
173 
apply_tabbed_layout(list_t * children,struct wlr_box * parent)174 static void apply_tabbed_layout(list_t *children, struct wlr_box *parent) {
175 	if (!children->length) {
176 		return;
177 	}
178 	for (int i = 0; i < children->length; ++i) {
179 		struct sway_container *child = children->items[i];
180 		int parent_offset = child->view ? 0 : container_titlebar_height();
181 		child->x = parent->x;
182 		child->y = parent->y + parent_offset;
183 		child->width = parent->width;
184 		child->height = parent->height - parent_offset;
185 	}
186 }
187 
apply_stacked_layout(list_t * children,struct wlr_box * parent)188 static void apply_stacked_layout(list_t *children, struct wlr_box *parent) {
189 	if (!children->length) {
190 		return;
191 	}
192 	for (int i = 0; i < children->length; ++i) {
193 		struct sway_container *child = children->items[i];
194 		int parent_offset = child->view ?  0 :
195 			container_titlebar_height() * children->length;
196 		child->x = parent->x;
197 		child->y = parent->y + parent_offset;
198 		child->width = parent->width;
199 		child->height = parent->height - parent_offset;
200 	}
201 }
202 
arrange_floating(list_t * floating)203 static void arrange_floating(list_t *floating) {
204 	for (int i = 0; i < floating->length; ++i) {
205 		struct sway_container *floater = floating->items[i];
206 		arrange_container(floater);
207 	}
208 }
209 
arrange_children(list_t * children,enum sway_container_layout layout,struct wlr_box * parent)210 static void arrange_children(list_t *children,
211 		enum sway_container_layout layout, struct wlr_box *parent) {
212 	// Calculate x, y, width and height of children
213 	switch (layout) {
214 	case L_HORIZ:
215 		apply_horiz_layout(children, parent);
216 		break;
217 	case L_VERT:
218 		apply_vert_layout(children, parent);
219 		break;
220 	case L_TABBED:
221 		apply_tabbed_layout(children, parent);
222 		break;
223 	case L_STACKED:
224 		apply_stacked_layout(children, parent);
225 		break;
226 	case L_NONE:
227 		apply_horiz_layout(children, parent);
228 		break;
229 	}
230 
231 	// Recurse into child containers
232 	for (int i = 0; i < children->length; ++i) {
233 		struct sway_container *child = children->items[i];
234 		arrange_container(child);
235 	}
236 }
237 
arrange_container(struct sway_container * container)238 void arrange_container(struct sway_container *container) {
239 	if (config->reloading) {
240 		return;
241 	}
242 	if (container->view) {
243 		view_autoconfigure(container->view);
244 		node_set_dirty(&container->node);
245 		return;
246 	}
247 	struct wlr_box box;
248 	container_get_box(container, &box);
249 	arrange_children(container->children, container->layout, &box);
250 	node_set_dirty(&container->node);
251 }
252 
arrange_workspace(struct sway_workspace * workspace)253 void arrange_workspace(struct sway_workspace *workspace) {
254 	if (config->reloading) {
255 		return;
256 	}
257 	if (!workspace->output) {
258 		// Happens when there are no outputs connected
259 		return;
260 	}
261 	struct sway_output *output = workspace->output;
262 	struct wlr_box *area = &output->usable_area;
263 	sway_log(SWAY_DEBUG, "Usable area for ws: %dx%d@%d,%d",
264 			area->width, area->height, area->x, area->y);
265 
266 	bool first_arrange = workspace->width == 0 && workspace->height == 0;
267 	double prev_x = workspace->x - workspace->current_gaps.left;
268 	double prev_y = workspace->y - workspace->current_gaps.top;
269 	workspace->width = area->width;
270 	workspace->height = area->height;
271 	workspace->x = output->lx + area->x;
272 	workspace->y = output->ly + area->y;
273 
274 	// Adjust any floating containers
275 	double diff_x = workspace->x - prev_x;
276 	double diff_y = workspace->y - prev_y;
277 	if (!first_arrange && (diff_x != 0 || diff_y != 0)) {
278 		for (int i = 0; i < workspace->floating->length; ++i) {
279 			struct sway_container *floater = workspace->floating->items[i];
280 			container_floating_translate(floater, diff_x, diff_y);
281 			double center_x = floater->x + floater->width / 2;
282 			double center_y = floater->y + floater->height / 2;
283 			struct wlr_box workspace_box;
284 			workspace_get_box(workspace, &workspace_box);
285 			if (!wlr_box_contains_point(&workspace_box, center_x, center_y)) {
286 				container_floating_move_to_center(floater);
287 			}
288 		}
289 	}
290 
291 	workspace_add_gaps(workspace);
292 	node_set_dirty(&workspace->node);
293 	sway_log(SWAY_DEBUG, "Arranging workspace '%s' at %f, %f", workspace->name,
294 			workspace->x, workspace->y);
295 	if (workspace->fullscreen) {
296 		struct sway_container *fs = workspace->fullscreen;
297 		fs->x = output->lx;
298 		fs->y = output->ly;
299 		fs->width = output->width;
300 		fs->height = output->height;
301 		arrange_container(fs);
302 	} else {
303 		struct wlr_box box;
304 		workspace_get_box(workspace, &box);
305 		arrange_children(workspace->tiling, workspace->layout, &box);
306 		arrange_floating(workspace->floating);
307 	}
308 }
309 
arrange_output(struct sway_output * output)310 void arrange_output(struct sway_output *output) {
311 	if (config->reloading) {
312 		return;
313 	}
314 	const struct wlr_box *output_box = wlr_output_layout_get_box(
315 			root->output_layout, output->wlr_output);
316 	output->lx = output_box->x;
317 	output->ly = output_box->y;
318 	output->width = output_box->width;
319 	output->height = output_box->height;
320 
321 	for (int i = 0; i < output->workspaces->length; ++i) {
322 		struct sway_workspace *workspace = output->workspaces->items[i];
323 		arrange_workspace(workspace);
324 	}
325 }
326 
arrange_root(void)327 void arrange_root(void) {
328 	if (config->reloading) {
329 		return;
330 	}
331 	const struct wlr_box *layout_box =
332 		wlr_output_layout_get_box(root->output_layout, NULL);
333 	root->x = layout_box->x;
334 	root->y = layout_box->y;
335 	root->width = layout_box->width;
336 	root->height = layout_box->height;
337 
338 	if (root->fullscreen_global) {
339 		struct sway_container *fs = root->fullscreen_global;
340 		fs->x = root->x;
341 		fs->y = root->y;
342 		fs->width = root->width;
343 		fs->height = root->height;
344 		arrange_container(fs);
345 	} else {
346 		for (int i = 0; i < root->outputs->length; ++i) {
347 			struct sway_output *output = root->outputs->items[i];
348 			arrange_output(output);
349 		}
350 	}
351 }
352 
arrange_node(struct sway_node * node)353 void arrange_node(struct sway_node *node) {
354 	switch (node->type) {
355 	case N_ROOT:
356 		arrange_root();
357 		break;
358 	case N_OUTPUT:
359 		arrange_output(node->sway_output);
360 		break;
361 	case N_WORKSPACE:
362 		arrange_workspace(node->sway_workspace);
363 		break;
364 	case N_CONTAINER:
365 		arrange_container(node->sway_container);
366 		break;
367 	}
368 }
369