summaryrefslogtreecommitdiff
path: root/src/config/supplementary/jeremy/Jeremy.cpp
blob: 67b7b4937cdd33ded666e12e1ad4f148a45ab8c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Jeremy.hpp"

#include "../../../Compositor.hpp"

#include <hyprutils/path/Path.hpp>
#include <filesystem>

using namespace Config;
using namespace Config::Supplementary;
using namespace Config::Supplementary::Jeremy;

std::expected<SConfigStateReply, std::string> Jeremy::getMainConfigPath() {
    static bool lastSafeMode = g_pCompositor->m_safeMode;

    static auto regularOrLuaIfAvail = [](std::filesystem::path p) -> std::filesystem::path {
        std::error_code ec;
        auto            p2 = p;
        p2.replace_extension(".lua");

        // if we don't have a .conf file, use lua by default.
        if (!std::filesystem::exists(p, ec) || ec)
            return p2;

        // if we have a .lua file, use that
        if (std::filesystem::exists(p2, ec) && !ec)
            return p2;

        // otherwise use .conf
        return p;
    };

    static auto getCfgPath = []() -> std::expected<SConfigStateReply, std::string> {
        lastSafeMode = g_pCompositor->m_safeMode;

        if (g_pCompositor->m_safeMode)
            return SConfigStateReply{(std::filesystem::path{g_pCompositor->m_instancePath} / "recoverycfg.conf").string(), CONFIG_TYPE_SPECIAL};

        if (!g_pCompositor->m_explicitConfigPath.empty())
            return SConfigStateReply{g_pCompositor->m_explicitConfigPath, CONFIG_TYPE_EXPLICIT};

        if (const auto CFG_ENV = getenv("HYPRLAND_CONFIG"); CFG_ENV)
            return SConfigStateReply{CFG_ENV, CONFIG_TYPE_EXPLICIT};

        const auto PATHS = Hyprutils::Path::findConfig(ISDEBUG ? "hyprlandd" : "hyprland");
        if (PATHS.first.has_value()) {
            return SConfigStateReply{regularOrLuaIfAvail(PATHS.first.value()), CONFIG_TYPE_REGULAR};
        } else if (PATHS.second.has_value()) {
            auto CONFIGPATH = Hyprutils::Path::fullConfigPath(PATHS.second.value(), ISDEBUG ? "hyprlandd" : "hyprland");
            return SConfigStateReply{regularOrLuaIfAvail(CONFIGPATH), CONFIG_TYPE_REGULAR};
        } else
            return std::unexpected("Neither HOME nor XDG_CONFIG_HOME are set in the environment. Could not find config in XDG_CONFIG_DIRS or /etc/xdg.");
    };
    static auto CONFIG_PATH = getCfgPath();

    if (lastSafeMode != g_pCompositor->m_safeMode)
        CONFIG_PATH = getCfgPath();

    return CONFIG_PATH;
}