action_tapping.c 20 KB

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