From 0cd3ef7c066dbd96cd54219c3af6ef72ba3ca826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D1=80=D1=8B=D0=BB=D0=BE=D0=B2=20=D0=90=D0=BB=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Wed, 19 Jun 2024 18:57:17 +0300 Subject: [PATCH] feat: [compositor, misc] ability to select previous workspace per monitor --- src/Compositor.cpp | 3 +- src/config/ConfigManager.cpp | 13 ++--- src/desktop/Workspace.cpp | 20 +++++-- src/desktop/Workspace.hpp | 34 ++++++------ src/events/Windows.cpp | 3 +- src/helpers/MiscFunctions.cpp | 98 ++++++++++++++++----------------- src/helpers/MiscFunctions.hpp | 9 ++- src/helpers/Monitor.cpp | 25 +++++---- src/macros.hpp | 4 +- src/managers/KeybindManager.cpp | 88 ++++++++++++++--------------- src/managers/input/Swipe.cpp | 12 ++-- 11 files changed, 156 insertions(+), 153 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 7777a55f77b2..0afde304d65e 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1657,8 +1657,7 @@ PHLWORKSPACE CCompositor::getWorkspaceByString(const std::string& str) { } try { - std::string name = ""; - return getWorkspaceByID(getWorkspaceIDFromString(str, name)); + return getWorkspaceByID(getWorkspaceIDNameFromString(str).id); } catch (std::exception& e) { Debug::log(ERR, "Error in getWorkspaceByString, invalid id"); } return nullptr; diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 17bbe465974d..6a1c896c3566 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -1815,14 +1815,13 @@ std::optional CConfigManager::handleMonitor(const std::string& comm newrule.vrr = std::stoi(ARGS[argno + 1]); argno++; } else if (ARGS[argno] == "workspace") { - std::string name = ""; - int wsId = getWorkspaceIDFromString(ARGS[argno + 1], name); + const auto& [id, name] = getWorkspaceIDNameFromString(ARGS[argno + 1]); SWorkspaceRule wsRule; wsRule.monitor = newrule.name; wsRule.workspaceString = ARGS[argno + 1]; + wsRule.workspaceId = id; wsRule.workspaceName = name; - wsRule.workspaceId = wsId; m_dWorkspaceRules.emplace_back(wsRule); argno++; @@ -2370,11 +2369,11 @@ std::optional CConfigManager::handleBlurLS(const std::string& comma std::optional CConfigManager::handleWorkspaceRules(const std::string& command, const std::string& value) { // This can either be the monitor or the workspace identifier - const auto FIRST_DELIM = value.find_first_of(','); + const auto FIRST_DELIM = value.find_first_of(','); - std::string name = ""; - auto first_ident = trim(value.substr(0, FIRST_DELIM)); - int id = getWorkspaceIDFromString(first_ident, name); + auto first_ident = trim(value.substr(0, FIRST_DELIM)); + + const auto& [id, name] = getWorkspaceIDNameFromString(first_ident); auto rules = value.substr(FIRST_DELIM + 1); SWorkspaceRule wsRule; diff --git a/src/desktop/Workspace.cpp b/src/desktop/Workspace.cpp index 04685bd5df2e..34db914e53ce 100644 --- a/src/desktop/Workspace.cpp +++ b/src/desktop/Workspace.cpp @@ -57,6 +57,13 @@ void CWorkspace::init(PHLWORKSPACE self) { EMIT_HOOK_EVENT("createWorkspace", this); } +SWorkspaceIDName CWorkspace::getPrevWorkspaceIDName(bool perMonitor) const { + if (perMonitor) + return m_sPrevWorkspacePerMonitor; + + return m_sPrevWorkspace; +} + CWorkspace::~CWorkspace() { m_vRenderOffset.unregister(); @@ -196,7 +203,7 @@ PHLWINDOW CWorkspace::getLastFocusedWindow() { void CWorkspace::rememberPrevWorkspace(const PHLWORKSPACE& prev) { if (!prev) { - m_sPrevWorkspace.iID = -1; + m_sPrevWorkspace.id = -1; m_sPrevWorkspace.name = ""; return; } @@ -206,8 +213,13 @@ void CWorkspace::rememberPrevWorkspace(const PHLWORKSPACE& prev) { return; } - m_sPrevWorkspace.iID = prev->m_iID; + m_sPrevWorkspace.id = prev->m_iID; m_sPrevWorkspace.name = prev->m_szName; + + if (prev->m_iMonitorID == m_iMonitorID) { + m_sPrevWorkspacePerMonitor.id = prev->m_iID; + m_sPrevWorkspacePerMonitor.name = prev->m_szName; + } } std::string CWorkspace::getConfigName() { @@ -228,9 +240,7 @@ bool CWorkspace::matchesStaticSelector(const std::string& selector_) { return true; if (isNumber(selector)) { - - std::string wsname = ""; - int wsid = getWorkspaceIDFromString(selector, wsname); + const auto& [wsid, wsname] = getWorkspaceIDNameFromString(selector); if (wsid == WORKSPACE_INVALID) return false; diff --git a/src/desktop/Workspace.hpp b/src/desktop/Workspace.hpp index 1743121535e8..ab3907aa836d 100644 --- a/src/desktop/Workspace.hpp +++ b/src/desktop/Workspace.hpp @@ -4,6 +4,7 @@ #include #include "../defines.hpp" #include "DesktopTypes.hpp" +#include "helpers/MiscFunctions.hpp" enum eFullscreenMode : int8_t { FULLSCREEN_INVALID = -1, @@ -25,17 +26,14 @@ class CWorkspace { int m_iID = -1; std::string m_szName = ""; uint64_t m_iMonitorID = -1; - // Previous workspace ID is stored during a workspace change, allowing travel + // Previous workspace ID and name is stored during a workspace change, allowing travel // to the previous workspace. - struct SPrevWorkspaceData { - int iID = -1; - std::string name = ""; - } m_sPrevWorkspace; + SWorkspaceIDName m_sPrevWorkspace, m_sPrevWorkspacePerMonitor; - bool m_bHasFullscreenWindow = false; - eFullscreenMode m_efFullscreenMode = FULLSCREEN_FULL; + bool m_bHasFullscreenWindow = false; + eFullscreenMode m_efFullscreenMode = FULLSCREEN_FULL; - wl_array m_wlrCoordinateArr; + wl_array m_wlrCoordinateArr; // for animations CAnimatedVariable m_vRenderOffset; @@ -63,21 +61,23 @@ class CWorkspace { bool m_bPersistent = false; // Inert: destroyed and invalid. If this is true, release the ptr you have. - bool inert(); + bool inert(); - void startAnim(bool in, bool left, bool instant = false); - void setActive(bool on); + void startAnim(bool in, bool left, bool instant = false); + void setActive(bool on); - void moveToMonitor(const int&); + void moveToMonitor(const int&); - PHLWINDOW getLastFocusedWindow(); - void rememberPrevWorkspace(const PHLWORKSPACE& prevWorkspace); + PHLWINDOW getLastFocusedWindow(); + void rememberPrevWorkspace(const PHLWORKSPACE& prevWorkspace); - std::string getConfigName(); + std::string getConfigName(); - bool matchesStaticSelector(const std::string& selector); + bool matchesStaticSelector(const std::string& selector); - void markInert(); + void markInert(); + + SWorkspaceIDName getPrevWorkspaceIDName(bool perMonitor) const; private: void init(PHLWORKSPACE self); diff --git a/src/events/Windows.cpp b/src/events/Windows.cpp index d37ba12b4e97..bb1197e59942 100644 --- a/src/events/Windows.cpp +++ b/src/events/Windows.cpp @@ -293,8 +293,7 @@ void Events::listener_mapWindow(void* owner, void* data) { if (WORKSPACEARGS[WORKSPACEARGS.size() - 1].starts_with("silent")) workspaceSilent = true; - std::string requestedWorkspaceName; - const int REQUESTEDWORKSPACEID = getWorkspaceIDFromString(WORKSPACEARGS.join(" ", 0, workspaceSilent ? WORKSPACEARGS.size() - 1 : 0), requestedWorkspaceName); + const auto& [REQUESTEDWORKSPACEID, requestedWorkspaceName] = getWorkspaceIDNameFromString(WORKSPACEARGS.join(" ", 0, workspaceSilent ? WORKSPACEARGS.size() - 1 : 0)); if (REQUESTEDWORKSPACEID != WORKSPACE_INVALID) { auto pWorkspace = g_pCompositor->getWorkspaceByID(REQUESTEDWORKSPACEID); diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 654ccf35e283..712e4e50eafa 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -214,37 +214,36 @@ bool isDirection(const char& arg) { return arg == 'l' || arg == 'r' || arg == 'u' || arg == 'd' || arg == 't' || arg == 'b'; } -int getWorkspaceIDFromString(const std::string& in, std::string& outName) { - int result = WORKSPACE_INVALID; +SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { + SWorkspaceIDName result = {WORKSPACE_INVALID, ""}; + if (in.starts_with("special")) { - outName = "special:special"; + result.name = "special:special"; if (in.length() > 8) { const auto NAME = in.substr(8); + const auto WS = g_pCompositor->getWorkspaceByName("special:" + NAME); - const auto WS = g_pCompositor->getWorkspaceByName("special:" + NAME); - - outName = "special:" + NAME; - - return WS ? WS->m_iID : g_pCompositor->getNewSpecialID(); + return {WS ? WS->m_iID : g_pCompositor->getNewSpecialID(), "special:" + NAME}; } - return SPECIAL_WORKSPACE_START; + result.id = SPECIAL_WORKSPACE_START; + return result; } else if (in.starts_with("name:")) { const auto WORKSPACENAME = in.substr(in.find_first_of(':') + 1); const auto WORKSPACE = g_pCompositor->getWorkspaceByName(WORKSPACENAME); if (!WORKSPACE) { - result = g_pCompositor->getNextAvailableNamedWorkspace(); + result.id = g_pCompositor->getNextAvailableNamedWorkspace(); } else { - result = WORKSPACE->m_iID; + result.id = WORKSPACE->m_iID; } - outName = WORKSPACENAME; + result.name = WORKSPACENAME; } else if (in.starts_with("empty")) { const bool same_mon = in.substr(5).contains("m"); const bool next = in.substr(5).contains("n"); if ((same_mon || next) && !g_pCompositor->m_pLastMonitor) { Debug::log(ERR, "Empty monitor workspace on monitor null!"); - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; } std::set invalidWSes; @@ -259,41 +258,42 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { int id = next ? g_pCompositor->m_pLastMonitor->activeWorkspaceID() : 0; while (++id < INT_MAX) { const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(id); - if (!invalidWSes.contains(id) && (!PWORKSPACE || g_pCompositor->getWindowsOnWorkspace(id) == 0)) - return id; + if (!invalidWSes.contains(id) && (!PWORKSPACE || g_pCompositor->getWindowsOnWorkspace(id) == 0)) { + result.id = id; + return result; + } } } else if (in.starts_with("prev")) { if (!g_pCompositor->m_pLastMonitor) - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; const auto PWORKSPACE = g_pCompositor->m_pLastMonitor->activeWorkspace; if (!valid(PWORKSPACE)) - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; - const auto PLASTWORKSPACE = g_pCompositor->getWorkspaceByID(PWORKSPACE->m_sPrevWorkspace.iID); + const auto PLASTWORKSPACE = g_pCompositor->getWorkspaceByID(PWORKSPACE->m_sPrevWorkspace.id); if (!PLASTWORKSPACE) - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; - outName = PLASTWORKSPACE->m_szName; - return PLASTWORKSPACE->m_iID; + return {PLASTWORKSPACE->m_iID, PLASTWORKSPACE->m_szName}; } else { if (in[0] == 'r' && (in[1] == '-' || in[1] == '+' || in[1] == '~') && isNumber(in.substr(2))) { bool absolute = in[1] == '~'; if (!g_pCompositor->m_pLastMonitor) { Debug::log(ERR, "Relative monitor workspace on monitor null!"); - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; } const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in.substr(absolute ? 2 : 1), 0); if (!PLUSMINUSRESULT.has_value()) - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; - result = (int)PLUSMINUSRESULT.value(); + result.id = (int)PLUSMINUSRESULT.value(); - int remains = (int)result; + int remains = (int)result.id; std::set invalidWSes; @@ -330,13 +330,13 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { // traverse valid workspaces until we reach the remains if ((size_t)remains < namedWSes.size()) { - result = namedWSes[remains]; + result.id = namedWSes[remains]; } else { remains -= namedWSes.size(); - result = 0; + result.id = 0; while (remains >= 0) { - result++; - if (!invalidWSes.contains(result)) { + result.id++; + if (!invalidWSes.contains(result.id)) { remains--; } } @@ -430,14 +430,14 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { finalWSID = curID; } } - result = finalWSID; + result.id = finalWSID; } - const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(result); + const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(result.id); if (PWORKSPACE) - outName = g_pCompositor->getWorkspaceByID(result)->m_szName; + result.name = g_pCompositor->getWorkspaceByID(result.id)->m_szName; else - outName = std::to_string(result); + result.name = std::to_string(result.id); } else if ((in[0] == 'm' || in[0] == 'e') && (in[1] == '-' || in[1] == '+' || in[1] == '~') && isNumber(in.substr(2))) { bool onAllMonitors = in[0] == 'e'; @@ -445,19 +445,19 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { if (!g_pCompositor->m_pLastMonitor) { Debug::log(ERR, "Relative monitor workspace on monitor null!"); - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; } // monitor relative const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in.substr(absolute ? 2 : 1), 0); if (!PLUSMINUSRESULT.has_value()) - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; - result = (int)PLUSMINUSRESULT.value(); + result.id = (int)PLUSMINUSRESULT.value(); // result now has +/- what we should move on mon - int remains = (int)result; + int remains = (int)result.id; std::vector validWSes; for (auto& ws : g_pCompositor->m_vWorkspaces) { @@ -505,30 +505,30 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { } } - result = validWSes[currentItem]; - outName = g_pCompositor->getWorkspaceByID(validWSes[currentItem])->m_szName; + result.id = validWSes[currentItem]; + result.name = g_pCompositor->getWorkspaceByID(validWSes[currentItem])->m_szName; } else { if (in[0] == '+' || in[0] == '-') { if (g_pCompositor->m_pLastMonitor) { const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in, g_pCompositor->m_pLastMonitor->activeWorkspaceID()); if (!PLUSMINUSRESULT.has_value()) - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; - result = std::max((int)PLUSMINUSRESULT.value(), 1); + result.id = std::max((int)PLUSMINUSRESULT.value(), 1); } else { Debug::log(ERR, "Relative workspace on no mon!"); - return WORKSPACE_INVALID; + return {WORKSPACE_INVALID}; } } else if (isNumber(in)) - result = std::max(std::stoi(in), 1); + result.id = std::max(std::stoi(in), 1); else { // maybe name const auto PWORKSPACE = g_pCompositor->getWorkspaceByName(in); if (PWORKSPACE) - result = PWORKSPACE->m_iID; + result.id = PWORKSPACE->m_iID; } - outName = std::to_string(result); + result.name = std::to_string(result.id); } } @@ -649,13 +649,9 @@ void matrixProjection(float mat[9], int w, int h, wl_output_transform tr) { int64_t getPPIDof(int64_t pid) { #if defined(KERN_PROC_PID) int mib[] = { - CTL_KERN, - KERN_PROC, - KERN_PROC_PID, - (int)pid, + CTL_KERN, KERN_PROC, KERN_PROC_PID, (int)pid, #if defined(__NetBSD__) || defined(__OpenBSD__) - sizeof(KINFO_PROC), - 1, + sizeof(KINFO_PROC), 1, #endif }; u_int miblen = sizeof(mib) / sizeof(mib[0]); diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index 111abba01946..33be796599d8 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -13,13 +13,18 @@ struct SCallstackFrameInfo { std::string desc; }; +struct SWorkspaceIDName { + int id = -1; + std::string name; +}; + 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); bool isDirection(const std::string&); bool isDirection(const char&); -int getWorkspaceIDFromString(const std::string&, std::string&); +SWorkspaceIDName getWorkspaceIDNameFromString(const std::string&); std::optional cleanCmdForWorkspace(const std::string&, std::string); float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2); void logSystemInfo(); @@ -42,4 +47,4 @@ template // because any suck format specifier will cause a compilation error // this is actually what std::format in stdlib does return std::vformat(fmt.get(), std::make_format_args(args...)); -} \ No newline at end of file +} diff --git a/src/helpers/Monitor.cpp b/src/helpers/Monitor.cpp index e99e16ef410d..6c2ec5cab880 100644 --- a/src/helpers/Monitor.cpp +++ b/src/helpers/Monitor.cpp @@ -419,20 +419,25 @@ int CMonitor::findAvailableDefaultWS() { void CMonitor::setupDefaultWS(const SMonitorRule& monitorRule) { // Workspace std::string newDefaultWorkspaceName = ""; - int64_t WORKSPACEID = g_pConfigManager->getDefaultWorkspaceFor(szName).empty() ? - findAvailableDefaultWS() : - getWorkspaceIDFromString(g_pConfigManager->getDefaultWorkspaceFor(szName), newDefaultWorkspaceName); + int64_t wsID = WORKSPACE_INVALID; + if (g_pConfigManager->getDefaultWorkspaceFor(szName).empty()) + wsID = findAvailableDefaultWS(); + else { + const auto ws = getWorkspaceIDNameFromString(g_pConfigManager->getDefaultWorkspaceFor(szName)); + wsID = ws.id; + newDefaultWorkspaceName = ws.name; + } - if (WORKSPACEID == WORKSPACE_INVALID || (WORKSPACEID >= SPECIAL_WORKSPACE_START && WORKSPACEID <= -2)) { - WORKSPACEID = g_pCompositor->m_vWorkspaces.size() + 1; - newDefaultWorkspaceName = std::to_string(WORKSPACEID); + if (wsID == WORKSPACE_INVALID || (wsID >= SPECIAL_WORKSPACE_START && wsID <= -2)) { + wsID = g_pCompositor->m_vWorkspaces.size() + 1; + newDefaultWorkspaceName = std::to_string(wsID); Debug::log(LOG, "Invalid workspace= directive name in monitor parsing, workspace name \"{}\" is invalid.", g_pConfigManager->getDefaultWorkspaceFor(szName)); } - auto PNEWWORKSPACE = g_pCompositor->getWorkspaceByID(WORKSPACEID); + auto PNEWWORKSPACE = g_pCompositor->getWorkspaceByID(wsID); - Debug::log(LOG, "New monitor: WORKSPACEID {}, exists: {}", WORKSPACEID, (int)(PNEWWORKSPACE != nullptr)); + Debug::log(LOG, "New monitor: WORKSPACEID {}, exists: {}", wsID, (int)(PNEWWORKSPACE != nullptr)); if (PNEWWORKSPACE) { // workspace exists, move it to the newly connected monitor @@ -442,9 +447,9 @@ void CMonitor::setupDefaultWS(const SMonitorRule& monitorRule) { PNEWWORKSPACE->startAnim(true, true, true); } else { if (newDefaultWorkspaceName == "") - newDefaultWorkspaceName = std::to_string(WORKSPACEID); + newDefaultWorkspaceName = std::to_string(wsID); - PNEWWORKSPACE = g_pCompositor->m_vWorkspaces.emplace_back(CWorkspace::create(WORKSPACEID, ID, newDefaultWorkspaceName)); + PNEWWORKSPACE = g_pCompositor->m_vWorkspaces.emplace_back(CWorkspace::create(wsID, ID, newDefaultWorkspaceName)); } activeWorkspace = PNEWWORKSPACE; diff --git a/src/macros.hpp b/src/macros.hpp index cacfdfeab2d0..953baf104543 100644 --- a/src/macros.hpp +++ b/src/macros.hpp @@ -25,6 +25,7 @@ #define STRVAL_EMPTY "[[EMPTY]]" #define WORKSPACE_INVALID -1L +constexpr int WORKSPACE_NOT_CHANGED = -42; #define LISTENER(name) \ void listener_##name(wl_listener*, void*); \ @@ -39,8 +40,7 @@ #define STICKS(a, b) abs((a) - (b)) < 2 -#define HYPRATOM(name) \ - { name, 0 } +#define HYPRATOM(name) {name, 0} #define RASSERT(expr, reason, ...) \ if (!(expr)) { \ diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 875ba239d6d8..f123e89ecccf 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1027,10 +1027,26 @@ void CKeybindManager::toggleActivePseudo(std::string args) { g_pLayoutManager->getCurrentLayout()->recalculateWindow(ACTIVEWINDOW); } -void CKeybindManager::changeworkspace(std::string args) { - int workspaceToChangeTo = 0; - std::string workspaceName = ""; +SWorkspaceIDName getWorkspaceToChangeFromArgs(std::string args, PHLWORKSPACE PCURRENTWORKSPACE) { + if (!args.starts_with("previous")) { + return getWorkspaceIDNameFromString(args); + } + + const SWorkspaceIDName PPREVWS = PCURRENTWORKSPACE->getPrevWorkspaceIDName(args.contains("_per_monitor")); + // Do nothing if there's no previous workspace, otherwise switch to it. + if (PPREVWS.id == -1) { + Debug::log(LOG, "No previous workspace to change to"); + return {WORKSPACE_NOT_CHANGED, ""}; + } + + const auto ID = PCURRENTWORKSPACE->m_iID; + if (const auto PWORKSPACETOCHANGETO = g_pCompositor->getWorkspaceByID(PPREVWS.id); PWORKSPACETOCHANGETO) + return {ID, PWORKSPACETOCHANGETO->m_szName}; + + return {ID, PPREVWS.name.empty() ? std::to_string(PPREVWS.id) : PPREVWS.name}; +} +void CKeybindManager::changeworkspace(std::string args) { // Workspace_back_and_forth being enabled means that an attempt to switch to // the current workspace will instead switch to the previous. static auto PBACKANDFORTH = CConfigValue("binds:workspace_back_and_forth"); @@ -1043,43 +1059,31 @@ void CKeybindManager::changeworkspace(std::string args) { return; const auto PCURRENTWORKSPACE = PMONITOR->activeWorkspace; - const bool EXPLICITPREVIOUS = args.starts_with("previous"); - - 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"); - return; - } else { - workspaceToChangeTo = PCURRENTWORKSPACE->m_iID; - - if (const auto PWORKSPACETOCHANGETO = g_pCompositor->getWorkspaceByID(PCURRENTWORKSPACE->m_sPrevWorkspace.iID); PWORKSPACETOCHANGETO) - workspaceName = PWORKSPACETOCHANGETO->m_szName; - else - workspaceName = - PCURRENTWORKSPACE->m_sPrevWorkspace.name.empty() ? std::to_string(PCURRENTWORKSPACE->m_sPrevWorkspace.iID) : PCURRENTWORKSPACE->m_sPrevWorkspace.name; - } - } else { - workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName); - } + const bool EXPLICITPREVIOUS = args.contains("previous"); + const auto& [workspaceToChangeTo, workspaceName] = getWorkspaceToChangeFromArgs(args, PCURRENTWORKSPACE); if (workspaceToChangeTo == WORKSPACE_INVALID) { Debug::log(ERR, "Error in changeworkspace, invalid value"); return; } - const bool BISWORKSPACECURRENT = workspaceToChangeTo == PCURRENTWORKSPACE->m_iID; + if (workspaceToChangeTo == WORKSPACE_NOT_CHANGED) { + return; + } + + const auto PREVWS = PCURRENTWORKSPACE->getPrevWorkspaceIDName(args.contains("_per_monitor")); - if (BISWORKSPACECURRENT && (!(*PBACKANDFORTH || EXPLICITPREVIOUS) || PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1)) + const bool BISWORKSPACECURRENT = workspaceToChangeTo == PCURRENTWORKSPACE->m_iID; + if (BISWORKSPACECURRENT && (!(*PBACKANDFORTH || EXPLICITPREVIOUS) || PREVWS.id == -1)) return; g_pInputManager->unconstrainMouse(); g_pInputManager->m_bEmptyFocusCursorSet = false; - auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo); + auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PREVWS.id : workspaceToChangeTo); if (!pWorkspaceToChangeTo) - pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo, PMONITOR->ID, - BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.name : workspaceName); + pWorkspaceToChangeTo = + g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PREVWS.id : workspaceToChangeTo, PMONITOR->ID, BISWORKSPACECURRENT ? PREVWS.name : workspaceName); if (!BISWORKSPACECURRENT && pWorkspaceToChangeTo->m_bIsSpecialWorkspace) { PMONITOR->setSpecialWorkspace(pWorkspaceToChangeTo); @@ -1162,10 +1166,7 @@ void CKeybindManager::moveActiveToWorkspace(std::string args) { if (!PWINDOW) return; - // hack - std::string workspaceName; - const auto WORKSPACEID = getWorkspaceIDFromString(args, workspaceName); - + const auto& [WORKSPACEID, workspaceName] = getWorkspaceIDNameFromString(args); if (WORKSPACEID == WORKSPACE_INVALID) { Debug::log(LOG, "Invalid workspace in moveActiveToWorkspace"); return; @@ -1226,10 +1227,7 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) { if (!PWINDOW) return; - std::string workspaceName = ""; - - const int WORKSPACEID = getWorkspaceIDFromString(args, workspaceName); - + const auto& [WORKSPACEID, workspaceName] = getWorkspaceIDNameFromString(args); if (WORKSPACEID == WORKSPACE_INVALID) { Debug::log(ERR, "Error in moveActiveToWorkspaceSilent, invalid value"); return; @@ -1695,8 +1693,7 @@ void CKeybindManager::moveWorkspaceToMonitor(std::string args) { return; } - std::string workspaceName; - const int WORKSPACEID = getWorkspaceIDFromString(workspace, workspaceName); + const int WORKSPACEID = getWorkspaceIDNameFromString(workspace).id; if (WORKSPACEID == WORKSPACE_INVALID) { Debug::log(ERR, "moveWorkspaceToMonitor invalid workspace!"); @@ -1714,9 +1711,7 @@ void CKeybindManager::moveWorkspaceToMonitor(std::string args) { } void CKeybindManager::focusWorkspaceOnCurrentMonitor(std::string args) { - std::string workspaceName; - int workspaceID = getWorkspaceIDFromString(args, workspaceName); - + int workspaceID = getWorkspaceIDNameFromString(args).id; if (workspaceID == WORKSPACE_INVALID) { Debug::log(ERR, "focusWorkspaceOnCurrentMonitor invalid workspace!"); return; @@ -1739,14 +1734,13 @@ void CKeybindManager::focusWorkspaceOnCurrentMonitor(std::string args) { } static auto PBACKANDFORTH = CConfigValue("binds:workspace_back_and_forth"); + const auto PREVWS = pWorkspace->getPrevWorkspaceIDName(false); - if (*PBACKANDFORTH && PCURRMONITOR->activeWorkspaceID() == workspaceID && pWorkspace->m_sPrevWorkspace.iID != -1) { - const int PREVWORKSPACEID = pWorkspace->m_sPrevWorkspace.iID; - const auto PREVWORKSPACENAME = pWorkspace->m_sPrevWorkspace.name; + if (*PBACKANDFORTH && PCURRMONITOR->activeWorkspaceID() == workspaceID && PREVWS.id != -1) { // Workspace to focus is previous workspace - pWorkspace = g_pCompositor->getWorkspaceByID(PREVWORKSPACEID); + pWorkspace = g_pCompositor->getWorkspaceByID(PREVWS.id); if (!pWorkspace) - pWorkspace = g_pCompositor->createNewWorkspace(PREVWORKSPACEID, PCURRMONITOR->ID, PREVWORKSPACENAME); + pWorkspace = g_pCompositor->createNewWorkspace(PREVWS.id, PCURRMONITOR->ID, PREVWS.name); workspaceID = pWorkspace->m_iID; } @@ -1769,9 +1763,7 @@ void CKeybindManager::focusWorkspaceOnCurrentMonitor(std::string args) { } void CKeybindManager::toggleSpecialWorkspace(std::string args) { - std::string workspaceName = ""; - int workspaceID = getWorkspaceIDFromString("special:" + args, workspaceName); - + const auto& [workspaceID, workspaceName] = getWorkspaceIDNameFromString("special:" + args); if (workspaceID == WORKSPACE_INVALID || !g_pCompositor->isWorkspaceSpecial(workspaceID)) { Debug::log(ERR, "Invalid workspace passed to special"); return; diff --git a/src/managers/input/Swipe.cpp b/src/managers/input/Swipe.cpp index ead7c5b8460e..775881cdf031 100644 --- a/src/managers/input/Swipe.cpp +++ b/src/managers/input/Swipe.cpp @@ -63,10 +63,9 @@ void CInputManager::endWorkspaceSwipe() { m_sActiveSwipe.pWorkspaceBegin->m_vRenderOffset.getConfig()->pValues->internalStyle.starts_with("slidefadevert"); // commit - std::string wsname = ""; - auto workspaceIDLeft = getWorkspaceIDFromString((*PSWIPEUSER ? "r-1" : "m-1"), wsname); - auto workspaceIDRight = getWorkspaceIDFromString((*PSWIPEUSER ? "r+1" : "m+1"), wsname); - const auto SWIPEDISTANCE = std::clamp(*PSWIPEDIST, (int64_t)1LL, (int64_t)UINT32_MAX); + auto workspaceIDLeft = getWorkspaceIDNameFromString((*PSWIPEUSER ? "r-1" : "m-1")).id; + auto workspaceIDRight = getWorkspaceIDNameFromString((*PSWIPEUSER ? "r+1" : "m+1")).id; + const auto SWIPEDISTANCE = std::clamp(*PSWIPEDIST, (int64_t)1LL, (int64_t)UINT32_MAX); // If we've been swiping off the right end with PSWIPENEW enabled, there is // no workspace there yet, and we need to choose an ID for a new one now. @@ -232,9 +231,8 @@ void CInputManager::updateWorkspaceSwipe(double delta) { m_sActiveSwipe.avgSpeed = (m_sActiveSwipe.avgSpeed * m_sActiveSwipe.speedPoints + abs(d)) / (m_sActiveSwipe.speedPoints + 1); m_sActiveSwipe.speedPoints++; - std::string wsname = ""; - auto workspaceIDLeft = getWorkspaceIDFromString((*PSWIPEUSER ? "r-1" : "m-1"), wsname); - auto workspaceIDRight = getWorkspaceIDFromString((*PSWIPEUSER ? "r+1" : "m+1"), wsname); + auto workspaceIDLeft = getWorkspaceIDNameFromString((*PSWIPEUSER ? "r-1" : "m-1")).id; + auto workspaceIDRight = getWorkspaceIDNameFromString((*PSWIPEUSER ? "r+1" : "m+1")).id; if ((workspaceIDLeft == WORKSPACE_INVALID || workspaceIDRight == WORKSPACE_INVALID || workspaceIDLeft == m_sActiveSwipe.pWorkspaceBegin->m_iID) && !*PSWIPENEW) { m_sActiveSwipe.pWorkspaceBegin = nullptr; // invalidate the swipe