action_tapping.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) || defined(PERMISSIVE_HOLD_PER_KEY) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
  105. # define TAP_DEFINE_KEYCODE const uint16_t tapping_keycode = get_record_keycode(&tapping_key, false)
  106. # else
  107. # define TAP_DEFINE_KEYCODE
  108. # endif
  109. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  110. # ifdef RETRO_TAPPING_PER_KEY
  111. # define TAP_GET_RETRO_TAPPING get_retro_tapping(tapping_keycode, &tapping_key)
  112. # else
  113. # define TAP_GET_RETRO_TAPPING true
  114. # endif
  115. # define MAYBE_RETRO_SHIFTING(ev) (TAP_GET_RETRO_TAPPING && (RETRO_SHIFT + 0) != 0 && TIMER_DIFF_16((ev).time, tapping_key.event.time) < (RETRO_SHIFT + 0))
  116. # define TAP_IS_LT IS_QK_LAYER_TAP(tapping_keycode)
  117. # define TAP_IS_MT IS_QK_MOD_TAP(tapping_keycode)
  118. # define TAP_IS_RETRO IS_RETRO(tapping_keycode)
  119. # else
  120. # define TAP_GET_RETRO_TAPPING false
  121. # define MAYBE_RETRO_SHIFTING(ev) false
  122. # define TAP_IS_LT false
  123. # define TAP_IS_MT false
  124. # define TAP_IS_RETRO false
  125. # endif
  126. # ifdef PERMISSIVE_HOLD_PER_KEY
  127. # define TAP_GET_PERMISSIVE_HOLD get_permissive_hold(tapping_keycode, &tapping_key)
  128. # elif defined(PERMISSIVE_HOLD)
  129. # define TAP_GET_PERMISSIVE_HOLD true
  130. # else
  131. # define TAP_GET_PERMISSIVE_HOLD false
  132. # endif
  133. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  134. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS get_hold_on_other_key_press(tapping_keycode, &tapping_key)
  135. # elif defined(HOLD_ON_OTHER_KEY_PRESS)
  136. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS true
  137. # else
  138. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS false
  139. # endif
  140. /** \brief Tapping
  141. *
  142. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  143. * (without interfering by typing other key)
  144. */
  145. /* return true when key event is processed or consumed. */
  146. bool process_tapping(keyrecord_t *keyp) {
  147. const keyevent_t event = keyp->event;
  148. // state machine is in the "reset" state, no tapping key is to be
  149. // processed
  150. if (IS_NOEVENT(tapping_key.event) && IS_EVENT(event)) {
  151. if (event.pressed && is_tap_record(keyp)) {
  152. // the currently pressed key is a tapping key, therefore transition
  153. // into the "pressed" tapping key state
  154. ac_dprintf("Tapping: Start(Press tap key).\n");
  155. tapping_key = *keyp;
  156. process_record_tap_hint(&tapping_key);
  157. waiting_buffer_scan_tap();
  158. debug_tapping_key();
  159. return true;
  160. } else {
  161. // the current key is just a regular key, pass it on for regular
  162. // processing
  163. process_record(keyp);
  164. return true;
  165. }
  166. }
  167. TAP_DEFINE_KEYCODE;
  168. // process "pressed" tapping key state
  169. if (tapping_key.event.pressed) {
  170. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) {
  171. if (IS_NOEVENT(event)) {
  172. // early return for tick events
  173. return true;
  174. }
  175. if (tapping_key.tap.count == 0) {
  176. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  177. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  178. retroshift_swap_times();
  179. # endif
  180. // first tap!
  181. ac_dprintf("Tapping: First tap(0->1).\n");
  182. tapping_key.tap.count = 1;
  183. debug_tapping_key();
  184. process_record(&tapping_key);
  185. // copy tapping state
  186. keyp->tap = tapping_key.tap;
  187. // enqueue
  188. return false;
  189. }
  190. /* Process a key typed within TAPPING_TERM
  191. * This can register the key before settlement of tapping,
  192. * useful for long TAPPING_TERM but may prevent fast typing.
  193. */
  194. // clang-format off
  195. else if (
  196. (
  197. !event.pressed && waiting_buffer_typed(event) &&
  198. TAP_GET_PERMISSIVE_HOLD
  199. )
  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 &&
  203. (
  204. // Rolled over the two keys.
  205. (tapping_key.tap.interrupted == true && (
  206. (TAP_IS_LT && TAP_GET_HOLD_ON_OTHER_KEY_PRESS) ||
  207. (TAP_IS_MT && TAP_GET_HOLD_ON_OTHER_KEY_PRESS)
  208. )
  209. )
  210. // Makes Retro Shift ignore the default behavior of
  211. // MTs and LTs on nested taps below TAPPING_TERM or RETRO_SHIFT
  212. || (
  213. TAP_IS_RETRO
  214. && (event.key.col != tapping_key.event.key.col || event.key.row != tapping_key.event.key.row)
  215. && !event.pressed && waiting_buffer_typed(event)
  216. )
  217. )
  218. )
  219. ) {
  220. // clang-format on
  221. ac_dprintf("Tapping: End. No tap. Interfered by typing key\n");
  222. process_record(&tapping_key);
  223. tapping_key = (keyrecord_t){0};
  224. debug_tapping_key();
  225. // enqueue
  226. return false;
  227. }
  228. /* Process release event of a key pressed before tapping starts
  229. * Without this unexpected repeating will occur with having fast repeating setting
  230. * https://github.com/tmk/tmk_keyboard/issues/60
  231. */
  232. else if (!event.pressed && !waiting_buffer_typed(event)) {
  233. // Modifier/Layer should be retained till end of this tapping.
  234. action_t action = layer_switch_get_action(event.key);
  235. switch (action.kind.id) {
  236. case ACT_LMODS:
  237. case ACT_RMODS:
  238. if (action.key.mods && !action.key.code) return false;
  239. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  240. break;
  241. case ACT_LMODS_TAP:
  242. case ACT_RMODS_TAP:
  243. if (action.key.mods && keyp->tap.count == 0) return false;
  244. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  245. break;
  246. case ACT_LAYER_TAP:
  247. case ACT_LAYER_TAP_EXT:
  248. switch (action.layer_tap.code) {
  249. case 0 ...(OP_TAP_TOGGLE - 1):
  250. case OP_ON_OFF:
  251. case OP_OFF_ON:
  252. case OP_SET_CLEAR:
  253. return false;
  254. }
  255. break;
  256. }
  257. // Release of key should be process immediately.
  258. ac_dprintf("Tapping: release event of a key pressed before tapping\n");
  259. process_record(keyp);
  260. return true;
  261. } else {
  262. // set interrupted flag when other key preesed during tapping
  263. if (event.pressed) {
  264. tapping_key.tap.interrupted = true;
  265. if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS) {
  266. ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n");
  267. process_record(&tapping_key);
  268. tapping_key = (keyrecord_t){0};
  269. debug_tapping_key();
  270. // enqueue
  271. return false;
  272. }
  273. }
  274. // enqueue
  275. return false;
  276. }
  277. }
  278. // tap_count > 0
  279. else {
  280. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  281. ac_dprintf("Tapping: Tap release(%u)\n", tapping_key.tap.count);
  282. keyp->tap = tapping_key.tap;
  283. process_record(keyp);
  284. tapping_key = *keyp;
  285. debug_tapping_key();
  286. return true;
  287. } else if (is_tap_record(keyp) && event.pressed) {
  288. if (tapping_key.tap.count > 1) {
  289. ac_dprintf("Tapping: Start new tap with releasing last tap(>1).\n");
  290. // unregister key
  291. process_record(&(keyrecord_t){
  292. .tap = tapping_key.tap,
  293. .event.key = tapping_key.event.key,
  294. .event.time = event.time,
  295. .event.pressed = false,
  296. .event.type = tapping_key.event.type,
  297. # ifdef COMBO_ENABLE
  298. .keycode = tapping_key.keycode,
  299. # endif
  300. });
  301. } else {
  302. ac_dprintf("Tapping: Start while last tap(1).\n");
  303. }
  304. tapping_key = *keyp;
  305. waiting_buffer_scan_tap();
  306. debug_tapping_key();
  307. return true;
  308. } else {
  309. ac_dprintf("Tapping: key event while last tap(>0).\n");
  310. process_record(keyp);
  311. return true;
  312. }
  313. }
  314. }
  315. // after TAPPING_TERM
  316. else {
  317. if (tapping_key.tap.count == 0) {
  318. ac_dprintf("Tapping: End. Timeout. Not tap(0): ");
  319. debug_event(event);
  320. ac_dprintf("\n");
  321. process_record(&tapping_key);
  322. tapping_key = (keyrecord_t){0};
  323. debug_tapping_key();
  324. return false;
  325. } else {
  326. if (IS_NOEVENT(event)) {
  327. return true;
  328. }
  329. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  330. ac_dprintf("Tapping: End. last timeout tap release(>0).");
  331. keyp->tap = tapping_key.tap;
  332. process_record(keyp);
  333. tapping_key = (keyrecord_t){0};
  334. return true;
  335. } else if (is_tap_record(keyp) && event.pressed) {
  336. if (tapping_key.tap.count > 1) {
  337. ac_dprintf("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  338. // unregister key
  339. process_record(&(keyrecord_t){
  340. .tap = tapping_key.tap,
  341. .event.key = tapping_key.event.key,
  342. .event.time = event.time,
  343. .event.pressed = false,
  344. .event.type = tapping_key.event.type,
  345. # ifdef COMBO_ENABLE
  346. .keycode = tapping_key.keycode,
  347. # endif
  348. });
  349. } else {
  350. ac_dprintf("Tapping: Start while last timeout tap(1).\n");
  351. }
  352. tapping_key = *keyp;
  353. waiting_buffer_scan_tap();
  354. debug_tapping_key();
  355. return true;
  356. } else {
  357. ac_dprintf("Tapping: key event while last timeout tap(>0).\n");
  358. process_record(keyp);
  359. return true;
  360. }
  361. }
  362. }
  363. }
  364. // process "released" tapping key state
  365. else {
  366. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) {
  367. if (IS_NOEVENT(event)) {
  368. // early return for tick events
  369. return true;
  370. }
  371. if (event.pressed) {
  372. if (IS_TAPPING_RECORD(keyp)) {
  373. if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  374. // sequential tap.
  375. keyp->tap = tapping_key.tap;
  376. if (keyp->tap.count < 15) keyp->tap.count += 1;
  377. ac_dprintf("Tapping: Tap press(%u)\n", keyp->tap.count);
  378. process_record(keyp);
  379. tapping_key = *keyp;
  380. debug_tapping_key();
  381. return true;
  382. }
  383. // FIX: start new tap again
  384. tapping_key = *keyp;
  385. return true;
  386. } else if (is_tap_record(keyp)) {
  387. // Sequential tap can be interfered with other tap key.
  388. ac_dprintf("Tapping: Start with interfering other tap.\n");
  389. tapping_key = *keyp;
  390. waiting_buffer_scan_tap();
  391. debug_tapping_key();
  392. return true;
  393. } else {
  394. // should none in buffer
  395. // FIX: interrupted when other key is pressed
  396. tapping_key.tap.interrupted = true;
  397. process_record(keyp);
  398. return true;
  399. }
  400. } else {
  401. ac_dprintf("Tapping: other key just after tap.\n");
  402. process_record(keyp);
  403. return true;
  404. }
  405. } else {
  406. // Timeout - reset state machine.
  407. ac_dprintf("Tapping: End(Timeout after releasing last tap): ");
  408. debug_event(event);
  409. ac_dprintf("\n");
  410. tapping_key = (keyrecord_t){0};
  411. debug_tapping_key();
  412. return false;
  413. }
  414. }
  415. }
  416. /** \brief Waiting buffer enq
  417. *
  418. * FIXME: Needs docs
  419. */
  420. bool waiting_buffer_enq(keyrecord_t record) {
  421. if (IS_NOEVENT(record.event)) {
  422. return true;
  423. }
  424. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  425. ac_dprintf("waiting_buffer_enq: Over flow.\n");
  426. return false;
  427. }
  428. waiting_buffer[waiting_buffer_head] = record;
  429. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  430. ac_dprintf("waiting_buffer_enq: ");
  431. debug_waiting_buffer();
  432. return true;
  433. }
  434. /** \brief Waiting buffer clear
  435. *
  436. * FIXME: Needs docs
  437. */
  438. void waiting_buffer_clear(void) {
  439. waiting_buffer_head = 0;
  440. waiting_buffer_tail = 0;
  441. }
  442. /** \brief Waiting buffer typed
  443. *
  444. * FIXME: Needs docs
  445. */
  446. bool waiting_buffer_typed(keyevent_t event) {
  447. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  448. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  449. return true;
  450. }
  451. }
  452. return false;
  453. }
  454. /** \brief Waiting buffer has anykey pressed
  455. *
  456. * FIXME: Needs docs
  457. */
  458. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  459. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  460. if (waiting_buffer[i].event.pressed) return true;
  461. }
  462. return false;
  463. }
  464. /** \brief Scan buffer for tapping
  465. *
  466. * FIXME: Needs docs
  467. */
  468. void waiting_buffer_scan_tap(void) {
  469. // early return if:
  470. // - tapping already is settled
  471. // - invalid state: tapping_key released && tap.count == 0
  472. if ((tapping_key.tap.count > 0) || !tapping_key.event.pressed) {
  473. return;
  474. }
  475. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  476. keyrecord_t *candidate = &waiting_buffer[i];
  477. if (IS_EVENT(candidate->event) && KEYEQ(candidate->event.key, tapping_key.event.key) && !candidate->event.pressed && WITHIN_TAPPING_TERM(candidate->event)) {
  478. tapping_key.tap.count = 1;
  479. candidate->tap.count = 1;
  480. process_record(&tapping_key);
  481. ac_dprintf("waiting_buffer_scan_tap: found at [%u]\n", i);
  482. debug_waiting_buffer();
  483. return;
  484. }
  485. }
  486. }
  487. /** \brief Tapping key debug print
  488. *
  489. * FIXME: Needs docs
  490. */
  491. static void debug_tapping_key(void) {
  492. ac_dprintf("TAPPING_KEY=");
  493. debug_record(tapping_key);
  494. ac_dprintf("\n");
  495. }
  496. /** \brief Waiting buffer debug print
  497. *
  498. * FIXME: Needs docs
  499. */
  500. static void debug_waiting_buffer(void) {
  501. ac_dprintf("{ ");
  502. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  503. ac_dprintf("[%u]=", i);
  504. debug_record(waiting_buffer[i]);
  505. ac_dprintf(" ");
  506. }
  507. ac_dprintf("}\n");
  508. }
  509. #endif