From 08cfba2192f5770d975c5fe70789a81aaee4dc7e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 26 Jul 2018 18:36:46 +1000 Subject: [PATCH 01/10] Allow containers to float Things worth noting: * When a fullscreen view unmaps, the check to unset fullscreen on the workspace has been moved out of view_unmap and into container_destroy, because containers can be fullscreen too * The calls to `container_reap_empty_recursive(workspace)` have been removed from `container_set_floating`. That function reaps upwards so it wouldn't do anything. I'm probably the one who originally added it... * My fix (b14bd1b0b1536039e4f46fe94515c7c44e7afc61) for the tabbed child crash has a side effect where when you close a floating container, focus is not given to the tiled container again. I've removed my fix and removed the call to `send_cursor_motion` from `seat_set_focus_warp`. We should consider calling it from somewhere earlier in the call stack. --- include/sway/tree/container.h | 11 +++++ include/sway/tree/view.h | 5 --- sway/commands/floating.c | 13 ++++-- sway/commands/move.c | 10 ++++- sway/desktop/render.c | 2 +- sway/input/cursor.c | 5 ++- sway/input/seat.c | 5 --- sway/scratchpad.c | 7 +-- sway/tree/container.c | 82 ++++++++++++++++++++++++++++++++--- sway/tree/layout.c | 19 +++++--- sway/tree/view.c | 53 +--------------------- 11 files changed, 126 insertions(+), 86 deletions(-) diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index c584cd92c..d91b3bf1b 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -297,6 +297,11 @@ void container_notify_subtree_changed(struct sway_container *container); */ size_t container_titlebar_height(void); +/** + * Resize and center the container in its workspace. + */ +void container_init_floating(struct sway_container *container); + void container_set_floating(struct sway_container *container, bool enable); void container_set_geometry_from_floating_view(struct sway_container *con); @@ -340,6 +345,12 @@ void container_end_mouse_operation(struct sway_container *container); void container_set_fullscreen(struct sway_container *container, bool enable); +/** + * Return true if the container is floating, or a child of a floating split + * container. + */ +bool container_is_floating_or_child(struct sway_container *container); + /** * Return true if the container is fullscreen, or a child of a fullscreen split * container. diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 7086314fb..0152ed554 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -234,11 +234,6 @@ void view_get_constraints(struct sway_view *view, double *min_width, uint32_t view_configure(struct sway_view *view, double lx, double ly, int width, int height); -/** - * Center the view in its workspace and build the swayc decorations around it. - */ -void view_init_floating(struct sway_view *view); - /** * Configure the view's position and size based on the swayc's position and * size, taking borders into consideration. diff --git a/sway/commands/floating.c b/sway/commands/floating.c index 6ab56c3b1..b67e736f6 100644 --- a/sway/commands/floating.c +++ b/sway/commands/floating.c @@ -17,9 +17,16 @@ struct cmd_results *cmd_floating(int argc, char **argv) { } struct sway_container *container = config->handler_context.current_container; - if (container->type != C_VIEW) { - // TODO: This doesn't strictly speaking have to be true - return cmd_results_new(CMD_INVALID, "float", "Only views can float"); + if (container->type == C_WORKSPACE && container->children->length == 0) { + return cmd_results_new(CMD_INVALID, "floating", + "Can't float an empty workspace"); + } + if (container->type == C_WORKSPACE) { + // Wrap the workspace's children in a container so we can float it + struct sway_container *workspace = container; + container = container_wrap_children(container); + workspace->layout = L_HORIZ; + seat_set_focus(config->handler_context.seat, container); } bool wants_floating; diff --git a/sway/commands/move.c b/sway/commands/move.c index aede3d6c9..b127c89fa 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -298,9 +298,15 @@ static struct cmd_results *move_to_position(struct sway_container *container, } static struct cmd_results *move_to_scratchpad(struct sway_container *con) { - if (con->type != C_CONTAINER && con->type != C_VIEW) { + if (con->type == C_WORKSPACE && con->children->length == 0) { return cmd_results_new(CMD_INVALID, "move", - "Only views and containers can be moved to the scratchpad"); + "Can't move an empty workspace to the scratchpad"); + } + if (con->type == C_WORKSPACE) { + // Wrap the workspace's children in a container + struct sway_container *workspace = con; + con = container_wrap_children(con); + workspace->layout = L_HORIZ; } if (con->scratchpad) { return cmd_results_new(CMD_INVALID, "move", diff --git a/sway/desktop/render.c b/sway/desktop/render.c index d2f1c9f23..c9fdfd95e 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -779,7 +779,7 @@ static void render_floating_container(struct sway_output *soutput, } render_view(soutput, damage, con, colors); } else { - render_container(soutput, damage, con, false); + render_container(soutput, damage, con, con->current.focused); } } diff --git a/sway/input/cursor.c b/sway/input/cursor.c index cc0dbe99b..d13471986 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -598,7 +598,10 @@ void dispatch_cursor_button(struct sway_cursor *cursor, seat_set_focus_layer(cursor->seat, layer); } seat_pointer_notify_button(cursor->seat, time_msec, button, state); - } else if (cont && container_is_floating(cont)) { + } else if (cont && container_is_floating_or_child(cont)) { + while (cont->parent->layout != L_FLOATING) { + cont = cont->parent; + } dispatch_cursor_button_floating(cursor, time_msec, button, state, surface, sx, sy, cont); } else if (surface && cont && cont->type != C_VIEW) { diff --git a/sway/input/seat.c b/sway/input/seat.c index e7b6e0c59..877a93c6c 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -184,7 +184,6 @@ static void handle_seat_container_destroy(struct wl_listener *listener, bool set_focus = focus != NULL && (focus == con || container_has_child(con, focus)) && - con->parent && con->parent->children->length > 1 && con->type != C_WORKSPACE; seat_container_destroy(seat_con); @@ -754,10 +753,6 @@ void seat_set_focus_warp(struct sway_seat *seat, } } - if (last_focus != NULL) { - cursor_send_pointer_motion(seat->cursor, 0, true); - } - seat->has_focus = (container != NULL); update_debug_tree(); diff --git a/sway/scratchpad.c b/sway/scratchpad.c index 64636c776..181d9b3b2 100644 --- a/sway/scratchpad.c +++ b/sway/scratchpad.c @@ -72,11 +72,7 @@ static void scratchpad_show(struct sway_container *con) { if (!wlr_box_contains_point(&workspace_box, center_lx, center_ly)) { // Maybe resize it if (con->width > ws->width || con->height > ws->height) { - // TODO: Do this properly once we can float C_CONTAINERs - if (con->type == C_VIEW) { - view_init_floating(con->sway_view); - arrange_windows(con); - } + container_init_floating(con); } // Center it @@ -85,6 +81,7 @@ static void scratchpad_show(struct sway_container *con) { container_floating_move_to(con, new_lx, new_ly); } + arrange_windows(ws); seat_set_focus(seat, con); container_set_dirty(con->parent); diff --git a/sway/tree/container.c b/sway/tree/container.c index 6ebf26532..566432b1a 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -407,6 +407,10 @@ struct sway_container *container_flatten(struct sway_container *container) { * This function just wraps container_destroy_noreaping(), then does reaping. */ struct sway_container *container_destroy(struct sway_container *con) { + if (con->is_fullscreen) { + struct sway_container *ws = container_parent(con, C_WORKSPACE); + ws->sway_workspace->fullscreen = NULL; + } struct sway_container *parent = container_destroy_noreaping(con); if (!parent) { @@ -945,23 +949,81 @@ size_t container_titlebar_height() { return config->font_height + TITLEBAR_V_PADDING * 2; } +void container_init_floating(struct sway_container *con) { + if (!sway_assert(con->type == C_VIEW || con->type == C_CONTAINER, + "Expected a view or container")) { + return; + } + struct sway_container *ws = container_parent(con, C_WORKSPACE); + int min_width, min_height; + int max_width, max_height; + + if (config->floating_minimum_width == -1) { // no minimum + min_width = 0; + } else if (config->floating_minimum_width == 0) { // automatic + min_width = 75; + } else { + min_width = config->floating_minimum_width; + } + + if (config->floating_minimum_height == -1) { // no minimum + min_height = 0; + } else if (config->floating_minimum_height == 0) { // automatic + min_height = 50; + } else { + min_height = config->floating_minimum_height; + } + + if (config->floating_maximum_width == -1) { // no maximum + max_width = INT_MAX; + } else if (config->floating_maximum_width == 0) { // automatic + max_width = ws->width * 0.6666; + } else { + max_width = config->floating_maximum_width; + } + + if (config->floating_maximum_height == -1) { // no maximum + max_height = INT_MAX; + } else if (config->floating_maximum_height == 0) { // automatic + max_height = ws->height * 0.6666; + } else { + max_height = config->floating_maximum_height; + } + + if (con->type == C_CONTAINER) { + con->width = max_width; + con->height = max_height; + con->x = ws->x + (ws->width - con->width) / 2; + con->y = ws->y + (ws->height - con->height) / 2; + } else { + struct sway_view *view = con->sway_view; + view->width = fmax(min_width, fmin(view->natural_width, max_width)); + view->height = fmax(min_height, fmin(view->natural_height, max_height)); + view->x = ws->x + (ws->width - view->width) / 2; + view->y = ws->y + (ws->height - view->height) / 2; + + // If the view's border is B_NONE then these properties are ignored. + view->border_top = view->border_bottom = true; + view->border_left = view->border_right = true; + + container_set_geometry_from_floating_view(view->swayc); + } +} + void container_set_floating(struct sway_container *container, bool enable) { if (container_is_floating(container) == enable) { return; } struct sway_container *workspace = container_parent(container, C_WORKSPACE); - struct sway_seat *seat = input_manager_current_seat(input_manager); if (enable) { container_remove_child(container); container_add_child(workspace->sway_workspace->floating, container); + container_init_floating(container); if (container->type == C_VIEW) { - view_init_floating(container->sway_view); view_set_tiled(container->sway_view, false); } - seat_set_focus(seat, seat_get_focus_inactive(seat, container)); - container_reap_empty_recursive(workspace); } else { // Returning to tiled if (container->scratchpad) { @@ -975,7 +1037,6 @@ void container_set_floating(struct sway_container *container, bool enable) { view_set_tiled(container->sway_view, true); } container->is_sticky = false; - container_reap_empty_recursive(workspace->sway_workspace->floating); } container_end_mouse_operation(container); @@ -1195,6 +1256,17 @@ void container_set_fullscreen(struct sway_container *container, bool enable) { ipc_event_window(container, "fullscreen_mode"); } +bool container_is_floating_or_child(struct sway_container *container) { + do { + if (container->parent && container->parent->layout == L_FLOATING) { + return true; + } + container = container->parent; + } while (container && container->type != C_WORKSPACE); + + return false; +} + bool container_is_fullscreen_or_child(struct sway_container *container) { do { if (container->is_fullscreen) { diff --git a/sway/tree/layout.c b/sway/tree/layout.c index ab5acc161..a0764a549 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -387,9 +387,11 @@ void container_move(struct sway_container *container, // If moving a fullscreen view, only consider outputs if (container->is_fullscreen) { current = container_parent(container, C_OUTPUT); - } else if (container_is_fullscreen_or_child(container)) { + } else if (container_is_fullscreen_or_child(container) || + container_is_floating_or_child(container)) { // If we've fullscreened a split container, only allow the child to move // around within the fullscreen parent. + // Same with floating a split container. struct sway_container *ws = container_parent(container, C_WORKSPACE); top = ws->sway_workspace->fullscreen; } @@ -465,6 +467,9 @@ void container_move(struct sway_container *container, if ((index == parent->children->length - 1 && offs > 0) || (index == 0 && offs < 0)) { if (current->parent == container->parent) { + if (parent->parent->layout == L_FLOATING) { + return; + } if (!parent->is_fullscreen && (parent->layout == L_TABBED || parent->layout == L_STACKED)) { @@ -488,10 +493,14 @@ void container_move(struct sway_container *container, sibling = parent->children->items[index + offs]; wlr_log(WLR_DEBUG, "Selecting sibling id:%zd", sibling->id); } - } else if (!parent->is_fullscreen && (parent->layout == L_TABBED || + } else if (!parent->is_fullscreen && + parent->parent->layout != L_FLOATING && + (parent->layout == L_TABBED || parent->layout == L_STACKED)) { move_out_of_tabs_stacks(container, current, move_dir, offs); return; + } else if (parent->parent->layout == L_FLOATING) { + return; } else { wlr_log(WLR_DEBUG, "Moving up to find a parallel container"); current = current->parent; @@ -717,10 +726,6 @@ struct sway_container *container_get_in_direction( enum movement_direction dir) { struct sway_container *parent = container->parent; - if (container_is_floating(container)) { - return NULL; - } - if (dir == MOVE_CHILD) { return seat_get_focus_inactive(seat, container); } @@ -732,7 +737,7 @@ struct sway_container *container_get_in_direction( parent = container->parent; } else { if (dir == MOVE_PARENT) { - if (parent->type == C_OUTPUT) { + if (parent->type == C_OUTPUT || container_is_floating(container)) { return NULL; } else { return parent; diff --git a/sway/tree/view.c b/sway/tree/view.c index e641544e3..253f30010 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -168,55 +168,6 @@ uint32_t view_configure(struct sway_view *view, double lx, double ly, int width, return 0; } -void view_init_floating(struct sway_view *view) { - struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); - int min_width, min_height; - int max_width, max_height; - - if (config->floating_minimum_width == -1) { // no minimum - min_width = 0; - } else if (config->floating_minimum_width == 0) { // automatic - min_width = 75; - } else { - min_width = config->floating_minimum_width; - } - - if (config->floating_minimum_height == -1) { // no minimum - min_height = 0; - } else if (config->floating_minimum_height == 0) { // automatic - min_height = 50; - } else { - min_height = config->floating_minimum_height; - } - - if (config->floating_maximum_width == -1) { // no maximum - max_width = INT_MAX; - } else if (config->floating_maximum_width == 0) { // automatic - max_width = ws->width * 0.6666; - } else { - max_width = config->floating_maximum_width; - } - - if (config->floating_maximum_height == -1) { // no maximum - max_height = INT_MAX; - } else if (config->floating_maximum_height == 0) { // automatic - max_height = ws->height * 0.6666; - } else { - max_height = config->floating_maximum_height; - } - - view->width = fmax(min_width, fmin(view->natural_width, max_width)); - view->height = fmax(min_height, fmin(view->natural_height, max_height)); - view->x = ws->x + (ws->width - view->width) / 2; - view->y = ws->y + (ws->height - view->height) / 2; - - // If the view's border is B_NONE then these properties are ignored. - view->border_top = view->border_bottom = true; - view->border_left = view->border_right = true; - - container_set_geometry_from_floating_view(view->swayc); -} - void view_autoconfigure(struct sway_view *view) { if (!sway_assert(view->swayc, "Called view_autoconfigure() on a view without a swayc")) { @@ -626,10 +577,8 @@ void view_unmap(struct sway_view *view) { struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); struct sway_container *parent; - if (view->swayc->is_fullscreen) { - ws->sway_workspace->fullscreen = NULL; + if (container_is_fullscreen_or_child(view->swayc)) { parent = container_destroy(view->swayc); - arrange_windows(ws->parent); } else { parent = container_destroy(view->swayc); From 0b6b6716e28b97213c8f4a3c9e65aeba6409987e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 26 Jul 2018 19:10:53 +1000 Subject: [PATCH 02/10] Fix clicking a floating split container It would focus the split container rather than the child. This commit makes it track the child and the split container separately and send the surface click to the child. --- sway/input/cursor.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sway/input/cursor.c b/sway/input/cursor.c index d13471986..96ac7b330 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -437,18 +437,22 @@ static void dispatch_cursor_button_floating(struct sway_cursor *cursor, seat_pointer_notify_button(seat, time_msec, button, state); return; } + struct sway_container *floater = cont; + while (floater->parent->layout != L_FLOATING) { + floater = floater->parent; + } struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat); bool mod_pressed = keyboard && (wlr_keyboard_get_modifiers(keyboard) & config->floating_mod); - enum wlr_edges edge = find_resize_edge(cont, cursor); + enum wlr_edges edge = find_resize_edge(floater, cursor); bool over_title = edge == WLR_EDGE_NONE && !surface; // Check for beginning move uint32_t btn_move = config->floating_mod_inverse ? BTN_RIGHT : BTN_LEFT; if (button == btn_move && state == WLR_BUTTON_PRESSED && (mod_pressed || over_title)) { - seat_begin_move(seat, cont, button); + seat_begin_move(seat, floater, button); return; } @@ -459,12 +463,12 @@ static void dispatch_cursor_button_floating(struct sway_cursor *cursor, if ((resizing_via_border || resizing_via_mod) && state == WLR_BUTTON_PRESSED) { if (edge == WLR_EDGE_NONE) { - edge |= cursor->cursor->x > cont->x + cont->width / 2 ? + edge |= cursor->cursor->x > floater->x + floater->width / 2 ? WLR_EDGE_RIGHT : WLR_EDGE_LEFT; - edge |= cursor->cursor->y > cont->y + cont->height / 2 ? + edge |= cursor->cursor->y > floater->y + floater->height / 2 ? WLR_EDGE_BOTTOM : WLR_EDGE_TOP; } - seat_begin_resize(seat, cont, button, edge); + seat_begin_resize(seat, floater, button, edge); return; } @@ -599,9 +603,6 @@ void dispatch_cursor_button(struct sway_cursor *cursor, } seat_pointer_notify_button(cursor->seat, time_msec, button, state); } else if (cont && container_is_floating_or_child(cont)) { - while (cont->parent->layout != L_FLOATING) { - cont = cont->parent; - } dispatch_cursor_button_floating(cursor, time_msec, button, state, surface, sx, sy, cont); } else if (surface && cont && cont->type != C_VIEW) { From 902a1402ba35be3fbc3c054cd7af5a8bda5f8fd6 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 26 Jul 2018 22:27:42 +1000 Subject: [PATCH 03/10] Fix crash when a child of a floating container is in the scratchpad --- sway/tree/view.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sway/tree/view.c b/sway/tree/view.c index 253f30010..8f54cc119 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -999,11 +999,14 @@ void view_update_marks_textures(struct sway_view *view) { } bool view_is_visible(struct sway_view *view) { - if (!view->swayc || view->swayc->destroying || !view->swayc->parent) { + if (!view->swayc || view->swayc->destroying) { return false; } struct sway_container *workspace = container_parent(view->swayc, C_WORKSPACE); + if (!workspace) { + return false; + } // Determine if view is nested inside a floating container which is sticky. // A simple floating view will have this ancestry: // C_VIEW -> floating -> workspace From 936168e740067a8658c7097ff76588b070faa71f Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 26 Jul 2018 22:42:58 +1000 Subject: [PATCH 04/10] Operate on floating split container when a child is focused --- sway/commands/floating.c | 8 ++++++++ sway/commands/scratchpad.c | 12 ++++++++++-- sway/scratchpad.c | 9 +++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/sway/commands/floating.c b/sway/commands/floating.c index b67e736f6..31de5ec39 100644 --- a/sway/commands/floating.c +++ b/sway/commands/floating.c @@ -29,6 +29,14 @@ struct cmd_results *cmd_floating(int argc, char **argv) { seat_set_focus(config->handler_context.seat, container); } + // If the container is in a floating split container, + // operate on the split container instead of the child. + if (container_is_floating_or_child(container)) { + while (container->parent->layout != L_FLOATING) { + container = container->parent; + } + } + bool wants_floating; if (strcasecmp(argv[0], "enable") == 0) { wants_floating = true; diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c index ccc07c878..01a91d65e 100644 --- a/sway/commands/scratchpad.c +++ b/sway/commands/scratchpad.c @@ -19,11 +19,19 @@ struct cmd_results *cmd_scratchpad(int argc, char **argv) { } if (config->handler_context.using_criteria) { + struct sway_container *con = config->handler_context.current_container; + + // If the container is in a floating split container, + // operate on the split container instead of the child. + if (container_is_floating_or_child(con)) { + while (con->parent->layout != L_FLOATING) { + con = con->parent; + } + } + // If using criteria, this command is executed for every container which // matches the criteria. If this container isn't in the scratchpad, // we'll just silently return a success. - struct sway_container *con = config->handler_context.current_container; - wlr_log(WLR_INFO, "cmd_scratchpad(%s)", con->name); if (!con->scratchpad) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/scratchpad.c b/sway/scratchpad.c index 181d9b3b2..787af6e1e 100644 --- a/sway/scratchpad.c +++ b/sway/scratchpad.c @@ -110,6 +110,15 @@ void scratchpad_toggle_auto(void) { struct sway_container *ws = focus->type == C_WORKSPACE ? focus : container_parent(focus, C_WORKSPACE); + // If the focus is in a floating split container, + // operate on the split container instead of the child. + if (container_is_floating_or_child(focus)) { + while (focus->parent->layout != L_FLOATING) { + focus = focus->parent; + } + } + + // Check if the currently focused window is a scratchpad window and should // be hidden again. if (focus->scratchpad) { From da2a87f6c71bfe90a4d77542bfc7ed22899f67be Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 26 Jul 2018 22:58:42 +1000 Subject: [PATCH 05/10] When unfloating, return container to previously focused tiled container This introduces seat_get_focus_inactive_tiling and updates `focus mode_toggle` to use it instead, because the previous method wasn't guaranteed to return a tiling view. --- include/sway/input/seat.h | 3 +++ sway/commands/focus.c | 14 ++++++++------ sway/input/seat.c | 23 +++++++++++++++++------ sway/tree/container.c | 5 ++++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index ab25788f0..07febe2ca 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -124,6 +124,9 @@ struct sway_container *seat_get_focus(struct sway_seat *seat); struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container); +struct sway_container *seat_get_focus_inactive_tiling(struct sway_seat *seat, + struct sway_container *container); + /** * Descend into the focus stack to find the focus-inactive view. Useful for * container placement when they change position in the tree. diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 9cd8bfae7..ce3d032f5 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -35,14 +35,16 @@ static struct cmd_results *focus_mode(struct sway_container *con, struct sway_seat *seat, bool floating) { struct sway_container *ws = con->type == C_WORKSPACE ? con : container_parent(con, C_WORKSPACE); - struct sway_container *new_focus = ws; + struct sway_container *new_focus = NULL; if (floating) { - new_focus = ws->sway_workspace->floating; - if (new_focus->children->length == 0) { - return cmd_results_new(CMD_SUCCESS, NULL, NULL); - } + new_focus = seat_get_focus_inactive(seat, ws->sway_workspace->floating); + } else { + new_focus = seat_get_focus_inactive_tiling(seat, ws); } - seat_set_focus(seat, seat_get_active_child(seat, new_focus)); + if (!new_focus) { + new_focus = ws; + } + seat_set_focus(seat, new_focus); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/input/seat.c b/sway/input/seat.c index 877a93c6c..18d5591df 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -124,12 +124,14 @@ static void seat_send_focus(struct sway_container *con, } static struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, - struct sway_container *container, enum sway_container_type type) { + struct sway_container *container, enum sway_container_type type, + bool only_tiling) { if (container->type == C_VIEW) { return container; } - struct sway_container *floating = container->type == C_WORKSPACE ? + struct sway_container *floating = + container->type == C_WORKSPACE && !only_tiling ? container->sway_workspace->floating : NULL; if (container->children->length == 0 && (!floating || floating->children->length == 0)) { @@ -143,6 +145,10 @@ static struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, } if (container_has_child(container, current->container)) { + if (only_tiling && + container_is_floating_or_child(current->container)) { + continue; + } return current->container; } if (floating && container_has_child(floating, current->container)) { @@ -169,7 +175,7 @@ void seat_focus_inactive_children_for_each(struct sway_seat *seat, struct sway_container *seat_get_focus_inactive_view(struct sway_seat *seat, struct sway_container *container) { - return seat_get_focus_by_type(seat, container, C_VIEW); + return seat_get_focus_by_type(seat, container, C_VIEW, false); } static void handle_seat_container_destroy(struct wl_listener *listener, @@ -191,7 +197,7 @@ static void handle_seat_container_destroy(struct wl_listener *listener, if (set_focus) { struct sway_container *next_focus = NULL; while (next_focus == NULL) { - next_focus = seat_get_focus_by_type(seat, parent, C_VIEW); + next_focus = seat_get_focus_by_type(seat, parent, C_VIEW, false); if (next_focus == NULL && parent->type == C_WORKSPACE) { next_focus = parent; @@ -648,7 +654,7 @@ void seat_set_focus_warp(struct sway_seat *seat, struct sway_container *new_output_last_ws = NULL; if (last_output && new_output && last_output != new_output) { new_output_last_ws = - seat_get_focus_by_type(seat, new_output, C_WORKSPACE); + seat_get_focus_by_type(seat, new_output, C_WORKSPACE, false); } if (container && container->parent) { @@ -853,7 +859,12 @@ void seat_set_exclusive_client(struct sway_seat *seat, struct sway_container *seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { - return seat_get_focus_by_type(seat, container, C_TYPES); + return seat_get_focus_by_type(seat, container, C_TYPES, false); +} + +struct sway_container *seat_get_focus_inactive_tiling(struct sway_seat *seat, + struct sway_container *container) { + return seat_get_focus_by_type(seat, container, C_TYPES, true); } struct sway_container *seat_get_active_child(struct sway_seat *seat, diff --git a/sway/tree/container.c b/sway/tree/container.c index 566432b1a..b8ff87e16 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -1015,6 +1015,7 @@ void container_set_floating(struct sway_container *container, bool enable) { return; } + struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *workspace = container_parent(container, C_WORKSPACE); if (enable) { @@ -1029,8 +1030,10 @@ void container_set_floating(struct sway_container *container, bool enable) { if (container->scratchpad) { scratchpad_remove_container(container); } + struct sway_container *sibling = + seat_get_focus_inactive_tiling(seat, workspace); container_remove_child(container); - container_add_child(workspace, container); + container_add_child(sibling, container); container->width = container->parent->width; container->height = container->parent->height; if (container->type == C_VIEW) { From c95847c9e17cf42a8ff23f791232c0ddd671ba6e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 26 Jul 2018 23:00:58 +1000 Subject: [PATCH 06/10] Allow splitting a floating view --- sway/commands/split.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sway/commands/split.c b/sway/commands/split.c index 313799dab..a8eddf543 100644 --- a/sway/commands/split.c +++ b/sway/commands/split.c @@ -10,10 +10,6 @@ static struct cmd_results *do_split(int layout) { struct sway_container *con = config->handler_context.current_container; - if (container_is_floating(con)) { - return cmd_results_new(CMD_FAILURE, "split", - "Can't split a floating view"); - } struct sway_container *parent = container_split(con, layout); container_create_notify(parent); arrange_windows(parent->parent); From 538e083f61c363ef1127636d8fac1b7e4872e4c4 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Fri, 27 Jul 2018 09:10:10 +1000 Subject: [PATCH 07/10] Fix focus mode_toggle from a child of a floating container Also fixes a crash when unfloating a window. It needs to add it back to the tiling tree as a sibling rather than a child, because the reference container might be a view. --- sway/commands/focus.c | 11 ++++++++++- sway/tree/container.c | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sway/commands/focus.c b/sway/commands/focus.c index ce3d032f5..76d3f1dcc 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -35,6 +35,15 @@ static struct cmd_results *focus_mode(struct sway_container *con, struct sway_seat *seat, bool floating) { struct sway_container *ws = con->type == C_WORKSPACE ? con : container_parent(con, C_WORKSPACE); + + // If the container is in a floating split container, + // operate on the split container instead of the child. + if (container_is_floating_or_child(con)) { + while (con->parent->layout != L_FLOATING) { + con = con->parent; + } + } + struct sway_container *new_focus = NULL; if (floating) { new_focus = seat_get_focus_inactive(seat, ws->sway_workspace->floating); @@ -99,7 +108,7 @@ struct cmd_results *cmd_focus(int argc, char **argv) { } else if (strcmp(argv[0], "tiling") == 0) { return focus_mode(con, seat, false); } else if (strcmp(argv[0], "mode_toggle") == 0) { - return focus_mode(con, seat, !container_is_floating(con)); + return focus_mode(con, seat, !container_is_floating_or_child(con)); } if (strcmp(argv[0], "output") == 0) { diff --git a/sway/tree/container.c b/sway/tree/container.c index b8ff87e16..71babe5c0 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -1033,7 +1033,7 @@ void container_set_floating(struct sway_container *container, bool enable) { struct sway_container *sibling = seat_get_focus_inactive_tiling(seat, workspace); container_remove_child(container); - container_add_child(sibling, container); + container_add_sibling(sibling, container); container->width = container->parent->width; container->height = container->parent->height; if (container->type == C_VIEW) { From 9d1f6e095b08252585e6279cd32391a23a877998 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Fri, 27 Jul 2018 09:14:12 +1000 Subject: [PATCH 08/10] Send floating container to scratchpad when a child is focused --- sway/commands/move.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sway/commands/move.c b/sway/commands/move.c index b127c89fa..1aae38381 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -308,6 +308,15 @@ static struct cmd_results *move_to_scratchpad(struct sway_container *con) { con = container_wrap_children(con); workspace->layout = L_HORIZ; } + + // If the container is in a floating split container, + // operate on the split container instead of the child. + if (container_is_floating_or_child(con)) { + while (con->parent->layout != L_FLOATING) { + con = con->parent; + } + } + if (con->scratchpad) { return cmd_results_new(CMD_INVALID, "move", "Container is already in the scratchpad"); From debb8e85b2808e4b5e8ff79a702ffc48efc36389 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Fri, 27 Jul 2018 13:46:01 +1000 Subject: [PATCH 09/10] Fix crash when unfloating child of container into empty workspace --- sway/tree/container.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sway/tree/container.c b/sway/tree/container.c index 71babe5c0..2332d81f3 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -1030,10 +1030,13 @@ void container_set_floating(struct sway_container *container, bool enable) { if (container->scratchpad) { scratchpad_remove_container(container); } - struct sway_container *sibling = - seat_get_focus_inactive_tiling(seat, workspace); container_remove_child(container); - container_add_sibling(sibling, container); + struct sway_container *reference = + seat_get_focus_inactive_tiling(seat, workspace); + if (reference->type == C_VIEW) { + reference = reference->parent; + } + container_add_child(reference, container); container->width = container->parent->width; container->height = container->parent->height; if (container->type == C_VIEW) { From 0337609667ad1d01e0e0dc19231373df3fbf7c87 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Fri, 27 Jul 2018 13:48:20 +1000 Subject: [PATCH 10/10] Focus child when showing a scratchpad container --- sway/scratchpad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway/scratchpad.c b/sway/scratchpad.c index 787af6e1e..b7d6fd998 100644 --- a/sway/scratchpad.c +++ b/sway/scratchpad.c @@ -82,7 +82,7 @@ static void scratchpad_show(struct sway_container *con) { } arrange_windows(ws); - seat_set_focus(seat, con); + seat_set_focus(seat, seat_get_focus_inactive(seat, con)); container_set_dirty(con->parent); }