From da956c8a979471282db6790f5fa5dcec320ec226 Mon Sep 17 00:00:00 2001 From: Mihai Fufezan Date: Tue, 16 Jul 2024 22:23:37 +0300 Subject: 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 --- src/config/ConfigManager.cpp | 69 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'src/config/ConfigManager.cpp') 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 #include +#include #include #include #include @@ -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 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 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 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 CConfigManager::resetHLConfig() { m_dMonitorRules.clear(); m_dWindowRules.clear(); -- cgit v1.2.3