From 2c7cb0e9d482e629e3890d867998e30085a25c3a Mon Sep 17 00:00:00 2001 From: herlev <> Date: Thu, 24 Nov 2022 02:16:44 +0100 Subject: [PATCH] Fix crashes when using named workspaces in Hyprland The first crash occurs when trying to parse the ID of a workspace as an uint, since named workspaces has negative IDs. This is fixed by using ints for workspace IDs instead of uints. The second crash occurs when converting a workspace name that isn't a number to an integer. This is fixed by wrapping std::stoi in a try block and only sorting by number, when both names can successfully be converted to integers. --- include/modules/hyprland/window.hpp | 4 ++-- src/modules/hyprland/window.cpp | 8 ++++---- src/modules/wlr/workspace_manager.cpp | 8 ++++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 4b697cf3..fa9f1dd2 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -18,8 +18,8 @@ class Window : public waybar::ALabel, auto update() -> void; private: - uint getActiveWorkspaceID(std::string); - std::string getLastWindowTitle(uint); + int getActiveWorkspaceID(std::string); + std::string getLastWindowTitle(int); void onEvent(const std::string&); bool separate_outputs; diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index c3bebe49..d3d06cc5 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -49,7 +49,7 @@ auto Window::update() -> void { ALabel::update(); } -uint Window::getActiveWorkspaceID(std::string monitorName) { +int Window::getActiveWorkspaceID(std::string monitorName) { auto cmd = waybar::util::command::exec("hyprctl monitors -j"); assert(cmd.exit_code == 0); Json::Value json = parser_.parse(cmd.out); @@ -59,16 +59,16 @@ uint Window::getActiveWorkspaceID(std::string monitorName) { if (monitor == std::end(json)) { return 0; } - return (*monitor)["activeWorkspace"]["id"].as(); + return (*monitor)["activeWorkspace"]["id"].as(); } -std::string Window::getLastWindowTitle(uint workspaceID) { +std::string Window::getLastWindowTitle(int workspaceID) { auto cmd = waybar::util::command::exec("hyprctl workspaces -j"); assert(cmd.exit_code == 0); Json::Value json = parser_.parse(cmd.out); assert(json.isArray()); auto workspace = std::find_if(json.begin(), json.end(), [&](Json::Value workspace) { - return workspace["id"].as() == workspaceID; + return workspace["id"].as() == workspaceID; }); if (workspace == std::end(json)) { diff --git a/src/modules/wlr/workspace_manager.cpp b/src/modules/wlr/workspace_manager.cpp index da83cb79..6f11e1f2 100644 --- a/src/modules/wlr/workspace_manager.cpp +++ b/src/modules/wlr/workspace_manager.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include "gtkmm/widget.h" @@ -66,10 +67,13 @@ auto WorkspaceManager::workspace_comparator() const auto is_name_less = lhs->get_name() < rhs->get_name(); auto is_name_eq = lhs->get_name() == rhs->get_name(); auto is_coords_less = lhs->get_coords() < rhs->get_coords(); - auto is_number_less = std::stoi(lhs->get_name()) < std::stoi(rhs->get_name()); if (sort_by_number_) { - return is_number_less; + try { + auto is_number_less = std::stoi(lhs->get_name()) < std::stoi(rhs->get_name()); + return is_number_less; + } catch (std::invalid_argument) { + } } if (sort_by_name_) {