mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-22 21:45:58 +01:00
miscfunctions: move configStringToInt to std::expected
This commit is contained in:
parent
936dfedbad
commit
47a1650c48
8 changed files with 106 additions and 72 deletions
|
@ -67,7 +67,7 @@ static Hyprlang::CParseResult configHandleGradientSet(const char* VALUE, void**
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DATA->m_vColors.push_back(CColor(configStringToInt(var)));
|
DATA->m_vColors.push_back(CColor(*configStringToInt(var)));
|
||||||
} catch (std::exception& e) {
|
} catch (std::exception& e) {
|
||||||
Debug::log(WARN, "Error parsing gradient {}", V);
|
Debug::log(WARN, "Error parsing gradient {}", V);
|
||||||
parseError = "Error parsing gradient " + V + ": " + e.what();
|
parseError = "Error parsing gradient " + V + ": " + e.what();
|
||||||
|
@ -2615,6 +2615,13 @@ std::optional<std::string> CConfigManager::handleWorkspaceRules(const std::strin
|
||||||
const static std::string ruleOnCreatedEmpty = "on-created-empty:";
|
const static std::string ruleOnCreatedEmpty = "on-created-empty:";
|
||||||
const static auto ruleOnCreatedEmptyLen = ruleOnCreatedEmpty.length();
|
const static auto ruleOnCreatedEmptyLen = ruleOnCreatedEmpty.length();
|
||||||
|
|
||||||
|
#define CHECK_OR_THROW(expr) \
|
||||||
|
\
|
||||||
|
auto X = expr; \
|
||||||
|
if (!X) { \
|
||||||
|
return "Failed parsing a workspace rule"; \
|
||||||
|
}
|
||||||
|
|
||||||
auto assignRule = [&](std::string rule) -> std::optional<std::string> {
|
auto assignRule = [&](std::string rule) -> std::optional<std::string> {
|
||||||
size_t delim = std::string::npos;
|
size_t delim = std::string::npos;
|
||||||
if ((delim = rule.find("gapsin:")) != std::string::npos) {
|
if ((delim = rule.find("gapsin:")) != std::string::npos) {
|
||||||
|
@ -2633,25 +2640,32 @@ std::optional<std::string> CConfigManager::handleWorkspaceRules(const std::strin
|
||||||
try {
|
try {
|
||||||
wsRule.borderSize = std::stoi(rule.substr(delim + 11));
|
wsRule.borderSize = std::stoi(rule.substr(delim + 11));
|
||||||
} catch (...) { return "Error parsing workspace rule bordersize: {}", rule.substr(delim + 11); }
|
} catch (...) { return "Error parsing workspace rule bordersize: {}", rule.substr(delim + 11); }
|
||||||
else if ((delim = rule.find("border:")) != std::string::npos)
|
else if ((delim = rule.find("border:")) != std::string::npos) {
|
||||||
wsRule.noBorder = !configStringToInt(rule.substr(delim + 7));
|
CHECK_OR_THROW(configStringToInt(rule.substr(delim + 7)))
|
||||||
else if ((delim = rule.find("shadow:")) != std::string::npos)
|
wsRule.noBorder = !*X;
|
||||||
wsRule.noShadow = !configStringToInt(rule.substr(delim + 7));
|
} else if ((delim = rule.find("shadow:")) != std::string::npos) {
|
||||||
else if ((delim = rule.find("rounding:")) != std::string::npos)
|
CHECK_OR_THROW(configStringToInt(rule.substr(delim + 7)))
|
||||||
wsRule.noRounding = !configStringToInt(rule.substr(delim + 9));
|
wsRule.noShadow = !*X;
|
||||||
else if ((delim = rule.find("decorate:")) != std::string::npos)
|
} else if ((delim = rule.find("rounding:")) != std::string::npos) {
|
||||||
wsRule.decorate = configStringToInt(rule.substr(delim + 9));
|
CHECK_OR_THROW(configStringToInt(rule.substr(delim + 9)))
|
||||||
else if ((delim = rule.find("monitor:")) != std::string::npos)
|
wsRule.noRounding = !*X;
|
||||||
|
} else if ((delim = rule.find("decorate:")) != std::string::npos) {
|
||||||
|
CHECK_OR_THROW(configStringToInt(rule.substr(delim + 9)))
|
||||||
|
wsRule.decorate = *X;
|
||||||
|
} else if ((delim = rule.find("monitor:")) != std::string::npos)
|
||||||
wsRule.monitor = rule.substr(delim + 8);
|
wsRule.monitor = rule.substr(delim + 8);
|
||||||
else if ((delim = rule.find("default:")) != std::string::npos)
|
else if ((delim = rule.find("default:")) != std::string::npos) {
|
||||||
wsRule.isDefault = configStringToInt(rule.substr(delim + 8));
|
CHECK_OR_THROW(configStringToInt(rule.substr(delim + 8)))
|
||||||
else if ((delim = rule.find("persistent:")) != std::string::npos)
|
wsRule.isDefault = *X;
|
||||||
wsRule.isPersistent = configStringToInt(rule.substr(delim + 11));
|
} else if ((delim = rule.find("persistent:")) != std::string::npos) {
|
||||||
else if ((delim = rule.find("defaultName:")) != std::string::npos)
|
CHECK_OR_THROW(configStringToInt(rule.substr(delim + 11)))
|
||||||
|
wsRule.isPersistent = *X;
|
||||||
|
} else if ((delim = rule.find("defaultName:")) != std::string::npos)
|
||||||
wsRule.defaultName = rule.substr(delim + 12);
|
wsRule.defaultName = rule.substr(delim + 12);
|
||||||
else if ((delim = rule.find(ruleOnCreatedEmpty)) != std::string::npos)
|
else if ((delim = rule.find(ruleOnCreatedEmpty)) != std::string::npos) {
|
||||||
wsRule.onCreatedEmptyRunCmd = cleanCmdForWorkspace(name, rule.substr(delim + ruleOnCreatedEmptyLen));
|
CHECK_OR_THROW(cleanCmdForWorkspace(name, rule.substr(delim + ruleOnCreatedEmptyLen)))
|
||||||
else if ((delim = rule.find("layoutopt:")) != std::string::npos) {
|
wsRule.onCreatedEmptyRunCmd = *X;
|
||||||
|
} else if ((delim = rule.find("layoutopt:")) != std::string::npos) {
|
||||||
std::string opt = rule.substr(delim + 10);
|
std::string opt = rule.substr(delim + 10);
|
||||||
if (!opt.contains(":")) {
|
if (!opt.contains(":")) {
|
||||||
// invalid
|
// invalid
|
||||||
|
@ -2668,6 +2682,8 @@ std::optional<std::string> CConfigManager::handleWorkspaceRules(const std::strin
|
||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#undef CHECK_OR_THROW
|
||||||
|
|
||||||
CVarList rulesList{rules, 0, ',', true};
|
CVarList rulesList{rules, 0, ',', true};
|
||||||
for (auto const& r : rulesList) {
|
for (auto const& r : rulesList) {
|
||||||
const auto R = assignRule(r);
|
const auto R = assignRule(r);
|
||||||
|
|
|
@ -1286,7 +1286,7 @@ std::string dispatchSeterror(eHyprCtlOutputFormat format, std::string request) {
|
||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
const CColor COLOR = configStringToInt(vars[1]);
|
const CColor COLOR = configStringToInt(vars[1]).value_or(0);
|
||||||
|
|
||||||
for (size_t i = 2; i < vars.size(); ++i)
|
for (size_t i = 2; i < vars.size(); ++i)
|
||||||
errorMessage += vars[i] + ' ';
|
errorMessage += vars[i] + ' ';
|
||||||
|
@ -1539,7 +1539,7 @@ std::string dispatchNotify(eHyprCtlOutputFormat format, std::string request) {
|
||||||
time = std::stoi(TIME);
|
time = std::stoi(TIME);
|
||||||
} catch (std::exception& e) { return "invalid arg 2"; }
|
} catch (std::exception& e) { return "invalid arg 2"; }
|
||||||
|
|
||||||
CColor color = configStringToInt(vars[3]);
|
CColor color = configStringToInt(vars[3]).value_or(0);
|
||||||
|
|
||||||
size_t msgidx = 4;
|
size_t msgidx = 4;
|
||||||
float fontsize = 13.f;
|
float fontsize = 13.f;
|
||||||
|
|
|
@ -400,7 +400,7 @@ void CLayerSurface::applyRules() {
|
||||||
} else if (rule.rule.starts_with("xray")) {
|
} else if (rule.rule.starts_with("xray")) {
|
||||||
CVarList vars{rule.rule, 0, ' '};
|
CVarList vars{rule.rule, 0, ' '};
|
||||||
try {
|
try {
|
||||||
xray = configStringToInt(vars[1]);
|
xray = configStringToInt(vars[1]).value_or(false);
|
||||||
} catch (...) {}
|
} catch (...) {}
|
||||||
} else if (rule.rule.starts_with("animation")) {
|
} else if (rule.rule.starts_with("animation")) {
|
||||||
CVarList vars{rule.rule, 2, 's'};
|
CVarList vars{rule.rule, 2, 's'};
|
||||||
|
|
|
@ -672,8 +672,8 @@ void CWindow::applyDynamicRule(const SWindowRule& r) {
|
||||||
|
|
||||||
// Basic form has only two colors, everything else can be parsed as a gradient
|
// Basic form has only two colors, everything else can be parsed as a gradient
|
||||||
if (colorsAndAngles.size() == 2 && !colorsAndAngles[1].contains("deg")) {
|
if (colorsAndAngles.size() == 2 && !colorsAndAngles[1].contains("deg")) {
|
||||||
m_sWindowData.activeBorderColor = CWindowOverridableVar(CGradientValueData(CColor(configStringToInt(colorsAndAngles[0]))), priority);
|
m_sWindowData.activeBorderColor = CWindowOverridableVar(CGradientValueData(CColor(configStringToInt(colorsAndAngles[0]).value_or(0))), priority);
|
||||||
m_sWindowData.inactiveBorderColor = CWindowOverridableVar(CGradientValueData(CColor(configStringToInt(colorsAndAngles[1]))), priority);
|
m_sWindowData.inactiveBorderColor = CWindowOverridableVar(CGradientValueData(CColor(configStringToInt(colorsAndAngles[1]).value_or(0))), priority);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -685,9 +685,9 @@ void CWindow::applyDynamicRule(const SWindowRule& r) {
|
||||||
} else if (token.contains("deg"))
|
} else if (token.contains("deg"))
|
||||||
inactiveBorderGradient.m_fAngle = std::stoi(token.substr(0, token.size() - 3)) * (PI / 180.0);
|
inactiveBorderGradient.m_fAngle = std::stoi(token.substr(0, token.size() - 3)) * (PI / 180.0);
|
||||||
else if (active)
|
else if (active)
|
||||||
activeBorderGradient.m_vColors.push_back(configStringToInt(token));
|
activeBorderGradient.m_vColors.push_back(configStringToInt(token).value_or(0));
|
||||||
else
|
else
|
||||||
inactiveBorderGradient.m_vColors.push_back(configStringToInt(token));
|
inactiveBorderGradient.m_vColors.push_back(configStringToInt(token).value_or(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Includes sanity checks for the number of colors in each gradient
|
// Includes sanity checks for the number of colors in each gradient
|
||||||
|
|
|
@ -691,15 +691,15 @@ int64_t getPPIDof(int64_t pid) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t configStringToInt(const std::string& VALUE) {
|
std::expected<int64_t, std::string> configStringToInt(const std::string& VALUE) {
|
||||||
auto parseHex = [](const std::string& value) -> int64_t {
|
auto parseHex = [](const std::string& value) -> std::expected<int64_t, std::string> {
|
||||||
try {
|
try {
|
||||||
size_t position;
|
size_t position;
|
||||||
auto result = stoll(value, &position, 16);
|
auto result = stoll(value, &position, 16);
|
||||||
if (position == value.size())
|
if (position == value.size())
|
||||||
return result;
|
return result;
|
||||||
} catch (const std::exception&) {}
|
} catch (const std::exception&) {}
|
||||||
throw std::invalid_argument("invalid hex " + value);
|
return std::unexpected("invalid hex " + value);
|
||||||
};
|
};
|
||||||
if (VALUE.starts_with("0x")) {
|
if (VALUE.starts_with("0x")) {
|
||||||
// Values with 0x are hex
|
// Values with 0x are hex
|
||||||
|
@ -718,18 +718,25 @@ int64_t configStringToInt(const std::string& VALUE) {
|
||||||
auto b = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
auto b = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
rolling = rolling.substr(rolling.find(',') + 1);
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
uint8_t a = 0;
|
uint8_t a = 0;
|
||||||
|
|
||||||
|
if (!r || !g || !b)
|
||||||
|
return std::unexpected("failed parsing " + VALUEWITHOUTFUNC);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
a = std::round(std::stof(trim(rolling.substr(0, rolling.find(',')))) * 255.f);
|
a = std::round(std::stof(trim(rolling.substr(0, rolling.find(',')))) * 255.f);
|
||||||
} catch (std::exception& e) { throw std::invalid_argument("failed parsing " + VALUEWITHOUTFUNC); }
|
} catch (std::exception& e) { return std::unexpected("failed parsing " + VALUEWITHOUTFUNC); }
|
||||||
|
|
||||||
return a * (Hyprlang::INT)0x1000000 + r * (Hyprlang::INT)0x10000 + g * (Hyprlang::INT)0x100 + b;
|
return a * (Hyprlang::INT)0x1000000 + *r * (Hyprlang::INT)0x10000 + *g * (Hyprlang::INT)0x100 + *b;
|
||||||
} else if (VALUEWITHOUTFUNC.length() == 8) {
|
} else if (VALUEWITHOUTFUNC.length() == 8) {
|
||||||
const auto RGBA = parseHex(VALUEWITHOUTFUNC);
|
const auto RGBA = parseHex(VALUEWITHOUTFUNC);
|
||||||
|
|
||||||
|
if (!RGBA)
|
||||||
|
return RGBA;
|
||||||
// now we need to RGBA -> ARGB. The config holds ARGB only.
|
// now we need to RGBA -> ARGB. The config holds ARGB only.
|
||||||
return (RGBA >> 8) + 0x1000000 * (RGBA & 0xFF);
|
return (*RGBA >> 8) + 0x1000000 * (*RGBA & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::invalid_argument("rgba() expects length of 8 characters (4 bytes) or 4 comma separated values");
|
return std::unexpected("rgba() expects length of 8 characters (4 bytes) or 4 comma separated values");
|
||||||
|
|
||||||
} else if (VALUE.starts_with("rgb(") && VALUE.ends_with(')')) {
|
} else if (VALUE.starts_with("rgb(") && VALUE.ends_with(')')) {
|
||||||
const auto VALUEWITHOUTFUNC = trim(VALUE.substr(4, VALUE.length() - 5));
|
const auto VALUEWITHOUTFUNC = trim(VALUE.substr(4, VALUE.length() - 5));
|
||||||
|
@ -744,12 +751,16 @@ int64_t configStringToInt(const std::string& VALUE) {
|
||||||
rolling = rolling.substr(rolling.find(',') + 1);
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
auto b = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
auto b = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
|
||||||
return (Hyprlang::INT)0xFF000000 + r * (Hyprlang::INT)0x10000 + g * (Hyprlang::INT)0x100 + b;
|
if (!r || !g || !b)
|
||||||
|
return std::unexpected("failed parsing " + VALUEWITHOUTFUNC);
|
||||||
|
|
||||||
|
return (Hyprlang::INT)0xFF000000 + *r * (Hyprlang::INT)0x10000 + *g * (Hyprlang::INT)0x100 + *b;
|
||||||
} else if (VALUEWITHOUTFUNC.length() == 6) {
|
} else if (VALUEWITHOUTFUNC.length() == 6) {
|
||||||
return parseHex(VALUEWITHOUTFUNC) + 0xFF000000;
|
auto r = parseHex(VALUEWITHOUTFUNC);
|
||||||
|
return r ? *r + 0xFF000000 : r;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::invalid_argument("rgb() expects length of 6 characters (3 bytes) or 3 comma separated values");
|
return std::unexpected("rgb() expects length of 6 characters (3 bytes) or 3 comma separated values");
|
||||||
} else if (VALUE.starts_with("true") || VALUE.starts_with("on") || VALUE.starts_with("yes")) {
|
} else if (VALUE.starts_with("true") || VALUE.starts_with("on") || VALUE.starts_with("yes")) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if (VALUE.starts_with("false") || VALUE.starts_with("off") || VALUE.starts_with("no")) {
|
} else if (VALUE.starts_with("false") || VALUE.starts_with("off") || VALUE.starts_with("no")) {
|
||||||
|
@ -757,14 +768,14 @@ int64_t configStringToInt(const std::string& VALUE) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VALUE.empty() || !isNumber(VALUE, false))
|
if (VALUE.empty() || !isNumber(VALUE, false))
|
||||||
throw std::invalid_argument("cannot parse \"" + VALUE + "\" as an int.");
|
return std::unexpected("cannot parse \"" + VALUE + "\" as an int.");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const auto RES = std::stoll(VALUE);
|
const auto RES = std::stoll(VALUE);
|
||||||
return RES;
|
return RES;
|
||||||
} catch (std::exception& e) { throw std::invalid_argument(std::string{"stoll threw: "} + e.what()); }
|
} catch (std::exception& e) { return std::unexpected(std::string{"stoll threw: "} + e.what()); }
|
||||||
|
|
||||||
return 0;
|
return std::unexpected("parse error");
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2D configStringToVector2D(const std::string& VALUE) {
|
Vector2D configStringToVector2D(const std::string& VALUE) {
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include "math/Math.hpp"
|
#include "math/Math.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <expected>
|
||||||
#include "../SharedDefs.hpp"
|
#include "../SharedDefs.hpp"
|
||||||
#include "../macros.hpp"
|
#include "../macros.hpp"
|
||||||
|
|
||||||
|
@ -31,7 +32,7 @@ float vecToRectDistanceSquared(const Vector2D& vec, c
|
||||||
void logSystemInfo();
|
void logSystemInfo();
|
||||||
std::string execAndGet(const char*);
|
std::string execAndGet(const char*);
|
||||||
int64_t getPPIDof(int64_t pid);
|
int64_t getPPIDof(int64_t pid);
|
||||||
int64_t configStringToInt(const std::string&);
|
std::expected<int64_t, std::string> configStringToInt(const std::string&);
|
||||||
Vector2D configStringToVector2D(const std::string&);
|
Vector2D configStringToVector2D(const std::string&);
|
||||||
std::optional<float> getPlusMinusKeywordResult(std::string in, float relative);
|
std::optional<float> getPlusMinusKeywordResult(std::string in, float relative);
|
||||||
double normalizeAngleRad(double ang);
|
double normalizeAngleRad(double ang);
|
||||||
|
|
|
@ -2972,11 +2972,14 @@ SDispatchResult CKeybindManager::setProp(std::string args) {
|
||||||
const auto TOKEN = vars[i];
|
const auto TOKEN = vars[i];
|
||||||
if (TOKEN.ends_with("deg"))
|
if (TOKEN.ends_with("deg"))
|
||||||
colorData.m_fAngle = std::stoi(TOKEN.substr(0, TOKEN.size() - 3)) * (PI / 180.0);
|
colorData.m_fAngle = std::stoi(TOKEN.substr(0, TOKEN.size() - 3)) * (PI / 180.0);
|
||||||
else
|
else if (const auto V = configStringToInt(TOKEN); V)
|
||||||
colorData.m_vColors.push_back(configStringToInt(TOKEN));
|
colorData.m_vColors.push_back(*V);
|
||||||
|
}
|
||||||
|
} else if (VAL != "-1") {
|
||||||
|
const auto V = configStringToInt(VAL);
|
||||||
|
if (V)
|
||||||
|
colorData.m_vColors.push_back(*V);
|
||||||
}
|
}
|
||||||
} else if (VAL != "-1")
|
|
||||||
colorData.m_vColors.push_back(configStringToInt(VAL));
|
|
||||||
|
|
||||||
if (PROP == "activebordercolor")
|
if (PROP == "activebordercolor")
|
||||||
PWINDOW->m_sWindowData.activeBorderColor = CWindowOverridableVar(colorData, PRIORITY_SET_PROP);
|
PWINDOW->m_sWindowData.activeBorderColor = CWindowOverridableVar(colorData, PRIORITY_SET_PROP);
|
||||||
|
@ -2993,8 +2996,8 @@ SDispatchResult CKeybindManager::setProp(std::string args) {
|
||||||
} else if (auto search = g_pConfigManager->miWindowProperties.find(PROP); search != g_pConfigManager->miWindowProperties.end()) {
|
} else if (auto search = g_pConfigManager->miWindowProperties.find(PROP); search != g_pConfigManager->miWindowProperties.end()) {
|
||||||
if (VAL == "unset")
|
if (VAL == "unset")
|
||||||
search->second(PWINDOW)->unset(PRIORITY_SET_PROP);
|
search->second(PWINDOW)->unset(PRIORITY_SET_PROP);
|
||||||
else
|
else if (const auto V = configStringToInt(VAL); V)
|
||||||
*(search->second(PWINDOW)) = CWindowOverridableVar((int)configStringToInt(VAL), PRIORITY_SET_PROP);
|
*(search->second(PWINDOW)) = CWindowOverridableVar((int)*V, PRIORITY_SET_PROP);
|
||||||
} else {
|
} else {
|
||||||
return {.success = false, .error = "Prop not found"};
|
return {.success = false, .error = "Prop not found"};
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,10 @@ CFunctionHook::SAssembly CFunctionHook::fixInstructionProbeRIPCalls(const SInstr
|
||||||
size_t plusPresent = tokens[1][0] == '+' ? 1 : 0;
|
size_t plusPresent = tokens[1][0] == '+' ? 1 : 0;
|
||||||
size_t minusPresent = tokens[1][0] == '-' ? 1 : 0;
|
size_t minusPresent = tokens[1][0] == '-' ? 1 : 0;
|
||||||
std::string addr = tokens[1].substr((plusPresent || minusPresent), tokens[1].find("(%rip)") - (plusPresent || minusPresent));
|
std::string addr = tokens[1].substr((plusPresent || minusPresent), tokens[1].find("(%rip)") - (plusPresent || minusPresent));
|
||||||
const int32_t OFFSET = (minusPresent ? -1 : 1) * configStringToInt(addr);
|
auto addrResult = configStringToInt(addr);
|
||||||
|
if (!addrResult)
|
||||||
|
return {};
|
||||||
|
const int32_t OFFSET = (minusPresent ? -1 : 1) * *addrResult;
|
||||||
if (OFFSET == 0)
|
if (OFFSET == 0)
|
||||||
return {};
|
return {};
|
||||||
const uint64_t DESTINATION = currentAddress + OFFSET + len;
|
const uint64_t DESTINATION = currentAddress + OFFSET + len;
|
||||||
|
|
Loading…
Reference in a new issue