-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
342 lines (282 loc) · 10.5 KB
/
Copy pathMain.cpp
File metadata and controls
342 lines (282 loc) · 10.5 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/devices/IKeyboard.hpp>
#include <hyprland/src/managers/SeatManager.hpp>
#include <hyprland/src/helpers/Monitor.hpp>
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprutils/string/VarList.hpp>
using namespace std::literals;
using namespace Hyprutils::String;
enum class ToggleMode
{
Hover, Hold, Press
};
struct WaybarRegion
{
std::string process_name;
int32_t x;
int32_t y;
int32_t width;
int32_t height;
bool visible = false;
void toggle();
};
struct PluginState
{
HANDLE handle;
bool allow_show_waybar;
ToggleMode toggle_mode;
WaybarRegion* hovered_region;
std::optional<uint32_t> toggle_bind_keycode;
std::vector<std::vector<WaybarRegion>> monitor_regions;
std::unordered_map<std::string, uint32_t> keycode_cache;
PluginState(HANDLE handle)
: handle(handle)
{
reset();
}
void reset()
{
hovered_region = nullptr;
allow_show_waybar = true;
toggle_mode = ToggleMode::Hover;
toggle_bind_keycode = std::nullopt;
}
};
inline std::unique_ptr<PluginState> global_plugin_state;
void add_notification(std::string_view message)
{
HyprlandAPI::addNotification(
global_plugin_state->handle,
std::format("[hypr-waybar-toggle]: {}", message),
CHyprColor(1.0, 0.2, 0.2, 1.0), 5000);
}
auto fetch_process_pid(std::string_view name) -> pid_t
{
auto cmd = std::format("pidof -s {}", name);
char buf[512];
auto* pidof = popen(cmd.c_str(), "r");
fgets(buf, 512, pidof);
auto pid = strtoul(buf, nullptr, 10);
pclose(pidof);
return pid;
}
void WaybarRegion::toggle()
{
kill(fetch_process_pid(process_name), SIGUSR1);
visible = !visible;
}
// NOTE(Peter): Would love to take std::string_view instead, but can't really use those as keys for unordered_map here.
// At least this way we avoid additional allocations
auto keycode_from_name(const std::string& name) -> std::optional<uint32_t>
{
if (name.empty())
{
return {};
}
if (global_plugin_state->keycode_cache.contains(name))
{
return global_plugin_state->keycode_cache.at(name);
}
auto sym = xkb_keysym_from_name(name.data(), XKB_KEYSYM_CASE_INSENSITIVE);
if (sym == XKB_KEY_NoSymbol)
{
return std::nullopt;
}
// NOTE(Peter): Code referenced from here: https://github.com/hyprwm/Hyprland/blob/bc764f7065c8e46b4e62cbb511b419034712c509/src/managers/KeybindManager.cpp#L2539
// It's very annyoing that Hyprland doesn't expose a nice API for doing this, but whatever.
auto* keymap = g_pSeatManager->m_keyboard->m_xkbKeymap;
auto* xkb_state = g_pSeatManager->m_keyboard->m_xkbState;
auto keycode_min = xkb_keymap_min_keycode(keymap);
auto keycode_max = xkb_keymap_max_keycode(keymap);
for (auto kc = keycode_min; kc <= keycode_max; ++kc)
{
if (sym == xkb_state_key_get_one_sym(xkb_state, kc))
{
// NOTE(Peter): I still don't understand why xkb keycodes are offset by 8...
global_plugin_state->keycode_cache[name] = kc - 8;
return kc - 8;
}
}
return std::nullopt;
}
APICALL EXPORT std::string PLUGIN_API_VERSION()
{
return HYPRLAND_API_VERSION;
}
void hide_all()
{
for (auto& regions : global_plugin_state->monitor_regions)
{
for (auto& region : regions)
{
if (region.visible)
{
region.toggle();
}
}
}
}
void update_mouse(int32_t mx, int32_t my)
{
auto active_monitor = g_pCompositor->getMonitorFromCursor();
auto& regions = global_plugin_state->monitor_regions[active_monitor->m_id];
if (regions.empty())
{
return;
}
auto monitor_bounds = active_monitor->logicalBox();
auto monitor_local_x = mx - static_cast<int32_t>(monitor_bounds.pos().x);
auto monitor_local_y = my - static_cast<int32_t>(monitor_bounds.pos().y);
auto* old_region = global_plugin_state->hovered_region;
global_plugin_state->hovered_region = nullptr;
for (auto [idx, region] : regions | std::views::enumerate)
{
if (monitor_local_x >= region.x && monitor_local_x <= region.x + region.width && monitor_local_y >= region.y && monitor_local_y <= region.y + region.height)
{
global_plugin_state->hovered_region = ®ion;
break;
}
}
if (!global_plugin_state->hovered_region && old_region)
{
hide_all();
}
}
void on_config_pre_reload()
{
for (auto& regions : global_plugin_state->monitor_regions)
{
regions.clear();
}
}
void on_config_reloaded()
{
global_plugin_state->reset();
std::string toggle_bind_str = static_cast<Hyprlang::STRING>(*HyprlandAPI::getConfigValue(global_plugin_state->handle, "plugin:hypr_waybar:toggle_bind")->getDataStaticPtr());
std::string_view toggle_mode_str = static_cast<Hyprlang::STRING>(*HyprlandAPI::getConfigValue(global_plugin_state->handle, "plugin:hypr_waybar:toggle_mode")->getDataStaticPtr());
global_plugin_state->toggle_bind_keycode = keycode_from_name(toggle_bind_str);
if (global_plugin_state->toggle_bind_keycode.has_value())
{
// If toggle is enabled we don't want to show the waybar unless the toggle
// criteria are met.
global_plugin_state->allow_show_waybar = false;
if (toggle_mode_str == "hold")
{
global_plugin_state->toggle_mode = ToggleMode::Hold;
}
else if (toggle_mode_str == "press")
{
global_plugin_state->toggle_mode = ToggleMode::Press;
}
else
{
add_notification(std::format("Invalid value '{}' for toggle_mode", toggle_mode_str));
}
}
else if (!toggle_bind_str.empty())
{
add_notification(std::format("Invalid key name `{}` for toggle_bind", toggle_bind_str));
}
}
Hyprlang::CParseResult register_waybar_region(const char* cmd, const char* v)
{
auto result = Hyprlang::CParseResult{};
auto value = std::string{ v };
auto vars = CVarList{ value };
// hypr-waybar-region = MONITOR_NAME, X, Y, WIDTH, HEIGHT, [PROCESS_NAME]
if (vars.size() < 5)
{
add_notification(std::format("Invalid number of parameters passed to hypr-waybar-region. Expected at leats 5 but got {}", vars.size()));
result.setError("[hypr-waybar-toggle]: Invalid number of parameters passed to hypr-waybar-region");
return result;
}
auto monitor = g_pCompositor->getMonitorFromName(vars[0]);
if (!monitor)
{
add_notification(std::format("No monitor with name {} was found.", vars[0]));
result.setError("[hypr-waybar-toggle]: Failed to find monitor.");
return result;
}
auto region = WaybarRegion{};
try
{
region.x = std::stoi(vars[1]);
region.y = std::stoi(vars[2]);
region.width = std::stoi(vars[3]);
region.height = std::stoi(vars[4]);
}
catch (std::exception& ex)
{
add_notification("Failed to parse `hypr-waybar-region` parameters as integers.");
result.setError("[hypr-waybar-toggle]: Failed to parse parameters as integers.");
return result;
}
// Assume default waybar process name unless otherwise specified
region.process_name = "waybar";
if (vars.size() == 6)
{
region.process_name = vars[5];
}
global_plugin_state->monitor_regions[monitor->m_id].emplace_back(region);
return result;
}
void try_update_hovered_region_state()
{
if (global_plugin_state->hovered_region && global_plugin_state->hovered_region->visible != global_plugin_state->allow_show_waybar)
{
global_plugin_state->hovered_region->toggle();
}
}
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle)
{
global_plugin_state = std::make_unique<PluginState>(handle);
std::string_view hash = __hyprland_api_get_hash();
if (hash != GIT_COMMIT_HASH)
{
add_notification("Mismatched headers! Can't proceed.");
throw std::runtime_error("[hypr-waybar-toggle] Version mismatch");
}
global_plugin_state->monitor_regions.resize(g_pCompositor->m_monitors.size());
HyprlandAPI::addConfigValue(global_plugin_state->handle, "plugin:hypr_waybar:toggle_bind", Hyprlang::STRING{""});
HyprlandAPI::addConfigValue(global_plugin_state->handle, "plugin:hypr_waybar:toggle_mode", Hyprlang::STRING{"hold"});
HyprlandAPI::addConfigKeyword(global_plugin_state->handle, "hypr-waybar-region", register_waybar_region, Hyprlang::SHandlerOptions{});
static auto mouse_move = HyprlandAPI::registerCallbackDynamic(global_plugin_state->handle, "mouseMove", [](void* handle, SCallbackInfo& callback_info, std::any value)
{
auto pos = std::any_cast<const Vector2D>(value);
update_mouse(static_cast<int32_t>(pos.x), static_cast<int32_t>(pos.y));
try_update_hovered_region_state();
});
static auto pre_config_reload = HyprlandAPI::registerCallbackDynamic(global_plugin_state->handle, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { on_config_pre_reload(); });
static auto config_reloaded = HyprlandAPI::registerCallbackDynamic(global_plugin_state->handle, "configReloaded", [&](void* self, SCallbackInfo& info, std::any data) { on_config_reloaded(); });
static auto key_press = HyprlandAPI::registerCallbackDynamic(global_plugin_state->handle, "keyPress", [](void* handle, SCallbackInfo& callback_info, std::any value)
{
auto storage = std::any_cast<std::unordered_map<std::string, std::any>>(value);
auto key_event = std::any_cast<IKeyboard::SKeyEvent>(storage.at("event"));
if (key_event.keycode == global_plugin_state->toggle_bind_keycode)
{
switch (global_plugin_state->toggle_mode)
{
case ToggleMode::Hold:
{
global_plugin_state->allow_show_waybar = key_event.state == WL_KEYBOARD_KEY_STATE_PRESSED;
break;
}
case ToggleMode::Press:
{
if (key_event.state == WL_KEYBOARD_KEY_STATE_RELEASED)
{
global_plugin_state->allow_show_waybar = !global_plugin_state->allow_show_waybar;
}
break;
}
default:
break;
}
try_update_hovered_region_state();
}
});
return { "hypr-waybar-toggle", "Plugin to toggle waybar visibility based on mouse position.", "Peter Nilsson", "1.0" };
}
APICALL EXPORT void PLUGIN_EXIT()
{
}