Skip to content

Commit

Permalink
windowrules: precompute regexes for window/layer rules
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Dec 21, 2024
1 parent 8e8073c commit 57921d7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
34 changes: 23 additions & 11 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,10 +1258,10 @@ std::vector<SP<CWindowRule>> 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->szValue.substr(6)))
if (rule->szValue.starts_with("title:") && !RE2::FullMatch(pWindow->m_szTitle, *rule->rV1Regex))
continue;

if (!RE2::FullMatch(pWindow->m_szClass, rule->szValue))
if (!RE2::FullMatch(pWindow->m_szClass, *rule->rV1Regex))
continue;

} catch (...) {
Expand Down Expand Up @@ -1351,16 +1351,16 @@ std::vector<SP<CWindowRule>> CConfigManager::getMatchingRules(PHLWINDOW pWindow,
if (!rule->szTag.empty() && !tags.isTagged(rule->szTag))
continue;

if (!rule->szClass.empty() && !RE2::FullMatch(pWindow->m_szClass, rule->szClass))
if (!rule->szClass.empty() && !RE2::FullMatch(pWindow->m_szClass, *rule->rClass))
continue;

if (!rule->szTitle.empty() && !RE2::FullMatch(pWindow->m_szTitle, rule->szTitle))
if (!rule->szTitle.empty() && !RE2::FullMatch(pWindow->m_szTitle, *rule->rTitle))
continue;

if (!rule->szInitialTitle.empty() && !RE2::FullMatch(pWindow->m_szInitialTitle, rule->szInitialTitle))
if (!rule->szInitialTitle.empty() && !RE2::FullMatch(pWindow->m_szInitialTitle, *rule->rInitialTitle))
continue;

if (!rule->szInitialClass.empty() && !RE2::FullMatch(pWindow->m_szInitialClass, rule->szInitialClass))
if (!rule->szInitialClass.empty() && !RE2::FullMatch(pWindow->m_szInitialClass, *rule->rInitialClass))
continue;

} catch (std::exception& e) {
Expand Down Expand Up @@ -1419,7 +1419,7 @@ std::vector<SP<CLayerRule>> 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->targetNamespace))
} else if (!RE2::FullMatch(pLS->layerSurface->layerNamespace, *lr->rTargetNamespaceRegex))
continue;

// hit
Expand Down Expand Up @@ -2281,6 +2281,8 @@ std::optional<std::string> CConfigManager::handleWindowRule(const std::string& c
return "Invalid rule: " + RULE;
}

newRule->rV1Regex = std::make_unique<RE2>(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);
else
Expand Down Expand Up @@ -2309,6 +2311,8 @@ std::optional<std::string> CConfigManager::handleLayerRule(const std::string& co
return "Invalid rule found: " + RULE;
}

rule->rTargetNamespaceRegex = std::make_unique<RE2>(VALUE);

m_vLayerRules.emplace_back(rule);

for (auto const& m : g_pCompositor->m_vMonitors)
Expand Down Expand Up @@ -2407,17 +2411,25 @@ std::optional<std::string> CConfigManager::handleWindowRuleV2(const std::string&
if (TAGPOS != std::string::npos)
rule->szTag = extract(TAGPOS + 4);

if (CLASSPOS != std::string::npos)
if (CLASSPOS != std::string::npos) {
rule->szClass = extract(CLASSPOS + 6);
rule->rClass = std::make_unique<RE2>(rule->szClass);
}

if (TITLEPOS != std::string::npos)
if (TITLEPOS != std::string::npos) {
rule->szTitle = extract(TITLEPOS + 6);
rule->rTitle = std::make_unique<RE2>(rule->szTitle);
}

if (INITIALCLASSPOS != std::string::npos)
if (INITIALCLASSPOS != std::string::npos) {
rule->szInitialClass = extract(INITIALCLASSPOS + 13);
rule->rInitialClass = std::make_unique<RE2>(rule->szInitialClass);
}

if (INITIALTITLEPOS != std::string::npos)
if (INITIALTITLEPOS != std::string::npos) {
rule->szInitialTitle = extract(INITIALTITLEPOS + 13);
rule->rInitialTitle = std::make_unique<RE2>(rule->szInitialTitle);
}

if (X11POS != std::string::npos)
rule->bX11 = extract(X11POS + 9) == "1" ? 1 : 0;
Expand Down
1 change: 1 addition & 0 deletions src/desktop/LayerRule.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "LayerRule.hpp"
#include <unordered_set>
#include <algorithm>
#include <re2/re2.h>
#include "../debug/Log.hpp"

static const auto RULES = std::unordered_set<std::string>{"noanim", "blur", "blurpopups", "dimaround"};
Expand Down
14 changes: 11 additions & 3 deletions src/desktop/LayerRule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include <string>
#include <cstdint>
#include <memory>

//NOLINTNEXTLINE
namespace re2 {
class RE2;
};

class CLayerRule {
public:
Expand All @@ -21,8 +27,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<re2::RE2> rTargetNamespaceRegex;
};
1 change: 1 addition & 0 deletions src/desktop/WindowRule.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "WindowRule.hpp"
#include <unordered_set>
#include <algorithm>
#include <re2/re2.h>
#include "../config/ConfigManager.hpp"

static const auto RULES = std::unordered_set<std::string>{
Expand Down
13 changes: 13 additions & 0 deletions src/desktop/WindowRule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include <string>
#include <cstdint>
#include <memory>

//NOLINTNEXTLINE
namespace re2 {
class RE2;
};

class CWindowRule {
public:
Expand Down Expand Up @@ -57,4 +63,11 @@ class CWindowRule {
std::string szFullscreenState = ""; // empty means any
std::string szOnWorkspace = ""; // empty means any
std::string szWorkspace = ""; // empty means any

// precompiled regexes
std::unique_ptr<re2::RE2> rTitle;
std::unique_ptr<re2::RE2> rClass;
std::unique_ptr<re2::RE2> rInitialTitle;
std::unique_ptr<re2::RE2> rInitialClass;
std::unique_ptr<re2::RE2> rV1Regex;
};

0 comments on commit 57921d7

Please sign in to comment.