-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathConfigManager.cpp
More file actions
262 lines (201 loc) · 8.45 KB
/
Copy pathConfigManager.cpp
File metadata and controls
262 lines (201 loc) · 8.45 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "ConfigManager.hpp"
#include "../Hyprpaper.hpp"
#include <hyprutils/path/Path.hpp>
#include <filesystem>
static Hyprlang::CParseResult handleWallpaper(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
Hyprlang::CParseResult result;
if (VALUE.find_first_of(',') == std::string::npos) {
result.setError("wallpaper failed (syntax)");
return result;
}
auto MONITOR = VALUE.substr(0, VALUE.find_first_of(','));
auto WALLPAPER = g_pConfigManager->trimPath(VALUE.substr(VALUE.find_first_of(',') + 1));
bool contain = false;
if (WALLPAPER.find("contain:") == 0) {
WALLPAPER = WALLPAPER.substr(8);
contain = true;
}
bool tile = false;
if (WALLPAPER.find("tile:") == 0) {
WALLPAPER = WALLPAPER.substr(5);
tile = true;
}
if (WALLPAPER[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
WALLPAPER = std::string(ENVHOME) + WALLPAPER.substr(1);
}
std::error_code ec;
if (!std::filesystem::exists(WALLPAPER, ec)) {
result.setError((std::string{"wallpaper failed ("} + (ec ? ec.message() : std::string{"no such file"}) + std::string{": "} + WALLPAPER + std::string{")"}).c_str());
return result;
}
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;
}
g_pHyprpaper->clearWallpaperFromMonitor(MONITOR);
g_pHyprpaper->m_mMonitorActiveWallpapers[MONITOR] = WALLPAPER;
g_pHyprpaper->m_mMonitorWallpaperRenderData[MONITOR].contain = contain;
g_pHyprpaper->m_mMonitorWallpaperRenderData[MONITOR].tile = tile;
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;
g_pHyprpaper->m_mMonitorWallpaperRenderData[m->name].tile = tile;
}
}
} else {
const auto PMON = g_pHyprpaper->getMonitorFromName(MONITOR);
if (PMON)
PMON->wildcard = false;
}
return result;
}
static Hyprlang::CParseResult handlePreload(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
auto WALLPAPER = VALUE;
if (WALLPAPER[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
WALLPAPER = std::string(ENVHOME) + WALLPAPER.substr(1);
}
std::error_code ec;
if (!std::filesystem::exists(WALLPAPER, ec)) {
Hyprlang::CParseResult result;
result.setError(((ec ? ec.message() : std::string{"no such file"}) + std::string{": "} + WALLPAPER).c_str());
return result;
}
g_pConfigManager->m_dRequestedPreloads.emplace_back(WALLPAPER);
return Hyprlang::CParseResult{};
}
static Hyprlang::CParseResult handleUnloadAll(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
std::vector<std::string> toUnload;
for (auto& [name, target] : g_pHyprpaper->m_mWallpaperTargets) {
if (VALUE == "unused") {
bool exists = false;
for (auto& [mon, target2] : g_pHyprpaper->m_mMonitorActiveWallpaperTargets) {
if (&target == target2) {
exists = true;
break;
}
}
if (exists)
continue;
}
toUnload.emplace_back(name);
}
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" || VALUE == "unused")
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{};
}
static Hyprlang::CParseResult handleReload(const char* C, const char* V) {
const std::string COMMAND = C;
const std::string VALUE = V;
auto WALLPAPER = g_pConfigManager->trimPath(VALUE.substr(VALUE.find_first_of(',') + 1));
if (WALLPAPER.find("contain:") == 0) {
WALLPAPER = WALLPAPER.substr(8);
}
if (WALLPAPER.find("tile:") == 0)
WALLPAPER = WALLPAPER.substr(5);
auto preloadResult = handlePreload(C, WALLPAPER.c_str());
if (preloadResult.error)
return preloadResult;
auto MONITOR = VALUE.substr(0, VALUE.find_first_of(','));
if (MONITOR.empty()) {
for (auto& m : g_pHyprpaper->m_vMonitors) {
auto OLD_WALLPAPER = g_pHyprpaper->m_mMonitorActiveWallpapers[m->name];
g_pHyprpaper->unloadWallpaper(OLD_WALLPAPER);
}
} else {
auto OLD_WALLPAPER = g_pHyprpaper->m_mMonitorActiveWallpapers[MONITOR];
g_pHyprpaper->unloadWallpaper(OLD_WALLPAPER);
}
auto wallpaperResult = handleWallpaper(C, V);
if (wallpaperResult.error)
return wallpaperResult;
return Hyprlang::CParseResult{};
}
static Hyprlang::CParseResult handleSource(const char* C, const char* V) {
Hyprlang::CParseResult result;
const std::string path = g_pConfigManager->absolutePath(V);
std::error_code ec;
if (!std::filesystem::exists(path, ec)) {
result.setError((ec ? ec.message() : "no such file").c_str());
return result;
}
g_pConfigManager->config->parseFile(path.c_str());
return result;
}
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->addConfigValue("splash_color", Hyprlang::INT{0x55ffffff});
config->addConfigValue("splash_font_family", Hyprlang::STRING{"Sans"});
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->registerHandler(&handleReload, "reload", {.allowFlags = false});
config->registerHandler(&handleSource, "source", {.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 auto paths = Hyprutils::Path::findConfig("hyprpaper");
if (paths.first.has_value())
return paths.first.value();
else
return "";
}
// trim from both ends
std::string CConfigManager::trimPath(std::string path) {
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);
}
std::string CConfigManager::absolutePath(const std::string& path) {
if (path.empty())
return "";
std::string result = path;
if (result[0] == '~') {
const char* home = getenv("HOME");
if (home)
result = std::string(home) + result.substr(1);
}
return std::filesystem::absolute(result).string();
}