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