action_tapping.c (43776B)
1 #include <stdint.h> 2 #include <stdbool.h> 3 4 #include "action.h" 5 #include "action_layer.h" 6 #include "action_tapping.h" 7 #include "action_util.h" 8 #include "keycode.h" 9 #include "keycode_config.h" 10 #include "quantum_keycodes.h" 11 #include "timer.h" 12 #include "wait.h" 13 14 #ifndef NO_ACTION_TAPPING 15 16 # if defined(IGNORE_MOD_TAP_INTERRUPT_PER_KEY) 17 # error "IGNORE_MOD_TAP_INTERRUPT_PER_KEY has been removed; the code needs to be ported to use HOLD_ON_OTHER_KEY_PRESS_PER_KEY instead." 18 # elif defined(IGNORE_MOD_TAP_INTERRUPT) 19 # 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." 20 # endif 21 22 # ifndef COMBO_ENABLE 23 # define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key))) 24 # else 25 # define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode) 26 # 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)) 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)) 29 30 # ifdef DYNAMIC_TAPPING_TERM_ENABLE 31 uint16_t g_tapping_term = TAPPING_TERM; 32 # endif 33 34 # ifdef TAPPING_TERM_PER_KEY 35 __attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { 36 # ifdef DYNAMIC_TAPPING_TERM_ENABLE 37 return g_tapping_term; 38 # else 39 return TAPPING_TERM; 40 # endif 41 } 42 # endif 43 44 # ifdef QUICK_TAP_TERM_PER_KEY 45 __attribute__((weak)) uint16_t get_quick_tap_term(uint16_t keycode, keyrecord_t *record) { 46 return QUICK_TAP_TERM; 47 } 48 # endif 49 50 # ifdef PERMISSIVE_HOLD_PER_KEY 51 __attribute__((weak)) bool get_permissive_hold(uint16_t keycode, keyrecord_t *record) { 52 return false; 53 } 54 # endif 55 56 # ifdef SPECULATIVE_HOLD 57 typedef struct { 58 keypos_t key; 59 uint8_t mods; 60 } speculative_key_t; 61 # define SPECULATIVE_KEYS_SIZE 8 62 static speculative_key_t speculative_keys[SPECULATIVE_KEYS_SIZE] = {}; 63 static uint8_t num_speculative_keys = 0; 64 static uint8_t prev_speculative_mods = 0; 65 static uint8_t speculative_mods = 0; 66 67 /** Handler to be called on incoming press events. */ 68 static void speculative_key_press(keyrecord_t *record); 69 # endif // SPECULATIVE_HOLD 70 71 # if defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM) 72 # define REGISTERED_TAPS_SIZE 8 73 // Array of tap-hold keys that have been settled as tapped but not yet released. 74 static keypos_t registered_taps[REGISTERED_TAPS_SIZE] = {}; 75 static uint8_t num_registered_taps = 0; 76 77 /** Adds `key` to the registered_taps array. */ 78 static void registered_taps_add(keypos_t key); 79 /** Returns the index of `key` in registered_taps, or -1 if not found. */ 80 static int8_t registered_tap_find(keypos_t key); 81 /** Removes index `i` from the registered_taps array. */ 82 static void registered_taps_del_index(uint8_t i); 83 /** Logs the registered_taps array for debugging. */ 84 static void debug_registered_taps(void); 85 86 static bool is_mt_or_lt(uint16_t keycode) { 87 return IS_QK_MOD_TAP(keycode) || IS_QK_LAYER_TAP(keycode); 88 } 89 # endif // defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM) 90 91 # if defined(CHORDAL_HOLD) 92 extern const char chordal_hold_layout[MATRIX_ROWS][MATRIX_COLS] PROGMEM; 93 94 /** \brief Finds which queued events should be held according to Chordal Hold. 95 * 96 * In a situation with multiple unsettled tap-hold key presses, scan the queue 97 * up until the first release, non-tap-hold, or one-shot event and find the 98 * latest event in the queue that settles as held according to 99 * get_chordal_hold(). 100 * 101 * \return Index of the first tap, or equivalently, one past the latest hold. 102 */ 103 static uint8_t waiting_buffer_find_chordal_hold_tap(void); 104 105 /** Processes queued events up to and including `key` as tapped. */ 106 static void waiting_buffer_chordal_hold_taps_until(keypos_t key); 107 108 /** \brief Processes and pops buffered events until the first tap-hold event. */ 109 static void waiting_buffer_process_regular(void); 110 # endif // CHORDAL_HOLD 111 112 # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY 113 __attribute__((weak)) bool get_hold_on_other_key_press(uint16_t keycode, keyrecord_t *record) { 114 return false; 115 } 116 # endif 117 118 # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) 119 # include "process_auto_shift.h" 120 # endif 121 122 # if defined(FLOW_TAP_TERM) 123 static uint16_t flow_tap_prev_keycode = KC_NO; 124 static uint16_t flow_tap_prev_time = 0; 125 static bool flow_tap_expired = true; 126 127 static bool flow_tap_key_if_within_term(keyrecord_t *record, uint16_t prev_time); 128 # endif // defined(FLOW_TAP_TERM) 129 130 static keyrecord_t tapping_key = {}; 131 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {}; 132 static uint8_t waiting_buffer_head = 0; 133 static uint8_t waiting_buffer_tail = 0; 134 135 static bool process_tapping(keyrecord_t *record); 136 static bool waiting_buffer_enq(keyrecord_t record); 137 static void waiting_buffer_clear(void); 138 static bool waiting_buffer_typed(keyevent_t event); 139 static bool waiting_buffer_has_anykey_pressed(void); 140 static void waiting_buffer_scan_tap(void); 141 static void debug_tapping_key(void); 142 static void debug_waiting_buffer(void); 143 144 /** \brief Action Tapping Process 145 * 146 * FIXME: Needs doc 147 */ 148 void action_tapping_process(keyrecord_t record) { 149 # ifdef SPECULATIVE_HOLD 150 prev_speculative_mods = speculative_mods; 151 if (record.event.pressed) { 152 speculative_key_press(&record); 153 } 154 # endif // SPECULATIVE_HOLD 155 156 if (process_tapping(&record)) { 157 if (IS_EVENT(record.event)) { 158 ac_dprintf("processed: "); 159 debug_record(record); 160 ac_dprintf("\n"); 161 } 162 } else { 163 if (!waiting_buffer_enq(record)) { 164 // clear all in case of overflow. 165 ac_dprintf("OVERFLOW: CLEAR ALL STATES\n"); 166 clear_keyboard(); 167 waiting_buffer_clear(); 168 tapping_key = (keyrecord_t){0}; 169 } 170 } 171 172 # ifdef SPECULATIVE_HOLD 173 if (speculative_mods != prev_speculative_mods) { 174 send_keyboard_report(); 175 } 176 # endif // SPECULATIVE_HOLD 177 178 // process waiting_buffer 179 if (IS_EVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) { 180 ac_dprintf("---- action_exec: process waiting_buffer -----\n"); 181 } 182 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { 183 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { 184 ac_dprintf("processed: waiting_buffer[%u] =", waiting_buffer_tail); 185 debug_record(waiting_buffer[waiting_buffer_tail]); 186 ac_dprintf("\n\n"); 187 } else { 188 break; 189 } 190 } 191 if (IS_EVENT(record.event)) { 192 ac_dprintf("\n"); 193 } else { 194 # ifdef FLOW_TAP_TERM 195 if (!flow_tap_expired && TIMER_DIFF_16(record.event.time, flow_tap_prev_time) >= INT16_MAX / 2) { 196 flow_tap_expired = true; 197 } 198 # endif // FLOW_TAP_TERM 199 } 200 } 201 202 /* Some conditionally defined helper macros to keep process_tapping more 203 * readable. The conditional definition of tapping_keycode and all the 204 * conditional uses of it are hidden inside macros named TAP_... 205 */ 206 # define TAP_DEFINE_KEYCODE const uint16_t tapping_keycode = get_record_keycode(&tapping_key, false) 207 208 # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) 209 # ifdef RETRO_TAPPING_PER_KEY 210 # define TAP_GET_RETRO_TAPPING(keyp) get_auto_shifted_key(tapping_keycode, keyp) && get_retro_tapping(tapping_keycode, &tapping_key) 211 # else 212 # define TAP_GET_RETRO_TAPPING(keyp) get_auto_shifted_key(tapping_keycode, keyp) 213 # endif 214 /* Used to extend TAPPING_TERM: 215 * indefinitely if RETRO_SHIFT does not have a value 216 * to RETRO_SHIFT if RETRO_SHIFT is set 217 * for possibly retro shifted keys. 218 */ 219 # define MAYBE_RETRO_SHIFTING(ev, keyp) (get_auto_shifted_key(tapping_keycode, keyp) && TAP_GET_RETRO_TAPPING(keyp) && ((RETRO_SHIFT + 0) == 0 || TIMER_DIFF_16((ev).time, tapping_key.event.time) < (RETRO_SHIFT + 0))) 220 # define TAP_IS_LT IS_QK_LAYER_TAP(tapping_keycode) 221 # define TAP_IS_MT IS_QK_MOD_TAP(tapping_keycode) 222 # define TAP_IS_RETRO IS_RETRO(tapping_keycode) 223 # else 224 # define TAP_GET_RETRO_TAPPING(keyp) false 225 # define MAYBE_RETRO_SHIFTING(ev, kp) false 226 # define TAP_IS_LT false 227 # define TAP_IS_MT false 228 # define TAP_IS_RETRO false 229 # endif 230 231 # ifdef PERMISSIVE_HOLD_PER_KEY 232 # define TAP_GET_PERMISSIVE_HOLD get_permissive_hold(tapping_keycode, &tapping_key) 233 # elif defined(PERMISSIVE_HOLD) 234 # define TAP_GET_PERMISSIVE_HOLD true 235 # else 236 # define TAP_GET_PERMISSIVE_HOLD false 237 # endif 238 239 # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY 240 # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS get_hold_on_other_key_press(tapping_keycode, &tapping_key) 241 # elif defined(HOLD_ON_OTHER_KEY_PRESS) 242 # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS true 243 # else 244 # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS false 245 # endif 246 247 /** \brief Tapping 248 * 249 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM. 250 * (without interfering by typing other key) 251 */ 252 /* return true when key event is processed or consumed. */ 253 bool process_tapping(keyrecord_t *keyp) { 254 const keyevent_t event = keyp->event; 255 256 # if defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM) 257 if (!event.pressed) { 258 const int8_t i = registered_tap_find(event.key); 259 if (i != -1) { 260 // If a tap-hold key was previously settled as tapped, set its 261 // tap.count correspondingly on release. 262 keyp->tap.count = 1; 263 registered_taps_del_index(i); 264 ac_dprintf("Found tap release for [%d]\n", i); 265 debug_registered_taps(); 266 } 267 } 268 # endif // defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM) 269 270 // state machine is in the "reset" state, no tapping key is to be 271 // processed 272 if (IS_NOEVENT(tapping_key.event)) { 273 if (!IS_EVENT(event)) { 274 // early return for tick events 275 } else if (event.pressed && is_tap_record(keyp)) { 276 // the currently pressed key is a tapping key, therefore transition 277 // into the "pressed" tapping key state 278 279 # if defined(FLOW_TAP_TERM) 280 if (flow_tap_key_if_within_term(keyp, flow_tap_prev_time)) { 281 return true; 282 } 283 # endif // defined(FLOW_TAP_TERM) 284 285 ac_dprintf("Tapping: Start(Press tap key).\n"); 286 tapping_key = *keyp; 287 process_record_tap_hint(&tapping_key); 288 waiting_buffer_scan_tap(); 289 debug_tapping_key(); 290 } else { 291 // the current key is just a regular key, pass it on for regular 292 // processing 293 process_record(keyp); 294 } 295 296 return true; 297 } 298 299 # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) || defined(PERMISSIVE_HOLD_PER_KEY) || defined(CHORDAL_HOLD) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY) 300 TAP_DEFINE_KEYCODE; 301 # endif 302 303 // process "pressed" tapping key state 304 if (tapping_key.event.pressed) { 305 if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event, keyp)) { 306 if (IS_NOEVENT(event)) { 307 // early return for tick events 308 return true; 309 } 310 311 if (tapping_key.tap.count == 0) { 312 if (IS_TAPPING_RECORD(keyp) && !event.pressed) { 313 // first tap! 314 ac_dprintf("Tapping: First tap(0->1).\n"); 315 tapping_key.tap.count = 1; 316 debug_tapping_key(); 317 process_record(&tapping_key); 318 319 // copy tapping state 320 keyp->tap = tapping_key.tap; 321 322 # if defined(FLOW_TAP_TERM) 323 // Now that tapping_key has settled as tapped, check whether 324 // Flow Tap applies to following yet-unsettled keys. 325 uint16_t prev_time = tapping_key.event.time; 326 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { 327 keyrecord_t *record = &waiting_buffer[waiting_buffer_tail]; 328 if (!record->event.pressed) { 329 break; 330 } 331 const int16_t next_time = record->event.time; 332 if (!is_tap_record(record)) { 333 process_record(record); 334 } else if (!flow_tap_key_if_within_term(record, prev_time)) { 335 break; 336 } 337 prev_time = next_time; 338 } 339 debug_waiting_buffer(); 340 # endif // defined(FLOW_TAP_TERM) 341 342 // enqueue 343 return false; 344 } 345 # if defined(CHORDAL_HOLD) 346 else if (is_mt_or_lt(tapping_keycode) && !event.pressed && waiting_buffer_typed(event) && !get_chordal_hold(tapping_keycode, &tapping_key, get_record_keycode(keyp, false), keyp)) { 347 // Key release that is not a chord with the tapping key. 348 // Settle the tapping key and any other pending tap-hold 349 // keys preceding the press of this key as tapped. 350 351 ac_dprintf("Tapping: End. Chord considered a tap\n"); 352 tapping_key.tap.count = 1; 353 registered_taps_add(tapping_key.event.key); 354 process_record(&tapping_key); 355 tapping_key = (keyrecord_t){0}; 356 357 waiting_buffer_chordal_hold_taps_until(event.key); 358 debug_registered_taps(); 359 debug_waiting_buffer(); 360 // enqueue 361 return false; 362 } 363 # endif // CHORDAL_HOLD 364 /* Process a key typed within TAPPING_TERM 365 * This can register the key before settlement of tapping, 366 * useful for long TAPPING_TERM but may prevent fast typing. 367 */ 368 // clang-format off 369 else if ( 370 !event.pressed && waiting_buffer_typed(event) && 371 ( 372 TAP_GET_PERMISSIVE_HOLD || 373 // Causes nested taps to not wait past TAPPING_TERM/RETRO_SHIFT 374 // unnecessarily and fixes them for Layer Taps. 375 TAP_GET_RETRO_TAPPING(keyp) 376 ) 377 ) { 378 // clang-format on 379 ac_dprintf("Tapping: End. No tap. Interfered by typing key\n"); 380 process_record(&tapping_key); 381 382 # if defined(CHORDAL_HOLD) 383 uint8_t first_tap = waiting_buffer_find_chordal_hold_tap(); 384 ac_dprintf("first_tap = %u\n", first_tap); 385 if (first_tap < WAITING_BUFFER_SIZE) { 386 for (; waiting_buffer_tail != first_tap; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { 387 ac_dprintf("Processing [%u]\n", waiting_buffer_tail); 388 process_record(&waiting_buffer[waiting_buffer_tail]); 389 } 390 } 391 392 waiting_buffer_chordal_hold_taps_until(event.key); 393 debug_registered_taps(); 394 debug_waiting_buffer(); 395 # endif // CHORDAL_HOLD 396 397 tapping_key = (keyrecord_t){0}; 398 debug_tapping_key(); 399 // enqueue 400 return false; 401 } 402 /* Process release event of a key pressed before tapping starts 403 * Without this unexpected repeating will occur with having fast repeating setting 404 * https://github.com/tmk/tmk_keyboard/issues/60 405 * 406 * NOTE: This workaround causes events to process out of order, 407 * e.g. in a rolled press of three tap-hold keys like 408 * 409 * "A down, B down, C down, A up, B up, C up" 410 * 411 * events are processed as 412 * 413 * "A down, B down, A up, B up, C down, C up" 414 * 415 * It seems incorrect to process keyp before the tapping key. 416 * This workaround is old, from 2013. This might no longer 417 * be needed for the original problem it was meant to address. 418 */ 419 else if (!event.pressed && !waiting_buffer_typed(event)) { 420 // Modifier/Layer should be retained till end of this tapping. 421 action_t action = layer_switch_get_action(event.key); 422 switch (action.kind.id) { 423 case ACT_LMODS: 424 case ACT_RMODS: 425 if (action.key.mods && !action.key.code) return false; 426 if (IS_MODIFIER_KEYCODE(action.key.code)) return false; 427 break; 428 case ACT_LMODS_TAP: 429 case ACT_RMODS_TAP: 430 if (action.key.mods && keyp->tap.count == 0) return false; 431 if (IS_MODIFIER_KEYCODE(action.key.code)) return false; 432 break; 433 case ACT_LAYER_TAP: 434 case ACT_LAYER_TAP_EXT: 435 switch (action.layer_tap.code) { 436 case 0 ...(OP_TAP_TOGGLE - 1): 437 case OP_ON_OFF: 438 case OP_OFF_ON: 439 case OP_SET_CLEAR: 440 return false; 441 } 442 break; 443 } 444 // Release of key should be process immediately. 445 ac_dprintf("Tapping: release event of a key pressed before tapping\n"); 446 process_record(keyp); 447 return true; 448 } else { 449 // set interrupted flag when other key pressed during tapping 450 if (event.pressed) { 451 tapping_key.tap.interrupted = true; 452 453 # if defined(CHORDAL_HOLD) 454 if (is_mt_or_lt(tapping_keycode) && !get_chordal_hold(tapping_keycode, &tapping_key, get_record_keycode(keyp, false), keyp)) { 455 // In process_action(), HOLD_ON_OTHER_KEY_PRESS 456 // will revert interrupted events to holds, so 457 // this needs to be set false. 458 tapping_key.tap.interrupted = false; 459 460 if (!is_tap_record(keyp)) { 461 ac_dprintf("Tapping: End. Chord considered a tap\n"); 462 tapping_key.tap.count = 1; 463 registered_taps_add(tapping_key.event.key); 464 debug_registered_taps(); 465 process_record(&tapping_key); 466 tapping_key = (keyrecord_t){0}; 467 } 468 } else 469 # endif // CHORDAL_HOLD 470 if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS 471 # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) 472 // Auto Shift cannot evaluate this early 473 // Retro Shift uses the hold action for all nested taps even without HOLD_ON_OTHER_KEY_PRESS, so this is fine to skip 474 && !(MAYBE_RETRO_SHIFTING(event, keyp) && get_auto_shifted_key(get_record_keycode(keyp, false), keyp)) 475 # endif 476 ) { 477 // Settle the tapping key as *held*, since 478 // HOLD_ON_OTHER_KEY_PRESS is enabled for this key. 479 ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n"); 480 process_record(&tapping_key); 481 482 # if defined(CHORDAL_HOLD) 483 if (waiting_buffer_tail != waiting_buffer_head && is_tap_record(&waiting_buffer[waiting_buffer_tail])) { 484 tapping_key = waiting_buffer[waiting_buffer_tail]; 485 // Pop tail from the queue. 486 waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE; 487 debug_waiting_buffer(); 488 } else 489 # endif // CHORDAL_HOLD 490 { 491 tapping_key = (keyrecord_t){0}; 492 } 493 debug_tapping_key(); 494 495 # if defined(CHORDAL_HOLD) 496 waiting_buffer_process_regular(); 497 # endif // CHORDAL_HOLD 498 } 499 } 500 // enqueue 501 return false; 502 } 503 } 504 // tap_count > 0 505 else { 506 if (IS_TAPPING_RECORD(keyp) && !event.pressed) { 507 ac_dprintf("Tapping: Tap release(%u)\n", tapping_key.tap.count); 508 keyp->tap = tapping_key.tap; 509 process_record(keyp); 510 tapping_key = *keyp; 511 debug_tapping_key(); 512 return true; 513 } else if (is_tap_record(keyp) && event.pressed) { 514 if (tapping_key.tap.count > 1) { 515 ac_dprintf("Tapping: Start new tap with releasing last tap(>1).\n"); 516 // unregister key 517 process_record(&(keyrecord_t){ 518 .tap = tapping_key.tap, 519 .event.key = tapping_key.event.key, 520 .event.time = event.time, 521 .event.pressed = false, 522 .event.type = tapping_key.event.type, 523 # ifdef COMBO_ENABLE 524 .keycode = tapping_key.keycode, 525 # endif 526 }); 527 } else { 528 ac_dprintf("Tapping: Start while last tap(1).\n"); 529 } 530 tapping_key = *keyp; 531 waiting_buffer_scan_tap(); 532 debug_tapping_key(); 533 return true; 534 } else { 535 ac_dprintf("Tapping: key event while last tap(>0).\n"); 536 # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) 537 retroshift_swap_times(); 538 # endif 539 process_record(keyp); 540 return true; 541 } 542 } 543 } 544 // after TAPPING_TERM 545 else { 546 if (tapping_key.tap.count == 0) { 547 ac_dprintf("Tapping: End. Timeout. Not tap(0): "); 548 debug_event(event); 549 ac_dprintf("\n"); 550 process_record(&tapping_key); 551 tapping_key = (keyrecord_t){0}; 552 debug_tapping_key(); 553 return false; 554 } else { 555 if (IS_NOEVENT(event)) { 556 return true; 557 } 558 if (IS_TAPPING_RECORD(keyp) && !event.pressed) { 559 ac_dprintf("Tapping: End. last timeout tap release(>0)."); 560 keyp->tap = tapping_key.tap; 561 process_record(keyp); 562 tapping_key = (keyrecord_t){0}; 563 return true; 564 } else if (is_tap_record(keyp) && event.pressed) { 565 if (tapping_key.tap.count > 1) { 566 ac_dprintf("Tapping: Start new tap with releasing last timeout tap(>1).\n"); 567 // unregister key 568 process_record(&(keyrecord_t){ 569 .tap = tapping_key.tap, 570 .event.key = tapping_key.event.key, 571 .event.time = event.time, 572 .event.pressed = false, 573 .event.type = tapping_key.event.type, 574 # ifdef COMBO_ENABLE 575 .keycode = tapping_key.keycode, 576 # endif 577 }); 578 } else { 579 ac_dprintf("Tapping: Start while last timeout tap(1).\n"); 580 } 581 tapping_key = *keyp; 582 waiting_buffer_scan_tap(); 583 debug_tapping_key(); 584 return true; 585 } else { 586 ac_dprintf("Tapping: key event while last timeout tap(>0).\n"); 587 process_record(keyp); 588 return true; 589 } 590 } 591 } 592 } 593 // process "released" tapping key state 594 else { 595 if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event, keyp)) { 596 if (IS_NOEVENT(event)) { 597 // early return for tick events 598 return true; 599 } 600 if (event.pressed) { 601 if (IS_TAPPING_RECORD(keyp)) { 602 if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) { 603 // sequential tap. 604 keyp->tap = tapping_key.tap; 605 if (keyp->tap.count < 15) keyp->tap.count += 1; 606 ac_dprintf("Tapping: Tap press(%u)\n", keyp->tap.count); 607 process_record(keyp); 608 tapping_key = *keyp; 609 debug_tapping_key(); 610 return true; 611 } 612 // FIX: start new tap again 613 tapping_key = *keyp; 614 return true; 615 } else if (is_tap_record(keyp)) { 616 // Sequential tap can be interfered with other tap key. 617 # if defined(FLOW_TAP_TERM) 618 if (flow_tap_key_if_within_term(keyp, flow_tap_prev_time)) { 619 tapping_key = (keyrecord_t){0}; 620 debug_tapping_key(); 621 return true; 622 } 623 # endif // defined(FLOW_TAP_TERM) 624 ac_dprintf("Tapping: Start with interfering other tap.\n"); 625 tapping_key = *keyp; 626 waiting_buffer_scan_tap(); 627 debug_tapping_key(); 628 return true; 629 } else { 630 // should none in buffer 631 // FIX: interrupted when other key is pressed 632 tapping_key.tap.interrupted = true; 633 process_record(keyp); 634 return true; 635 } 636 } else { 637 ac_dprintf("Tapping: other key just after tap.\n"); 638 process_record(keyp); 639 return true; 640 } 641 } else { 642 // Timeout - reset state machine. 643 ac_dprintf("Tapping: End(Timeout after releasing last tap): "); 644 debug_event(event); 645 ac_dprintf("\n"); 646 tapping_key = (keyrecord_t){0}; 647 debug_tapping_key(); 648 return false; 649 } 650 } 651 } 652 653 /** \brief Waiting buffer enq 654 * 655 * FIXME: Needs docs 656 */ 657 bool waiting_buffer_enq(keyrecord_t record) { 658 if (IS_NOEVENT(record.event)) { 659 return true; 660 } 661 662 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) { 663 ac_dprintf("waiting_buffer_enq: Over flow.\n"); 664 return false; 665 } 666 667 waiting_buffer[waiting_buffer_head] = record; 668 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; 669 670 ac_dprintf("waiting_buffer_enq: "); 671 debug_waiting_buffer(); 672 return true; 673 } 674 675 /** \brief Waiting buffer clear 676 * 677 * FIXME: Needs docs 678 */ 679 void waiting_buffer_clear(void) { 680 waiting_buffer_head = 0; 681 waiting_buffer_tail = 0; 682 } 683 684 /** \brief Waiting buffer typed 685 * 686 * FIXME: Needs docs 687 */ 688 bool waiting_buffer_typed(keyevent_t event) { 689 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 690 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { 691 return true; 692 } 693 } 694 return false; 695 } 696 697 /** \brief Waiting buffer has anykey pressed 698 * 699 * FIXME: Needs docs 700 */ 701 __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) { 702 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 703 if (waiting_buffer[i].event.pressed) return true; 704 } 705 return false; 706 } 707 708 /** \brief Scan buffer for tapping 709 * 710 * FIXME: Needs docs 711 */ 712 void waiting_buffer_scan_tap(void) { 713 // early return if: 714 // - tapping already is settled 715 // - invalid state: tapping_key released && tap.count == 0 716 if ((tapping_key.tap.count > 0) || !tapping_key.event.pressed) { 717 return; 718 } 719 720 # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) 721 TAP_DEFINE_KEYCODE; 722 # endif 723 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 724 keyrecord_t *candidate = &waiting_buffer[i]; 725 // clang-format off 726 if (IS_EVENT(candidate->event) && KEYEQ(candidate->event.key, tapping_key.event.key) && !candidate->event.pressed && ( 727 WITHIN_TAPPING_TERM(waiting_buffer[i].event) || MAYBE_RETRO_SHIFTING(waiting_buffer[i].event, &tapping_key) 728 )) { 729 // clang-format on 730 tapping_key.tap.count = 1; 731 candidate->tap.count = 1; 732 process_record(&tapping_key); 733 734 ac_dprintf("waiting_buffer_scan_tap: found at [%u]\n", i); 735 debug_waiting_buffer(); 736 return; 737 } 738 } 739 } 740 741 # ifdef SPECULATIVE_HOLD 742 static void debug_speculative_keys(void) { 743 ac_dprintf("mods = { "); 744 for (int8_t i = 0; i < num_speculative_keys; ++i) { 745 ac_dprintf("%02X ", speculative_keys[i].mods); 746 } 747 ac_dprintf("}, keys = { "); 748 for (int8_t i = 0; i < num_speculative_keys; ++i) { 749 ac_dprintf("%02X%02X ", speculative_keys[i].key.row, speculative_keys[i].key.col); 750 } 751 ac_dprintf("}\n"); 752 } 753 754 // Find key in speculative_keys. Returns num_speculative_keys if not found. 755 static int8_t speculative_keys_find(keypos_t key) { 756 uint8_t i; 757 for (i = 0; i < num_speculative_keys; ++i) { 758 if (KEYEQ(speculative_keys[i].key, key)) { 759 break; 760 } 761 } 762 return i; 763 } 764 765 static void speculative_key_press(keyrecord_t *record) { 766 if (num_speculative_keys >= SPECULATIVE_KEYS_SIZE) { // Overflow! 767 ac_dprintf("SPECULATIVE KEYS OVERFLOW: IGNORING EVENT\n"); 768 return; // Don't trigger: speculative_keys is full. 769 } 770 if (speculative_keys_find(record->event.key) < num_speculative_keys) { 771 return; // Don't trigger: key is already in speculative_keys. 772 } 773 774 const uint16_t keycode = get_record_keycode(record, false); 775 if (!IS_QK_MOD_TAP(keycode)) { 776 return; // Don't trigger: not a mod-tap key. 777 } 778 779 uint8_t mods = mod_config(QK_MOD_TAP_GET_MODS(keycode)); 780 if ((mods & 0x10) != 0) { // Unpack 5-bit mods to 8-bit representation. 781 mods <<= 4; 782 } 783 if ((~(get_mods() | speculative_mods) & mods) == 0) { 784 return; // Don't trigger: mods are already active. 785 } 786 787 // Don't do Speculative Hold when there are non-speculated buffered events, 788 // since that could result in sending keys out of order. 789 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 790 if (!waiting_buffer[i].tap.speculated) { 791 return; 792 } 793 } 794 795 if (get_speculative_hold(keycode, record)) { 796 record->tap.speculated = true; 797 speculative_mods |= mods; 798 // Remember the keypos and mods associated with this key. 799 speculative_keys[num_speculative_keys] = (speculative_key_t){ 800 .key = record->event.key, 801 .mods = mods, 802 }; 803 ++num_speculative_keys; 804 805 ac_dprintf("Speculative Hold: "); 806 debug_speculative_keys(); 807 } 808 } 809 810 uint8_t get_speculative_mods(void) { 811 return speculative_mods; 812 } 813 814 __attribute__((weak)) bool get_speculative_hold(uint16_t keycode, keyrecord_t *record) { 815 const uint8_t mods = mod_config(QK_MOD_TAP_GET_MODS(keycode)); 816 return (mods & (MOD_LCTL | MOD_LSFT)) == (mods & (MOD_HYPR)); 817 } 818 819 void speculative_key_settled(keyrecord_t *record) { 820 if (num_speculative_keys == 0) { 821 return; // Early return when there are no active speculative keys. 822 } 823 824 uint8_t i = speculative_keys_find(record->event.key); 825 826 const uint16_t keycode = get_record_keycode(record, false); 827 if (IS_QK_MOD_TAP(keycode) && record->tap.count == 0) { // MT hold press. 828 if (i < num_speculative_keys) { 829 --num_speculative_keys; 830 const uint8_t cleared_mods = speculative_keys[i].mods; 831 832 if (num_speculative_keys) { 833 speculative_mods &= ~cleared_mods; 834 // Don't call send_keyboard_report() here; allow default 835 // handling to reapply the mod before the next report. 836 837 // Remove the ith entry from speculative_keys. 838 for (uint8_t j = i; j < num_speculative_keys; ++j) { 839 speculative_keys[j] = speculative_keys[j + 1]; 840 } 841 } else { 842 speculative_mods = 0; 843 } 844 845 ac_dprintf("Speculative Hold: settled %02x, ", cleared_mods); 846 debug_speculative_keys(); 847 } 848 } else { // Tap press event; cancel speculatively-held mod. 849 if (i >= num_speculative_keys) { 850 i = 0; 851 } 852 853 // Clear mods for the ith key and all keys that follow. 854 uint8_t cleared_mods = 0; 855 for (uint8_t j = i; j < num_speculative_keys; ++j) { 856 cleared_mods |= speculative_keys[j].mods; 857 } 858 859 num_speculative_keys = i; // Remove ith and following entries. 860 861 if ((prev_speculative_mods & cleared_mods) != 0) { 862 # ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE 863 neutralize_flashing_modifiers(get_mods() | prev_speculative_mods); 864 # endif // DUMMY_MOD_NEUTRALIZER_KEYCODE 865 } 866 867 if (num_speculative_keys) { 868 speculative_mods &= ~cleared_mods; 869 } else { 870 speculative_mods = 0; 871 } 872 873 send_keyboard_report(); 874 wait_ms(TAP_CODE_DELAY); 875 876 ac_dprintf("Speculative Hold: canceled %02x, ", cleared_mods); 877 debug_speculative_keys(); 878 } 879 } 880 # endif // SPECULATIVE_HOLD 881 882 # if defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM) 883 static void registered_taps_add(keypos_t key) { 884 if (num_registered_taps >= REGISTERED_TAPS_SIZE) { 885 ac_dprintf("TAPS OVERFLOW: CLEAR ALL STATES\n"); 886 clear_keyboard(); 887 num_registered_taps = 0; 888 } 889 890 registered_taps[num_registered_taps] = key; 891 ++num_registered_taps; 892 } 893 894 static int8_t registered_tap_find(keypos_t key) { 895 for (int8_t i = 0; i < num_registered_taps; ++i) { 896 if (KEYEQ(registered_taps[i], key)) { 897 return i; 898 } 899 } 900 return -1; 901 } 902 903 static void registered_taps_del_index(uint8_t i) { 904 if (i < num_registered_taps) { 905 --num_registered_taps; 906 if (i < num_registered_taps) { 907 registered_taps[i] = registered_taps[num_registered_taps]; 908 } 909 } 910 } 911 912 static void debug_registered_taps(void) { 913 ac_dprintf("registered_taps = { "); 914 for (int8_t i = 0; i < num_registered_taps; ++i) { 915 ac_dprintf("%02X%02X ", registered_taps[i].row, registered_taps[i].col); 916 } 917 ac_dprintf("}\n"); 918 } 919 920 # endif // defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM) 921 922 # ifdef CHORDAL_HOLD 923 __attribute__((weak)) bool get_chordal_hold(uint16_t tap_hold_keycode, keyrecord_t *tap_hold_record, uint16_t other_keycode, keyrecord_t *other_record) { 924 return get_chordal_hold_default(tap_hold_record, other_record); 925 } 926 927 bool get_chordal_hold_default(keyrecord_t *tap_hold_record, keyrecord_t *other_record) { 928 if (tap_hold_record->event.type != KEY_EVENT || other_record->event.type != KEY_EVENT) { 929 return true; // Return true on combos or other non-key events. 930 } 931 932 char tap_hold_hand = chordal_hold_handedness(tap_hold_record->event.key); 933 if (tap_hold_hand == '*') { 934 return true; 935 } 936 char other_hand = chordal_hold_handedness(other_record->event.key); 937 return other_hand == '*' || tap_hold_hand != other_hand; 938 } 939 940 __attribute__((weak)) char chordal_hold_handedness(keypos_t key) { 941 return (char)pgm_read_byte(&chordal_hold_layout[key.row][key.col]); 942 } 943 944 static uint8_t waiting_buffer_find_chordal_hold_tap(void) { 945 keyrecord_t *prev = &tapping_key; 946 uint16_t prev_keycode = get_record_keycode(&tapping_key, false); 947 uint8_t first_tap = WAITING_BUFFER_SIZE; 948 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 949 keyrecord_t *cur = &waiting_buffer[i]; 950 const uint16_t cur_keycode = get_record_keycode(cur, false); 951 if (!cur->event.pressed || !is_mt_or_lt(prev_keycode)) { 952 break; 953 } else if (get_chordal_hold(prev_keycode, prev, cur_keycode, cur)) { 954 first_tap = i; // Track one index past the latest hold. 955 } 956 prev = cur; 957 prev_keycode = cur_keycode; 958 } 959 return first_tap; 960 } 961 962 static void waiting_buffer_chordal_hold_taps_until(keypos_t key) { 963 while (waiting_buffer_tail != waiting_buffer_head) { 964 keyrecord_t *record = &waiting_buffer[waiting_buffer_tail]; 965 ac_dprintf("waiting_buffer_chordal_hold_taps_until: processing [%u]\n", waiting_buffer_tail); 966 if (record->event.pressed && is_tap_record(record)) { 967 record->tap.count = 1; 968 registered_taps_add(record->event.key); 969 } 970 process_record(record); 971 waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE; 972 973 if (KEYEQ(key, record->event.key) && record->event.pressed) { 974 break; 975 } 976 } 977 } 978 979 static void waiting_buffer_process_regular(void) { 980 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { 981 if (is_tap_record(&waiting_buffer[waiting_buffer_tail])) { 982 break; // Stop once a tap-hold key event is reached. 983 } 984 ac_dprintf("waiting_buffer_process_regular: processing [%u]\n", waiting_buffer_tail); 985 process_record(&waiting_buffer[waiting_buffer_tail]); 986 } 987 debug_waiting_buffer(); 988 } 989 # endif // CHORDAL_HOLD 990 991 # ifdef FLOW_TAP_TERM 992 void flow_tap_update_last_event(keyrecord_t *record) { 993 const uint16_t keycode = get_record_keycode(record, false); 994 // Don't update while a tap-hold key is unsettled. 995 if (record->tap.count == 0 && (waiting_buffer_tail != waiting_buffer_head || (tapping_key.event.pressed && tapping_key.tap.count == 0))) { 996 return; 997 } 998 // Ignore releases of modifiers and held layer switches. 999 if (!record->event.pressed) { 1000 switch (keycode) { 1001 case MODIFIER_KEYCODE_RANGE: 1002 case QK_MOMENTARY ... QK_MOMENTARY_MAX: 1003 case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: 1004 # ifndef NO_ACTION_ONESHOT // Ignore one-shot keys. 1005 case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX: 1006 case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: 1007 # endif // NO_ACTION_ONESHOT 1008 # ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys. 1009 case QK_TRI_LAYER_LOWER: 1010 case QK_TRI_LAYER_UPPER: 1011 # endif // TRI_LAYER_ENABLE 1012 return; 1013 case QK_MODS ... QK_MODS_MAX: 1014 if (QK_MODS_GET_BASIC_KEYCODE(keycode) == KC_NO) { 1015 return; 1016 } 1017 break; 1018 case QK_MOD_TAP ... QK_MOD_TAP_MAX: 1019 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: 1020 if (record->tap.count == 0) { 1021 return; 1022 } 1023 break; 1024 } 1025 } 1026 1027 flow_tap_prev_keycode = keycode; 1028 flow_tap_prev_time = record->event.time; 1029 flow_tap_expired = false; 1030 } 1031 1032 static bool flow_tap_key_if_within_term(keyrecord_t *record, uint16_t prev_time) { 1033 const uint16_t idle_time = TIMER_DIFF_16(record->event.time, prev_time); 1034 if (flow_tap_expired || idle_time >= 500) { 1035 return false; 1036 } 1037 1038 const uint16_t keycode = get_record_keycode(record, false); 1039 if (is_mt_or_lt(keycode)) { 1040 uint16_t term = get_flow_tap_term(keycode, record, flow_tap_prev_keycode); 1041 if (term > 500) { 1042 term = 500; 1043 } 1044 if (idle_time < term) { 1045 debug_event(record->event); 1046 ac_dprintf(" within flow tap term (%u < %u) considered a tap\n", idle_time, term); 1047 record->tap.count = 1; 1048 registered_taps_add(record->event.key); 1049 debug_registered_taps(); 1050 process_record(record); 1051 return true; 1052 } 1053 } 1054 return false; 1055 } 1056 1057 // Checks both flow_tap_expired flag and elapsed time to determine 1058 // if the key is within the flow tap term. 1059 bool within_flow_tap_term(uint16_t keycode, keyrecord_t *record) { 1060 uint16_t term = get_flow_tap_term(keycode, record, flow_tap_prev_keycode); 1061 return !flow_tap_expired && TIMER_DIFF_16(record->event.time, flow_tap_prev_time) <= term; 1062 } 1063 1064 // By default, enable Flow Tap for the keys in the main alphas area and Space. 1065 // This should work reasonably even if the layout is remapped on the host to an 1066 // alt layout or international layout (e.g. Dvorak or AZERTY), where these same 1067 // key positions are mostly used for typing letters. 1068 __attribute__((weak)) bool is_flow_tap_key(uint16_t keycode) { 1069 if ((get_mods() & (MOD_MASK_CG | MOD_BIT_LALT)) != 0) { 1070 return false; // Disable Flow Tap on hotkeys. 1071 } 1072 switch (get_tap_keycode(keycode)) { 1073 case KC_SPC: 1074 case KC_A ... KC_Z: 1075 case KC_DOT: 1076 case KC_COMM: 1077 case KC_SCLN: 1078 case KC_SLSH: 1079 return true; 1080 } 1081 return false; 1082 } 1083 1084 __attribute__((weak)) uint16_t get_flow_tap_term(uint16_t keycode, keyrecord_t *record, uint16_t prev_keycode) { 1085 if (is_flow_tap_key(keycode) && is_flow_tap_key(prev_keycode)) { 1086 return FLOW_TAP_TERM; 1087 } 1088 return 0; 1089 } 1090 # endif // FLOW_TAP_TERM 1091 1092 /** \brief Logs tapping key if ACTION_DEBUG is enabled. */ 1093 static void debug_tapping_key(void) { 1094 ac_dprintf("TAPPING_KEY="); 1095 debug_record(tapping_key); 1096 ac_dprintf("\n"); 1097 } 1098 1099 /** \brief Logs waiting buffer if ACTION_DEBUG is enabled. */ 1100 static void debug_waiting_buffer(void) { 1101 ac_dprintf("{"); 1102 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { 1103 ac_dprintf(" [%u]=", i); 1104 debug_record(waiting_buffer[i]); 1105 } 1106 ac_dprintf("}\n"); 1107 } 1108 1109 #endif