fix urgent window icons

This commit is contained in:
2025-12-17 23:34:12 +01:00
parent 11ccd55a52
commit 9b0a036925
11 changed files with 172 additions and 194 deletions

View File

@@ -14,16 +14,6 @@
#include "helpers/systemHelper.hpp"
namespace {
const char *kMonitorCommand = "hyprctl monitors -j";
const char *kWorkspaceCommand = "hyprctl workspaces -j";
const char *kClientsCommand = "hyprctl clients -j";
bool is_workspace_event(const std::string &event) {
return event.find("workspace") != std::string::npos;
}
} // namespace
HyprlandService::HyprlandService() = default;
HyprlandService::~HyprlandService() {
@@ -39,13 +29,19 @@ void HyprlandService::on_hyprland_event(std::string event, std::string data) {
}
if (event == "urgent") {
handle_urgent_window(data);
onUrgentEvent(data);
}
if (event == "workspace" || event == "focusedmon" || event == "monitoradded" ||
event == "monitorremoved" || event == "movewindow" || event == "activewindow") {
if (event == "workspace" || event == "movewindow") {
refresh_workspaces();
}
// use for
// event == "focusedmon"
if (event == "activewindowv2") {
onActiveWindowEvent(data);
}
}
void HyprlandService::start() {
@@ -126,7 +122,7 @@ void HyprlandService::parse_message(const std::string &line) {
}
std::string HyprlandService::get_socket_path() {
const char *sig = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
const char *sig = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
const char *runtime = std::getenv("XDG_RUNTIME_DIR");
if (!sig || !runtime) {
@@ -142,16 +138,14 @@ void HyprlandService::refresh_monitors() {
this->workspaces.clear();
std::string output = SystemHelper::get_command_output(kMonitorCommand);
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) {
assert(monitorJson.is_object());
Monitor monitor;
monitor.id = monitorJson.value("id", -1);
monitor.id = monitorJson.value("id", -1);
monitor.name = monitorJson.value("name", "");
monitor.x = monitorJson.value("x", 0);
monitor.y = monitorJson.value("y", 0);
monitor.x = monitorJson.value("x", 0);
monitor.y = monitorJson.value("y", 0);
monitor.focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
@@ -161,14 +155,14 @@ void HyprlandService::refresh_monitors() {
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot) {
WorkspaceState wsState;
wsState.focused = false;
wsState.active = false;
wsState.label = std::to_string(slot);
wsState.focused = false;
wsState.active = false;
wsState.label = std::to_string(slot);
wsState.monitorId = monitor.id;
int id = slot + monitor.id * HyprlandService::kWorkspaceSlotCount;
wsState.hyprId = id;
this->workspaces[id] = new WorkspaceState(wsState);
int id = slot + monitor.id * HyprlandService::kWorkspaceSlotCount;
wsState.hyprId = id;
this->workspaces[id] = new WorkspaceState(wsState);
this->monitors[monitor.id].workspaceStates[slot] = this->workspaces[id];
}
}
@@ -182,77 +176,37 @@ void HyprlandService::refresh_monitors() {
* Used to Update the internal state for the Workspaces,
*/
void HyprlandService::refresh_workspaces() {
std::string output = SystemHelper::get_command_output(kWorkspaceCommand);
std::string output = SystemHelper::get_command_output(kWorkspaceCommand);
auto workspacesJson = nlohmann::json::parse(output, nullptr, false);
for (auto &[id, ws] : this->workspaces) {
ws->focused = false;
ws->active = false;
ws->active = false;
}
output = SystemHelper::get_command_output(kMonitorCommand);
output = SystemHelper::get_command_output(kMonitorCommand);
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) {
const int monitorId = monitorJson.value("id", -1);
const int monitorId = monitorJson.value("id", -1);
const int focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
auto monitorIt = this->monitors.find(monitorId);
monitorIt->second.focusedWorkspaceId = focusedWorkspaceId;
auto monitor = this->monitors[monitorId];
monitor.focusedWorkspaceId = focusedWorkspaceId;
}
for (const auto &workspaceJson : workspacesJson) {
const int workspaceId = workspaceJson.value("id", -1);
const int workspaceId = workspaceJson.value("id", -1);
std::map<int, WorkspaceState *>::iterator workspaceStateIt = this->workspaces.find(workspaceId);
WorkspaceState *workspaceState = workspaceStateIt->second;
WorkspaceState *workspaceState = workspaceStateIt->second;
workspaceState->focused = monitors
.at(workspaceState->monitorId)
.focusedWorkspaceId ==
workspaceId;
workspaceState->active = true;
this->workspaces[workspaceId] = workspaceState;
workspaceState->focused = monitors[workspaceState->monitorId].focusedWorkspaceId == workspaceId;
workspaceState->active = true;
}
workspaceStateChanged.emit();
}
HyprlandService::Monitor *HyprlandService::getMonitorById(int id) {
auto it = monitors.find(id);
if (it == monitors.end()) {
throw std::runtime_error("Monitor with ID " + std::to_string(id) +
" not found.");
}
return &it->second;
}
const HyprlandService::Monitor *HyprlandService::getMonitorById(int id) const {
auto it = monitors.find(id);
if (it == monitors.end()) {
throw std::runtime_error("Monitor with ID " + std::to_string(id) +
" not found.");
}
return &it->second;
}
HyprlandService::Monitor *HyprlandService::getMonitorByIndex(std::size_t index) {
if (index >= monitors.size()) {
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
}
auto it = monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}
void HyprlandService::switchToWorkspace(int workspaceId) {
std::string cmd =
"hyprctl dispatch workspace " + std::to_string(workspaceId);
@@ -265,36 +219,51 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
}
}
void HyprlandService::handle_urgent_window(std::string windowAddress) {
void HyprlandService::onUrgentEvent(std::string windowAddress) {
std::string output = SystemHelper::get_command_output(kClientsCommand);
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &clientJson : clientsJson) {
if (!clientJson.is_object()) {
continue;
}
const std::string addr = clientJson.value("address", "");
if (addr != windowAddress) {
continue;
if (addr == "0x" + windowAddress) {
int workspaceId = clientJson["workspace"].value("id", -1);
WorkspaceState *ws = this->workspaces[workspaceId];
// todo: maybee better method of access
if (ws) {
ws->urgentWindows.push_back(windowAddress);
workspaceStateChanged.emit();
}
break;
}
}
}
void HyprlandService::onActiveWindowEvent(std::string windowAddress) {
std::string output = SystemHelper::get_command_output(kClientsCommand);
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &clientJson : clientsJson) {
const std::string addr = clientJson.value("address", "");
if (addr == "0x" + windowAddress) {
int workspaceId = clientJson["workspace"]["id"];
WorkspaceState *ws = this->workspaces[workspaceId];
if (ws) {
auto it = std::find(ws->urgentWindows.begin(), ws->urgentWindows.end(), windowAddress);
if (it != ws->urgentWindows.end()) {
ws->urgentWindows.erase(it);
workspaceStateChanged.emit();
}
break;
}
}
const int workspaceId = clientJson.value("workspace", -1);
if (workspaceId < 0) {
continue;
}
auto workspaceIt = this->workspaces.find(workspaceId);
if (workspaceIt == this->workspaces.end()) {
continue;
}
WorkspaceState *wsState = workspaceIt->second;
wsState->urgentWindows.push_back(1);
break;
}
workspaceStateChanged.emit();
}
void HyprlandService::printMonitor(const Monitor &mon) const {
@@ -335,3 +304,24 @@ void HyprlandService::printMonitor(const Monitor &mon) const {
std::cout << "====================\n";
}
HyprlandService::Monitor *HyprlandService::getMonitorById(int id) {
auto it = monitors.find(id);
if (it == monitors.end()) {
throw std::runtime_error("Monitor with ID " + std::to_string(id) +
" not found.");
}
return &it->second;
}
HyprlandService::Monitor *HyprlandService::getMonitorByIndex(std::size_t index) {
if (index >= monitors.size()) {
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
}
auto it = monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}