hyprpaper/src/config/ConfigManager.cpp

179 lines
5.6 KiB
C++
Raw Normal View History

2022-07-01 23:05:58 +02:00
#include "ConfigManager.hpp"
#include "../Hyprpaper.hpp"
static Hyprlang::CParseResult handleWallpaper(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
Hyprlang::CParseResult result;
2022-07-01 23:05:58 +02:00
if (VALUE.find_first_of(',') == std::string::npos) {
result.setError("wallpaper failed (syntax)");
return result;
2022-07-01 23:05:58 +02:00
}
auto MONITOR = VALUE.substr(0, VALUE.find_first_of(','));
auto WALLPAPER = g_pConfigManager->trimPath(VALUE.substr(VALUE.find_first_of(',') + 1));
2022-07-01 23:05:58 +02:00
2022-08-03 18:06:33 +02:00
bool contain = false;
if (WALLPAPER.find("contain:") == 0) {
WALLPAPER = WALLPAPER.substr(8);
contain = true;
}
if (WALLPAPER[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
WALLPAPER = std::string(ENVHOME) + WALLPAPER.substr(1);
}
2022-07-01 23:05:58 +02:00
if (!std::filesystem::exists(WALLPAPER)) {
result.setError("wallpaper failed (no such file)");
return result;
2022-07-01 23:05:58 +02:00
}
if (std::find(g_pConfigManager->m_dRequestedPreloads.begin(), g_pConfigManager->m_dRequestedPreloads.end(), WALLPAPER) == g_pConfigManager->m_dRequestedPreloads.end() && !g_pHyprpaper->isPreloaded(WALLPAPER)) {
result.setError("wallpaper failed (not preloaded)");
return result;
2022-07-01 23:05:58 +02:00
}
2022-07-02 18:26:59 +02:00
g_pHyprpaper->clearWallpaperFromMonitor(MONITOR);
2022-07-01 23:05:58 +02:00
g_pHyprpaper->m_mMonitorActiveWallpapers[MONITOR] = WALLPAPER;
2022-08-03 18:06:33 +02:00
g_pHyprpaper->m_mMonitorWallpaperRenderData[MONITOR].contain = contain;
2024-01-03 13:47:49 +01:00
if (MONITOR.empty()) {
for (auto& m : g_pHyprpaper->m_vMonitors) {
if (!m->hasATarget || m->wildcard) {
g_pHyprpaper->clearWallpaperFromMonitor(m->name);
g_pHyprpaper->m_mMonitorActiveWallpapers[m->name] = WALLPAPER;
g_pHyprpaper->m_mMonitorWallpaperRenderData[m->name].contain = contain;
}
}
} else {
const auto PMON = g_pHyprpaper->getMonitorFromName(MONITOR);
if (PMON)
PMON->wildcard = false;
}
return result;
2022-07-01 23:05:58 +02:00
}
static Hyprlang::CParseResult handlePreload(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
2022-07-01 23:05:58 +02:00
auto WALLPAPER = VALUE;
if (WALLPAPER[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
WALLPAPER = std::string(ENVHOME) + WALLPAPER.substr(1);
}
if (!std::filesystem::exists(WALLPAPER)) {
Hyprlang::CParseResult result;
result.setError((std::string{"no such file: "} + WALLPAPER).c_str());
return result;
2022-07-01 23:05:58 +02:00
}
g_pConfigManager->m_dRequestedPreloads.emplace_back(WALLPAPER);
2022-07-17 14:17:54 +02:00
return Hyprlang::CParseResult{};
2022-07-17 14:17:54 +02:00
}
2022-11-26 18:27:19 +00:00
static Hyprlang::CParseResult handleUnloadAll(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
2022-11-28 11:43:08 +00:00
std::vector<std::string> toUnload;
for (auto& [name, target] : g_pHyprpaper->m_mWallpaperTargets) {
2022-11-26 18:27:19 +00:00
bool exists = false;
for (auto& [mon, target2] : g_pHyprpaper->m_mMonitorActiveWallpaperTargets) {
2022-11-26 18:27:19 +00:00
if (&target == target2) {
exists = true;
break;
}
}
if (exists)
continue;
2022-11-28 11:43:08 +00:00
toUnload.emplace_back(name);
2022-11-26 18:27:19 +00:00
}
2022-11-28 11:43:08 +00:00
for (auto& tu : toUnload)
g_pHyprpaper->unloadWallpaper(tu);
return Hyprlang::CParseResult{};
}
static Hyprlang::CParseResult handleUnload(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
auto WALLPAPER = VALUE;
if (VALUE == "all")
return handleUnloadAll(C, V);
if (WALLPAPER[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
WALLPAPER = std::string(ENVHOME) + WALLPAPER.substr(1);
}
g_pHyprpaper->unloadWallpaper(WALLPAPER);
return Hyprlang::CParseResult{};
}
CConfigManager::CConfigManager() {
// Initialize the configuration
// Read file from default location
// or from an explicit location given by user
std::string configPath = getMainConfigPath();
config = std::make_unique<Hyprlang::CConfig>(configPath.c_str(), Hyprlang::SConfigOptions{.allowMissingConfig = true});
config->addConfigValue("ipc", Hyprlang::INT{1L});
config->addConfigValue("splash", Hyprlang::INT{0L});
config->addConfigValue("splash_offset", Hyprlang::FLOAT{2.F});
config->registerHandler(&handleWallpaper, "wallpaper", {.allowFlags = false});
config->registerHandler(&handleUnload, "unload", {.allowFlags = false});
config->registerHandler(&handlePreload, "preload", {.allowFlags = false});
config->registerHandler(&handleUnloadAll, "unloadAll", {.allowFlags = false});
config->commence();
}
void CConfigManager::parse() {
const auto ERROR = config->parse();
if (ERROR.error)
std::cout << "Error in config: \n"
<< ERROR.getError() << "\n";
}
std::string CConfigManager::getMainConfigPath() {
if (!g_pHyprpaper->m_szExplicitConfigPath.empty())
return g_pHyprpaper->m_szExplicitConfigPath;
static const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
std::string configPath;
if (!xdgConfigHome)
configPath = getenv("HOME") + std::string("/.config");
else
configPath = xdgConfigHome;
return configPath + "/hypr/hyprpaper.conf";
2022-11-26 18:27:19 +00:00
}
// trim from both ends
std::string CConfigManager::trimPath(std::string path) {
2023-12-24 01:12:34 +08:00
if (path.empty())
return "";
// trims whitespaces, tabs and new line feeds
size_t pathStartIndex = path.find_first_not_of(" \t\r\n");
size_t pathEndIndex = path.find_last_not_of(" \t\r\n");
return path.substr(pathStartIndex, pathEndIndex - pathStartIndex + 1);
2023-01-05 00:50:01 +03:00
}