Skip to content

Commit

Permalink
API: add a few convenience funcs needed by hyprland
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Feb 9, 2024
1 parent 26d2638 commit ff30ccf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/hyprlang.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ namespace Hyprlang {
CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* defaultValue);
~CConfigCustomValueType();

/*!
\since 0.3.0
get the data pointer for the custom value type.
*/
void* getData() {
return data;
}

private:
PCONFIGCUSTOMVALUEHANDLERFUNC handler = nullptr;
PCONFIGCUSTOMVALUEDESTRUCTOR dtor = nullptr;
Expand Down Expand Up @@ -210,6 +219,14 @@ namespace Hyprlang {
return {}; // unreachable
}

/*!
\since 0.3.0
a flag to notify whether this value has been set explicitly by the user,
or not.
*/
bool m_bSetByUser = false;

private:
// remember to also edit config.hpp if editing
enum eDataType {
Expand Down Expand Up @@ -249,6 +266,13 @@ namespace Hyprlang {
*/
void registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options);

/*!
\since 0.3.0
Unregister a handler.
*/
void unregisterHandler(const char* name);

/*!
Commence the config state. Config becomes immutable, as in
no new values may be added or removed. Required for parsing.
Expand All @@ -260,6 +284,13 @@ namespace Hyprlang {
*/
void addSpecialCategory(const char* name, SSpecialCategoryOptions options);

/*!
\since 0.3.0
Remove a special category. Can be done dynamically.
*/
void removeSpecialCategory(const char* name);

/*!
Add a config value to a special category.
*/
Expand Down Expand Up @@ -318,6 +349,13 @@ namespace Hyprlang {
return val->getValue();
}

/*!
Check whether a special category with the provided key value exists
\since 0.3.0
*/
bool specialCategoryExistsForKey(const char* category, const char* key);

private:
bool m_bCommenced = false;

Expand Down
2 changes: 2 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ void CConfigValue::defaultFrom(SConfigDefaultValue& ref) {
throw "bad defaultFrom type";
}
}

m_bSetByUser = false;
}

void CConfigValue::setFrom(std::any value) {
Expand Down
25 changes: 25 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ void CConfig::addSpecialCategory(const char* name, SSpecialCategoryOptions optio
}
}

void CConfig::removeSpecialCategory(const char* name) {
std::erase_if(impl->specialCategories, [name](const auto& other) { return other->name == name; });
std::erase_if(impl->specialCategoryDescriptors, [name](const auto& other) { return other->name == name; });
}

void CConfig::applyDefaultsToCat(SSpecialCategory& cat) {
for (auto& [k, v] : cat.descriptor->defaultValues) {
cat.values[k].defaultFrom(v);
Expand Down Expand Up @@ -340,6 +345,8 @@ CParseResult CConfig::configSetValueSafe(const std::string& command, const std::
}
}

VALUEIT->second.m_bSetByUser = true;

return result;
}

Expand Down Expand Up @@ -594,4 +601,22 @@ CConfigValue* CConfig::getSpecialConfigValuePtr(const char* category, const char

void CConfig::registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options) {
impl->handlers.push_back(SHandler{name, options, func});
}

void CConfig::unregisterHandler(const char* name) {
std::erase_if(impl->handlers, [name](const auto& other) { return other.name == name; });
}

bool CConfig::specialCategoryExistsForKey(const char* category, const char* key) {
for (auto& sc : impl->specialCategories) {
if (sc->isStatic)
continue;

if (sc->name != category || std::string{std::any_cast<const char*>(sc->values[sc->key].getValue())} != key)
continue;

return true;
}

return false;
}

0 comments on commit ff30ccf

Please sign in to comment.