From ac3edec14b1b31942fb8dff3ba01a9b70990ced3 Mon Sep 17 00:00:00 2001 From: Yavor Kolev Date: Tue, 2 May 2023 06:51:52 -0700 Subject: [PATCH] Add `activeworkspace` hyprctl command (#2202) * Add `activeworkspace` hyprctl command * fix format in hyprctl * Make stuff more shared in workspace hyprctl --------- Co-authored-by: vaxerski <43317083+vaxerski@users.noreply.github.com> --- hyprctl/main.cpp | 7 ++++- src/debug/HyprCtl.cpp | 63 +++++++++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 2f7ab2b9..31efcd16 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -23,6 +23,7 @@ const std::string USAGE = R"#(usage: hyprctl [(opt)flags] [command] [(opt)args] commands: monitors workspaces + activeworkspace clients activewindow layers @@ -50,7 +51,9 @@ flags: --batch -> execute a batch of commands, separated by ';' )#"; -void request(std::string arg, int minArgs = 0) { +#define PAD + +void request(std::string arg, int minArgs = 0) { const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); const auto ARGS = std::count(arg.begin(), arg.end(), ' '); @@ -327,6 +330,8 @@ int main(int argc, char** argv) { request(fullRequest); else if (fullRequest.contains("/workspaces")) request(fullRequest); + else if (fullRequest.contains("/activeworkspace")) + request(fullRequest); else if (fullRequest.contains("/activewindow")) request(fullRequest); else if (fullRequest.contains("/layers")) diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 2a92c1d6..32d544a0 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -188,17 +188,11 @@ std::string clientsRequest(HyprCtl::eHyprCtlOutputFormat format) { return result; } -std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) { - std::string result = ""; +static std::string getWorkspaceData(CWorkspace* w, HyprCtl::eHyprCtlOutputFormat format) { + const auto PLASTW = w->getLastFocusedWindow(); + const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID); if (format == HyprCtl::FORMAT_JSON) { - result += "["; - - for (auto& w : g_pCompositor->m_vWorkspaces) { - const auto PLASTW = w->getLastFocusedWindow(); - const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID); - - result += getFormat( - R"#({ + return getFormat(R"#({ "id": %i, "name": "%s", "monitor": "%s", @@ -206,24 +200,41 @@ std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) { "hasfullscreen": %s, "lastwindow": "0x%lx", "lastwindowtitle": "%s" -},)#", - w->m_iID, escapeJSONStrings(w->m_szName).c_str(), escapeJSONStrings(PMONITOR ? PMONITOR->szName : "?").c_str(), g_pCompositor->getWindowsOnWorkspace(w->m_iID), - ((int)w->m_bHasFullscreenWindow == 1 ? "true" : "false"), PLASTW, PLASTW ? escapeJSONStrings(PLASTW->m_szTitle).c_str() : ""); +})#", + w->m_iID, escapeJSONStrings(w->m_szName).c_str(), escapeJSONStrings(PMONITOR ? PMONITOR->szName : "?").c_str(), + g_pCompositor->getWindowsOnWorkspace(w->m_iID), ((int)w->m_bHasFullscreenWindow == 1 ? "true" : "false"), PLASTW, + PLASTW ? escapeJSONStrings(PLASTW->m_szTitle).c_str() : ""); + } else { + return getFormat("workspace ID %i (%s) on monitor %s:\n\twindows: %i\n\thasfullscreen: %i\n\tlastwindow: 0x%lx\n\tlastwindowtitle: %s\n\n", w->m_iID, w->m_szName.c_str(), + PMONITOR ? PMONITOR->szName.c_str() : "?", g_pCompositor->getWindowsOnWorkspace(w->m_iID), (int)w->m_bHasFullscreenWindow, PLASTW, + PLASTW ? PLASTW->m_szTitle.c_str() : ""); + } +} + +std::string activeWorkspaceRequest(HyprCtl::eHyprCtlOutputFormat format) { + std::string result = ""; + auto w = g_pCompositor->getWorkspaceByID(g_pCompositor->m_pLastMonitor->activeWorkspace); + return getWorkspaceData(w, format); +} + +std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) { + std::string result = ""; + + if (format == HyprCtl::FORMAT_JSON) { + result += "["; + for (auto& w : g_pCompositor->m_vWorkspaces) { + result += getWorkspaceData(w.get(), format); + result += ","; } - // remove trailing comma result.pop_back(); - result += "]"; } else { for (auto& w : g_pCompositor->m_vWorkspaces) { - const auto PLASTW = w->getLastFocusedWindow(); - const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID); - result += getFormat("workspace ID %i (%s) on monitor %s:\n\twindows: %i\n\thasfullscreen: %i\n\tlastwindow: 0x%lx\n\tlastwindowtitle: %s\n\n", w->m_iID, - w->m_szName.c_str(), PMONITOR ? PMONITOR->szName.c_str() : "?", g_pCompositor->getWindowsOnWorkspace(w->m_iID), (int)w->m_bHasFullscreenWindow, - PLASTW, PLASTW ? PLASTW->m_szTitle.c_str() : ""); + result += getWorkspaceData(w.get(), format); } } + return result; } @@ -804,10 +815,10 @@ std::string dispatchBatch(std::string request) { } std::string dispatchSetCursor(std::string request) { - CVarList vars(request, 0, ' '); + CVarList vars(request, 0, ' '); - const auto SIZESTR = vars[vars.size() - 1]; - std::string theme = ""; + const auto SIZESTR = vars[vars.size() - 1]; + std::string theme = ""; for (size_t i = 1; i < vars.size() - 1; ++i) theme += vars[i] + " "; theme.pop_back(); @@ -815,9 +826,7 @@ std::string dispatchSetCursor(std::string request) { int size = 0; try { size = std::stoi(SIZESTR); - } catch (...) { - return "size not int"; - } + } catch (...) { return "size not int"; } if (size <= 0) return "size not positive"; @@ -1215,6 +1224,8 @@ std::string getReply(std::string request) { return monitorsRequest(format); else if (request == "workspaces") return workspacesRequest(format); + else if (request == "activeworkspace") + return activeWorkspaceRequest(format); else if (request == "clients") return clientsRequest(format); else if (request == "kill")