2025-06-10 16:46:58 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2025-06-10 14:16:44 +02:00
|
|
|
#include "../include/HyprlandService.h"
|
2025-06-10 16:46:58 +02:00
|
|
|
#include "../include/ShellService.h"
|
|
|
|
|
#include "../include/Macros.h"
|
|
|
|
|
#include "../include/FileService.h"
|
2025-06-10 14:09:56 +02:00
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
|
|
Workspace HyprlandService::getCurrentWorkspace() {
|
2025-06-10 16:46:58 +02:00
|
|
|
json j = json::parse(ShellService::exec(HYPRCTL_BINARY " activeworkspace -j"));
|
2025-06-10 14:09:56 +02:00
|
|
|
return j.get<Workspace>();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::list<Client> HyprlandService::getClients() {
|
2025-06-10 16:46:58 +02:00
|
|
|
json j = json::parse(ShellService::exec(HYPRCTL_BINARY " clients -j"));
|
2025-06-10 14:09:56 +02:00
|
|
|
return j.get<std::list<Client>>();
|
|
|
|
|
};
|
2025-06-10 14:55:42 +02:00
|
|
|
|
|
|
|
|
std::list<Client> HyprlandService::getClientsOnActiveWorkspace() {
|
2025-06-11 11:48:31 +02:00
|
|
|
std::list<Client> clients = getClients();
|
|
|
|
|
int activeWorkspaceID = getCurrentWorkspace().id;
|
2025-06-10 14:55:42 +02:00
|
|
|
|
|
|
|
|
for (auto it = clients.begin(); it != clients.end(); ) {
|
|
|
|
|
if (it->workspace.id != activeWorkspaceID) {
|
|
|
|
|
it = clients.erase(it);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clients;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-11 11:48:31 +02:00
|
|
|
std::list<WindowRule> HyprlandService::getWindowRules() {
|
|
|
|
|
std::list<WindowRule> rules;
|
|
|
|
|
|
|
|
|
|
for (auto& line : FileService::readLines(ShellService::getHomePath()+FLOATING_RULE_CONF_FILE)) {
|
|
|
|
|
rules.push_back(WindowRule::parse(line));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rules;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-10 14:55:42 +02:00
|
|
|
void HyprlandService::setClientFloating(Client& c) {
|
2025-06-10 16:46:58 +02:00
|
|
|
ShellService::exec(HYPRCTL_BINARY " dispatch setfloating address:" + c.address);
|
2025-06-10 14:55:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void HyprlandService::setClientTiled(Client& c) {
|
2025-06-10 16:46:58 +02:00
|
|
|
ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address);
|
2025-06-10 14:55:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HyprlandService::toggleClientFloating(Client& c) {
|
2025-06-10 16:46:58 +02:00
|
|
|
ShellService::exec(HYPRCTL_BINARY " dispatch togglefloating address:" + c.address);
|
2025-06-10 15:45:25 +02:00
|
|
|
};
|
|
|
|
|
|
2025-06-11 11:48:31 +02:00
|
|
|
//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};
|
|
|
|
|
auto conflictingRule = findConflictingRule(rule);
|
|
|
|
|
if (conflictingRule.has_value()) {
|
|
|
|
|
removeRule(conflictingRule.value());
|
2025-06-10 15:45:25 +02:00
|
|
|
}
|
2025-06-11 11:48:31 +02:00
|
|
|
|
|
|
|
|
FileService::appendToFile(
|
|
|
|
|
ShellService::getHomePath() + FLOATING_RULE_CONF_FILE,
|
|
|
|
|
rule.toString()
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
return rule;
|
|
|
|
|
}
|
2025-06-10 15:45:25 +02:00
|
|
|
}
|
2025-06-11 11:48:31 +02:00
|
|
|
|
|
|
|
|
return std::nullopt;
|
2025-06-10 16:45:58 +02:00
|
|
|
};
|
|
|
|
|
|
2025-06-11 11:48:31 +02:00
|
|
|
void HyprlandService::removeRule(WindowRule rule) {
|
|
|
|
|
std::list<WindowRule> rules = getWindowRules();
|
|
|
|
|
int index = 0;
|
|
|
|
|
int foundIndex = -1;
|
|
|
|
|
|
|
|
|
|
for (auto& it : rules) {
|
|
|
|
|
if (it.toString() == rule.toString())
|
|
|
|
|
{
|
|
|
|
|
foundIndex = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (foundIndex != -1) {
|
|
|
|
|
FileService::deleteNthLine(
|
|
|
|
|
ShellService::getHomePath() + FLOATING_RULE_CONF_FILE,
|
|
|
|
|
foundIndex
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//else: rule not found, do nothing
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 16:45:58 +02:00
|
|
|
bool HyprlandService::isFloatingRulePresent() {
|
2025-06-11 11:48:31 +02:00
|
|
|
//Checks if there's a valid window rule in place that enables floating mode for the currently active workspace
|
|
|
|
|
std::list<WindowRule> rules = getWindowRules();
|
|
|
|
|
int id = getCurrentWorkspace().id;
|
|
|
|
|
|
|
|
|
|
for (auto& rule : rules) {
|
|
|
|
|
if (rule.workspaceID == id && rule.tile == false) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2025-06-10 14:55:42 +02:00
|
|
|
};
|