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.
This commit is contained in:
herlev
2022-11-24 02:16:44 +01:00
parent ce8ae5bf17
commit 2c7cb0e9d4
3 changed files with 12 additions and 8 deletions

View File

@ -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<uint>();
return (*monitor)["activeWorkspace"]["id"].as<int>();
}
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<uint>() == workspaceID;
return workspace["id"].as<int>() == workspaceID;
});
if (workspace == std::end(json)) {