From 5a520914ace7a52845f7ec707581b4d01b1b94fb Mon Sep 17 00:00:00 2001 From: Tom Benham Date: Fri, 12 Apr 2024 10:46:56 +0200 Subject: [PATCH] Added m_pSwallowedBy symmetric reference to avoid dangling m_pSwallowed pointer --- src/desktop/Window.hpp | 1 + src/events/Windows.cpp | 13 ++++++++++--- src/managers/KeybindManager.cpp | 10 ++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/desktop/Window.hpp b/src/desktop/Window.hpp index 530501d3..7e700199 100644 --- a/src/desktop/Window.hpp +++ b/src/desktop/Window.hpp @@ -342,6 +342,7 @@ class CWindow { // swallowing CWindow* m_pSwallowed = nullptr; CWindow* m_pPreviouslySwallowed = nullptr; + CWindow* m_pSwallowedBy = nullptr; // focus stuff bool m_bStayFocused = false; diff --git a/src/events/Windows.cpp b/src/events/Windows.cpp index b1db5286..2ac198e7 100644 --- a/src/events/Windows.cpp +++ b/src/events/Windows.cpp @@ -583,7 +583,8 @@ void Events::listener_mapWindow(void* owner, void* data) { // check if it's the window we want & not exempt from getting swallowed if (valid) { // swallow - PWINDOW->m_pSwallowed = finalFound; + PWINDOW->m_pSwallowed = finalFound; + finalFound->m_pSwallowedBy = PWINDOW; g_pLayoutManager->getCurrentLayout()->onWindowRemoved(finalFound); @@ -687,10 +688,16 @@ void Events::listener_unmapWindow(void* owner, void* data) { g_pHyprOpenGL->makeWindowSnapshot(PWINDOW); // swallowing - if (PWINDOW->m_pSwallowed && g_pCompositor->windowExists(PWINDOW->m_pSwallowed)) { + if (PWINDOW->m_pSwallowed) { PWINDOW->m_pSwallowed->setHidden(false); g_pLayoutManager->getCurrentLayout()->onWindowCreated(PWINDOW->m_pSwallowed); - PWINDOW->m_pSwallowed = nullptr; + PWINDOW->m_pSwallowed->m_pSwallowedBy = nullptr; + PWINDOW->m_pSwallowed = nullptr; + } + // swallowed + if (PWINDOW->m_pSwallowedBy) { + PWINDOW->m_pSwallowedBy->m_pSwallowed = nullptr; + PWINDOW->m_pSwallowedBy = nullptr; } bool wasLastWindow = false; diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 347e2c77..a703664f 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1823,17 +1823,19 @@ void CKeybindManager::focusWindow(std::string regexp) { void CKeybindManager::toggleSwallow(std::string args) { CWindow* pWindow = g_pCompositor->m_pLastWindow; - if (pWindow->m_pSwallowed && g_pCompositor->windowExists(pWindow->m_pSwallowed)) { + if (pWindow->m_pSwallowed) { pWindow->m_pPreviouslySwallowed = pWindow->m_pSwallowed; pWindow->m_pSwallowed->setHidden(false); g_pLayoutManager->getCurrentLayout()->onWindowCreated(pWindow->m_pSwallowed); - pWindow->m_pSwallowed = nullptr; + pWindow->m_pSwallowed->m_pSwallowedBy = nullptr; + pWindow->m_pSwallowed = nullptr; - } else if (pWindow->m_pPreviouslySwallowed && g_pCompositor->windowExists(pWindow->m_pPreviouslySwallowed)) { - pWindow->m_pSwallowed = pWindow->m_pPreviouslySwallowed; + } else if (pWindow->m_pPreviouslySwallowed) { + pWindow->m_pSwallowed = pWindow->m_pPreviouslySwallowed; + pWindow->m_pSwallowed->m_pSwallowedBy = pWindow; g_pLayoutManager->getCurrentLayout()->onWindowRemoved(pWindow->m_pPreviouslySwallowed);