From ea9e3b43d7c786adee4d2de6bf0ca8d5a4eb2bfd Mon Sep 17 00:00:00 2001 From: Campbell Jones Date: Sat, 4 Nov 2023 12:01:16 -0400 Subject: [PATCH] Remove focus when clicking on the background --- src/foreign_toplevel.cpp | 2 +- src/input/cursor.cpp | 4 +++- src/input/keyboard.cpp | 2 +- src/server.cpp | 34 ++++++++++++++++++---------------- src/server.hpp | 2 +- src/surface/xdg_view.cpp | 2 +- src/surface/xwayland_view.cpp | 2 +- 7 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/foreign_toplevel.cpp b/src/foreign_toplevel.cpp index 7b2e18c05..e473a0b31 100644 --- a/src/foreign_toplevel.cpp +++ b/src/foreign_toplevel.cpp @@ -24,7 +24,7 @@ static void foreign_toplevel_handle_request_activate_notify(wl_listener* listene (void) data; handle.view.set_minimized(false); - handle.view.get_server().focus_view(handle.view, handle.view.get_wlr_surface()); + handle.view.get_server().focus_view(&handle.view, handle.view.get_wlr_surface()); } static void foreign_toplevel_handle_request_fullscreen_notify(wl_listener* listener, void* data) { diff --git a/src/input/cursor.cpp b/src/input/cursor.cpp index 7deba33eb..48d054f4c 100644 --- a/src/input/cursor.cpp +++ b/src/input/cursor.cpp @@ -154,7 +154,9 @@ static void cursor_button_notify(wl_listener* listener, void* data) { } } else if (magpie_surface != nullptr && magpie_surface->is_view()) { /* Focus that client if the button was _pressed_ */ - server.focus_view(*static_cast(magpie_surface), surface); + server.focus_view(static_cast(magpie_surface), surface); + } else { + server.focus_view(nullptr); } } diff --git a/src/input/keyboard.cpp b/src/input/keyboard.cpp index 2cf674058..0df60e35e 100644 --- a/src/input/keyboard.cpp +++ b/src/input/keyboard.cpp @@ -40,7 +40,7 @@ static bool handle_compositor_keybinding(const Keyboard& keyboard, const uint32_ if (server.views.size() < 2) { return true; } - View& next_view = **server.views.begin()++; + View* next_view = *server.views.begin()++; server.focus_view(next_view); return true; } diff --git a/src/server.cpp b/src/server.cpp index bb49a3eaf..db04490ba 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -30,17 +30,15 @@ #include #include "wlr-wrap-end.hpp" -void Server::focus_view(View& view, wlr_surface* surface) { - Server& server = view.get_server(); - wlr_seat* seat = server.seat->seat; - wlr_surface* prev_surface = seat->keyboard_state.focused_surface; +void Server::focus_view(View* view, wlr_surface* surface) { + wlr_surface* prev_surface = seat->seat->keyboard_state.focused_surface; if (prev_surface == surface) { /* Don't re-focus an already focused surface. */ return; } if (prev_surface) { - wlr_surface* previous = seat->keyboard_state.focused_surface; + wlr_surface* previous = seat->seat->keyboard_state.focused_surface; if (wlr_surface_is_xdg_surface(previous)) { wlr_xdg_surface* xdg_previous = wlr_xdg_surface_from_wlr_surface(previous); @@ -52,31 +50,35 @@ void Server::focus_view(View& view, wlr_surface* surface) { } } + if (view == nullptr) { + return; + } + /* Move the view to the front */ - wlr_scene_node_raise_to_top(view.scene_node); - (void) std::remove(server.views.begin(), server.views.end(), &view); - for (auto* view : server.views) { + wlr_scene_node_raise_to_top(view->scene_node); + (void) std::remove(views.begin(), views.end(), view); + for (auto* view : views) { view->set_activated(false); } /* Activate the new surface */ - server.views.insert(server.views.begin(), &view); - view.set_activated(true); - focused_view = &view; + views.insert(views.begin(), view); + view->set_activated(true); + focused_view = view; /* * Tell the seat to have the keyboard enter this surface. wlroots will keep * track of this and automatically send key events to the appropriate * clients without additional work on your part. */ - wlr_keyboard* keyboard = wlr_seat_get_keyboard(seat); + wlr_keyboard* keyboard = wlr_seat_get_keyboard(seat->seat); if (keyboard != nullptr) { - wlr_seat_keyboard_notify_enter(seat, view.get_wlr_surface(), keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers); + wlr_seat_keyboard_notify_enter(seat->seat, view->get_wlr_surface(), keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers); } wlr_pointer_constraint_v1* constraint = - wlr_pointer_constraints_v1_constraint_for_surface(server.seat->pointer_constraints, surface, seat); - server.seat->set_constraint(constraint); + wlr_pointer_constraints_v1_constraint_for_surface(seat->pointer_constraints, surface, seat->seat); + seat->set_constraint(constraint); } Surface* Server::surface_at(const double lx, const double ly, wlr_surface** wlr, double* sx, double* sy) { @@ -206,7 +208,7 @@ static void request_activation_notify(wl_listener* listener, void* data) { wlr_xdg_surface* xdg_surface = wlr_xdg_surface_from_wlr_surface(event->surface); auto* view = dynamic_cast(static_cast(xdg_surface->surface->data)); if (view != nullptr && xdg_surface->mapped) { - server.focus_view(*view, xdg_surface->surface); + server.focus_view(view, xdg_surface->surface); } } diff --git a/src/server.hpp b/src/server.hpp index a4b279da3..20a24ce72 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -92,7 +92,7 @@ class Server { Server(); Surface* surface_at(const double lx, const double ly, wlr_surface** wlr, double* sx, double* sy); - void focus_view(View& view, wlr_surface* surface = nullptr); + void focus_view(View* view, wlr_surface* surface = nullptr); }; #endif diff --git a/src/surface/xdg_view.cpp b/src/surface/xdg_view.cpp index 37fd12d73..1e9edb16b 100644 --- a/src/surface/xdg_view.cpp +++ b/src/surface/xdg_view.cpp @@ -215,7 +215,7 @@ void XdgView::map() { wlr_scene_node_set_enabled(scene_node, true); is_maximized = xdg_toplevel.current.maximized; - server.focus_view(*this); + server.focus_view(this); } void XdgView::unmap() { diff --git a/src/surface/xwayland_view.cpp b/src/surface/xwayland_view.cpp index e41a09fa2..58318cc0b 100644 --- a/src/surface/xwayland_view.cpp +++ b/src/surface/xwayland_view.cpp @@ -211,7 +211,7 @@ void XWaylandView::map() { wlr_scene_node_set_position(scene_node, current.x, current.y); server.views.insert(server.views.begin(), this); - server.focus_view(*this); + server.focus_view(this); } void XWaylandView::unmap() {