From 095c242fcfe3633d7e78236f641a7840c2e12c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Thu, 5 Mar 2026 16:15:10 +0100 Subject: [PATCH] Refactoring --- src/HyprlandService.cpp | 77 +++++++++++++++++++---------------------- src/ShellService.cpp | 27 ++++++++------- 2 files changed, 50 insertions(+), 54 deletions(-) diff --git a/src/HyprlandService.cpp b/src/HyprlandService.cpp index 4764e5c..352b835 100644 --- a/src/HyprlandService.cpp +++ b/src/HyprlandService.cpp @@ -20,9 +20,7 @@ void HyprlandService::setConfigFilePath(std::string path) { configFilePath = path; }; -std::string HyprlandService::getConfigFilePath() { - return configFilePath; -}; +std::string HyprlandService::getConfigFilePath() { return configFilePath; }; std::list HyprlandService::getWorkspaces() { json j = json::parse(ShellService::exec(HYPRCTL_BINARY " workspaces -j")); @@ -31,10 +29,10 @@ std::list HyprlandService::getWorkspaces() { std::optional HyprlandService::getWorkspace(int id) { std::list workspaces = getWorkspaces(); - - for (auto it = workspaces.begin(); it != workspaces.end(); ) { + + for (auto it = workspaces.begin(); it != workspaces.end();) { if (it->id == id) { - return *it; + return *it; } else { ++it; } @@ -44,7 +42,8 @@ std::optional HyprlandService::getWorkspace(int id) { } Workspace HyprlandService::getCurrentWorkspace() { - json j = json::parse(ShellService::exec(HYPRCTL_BINARY " activeworkspace -j")); + json j = + json::parse(ShellService::exec(HYPRCTL_BINARY " activeworkspace -j")); return j.get(); }; @@ -62,7 +61,7 @@ std::list HyprlandService::getClientsOnActiveWorkspace() { std::list clients = getClients(); int activeWorkspaceID = getCurrentWorkspace().id; - for (auto it = clients.begin(); it != clients.end(); ) { + for (auto it = clients.begin(); it != clients.end();) { if (it->workspace.id != activeWorkspaceID) { it = clients.erase(it); } else { @@ -76,7 +75,7 @@ std::list HyprlandService::getClientsOnActiveWorkspace() { std::list HyprlandService::getWindowRules() { std::list rules; - for (auto& line : FileService::readLines(getConfigFilePath())) { + for (auto &line : FileService::readLines(getConfigFilePath())) { rules.push_back(WindowRule::parse(line)); } @@ -84,35 +83,34 @@ std::list HyprlandService::getWindowRules() { }; void HyprlandService::setClientFloating(Client c) { - ShellService::exec(HYPRCTL_BINARY " dispatch setfloating address:" + c.address); + ShellService::exec(HYPRCTL_BINARY " dispatch setfloating address:" + + c.address); }; void HyprlandService::setClientTiled(Client c) { ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address); } -//on = true -> creates a window rule to ENABLE floating mode for currently active workspace -//on = false -> creates a window rule to DISABLE floating mode for currently active workspace +// on = true -> creates a window rule to ENABLE floating mode for currently +// active workspace on = false -> creates a window rule to DISABLE floating mode +// for currently active workspace void HyprlandService::setFloatingRule(bool on) { - WindowRule rule {.tile = !on, .workspaceID = getCurrentWorkspace().id}; + WindowRule rule{.tile = !on, .workspaceID = getCurrentWorkspace().id}; auto conflictingRule = findConflictingRule(rule); if (conflictingRule.has_value()) { removeRule(conflictingRule.value()); } - FileService::appendToFile( - getConfigFilePath(), - rule.toString() - ); + FileService::appendToFile(getConfigFilePath(), rule.toString()); }; -std::optional HyprlandService::findConflictingRule(WindowRule subject) { +std::optional +HyprlandService::findConflictingRule(WindowRule subject) { std::list rules = getWindowRules(); int id = getCurrentWorkspace().id; - for (auto& rule : rules) { - if (rule.tile == !subject.tile && rule.workspaceID == subject.workspaceID) - { + for (auto &rule : rules) { + if (rule.tile == !subject.tile && rule.workspaceID == subject.workspaceID) { return rule; } } @@ -125,9 +123,8 @@ void HyprlandService::removeRule(WindowRule rule) { int index = 0; int foundIndex = -1; - for (auto& it : rules) { - if (it.toString() == rule.toString()) - { + for (auto &it : rules) { + if (it.toString() == rule.toString()) { foundIndex = index; break; } @@ -135,19 +132,16 @@ void HyprlandService::removeRule(WindowRule rule) { } if (foundIndex != -1) { - FileService::deleteNthLine( - getConfigFilePath(), - foundIndex - ); + FileService::deleteNthLine(getConfigFilePath(), foundIndex); } - //else: rule not found, do nothing + // else: rule not found, do nothing } bool HyprlandService::isFloatingRulePresent(int workspaceId) { std::list rules = getWindowRules(); - for (auto& rule : rules) { + for (auto &rule : rules) { if (rule.workspaceID == workspaceId && rule.tile == false) { return true; } @@ -164,35 +158,36 @@ WindowRule HyprlandService::getActiveWorkspaceRule() { std::list rules = getWindowRules(); int id = getCurrentWorkspace().id; - for (auto& rule : rules) { + for (auto &rule : rules) { if (rule.workspaceID == id) { return rule; } } - //If no rule is found, return a default rule (tiled) - return WindowRule {.tile = true, .workspaceID = id}; + // If no rule is found, return a default rule (tiled) + return WindowRule{.tile = true, .workspaceID = id}; }; void HyprlandService::moveToWorkspace(int workspaceId) { if (isFloatingRulePresent(workspaceId)) { - setClientFloating(getActiveClient()); + setClientFloating(getActiveClient()); } else { - setClientTiled(getActiveClient()); + setClientTiled(getActiveClient()); } - - ShellService::exec(HYPRCTL_BINARY " dispatch movetoworkspace " + std::to_string(workspaceId)); + + ShellService::exec(HYPRCTL_BINARY " dispatch movetoworkspace " + + std::to_string(workspaceId)); } void HyprlandService::toggleFloating() { if (isFloatingRulePresent(getCurrentWorkspace())) { - for (auto& c : getClientsOnActiveWorkspace()) { - setClientTiled(c); + for (auto &c : getClientsOnActiveWorkspace()) { + setClientTiled(c); } setFloatingRule(false); } else { - for (auto& c : getClientsOnActiveWorkspace()) { - setClientFloating(c); + for (auto &c : getClientsOnActiveWorkspace()) { + setClientFloating(c); } setFloatingRule(true); } diff --git a/src/ShellService.cpp b/src/ShellService.cpp index 5c25413..552454c 100644 --- a/src/ShellService.cpp +++ b/src/ShellService.cpp @@ -2,41 +2,42 @@ #include #include -std::string ShellService::exec(const std::string& command) { +std::string ShellService::exec(const std::string &command) { char buffer[128]; std::string result = ""; - FILE* pipe = popen(command.c_str(), "r"); + FILE *pipe = popen(command.c_str(), "r"); if (!pipe) { - return "popen failed"; + return "popen failed"; } while (fgets(buffer, sizeof(buffer), pipe) != NULL) { - result += buffer; + result += buffer; } int status = pclose(pipe); if (status == -1) { - return "Error closing pipe"; + return "Error closing pipe"; } return result; }; -std::string ShellService::getHomePath() -{ - char* home = getenv("HOME"); +std::string ShellService::getHomePath() { + char *home = getenv("HOME"); if (home && *home) { - return std::string(home) + std::string("/"); + return std::string(home) + std::string("/"); } // Fallback: try to get home directory from passwd if HOME is not set - // This assumes that the 'htt' process is ran with the user as the process owner. Should it be run with systemd or some other wonky method, it will behave unexpectedly - struct passwd* pw = getpwuid(getuid()); + // This assumes that the 'htt' process is ran with the user as the process + // owner. Should it be run with systemd or some other wonky method, it will + // behave unexpectedly + struct passwd *pw = getpwuid(getuid()); if (pw && pw->pw_dir) { - return std::string(pw->pw_dir); + return std::string(pw->pw_dir); } // As a last resort, exit exit(1); -} \ No newline at end of file +}