mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-23 01:25:58 +01:00
pluginapi: add a config keyword adding method
This commit is contained in:
parent
7f4b0aaadc
commit
af9440152e
4 changed files with 47 additions and 3 deletions
|
@ -1389,9 +1389,16 @@ std::string CConfigManager::parseKeyword(const std::string& COMMAND, const std::
|
|||
else if (COMMAND.starts_with("plugin"))
|
||||
handlePlugin(COMMAND, VALUE);
|
||||
else {
|
||||
// try config
|
||||
const auto IT = std::find_if(pluginKeywords.begin(), pluginKeywords.end(), [&](const auto& other) { return other.name == COMMAND; });
|
||||
|
||||
if (IT != pluginKeywords.end()) {
|
||||
IT->fn(COMMAND, VALUE);
|
||||
} else {
|
||||
configSetValueSafe(currentCategory + (currentCategory == "" ? "" : ":") + COMMAND, VALUE);
|
||||
needsLayoutRecalc = 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (dynamic) {
|
||||
std::string retval = parseError;
|
||||
|
@ -1524,6 +1531,8 @@ void CConfigManager::parseLine(std::string& line) {
|
|||
}
|
||||
|
||||
void CConfigManager::loadConfigLoadVars() {
|
||||
EMIT_HOOK_EVENT("preConfigReload", nullptr);
|
||||
|
||||
Debug::log(LOG, "Reloading the config!");
|
||||
parseError = ""; // reset the error
|
||||
currentCategory = ""; // reset the category
|
||||
|
@ -2312,8 +2321,13 @@ void CConfigManager::addPluginConfigVar(HANDLE handle, const std::string& name,
|
|||
}
|
||||
}
|
||||
|
||||
void CConfigManager::addPluginKeyword(HANDLE handle, const std::string& name, std::function<void(const std::string&, const std::string&)> fn) {
|
||||
pluginKeywords.emplace_back(SPluginKeyword{handle, name, fn});
|
||||
}
|
||||
|
||||
void CConfigManager::removePluginConfig(HANDLE handle) {
|
||||
std::erase_if(pluginConfigs, [&](const auto& other) { return other.first == handle; });
|
||||
std::erase_if(pluginKeywords, [&](const auto& other) { return other.handle == handle; });
|
||||
}
|
||||
|
||||
std::string CConfigManager::getDefaultWorkspaceFor(const std::string& name) {
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <algorithm>
|
||||
#include <regex>
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include <xf86drmMode.h>
|
||||
#include "../Window.hpp"
|
||||
#include "../helpers/WLClasses.hpp"
|
||||
|
@ -71,6 +72,12 @@ struct SAnimationPropertyConfig {
|
|||
SAnimationPropertyConfig* pParentAnimation = nullptr;
|
||||
};
|
||||
|
||||
struct SPluginKeyword {
|
||||
HANDLE handle = 0;
|
||||
std::string name = "";
|
||||
std::function<void(const std::string&, const std::string&)> fn;
|
||||
};
|
||||
|
||||
struct SExecRequestedRule {
|
||||
std::string szRule = "";
|
||||
uint64_t iPid = 0;
|
||||
|
@ -120,6 +127,7 @@ class CConfigManager {
|
|||
std::unordered_map<std::string, SAnimationPropertyConfig> getAnimationConfig();
|
||||
|
||||
void addPluginConfigVar(HANDLE handle, const std::string& name, const SConfigValue& value);
|
||||
void addPluginKeyword(HANDLE handle, const std::string& name, std::function<void(const std::string& cmd, const std::string& val)> fun);
|
||||
void removePluginConfig(HANDLE handle);
|
||||
|
||||
// no-op when done.
|
||||
|
@ -163,6 +171,7 @@ class CConfigManager {
|
|||
|
||||
std::vector<std::string> m_vDeclaredPlugins;
|
||||
std::unordered_map<HANDLE, std::unique_ptr<std::unordered_map<std::string, SConfigValue>>> pluginConfigs; // stores plugin configs
|
||||
std::vector<SPluginKeyword> pluginKeywords;
|
||||
|
||||
bool isFirstLaunch = true; // For exec-once
|
||||
|
||||
|
|
|
@ -160,6 +160,19 @@ APICALL bool HyprlandAPI::addConfigValue(HANDLE handle, const std::string& name,
|
|||
return true;
|
||||
}
|
||||
|
||||
APICALL bool HyprlandAPI::addConfigKeyword(HANDLE handle, const std::string& name, std::function<void(const std::string& key, const std::string& val)> fn) {
|
||||
auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle);
|
||||
|
||||
if (!g_pPluginSystem->m_bAllowConfigVars)
|
||||
return false;
|
||||
|
||||
if (!PLUGIN)
|
||||
return false;
|
||||
|
||||
g_pConfigManager->addPluginKeyword(handle, name, fn);
|
||||
return true;
|
||||
}
|
||||
|
||||
APICALL SConfigValue* HyprlandAPI::getConfigValue(HANDLE handle, const std::string& name) {
|
||||
auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle);
|
||||
|
||||
|
|
|
@ -114,6 +114,14 @@ namespace HyprlandAPI {
|
|||
*/
|
||||
APICALL bool addConfigValue(HANDLE handle, const std::string& name, const SConfigValue& value);
|
||||
|
||||
/*
|
||||
Add a config keyword.
|
||||
This method may only be called in "pluginInit"
|
||||
|
||||
returns: true on success, false on fail
|
||||
*/
|
||||
APICALL bool addConfigKeyword(HANDLE handle, const std::string& name, std::function<void(const std::string& key, const std::string& val)> fn);
|
||||
|
||||
/*
|
||||
Get a config value.
|
||||
|
||||
|
|
Loading…
Reference in a new issue