From 036f431206b6c530497fb6e204c9765745c2c17d Mon Sep 17 00:00:00 2001 From: bazuin-32 Date: Tue, 12 Jul 2022 11:57:33 -0600 Subject: [PATCH 1/6] Add hyprctl output in json when `-j` flag is used --- hyprctl/main.cpp | 27 +-- src/debug/HyprCtl.cpp | 398 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 364 insertions(+), 61 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index b0aa1be8..291fc207 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -195,16 +195,23 @@ int main(int argc, char** argv) { return 1; } - if (!strcmp(argv[1], "monitors")) request("monitors"); - else if (!strcmp(argv[1], "clients")) request("clients"); - else if (!strcmp(argv[1], "workspaces")) request("workspaces"); - else if (!strcmp(argv[1], "activewindow")) request("activewindow"); - else if (!strcmp(argv[1], "layers")) request("layers"); - else if (!strcmp(argv[1], "version")) request("version"); - else if (!strcmp(argv[1], "kill")) request("kill"); - else if (!strcmp(argv[1], "splash")) request("splash"); - else if (!strcmp(argv[1], "devices")) request("devices"); - else if (!strcmp(argv[1], "reload")) request("reload"); + std::string args(argv[1]); + + if (argc > 2) { + args += " "; + args += argv[2]; + } + + if (!strcmp(argv[1], "monitors")) request(args); + else if (!strcmp(argv[1], "clients")) request(args); + else if (!strcmp(argv[1], "workspaces")) request(args); + else if (!strcmp(argv[1], "activewindow")) request(args); + else if (!strcmp(argv[1], "layers")) request(args); + else if (!strcmp(argv[1], "version")) request(args); + else if (!strcmp(argv[1], "kill")) request(args); + else if (!strcmp(argv[1], "splash")) request(args); + else if (!strcmp(argv[1], "devices")) request(args); + else if (!strcmp(argv[1], "reload")) request(args); else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv); else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv); else if (!strcmp(argv[1], "hyprpaper")) hyprpaperRequest(argc, argv); diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 3dffab12..40480bd3 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -13,95 +13,386 @@ #include -std::string monitorsRequest() { +std::string monitorsRequest(bool json) { std::string result = ""; - for (auto& m : g_pCompositor->m_vMonitors) { - result += getFormat("Monitor %s (ID %i):\n\t%ix%i@%f at %ix%i\n\tactive workspace: %i (%s)\n\treserved: %i %i %i %i\n\tscale: %.2f\n\ttransform: %i\n\tactive: %s\n\n", - m->szName.c_str(), m->ID, (int)m->vecPixelSize.x, (int)m->vecPixelSize.y, m->refreshRate, (int)m->vecPosition.x, (int)m->vecPosition.y, m->activeWorkspace, g_pCompositor->getWorkspaceByID(m->activeWorkspace)->m_szName.c_str(), (int)m->vecReservedTopLeft.x, (int)m->vecReservedTopLeft.y, (int)m->vecReservedBottomRight.x, (int)m->vecReservedBottomRight.y, m->scale, (int)m->transform, (m.get() == g_pCompositor->m_pLastMonitor ? "yes" : "no")); + if (json) { + result += "["; + + for (auto& m : g_pCompositor->m_vMonitors) { + result += getFormat( +R"#({ + "id": %i, + "name": "%s", + "width": %i, + "height": %i, + "refreshRate": %f, + "x": %i, + "y": %i, + "activeWorkspace": { + "id": %i, + "name": "%s" + }, + "reserved": [%i, %i, %i, %i], + "scale": %.2f, + "transform": %i, + "active": "%s" +},)#", + m->ID, + m->szName.c_str(), + (int)m->vecPixelSize.x, (int)m->vecPixelSize.y, + m->refreshRate, + (int)m->vecPosition.x, (int)m->vecPosition.y, + m->activeWorkspace, g_pCompositor->getWorkspaceByID(m->activeWorkspace)->m_szName.c_str(), + (int)m->vecReservedTopLeft.x, (int)m->vecReservedTopLeft.y, (int)m->vecReservedBottomRight.x, (int)m->vecReservedBottomRight.y, + m->scale, + (int)m->transform, + (m.get() == g_pCompositor->m_pLastMonitor ? "yes" : "no") + ); + } + + // remove trailing comma + result.pop_back(); + + result += "]"; + } else { + for (auto& m : g_pCompositor->m_vMonitors) { + result += getFormat("Monitor %s (ID %i):\n\t%ix%i@%f at %ix%i\n\tactive workspace: %i (%s)\n\treserved: %i %i %i %i\n\tscale: %.2f\n\ttransform: %i\n\tactive: %s\n\n", + m->szName.c_str(), m->ID, (int)m->vecPixelSize.x, (int)m->vecPixelSize.y, m->refreshRate, (int)m->vecPosition.x, (int)m->vecPosition.y, m->activeWorkspace, g_pCompositor->getWorkspaceByID(m->activeWorkspace)->m_szName.c_str(), (int)m->vecReservedTopLeft.x, (int)m->vecReservedTopLeft.y, (int)m->vecReservedBottomRight.x, (int)m->vecReservedBottomRight.y, m->scale, (int)m->transform, (m.get() == g_pCompositor->m_pLastMonitor ? "yes" : "no")); + } } return result; } -std::string clientsRequest() { +std::string clientsRequest(bool json) { std::string result = ""; - for (auto& w : g_pCompositor->m_vWindows) { - if (w->m_bIsMapped) { - result += getFormat("Window %x -> %s:\n\tat: %i,%i\n\tsize: %i,%i\n\tworkspace: %i (%s)\n\tfloating: %i\n\tmonitor: %i\n\tclass: %s\n\ttitle: %s\n\tpid: %i\n\n", - &w, w->m_szTitle.c_str(), (int)w->m_vRealPosition.vec().x, (int)w->m_vRealPosition.vec().y, (int)w->m_vRealSize.vec().x, (int)w->m_vRealSize.vec().y, w->m_iWorkspaceID, (w->m_iWorkspaceID == -1 ? "" : g_pCompositor->getWorkspaceByID(w->m_iWorkspaceID) ? g_pCompositor->getWorkspaceByID(w->m_iWorkspaceID)->m_szName.c_str() : std::string("Invalid workspace " + std::to_string(w->m_iWorkspaceID)).c_str()), (int)w->m_bIsFloating, w->m_iMonitorID, g_pXWaylandManager->getAppIDClass(w.get()).c_str(), g_pXWaylandManager->getTitle(w.get()).c_str(), w->getPID()); - + if (json) { + result += "["; + + for (auto& w : g_pCompositor->m_vWindows) { + if (w->m_bIsMapped) { + result += getFormat( +R"#({ + "address": "0x%x", + "at": [%i, %i], + "size": [%i, %i], + "workspace": { + "id": %i, + "name": "%s" + }, + "floating": %i, + "monitor": %i, + "class": "%s", + "title": "%s", + "pid": %i +},)#", + &w, + (int)w->m_vRealPosition.vec().x, (int)w->m_vRealPosition.vec().y, + (int)w->m_vRealSize.vec().x, (int)w->m_vRealSize.vec().y, + w->m_iWorkspaceID, (w->m_iWorkspaceID == -1 ? "" : g_pCompositor->getWorkspaceByID(w->m_iWorkspaceID) ? g_pCompositor->getWorkspaceByID(w->m_iWorkspaceID)->m_szName.c_str() : std::string("Invalid workspace " + std::to_string(w->m_iWorkspaceID)).c_str()), + (int)w->m_bIsFloating, + w->m_iMonitorID, + g_pXWaylandManager->getAppIDClass(w.get()).c_str(), + g_pXWaylandManager->getTitle(w.get()).c_str(), + w->getPID() + ); + } + } + + // remove trailing comma + result.pop_back(); + + result += "]"; + } else { + for (auto& w : g_pCompositor->m_vWindows) { + if (w->m_bIsMapped) { + result += getFormat("Window %x -> %s:\n\tat: %i,%i\n\tsize: %i,%i\n\tworkspace: %i (%s)\n\tfloating: %i\n\tmonitor: %i\n\tclass: %s\n\ttitle: %s\n\tpid: %i\n\n", + &w, w->m_szTitle.c_str(), (int)w->m_vRealPosition.vec().x, (int)w->m_vRealPosition.vec().y, (int)w->m_vRealSize.vec().x, (int)w->m_vRealSize.vec().y, w->m_iWorkspaceID, (w->m_iWorkspaceID == -1 ? "" : g_pCompositor->getWorkspaceByID(w->m_iWorkspaceID) ? g_pCompositor->getWorkspaceByID(w->m_iWorkspaceID)->m_szName.c_str() : std::string("Invalid workspace " + std::to_string(w->m_iWorkspaceID)).c_str()), (int)w->m_bIsFloating, w->m_iMonitorID, g_pXWaylandManager->getAppIDClass(w.get()).c_str(), g_pXWaylandManager->getTitle(w.get()).c_str(), w->getPID()); + + } } } return result; } -std::string workspacesRequest() { +std::string workspacesRequest(bool json) { std::string result = ""; - for (auto& w : g_pCompositor->m_vWorkspaces) { - result += getFormat("workspace ID %i (%s) on monitor %s:\n\twindows: %i\n\thasfullscreen: %i\n\n", - w->m_iID, w->m_szName.c_str(), g_pCompositor->getMonitorFromID(w->m_iMonitorID)->szName.c_str(), g_pCompositor->getWindowsOnWorkspace(w->m_iID), (int)w->m_bHasFullscreenWindow); + if (json) { + result += "["; + + for (auto& w : g_pCompositor->m_vWorkspaces) { + result += getFormat( +R"#({ + "id": %i, + "name": "%s", + "monitor": "%s", + "windows": %i, + "hasfullscreen": %i +},)#", + w->m_iID, + w->m_szName.c_str(), + g_pCompositor->getMonitorFromID(w->m_iMonitorID)->szName.c_str(), + g_pCompositor->getWindowsOnWorkspace(w->m_iID), + (int)w->m_bHasFullscreenWindow + ); + } + + // remove trailing comma + result.pop_back(); + + result += "]"; + } else { + for (auto& w : g_pCompositor->m_vWorkspaces) { + result += getFormat("workspace ID %i (%s) on monitor %s:\n\twindows: %i\n\thasfullscreen: %i\n\n", + w->m_iID, w->m_szName.c_str(), g_pCompositor->getMonitorFromID(w->m_iMonitorID)->szName.c_str(), g_pCompositor->getWindowsOnWorkspace(w->m_iID), (int)w->m_bHasFullscreenWindow); + } } return result; } -std::string activeWindowRequest() { +std::string activeWindowRequest(bool json) { const auto PWINDOW = g_pCompositor->m_pLastWindow; if (!g_pCompositor->windowValidMapped(PWINDOW)) return "Invalid"; - return getFormat("Window %x -> %s:\n\tat: %i,%i\n\tsize: %i,%i\n\tworkspace: %i (%s)\n\tfloating: %i\n\tmonitor: %i\n\tclass: %s\n\ttitle: %s\n\tpid: %i\n\n", - PWINDOW, PWINDOW->m_szTitle.c_str(), (int)PWINDOW->m_vRealPosition.vec().x, (int)PWINDOW->m_vRealPosition.vec().y, (int)PWINDOW->m_vRealSize.vec().x, (int)PWINDOW->m_vRealSize.vec().y, PWINDOW->m_iWorkspaceID, (PWINDOW->m_iWorkspaceID == -1 ? "" : g_pCompositor->getWorkspaceByID(PWINDOW->m_iWorkspaceID)->m_szName.c_str()), (int)PWINDOW->m_bIsFloating, (int)PWINDOW->m_iMonitorID, g_pXWaylandManager->getAppIDClass(PWINDOW).c_str(), g_pXWaylandManager->getTitle(PWINDOW).c_str(), PWINDOW->getPID()); + if (json) { + return getFormat( +R"#({ + "address": "0x%x", + "at": [%i, %i], + "size": [%i, %i], + "workspace": { + "id": %i, + "name": "%s" + }, + "floating": %i, + "monitor": %i, + "class": "%s", + "title": "%s", + "pid": %i +})#", + PWINDOW, + (int)PWINDOW->m_vRealPosition.vec().x, (int)PWINDOW->m_vRealPosition.vec().y, + (int)PWINDOW->m_vRealSize.vec().x, (int)PWINDOW->m_vRealSize.vec().y, + PWINDOW->m_iWorkspaceID, (PWINDOW->m_iWorkspaceID == -1 ? "" : g_pCompositor->getWorkspaceByID(PWINDOW->m_iWorkspaceID)->m_szName.c_str()), + (int)PWINDOW->m_bIsFloating, + PWINDOW->m_iMonitorID, + g_pXWaylandManager->getAppIDClass(PWINDOW).c_str(), + g_pXWaylandManager->getTitle(PWINDOW).c_str(), + PWINDOW->getPID() + ); + } else { + return getFormat("Window %x -> %s:\n\tat: %i,%i\n\tsize: %i,%i\n\tworkspace: %i (%s)\n\tfloating: %i\n\tmonitor: %i\n\tclass: %s\n\ttitle: %s\n\tpid: %i\n\n", + PWINDOW, PWINDOW->m_szTitle.c_str(), (int)PWINDOW->m_vRealPosition.vec().x, (int)PWINDOW->m_vRealPosition.vec().y, (int)PWINDOW->m_vRealSize.vec().x, (int)PWINDOW->m_vRealSize.vec().y, PWINDOW->m_iWorkspaceID, (PWINDOW->m_iWorkspaceID == -1 ? "" : g_pCompositor->getWorkspaceByID(PWINDOW->m_iWorkspaceID)->m_szName.c_str()), (int)PWINDOW->m_bIsFloating, (int)PWINDOW->m_iMonitorID, g_pXWaylandManager->getAppIDClass(PWINDOW).c_str(), g_pXWaylandManager->getTitle(PWINDOW).c_str(), PWINDOW->getPID()); + } } -std::string layersRequest() { +std::string layersRequest(bool json) { std::string result = ""; - for (auto& mon : g_pCompositor->m_vMonitors) { - result += getFormat("Monitor %s:\n", mon->szName.c_str()); - int layerLevel = 0; - for (auto& level : mon->m_aLayerSurfaceLists) { - result += getFormat("\tLayer level %i:\n", layerLevel); + if (json) { + result += "{\n"; - for (auto& layer : level) { - result += getFormat("\t\tLayer %x: xywh: %i %i %i %i, namespace: %s\n", layer, layer->geometry.x, layer->geometry.y, layer->geometry.width, layer->geometry.height, layer->szNamespace.c_str()); + for (auto& mon : g_pCompositor->m_vMonitors) { + result += getFormat( +R"#("%s": { + "levels": { +)#", + mon->szName.c_str() + ); + + int layerLevel = 0; + for (auto& level : mon->m_aLayerSurfaceLists) { + result += getFormat( +R"#( + "%i": [ +)#", + layerLevel + ); + for (auto& layer : level) { + result += getFormat( +R"#( { + "address": "0x%x", + "x": %i, + "y": %i, + "w": %i, + "h": %i, + "namespace": "%s" + },)#", + layer, + layer->geometry.x, + layer->geometry.y, + layer->geometry.width, + layer->geometry.height, + layer->szNamespace.c_str() + ); + } + + // remove trailing comma + result.pop_back(); + + if (level.size() > 0) + result += "\n "; + + result += "],"; + + layerLevel++; } - layerLevel++; + // remove trailing comma + result.pop_back(); + + result += "\n }\n},"; + } + + // remove trailing comma + result.pop_back(); + + result += "\n}\n"; + + } else { + for (auto& mon : g_pCompositor->m_vMonitors) { + result += getFormat("Monitor %s:\n", mon->szName.c_str()); + int layerLevel = 0; + for (auto& level : mon->m_aLayerSurfaceLists) { + result += getFormat("\tLayer level %i:\n", layerLevel); + + for (auto& layer : level) { + result += getFormat("\t\tLayer %x: xywh: %i %i %i %i, namespace: %s\n", layer, layer->geometry.x, layer->geometry.y, layer->geometry.width, layer->geometry.height, layer->szNamespace.c_str()); + } + + layerLevel++; + } + result += "\n\n"; } - result += "\n\n"; } return result; } -std::string devicesRequest() { +std::string devicesRequest(bool json) { std::string result = ""; - result += "mice:\n"; + if (json) { + result += "{\n"; + result += "\"mice\": [\n"; - for (auto& m : g_pInputManager->m_lMice) { - result += getFormat("\tMouse at %x:\n\t\t%s\n", &m, m.mouse->name); - } + for (auto& m : g_pInputManager->m_lMice) { + result += getFormat( +R"#( { + "address": "0x%x", + "name": "%s" + },)#", + &m, + m.mouse->name + ); + } - result += "\n\nKeyboards:\n"; + // remove trailing comma + result.pop_back(); + result += "\n],\n"; - for (auto& k : g_pInputManager->m_lKeyboards) { - result += getFormat("\tKeyboard at %x:\n\t\t%s\n\t\t\trules: r \"%s\", m \"%s\", l \"%s\", v \"%s\", o \"%s\"\n", &k, k.keyboard->name, k.currentRules.rules.c_str(), k.currentRules.model.c_str(), k.currentRules.layout.c_str(), k.currentRules.variant.c_str(), k.currentRules.options.c_str()); - } + result += "\"keyboards\": [\n"; + for (auto& k : g_pInputManager->m_lKeyboards) { + result += getFormat( +R"#( { + "address": "0x%x", + "name": "%s", + "rules": "%s", + "model": "%s", + "layout": "%s", + "variant": "%s", + "options": "%s" + },)#", + &k, + k.keyboard->name, + k.currentRules.rules.c_str(), + k.currentRules.model.c_str(), + k.currentRules.layout.c_str(), + k.currentRules.variant.c_str(), + k.currentRules.options.c_str() + ); + } - result += "\n\nTablets:\n"; + // remove trailing comma + result.pop_back(); + result += "\n],\n"; - for (auto& d : g_pInputManager->m_lTabletPads) { - result += getFormat("\tTablet Pad at %x (belongs to %x -> %s)\n", &d, d.pTabletParent, d.pTabletParent ? d.pTabletParent->wlrDevice ? d.pTabletParent->wlrDevice->name : "" : ""); - } + result += "\"tablets\": [\n"; - for (auto& d : g_pInputManager->m_lTablets) { - result += getFormat("\tTablet at %x:\n\t\t%s\n", &d, d.wlrDevice ? d.wlrDevice->name : ""); - } + for (auto& d : g_pInputManager->m_lTabletPads) { + result += getFormat( +R"#( { + "address": "0x%x", + "type": "tabletPad", + "belongsTo": { + "address": "0x%x", + "name": "%s" + } + },)#", + &d, + d.pTabletParent, + d.pTabletParent ? d.pTabletParent->wlrDevice ? d.pTabletParent->wlrDevice->name : "" : "" + ); + } - for (auto& d : g_pInputManager->m_lTabletTools) { - result += getFormat("\tTablet Tool at %x (belongs to %x)\n", &d, d.wlrTabletTool ? d.wlrTabletTool->data : 0); + for (auto& d : g_pInputManager->m_lTablets) { + result += getFormat( +R"#( { + "address": "0x%x", + "name": "%s" + },)#", + &d, + d.wlrDevice ? d.wlrDevice->name : "" + ); + } + + for (auto& d : g_pInputManager->m_lTabletTools) { + result += getFormat( +R"#( { + "address": "0x%x", + "type": "tabletTool", + "belongsTo": "0x%x" + },)#", + &d, + d.wlrTabletTool ? d.wlrTabletTool->data : 0 + ); + } + + // remove trailing comma + if (result[result.size() - 1] == ',') + result.pop_back(); + result += "\n]\n"; + + result += "}\n"; + + } else { + result += "mice:\n"; + + for (auto& m : g_pInputManager->m_lMice) { + result += getFormat("\tMouse at %x:\n\t\t%s\n", &m, m.mouse->name); + } + + result += "\n\nKeyboards:\n"; + + for (auto& k : g_pInputManager->m_lKeyboards) { + result += getFormat("\tKeyboard at %x:\n\t\t%s\n\t\t\trules: r \"%s\", m \"%s\", l \"%s\", v \"%s\", o \"%s\"\n", &k, k.keyboard->name, k.currentRules.rules.c_str(), k.currentRules.model.c_str(), k.currentRules.layout.c_str(), k.currentRules.variant.c_str(), k.currentRules.options.c_str()); + } + + result += "\n\nTablets:\n"; + + for (auto& d : g_pInputManager->m_lTabletPads) { + result += getFormat("\tTablet Pad at %x (belongs to %x -> %s)\n", &d, d.pTabletParent, d.pTabletParent ? d.pTabletParent->wlrDevice ? d.pTabletParent->wlrDevice->name : "" : ""); + } + + for (auto& d : g_pInputManager->m_lTablets) { + result += getFormat("\tTablet at %x:\n\t\t%s\n", &d, d.wlrDevice ? d.wlrDevice->name : ""); + } + + for (auto& d : g_pInputManager->m_lTabletTools) { + result += getFormat("\tTablet Tool at %x (belongs to %x)\n", &d, d.wlrTabletTool ? d.wlrTabletTool->data : 0); + } } return result; @@ -220,24 +511,29 @@ std::string dispatchBatch(std::string request) { } std::string getReply(std::string request) { + std::size_t jpos = request.find(" -j"); + bool json = jpos != std::string::npos; + if (json) + request.erase(jpos, 3); // remove the `-j` flag so `==` still works + if (request == "monitors") - return monitorsRequest(); + return monitorsRequest(json); else if (request == "workspaces") - return workspacesRequest(); + return workspacesRequest(json); else if (request == "clients") - return clientsRequest(); + return clientsRequest(json); else if (request == "kill") return killRequest(); else if (request == "activewindow") - return activeWindowRequest(); + return activeWindowRequest(json); else if (request == "layers") - return layersRequest(); + return layersRequest(json); else if (request == "version") return versionRequest(); else if (request == "reload") return reloadRequest(); else if (request == "devices") - return devicesRequest(); + return devicesRequest(json); else if (request == "splash") return splashRequest(); else if (request.find("dispatch") == 0) From 2402f2e36498686dfebe7c06f1203f595b888252 Mon Sep 17 00:00:00 2001 From: bazuin-32 Date: Tue, 12 Jul 2022 14:59:36 -0600 Subject: [PATCH 2/6] Use enum for specifying hyprctl output format and change the way flags are passed for json output --- hyprctl/Makefile | 4 ++-- hyprctl/main.cpp | 39 +++++++++++++++--------------- src/debug/HyprCtl.cpp | 56 ++++++++++++++++++++++++++----------------- src/debug/HyprCtl.hpp | 5 ++++ 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/hyprctl/Makefile b/hyprctl/Makefile index 0e997886..cd1df201 100644 --- a/hyprctl/Makefile +++ b/hyprctl/Makefile @@ -1,4 +1,4 @@ all: - g++ -std=c++20 ./main.cpp -o ./hyprctl + g++ -std=c++23 ./main.cpp -o ./hyprctl clean: - rm ./hyprctl \ No newline at end of file + rm ./hyprctl diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 291fc207..5ab170e6 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -195,28 +195,29 @@ int main(int argc, char** argv) { return 1; } - std::string args(argv[1]); + std::string ourRequest = argv[1]; - if (argc > 2) { - args += " "; - args += argv[2]; + if (!ourRequest.contains("/")) { + ourRequest = "/" + ourRequest; } - if (!strcmp(argv[1], "monitors")) request(args); - else if (!strcmp(argv[1], "clients")) request(args); - else if (!strcmp(argv[1], "workspaces")) request(args); - else if (!strcmp(argv[1], "activewindow")) request(args); - else if (!strcmp(argv[1], "layers")) request(args); - else if (!strcmp(argv[1], "version")) request(args); - else if (!strcmp(argv[1], "kill")) request(args); - else if (!strcmp(argv[1], "splash")) request(args); - else if (!strcmp(argv[1], "devices")) request(args); - else if (!strcmp(argv[1], "reload")) request(args); - else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv); - else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv); - else if (!strcmp(argv[1], "hyprpaper")) hyprpaperRequest(argc, argv); - else if (!strcmp(argv[1], "--batch")) batchRequest(argc, argv); - else if (!strcmp(argv[1], "--help")) printf("%s", USAGE.c_str()); + ourRequest.contains("/"); + + if (ourRequest.contains("/monitors")) request(ourRequest); + else if (ourRequest.contains("/clients")) request(ourRequest); + else if (ourRequest.contains("/workspaces")) request(ourRequest); + else if (ourRequest.contains("/activewindow")) request(ourRequest); + else if (ourRequest.contains("/layers")) request(ourRequest); + else if (ourRequest.contains("/version")) request(ourRequest); + else if (ourRequest.contains("/kill")) request(ourRequest); + else if (ourRequest.contains("/splash")) request(ourRequest); + else if (ourRequest.contains("/devices")) request(ourRequest); + else if (ourRequest.contains("/reload")) request(ourRequest); + else if (ourRequest.contains("/dispatch")) dispatchRequest(argc, argv); + else if (ourRequest.contains("/keyword")) keywordRequest(argc, argv); + else if (ourRequest.contains("/hyprpaper")) hyprpaperRequest(argc, argv); + else if (ourRequest.contains("/--batch")) batchRequest(argc, argv); + else if (ourRequest.contains("/--help")) printf("%s", USAGE.c_str()); else { printf("%s\n", USAGE.c_str()); return 1; diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 40480bd3..76fe3c56 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -13,9 +13,9 @@ #include -std::string monitorsRequest(bool json) { +std::string monitorsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string result = ""; - if (json) { + if (format == HyprCtl::FORMAT_JSON) { result += "["; for (auto& m : g_pCompositor->m_vMonitors) { @@ -64,9 +64,9 @@ R"#({ return result; } -std::string clientsRequest(bool json) { +std::string clientsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string result = ""; - if (json) { + if (format == HyprCtl::FORMAT_JSON) { result += "["; for (auto& w : g_pCompositor->m_vWindows) { @@ -115,9 +115,9 @@ R"#({ return result; } -std::string workspacesRequest(bool json) { +std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string result = ""; - if (json) { + if (format == HyprCtl::FORMAT_JSON) { result += "["; for (auto& w : g_pCompositor->m_vWorkspaces) { @@ -150,13 +150,13 @@ R"#({ return result; } -std::string activeWindowRequest(bool json) { +std::string activeWindowRequest(HyprCtl::eHyprCtlOutputFormat format) { const auto PWINDOW = g_pCompositor->m_pLastWindow; if (!g_pCompositor->windowValidMapped(PWINDOW)) return "Invalid"; - if (json) { + if (format == HyprCtl::FORMAT_JSON) { return getFormat( R"#({ "address": "0x%x", @@ -188,10 +188,10 @@ R"#({ } } -std::string layersRequest(bool json) { +std::string layersRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string result = ""; - if (json) { + if (format == HyprCtl::FORMAT_JSON) { result += "{\n"; for (auto& mon : g_pCompositor->m_vMonitors) { @@ -271,10 +271,10 @@ R"#( { return result; } -std::string devicesRequest(bool json) { +std::string devicesRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string result = ""; - if (json) { + if (format == HyprCtl::FORMAT_JSON) { result += "{\n"; result += "\"mice\": [\n"; @@ -511,29 +511,41 @@ std::string dispatchBatch(std::string request) { } std::string getReply(std::string request) { - std::size_t jpos = request.find(" -j"); - bool json = jpos != std::string::npos; - if (json) - request.erase(jpos, 3); // remove the `-j` flag so `==` still works + auto format = HyprCtl::FORMAT_NORMAL; + + // process flags + int sepIndex = 0; + for (const auto& c : request) { + if (c == '/') { // stop at separator + break; + } + + sepIndex++; + + if (c == 'j') + format = HyprCtl::FORMAT_JSON; + } + + request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string if (request == "monitors") - return monitorsRequest(json); + return monitorsRequest(format); else if (request == "workspaces") - return workspacesRequest(json); + return workspacesRequest(format); else if (request == "clients") - return clientsRequest(json); + return clientsRequest(format); else if (request == "kill") return killRequest(); else if (request == "activewindow") - return activeWindowRequest(json); + return activeWindowRequest(format); else if (request == "layers") - return layersRequest(json); + return layersRequest(format); else if (request == "version") return versionRequest(); else if (request == "reload") return reloadRequest(); else if (request == "devices") - return devicesRequest(json); + return devicesRequest(format); else if (request == "splash") return splashRequest(); else if (request.find("dispatch") == 0) diff --git a/src/debug/HyprCtl.hpp b/src/debug/HyprCtl.hpp index 3da4aa07..60559e9f 100644 --- a/src/debug/HyprCtl.hpp +++ b/src/debug/HyprCtl.hpp @@ -14,4 +14,9 @@ namespace HyprCtl { inline std::string request = ""; inline std::ifstream requestStream; + + enum eHyprCtlOutputFormat { + FORMAT_NORMAL = 0, + FORMAT_JSON + }; }; \ No newline at end of file From bca93f4d3cd29c88ff32af96dcc1338196feabdb Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Tue, 12 Jul 2022 23:17:55 +0200 Subject: [PATCH 3/6] code styling --- hyprctl/main.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 5ab170e6..4c7e3a04 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -195,29 +195,27 @@ int main(int argc, char** argv) { return 1; } - std::string ourRequest = argv[1]; + std::string fullRequest = argv[1]; - if (!ourRequest.contains("/")) { - ourRequest = "/" + ourRequest; + if (!fullRequest.contains("/")) { + fullRequest = "/" + fullRequest; } - ourRequest.contains("/"); - - if (ourRequest.contains("/monitors")) request(ourRequest); - else if (ourRequest.contains("/clients")) request(ourRequest); - else if (ourRequest.contains("/workspaces")) request(ourRequest); - else if (ourRequest.contains("/activewindow")) request(ourRequest); - else if (ourRequest.contains("/layers")) request(ourRequest); - else if (ourRequest.contains("/version")) request(ourRequest); - else if (ourRequest.contains("/kill")) request(ourRequest); - else if (ourRequest.contains("/splash")) request(ourRequest); - else if (ourRequest.contains("/devices")) request(ourRequest); - else if (ourRequest.contains("/reload")) request(ourRequest); - else if (ourRequest.contains("/dispatch")) dispatchRequest(argc, argv); - else if (ourRequest.contains("/keyword")) keywordRequest(argc, argv); - else if (ourRequest.contains("/hyprpaper")) hyprpaperRequest(argc, argv); - else if (ourRequest.contains("/--batch")) batchRequest(argc, argv); - else if (ourRequest.contains("/--help")) printf("%s", USAGE.c_str()); + if (fullRequest.contains("/monitors")) request(fullRequest); + else if (fullRequest.contains("/clients")) request(fullRequest); + else if (fullRequest.contains("/workspaces")) request(fullRequest); + else if (fullRequest.contains("/activewindow")) request(fullRequest); + else if (fullRequest.contains("/layers")) request(fullRequest); + else if (fullRequest.contains("/version")) request(fullRequest); + else if (fullRequest.contains("/kill")) request(fullRequest); + else if (fullRequest.contains("/splash")) request(fullRequest); + else if (fullRequest.contains("/devices")) request(fullRequest); + else if (fullRequest.contains("/reload")) request(fullRequest); + else if (fullRequest.contains("/dispatch")) dispatchRequest(argc, argv); + else if (fullRequest.contains("/keyword")) keywordRequest(argc, argv); + else if (fullRequest.contains("/hyprpaper")) hyprpaperRequest(argc, argv); + else if (fullRequest.contains("/--batch")) batchRequest(argc, argv); + else if (fullRequest.contains("/--help")) printf("%s", USAGE.c_str()); else { printf("%s\n", USAGE.c_str()); return 1; From 4bea3467e03eae7c3db251b84605345c576c4313 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Tue, 12 Jul 2022 23:21:00 +0200 Subject: [PATCH 4/6] update hyprctl help --- hyprctl/main.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 4c7e3a04..b2c00ce7 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -15,8 +15,9 @@ #include #include -const std::string USAGE = R"#(usage: hyprctl [command] [(opt)args] +const std::string USAGE = R"#(usage: hyprctl [(opt)flag] [command] [(opt)args] +commands: monitors workspaces clients @@ -29,7 +30,11 @@ const std::string USAGE = R"#(usage: hyprctl [command] [(opt)args] kill splash hyprpaper - reload)#"; + reload + +flags: + -j -> output in JSON +)#"; void request(std::string arg) { const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); From ac9ff795cdfd25c37b9b9be44f3d034bfa3284d3 Mon Sep 17 00:00:00 2001 From: bazuin-32 Date: Tue, 12 Jul 2022 15:38:50 -0600 Subject: [PATCH 5/6] Fix calls to `substr` with invalid `pos` --- src/debug/HyprCtl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 76fe3c56..684bca9c 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -526,7 +526,8 @@ std::string getReply(std::string request) { format = HyprCtl::FORMAT_JSON; } - request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string + if (sepIndex < request.size()) + request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string if (request == "monitors") return monitorsRequest(format); From ab859ec9fcd749f3f898684710085ff8dc1303d4 Mon Sep 17 00:00:00 2001 From: bazuin-32 Date: Tue, 12 Jul 2022 16:34:28 -0600 Subject: [PATCH 6/6] Fix issues with batch requests --- hyprctl/main.cpp | 16 ++++++++++------ src/debug/HyprCtl.cpp | 26 ++++++++++++++------------ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index b2c00ce7..0f30e588 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -186,8 +186,8 @@ void hyprpaperRequest(int argc, char** argv) { requestHyprpaper(rq); } -void batchRequest(int argc, char** argv) { - std::string rq = "[[BATCH]]" + std::string(argv[2]); +void batchRequest(std::string arg) { + std::string rq = "[[BATCH]]" + arg.substr(arg.find_first_of(" ") + 1); request(rq); } @@ -200,13 +200,18 @@ int main(int argc, char** argv) { return 1; } - std::string fullRequest = argv[1]; + std::string fullRequest = ""; + for (int i = 1; i < argc; i++) { + fullRequest += std::string(argv[i]) + " "; + } + fullRequest.pop_back(); // remove trailing space - if (!fullRequest.contains("/")) { + if (!std::string(argv[1]).contains("/")) { fullRequest = "/" + fullRequest; } - if (fullRequest.contains("/monitors")) request(fullRequest); + if (fullRequest.contains("/--batch")) batchRequest(fullRequest); + else if (fullRequest.contains("/monitors")) request(fullRequest); else if (fullRequest.contains("/clients")) request(fullRequest); else if (fullRequest.contains("/workspaces")) request(fullRequest); else if (fullRequest.contains("/activewindow")) request(fullRequest); @@ -219,7 +224,6 @@ int main(int argc, char** argv) { else if (fullRequest.contains("/dispatch")) dispatchRequest(argc, argv); else if (fullRequest.contains("/keyword")) keywordRequest(argc, argv); else if (fullRequest.contains("/hyprpaper")) hyprpaperRequest(argc, argv); - else if (fullRequest.contains("/--batch")) batchRequest(argc, argv); else if (fullRequest.contains("/--help")) printf("%s", USAGE.c_str()); else { printf("%s\n", USAGE.c_str()); diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 684bca9c..ab72d86e 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -513,22 +513,24 @@ std::string dispatchBatch(std::string request) { std::string getReply(std::string request) { auto format = HyprCtl::FORMAT_NORMAL; - // process flags - int sepIndex = 0; - for (const auto& c : request) { - if (c == '/') { // stop at separator - break; + // process flags for non-batch requests + if (!(request.find("[[BATCH]]") == 0)) { + int sepIndex = 0; + for (const auto& c : request) { + if (c == '/') { // stop at separator + break; + } + + sepIndex++; + + if (c == 'j') + format = HyprCtl::FORMAT_JSON; } - sepIndex++; - - if (c == 'j') - format = HyprCtl::FORMAT_JSON; + if (sepIndex < request.size()) + request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string } - if (sepIndex < request.size()) - request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string - if (request == "monitors") return monitorsRequest(format); else if (request == "workspaces")