Fixes #1: catch exception if workspace not an integer

This commit is contained in:
zjeffer 2023-06-18 12:13:53 +02:00 committed by Stanisław Zagórowski
parent 698c1510e3
commit c152797c2b

View file

@ -1,8 +1,8 @@
#include <hyprland/src/helpers/Color.hpp>
#include <hyprland/src/managers/KeybindManager.hpp>
#include <hyprland/src/Compositor.hpp> #include <hyprland/src/Compositor.hpp>
#include <hyprland/src/config/ConfigManager.hpp> #include <hyprland/src/config/ConfigManager.hpp>
#include <hyprland/src/helpers/Color.hpp>
#include <hyprland/src/helpers/Workspace.hpp> #include <hyprland/src/helpers/Workspace.hpp>
#include <hyprland/src/managers/KeybindManager.hpp>
#include "globals.hpp" #include "globals.hpp"
@ -22,16 +22,25 @@ static HOOK_CALLBACK_FN* e_monitorRemovedHandle = nullptr;
const std::string& getWorkspaceFromMonitor(CMonitor* monitor, const std::string& workspace) const std::string& getWorkspaceFromMonitor(CMonitor* monitor, const std::string& workspace)
{ {
int workspaceIndex = std::stoi(workspace); int workspaceIndex = 0;
if (workspaceIndex - 1 < 0) { try {
// convert to 0-indexed int
workspaceIndex = std::stoi(workspace) - 1;
}
catch (std::invalid_argument) {
Debug::log(WARN, "Invalid workspace index: %s", workspace.c_str());
return workspace; return workspace;
} }
if (workspaceIndex - 1 >= g_vMonitorWorkspaceMap[monitor->ID].size()) { if (workspaceIndex < 0) {
return workspace; return workspace;
} }
return g_vMonitorWorkspaceMap[monitor->ID][workspaceIndex - 1]; if (workspaceIndex >= g_vMonitorWorkspaceMap[monitor->ID].size()) {
return workspace;
}
return g_vMonitorWorkspaceMap[monitor->ID][workspaceIndex];
} }
void monitorWorkspace(std::string workspace) void monitorWorkspace(std::string workspace)