Refactoring

This commit is contained in:
2026-03-05 16:15:10 +01:00
parent c2c695ff45
commit 095c242fcf
2 changed files with 50 additions and 54 deletions

View File

@@ -20,9 +20,7 @@ void HyprlandService::setConfigFilePath(std::string path) {
configFilePath = path; configFilePath = path;
}; };
std::string HyprlandService::getConfigFilePath() { std::string HyprlandService::getConfigFilePath() { return configFilePath; };
return configFilePath;
};
std::list<Workspace> HyprlandService::getWorkspaces() { std::list<Workspace> HyprlandService::getWorkspaces() {
json j = json::parse(ShellService::exec(HYPRCTL_BINARY " workspaces -j")); json j = json::parse(ShellService::exec(HYPRCTL_BINARY " workspaces -j"));
@@ -32,9 +30,9 @@ std::list<Workspace> HyprlandService::getWorkspaces() {
std::optional<Workspace> HyprlandService::getWorkspace(int id) { std::optional<Workspace> HyprlandService::getWorkspace(int id) {
std::list<Workspace> workspaces = getWorkspaces(); std::list<Workspace> workspaces = getWorkspaces();
for (auto it = workspaces.begin(); it != workspaces.end(); ) { for (auto it = workspaces.begin(); it != workspaces.end();) {
if (it->id == id) { if (it->id == id) {
return *it; return *it;
} else { } else {
++it; ++it;
} }
@@ -44,7 +42,8 @@ std::optional<Workspace> HyprlandService::getWorkspace(int id) {
} }
Workspace HyprlandService::getCurrentWorkspace() { 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<Workspace>(); return j.get<Workspace>();
}; };
@@ -62,7 +61,7 @@ std::list<Client> HyprlandService::getClientsOnActiveWorkspace() {
std::list<Client> clients = getClients(); std::list<Client> clients = getClients();
int activeWorkspaceID = getCurrentWorkspace().id; 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) { if (it->workspace.id != activeWorkspaceID) {
it = clients.erase(it); it = clients.erase(it);
} else { } else {
@@ -76,7 +75,7 @@ std::list<Client> HyprlandService::getClientsOnActiveWorkspace() {
std::list<WindowRule> HyprlandService::getWindowRules() { std::list<WindowRule> HyprlandService::getWindowRules() {
std::list<WindowRule> rules; std::list<WindowRule> rules;
for (auto& line : FileService::readLines(getConfigFilePath())) { for (auto &line : FileService::readLines(getConfigFilePath())) {
rules.push_back(WindowRule::parse(line)); rules.push_back(WindowRule::parse(line));
} }
@@ -84,35 +83,34 @@ std::list<WindowRule> HyprlandService::getWindowRules() {
}; };
void HyprlandService::setClientFloating(Client c) { 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) { void HyprlandService::setClientTiled(Client c) {
ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address); ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address);
} }
//on = true -> creates a window rule to ENABLE floating mode for currently active workspace // on = true -> creates a window rule to ENABLE floating mode for currently
//on = false -> creates a window rule to DISABLE floating mode for currently active workspace // active workspace on = false -> creates a window rule to DISABLE floating mode
// for currently active workspace
void HyprlandService::setFloatingRule(bool on) { void HyprlandService::setFloatingRule(bool on) {
WindowRule rule {.tile = !on, .workspaceID = getCurrentWorkspace().id}; WindowRule rule{.tile = !on, .workspaceID = getCurrentWorkspace().id};
auto conflictingRule = findConflictingRule(rule); auto conflictingRule = findConflictingRule(rule);
if (conflictingRule.has_value()) { if (conflictingRule.has_value()) {
removeRule(conflictingRule.value()); removeRule(conflictingRule.value());
} }
FileService::appendToFile( FileService::appendToFile(getConfigFilePath(), rule.toString());
getConfigFilePath(),
rule.toString()
);
}; };
std::optional<WindowRule> HyprlandService::findConflictingRule(WindowRule subject) { std::optional<WindowRule>
HyprlandService::findConflictingRule(WindowRule subject) {
std::list<WindowRule> rules = getWindowRules(); std::list<WindowRule> rules = getWindowRules();
int id = getCurrentWorkspace().id; int id = getCurrentWorkspace().id;
for (auto& rule : rules) { for (auto &rule : rules) {
if (rule.tile == !subject.tile && rule.workspaceID == subject.workspaceID) if (rule.tile == !subject.tile && rule.workspaceID == subject.workspaceID) {
{
return rule; return rule;
} }
} }
@@ -125,9 +123,8 @@ void HyprlandService::removeRule(WindowRule rule) {
int index = 0; int index = 0;
int foundIndex = -1; int foundIndex = -1;
for (auto& it : rules) { for (auto &it : rules) {
if (it.toString() == rule.toString()) if (it.toString() == rule.toString()) {
{
foundIndex = index; foundIndex = index;
break; break;
} }
@@ -135,19 +132,16 @@ void HyprlandService::removeRule(WindowRule rule) {
} }
if (foundIndex != -1) { if (foundIndex != -1) {
FileService::deleteNthLine( FileService::deleteNthLine(getConfigFilePath(), foundIndex);
getConfigFilePath(),
foundIndex
);
} }
//else: rule not found, do nothing // else: rule not found, do nothing
} }
bool HyprlandService::isFloatingRulePresent(int workspaceId) { bool HyprlandService::isFloatingRulePresent(int workspaceId) {
std::list<WindowRule> rules = getWindowRules(); std::list<WindowRule> rules = getWindowRules();
for (auto& rule : rules) { for (auto &rule : rules) {
if (rule.workspaceID == workspaceId && rule.tile == false) { if (rule.workspaceID == workspaceId && rule.tile == false) {
return true; return true;
} }
@@ -164,35 +158,36 @@ WindowRule HyprlandService::getActiveWorkspaceRule() {
std::list<WindowRule> rules = getWindowRules(); std::list<WindowRule> rules = getWindowRules();
int id = getCurrentWorkspace().id; int id = getCurrentWorkspace().id;
for (auto& rule : rules) { for (auto &rule : rules) {
if (rule.workspaceID == id) { if (rule.workspaceID == id) {
return rule; return rule;
} }
} }
//If no rule is found, return a default rule (tiled) // If no rule is found, return a default rule (tiled)
return WindowRule {.tile = true, .workspaceID = id}; return WindowRule{.tile = true, .workspaceID = id};
}; };
void HyprlandService::moveToWorkspace(int workspaceId) { void HyprlandService::moveToWorkspace(int workspaceId) {
if (isFloatingRulePresent(workspaceId)) { if (isFloatingRulePresent(workspaceId)) {
setClientFloating(getActiveClient()); setClientFloating(getActiveClient());
} else { } 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() { void HyprlandService::toggleFloating() {
if (isFloatingRulePresent(getCurrentWorkspace())) { if (isFloatingRulePresent(getCurrentWorkspace())) {
for (auto& c : getClientsOnActiveWorkspace()) { for (auto &c : getClientsOnActiveWorkspace()) {
setClientTiled(c); setClientTiled(c);
} }
setFloatingRule(false); setFloatingRule(false);
} else { } else {
for (auto& c : getClientsOnActiveWorkspace()) { for (auto &c : getClientsOnActiveWorkspace()) {
setClientFloating(c); setClientFloating(c);
} }
setFloatingRule(true); setFloatingRule(true);
} }

View File

@@ -2,39 +2,40 @@
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
std::string ShellService::exec(const std::string& command) { std::string ShellService::exec(const std::string &command) {
char buffer[128]; char buffer[128];
std::string result = ""; std::string result = "";
FILE* pipe = popen(command.c_str(), "r"); FILE *pipe = popen(command.c_str(), "r");
if (!pipe) { if (!pipe) {
return "popen failed"; return "popen failed";
} }
while (fgets(buffer, sizeof(buffer), pipe) != NULL) { while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
result += buffer; result += buffer;
} }
int status = pclose(pipe); int status = pclose(pipe);
if (status == -1) { if (status == -1) {
return "Error closing pipe"; return "Error closing pipe";
} }
return result; return result;
}; };
std::string ShellService::getHomePath() std::string ShellService::getHomePath() {
{ char *home = getenv("HOME");
char* home = getenv("HOME");
if (home && *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 // 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 // This assumes that the 'htt' process is ran with the user as the process
struct passwd* pw = getpwuid(getuid()); // 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) { if (pw && pw->pw_dir) {
return std::string(pw->pw_dir); return std::string(pw->pw_dir);
} }
// As a last resort, exit // As a last resort, exit