action_tapping.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "action.h"
  4. #include "action_layer.h"
  5. #include "action_tapping.h"
  6. #include "keycode.h"
  7. #include "timer.h"
  8. #ifndef NO_ACTION_TAPPING
  9. # if defined(IGNORE_MOD_TAP_INTERRUPT_PER_KEY)
  10. # 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."
  11. # elif defined(IGNORE_MOD_TAP_INTERRUPT)
  12. # 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."
  13. # endif
  14. # ifndef COMBO_ENABLE
  15. # define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key)))
  16. # else
  17. # define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode)
  18. # endif
  19. # 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))
  20. # 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))
  21. # ifdef DYNAMIC_TAPPING_TERM_ENABLE
  22. uint16_t g_tapping_term = TAPPING_TERM;
  23. # endif
  24. # ifdef TAPPING_TERM_PER_KEY
  25. __attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
  26. # ifdef DYNAMIC_TAPPING_TERM_ENABLE
  27. return g_tapping_term;
  28. # else
  29. return TAPPING_TERM;
  30. # endif
  31. }
  32. # endif
  33. # ifdef QUICK_TAP_TERM_PER_KEY
  34. __attribute__((weak)) uint16_t get_quick_tap_term(uint16_t keycode, keyrecord_t *record) {
  35. return QUICK_TAP_TERM;
  36. }
  37. # endif
  38. # ifdef PERMISSIVE_HOLD_PER_KEY
  39. __attribute__((weak)) bool get_permissive_hold(uint16_t keycode, keyrecord_t *record) {
  40. return false;
  41. }
  42. # endif
  43. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  44. __attribute__((weak)) bool get_hold_on_other_key_press(uint16_t keycode, keyrecord_t *record) {
  45. return false;
  46. }
  47. # endif
  48. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  49. # include "process_auto_shift.h"
  50. # endif
  51. static keyrecord_t tapping_key = {};
  52. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  53. static uint8_t waiting_buffer_head = 0;
  54. static uint8_t waiting_buffer_tail = 0;
  55. static bool process_tapping(keyrecord_t *record);
  56. static bool waiting_buffer_enq(keyrecord_t record);
  57. static void waiting_buffer_clear(void);
  58. static bool waiting_buffer_typed(keyevent_t event);
  59. static bool waiting_buffer_has_anykey_pressed(void);
  60. static void waiting_buffer_scan_tap(void);
  61. static void debug_tapping_key(void);
  62. static void debug_waiting_buffer(void);
  63. /** \brief Action Tapping Process
  64. *
  65. * FIXME: Needs doc
  66. */
  67. void action_tapping_process(keyrecord_t record) {
  68. if (process_tapping(&record)) {
  69. if (IS_EVENT(record.event)) {
  70. ac_dprintf("processed: ");
  71. debug_record(record);
  72. ac_dprintf("\n");
  73. }
  74. } else {
  75. if (!waiting_buffer_enq(record)) {
  76. // clear all in case of overflow.
  77. ac_dprintf("OVERFLOW: CLEAR ALL STATES\n");
  78. clear_keyboard();
  79. waiting_buffer_clear();
  80. tapping_key = (keyrecord_t){0};
  81. }
  82. }
  83. // process waiting_buffer
  84. if (IS_EVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
  85. ac_dprintf("---- action_exec: process waiting_buffer -----\n");
  86. }
  87. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  88. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  89. ac_dprintf("processed: waiting_buffer[%u] =", waiting_buffer_tail);
  90. debug_record(waiting_buffer[waiting_buffer_tail]);
  91. ac_dprintf("\n\n");
  92. } else {
  93. break;
  94. }
  95. }
  96. if (IS_EVENT(record.event)) {
  97. ac_dprintf("\n");
  98. }
  99. }
  100. /* Some conditionally defined helper macros to keep process_tapping more
  101. * readable. The conditional definition of tapping_keycode and all the
  102. * conditional uses of it are hidden inside macros named TAP_...
  103. */
  104. # define TAP_DEFINE_KEYCODE const uint16_t tapping_keycode = get_record_keycode(&tapping_key, false)
  105. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  106. # ifdef RETRO_TAPPING_PER_KEY
  107. # define TAP_GET_RETRO_TAPPING(keyp) get_auto_shifted_key(tapping_keycode, keyp) && get_retro_tapping(tapping_keycode, &tapping_key)
  108. # else
  109. # define TAP_GET_RETRO_TAPPING(keyp) get_auto_shifted_key(tapping_keycode, keyp)
  110. # endif
  111. /* Used to extend TAPPING_TERM:
  112. * indefinitely if RETRO_SHIFT does not have a value
  113. * to RETRO_SHIFT if RETRO_SHIFT is set
  114. * for possibly retro shifted keys.
  115. */
  116. # 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)))
  117. # define TAP_IS_LT IS_QK_LAYER_TAP(tapping_keycode)
  118. # define TAP_IS_MT IS_QK_MOD_TAP(tapping_keycode)
  119. # define TAP_IS_RETRO IS_RETRO(tapping_keycode)
  120. # else
  121. # define TAP_GET_RETRO_TAPPING(keyp) false
  122. # define MAYBE_RETRO_SHIFTING(ev, kp) false
  123. # define TAP_IS_LT false
  124. # define TAP_IS_MT false
  125. # define TAP_IS_RETRO false
  126. # endif
  127. # ifdef PERMISSIVE_HOLD_PER_KEY
  128. # define TAP_GET_PERMISSIVE_HOLD get_permissive_hold(tapping_keycode, &tapping_key)
  129. # elif defined(PERMISSIVE_HOLD)
  130. # define TAP_GET_PERMISSIVE_HOLD true
  131. # else
  132. # define TAP_GET_PERMISSIVE_HOLD false
  133. # endif
  134. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  135. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS get_hold_on_other_key_press(tapping_keycode, &tapping_key)
  136. # elif defined(HOLD_ON_OTHER_KEY_PRESS)
  137. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS true
  138. # else
  139. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS false
  140. # endif
  141. /** \brief Tapping
  142. *
  143. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  144. * (without interfering by typing other key)
  145. */
  146. /* return true when key event is processed or consumed. */
  147. bool process_tapping(keyrecord_t *keyp) {
  148. const keyevent_t event = keyp->event;
  149. // state machine is in the "reset" state, no tapping key is to be
  150. // processed
  151. if (IS_NOEVENT(tapping_key.event)) {
  152. if (!IS_EVENT(event)) {
  153. // early return for tick events
  154. } else if (event.pressed && is_tap_record(keyp)) {
  155. // the currently pressed key is a tapping key, therefore transition
  156. // into the "pressed" tapping key state
  157. ac_dprintf("Tapping: Start(Press tap key).\n");
  158. tapping_key = *keyp;
  159. process_record_tap_hint(&tapping_key);
  160. waiting_buffer_scan_tap();
  161. debug_tapping_key();
  162. } else {
  163. // the current key is just a regular key, pass it on for regular
  164. // processing
  165. process_record(keyp);
  166. }
  167. return true;
  168. }
  169. # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) || defined(PERMISSIVE_HOLD_PER_KEY) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
  170. TAP_DEFINE_KEYCODE;
  171. # endif
  172. // process "pressed" tapping key state
  173. if (tapping_key.event.pressed) {
  174. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event, keyp)) {
  175. if (IS_NOEVENT(event)) {
  176. // early return for tick events
  177. return true;
  178. }
  179. if (tapping_key.tap.count == 0) {
  180. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  181. // first tap!
  182. ac_dprintf("Tapping: First tap(0->1).\n");
  183. tapping_key.tap.count = 1;
  184. debug_tapping_key();
  185. process_record(&tapping_key);
  186. // copy tapping state
  187. keyp->tap = tapping_key.tap;
  188. // enqueue
  189. return false;
  190. }
  191. /* Process a key typed within TAPPING_TERM
  192. * This can register the key before settlement of tapping,
  193. * useful for long TAPPING_TERM but may prevent fast typing.
  194. */
  195. // clang-format off
  196. else if (
  197. !event.pressed && waiting_buffer_typed(event) &&
  198. (
  199. TAP_GET_PERMISSIVE_HOLD ||
  200. // Causes nested taps to not wait past TAPPING_TERM/RETRO_SHIFT
  201. // unnecessarily and fixes them for Layer Taps.
  202. TAP_GET_RETRO_TAPPING(keyp)
  203. )
  204. ) {
  205. // clang-format on
  206. ac_dprintf("Tapping: End. No tap. Interfered by typing key\n");
  207. process_record(&tapping_key);
  208. tapping_key = (keyrecord_t){0};
  209. debug_tapping_key();
  210. // enqueue
  211. return false;
  212. }
  213. /* Process release event of a key pressed before tapping starts
  214. * Without this unexpected repeating will occur with having fast repeating setting
  215. * https://github.com/tmk/tmk_keyboard/issues/60
  216. */
  217. else if (!event.pressed && !waiting_buffer_typed(event)) {
  218. // Modifier/Layer should be retained till end of this tapping.
  219. action_t action = layer_switch_get_action(event.key);
  220. switch (action.kind.id) {
  221. case ACT_LMODS:
  222. case ACT_RMODS:
  223. if (action.key.mods && !action.key.code) return false;
  224. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  225. break;
  226. case ACT_LMODS_TAP:
  227. case ACT_RMODS_TAP:
  228. if (action.key.mods && keyp->tap.count == 0) return false;
  229. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  230. break;
  231. case ACT_LAYER_TAP:
  232. case ACT_LAYER_TAP_EXT:
  233. switch (action.layer_tap.code) {
  234. case 0 ...(OP_TAP_TOGGLE - 1):
  235. case OP_ON_OFF:
  236. case OP_OFF_ON:
  237. case OP_SET_CLEAR:
  238. return false;
  239. }
  240. break;
  241. }
  242. // Release of key should be process immediately.
  243. ac_dprintf("Tapping: release event of a key pressed before tapping\n");
  244. process_record(keyp);
  245. return true;
  246. } else {
  247. // set interrupted flag when other key pressed during tapping
  248. if (event.pressed) {
  249. tapping_key.tap.interrupted = true;
  250. if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS
  251. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  252. // Auto Shift cannot evaluate this early
  253. // Retro Shift uses the hold action for all nested taps even without HOLD_ON_OTHER_KEY_PRESS, so this is fine to skip
  254. && !(MAYBE_RETRO_SHIFTING(event, keyp) && get_auto_shifted_key(get_record_keycode(keyp, false), keyp))
  255. # endif
  256. ) {
  257. ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n");
  258. process_record(&tapping_key);
  259. tapping_key = (keyrecord_t){0};
  260. debug_tapping_key();
  261. // enqueue
  262. return false;
  263. }
  264. }
  265. // enqueue
  266. return false;
  267. }
  268. }
  269. // tap_count > 0
  270. else {
  271. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  272. ac_dprintf("Tapping: Tap release(%u)\n", tapping_key.tap.count);
  273. keyp->tap = tapping_key.tap;
  274. process_record(keyp);
  275. tapping_key = *keyp;
  276. debug_tapping_key();
  277. return true;
  278. } else if (is_tap_record(keyp) && event.pressed) {
  279. if (tapping_key.tap.count > 1) {
  280. ac_dprintf("Tapping: Start new tap with releasing last tap(>1).\n");
  281. // unregister key
  282. process_record(&(keyrecord_t){
  283. .tap = tapping_key.tap,
  284. .event.key = tapping_key.event.key,
  285. .event.time = event.time,
  286. .event.pressed = false,
  287. .event.type = tapping_key.event.type,
  288. # ifdef COMBO_ENABLE
  289. .keycode = tapping_key.keycode,
  290. # endif
  291. });
  292. } else {
  293. ac_dprintf("Tapping: Start while last tap(1).\n");
  294. }
  295. tapping_key = *keyp;
  296. waiting_buffer_scan_tap();
  297. debug_tapping_key();
  298. return true;
  299. } else {
  300. ac_dprintf("Tapping: key event while last tap(>0).\n");
  301. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  302. retroshift_swap_times();
  303. # endif
  304. process_record(keyp);
  305. return true;
  306. }
  307. }
  308. }
  309. // after TAPPING_TERM
  310. else {
  311. if (tapping_key.tap.count == 0) {
  312. ac_dprintf("Tapping: End. Timeout. Not tap(0): ");
  313. debug_event(event);
  314. ac_dprintf("\n");
  315. process_record(&tapping_key);
  316. tapping_key = (keyrecord_t){0};
  317. debug_tapping_key();
  318. return false;
  319. } else {
  320. if (IS_NOEVENT(event)) {
  321. return true;
  322. }
  323. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  324. ac_dprintf("Tapping: End. last timeout tap release(>0).");
  325. keyp->tap = tapping_key.tap;
  326. process_record(keyp);
  327. tapping_key = (keyrecord_t){0};
  328. return true;
  329. } else if (is_tap_record(keyp) && event.pressed) {
  330. if (tapping_key.tap.count > 1) {
  331. ac_dprintf("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  332. // unregister key
  333. process_record(&(keyrecord_t){
  334. .tap = tapping_key.tap,
  335. .event.key = tapping_key.event.key,
  336. .event.time = event.time,
  337. .event.pressed = false,
  338. .event.type = tapping_key.event.type,
  339. # ifdef COMBO_ENABLE
  340. .keycode = tapping_key.keycode,
  341. # endif
  342. });
  343. } else {
  344. ac_dprintf("Tapping: Start while last timeout tap(1).\n");
  345. }
  346. tapping_key = *keyp;
  347. waiting_buffer_scan_tap();
  348. debug_tapping_key();
  349. return true;
  350. } else {
  351. ac_dprintf("Tapping: key event while last timeout tap(>0).\n");
  352. process_record(keyp);
  353. return true;
  354. }
  355. }
  356. }
  357. }
  358. // process "released" tapping key state
  359. else {
  360. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event, keyp)) {
  361. if (IS_NOEVENT(event)) {
  362. // early return for tick events
  363. return true;
  364. }
  365. if (event.pressed) {
  366. if (IS_TAPPING_RECORD(keyp)) {
  367. if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  368. // sequential tap.
  369. keyp->tap = tapping_key.tap;
  370. if (keyp->tap.count < 15) keyp->tap.count += 1;
  371. ac_dprintf("Tapping: Tap press(%u)\n", keyp->tap.count);
  372. process_record(keyp);
  373. tapping_key = *keyp;
  374. debug_tapping_key();
  375. return true;
  376. }
  377. // FIX: start new tap again
  378. tapping_key = *keyp;
  379. return true;
  380. } else if (is_tap_record(keyp)) {
  381. // Sequential tap can be interfered with other tap key.
  382. ac_dprintf("Tapping: Start with interfering other tap.\n");
  383. tapping_key = *keyp;
  384. waiting_buffer_scan_tap();
  385. debug_tapping_key();
  386. return true;
  387. } else {
  388. // should none in buffer
  389. // FIX: interrupted when other key is pressed
  390. tapping_key.tap.interrupted = true;
  391. process_record(keyp);
  392. return true;
  393. }
  394. } else {
  395. ac_dprintf("Tapping: other key just after tap.\n");
  396. process_record(keyp);
  397. return true;
  398. }
  399. } else {
  400. // Timeout - reset state machine.
  401. ac_dprintf("Tapping: End(Timeout after releasing last tap): ");
  402. debug_event(event);
  403. ac_dprintf("\n");
  404. tapping_key = (keyrecord_t){0};
  405. debug_tapping_key();
  406. return false;
  407. }
  408. }
  409. }
  410. /** \brief Waiting buffer enq
  411. *
  412. * FIXME: Needs docs
  413. */
  414. bool waiting_buffer_enq(keyrecord_t record) {
  415. if (IS_NOEVENT(record.event)) {
  416. return true;
  417. }
  418. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  419. ac_dprintf("waiting_buffer_enq: Over flow.\n");
  420. return false;
  421. }
  422. waiting_buffer[waiting_buffer_head] = record;
  423. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  424. ac_dprintf("waiting_buffer_enq: ");
  425. debug_waiting_buffer();
  426. return true;
  427. }
  428. /** \brief Waiting buffer clear
  429. *
  430. * FIXME: Needs docs
  431. */
  432. void waiting_buffer_clear(void) {
  433. waiting_buffer_head = 0;
  434. waiting_buffer_tail = 0;
  435. }
  436. /** \brief Waiting buffer typed
  437. *
  438. * FIXME: Needs docs
  439. */
  440. bool waiting_buffer_typed(keyevent_t event) {
  441. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  442. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  443. return true;
  444. }
  445. }
  446. return false;
  447. }
  448. /** \brief Waiting buffer has anykey pressed
  449. *
  450. * FIXME: Needs docs
  451. */
  452. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  453. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  454. if (waiting_buffer[i].event.pressed) return true;
  455. }
  456. return false;
  457. }
  458. /** \brief Scan buffer for tapping
  459. *
  460. * FIXME: Needs docs
  461. */
  462. void waiting_buffer_scan_tap(void) {
  463. // early return if:
  464. // - tapping already is settled
  465. // - invalid state: tapping_key released && tap.count == 0
  466. if ((tapping_key.tap.count > 0) || !tapping_key.event.pressed) {
  467. return;
  468. }
  469. # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT))
  470. TAP_DEFINE_KEYCODE;
  471. # endif
  472. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  473. keyrecord_t *candidate = &waiting_buffer[i];
  474. // clang-format off
  475. if (IS_EVENT(candidate->event) && KEYEQ(candidate->event.key, tapping_key.event.key) && !candidate->event.pressed && (
  476. WITHIN_TAPPING_TERM(waiting_buffer[i].event) || MAYBE_RETRO_SHIFTING(waiting_buffer[i].event, &tapping_key)
  477. )) {
  478. // clang-format on
  479. tapping_key.tap.count = 1;
  480. candidate->tap.count = 1;
  481. process_record(&tapping_key);
  482. ac_dprintf("waiting_buffer_scan_tap: found at [%u]\n", i);
  483. debug_waiting_buffer();
  484. return;
  485. }
  486. }
  487. }
  488. /** \brief Tapping key debug print
  489. *
  490. * FIXME: Needs docs
  491. */
  492. static void debug_tapping_key(void) {
  493. ac_dprintf("TAPPING_KEY=");
  494. debug_record(tapping_key);
  495. ac_dprintf("\n");
  496. }
  497. /** \brief Waiting buffer debug print
  498. *
  499. * FIXME: Needs docs
  500. */
  501. static void debug_waiting_buffer(void) {
  502. ac_dprintf("{ ");
  503. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  504. ac_dprintf("[%u]=", i);
  505. debug_record(waiting_buffer[i]);
  506. ac_dprintf(" ");
  507. }
  508. ac_dprintf("}\n");
  509. }
  510. #endif