Refactoring
This commit is contained in:
@@ -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<Workspace> HyprlandService::getWorkspaces() {
|
||||
json j = json::parse(ShellService::exec(HYPRCTL_BINARY " workspaces -j"));
|
||||
@@ -31,10 +29,10 @@ std::list<Workspace> HyprlandService::getWorkspaces() {
|
||||
|
||||
std::optional<Workspace> HyprlandService::getWorkspace(int id) {
|
||||
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) {
|
||||
return *it;
|
||||
return *it;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
@@ -44,7 +42,8 @@ std::optional<Workspace> 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<Workspace>();
|
||||
};
|
||||
|
||||
@@ -62,7 +61,7 @@ std::list<Client> HyprlandService::getClientsOnActiveWorkspace() {
|
||||
std::list<Client> 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<Client> HyprlandService::getClientsOnActiveWorkspace() {
|
||||
std::list<WindowRule> HyprlandService::getWindowRules() {
|
||||
std::list<WindowRule> 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<WindowRule> 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<WindowRule> HyprlandService::findConflictingRule(WindowRule subject) {
|
||||
std::optional<WindowRule>
|
||||
HyprlandService::findConflictingRule(WindowRule subject) {
|
||||
std::list<WindowRule> 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<WindowRule> 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<WindowRule> 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);
|
||||
}
|
||||
|
||||
@@ -2,41 +2,42 @@
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user