From ad87629d51305c9f39382d65b0a516621a96f1a9 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Wed, 24 Nov 2021 18:51:34 +0100 Subject: [PATCH] Sanity check against duped children --- src/events/events.cpp | 17 +++++++++---- src/windowManager.cpp | 56 +++++++++++++++++++++++++++++++++++++++++-- src/windowManager.hpp | 1 + 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/events/events.cpp b/src/events/events.cpp index 14059e7..efcd4e7 100644 --- a/src/events/events.cpp +++ b/src/events/events.cpp @@ -100,6 +100,19 @@ CWindow* Events::remapFloatingWindow(int windowID) { CWindow* Events::remapWindow(int windowID, bool wasfloating) { // Do the setup of the window's params and stuf + + // For all floating windows, get their default size + const auto GEOMETRYCOOKIE = xcb_get_geometry(g_pWindowManager->DisplayConnection, windowID); + const auto GEOMETRY = xcb_get_geometry_reply(g_pWindowManager->DisplayConnection, GEOMETRYCOOKIE, 0); + + if (GEOMETRY) { + // Check if the window isn't bullshit. + if (GEOMETRY->width < 2 || GEOMETRY->height < 2) { + // Bullshit window. set to floating. + return Events::remapFloatingWindow(windowID); + } + } + CWindow window; window.setDrawable(windowID); window.setIsFloating(false); @@ -112,10 +125,6 @@ CWindow* Events::remapWindow(int windowID, bool wasfloating) { window.setWorkspaceID(g_pWindowManager->activeWorkspaces[CURRENTSCREEN]); window.setMonitor(CURRENTSCREEN); - // For all floating windows, get their default size - const auto GEOMETRYCOOKIE = xcb_get_geometry(g_pWindowManager->DisplayConnection, windowID); - const auto GEOMETRY = xcb_get_geometry_reply(g_pWindowManager->DisplayConnection, GEOMETRYCOOKIE, 0); - if (GEOMETRY) { window.setDefaultPosition(Vector2D(GEOMETRY->x, GEOMETRY->y)); window.setDefaultSize(Vector2D(GEOMETRY->width, GEOMETRY->height)); diff --git a/src/windowManager.cpp b/src/windowManager.cpp index b290372..cd1f1b7 100644 --- a/src/windowManager.cpp +++ b/src/windowManager.cpp @@ -259,11 +259,14 @@ bool CWindowManager::handleEvent() { free(ev); } - // TODO: sanity check on open/closed window. - // refresh and apply the parameters of all dirty windows. refreshDirtyWindows(); + // Sanity checks + for (const auto active : activeWorkspaces) { + sanityCheckOnWorkspace(active); + } + // remove unused workspaces cleanupUnusedWorkspaces(); @@ -405,6 +408,55 @@ void CWindowManager::setFocusedWindow(xcb_drawable_t window) { } } +void CWindowManager::sanityCheckOnWorkspace(int workspaceID) { + for (auto& w : windows) { + if (w.getWorkspaceID() == workspaceID) { + + // Check #1: Parent has 2 identical children (happens!) + if (w.getDrawable() < 0) { + const auto CHILDA = w.getChildNodeAID(); + const auto CHILDB = w.getChildNodeBID(); + + if (CHILDA == CHILDB) { + // Fix. Remove this parent, replace with child. + Debug::log(LOG, "Sanity check A triggered for window ID " + std::to_string(w.getDrawable())); + + const auto PCHILD = getWindowFromDrawable(CHILDA); + + if (!PCHILD) + continue; // Should be cleaned later. + + PCHILD->setPosition(w.getPosition()); + PCHILD->setSize(w.getSize()); + + // make the sibling replace the parent + PCHILD->setParentNodeID(w.getParentNodeID()); + + if (w.getParentNodeID() != 0 && getWindowFromDrawable(w.getParentNodeID())) { + if (getWindowFromDrawable(w.getParentNodeID())->getChildNodeAID() == w.getDrawable()) { + getWindowFromDrawable(w.getParentNodeID())->setChildNodeAID(w.getDrawable()); + } else { + getWindowFromDrawable(w.getParentNodeID())->setChildNodeBID(w.getDrawable()); + } + } + + // Make the sibling eat the closed window + PCHILD->setDirtyRecursive(true); + PCHILD->recalcSizePosRecursive(); + + // Remove the parent + removeWindowFromVectorSafe(w.getDrawable()); + + if (findWindowAtCursor()) + setFocusedWindow(findWindowAtCursor()->getDrawable()); // Set focus. :) + + Debug::log(LOG, "Sanity check A finished successfully."); + } + } + } + } +} + CWindow* CWindowManager::getWindowFromDrawable(int64_t window) { for(auto& w : windows) { if (w.getDrawable() == window) { diff --git a/src/windowManager.hpp b/src/windowManager.hpp index 4cc91fa..e656036 100644 --- a/src/windowManager.hpp +++ b/src/windowManager.hpp @@ -86,6 +86,7 @@ public: void setupRandrMonitors(); + void sanityCheckOnWorkspace(int); CWindow* getNeighborInDir(char dir); void eatWindow(CWindow* a, CWindow* toEat); bool canEatWindow(CWindow* a, CWindow* toEat);