Skip to content

Commit

Permalink
Tighten up wlr_surface handling
Browse files Browse the repository at this point in the history
  • Loading branch information
serebit committed Nov 4, 2023
1 parent 95943a1 commit c6859a9
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/foreign_toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.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) {
Expand Down
9 changes: 5 additions & 4 deletions src/input/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,16 @@ void Cursor::process_motion(const uint32_t time) {

/* Otherwise, find the view under the pointer and send the event along. */
double sx, sy;
wlr_surface* surface = NULL;
wlr_surface* surface = nullptr;
Surface* magpie_surface = seat.server.surface_at(cursor->x, cursor->y, &surface, &sx, &sy);
if (!magpie_surface) {
if (magpie_surface == nullptr) {
/* If there's no view under the cursor, set the cursor image to a
* default. This is what makes the cursor image appear when you move it
* around the screen, not over any views. */
set_image("left_ptr");
}
if (surface) {

if (surface != nullptr) {
/*
* Send pointer enter and motion events.
*
Expand Down Expand Up @@ -354,7 +355,7 @@ void Cursor::reset_mode() {
}

void Cursor::warp_to_constraint(PointerConstraint& constraint) {
if (seat.server.focused_view->surface != constraint.wlr->surface) {
if (seat.server.focused_view->get_wlr_surface() != constraint.wlr->surface) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/input/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static bool handle_compositor_keybinding(const Keyboard& keyboard, const uint32_
return true;
}
View& next_view = **server.views.begin()++;
server.focus_view(next_view, next_view.surface);
server.focus_view(next_view);
return true;
}
} else if (sym >= XKB_KEY_XF86Switch_VT_1 && sym <= XKB_KEY_XF86Switch_VT_12) {
Expand Down
6 changes: 3 additions & 3 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ void Server::focus_view(View& view, wlr_surface* surface) {
*/
wlr_keyboard* keyboard = wlr_seat_get_keyboard(seat);
if (keyboard != nullptr) {
wlr_seat_keyboard_notify_enter(seat, view.surface, keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
wlr_seat_keyboard_notify_enter(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);
}

Surface* Server::surface_at(const double lx, const double ly, wlr_surface** surface, double* sx, double* sy) {
Surface* Server::surface_at(const double lx, const double ly, wlr_surface** wlr, double* sx, double* sy) {
/* This returns the topmost node in the scene at the given layout coords.
* we only care about surface nodes as we are specifically looking for a
* surface in the surface tree of a magpie_view. */
Expand All @@ -93,7 +93,7 @@ Surface* Server::surface_at(const double lx, const double ly, wlr_surface** surf
return NULL;
}

*surface = scene_surface->surface;
*wlr = scene_surface->surface;
/* Find the node corresponding to the magpie_view at the root of this
* surface tree, it is the only one for which we set the data field. */
wlr_scene_tree* tree = node->parent;
Expand Down
4 changes: 2 additions & 2 deletions src/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ class Server {

Server();

Surface* surface_at(const double lx, const double ly, wlr_surface** surface, double* sx, double* sy);
void focus_view(View& view, wlr_surface* surface);
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);
};

#endif
8 changes: 6 additions & 2 deletions src/surface/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,14 @@ Layer::~Layer() noexcept {
wl_list_remove(&listeners.new_subsurface.link);
}

inline Server& Layer::get_server() const {
constexpr wlr_surface* Layer::get_wlr_surface() const {
return layer_surface.surface;
}

constexpr Server& Layer::get_server() const {
return server;
}

bool Layer::is_view() const {
constexpr bool Layer::is_view() const {
return false;
}
5 changes: 3 additions & 2 deletions src/surface/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class Layer : public Surface {
Layer(Output& output, wlr_layer_surface_v1& surface) noexcept;
~Layer() noexcept;

inline Server& get_server() const override;
bool is_view() const override;
constexpr wlr_surface* get_wlr_surface() const override;
constexpr Server& get_server() const override;
constexpr bool is_view() const override;
};

class LayerSubsurface {
Expand Down
4 changes: 4 additions & 0 deletions src/surface/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Popup::~Popup() noexcept {
wl_list_remove(&listeners.new_popup.link);
}

constexpr wlr_surface* Popup::get_wlr_surface() const {
return xdg_popup->base->surface;
}

constexpr Server& Popup::get_server() const {
return server;
}
Expand Down
1 change: 1 addition & 0 deletions src/surface/popup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Popup : public Surface {
Popup(const Surface& parent, wlr_xdg_popup* xdg_popup) noexcept;
~Popup() noexcept;

constexpr wlr_surface* get_wlr_surface() const override;
constexpr Server& get_server() const override;
constexpr bool is_view() const override;
};
Expand Down
5 changes: 3 additions & 2 deletions src/surface/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
enum SurfaceType { MAGPIE_SURFACE_TYPE_VIEW, MAGPIE_SURFACE_TYPE_LAYER, MAGPIE_SURFACE_TYPE_POPUP };

struct Surface {
wlr_scene_node* scene_node;
wlr_scene_node* scene_node = nullptr;

virtual ~Surface() noexcept {};

virtual constexpr Server& get_server() const = 0;
virtual bool is_view() const = 0;
virtual constexpr wlr_surface* get_wlr_surface() const = 0;
virtual constexpr bool is_view() const = 0;
};

#endif
4 changes: 2 additions & 2 deletions src/surface/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void View::begin_interactive(const CursorMode mode, const uint32_t edges) {
Cursor& cursor = server.seat->cursor;
wlr_surface* focused_surface = server.seat->seat->pointer_state.focused_surface;

if (surface != wlr_surface_get_root_surface(focused_surface)) {
if (get_wlr_surface() != wlr_surface_get_root_surface(focused_surface)) {
/* Deny move/resize requests from unfocused clients. */
return;
}
Expand Down Expand Up @@ -122,7 +122,7 @@ void View::set_maximized(const bool maximized) {
}

wlr_surface* focused_surface = server.seat->seat->pointer_state.focused_surface;
if (surface != wlr_surface_get_root_surface(focused_surface)) {
if (get_wlr_surface() != wlr_surface_get_root_surface(focused_surface)) {
/* Deny maximize requests from unfocused clients. */
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/surface/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct View : public Surface {
wlr_box current;
wlr_box pending;
wlr_box previous;
wlr_surface* surface = nullptr;
std::optional<ForeignToplevelHandle> toplevel_handle = {};

virtual ~View() noexcept {};
Expand Down Expand Up @@ -81,6 +80,7 @@ class XdgView : public View {
XdgView(Server& server, wlr_xdg_toplevel& toplevel) noexcept;
~XdgView() noexcept;

constexpr wlr_surface* get_wlr_surface() const override;
constexpr Server& get_server() const override;
const wlr_box get_geometry() const override;
void map() override;
Expand Down Expand Up @@ -123,6 +123,7 @@ class XWaylandView : public View {
XWaylandView(Server& server, wlr_xwayland_surface& surface) noexcept;
~XWaylandView() noexcept;

constexpr wlr_surface* get_wlr_surface() const override;
constexpr Server& get_server() const override;
const wlr_box get_geometry() const override;
void map() override;
Expand Down
7 changes: 5 additions & 2 deletions src/surface/xdg_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ XdgView::XdgView(Server& server, wlr_xdg_toplevel& toplevel) noexcept
: listeners(*this), server(server), xdg_toplevel(toplevel) {
auto* scene_tree = wlr_scene_xdg_surface_create(&server.scene->tree, toplevel.base);
scene_node = &scene_tree->node;
surface = toplevel.base->surface;

wlr_xdg_toplevel_set_wm_capabilities(
&toplevel, WLR_XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE | WLR_XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE);
Expand Down Expand Up @@ -184,6 +183,10 @@ XdgView::~XdgView() noexcept {
wl_list_remove(&listeners.set_parent.link);
}

constexpr wlr_surface* XdgView::get_wlr_surface() const {
return xdg_toplevel.base->surface;
}

constexpr Server& XdgView::get_server() const {
return server;
}
Expand Down Expand Up @@ -212,7 +215,7 @@ void XdgView::map() {

wlr_scene_node_set_enabled(scene_node, true);
is_maximized = xdg_toplevel.current.maximized;
server.focus_view(*this, xdg_toplevel.base->surface);
server.focus_view(*this);
}

void XdgView::unmap() {
Expand Down
10 changes: 6 additions & 4 deletions src/surface/xwayland_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ XWaylandView::~XWaylandView() noexcept {
wl_list_remove(&listeners.set_parent.link);
}

constexpr wlr_surface* XWaylandView::get_wlr_surface() const {
return xwayland_surface.surface;
}

constexpr Server& XWaylandView::get_server() const {
return server;
}
Expand All @@ -188,8 +192,6 @@ void XWaylandView::map() {
xwayland_surface.data = this;
xwayland_surface.surface->data = this;

this->surface = xwayland_surface.surface;

toplevel_handle.emplace(*this);
toplevel_handle->set_title(xwayland_surface.title);
toplevel_handle->set_app_id(xwayland_surface._class);
Expand All @@ -209,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, this->surface);
server.focus_view(*this);
}

void XWaylandView::unmap() {
Expand All @@ -225,7 +227,7 @@ void XWaylandView::unmap() {
server.focused_view = nullptr;
}

if (server.seat->seat->keyboard_state.focused_surface == surface) {
if (server.seat->seat->keyboard_state.focused_surface == xwayland_surface.surface) {
server.seat->seat->keyboard_state.focused_surface = NULL;
}

Expand Down

0 comments on commit c6859a9

Please sign in to comment.