Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printdesks command #42

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ compile_flags.txt
compile_commands.json
.cache
result/
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |


Expand Down
1 change: 1 addition & 0 deletions include/VirtualDesk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <hyprland/src/helpers/memory/SharedPtr.hpp>

typedef std::unordered_map<int, int> WorkspaceMap;
// map with CMonitor* -> hyprland workspace id
typedef std::unordered_map<const CMonitor*, int> Layout;
levnikmyskin marked this conversation as resolved.
Show resolved Hide resolved
typedef std::string MonitorName;

Expand Down
1 change: 1 addition & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 3 additions & 3 deletions src/VirtualDeskManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void VirtualDeskManager::cycleWorkspaces() {
return;

auto n_monitors = g_pCompositor->m_vMonitors.size();
CMonitor* currentMonitor = g_pCompositor->m_pLastMonitor;
CMonitor* currentMonitor = g_pCompositor->m_pLastMonitor.get();

// TODO: implement for more than two monitors as well.
// This probably requires to compute monitors position
Expand Down Expand Up @@ -281,7 +281,7 @@ void VirtualDeskManager::invalidateAllLayouts() {
}

CMonitor* VirtualDeskManager::getCurrentMonitor() {
CMonitor* currentMonitor = g_pCompositor->m_pLastMonitor;
CMonitor* currentMonitor = g_pCompositor->m_pLastMonitor.get();
// This can happen when we receive the "on disconnect" signal
// let's just take first monitor we can find
if (currentMonitor && (!currentMonitor->m_bEnabled || !currentMonitor->output)) {
Expand All @@ -292,4 +292,4 @@ CMonitor* VirtualDeskManager::getCurrentMonitor() {
return nullptr;
}
return currentMonitor;
}
}
64 changes: 63 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down