-
Notifications
You must be signed in to change notification settings - Fork 13
/
util.cpp
207 lines (166 loc) · 6.62 KB
/
util.cpp
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
#include "types.hpp"
#include "globals.hpp"
#include "util.hpp"
#include <filesystem>
#include <optional>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <hyprland/src/SharedDefs.hpp>
#include <hyprland/src/debug/Log.hpp>
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/config/ConfigManager.hpp>
namespace hyprload {
std::filesystem::path getRootPath() {
static SConfigValue* hyprloadRoot = HyprlandAPI::getConfigValue(PHANDLE, c_pluginRoot);
return std::filesystem::path(hyprloadRoot->strValue);
}
std::optional<std::filesystem::path> getConfigHyprlandHeadersPath() {
static SConfigValue* hyprloadHeaders =
HyprlandAPI::getConfigValue(PHANDLE, c_hyprlandHeaders);
if (hyprloadHeaders->strValue.empty() || hyprloadHeaders->strValue == STRVAL_EMPTY) {
return std::nullopt;
}
debug("Using Hyprland headers from: " + hyprloadHeaders->strValue);
return std::filesystem::path(hyprloadHeaders->strValue);
}
std::optional<std::filesystem::path> getHyprlandInstallationPath() {
std::optional<std::filesystem::path> path = getConfigHyprlandHeadersPath();
if (path.has_value()) {
return std::nullopt;
}
return getRootPath() / "include" / "hyprland";
}
std::filesystem::path getHyprlandHeadersPath() {
std::optional<std::filesystem::path> path = getConfigHyprlandHeadersPath();
if (path.has_value()) {
return path.value();
}
return getRootPath() / "include";
}
std::filesystem::path getPkgConfigOverridePath() {
return getRootPath() / "pkgconfig";
}
std::filesystem::path getHyprlandPkgConfigPath() {
return getPkgConfigOverridePath() / "hyprland.pc";
}
std::filesystem::path getPluginsPath() {
return getRootPath() / "plugins";
}
std::filesystem::path getPluginBinariesPath() {
return getPluginsPath() / "bin";
}
bool isQuiet() {
static SConfigValue* hyprloadQuiet = HyprlandAPI::getConfigValue(PHANDLE, c_pluginQuiet);
return hyprloadQuiet->intValue;
}
bool isDebug() {
static SConfigValue* hyprloadDebug = HyprlandAPI::getConfigValue(PHANDLE, c_pluginDebug);
return hyprloadDebug->intValue;
}
void info(const std::string& message, usize duration) {
std::string logMessage = "[hyprload] " + message;
if (!isQuiet()) {
HyprlandAPI::addNotificationV2(PHANDLE,
std::unordered_map<std::string, std::any>{
{"text", logMessage},
{"time", duration},
{"color", CColor(0)},
{"icon", eIcons::ICON_INFO},
});
}
Debug::log(LOG, " {}", logMessage);
}
void success(const std::string& message, usize duration) {
std::string logMessage = "[hyprload] " + message;
if (!isQuiet()) {
HyprlandAPI::addNotificationV2(PHANDLE,
std::unordered_map<std::string, std::any>{
{"text", logMessage},
{"time", duration},
{"color", CColor(0)},
{"icon", eIcons::ICON_OK},
});
}
Debug::log(LOG, " {}", logMessage);
}
void error(const std::string& message, usize duration) {
std::string logMessage = "[hyprload] " + message;
if (!isQuiet()) {
HyprlandAPI::addNotificationV2(PHANDLE,
std::unordered_map<std::string, std::any>{
{"text", logMessage},
{"time", duration},
{"color", CColor(0)},
{"icon", eIcons::ICON_ERROR},
});
}
Debug::log(LOG, " {}", logMessage);
}
void debug(const std::string& message, usize duration) {
std::string debugMessage = "[hyprload] " + message;
if (isDebug()) {
HyprlandAPI::addNotificationV2(PHANDLE,
std::unordered_map<std::string, std::any>{
{"text", debugMessage},
{"time", duration},
{"color", s_debugColor},
{"icon", eIcons::ICON_INFO},
});
}
Debug::log(LOG, " {}", debugMessage);
}
std::optional<int> tryCreateLock(const std::filesystem::path& lockFile) {
mode_t oldMask = umask(0);
fd_t fd = open(lockFile.c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
umask(oldMask);
if (fd < 0) {
close(fd);
return -1;
}
flock_t lock = flock(fd, LOCK_EX | LOCK_NB);
if (lock < 0) {
close(fd);
return std::nullopt;
}
return fd;
}
std::optional<int> tryGetLock(const std::filesystem::path& lockFile) {
fd_t fd = open(lockFile.c_str(), O_RDWR);
if (fd < 0) {
close(fd);
return std::nullopt;
}
flock_t lock = flock(fd, LOCK_EX | LOCK_NB);
if (lock < 0) {
close(fd);
return std::nullopt;
}
return fd;
}
void releaseLock(flock_t lock) {
if (lock < 0) {
return;
}
flock(lock, LOCK_UN);
}
std::tuple<int, std::string> executeCommand(const std::string& command) {
std::string result = "";
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
return std::make_tuple(-1, "Failed to execute command");
}
char buffer[128];
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr) {
result += buffer;
}
}
int exit = pclose(pipe);
Debug::log(LOG, " [hyprload] Command: %s", command.c_str());
Debug::log(LOG, " [hyprload] Exit code: %d", exit);
Debug::log(LOG, " [hyprload] Result: %s", result.c_str());
return std::make_tuple(exit, result);
}
}