diff options
| author | Mihai Fufezan <fufexan@protonmail.com> | 2024-07-16 22:23:37 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 21:23:37 +0200 |
| commit | da956c8a979471282db6790f5fa5dcec320ec226 (patch) | |
| tree | 36fcb6c4b7bcc4be68e69bf609ca86a8b51f6dd6 /src/config/ConfigManager.cpp | |
| parent | bd526822deb9ed47c0b51b534817aa8541fff07b (diff) | |
config: use hyprutils helper (#6891)
* flake.lock: update
nix/overlays: remove xwayland overlay (merged upstream)
* config: use hyprutils helper
* flake.lock: update
* CMake & Meson: update required versions
Diffstat (limited to 'src/config/ConfigManager.cpp')
| -rw-r--r-- | src/config/ConfigManager.cpp | 69 |
1 files changed, 34 insertions, 35 deletions
diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 04d29f3a..944aa0b2 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -8,6 +8,7 @@ #include <cstddef> #include <cstdint> +#include <hyprutils/path/Path.hpp> #include <string.h> #include <string> #include <sys/stat.h> @@ -628,25 +629,49 @@ CConfigManager::CConfigManager() { g_pHyprError->queueCreate(ERR.value(), CColor{1.0, 0.1, 0.1, 1.0}); } -std::string CConfigManager::getConfigDir() { - static const char* xdgConfigHome = getenv("XDG_CONFIG_HOME"); +std::optional<std::string> CConfigManager::generateConfig(std::string configPath) { + std::string parentPath = std::filesystem::path(configPath).parent_path(); - if (xdgConfigHome && std::filesystem::path(xdgConfigHome).is_absolute()) - return xdgConfigHome; + if (!std::filesystem::is_directory(parentPath)) { + Debug::log(WARN, "Creating config home directory"); + try { + std::filesystem::create_directories(parentPath); + } catch (std::exception e) { throw e; } + } - static const char* home = getenv("HOME"); + Debug::log(WARN, "No config file found; attempting to generate."); + std::ofstream ofs; + ofs.open(configPath, std::ios::trunc); + ofs << AUTOCONFIG; + ofs.close(); - if (!home) - throw std::runtime_error("Neither HOME nor XDG_CONFIG_HOME is set in the environment. Cannot determine config directory."); + if (!std::filesystem::exists(configPath)) + return "Config could not be generated."; - return home + std::string("/.config"); + return configPath; } std::string CConfigManager::getMainConfigPath() { if (!g_pCompositor->explicitConfigPath.empty()) return g_pCompositor->explicitConfigPath; - return getConfigDir() + "/hypr/" + (ISDEBUG ? "hyprlandd.conf" : "hyprland.conf"); + static const auto paths = Hyprutils::Path::findConfig(ISDEBUG ? "hyprlandd" : "hyprland"); + if (paths.first.has_value()) { + return paths.first.value(); + } else if (paths.second.has_value()) { + auto configPath = Hyprutils::Path::fullConfigPath(paths.second.value(), ISDEBUG ? "hyprlandd" : "hyprland"); + return generateConfig(configPath).value(); + } else + throw std::runtime_error("Neither HOME nor XDG_CONFIG_HOME are set in the environment. Could not find config in XDG_CONFIG_DIRS or /etc/xdg."); +} + +std::optional<std::string> CConfigManager::verifyConfigExists() { + std::string mainConfigPath = getMainConfigPath(); + + if (!std::filesystem::exists(mainConfigPath)) + return "broken config dir?"; + + return {}; } const std::string CConfigManager::getConfigString() { @@ -742,32 +767,6 @@ void CConfigManager::setDefaultAnimationVars() { CREATEANIMCFG("specialWorkspace", "workspaces"); } -std::optional<std::string> CConfigManager::verifyConfigExists() { - std::string mainConfigPath = getMainConfigPath(); - - if (g_pCompositor->explicitConfigPath.empty() && !std::filesystem::exists(mainConfigPath)) { - std::string configPath = std::filesystem::path(mainConfigPath).parent_path(); - - if (!std::filesystem::is_directory(configPath)) { - Debug::log(WARN, "Creating config home directory"); - try { - std::filesystem::create_directories(configPath); - } catch (...) { return "Broken config file! (Could not create config directory)"; } - } - - Debug::log(WARN, "No config file found; attempting to generate."); - std::ofstream ofs; - ofs.open(mainConfigPath, std::ios::trunc); - ofs << AUTOCONFIG; - ofs.close(); - } - - if (!std::filesystem::exists(mainConfigPath)) - return "broken config dir?"; - - return {}; -} - std::optional<std::string> CConfigManager::resetHLConfig() { m_dMonitorRules.clear(); m_dWindowRules.clear(); |
