action_tapping.c 21 KB

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