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