summaryrefslogtreecommitdiff
path: root/quantum/action_tapping.c
diff options
context:
space:
mode:
authorStefan Kerkmann <karlk90@pm.me>2023-04-03 10:33:45 +0200
committerGitHub <noreply@github.com>2023-04-03 18:33:45 +1000
commitfcf8b804ed95a98561bd4c1d6c85604be0f7cc7b (patch)
tree6b6917d99ced027d614e7b461e1cd1939833a9cd /quantum/action_tapping.c
parent2d9140af53e4e5bbc5cd50a2b6f3eda20ed8f71e (diff)
[Core] Refactor `keyevent_t` for 1ms timing resolution (#15847)
Diffstat (limited to 'quantum/action_tapping.c')
-rw-r--r--quantum/action_tapping.c116
1 files changed, 66 insertions, 50 deletions
diff --git a/quantum/action_tapping.c b/quantum/action_tapping.c
index dbb5b8d4e5..362b15105c 100644
--- a/quantum/action_tapping.c
+++ b/quantum/action_tapping.c
@@ -15,14 +15,10 @@
15# error "IGNORE_MOD_TAP_INTERRUPT is no longer necessary as it is now the default behavior of mod-tap keys. Please remove it from your config." 15# error "IGNORE_MOD_TAP_INTERRUPT is no longer necessary as it is now the default behavior of mod-tap keys. Please remove it from your config."
16# endif 16# endif
17 17
18# define IS_TAPPING() IS_EVENT(tapping_key.event)
19# define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
20# define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
21# define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
22# ifndef COMBO_ENABLE 18# ifndef COMBO_ENABLE
23# define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key))) 19# define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key)))
24# else 20# else
25# define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode) 21# define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode)
26# endif 22# endif
27# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < GET_TAPPING_TERM(get_record_keycode(&tapping_key, false), &tapping_key)) 23# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < GET_TAPPING_TERM(get_record_keycode(&tapping_key, false), &tapping_key))
28# define WITHIN_QUICK_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < GET_QUICK_TAP_TERM(get_record_keycode(&tapping_key, false), &tapping_key)) 24# define WITHIN_QUICK_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < GET_QUICK_TAP_TERM(get_record_keycode(&tapping_key, false), &tapping_key))
@@ -94,7 +90,7 @@ void action_tapping_process(keyrecord_t record) {
94 ac_dprintf("OVERFLOW: CLEAR ALL STATES\n"); 90 ac_dprintf("OVERFLOW: CLEAR ALL STATES\n");
95 clear_keyboard(); 91 clear_keyboard();
96 waiting_buffer_clear(); 92 waiting_buffer_clear();
97 tapping_key = (keyrecord_t){}; 93 tapping_key = (keyrecord_t){0};
98 } 94 }
99 } 95 }
100 96
@@ -121,7 +117,7 @@ void action_tapping_process(keyrecord_t record) {
121 * conditional uses of it are hidden inside macros named TAP_... 117 * conditional uses of it are hidden inside macros named TAP_...
122 */ 118 */
123# if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) || defined(PERMISSIVE_HOLD_PER_KEY) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY) 119# if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) || defined(PERMISSIVE_HOLD_PER_KEY) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
124# define TAP_DEFINE_KEYCODE uint16_t tapping_keycode = get_record_keycode(&tapping_key, false) 120# define TAP_DEFINE_KEYCODE const uint16_t tapping_keycode = get_record_keycode(&tapping_key, false)
125# else 121# else
126# define TAP_DEFINE_KEYCODE 122# define TAP_DEFINE_KEYCODE
127# endif 123# endif
@@ -167,12 +163,37 @@ void action_tapping_process(keyrecord_t record) {
167 */ 163 */
168/* return true when key event is processed or consumed. */ 164/* return true when key event is processed or consumed. */
169bool process_tapping(keyrecord_t *keyp) { 165bool process_tapping(keyrecord_t *keyp) {
170 keyevent_t event = keyp->event; 166 const keyevent_t event = keyp->event;
167
168 // state machine is in the "reset" state, no tapping key is to be
169 // processed
170 if (IS_NOEVENT(tapping_key.event) && IS_EVENT(event)) {
171 if (event.pressed && is_tap_record(keyp)) {
172 // the currently pressed key is a tapping key, therefore transition
173 // into the "pressed" tapping key state
174 ac_dprintf("Tapping: Start(Press tap key).\n");
175 tapping_key = *keyp;
176 process_record_tap_hint(&tapping_key);
177 waiting_buffer_scan_tap();
178 debug_tapping_key();
179 return true;
180 } else {
181 // the current key is just a regular key, pass it on for regular
182 // processing
183 process_record(keyp);
184 return true;
185 }
186 }
187
171 TAP_DEFINE_KEYCODE; 188 TAP_DEFINE_KEYCODE;
172 189
173 // if tapping 190 // process "pressed" tapping key state
174 if (IS_TAPPING_PRESSED()) { 191 if (tapping_key.event.pressed) {
175 if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) { 192 if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) {
193 if (IS_NOEVENT(event)) {
194 // early return for tick events
195 return true;
196 }
176 if (tapping_key.tap.count == 0) { 197 if (tapping_key.tap.count == 0) {
177 if (IS_TAPPING_RECORD(keyp) && !event.pressed) { 198 if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
178# if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) 199# if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
@@ -196,7 +217,7 @@ bool process_tapping(keyrecord_t *keyp) {
196 // clang-format off 217 // clang-format off
197 else if ( 218 else if (
198 ( 219 (
199 IS_RELEASED(event) && waiting_buffer_typed(event) && 220 !event.pressed && waiting_buffer_typed(event) &&
200 TAP_GET_PERMISSIVE_HOLD 221 TAP_GET_PERMISSIVE_HOLD
201 ) 222 )
202 // Causes nested taps to not wait past TAPPING_TERM/RETRO_SHIFT 223 // Causes nested taps to not wait past TAPPING_TERM/RETRO_SHIFT
@@ -214,7 +235,7 @@ bool process_tapping(keyrecord_t *keyp) {
214 || ( 235 || (
215 TAP_IS_RETRO 236 TAP_IS_RETRO
216 && (event.key.col != tapping_key.event.key.col || event.key.row != tapping_key.event.key.row) 237 && (event.key.col != tapping_key.event.key.col || event.key.row != tapping_key.event.key.row)
217 && IS_RELEASED(event) && waiting_buffer_typed(event) 238 && !event.pressed && waiting_buffer_typed(event)
218 ) 239 )
219 ) 240 )
220 ) 241 )
@@ -222,7 +243,7 @@ bool process_tapping(keyrecord_t *keyp) {
222 // clang-format on 243 // clang-format on
223 ac_dprintf("Tapping: End. No tap. Interfered by typing key\n"); 244 ac_dprintf("Tapping: End. No tap. Interfered by typing key\n");
224 process_record(&tapping_key); 245 process_record(&tapping_key);
225 tapping_key = (keyrecord_t){}; 246 tapping_key = (keyrecord_t){0};
226 debug_tapping_key(); 247 debug_tapping_key();
227 // enqueue 248 // enqueue
228 return false; 249 return false;
@@ -231,7 +252,7 @@ bool process_tapping(keyrecord_t *keyp) {
231 * Without this unexpected repeating will occur with having fast repeating setting 252 * Without this unexpected repeating will occur with having fast repeating setting
232 * https://github.com/tmk/tmk_keyboard/issues/60 253 * https://github.com/tmk/tmk_keyboard/issues/60
233 */ 254 */
234 else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) { 255 else if (!event.pressed && !waiting_buffer_typed(event)) {
235 // Modifier/Layer should be retained till end of this tapping. 256 // Modifier/Layer should be retained till end of this tapping.
236 action_t action = layer_switch_get_action(event.key); 257 action_t action = layer_switch_get_action(event.key);
237 switch (action.kind.id) { 258 switch (action.kind.id) {
@@ -267,7 +288,7 @@ bool process_tapping(keyrecord_t *keyp) {
267 if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS) { 288 if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS) {
268 ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n"); 289 ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n");
269 process_record(&tapping_key); 290 process_record(&tapping_key);
270 tapping_key = (keyrecord_t){}; 291 tapping_key = (keyrecord_t){0};
271 debug_tapping_key(); 292 debug_tapping_key();
272 // enqueue 293 // enqueue
273 return false; 294 return false;
@@ -295,6 +316,7 @@ bool process_tapping(keyrecord_t *keyp) {
295 .event.key = tapping_key.event.key, 316 .event.key = tapping_key.event.key,
296 .event.time = event.time, 317 .event.time = event.time,
297 .event.pressed = false, 318 .event.pressed = false,
319 .event.type = tapping_key.event.type,
298# ifdef COMBO_ENABLE 320# ifdef COMBO_ENABLE
299 .keycode = tapping_key.keycode, 321 .keycode = tapping_key.keycode,
300# endif 322# endif
@@ -307,9 +329,7 @@ bool process_tapping(keyrecord_t *keyp) {
307 debug_tapping_key(); 329 debug_tapping_key();
308 return true; 330 return true;
309 } else { 331 } else {
310 if (IS_EVENT(event)) { 332 ac_dprintf("Tapping: key event while last tap(>0).\n");
311 ac_dprintf("Tapping: key event while last tap(>0).\n");
312 }
313 process_record(keyp); 333 process_record(keyp);
314 return true; 334 return true;
315 } 335 }
@@ -322,15 +342,18 @@ bool process_tapping(keyrecord_t *keyp) {
322 debug_event(event); 342 debug_event(event);
323 ac_dprintf("\n"); 343 ac_dprintf("\n");
324 process_record(&tapping_key); 344 process_record(&tapping_key);
325 tapping_key = (keyrecord_t){}; 345 tapping_key = (keyrecord_t){0};
326 debug_tapping_key(); 346 debug_tapping_key();
327 return false; 347 return false;
328 } else { 348 } else {
349 if (IS_NOEVENT(event)) {
350 return true;
351 }
329 if (IS_TAPPING_RECORD(keyp) && !event.pressed) { 352 if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
330 ac_dprintf("Tapping: End. last timeout tap release(>0)."); 353 ac_dprintf("Tapping: End. last timeout tap release(>0).");
331 keyp->tap = tapping_key.tap; 354 keyp->tap = tapping_key.tap;
332 process_record(keyp); 355 process_record(keyp);
333 tapping_key = (keyrecord_t){}; 356 tapping_key = (keyrecord_t){0};
334 return true; 357 return true;
335 } else if (is_tap_record(keyp) && event.pressed) { 358 } else if (is_tap_record(keyp) && event.pressed) {
336 if (tapping_key.tap.count > 1) { 359 if (tapping_key.tap.count > 1) {
@@ -341,6 +364,7 @@ bool process_tapping(keyrecord_t *keyp) {
341 .event.key = tapping_key.event.key, 364 .event.key = tapping_key.event.key,
342 .event.time = event.time, 365 .event.time = event.time,
343 .event.pressed = false, 366 .event.pressed = false,
367 .event.type = tapping_key.event.type,
344# ifdef COMBO_ENABLE 368# ifdef COMBO_ENABLE
345 .keycode = tapping_key.keycode, 369 .keycode = tapping_key.keycode,
346# endif 370# endif
@@ -353,16 +377,20 @@ bool process_tapping(keyrecord_t *keyp) {
353 debug_tapping_key(); 377 debug_tapping_key();
354 return true; 378 return true;
355 } else { 379 } else {
356 if (IS_EVENT(event)) { 380 ac_dprintf("Tapping: key event while last timeout tap(>0).\n");
357 ac_dprintf("Tapping: key event while last timeout tap(>0).\n");
358 }
359 process_record(keyp); 381 process_record(keyp);
360 return true; 382 return true;
361 } 383 }
362 } 384 }
363 } 385 }
364 } else if (IS_TAPPING_RELEASED()) { 386 }
387 // process "released" tapping key state
388 else {
365 if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) { 389 if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) {
390 if (IS_NOEVENT(event)) {
391 // early return for tick events
392 return true;
393 }
366 if (event.pressed) { 394 if (event.pressed) {
367 if (IS_TAPPING_RECORD(keyp)) { 395 if (IS_TAPPING_RECORD(keyp)) {
368 if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) { 396 if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
@@ -393,35 +421,20 @@ bool process_tapping(keyrecord_t *keyp) {
393 return true; 421 return true;
394 } 422 }
395 } else { 423 } else {
396 if (IS_EVENT(event)) ac_dprintf("Tapping: other key just after tap.\n"); 424 ac_dprintf("Tapping: other key just after tap.\n");
397 process_record(keyp); 425 process_record(keyp);
398 return true; 426 return true;
399 } 427 }
400 } else { 428 } else {
401 // FIX: process_action here? 429 // Timeout - reset state machine.
402 // timeout. no sequential tap.
403 ac_dprintf("Tapping: End(Timeout after releasing last tap): "); 430 ac_dprintf("Tapping: End(Timeout after releasing last tap): ");
404 debug_event(event); 431 debug_event(event);
405 ac_dprintf("\n"); 432 ac_dprintf("\n");
406 tapping_key = (keyrecord_t){}; 433 tapping_key = (keyrecord_t){0};
407 debug_tapping_key(); 434 debug_tapping_key();
408 return false; 435 return false;
409 } 436 }
410 } 437 }
411 // not tapping state
412 else {
413 if (event.pressed && is_tap_record(keyp)) {
414 ac_dprintf("Tapping: Start(Press tap key).\n");
415 tapping_key = *keyp;
416 process_record_tap_hint(&tapping_key);
417 waiting_buffer_scan_tap();
418 debug_tapping_key();
419 return true;
420 } else {
421 process_record(keyp);
422 return true;
423 }
424 }
425} 438}
426 439
427/** \brief Waiting buffer enq 440/** \brief Waiting buffer enq
@@ -484,15 +497,18 @@ __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
484 * FIXME: Needs docs 497 * FIXME: Needs docs
485 */ 498 */
486void waiting_buffer_scan_tap(void) { 499void waiting_buffer_scan_tap(void) {
487 // tapping already is settled 500 // early return if:
488 if (tapping_key.tap.count > 0) return; 501 // - tapping already is settled
489 // invalid state: tapping_key released && tap.count == 0 502 // - invalid state: tapping_key released && tap.count == 0
490 if (!tapping_key.event.pressed) return; 503 if ((tapping_key.tap.count > 0) || !tapping_key.event.pressed) {
504 return;
505 }
491 506
492 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 507 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
493 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && !waiting_buffer[i].event.pressed && WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { 508 keyrecord_t *candidate = &waiting_buffer[i];
494 tapping_key.tap.count = 1; 509 if (IS_EVENT(candidate->event) && KEYEQ(candidate->event.key, tapping_key.event.key) && !candidate->event.pressed && WITHIN_TAPPING_TERM(candidate->event)) {
495 waiting_buffer[i].tap.count = 1; 510 tapping_key.tap.count = 1;
511 candidate->tap.count = 1;
496 process_record(&tapping_key); 512 process_record(&tapping_key);
497 513
498 ac_dprintf("waiting_buffer_scan_tap: found at [%u]\n", i); 514 ac_dprintf("waiting_buffer_scan_tap: found at [%u]\n", i);