hyprland/window: fix crash when no return from socket

Gracefully handle lack of response from the IPC. If socket isn't
available, we already log the IPC isn't running. We dont need to crash
program just because we couldn't get responses. We can just return an
empty object.
This commit is contained in:
Austin Horstman 2024-07-15 08:44:26 -05:00
parent 0a78da0315
commit b41fcdedff
No known key found for this signature in database
1 changed files with 71 additions and 61 deletions

View File

@ -92,15 +92,20 @@ auto Window::update() -> void {
auto Window::getActiveWorkspace() -> Workspace { auto Window::getActiveWorkspace() -> Workspace {
const auto workspace = gIPC->getSocket1JsonReply("activeworkspace"); const auto workspace = gIPC->getSocket1JsonReply("activeworkspace");
assert(workspace.isObject());
if (workspace.isObject()) {
return Workspace::parse(workspace); return Workspace::parse(workspace);
} }
return {};
}
auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace { auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
const auto monitors = gIPC->getSocket1JsonReply("monitors"); const auto monitors = gIPC->getSocket1JsonReply("monitors");
assert(monitors.isArray()); if (monitors.isArray()) {
auto monitor = std::find_if(monitors.begin(), monitors.end(), auto monitor = std::find_if(monitors.begin(), monitors.end(), [&](Json::Value monitor) {
[&](Json::Value monitor) { return monitor["name"] == monitorName; }); return monitor["name"] == monitorName;
});
if (monitor == std::end(monitors)) { if (monitor == std::end(monitors)) {
spdlog::warn("Monitor not found: {}", monitorName); spdlog::warn("Monitor not found: {}", monitorName);
return Workspace{-1, 0, "", ""}; return Workspace{-1, 0, "", ""};
@ -108,7 +113,7 @@ auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
const int id = (*monitor)["activeWorkspace"]["id"].asInt(); const int id = (*monitor)["activeWorkspace"]["id"].asInt();
const auto workspaces = gIPC->getSocket1JsonReply("workspaces"); const auto workspaces = gIPC->getSocket1JsonReply("workspaces");
assert(workspaces.isArray()); if (workspaces.isArray()) {
auto workspace = std::find_if(workspaces.begin(), workspaces.end(), auto workspace = std::find_if(workspaces.begin(), workspaces.end(),
[&](Json::Value workspace) { return workspace["id"] == id; }); [&](Json::Value workspace) { return workspace["id"] == id; });
if (workspace == std::end(workspaces)) { if (workspace == std::end(workspaces)) {
@ -116,6 +121,10 @@ auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
return Workspace{-1, 0, "", ""}; return Workspace{-1, 0, "", ""};
} }
return Workspace::parse(*workspace); return Workspace::parse(*workspace);
};
};
return {};
} }
auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace { auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace {
@ -146,7 +155,7 @@ void Window::queryActiveWorkspace() {
focused_ = true; focused_ = true;
if (workspace_.windows > 0) { if (workspace_.windows > 0) {
const auto clients = gIPC->getSocket1JsonReply("clients"); const auto clients = gIPC->getSocket1JsonReply("clients");
assert(clients.isArray()); if (clients.isArray()) {
auto activeWindow = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) { auto activeWindow = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) {
return window["address"] == workspace_.last_window; return window["address"] == workspace_.last_window;
}); });
@ -193,6 +202,7 @@ void Window::queryActiveWorkspace() {
} else { } else {
soloClass_ = ""; soloClass_ = "";
} }
};
} else { } else {
focused_ = false; focused_ = false;
windowData_ = WindowData{}; windowData_ = WindowData{};