From 5eeec8860e2dc8ff80dda26243778b5ecc923ca2 Mon Sep 17 00:00:00 2001 From: vaxerski Date: Fri, 19 Jan 2024 16:20:22 +0100 Subject: [PATCH] core: improve cleanup logic --- src/Compositor.cpp | 96 +++++++++++++++++++++++------ src/Compositor.hpp | 1 + src/helpers/MiscFunctions.cpp | 7 +++ src/helpers/MiscFunctions.hpp | 1 + src/helpers/WLListener.cpp | 6 +- src/managers/KeybindManager.cpp | 2 +- src/managers/input/InputManager.cpp | 3 + src/managers/input/InputManager.hpp | 2 + 8 files changed, 95 insertions(+), 23 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 8d16f22d..c6ac224a 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -81,26 +81,6 @@ CCompositor::CCompositor() { CCompositor::~CCompositor() { cleanup(); - g_pDecorationPositioner.reset(); - g_pPluginSystem.reset(); - g_pHyprNotificationOverlay.reset(); - g_pDebugOverlay.reset(); - g_pEventManager.reset(); - g_pSessionLockManager.reset(); - g_pProtocolManager.reset(); - g_pXWaylandManager.reset(); - g_pHyprRenderer.reset(); - g_pHyprOpenGL.reset(); - g_pInputManager.reset(); - g_pThreadManager.reset(); - g_pConfigManager.reset(); - g_pLayoutManager.reset(); - g_pHyprError.reset(); - g_pConfigManager.reset(); - g_pAnimationManager.reset(); - g_pKeybindManager.reset(); - g_pHookSystem.reset(); - g_pWatchdog.reset(); } void CCompositor::setRandomSplash() { @@ -332,6 +312,59 @@ void CCompositor::initAllSignals() { addWLSignal(&m_sWLRSession->events.active, &Events::listen_sessionActive, m_sWLRSession, "Session"); } +void CCompositor::removeAllSignals() { + removeWLSignal(&Events::listen_newOutput); + removeWLSignal(&Events::listen_newXDGToplevel); + removeWLSignal(&Events::listen_mouseMove); + removeWLSignal(&Events::listen_mouseMoveAbsolute); + removeWLSignal(&Events::listen_mouseButton); + removeWLSignal(&Events::listen_mouseAxis); + removeWLSignal(&Events::listen_mouseFrame); + removeWLSignal(&Events::listen_swipeBegin); + removeWLSignal(&Events::listen_swipeUpdate); + removeWLSignal(&Events::listen_swipeEnd); + removeWLSignal(&Events::listen_pinchBegin); + removeWLSignal(&Events::listen_pinchUpdate); + removeWLSignal(&Events::listen_pinchEnd); + removeWLSignal(&Events::listen_touchBegin); + removeWLSignal(&Events::listen_touchEnd); + removeWLSignal(&Events::listen_touchUpdate); + removeWLSignal(&Events::listen_touchFrame); + removeWLSignal(&Events::listen_holdBegin); + removeWLSignal(&Events::listen_holdEnd); + removeWLSignal(&Events::listen_newInput); + removeWLSignal(&Events::listen_requestMouse); + removeWLSignal(&Events::listen_requestSetSel); + removeWLSignal(&Events::listen_requestDrag); + removeWLSignal(&Events::listen_startDrag); + removeWLSignal(&Events::listen_requestSetSel); + removeWLSignal(&Events::listen_requestSetPrimarySel); + removeWLSignal(&Events::listen_newLayerSurface); + removeWLSignal(&Events::listen_change); + removeWLSignal(&Events::listen_outputMgrApply); + removeWLSignal(&Events::listen_outputMgrTest); + removeWLSignal(&Events::listen_newConstraint); + removeWLSignal(&Events::listen_NewXDGDeco); + removeWLSignal(&Events::listen_newVirtPtr); + removeWLSignal(&Events::listen_newVirtualKeyboard); + removeWLSignal(&Events::listen_RendererDestroy); + removeWLSignal(&Events::listen_newIdleInhibitor); + removeWLSignal(&Events::listen_powerMgrSetMode); + removeWLSignal(&Events::listen_newIME); + removeWLSignal(&Events::listen_newTextInput); + removeWLSignal(&Events::listen_activateXDG); + removeWLSignal(&Events::listen_newSessionLock); + removeWLSignal(&Events::listen_setGamma); + removeWLSignal(&Events::listen_setCursorShape); + removeWLSignal(&Events::listen_newTearingHint); + + if (m_sWRLDRMLeaseMgr) + removeWLSignal(&Events::listen_leaseRequest); + + if (m_sWLRSession) + removeWLSignal(&Events::listen_sessionActive); +} + void CCompositor::cleanup() { if (!m_sWLDisplay || m_bIsShuttingDown) return; @@ -373,8 +406,31 @@ void CCompositor::cleanup() { g_pXWaylandManager->m_sWLRXWayland = nullptr; } + removeAllSignals(); + wl_display_destroy_clients(g_pCompositor->m_sWLDisplay); + g_pDecorationPositioner.reset(); + g_pPluginSystem.reset(); + g_pHyprNotificationOverlay.reset(); + g_pDebugOverlay.reset(); + g_pEventManager.reset(); + g_pSessionLockManager.reset(); + g_pProtocolManager.reset(); + g_pHyprRenderer.reset(); + g_pHyprOpenGL.reset(); + g_pInputManager.reset(); + g_pThreadManager.reset(); + g_pConfigManager.reset(); + g_pLayoutManager.reset(); + g_pHyprError.reset(); + g_pConfigManager.reset(); + g_pAnimationManager.reset(); + g_pKeybindManager.reset(); + g_pHookSystem.reset(); + g_pWatchdog.reset(); + g_pXWaylandManager.reset(); + wl_display_terminate(m_sWLDisplay); m_sWLDisplay = nullptr; diff --git a/src/Compositor.hpp b/src/Compositor.hpp index 22b94dbb..9372839a 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -214,6 +214,7 @@ class CCompositor { private: void initAllSignals(); + void removeAllSignals(); void setRandomSplash(); void initManagers(eManagersInitStage stage); void prepareFallbackOutput(); diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 5eb99bfd..2905ce2b 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -159,6 +159,13 @@ void addWLSignal(wl_signal* pSignal, wl_listener* pListener, void* pOwner, const Debug::log(LOG, "Registered signal for owner {:x}: {:x} -> {:x} (owner: {})", (uintptr_t)pOwner, (uintptr_t)pSignal, (uintptr_t)pListener, ownerString); } +void removeWLSignal(wl_listener* pListener) { + wl_list_remove(&pListener->link); + wl_list_init(&pListener->link); + + Debug::log(LOG, "Removed listener {:x}", (uintptr_t)pListener); +} + void handleNoop(struct wl_listener* listener, void* data) { // Do nothing } diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index 1ccbdc0e..23fc6e5a 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -15,6 +15,7 @@ struct SCallstackFrameInfo { std::string absolutePath(const std::string&, const std::string&); void addWLSignal(wl_signal*, wl_listener*, void* pOwner, const std::string& ownerString); +void removeWLSignal(wl_listener*); std::string escapeJSONStrings(const std::string& str); std::string removeBeginEndSpacesTabs(std::string); bool isNumber(const std::string&, bool allowfloat = false); diff --git a/src/helpers/WLListener.cpp b/src/helpers/WLListener.cpp index 402c999c..f499b929 100644 --- a/src/helpers/WLListener.cpp +++ b/src/helpers/WLListener.cpp @@ -7,13 +7,15 @@ void handleWrapped(wl_listener* listener, void* data) { CHyprWLListener::SWrapper* pWrap = wl_container_of(listener, pWrap, m_sListener); - g_pWatchdog->startWatching(); + if (g_pWatchdog) + g_pWatchdog->startWatching(); try { pWrap->m_pSelf->emit(data); } catch (std::exception& e) { Debug::log(ERR, "Listener {} timed out and was killed by Watchdog!!!", (uintptr_t)listener); } - g_pWatchdog->endWatching(); + if (g_pWatchdog) + g_pWatchdog->endWatching(); } CHyprWLListener::CHyprWLListener(wl_signal* pSignal, std::function callback, void* pOwner) { diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 1fc8a679..ead94ed9 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1455,7 +1455,7 @@ void CKeybindManager::renameWorkspace(std::string args) { } void CKeybindManager::exitHyprland(std::string argz) { - g_pCompositor->cleanup(); + g_pInputManager->m_bExitTriggered = true; } void CKeybindManager::moveCurrentWorkspaceToMonitor(std::string args) { diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index ce7e2b30..5a98accb 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -1185,6 +1185,9 @@ void CInputManager::onKeyboardKey(wlr_keyboard_key_event* e, SKeyboard* pKeyboar updateKeyboardsLeds(pKeyboard->keyboard); } + + if (m_bExitTriggered) + g_pCompositor->cleanup(); } void CInputManager::onKeyboardMod(void* data, SKeyboard* pKeyboard) { diff --git a/src/managers/input/InputManager.hpp b/src/managers/input/InputManager.hpp index 5fa5824e..eec45eff 100644 --- a/src/managers/input/InputManager.hpp +++ b/src/managers/input/InputManager.hpp @@ -243,6 +243,8 @@ class CInputManager { void restoreCursorIconToApp(); // no-op if restored + bool m_bExitTriggered = false; // for exit dispatcher + friend class CKeybindManager; friend class CWLSurface; };