From 1f2e399ade77070a2d0b82856ad9a3eef96b8676 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 24 May 2018 22:30:44 +1000 Subject: [PATCH] Implement floating --- include/sway/tree/container.h | 22 ++++++ include/sway/tree/layout.h | 3 - include/sway/tree/view.h | 10 ++- include/sway/tree/workspace.h | 4 +- sway/commands.c | 1 + sway/commands/floating.c | 25 +------ sway/commands/sticky.c | 40 ++++++++++ sway/criteria.c | 9 ++- sway/desktop/output.c | 114 ++++++++++++++++++++++++++--- sway/desktop/xdg_shell.c | 32 ++++++-- sway/desktop/xdg_shell_v6.c | 32 ++++++-- sway/desktop/xwayland.c | 97 ++++++++++++++++++++----- sway/input/cursor.c | 3 + sway/input/seat.c | 16 +++- sway/ipc-json.c | 10 +++ sway/meson.build | 1 + sway/tree/arrange.c | 12 +++ sway/tree/container.c | 133 +++++++++++++++++++++++++++++++--- sway/tree/layout.c | 73 ++++++------------- sway/tree/view.c | 87 ++++++++++++++-------- sway/tree/workspace.c | 17 ++++- 21 files changed, 572 insertions(+), 169 deletions(-) create mode 100644 sway/commands/sticky.c diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index a4ffd25be..b802e1d1b 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -75,8 +75,13 @@ struct sway_container { enum sway_container_layout layout; enum sway_container_layout prev_layout; + // Allow the container to be automatically removed if it's empty. True by + // default, false for the magic floating container that each workspace has. + bool reapable; + // Saves us from searching the list of children/floating in the parent bool is_floating; + bool is_sticky; // For C_ROOT, this has no meaning // For C_OUTPUT, this is the output position in layout coordinates @@ -173,6 +178,13 @@ struct sway_container *container_at(struct sway_container *container, double ox, double oy, struct wlr_surface **surface, double *sx, double *sy); +/** + * Same as container_at, but only checks floating views and expects coordinates + * to be layout coordinates, as that's what floating views use. + */ +struct sway_container *floating_container_at(double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy); + /** * Apply the function for each descendant of the container breadth first. */ @@ -229,4 +241,14 @@ void container_notify_subtree_changed(struct sway_container *container); */ size_t container_titlebar_height(void); +void container_set_floating(struct sway_container *container, bool enable); + +void container_set_geometry_from_view(struct sway_container *container); + +/** + * Determine if the given container is itself floating or has a floating + * ancestor. + */ +bool container_self_or_parent_floating(struct sway_container *container); + #endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index 33d0a5d0d..2e0f2abf8 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -46,9 +46,6 @@ struct sway_container *container_add_sibling(struct sway_container *parent, struct sway_container *container_remove_child(struct sway_container *child); -void container_add_floating(struct sway_container *workspace, - struct sway_container *child); - struct sway_container *container_replace_child(struct sway_container *child, struct sway_container *new_child); diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index a8bf4955b..6990e5b6c 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -32,7 +32,9 @@ struct sway_view_impl { void (*configure)(struct sway_view *view, double ox, double oy, int width, int height); void (*set_activated)(struct sway_view *view, bool activated); + void (*set_maximized)(struct sway_view *view, bool maximized); void (*set_fullscreen)(struct sway_view *view, bool fullscreen); + bool (*wants_floating)(struct sway_view *view); void (*for_each_surface)(struct sway_view *view, wlr_surface_iterator_func_t iterator, void *user_data); void (*close)(struct sway_view *view); @@ -50,6 +52,10 @@ struct sway_view { double x, y; int width, height; + // The size the view would want to be if it weren't tiled. + // Used when changing a view from tiled to floating. + int natural_width, natural_height; + bool is_fullscreen; char *title_format; @@ -214,6 +220,8 @@ void view_autoconfigure(struct sway_view *view); void view_set_activated(struct sway_view *view, bool activated); +void view_set_maximized(struct sway_view *view, bool maximized); + void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen); void view_set_fullscreen(struct sway_view *view, bool fullscreen); @@ -236,7 +244,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface); void view_unmap(struct sway_view *view); -void view_update_position(struct sway_view *view, double ox, double oy); +void view_update_position(struct sway_view *view, double lx, double ly); void view_update_size(struct sway_view *view, int width, int height); diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h index ece0ab5c3..81321fc89 100644 --- a/include/sway/tree/workspace.h +++ b/include/sway/tree/workspace.h @@ -8,7 +8,7 @@ struct sway_view; struct sway_workspace { struct sway_container *swayc; struct sway_view *fullscreen; - list_t *floating; + struct sway_container *floating; }; extern char *prev_workspace_name; @@ -31,4 +31,6 @@ struct sway_container *workspace_prev(struct sway_container *current); bool workspace_is_visible(struct sway_container *ws); +bool workspace_is_empty(struct sway_container *ws); + #endif diff --git a/sway/commands.c b/sway/commands.c index 4a8d11ba3..e9762bef2 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -190,6 +190,7 @@ static struct cmd_handler command_handlers[] = { { "splith", cmd_splith }, { "splitt", cmd_splitt }, { "splitv", cmd_splitv }, + { "sticky", cmd_sticky }, { "swap", cmd_swap }, { "title_format", cmd_title_format }, { "unmark", cmd_unmark }, diff --git a/sway/commands/floating.c b/sway/commands/floating.c index 9e0be9d0c..38a4e1da8 100644 --- a/sway/commands/floating.c +++ b/sway/commands/floating.c @@ -31,31 +31,10 @@ struct cmd_results *cmd_floating(int argc, char **argv) { wants_floating = !container->is_floating; } else { return cmd_results_new(CMD_FAILURE, "floating", - "Expected 'floating "); + "Expected 'floating '"); } - // Change from tiled to floating - if (!container->is_floating && wants_floating) { - struct sway_container *workspace = container_parent( - container, C_WORKSPACE); - container_remove_child(container); - container_add_floating(workspace, container); - - struct sway_output *output = workspace->parent->sway_output; - output_damage_whole_container(output, container); - // Reset to sane size and position - container->width = 640; - container->height = 480; - container->x = workspace->width / 2 - container->width / 2; - container->y = workspace->height / 2 - container->height / 2; - view_autoconfigure(container->sway_view); - output_damage_whole_container(output, container); - - seat_set_focus(config->handler_context.seat, container); - arrange_workspace(workspace); - } else if (container->is_floating && !wants_floating) { - // TODO - } + container_set_floating(container, wants_floating); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/sticky.c b/sway/commands/sticky.c new file mode 100644 index 000000000..4bb4bd391 --- /dev/null +++ b/sway/commands/sticky.c @@ -0,0 +1,40 @@ +#include +#include +#include "sway/commands.h" +#include "sway/input/seat.h" +#include "sway/ipc-server.h" +#include "sway/output.h" +#include "sway/tree/arrange.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" +#include "sway/tree/view.h" +#include "list.h" + +struct cmd_results *cmd_sticky(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "sticky", EXPECTED_EQUAL_TO, 1))) { + return error; + } + struct sway_container *container = + config->handler_context.current_container; + if (!container->is_floating) { + return cmd_results_new(CMD_FAILURE, "sticky", + "Can't set sticky on a tiled container"); + } + + bool wants_sticky; + if (strcasecmp(argv[0], "enable") == 0) { + wants_sticky = true; + } else if (strcasecmp(argv[0], "disable") == 0) { + wants_sticky = false; + } else if (strcasecmp(argv[0], "toggle") == 0) { + wants_sticky = !container->is_sticky; + } else { + return cmd_results_new(CMD_FAILURE, "sticky", + "Expected 'sticky '"); + } + + container->is_sticky = wants_sticky; + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/criteria.c b/sway/criteria.c index dec5fed79..e97b12f83 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -121,12 +121,15 @@ static bool criteria_matches_view(struct criteria *criteria, } if (criteria->floating) { - // TODO - return false; + if (!view->swayc->is_floating) { + return false; + } } if (criteria->tiling) { - // TODO + if (view->swayc->is_floating) { + return false; + } } if (criteria->urgent) { diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 954798198..1d21e80f2 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -754,9 +754,87 @@ static void render_container(struct sway_output *output, case L_TABBED: render_container_tabbed(output, damage, con, parent_focused); break; - case L_FLOATING: - // TODO - break; + } +} + +static bool floater_intersects_output(struct sway_container *floater, + struct sway_container *output) { + struct wlr_box box = { + .x = floater->x, + .y = floater->y, + .width = floater->width, + .height = floater->height, + }; + return wlr_output_layout_intersects(root_container.sway_root->output_layout, + output->sway_output->wlr_output, &box); +} + +static void container_translate(struct sway_container *con, int x, int y) { + con->x += x; + con->y += y; + if (con->type == C_VIEW) { + con->sway_view->x += x; + con->sway_view->y += y; + } else { + for (int i = 0; i < con->children->length; ++i) { + struct sway_container *child = con->children->items[i]; + container_translate(child, x, y); + } + } +} + +static void render_floating_container(struct sway_output *soutput, + pixman_region32_t *damage, struct sway_container *con) { + // We need to translate the floating container's coordinates from layout + // coordinates into output-local coordinates. This needs to happen for all + // children of the floating container too. + struct sway_container *output = container_parent(con, C_OUTPUT); + container_translate(con, -output->x, -output->y); + + if (con->type == C_VIEW) { + struct sway_view *view = con->sway_view; + struct sway_seat *seat = input_manager_current_seat(input_manager); + struct sway_container *focus = seat_get_focus(seat); + struct border_colors *colors; + struct wlr_texture *title_texture; + struct wlr_texture *marks_texture; + + if (focus == con) { + colors = &config->border_colors.focused; + title_texture = con->title_focused; + marks_texture = view->marks_focused; + } else { + colors = &config->border_colors.unfocused; + title_texture = con->title_unfocused; + marks_texture = view->marks_unfocused; + } + render_titlebar(soutput, damage, con, con->x, con->y, con->width, + colors, title_texture, marks_texture); + render_view(soutput, damage, con, colors); + } else { + render_container(soutput, damage, con, false); + } + // Undo the translation + container_translate(con, output->x, output->y); +} + +static void render_floating(struct sway_output *soutput, + pixman_region32_t *damage) { + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + for (int j = 0; j < output->children->length; ++j) { + struct sway_container *workspace = output->children->items[j]; + struct sway_workspace *ws = workspace->sway_workspace; + bool ws_is_visible = workspace_is_visible(workspace); + for (int k = 0; k < ws->floating->children->length; ++k) { + struct sway_container *floater = + ws->floating->children->items[k]; + if ((ws_is_visible || floater->is_sticky) + && floater_intersects_output(floater, soutput->swayc)) { + render_floating_container(soutput, damage, floater); + } + } + } } } @@ -794,8 +872,6 @@ static void render_output(struct sway_output *output, struct timespec *when, goto renderer_end; } - //wlr_renderer_clear(renderer, (float[]){1, 1, 0, 1}); - struct sway_container *workspace = output_get_active_workspace(output); if (workspace->sway_workspace->fullscreen) { @@ -818,7 +894,6 @@ static void render_output(struct sway_output *output, struct timespec *when, } } else { float clear_color[] = {0.25f, 0.25f, 0.25f, 1.0f}; - wlr_renderer_clear(renderer, clear_color); int nrects; pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); @@ -840,6 +915,8 @@ static void render_output(struct sway_output *output, struct timespec *when, &root_container.sway_root->xwayland_unmanaged); render_layer(output, damage, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]); + + render_floating(output, damage); } render_layer(output, damage, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); @@ -916,6 +993,7 @@ static void send_frame_done(struct sway_output *output, struct timespec *when) { struct sway_container *workspace = output_get_active_workspace(output); send_frame_done_container(&data, workspace); + send_frame_done_container(&data, workspace->sway_workspace->floating); send_frame_done_unmanaged(&data, &root_container.sway_root->xwayland_unmanaged); @@ -1037,7 +1115,15 @@ static void output_damage_view(struct sway_output *output, void output_damage_from_view(struct sway_output *output, struct sway_view *view) { - output_damage_view(output, view, false); + if (container_self_or_parent_floating(view->swayc)) { + view->x -= output->swayc->x; + view->y -= output->swayc->y; + output_damage_view(output, view, false); + view->x += output->swayc->x; + view->y += output->swayc->y; + } else { + output_damage_view(output, view, false); + } } static void output_damage_whole_container_iterator(struct sway_container *con, @@ -1053,13 +1139,17 @@ static void output_damage_whole_container_iterator(struct sway_container *con, void output_damage_whole_container(struct sway_output *output, struct sway_container *con) { - float scale = output->wlr_output->scale; struct wlr_box box = { - .x = con->x * scale, - .y = con->y * scale, - .width = con->width * scale, - .height = con->height * scale, + .x = con->x, + .y = con->y, + .width = con->width, + .height = con->height, }; + if (con->is_floating) { + box.x -= output->wlr_output->lx; + box.y -= output->wlr_output->ly; + } + scale_box(&box, output->wlr_output->scale); wlr_output_damage_add_box(output->damage, &box); if (con->type == C_VIEW) { diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index b2b95fa09..e1a73b204 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -98,6 +98,7 @@ static void configure(struct sway_view *view, double ox, double oy, int width, xdg_shell_view->pending_width = width; xdg_shell_view->pending_height = height; wlr_xdg_toplevel_set_size(view->wlr_xdg_surface, width, height); + view_update_position(view, ox, oy); } static void set_activated(struct sway_view *view, bool activated) { @@ -110,6 +111,14 @@ static void set_activated(struct sway_view *view, bool activated) { } } +static void set_maximized(struct sway_view *view, bool maximized) { + if (xdg_shell_view_from_view(view) == NULL) { + return; + } + struct wlr_xdg_surface *surface = view->wlr_xdg_surface; + wlr_xdg_toplevel_set_maximized(surface, maximized); +} + static void set_fullscreen(struct sway_view *view, bool fullscreen) { if (xdg_shell_view_from_view(view) == NULL) { return; @@ -118,6 +127,11 @@ static void set_fullscreen(struct sway_view *view, bool fullscreen) { wlr_xdg_toplevel_set_fullscreen(surface, fullscreen); } +static bool wants_floating(struct sway_view *view) { + // TODO + return false; +} + static void for_each_surface(struct sway_view *view, wlr_surface_iterator_func_t iterator, void *user_data) { if (xdg_shell_view_from_view(view) == NULL) { @@ -154,7 +168,9 @@ static const struct sway_view_impl view_impl = { .get_string_prop = get_string_prop, .configure = configure, .set_activated = set_activated, + .set_maximized = set_maximized, .set_fullscreen = set_fullscreen, + .wants_floating = wants_floating, .for_each_surface = for_each_surface, .close = _close, .destroy = destroy, @@ -164,11 +180,17 @@ static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, commit); struct sway_view *view = &xdg_shell_view->view; - // NOTE: We intentionally discard the view's desired width here - // TODO: Store this for restoration when moving to floating plane - // TODO: Let floating views do whatever - view_update_size(view, xdg_shell_view->pending_width, - xdg_shell_view->pending_height); + struct wlr_box *geometry = &view->wlr_xdg_surface->geometry; + if (!view->natural_width && !view->natural_height) { + view->natural_width = geometry->width; + view->natural_height = geometry->height; + } + if (view->swayc && view->swayc->is_floating) { + view_update_size(view, geometry->width, geometry->height); + } else { + view_update_size(view, xdg_shell_view->pending_width, + xdg_shell_view->pending_height); + } view_update_title(view, false); view_damage_from(view); } diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index d098c7975..47e4162a5 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -97,6 +97,7 @@ static void configure(struct sway_view *view, double ox, double oy, int width, xdg_shell_v6_view->pending_width = width; xdg_shell_v6_view->pending_height = height; wlr_xdg_toplevel_v6_set_size(view->wlr_xdg_surface_v6, width, height); + view_update_position(view, ox, oy); } static void set_activated(struct sway_view *view, bool activated) { @@ -109,6 +110,14 @@ static void set_activated(struct sway_view *view, bool activated) { } } +static void set_maximized(struct sway_view *view, bool maximized) { + if (xdg_shell_v6_view_from_view(view) == NULL) { + return; + } + struct wlr_xdg_surface_v6 *surface = view->wlr_xdg_surface_v6; + wlr_xdg_toplevel_v6_set_maximized(surface, maximized); +} + static void set_fullscreen(struct sway_view *view, bool fullscreen) { if (xdg_shell_v6_view_from_view(view) == NULL) { return; @@ -117,6 +126,11 @@ static void set_fullscreen(struct sway_view *view, bool fullscreen) { wlr_xdg_toplevel_v6_set_fullscreen(surface, fullscreen); } +static bool wants_floating(struct sway_view *view) { + // TODO + return false; +} + static void for_each_surface(struct sway_view *view, wlr_surface_iterator_func_t iterator, void *user_data) { if (xdg_shell_v6_view_from_view(view) == NULL) { @@ -153,7 +167,9 @@ static const struct sway_view_impl view_impl = { .get_string_prop = get_string_prop, .configure = configure, .set_activated = set_activated, + .set_maximized = set_maximized, .set_fullscreen = set_fullscreen, + .wants_floating = wants_floating, .for_each_surface = for_each_surface, .close = _close, .destroy = destroy, @@ -163,11 +179,17 @@ static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xdg_shell_v6_view *xdg_shell_v6_view = wl_container_of(listener, xdg_shell_v6_view, commit); struct sway_view *view = &xdg_shell_v6_view->view; - // NOTE: We intentionally discard the view's desired width here - // TODO: Store this for restoration when moving to floating plane - // TODO: Let floating views do whatever - view_update_size(view, xdg_shell_v6_view->pending_width, - xdg_shell_v6_view->pending_height); + struct wlr_box *geometry = &view->wlr_xdg_surface_v6->geometry; + if (!view->natural_width && !view->natural_height) { + view->natural_width = geometry->width; + view->natural_height = geometry->height; + } + if (view->swayc && view->swayc->is_floating) { + view_update_size(view, geometry->width, geometry->height); + } else { + view_update_size(view, xdg_shell_v6_view->pending_width, + xdg_shell_v6_view->pending_height); + } view_update_title(view, false); view_damage_from(view); } diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 6a99a66aa..56cac1bd9 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -152,7 +152,9 @@ static uint32_t get_int_prop(struct sway_view *view, enum sway_view_prop prop) { } } -static void configure(struct sway_view *view, double ox, double oy, int width, +// The x and y arguments are output-local for tiled views, and layout +// coordinates for floating views. +static void configure(struct sway_view *view, double x, double y, int width, int height) { struct sway_xwayland_view *xwayland_view = xwayland_view_from_view(view); if (xwayland_view == NULL) { @@ -160,25 +162,33 @@ static void configure(struct sway_view *view, double ox, double oy, int width, } struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; - struct sway_container *output = container_parent(view->swayc, C_OUTPUT); - if (!sway_assert(output, "view must be within tree to set position")) { - return; - } - struct sway_container *root = container_parent(output, C_ROOT); - if (!sway_assert(root, "output must be within tree to set position")) { - return; - } - struct wlr_output_layout *layout = root->sway_root->output_layout; - struct wlr_output_layout_output *loutput = - wlr_output_layout_get(layout, output->sway_output->wlr_output); - if (!sway_assert(loutput, "output must be within layout to set position")) { - return; + double lx, ly; + if (view->swayc->is_floating) { + lx = x; + ly = y; + } else { + struct sway_container *output = container_parent(view->swayc, C_OUTPUT); + if (!sway_assert(output, "view must be within tree to set position")) { + return; + } + struct sway_container *root = container_parent(output, C_ROOT); + if (!sway_assert(root, "output must be within tree to set position")) { + return; + } + struct wlr_output_layout *layout = root->sway_root->output_layout; + struct wlr_output_layout_output *loutput = + wlr_output_layout_get(layout, output->sway_output->wlr_output); + if (!sway_assert(loutput, + "output must be within layout to set position")) { + return; + } + lx = x + loutput->x; + ly = y + loutput->y; } xwayland_view->pending_width = width; xwayland_view->pending_height = height; - wlr_xwayland_surface_configure(xsurface, ox + loutput->x, oy + loutput->y, - width, height); + wlr_xwayland_surface_configure(xsurface, lx, ly, width, height); } static void set_activated(struct sway_view *view, bool activated) { @@ -189,6 +199,14 @@ static void set_activated(struct sway_view *view, bool activated) { wlr_xwayland_surface_activate(surface, activated); } +static void set_maximized(struct sway_view *view, bool maximized) { + if (xwayland_view_from_view(view) == NULL) { + return; + } + struct wlr_xwayland_surface *surface = view->wlr_xwayland_surface; + wlr_xwayland_surface_set_maximized(surface, maximized); +} + static void set_fullscreen(struct sway_view *view, bool fullscreen) { if (xwayland_view_from_view(view) == NULL) { return; @@ -197,6 +215,35 @@ static void set_fullscreen(struct sway_view *view, bool fullscreen) { wlr_xwayland_surface_set_fullscreen(surface, fullscreen); } +static bool wants_floating(struct sway_view *view) { + // TODO: + // We want to return true if the window type contains any of these: + // NET_WM_WINDOW_TYPE_DIALOG + // NET_WM_WINDOW_TYPE_UTILITY + // NET_WM_WINDOW_TYPE_TOOLBAR + // NET_WM_WINDOW_TYPE_SPLASH + // + // We also want to return true if the NET_WM_STATE is MODAL. + // wlroots doesn't appear to provide all this information at the moment. + struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; + uint32_t *atom = xsurface->window_type; + for (size_t i = 0; i < xsurface->window_type_len; ++i) { + wlr_log(L_DEBUG, "xwayland window type %i", *atom); + // TODO: Come up with a better way of doing this + switch (*atom) { + case 36: // NET_WM_WINDOW_TYPE_UTILITY + case 44: // NET_WM_WINDOW_TYPE_SPLASH + case 276: // ? PGP passphrase dialog + case 337: // ? Firefox open file dialog + case 338: // ? Firefox open file dialog + return true; + } + ++atom; + } + + return false; +} + static void _close(struct sway_view *view) { if (xwayland_view_from_view(view) == NULL) { return; @@ -225,7 +272,9 @@ static const struct sway_view_impl view_impl = { .get_int_prop = get_int_prop, .configure = configure, .set_activated = set_activated, + .set_maximized = set_maximized, .set_fullscreen = set_fullscreen, + .wants_floating = wants_floating, .close = _close, .destroy = destroy, }; @@ -234,10 +283,18 @@ static void handle_commit(struct wl_listener *listener, void *data) { struct sway_xwayland_view *xwayland_view = wl_container_of(listener, xwayland_view, commit); struct sway_view *view = &xwayland_view->view; - // NOTE: We intentionally discard the view's desired width here - // TODO: Let floating views do whatever - view_update_size(view, xwayland_view->pending_width, - xwayland_view->pending_height); + struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; + if (!view->natural_width && !view->natural_height) { + view->natural_width = xsurface->width; + view->natural_height = xsurface->height; + } + if (view->swayc && view->swayc->is_floating) { + view_update_size(view, xsurface->width, xsurface->height); + view_update_position(view, xsurface->x, xsurface->y); + } else { + view_update_size(view, xwayland_view->pending_width, + xwayland_view->pending_height); + } view_damage_from(view); } diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 6751931d0..96bf574c2 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -108,6 +108,9 @@ static struct sway_container *container_at_coords( } struct sway_container *c; + if ((c = floating_container_at(x, y, surface, sx, sy))) { + return c; + } if ((c = container_at(ws, ox, oy, surface, sx, sy))) { return c; } diff --git a/sway/input/seat.c b/sway/input/seat.c index 0295212cd..6a266fbac 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -113,7 +113,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) { - if (container->type == C_VIEW || container->children->length == 0) { + if (container->type == C_VIEW) { + return container; + } + + struct sway_container *floating = container->type == C_WORKSPACE ? + container->sway_workspace->floating : NULL; + if (container->children->length == 0 && + (!floating || floating->children->length == 0)) { return container; } @@ -126,6 +133,9 @@ static struct sway_container *seat_get_focus_by_type(struct sway_seat *seat, if (container_has_child(container, current->container)) { return current->container; } + if (floating && container_has_child(floating, current->container)) { + return current->container; + } } return NULL; @@ -568,7 +578,7 @@ void seat_set_focus_warp(struct sway_seat *seat, // clean up unfocused empty workspace on new output if (new_output_last_ws) { if (!workspace_is_visible(new_output_last_ws) - && new_output_last_ws->children->length == 0) { + && workspace_is_empty(new_output_last_ws)) { if (last_workspace == new_output_last_ws) { last_focus = NULL; last_workspace = NULL; @@ -581,7 +591,7 @@ void seat_set_focus_warp(struct sway_seat *seat, if (last_workspace) { ipc_event_workspace(last_workspace, container, "focus"); if (!workspace_is_visible(last_workspace) - && last_workspace->children->length == 0) { + && workspace_is_empty(last_workspace)) { if (last_workspace == last_focus) { last_focus = NULL; } diff --git a/sway/ipc-json.c b/sway/ipc-json.c index da4bef60b..4d7024a8a 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -4,6 +4,7 @@ #include "log.h" #include "sway/ipc-json.h" #include "sway/tree/container.h" +#include "sway/tree/workspace.h" #include "sway/output.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" @@ -150,6 +151,15 @@ static void ipc_json_describe_workspace(struct sway_container *workspace, const char *layout = ipc_json_layout_description(workspace->layout); json_object_object_add(object, "layout", json_object_new_string(layout)); + + // Floating + json_object *floating_array = json_object_new_array(); + struct sway_container *floating = workspace->sway_workspace->floating; + for (int i = 0; i < floating->children->length; ++i) { + struct sway_container *floater = floating->children->items[i]; + json_object_array_add(floating_array, ipc_json_describe_container_recursive(floater)); + } + json_object_object_add(object, "floating_nodes", floating_array); } static void ipc_json_describe_view(struct sway_container *c, json_object *object) { diff --git a/sway/meson.build b/sway/meson.build index 5618a2572..4c0385830 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -65,6 +65,7 @@ sway_sources = files( 'commands/set.c', 'commands/show_marks.c', 'commands/split.c', + 'commands/sticky.c', 'commands/swaybg_command.c', 'commands/swap.c', 'commands/title_format.c', diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index bdef56eaa..da4f7889b 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -247,6 +247,18 @@ void arrange_children_of(struct sway_container *parent) { arrange_children_of(child); } } + + // If container is a workspace, process floating containers too + if (parent->type == C_WORKSPACE) { + struct sway_workspace *ws = workspace->sway_workspace; + for (int i = 0; i < ws->floating->children->length; ++i) { + struct sway_container *child = ws->floating->children->items[i]; + if (child->type != C_VIEW) { + arrange_children_of(child); + } + } + } + container_damage_whole(parent); update_debug_tree(); } diff --git a/sway/tree/container.c b/sway/tree/container.c index 33d88d7f8..f9a0474cd 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -123,6 +123,7 @@ struct sway_container *container_create(enum sway_container_type type) { c->layout = L_NONE; c->type = type; c->alpha = 1.0f; + c->reapable = true; if (type != C_VIEW) { c->children = create_list(); @@ -189,14 +190,13 @@ static struct sway_container *container_workspace_destroy( } struct sway_container *parent = workspace->parent; - if (workspace->children->length == 0) { - // destroy the WS if there are no children (TODO check for floating) + if (workspace_is_empty(workspace)) { + // destroy the WS if there are no children wlr_log(L_DEBUG, "destroying workspace '%s'", workspace->name); ipc_event_workspace(workspace, NULL, "empty"); } else if (output) { // Move children to a different workspace on this output struct sway_container *new_workspace = NULL; - // TODO move floating for (int i = 0; i < output->children->length; i++) { if (output->children->items[i] != workspace) { new_workspace = output->children->items[i]; @@ -209,6 +209,11 @@ static struct sway_container *container_workspace_destroy( for (int i = 0; i < workspace->children->length; i++) { container_move_to(workspace->children->items[i], new_workspace); } + struct sway_container *floating = workspace->sway_workspace->floating; + for (int i = 0; i < floating->children->length; i++) { + container_move_to(floating->children->items[i], + new_workspace->sway_workspace->floating); + } } free(workspace->sway_workspace); @@ -275,13 +280,16 @@ static void container_root_finish(struct sway_container *con) { } bool container_reap_empty(struct sway_container *con) { + if (!con->reapable) { + return false; + } switch (con->type) { case C_ROOT: case C_OUTPUT: // dont reap these break; case C_WORKSPACE: - if (!workspace_is_visible(con) && con->children->length == 0) { + if (!workspace_is_visible(con) && workspace_is_empty(con)) { wlr_log(L_DEBUG, "Destroying workspace via reaper"); container_workspace_destroy(con); return true; @@ -436,7 +444,6 @@ struct sway_container *container_find(struct sway_container *container, if (!container->children) { return NULL; } - // TODO: floating windows for (int i = 0; i < container->children->length; ++i) { struct sway_container *child = container->children->items[i]; if (test(child, data)) { @@ -448,6 +455,9 @@ struct sway_container *container_find(struct sway_container *container, } } } + if (container->type == C_WORKSPACE) { + return container_find(container->sway_workspace->floating, test, data); + } return NULL; } @@ -608,8 +618,6 @@ struct sway_container *container_at(struct sway_container *parent, return container_at_tabbed(parent, ox, oy, surface, sx, sy); case L_STACKED: return container_at_stacked(parent, ox, oy, surface, sx, sy); - case L_FLOATING: - return NULL; // TODO case L_NONE: return NULL; } @@ -617,6 +625,34 @@ struct sway_container *container_at(struct sway_container *parent, return NULL; } +struct sway_container *floating_container_at(double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy) { + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + for (int j = 0; j < output->children->length; ++j) { + struct sway_container *workspace = output->children->items[j]; + struct sway_workspace *ws = workspace->sway_workspace; + bool ws_is_visible = workspace_is_visible(workspace); + for (int k = 0; k < ws->floating->children->length; ++k) { + struct sway_container *floater = + ws->floating->children->items[k]; + if (ws_is_visible || floater->is_sticky) { + struct wlr_box box = { + .x = floater->x, + .y = floater->y, + .width = floater->width, + .height = floater->height, + }; + if (wlr_box_contains_point(&box, lx, ly)) { + return container_at(floater, lx, ly, surface, sx, sy); + } + } + } + } + } + return NULL; +} + void container_for_each_descendant_dfs(struct sway_container *container, void (*f)(struct sway_container *container, void *data), void *data) { @@ -674,7 +710,7 @@ static bool find_child_func(struct sway_container *con, void *data) { bool container_has_child(struct sway_container *con, struct sway_container *child) { - if (con == NULL || con->type == C_VIEW || con->children->length == 0) { + if (con == NULL || con->type == C_VIEW) { return false; } return container_find(con, find_child_func, child); @@ -806,9 +842,6 @@ static size_t get_tree_representation(struct sway_container *parent, char *buffe case L_STACKED: lenient_strcat(buffer, "S["); break; - case L_FLOATING: - lenient_strcat(buffer, "F["); - break; case L_NONE: lenient_strcat(buffer, "D["); break; @@ -866,3 +899,81 @@ void container_notify_subtree_changed(struct sway_container *container) { size_t container_titlebar_height() { return config->font_height + TITLEBAR_V_PADDING * 2; } + +static void configure_floating_view(struct sway_view *view) { + struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); + int max_width = ws->width * 0.6666; + int max_height = ws->height * 0.6666; + int width = + view->natural_width > max_width ? max_width : view->natural_width; + int height = + view->natural_height > max_height ? max_height : view->natural_height; + struct sway_container *output = ws->parent; + int lx = output->x + (ws->width - width) / 2; + int ly = output->y + (ws->height - height) / 2; + + view->border_left = view->border_right = view->border_bottom = true; + view_set_maximized(view, false); + view_configure(view, lx, ly, width, height); +} + +void container_set_floating(struct sway_container *container, bool enable) { + if (container->is_floating == enable) { + return; + } + + struct sway_container *workspace = container_parent(container, C_WORKSPACE); + struct sway_seat *seat = input_manager_current_seat(input_manager); + container_damage_whole(container); + + if (enable) { + container_remove_child(container); + container_add_child(workspace->sway_workspace->floating, container); + container->is_floating = true; + if (container->type == C_VIEW) { + configure_floating_view(container->sway_view); + } + seat_set_focus(seat, seat_get_focus_inactive(seat, container)); + container_reap_empty_recursive(workspace); + } else { + // Returning to tiled + container_remove_child(container); + container_add_child(workspace, container); + container->width = container->parent->width; + container->height = container->parent->height; + if (container->type == C_VIEW) { + view_set_maximized(container->sway_view, true); + } + container->is_floating = false; + container->is_sticky = false; + container_reap_empty_recursive(workspace->sway_workspace->floating); + } + arrange_workspace(workspace); + container_damage_whole(container); +} + +void container_set_geometry_from_view(struct sway_container *container) { + if (!sway_assert(container->type == C_VIEW, "Expected a view")) { + return; + } + if (!sway_assert(container->is_floating, "Expected a floating view")) { + return; + } + struct sway_view *view = container->sway_view; + size_t border_width = view->border_thickness * (view->border != B_NONE); + size_t top = + view->border == B_NORMAL ? container_titlebar_height() : border_width; + + container->x = view->x - border_width; + container->y = view->y - top; + container->width = view->width + border_width * 2; + container->height = top + view->height + border_width; +} + +bool container_self_or_parent_floating(struct sway_container *container) { + while (container->parent->type != C_WORKSPACE + && container->parent->parent->type != C_WORKSPACE) { + container = container->parent; + } + return container->is_floating; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 7bbeb4b18..59ad0b533 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -45,20 +45,14 @@ void layout_init(void) { } static int index_child(const struct sway_container *child) { - // TODO handle floating struct sway_container *parent = child->parent; - int i, len; - len = parent->children->length; - for (i = 0; i < len; ++i) { + for (int i = 0; i < parent->children->length; ++i) { if (parent->children->items[i] == child) { - break; + return i; } } - - if (!sway_assert(i < len, "Stray container")) { - return -1; - } - return i; + // This happens if the child is a floating container + return -1; } static void container_handle_fullscreen_reparent(struct sway_container *viewcon, @@ -142,26 +136,11 @@ struct sway_container *container_remove_child(struct sway_container *child) { } struct sway_container *parent = child->parent; - if (!child->is_floating) { - for (int i = 0; i < parent->children->length; ++i) { - if (parent->children->items[i] == child) { - list_del(parent->children, i); - break; - } + for (int i = 0; i < parent->children->length; ++i) { + if (parent->children->items[i] == child) { + list_del(parent->children, i); + break; } - } else { - if (!sway_assert(parent->type == C_WORKSPACE && child->type == C_VIEW, - "Found floating non-view and/or in non-workspace")) { - return parent; - } - struct sway_workspace *ws = parent->sway_workspace; - for (int i = 0; i < ws->floating->length; ++i) { - if (ws->floating->items[i] == child) { - list_del(ws->floating, i); - break; - } - } - child->is_floating = false; } child->parent = NULL; container_notify_subtree_changed(parent); @@ -169,32 +148,16 @@ struct sway_container *container_remove_child(struct sway_container *child) { return parent; } -void container_add_floating(struct sway_container *workspace, - struct sway_container *child) { - if (!sway_assert(workspace->type == C_WORKSPACE && child->type == C_VIEW, - "Attempted to float non-view and/or in non-workspace")) { - return; - } - if (!sway_assert(!child->parent, - "child already has a parent (invalid call)")) { - return; - } - if (!sway_assert(!child->is_floating, - "child is already floating (invalid state)")) { - return; - } - struct sway_workspace *ws = workspace->sway_workspace; - list_add(ws->floating, child); - child->parent = workspace; - child->is_floating = true; -} - void container_move_to(struct sway_container *container, struct sway_container *destination) { if (container == destination || container_has_ancestor(container, destination)) { return; } + if (container->is_floating) { + // TODO + return; + } struct sway_container *old_parent = container_remove_child(container); container->width = container->height = 0; container->saved_width = container->saved_height = 0; @@ -207,8 +170,9 @@ void container_move_to(struct sway_container *container, } wl_signal_emit(&container->events.reparent, old_parent); if (container->type == C_WORKSPACE) { - struct sway_seat *seat = input_manager_get_default_seat( - input_manager); + // If moving a workspace to a new output, maybe create a new workspace + // on the previous output + struct sway_seat *seat = input_manager_get_default_seat(input_manager); if (old_parent->children->length == 0) { char *ws_name = workspace_next_name(old_parent->name); struct sway_container *ws = @@ -754,6 +718,10 @@ struct sway_container *container_get_in_direction( enum movement_direction dir) { struct sway_container *parent = container->parent; + if (container->is_floating) { + return NULL; + } + if (container->type == C_VIEW && container->sway_view->is_fullscreen) { if (dir == MOVE_PARENT || dir == MOVE_CHILD) { return NULL; @@ -778,6 +746,9 @@ struct sway_container *container_get_in_direction( bool can_move = false; int desired; int idx = index_child(container); + if (idx == -1) { + return NULL; + } if (parent->type == C_ROOT) { enum wlr_direction wlr_dir = 0; if (!sway_assert(sway_dir_to_wlr(dir, &wlr_dir), diff --git a/sway/tree/view.c b/sway/tree/view.c index 26ff1e8de..651a2be65 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -158,21 +158,19 @@ void view_autoconfigure(struct sway_view *view) { view->border_top = view->border_bottom = true; view->border_left = view->border_right = true; - if (view->swayc->layout != L_FLOATING) { - if (config->hide_edge_borders == E_BOTH - || config->hide_edge_borders == E_VERTICAL - || (config->hide_edge_borders == E_SMART && !other_views)) { - view->border_left = view->swayc->x != ws->x; - int right_x = view->swayc->x + view->swayc->width; - view->border_right = right_x != ws->x + ws->width; - } - if (config->hide_edge_borders == E_BOTH - || config->hide_edge_borders == E_HORIZONTAL - || (config->hide_edge_borders == E_SMART && !other_views)) { - view->border_top = view->swayc->y != ws->y; - int bottom_y = view->swayc->y + view->swayc->height; - view->border_bottom = bottom_y != ws->y + ws->height; - } + if (config->hide_edge_borders == E_BOTH + || config->hide_edge_borders == E_VERTICAL + || (config->hide_edge_borders == E_SMART && !other_views)) { + view->border_left = view->swayc->x != ws->x; + int right_x = view->swayc->x + view->swayc->width; + view->border_right = right_x != ws->x + ws->width; + } + if (config->hide_edge_borders == E_BOTH + || config->hide_edge_borders == E_HORIZONTAL + || (config->hide_edge_borders == E_SMART && !other_views)) { + view->border_top = view->swayc->y != ws->y; + int bottom_y = view->swayc->y + view->swayc->height; + view->border_bottom = bottom_y != ws->y + ws->height; } double x, y, width, height; @@ -184,11 +182,11 @@ void view_autoconfigure(struct sway_view *view) { // disable any top border because we'll always have the title bar. if (view->swayc->parent->layout == L_TABBED) { y_offset = container_titlebar_height(); - view->border_top = 0; + view->border_top = false; } else if (view->swayc->parent->layout == L_STACKED) { y_offset = container_titlebar_height() * view->swayc->parent->children->length; - view->border_top = 0; + view->border_top = false; } switch (view->border) { @@ -237,6 +235,12 @@ void view_set_activated(struct sway_view *view, bool activated) { } } +void view_set_maximized(struct sway_view *view, bool maximized) { + if (view->impl->set_maximized) { + view->impl->set_maximized(view, maximized); + } +} + // Set fullscreen, but without IPC events or arranging windows. void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) { if (view->is_fullscreen == fullscreen) { @@ -452,6 +456,11 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { // TODO: CT_ASSIGN_OUTPUT } } + // If we're about to launch the view into the floating container, then + // launch it as a tiled view in the root of the workspace instead. + if (focus->is_floating) { + focus = focus->parent->parent; + } free(criterias); cont = container_view_create(focus, view); @@ -468,7 +477,12 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { wl_signal_add(&view->swayc->events.reparent, &view->container_reparent); view->container_reparent.notify = view_handle_container_reparent; - arrange_children_of(cont->parent); + if (view->impl->wants_floating && view->impl->wants_floating(view)) { + container_set_floating(view->swayc, true); + } else { + arrange_children_of(cont->parent); + } + input_manager_set_focus(input_manager, cont); if (workspace) { workspace_switch(workspace); @@ -516,16 +530,14 @@ void view_unmap(struct sway_view *view) { } } -void view_update_position(struct sway_view *view, double ox, double oy) { - if (view->swayc->x == ox && view->swayc->y == oy) { +void view_update_position(struct sway_view *view, double lx, double ly) { + if (!view->swayc->is_floating) { return; } - - // TODO: Only allow this if the view is floating (this function will only be - // called in response to wayland clients wanting to reposition themselves). container_damage_whole(view->swayc); - view->swayc->x = ox; - view->swayc->y = oy; + view->x = lx; + view->y = ly; + container_set_geometry_from_view(view->swayc); container_damage_whole(view->swayc); } @@ -533,15 +545,15 @@ void view_update_size(struct sway_view *view, int width, int height) { if (view->width == width && view->height == height) { return; } - container_damage_whole(view->swayc); - // Should we update the swayc width/height here too? view->width = width; view->height = height; + if (view->swayc->is_floating) { + container_set_geometry_from_view(view->swayc); + } container_damage_whole(view->swayc); } - static void view_subsurface_create(struct sway_view *view, struct wlr_subsurface *subsurface) { struct sway_view_child *child = calloc(1, sizeof(struct sway_view_child)); @@ -888,6 +900,19 @@ bool view_is_visible(struct sway_view *view) { if (!view->swayc) { return false; } + struct sway_container *workspace = + container_parent(view->swayc, C_WORKSPACE); + // Determine if view is nested inside a floating container which is sticky. + // A simple floating view will have this ancestry: + // C_VIEW (is_floating=true) -> floating -> workspace + // A more complex ancestry could be: + // C_VIEW -> C_CONTAINER (tabbed and is_floating) -> floating -> workspace + struct sway_container *floater = view->swayc; + while (floater->parent->type != C_WORKSPACE + && floater->parent->parent->type != C_WORKSPACE) { + floater = floater->parent; + } + bool is_sticky = floater->is_floating && floater->is_sticky; // Check view isn't in a tabbed or stacked container on an inactive tab struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_container *container = view->swayc; @@ -901,10 +926,12 @@ bool view_is_visible(struct sway_view *view) { container = container->parent; } // Check view isn't hidden by another fullscreen view - struct sway_container *workspace = container; if (workspace->sway_workspace->fullscreen && !view->is_fullscreen) { return false; } // Check the workspace is visible - return workspace_is_visible(workspace); + if (!is_sticky) { + return workspace_is_visible(workspace); + } + return true; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index c4f8ac5ee..5bef409aa 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -65,7 +65,9 @@ struct sway_container *workspace_create(struct sway_container *output, return NULL; } swayws->swayc = workspace; - swayws->floating = create_list(); + swayws->floating = container_create(C_CONTAINER); + swayws->floating->parent = swayws->swayc; + swayws->floating->reapable = false; workspace->sway_workspace = swayws; container_add_child(output, workspace); @@ -408,3 +410,16 @@ bool workspace_is_visible(struct sway_container *ws) { } return focus == ws; } + +bool workspace_is_empty(struct sway_container *ws) { + if (!sway_assert(ws->type == C_WORKSPACE, "Expected a workspace")) { + return false; + } + if (ws->children->length) { + return false; + } + if (ws->sway_workspace->floating->children->length) { + return false; + } + return true; +}