Merge pull request #360 from bazuin-32/main

Add hyprctl output in json when `-j` flag is used
This commit is contained in:
Vaxry 2022-07-13 15:44:59 +02:00 committed by GitHub
commit be69098cb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 403 additions and 72 deletions

View file

@ -1,4 +1,4 @@
all: all:
g++ -std=c++20 ./main.cpp -o ./hyprctl g++ -std=c++23 ./main.cpp -o ./hyprctl
clean: clean:
rm ./hyprctl rm ./hyprctl

View file

@ -15,8 +15,9 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
const std::string USAGE = R"#(usage: hyprctl [command] [(opt)args] const std::string USAGE = R"#(usage: hyprctl [(opt)flag] [command] [(opt)args]
commands:
monitors monitors
workspaces workspaces
clients clients
@ -29,7 +30,11 @@ const std::string USAGE = R"#(usage: hyprctl [command] [(opt)args]
kill kill
splash splash
hyprpaper hyprpaper
reload)#"; reload
flags:
-j -> output in JSON
)#";
void request(std::string arg) { void request(std::string arg) {
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
@ -181,8 +186,8 @@ void hyprpaperRequest(int argc, char** argv) {
requestHyprpaper(rq); requestHyprpaper(rq);
} }
void batchRequest(int argc, char** argv) { void batchRequest(std::string arg) {
std::string rq = "[[BATCH]]" + std::string(argv[2]); std::string rq = "[[BATCH]]" + arg.substr(arg.find_first_of(" ") + 1);
request(rq); request(rq);
} }
@ -195,21 +200,31 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
if (!strcmp(argv[1], "monitors")) request("monitors"); std::string fullRequest = "";
else if (!strcmp(argv[1], "clients")) request("clients"); for (int i = 1; i < argc; i++) {
else if (!strcmp(argv[1], "workspaces")) request("workspaces"); fullRequest += std::string(argv[i]) + " ";
else if (!strcmp(argv[1], "activewindow")) request("activewindow"); }
else if (!strcmp(argv[1], "layers")) request("layers"); fullRequest.pop_back(); // remove trailing space
else if (!strcmp(argv[1], "version")) request("version");
else if (!strcmp(argv[1], "kill")) request("kill"); if (!std::string(argv[1]).contains("/")) {
else if (!strcmp(argv[1], "splash")) request("splash"); fullRequest = "/" + fullRequest;
else if (!strcmp(argv[1], "devices")) request("devices"); }
else if (!strcmp(argv[1], "reload")) request("reload");
else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv); if (fullRequest.contains("/--batch")) batchRequest(fullRequest);
else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv); else if (fullRequest.contains("/monitors")) request(fullRequest);
else if (!strcmp(argv[1], "hyprpaper")) hyprpaperRequest(argc, argv); else if (fullRequest.contains("/clients")) request(fullRequest);
else if (!strcmp(argv[1], "--batch")) batchRequest(argc, argv); else if (fullRequest.contains("/workspaces")) request(fullRequest);
else if (!strcmp(argv[1], "--help")) printf("%s", USAGE.c_str()); 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("/--help")) printf("%s", USAGE.c_str());
else { else {
printf("%s\n", USAGE.c_str()); printf("%s\n", USAGE.c_str());
return 1; return 1;

View file

@ -13,18 +13,97 @@
#include <string> #include <string>
std::string monitorsRequest() { std::string monitorsRequest(HyprCtl::eHyprCtlOutputFormat format) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_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) { 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", 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")); 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; return result;
} }
std::string clientsRequest() { std::string clientsRequest(HyprCtl::eHyprCtlOutputFormat format) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_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) { for (auto& w : g_pCompositor->m_vWindows) {
if (w->m_bIsMapped) { 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", 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",
@ -32,31 +111,147 @@ std::string clientsRequest() {
} }
} }
}
return result; return result;
} }
std::string workspacesRequest() { std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_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) { for (auto& w : g_pCompositor->m_vWorkspaces) {
result += getFormat("workspace ID %i (%s) on monitor %s:\n\twindows: %i\n\thasfullscreen: %i\n\n", 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); 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; return result;
} }
std::string activeWindowRequest() { std::string activeWindowRequest(HyprCtl::eHyprCtlOutputFormat format) {
const auto PWINDOW = g_pCompositor->m_pLastWindow; const auto PWINDOW = g_pCompositor->m_pLastWindow;
if (!g_pCompositor->windowValidMapped(PWINDOW)) if (!g_pCompositor->windowValidMapped(PWINDOW))
return "Invalid"; return "Invalid";
if (format == HyprCtl::FORMAT_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", 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()); 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(HyprCtl::eHyprCtlOutputFormat format) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) {
result += "{\n";
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++;
}
// 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) { for (auto& mon : g_pCompositor->m_vMonitors) {
result += getFormat("Monitor %s:\n", mon->szName.c_str()); result += getFormat("Monitor %s:\n", mon->szName.c_str());
int layerLevel = 0; int layerLevel = 0;
@ -71,13 +266,108 @@ std::string layersRequest() {
} }
result += "\n\n"; result += "\n\n";
} }
}
return result; return result;
} }
std::string devicesRequest() { std::string devicesRequest(HyprCtl::eHyprCtlOutputFormat format) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) {
result += "{\n";
result += "\"mice\": [\n";
for (auto& m : g_pInputManager->m_lMice) {
result += getFormat(
R"#( {
"address": "0x%x",
"name": "%s"
},)#",
&m,
m.mouse->name
);
}
// remove trailing comma
result.pop_back();
result += "\n],\n";
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()
);
}
// remove trailing comma
result.pop_back();
result += "\n],\n";
result += "\"tablets\": [\n";
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_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"; result += "mice:\n";
for (auto& m : g_pInputManager->m_lMice) { for (auto& m : g_pInputManager->m_lMice) {
@ -103,6 +393,7 @@ std::string devicesRequest() {
for (auto& d : g_pInputManager->m_lTabletTools) { for (auto& d : g_pInputManager->m_lTabletTools) {
result += getFormat("\tTablet Tool at %x (belongs to %x)\n", &d, d.wlrTabletTool ? d.wlrTabletTool->data : 0); result += getFormat("\tTablet Tool at %x (belongs to %x)\n", &d, d.wlrTabletTool ? d.wlrTabletTool->data : 0);
} }
}
return result; return result;
} }
@ -220,24 +511,44 @@ std::string dispatchBatch(std::string request) {
} }
std::string getReply(std::string request) { std::string getReply(std::string request) {
auto format = HyprCtl::FORMAT_NORMAL;
// 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;
}
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") if (request == "monitors")
return monitorsRequest(); return monitorsRequest(format);
else if (request == "workspaces") else if (request == "workspaces")
return workspacesRequest(); return workspacesRequest(format);
else if (request == "clients") else if (request == "clients")
return clientsRequest(); return clientsRequest(format);
else if (request == "kill") else if (request == "kill")
return killRequest(); return killRequest();
else if (request == "activewindow") else if (request == "activewindow")
return activeWindowRequest(); return activeWindowRequest(format);
else if (request == "layers") else if (request == "layers")
return layersRequest(); return layersRequest(format);
else if (request == "version") else if (request == "version")
return versionRequest(); return versionRequest();
else if (request == "reload") else if (request == "reload")
return reloadRequest(); return reloadRequest();
else if (request == "devices") else if (request == "devices")
return devicesRequest(); return devicesRequest(format);
else if (request == "splash") else if (request == "splash")
return splashRequest(); return splashRequest();
else if (request.find("dispatch") == 0) else if (request.find("dispatch") == 0)

View file

@ -14,4 +14,9 @@ namespace HyprCtl {
inline std::string request = ""; inline std::string request = "";
inline std::ifstream requestStream; inline std::ifstream requestStream;
enum eHyprCtlOutputFormat {
FORMAT_NORMAL = 0,
FORMAT_JSON
};
}; };