From 31422ae25dee33dd000798b64a80bd7fd08d2ece Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 21 Dec 2024 23:07:23 +0000 Subject: [PATCH] windowrules: add negative: prefix for negating a regex fixes #8799 --- src/config/ConfigManager.cpp | 26 +++++++++++++------------- src/desktop/LayerRule.hpp | 15 +++++---------- src/desktop/Rule.cpp | 16 ++++++++++++++++ src/desktop/Rule.hpp | 21 +++++++++++++++++++++ src/desktop/WindowRule.hpp | 17 ++++++----------- 5 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 src/desktop/Rule.cpp create mode 100644 src/desktop/Rule.hpp diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index c40787776..074213823 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -1258,10 +1258,10 @@ std::vector> CConfigManager::getMatchingRules(PHLWINDOW pWindow, if (rule->szValue.starts_with("tag:") && !tags.isTagged(rule->szValue.substr(4))) continue; - if (rule->szValue.starts_with("title:") && !RE2::FullMatch(pWindow->m_szTitle, *rule->rV1Regex)) + if (rule->szValue.starts_with("title:") && !rule->rV1Regex.passes(pWindow->m_szTitle)) continue; - if (!RE2::FullMatch(pWindow->m_szClass, *rule->rV1Regex)) + if (!rule->rV1Regex.passes(pWindow->m_szClass)) continue; } catch (...) { @@ -1351,16 +1351,16 @@ std::vector> CConfigManager::getMatchingRules(PHLWINDOW pWindow, if (!rule->szTag.empty() && !tags.isTagged(rule->szTag)) continue; - if (!rule->szClass.empty() && !RE2::FullMatch(pWindow->m_szClass, *rule->rClass)) + if (!rule->szClass.empty() && !rule->rClass.passes(pWindow->m_szClass)) continue; - if (!rule->szTitle.empty() && !RE2::FullMatch(pWindow->m_szTitle, *rule->rTitle)) + if (!rule->szTitle.empty() && !rule->rTitle.passes(pWindow->m_szTitle)) continue; - if (!rule->szInitialTitle.empty() && !RE2::FullMatch(pWindow->m_szInitialTitle, *rule->rInitialTitle)) + if (!rule->szInitialTitle.empty() && !rule->rInitialTitle.passes(pWindow->m_szInitialTitle)) continue; - if (!rule->szInitialClass.empty() && !RE2::FullMatch(pWindow->m_szInitialClass, *rule->rInitialClass)) + if (!rule->szInitialClass.empty() && !rule->rInitialClass.passes(pWindow->m_szInitialClass)) continue; } catch (std::exception& e) { @@ -1419,7 +1419,7 @@ std::vector> CConfigManager::getMatchingRules(PHLLS pLS) { if (lr->targetNamespace.starts_with("address:0x")) { if (std::format("address:0x{:x}", (uintptr_t)pLS.get()) != lr->targetNamespace) continue; - } else if (!RE2::FullMatch(pLS->layerSurface->layerNamespace, *lr->rTargetNamespaceRegex)) + } else if (!lr->targetNamespaceRegex.passes(pLS->layerSurface->layerNamespace)) continue; // hit @@ -2281,7 +2281,7 @@ std::optional CConfigManager::handleWindowRule(const std::string& c return "Invalid rule: " + RULE; } - newRule->rV1Regex = std::make_unique(VALUE.starts_with("title:") ? VALUE.substr(6) : VALUE); + newRule->rV1Regex = {VALUE.starts_with("title:") ? VALUE.substr(6) : VALUE}; if (RULE.starts_with("size") || RULE.starts_with("maxsize") || RULE.starts_with("minsize")) m_vWindowRules.insert(m_vWindowRules.begin(), newRule); @@ -2311,7 +2311,7 @@ std::optional CConfigManager::handleLayerRule(const std::string& co return "Invalid rule found: " + RULE; } - rule->rTargetNamespaceRegex = std::make_unique(VALUE); + rule->targetNamespaceRegex = {VALUE}; m_vLayerRules.emplace_back(rule); @@ -2413,22 +2413,22 @@ std::optional CConfigManager::handleWindowRuleV2(const std::string& if (CLASSPOS != std::string::npos) { rule->szClass = extract(CLASSPOS + 6); - rule->rClass = std::make_unique(rule->szClass); + rule->rClass = {rule->szClass}; } if (TITLEPOS != std::string::npos) { rule->szTitle = extract(TITLEPOS + 6); - rule->rTitle = std::make_unique(rule->szTitle); + rule->rTitle = {rule->szTitle}; } if (INITIALCLASSPOS != std::string::npos) { rule->szInitialClass = extract(INITIALCLASSPOS + 13); - rule->rInitialClass = std::make_unique(rule->szInitialClass); + rule->rInitialClass = {rule->szInitialClass}; } if (INITIALTITLEPOS != std::string::npos) { rule->szInitialTitle = extract(INITIALTITLEPOS + 13); - rule->rInitialTitle = std::make_unique(rule->szInitialTitle); + rule->rInitialTitle = {rule->szInitialTitle}; } if (X11POS != std::string::npos) diff --git a/src/desktop/LayerRule.hpp b/src/desktop/LayerRule.hpp index 117fa6fb6..8cdb332e1 100644 --- a/src/desktop/LayerRule.hpp +++ b/src/desktop/LayerRule.hpp @@ -2,12 +2,7 @@ #include #include -#include - -//NOLINTNEXTLINE -namespace re2 { - class RE2; -}; +#include "Rule.hpp" class CLayerRule { public: @@ -27,10 +22,10 @@ class CLayerRule { RULE_ZUMBA, }; - eRuleType ruleType = RULE_INVALID; + eRuleType ruleType = RULE_INVALID; - const std::string targetNamespace; - const std::string rule; + const std::string targetNamespace; + const std::string rule; - std::unique_ptr rTargetNamespaceRegex; + CRuleRegexContainer targetNamespaceRegex; }; \ No newline at end of file diff --git a/src/desktop/Rule.cpp b/src/desktop/Rule.cpp new file mode 100644 index 000000000..dd1848d48 --- /dev/null +++ b/src/desktop/Rule.cpp @@ -0,0 +1,16 @@ +#include "Rule.hpp" +#include + +CRuleRegexContainer::CRuleRegexContainer(const std::string& regex_) { + const bool NEGATIVE = regex_.starts_with("negative:"); + + negative = NEGATIVE; + regex = std::make_unique(NEGATIVE ? regex_.substr(9) : regex_); +} + +bool CRuleRegexContainer::passes(const std::string& str) const { + if (!regex) + return false; + + return RE2::FullMatch(str, *regex) != negative; +} \ No newline at end of file diff --git a/src/desktop/Rule.hpp b/src/desktop/Rule.hpp new file mode 100644 index 000000000..4b178ee8e --- /dev/null +++ b/src/desktop/Rule.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +//NOLINTNEXTLINE +namespace re2 { + class RE2; +}; + +class CRuleRegexContainer { + public: + CRuleRegexContainer() = default; + + CRuleRegexContainer(const std::string& regex); + + bool passes(const std::string& str) const; + + private: + std::unique_ptr regex; + bool negative = false; +}; \ No newline at end of file diff --git a/src/desktop/WindowRule.hpp b/src/desktop/WindowRule.hpp index 3ce31179d..be9c2d9c9 100644 --- a/src/desktop/WindowRule.hpp +++ b/src/desktop/WindowRule.hpp @@ -2,12 +2,7 @@ #include #include -#include - -//NOLINTNEXTLINE -namespace re2 { - class RE2; -}; +#include "Rule.hpp" class CWindowRule { public: @@ -65,9 +60,9 @@ class CWindowRule { std::string szWorkspace = ""; // empty means any // precompiled regexes - std::unique_ptr rTitle; - std::unique_ptr rClass; - std::unique_ptr rInitialTitle; - std::unique_ptr rInitialClass; - std::unique_ptr rV1Regex; + CRuleRegexContainer rTitle; + CRuleRegexContainer rClass; + CRuleRegexContainer rInitialTitle; + CRuleRegexContainer rInitialClass; + CRuleRegexContainer rV1Regex; }; \ No newline at end of file