aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/arrange.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-04-28 11:26:14 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-04-28 11:26:14 +1000
commit167c2e1aa99aa8011e169b0fb61c47953cc9a5f6 (patch)
treef312cac40cd542964f62d8b7ed53d44bb13dea37 /sway/tree/arrange.c
parentMerge pull request #1866 from ggreer/swaybar-cmd-sh (diff)
downloadsway-167c2e1aa99aa8011e169b0fb61c47953cc9a5f6.tar.gz
sway-167c2e1aa99aa8011e169b0fb61c47953cc9a5f6.tar.zst
sway-167c2e1aa99aa8011e169b0fb61c47953cc9a5f6.zip
Refactor arrange_windows()
Replaces arrange_windows() with arrange_root(), arrange_output(), arrange_workspace() and arrange_children_of(). Also makes fullscreen views save and restore their dimensions, which allows it to preserve any custom resize and is also a requirement for floating views once they are implemented.
Diffstat (limited to 'sway/tree/arrange.c')
-rw-r--r--sway/tree/arrange.c239
1 files changed, 239 insertions, 0 deletions
diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c
new file mode 100644
index 00000000..dd87ce7c
--- /dev/null
+++ b/sway/tree/arrange.c
@@ -0,0 +1,239 @@
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/debug.h"
9#include "sway/tree/arrange.h"
10#include "sway/tree/container.h"
11#include "sway/tree/layout.h"
12#include "sway/output.h"
13#include "sway/tree/workspace.h"
14#include "sway/tree/view.h"
15#include "list.h"
16#include "log.h"
17
18struct sway_container root_container;
19
20void arrange_windows(struct sway_container *container) {
21 switch (container->type) {
22 case C_ROOT:
23 arrange_root();
24 break;
25 case C_OUTPUT:
26 arrange_output(container);
27 break;
28 case C_WORKSPACE:
29 arrange_workspace(container);
30 break;
31 case C_CONTAINER:
32 arrange_children_of(container);
33 break;
34 case C_VIEW:
35 // ignore
36 break;
37 case C_TYPES:
38 sway_assert(
39 false, "Called arrange_windows() with container type C_TYPES");
40 break;
41 }
42}
43
44void arrange_root() {
45 if (config->reloading) {
46 return;
47 }
48 struct wlr_output_layout *output_layout =
49 root_container.sway_root->output_layout;
50 const struct wlr_box *layout_box =
51 wlr_output_layout_get_box(output_layout, NULL);
52 root_container.x = layout_box->x;
53 root_container.y = layout_box->y;
54 root_container.width = layout_box->width;
55 root_container.height = layout_box->height;
56 for (int i = 0; i < root_container.children->length; ++i) {
57 struct sway_container *output = root_container.children->items[i];
58 arrange_output(output);
59 }
60}
61
62void arrange_output(struct sway_container *output) {
63 if (config->reloading) {
64 return;
65 }
66 if (!sway_assert(output->type == C_OUTPUT,
67 "called arrange_output() on non-output container")) {
68 return;
69 }
70 const struct wlr_box *output_box = wlr_output_layout_get_box(
71 root_container.sway_root->output_layout,
72 output->sway_output->wlr_output);
73 output->x = output_box->x;
74 output->y = output_box->y;
75 output->width = output_box->width;
76 output->height = output_box->height;
77 wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f",
78 output->name, output->x, output->y);
79 for (int i = 0; i < output->children->length; ++i) {
80 struct sway_container *workspace = output->children->items[i];
81 arrange_workspace(workspace);
82 }
83 container_damage_whole(output);
84}
85
86void arrange_workspace(struct sway_container *workspace) {
87 if (config->reloading) {
88 return;
89 }
90 if (!sway_assert(workspace->type == C_WORKSPACE,
91 "called arrange_workspace() on non-workspace container")) {
92 return;
93 }
94 struct sway_container *output = workspace->parent;
95 struct wlr_box *area = &output->sway_output->usable_area;
96 wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d",
97 area->width, area->height, area->x, area->y);
98 workspace->width = area->width;
99 workspace->height = area->height;
100 workspace->x = area->x;
101 workspace->y = area->y;
102 wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f",
103 workspace->name, workspace->x, workspace->y);
104 arrange_children_of(workspace);
105 container_damage_whole(workspace);
106}
107
108static void apply_horiz_layout(struct sway_container *parent) {
109 size_t num_children = parent->children->length;
110 if (!num_children) {
111 return;
112 }
113 // Calculate total width of children
114 double total_width = 0;
115 for (size_t i = 0; i < num_children; ++i) {
116 struct sway_container *child = parent->children->items[i];
117 if (child->width <= 0) {
118 if (num_children > 1) {
119 child->width = parent->width / (num_children - 1);
120 } else {
121 child->width = parent->width;
122 }
123 }
124 total_width += child->width;
125 }
126 double scale = parent->width / total_width;
127
128 // Resize windows
129 wlr_log(L_DEBUG, "Arranging %p horizontally", parent);
130 double child_x = parent->x;
131 struct sway_container *child;
132 for (size_t i = 0; i < num_children; ++i) {
133 child = parent->children->items[i];
134 wlr_log(L_DEBUG,
135 "Calculating arrangement for %p:%d (will scale %f by %f)",
136 child, child->type, child->width, scale);
137 child->x = child_x;
138 child->y = parent->y;
139 child->width = floor(child->width * scale);
140 child->height = parent->height;
141 child_x += child->width;
142 }
143 // Make last child use remaining width of parent
144 child->width = parent->x + parent->width - child->x;
145}
146
147static void apply_vert_layout(struct sway_container *parent) {
148 size_t num_children = parent->children->length;
149 if (!num_children) {
150 return;
151 }
152 // Calculate total height of children
153 double total_height = 0;
154 for (size_t i = 0; i < num_children; ++i) {
155 struct sway_container *child = parent->children->items[i];
156 if (child->height <= 0) {
157 if (num_children > 1) {
158 child->height = parent->height / (num_children - 1);
159 } else {
160 child->height = parent->height;
161 }
162 }
163 total_height += child->height;
164 }
165 double scale = parent->height / total_height;
166
167 // Resize
168 wlr_log(L_DEBUG, "Arranging %p vertically", parent);
169 double child_y = parent->y;
170 struct sway_container *child;
171 for (size_t i = 0; i < num_children; ++i) {
172 child = parent->children->items[i];
173 wlr_log(L_DEBUG,
174 "Calculating arrangement for %p:%d (will scale %f by %f)",
175 child, child->type, child->height, scale);
176 child->x = parent->x;
177 child->y = child_y;
178 child->width = parent->width;
179 child->height = floor(child->height * scale);
180 child_y += child->height;
181 }
182 // Make last child use remaining height of parent
183 child->height = parent->y + parent->height - child->y;
184}
185
186void arrange_children_of(struct sway_container *parent) {
187 if (config->reloading) {
188 return;
189 }
190 if (!sway_assert(parent->type == C_WORKSPACE || parent->type == C_CONTAINER,
191 "container is a %s", container_type_to_str(parent->type))) {
192 return;
193 }
194
195 struct sway_container *workspace = parent;
196 if (workspace->type != C_WORKSPACE) {
197 workspace = container_parent(workspace, C_WORKSPACE);
198 }
199 if (workspace->sway_workspace->fullscreen) {
200 // Just arrange the fullscreen view and jump out
201 struct sway_container *view =
202 workspace->sway_workspace->fullscreen->swayc;
203 view_configure(view->sway_view, 0, 0,
204 workspace->parent->width, workspace->parent->height);
205 view->width = workspace->parent->width;
206 view->height = workspace->parent->height;
207 return;
208 }
209
210 wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent,
211 parent->name, parent->width, parent->height, parent->x, parent->y);
212
213 // Calculate x, y, width and height of children
214 switch (parent->layout) {
215 case L_HORIZ:
216 apply_horiz_layout(parent);
217 break;
218 case L_VERT:
219 apply_vert_layout(parent);
220 break;
221 default:
222 wlr_log(L_DEBUG, "TODO: arrange layout type %d", parent->layout);
223 apply_horiz_layout(parent);
224 break;
225 }
226
227 // Apply x, y, width and height to children and recurse if needed
228 for (int i = 0; i < parent->children->length; ++i) {
229 struct sway_container *child = parent->children->items[i];
230 if (child->type == C_VIEW) {
231 view_configure(child->sway_view, child->x, child->y,
232 child->width, child->height);
233 } else {
234 arrange_children_of(child);
235 }
236 }
237 container_damage_whole(parent);
238 update_debug_tree();
239}