fix code style
This commit is contained in:
@@ -14,48 +14,41 @@
|
||||
|
||||
#include "helpers/systemHelper.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
const char *kMonitorCommand = "hyprctl monitors -j";
|
||||
namespace {
|
||||
const char *kMonitorCommand = "hyprctl monitors -j";
|
||||
const char *kWorkspaceCommand = "hyprctl workspaces -j";
|
||||
|
||||
bool is_workspace_event(const std::string &event)
|
||||
{
|
||||
bool is_workspace_event(const std::string &event) {
|
||||
return event.find("workspace") != std::string::npos;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
HyprlandService::HyprlandService() = default;
|
||||
|
||||
HyprlandService::~HyprlandService()
|
||||
{
|
||||
if (m_fd != -1)
|
||||
{
|
||||
close(m_fd);
|
||||
m_fd = -1;
|
||||
HyprlandService::~HyprlandService() {
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HyprlandService::on_hyprland_event(std::string event, std::string /*data*/)
|
||||
{
|
||||
if (is_workspace_event(event) || event == "focusedmon" || event == "monitoradded" || event == "monitorremoved")
|
||||
{
|
||||
void HyprlandService::on_hyprland_event(std::string event,
|
||||
std::string /*data*/) {
|
||||
if (is_workspace_event(event) || event == "focusedmon" ||
|
||||
event == "monitoradded" || event == "monitorremoved") {
|
||||
refresh_monitors();
|
||||
refresh_workspaces();
|
||||
}
|
||||
}
|
||||
|
||||
void HyprlandService::start()
|
||||
{
|
||||
void HyprlandService::start() {
|
||||
const std::string socket_path = get_socket_path();
|
||||
if (socket_path.empty())
|
||||
{
|
||||
if (socket_path.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (m_fd == -1)
|
||||
{
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd == -1) {
|
||||
std::cerr << "[Hyprland] Failed to create socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
@@ -65,63 +58,59 @@ void HyprlandService::start()
|
||||
addr.sun_family = AF_UNIX;
|
||||
std::strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 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);
|
||||
m_fd = -1;
|
||||
if (connect(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) ==
|
||||
-1) {
|
||||
std::cerr << "[Hyprland] Failed to connect to " << socket_path
|
||||
<< std::endl;
|
||||
close(fd);
|
||||
fd = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "[Hyprland] Connected to event socket." << std::endl;
|
||||
|
||||
Glib::signal_io().connect(
|
||||
sigc::mem_fun(*this, &HyprlandService::on_socket_read),
|
||||
m_fd,
|
||||
Glib::IOCondition::IO_IN | Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR);
|
||||
sigc::mem_fun(*this, &HyprlandService::on_socket_read), fd,
|
||||
Glib::IOCondition::IO_IN | Glib::IOCondition::IO_HUP |
|
||||
Glib::IOCondition::IO_ERR);
|
||||
|
||||
refresh_monitors();
|
||||
refresh_workspaces();
|
||||
}
|
||||
|
||||
bool HyprlandService::on_socket_read(Glib::IOCondition condition)
|
||||
{
|
||||
const auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
|
||||
bool HyprlandService::on_socket_read(Glib::IOCondition condition) {
|
||||
const auto error_mask =
|
||||
Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
|
||||
|
||||
if (static_cast<int>(condition & error_mask) != 0)
|
||||
{
|
||||
if (static_cast<int>(condition & error_mask) != 0) {
|
||||
std::cerr << "[Hyprland] Socket disconnected." << std::endl;
|
||||
close(m_fd);
|
||||
m_fd = -1;
|
||||
close(fd);
|
||||
fd = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[4096];
|
||||
const ssize_t bytes_read = read(m_fd, buffer, sizeof(buffer) - 1);
|
||||
const ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
|
||||
|
||||
if (bytes_read > 0)
|
||||
{
|
||||
if (bytes_read > 0) {
|
||||
buffer[bytes_read] = '\0';
|
||||
m_buffer.append(buffer);
|
||||
this->buffer.append(buffer);
|
||||
|
||||
size_t pos = 0;
|
||||
|
||||
while ((pos = m_buffer.find('\n')) != std::string::npos)
|
||||
{
|
||||
const std::string line = m_buffer.substr(0, pos);
|
||||
while ((pos = this->buffer.find('\n')) != std::string::npos) {
|
||||
const std::string line = this->buffer.substr(0, pos);
|
||||
parse_message(line);
|
||||
m_buffer.erase(0, pos + 1);
|
||||
this->buffer.erase(0, pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HyprlandService::parse_message(const std::string &line)
|
||||
{
|
||||
void HyprlandService::parse_message(const std::string &line) {
|
||||
const size_t split = line.find(">>");
|
||||
if (split != std::string::npos)
|
||||
{
|
||||
if (split != std::string::npos) {
|
||||
const std::string event_name = line.substr(0, split);
|
||||
const std::string event_data = line.substr(split + 2);
|
||||
|
||||
@@ -129,13 +118,11 @@ void HyprlandService::parse_message(const std::string &line)
|
||||
}
|
||||
}
|
||||
|
||||
std::string HyprlandService::get_socket_path()
|
||||
{
|
||||
const char *sig = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
||||
std::string HyprlandService::get_socket_path() {
|
||||
const char *sig = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
||||
const char *runtime = std::getenv("XDG_RUNTIME_DIR");
|
||||
|
||||
if (!sig || !runtime)
|
||||
{
|
||||
if (!sig || !runtime) {
|
||||
std::cerr << "[Hyprland] Environment variables missing!" << std::endl;
|
||||
return "";
|
||||
}
|
||||
@@ -143,232 +130,200 @@ std::string HyprlandService::get_socket_path()
|
||||
return std::string(runtime) + "/hypr/" + sig + "/.socket2.sock";
|
||||
}
|
||||
|
||||
void HyprlandService::refresh_monitors()
|
||||
{
|
||||
void HyprlandService::refresh_monitors() {
|
||||
std::string output;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
output = SystemHelper::get_command_output(kMonitorCommand);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
std::cerr << "[Hyprland] Failed to query monitors: " << ex.what() << std::endl;
|
||||
} 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())
|
||||
{
|
||||
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())
|
||||
{
|
||||
for (const auto &monitorJson : monitorsJson) {
|
||||
if (!monitorJson.is_object()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (monitorJson.contains("activeWorkspace") && monitorJson["activeWorkspace"].is_object())
|
||||
{
|
||||
monitor.focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
|
||||
if (monitorJson.contains("activeWorkspace") &&
|
||||
monitorJson["activeWorkspace"].is_object()) {
|
||||
monitor.focusedWorkspaceId =
|
||||
monitorJson["activeWorkspace"].value("id", -1);
|
||||
}
|
||||
|
||||
if (monitor.id >= 0)
|
||||
{
|
||||
if (monitor.id >= 0) {
|
||||
updated.emplace(monitor.id, std::move(monitor));
|
||||
}
|
||||
}
|
||||
|
||||
m_monitors.swap(updated);
|
||||
monitors.swap(updated);
|
||||
monitorStateChanged.emit();
|
||||
}
|
||||
|
||||
void HyprlandService::refresh_workspaces()
|
||||
{
|
||||
if (m_monitors.empty())
|
||||
{
|
||||
void HyprlandService::refresh_workspaces() {
|
||||
if (monitors.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string output;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
output = SystemHelper::get_command_output(kWorkspaceCommand);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
std::cerr << "[Hyprland] Failed to query workspaces: " << ex.what() << std::endl;
|
||||
} 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())
|
||||
{
|
||||
if (!workspacesJson.is_array()) {
|
||||
std::cerr << "[Hyprland] Unexpected workspace payload" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &pair : m_monitors)
|
||||
{
|
||||
for (auto &pair : monitors) {
|
||||
auto &monitor = pair.second;
|
||||
monitor.workspaceStates.clear();
|
||||
|
||||
int focusedSlot = -1;
|
||||
if (monitor.focusedWorkspaceId > 0)
|
||||
{
|
||||
focusedSlot = ((monitor.focusedWorkspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
|
||||
if (monitor.focusedWorkspaceId > 0) {
|
||||
focusedSlot = ((monitor.focusedWorkspaceId - 1) %
|
||||
HyprlandService::kWorkspaceSlotCount) +
|
||||
1;
|
||||
}
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot)
|
||||
{
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount;
|
||||
++slot) {
|
||||
WorkspaceState state;
|
||||
state.id = slot;
|
||||
state.hyprId = -1;
|
||||
state.label = std::to_string(slot);
|
||||
state.id = slot;
|
||||
state.hyprId = -1;
|
||||
state.label = std::to_string(slot);
|
||||
state.focused = (slot == focusedSlot);
|
||||
state.active = state.focused;
|
||||
state.urgent = false;
|
||||
state.active = state.focused;
|
||||
state.urgent = false;
|
||||
monitor.workspaceStates.emplace(slot, state);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &workspaceJson : workspacesJson)
|
||||
{
|
||||
if (!workspaceJson.is_object())
|
||||
{
|
||||
for (const auto &workspaceJson : workspacesJson) {
|
||||
if (!workspaceJson.is_object()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int monitorId = workspaceJson.value("monitorID", -1);
|
||||
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)
|
||||
{
|
||||
auto monitorIt = monitors.find(monitorId);
|
||||
if (monitorIt == monitors.end() || workspaceId < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto &monitor = monitorIt->second;
|
||||
const int slot = ((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
|
||||
const int slot =
|
||||
((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
|
||||
auto &workspaceState = monitor.workspaceStates[slot];
|
||||
|
||||
workspaceState.id = slot;
|
||||
workspaceState.hyprId = workspaceId;
|
||||
workspaceState.id = slot;
|
||||
workspaceState.hyprId = workspaceId;
|
||||
workspaceState.focused = (monitor.focusedWorkspaceId == workspaceId);
|
||||
workspaceState.active = workspaceState.focused || workspaceJson.value("windows", 0) > 0;
|
||||
workspaceState.urgent = false;
|
||||
workspaceState.active =
|
||||
workspaceState.focused || workspaceJson.value("windows", 0) > 0;
|
||||
workspaceState.urgent = false;
|
||||
|
||||
std::string labelCandidate;
|
||||
if (workspaceJson.contains("name") && workspaceJson["name"].is_string())
|
||||
{
|
||||
if (workspaceJson.contains("name") &&
|
||||
workspaceJson["name"].is_string()) {
|
||||
labelCandidate = workspaceJson["name"].get<std::string>();
|
||||
}
|
||||
|
||||
if (labelCandidate.empty() || labelCandidate == std::to_string(workspaceId))
|
||||
{
|
||||
if (labelCandidate.empty() ||
|
||||
labelCandidate == std::to_string(workspaceId)) {
|
||||
workspaceState.label = std::to_string(slot);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
workspaceState.label = labelCandidate;
|
||||
}
|
||||
|
||||
if (workspaceJson.contains("urgent") && workspaceJson["urgent"].is_boolean())
|
||||
{
|
||||
if (workspaceJson.contains("urgent") &&
|
||||
workspaceJson["urgent"].is_boolean()) {
|
||||
workspaceState.urgent = workspaceJson["urgent"].get<bool>();
|
||||
}
|
||||
else if (workspaceJson.contains("hasurgent") && workspaceJson["hasurgent"].is_boolean())
|
||||
{
|
||||
} else if (workspaceJson.contains("hasurgent") &&
|
||||
workspaceJson["hasurgent"].is_boolean()) {
|
||||
workspaceState.urgent = workspaceJson["hasurgent"].get<bool>();
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &pair : m_monitors)
|
||||
{
|
||||
for (const auto &pair : 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.");
|
||||
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 = m_monitors.find(id);
|
||||
if (it == m_monitors.end())
|
||||
{
|
||||
throw std::runtime_error("Monitor with ID " + std::to_string(id) + " not found.");
|
||||
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 >= m_monitors.size())
|
||||
{
|
||||
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
|
||||
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 = m_monitors.begin();
|
||||
auto it = monitors.begin();
|
||||
std::advance(it, static_cast<long>(index));
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
void HyprlandService::switchToWorkspace(int monitorId, int slot)
|
||||
{
|
||||
auto it = m_monitors.find(monitorId);
|
||||
if (it == m_monitors.end())
|
||||
{
|
||||
std::cerr << "[Hyprland] switchToWorkspace: monitor " << monitorId << " not found\n";
|
||||
return;
|
||||
}
|
||||
void HyprlandService::switchToWorkspace(int workspaceId) {
|
||||
std::string cmd =
|
||||
"hyprctl dispatch workspace " + std::to_string(workspaceId);
|
||||
|
||||
const auto &monitor = it->second;
|
||||
auto wsIt = monitor.workspaceStates.find(slot);
|
||||
std::string cmd;
|
||||
|
||||
// Use the Hyprland workspace id if available
|
||||
cmd = "hyprctl dispatch workspace " + std::to_string(wsIt->second.hyprId);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
(void)SystemHelper::get_command_output(cmd.c_str());
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
std::cerr << "[Hyprland] Failed to dispatch workspace command: " << ex.what() << " cmd=" << cmd << std::endl;
|
||||
} catch (const std::exception &ex) {
|
||||
std::cerr << "[Hyprland] Failed to dispatch workspace command: "
|
||||
<< ex.what() << " cmd=" << cmd << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
const HyprlandService::Monitor *
|
||||
HyprlandService::getMonitorByIndex(std::size_t index) const {
|
||||
if (index >= monitors.size()) {
|
||||
throw std::runtime_error("Monitor index out of bounds: " +
|
||||
std::to_string(index));
|
||||
}
|
||||
|
||||
auto it = m_monitors.begin();
|
||||
auto it = monitors.begin();
|
||||
std::advance(it, static_cast<long>(index));
|
||||
return &it->second;
|
||||
}
|
||||
Reference in New Issue
Block a user