From a6b1111230061899ae8ca84a52f180537132b81a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 30 Oct 2025 20:12:47 +0100 Subject: [PATCH] Properly cleanup wl outputs for cursor tracker --- .../CursorTracker/CursorTrackerWayland.hpp | 1 + src/CursorTracker/CursorTrackerWayland.cpp | 35 +++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/include/CursorTracker/CursorTrackerWayland.hpp b/include/CursorTracker/CursorTrackerWayland.hpp index 1eeee83..1bd1e6f 100644 --- a/include/CursorTracker/CursorTrackerWayland.hpp +++ b/include/CursorTracker/CursorTrackerWayland.hpp @@ -34,6 +34,7 @@ namespace gsr { std::vector monitors; struct zxdg_output_manager_v1 *xdg_output_manager = nullptr; private: + void clear_monitors(); void set_monitor_outputs_from_xdg_output(struct wl_display *dpy); private: int drm_fd = -1; diff --git a/src/CursorTracker/CursorTrackerWayland.cpp b/src/CursorTracker/CursorTrackerWayland.cpp index 7af86b4..6cec683 100644 --- a/src/CursorTracker/CursorTrackerWayland.cpp +++ b/src/CursorTracker/CursorTrackerWayland.cpp @@ -403,6 +403,7 @@ namespace gsr { } CursorTrackerWayland::~CursorTrackerWayland() { + clear_monitors(); if(drm_fd > 0) close(drm_fd); } @@ -480,6 +481,21 @@ namespace gsr { wl_display_roundtrip(dpy); } + void CursorTrackerWayland::clear_monitors() { + for(WaylandOutput &monitor : monitors) { + if(monitor.output) { + wl_output_destroy(monitor.output); + monitor.output = nullptr; + } + + if(monitor.xdg_output) { + zxdg_output_v1_destroy(monitor.xdg_output); + monitor.xdg_output = nullptr; + } + } + monitors.clear(); + } + std::optional CursorTrackerWayland::get_latest_cursor_info() { if(drm_fd <= 0 || latest_crtc_id == -1) return std::nullopt; @@ -494,7 +510,7 @@ namespace gsr { return std::nullopt; } - monitors.clear(); + clear_monitors(); struct wl_registry *registry = wl_display_get_registry(dpy); wl_registry_add_listener(registry, ®istry_listener, this); @@ -508,22 +524,13 @@ namespace gsr { mgl::vec2i cursor_position = latest_cursor_position; const WaylandOutput *wayland_monitor = get_wayland_monitor_by_name(monitors, monitor_name); - if(!wayland_monitor) + if(!wayland_monitor) { + clear_monitors(); return std::nullopt; + } cursor_position = wayland_monitor->pos + latest_cursor_position; - for(WaylandOutput &monitor : monitors) { - if(monitor.output) { - wl_output_destroy(monitor.output); - monitor.output = nullptr; - } - - if(monitor.xdg_output) { - zxdg_output_v1_destroy(monitor.xdg_output); - monitor.xdg_output = nullptr; - } - } - monitors.clear(); + clear_monitors(); if(xdg_output_manager) { zxdg_output_manager_v1_destroy(xdg_output_manager);