add workspace indicators

This commit is contained in:
2025-12-09 22:53:29 +01:00
parent 25c73f67a7
commit d53dfa27f1
8 changed files with 501 additions and 132 deletions

View File

@@ -2,38 +2,52 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <nlohmann/json.hpp>
#include <ostream>
#include <stdexcept>
#include <string>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <iostream>
#include "helpers/systemHelper.hpp"
HyprlandService::HyprlandService() {}
namespace
{
const char *kMonitorCommand = "hyprctl monitors -j";
const char *kWorkspaceCommand = "hyprctl workspaces -j";
bool is_workspace_event(const std::string &event)
{
return event.find("workspace") != std::string::npos;
}
}
HyprlandService::HyprlandService() = default;
HyprlandService::~HyprlandService()
{
if (m_fd != -1)
{
close(m_fd);
m_fd = -1;
}
}
void HyprlandService::on_hyprland_event(std::string event, std::string data)
void HyprlandService::on_hyprland_event(std::string event, std::string /*data*/)
{
if (event == "workspacev2")
if (is_workspace_event(event) || event == "focusedmon" || event == "monitoradded" || event == "monitorremoved")
{
std::cout << event << " " << data << std::endl;
refresh_monitors();
refresh_workspaces();
}
}
void HyprlandService::start()
{
// setup socket
std::string socket_path = get_socket_path();
const std::string socket_path = get_socket_path();
if (socket_path.empty())
{
return;
@@ -49,9 +63,9 @@ void HyprlandService::start()
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1);
std::strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1);
if (connect(m_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
if (connect(m_fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) == -1)
{
std::cerr << "[Hyprland] Failed to connect to " << socket_path << std::endl;
close(m_fd);
@@ -66,63 +80,13 @@ void HyprlandService::start()
m_fd,
Glib::IOCondition::IO_IN | Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR);
// setup monitors
std::string hyprctlMonitorsOutput = SystemHelper::get_command_output("hyprctl monitors -j");
auto monitorsJson = nlohmann::json::parse(hyprctlMonitorsOutput);
if (monitorsJson.is_array())
{
for (auto &monitorJson : monitorsJson)
{
Monitor* monitor = new Monitor();
monitor->id = monitorJson["id"];
monitor->name = monitorJson["name"];
monitor->x = monitorJson["x"];
monitor->y = monitorJson["y"];
monitor->focusedWorkspaceId = monitorJson["activeWorkspace"]["id"];
this->monitors.insert({monitor->id, monitor});
}
}
int numWorkspaces = 6;
int numMonitors = 2;
for (int i = 0; i < numWorkspaces * numMonitors; i++)
{
WorkspaceState tempState;
tempState.active = false;
tempState.focused = false;
tempState.urgent = false;
tempState.id = i;
this->monitors[i / numWorkspaces]->workspaceStates.insert({i % numWorkspaces, tempState});
}
std::string hyprctlWorkspacesOutput = SystemHelper::get_command_output("hyprctl workspaces -j");
auto workspacesJson = nlohmann::json::parse(hyprctlWorkspacesOutput);
if (workspacesJson.is_array())
{
for (auto &workspaceJson : workspacesJson)
{
int currentId = workspaceJson["id"];
int monitorId = workspaceJson["monitorID"];
Monitor* monitor = this->monitors[monitorId];
WorkspaceState &workspaceState = monitor->workspaceStates.at((currentId - 1) % numWorkspaces);
int focusedWorkspaceId = monitor[monitorId].focusedWorkspaceId;
workspaceState.focused = currentId == focusedWorkspaceId;
workspaceState.active = true;
}
}
refresh_monitors();
refresh_workspaces();
}
bool HyprlandService::on_socket_read(Glib::IOCondition condition)
{
auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
const auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
if (static_cast<int>(condition & error_mask) != 0)
{
@@ -133,7 +97,7 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition)
}
char buffer[4096];
ssize_t bytes_read = read(m_fd, buffer, sizeof(buffer) - 1);
const ssize_t bytes_read = read(m_fd, buffer, sizeof(buffer) - 1);
if (bytes_read > 0)
{
@@ -144,7 +108,7 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition)
while ((pos = m_buffer.find('\n')) != std::string::npos)
{
std::string line = m_buffer.substr(0, pos);
const std::string line = m_buffer.substr(0, pos);
parse_message(line);
m_buffer.erase(0, pos + 1);
}
@@ -155,11 +119,11 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition)
void HyprlandService::parse_message(const std::string &line)
{
size_t split = line.find(">>");
const size_t split = line.find(">>");
if (split != std::string::npos)
{
std::string event_name = line.substr(0, split);
std::string event_data = line.substr(split + 2);
const std::string event_name = line.substr(0, split);
const std::string event_data = line.substr(split + 2);
socketEventSignal.emit(event_name, event_data);
}
@@ -179,15 +143,206 @@ std::string HyprlandService::get_socket_path()
return std::string(runtime) + "/hypr/" + sig + "/.socket2.sock";
}
HyprlandService::Monitor* HyprlandService::getMonitorById(const int &id)
void HyprlandService::refresh_monitors()
{
if (this->monitors.find(id) != this->monitors.end())
std::string output;
try
{
return this->monitors.at(id);
output = SystemHelper::get_command_output(kMonitorCommand);
}
else
catch (const std::exception &ex)
{
std::cerr << "[Hyprland] Failed to query monitors: " << ex.what() << std::endl;
return;
}
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
if (!monitorsJson.is_array())
{
std::cerr << "[Hyprland] Unexpected monitor payload" << std::endl;
return;
}
std::map<int, Monitor> updated;
for (const auto &monitorJson : monitorsJson)
{
if (!monitorJson.is_object())
{
continue;
}
Monitor monitor;
monitor.id = monitorJson.value("id", -1);
monitor.name = monitorJson.value("name", "");
monitor.x = monitorJson.value("x", 0);
monitor.y = monitorJson.value("y", 0);
if (monitorJson.contains("activeWorkspace") && monitorJson["activeWorkspace"].is_object())
{
monitor.focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
}
if (monitor.id >= 0)
{
updated.emplace(monitor.id, std::move(monitor));
}
}
m_monitors.swap(updated);
monitorStateChanged.emit();
}
void HyprlandService::refresh_workspaces()
{
if (m_monitors.empty())
{
return;
}
std::string output;
try
{
output = SystemHelper::get_command_output(kWorkspaceCommand);
}
catch (const std::exception &ex)
{
std::cerr << "[Hyprland] Failed to query workspaces: " << ex.what() << std::endl;
return;
}
auto workspacesJson = nlohmann::json::parse(output, nullptr, false);
if (!workspacesJson.is_array())
{
std::cerr << "[Hyprland] Unexpected workspace payload" << std::endl;
return;
}
for (auto &pair : m_monitors)
{
auto &monitor = pair.second;
monitor.workspaceStates.clear();
int focusedSlot = -1;
if (monitor.focusedWorkspaceId > 0)
{
focusedSlot = ((monitor.focusedWorkspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
}
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot)
{
WorkspaceState state;
state.id = slot;
state.hyprId = -1;
state.label = std::to_string(slot);
state.focused = (slot == focusedSlot);
state.active = state.focused;
state.urgent = false;
monitor.workspaceStates.emplace(slot, state);
}
}
for (const auto &workspaceJson : workspacesJson)
{
if (!workspaceJson.is_object())
{
continue;
}
const int monitorId = workspaceJson.value("monitorID", -1);
const int workspaceId = workspaceJson.value("id", -1);
auto monitorIt = m_monitors.find(monitorId);
if (monitorIt == m_monitors.end() || workspaceId < 0)
{
continue;
}
auto &monitor = monitorIt->second;
const int slot = ((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
auto &workspaceState = monitor.workspaceStates[slot];
workspaceState.id = slot;
workspaceState.hyprId = workspaceId;
workspaceState.focused = (monitor.focusedWorkspaceId == workspaceId);
workspaceState.active = workspaceState.focused || workspaceJson.value("windows", 0) > 0;
workspaceState.urgent = false;
std::string labelCandidate;
if (workspaceJson.contains("name") && workspaceJson["name"].is_string())
{
labelCandidate = workspaceJson["name"].get<std::string>();
}
if (labelCandidate.empty() || labelCandidate == std::to_string(workspaceId))
{
workspaceState.label = std::to_string(slot);
}
else
{
workspaceState.label = labelCandidate;
}
if (workspaceJson.contains("urgent") && workspaceJson["urgent"].is_boolean())
{
workspaceState.urgent = workspaceJson["urgent"].get<bool>();
}
else if (workspaceJson.contains("hasurgent") && workspaceJson["hasurgent"].is_boolean())
{
workspaceState.urgent = workspaceJson["hasurgent"].get<bool>();
}
}
for (const auto &pair : m_monitors)
{
workspaceStateChanged.emit(pair.first);
}
}
HyprlandService::Monitor *HyprlandService::getMonitorById(int id)
{
auto it = m_monitors.find(id);
if (it == m_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 = m_monitors.find(id);
if (it == m_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 >= m_monitors.size())
{
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
}
auto it = m_monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}
const HyprlandService::Monitor *HyprlandService::getMonitorByIndex(std::size_t index) const
{
if (index >= m_monitors.size())
{
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
}
auto it = m_monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}