action_tapping.c 21 KB

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