From b608bb813b70a51e7e06bd1ac06ca1f5bfe4419c Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Thu, 12 Oct 2023 14:50:27 -0300 Subject: [PATCH 1/9] feat: run optional default command when switching to an empty workspace --- src/helpers/MiscFunctions.cpp | 54 +++++++++++++++++++++++++++++++++ src/helpers/MiscFunctions.hpp | 1 + src/helpers/VarList.hpp | 10 ++++++ src/managers/KeybindManager.cpp | 24 +++++++++++---- 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 47c1180a1aa..9d7995f7db1 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -503,6 +503,60 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { return result; } +static inline void ltrim(std::string& s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); +} + +int parseWorkspaceAndDefaultCmd(std::string inArgs, std::string& outWorkspaceName, std::string& outDefaultCmd) { + + std::string workspaceToToggle; + if (inArgs.contains(',')) { + const int commaIdx = inArgs.find_first_of(','); + workspaceToToggle = inArgs.substr(0, commaIdx); + outDefaultCmd = inArgs.substr(commaIdx + 1); + } else { + workspaceToToggle = inArgs; + } + + int workspaceID = getWorkspaceIDFromString(workspaceToToggle, outWorkspaceName); + + ltrim(outDefaultCmd); + + if (!outDefaultCmd.empty()) { + std::string rules; + const std::string workspaceRule = "workspace " + outWorkspaceName; + std::string cmd = outDefaultCmd; + + if (outDefaultCmd[0] == '[') { + const int closingBracketIdx = outDefaultCmd.find_last_of(']'); + auto tmpRules = outDefaultCmd.substr(1, closingBracketIdx - 1); + cmd = outDefaultCmd.substr(closingBracketIdx + 1); + + auto rulesList = CVarList(tmpRules, 0, ';'); + + bool hadWorkspaceRule = false; + rulesList.map([&](std::string& rule) { + if (rule.find("workspace") == 0) { + rule = workspaceRule; + hadWorkspaceRule = true; + } + }); + + if (!hadWorkspaceRule) { + rulesList.append(workspaceRule); + } + + rules = "[" + rulesList.join(";") + "]"; + } else { + rules = "[" + workspaceRule + "]"; + } + + outDefaultCmd = rules + " " + cmd; + } + + return workspaceID; +} + float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2) { const float DX = std::max({0.0, p1.x - vec.x, vec.x - p2.x}); const float DY = std::max({0.0, p1.y - vec.y, vec.y - p2.y}); diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index 429f381f39a..628a71faa24 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -20,6 +20,7 @@ std::string removeBeginEndSpacesTabs(std::string); bool isNumber(const std::string&, bool allowfloat = false); bool isDirection(const std::string&); int getWorkspaceIDFromString(const std::string&, std::string&); +int parseWorkspaceAndDefaultCmd(std::string, std::string&, std::string&); float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2); void logSystemInfo(); std::string execAndGet(const char*); diff --git a/src/helpers/VarList.hpp b/src/helpers/VarList.hpp index 60e9a5519a5..8f33f16673a 100644 --- a/src/helpers/VarList.hpp +++ b/src/helpers/VarList.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include "../macros.hpp" @@ -20,6 +21,15 @@ class CVarList { std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const; + void map(std::function func) { + for (auto& s : m_vArgs) + func(s); + } + + void append(const std::string arg) { + m_vArgs.emplace_back(arg); + } + std::string operator[](const size_t& idx) const { if (idx >= m_vArgs.size()) return ""; diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index fd298d34f59..9ac8db95878 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1,5 +1,7 @@ #include "KeybindManager.hpp" #include "../render/decorations/CHyprGroupBarDecoration.hpp" +#include "debug/Log.hpp" +#include "helpers/VarList.hpp" #include @@ -799,6 +801,7 @@ void CKeybindManager::toggleActivePseudo(std::string args) { void CKeybindManager::changeworkspace(std::string args) { int workspaceToChangeTo = 0; std::string workspaceName = ""; + std::string defaultCmd = ""; // Workspace_back_and_forth being enabled means that an attempt to switch to // the current workspace will instead switch to the previous. @@ -810,7 +813,7 @@ void CKeybindManager::changeworkspace(std::string args) { const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace); const bool EXPLICITPREVIOUS = args.find("previous") == 0; - if (args.find("previous") == 0) { + if (EXPLICITPREVIOUS) { // Do nothing if there's no previous workspace, otherwise switch to it. if (PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1) { Debug::log(LOG, "No previous workspace to change to"); @@ -825,7 +828,7 @@ void CKeybindManager::changeworkspace(std::string args) { PCURRENTWORKSPACE->m_sPrevWorkspace.name.empty() ? std::to_string(PCURRENTWORKSPACE->m_sPrevWorkspace.iID) : PCURRENTWORKSPACE->m_sPrevWorkspace.name; } } else { - workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName); + workspaceToChangeTo = parseWorkspaceAndDefaultCmd(args, workspaceName, defaultCmd); } if (workspaceToChangeTo == INT_MAX) { @@ -842,10 +845,15 @@ void CKeybindManager::changeworkspace(std::string args) { g_pInputManager->m_bEmptyFocusCursorSet = false; auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo); - if (!pWorkspaceToChangeTo) + if (!pWorkspaceToChangeTo) { pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo, PMONITOR->ID, BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.name : workspaceName); + if (!defaultCmd.empty()) { + spawn(defaultCmd); + } + } + if (!BISWORKSPACECURRENT && pWorkspaceToChangeTo->m_bIsSpecialWorkspace) { PMONITOR->setSpecialWorkspace(pWorkspaceToChangeTo); g_pInputManager->simulateMouseMovement(); @@ -1440,8 +1448,8 @@ void CKeybindManager::toggleSpecialWorkspace(std::string args) { static auto* const PFOLLOWMOUSE = &g_pConfigManager->getConfigValuePtr("input:follow_mouse")->intValue; - std::string workspaceName = ""; - int workspaceID = getWorkspaceIDFromString("special:" + args, workspaceName); + std::string workspaceName, defaultCmd; + int workspaceID = parseWorkspaceAndDefaultCmd("special:" + args, workspaceName, defaultCmd); if (workspaceID == INT_MAX || !g_pCompositor->isWorkspaceSpecial(workspaceID)) { Debug::log(ERR, "Invalid workspace passed to special"); @@ -1467,8 +1475,12 @@ void CKeybindManager::toggleSpecialWorkspace(std::string args) { Debug::log(LOG, "Toggling special workspace {} to open", workspaceID); auto PSPECIALWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceID); - if (!PSPECIALWORKSPACE) + if (!PSPECIALWORKSPACE) { PSPECIALWORKSPACE = g_pCompositor->createNewWorkspace(workspaceID, PMONITOR->ID, workspaceName); + if (!defaultCmd.empty()) { + spawn(defaultCmd); + } + } PMONITOR->setSpecialWorkspace(PSPECIALWORKSPACE); } From 0bbad21d7b64bed58674ce1cfbbdbe6369ab6091 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Fri, 13 Oct 2023 19:45:03 -0300 Subject: [PATCH 2/9] feat: move default cmd for workspaces into workspace rule --- src/Compositor.cpp | 11 +++++++++-- src/Compositor.hpp | 2 +- src/config/ConfigManager.cpp | 6 ++++++ src/config/ConfigManager.hpp | 1 + src/helpers/MiscFunctions.cpp | 35 ++++++++++----------------------- src/helpers/MiscFunctions.hpp | 2 +- src/managers/KeybindManager.cpp | 18 +++++------------ 7 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index b9be9f18e34..e68d7f409bb 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -2044,7 +2044,7 @@ void CCompositor::moveWorkspaceToMonitor(CWorkspace* pWorkspace, CMonitor* pMoni Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with new {}", nextWorkspaceOnMonitorID); - g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID); + g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID, "", false); } Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with existing {}", nextWorkspaceOnMonitorID); @@ -2431,7 +2431,7 @@ bool CCompositor::cursorOnReservedArea() { return !VECINRECT(CURSORPOS, XY1.x, XY1.y, XY2.x, XY2.y); } -CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, const std::string& name) { +CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, const std::string& name, bool isEmpty) { const auto NAME = name == "" ? std::to_string(id) : name; auto monID = monid; @@ -2447,6 +2447,13 @@ CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, con PWORKSPACE->m_iID = id; PWORKSPACE->m_iMonitorID = monID; + if (isEmpty) { + const SWorkspaceRule workspaceRule = g_pConfigManager->getWorkspaceRuleFor(PWORKSPACE); + if (!workspaceRule.onCreatedEmptyRunCmd.empty()) { + g_pKeybindManager->spawn(workspaceRule.onCreatedEmptyRunCmd); + } + } + return PWORKSPACE; } diff --git a/src/Compositor.hpp b/src/Compositor.hpp index 86460211c93..a0f4da8a33b 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -194,7 +194,7 @@ class CCompositor { Vector2D parseWindowVectorArgsRelative(const std::string&, const Vector2D&); void forceReportSizesToWindowsOnWorkspace(const int&); bool cursorOnReservedArea(); - CWorkspace* createNewWorkspace(const int&, const int&, const std::string& name = ""); // will be deleted next frame if left empty and unfocused! + CWorkspace* createNewWorkspace(const int&, const int&, const std::string& name = "", bool isEmpty = true); // will be deleted next frame if left empty and unfocused! void renameWorkspace(const int&, const std::string& name = ""); void setActiveMonitor(CMonitor*); bool isWorkspaceSpecial(const int&); diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 2d19389e08f..c74f858d41b 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -2,6 +2,7 @@ #include "../managers/KeybindManager.hpp" #include +#include #include #include #include @@ -1172,6 +1173,9 @@ void CConfigManager::handleWorkspaceRules(const std::string& command, const std: rules = value.substr(WORKSPACE_DELIM + 1); } + const static std::string ruleOnCreatedEmtpy = "on-created-empty:"; + const static int ruleOnCreatedEmtpyLen = ruleOnCreatedEmtpy.length(); + auto assignRule = [&](std::string rule) { size_t delim = std::string::npos; if ((delim = rule.find("gapsin:")) != std::string::npos) @@ -1194,6 +1198,8 @@ void CConfigManager::handleWorkspaceRules(const std::string& command, const std: wsRule.isDefault = configStringToInt(rule.substr(delim + 8)); else if ((delim = rule.find("persistent:")) != std::string::npos) wsRule.isPersistent = configStringToInt(rule.substr(delim + 11)); + else if ((delim = rule.find(ruleOnCreatedEmtpy)) != std::string::npos) + wsRule.onCreatedEmptyRunCmd = cleanCmdForWorkspace(name, rule.substr(delim + ruleOnCreatedEmtpyLen)); }; size_t pos = 0; diff --git a/src/config/ConfigManager.hpp b/src/config/ConfigManager.hpp index e637979d275..23510c3e1dc 100644 --- a/src/config/ConfigManager.hpp +++ b/src/config/ConfigManager.hpp @@ -49,6 +49,7 @@ struct SWorkspaceRule { std::optional rounding; std::optional decorate; std::optional shadow; + std::string onCreatedEmptyRunCmd; }; struct SMonitorAdditionalReservedArea { diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 9d7995f7db1..fdc188fe1ad 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -503,34 +503,19 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { return result; } -static inline void ltrim(std::string& s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); -} - -int parseWorkspaceAndDefaultCmd(std::string inArgs, std::string& outWorkspaceName, std::string& outDefaultCmd) { - - std::string workspaceToToggle; - if (inArgs.contains(',')) { - const int commaIdx = inArgs.find_first_of(','); - workspaceToToggle = inArgs.substr(0, commaIdx); - outDefaultCmd = inArgs.substr(commaIdx + 1); - } else { - workspaceToToggle = inArgs; - } +std::string cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string dirtyCmd) { - int workspaceID = getWorkspaceIDFromString(workspaceToToggle, outWorkspaceName); - ltrim(outDefaultCmd); + std::string cmd = removeBeginEndSpacesTabs(dirtyCmd); - if (!outDefaultCmd.empty()) { + if (!cmd.empty()) { std::string rules; - const std::string workspaceRule = "workspace " + outWorkspaceName; - std::string cmd = outDefaultCmd; + const std::string workspaceRule = "workspace " + inWorkspaceName; - if (outDefaultCmd[0] == '[') { - const int closingBracketIdx = outDefaultCmd.find_last_of(']'); - auto tmpRules = outDefaultCmd.substr(1, closingBracketIdx - 1); - cmd = outDefaultCmd.substr(closingBracketIdx + 1); + if (cmd[0] == '[') { + const int closingBracketIdx = cmd.find_last_of(']'); + auto tmpRules = cmd.substr(1, closingBracketIdx - 1); + cmd = cmd.substr(closingBracketIdx + 1); auto rulesList = CVarList(tmpRules, 0, ';'); @@ -551,10 +536,10 @@ int parseWorkspaceAndDefaultCmd(std::string inArgs, std::string& outWorkspaceNam rules = "[" + workspaceRule + "]"; } - outDefaultCmd = rules + " " + cmd; + return rules + " " + cmd; } - return workspaceID; + return {}; } float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2) { diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index 628a71faa24..8b3c671654e 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -20,7 +20,7 @@ std::string removeBeginEndSpacesTabs(std::string); bool isNumber(const std::string&, bool allowfloat = false); bool isDirection(const std::string&); int getWorkspaceIDFromString(const std::string&, std::string&); -int parseWorkspaceAndDefaultCmd(std::string, std::string&, std::string&); +std::string cleanCmdForWorkspace(const std::string&, std::string); float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2); void logSystemInfo(); std::string execAndGet(const char*); diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 9ac8db95878..14620f41d8b 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -801,7 +801,6 @@ void CKeybindManager::toggleActivePseudo(std::string args) { void CKeybindManager::changeworkspace(std::string args) { int workspaceToChangeTo = 0; std::string workspaceName = ""; - std::string defaultCmd = ""; // Workspace_back_and_forth being enabled means that an attempt to switch to // the current workspace will instead switch to the previous. @@ -828,7 +827,7 @@ void CKeybindManager::changeworkspace(std::string args) { PCURRENTWORKSPACE->m_sPrevWorkspace.name.empty() ? std::to_string(PCURRENTWORKSPACE->m_sPrevWorkspace.iID) : PCURRENTWORKSPACE->m_sPrevWorkspace.name; } } else { - workspaceToChangeTo = parseWorkspaceAndDefaultCmd(args, workspaceName, defaultCmd); + workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName); } if (workspaceToChangeTo == INT_MAX) { @@ -848,10 +847,6 @@ void CKeybindManager::changeworkspace(std::string args) { if (!pWorkspaceToChangeTo) { pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo, PMONITOR->ID, BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.name : workspaceName); - - if (!defaultCmd.empty()) { - spawn(defaultCmd); - } } if (!BISWORKSPACECURRENT && pWorkspaceToChangeTo->m_bIsSpecialWorkspace) { @@ -947,7 +942,7 @@ void CKeybindManager::moveActiveToWorkspace(std::string args) { pMonitor = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID); g_pCompositor->setActiveMonitor(pMonitor); } else { - pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName); + pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName, false); pMonitor = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID); g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace); } @@ -998,7 +993,7 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) { if (pWorkspace) { g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace); } else { - pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName); + pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName, false); g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace); } @@ -1448,8 +1443,8 @@ void CKeybindManager::toggleSpecialWorkspace(std::string args) { static auto* const PFOLLOWMOUSE = &g_pConfigManager->getConfigValuePtr("input:follow_mouse")->intValue; - std::string workspaceName, defaultCmd; - int workspaceID = parseWorkspaceAndDefaultCmd("special:" + args, workspaceName, defaultCmd); + std::string workspaceName = ""; + int workspaceID = getWorkspaceIDFromString("special:" + args, workspaceName); if (workspaceID == INT_MAX || !g_pCompositor->isWorkspaceSpecial(workspaceID)) { Debug::log(ERR, "Invalid workspace passed to special"); @@ -1477,9 +1472,6 @@ void CKeybindManager::toggleSpecialWorkspace(std::string args) { if (!PSPECIALWORKSPACE) { PSPECIALWORKSPACE = g_pCompositor->createNewWorkspace(workspaceID, PMONITOR->ID, workspaceName); - if (!defaultCmd.empty()) { - spawn(defaultCmd); - } } PMONITOR->setSpecialWorkspace(PSPECIALWORKSPACE); From 41a073478f8b986ef20853cc5ba6c2c23e4bb271 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Tue, 17 Oct 2023 22:00:08 -0300 Subject: [PATCH 3/9] chore: remove on created empty from createNewWorkspace --- src/Compositor.cpp | 11 ++--------- src/Compositor.hpp | 2 +- src/managers/KeybindManager.cpp | 4 ++-- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 3f0385bbe3c..1a2b8823015 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -2044,7 +2044,7 @@ void CCompositor::moveWorkspaceToMonitor(CWorkspace* pWorkspace, CMonitor* pMoni Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with new {}", nextWorkspaceOnMonitorID); - g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID, "", false); + g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID, ""); } Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with existing {}", nextWorkspaceOnMonitorID); @@ -2431,7 +2431,7 @@ bool CCompositor::cursorOnReservedArea() { return !VECINRECT(CURSORPOS, XY1.x, XY1.y, XY2.x, XY2.y); } -CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, const std::string& name, bool isEmpty) { +CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, const std::string& name) { const auto NAME = name == "" ? std::to_string(id) : name; auto monID = monid; @@ -2447,13 +2447,6 @@ CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, con PWORKSPACE->m_iID = id; PWORKSPACE->m_iMonitorID = monID; - if (isEmpty) { - const SWorkspaceRule workspaceRule = g_pConfigManager->getWorkspaceRuleFor(PWORKSPACE); - if (!workspaceRule.onCreatedEmptyRunCmd.empty()) { - g_pKeybindManager->spawn(workspaceRule.onCreatedEmptyRunCmd); - } - } - return PWORKSPACE; } diff --git a/src/Compositor.hpp b/src/Compositor.hpp index a0f4da8a33b..86460211c93 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -194,7 +194,7 @@ class CCompositor { Vector2D parseWindowVectorArgsRelative(const std::string&, const Vector2D&); void forceReportSizesToWindowsOnWorkspace(const int&); bool cursorOnReservedArea(); - CWorkspace* createNewWorkspace(const int&, const int&, const std::string& name = "", bool isEmpty = true); // will be deleted next frame if left empty and unfocused! + CWorkspace* createNewWorkspace(const int&, const int&, const std::string& name = ""); // will be deleted next frame if left empty and unfocused! void renameWorkspace(const int&, const std::string& name = ""); void setActiveMonitor(CMonitor*); bool isWorkspaceSpecial(const int&); diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index d208df66d3e..1b0b34f2842 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -942,7 +942,7 @@ void CKeybindManager::moveActiveToWorkspace(std::string args) { pMonitor = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID); g_pCompositor->setActiveMonitor(pMonitor); } else { - pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName, false); + pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName); pMonitor = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID); g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace); } @@ -993,7 +993,7 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) { if (pWorkspace) { g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace); } else { - pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName, false); + pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName); g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace); } From 1834dbca7023622aff76205e482de8ade4919b32 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Wed, 18 Oct 2023 11:59:34 -0300 Subject: [PATCH 4/9] feat: check on-created-empty command during workspace sanity check --- src/Compositor.cpp | 33 +++++++++++++++++++++------------ src/helpers/Workspace.hpp | 3 +++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 1a2b8823015..bf387d4be5a 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1194,24 +1194,33 @@ void CCompositor::sanityCheckWorkspaces() { continue; } - const auto WINDOWSONWORKSPACE = getWindowsOnWorkspace((*it)->m_iID); + const auto& WORKSPACE = *it; + const auto WINDOWSONWORKSPACE = getWindowsOnWorkspace(WORKSPACE->m_iID); - if ((WINDOWSONWORKSPACE == 0 && !isWorkspaceVisible((*it)->m_iID))) { + if (WINDOWSONWORKSPACE == 0) { + if (!isWorkspaceVisible(WORKSPACE->m_iID)) { - if ((*it)->m_bIsSpecialWorkspace) { - if ((*it)->m_fAlpha.fl() > 0.f /* don't abruptly end the fadeout */) { - ++it; - continue; - } + if (WORKSPACE->m_bIsSpecialWorkspace) { + if (WORKSPACE->m_fAlpha.fl() > 0.f /* don't abruptly end the fadeout */) { + ++it; + continue; + } + + const auto PMONITOR = getMonitorFromID(WORKSPACE->m_iMonitorID); - const auto PMONITOR = getMonitorFromID((*it)->m_iMonitorID); + if (PMONITOR && PMONITOR->specialWorkspaceID == WORKSPACE->m_iID) + PMONITOR->setSpecialWorkspace(nullptr); + } - if (PMONITOR && PMONITOR->specialWorkspaceID == (*it)->m_iID) - PMONITOR->setSpecialWorkspace(nullptr); + it = m_vWorkspaces.erase(it); + continue; } + if (!WORKSPACE->m_bOnCreatedEmptyExecuted) { + if (!WORKSPACERULE.onCreatedEmptyRunCmd.empty()) + g_pKeybindManager->spawn(WORKSPACERULE.onCreatedEmptyRunCmd); - it = m_vWorkspaces.erase(it); - continue; + WORKSPACE->m_bOnCreatedEmptyExecuted = true; + } } ++it; diff --git a/src/helpers/Workspace.hpp b/src/helpers/Workspace.hpp index cbdbc07aac1..f3ccd930967 100644 --- a/src/helpers/Workspace.hpp +++ b/src/helpers/Workspace.hpp @@ -53,6 +53,9 @@ class CWorkspace { // last monitor (used on reconnect) std::string m_szLastMonitor = ""; + // Whether the user configured command for on-created-empty has been executed, if any + bool m_bOnCreatedEmptyExecuted = false; + void startAnim(bool in, bool left, bool instant = false); void setActive(bool on); From 1f9b570e6da7c7cd6a996eab460af05686efd61d Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Wed, 18 Oct 2023 12:02:06 -0300 Subject: [PATCH 5/9] chore: style suggestions --- src/helpers/MiscFunctions.cpp | 5 ++--- src/managers/KeybindManager.cpp | 8 +++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index fba9504afe1..56f7937f817 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -527,9 +527,8 @@ std::string cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string } }); - if (!hadWorkspaceRule) { + if (!hadWorkspaceRule) rulesList.append(workspaceRule); - } rules = "[" + rulesList.join(";") + "]"; } else { @@ -539,7 +538,7 @@ std::string cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string return rules + " " + cmd; } - return {}; + return ""; } float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2) { diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 1b0b34f2842..dfa1a075b06 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -812,7 +812,7 @@ void CKeybindManager::changeworkspace(std::string args) { const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace); const bool EXPLICITPREVIOUS = args.starts_with("previous"); - if (EXPLICITPREVIOUS) { + if (args.starts_with("previous")) { // Do nothing if there's no previous workspace, otherwise switch to it. if (PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1) { Debug::log(LOG, "No previous workspace to change to"); @@ -844,10 +844,9 @@ void CKeybindManager::changeworkspace(std::string args) { g_pInputManager->m_bEmptyFocusCursorSet = false; auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo); - if (!pWorkspaceToChangeTo) { + if (!pWorkspaceToChangeTo) pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo, PMONITOR->ID, BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.name : workspaceName); - } if (!BISWORKSPACECURRENT && pWorkspaceToChangeTo->m_bIsSpecialWorkspace) { PMONITOR->setSpecialWorkspace(pWorkspaceToChangeTo); @@ -1470,9 +1469,8 @@ void CKeybindManager::toggleSpecialWorkspace(std::string args) { Debug::log(LOG, "Toggling special workspace {} to open", workspaceID); auto PSPECIALWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceID); - if (!PSPECIALWORKSPACE) { + if (!PSPECIALWORKSPACE) PSPECIALWORKSPACE = g_pCompositor->createNewWorkspace(workspaceID, PMONITOR->ID, workspaceName); - } PMONITOR->setSpecialWorkspace(PSPECIALWORKSPACE); } From d8d258ea3d152cf831f152feaa668ff8f001fad9 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Sun, 22 Oct 2023 12:45:36 -0300 Subject: [PATCH 6/9] fix: leftover explicit optional parameter --- src/Compositor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index bf387d4be5a..19c90d13b33 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -2053,7 +2053,7 @@ void CCompositor::moveWorkspaceToMonitor(CWorkspace* pWorkspace, CMonitor* pMoni Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with new {}", nextWorkspaceOnMonitorID); - g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID, ""); + g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID); } Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with existing {}", nextWorkspaceOnMonitorID); From 0f0711f4475ee247761c80f6daa2ce2b2726984e Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Sun, 22 Oct 2023 13:19:45 -0300 Subject: [PATCH 7/9] feat: use optionals rather than empty strings for on created empty run cmd --- src/Compositor.cpp | 4 ++-- src/config/ConfigManager.hpp | 2 +- src/helpers/MiscFunctions.cpp | 7 ++++--- src/helpers/MiscFunctions.hpp | 3 ++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 19c90d13b33..8db18684bb4 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1216,8 +1216,8 @@ void CCompositor::sanityCheckWorkspaces() { continue; } if (!WORKSPACE->m_bOnCreatedEmptyExecuted) { - if (!WORKSPACERULE.onCreatedEmptyRunCmd.empty()) - g_pKeybindManager->spawn(WORKSPACERULE.onCreatedEmptyRunCmd); + if (auto cmd = WORKSPACERULE.onCreatedEmptyRunCmd) + g_pKeybindManager->spawn(*cmd); WORKSPACE->m_bOnCreatedEmptyExecuted = true; } diff --git a/src/config/ConfigManager.hpp b/src/config/ConfigManager.hpp index 23510c3e1dc..a6e68d9ce37 100644 --- a/src/config/ConfigManager.hpp +++ b/src/config/ConfigManager.hpp @@ -49,7 +49,7 @@ struct SWorkspaceRule { std::optional rounding; std::optional decorate; std::optional shadow; - std::string onCreatedEmptyRunCmd; + std::optional onCreatedEmptyRunCmd; }; struct SMonitorAdditionalReservedArea { diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 56f7937f817..65048342a2c 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -2,6 +2,7 @@ #include "../defines.hpp" #include #include "../Compositor.hpp" +#include #include #include #include @@ -503,7 +504,7 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { return result; } -std::string cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string dirtyCmd) { +std::optional cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string dirtyCmd) { std::string cmd = removeBeginEndSpacesTabs(dirtyCmd); @@ -535,10 +536,10 @@ std::string cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string rules = "[" + workspaceRule + "]"; } - return rules + " " + cmd; + return std::optional(rules + " " + cmd); } - return ""; + return std::nullopt; } float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2) { diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index 8b3c671654e..d489a0cd39b 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -20,7 +21,7 @@ std::string removeBeginEndSpacesTabs(std::string); bool isNumber(const std::string&, bool allowfloat = false); bool isDirection(const std::string&); int getWorkspaceIDFromString(const std::string&, std::string&); -std::string cleanCmdForWorkspace(const std::string&, std::string); +std::optional cleanCmdForWorkspace(const std::string&, std::string); float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2); void logSystemInfo(); std::string execAndGet(const char*); From 11eddd916df66a71bffbf325b594745f1247cd18 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Sun, 22 Oct 2023 13:20:45 -0300 Subject: [PATCH 8/9] chore(style): remove empty line --- src/helpers/MiscFunctions.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 65048342a2c..286b64cc538 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -506,7 +506,6 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { std::optional cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string dirtyCmd) { - std::string cmd = removeBeginEndSpacesTabs(dirtyCmd); if (!cmd.empty()) { From 1c3746fc50c510cd987975cdd1827a3695d4daa1 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Sun, 22 Oct 2023 13:33:56 -0300 Subject: [PATCH 9/9] chore(style): clang-format --- src/Compositor.cpp | 4 ++-- src/config/ConfigManager.cpp | 4 ++-- src/config/ConfigManager.hpp | 26 +++++++++++++------------- src/helpers/MiscFunctions.cpp | 4 ++-- src/helpers/Workspace.hpp | 5 ++--- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 8db18684bb4..8f0b6f0c8b2 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1194,8 +1194,8 @@ void CCompositor::sanityCheckWorkspaces() { continue; } - const auto& WORKSPACE = *it; - const auto WINDOWSONWORKSPACE = getWindowsOnWorkspace(WORKSPACE->m_iID); + const auto& WORKSPACE = *it; + const auto WINDOWSONWORKSPACE = getWindowsOnWorkspace(WORKSPACE->m_iID); if (WINDOWSONWORKSPACE == 0) { if (!isWorkspaceVisible(WORKSPACE->m_iID)) { diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 6edf73dfaa7..0fba90fcb5b 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -1173,10 +1173,10 @@ void CConfigManager::handleWorkspaceRules(const std::string& command, const std: rules = value.substr(WORKSPACE_DELIM + 1); } - const static std::string ruleOnCreatedEmtpy = "on-created-empty:"; + const static std::string ruleOnCreatedEmtpy = "on-created-empty:"; const static int ruleOnCreatedEmtpyLen = ruleOnCreatedEmtpy.length(); - auto assignRule = [&](std::string rule) { + auto assignRule = [&](std::string rule) { size_t delim = std::string::npos; if ((delim = rule.find("gapsin:")) != std::string::npos) wsRule.gapsIn = std::stoi(rule.substr(delim + 7)); diff --git a/src/config/ConfigManager.hpp b/src/config/ConfigManager.hpp index a6e68d9ce37..c6c726bfc8a 100644 --- a/src/config/ConfigManager.hpp +++ b/src/config/ConfigManager.hpp @@ -36,19 +36,19 @@ struct SConfigValue { }; struct SWorkspaceRule { - std::string monitor = ""; - std::string workspaceString = ""; - std::string workspaceName = ""; - int workspaceId = -1; - bool isDefault = false; - bool isPersistent = false; - std::optional gapsIn; - std::optional gapsOut; - std::optional borderSize; - std::optional border; - std::optional rounding; - std::optional decorate; - std::optional shadow; + std::string monitor = ""; + std::string workspaceString = ""; + std::string workspaceName = ""; + int workspaceId = -1; + bool isDefault = false; + bool isPersistent = false; + std::optional gapsIn; + std::optional gapsOut; + std::optional borderSize; + std::optional border; + std::optional rounding; + std::optional decorate; + std::optional shadow; std::optional onCreatedEmptyRunCmd; }; diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 286b64cc538..3cce73d2b1b 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -632,8 +632,8 @@ int64_t getPPIDof(int64_t pid) { return 0; #else - std::string dir = "/proc/" + std::to_string(pid) + "/status"; - FILE* infile; + std::string dir = "/proc/" + std::to_string(pid) + "/status"; + FILE* infile; infile = fopen(dir.c_str(), "r"); if (!infile) diff --git a/src/helpers/Workspace.hpp b/src/helpers/Workspace.hpp index f3ccd930967..a0bbb8157ff 100644 --- a/src/helpers/Workspace.hpp +++ b/src/helpers/Workspace.hpp @@ -4,8 +4,7 @@ #include #include "../defines.hpp" -enum eFullscreenMode : int8_t -{ +enum eFullscreenMode : int8_t { FULLSCREEN_INVALID = -1, FULLSCREEN_FULL = 0, FULLSCREEN_MAXIMIZED @@ -54,7 +53,7 @@ class CWorkspace { std::string m_szLastMonitor = ""; // Whether the user configured command for on-created-empty has been executed, if any - bool m_bOnCreatedEmptyExecuted = false; + bool m_bOnCreatedEmptyExecuted = false; void startAnim(bool in, bool left, bool instant = false); void setActive(bool on);