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. # 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)) {
  151. if (!IS_EVENT(event)) {
  152. // early return for tick events
  153. } else if (event.pressed && is_tap_record(keyp)) {
  154. // the currently pressed key is a tapping key, therefore transition
  155. // into the "pressed" tapping key state
  156. ac_dprintf("Tapping: Start(Press tap key).\n");
  157. tapping_key = *keyp;
  158. process_record_tap_hint(&tapping_key);
  159. waiting_buffer_scan_tap();
  160. debug_tapping_key();
  161. } else {
  162. // the current key is just a regular key, pass it on for regular
  163. // processing
  164. process_record(keyp);
  165. }
  166. return true;
  167. }
  168. TAP_DEFINE_KEYCODE;
  169. // process "pressed" tapping key state
  170. if (tapping_key.event.pressed) {
  171. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) {
  172. if (IS_NOEVENT(event)) {
  173. // early return for tick events
  174. return true;
  175. }
  176. if (tapping_key.tap.count == 0) {
  177. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  178. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  179. retroshift_swap_times();
  180. # endif
  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. (
  198. !event.pressed && waiting_buffer_typed(event) &&
  199. TAP_GET_PERMISSIVE_HOLD
  200. )
  201. // Causes nested taps to not wait past TAPPING_TERM/RETRO_SHIFT
  202. // unnecessarily and fixes them for Layer Taps.
  203. || (TAP_GET_RETRO_TAPPING &&
  204. (
  205. // Rolled over the two keys.
  206. (tapping_key.tap.interrupted == true && (
  207. (TAP_IS_LT && TAP_GET_HOLD_ON_OTHER_KEY_PRESS) ||
  208. (TAP_IS_MT && TAP_GET_HOLD_ON_OTHER_KEY_PRESS)
  209. )
  210. )
  211. // Makes Retro Shift ignore the default behavior of
  212. // MTs and LTs on nested taps below TAPPING_TERM or RETRO_SHIFT
  213. || (
  214. TAP_IS_RETRO
  215. && (event.key.col != tapping_key.event.key.col || event.key.row != tapping_key.event.key.row)
  216. && !event.pressed && waiting_buffer_typed(event)
  217. )
  218. )
  219. )
  220. ) {
  221. // clang-format on
  222. ac_dprintf("Tapping: End. No tap. Interfered by typing key\n");
  223. process_record(&tapping_key);
  224. tapping_key = (keyrecord_t){0};
  225. debug_tapping_key();
  226. // enqueue
  227. return false;
  228. }
  229. /* Process release event of a key pressed before tapping starts
  230. * Without this unexpected repeating will occur with having fast repeating setting
  231. * https://github.com/tmk/tmk_keyboard/issues/60
  232. */
  233. else if (!event.pressed && !waiting_buffer_typed(event)) {
  234. // Modifier/Layer should be retained till end of this tapping.
  235. action_t action = layer_switch_get_action(event.key);
  236. switch (action.kind.id) {
  237. case ACT_LMODS:
  238. case ACT_RMODS:
  239. if (action.key.mods && !action.key.code) return false;
  240. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  241. break;
  242. case ACT_LMODS_TAP:
  243. case ACT_RMODS_TAP:
  244. if (action.key.mods && keyp->tap.count == 0) return false;
  245. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  246. break;
  247. case ACT_LAYER_TAP:
  248. case ACT_LAYER_TAP_EXT:
  249. switch (action.layer_tap.code) {
  250. case 0 ...(OP_TAP_TOGGLE - 1):
  251. case OP_ON_OFF:
  252. case OP_OFF_ON:
  253. case OP_SET_CLEAR:
  254. return false;
  255. }
  256. break;
  257. }
  258. // Release of key should be process immediately.
  259. ac_dprintf("Tapping: release event of a key pressed before tapping\n");
  260. process_record(keyp);
  261. return true;
  262. } else {
  263. // set interrupted flag when other key preesed during tapping
  264. if (event.pressed) {
  265. tapping_key.tap.interrupted = true;
  266. if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS) {
  267. ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n");
  268. process_record(&tapping_key);
  269. tapping_key = (keyrecord_t){0};
  270. debug_tapping_key();
  271. // enqueue
  272. return false;
  273. }
  274. }
  275. // enqueue
  276. return false;
  277. }
  278. }
  279. // tap_count > 0
  280. else {
  281. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  282. ac_dprintf("Tapping: Tap release(%u)\n", tapping_key.tap.count);
  283. keyp->tap = tapping_key.tap;
  284. process_record(keyp);
  285. tapping_key = *keyp;
  286. debug_tapping_key();
  287. return true;
  288. } else if (is_tap_record(keyp) && event.pressed) {
  289. if (tapping_key.tap.count > 1) {
  290. ac_dprintf("Tapping: Start new tap with releasing last tap(>1).\n");
  291. // unregister key
  292. process_record(&(keyrecord_t){
  293. .tap = tapping_key.tap,
  294. .event.key = tapping_key.event.key,
  295. .event.time = event.time,
  296. .event.pressed = false,
  297. .event.type = tapping_key.event.type,
  298. # ifdef COMBO_ENABLE
  299. .keycode = tapping_key.keycode,
  300. # endif
  301. });
  302. } else {
  303. ac_dprintf("Tapping: Start while last tap(1).\n");
  304. }
  305. tapping_key = *keyp;
  306. waiting_buffer_scan_tap();
  307. debug_tapping_key();
  308. return true;
  309. } else {
  310. ac_dprintf("Tapping: key event while last tap(>0).\n");
  311. process_record(keyp);
  312. return true;
  313. }
  314. }
  315. }
  316. // after TAPPING_TERM
  317. else {
  318. if (tapping_key.tap.count == 0) {
  319. ac_dprintf("Tapping: End. Timeout. Not tap(0): ");
  320. debug_event(event);
  321. ac_dprintf("\n");
  322. process_record(&tapping_key);
  323. tapping_key = (keyrecord_t){0};
  324. debug_tapping_key();
  325. return false;
  326. } else {
  327. if (IS_NOEVENT(event)) {
  328. return true;
  329. }
  330. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  331. ac_dprintf("Tapping: End. last timeout tap release(>0).");
  332. keyp->tap = tapping_key.tap;
  333. process_record(keyp);
  334. tapping_key = (keyrecord_t){0};
  335. return true;
  336. } else if (is_tap_record(keyp) && event.pressed) {
  337. if (tapping_key.tap.count > 1) {
  338. ac_dprintf("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  339. // unregister key
  340. process_record(&(keyrecord_t){
  341. .tap = tapping_key.tap,
  342. .event.key = tapping_key.event.key,
  343. .event.time = event.time,
  344. .event.pressed = false,
  345. .event.type = tapping_key.event.type,
  346. # ifdef COMBO_ENABLE
  347. .keycode = tapping_key.keycode,
  348. # endif
  349. });
  350. } else {
  351. ac_dprintf("Tapping: Start while last timeout tap(1).\n");
  352. }
  353. tapping_key = *keyp;
  354. waiting_buffer_scan_tap();
  355. debug_tapping_key();
  356. return true;
  357. } else {
  358. ac_dprintf("Tapping: key event while last timeout tap(>0).\n");
  359. process_record(keyp);
  360. return true;
  361. }
  362. }
  363. }
  364. }
  365. // process "released" tapping key state
  366. else {
  367. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event)) {
  368. if (IS_NOEVENT(event)) {
  369. // early return for tick events
  370. return true;
  371. }
  372. if (event.pressed) {
  373. if (IS_TAPPING_RECORD(keyp)) {
  374. if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  375. // sequential tap.
  376. keyp->tap = tapping_key.tap;
  377. if (keyp->tap.count < 15) keyp->tap.count += 1;
  378. ac_dprintf("Tapping: Tap press(%u)\n", keyp->tap.count);
  379. process_record(keyp);
  380. tapping_key = *keyp;
  381. debug_tapping_key();
  382. return true;
  383. }
  384. // FIX: start new tap again
  385. tapping_key = *keyp;
  386. return true;
  387. } else if (is_tap_record(keyp)) {
  388. // Sequential tap can be interfered with other tap key.
  389. ac_dprintf("Tapping: Start with interfering other tap.\n");
  390. tapping_key = *keyp;
  391. waiting_buffer_scan_tap();
  392. debug_tapping_key();
  393. return true;
  394. } else {
  395. // should none in buffer
  396. // FIX: interrupted when other key is pressed
  397. tapping_key.tap.interrupted = true;
  398. process_record(keyp);
  399. return true;
  400. }
  401. } else {
  402. ac_dprintf("Tapping: other key just after tap.\n");
  403. process_record(keyp);
  404. return true;
  405. }
  406. } else {
  407. // Timeout - reset state machine.
  408. ac_dprintf("Tapping: End(Timeout after releasing last tap): ");
  409. debug_event(event);
  410. ac_dprintf("\n");
  411. tapping_key = (keyrecord_t){0};
  412. debug_tapping_key();
  413. return false;
  414. }
  415. }
  416. }
  417. /** \brief Waiting buffer enq
  418. *
  419. * FIXME: Needs docs
  420. */
  421. bool waiting_buffer_enq(keyrecord_t record) {
  422. if (IS_NOEVENT(record.event)) {
  423. return true;
  424. }
  425. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  426. ac_dprintf("waiting_buffer_enq: Over flow.\n");
  427. return false;
  428. }
  429. waiting_buffer[waiting_buffer_head] = record;
  430. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  431. ac_dprintf("waiting_buffer_enq: ");
  432. debug_waiting_buffer();
  433. return true;
  434. }
  435. /** \brief Waiting buffer clear
  436. *
  437. * FIXME: Needs docs
  438. */
  439. void waiting_buffer_clear(void) {
  440. waiting_buffer_head = 0;
  441. waiting_buffer_tail = 0;
  442. }
  443. /** \brief Waiting buffer typed
  444. *
  445. * FIXME: Needs docs
  446. */
  447. bool waiting_buffer_typed(keyevent_t event) {
  448. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  449. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  450. return true;
  451. }
  452. }
  453. return false;
  454. }
  455. /** \brief Waiting buffer has anykey pressed
  456. *
  457. * FIXME: Needs docs
  458. */
  459. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  460. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  461. if (waiting_buffer[i].event.pressed) return true;
  462. }
  463. return false;
  464. }
  465. /** \brief Scan buffer for tapping
  466. *
  467. * FIXME: Needs docs
  468. */
  469. void waiting_buffer_scan_tap(void) {
  470. // early return if:
  471. // - tapping already is settled
  472. // - invalid state: tapping_key released && tap.count == 0
  473. if ((tapping_key.tap.count > 0) || !tapping_key.event.pressed) {
  474. return;
  475. }
  476. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  477. keyrecord_t *candidate = &waiting_buffer[i];
  478. if (IS_EVENT(candidate->event) && KEYEQ(candidate->event.key, tapping_key.event.key) && !candidate->event.pressed && WITHIN_TAPPING_TERM(candidate->event)) {
  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