summaryrefslogtreecommitdiffstats
path: root/sway/commands/resize.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r--sway/commands/resize.c343
1 files changed, 342 insertions, 1 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c
index 90ab824f..2c5b3f6b 100644
--- a/sway/commands/resize.c
+++ b/sway/commands/resize.c
@@ -1,11 +1,352 @@
1#include <errno.h> 1#include <errno.h>
2#include <math.h>
2#include <stdbool.h> 3#include <stdbool.h>
3#include <stdlib.h> 4#include <stdlib.h>
4#include <string.h> 5#include <string.h>
6#include <wlc/wlc.h>
5#include "sway/commands.h" 7#include "sway/commands.h"
6#include "sway/resize.h" 8#include "sway/layout.h"
9#include "sway/focus.h"
10#include "sway/input_state.h"
11#include "sway/handlers.h"
7#include "log.h" 12#include "log.h"
8 13
14enum resize_dim_types {
15 RESIZE_DIM_PX,
16 RESIZE_DIM_PPT,
17 RESIZE_DIM_DEFAULT,
18};
19
20static bool set_size_floating(int new_dimension, bool use_width) {
21 swayc_t *view = get_focused_float(swayc_active_workspace());
22 if (view) {
23 if (use_width) {
24 int current_width = view->width;
25 view->desired_width = new_dimension;
26 floating_view_sane_size(view);
27
28 int new_x = view->x + (int)(((view->desired_width - current_width) / 2) * -1);
29 view->width = view->desired_width;
30 view->x = new_x;
31
32 update_geometry(view);
33 } else {
34 int current_height = view->height;
35 view->desired_height = new_dimension;
36 floating_view_sane_size(view);
37
38 int new_y = view->y + (int)(((view->desired_height - current_height) / 2) * -1);
39 view->height = view->desired_height;
40 view->y = new_y;
41
42 update_geometry(view);
43 }
44
45 return true;
46 }
47
48 return false;
49}
50
51static bool resize_floating(int amount, bool use_width) {
52 swayc_t *view = get_focused_float(swayc_active_workspace());
53
54 if (view) {
55 if (use_width) {
56 return set_size_floating(view->width + amount, true);
57 } else {
58 return set_size_floating(view->height + amount, false);
59 }
60 }
61
62 return false;
63}
64
65static bool resize_tiled(int amount, bool use_width) {
66 swayc_t *parent = get_focused_view(swayc_active_workspace());
67 swayc_t *focused = parent;
68 swayc_t *sibling;
69 if (!parent) {
70 return true;
71 }
72 // Find the closest parent container which has siblings of the proper layout.
73 // Then apply the resize to all of them.
74 int i;
75 if (use_width) {
76 int lnumber = 0;
77 int rnumber = 0;
78 while (parent->parent) {
79 if (parent->parent->layout == L_HORIZ && parent->parent->children) {
80 for (i = 0; i < parent->parent->children->length; i++) {
81 sibling = parent->parent->children->items[i];
82 if (sibling->x != focused->x) {
83 if (sibling->x < parent->x) {
84 lnumber++;
85 } else if (sibling->x > parent->x) {
86 rnumber++;
87 }
88 }
89 }
90 if (rnumber || lnumber) {
91 break;
92 }
93 }
94 parent = parent->parent;
95 }
96 if (parent == &root_container) {
97 return true;
98 }
99 sway_log(L_DEBUG, "Found the proper parent: %p. It has %d l conts, and %d r conts", parent->parent, lnumber, rnumber);
100 //TODO: Ensure rounding is done in such a way that there are NO pixel leaks
101 bool valid = true;
102 for (i = 0; i < parent->parent->children->length; i++) {
103 sibling = parent->parent->children->items[i];
104 if (sibling->x != focused->x) {
105 if (sibling->x < parent->x) {
106 double pixels = -1 * amount;
107 pixels /= lnumber;
108 if (rnumber) {
109 if ((sibling->width + pixels/2) < min_sane_w) {
110 valid = false;
111 break;
112 }
113 } else {
114 if ((sibling->width + pixels) < min_sane_w) {
115 valid = false;
116 break;
117 }
118 }
119 } else if (sibling->x > parent->x) {
120 double pixels = -1 * amount;
121 pixels /= rnumber;
122 if (lnumber) {
123 if ((sibling->width + pixels/2) < min_sane_w) {
124 valid = false;
125 break;
126 }
127 } else {
128 if ((sibling->width + pixels) < min_sane_w) {
129 valid = false;
130 break;
131 }
132 }
133 }
134 } else {
135 double pixels = amount;
136 if (parent->width + pixels < min_sane_w) {
137 valid = false;
138 break;
139 }
140 }
141 }
142 if (valid) {
143 for (i = 0; i < parent->parent->children->length; i++) {
144 sibling = parent->parent->children->items[i];
145 if (sibling->x != focused->x) {
146 if (sibling->x < parent->x) {
147 double pixels = -1 * amount;
148 pixels /= lnumber;
149 if (rnumber) {
150 recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_RIGHT);
151 } else {
152 recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_RIGHT);
153 }
154 } else if (sibling->x > parent->x) {
155 double pixels = -1 * amount;
156 pixels /= rnumber;
157 if (lnumber) {
158 recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_LEFT);
159 } else {
160 recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_LEFT);
161 }
162 }
163 } else {
164 if (rnumber != 0 && lnumber != 0) {
165 double pixels = amount;
166 pixels /= 2;
167 recursive_resize(parent, pixels, WLC_RESIZE_EDGE_LEFT);
168 recursive_resize(parent, pixels, WLC_RESIZE_EDGE_RIGHT);
169 } else if (rnumber) {
170 recursive_resize(parent, amount, WLC_RESIZE_EDGE_RIGHT);
171 } else if (lnumber) {
172 recursive_resize(parent, amount, WLC_RESIZE_EDGE_LEFT);
173 }
174 }
175 }
176 // Recursive resize does not handle positions, let arrange_windows
177 // take care of that.
178 arrange_windows(swayc_active_workspace(), -1, -1);
179 }
180 return true;
181 } else {
182 int tnumber = 0;
183 int bnumber = 0;
184 while (parent->parent) {
185 if (parent->parent->layout == L_VERT) {
186 for (i = 0; i < parent->parent->children->length; i++) {
187 sibling = parent->parent->children->items[i];
188 if (sibling->y != focused->y) {
189 if (sibling->y < parent->y) {
190 bnumber++;
191 } else if (sibling->y > parent->y) {
192 tnumber++;
193 }
194 }
195 }
196 if (bnumber || tnumber) {
197 break;
198 }
199 }
200 parent = parent->parent;
201 }
202 if (parent->parent == NULL || parent->parent->children == NULL) {
203 return true;
204 }
205 sway_log(L_DEBUG, "Found the proper parent: %p. It has %d b conts, and %d t conts", parent->parent, bnumber, tnumber);
206 //TODO: Ensure rounding is done in such a way that there are NO pixel leaks
207 bool valid = true;
208 for (i = 0; i < parent->parent->children->length; i++) {
209 sibling = parent->parent->children->items[i];
210 if (sibling->y != focused->y) {
211 if (sibling->y < parent->y) {
212 double pixels = -1 * amount;
213 pixels /= bnumber;
214 if (tnumber) {
215 if ((sibling->height + pixels/2) < min_sane_h) {
216 valid = false;
217 break;
218 }
219 } else {
220 if ((sibling->height + pixels) < min_sane_h) {
221 valid = false;
222 break;
223 }
224 }
225 } else if (sibling->y > parent->y) {
226 double pixels = -1 * amount;
227 pixels /= tnumber;
228 if (bnumber) {
229 if ((sibling->height + pixels/2) < min_sane_h) {
230 valid = false;
231 break;
232 }
233 } else {
234 if ((sibling->height + pixels) < min_sane_h) {
235 valid = false;
236 break;
237 }
238 }
239 }
240 } else {
241 double pixels = amount;
242 if (parent->height + pixels < min_sane_h) {
243 valid = false;
244 break;
245 }
246 }
247 }
248 if (valid) {
249 for (i = 0; i < parent->parent->children->length; i++) {
250 sibling = parent->parent->children->items[i];
251 if (sibling->y != focused->y) {
252 if (sibling->y < parent->y) {
253 double pixels = -1 * amount;
254 pixels /= bnumber;
255 if (tnumber) {
256 recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_BOTTOM);
257 } else {
258 recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_BOTTOM);
259 }
260 } else if (sibling->x > parent->x) {
261 double pixels = -1 * amount;
262 pixels /= tnumber;
263 if (bnumber) {
264 recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_TOP);
265 } else {
266 recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_TOP);
267 }
268 }
269 } else {
270 if (bnumber != 0 && tnumber != 0) {
271 double pixels = amount/2;
272 recursive_resize(parent, pixels, WLC_RESIZE_EDGE_TOP);
273 recursive_resize(parent, pixels, WLC_RESIZE_EDGE_BOTTOM);
274 } else if (tnumber) {
275 recursive_resize(parent, amount, WLC_RESIZE_EDGE_TOP);
276 } else if (bnumber) {
277 recursive_resize(parent, amount, WLC_RESIZE_EDGE_BOTTOM);
278 }
279 }
280 }
281 arrange_windows(swayc_active_workspace(), -1, -1);
282 }
283 return true;
284 }
285 return true;
286}
287
288static bool set_size_tiled(int amount, bool use_width) {
289 int desired;
290 swayc_t *focused = get_focused_view(swayc_active_workspace());
291
292 if (use_width) {
293 desired = amount - focused->width;
294 } else {
295 desired = amount - focused->height;
296 }
297
298 return resize_tiled(desired, use_width);
299}
300
301static bool set_size(int dimension, bool use_width) {
302 swayc_t *focused = get_focused_view_include_floating(swayc_active_workspace());
303
304 if (focused) {
305 if (focused->is_floating) {
306 return set_size_floating(dimension, use_width);
307 } else {
308 return set_size_tiled(dimension, use_width);
309 }
310 }
311
312 return false;
313}
314
315static bool resize(int dimension, bool use_width, enum resize_dim_types dim_type) {
316 swayc_t *focused = get_focused_view_include_floating(swayc_active_workspace());
317
318 // translate "10 ppt" (10%) to appropriate # of pixels in case we need it
319 float ppt_dim = (float)dimension / 100;
320
321 if (use_width) {
322 ppt_dim = focused->width * ppt_dim;
323 } else {
324 ppt_dim = focused->height * ppt_dim;
325 }
326
327 if (focused) {
328 if (focused->is_floating) {
329 // floating view resize dimensions should default to px, so only
330 // use ppt if specified
331 if (dim_type == RESIZE_DIM_PPT) {
332 dimension = (int)ppt_dim;
333 }
334
335 return resize_floating(dimension, use_width);
336 } else {
337 // tiled view resize dimensions should default to ppt, so only use
338 // px if specified
339 if (dim_type != RESIZE_DIM_PX) {
340 dimension = (int)ppt_dim;
341 }
342
343 return resize_tiled(dimension, use_width);
344 }
345 }
346
347 return false;
348}
349
9static struct cmd_results *cmd_resize_set(int argc, char **argv) { 350static struct cmd_results *cmd_resize_set(int argc, char **argv) {
10 struct cmd_results *error = NULL; 351 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) { 352 if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) {