forked from pohlrabi404/Hyprfoci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
252 lines (216 loc) · 8.62 KB
/
main.cpp
File metadata and controls
252 lines (216 loc) · 8.62 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
#include "CDotDecoration.hpp"
#include "cairo.h"
#include "globals.hpp"
#include <GLES2/gl2ext.h>
#include <filesystem>
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/helpers/MiscFunctions.hpp>
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/desktop/state/FocusState.hpp>
#include <hyprland/src/desktop/view/Window.hpp>
#include <hyprlang.hpp>
#include <hyprutils/math/Vector2D.hpp>
#include <hyprutils/memory/SharedPtr.hpp>
#include <hyprutils/memory/UniquePtr.hpp>
#include <string>
static CDotDecoration *current = nullptr;
static bool isTextureLoaded = false;
// TODO this is messy as hell lol
void loadTexture(fs::path parentPath, bool animated) {
if (!animated) {
g_pTexture = nullptr;
g_pTexture = makeShared<CTexture>();
g_pTexture->allocate();
fs::path path = parentPath;
if (!fs::exists(path))
noti("[hyprfoci] {} not found", path.generic_string());
const auto CAIROSURFACE = cairo_image_surface_create_from_png(path.c_str());
const auto CAIRO = cairo_create(CAIROSURFACE);
const Vector2D textureSize = {
static_cast<double>(cairo_image_surface_get_width(CAIROSURFACE)),
static_cast<double>(cairo_image_surface_get_height(CAIROSURFACE))};
g_pTexture->m_size = textureSize;
const auto DATA = cairo_image_surface_get_data(CAIROSURFACE);
glBindTexture(GL_TEXTURE_2D, g_pTexture->m_texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifndef GLES2
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSize.x, textureSize.y, 0,
GL_RGBA, GL_UNSIGNED_BYTE, DATA);
cairo_surface_destroy(CAIROSURFACE);
cairo_destroy(CAIRO);
} else {
for (auto &pair : g_pTextures) {
pair.second = nullptr;
pair.second = makeShared<CTexture>();
pair.second->allocate();
fs::path path = parentPath / pair.first;
if (!fs::exists(path))
noti("[hyprfoci] {} not found", pair.first, path.generic_string());
const auto CAIROSURFACE =
cairo_image_surface_create_from_png(path.c_str());
const auto CAIRO = cairo_create(CAIROSURFACE);
const Vector2D textureSize = {
static_cast<double>(cairo_image_surface_get_width(CAIROSURFACE)),
static_cast<double>(cairo_image_surface_get_height(CAIROSURFACE))};
pair.second->m_size = textureSize;
const auto DATA = cairo_image_surface_get_data(CAIROSURFACE);
glBindTexture(GL_TEXTURE_2D, pair.second->m_texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifndef GLES2
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSize.x, textureSize.y, 0,
GL_RGBA, GL_UNSIGNED_BYTE, DATA);
cairo_surface_destroy(CAIROSURFACE);
cairo_destroy(CAIRO);
}
}
}
void initialLoad() {
static const auto m_pImgPaths =
(const Hyprlang::STRING *)HyprlandAPI::getConfigValue(
PHANDLE, "plugin:hyprfoci:imgs")
->getDataStaticPtr();
static const auto m_pImgPath =
(const Hyprlang::STRING *)HyprlandAPI::getConfigValue(
PHANDLE, "plugin:hyprfoci:img")
->getDataStaticPtr();
// make sure only load img or imgs
if (std::string{*m_pImgPaths} != "none") {
g_pTexture = nullptr;
const auto path = expandTilde(std::string{*m_pImgPaths});
loadTexture(path, true);
} else if (std::string{*m_pImgPath} != "none") {
for (auto &t : g_pTextures) {
t.second = nullptr;
}
const auto path = expandTilde(std::string{*m_pImgPath});
loadTexture(path, false);
} else {
for (auto &t : g_pTextures) {
t.second = nullptr;
}
g_pTexture = nullptr;
}
}
void onCloseWindow(void *self, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
auto square = current;
if (current && current->getOwner() == PWINDOW) {
HyprlandAPI::removeWindowDecoration(PHANDLE, square);
current = nullptr;
}
}
void onActiveWindow(void *self, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
if (!isTextureLoaded)
initialLoad();
if (current) {
HyprlandAPI::removeWindowDecoration(PHANDLE, current);
}
if (PWINDOW) {
static const auto excluded =
(const Hyprlang::STRING *)HyprlandAPI::getConfigValue(
PHANDLE, "plugin:hyprfoci:exclude")
->getDataStaticPtr();
std::string WindowTitle = PWINDOW->m_class;
std::string className;
for(char c : std::string{*excluded} + ',') {
if (c == ' ' || c == ',') {
if(!className.empty() && WindowTitle == className) {
current = nullptr;
return;
}
className = "";
}
else className += c;
}
auto square = makeUnique<CDotDecoration>(PWINDOW);
current = square.get();
HyprlandAPI::addWindowDecoration(PHANDLE, PWINDOW, std::move(square));
}
}
void onConfigReload(void *self, std::any data) {
const auto PWINDOW = Desktop::focusState()->window();
if (current) {
HyprlandAPI::removeWindowDecoration(PHANDLE, current);
}
if (PWINDOW) {
initialLoad();
auto square = makeUnique<CDotDecoration>(PWINDOW);
current = square.get();
HyprlandAPI::addWindowDecoration(PHANDLE, PWINDOW, std::move(square));
}
}
APICALL EXPORT std::string PLUGIN_API_VERSION() { return HYPRLAND_API_VERSION; }
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
PHANDLE = handle;
const std::string HASH = __hyprland_api_get_hash();
if (HASH.find(GIT_COMMIT_HASH) != 0) {
HyprlandAPI::addNotification(
PHANDLE,
"[Hyprfoci] Failure in initialization: Version mismatch (headers ver "
"is not equal to running hyprland ver)",
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[Hyprfoci] Version mismatch");
}
// config variables
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:size",
Hyprlang::VEC2{20, 20});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:pos",
Hyprlang::VEC2{10, 10});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:origin",
Hyprlang::VEC2{0, 0});
HyprlandAPI::addConfigValue(
PHANDLE, "plugin:hyprfoci:color",
Hyprlang::INT{*configStringToInt("rgba(11ff3388)")});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:rounding",
Hyprlang::FLOAT{4.0});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:exclude",
Hyprlang::STRING{""});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:img",
Hyprlang::STRING{"none"});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfoci:imgs",
Hyprlang::STRING{"none"});
static auto P = HyprlandAPI::registerCallbackDynamic(
PHANDLE, "closeWindow",
[&](void *self, SCallbackInfo &info, std::any data) {
onCloseWindow(self, data);
});
static auto P1 = HyprlandAPI::registerCallbackDynamic(
PHANDLE, "activeWindow",
[&](void *self, SCallbackInfo &info, std::any data) {
onActiveWindow(self, data);
});
static auto P2 = HyprlandAPI::registerCallbackDynamic(
PHANDLE, "configReloaded",
[&](void *self, SCallbackInfo &info, std::any data) {
onConfigReload(self, data);
});
// generate a deco for current window if exists
for (auto &w : g_pCompositor->m_windows) {
if (g_pCompositor->isWindowActive(w)) {
auto deco = makeUnique<CDotDecoration>(w);
current = deco.get();
HyprlandAPI::addWindowDecoration(PHANDLE, w, std::move(deco));
}
}
HyprlandAPI::addNotification(PHANDLE, "[Hyprfoci] init successful",
CHyprColor{0.2, 1.0, 0.2, 1.0}, 5000);
return {"hyprfoci", "A plugin to add a dot focus indicator", "Pohlrabi",
"0.2.1"};
}
APICALL EXPORT void PLUGIN_EXIT() {
HyprlandAPI::addNotification(PHANDLE, "[Hyprfoci] unload successful",
CHyprColor{0.2, 1.0, 0.2, 1.0}, 5000);
}