From 6bbcc08fe0a7cb2b9b38a0112242f4f14e7430d1 Mon Sep 17 00:00:00 2001 From: WhySoBad <49595640+WhySoBad@users.noreply.github.com> Date: Fri, 10 May 2024 12:53:41 +0200 Subject: [PATCH] Printdesks command (#42) * feat: start of printdesks command * feat: update README, finish printdesks print * fix: support for new pointer wrapper for monitors * feat: easier printdesks implementation * feat: rename printdesks to printstate, only use active layout --- .gitignore | 1 + README.md | 1 + include/VirtualDesk.hpp | 1 + include/utils.hpp | 1 + src/VirtualDeskManager.cpp | 2 +- src/main.cpp | 64 +++++++++++++++++++++++++++++++++++++- 6 files changed, 68 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 41be4b0..a53c6bd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ compile_flags.txt compile_commands.json .cache result/ +.idea \ No newline at end of file diff --git a/README.md b/README.md index 7b602a7..5071147 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ Since version 2.2, this plugin exposes a couple of `hyprctl` commands. That is, | Command | description | args | example| |------------|-------------|------|--------| | printdesk (vdesk)| Prints to Hyprland log the specified vdesk or the currently active vdesk* (if no argument is given) | optional vdesk, see [above](#hyprctl-dispatchers) | `hyprctl printdesk` or `hyprctl printdesk 2` or `hyprctl printdesk coding`| +| printstate | Prints state of all vdesks | `none` | `hyprctl printstate` | | printlayout | print to Hyprland logs the current layout | `none` | `hyprctl printlayout` | diff --git a/include/VirtualDesk.hpp b/include/VirtualDesk.hpp index 8834fa3..4bd1263 100644 --- a/include/VirtualDesk.hpp +++ b/include/VirtualDesk.hpp @@ -14,6 +14,7 @@ #include typedef std::unordered_map WorkspaceMap; +// map with CMonitor* -> hyprland workspace id typedef std::unordered_map Layout; typedef std::string MonitorName; diff --git a/include/utils.hpp b/include/utils.hpp index d8803c2..bf2c720 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -37,6 +37,7 @@ const std::string BACKCYCLE_DISPATCH_STR = "backcyclevdesks"; const std::string RESET_VDESK_DISPATCH_STR = "vdeskreset"; const std::string PRINTDESK_DISPATCH_STR = "printdesk"; +const std::string PRINTSTATE_DISPATCH_STR = "printstate"; const std::string PRINTLAYOUT_DISPATCH_STR = "printlayout"; const std::string REMEMBER_NONE = "none"; diff --git a/src/VirtualDeskManager.cpp b/src/VirtualDeskManager.cpp index 0045da7..e48c4ce 100644 --- a/src/VirtualDeskManager.cpp +++ b/src/VirtualDeskManager.cpp @@ -292,4 +292,4 @@ CMonitor* VirtualDeskManager::getCurrentMonitor() { return nullptr; } return currentMonitor; -} +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6034374..adb2bea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -174,6 +174,60 @@ std::string printVDeskDispatch(eHyprCtlOutputFormat format, std::string arg) { return ""; } +std::string printStateDispatch(eHyprCtlOutputFormat format, std::string arg) { + std::string out; + if (format == eHyprCtlOutputFormat::FORMAT_NORMAL) { + out += "Virtual desks\n"; + int index = 0; + for(auto const& [vdeskId, desk] : manager->vdesksMap) { + unsigned int windows = 0; + std::string workspaces; + bool first = true; + for(auto const& [monitor, workspaceId] : desk->activeLayout(manager->conf)) { + windows += g_pCompositor->getWindowsOnWorkspace(workspaceId); + if(!first) workspaces += ", "; + else first = false; + workspaces += std::format("{}", workspaceId); + } + out += std::format( + "- {}: {}\n Focused: {}\n Populated: {}\n Workspaces: {}\n Windows: {}\n", + desk->name, + desk->id, + manager->activeVdesk().get() == desk.get(), + windows > 0, + workspaces, + windows + ); + if(index++ < manager->vdesksMap.size() - 1) out += "\n"; + } + } else if(format == eHyprCtlOutputFormat::FORMAT_JSON) { + std::string vdesks; + int index = 0; + for(auto const& [vdeskId, desk] : manager->vdesksMap) { + unsigned int windows = 0; + std::string workspaces; + bool first = true; + for(auto const& [monitor, workspaceId] : desk->activeLayout(manager->conf)) { + windows += g_pCompositor->getWindowsOnWorkspace(workspaceId); + if(!first) workspaces += ", "; + else first = false; + workspaces += std::format("{}", workspaceId); + } + vdesks += std::format(R"#({{ + "id": {}, + "name": "{}", + "focused": {}, + "populated": {}, + "workspaces": [{}], + "windows": {} + }})#", vdeskId, desk->name, manager->activeVdesk().get() == desk.get(), windows > 0, workspaces, windows); + if(index++ < manager->vdesksMap.size() - 1) vdesks += ","; + } + out += std::format(R"#([{}])#", vdesks); + } + return out; +} + std::string printLayoutDispatch(eHyprCtlOutputFormat format, std::string arg) { auto activeDesk = manager->activeVdesk(); auto layout = activeDesk->activeLayout(manager->conf); @@ -290,13 +344,21 @@ void registerHyprctlCommands() { if (!ptr) printLog(std::format("Failed to register hyprctl command: {}", PRINTLAYOUT_DISPATCH_STR)); + // Register printstate + cmd.name = PRINTSTATE_DISPATCH_STR; + cmd.fn = printStateDispatch; + cmd.exact = true; + ptr = HyprlandAPI::registerHyprCtlCommand(PHANDLE, cmd); + if (!ptr) + printLog(std::format("Failed to register hyprctl command: {}", PRINTSTATE_DISPATCH_STR)); + // Register printdesk cmd.name = PRINTDESK_DISPATCH_STR; cmd.fn = printVDeskDispatch; cmd.exact = false; ptr = HyprlandAPI::registerHyprCtlCommand(PHANDLE, cmd); if (!ptr) - printLog(std::format("Failed to register hyprctl command: {}", VDESK_DISPATCH_STR)); + printLog(std::format("Failed to register hyprctl command: {}", PRINTDESK_DISPATCH_STR)); } // Do NOT change this function.