action_tapping.c 21 KB

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