Debian Codegraph @

hyprtester/src/tests/main/keybinds.cpp

raw
1 #include <filesystem>
2 #include <linux/input-event-codes.h>
3 #include <thread>
4 #include "../../shared.hpp"
5 #include "../../hyprctlCompat.hpp"
6 #include "../shared.hpp"
7 #include "tests.hpp"
8
9 using namespace Hyprutils::OS;
10 using namespace Hyprutils::Memory;
11 static std::string flagFile = "/tmp/hyprtester-keybinds.txt";
12
13 static std::string pluginKeybindCmd(bool pressed, uint32_t modifier, uint32_t key) {
14 return "/eval hl.plugin.test.keybind(" + std::to_string(pressed ? 1 : 0) + ", " + std::to_string(modifier) + ", " + std::to_string(key) + ")";
15 }
16
17 static std::string pluginKeybindMaskCmd(bool pressed, const std::vector<uint8_t>& mods, uint32_t key) {
18 uint32_t mask = 0;
19 for (auto m : mods)
20 mask |= (1 << (m - 1));
21
22 return "/eval hl.plugin.test.keybind_modmask(" + std::to_string(pressed ? 1 : 0) + ", " + std::to_string(mask) + ", " + std::to_string(key) + ")";
23 }
24
25 static std::string pluginScrollCmd(int delta) {
26 return "/eval hl.plugin.test.scroll(" + std::to_string(delta) + ")";
27 }
28
29 static std::string pluginClickCmd(bool pressed, uint32_t button) {
30 return "/eval hl.plugin.test.click(" + std::to_string(button) + ", " + std::to_string(pressed ? 1 : 0) + ")";
31 }
32
33 // Because i don't feel like changing someone elses code.
34 enum eKeyboardModifierIndex : uint8_t {
35 MOD_SHIFT = 1,
36 MOD_CAPS,
37 MOD_CTRL,
38 MOD_ALT,
39 MOD_MOD2,
40 MOD_MOD3,
41 MOD_META,
42 MOD_MOD5
43 };
44
45 static void clearFlag() {
46 std::filesystem::remove(flagFile);
47 }
48
49 static bool checkFlag() {
50 bool exists = std::filesystem::exists(flagFile);
51 clearFlag();
52 return exists;
53 }
54
55 static bool attemptCheckFlag(int attempts, int intervalMs) {
56 for (int i = 0; i < attempts; i++) {
57 if (checkFlag())
58 return true;
59
60 std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs));
61 }
62
63 return false;
64 }
65
66 static std::string readKittyOutput() {
67 std::string output = Tests::execAndGet("kitten @ --to unix:/tmp/hyprtester-kitty.sock get-text --extent all");
68 // chop off shell prompt
69 std::size_t pos = output.rfind("$");
70 if (pos != std::string::npos) {
71 pos += 1;
72 if (pos < output.size())
73 output.erase(0, pos);
74 }
75 // NLog::log("Kitty output: '{}'", output);
76 return output;
77 }
78
79 static void awaitKittyPrompt() {
80 // wait until we see the shell prompt, meaning it's ready for test inputs
81 for (int i = 0; i < 10; i++) {
82 std::string output = Tests::execAndGet("kitten @ --to unix:/tmp/hyprtester-kitty.sock get-text --extent all");
83 if (output.rfind("$") == std::string::npos) {
84 std::this_thread::sleep_for(std::chrono::milliseconds(200));
85 continue;
86 }
87 return;
88 }
89 NLog::log("{}Error: timed out waiting for kitty prompt", Colors::RED);
90 }
91
92 static CUniquePointer<CProcess> spawnRemoteControlKitty() {
93 auto kittyProc = Tests::spawnKitty("keybinds_test", {"-o", "allow_remote_control=yes", "--listen-on", "unix:/tmp/hyprtester-kitty.sock", "--config", "NONE", "/bin/sh"});
94 // wait a bit to ensure shell prompt is sent, we are going to read the text after it
95 std::this_thread::sleep_for(std::chrono::milliseconds(100));
96 if (kittyProc)
97 awaitKittyPrompt();
98 return kittyProc;
99 }
100
101 // All the `SUBTEST`s below are supposed to be independent `TEST_CASE`s.
102 // But if isolated trivially, some of them fail.
103 // TODO: investigate and isolate tests by turning `SUBTEST`s into `TEST_CASE`s.
104
105 SUBTEST(bind) {
106 EXPECT(checkFlag(), false);
107 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'))"), "ok");
108 // press keybind
109 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
110 // await flag
111 EXPECT(attemptCheckFlag(20, 50), true);
112 // release keybind
113 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
114 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
115 }
116
117 SUBTEST(bindKey) {
118 EXPECT(checkFlag(), false);
119 EXPECT(getFromSocket("/eval hl.bind('Y', hl.dsp.exec_cmd('touch " + flagFile + "'))"), "ok");
120 // press keybind
121 OK(getFromSocket(pluginKeybindCmd(true, 0, 29)));
122 // await flag
123 EXPECT(attemptCheckFlag(20, 50), true);
124 // release keybind
125 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
126 EXPECT(getFromSocket("/eval hl.unbind('Y')"), "ok");
127 }
128
129 SUBTEST(longPress) {
130 EXPECT(checkFlag(), false);
131 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { long_press = true })"), "ok");
132 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
133 // press keybind
134 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
135 // check no flag on short press
136 std::this_thread::sleep_for(std::chrono::milliseconds(50));
137 EXPECT(checkFlag(), false);
138 // await repeat delay
139 std::this_thread::sleep_for(std::chrono::milliseconds(200));
140 EXPECT(checkFlag(), true);
141 // release keybind
142 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
143 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
144 }
145 SUBTEST(keyLongPress) {
146 EXPECT(checkFlag(), false);
147 EXPECT(getFromSocket("/eval hl.bind('Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { long_press = true })"), "ok");
148 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
149 // press keybind
150 OK(getFromSocket(pluginKeybindCmd(true, 0, 29)));
151 // check no flag on short press
152 std::this_thread::sleep_for(std::chrono::milliseconds(50));
153 EXPECT(checkFlag(), false);
154 // await repeat delay
155 std::this_thread::sleep_for(std::chrono::milliseconds(200));
156 EXPECT(checkFlag(), true);
157 // release keybind
158 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
159 EXPECT(getFromSocket("/eval hl.unbind('Y')"), "ok");
160 }
161
162 SUBTEST(longPressRelease) {
163 EXPECT(checkFlag(), false);
164 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { long_press = true })"), "ok");
165 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
166 // press keybind
167 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
168 // check no flag on short press
169 std::this_thread::sleep_for(std::chrono::milliseconds(50));
170 EXPECT(checkFlag(), false);
171 // release keybind
172 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
173 // await repeat delay
174 std::this_thread::sleep_for(std::chrono::milliseconds(200));
175 EXPECT(checkFlag(), false);
176 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
177 }
178 SUBTEST(longPressOnlyKeyRelease) {
179 EXPECT(checkFlag(), false);
180 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { long_press = true })"), "ok");
181 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
182 // press keybind
183 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
184 // check no flag on short press
185 std::this_thread::sleep_for(std::chrono::milliseconds(50));
186 EXPECT(checkFlag(), false);
187 // release key, keep modifier
188 OK(getFromSocket(pluginKeybindCmd(false, 7, 29)));
189 // await repeat delay
190 std::this_thread::sleep_for(std::chrono::milliseconds(200));
191 EXPECT(checkFlag(), false);
192 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
193 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
194 }
195
196 SUBTEST(repeat) {
197 EXPECT(checkFlag(), false);
198 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { repeating = true })"), "ok");
199 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
200 // press keybind
201 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
202 // await flag
203 std::this_thread::sleep_for(std::chrono::milliseconds(200));
204 EXPECT(checkFlag(), true);
205 // await repeat delay
206 std::this_thread::sleep_for(std::chrono::milliseconds(200));
207 EXPECT(checkFlag(), true);
208 // check that it continues repeating
209 std::this_thread::sleep_for(std::chrono::milliseconds(200));
210 EXPECT(checkFlag(), true);
211 // release keybind
212 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
213 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
214 }
215
216 SUBTEST(keyRepeat) {
217 // wait until flag becomes false (CI timing can vary)
218 bool ok = false;
219 for (int i = 0; i < 20; ++i) {
220 if (!checkFlag()) {
221 ok = true;
222 break;
223 }
224 std::this_thread::sleep_for(std::chrono::milliseconds(10));
225 }
226 EXPECT(ok, true);
227
228 EXPECT(getFromSocket("/eval hl.bind('Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { repeating = true })"), "ok");
229 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
230 // press keybind
231 OK(getFromSocket(pluginKeybindCmd(true, 0, 29)));
232 // await flag
233 std::this_thread::sleep_for(std::chrono::milliseconds(50));
234 EXPECT(checkFlag(), true);
235 // await repeat delay
236 std::this_thread::sleep_for(std::chrono::milliseconds(200));
237 EXPECT(checkFlag(), true);
238 // check that it continues repeating
239 std::this_thread::sleep_for(std::chrono::milliseconds(200));
240 EXPECT(checkFlag(), true);
241 // release keybind
242 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
243 EXPECT(getFromSocket("/eval hl.unbind('Y')"), "ok");
244 }
245
246 SUBTEST(repeatRelease) {
247 // wait until flag becomes false (CI timing can vary)
248 bool ok = false;
249 for (int i = 0; i < 20; ++i) {
250 if (!checkFlag()) {
251 ok = true;
252 break;
253 }
254 std::this_thread::sleep_for(std::chrono::milliseconds(10));
255 }
256
257 EXPECT(ok, true);
258 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { repeating = true })"), "ok");
259 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
260 // press keybind
261 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
262 // await flag
263 std::this_thread::sleep_for(std::chrono::milliseconds(50));
264 EXPECT(checkFlag(), true);
265 // release keybind
266 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
267 // await repeat delay
268 std::this_thread::sleep_for(std::chrono::milliseconds(200));
269 clearFlag();
270 std::this_thread::sleep_for(std::chrono::milliseconds(200));
271 EXPECT(checkFlag(), false);
272 // check that it is not repeating
273 std::this_thread::sleep_for(std::chrono::milliseconds(200));
274 EXPECT(checkFlag(), false);
275 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
276 }
277
278 SUBTEST(repeatOnlyKeyRelease) {
279 EXPECT(checkFlag(), false);
280 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { repeating = true })"), "ok");
281 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
282 // press keybind
283 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
284 // await flag
285 std::this_thread::sleep_for(std::chrono::milliseconds(200));
286 EXPECT(checkFlag(), true);
287 // release key, keep modifier
288 OK(getFromSocket(pluginKeybindCmd(false, 7, 29)));
289 // await repeat delay
290 std::this_thread::sleep_for(std::chrono::milliseconds(200));
291 clearFlag();
292 std::this_thread::sleep_for(std::chrono::milliseconds(200));
293 EXPECT(checkFlag(), false);
294 // check that it is not repeating
295 std::this_thread::sleep_for(std::chrono::milliseconds(200));
296 EXPECT(checkFlag(), false);
297 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
298 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
299 }
300
301 SUBTEST(shortcutBind) {
302 auto kittyProc = spawnRemoteControlKitty();
303 if (!kittyProc) {
304 FAIL_TEST("Could not spawn kitty");
305 }
306
307 // test SUPER.
308 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
309 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.send_shortcut({ mods = '', key = 'q', window = 'activewindow' }))"), "ok");
310 // press keybind
311 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
312 // release keybind
313 std::this_thread::sleep_for(std::chrono::milliseconds(50));
314 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
315 std::this_thread::sleep_for(std::chrono::milliseconds(50));
316 std::string output = readKittyOutput();
317 EXPECT_COUNT_STRING(output, "y", 0);
318 EXPECT(output.find("q") != std::string::npos, true);
319 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
320
321 // test SUPER + SHIFT (pick a different shortcut to avoid caching).
322 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
323 EXPECT(getFromSocket("/eval hl.bind('SUPER + SHIFT + Y', hl.dsp.send_shortcut({ mods = '', key = 'w', window = 'activewindow' }))"), "ok");
324 // press keybind
325 OK(getFromSocket(pluginKeybindMaskCmd(true, {MOD_META, MOD_SHIFT}, 29)));
326 // release keybind
327 std::this_thread::sleep_for(std::chrono::milliseconds(50));
328 OK(getFromSocket(pluginKeybindMaskCmd(false, {}, 29)));
329 std::this_thread::sleep_for(std::chrono::milliseconds(50));
330 output = readKittyOutput();
331 EXPECT_COUNT_STRING(output, "y", 0);
332 EXPECT_COUNT_STRING(output, "Y", 0);
333 EXPECT(output.find("w") != std::string::npos, true);
334 EXPECT(getFromSocket("/eval hl.unbind('SUPER + SHIFT + Y')"), "ok");
335
336 // test SUPER + SHIFT (check numbers (1 -> ! is not captured by case sensitivity)).
337 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
338 EXPECT(getFromSocket("/eval hl.bind('SUPER + SHIFT + Y', hl.dsp.send_shortcut({ mods = '', key = '1', window = 'activewindow' }))"), "ok");
339 // press keybind
340 OK(getFromSocket(pluginKeybindMaskCmd(true, {MOD_META, MOD_SHIFT}, 29)));
341 // release keybind
342 std::this_thread::sleep_for(std::chrono::milliseconds(50));
343 OK(getFromSocket(pluginKeybindMaskCmd(false, {}, 29)));
344 std::this_thread::sleep_for(std::chrono::milliseconds(50));
345 output = readKittyOutput();
346 EXPECT_COUNT_STRING(output, "y", 0);
347 EXPECT_COUNT_STRING(output, "Y", 0);
348 EXPECT(output.find("1") != std::string::npos, true);
349 EXPECT(getFromSocket("/eval hl.unbind('SUPER + SHIFT + Y')"), "ok");
350
351 Tests::killAllWindows();
352 }
353
354 SUBTEST(shortcutBindKey) {
355 auto kittyProc = spawnRemoteControlKitty();
356 if (!kittyProc) {
357 FAIL_TEST("Could not spawn kitty");
358 }
359
360 // test lowercase Y.
361 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
362 EXPECT(getFromSocket("/eval hl.bind('Y', hl.dsp.send_shortcut({ mods = '', key = 'e', window = 'activewindow' }))"), "ok");
363 // press keybind
364 OK(getFromSocket(pluginKeybindCmd(true, 0, 29)));
365 // release keybind
366 std::this_thread::sleep_for(std::chrono::milliseconds(50));
367 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
368 std::this_thread::sleep_for(std::chrono::milliseconds(50));
369 std::string output = readKittyOutput();
370 EXPECT_COUNT_STRING(output, "y", 0);
371 // disabled: doesn't work in CI
372 // EXPECT_COUNT_STRING(output, "e", 1);
373 EXPECT(getFromSocket("/eval hl.unbind('Y')"), "ok");
374
375 // test SHIFT + Y (pick a different shortcut to avoid caching).
376 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
377 EXPECT(getFromSocket("/eval hl.bind('SHIFT + Y', hl.dsp.send_shortcut({ mods = '', key = 'r', window = 'activewindow' }))"), "ok");
378 // press keybind
379 OK(getFromSocket(pluginKeybindMaskCmd(true, {MOD_SHIFT}, 29)));
380 // release keybind
381 std::this_thread::sleep_for(std::chrono::milliseconds(50));
382 OK(getFromSocket(pluginKeybindMaskCmd(false, {}, 29)));
383 std::this_thread::sleep_for(std::chrono::milliseconds(50));
384 output = readKittyOutput();
385 EXPECT_COUNT_STRING(output, "y", 0);
386 EXPECT_COUNT_STRING(output, "Y", 0);
387 // disabled: doesn't work in CI
388 // EXPECT_COUNT_STRING(output, "r", 1);
389 EXPECT(getFromSocket("/eval hl.unbind('SHIFT + Y')"), "ok");
390
391 // test SHIFT + Y (check numbers (2 -> @ is not captured by case sensitivity)).
392 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
393 EXPECT(getFromSocket("/eval hl.bind('SHIFT + Y', hl.dsp.send_shortcut({ mods = '', key = '2', window = 'activewindow' }))"), "ok");
394 // press keybind
395 OK(getFromSocket(pluginKeybindMaskCmd(true, {MOD_SHIFT}, 29)));
396 // release keybind
397 std::this_thread::sleep_for(std::chrono::milliseconds(50));
398 OK(getFromSocket(pluginKeybindMaskCmd(false, {}, 29)));
399 std::this_thread::sleep_for(std::chrono::milliseconds(50));
400 output = readKittyOutput();
401 EXPECT_COUNT_STRING(output, "y", 0);
402 EXPECT_COUNT_STRING(output, "Y", 0);
403 // disabled: doesn't work in CI
404 // EXPECT_COUNT_STRING(output, "2", 1);
405 EXPECT(getFromSocket("/eval hl.unbind('SHIFT + Y')"), "ok");
406
407 Tests::killAllWindows();
408 }
409
410 SUBTEST(shortcutLongPress) {
411 auto kittyProc = spawnRemoteControlKitty();
412 if (!kittyProc) {
413 FAIL_TEST("Could not spawn kitty");
414 }
415 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
416 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.send_shortcut({ mods = '', key = 'q', window = 'activewindow' }), { long_press = true })"), "ok");
417 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
418 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_rate = 10 } })"), "ok");
419 // press keybind
420 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
421 // await repeat delay
422 std::this_thread::sleep_for(std::chrono::milliseconds(200));
423 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
424 std::this_thread::sleep_for(std::chrono::milliseconds(200));
425 const std::string output = readKittyOutput();
426 int yCount = Tests::countOccurrences(output, "y");
427 // sometimes 1, sometimes 2, not sure why
428 // keybind press sends 1 y immediately
429 // then repeat triggers, sending 1 y
430 // final release stop repeats, and shouldn't send any more
431 EXPECT(true, yCount == 1 || yCount == 2);
432 EXPECT_COUNT_STRING(output, "q", 1);
433 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
434 Tests::killAllWindows();
435 }
436
437 SUBTEST(shortcutLongPressKeyRelease) {
438 auto kittyProc = spawnRemoteControlKitty();
439 if (!kittyProc) {
440 FAIL_TEST("Could not spawn kitty");
441 }
442 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
443 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.send_shortcut({ mods = '', key = 'q', window = 'activewindow' }), { long_press = true })"), "ok");
444 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 100 } })"), "ok");
445 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_rate = 10 } })"), "ok");
446 // press keybind
447 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
448 std::this_thread::sleep_for(std::chrono::milliseconds(10));
449 // release key, keep modifier
450 OK(getFromSocket(pluginKeybindCmd(false, 7, 29)));
451 // await repeat delay
452 std::this_thread::sleep_for(std::chrono::milliseconds(200));
453 const std::string output = readKittyOutput();
454 // disabled: doesn't work on CI
455 // EXPECT_COUNT_STRING(output, "y", 1);
456 EXPECT_COUNT_STRING(output, "q", 0);
457 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
458 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
459 Tests::killAllWindows();
460 }
461
462 SUBTEST(shortcutRepeat) {
463 auto kittyProc = spawnRemoteControlKitty();
464 if (!kittyProc) {
465 FAIL_TEST("Could not spawn kitty");
466 }
467 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
468 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.send_shortcut({ mods = '', key = 'q', window = 'activewindow' }), { repeating = true })"), "ok");
469 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_rate = 5 } })"), "ok");
470 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 200 } })"), "ok");
471 // press keybind
472 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
473 // await repeat
474 std::this_thread::sleep_for(std::chrono::milliseconds(210));
475 // release keybind
476 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
477 std::this_thread::sleep_for(std::chrono::milliseconds(450));
478 const std::string output = readKittyOutput();
479 EXPECT_COUNT_STRING(output, "y", 0);
480 int qCount = Tests::countOccurrences(output, "q");
481 // sometimes 2, sometimes 3, not sure why
482 // keybind press sends 1 q immediately
483 // then repeat triggers, sending 1 q
484 // final release stop repeats, and shouldn't send any more
485 EXPECT(true, qCount == 2 || qCount == 3);
486 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
487 Tests::killAllWindows();
488 }
489
490 SUBTEST(shortcutRepeatKeyRelease) {
491 auto kittyProc = spawnRemoteControlKitty();
492 if (!kittyProc) {
493 FAIL_TEST("Could not spawn kitty");
494 }
495 EXPECT(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:keybinds_test' })"), "ok");
496 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.send_shortcut({ mods = '', key = 'q', window = 'activewindow' }), { repeating = true })"), "ok");
497 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_rate = 5 } })"), "ok");
498 EXPECT(getFromSocket("r/eval hl.config({ input = { repeat_delay = 200 } })"), "ok");
499 // press keybind
500 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
501 std::this_thread::sleep_for(std::chrono::milliseconds(210));
502 // release key, keep modifier
503 OK(getFromSocket(pluginKeybindCmd(false, 7, 29)));
504 // if repeat was still active, we'd get 2 more q's here
505 std::this_thread::sleep_for(std::chrono::milliseconds(450));
506 // release modifier
507 const std::string output = readKittyOutput();
508 EXPECT_COUNT_STRING(output, "y", 0);
509 int qCount = Tests::countOccurrences(output, "q");
510 // sometimes 2, sometimes 3, not sure why
511 // keybind press sends 1 q immediately
512 // then repeat triggers, sending 1 q
513 // final release stop repeats, and shouldn't send any more
514 EXPECT(true, qCount == 2 || qCount == 3);
515 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
516 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
517 Tests::killAllWindows();
518 }
519
520 SUBTEST(submap) {
521 const auto press = [](const uint32_t key, const uint32_t mod = 0) {
522 // +8 because udev -> XKB keycode.
523 getFromSocket(pluginKeybindCmd(true, mod, key + 8));
524 getFromSocket(pluginKeybindCmd(false, mod, key + 8));
525 };
526
527 NLog::log("{}Testing submaps", Colors::GREEN);
528 // submap 1 no resets
529 press(KEY_U, MOD_META);
530 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
531 press(KEY_O);
532 Tests::waitUntilWindowsN(1);
533 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
534 // submap 2 resets to submap 1
535 press(KEY_U);
536 EXPECT_CONTAINS(getFromSocket("/submap"), "submap2");
537 press(KEY_O);
538 Tests::waitUntilWindowsN(2);
539 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
540 // submap 3 resets to default
541 press(KEY_I);
542 EXPECT_CONTAINS(getFromSocket("/submap"), "submap3");
543 press(KEY_O);
544 Tests::waitUntilWindowsN(3);
545 EXPECT_CONTAINS(getFromSocket("/submap"), "default");
546 // submap 1 reset via keybind
547 press(KEY_U, MOD_META);
548 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
549 press(KEY_P);
550 EXPECT_CONTAINS(getFromSocket("/submap"), "default");
551
552 // submap 1 with identical keybind bound inside it
553 press(KEY_U, MOD_META);
554 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
555 press(KEY_U, MOD_META);
556 EXPECT_CONTAINS(getFromSocket("/submap"), "submap3");
557 press(KEY_O);
558 Tests::waitUntilWindowsN(4);
559 EXPECT_CONTAINS(getFromSocket("/submap"), "default");
560 Tests::killAllWindows();
561 }
562
563 SUBTEST(submapMouseBinds) {
564 // regression #14856: scroll / mouse-button binds in a non-default submap stopped firing
565 NLog::log("{}Testing mouse binds inside a submap", Colors::GREEN);
566
567 EXPECT(checkFlag(), false);
568 EXPECT(getFromSocket("r/eval hl.config({ binds = { scroll_event_delay = 0 } })"), "ok");
569
570 // enter submap1 (SUPER+U); release with mods cleared so the modless mouse binds match
571 getFromSocket(pluginKeybindCmd(true, 7, 30));
572 getFromSocket(pluginKeybindCmd(false, 0, 30));
573 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
574
575 // scroll
576 OK(getFromSocket(pluginScrollCmd(120)));
577 EXPECT(attemptCheckFlag(20, 50), true);
578
579 // mouse-button
580 OK(getFromSocket(pluginClickCmd(true, BTN_LEFT)));
581 OK(getFromSocket(pluginClickCmd(false, BTN_LEFT)));
582 EXPECT(attemptCheckFlag(20, 50), true);
583
584 // reset to default submap
585 getFromSocket(pluginKeybindCmd(true, 0, 33));
586 getFromSocket(pluginKeybindCmd(false, 0, 33));
587 EXPECT_CONTAINS(getFromSocket("/submap"), "default");
588 }
589
590 SUBTEST(bindsAfterScroll) {
591 NLog::log("{}Testing binds after scroll", Colors::GREEN);
592
593 clearFlag();
594 OK(getFromSocket("/eval hl.bind('ALT + w', hl.dsp.exec_cmd('touch " + flagFile + "'))"));
595
596 // press keybind before scroll
597 OK(getFromSocket(pluginKeybindCmd(true, 0, 108))); // Alt_R press
598 OK(getFromSocket(pluginKeybindCmd(true, 4, 25))); // w press
599 EXPECT(attemptCheckFlag(20, 50), true);
600 OK(getFromSocket(pluginKeybindCmd(false, 4, 25))); // w release
601 OK(getFromSocket(pluginKeybindCmd(false, 0, 108))); // Alt_R release
602
603 // scroll
604 OK(getFromSocket(pluginScrollCmd(120)));
605 OK(getFromSocket(pluginScrollCmd(-120)));
606 OK(getFromSocket(pluginScrollCmd(120)));
607
608 // press keybind after scroll
609 OK(getFromSocket(pluginKeybindCmd(true, 0, 108))); // Alt_R press
610 OK(getFromSocket(pluginKeybindCmd(true, 4, 25))); // w press
611 EXPECT(attemptCheckFlag(20, 50), true);
612 OK(getFromSocket(pluginKeybindCmd(false, 4, 25))); // w release
613 OK(getFromSocket(pluginKeybindCmd(false, 0, 108))); // Alt_R release
614
615 clearFlag();
616 OK(getFromSocket("/eval hl.unbind('ALT + w')"));
617 }
618
619 SUBTEST(submapUniversal) {
620 NLog::log("{}Testing submap universal", Colors::GREEN);
621
622 EXPECT(checkFlag(), false);
623 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { submap_universal = true })"), "ok");
624 EXPECT_CONTAINS(getFromSocket("/submap"), "default");
625
626 // keybind works on default submap
627 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
628 OK(getFromSocket(pluginKeybindCmd(false, 7, 29)));
629 EXPECT(attemptCheckFlag(30, 5), true);
630
631 // keybind works on submap1
632 getFromSocket(pluginKeybindCmd(true, 7, 30));
633 getFromSocket(pluginKeybindCmd(false, 7, 30));
634 EXPECT_CONTAINS(getFromSocket("/submap"), "submap1");
635 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
636 OK(getFromSocket(pluginKeybindCmd(false, 7, 29)));
637 EXPECT(attemptCheckFlag(30, 5), true);
638
639 // reset to default submap
640 getFromSocket(pluginKeybindCmd(true, 0, 33));
641 getFromSocket(pluginKeybindCmd(false, 0, 33));
642 EXPECT_CONTAINS(getFromSocket("/submap"), "default");
643
644 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
645 }
646
647 SUBTEST(perDeviceKeybind) {
648 NLog::log("{}Testing per-device binds", Colors::GREEN);
649
650 // Inclusive
651 EXPECT(checkFlag(), false);
652 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { device = { inclusive = true, list = { 'test-keyboard-1' } } })"), "ok");
653 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
654 EXPECT(attemptCheckFlag(20, 50), true);
655 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
656 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
657
658 // Exclusive
659 EXPECT(checkFlag(), false);
660 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { device = { inclusive = false, list = { 'test-keyboard-1' } } })"), "ok");
661 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
662 EXPECT(attemptCheckFlag(20, 50), false);
663 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
664 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
665
666 // With description
667 EXPECT(checkFlag(), false);
668 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile +
669 "'), { description = 'test description', device = { inclusive = true, list = { 'test-keyboard-1' } } })"),
670 "ok");
671 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
672 EXPECT(attemptCheckFlag(20, 50), true);
673 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
674 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
675
676 // Tags
677 EXPECT(checkFlag(), false);
678 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { device = { inclusive = true, list = { 'test-tag' } } })"), "ok");
679 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
680 EXPECT(attemptCheckFlag(20, 50), true);
681 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
682 EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok");
683 }
684
685 SUBTEST(unbind) {
686 NLog::log("{}Testing unbind behavior", Colors::GREEN);
687
688 // unbind should normalize the string: no spaces, lowercase OK
689 EXPECT(checkFlag(), false);
690 EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { device = { inclusive = true, list = { 'test-keyboard-1' } } })"), "ok");
691 EXPECT(getFromSocket("/eval hl.unbind(' super + y ')"), "ok");
692
693 OK(getFromSocket(pluginKeybindCmd(true, 7, 29)));
694 OK(getFromSocket(pluginKeybindCmd(false, 0, 29)));
695
696 EXPECT(attemptCheckFlag(20, 50), false);
697 }
698
699 // TODO: remove this test after subtests above are properly isolated into independent tests
700 TEST_CASE(keybinds) {
701 CALL_SUBTEST(bind);
702 CALL_SUBTEST(bindKey);
703 CALL_SUBTEST(longPress);
704 CALL_SUBTEST(keyLongPress);
705 CALL_SUBTEST(longPressRelease);
706 CALL_SUBTEST(longPressOnlyKeyRelease);
707 CALL_SUBTEST(repeat);
708 CALL_SUBTEST(keyRepeat);
709 CALL_SUBTEST(repeatRelease);
710 CALL_SUBTEST(repeatOnlyKeyRelease);
711 CALL_SUBTEST(shortcutBind);
712 CALL_SUBTEST(shortcutBindKey);
713 CALL_SUBTEST(shortcutLongPress);
714 CALL_SUBTEST(shortcutLongPressKeyRelease);
715 CALL_SUBTEST(shortcutRepeat);
716 CALL_SUBTEST(shortcutRepeatKeyRelease);
717 CALL_SUBTEST(submap);
718 CALL_SUBTEST(submapMouseBinds);
719 CALL_SUBTEST(submapUniversal);
720 CALL_SUBTEST(bindsAfterScroll);
721 CALL_SUBTEST(perDeviceKeybind);
722 CALL_SUBTEST(unbind);
723 }