optimized the code, added some bugs :)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "services/hyprland.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
@@ -93,19 +94,20 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[4096];
|
||||
const ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
|
||||
std::string buffer;
|
||||
char temp_buffer[4096];
|
||||
const ssize_t bytes_read = read(fd, temp_buffer, sizeof(temp_buffer) - 1);
|
||||
|
||||
if (bytes_read > 0) {
|
||||
buffer[bytes_read] = '\0';
|
||||
this->buffer.append(buffer);
|
||||
temp_buffer[bytes_read] = '\0';
|
||||
buffer.append(temp_buffer);
|
||||
|
||||
size_t pos = 0;
|
||||
|
||||
while ((pos = this->buffer.find('\n')) != std::string::npos) {
|
||||
const std::string line = this->buffer.substr(0, pos);
|
||||
while ((pos = buffer.find('\n')) != std::string::npos) {
|
||||
const std::string line = buffer.substr(0, pos);
|
||||
parse_message(line);
|
||||
this->buffer.erase(0, pos + 1);
|
||||
buffer.erase(0, pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,6 +137,9 @@ std::string HyprlandService::get_socket_path() {
|
||||
}
|
||||
|
||||
void HyprlandService::refresh_monitors() {
|
||||
this->monitors.clear();
|
||||
this->workspaces.clear();
|
||||
|
||||
std::string output;
|
||||
|
||||
try {
|
||||
@@ -151,8 +156,6 @@ void HyprlandService::refresh_monitors() {
|
||||
return;
|
||||
}
|
||||
|
||||
std::map<int, Monitor> updated;
|
||||
|
||||
for (const auto &monitorJson : monitorsJson) {
|
||||
if (!monitorJson.is_object()) {
|
||||
continue;
|
||||
@@ -171,11 +174,24 @@ void HyprlandService::refresh_monitors() {
|
||||
}
|
||||
|
||||
if (monitor.id >= 0) {
|
||||
updated.emplace(monitor.id, std::move(monitor));
|
||||
this->monitors[monitor.id] = monitor;
|
||||
}
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot) {
|
||||
WorkspaceState wsState;
|
||||
wsState.focused = false;
|
||||
wsState.active = false;
|
||||
wsState.urgentWindows.clear();
|
||||
wsState.label = std::to_string(slot);
|
||||
wsState.monitorId = monitor.id;
|
||||
|
||||
int id = slot + monitor.id * HyprlandService::kWorkspaceSlotCount;
|
||||
wsState.hyprId = id;
|
||||
this->workspaces[id] = wsState;
|
||||
this->monitors[monitor.id].workspaceStates[slot] = &this->workspaces[id];
|
||||
}
|
||||
}
|
||||
|
||||
monitors.swap(updated);
|
||||
monitorStateChanged.emit();
|
||||
}
|
||||
|
||||
@@ -200,75 +216,38 @@ void HyprlandService::refresh_workspaces() {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount;
|
||||
++slot) {
|
||||
WorkspaceState state;
|
||||
state.id = slot;
|
||||
state.hyprId = -1;
|
||||
state.label = std::to_string(slot);
|
||||
state.active = (slot == focusedSlot);
|
||||
state.focused = 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 = monitors.find(monitorId);
|
||||
if (monitorIt == monitors.end() || workspaceId < 0) {
|
||||
continue;
|
||||
std::cout << "Workspace ID: " << workspaceId << std::endl;
|
||||
|
||||
assert(workspaceId != -1);
|
||||
|
||||
std::map<int, WorkspaceState>::iterator workspaceStateIt = this->workspaces.find(workspaceId);
|
||||
|
||||
if (workspaceStateIt == this->workspaces.end()) {
|
||||
assert(false); // should not happen
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
WorkspaceState *workspaceState = &workspaceStateIt->second;
|
||||
workspaceState->hyprId = workspaceId;
|
||||
workspaceState->focused = (this->monitors[workspaceState->monitorId].focusedWorkspaceId == workspaceId);
|
||||
workspaceState->active =
|
||||
workspaceState->focused || workspaceJson.value("windows", 0) > 0;
|
||||
workspaceState->urgentWindows.clear();
|
||||
|
||||
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>();
|
||||
if (workspaceJson["urgent"].get<bool>()) {
|
||||
workspaceState->urgentWindows.push_back(workspaceId);
|
||||
}
|
||||
}
|
||||
|
||||
this->workspaces[workspaceId] = *workspaceState;
|
||||
}
|
||||
|
||||
for (const auto &pair : monitors) {
|
||||
@@ -320,20 +299,11 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
|
||||
}
|
||||
}
|
||||
|
||||
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 = monitors.begin();
|
||||
std::advance(it, static_cast<long>(index));
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
void HyprlandService::handle_urgent_window(std::string windowAddress) {
|
||||
std::string output;
|
||||
// TODO: fix bugs first
|
||||
return;
|
||||
|
||||
try {
|
||||
output = SystemHelper::get_command_output(kClientsCommand);
|
||||
} catch (const std::exception &ex) {
|
||||
@@ -372,16 +342,55 @@ void HyprlandService::handle_urgent_window(std::string windowAddress) {
|
||||
bool changed = false;
|
||||
|
||||
for (auto &wsPair : monitor.workspaceStates) {
|
||||
if (wsPair.second.hyprId == workspaceId) {
|
||||
if (!wsPair.second.urgent) {
|
||||
wsPair.second.urgent = true;
|
||||
if (wsPair.second->hyprId == workspaceId) {
|
||||
if (wsPair.second->urgentWindows.empty()) {
|
||||
wsPair.second->urgentWindows.push_back(workspaceId);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
workspaceStateChanged.emit(monitor.id);
|
||||
this->workspaceStateChanged.emit(monitor.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HyprlandService::printMonitor(const Monitor &mon) const {
|
||||
std::cout << "=== Monitor Info ===\n";
|
||||
std::cout << "Name: " << mon.name << " (ID: " << mon.id << ")\n";
|
||||
std::cout << "Position: (" << mon.x << ", " << mon.y << ")\n";
|
||||
std::cout << "Focused Workspace ID: " << mon.focusedWorkspaceId << "\n";
|
||||
|
||||
std::cout << "Workspaces:\n";
|
||||
|
||||
if (mon.workspaceStates.empty()) {
|
||||
std::cout << " (None)\n";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot) {
|
||||
const auto it = mon.workspaceStates.find(slot);
|
||||
if (it == mon.workspaceStates.end()) {
|
||||
std::cout << " - [Slot: " << slot << " | HyprID: n/a]"
|
||||
<< " Label: <none> | Active: No | Focused: No | "
|
||||
"Urgent: No\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
const WorkspaceState *ws = it->second;
|
||||
std::cout << " - [HyprID: "
|
||||
<< (ws->hyprId >= 0 ? std::to_string(ws->hyprId)
|
||||
: std::string("n/a"))
|
||||
<< "] "
|
||||
<< "Label: " << (ws->label.empty() ? "<none>" : ws->label)
|
||||
<< " | "
|
||||
<< "Active: " << (ws->active ? "Yes" : "No") << " | "
|
||||
<< "Focused: " << (ws->focused ? "Yes" : "No") << " | "
|
||||
<< "Urgent: " << (ws->urgentWindows.size() > 0 ? "Yes" : "No")
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
std::cout << "====================\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user