Add hyprctl output in json when -j flag is used

This commit is contained in:
bazuin-32 2022-07-12 11:57:33 -06:00
parent f9985a36b3
commit 036f431206
2 changed files with 364 additions and 61 deletions

View file

@ -195,16 +195,23 @@ int main(int argc, char** argv) {
return 1;
}
if (!strcmp(argv[1], "monitors")) request("monitors");
else if (!strcmp(argv[1], "clients")) request("clients");
else if (!strcmp(argv[1], "workspaces")) request("workspaces");
else if (!strcmp(argv[1], "activewindow")) request("activewindow");
else if (!strcmp(argv[1], "layers")) request("layers");
else if (!strcmp(argv[1], "version")) request("version");
else if (!strcmp(argv[1], "kill")) request("kill");
else if (!strcmp(argv[1], "splash")) request("splash");
else if (!strcmp(argv[1], "devices")) request("devices");
else if (!strcmp(argv[1], "reload")) request("reload");
std::string args(argv[1]);
if (argc > 2) {
args += " ";
args += argv[2];
}
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);

View file

@ -13,18 +13,97 @@
#include <string>
std::string monitorsRequest() {
std::string monitorsRequest(bool json) {
std::string result = "";
if (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) {
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"));
}
}
return result;
}
std::string clientsRequest() {
std::string clientsRequest(bool json) {
std::string result = "";
if (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) {
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",
@ -32,31 +111,147 @@ std::string clientsRequest() {
}
}
return result;
}
std::string workspacesRequest() {
std::string result = "";
for (auto& w : g_pCompositor->m_vWorkspaces) {
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);
}
return result;
}
std::string activeWindowRequest() {
std::string workspacesRequest(bool json) {
std::string result = "";
if (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) {
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);
}
}
return result;
}
std::string activeWindowRequest(bool json) {
const auto PWINDOW = g_pCompositor->m_pLastWindow;
if (!g_pCompositor->windowValidMapped(PWINDOW))
return "Invalid";
if (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",
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(bool json) {
std::string result = "";
if (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) {
result += getFormat("Monitor %s:\n", mon->szName.c_str());
int layerLevel = 0;
@ -71,13 +266,108 @@ std::string layersRequest() {
}
result += "\n\n";
}
}
return result;
}
std::string devicesRequest() {
std::string devicesRequest(bool json) {
std::string result = "";
if (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";
for (auto& m : g_pInputManager->m_lMice) {
@ -103,6 +393,7 @@ std::string devicesRequest() {
for (auto& d : g_pInputManager->m_lTabletTools) {
result += getFormat("\tTablet Tool at %x (belongs to %x)\n", &d, d.wlrTabletTool ? d.wlrTabletTool->data : 0);
}
}
return result;
}
@ -220,24 +511,29 @@ 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
if (request == "monitors")
return monitorsRequest();
return monitorsRequest(json);
else if (request == "workspaces")
return workspacesRequest();
return workspacesRequest(json);
else if (request == "clients")
return clientsRequest();
return clientsRequest(json);
else if (request == "kill")
return killRequest();
else if (request == "activewindow")
return activeWindowRequest();
return activeWindowRequest(json);
else if (request == "layers")
return layersRequest();
return layersRequest(json);
else if (request == "version")
return versionRequest();
else if (request == "reload")
return reloadRequest();
else if (request == "devices")
return devicesRequest();
return devicesRequest(json);
else if (request == "splash")
return splashRequest();
else if (request.find("dispatch") == 0)