desktop/rules: use pid for exec rules (#13374)

This commit is contained in:
ItsOhen 2026-02-26 19:13:49 +01:00 committed by GitHub
parent cc14dd1baf
commit 70cdd819e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 28 deletions

View file

@ -52,6 +52,7 @@ static const std::unordered_map<eRuleProperty, eRuleMatchEngine> RULE_ENGINES =
{RULE_PROP_XDG_TAG, RULE_MATCH_ENGINE_REGEX}, //
{RULE_PROP_NAMESPACE, RULE_MATCH_ENGINE_REGEX}, //
{RULE_PROP_EXEC_TOKEN, RULE_MATCH_ENGINE_REGEX}, //
{RULE_PROP_EXEC_PID, RULE_MATCH_ENGINE_INT}, //
};
const std::vector<std::string>& Rule::allMatchPropStrings() {
@ -125,10 +126,11 @@ const std::string& IRule::name() {
return m_name;
}
void IRule::markAsExecRule(const std::string& token, bool persistent) {
void IRule::markAsExecRule(const std::string& token, uint64_t pid, bool persistent) {
m_execData.isExecRule = true;
m_execData.isExecPersistent = persistent;
m_execData.token = token;
m_execData.pid = pid;
m_execData.expiresAt = Time::steadyNow() + std::chrono::minutes(1);
}

View file

@ -6,7 +6,6 @@
#include "../../helpers/time/Time.hpp"
#include <vector>
#include <unordered_map>
#include <cstdint>
#include <optional>
namespace Desktop::Rule {
@ -31,6 +30,7 @@ namespace Desktop::Rule {
RULE_PROP_XDG_TAG = (1 << 16),
RULE_PROP_NAMESPACE = (1 << 17),
RULE_PROP_EXEC_TOKEN = (1 << 18),
RULE_PROP_EXEC_PID = (1 << 19),
RULE_PROP_ALL = std::numeric_limits<std::underlying_type_t<eRuleProperty>>::max(),
};
@ -52,7 +52,7 @@ namespace Desktop::Rule {
virtual std::underlying_type_t<eRuleProperty> getPropertiesMask();
void registerMatch(eRuleProperty, const std::string&);
void markAsExecRule(const std::string& token, bool persistent = false);
void markAsExecRule(const std::string& token, uint64_t pid, bool persistent = false);
bool isExecRule();
bool isExecPersistent();
bool execExpired();
@ -78,7 +78,8 @@ namespace Desktop::Rule {
bool isExecRule = false;
bool isExecPersistent = false;
std::string token;
uint64_t pid = 0;
Time::steady_tp expiresAt;
} m_execData;
};
}
}

View file

@ -104,20 +104,24 @@ bool CWindowRule::matches(PHLWINDOW w, bool allowEnvLookup) {
if (!w->xdgTag().has_value() || !engine->match(*w->xdgTag()))
return false;
break;
case RULE_PROP_EXEC_TOKEN:
// this is only allowed on static rules, we don't need it on dynamic plus it's expensive
if (!allowEnvLookup)
break;
const auto ENV = w->getEnv();
if (ENV.contains(EXEC_RULE_ENV_NAME)) {
const auto TKN = ENV.at(EXEC_RULE_ENV_NAME);
if (!engine->match(TKN))
return false;
break;
}
const auto ENV = w->getEnv();
bool match = false;
return false;
if (ENV.contains(EXEC_RULE_ENV_NAME)) {
if (engine->match(ENV.at(EXEC_RULE_ENV_NAME)))
match = true;
} else if (m_matchEngines.contains(RULE_PROP_EXEC_PID)) {
if (m_matchEngines.at(RULE_PROP_EXEC_PID)->match(w->getPID()))
match = true;
}
if (!match)
return false;
break;
}
}
@ -153,11 +157,6 @@ SP<CWindowRule> CWindowRule::buildFromExecString(std::string&& s) {
wr->addEffect(*EFFECT, std::string{"1"});
}
const auto TOKEN = g_pTokenManager->registerNewToken(nullptr, std::chrono::seconds(1));
wr->markAsExecRule(TOKEN, false /* TODO: could be nice. */);
wr->registerMatch(RULE_PROP_EXEC_TOKEN, TOKEN);
return wr;
}