From 27ca640abeef2d425b5dbecf804f5eb622cef56d Mon Sep 17 00:00:00 2001 From: Ikalco <73481042+ikalco@users.noreply.github.com> Date: Tue, 21 May 2024 16:45:11 -0500 Subject: [PATCH] core/API: add option to not use default fallbacks (env and first available) (#43) * add option to not use default fallbacks (env and first available) --- include/hyprcursor/hyprcursor.hpp | 24 +++++++++++++++++++++--- libhyprcursor/hyprcursor.cpp | 25 ++++++++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/include/hyprcursor/hyprcursor.hpp b/include/hyprcursor/hyprcursor.hpp index bf91706..2be1bf3 100644 --- a/include/hyprcursor/hyprcursor.hpp +++ b/include/hyprcursor/hyprcursor.hpp @@ -47,6 +47,22 @@ namespace Hyprcursor { eHyprcursorDataType type = HC_DATA_PNG; }; + /*! + struct for cursor manager options + */ + struct SManagerOptions { + explicit SManagerOptions(); + + /*! + The function used for logging by the cursor manager + */ + PHYPRCURSORLOGFUNC logFn; + /*! + Allow fallback to env and first theme found + */ + bool allowDefaultFallback; + }; + /*! Basic Hyprcursor manager. @@ -68,6 +84,7 @@ namespace Hyprcursor { \since 0.1.6 */ CHyprcursorManager(const char* themeName, PHYPRCURSORLOGFUNC fn); + CHyprcursorManager(const char* themeName, SManagerOptions options); ~CHyprcursorManager(); /*! @@ -172,9 +189,10 @@ namespace Hyprcursor { private: void init(const char* themeName_); - CHyprcursorImplementation* impl = nullptr; - bool finalizedAndValid = false; - PHYPRCURSORLOGFUNC logFn = nullptr; + CHyprcursorImplementation* impl = nullptr; + bool finalizedAndValid = false; + bool allowDefaultFallback = true; + PHYPRCURSORLOGFUNC logFn = nullptr; friend class CHyprcursorImplementation; }; diff --git a/libhyprcursor/hyprcursor.cpp b/libhyprcursor/hyprcursor.cpp index 8ffa7bb..b6793ec 100644 --- a/libhyprcursor/hyprcursor.cpp +++ b/libhyprcursor/hyprcursor.cpp @@ -108,7 +108,7 @@ static std::string getFirstTheme(PHYPRCURSORLOGFUNC logfn) { return ""; } -static std::string getFullPathForThemeName(const std::string& name, PHYPRCURSORLOGFUNC logfn) { +static std::string getFullPathForThemeName(const std::string& name, PHYPRCURSORLOGFUNC logfn, bool allowDefaultFallback) { const auto HOMEENV = getenv("HOME"); if (!HOMEENV) return ""; @@ -134,7 +134,7 @@ static std::string getFullPathForThemeName(const std::string& name, PHYPRCURSORL const auto MANIFESTPATH = themeDir.path().string() + "/manifest"; - if (name.empty()) { + if (allowDefaultFallback && name.empty()) { if (std::filesystem::exists(MANIFESTPATH + ".hl") || std::filesystem::exists(MANIFESTPATH + ".toml")) { Debug::log(HC_LOG_INFO, logfn, "getFullPathForThemeName: found {}", themeDir.path().string()); return std::filesystem::canonical(themeDir.path()).string(); @@ -193,14 +193,19 @@ static std::string getFullPathForThemeName(const std::string& name, PHYPRCURSORL } } - if (!name.empty()) { // try without name + if (allowDefaultFallback && !name.empty()) { // try without name Debug::log(HC_LOG_INFO, logfn, "getFullPathForThemeName: failed, trying without name of {}", name); - return getFullPathForThemeName("", logfn); + return getFullPathForThemeName("", logfn, allowDefaultFallback); } return ""; } +SManagerOptions::SManagerOptions() { + logFn = nullptr; + allowDefaultFallback = true; +} + CHyprcursorManager::CHyprcursorManager(const char* themeName_) { init(themeName_); } @@ -210,16 +215,22 @@ CHyprcursorManager::CHyprcursorManager(const char* themeName_, PHYPRCURSORLOGFUN init(themeName_); } +CHyprcursorManager::CHyprcursorManager(const char* themeName_, SManagerOptions options) { + logFn = options.logFn; + allowDefaultFallback = options.allowDefaultFallback; + init(themeName_); +} + void CHyprcursorManager::init(const char* themeName_) { std::string themeName = themeName_ ? themeName_ : ""; - if (themeName.empty()) { + if (allowDefaultFallback && themeName.empty()) { // try reading from env Debug::log(HC_LOG_INFO, logFn, "CHyprcursorManager: attempting to find theme from env"); themeName = themeNameFromEnv(logFn); } - if (themeName.empty()) { + if (allowDefaultFallback && themeName.empty()) { // try finding first, in the hierarchy Debug::log(HC_LOG_INFO, logFn, "CHyprcursorManager: attempting to find any theme"); themeName = getFirstTheme(logFn); @@ -234,7 +245,7 @@ void CHyprcursorManager::init(const char* themeName_) { // initialize theme impl = new CHyprcursorImplementation(this, logFn); impl->themeName = themeName; - impl->themeFullDir = getFullPathForThemeName(themeName, logFn); + impl->themeFullDir = getFullPathForThemeName(themeName, logFn, allowDefaultFallback); if (impl->themeFullDir.empty()) return;