From 7b138e5ef0f679c9bb0078019d7c9c63fef36273 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Mon, 24 Sep 2018 20:54:57 +1000 Subject: [PATCH 1/5] Add CSD to border modes This replaces view.using_csd with a new border mode: B_CSD. This also removes sway_xdg_shell{_v6}_view.deco_mode and view->has_client_side_decorations as we can now get these from the border. You can use `border toggle` to cycle through the modes including CSD, or use `border csd` to set it directly. The client must support the xdg-decoration protocol, and the only client I know of that does is the example in wlroots. If the client switches from SSD to CSD without us expecting it (via the server-decoration protocol), we stash the previous border type into view.saved_border so we can restore it if the client returns to SSD. I haven't found a way to test this though. --- include/sway/server.h | 5 ++ include/sway/tree/container.h | 2 +- include/sway/tree/view.h | 21 +++++-- include/sway/xdg_decoration.h | 19 ++++++ sway/commands/border.c | 21 +++++-- sway/decoration.c | 22 +++---- sway/desktop/render.c | 110 ++++++++++++++++------------------ sway/desktop/transaction.c | 1 - sway/desktop/xdg_shell.c | 22 ++----- sway/desktop/xdg_shell_v6.c | 22 ++----- sway/desktop/xwayland.c | 20 ++++--- sway/input/cursor.c | 3 +- sway/ipc-json.c | 2 + sway/meson.build | 1 + sway/server.c | 9 +++ sway/tree/container.c | 2 +- sway/tree/view.c | 37 +++++++----- sway/xdg_decoration.c | 73 ++++++++++++++++++++++ 18 files changed, 251 insertions(+), 141 deletions(-) create mode 100644 include/sway/xdg_decoration.h create mode 100644 sway/xdg_decoration.c diff --git a/include/sway/server.h b/include/sway/server.h index 5dabb61f4..3e1cbb33d 100644 --- a/include/sway/server.h +++ b/include/sway/server.h @@ -54,6 +54,10 @@ struct sway_server { struct wl_listener server_decoration; struct wl_list decorations; // sway_server_decoration::link + struct wlr_xdg_decoration_manager_v1 *xdg_decoration_manager; + struct wl_listener xdg_decoration; + struct wl_list xdg_decorations; // sway_xdg_decoration::link + size_t txn_timeout_ms; list_t *transactions; list_t *dirty_nodes; @@ -78,5 +82,6 @@ void handle_xdg_shell_surface(struct wl_listener *listener, void *data); void handle_xwayland_surface(struct wl_listener *listener, void *data); #endif void handle_server_decoration(struct wl_listener *listener, void *data); +void handle_xdg_decoration(struct wl_listener *listener, void *data); #endif diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 5e281a2f8..6019602c9 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -28,6 +28,7 @@ enum sway_container_border { B_NONE, B_PIXEL, B_NORMAL, + B_CSD, }; struct sway_root; @@ -63,7 +64,6 @@ struct sway_container_state { bool border_bottom; bool border_left; bool border_right; - bool using_csd; }; struct sway_container { diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index d10251dda..49df7c88f 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -11,6 +11,7 @@ #include "sway/input/seat.h" struct sway_container; +struct sway_xdg_decoration; enum sway_view_type { SWAY_VIEW_XDG_SHELL_V6, @@ -44,7 +45,6 @@ struct sway_view_impl { void (*set_tiled)(struct sway_view *view, bool tiled); void (*set_fullscreen)(struct sway_view *view, bool fullscreen); bool (*wants_floating)(struct sway_view *view); - bool (*has_client_side_decorations)(struct sway_view *view); void (*for_each_surface)(struct sway_view *view, wlr_surface_iterator_func_t iterator, void *user_data); void (*for_each_popup)(struct sway_view *view, @@ -60,6 +60,7 @@ struct sway_view { struct sway_container *container; // NULL if unmapped and transactions finished struct wlr_surface *surface; // NULL for unmapped views + struct sway_xdg_decoration *xdg_decoration; pid_t pid; @@ -76,12 +77,12 @@ struct sway_view { char *title_format; enum sway_container_border border; + enum sway_container_border saved_border; int border_thickness; bool border_top; bool border_bottom; bool border_left; bool border_right; - bool using_csd; struct timespec urgent; bool allow_request_urgent; @@ -127,8 +128,6 @@ struct sway_view { struct sway_xdg_shell_v6_view { struct sway_view view; - enum wlr_server_decoration_manager_mode deco_mode; - struct wl_listener commit; struct wl_listener request_move; struct wl_listener request_resize; @@ -145,8 +144,6 @@ struct sway_xdg_shell_v6_view { struct sway_xdg_shell_view { struct sway_view view; - enum wlr_server_decoration_manager_mode deco_mode; - struct wl_listener commit; struct wl_listener request_move; struct wl_listener request_resize; @@ -175,6 +172,7 @@ struct sway_xwayland_view { struct wl_listener set_role; struct wl_listener set_window_type; struct wl_listener set_hints; + struct wl_listener set_decorations; struct wl_listener map; struct wl_listener unmap; struct wl_listener destroy; @@ -268,6 +266,17 @@ void view_set_activated(struct sway_view *view, bool activated); */ void view_request_activate(struct sway_view *view); +/** + * If possible, instructs the client to change their decoration mode. + */ +void view_set_csd_from_server(struct sway_view *view, bool enabled); + +/** + * Updates the view's border setting when the client unexpectedly changes their + * decoration mode. + */ +void view_set_csd_from_client(struct sway_view *view, bool enabled); + void view_set_tiled(struct sway_view *view, bool tiled); void view_close(struct sway_view *view); diff --git a/include/sway/xdg_decoration.h b/include/sway/xdg_decoration.h new file mode 100644 index 000000000..46fb8d34c --- /dev/null +++ b/include/sway/xdg_decoration.h @@ -0,0 +1,19 @@ +#ifndef _SWAY_XDG_DECORATION_H +#define _SWAY_XDG_DECORATION_H + +#include + +struct sway_xdg_decoration { + struct wlr_xdg_toplevel_decoration_v1 *wlr_xdg_decoration; + struct wl_list link; + + struct sway_view *view; + + struct wl_listener destroy; + struct wl_listener surface_commit; +}; + +struct sway_xdg_decoration *xdg_decoration_from_surface( + struct wlr_surface *surface); + +#endif diff --git a/sway/commands/border.c b/sway/commands/border.c index 95498b2f8..673fea08a 100644 --- a/sway/commands/border.c +++ b/sway/commands/border.c @@ -7,6 +7,17 @@ #include "sway/tree/container.h" #include "sway/tree/view.h" +static void set_border(struct sway_view *view, + enum sway_container_border new_border) { + if (view->border == B_CSD && new_border != B_CSD) { + view_set_csd_from_server(view, false); + } else if (view->border != B_CSD && new_border == B_CSD) { + view_set_csd_from_server(view, true); + } + view->saved_border = view->border; + view->border = new_border; +} + struct cmd_results *cmd_border(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "border", EXPECTED_AT_LEAST, 1))) { @@ -21,13 +32,15 @@ struct cmd_results *cmd_border(int argc, char **argv) { struct sway_view *view = container->view; if (strcmp(argv[0], "none") == 0) { - view->border = B_NONE; + set_border(view, B_NONE); } else if (strcmp(argv[0], "normal") == 0) { - view->border = B_NORMAL; + set_border(view, B_NORMAL); } else if (strcmp(argv[0], "pixel") == 0) { - view->border = B_PIXEL; + set_border(view, B_PIXEL); + } else if (strcmp(argv[0], "csd") == 0) { + set_border(view, B_CSD); } else if (strcmp(argv[0], "toggle") == 0) { - view->border = (view->border + 1) % 3; + set_border(view, (view->border + 1) % 4); } else { return cmd_results_new(CMD_INVALID, "border", "Expected 'border ' " diff --git a/sway/decoration.c b/sway/decoration.c index 0e3e67ac8..fea6ed4c0 100644 --- a/sway/decoration.c +++ b/sway/decoration.c @@ -1,6 +1,8 @@ #include #include "sway/decoration.h" +#include "sway/desktop/transaction.h" #include "sway/server.h" +#include "sway/tree/arrange.h" #include "sway/tree/view.h" #include "log.h" @@ -24,20 +26,12 @@ static void server_decoration_handle_mode(struct wl_listener *listener, return; } - switch (view->type) { - case SWAY_VIEW_XDG_SHELL_V6:; - struct sway_xdg_shell_v6_view *xdg_shell_v6_view = - (struct sway_xdg_shell_v6_view *)view; - xdg_shell_v6_view->deco_mode = deco->wlr_server_decoration->mode; - break; - case SWAY_VIEW_XDG_SHELL:; - struct sway_xdg_shell_view *xdg_shell_view = - (struct sway_xdg_shell_view *)view; - xdg_shell_view->deco_mode = deco->wlr_server_decoration->mode; - break; - default: - break; - } + bool csd = deco->wlr_server_decoration->mode == + WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; + view_set_csd_from_client(view, csd); + + arrange_container(view->container); + transaction_commit_dirty(); } void handle_server_decoration(struct wl_listener *listener, void *data) { diff --git a/sway/desktop/render.c b/sway/desktop/render.c index af4e29057..c8b08a585 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -272,7 +272,7 @@ static void render_view(struct sway_output *output, pixman_region32_t *damage, render_view_toplevels(view, output, damage, view->container->alpha); } - if (view->container->current.using_csd) { + if (con->current.border == B_NONE || con->current.border == B_CSD) { return; } @@ -281,51 +281,49 @@ static void render_view(struct sway_output *output, pixman_region32_t *damage, float color[4]; struct sway_container_state *state = &con->current; - if (state->border != B_NONE) { - if (state->border_left) { + if (state->border_left) { + memcpy(&color, colors->child_border, sizeof(float) * 4); + premultiply_alpha(color, con->alpha); + box.x = state->con_x; + box.y = state->view_y; + box.width = state->border_thickness; + box.height = state->view_height; + scale_box(&box, output_scale); + render_rect(output->wlr_output, damage, &box, color); + } + + list_t *siblings = container_get_current_siblings(con); + enum sway_container_layout layout = + container_current_parent_layout(con); + + if (state->border_right) { + if (siblings->length == 1 && layout == L_HORIZ) { + memcpy(&color, colors->indicator, sizeof(float) * 4); + } else { memcpy(&color, colors->child_border, sizeof(float) * 4); - premultiply_alpha(color, con->alpha); - box.x = state->con_x; - box.y = state->view_y; - box.width = state->border_thickness; - box.height = state->view_height; - scale_box(&box, output_scale); - render_rect(output->wlr_output, damage, &box, color); } + premultiply_alpha(color, con->alpha); + box.x = state->view_x + state->view_width; + box.y = state->view_y; + box.width = state->border_thickness; + box.height = state->view_height; + scale_box(&box, output_scale); + render_rect(output->wlr_output, damage, &box, color); + } - list_t *siblings = container_get_current_siblings(con); - enum sway_container_layout layout = - container_current_parent_layout(con); - - if (state->border_right) { - if (siblings->length == 1 && layout == L_HORIZ) { - memcpy(&color, colors->indicator, sizeof(float) * 4); - } else { - memcpy(&color, colors->child_border, sizeof(float) * 4); - } - premultiply_alpha(color, con->alpha); - box.x = state->view_x + state->view_width; - box.y = state->view_y; - box.width = state->border_thickness; - box.height = state->view_height; - scale_box(&box, output_scale); - render_rect(output->wlr_output, damage, &box, color); - } - - if (state->border_bottom) { - if (siblings->length == 1 && layout == L_VERT) { - memcpy(&color, colors->indicator, sizeof(float) * 4); - } else { - memcpy(&color, colors->child_border, sizeof(float) * 4); - } - premultiply_alpha(color, con->alpha); - box.x = state->con_x; - box.y = state->view_y + state->view_height; - box.width = state->con_width; - box.height = state->border_thickness; - scale_box(&box, output_scale); - render_rect(output->wlr_output, damage, &box, color); + if (state->border_bottom) { + if (siblings->length == 1 && layout == L_VERT) { + memcpy(&color, colors->indicator, sizeof(float) * 4); + } else { + memcpy(&color, colors->child_border, sizeof(float) * 4); } + premultiply_alpha(color, con->alpha); + box.x = state->con_x; + box.y = state->view_y + state->view_height; + box.width = state->con_width; + box.height = state->border_thickness; + scale_box(&box, output_scale); + render_rect(output->wlr_output, damage, &box, color); } } @@ -645,14 +643,12 @@ static void render_containers_linear(struct sway_output *output, marks_texture = view->marks_unfocused; } - if (!view->container->current.using_csd) { - if (state->border == B_NORMAL) { - render_titlebar(output, damage, child, state->con_x, - state->con_y, state->con_width, colors, - title_texture, marks_texture); - } else { - render_top_border(output, damage, child, colors); - } + if (state->border == B_NORMAL) { + render_titlebar(output, damage, child, state->con_x, + state->con_y, state->con_width, colors, + title_texture, marks_texture); + } else if (state->border == B_PIXEL) { + render_top_border(output, damage, child, colors); } render_view(output, damage, child, colors); } else { @@ -859,14 +855,12 @@ static void render_floating_container(struct sway_output *soutput, marks_texture = view->marks_unfocused; } - if (!view->container->current.using_csd) { - if (con->current.border == B_NORMAL) { - render_titlebar(soutput, damage, con, con->current.con_x, - con->current.con_y, con->current.con_width, colors, - title_texture, marks_texture); - } else if (con->current.border != B_NONE) { - render_top_border(soutput, damage, con, colors); - } + if (con->current.border == B_NORMAL) { + render_titlebar(soutput, damage, con, con->current.con_x, + con->current.con_y, con->current.con_width, colors, + title_texture, marks_texture); + } else if (con->current.border == B_PIXEL) { + render_top_border(soutput, damage, con, colors); } render_view(soutput, damage, con, colors); } else { diff --git a/sway/desktop/transaction.c b/sway/desktop/transaction.c index 797f6b4cb..4624d8245 100644 --- a/sway/desktop/transaction.c +++ b/sway/desktop/transaction.c @@ -167,7 +167,6 @@ static void copy_container_state(struct sway_container *container, state->border_left = view->border_left; state->border_right = view->border_right; state->border_bottom = view->border_bottom; - state->using_csd = view->using_csd; } else { state->children = create_list(); list_cat(state->children, container->children); diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index 6d1ccdd76..d563edaec 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -175,15 +175,6 @@ static bool wants_floating(struct sway_view *view) { || toplevel->parent; } -static bool has_client_side_decorations(struct sway_view *view) { - struct sway_xdg_shell_view *xdg_shell_view = - xdg_shell_view_from_view(view); - if (xdg_shell_view == NULL) { - return true; - } - return xdg_shell_view->deco_mode != WLR_SERVER_DECORATION_MANAGER_MODE_SERVER; -} - 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) { @@ -240,7 +231,6 @@ static const struct sway_view_impl view_impl = { .set_tiled = set_tiled, .set_fullscreen = set_fullscreen, .wants_floating = wants_floating, - .has_client_side_decorations = has_client_side_decorations, .for_each_surface = for_each_surface, .for_each_popup = for_each_popup, .close = _close, @@ -385,15 +375,13 @@ static void handle_map(struct wl_listener *listener, void *data) { view->natural_height = view->wlr_xdg_surface->surface->current.height; } + view_map(view, view->wlr_xdg_surface->surface); + struct sway_server_decoration *deco = decoration_from_surface(xdg_surface->surface); - if (deco != NULL) { - xdg_shell_view->deco_mode = deco->wlr_server_decoration->mode; - } else { - xdg_shell_view->deco_mode = WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; - } - - view_map(view, view->wlr_xdg_surface->surface); + bool csd = !deco || deco->wlr_server_decoration->mode == + WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; + view_set_csd_from_client(view, csd); if (xdg_surface->toplevel->client_pending.fullscreen) { container_set_fullscreen(view->container, true); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 95ca396c1..8c8085f72 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -171,15 +171,6 @@ static bool wants_floating(struct sway_view *view) { || toplevel->parent; } -static bool has_client_side_decorations(struct sway_view *view) { - struct sway_xdg_shell_v6_view *xdg_shell_v6_view = - xdg_shell_v6_view_from_view(view); - if (xdg_shell_v6_view == NULL) { - return true; - } - return xdg_shell_v6_view->deco_mode != WLR_SERVER_DECORATION_MANAGER_MODE_SERVER; -} - 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) { @@ -237,7 +228,6 @@ static const struct sway_view_impl view_impl = { .set_tiled = set_tiled, .set_fullscreen = set_fullscreen, .wants_floating = wants_floating, - .has_client_side_decorations = has_client_side_decorations, .for_each_surface = for_each_surface, .for_each_popup = for_each_popup, .close = _close, @@ -382,15 +372,13 @@ static void handle_map(struct wl_listener *listener, void *data) { view->natural_height = view->wlr_xdg_surface_v6->surface->current.height; } + view_map(view, view->wlr_xdg_surface_v6->surface); + struct sway_server_decoration *deco = decoration_from_surface(xdg_surface->surface); - if (deco != NULL) { - xdg_shell_v6_view->deco_mode = deco->wlr_server_decoration->mode; - } else { - xdg_shell_v6_view->deco_mode = WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; - } - - view_map(view, view->wlr_xdg_surface_v6->surface); + bool csd = !deco || deco->wlr_server_decoration->mode == + WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; + view_set_csd_from_client(view, csd); if (xdg_surface->toplevel->client_pending.fullscreen) { container_set_fullscreen(view->container, true); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index a12ac8545..f1205518d 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -243,12 +243,14 @@ static bool wants_floating(struct sway_view *view) { return false; } -static bool has_client_side_decorations(struct sway_view *view) { - if (xwayland_view_from_view(view) == NULL) { - return false; - } - struct wlr_xwayland_surface *surface = view->wlr_xwayland_surface; - return surface->decorations != WLR_XWAYLAND_SURFACE_DECORATIONS_ALL; +static void handle_set_decorations(struct wl_listener *listener, void *data) { + struct sway_xwayland_view *xwayland_view = + wl_container_of(listener, xwayland_view, set_decorations); + struct sway_view *view = &xwayland_view->view; + struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; + + bool csd = xsurface->decorations != WLR_XWAYLAND_SURFACE_DECORATIONS_ALL; + view_set_csd_from_client(view, csd); } static void _close(struct sway_view *view) { @@ -274,7 +276,6 @@ static const struct sway_view_impl view_impl = { .set_tiled = set_tiled, .set_fullscreen = set_fullscreen, .wants_floating = wants_floating, - .has_client_side_decorations = has_client_side_decorations, .close = _close, .destroy = destroy, }; @@ -343,6 +344,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&xwayland_view->set_role.link); wl_list_remove(&xwayland_view->set_window_type.link); wl_list_remove(&xwayland_view->set_hints.link); + wl_list_remove(&xwayland_view->set_decorations.link); wl_list_remove(&xwayland_view->map.link); wl_list_remove(&xwayland_view->unmap.link); view_begin_destroy(&xwayland_view->view); @@ -613,6 +615,10 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xsurface->events.set_hints, &xwayland_view->set_hints); xwayland_view->set_hints.notify = handle_set_hints; + wl_signal_add(&xsurface->events.set_decorations, + &xwayland_view->set_decorations); + xwayland_view->set_decorations.notify = handle_set_decorations; + wl_signal_add(&xsurface->events.unmap, &xwayland_view->unmap); xwayland_view->unmap.notify = handle_unmap; diff --git a/sway/input/cursor.c b/sway/input/cursor.c index aa0e07f5a..eab102fdf 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -175,7 +175,8 @@ static enum wlr_edges find_edge(struct sway_container *cont, return WLR_EDGE_NONE; } struct sway_view *view = cont->view; - if (view->border == B_NONE || !view->border_thickness || view->using_csd) { + if (view->border == B_NONE || !view->border_thickness || + view->border == B_CSD) { return WLR_EDGE_NONE; } diff --git a/sway/ipc-json.c b/sway/ipc-json.c index f054ac9ff..df24b812a 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -216,6 +216,8 @@ static const char *describe_container_border(enum sway_container_border border) return "pixel"; case B_NORMAL: return "normal"; + case B_CSD: + return "csd"; } return "unknown"; } diff --git a/sway/meson.build b/sway/meson.build index d67a4c64c..2a3c5b5ea 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -10,6 +10,7 @@ sway_sources = files( 'security.c', 'server.c', 'swaynag.c', + 'xdg_decoration.c', 'desktop/desktop.c', 'desktop/idle_inhibit_v1.c', diff --git a/sway/server.c b/sway/server.c index bed5aed1f..63bfa7e14 100644 --- a/sway/server.c +++ b/sway/server.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include "list.h" @@ -115,6 +116,14 @@ bool server_init(struct sway_server *server) { server->server_decoration.notify = handle_server_decoration; wl_list_init(&server->decorations); + server->xdg_decoration_manager = + wlr_xdg_decoration_manager_v1_create(server->wl_display); + wl_signal_add( + &server->xdg_decoration_manager->events.new_toplevel_decoration, + &server->xdg_decoration); + server->xdg_decoration.notify = handle_xdg_decoration; + wl_list_init(&server->xdg_decorations); + wlr_export_dmabuf_manager_v1_create(server->wl_display); wlr_screencopy_manager_v1_create(server->wl_display); diff --git a/sway/tree/container.c b/sway/tree/container.c index baaa82fdb..d75e34a5a 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -715,7 +715,7 @@ void container_set_geometry_from_floating_view(struct sway_container *con) { size_t border_width = 0; size_t top = 0; - if (!view->using_csd) { + if (view->border != B_CSD) { border_width = view->border_thickness * (view->border != B_NONE); top = view->border == B_NORMAL ? container_titlebar_height() : border_width; diff --git a/sway/tree/view.c b/sway/tree/view.c index e370443ca..c370de2d5 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "config.h" #ifdef HAVE_XWAYLAND #include @@ -23,6 +25,7 @@ #include "sway/tree/view.h" #include "sway/tree/workspace.h" #include "sway/config.h" +#include "sway/xdg_decoration.h" #include "pango.h" #include "stringop.h" @@ -231,12 +234,8 @@ void view_autoconfigure(struct sway_view *view) { view->border_top = false; } - enum sway_container_border border = view->border; - if (view->using_csd) { - border = B_NONE; - } - - switch (border) { + switch (view->border) { + case B_CSD: case B_NONE: x = con->x; y = con->y + y_offset; @@ -309,16 +308,26 @@ void view_request_activate(struct sway_view *view) { } } -void view_set_tiled(struct sway_view *view, bool tiled) { - if (!tiled) { - view->using_csd = true; - if (view->impl->has_client_side_decorations) { - view->using_csd = view->impl->has_client_side_decorations(view); - } - } else { - view->using_csd = false; +void view_set_csd_from_server(struct sway_view *view, bool enabled) { + if (view->xdg_decoration) { + uint32_t mode = enabled ? + WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE : + WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE; + wlr_xdg_toplevel_decoration_v1_set_mode( + view->xdg_decoration->wlr_xdg_decoration, mode); } +} +void view_set_csd_from_client(struct sway_view *view, bool enabled) { + if (enabled && view->border != B_CSD) { + view->saved_border = view->border; + view->border = B_CSD; + } else if (!enabled && view->border == B_CSD) { + view->border = view->saved_border; + } +} + +void view_set_tiled(struct sway_view *view, bool tiled) { if (view->impl->set_tiled) { view->impl->set_tiled(view, tiled); } diff --git a/sway/xdg_decoration.c b/sway/xdg_decoration.c new file mode 100644 index 000000000..2e7e4bd04 --- /dev/null +++ b/sway/xdg_decoration.c @@ -0,0 +1,73 @@ +#include +#include "sway/desktop/transaction.h" +#include "sway/server.h" +#include "sway/tree/arrange.h" +#include "sway/tree/view.h" +#include "sway/xdg_decoration.h" +#include "log.h" + +static void xdg_decoration_handle_destroy(struct wl_listener *listener, + void *data) { + struct sway_xdg_decoration *deco = + wl_container_of(listener, deco, destroy); + deco->view->xdg_decoration = NULL; + wl_list_remove(&deco->destroy.link); + wl_list_remove(&deco->surface_commit.link); + wl_list_remove(&deco->link); + free(deco); +} + +static void xdg_decoration_handle_surface_commit(struct wl_listener *listener, + void *data) { + struct sway_xdg_decoration *decoration = + wl_container_of(listener, decoration, surface_commit); + + bool csd = decoration->wlr_xdg_decoration->current_mode == + WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE; + struct sway_view *view = decoration->view; + + view_set_csd_from_client(view, csd); + + arrange_container(view->container); + transaction_commit_dirty(); +} + +void handle_xdg_decoration(struct wl_listener *listener, void *data) { + struct wlr_xdg_toplevel_decoration_v1 *wlr_deco = data; + struct sway_xdg_shell_view *xdg_shell_view = wlr_deco->surface->data; + struct wlr_xdg_surface *wlr_xdg_surface = + xdg_shell_view->view.wlr_xdg_surface; + + struct sway_xdg_decoration *deco = calloc(1, sizeof(*deco)); + if (deco == NULL) { + return; + } + + deco->view = &xdg_shell_view->view; + deco->view->xdg_decoration = deco; + deco->wlr_xdg_decoration = wlr_deco; + + wl_signal_add(&wlr_deco->events.destroy, &deco->destroy); + deco->destroy.notify = xdg_decoration_handle_destroy; + + // Note: We don't listen to the request_mode signal here, effectively + // ignoring any modes the client asks to set. The client can still force a + // mode upon us, in which case we get upset but live with it. + + deco->surface_commit.notify = xdg_decoration_handle_surface_commit; + wl_signal_add(&wlr_xdg_surface->surface->events.commit, + &deco->surface_commit); + + wl_list_insert(&server.xdg_decorations, &deco->link); +} + +struct sway_xdg_decoration *xdg_decoration_from_surface( + struct wlr_surface *surface) { + struct sway_xdg_decoration *deco; + wl_list_for_each(deco, &server.xdg_decorations, link) { + if (deco->wlr_xdg_decoration->surface->surface == surface) { + return deco; + } + } + return NULL; +} From efcfe57b1072283c94a74d074f0e38efc3b57af8 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Tue, 25 Sep 2018 19:34:53 +1000 Subject: [PATCH 2/5] Remove CSD from toggle list if client doesn't support it --- sway/commands/border.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sway/commands/border.c b/sway/commands/border.c index 673fea08a..5e101564e 100644 --- a/sway/commands/border.c +++ b/sway/commands/border.c @@ -38,9 +38,14 @@ struct cmd_results *cmd_border(int argc, char **argv) { } else if (strcmp(argv[0], "pixel") == 0) { set_border(view, B_PIXEL); } else if (strcmp(argv[0], "csd") == 0) { + if (!view->xdg_decoration) { + return cmd_results_new(CMD_INVALID, "border", + "This window doesn't support client side decorations"); + } set_border(view, B_CSD); } else if (strcmp(argv[0], "toggle") == 0) { - set_border(view, (view->border + 1) % 4); + int num_available = view->xdg_decoration ? 4 : 3; + set_border(view, (view->border + 1) % num_available); } else { return cmd_results_new(CMD_INVALID, "border", "Expected 'border ' " From 6d0442c0c2cfcb75f855348b8133abcb2f3c2427 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 27 Sep 2018 17:09:05 +1000 Subject: [PATCH 3/5] Rename view_set_csd_from_client to view_update_csd_from_client --- include/sway/tree/view.h | 2 +- sway/decoration.c | 2 +- sway/desktop/xdg_shell.c | 2 +- sway/desktop/xdg_shell_v6.c | 2 +- sway/desktop/xwayland.c | 2 +- sway/tree/view.c | 2 +- sway/xdg_decoration.c | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 49df7c88f..e7aaffd7d 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -275,7 +275,7 @@ void view_set_csd_from_server(struct sway_view *view, bool enabled); * Updates the view's border setting when the client unexpectedly changes their * decoration mode. */ -void view_set_csd_from_client(struct sway_view *view, bool enabled); +void view_update_csd_from_client(struct sway_view *view, bool enabled); void view_set_tiled(struct sway_view *view, bool tiled); diff --git a/sway/decoration.c b/sway/decoration.c index fea6ed4c0..849fa89c9 100644 --- a/sway/decoration.c +++ b/sway/decoration.c @@ -28,7 +28,7 @@ static void server_decoration_handle_mode(struct wl_listener *listener, bool csd = deco->wlr_server_decoration->mode == WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; - view_set_csd_from_client(view, csd); + view_update_csd_from_client(view, csd); arrange_container(view->container); transaction_commit_dirty(); diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index d563edaec..2680af649 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -381,7 +381,7 @@ static void handle_map(struct wl_listener *listener, void *data) { decoration_from_surface(xdg_surface->surface); bool csd = !deco || deco->wlr_server_decoration->mode == WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; - view_set_csd_from_client(view, csd); + view_update_csd_from_client(view, csd); if (xdg_surface->toplevel->client_pending.fullscreen) { container_set_fullscreen(view->container, true); diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c index 8c8085f72..a7838c0fb 100644 --- a/sway/desktop/xdg_shell_v6.c +++ b/sway/desktop/xdg_shell_v6.c @@ -378,7 +378,7 @@ static void handle_map(struct wl_listener *listener, void *data) { decoration_from_surface(xdg_surface->surface); bool csd = !deco || deco->wlr_server_decoration->mode == WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; - view_set_csd_from_client(view, csd); + view_update_csd_from_client(view, csd); if (xdg_surface->toplevel->client_pending.fullscreen) { container_set_fullscreen(view->container, true); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index f1205518d..4c710f7ec 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -250,7 +250,7 @@ static void handle_set_decorations(struct wl_listener *listener, void *data) { struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; bool csd = xsurface->decorations != WLR_XWAYLAND_SURFACE_DECORATIONS_ALL; - view_set_csd_from_client(view, csd); + view_update_csd_from_client(view, csd); } static void _close(struct sway_view *view) { diff --git a/sway/tree/view.c b/sway/tree/view.c index c370de2d5..1c94de4c0 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -318,7 +318,7 @@ void view_set_csd_from_server(struct sway_view *view, bool enabled) { } } -void view_set_csd_from_client(struct sway_view *view, bool enabled) { +void view_update_csd_from_client(struct sway_view *view, bool enabled) { if (enabled && view->border != B_CSD) { view->saved_border = view->border; view->border = B_CSD; diff --git a/sway/xdg_decoration.c b/sway/xdg_decoration.c index 2e7e4bd04..80b2f57e3 100644 --- a/sway/xdg_decoration.c +++ b/sway/xdg_decoration.c @@ -26,7 +26,7 @@ static void xdg_decoration_handle_surface_commit(struct wl_listener *listener, WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE; struct sway_view *view = decoration->view; - view_set_csd_from_client(view, csd); + view_update_csd_from_client(view, csd); arrange_container(view->container); transaction_commit_dirty(); From 21ff87d72b44604d348cf71da3175b85ac5b2f75 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 27 Sep 2018 22:44:57 +1000 Subject: [PATCH 4/5] Improve CSD logic This does the following: * Removes the xdg-decoration surface_commit listener. I was under the impression the client could ignore the server's preference and set whatever decoration they like using this protocol, but I don't think that's right. * Adds a listener for the xdg-decoration request_mode signal. The protocol states that the server should respond to this with its preference. We'll always respond with SSD here. * Makes it so tiled views which use CSD will still have sway decorations rendered. To do this, using_csd had to be added back to the view struct, and the border is changed when floating or unfloating a view. --- include/sway/tree/view.h | 11 +++++++++ include/sway/xdg_decoration.h | 2 +- sway/commands/border.c | 45 ++++++++++++++++++++++++++++++----- sway/tree/container.c | 6 +++++ sway/tree/view.c | 8 ++++++- sway/xdg_decoration.c | 30 +++++++---------------- 6 files changed, 72 insertions(+), 30 deletions(-) diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index e7aaffd7d..2c7b4c2b3 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -76,8 +76,19 @@ struct sway_view { int natural_width, natural_height; char *title_format; + + // Our border types are B_NONE, B_PIXEL, B_NORMAL and B_CSD. We normally + // just assign this to the border property and ignore the other two. + // However, when a view using CSD is tiled, we want to render our own + // borders as well. So in this case the border property becomes one of the + // first three, and using_csd is true. + // Lastly, views can change their decoration mode at any time. When an SSD + // view becomes CSD without our approval, we save the SSD border type so it + // can be restored if/when the view returns from CSD to SSD. enum sway_container_border border; enum sway_container_border saved_border; + bool using_csd; + int border_thickness; bool border_top; bool border_bottom; diff --git a/include/sway/xdg_decoration.h b/include/sway/xdg_decoration.h index 46fb8d34c..8bef4c6d4 100644 --- a/include/sway/xdg_decoration.h +++ b/include/sway/xdg_decoration.h @@ -10,7 +10,7 @@ struct sway_xdg_decoration { struct sway_view *view; struct wl_listener destroy; - struct wl_listener surface_commit; + struct wl_listener request_mode; }; struct sway_xdg_decoration *xdg_decoration_from_surface( diff --git a/sway/commands/border.c b/sway/commands/border.c index 5e101564e..bfd3b9eda 100644 --- a/sway/commands/border.c +++ b/sway/commands/border.c @@ -7,15 +7,49 @@ #include "sway/tree/container.h" #include "sway/tree/view.h" +// A couple of things here: +// - view->border should never be B_CSD when the view is tiled, even when CSD is +// in use (we set using_csd instead and render a sway border). +// - view->saved_border should be the last applied border when switching to CSD. +// - view->using_csd should always reflect whether CSD is applied or not. static void set_border(struct sway_view *view, enum sway_container_border new_border) { - if (view->border == B_CSD && new_border != B_CSD) { + if (view->using_csd && new_border != B_CSD) { view_set_csd_from_server(view, false); - } else if (view->border != B_CSD && new_border == B_CSD) { + } else if (!view->using_csd && new_border == B_CSD) { view_set_csd_from_server(view, true); + view->saved_border = view->border; + } + if (new_border != B_CSD || container_is_floating(view->container)) { + view->border = new_border; + } + view->using_csd = new_border == B_CSD; +} + +static void border_toggle(struct sway_view *view) { + if (view->using_csd) { + set_border(view, B_NONE); + return; + } + switch (view->border) { + case B_NONE: + set_border(view, B_PIXEL); + break; + case B_PIXEL: + set_border(view, B_NORMAL); + break; + case B_NORMAL: + if (view->xdg_decoration) { + set_border(view, B_CSD); + } else { + set_border(view, B_NONE); + } + break; + case B_CSD: + // view->using_csd should be true so it would have returned above + sway_assert(false, "Unreachable"); + break; } - view->saved_border = view->border; - view->border = new_border; } struct cmd_results *cmd_border(int argc, char **argv) { @@ -44,8 +78,7 @@ struct cmd_results *cmd_border(int argc, char **argv) { } set_border(view, B_CSD); } else if (strcmp(argv[0], "toggle") == 0) { - int num_available = view->xdg_decoration ? 4 : 3; - set_border(view, (view->border + 1) % num_available); + border_toggle(view); } else { return cmd_results_new(CMD_INVALID, "border", "Expected 'border ' " diff --git a/sway/tree/container.c b/sway/tree/container.c index d75e34a5a..9b671c1d3 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -669,6 +669,9 @@ void container_set_floating(struct sway_container *container, bool enable) { container_init_floating(container); if (container->view) { view_set_tiled(container->view, false); + if (container->view->using_csd) { + container->view->border = B_CSD; + } } if (old_parent) { container_reap_empty(old_parent); @@ -695,6 +698,9 @@ void container_set_floating(struct sway_container *container, bool enable) { } if (container->view) { view_set_tiled(container->view, true); + if (container->view->using_csd) { + container->view->border = container->view->saved_border; + } } container->is_sticky = false; } diff --git a/sway/tree/view.c b/sway/tree/view.c index 1c94de4c0..9ffcf206c 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -309,6 +309,7 @@ void view_request_activate(struct sway_view *view) { } void view_set_csd_from_server(struct sway_view *view, bool enabled) { + wlr_log(WLR_DEBUG, "Telling view %p to set CSD to %i", view, enabled); if (view->xdg_decoration) { uint32_t mode = enabled ? WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE : @@ -316,15 +317,20 @@ void view_set_csd_from_server(struct sway_view *view, bool enabled) { wlr_xdg_toplevel_decoration_v1_set_mode( view->xdg_decoration->wlr_xdg_decoration, mode); } + view->using_csd = enabled; } void view_update_csd_from_client(struct sway_view *view, bool enabled) { + wlr_log(WLR_DEBUG, "View %p updated CSD to %i", view, enabled); if (enabled && view->border != B_CSD) { view->saved_border = view->border; - view->border = B_CSD; + if (container_is_floating(view->container)) { + view->border = B_CSD; + } } else if (!enabled && view->border == B_CSD) { view->border = view->saved_border; } + view->using_csd = enabled; } void view_set_tiled(struct sway_view *view, bool tiled) { diff --git a/sway/xdg_decoration.c b/sway/xdg_decoration.c index 80b2f57e3..39e0df564 100644 --- a/sway/xdg_decoration.c +++ b/sway/xdg_decoration.c @@ -12,31 +12,22 @@ static void xdg_decoration_handle_destroy(struct wl_listener *listener, wl_container_of(listener, deco, destroy); deco->view->xdg_decoration = NULL; wl_list_remove(&deco->destroy.link); - wl_list_remove(&deco->surface_commit.link); + wl_list_remove(&deco->request_mode.link); wl_list_remove(&deco->link); free(deco); } -static void xdg_decoration_handle_surface_commit(struct wl_listener *listener, +static void xdg_decoration_handle_request_mode(struct wl_listener *listener, void *data) { - struct sway_xdg_decoration *decoration = - wl_container_of(listener, decoration, surface_commit); - - bool csd = decoration->wlr_xdg_decoration->current_mode == - WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE; - struct sway_view *view = decoration->view; - - view_update_csd_from_client(view, csd); - - arrange_container(view->container); - transaction_commit_dirty(); + struct sway_xdg_decoration *deco = + wl_container_of(listener, deco, request_mode); + wlr_xdg_toplevel_decoration_v1_set_mode(deco->wlr_xdg_decoration, + WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); } void handle_xdg_decoration(struct wl_listener *listener, void *data) { struct wlr_xdg_toplevel_decoration_v1 *wlr_deco = data; struct sway_xdg_shell_view *xdg_shell_view = wlr_deco->surface->data; - struct wlr_xdg_surface *wlr_xdg_surface = - xdg_shell_view->view.wlr_xdg_surface; struct sway_xdg_decoration *deco = calloc(1, sizeof(*deco)); if (deco == NULL) { @@ -50,13 +41,8 @@ void handle_xdg_decoration(struct wl_listener *listener, void *data) { wl_signal_add(&wlr_deco->events.destroy, &deco->destroy); deco->destroy.notify = xdg_decoration_handle_destroy; - // Note: We don't listen to the request_mode signal here, effectively - // ignoring any modes the client asks to set. The client can still force a - // mode upon us, in which case we get upset but live with it. - - deco->surface_commit.notify = xdg_decoration_handle_surface_commit; - wl_signal_add(&wlr_xdg_surface->surface->events.commit, - &deco->surface_commit); + wl_signal_add(&wlr_deco->events.request_mode, &deco->request_mode); + deco->request_mode.notify = xdg_decoration_handle_request_mode; wl_list_insert(&server.xdg_decorations, &deco->link); } From f16529e2588f5e71d6777f4c06dfb58b29308cd0 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Thu, 27 Sep 2018 23:00:10 +1000 Subject: [PATCH 5/5] Remove server-decoration assumption if view supports xdg-decoration --- sway/desktop/xdg_shell.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index 2680af649..a8b527a7f 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -377,11 +377,13 @@ static void handle_map(struct wl_listener *listener, void *data) { view_map(view, view->wlr_xdg_surface->surface); - struct sway_server_decoration *deco = - decoration_from_surface(xdg_surface->surface); - bool csd = !deco || deco->wlr_server_decoration->mode == - WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; - view_update_csd_from_client(view, csd); + if (!view->xdg_decoration) { + struct sway_server_decoration *deco = + decoration_from_surface(xdg_surface->surface); + bool csd = !deco || deco->wlr_server_decoration->mode == + WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT; + view_update_csd_from_client(view, csd); + } if (xdg_surface->toplevel->client_pending.fullscreen) { container_set_fullscreen(view->container, true);