Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to not use default fallbacks (env and first available) #43

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions include/hyprcursor/hyprcursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ namespace Hyprcursor {
eHyprcursorDataType type = HC_DATA_PNG;
};

/*!
struct for cursor manager options
*/
struct SManagerOptions {
ikalco marked this conversation as resolved.
Show resolved Hide resolved
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.

Expand All @@ -68,6 +84,7 @@ namespace Hyprcursor {
\since 0.1.6
*/
CHyprcursorManager(const char* themeName, PHYPRCURSORLOGFUNC fn);
CHyprcursorManager(const char* themeName, SManagerOptions options);
~CHyprcursorManager();

/*!
Expand Down Expand Up @@ -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;
};
Expand Down
25 changes: 18 additions & 7 deletions libhyprcursor/hyprcursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand All @@ -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();
Expand Down Expand Up @@ -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_);
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down