hyprctl: move to a class and unify commands

This commit is contained in:
Vaxry 2024-02-05 01:43:45 +00:00
parent cbadf3e3f3
commit 939696f97e
6 changed files with 165 additions and 155 deletions

View file

@ -464,6 +464,9 @@ void CCompositor::initManagers(eManagersInitStage stage) {
Debug::log(LOG, "Creating the ThreadManager!"); Debug::log(LOG, "Creating the ThreadManager!");
g_pThreadManager = std::make_unique<CThreadManager>(); g_pThreadManager = std::make_unique<CThreadManager>();
Debug::log(LOG, "Creating CHyprCtl");
g_pHyprCtl = std::make_unique<CHyprCtl>();
Debug::log(LOG, "Creating the InputManager!"); Debug::log(LOG, "Creating the InputManager!");
g_pInputManager = std::make_unique<CInputManager>(); g_pInputManager = std::make_unique<CInputManager>();

View file

@ -600,12 +600,15 @@ static bool parseModeLine(const std::string& modeline, drmModeModeInfo& mode) {
mode.vtotal = std::stoi(args[argno++]); mode.vtotal = std::stoi(args[argno++]);
mode.vrefresh = mode.clock * 1000.0 * 1000.0 / mode.htotal / mode.vtotal; mode.vrefresh = mode.clock * 1000.0 * 1000.0 / mode.htotal / mode.vtotal;
// clang-format off
static std::unordered_map<std::string, uint32_t> flagsmap = { static std::unordered_map<std::string, uint32_t> flagsmap = {
{"+hsync", DRM_MODE_FLAG_PHSYNC}, {"+hsync", DRM_MODE_FLAG_PHSYNC},
{"-hsync", DRM_MODE_FLAG_NHSYNC}, {"-hsync", DRM_MODE_FLAG_NHSYNC},
{"+vsync", DRM_MODE_FLAG_PVSYNC}, {"+vsync", DRM_MODE_FLAG_PVSYNC},
{"-vsync", DRM_MODE_FLAG_NVSYNC}, {"-vsync", DRM_MODE_FLAG_NVSYNC},
{"Interlace", DRM_MODE_FLAG_INTERLACE},
}; };
// clang-format on
for (; argno < static_cast<int>(args.size()); argno++) { for (; argno < static_cast<int>(args.size()); argno++) {
auto key = args[argno]; auto key = args[argno];

View file

@ -29,7 +29,7 @@ static std::string getWorkspaceNameFromSpecialID(const int workspaceID) {
return workspace->m_szName; return workspace->m_szName;
} }
std::string monitorsRequest(std::string request, HyprCtl::eHyprCtlOutputFormat format) { std::string monitorsRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
auto allMonitors = false; auto allMonitors = false;
@ -40,7 +40,7 @@ std::string monitorsRequest(std::string request, HyprCtl::eHyprCtlOutputFormat f
allMonitors = true; allMonitors = true;
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "["; result += "[";
for (auto& m : allMonitors ? g_pCompositor->m_vRealMonitors : g_pCompositor->m_vMonitors) { for (auto& m : allMonitors ? g_pCompositor->m_vRealMonitors : g_pCompositor->m_vMonitors) {
@ -110,8 +110,8 @@ std::string monitorsRequest(std::string request, HyprCtl::eHyprCtlOutputFormat f
return result; return result;
} }
static std::string getGroupedData(CWindow* w, HyprCtl::eHyprCtlOutputFormat format) { static std::string getGroupedData(CWindow* w, CHyprCtl::eHyprCtlOutputFormat format) {
const bool isJson = format == HyprCtl::FORMAT_JSON; const bool isJson = format == CHyprCtl::FORMAT_JSON;
if (!w->m_sGroupData.pNextWindow) if (!w->m_sGroupData.pNextWindow)
return isJson ? "" : "0"; return isJson ? "" : "0";
@ -134,7 +134,7 @@ static std::string getGroupedData(CWindow* w, HyprCtl::eHyprCtlOutputFormat form
return result.str(); return result.str();
} }
static std::string getWindowData(CWindow* w, HyprCtl::eHyprCtlOutputFormat format) { static std::string getWindowData(CWindow* w, CHyprCtl::eHyprCtlOutputFormat format) {
auto getFocusHistoryID = [](CWindow* wnd) -> int { auto getFocusHistoryID = [](CWindow* wnd) -> int {
for (size_t i = 0; i < g_pCompositor->m_vWindowFocusHistory.size(); ++i) { for (size_t i = 0; i < g_pCompositor->m_vWindowFocusHistory.size(); ++i) {
if (g_pCompositor->m_vWindowFocusHistory[i] == wnd) if (g_pCompositor->m_vWindowFocusHistory[i] == wnd)
@ -143,7 +143,7 @@ static std::string getWindowData(CWindow* w, HyprCtl::eHyprCtlOutputFormat forma
return -1; return -1;
}; };
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
return std::format( return std::format(
R"#({{ R"#({{
"address": "0x{:x}", "address": "0x{:x}",
@ -199,9 +199,9 @@ static std::string getWindowData(CWindow* w, HyprCtl::eHyprCtlOutputFormat forma
} }
} }
std::string clientsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string clientsRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "["; result += "[";
for (auto& w : g_pCompositor->m_vWindows) { for (auto& w : g_pCompositor->m_vWindows) {
@ -219,10 +219,10 @@ std::string clientsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
static std::string getWorkspaceData(CWorkspace* w, HyprCtl::eHyprCtlOutputFormat format) { static std::string getWorkspaceData(CWorkspace* w, CHyprCtl::eHyprCtlOutputFormat format) {
const auto PLASTW = w->getLastFocusedWindow(); const auto PLASTW = w->getLastFocusedWindow();
const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID); const auto PMONITOR = g_pCompositor->getMonitorFromID(w->m_iMonitorID);
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
return std::format(R"#({{ return std::format(R"#({{
"id": {}, "id": {},
"name": "{}", "name": "{}",
@ -243,9 +243,9 @@ static std::string getWorkspaceData(CWorkspace* w, HyprCtl::eHyprCtlOutputFormat
} }
} }
static std::string getWorkspaceRuleData(const SWorkspaceRule& r, HyprCtl::eHyprCtlOutputFormat format) { static std::string getWorkspaceRuleData(const SWorkspaceRule& r, CHyprCtl::eHyprCtlOutputFormat format) {
const auto boolToString = [](const bool b) -> std::string { return b ? "true" : "false"; }; const auto boolToString = [](const bool b) -> std::string { return b ? "true" : "false"; };
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
const std::string monitor = r.monitor.empty() ? "" : std::format(",\n \"monitor\": \"{}\"", escapeJSONStrings(r.monitor)); const std::string monitor = r.monitor.empty() ? "" : std::format(",\n \"monitor\": \"{}\"", escapeJSONStrings(r.monitor));
const std::string default_ = (bool)(r.isDefault) ? std::format(",\n \"default\": {}", boolToString(r.isDefault)) : ""; const std::string default_ = (bool)(r.isDefault) ? std::format(",\n \"default\": {}", boolToString(r.isDefault)) : "";
const std::string persistent = (bool)(r.isPersistent) ? std::format(",\n \"persistent\": {}", boolToString(r.isPersistent)) : ""; const std::string persistent = (bool)(r.isPersistent) ? std::format(",\n \"persistent\": {}", boolToString(r.isPersistent)) : "";
@ -281,7 +281,7 @@ static std::string getWorkspaceRuleData(const SWorkspaceRule& r, HyprCtl::eHyprC
return result; return result;
} }
} }
std::string activeWorkspaceRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string activeWorkspaceRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
if (!g_pCompositor->m_pLastMonitor) if (!g_pCompositor->m_pLastMonitor)
return "unsafe state"; return "unsafe state";
@ -294,10 +294,10 @@ std::string activeWorkspaceRequest(HyprCtl::eHyprCtlOutputFormat format) {
return getWorkspaceData(w, format); return getWorkspaceData(w, format);
} }
std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string workspacesRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "["; result += "[";
for (auto& w : g_pCompositor->m_vWorkspaces) { for (auto& w : g_pCompositor->m_vWorkspaces) {
result += getWorkspaceData(w.get(), format); result += getWorkspaceData(w.get(), format);
@ -315,9 +315,9 @@ std::string workspacesRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string workspaceRulesRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string workspaceRulesRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "["; result += "[";
for (auto& r : g_pConfigManager->getAllWorkspaceRules()) { for (auto& r : g_pConfigManager->getAllWorkspaceRules()) {
result += getWorkspaceRuleData(r, format); result += getWorkspaceRuleData(r, format);
@ -335,24 +335,24 @@ std::string workspaceRulesRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string activeWindowRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string activeWindowRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
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 format == HyprCtl::FORMAT_JSON ? "{}" : "Invalid"; return format == CHyprCtl::FORMAT_JSON ? "{}" : "Invalid";
auto result = getWindowData(PWINDOW, format); auto result = getWindowData(PWINDOW, format);
if (format == HyprCtl::FORMAT_JSON) if (format == CHyprCtl::FORMAT_JSON)
result.pop_back(); result.pop_back();
return result; return result;
} }
std::string layersRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string layersRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "{\n"; result += "{\n";
for (auto& mon : g_pCompositor->m_vMonitors) { for (auto& mon : g_pCompositor->m_vMonitors) {
@ -423,9 +423,9 @@ std::string layersRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string layoutsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string layoutsRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "["; result += "[";
for (auto& m : g_pLayoutManager->getAllLayoutNames()) { for (auto& m : g_pLayoutManager->getAllLayoutNames()) {
@ -445,10 +445,10 @@ std::string layoutsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string devicesRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string devicesRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "{\n"; result += "{\n";
result += "\"mice\": [\n"; result += "\"mice\": [\n";
@ -604,9 +604,9 @@ std::string devicesRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string animationsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string animationsRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string ret = ""; std::string ret = "";
if (format == HyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) { if (format == CHyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) {
ret += "animations:\n"; ret += "animations:\n";
for (auto& ac : g_pConfigManager->getAnimationConfig()) { for (auto& ac : g_pConfigManager->getAnimationConfig()) {
@ -657,10 +657,10 @@ std::string animationsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return ret; return ret;
} }
std::string rollinglogRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string rollinglogRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "[\n\"log\":\""; result += "[\n\"log\":\"";
result += escapeJSONStrings(Debug::rollingLog); result += escapeJSONStrings(Debug::rollingLog);
result += "\"]"; result += "\"]";
@ -671,10 +671,10 @@ std::string rollinglogRequest(HyprCtl::eHyprCtlOutputFormat format) {
return result; return result;
} }
std::string globalShortcutsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string globalShortcutsRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string ret = ""; std::string ret = "";
const auto SHORTCUTS = g_pProtocolManager->m_pGlobalShortcutsProtocolManager->getAllShortcuts(); const auto SHORTCUTS = g_pProtocolManager->m_pGlobalShortcutsProtocolManager->getAllShortcuts();
if (format == HyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) { if (format == CHyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) {
for (auto& sh : SHORTCUTS) for (auto& sh : SHORTCUTS)
ret += std::format("{}:{} -> {}\n", sh.appid, sh.id, sh.description); ret += std::format("{}:{} -> {}\n", sh.appid, sh.id, sh.description);
} else { } else {
@ -694,9 +694,9 @@ std::string globalShortcutsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return ret; return ret;
} }
std::string bindsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string bindsRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string ret = ""; std::string ret = "";
if (format == HyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) { if (format == CHyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) {
for (auto& kb : g_pKeybindManager->m_lKeybinds) { for (auto& kb : g_pKeybindManager->m_lKeybinds) {
ret += "bind"; ret += "bind";
if (kb.locked) if (kb.locked)
@ -742,12 +742,12 @@ std::string bindsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return ret; return ret;
} }
std::string versionRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string versionRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
auto commitMsg = removeBeginEndSpacesTabs(GIT_COMMIT_MESSAGE); auto commitMsg = removeBeginEndSpacesTabs(GIT_COMMIT_MESSAGE);
std::replace(commitMsg.begin(), commitMsg.end(), '#', ' '); std::replace(commitMsg.begin(), commitMsg.end(), '#', ' ');
if (format == HyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) { if (format == CHyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) {
std::string result = "Hyprland, built from branch " + std::string(GIT_BRANCH) + " at commit " + GIT_COMMIT_HASH + " " + GIT_DIRTY + " (" + commitMsg + std::string result = "Hyprland, built from branch " + std::string(GIT_BRANCH) + " at commit " + GIT_COMMIT_HASH + " " + GIT_DIRTY + " (" + commitMsg +
").\nDate: " + GIT_COMMIT_DATE + "\nTag: " + GIT_TAG + "\n\nflags: (if any)\n"; ").\nDate: " + GIT_COMMIT_DATE + "\nTag: " + GIT_TAG + "\n\nflags: (if any)\n";
@ -794,8 +794,8 @@ std::string versionRequest(HyprCtl::eHyprCtlOutputFormat format) {
return ""; // make the compiler happy return ""; // make the compiler happy
} }
std::string systemInfoRequest() { std::string systemInfoRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string result = versionRequest(HyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL); std::string result = versionRequest(CHyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL, "");
result += "\n\nSystem Information:\n"; result += "\n\nSystem Information:\n";
@ -827,7 +827,7 @@ std::string systemInfoRequest() {
return result; return result;
} }
std::string dispatchRequest(std::string in) { std::string dispatchRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string in) {
// get rid of the dispatch keyword // get rid of the dispatch keyword
in = in.substr(in.find_first_of(' ') + 1); in = in.substr(in.find_first_of(' ') + 1);
@ -848,7 +848,7 @@ std::string dispatchRequest(std::string in) {
return "ok"; return "ok";
} }
std::string dispatchKeyword(std::string in) { std::string dispatchKeyword(CHyprCtl::eHyprCtlOutputFormat format, std::string in) {
// get rid of the keyword keyword // get rid of the keyword keyword
in = in.substr(in.find_first_of(' ') + 1); in = in.substr(in.find_first_of(' ') + 1);
@ -896,7 +896,7 @@ std::string dispatchKeyword(std::string in) {
return retval; return retval;
} }
std::string reloadRequest(const std::string& request) { std::string reloadRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
const auto REQMODE = request.substr(request.find_last_of(' ') + 1); const auto REQMODE = request.substr(request.find_last_of(' ') + 1);
@ -911,20 +911,20 @@ std::string reloadRequest(const std::string& request) {
return "ok"; return "ok";
} }
std::string killRequest() { std::string killRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
g_pInputManager->setClickMode(CLICKMODE_KILL); g_pInputManager->setClickMode(CLICKMODE_KILL);
return "ok"; return "ok";
} }
std::string splashRequest() { std::string splashRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
return g_pCompositor->m_szCurrentSplash; return g_pCompositor->m_szCurrentSplash;
} }
std::string cursorPosRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string cursorPosRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
const auto CURSORPOS = g_pInputManager->getMouseCoordsInternal().floor(); const auto CURSORPOS = g_pInputManager->getMouseCoordsInternal().floor();
if (format == HyprCtl::FORMAT_NORMAL) { if (format == CHyprCtl::FORMAT_NORMAL) {
return std::format("{}, {}", (int)CURSORPOS.x, (int)CURSORPOS.y); return std::format("{}, {}", (int)CURSORPOS.x, (int)CURSORPOS.y);
} else { } else {
return std::format(R"#( return std::format(R"#(
@ -939,9 +939,7 @@ std::string cursorPosRequest(HyprCtl::eHyprCtlOutputFormat format) {
return "error"; return "error";
} }
std::string getReply(std::string); std::string dispatchBatch(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string dispatchBatch(std::string request) {
// split by ; // split by ;
request = request.substr(9); request = request.substr(9);
@ -965,7 +963,7 @@ std::string dispatchBatch(std::string request) {
nextItem(); nextItem();
while (curitem != "") { while (curitem != "") {
reply += getReply(curitem); reply += g_pHyprCtl->getReply(curitem);
nextItem(); nextItem();
} }
@ -973,7 +971,7 @@ std::string dispatchBatch(std::string request) {
return reply; return reply;
} }
std::string dispatchSetCursor(std::string request) { std::string dispatchSetCursor(CHyprCtl::eHyprCtlOutputFormat format, 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];
@ -1005,7 +1003,7 @@ std::string dispatchSetCursor(std::string request) {
return "ok"; return "ok";
} }
std::string switchXKBLayoutRequest(const std::string& request) { std::string switchXKBLayoutRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
const auto KB = vars[1]; const auto KB = vars[1];
@ -1051,7 +1049,7 @@ std::string switchXKBLayoutRequest(const std::string& request) {
return "ok"; return "ok";
} }
std::string dispatchSeterror(std::string request) { std::string dispatchSeterror(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
std::string errorMessage = ""; std::string errorMessage = "";
@ -1080,7 +1078,7 @@ std::string dispatchSeterror(std::string request) {
return "ok"; return "ok";
} }
std::string dispatchSetProp(std::string request) { std::string dispatchSetProp(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
if (vars.size() < 4) if (vars.size() < 4)
@ -1164,7 +1162,7 @@ std::string dispatchSetProp(std::string request) {
return "ok"; return "ok";
} }
std::string dispatchGetOption(std::string request, HyprCtl::eHyprCtlOutputFormat format) { std::string dispatchGetOption(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string curitem = ""; std::string curitem = "";
auto nextItem = [&]() { auto nextItem = [&]() {
@ -1189,7 +1187,7 @@ std::string dispatchGetOption(std::string request, HyprCtl::eHyprCtlOutputFormat
if (!PCFGOPT) if (!PCFGOPT)
return "no such option"; return "no such option";
if (format == HyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL) if (format == CHyprCtl::eHyprCtlOutputFormat::FORMAT_NORMAL)
return std::format("option {}\n\tint: {}\n\tfloat: {:.5f}\n\tstr: \"{}\"\n\tdata: {:x}\n\tset: {}", curitem, PCFGOPT->intValue, PCFGOPT->floatValue, PCFGOPT->strValue, return std::format("option {}\n\tint: {}\n\tfloat: {:.5f}\n\tstr: \"{}\"\n\tdata: {:x}\n\tset: {}", curitem, PCFGOPT->intValue, PCFGOPT->floatValue, PCFGOPT->strValue,
(uintptr_t)PCFGOPT->data.get(), PCFGOPT->set); (uintptr_t)PCFGOPT->data.get(), PCFGOPT->set);
else { else {
@ -1208,7 +1206,7 @@ std::string dispatchGetOption(std::string request, HyprCtl::eHyprCtlOutputFormat
} }
} }
std::string decorationRequest(std::string request, HyprCtl::eHyprCtlOutputFormat format) { std::string decorationRequest(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
const auto PWINDOW = g_pCompositor->getWindowByRegex(vars[1]); const auto PWINDOW = g_pCompositor->getWindowByRegex(vars[1]);
@ -1216,7 +1214,7 @@ std::string decorationRequest(std::string request, HyprCtl::eHyprCtlOutputFormat
return "none"; return "none";
std::string result = ""; std::string result = "";
if (format == HyprCtl::FORMAT_JSON) { if (format == CHyprCtl::FORMAT_JSON) {
result += "["; result += "[";
for (auto& wd : PWINDOW->m_dWindowDecorations) { for (auto& wd : PWINDOW->m_dWindowDecorations) {
result += "{\n\"decorationName\": \"" + wd->getDisplayName() + "\",\n\"priority\": " + std::to_string(wd->getPositioningInfo().priority) + "\n},"; result += "{\n\"decorationName\": \"" + wd->getDisplayName() + "\",\n\"priority\": " + std::to_string(wd->getPositioningInfo().priority) + "\n},";
@ -1265,7 +1263,7 @@ void createOutputIter(wlr_backend* backend, void* data) {
} }
} }
std::string dispatchOutput(std::string request) { std::string dispatchOutput(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
std::string curitem = ""; std::string curitem = "";
auto nextItem = [&]() { auto nextItem = [&]() {
@ -1314,7 +1312,7 @@ std::string dispatchOutput(std::string request) {
return "ok"; return "ok";
} }
std::string dispatchPlugin(std::string request) { std::string dispatchPlugin(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
if (vars.size() < 2) if (vars.size() < 2)
@ -1357,7 +1355,7 @@ std::string dispatchPlugin(std::string request) {
return "ok"; return "ok";
} }
std::string dispatchNotify(std::string request) { std::string dispatchNotify(CHyprCtl::eHyprCtlOutputFormat format, std::string request) {
CVarList vars(request, 0, ' '); CVarList vars(request, 0, ' ');
if (vars.size() < 5) if (vars.size() < 5)
@ -1398,8 +1396,53 @@ std::string dispatchNotify(std::string request) {
return "ok"; return "ok";
} }
std::string getReply(std::string request) { CHyprCtl::CHyprCtl() {
auto format = HyprCtl::FORMAT_NORMAL; registerCommand(CHyprCtl::SCommand{"workspaces", true, workspacesRequest});
registerCommand(CHyprCtl::SCommand{"workspacerules", true, workspaceRulesRequest});
registerCommand(CHyprCtl::SCommand{"activeworkspace", true, activeWorkspaceRequest});
registerCommand(CHyprCtl::SCommand{"clients", true, clientsRequest});
registerCommand(CHyprCtl::SCommand{"kill", true, killRequest});
registerCommand(CHyprCtl::SCommand{"activewindow", true, activeWindowRequest});
registerCommand(CHyprCtl::SCommand{"layers", true, layersRequest});
registerCommand(CHyprCtl::SCommand{"version", true, versionRequest});
registerCommand(CHyprCtl::SCommand{"devices", true, devicesRequest});
registerCommand(CHyprCtl::SCommand{"splash", true, splashRequest});
registerCommand(CHyprCtl::SCommand{"cursorpos", true, cursorPosRequest});
registerCommand(CHyprCtl::SCommand{"binds", true, bindsRequest});
registerCommand(CHyprCtl::SCommand{"globalshortcuts", true, globalShortcutsRequest});
registerCommand(CHyprCtl::SCommand{"systeminfo", true, systemInfoRequest});
registerCommand(CHyprCtl::SCommand{"animations", true, animationsRequest});
registerCommand(CHyprCtl::SCommand{"rollinglog", true, rollinglogRequest});
registerCommand(CHyprCtl::SCommand{"layouts", true, layoutsRequest});
registerCommand(CHyprCtl::SCommand{"monitors", false, monitorsRequest});
registerCommand(CHyprCtl::SCommand{"reload", false, reloadRequest});
registerCommand(CHyprCtl::SCommand{"plugin", false, dispatchPlugin});
registerCommand(CHyprCtl::SCommand{"notify", false, dispatchNotify});
registerCommand(CHyprCtl::SCommand{"setprop", false, dispatchSetProp});
registerCommand(CHyprCtl::SCommand{"seterror", false, dispatchSeterror});
registerCommand(CHyprCtl::SCommand{"switchxkblayout", false, switchXKBLayoutRequest});
registerCommand(CHyprCtl::SCommand{"output", false, dispatchOutput});
registerCommand(CHyprCtl::SCommand{"dispatch", false, dispatchRequest});
registerCommand(CHyprCtl::SCommand{"keyword", false, dispatchKeyword});
registerCommand(CHyprCtl::SCommand{"setcursor", false, dispatchSetCursor});
registerCommand(CHyprCtl::SCommand{"getoption", false, dispatchGetOption});
registerCommand(CHyprCtl::SCommand{"decorations", false, decorationRequest});
registerCommand(CHyprCtl::SCommand{"[[BATCH]]", false, dispatchBatch});
startHyprCtlSocket();
}
std::shared_ptr<CHyprCtl::SCommand> CHyprCtl::registerCommand(SCommand cmd) {
return m_vCommands.emplace_back(std::make_shared<SCommand>(cmd));
}
void CHyprCtl::unregisterCommand(const std::shared_ptr<SCommand>& cmd) {
std::erase(m_vCommands, cmd);
}
std::string CHyprCtl::getReply(std::string request) {
auto format = CHyprCtl::FORMAT_NORMAL;
// process flags for non-batch requests // process flags for non-batch requests
if (!request.starts_with("[[BATCH]]") && request.contains("/")) { if (!request.starts_with("[[BATCH]]") && request.contains("/")) {
@ -1419,80 +1462,34 @@ std::string getReply(std::string request) {
sepIndex++; sepIndex++;
if (c == 'j') if (c == 'j')
format = HyprCtl::FORMAT_JSON; format = CHyprCtl::FORMAT_JSON;
} }
if (sepIndex < request.size()) if (sepIndex < request.size())
request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string request = request.substr(sepIndex + 1); // remove flags and separator so we can compare the rest of the string
} }
if (request.starts_with("monitors")) // parse exact cmds first, then non-exact.
return monitorsRequest(request, format); for (auto& cmd : m_vCommands) {
else if (request == "workspaces") if (!cmd->exact)
return workspacesRequest(format); continue;
else if (request == "workspacerules")
return workspaceRulesRequest(format); if (cmd->name == request)
else if (request == "activeworkspace") return cmd->fn(format, request);
return activeWorkspaceRequest(format); }
else if (request == "clients")
return clientsRequest(format); for (auto& cmd : m_vCommands) {
else if (request == "kill") if (cmd->exact)
return killRequest(); continue;
else if (request == "activewindow")
return activeWindowRequest(format); if (request.starts_with(cmd->name))
else if (request == "layers") return cmd->fn(format, request);
return layersRequest(format); }
else if (request == "version")
return versionRequest(format);
else if (request.starts_with("reload"))
return reloadRequest(request);
else if (request == "devices")
return devicesRequest(format);
else if (request == "splash")
return splashRequest();
else if (request == "cursorpos")
return cursorPosRequest(format);
else if (request == "binds")
return bindsRequest(format);
else if (request == "globalshortcuts")
return globalShortcutsRequest(format);
else if (request == "systeminfo")
return systemInfoRequest();
else if (request == "animations")
return animationsRequest(format);
else if (request == "rollinglog")
return rollinglogRequest(format);
else if (request == "layouts")
return layoutsRequest(format);
else if (request.starts_with("plugin"))
return dispatchPlugin(request);
else if (request.starts_with("notify"))
return dispatchNotify(request);
else if (request.starts_with("setprop"))
return dispatchSetProp(request);
else if (request.starts_with("seterror"))
return dispatchSeterror(request);
else if (request.starts_with("switchxkblayout"))
return switchXKBLayoutRequest(request);
else if (request.starts_with("output"))
return dispatchOutput(request);
else if (request.starts_with("dispatch"))
return dispatchRequest(request);
else if (request.starts_with("keyword"))
return dispatchKeyword(request);
else if (request.starts_with("setcursor"))
return dispatchSetCursor(request);
else if (request.starts_with("getoption"))
return dispatchGetOption(request, format);
else if (request.starts_with("decorations"))
return decorationRequest(request, format);
else if (request.starts_with("[[BATCH]]"))
return dispatchBatch(request);
return "unknown request"; return "unknown request";
} }
std::string HyprCtl::makeDynamicCall(const std::string& input) { std::string CHyprCtl::makeDynamicCall(const std::string& input) {
return getReply(input); return getReply(input);
} }
@ -1503,7 +1500,7 @@ int hyprCtlFDTick(int fd, uint32_t mask, void* data) {
sockaddr_in clientAddress; sockaddr_in clientAddress;
socklen_t clientSize = sizeof(clientAddress); socklen_t clientSize = sizeof(clientAddress);
const auto ACCEPTEDCONNECTION = accept4(HyprCtl::iSocketFD, (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC); const auto ACCEPTEDCONNECTION = accept4(g_pHyprCtl->m_iSocketFD, (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC);
std::array<char, 1024> readBuffer; std::array<char, 1024> readBuffer;
@ -1533,7 +1530,7 @@ int hyprCtlFDTick(int fd, uint32_t mask, void* data) {
std::string reply = ""; std::string reply = "";
try { try {
reply = getReply(request); reply = g_pHyprCtl->getReply(request);
} catch (std::exception& e) { } catch (std::exception& e) {
Debug::log(ERR, "Error in request: {}", e.what()); Debug::log(ERR, "Error in request: {}", e.what());
reply = "Err: " + std::string(e.what()); reply = "Err: " + std::string(e.what());
@ -1543,18 +1540,17 @@ int hyprCtlFDTick(int fd, uint32_t mask, void* data) {
close(ACCEPTEDCONNECTION); close(ACCEPTEDCONNECTION);
if (g_pConfigManager->m_bWantsMonitorReload) { if (g_pConfigManager->m_bWantsMonitorReload)
g_pConfigManager->ensureMonitorStatus(); g_pConfigManager->ensureMonitorStatus();
}
return 0; return 0;
} }
void HyprCtl::startHyprCtlSocket() { void CHyprCtl::startHyprCtlSocket() {
iSocketFD = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); m_iSocketFD = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (iSocketFD < 0) { if (m_iSocketFD < 0) {
Debug::log(ERR, "Couldn't start the Hyprland Socket. (1) IPC will not work."); Debug::log(ERR, "Couldn't start the Hyprland Socket. (1) IPC will not work.");
return; return;
} }
@ -1565,15 +1561,15 @@ void HyprCtl::startHyprCtlSocket() {
strcpy(SERVERADDRESS.sun_path, socketPath.c_str()); strcpy(SERVERADDRESS.sun_path, socketPath.c_str());
if (bind(iSocketFD, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)) < 0) { if (bind(m_iSocketFD, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)) < 0) {
Debug::log(ERR, "Couldn't start the Hyprland Socket. (2) IPC will not work."); Debug::log(ERR, "Couldn't start the Hyprland Socket. (2) IPC will not work.");
return; return;
} }
// 10 max queued. // 10 max queued.
listen(iSocketFD, 10); listen(m_iSocketFD, 10);
Debug::log(LOG, "Hypr socket started at {}", socketPath); Debug::log(LOG, "Hypr socket started at {}", socketPath);
wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, iSocketFD, WL_EVENT_READABLE, hyprCtlFDTick, nullptr); wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, m_iSocketFD, WL_EVENT_READABLE, hyprCtlFDTick, nullptr);
} }

View file

@ -3,24 +3,34 @@
#include "../Compositor.hpp" #include "../Compositor.hpp"
#include <fstream> #include <fstream>
#include "../helpers/MiscFunctions.hpp" #include "../helpers/MiscFunctions.hpp"
#include <functional>
namespace HyprCtl { class CHyprCtl {
void startHyprCtlSocket(); public:
std::string makeDynamicCall(const std::string& input); CHyprCtl();
// very simple thread-safe request method
inline bool requestMade = false;
inline bool requestReady = false;
inline std::string request = "";
inline std::ifstream requestStream;
inline wl_event_source* hyprCtlTickSource = nullptr;
inline int iSocketFD = -1;
enum eHyprCtlOutputFormat { enum eHyprCtlOutputFormat {
FORMAT_NORMAL = 0, FORMAT_NORMAL = 0,
FORMAT_JSON FORMAT_JSON
}; };
struct SCommand {
std::string name = "";
bool exact = true;
std::function<std::string(eHyprCtlOutputFormat, std::string)> fn;
};
std::string makeDynamicCall(const std::string& input);
std::shared_ptr<SCommand> registerCommand(SCommand cmd);
void unregisterCommand(const std::shared_ptr<SCommand>& cmd);
std::string getReply(std::string);
int m_iSocketFD = -1;
private:
void startHyprCtlSocket();
std::vector<std::shared_ptr<SCommand>> m_vCommands;
}; };
inline std::unique_ptr<CHyprCtl> g_pHyprCtl;

View file

@ -18,8 +18,6 @@ int handleTimer(void* data) {
} }
CThreadManager::CThreadManager() { CThreadManager::CThreadManager() {
HyprCtl::startHyprCtlSocket();
m_esConfigTimer = wl_event_loop_add_timer(g_pCompositor->m_sWLEventLoop, handleTimer, this); m_esConfigTimer = wl_event_loop_add_timer(g_pCompositor->m_sWLEventLoop, handleTimer, this);
wl_event_source_timer_update(m_esConfigTimer, 1000); wl_event_source_timer_update(m_esConfigTimer, 1000);

View file

@ -50,9 +50,9 @@ APICALL bool HyprlandAPI::unregisterCallback(HANDLE handle, HOOK_CALLBACK_FN* fn
APICALL std::string HyprlandAPI::invokeHyprctlCommand(const std::string& call, const std::string& args, const std::string& format) { APICALL std::string HyprlandAPI::invokeHyprctlCommand(const std::string& call, const std::string& args, const std::string& format) {
if (args.empty()) if (args.empty())
return HyprCtl::makeDynamicCall(format + "/" + call); return g_pHyprCtl->makeDynamicCall(format + "/" + call);
else else
return HyprCtl::makeDynamicCall(format + "/" + call + " " + args); return g_pHyprCtl->makeDynamicCall(format + "/" + call + " " + args);
} }
APICALL bool HyprlandAPI::addLayout(HANDLE handle, const std::string& name, IHyprLayout* layout) { APICALL bool HyprlandAPI::addLayout(HANDLE handle, const std::string& name, IHyprLayout* layout) {