Debian Codegraph @

hyprtester/plugin/src/main.cpp

raw
1 #include <unistd.h>
2 #include <src/includes.hpp>
3 #include <sstream>
4 #include <any>
5 #include <cmath>
6
7 #define private public
8 #include <src/managers/input/InputManager.hpp>
9 #include <src/pointer/PointerManager.hpp>
10 #include <src/pointer/PointerController.hpp>
11 #include <src/managers/SeatManager.hpp>
12 #include <src/managers/input/trackpad/TrackpadGestures.hpp>
13 #include <src/output/Monitor.hpp>
14 #include <src/desktop/rule/windowRule/WindowRuleEffectContainer.hpp>
15 #include <src/desktop/rule/layerRule/LayerRuleEffectContainer.hpp>
16 #include <src/desktop/rule/windowRule/WindowRuleApplicator.hpp>
17 #include <src/desktop/view/LayerSurface.hpp>
18 #include <src/desktop/state/WindowState.hpp>
19 #include <src/Compositor.hpp>
20 #include <src/desktop/state/FocusState.hpp>
21 #include <src/state/MonitorState.hpp>
22 #include <src/layout/LayoutManager.hpp>
23 #undef private
24
25 #include <hyprutils/utils/ScopeGuard.hpp>
26 #include <hyprutils/string/Numeric.hpp>
27 #include <hyprutils/string/VarList.hpp>
28 using namespace Hyprutils::Utils;
29 using namespace Hyprutils::String;
30
31 extern "C" {
32 #include <lua.h>
33 #include <lauxlib.h>
34 }
35
36 #include "globals.hpp"
37
38 // Do NOT change this function.
39 APICALL EXPORT std::string PLUGIN_API_VERSION() {
40 return HYPRLAND_API_VERSION;
41 }
42
43 static SDispatchResult test(std::string in) {
44 return {.success = true};
45 }
46
47 // Trigger a snap move event for the active window
48 static SDispatchResult snapMove(std::string in) {
49 const auto PLASTWINDOW = Desktop::focusState()->window();
50 if (!PLASTWINDOW->m_isFloating)
51 return {.success = false, .error = "Window must be floating"};
52
53 Vector2D pos = PLASTWINDOW->position(Desktop::View::IGeometric::GEOMETRIC_GOAL);
54 Vector2D size = PLASTWINDOW->size(Desktop::View::IGeometric::GEOMETRIC_GOAL);
55
56 g_layoutManager->performSnap(pos, size, PLASTWINDOW->layoutTarget(), MBIND_MOVE, -1, size);
57
58 PLASTWINDOW->layoutTarget()->setPositionGlobal(CBox{pos, size});
59
60 return {};
61 }
62
63 // Perform a full move-drag of the window with the given class and drop it at (x, y).
64 static SDispatchResult dragWindow(std::string in) {
65 CVarList2 data(std::move(in));
66
67 if (data.size() < 3)
68 return {.success = false, .error = "invalid input"};
69
70 const std::string cls = std::string{data[0]};
71
72 double x;
73 double y;
74 try {
75 x = std::stod(std::string{data[1]});
76 y = std::stod(std::string{data[2]});
77 } catch (...) { return {.success = false, .error = "invalid input"}; }
78
79 for (const auto& window : Desktop::windowState()->windows()) {
80 if (window->m_class != cls)
81 continue;
82
83 const auto target = window->layoutTarget();
84 if (!target)
85 return {.success = false, .error = "Window has no layout target"};
86
87 Pointer::pointerController()->warpTo({x, y}, true);
88 g_layoutManager->beginDragTarget(target, MBIND_MOVE);
89 g_layoutManager->endDragTarget();
90
91 return {};
92 }
93
94 return {.success = false, .error = std::format("No window with class '{}'", cls)};
95 }
96
97 class CTestKeyboard : public IKeyboard {
98 public:
99 static SP<CTestKeyboard> create(bool isVirtual) {
100 auto keeb = SP<CTestKeyboard>(new CTestKeyboard());
101 keeb->m_self = keeb;
102 keeb->m_isVirtual = isVirtual;
103 keeb->m_shareStates = !isVirtual;
104 keeb->m_hlName = "test-keyboard";
105 keeb->m_deviceName = "test-keyboard";
106 return keeb;
107 }
108
109 virtual bool isVirtual() {
110 return m_isVirtual;
111 }
112
113 virtual SP<Aquamarine::IKeyboard> aq() {
114 return nullptr;
115 }
116
117 void sendKey(uint32_t key, bool pressed) {
118 auto event = IKeyboard::SKeyEvent{
119 .timeMs = sc<uint32_t>(Time::millis(Time::steadyNow())),
120 .keycode = key,
121 .state = pressed ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED,
122 };
123 updatePressed(event.keycode, pressed);
124 m_keyboardEvents.key.emit(event);
125 }
126
127 void setMods(uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) {
128 updateModifiers(depressed, latched, locked, group);
129 }
130
131 void destroy() {
132 m_events.destroy.emit();
133 }
134
135 private:
136 bool m_isVirtual = false;
137 };
138
139 class CTestMouse : public IPointer {
140 public:
141 static SP<CTestMouse> create(bool isVirtual) {
142 auto maus = SP<CTestMouse>(new CTestMouse());
143 maus->m_self = maus;
144 maus->m_isVirtual = isVirtual;
145 maus->m_deviceName = "test-mouse";
146 maus->m_hlName = "test-mouse";
147 return maus;
148 }
149
150 virtual bool isVirtual() {
151 return m_isVirtual;
152 }
153
154 virtual SP<Aquamarine::IPointer> aq() {
155 return nullptr;
156 }
157
158 void destroy() {
159 m_events.destroy.emit();
160 }
161
162 private:
163 bool m_isVirtual = false;
164 };
165
166 SP<CTestMouse> g_mouse;
167 SP<CTestKeyboard> g_keyboard;
168 SP<CTestKeyboard> g_keyboard2;
169
170 static SDispatchResult pressAlt(std::string in) {
171 g_pInputManager->m_lastMods = in == "1" ? HL_MODIFIER_ALT : 0;
172
173 return {.success = true};
174 }
175
176 static SDispatchResult simulateGesture(std::string in) {
177 CVarList data(in);
178
179 uint32_t fingers = 3;
180 try {
181 fingers = std::stoul(data[1]);
182 } catch (...) { return {.success = false}; }
183
184 if (data[0] == "down") {
185 g_pTrackpadGestures->gestureBegin(IPointer::SSwipeBeginEvent{});
186 g_pTrackpadGestures->gestureUpdate(IPointer::SSwipeUpdateEvent{.fingers = fingers, .delta = {0, 300}});
187 g_pTrackpadGestures->gestureEnd(IPointer::SSwipeEndEvent{});
188 } else if (data[0] == "up") {
189 g_pTrackpadGestures->gestureBegin(IPointer::SSwipeBeginEvent{});
190 g_pTrackpadGestures->gestureUpdate(IPointer::SSwipeUpdateEvent{.fingers = fingers, .delta = {0, -300}});
191 g_pTrackpadGestures->gestureEnd(IPointer::SSwipeEndEvent{});
192 } else if (data[0] == "left") {
193 g_pTrackpadGestures->gestureBegin(IPointer::SSwipeBeginEvent{});
194 g_pTrackpadGestures->gestureUpdate(IPointer::SSwipeUpdateEvent{.fingers = fingers, .delta = {-300, 0}});
195 g_pTrackpadGestures->gestureEnd(IPointer::SSwipeEndEvent{});
196 } else {
197 g_pTrackpadGestures->gestureBegin(IPointer::SSwipeBeginEvent{});
198 g_pTrackpadGestures->gestureUpdate(IPointer::SSwipeUpdateEvent{.fingers = fingers, .delta = {300, 0}});
199 g_pTrackpadGestures->gestureEnd(IPointer::SSwipeEndEvent{});
200 }
201
202 return {.success = true};
203 }
204
205 static SDispatchResult pinchUpdate(std::string in) {
206 CVarList data(in);
207 uint32_t fingers = 2;
208 double scale = 1.0;
209 Vector2D delta = {};
210 double rotation{};
211
212 if (data.size() < 2)
213 return {.success = false, .error = "invalid input"};
214
215 if (const auto n = strToNumber<uint32_t>(data[0]); n)
216 fingers = n.value();
217 else
218 return {.success = false, .error = "invalid input"};
219
220 if (const auto n = strToNumber<double>(data[1]); n)
221 scale = n.value();
222 else
223 return {.success = false, .error = "invalid input"};
224
225 if (data.size() > 2) {
226 if (const auto n = strToNumber<double>(data[2]); n)
227 delta.x = n.value();
228 else
229 return {.success = false, .error = "invalid input"};
230 }
231
232 if (data.size() > 3) {
233 if (const auto n = strToNumber<double>(data[3]); n)
234 delta.y = n.value();
235 else
236 return {.success = false, .error = "invalid input"};
237 }
238
239 if (data.size() > 4) {
240 if (const auto n = strToNumber<double>(data[4]); n)
241 rotation = n.value();
242 else
243 return {.success = false, .error = "invalid input"};
244 }
245
246 g_pTrackpadGestures->gestureUpdate(IPointer::SPinchUpdateEvent{
247 .fingers = fingers,
248 .delta = delta,
249 .scale = scale,
250 .rotation = rotation,
251 });
252
253 return {};
254 }
255
256 static SDispatchResult pinchEnd(std::string in) {
257 g_pTrackpadGestures->gestureEnd(IPointer::SPinchEndEvent{});
258
259 return {};
260 }
261
262 static SDispatchResult expectCursorZoom(std::string in) {
263 CVarList data(in);
264 float expected = 1.F;
265 float delta = 0.01F;
266
267 if (data.size() < 1)
268 return {.success = false, .error = "invalid input"};
269
270 if (const auto n = strToNumber<float>(data[0]); n)
271 expected = n.value();
272 else
273 return {.success = false, .error = "invalid input"};
274
275 if (data.size() > 1) {
276 if (const auto n = strToNumber<float>(data[1]); n)
277 delta = n.value();
278 else
279 return {.success = false, .error = "invalid input"};
280 }
281
282 const auto PMONITOR = State::monitorState()->query().vec(g_pInputManager->getMouseCoordsInternal()).run();
283
284 if (!PMONITOR)
285 return {.success = false, .error = "No monitor under cursor"};
286
287 const auto actual = PMONITOR->m_cursorZoom->value();
288
289 if (std::abs(actual - expected) > delta)
290 return {.success = false, .error = std::format("Expected cursor zoom {} ± {}, got {}", expected, delta, actual)};
291
292 return {};
293 }
294
295 static SDispatchResult vkb(std::string in) {
296 auto tkb0 = CTestKeyboard::create(false);
297 auto tkb1 = CTestKeyboard::create(false);
298 auto vkb0 = CTestKeyboard::create(true);
299
300 g_pInputManager->newKeyboard(tkb0);
301 g_pInputManager->newKeyboard(tkb1);
302 g_pInputManager->newKeyboard(vkb0);
303
304 CScopeGuard x([&] {
305 tkb0->destroy();
306 tkb1->destroy();
307 vkb0->destroy();
308 });
309
310 const auto& PRESSED = g_pInputManager->getKeysFromAllKBs();
311 const uint32_t TESTKEY = 1;
312
313 tkb0->sendKey(TESTKEY, true);
314 if (!std::ranges::contains(PRESSED, TESTKEY)) {
315 return {
316 .success = false,
317 .error = "Expected pressed key not found",
318 };
319 }
320
321 tkb1->sendKey(TESTKEY, true);
322 tkb0->sendKey(TESTKEY, false);
323 if (!std::ranges::contains(PRESSED, TESTKEY)) {
324 return {
325 .success = false,
326 .error = "Expected pressed key not found (kb share state)",
327 };
328 }
329
330 vkb0->sendKey(TESTKEY, true);
331 tkb1->sendKey(TESTKEY, false);
332 if (std::ranges::contains(PRESSED, TESTKEY)) {
333 return {
334 .success = false,
335 .error = "Expected released key found in pressed (vkb no share state)",
336 };
337 }
338
339 return {};
340 }
341
342 static SDispatchResult scroll(std::string in) {
343 double by;
344 try {
345 by = std::stod(in);
346 } catch (...) { return SDispatchResult{.success = false, .error = "invalid input"}; }
347
348 Log::logger->log(Log::DEBUG, "tester: scrolling by {}", by);
349
350 g_mouse->m_pointerEvents.axis.emit(IPointer::SAxisEvent{
351 .delta = by,
352 .deltaDiscrete = 120,
353 .mouse = true,
354 });
355
356 return {};
357 }
358
359 static SDispatchResult click(std::string in) {
360 CVarList2 data(std::move(in));
361
362 uint32_t button;
363 bool pressed;
364 try {
365 button = std::stoul(std::string{data[0]});
366 pressed = std::stoul(std::string{data[1]}) == 1;
367 } catch (...) { return {.success = false, .error = "invalid input"}; }
368
369 Log::logger->log(Log::DEBUG, "tester: mouse button {} state {}", button, pressed);
370
371 g_mouse->m_pointerEvents.button.emit(IPointer::SButtonEvent{
372 .timeMs = sc<uint32_t>(Time::millis(Time::steadyNow())),
373 .button = button,
374 .state = pressed ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED,
375 .mouse = true,
376 });
377
378 return {};
379 }
380
381 static SDispatchResult keybind(std::string in) {
382 CVarList2 data(std::move(in));
383 // 0 = release, 1 = press
384 bool press;
385 // See src/devices/IKeyboard.hpp : eKeyboardModifiers for modifier bitmasks
386 // 0 = none, eKeyboardModifiers is shifted to start at 1
387 uint32_t modifier;
388 // keycode
389 uint32_t key;
390 try {
391 press = std::stoul(std::string{data[0]}) == 1;
392 modifier = std::stoul(std::string{data[1]});
393 key = std::stoul(std::string{data[2]}) - 8; // xkb offset
394 } catch (...) { return {.success = false, .error = "invalid input"}; }
395
396 uint32_t modifierMask = 0;
397 if (modifier > 0)
398 modifierMask = 1 << (modifier - 1);
399 g_pInputManager->m_lastMods = modifierMask;
400 g_keyboard->sendKey(key, press);
401
402 return {};
403 }
404
405 static SDispatchResult keybind2(std::string in) {
406 CVarList2 data(std::move(in));
407 bool press;
408 uint32_t modifier;
409 uint32_t key;
410 try {
411 press = std::stoul(std::string{data[0]}) == 1;
412 modifier = std::stoul(std::string{data[1]});
413 key = std::stoul(std::string{data[2]}) - 8;
414 } catch (...) { return {.success = false, .error = "invalid input"}; }
415
416 uint32_t modifierMask = 0;
417 if (modifier > 0)
418 modifierMask = 1 << (modifier - 1);
419 g_pInputManager->m_lastMods = modifierMask;
420 g_keyboard2->sendKey(key, press);
421
422 return {};
423 }
424
425 static SDispatchResult keybindModmask(std::string in) {
426 CVarList2 data(std::move(in));
427 // 0 = release, 1 = press
428 bool press;
429 // See src/devices/IKeyboard.hpp : eKeyboardModifiers for modifier bitmasks
430 // 0 = none, eKeyboardModifiers is shifted to start at 1
431 uint32_t modifierMask;
432 // keycode
433 uint32_t key;
434 try {
435 press = std::stoul(std::string{data[0]}) == 1;
436 modifierMask = std::stoul(std::string{data[1]});
437 key = std::stoul(std::string{data[2]}) - 8; // xkb offset
438 } catch (...) { return {.success = false, .error = "invalid input"}; }
439
440 g_pInputManager->m_lastMods = modifierMask;
441 g_keyboard->setMods(modifierMask, 0, 0, 0);
442 g_keyboard->sendKey(key, press);
443
444 return {};
445 }
446
447 static SDispatchResult setMods(std::string in) {
448 CVarList2 data(std::move(in));
449 try {
450 uint32_t kbIndex = std::stoul(std::string{data[0]});
451 uint32_t depressed = std::stoul(std::string{data[1]});
452 uint32_t latched = std::stoul(std::string{data[2]});
453 uint32_t locked = std::stoul(std::string{data[3]});
454 uint32_t group = std::stoul(std::string{data[4]});
455
456 SP<CTestKeyboard> kb = (kbIndex == 0) ? g_keyboard : g_keyboard2;
457 kb->setMods(depressed, latched, locked, group);
458 } catch (...) { return {.success = false, .error = "invalid input"}; }
459
460 return {};
461 }
462
463 static SDispatchResult nullfocus(std::string in) {
464 g_pSeatManager->setKeyboardFocus(nullptr);
465 return {};
466 }
467
468 static SDispatchResult clearSurfaceFocus(std::string in) {
469 Desktop::focusState()->m_focusSurface.reset();
470 return {};
471 }
472
473 static SDispatchResult checkKeyboardFocusWindow(std::string in) {
474 const auto KBSURF = g_pSeatManager->m_state.keyboardFocus.lock();
475 if (!KBSURF)
476 return {.success = false, .error = "No keyboard focus"};
477
478 const auto PWINDOW = Desktop::focusState()->window();
479 if (!PWINDOW)
480 return {.success = false, .error = "Keyboard focus surface is not a window"};
481
482 if (PWINDOW->m_class != in)
483 return {.success = false, .error = std::format("Keyboard focus window class is '{}', expected '{}'", PWINDOW->m_class, in)};
484
485 return {};
486 }
487
488 static Desktop::Rule::CWindowRuleEffectContainer::storageType windowRuleIDX = 0;
489
490 //
491 static SDispatchResult addWindowRule(std::string in) {
492 windowRuleIDX = Desktop::Rule::windowEffects()->registerEffect("plugin_rule");
493
494 if (Desktop::Rule::windowEffects()->registerEffect("plugin_rule") != windowRuleIDX)
495 return {.success = false, .error = "re-registering returned a different id?"};
496 return {};
497 }
498
499 static SDispatchResult checkWindowRule(std::string in) {
500 const auto PLASTWINDOW = Desktop::focusState()->window();
501
502 if (!PLASTWINDOW)
503 return {.success = false, .error = "No window"};
504
505 if (!PLASTWINDOW->m_ruleApplicator->m_otherProps.props.contains(windowRuleIDX))
506 return {.success = false, .error = "No rule"};
507
508 if (PLASTWINDOW->m_ruleApplicator->m_otherProps.props[windowRuleIDX]->effect != "effect")
509 return {.success = false, .error = "Effect isn't \"effect\""};
510
511 return {};
512 }
513
514 static Desktop::Rule::CLayerRuleEffectContainer::storageType layerRuleIDX = 0;
515
516 static SDispatchResult addLayerRule(std::string in) {
517 layerRuleIDX = Desktop::Rule::layerEffects()->registerEffect("plugin_rule");
518
519 if (Desktop::Rule::layerEffects()->registerEffect("plugin_rule") != layerRuleIDX)
520 return {.success = false, .error = "re-registering returned a different id?"};
521 return {};
522 }
523
524 static SDispatchResult checkLayerRule(std::string in) {
525 if (Desktop::layerState()->layers().size() != 3)
526 return {.success = false, .error = "Layers under test not here"};
527
528 for (const auto& layer : Desktop::layerState()->layers()) {
529 if (layer->m_namespace == "rule-layer") {
530
531 if (!layer->m_ruleApplicator->m_otherProps.props.contains(layerRuleIDX))
532 return {.success = false, .error = "No rule"};
533
534 if (layer->m_ruleApplicator->m_otherProps.props[layerRuleIDX]->effect != "effect")
535 return {.success = false, .error = "Effect isn't \"effect\""};
536
537 } else if (layer->m_namespace == "norule-layer") {
538
539 if (layer->m_ruleApplicator->m_otherProps.props.contains(layerRuleIDX))
540 return {.success = false, .error = "Rule even though it shouldn't"};
541
542 } else
543 return {.success = false, .error = "Unrecognized layer"};
544 }
545
546 return {};
547 }
548
549 static SDispatchResult checkPointerFocusLayer(std::string in) {
550 const auto POINTERSURF = g_pSeatManager->m_state.pointerFocus.lock();
551
552 if (!POINTERSURF)
553 return {.success = false, .error = "No pointer focus"};
554
555 const auto HLSURF = Desktop::View::CWLSurface::fromResource(POINTERSURF);
556 const auto VIEW = HLSURF ? HLSURF->view() : nullptr;
557 const auto LAYER = Desktop::View::CLayerSurface::fromView(VIEW);
558
559 if (!LAYER) {
560 const auto WINDOW = Desktop::viewState()->query().type(Desktop::View::VIEW_TYPE_WINDOW).surface(POINTERSURF).runWindow();
561 if (WINDOW)
562 return {.success = false, .error = std::format("Pointer focus is a window surface with class '{}'", WINDOW->m_class)};
563
564 return {.success = false, .error = std::format("Pointer focus is not a layer surface, view type is {}", VIEW ? sc<int>(VIEW->type()) : -1)};
565 }
566
567 if (LAYER->m_namespace != in)
568 return {.success = false, .error = std::format("Pointer focus layer namespace is '{}', expected '{}'", LAYER->m_namespace, in)};
569
570 return {};
571 }
572
573 static SDispatchResult setPointerFocusLayer(std::string in) {
574 for (const auto& layer : Desktop::layerState()->layers()) {
575 if (layer->m_namespace != in)
576 continue;
577
578 const auto SURFACE = layer->wlSurface() ? layer->wlSurface()->resource() : nullptr;
579 if (!SURFACE)
580 return {.success = false, .error = std::format("Layer '{}' has no surface", in)};
581
582 const auto LOCAL = layer->m_geometry.size() / 2.0;
583
584 g_pSeatManager->setPointerFocus(SURFACE, LOCAL);
585 g_pSeatManager->sendPointerMotion(Time::millis(Time::steadyNow()), LOCAL);
586 return {};
587 }
588
589 return {.success = false, .error = std::format("No layer with namespace '{}'", in)};
590 }
591
592 static SDispatchResult softFocusWindowByClass(std::string in) {
593 for (const auto& window : Desktop::windowState()->windows()) {
594 if (window->m_class != in)
595 continue;
596
597 Desktop::focusState()->rawWindowFocus(window, Desktop::FOCUS_REASON_FFM);
598 return {};
599 }
600
601 return {.success = false, .error = std::format("No window with class '{}'", in)};
602 }
603
604 static SDispatchResult floatingFocusOnFullscreen(std::string in) {
605 const auto PLASTWINDOW = Desktop::focusState()->window();
606
607 if (!PLASTWINDOW)
608 return {.success = false, .error = "No window"};
609
610 if (!PLASTWINDOW->m_isFloating)
611 return {.success = false, .error = "Window must be floating"};
612
613 if (PLASTWINDOW->alphaTotalGoal() != 1.F)
614 return {.success = false, .error = "floating window doesnt restore it opacity when focused on fullscreen workspace"};
615
616 if (!PLASTWINDOW->m_allowedOverFullscreen)
617 return {.success = false, .error = "floating window doesnt get flagged as allowedOverFullscreen"};
618
619 return {};
620 }
621
622 static int luaResult(lua_State* L, const SDispatchResult& result) {
623 if (result.success)
624 return 0;
625
626 lua_pushstring(L, result.error.empty() ? "plugin function failed" : result.error.c_str());
627 return lua_error(L);
628 }
629
630 static int luaTest(lua_State* L) {
631 return luaResult(L, ::test(""));
632 }
633
634 static int luaSnapMove(lua_State* L) {
635 return luaResult(L, ::snapMove(""));
636 }
637
638 static int luaDragWindow(lua_State* L) {
639 const auto cls = std::string{luaL_checkstring(L, 1)};
640 const auto x = (double)luaL_checknumber(L, 2);
641 const auto y = (double)luaL_checknumber(L, 3);
642 return luaResult(L, ::dragWindow(std::format("{},{},{}", cls, x, y)));
643 }
644
645 static int luaVkb(lua_State* L) {
646 return luaResult(L, ::vkb(""));
647 }
648
649 static int luaAlt(lua_State* L) {
650 return luaResult(L, ::pressAlt(std::to_string((int)luaL_checkinteger(L, 1))));
651 }
652
653 static int luaGesture(lua_State* L) {
654 const auto direction = std::string{luaL_checkstring(L, 1)};
655 const auto fingers = (int)luaL_optinteger(L, 2, 3);
656 return luaResult(L, ::simulateGesture(std::format("{},{}", direction, fingers)));
657 }
658
659 static int luaPinchUpdate(lua_State* L) {
660 std::string in = std::format("{},{}", (int)luaL_checkinteger(L, 1), (double)luaL_checknumber(L, 2));
661
662 if (lua_gettop(L) > 2)
663 in += std::format(",{}", (double)luaL_checknumber(L, 3));
664 if (lua_gettop(L) > 3)
665 in += std::format(",{}", (double)luaL_checknumber(L, 4));
666 if (lua_gettop(L) > 4)
667 in += std::format(",{}", (double)luaL_checknumber(L, 5));
668
669 return luaResult(L, ::pinchUpdate(in));
670 }
671
672 static int luaPinchEnd(lua_State* L) {
673 return luaResult(L, ::pinchEnd(""));
674 }
675
676 static int luaExpectCursorZoom(lua_State* L) {
677 const auto expected = (double)luaL_checknumber(L, 1);
678
679 if (lua_gettop(L) > 1)
680 return luaResult(L, ::expectCursorZoom(std::format("{},{}", expected, (double)luaL_checknumber(L, 2))));
681
682 return luaResult(L, ::expectCursorZoom(std::format("{}", expected)));
683 }
684
685 static int luaScroll(lua_State* L) {
686 return luaResult(L, ::scroll(std::to_string((double)luaL_checknumber(L, 1))));
687 }
688
689 static int luaClick(lua_State* L) {
690 const auto button = (int)luaL_checkinteger(L, 1);
691 const auto pressed = (int)luaL_checkinteger(L, 2);
692 return luaResult(L, ::click(std::format("{},{}", button, pressed)));
693 }
694
695 static int luaKeybind(lua_State* L) {
696 const auto press = (int)luaL_checkinteger(L, 1);
697 const auto modifier = (int)luaL_checkinteger(L, 2);
698 const auto key = (int)luaL_checkinteger(L, 3);
699 return luaResult(L, ::keybind(std::format("{},{},{}", press, modifier, key)));
700 }
701
702 static int luaKeybind2(lua_State* L) {
703 const auto press = (int)luaL_checkinteger(L, 1);
704 const auto modifier = (int)luaL_checkinteger(L, 2);
705 const auto key = (int)luaL_checkinteger(L, 3);
706 return luaResult(L, ::keybind2(std::format("{},{},{}", press, modifier, key)));
707 }
708
709 static int luaKeybindMask(lua_State* L) {
710 const auto press = (int)luaL_checkinteger(L, 1);
711 const auto modifierMask = (int)luaL_checkinteger(L, 2);
712 const auto key = (int)luaL_checkinteger(L, 3);
713 return luaResult(L, ::keybindModmask(std::format("{},{},{}", press, modifierMask, key)));
714 }
715
716 static int luaSetMods(lua_State* L) {
717 const auto kbIndex = (int)luaL_checkinteger(L, 1);
718 const auto depressed = (int)luaL_checkinteger(L, 2);
719 const auto latched = (int)luaL_checkinteger(L, 3);
720 const auto locked = (int)luaL_checkinteger(L, 4);
721 const auto group = (int)luaL_checkinteger(L, 5);
722 return luaResult(L, ::setMods(std::format("{},{},{},{},{}", kbIndex, depressed, latched, locked, group)));
723 }
724
725 static int luaNullfocus(lua_State* L) {
726 return luaResult(L, ::nullfocus(""));
727 }
728
729 static int luaClearSurfaceFocus(lua_State* L) {
730 return luaResult(L, ::clearSurfaceFocus(""));
731 }
732
733 static int luaCheckKeyboardFocusWindow(lua_State* L) {
734 return luaResult(L, ::checkKeyboardFocusWindow(luaL_checkstring(L, 1)));
735 }
736
737 static int luaAddWindowRule(lua_State* L) {
738 return luaResult(L, ::addWindowRule(""));
739 }
740
741 static int luaCheckWindowRule(lua_State* L) {
742 return luaResult(L, ::checkWindowRule(""));
743 }
744
745 static int luaAddLayerRule(lua_State* L) {
746 return luaResult(L, ::addLayerRule(""));
747 }
748
749 static int luaCheckLayerRule(lua_State* L) {
750 return luaResult(L, ::checkLayerRule(""));
751 }
752
753 static int luaCheckPointerFocusLayer(lua_State* L) {
754 return luaResult(L, ::checkPointerFocusLayer(luaL_checkstring(L, 1)));
755 }
756
757 static int luaSetPointerFocusLayer(lua_State* L) {
758 return luaResult(L, ::setPointerFocusLayer(luaL_checkstring(L, 1)));
759 }
760
761 static int luaSoftFocusWindowByClass(lua_State* L) {
762 return luaResult(L, ::softFocusWindowByClass(luaL_checkstring(L, 1)));
763 }
764
765 static int luaFloatingFocusOnFullscreen(lua_State* L) {
766 return luaResult(L, ::floatingFocusOnFullscreen(""));
767 }
768
769 APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
770 PHANDLE = handle;
771
772 auto addLuaFn = [](const std::string& name, PLUGIN_LUA_FN fn) {
773 if (!HyprlandAPI::addLuaFunction(PHANDLE, "test", name, fn))
774 Log::logger->log(Log::ERR, "hyprtester plugin: failed to register hl.plugin.test.{}", name);
775 };
776
777 addLuaFn("test", ::luaTest);
778 addLuaFn("snapmove", ::luaSnapMove);
779 addLuaFn("drag_window", ::luaDragWindow);
780 addLuaFn("vkb", ::luaVkb);
781 addLuaFn("alt", ::luaAlt);
782 addLuaFn("gesture", ::luaGesture);
783 addLuaFn("pinch_update", ::luaPinchUpdate);
784 addLuaFn("pinch_end", ::luaPinchEnd);
785 addLuaFn("expect_cursor_zoom", ::luaExpectCursorZoom);
786 addLuaFn("scroll", ::luaScroll);
787 addLuaFn("click", ::luaClick);
788 addLuaFn("keybind", ::luaKeybind);
789 addLuaFn("keybind2", ::luaKeybind2);
790 addLuaFn("keybind_modmask", ::luaKeybindMask);
791 addLuaFn("set_mods", ::luaSetMods);
792 addLuaFn("nullfocus", ::luaNullfocus);
793 addLuaFn("clear_surface_focus", ::luaClearSurfaceFocus);
794 addLuaFn("check_keyboard_focus_window", ::luaCheckKeyboardFocusWindow);
795 addLuaFn("add_window_rule", ::luaAddWindowRule);
796 addLuaFn("check_window_rule", ::luaCheckWindowRule);
797 addLuaFn("add_layer_rule", ::luaAddLayerRule);
798 addLuaFn("check_layer_rule", ::luaCheckLayerRule);
799 addLuaFn("check_pointer_focus_layer", ::luaCheckPointerFocusLayer);
800 addLuaFn("set_pointer_focus_layer", ::luaSetPointerFocusLayer);
801 addLuaFn("window_soft_focus", ::luaSoftFocusWindowByClass);
802 addLuaFn("floating_focus_on_fullscreen", ::luaFloatingFocusOnFullscreen);
803
804 // init mouse
805 g_mouse = CTestMouse::create(false);
806 g_pInputManager->newMouse(g_mouse);
807
808 // init keyboard
809 g_keyboard = CTestKeyboard::create(false);
810 g_pInputManager->newKeyboard(g_keyboard);
811
812 // init keyboard2
813 g_keyboard2 = CTestKeyboard::create(false);
814 g_pInputManager->newKeyboard(g_keyboard2);
815
816 return {"hyprtestplugin", "hyprtestplugin", "Vaxry", "1.0"};
817 }
818
819 APICALL EXPORT void PLUGIN_EXIT() {
820 g_mouse->destroy();
821 g_mouse.reset();
822 g_keyboard->destroy();
823 g_keyboard.reset();
824 g_keyboard2->destroy();
825 g_keyboard2.reset();
826 }