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>
This commit is contained in:
Yavor Kolev 2023-05-02 06:51:52 -07:00 committed by GitHub
parent cde7f79af0
commit ac3edec14b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 27 deletions

View file

@ -23,6 +23,7 @@ const std::string USAGE = R"#(usage: hyprctl [(opt)flags] [command] [(opt)args]
commands: commands:
monitors monitors
workspaces workspaces
activeworkspace
clients clients
activewindow activewindow
layers layers
@ -50,7 +51,9 @@ flags:
--batch -> execute a batch of commands, separated by ';' --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 SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
const auto ARGS = std::count(arg.begin(), arg.end(), ' '); const auto ARGS = std::count(arg.begin(), arg.end(), ' ');
@ -327,6 +330,8 @@ int main(int argc, char** argv) {
request(fullRequest); request(fullRequest);
else if (fullRequest.contains("/workspaces")) else if (fullRequest.contains("/workspaces"))
request(fullRequest); request(fullRequest);
else if (fullRequest.contains("/activeworkspace"))
request(fullRequest);
else if (fullRequest.contains("/activewindow")) else if (fullRequest.contains("/activewindow"))
request(fullRequest); request(fullRequest);
else if (fullRequest.contains("/layers")) else if (fullRequest.contains("/layers"))

View file

@ -188,17 +188,11 @@ std::string clientsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) { static std::string getWorkspaceData(CWorkspace* w, HyprCtl::eHyprCtlOutputFormat format) {
std::string result = ""; const auto PLASTW = w->getLastFocusedWindow();
const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID);
if (format == HyprCtl::FORMAT_JSON) { if (format == HyprCtl::FORMAT_JSON) {
result += "["; return getFormat(R"#({
for (auto& w : g_pCompositor->m_vWorkspaces) {
const auto PLASTW = w->getLastFocusedWindow();
const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID);
result += getFormat(
R"#({
"id": %i, "id": %i,
"name": "%s", "name": "%s",
"monitor": "%s", "monitor": "%s",
@ -206,24 +200,41 @@ std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) {
"hasfullscreen": %s, "hasfullscreen": %s,
"lastwindow": "0x%lx", "lastwindow": "0x%lx",
"lastwindowtitle": "%s" "lastwindowtitle": "%s"
},)#", })#",
w->m_iID, escapeJSONStrings(w->m_szName).c_str(), escapeJSONStrings(PMONITOR ? PMONITOR->szName : "?").c_str(), g_pCompositor->getWindowsOnWorkspace(w->m_iID), w->m_iID, escapeJSONStrings(w->m_szName).c_str(), escapeJSONStrings(PMONITOR ? PMONITOR->szName : "?").c_str(),
((int)w->m_bHasFullscreenWindow == 1 ? "true" : "false"), PLASTW, PLASTW ? escapeJSONStrings(PLASTW->m_szTitle).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.pop_back();
result += "]"; result += "]";
} else { } else {
for (auto& w : g_pCompositor->m_vWorkspaces) { for (auto& w : g_pCompositor->m_vWorkspaces) {
const auto PLASTW = w->getLastFocusedWindow(); result += getWorkspaceData(w.get(), format);
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() : "");
} }
} }
return result; return result;
} }
@ -804,10 +815,10 @@ std::string dispatchBatch(std::string request) {
} }
std::string dispatchSetCursor(std::string request) { std::string dispatchSetCursor(std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
const auto SIZESTR = vars[vars.size() - 1]; const auto SIZESTR = vars[vars.size() - 1];
std::string theme = ""; std::string theme = "";
for (size_t i = 1; i < vars.size() - 1; ++i) for (size_t i = 1; i < vars.size() - 1; ++i)
theme += vars[i] + " "; theme += vars[i] + " ";
theme.pop_back(); theme.pop_back();
@ -815,9 +826,7 @@ std::string dispatchSetCursor(std::string request) {
int size = 0; int size = 0;
try { try {
size = std::stoi(SIZESTR); size = std::stoi(SIZESTR);
} catch (...) { } catch (...) { return "size not int"; }
return "size not int";
}
if (size <= 0) if (size <= 0)
return "size not positive"; return "size not positive";
@ -1215,6 +1224,8 @@ std::string getReply(std::string request) {
return monitorsRequest(format); return monitorsRequest(format);
else if (request == "workspaces") else if (request == "workspaces")
return workspacesRequest(format); return workspacesRequest(format);
else if (request == "activeworkspace")
return activeWorkspaceRequest(format);
else if (request == "clients") else if (request == "clients")
return clientsRequest(format); return clientsRequest(format);
else if (request == "kill") else if (request == "kill")