diff options
author | 2018-04-07 11:19:25 -0400 | |
---|---|---|
committer | 2018-04-07 11:19:25 -0400 | |
commit | f5a9bd5cb75b37376b98eadbff2facb7e0021d57 (patch) | |
tree | f72219a5a2adec3380da5269fee025715f7bf333 /sway/tree | |
parent | Merge pull request #1762 from DanySpin97/wlroots (diff) | |
parent | Don't rejigger if parent has two children (diff) | |
download | sway-f5a9bd5cb75b37376b98eadbff2facb7e0021d57.tar.gz sway-f5a9bd5cb75b37376b98eadbff2facb7e0021d57.tar.zst sway-f5a9bd5cb75b37376b98eadbff2facb7e0021d57.zip |
Merge pull request #1700 from swaywm/move-cmd-full
Implement move [left|right|up|down]
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/container.c | 46 | ||||
-rw-r--r-- | sway/tree/layout.c | 342 |
2 files changed, 343 insertions, 45 deletions
diff --git a/sway/tree/container.c b/sway/tree/container.c index ab8363bc..ea1c93bb 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c | |||
@@ -208,7 +208,7 @@ static void container_root_finish(struct sway_container *con) { | |||
208 | wlr_log(L_ERROR, "TODO: destroy the root container"); | 208 | wlr_log(L_ERROR, "TODO: destroy the root container"); |
209 | } | 209 | } |
210 | 210 | ||
211 | static bool container_reap_empty(struct sway_container *con) { | 211 | bool container_reap_empty(struct sway_container *con) { |
212 | switch (con->type) { | 212 | switch (con->type) { |
213 | case C_ROOT: | 213 | case C_ROOT: |
214 | case C_OUTPUT: | 214 | case C_OUTPUT: |
@@ -225,14 +225,6 @@ static bool container_reap_empty(struct sway_container *con) { | |||
225 | if (con->children->length == 0) { | 225 | if (con->children->length == 0) { |
226 | _container_destroy(con); | 226 | _container_destroy(con); |
227 | return true; | 227 | return true; |
228 | } else if (con->children->length == 1) { | ||
229 | struct sway_container *child = con->children->items[0]; | ||
230 | if (child->type == C_CONTAINER) { | ||
231 | container_remove_child(child); | ||
232 | container_replace_child(con, child); | ||
233 | _container_destroy(con); | ||
234 | return true; | ||
235 | } | ||
236 | } | 228 | } |
237 | case C_VIEW: | 229 | case C_VIEW: |
238 | break; | 230 | break; |
@@ -245,6 +237,29 @@ static bool container_reap_empty(struct sway_container *con) { | |||
245 | return false; | 237 | return false; |
246 | } | 238 | } |
247 | 239 | ||
240 | struct sway_container *container_reap_empty_recursive( | ||
241 | struct sway_container *con) { | ||
242 | while (con) { | ||
243 | struct sway_container *next = con->parent; | ||
244 | if (!container_reap_empty(con)) { | ||
245 | break; | ||
246 | } | ||
247 | con = next; | ||
248 | } | ||
249 | return con; | ||
250 | } | ||
251 | |||
252 | struct sway_container *container_flatten(struct sway_container *container) { | ||
253 | while (container->type == C_CONTAINER && container->children->length == 1) { | ||
254 | struct sway_container *child = container->children->items[0]; | ||
255 | struct sway_container *parent = container->parent; | ||
256 | container_replace_child(container, child); | ||
257 | container_destroy(container); | ||
258 | container = parent; | ||
259 | } | ||
260 | return container; | ||
261 | } | ||
262 | |||
248 | struct sway_container *container_destroy(struct sway_container *con) { | 263 | struct sway_container *container_destroy(struct sway_container *con) { |
249 | if (con == NULL) { | 264 | if (con == NULL) { |
250 | return NULL; | 265 | return NULL; |
@@ -283,18 +298,7 @@ struct sway_container *container_destroy(struct sway_container *con) { | |||
283 | break; | 298 | break; |
284 | } | 299 | } |
285 | 300 | ||
286 | struct sway_container *tmp = parent; | 301 | return container_reap_empty_recursive(parent); |
287 | while (parent) { | ||
288 | tmp = parent->parent; | ||
289 | |||
290 | if (!container_reap_empty(parent)) { | ||
291 | break; | ||
292 | } | ||
293 | |||
294 | parent = tmp; | ||
295 | } | ||
296 | |||
297 | return tmp; | ||
298 | } | 302 | } |
299 | 303 | ||
300 | static void container_close_func(struct sway_container *container, void *data) { | 304 | static void container_close_func(struct sway_container *container, void *data) { |
diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 343f349a..78af8b8c 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c | |||
@@ -6,6 +6,7 @@ | |||
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <wlr/types/wlr_output.h> | 7 | #include <wlr/types/wlr_output.h> |
8 | #include <wlr/types/wlr_output_layout.h> | 8 | #include <wlr/types/wlr_output_layout.h> |
9 | #include "sway/debug.h" | ||
9 | #include "sway/tree/container.h" | 10 | #include "sway/tree/container.h" |
10 | #include "sway/tree/layout.h" | 11 | #include "sway/tree/layout.h" |
11 | #include "sway/output.h" | 12 | #include "sway/output.h" |
@@ -100,13 +101,31 @@ static int index_child(const struct sway_container *child) { | |||
100 | return i; | 101 | return i; |
101 | } | 102 | } |
102 | 103 | ||
104 | void container_insert_child(struct sway_container *parent, | ||
105 | struct sway_container *child, int i) { | ||
106 | struct sway_container *old_parent = child->parent; | ||
107 | if (old_parent) { | ||
108 | container_remove_child(child); | ||
109 | } | ||
110 | wlr_log(L_DEBUG, "Inserting id:%zd at index %d", child->id, i); | ||
111 | list_insert(parent->children, i, child); | ||
112 | child->parent = parent; | ||
113 | wl_signal_emit(&child->events.reparent, old_parent); | ||
114 | } | ||
115 | |||
103 | struct sway_container *container_add_sibling(struct sway_container *fixed, | 116 | struct sway_container *container_add_sibling(struct sway_container *fixed, |
104 | struct sway_container *active) { | 117 | struct sway_container *active) { |
105 | // TODO handle floating | 118 | // TODO handle floating |
119 | struct sway_container *old_parent = NULL; | ||
120 | if (active->parent) { | ||
121 | old_parent = active->parent; | ||
122 | container_remove_child(active); | ||
123 | } | ||
106 | struct sway_container *parent = fixed->parent; | 124 | struct sway_container *parent = fixed->parent; |
107 | int i = index_child(fixed); | 125 | int i = index_child(fixed); |
108 | list_insert(parent->children, i + 1, active); | 126 | list_insert(parent->children, i + 1, active); |
109 | active->parent = parent; | 127 | active->parent = parent; |
128 | wl_signal_emit(&active->events.reparent, old_parent); | ||
110 | return active->parent; | 129 | return active->parent; |
111 | } | 130 | } |
112 | 131 | ||
@@ -166,9 +185,290 @@ void container_move_to(struct sway_container *container, | |||
166 | arrange_windows(new_parent, -1, -1); | 185 | arrange_windows(new_parent, -1, -1); |
167 | } | 186 | } |
168 | 187 | ||
188 | static bool sway_dir_to_wlr(enum movement_direction dir, | ||
189 | enum wlr_direction *out) { | ||
190 | switch (dir) { | ||
191 | case MOVE_UP: | ||
192 | *out = WLR_DIRECTION_UP; | ||
193 | break; | ||
194 | case MOVE_DOWN: | ||
195 | *out = WLR_DIRECTION_DOWN; | ||
196 | break; | ||
197 | case MOVE_LEFT: | ||
198 | *out = WLR_DIRECTION_LEFT; | ||
199 | break; | ||
200 | case MOVE_RIGHT: | ||
201 | *out = WLR_DIRECTION_RIGHT; | ||
202 | break; | ||
203 | default: | ||
204 | return false; | ||
205 | } | ||
206 | |||
207 | return true; | ||
208 | } | ||
209 | |||
210 | static bool is_parallel(enum sway_container_layout layout, | ||
211 | enum movement_direction dir) { | ||
212 | switch (layout) { | ||
213 | case L_TABBED: | ||
214 | case L_STACKED: | ||
215 | case L_HORIZ: | ||
216 | return dir == MOVE_LEFT || dir == MOVE_RIGHT; | ||
217 | case L_VERT: | ||
218 | return dir == MOVE_UP || dir == MOVE_DOWN; | ||
219 | default: | ||
220 | return false; | ||
221 | } | ||
222 | } | ||
223 | |||
224 | static enum movement_direction invert_movement(enum movement_direction dir) { | ||
225 | switch (dir) { | ||
226 | case MOVE_LEFT: | ||
227 | return MOVE_RIGHT; | ||
228 | case MOVE_RIGHT: | ||
229 | return MOVE_LEFT; | ||
230 | case MOVE_UP: | ||
231 | return MOVE_DOWN; | ||
232 | case MOVE_DOWN: | ||
233 | return MOVE_UP; | ||
234 | default: | ||
235 | sway_assert(0, "This function expects left|right|up|down"); | ||
236 | return MOVE_LEFT; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | static int move_offs(enum movement_direction move_dir) { | ||
241 | return move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1; | ||
242 | } | ||
243 | |||
244 | /* Gets the index of the most extreme member based on the movement offset */ | ||
245 | static int container_limit(struct sway_container *container, | ||
246 | enum movement_direction move_dir) { | ||
247 | return move_offs(move_dir) < 0 ? 0 : container->children->length; | ||
248 | } | ||
249 | |||
250 | /* Takes one child, sets it aside, wraps the rest of the children in a new | ||
251 | * container, switches the layout of the workspace, and drops the child back in. | ||
252 | * In other words, rejigger it. */ | ||
253 | static void workspace_rejigger(struct sway_container *ws, | ||
254 | struct sway_container *child, enum movement_direction move_dir) { | ||
255 | struct sway_container *original_parent = child->parent; | ||
256 | struct sway_container *new_parent = | ||
257 | container_split(ws, ws->layout); | ||
258 | |||
259 | container_remove_child(child); | ||
260 | for (int i = 0; i < ws->children->length; ++i) { | ||
261 | struct sway_container *_child = ws->children->items[i]; | ||
262 | container_move_to(new_parent, _child); | ||
263 | } | ||
264 | |||
265 | int index = move_offs(move_dir); | ||
266 | container_insert_child(ws, child, index < 0 ? 0 : 1); | ||
267 | container_set_layout(ws, | ||
268 | move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT); | ||
269 | |||
270 | container_flatten(ws); | ||
271 | container_reap_empty_recursive(original_parent); | ||
272 | wl_signal_emit(&child->events.reparent, original_parent); | ||
273 | arrange_windows(ws, -1, -1); | ||
274 | } | ||
275 | |||
169 | void container_move(struct sway_container *container, | 276 | void container_move(struct sway_container *container, |
170 | enum movement_direction dir, int move_amt) { | 277 | enum movement_direction move_dir, int move_amt) { |
171 | // TODO | 278 | if (!sway_assert( |
279 | container->type != C_CONTAINER || container->type != C_VIEW, | ||
280 | "Can only move containers and views")) { | ||
281 | return; | ||
282 | } | ||
283 | int offs = move_offs(move_dir); | ||
284 | |||
285 | struct sway_container *sibling = NULL; | ||
286 | struct sway_container *current = container; | ||
287 | struct sway_container *parent = current->parent; | ||
288 | |||
289 | if (parent != container_flatten(parent)) { | ||
290 | // Special case: we were the last one in this container, so flatten it | ||
291 | // and leave | ||
292 | update_debug_tree(); | ||
293 | return; | ||
294 | } | ||
295 | |||
296 | while (!sibling) { | ||
297 | if (current->type == C_ROOT) { | ||
298 | return; | ||
299 | } | ||
300 | |||
301 | parent = current->parent; | ||
302 | wlr_log(L_DEBUG, "Visiting %p %s '%s'", current, | ||
303 | container_type_to_str(current->type), current->name); | ||
304 | |||
305 | int index = index_child(current); | ||
306 | |||
307 | switch (current->type) { | ||
308 | case C_OUTPUT: { | ||
309 | enum wlr_direction wlr_dir; | ||
310 | sway_dir_to_wlr(move_dir, &wlr_dir); | ||
311 | double ref_lx = current->x + current->width / 2; | ||
312 | double ref_ly = current->y + current->height / 2; | ||
313 | struct wlr_output *next = wlr_output_layout_adjacent_output( | ||
314 | root_container.sway_root->output_layout, wlr_dir, | ||
315 | current->sway_output->wlr_output, ref_lx, ref_ly); | ||
316 | if (!next) { | ||
317 | wlr_log(L_DEBUG, "Hit edge of output, nowhere else to go"); | ||
318 | return; | ||
319 | } | ||
320 | struct sway_output *next_output = next->data; | ||
321 | current = next_output->swayc; | ||
322 | wlr_log(L_DEBUG, "Selected next output (%s)", current->name); | ||
323 | // Select workspace and get outta here | ||
324 | current = seat_get_focus_inactive( | ||
325 | config->handler_context.seat, current); | ||
326 | if (current->type != C_WORKSPACE) { | ||
327 | current = container_parent(current, C_WORKSPACE); | ||
328 | } | ||
329 | sibling = current; | ||
330 | break; | ||
331 | } | ||
332 | case C_WORKSPACE: | ||
333 | if (!is_parallel(current->layout, move_dir)) { | ||
334 | if (current->children->length > 2) { | ||
335 | wlr_log(L_DEBUG, "Rejiggering the workspace (%d kiddos)", | ||
336 | current->children->length); | ||
337 | workspace_rejigger(current, container, move_dir); | ||
338 | } else if (current->children->length == 2) { | ||
339 | wlr_log(L_DEBUG, "Changing workspace layout"); | ||
340 | container_set_layout(current, | ||
341 | move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? | ||
342 | L_HORIZ : L_VERT); | ||
343 | container_insert_child(current, container, offs < 0 ? 0 : 1); | ||
344 | arrange_windows(current, -1, -1); | ||
345 | } | ||
346 | return; | ||
347 | } else { | ||
348 | wlr_log(L_DEBUG, "Selecting output"); | ||
349 | current = current->parent; | ||
350 | } | ||
351 | break; | ||
352 | case C_CONTAINER: | ||
353 | case C_VIEW: | ||
354 | if (is_parallel(parent->layout, move_dir)) { | ||
355 | if ((index == parent->children->length - 1 && offs > 0) | ||
356 | || (index == 0 && offs < 0)) { | ||
357 | if (current->parent == container->parent) { | ||
358 | wlr_log(L_DEBUG, "Hit limit, selecting parent"); | ||
359 | current = current->parent; | ||
360 | } else { | ||
361 | wlr_log(L_DEBUG, "Hit limit, " | ||
362 | "promoting descendant to sibling"); | ||
363 | // Special case | ||
364 | struct sway_container *old_parent = container->parent; | ||
365 | container_insert_child(current->parent, container, | ||
366 | index + (offs < 0 ? 0 : 1)); | ||
367 | container->width = container->height = 0; | ||
368 | arrange_windows(current->parent, -1, -1); | ||
369 | arrange_windows(old_parent, -1, -1); | ||
370 | return; | ||
371 | } | ||
372 | } else { | ||
373 | sibling = parent->children->items[index + offs]; | ||
374 | wlr_log(L_DEBUG, "Selecting sibling id:%zd", sibling->id); | ||
375 | } | ||
376 | } else { | ||
377 | wlr_log(L_DEBUG, "Moving up to find a parallel container"); | ||
378 | current = current->parent; | ||
379 | } | ||
380 | break; | ||
381 | default: | ||
382 | sway_assert(0, "Not expecting to see container of type %s here", | ||
383 | container_type_to_str(current->type)); | ||
384 | return; | ||
385 | } | ||
386 | } | ||
387 | |||
388 | // Part two: move stuff around | ||
389 | int index = index_child(container); | ||
390 | struct sway_container *old_parent = container->parent; | ||
391 | |||
392 | while (sibling) { | ||
393 | switch (sibling->type) { | ||
394 | case C_VIEW: | ||
395 | if (sibling->parent == container->parent) { | ||
396 | wlr_log(L_DEBUG, "Swapping siblings"); | ||
397 | sibling->parent->children->items[index + offs] = container; | ||
398 | sibling->parent->children->items[index] = sibling; | ||
399 | arrange_windows(sibling->parent, -1, -1); | ||
400 | } else { | ||
401 | wlr_log(L_DEBUG, "Promoting to sibling of cousin"); | ||
402 | container_insert_child(sibling->parent, container, | ||
403 | index_child(sibling) + (offs > 0 ? 0 : 1)); | ||
404 | container->width = container->height = 0; | ||
405 | arrange_windows(sibling->parent, -1, -1); | ||
406 | arrange_windows(old_parent, -1, -1); | ||
407 | } | ||
408 | sibling = NULL; | ||
409 | break; | ||
410 | case C_WORKSPACE: // Note: only in the case of moving between outputs | ||
411 | case C_CONTAINER: | ||
412 | if (is_parallel(sibling->layout, move_dir)) { | ||
413 | int limit = container_limit(sibling, invert_movement(move_dir)); | ||
414 | wlr_log(L_DEBUG, "limit: %d", limit); | ||
415 | wlr_log(L_DEBUG, | ||
416 | "Reparenting container (parallel) to index %d " | ||
417 | "(move dir: %d)", limit, move_dir); | ||
418 | container_insert_child(sibling, container, limit); | ||
419 | container->width = container->height = 0; | ||
420 | arrange_windows(sibling, -1, -1); | ||
421 | arrange_windows(old_parent, -1, -1); | ||
422 | sibling = NULL; | ||
423 | } else { | ||
424 | wlr_log(L_DEBUG, "Reparenting container (perpendicular)"); | ||
425 | container_remove_child(container); | ||
426 | struct sway_container *focus_inactive = seat_get_focus_inactive( | ||
427 | config->handler_context.seat, sibling); | ||
428 | if (focus_inactive) { | ||
429 | while (focus_inactive->parent != sibling) { | ||
430 | focus_inactive = focus_inactive->parent; | ||
431 | } | ||
432 | wlr_log(L_DEBUG, "Focus inactive: id:%zd", | ||
433 | focus_inactive->id); | ||
434 | sibling = focus_inactive; | ||
435 | continue; | ||
436 | } else if (sibling->children->length) { | ||
437 | wlr_log(L_DEBUG, "No focus-inactive, adding arbitrarily"); | ||
438 | container_add_sibling(sibling->children->items[0], container); | ||
439 | } else { | ||
440 | wlr_log(L_DEBUG, "No kiddos, adding container alone"); | ||
441 | container_add_child(sibling, container); | ||
442 | } | ||
443 | container->width = container->height = 0; | ||
444 | arrange_windows(sibling, -1, -1); | ||
445 | arrange_windows(old_parent, -1, -1); | ||
446 | sibling = NULL; | ||
447 | } | ||
448 | break; | ||
449 | default: | ||
450 | sway_assert(0, "Not expecting to see container of type %s here", | ||
451 | container_type_to_str(sibling->type)); | ||
452 | return; | ||
453 | } | ||
454 | } | ||
455 | |||
456 | if (old_parent) { | ||
457 | seat_set_focus(config->handler_context.seat, old_parent); | ||
458 | seat_set_focus(config->handler_context.seat, container); | ||
459 | } | ||
460 | |||
461 | struct sway_container *last_ws = old_parent; | ||
462 | struct sway_container *next_ws = container->parent; | ||
463 | if (last_ws && last_ws->type != C_WORKSPACE) { | ||
464 | last_ws = container_parent(last_ws, C_WORKSPACE); | ||
465 | } | ||
466 | if (next_ws && next_ws->type != C_WORKSPACE) { | ||
467 | next_ws = container_parent(next_ws, C_WORKSPACE); | ||
468 | } | ||
469 | if (last_ws && next_ws && last_ws != next_ws) { | ||
470 | ipc_event_workspace(last_ws, container, "focus"); | ||
471 | } | ||
172 | } | 472 | } |
173 | 473 | ||
174 | enum sway_container_layout container_get_default_layout( | 474 | enum sway_container_layout container_get_default_layout( |
@@ -320,6 +620,9 @@ void arrange_windows(struct sway_container *container, | |||
320 | container->children->length); | 620 | container->children->length); |
321 | break; | 621 | break; |
322 | } | 622 | } |
623 | container_damage_whole(container); | ||
624 | // TODO: Make this less shitty | ||
625 | update_debug_tree(); | ||
323 | } | 626 | } |
324 | 627 | ||
325 | static void apply_horiz_layout(struct sway_container *container, | 628 | static void apply_horiz_layout(struct sway_container *container, |
@@ -512,28 +815,6 @@ static void get_layout_center_position(struct sway_container *container, | |||
512 | } | 815 | } |
513 | } | 816 | } |
514 | 817 | ||
515 | static bool sway_dir_to_wlr(enum movement_direction dir, | ||
516 | enum wlr_direction *out) { | ||
517 | switch (dir) { | ||
518 | case MOVE_UP: | ||
519 | *out = WLR_DIRECTION_UP; | ||
520 | break; | ||
521 | case MOVE_DOWN: | ||
522 | *out = WLR_DIRECTION_DOWN; | ||
523 | break; | ||
524 | case MOVE_LEFT: | ||
525 | *out = WLR_DIRECTION_LEFT; | ||
526 | break; | ||
527 | case MOVE_RIGHT: | ||
528 | *out = WLR_DIRECTION_RIGHT; | ||
529 | break; | ||
530 | default: | ||
531 | return false; | ||
532 | } | ||
533 | |||
534 | return true; | ||
535 | } | ||
536 | |||
537 | static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { | 818 | static struct sway_container *sway_output_from_wlr(struct wlr_output *output) { |
538 | if (output == NULL) { | 819 | if (output == NULL) { |
539 | return NULL; | 820 | return NULL; |
@@ -673,6 +954,9 @@ struct sway_container *container_replace_child(struct sway_container *child, | |||
673 | int i = index_child(child); | 954 | int i = index_child(child); |
674 | 955 | ||
675 | // TODO floating | 956 | // TODO floating |
957 | if (new_child->parent) { | ||
958 | container_remove_child(new_child); | ||
959 | } | ||
676 | parent->children->items[i] = new_child; | 960 | parent->children->items[i] = new_child; |
677 | new_child->parent = parent; | 961 | new_child->parent = parent; |
678 | child->parent = NULL; | 962 | child->parent = NULL; |
@@ -696,6 +980,14 @@ struct sway_container *container_split(struct sway_container *child, | |||
696 | if (!sway_assert(child, "child cannot be null")) { | 980 | if (!sway_assert(child, "child cannot be null")) { |
697 | return NULL; | 981 | return NULL; |
698 | } | 982 | } |
983 | if (child->type == C_WORKSPACE && child->children->length == 0) { | ||
984 | // Special case: this just behaves like splitt | ||
985 | child->prev_layout = child->layout; | ||
986 | child->layout = layout; | ||
987 | arrange_windows(child, -1, -1); | ||
988 | return child; | ||
989 | } | ||
990 | |||
699 | struct sway_container *cont = container_create(C_CONTAINER); | 991 | struct sway_container *cont = container_create(C_CONTAINER); |
700 | 992 | ||
701 | wlr_log(L_DEBUG, "creating container %p around %p", cont, child); | 993 | wlr_log(L_DEBUG, "creating container %p around %p", cont, child); |
@@ -718,7 +1010,9 @@ struct sway_container *container_split(struct sway_container *child, | |||
718 | } | 1010 | } |
719 | 1011 | ||
720 | container_add_child(workspace, cont); | 1012 | container_add_child(workspace, cont); |
1013 | enum sway_container_layout old_layout = workspace->layout; | ||
721 | container_set_layout(workspace, layout); | 1014 | container_set_layout(workspace, layout); |
1015 | cont->layout = old_layout; | ||
722 | 1016 | ||
723 | if (set_focus) { | 1017 | if (set_focus) { |
724 | seat_set_focus(seat, cont); | 1018 | seat_set_focus(seat, cont); |