diff --git a/src/core/Backend.cpp b/src/core/Backend.cpp index d921846..a850103 100644 --- a/src/core/Backend.cpp +++ b/src/core/Backend.cpp @@ -664,6 +664,9 @@ void CBackend::cleanup() { if (m_cleaned.exchange(true)) return; + // apps drop their windows and outputs after enterLoop() returns, past the disconnect below + g_waylandPlatform->releaseProxies(); + g_asyncResourceGatherer.reset(); m_waylandPostDispatch.reset(); diff --git a/src/core/platforms/WaylandPlatform.cpp b/src/core/platforms/WaylandPlatform.cpp index e93ec47..72446e5 100644 --- a/src/core/platforms/WaylandPlatform.cpp +++ b/src/core/platforms/WaylandPlatform.cpp @@ -176,6 +176,28 @@ CWaylandPlatform::~CWaylandPlatform() { wl_display_disconnect(DPY); } +void CWaylandPlatform::releaseProxies() { + // by value: a release can drop the last ref and the destructor erases from the list + const auto release = [](auto windows) { + for (const auto& w : windows) { + if (const auto window = w.lock()) + window->releaseWayland(); + } + }; + + release(m_windows); + release(m_layers); + + if (const auto lock = m_sessionLockState.lock()) { + release(lock->m_lockSurfaces); + lock->m_lock.reset(); + } + + for (const auto& o : m_outputs) { + o->m_wlOutput.reset(); + } +} + bool CWaylandPlatform::dispatchEvents() { int ret = 0; do { diff --git a/src/core/platforms/WaylandPlatform.hpp b/src/core/platforms/WaylandPlatform.hpp index 06fd293..35f7616 100644 --- a/src/core/platforms/WaylandPlatform.hpp +++ b/src/core/platforms/WaylandPlatform.hpp @@ -54,6 +54,7 @@ namespace Hyprtoolkit { void setCursor(ePointerShape shape); bool dispatchEvents(); + void releaseProxies(); void setClipboard(const std::string& text); std::string readClipboard(); diff --git a/src/window/IWaylandWindow.cpp b/src/window/IWaylandWindow.cpp index 33ea5e3..edda939 100644 --- a/src/window/IWaylandWindow.cpp +++ b/src/window/IWaylandWindow.cpp @@ -246,6 +246,20 @@ SP IWaylandWindow::openPopup(const SWindowCreationData& data) { return x; } +void IWaylandWindow::releaseWayland() { + // popup close() erases itself from m_popups + const auto popups = m_popups; + for (const auto& p : popups) { + if (const auto popup = reinterpretPointerCast(p.lock())) + popup->releaseWayland(); + } + + close(); + + // layers and lock surfaces keep their wl_surface across close() for a reopen + m_waylandState = {}; +} + void IWaylandWindow::mouseButton(const Input::eMouseButton button, bool state) { if (m_popups.empty() || !state || m_ignoreNextButtonEvent) { m_ignoreNextButtonEvent = false; diff --git a/src/window/IWaylandWindow.hpp b/src/window/IWaylandWindow.hpp index 0bbb425..5ae990e 100644 --- a/src/window/IWaylandWindow.hpp +++ b/src/window/IWaylandWindow.hpp @@ -56,6 +56,7 @@ namespace Hyprtoolkit { virtual void setIMTo(const Hyprutils::Math::CBox& box, const std::string& str, size_t cursor); virtual void resetIM(); virtual void onPreRender(); + void releaseWayland(); std::vector> m_popups; diff --git a/src/window/WaylandPopup.cpp b/src/window/WaylandPopup.cpp index 74fd7fe..4cde5c3 100644 --- a/src/window/WaylandPopup.cpp +++ b/src/window/WaylandPopup.cpp @@ -117,7 +117,8 @@ void CWaylandPopup::open() { } void CWaylandPopup::close() { - if (g_waylandPlatform->m_currentWindow == m_self) + // the app can drop a popup after the backend is gone + if (g_waylandPlatform && g_waylandPlatform->m_currentWindow == m_self) g_waylandPlatform->m_currentWindow = m_parent; if (m_parent) @@ -138,6 +139,7 @@ void CWaylandPopup::close() { m_waylandState.surface->sendDestroy(); m_waylandState = {}; + m_wlPopupState = {}; m_events.popupClosed.emit(); }