From dbad30a409b83d2e327e5eb2a53b46f12e462258 Mon Sep 17 00:00:00 2001 From: taiyu Date: Mon, 24 Aug 2015 01:11:21 -0700 Subject: [PATCH] add limit to swayc_in_direction --- include/container.h | 5 +++++ include/layout.h | 1 + sway/container.c | 26 ++++++++++++++++++++++++++ sway/input_state.c | 13 +++++++++---- sway/layout.c | 9 ++++++--- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/include/container.h b/include/container.h index 2ced248b3..aadba5dea 100644 --- a/include/container.h +++ b/include/container.h @@ -84,6 +84,11 @@ swayc_t *destroy_view(swayc_t *view); swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types); swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts); +// Follow focused until type/layout +swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types); +swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts); + + swayc_t *swayc_by_handle(wlc_handle handle); swayc_t *swayc_active_output(void); swayc_t *swayc_active_workspace(void); diff --git a/include/layout.h b/include/layout.h index 8f2696072..11bf1a28a 100644 --- a/include/layout.h +++ b/include/layout.h @@ -29,6 +29,7 @@ void arrange_windows(swayc_t *container, double width, double height); swayc_t *get_focused_container(swayc_t *parent); swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir); +swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_direction dir, swayc_t *limit); void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge); diff --git a/sway/container.c b/sway/container.c index d4f7c6937..d23cef8fb 100644 --- a/sway/container.c +++ b/sway/container.c @@ -398,6 +398,32 @@ swayc_t *swayc_parent_by_layout(swayc_t *container, enum swayc_layouts layout) { return container; } +swayc_t *swayc_focus_by_type(swayc_t *container, enum swayc_types type) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(type < C_TYPES && type >= C_ROOT, "%s: invalid type", __func__)) { + return NULL; + } + do { + container = container->focused; + } while (container && container->type != type); + return container; +} +swayc_t *swayc_focus_by_layout(swayc_t *container, enum swayc_layouts layout) { + if (!ASSERT_NONNULL(container)) { + return NULL; + } + if (!sway_assert(layout < L_LAYOUTS && layout >= L_NONE, "%s: invalid layout", __func__)) { + return NULL; + } + do { + container = container->focused; + } while (container && container->layout != layout); + return container; +} + + static swayc_t *_swayc_by_handle_helper(wlc_handle handle, swayc_t *parent) { if (!parent || !parent->children) { return NULL; diff --git a/sway/input_state.c b/sway/input_state.c index e911d9cf6..10425c57f 100644 --- a/sway/input_state.c +++ b/sway/input_state.c @@ -119,15 +119,20 @@ static void set_initial_view(swayc_t *view) { static void set_initial_sibling(void) { bool reset = true; - if ((initial.horiz.ptr = get_swayc_in_direction(initial.ptr, lock.left ? MOVE_RIGHT: MOVE_LEFT))) { + swayc_t *ws = swayc_active_workspace_for(initial.ptr); + if ((initial.horiz.ptr = get_swayc_in_direction_under(initial.ptr, + lock.left ? MOVE_RIGHT: MOVE_LEFT, ws))) { initial.horiz.w = initial.horiz.ptr->width; - initial.horiz.parent.ptr = get_swayc_in_direction(initial.horiz.ptr, lock.left ? MOVE_LEFT : MOVE_RIGHT); + initial.horiz.parent.ptr = get_swayc_in_direction_under(initial.horiz.ptr, + lock.left ? MOVE_LEFT : MOVE_RIGHT, ws); initial.horiz.parent.w = initial.horiz.parent.ptr->width; reset = false; } - if ((initial.vert.ptr = get_swayc_in_direction(initial.ptr, lock.top ? MOVE_DOWN: MOVE_UP))) { + if ((initial.vert.ptr = get_swayc_in_direction_under(initial.ptr, + lock.top ? MOVE_DOWN: MOVE_UP, ws))) { initial.vert.h = initial.vert.ptr->height; - initial.vert.parent.ptr = get_swayc_in_direction(initial.vert.ptr, lock.top ? MOVE_UP : MOVE_DOWN); + initial.vert.parent.ptr = get_swayc_in_direction_under(initial.vert.ptr, + lock.top ? MOVE_UP : MOVE_DOWN, ws); initial.vert.parent.h = initial.vert.parent.ptr->height; reset = false; } diff --git a/sway/layout.c b/sway/layout.c index bc12b9b12..a37e137c8 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -353,8 +353,7 @@ void arrange_windows(swayc_t *container, double width, double height) { layout_log(&root_container, 0); } - -swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) { +swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_direction dir, swayc_t *limit) { swayc_t *parent = container->parent; if (dir == MOVE_PARENT) { @@ -453,7 +452,7 @@ swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) if (!can_move) { container = parent; parent = parent->parent; - if (!parent) { + if (!parent || container == limit) { // Nothing we can do return NULL; } @@ -461,6 +460,10 @@ swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) } } +swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) { + return get_swayc_in_direction_under(container, dir, NULL); +} + void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge) { int i; bool layout_match = true;