summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/focus.h4
-rw-r--r--include/input_state.h49
-rw-r--r--include/key_state.h18
-rw-r--r--include/layout.h2
-rw-r--r--sway/commands.c1
-rw-r--r--sway/focus.c74
-rw-r--r--sway/handlers.c93
-rw-r--r--sway/input_state.c70
-rw-r--r--sway/key_state.c52
-rw-r--r--sway/layout.c51
10 files changed, 211 insertions, 203 deletions
diff --git a/include/focus.h b/include/focus.h
index 410ed134..383993fa 100644
--- a/include/focus.h
+++ b/include/focus.h
@@ -1,7 +1,5 @@
1#ifndef _SWAY_FOCUS_H 1#ifndef _SWAY_FOCUS_H
2#define _SWAY_FOCUS_H 2#define _SWAY_FOCUS_H
3#include "container.h"
4
5enum movement_direction { 3enum movement_direction {
6 MOVE_LEFT, 4 MOVE_LEFT,
7 MOVE_RIGHT, 5 MOVE_RIGHT,
@@ -10,6 +8,8 @@ enum movement_direction {
10 MOVE_PARENT 8 MOVE_PARENT
11}; 9};
12 10
11#include "container.h"
12
13// focused_container - the container found by following the `focused` pointer 13// focused_container - the container found by following the `focused` pointer
14// from a given container to a container with `is_focused` boolean set 14// from a given container to a container with `is_focused` boolean set
15// --- 15// ---
diff --git a/include/input_state.h b/include/input_state.h
new file mode 100644
index 00000000..782b4b19
--- /dev/null
+++ b/include/input_state.h
@@ -0,0 +1,49 @@
1#ifndef _SWAY_KEY_STATE_H
2#define _SWAY_KEY_STATE_H
3#include <stdbool.h>
4#include <stdint.h>
5#include "container.h"
6
7/* Keyboard state */
8
9typedef uint32_t keycode;
10
11// returns true if key has been pressed, otherwise false
12bool check_key(keycode key);
13
14// sets a key as pressed
15void press_key(keycode key);
16
17// unsets a key as pressed
18void release_key(keycode key);
19
20/* Pointer state */
21
22enum pointer_values {
23 M_LEFT_CLICK = 272,
24 M_RIGHT_CLICK = 273,
25 M_SCROLL_CLICK = 274,
26 M_SCROLL_UP = 275,
27 M_SCROLL_DOWN = 276,
28};
29
30extern struct pointer_state {
31 bool l_held;
32 bool r_held;
33 struct pointer_floating {
34 bool drag;
35 bool resize;
36 } floating;
37 struct pointer_lock {
38 bool left;
39 bool right;
40 bool top;
41 bool bottom;
42 } lock;
43} pointer_state;
44
45void start_floating(swayc_t *view);
46void reset_floating(swayc_t *view);
47
48#endif
49
diff --git a/include/key_state.h b/include/key_state.h
deleted file mode 100644
index a8fa8d5e..00000000
--- a/include/key_state.h
+++ /dev/null
@@ -1,18 +0,0 @@
1#ifndef _SWAY_KEY_STATE_H
2#define _SWAY_KEY_STATE_H
3#include <stdbool.h>
4#include <stdint.h>
5
6typedef uint32_t keycode;
7
8// returns true if key has been pressed, otherwise false
9bool check_key(keycode key);
10
11// sets a key as pressed
12void press_key(keycode key);
13
14// unsets a key as pressed
15void release_key(keycode key);
16
17#endif
18
diff --git a/include/layout.h b/include/layout.h
index 98fdb531..75e72d2f 100644
--- a/include/layout.h
+++ b/include/layout.h
@@ -4,6 +4,7 @@
4#include <wlc/wlc.h> 4#include <wlc/wlc.h>
5#include "list.h" 5#include "list.h"
6#include "container.h" 6#include "container.h"
7#include "focus.h"
7 8
8extern swayc_t root_container; 9extern swayc_t root_container;
9 10
@@ -26,5 +27,6 @@ void focus_view_for(swayc_t *ancestor, swayc_t *container);
26 27
27swayc_t *get_focused_container(swayc_t *parent); 28swayc_t *get_focused_container(swayc_t *parent);
28swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); 29swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent);
30swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir);
29 31
30#endif 32#endif
diff --git a/sway/commands.c b/sway/commands.c
index 66c05a0c..c4cf96a2 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -243,7 +243,6 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) {
243 } 243 }
244 // Refocus on the view once its been put back into the layout 244 // Refocus on the view once its been put back into the layout
245 arrange_windows(active_workspace, -1, -1); 245 arrange_windows(active_workspace, -1, -1);
246 return true;
247 } 246 }
248 set_focused_container(view); 247 set_focused_container(view);
249 } 248 }
diff --git a/sway/focus.c b/sway/focus.c
index f76b2d9a..1dbac003 100644
--- a/sway/focus.c
+++ b/sway/focus.c
@@ -3,6 +3,7 @@
3#include "focus.h" 3#include "focus.h"
4#include "log.h" 4#include "log.h"
5#include "workspace.h" 5#include "workspace.h"
6#include "layout.h"
6 7
7bool locked_container_focus = false; 8bool locked_container_focus = false;
8bool locked_view_focus = false; 9bool locked_view_focus = false;
@@ -53,74 +54,13 @@ static void update_focus(swayc_t *c) {
53} 54}
54 55
55bool move_focus(enum movement_direction direction) { 56bool move_focus(enum movement_direction direction) {
56 if (locked_container_focus) { 57 swayc_t *view = get_swayc_in_direction(
57 return false; 58 get_focused_container(&root_container), direction);
58 } 59 if (view) {
59 swayc_t *current = get_focused_container(&root_container); 60 set_focused_container(view);
60 if (current->type == C_VIEW 61 return true;
61 && wlc_view_get_state(current->handle) & WLC_BIT_FULLSCREEN) {
62 return false;
63 }
64 swayc_t *parent = current->parent;
65
66 if (direction == MOVE_PARENT) {
67 if (parent->type == C_OUTPUT) {
68 sway_log(L_DEBUG, "Focus cannot move to parent");
69 return false;
70 } else {
71 sway_log(L_DEBUG, "Moving focus from %p:%ld to %p:%ld",
72 current, current->handle, parent, parent->handle);
73 set_focused_container(parent);
74 return true;
75 }
76 }
77
78 while (true) {
79 sway_log(L_DEBUG, "Moving focus away from %p", current);
80
81 // Test if we can even make a difference here
82 bool can_move = false;
83 int diff = 0;
84 if (direction == MOVE_LEFT || direction == MOVE_RIGHT) {
85 if (parent->layout == L_HORIZ || parent->type == C_ROOT) {
86 can_move = true;
87 diff = direction == MOVE_LEFT ? -1 : 1;
88 }
89 } else {
90 if (parent->layout == L_VERT) {
91 can_move = true;
92 diff = direction == MOVE_UP ? -1 : 1;
93 }
94 }
95 sway_log(L_DEBUG, "Can move? %s", can_move ? "yes" : "no");
96 if (can_move) {
97 int i;
98 for (i = 0; i < parent->children->length; ++i) {
99 swayc_t *child = parent->children->items[i];
100 if (child == current) {
101 break;
102 }
103 }
104 int desired = i + diff;
105 sway_log(L_DEBUG, "Moving from %d to %d", i, desired);
106 if (desired < 0 || desired >= parent->children->length) {
107 can_move = false;
108 } else {
109 swayc_t *newview = parent->children->items[desired];
110 set_focused_container(get_focused_view(newview));
111 return true;
112 }
113 }
114 if (!can_move) {
115 sway_log(L_DEBUG, "Can't move at current level, moving up tree");
116 current = parent;
117 parent = parent->parent;
118 if (!parent) {
119 // Nothing we can do
120 return false;
121 }
122 }
123 } 62 }
63 return false;
124} 64}
125 65
126swayc_t *get_focused_container(swayc_t *parent) { 66swayc_t *get_focused_container(swayc_t *parent) {
diff --git a/sway/handlers.c b/sway/handlers.c
index c9d7c7ac..03a32835 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -13,16 +13,10 @@
13#include "workspace.h" 13#include "workspace.h"
14#include "container.h" 14#include "container.h"
15#include "focus.h" 15#include "focus.h"
16#include "key_state.h" 16#include "input_state.h"
17 17
18static struct wlc_origin mouse_origin; 18static struct wlc_origin mouse_origin;
19 19
20static bool m1_held = false;
21static bool dragging = false;
22static bool m2_held = false;
23static bool resizing = false;
24static bool lock_left, lock_right, lock_top, lock_bottom = false;
25
26static bool pointer_test(swayc_t *view, void *_origin) { 20static bool pointer_test(swayc_t *view, void *_origin) {
27 const struct wlc_origin *origin = _origin; 21 const struct wlc_origin *origin = _origin;
28 // Determine the output that the view is under 22 // Determine the output that the view is under
@@ -88,29 +82,6 @@ swayc_t *container_under_pointer(void) {
88 return lookup; 82 return lookup;
89} 83}
90 84
91static struct wlc_geometry saved_floating;
92
93static void start_floating(swayc_t *view) {
94 if (view->is_floating) {
95 saved_floating.origin.x = view->x;
96 saved_floating.origin.y = view->y;
97 saved_floating.size.w = view->width;
98 saved_floating.size.h = view->height;
99 }
100}
101
102static void reset_floating(swayc_t *view) {
103 if (view->is_floating) {
104 view->x = saved_floating.origin.x;
105 view->y = saved_floating.origin.y;
106 view->width = saved_floating.size.w;
107 view->height = saved_floating.size.h;
108 arrange_windows(view->parent, -1, -1);
109 }
110 dragging = resizing = false;
111 lock_left = lock_right = lock_top = lock_bottom = false;
112}
113
114/* Handles */ 85/* Handles */
115 86
116static bool handle_output_created(wlc_handle output) { 87static bool handle_output_created(wlc_handle output) {
@@ -327,7 +298,8 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
327 } 298 }
328 299
329 // Revert floating container back to original position on keypress 300 // Revert floating container back to original position on keypress
330 if (state == WLC_KEY_STATE_PRESSED && (dragging || resizing)) { 301 if (state == WLC_KEY_STATE_PRESSED &&
302 (pointer_state.floating.drag || pointer_state.floating.resize)) {
331 reset_floating(get_focused_view(&root_container)); 303 reset_floating(get_focused_view(&root_container));
332 } 304 }
333 305
@@ -396,7 +368,7 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
396 // Do checks to determine if proper keys are being held 368 // Do checks to determine if proper keys are being held
397 swayc_t *view = get_focused_view(active_workspace); 369 swayc_t *view = get_focused_view(active_workspace);
398 uint32_t edge = 0; 370 uint32_t edge = 0;
399 if (dragging && view) { 371 if (pointer_state.floating.drag && view) {
400 if (view->is_floating) { 372 if (view->is_floating) {
401 int dx = mouse_origin.x - prev_pos.x; 373 int dx = mouse_origin.x - prev_pos.x;
402 int dy = mouse_origin.y - prev_pos.y; 374 int dy = mouse_origin.y - prev_pos.y;
@@ -404,7 +376,7 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
404 view->y += dy; 376 view->y += dy;
405 changed_floating = true; 377 changed_floating = true;
406 } 378 }
407 } else if (resizing && view) { 379 } else if (pointer_state.floating.resize && view) {
408 if (view->is_floating) { 380 if (view->is_floating) {
409 int dx = mouse_origin.x - prev_pos.x; 381 int dx = mouse_origin.x - prev_pos.x;
410 int dy = mouse_origin.y - prev_pos.y; 382 int dy = mouse_origin.y - prev_pos.y;
@@ -415,24 +387,24 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
415 int midway_x = view->x + view->width/2; 387 int midway_x = view->x + view->width/2;
416 int midway_y = view->y + view->height/2; 388 int midway_y = view->y + view->height/2;
417 if (dx < 0) { 389 if (dx < 0) {
418 if (!lock_right) { 390 if (!pointer_state.lock.right) {
419 if (view->width > min_sane_w) { 391 if (view->width > min_sane_w) {
420 changed_floating = true; 392 changed_floating = true;
421 view->width += dx; 393 view->width += dx;
422 edge += WLC_RESIZE_EDGE_RIGHT; 394 edge += WLC_RESIZE_EDGE_RIGHT;
423 } 395 }
424 } else if (mouse_origin.x < midway_x && !lock_left) { 396 } else if (mouse_origin.x < midway_x && !pointer_state.lock.left) {
425 changed_floating = true; 397 changed_floating = true;
426 view->x += dx; 398 view->x += dx;
427 view->width -= dx; 399 view->width -= dx;
428 edge += WLC_RESIZE_EDGE_LEFT; 400 edge += WLC_RESIZE_EDGE_LEFT;
429 } 401 }
430 } else if (dx > 0) { 402 } else if (dx > 0) {
431 if (mouse_origin.x > midway_x && !lock_right) { 403 if (mouse_origin.x > midway_x && !pointer_state.lock.right) {
432 changed_floating = true; 404 changed_floating = true;
433 view->width += dx; 405 view->width += dx;
434 edge += WLC_RESIZE_EDGE_RIGHT; 406 edge += WLC_RESIZE_EDGE_RIGHT;
435 } else if (!lock_left) { 407 } else if (!pointer_state.lock.left) {
436 if (view->width > min_sane_w) { 408 if (view->width > min_sane_w) {
437 changed_floating = true; 409 changed_floating = true;
438 view->x += dx; 410 view->x += dx;
@@ -443,24 +415,24 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
443 } 415 }
444 416
445 if (dy < 0) { 417 if (dy < 0) {
446 if (!lock_bottom) { 418 if (!pointer_state.lock.bottom) {
447 if (view->height > min_sane_h) { 419 if (view->height > min_sane_h) {
448 changed_floating = true; 420 changed_floating = true;
449 view->height += dy; 421 view->height += dy;
450 edge += WLC_RESIZE_EDGE_BOTTOM; 422 edge += WLC_RESIZE_EDGE_BOTTOM;
451 } 423 }
452 } else if (mouse_origin.y < midway_y && !lock_top) { 424 } else if (mouse_origin.y < midway_y && !pointer_state.lock.top) {
453 changed_floating = true; 425 changed_floating = true;
454 view->y += dy; 426 view->y += dy;
455 view->height -= dy; 427 view->height -= dy;
456 edge += WLC_RESIZE_EDGE_TOP; 428 edge += WLC_RESIZE_EDGE_TOP;
457 } 429 }
458 } else if (dy > 0) { 430 } else if (dy > 0) {
459 if (mouse_origin.y > midway_y && !lock_bottom) { 431 if (mouse_origin.y > midway_y && !pointer_state.lock.bottom) {
460 changed_floating = true; 432 changed_floating = true;
461 view->height += dy; 433 view->height += dy;
462 edge += WLC_RESIZE_EDGE_BOTTOM; 434 edge += WLC_RESIZE_EDGE_BOTTOM;
463 } else if (!lock_top) { 435 } else if (!pointer_state.lock.top) {
464 if (view->height > min_sane_h) { 436 if (view->height > min_sane_h) {
465 changed_floating = true; 437 changed_floating = true;
466 view->y += dy; 438 view->y += dy;
@@ -474,7 +446,8 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
474 if (config->focus_follows_mouse && prev_handle != handle) { 446 if (config->focus_follows_mouse && prev_handle != handle) {
475 //Dont change focus if fullscreen 447 //Dont change focus if fullscreen
476 swayc_t *focused = get_focused_view(view); 448 swayc_t *focused = get_focused_view(view);
477 if (!(focused->type == C_VIEW && wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) && !(m1_held || m2_held)) { 449 if (!(focused->type == C_VIEW && wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)
450 && !(pointer_state.l_held || pointer_state.r_held)) {
478 set_focused_container(container_under_pointer()); 451 set_focused_container(container_under_pointer());
479 } 452 }
480 } 453 }
@@ -497,13 +470,6 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
497 return false; 470 return false;
498} 471}
499 472
500enum pointer_values {
501 M_LEFT_CLICK = 272,
502 M_RIGHT_CLICK = 273,
503 M_SCROLL_CLICK = 274,
504 M_SCROLL_UP = 275,
505 M_SCROLL_DOWN = 276,
506};
507 473
508static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, 474static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers,
509 uint32_t button, enum wlc_button_state state, const struct wlc_origin *origin) { 475 uint32_t button, enum wlc_button_state state, const struct wlc_origin *origin) {
@@ -515,10 +481,10 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w
515 if (state == WLC_BUTTON_STATE_PRESSED) { 481 if (state == WLC_BUTTON_STATE_PRESSED) {
516 sway_log(L_DEBUG, "Mouse button %u pressed", button); 482 sway_log(L_DEBUG, "Mouse button %u pressed", button);
517 if (button == M_LEFT_CLICK) { 483 if (button == M_LEFT_CLICK) {
518 m1_held = true; 484 pointer_state.l_held = true;
519 } 485 }
520 if (button == M_RIGHT_CLICK) { 486 if (button == M_RIGHT_CLICK) {
521 m2_held = true; 487 pointer_state.r_held = true;
522 } 488 }
523 swayc_t *pointer = container_under_pointer(); 489 swayc_t *pointer = container_under_pointer();
524 set_focused_container(pointer); 490 set_focused_container(pointer);
@@ -533,30 +499,31 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w
533 } 499 }
534 arrange_windows(pointer->parent, -1, -1); 500 arrange_windows(pointer->parent, -1, -1);
535 if (modifiers->mods & config->floating_mod) { 501 if (modifiers->mods & config->floating_mod) {
536 dragging = m1_held;
537 resizing = m2_held;
538 int midway_x = pointer->x + pointer->width/2; 502 int midway_x = pointer->x + pointer->width/2;
539 int midway_y = pointer->y + pointer->height/2; 503 int midway_y = pointer->y + pointer->height/2;
540 lock_bottom = origin->y < midway_y; 504
541 lock_top = !lock_bottom; 505 pointer_state.floating.drag = pointer_state.l_held;
542 lock_right = origin->x < midway_x; 506 pointer_state.floating.resize = pointer_state.r_held;
543 lock_left = !lock_right; 507 pointer_state.lock.bottom = origin->y < midway_y;
508 pointer_state.lock.top = !pointer_state.lock.bottom;
509 pointer_state.lock.right = origin->x < midway_x;
510 pointer_state.lock.left = !pointer_state.lock.right;
544 start_floating(pointer); 511 start_floating(pointer);
545 } 512 }
546 //Dont want pointer sent to window while dragging or resizing 513 //Dont want pointer sent to window while dragging or resizing
547 return (dragging || resizing); 514 return (pointer_state.floating.drag || pointer_state.floating.resize);
548 } 515 }
549 return (pointer && pointer != focused); 516 return (pointer && pointer != focused);
550 } else { 517 } else {
551 sway_log(L_DEBUG, "Mouse button %u released", button); 518 sway_log(L_DEBUG, "Mouse button %u released", button);
552 if (button == M_LEFT_CLICK) { 519 if (button == M_LEFT_CLICK) {
553 m1_held = false; 520 pointer_state.l_held = false;
554 dragging = false; 521 pointer_state.floating.drag = false;
555 } 522 }
556 if (button == M_RIGHT_CLICK) { 523 if (button == M_RIGHT_CLICK) {
557 m2_held = false; 524 pointer_state.r_held = false;
558 resizing = false; 525 pointer_state.floating.resize = false;
559 lock_top = lock_bottom = lock_left = lock_right = false; 526 pointer_state.lock = (struct pointer_lock){false ,false ,false ,false};
560 } 527 }
561 } 528 }
562 return false; 529 return false;
diff --git a/sway/input_state.c b/sway/input_state.c
new file mode 100644
index 00000000..0769c30f
--- /dev/null
+++ b/sway/input_state.c
@@ -0,0 +1,70 @@
1#include <string.h>
2#include <stdbool.h>
3#include <ctype.h>
4
5#include "input_state.h"
6
7enum { KEY_STATE_MAX_LENGTH = 64 };
8
9static keycode key_state_array[KEY_STATE_MAX_LENGTH];
10static uint8_t key_state_length = 0;
11
12static uint8_t find_key(keycode key) {
13 int i;
14 for (i = 0; i < key_state_length; ++i) {
15 if (key_state_array[i] == key) {
16 break;
17 }
18 }
19 return i;
20}
21
22bool check_key(keycode key) {
23 return find_key(key) < key_state_length;
24}
25
26void press_key(keycode key) {
27 // Check if key exists
28 if (!check_key(key)) {
29 // Check that we dont exceed buffer length
30 if (key_state_length < KEY_STATE_MAX_LENGTH) {
31 key_state_array[key_state_length++] = key;
32 }
33 }
34}
35
36void release_key(keycode key) {
37 uint8_t index = find_key(key);
38 if (index < key_state_length) {
39 //shift it over and remove key
40 memmove(&key_state_array[index],
41 &key_state_array[index + 1],
42 sizeof(*key_state_array) * (--key_state_length - index));
43 }
44}
45
46struct pointer_state pointer_state = {0, 0, {0, 0}, {0, 0, 0, 0}};
47
48static struct wlc_geometry saved_floating;
49
50void start_floating(swayc_t *view) {
51 if (view->is_floating) {
52 saved_floating.origin.x = view->x;
53 saved_floating.origin.y = view->y;
54 saved_floating.size.w = view->width;
55 saved_floating.size.h = view->height;
56 }
57}
58
59void reset_floating(swayc_t *view) {
60 if (view->is_floating) {
61 view->x = saved_floating.origin.x;
62 view->y = saved_floating.origin.y;
63 view->width = saved_floating.size.w;
64 view->height = saved_floating.size.h;
65 arrange_windows(view->parent, -1, -1);
66 }
67 pointer_state.floating = (struct pointer_floating){0,0};
68 pointer_state.lock = (struct pointer_lock){0,0,0,0};
69}
70
diff --git a/sway/key_state.c b/sway/key_state.c
deleted file mode 100644
index 76561dbc..00000000
--- a/sway/key_state.c
+++ /dev/null
@@ -1,52 +0,0 @@
1#include <string.h>
2#include <stdbool.h>
3#include <ctype.h>
4
5#include "key_state.h"
6
7enum { KEY_STATE_MAX_LENGTH = 64 };
8
9static keycode key_state_array[KEY_STATE_MAX_LENGTH];
10static uint8_t key_state_length = 0;
11
12static uint8_t find_key(keycode key)
13{
14 int i;
15 for (i = 0; i < key_state_length; ++i)
16 {
17 if (key_state_array[i] == key)
18 {
19 break;
20 }
21 }
22 return i;
23}
24
25bool check_key(keycode key)
26{
27 return find_key(key) < key_state_length;
28}
29
30void press_key(keycode key)
31{
32 // Check if key exists
33 if (!check_key(key))
34 {
35 // Check that we dont exceed buffer length
36 if (key_state_length < KEY_STATE_MAX_LENGTH) {
37 key_state_array[key_state_length++] = key;
38 }
39 }
40}
41
42void release_key(keycode key)
43{
44 uint8_t index = find_key(key);
45 if (index < key_state_length)
46 {
47 //shift it over and remove key
48 memmove(&key_state_array[index],
49 &key_state_array[index + 1],
50 sizeof(*key_state_array) * (--key_state_length - index));
51 }
52}
diff --git a/sway/layout.c b/sway/layout.c
index eb8391ce..2a43671b 100644
--- a/sway/layout.c
+++ b/sway/layout.c
@@ -298,3 +298,54 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
298 return NULL; 298 return NULL;
299} 299}
300 300
301swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) {
302 swayc_t *parent = container->parent;
303
304 if (dir == MOVE_PARENT) {
305 if (parent->type == C_OUTPUT) {
306 return NULL;
307 } else {
308 return parent;
309 }
310 }
311 while (true) {
312 // Test if we can even make a difference here
313 bool can_move = false;
314 int diff = 0;
315 if (dir == MOVE_LEFT || dir == MOVE_RIGHT) {
316 if (parent->layout == L_HORIZ || parent->type == C_ROOT) {
317 can_move = true;
318 diff = dir == MOVE_LEFT ? -1 : 1;
319 }
320 } else {
321 if (parent->layout == L_VERT) {
322 can_move = true;
323 diff = dir == MOVE_UP ? -1 : 1;
324 }
325 }
326 if (can_move) {
327 int i;
328 for (i = 0; i < parent->children->length; ++i) {
329 swayc_t *child = parent->children->items[i];
330 if (child == container) {
331 break;
332 }
333 }
334 int desired = i + diff;
335 if (desired < 0 || desired >= parent->children->length) {
336 can_move = false;
337 } else {
338 return parent->children->items[desired];
339 }
340 }
341 if (!can_move) {
342 sway_log(L_DEBUG, "Can't move at current level, moving up tree");
343 container = parent;
344 parent = parent->parent;
345 if (!parent) {
346 // Nothing we can do
347 return NULL;
348 }
349 }
350 }
351}