summaryrefslogtreecommitdiff
path: root/src/config/legacy/ConfigManager.hpp
blob: def48408e58b2a9c372794422ff8c0137bf9b86e (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma once

#include <hyprutils/animation/AnimationConfig.hpp>

#include <vector>
#include <optional>
#include <xf86drmMode.h>

#include "../../desktop/DesktopTypes.hpp"
#include "../../desktop/rule/Rule.hpp"
#include "../../helpers/memory/Memory.hpp"
#include "../../managers/KeybindManager.hpp"

#include "../ConfigManager.hpp"

#include <hyprlang.hpp>

#define HANDLE void*

namespace Config::Supplementary {
    struct SConfigOptionDescription;
};

namespace Config::Legacy {

    class CConfigManager;

    struct SPluginKeyword {
        HANDLE                       handle = nullptr;
        std::string                  name   = "";
        Hyprlang::PCONFIGHANDLERFUNC fn     = nullptr;
    };

    struct SPluginVariable {
        HANDLE      handle = nullptr;
        std::string name   = "";
    };

    class CConfigManager : public Config::IConfigManager {
      public:
        CConfigManager();

        virtual eConfigManagerType               type() override;

        virtual void                             init() override;
        virtual void                             reload() override;
        virtual std::string                      verify() override;

        virtual int                              getDeviceInt(const std::string&, const std::string&, const std::string& fallback = "") override;
        virtual float                            getDeviceFloat(const std::string&, const std::string&, const std::string& fallback = "") override;
        virtual Vector2D                         getDeviceVec(const std::string&, const std::string&, const std::string& fallback = "") override;
        virtual std::string                      getDeviceString(const std::string&, const std::string&, const std::string& fallback = "") override;
        virtual bool                             deviceConfigExplicitlySet(const std::string&, const std::string&) override;
        virtual bool                             deviceConfigExists(const std::string&) override;

        virtual SConfigOptionReply               getConfigValue(const std::string&) override;

        virtual std::string                      getMainConfigPath() override;
        virtual std::string                      getErrors() override;
        virtual std::string                      getConfigString() override;
        virtual std::string                      currentConfigPath() override;
        virtual const std::vector<std::string>&  getConfigPaths() override;

        virtual std::expected<void, std::string> generateDefaultConfig(const std::filesystem::path&, bool safeMode) override;

        virtual void                             handlePluginLoads() override;
        virtual bool                             configVerifPassed() override;

        virtual std::expected<void, std::string> registerPluginValue(void* handle, SP<Config::Values::IValue> value) override;
        virtual void                             onPluginUnload(void* handle) override;

        void                                     addPluginConfigVar(HANDLE handle, const std::string& name, const Hyprlang::CConfigValue& value);
        void                                     addPluginKeyword(HANDLE handle, const std::string& name, Hyprlang::PCONFIGHANDLERFUNC fun, Hyprlang::SHandlerOptions opts = {});
        void                                     removePluginConfig(HANDLE handle);

        std::string                              parseKeyword(const std::string&, const std::string&);

        Hyprlang::CConfigValue*                  getHyprlangConfigValuePtr(const std::string& name, const std::string& specialCat = "");

        // keywords
        std::optional<std::string> handleRawExec(const std::string&, const std::string&);
        std::optional<std::string> handleExec(const std::string&, const std::string&);
        std::optional<std::string> handleExecOnce(const std::string&, const std::string&);
        std::optional<std::string> handleExecRawOnce(const std::string&, const std::string&);
        std::optional<std::string> handleExecShutdown(const std::string&, const std::string&);
        std::optional<std::string> handleMonitor(const std::string&, const std::string&);
        std::optional<std::string> handleBind(const std::string&, const std::string&);
        std::optional<std::string> handleUnbind(const std::string&, const std::string&);
        std::optional<std::string> handleWorkspaceRules(const std::string&, const std::string&);
        std::optional<std::string> handleBezier(const std::string&, const std::string&);
        std::optional<std::string> handleAnimation(const std::string&, const std::string&);
        std::optional<std::string> handleSource(const std::string&, const std::string&);
        std::optional<std::string> handleSubmap(const std::string&, const std::string&);
        std::optional<std::string> handleBindWS(const std::string&, const std::string&);
        std::optional<std::string> handleEnv(const std::string&, const std::string&);
        std::optional<std::string> handlePlugin(const std::string&, const std::string&);
        std::optional<std::string> handlePermission(const std::string&, const std::string&);
        std::optional<std::string> handleGesture(const std::string&, const std::string&);
        std::optional<std::string> handleWindowrule(const std::string&, const std::string&);
        std::optional<std::string> handleLayerrule(const std::string&, const std::string&);

        std::optional<std::string> handleMonitorv2(const std::string& output);
        Hyprlang::CParseResult     handleMonitorv2();
        std::optional<std::string> addRuleFromConfigKey(const std::string& name);
        std::optional<std::string> addLayerRuleFromConfigKey(const std::string& name);
        Hyprlang::CParseResult     reloadRules();

        std::string                m_configCurrentPath;

        bool                       m_isLaunchingExecOnce                 = false; // For exec-once to skip initial ws tracking
        bool                       m_lastConfigVerificationWasSuccessful = true;

      private:
        // internal methods
        std::optional<std::string>                       resetHLConfig();
        std::optional<std::string>                       verifyConfigExists();
        void                                             reloadRuleConfigs();

        void                                             postConfigReload(const Hyprlang::CParseResult& result);

        void                                             registerConfigVar(const char* name, const Hyprlang::INT& val);
        void                                             registerConfigVar(const char* name, const Hyprlang::FLOAT& val);
        void                                             registerConfigVar(const char* name, const Hyprlang::VEC2& val);
        void                                             registerConfigVar(const char* name, const Hyprlang::STRING& val);
        void                                             registerConfigVar(const char* name, Hyprlang::CUSTOMTYPE&& val);
        Hyprlang::CConfigValue*                          getConfigValueSafeDevice(const std::string& dev, const std::string& val, const std::string& fallback);

        UP<Hyprlang::CConfig>                            m_config;

        std::vector<std::string>                         m_configPaths;
        std::string                                      m_mainConfigPath;

        SSubmap                                          m_currentSubmap;

        std::vector<std::string>                         m_declaredPlugins;
        std::vector<SPluginKeyword>                      m_pluginKeywords;
        std::vector<SPluginVariable>                     m_pluginVariables;

        std::vector<SP<Desktop::Rule::IRule>>            m_keywordRules;

        bool                                             m_isFirstLaunch = true; // For exec-once

        bool                                             m_firstExecDispatched  = false;
        bool                                             m_manualCrashInitiated = false;

        std::vector<std::pair<std::string, std::string>> m_failedPluginConfigValues; // for plugin values of unloaded plugins
        std::string                                      m_configErrors = "";

        uint32_t                                         m_configValueNumber = 0;

        friend struct Config::Supplementary::SConfigOptionDescription;
        friend class CMonitorRuleParser;
    };

    WP<CConfigManager> mgr();
}