From 50370e4216110bed8068ce9b6c1095452cc8456f Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Sun, 20 Mar 2022 15:55:47 +0100 Subject: [PATCH] added workspaces --- src/Compositor.cpp | 60 ++++++++++++++++++++++++++++----- src/Compositor.hpp | 8 +++++ src/Window.hpp | 1 + src/events/Events.cpp | 18 ++++++++++ src/helpers/Monitor.hpp | 1 + src/helpers/Workspace.hpp | 9 +++++ src/layout/DwindleLayout.cpp | 4 ++- src/managers/KeybindManager.cpp | 41 ++++++++++++++++++++++ src/managers/KeybindManager.hpp | 2 +- src/render/Renderer.cpp | 4 +-- 10 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 src/helpers/Workspace.hpp diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 9e414a53..27e0d2fd 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -203,10 +203,10 @@ bool CCompositor::windowExists(CWindow* pWindow) { } CWindow* CCompositor::vectorToWindow(const Vector2D& pos) { - const auto PMONITOR = getMonitorFromCursor(); + const auto PMONITOR = getMonitorFromVector(pos); for (auto& w : m_lWindows) { wlr_box box = {w.m_vRealPosition.x, w.m_vRealPosition.y, w.m_vRealSize.x, w.m_vRealSize.y}; - if (wlr_box_contains_point(&box, pos.x, pos.y)) // TODO: workspaces + if (wlr_box_contains_point(&box, pos.x, pos.y) && w.m_iWorkspaceID == PMONITOR->activeWorkspace) return &w; } @@ -214,18 +214,18 @@ CWindow* CCompositor::vectorToWindow(const Vector2D& pos) { } CWindow* CCompositor::vectorToWindowIdeal(const Vector2D& pos) { - const auto PMONITOR = getMonitorFromCursor(); + const auto PMONITOR = getMonitorFromVector(pos); // first loop over floating cuz they're above // TODO: make an actual Z-system for (auto& w : m_lWindows) { wlr_box box = {w.m_vRealPosition.x, w.m_vRealPosition.y, w.m_vRealSize.x, w.m_vRealSize.y}; - if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsFloating) // TODO: workspaces + if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsFloating && w.m_iWorkspaceID == PMONITOR->activeWorkspace) return &w; } for (auto& w : m_lWindows) { wlr_box box = {w.m_vPosition.x, w.m_vPosition.y, w.m_vSize.x, w.m_vSize.y}; - if (wlr_box_contains_point(&box, pos.x, pos.y)) // TODO: workspaces + if (wlr_box_contains_point(&box, pos.x, pos.y) && w.m_iWorkspaceID == PMONITOR->activeWorkspace) return &w; } @@ -239,13 +239,13 @@ CWindow* CCompositor::windowFromCursor() { // TODO: make an actual Z-system for (auto& w : m_lWindows) { wlr_box box = {w.m_vRealPosition.x, w.m_vRealPosition.y, w.m_vRealSize.x, w.m_vRealSize.y}; - if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsFloating) // TODO: workspaces + if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsFloating && w.m_iWorkspaceID == PMONITOR->activeWorkspace) return &w; } for (auto& w : m_lWindows) { wlr_box box = {w.m_vPosition.x, w.m_vPosition.y, w.m_vSize.x, w.m_vSize.y}; - if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y)) // TODO: workspaces + if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_iWorkspaceID == PMONITOR->activeWorkspace) return &w; } @@ -256,7 +256,7 @@ CWindow* CCompositor::windowFloatingFromCursor() { const auto PMONITOR = getMonitorFromCursor(); for (auto& w : m_lWindows) { wlr_box box = {w.m_vRealPosition.x, w.m_vRealPosition.y, w.m_vRealSize.x, w.m_vRealSize.y}; - if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsFloating) // TODO: workspaces + if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsFloating && w.m_iWorkspaceID == PMONITOR->activeWorkspace) return &w; } @@ -359,5 +359,49 @@ CWindow* CCompositor::getWindowFromSurface(wlr_surface* pSurface) { return &w; } + return nullptr; +} + +bool CCompositor::isWorkspaceVisible(const int& w) { + for (auto& m : m_lMonitors) { + if (m.activeWorkspace == w) + return true; + } + + return false; +} + +SWorkspace* CCompositor::getWorkspaceByID(const int& id) { + for (auto& w : m_lWorkspaces) { + if (w.ID == id) + return &w; + } + + return nullptr; +} + +void CCompositor::sanityCheckWorkspaces() { + for (auto it = m_lWorkspaces.begin(); it != m_lWorkspaces.end(); ++it) { + if (getWindowsOnWorkspace(it->ID) == 0 && !isWorkspaceVisible(it->ID)) + it = m_lWorkspaces.erase(it); + } +} + +int CCompositor::getWindowsOnWorkspace(const int& id) { + int no = 0; + for (auto& w : m_lWindows) { + if (w.m_iWorkspaceID == id) + no++; + } + + return no; +} + +CWindow* CCompositor::getFirstWindowOnWorkspace(const int& id) { + for (auto& w : m_lWindows) { + if (w.m_iWorkspaceID == id) + return &w; + } + return nullptr; } \ No newline at end of file diff --git a/src/Compositor.hpp b/src/Compositor.hpp index b1cb1370..030aaa9b 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -14,8 +14,10 @@ #include "managers/LayoutManager.hpp" #include "managers/KeybindManager.hpp" #include "helpers/Monitor.hpp" +#include "helpers/Workspace.hpp" #include "Window.hpp" #include "render/Renderer.hpp" + class CCompositor { public: CCompositor(); @@ -50,6 +52,7 @@ public: std::list m_lWindows; std::list m_lLayerPopups; std::list m_lXDGPopups; + std::list m_lWorkspaces; void startCompositor(); @@ -75,6 +78,11 @@ public: SLayerSurface* getLayerForPopup(SLayerPopup*); CWindow* getWindowForPopup(wlr_xdg_popup*); CWindow* getWindowFromSurface(wlr_surface*); + bool isWorkspaceVisible(const int&); + SWorkspace* getWorkspaceByID(const int&); + void sanityCheckWorkspaces(); + int getWindowsOnWorkspace(const int&); + CWindow* getFirstWindowOnWorkspace(const int&); private: void initAllSignals(); diff --git a/src/Window.hpp b/src/Window.hpp index 66ef93c9..116ffb20 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -39,6 +39,7 @@ public: bool m_bIsFullscreen = false; uint64_t m_iMonitorID = -1; std::string m_szTitle = ""; + int m_iWorkspaceID = -1; // XWayland stuff bool m_bIsX11 = false; diff --git a/src/events/Events.cpp b/src/events/Events.cpp index 8f4dd105..db8318ba 100644 --- a/src/events/Events.cpp +++ b/src/events/Events.cpp @@ -67,6 +67,16 @@ void Events::listener_newOutput(wl_listener* listener, void* data) { newMonitor.vecPosition = monitorRule.offset; newMonitor.vecSize = monitorRule.resolution; newMonitor.refreshRate = monitorRule.refreshRate; + + // Workspace + g_pCompositor->m_lWorkspaces.push_back(SWorkspace()); + const auto PNEWWORKSPACE = &g_pCompositor->m_lWorkspaces.back(); + + PNEWWORKSPACE->ID = g_pCompositor->m_lWorkspaces.size(); + PNEWWORKSPACE->monitorID = newMonitor.ID; + + newMonitor.activeWorkspace = PNEWWORKSPACE->ID; + g_pCompositor->m_lMonitors.push_back(newMonitor); // @@ -106,6 +116,13 @@ void Events::listener_monitorFrame(wl_listener* listener, void* data) { wlr_renderer_end(g_pCompositor->m_sWLRRenderer); wlr_output_commit(PMONITOR->output); + + + // Sanity check the workspaces. + // Hack: only check when monitor number 1 refreshes, saves a bit of resources. + if (PMONITOR->ID == 0) { + g_pCompositor->sanityCheckWorkspaces(); + } } void Events::listener_monitorDestroy(wl_listener* listener, void* data) { @@ -485,6 +502,7 @@ void Events::listener_mapWindow(wl_listener* listener, void* data) { const auto PMONITOR = g_pCompositor->getMonitorFromCursor(); PWINDOW->m_iMonitorID = PMONITOR->ID; PWINDOW->m_bMappedX11 = true; + PWINDOW->m_iWorkspaceID = PMONITOR->activeWorkspace; if (g_pXWaylandManager->shouldBeFloated(PWINDOW)) g_pLayoutManager->getCurrentLayout()->onWindowCreatedFloating(PWINDOW); diff --git a/src/helpers/Monitor.hpp b/src/helpers/Monitor.hpp index b6d65f9b..0977b1f8 100644 --- a/src/helpers/Monitor.hpp +++ b/src/helpers/Monitor.hpp @@ -13,6 +13,7 @@ struct SMonitor { bool primary = false; uint64_t ID = -1; + int activeWorkspace = -1; std::string szName = ""; diff --git a/src/helpers/Workspace.hpp b/src/helpers/Workspace.hpp new file mode 100644 index 00000000..d4ec5b05 --- /dev/null +++ b/src/helpers/Workspace.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "../defines.hpp" + +struct SWorkspace { + int ID = -1; + uint64_t monitorID = -1; + bool hasFullscreenWindow = false; +}; \ No newline at end of file diff --git a/src/layout/DwindleLayout.cpp b/src/layout/DwindleLayout.cpp index b932c222..69808ae2 100644 --- a/src/layout/DwindleLayout.cpp +++ b/src/layout/DwindleLayout.cpp @@ -301,8 +301,10 @@ void CHyprDwindleLayout::onMouseMove(const Vector2D& mousePos) { // and check its monitor const auto PMONITOR = g_pCompositor->getMonitorFromVector(middle); - if (PMONITOR) + if (PMONITOR) { DRAGGINGWINDOW->m_iMonitorID = PMONITOR->ID; + DRAGGINGWINDOW->m_iWorkspaceID = PMONITOR->activeWorkspace; + } } void CHyprDwindleLayout::onWindowCreatedFloating(CWindow* pWindow) { diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 35f2269d..68f6dbc4 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -43,6 +43,7 @@ bool CKeybindManager::handleKeybinds(const uint32_t& modmask, const xkb_keysym_t if (k.handler == "exec") { spawn(k.arg); } else if (k.handler == "killactive") { killActive(k.arg); } else if (k.handler == "togglefloating") { toggleActiveFloating(k.arg); } + else if (k.handler == "workspace") { changeworkspace(k.arg); } found = true; } @@ -84,4 +85,44 @@ void CKeybindManager::toggleActiveFloating(std::string args) { g_pLayoutManager->getCurrentLayout()->changeWindowFloatingMode(ACTIVEWINDOW); } +} + +void CKeybindManager::changeworkspace(std::string args) { + int workspaceToChangeTo = 0; + try { + workspaceToChangeTo = stoi(args); + } catch (...) { + Debug::log(ERR, "Invalid arg \"%s\" in changeWorkspace!", args.c_str()); + } + + // if it exists, we warp to it + if (g_pCompositor->getWorkspaceByID(workspaceToChangeTo)) { + const auto PMONITOR = g_pCompositor->getMonitorFromID(g_pCompositor->getWorkspaceByID(workspaceToChangeTo)->monitorID); + + // if it's not visible, make it visible. + if (!g_pCompositor->isWorkspaceVisible(workspaceToChangeTo)) + PMONITOR->activeWorkspace = workspaceToChangeTo; + + // If the monitor is not the one our cursor's at, warp to it. + if (PMONITOR != g_pCompositor->getMonitorFromCursor()) { + Vector2D middle = PMONITOR->vecPosition + PMONITOR->vecSize / 2.f; + wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr, middle.x, middle.y); + } + + // focus the first window + g_pCompositor->focusWindow(g_pCompositor->getFirstWindowOnWorkspace(workspaceToChangeTo)); + + return; + } + + // Workspace doesn't exist, create and switch + const auto PMONITOR = g_pCompositor->getMonitorFromCursor(); + + g_pCompositor->m_lWorkspaces.push_back(SWorkspace()); + const auto PWORKSPACE = &g_pCompositor->m_lWorkspaces.back(); + + PWORKSPACE->ID = workspaceToChangeTo; + PWORKSPACE->monitorID = PMONITOR->ID; + + PMONITOR->activeWorkspace = workspaceToChangeTo; } \ No newline at end of file diff --git a/src/managers/KeybindManager.hpp b/src/managers/KeybindManager.hpp index 5aeb3a05..abff6b4b 100644 --- a/src/managers/KeybindManager.hpp +++ b/src/managers/KeybindManager.hpp @@ -26,7 +26,7 @@ private: void killActive(std::string); void spawn(std::string); void toggleActiveFloating(std::string); - + void changeworkspace(std::string); }; inline std::unique_ptr g_pKeybindManager; \ No newline at end of file diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index 74ebb122..6b01a00f 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -50,7 +50,7 @@ void CHyprRenderer::renderAllClientsForMonitor(const int& ID, timespec* time) { for (auto& w : g_pCompositor->m_lWindows) { - if (w.m_bIsX11) + if (w.m_bIsX11 || w.m_iWorkspaceID != PMONITOR->activeWorkspace) continue; wlr_box geometry = { w.m_vRealPosition.x, w.m_vRealPosition.y, w.m_vRealSize.x, w.m_vRealSize.y }; @@ -71,7 +71,7 @@ void CHyprRenderer::renderAllClientsForMonitor(const int& ID, timespec* time) { for (auto& w : g_pCompositor->m_lWindows) { - if (!w.m_bIsX11) + if (!w.m_bIsX11 || w.m_iWorkspaceID != PMONITOR->activeWorkspace) continue; if (!g_pCompositor->windowValidMapped(&w))