action_tapping.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "action.h"
  4. #include "action_layer.h"
  5. #include "action_tapping.h"
  6. #include "action_util.h"
  7. #include "keycode.h"
  8. #include "keycode_config.h"
  9. #include "quantum_keycodes.h"
  10. #include "timer.h"
  11. #include "wait.h"
  12. #ifndef NO_ACTION_TAPPING
  13. # if defined(IGNORE_MOD_TAP_INTERRUPT_PER_KEY)
  14. # 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."
  15. # elif defined(IGNORE_MOD_TAP_INTERRUPT)
  16. # 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."
  17. # endif
  18. # ifndef COMBO_ENABLE
  19. # define IS_TAPPING_RECORD(r) (KEYEQ(tapping_key.event.key, (r->event.key)))
  20. # else
  21. # define IS_TAPPING_RECORD(r) (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 SPECULATIVE_HOLD
  48. typedef struct {
  49. keypos_t key;
  50. uint8_t mods;
  51. } speculative_key_t;
  52. # define SPECULATIVE_KEYS_SIZE 8
  53. static speculative_key_t speculative_keys[SPECULATIVE_KEYS_SIZE] = {};
  54. static uint8_t num_speculative_keys = 0;
  55. static uint8_t prev_speculative_mods = 0;
  56. static uint8_t speculative_mods = 0;
  57. /** Handler to be called on incoming press events. */
  58. static void speculative_key_press(keyrecord_t *record);
  59. # endif // SPECULATIVE_HOLD
  60. # if defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM)
  61. # define REGISTERED_TAPS_SIZE 8
  62. // Array of tap-hold keys that have been settled as tapped but not yet released.
  63. static keypos_t registered_taps[REGISTERED_TAPS_SIZE] = {};
  64. static uint8_t num_registered_taps = 0;
  65. /** Adds `key` to the registered_taps array. */
  66. static void registered_taps_add(keypos_t key);
  67. /** Returns the index of `key` in registered_taps, or -1 if not found. */
  68. static int8_t registered_tap_find(keypos_t key);
  69. /** Removes index `i` from the registered_taps array. */
  70. static void registered_taps_del_index(uint8_t i);
  71. /** Logs the registered_taps array for debugging. */
  72. static void debug_registered_taps(void);
  73. static bool is_mt_or_lt(uint16_t keycode) {
  74. return IS_QK_MOD_TAP(keycode) || IS_QK_LAYER_TAP(keycode);
  75. }
  76. # endif // defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM)
  77. # if defined(CHORDAL_HOLD)
  78. extern const char chordal_hold_layout[MATRIX_ROWS][MATRIX_COLS] PROGMEM;
  79. /** \brief Finds which queued events should be held according to Chordal Hold.
  80. *
  81. * In a situation with multiple unsettled tap-hold key presses, scan the queue
  82. * up until the first release, non-tap-hold, or one-shot event and find the
  83. * latest event in the queue that settles as held according to
  84. * get_chordal_hold().
  85. *
  86. * \return Index of the first tap, or equivalently, one past the latest hold.
  87. */
  88. static uint8_t waiting_buffer_find_chordal_hold_tap(void);
  89. /** Processes queued events up to and including `key` as tapped. */
  90. static void waiting_buffer_chordal_hold_taps_until(keypos_t key);
  91. /** \brief Processes and pops buffered events until the first tap-hold event. */
  92. static void waiting_buffer_process_regular(void);
  93. # endif // CHORDAL_HOLD
  94. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  95. __attribute__((weak)) bool get_hold_on_other_key_press(uint16_t keycode, keyrecord_t *record) {
  96. return false;
  97. }
  98. # endif
  99. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  100. # include "process_auto_shift.h"
  101. # endif
  102. # if defined(FLOW_TAP_TERM) || defined(SPECULATIVE_HOLD_FLOW_TERM)
  103. static uint16_t flow_tap_prev_keycode = KC_NO;
  104. static uint16_t flow_tap_prev_time = 0;
  105. static bool flow_tap_expired = true;
  106. # endif // defined(FLOW_TAP_TERM) || defined(SPECULATIVE_HOLD_FLOW_TERM)
  107. # if defined(FLOW_TAP_TERM)
  108. static bool flow_tap_key_if_within_term(keyrecord_t *record, uint16_t prev_time);
  109. # endif // defined(FLOW_TAP_TERM)
  110. static keyrecord_t tapping_key = {};
  111. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  112. static uint8_t waiting_buffer_head = 0;
  113. static uint8_t waiting_buffer_tail = 0;
  114. static bool process_tapping(keyrecord_t *record);
  115. static bool waiting_buffer_enq(keyrecord_t record);
  116. static void waiting_buffer_clear(void);
  117. static bool waiting_buffer_typed(keyevent_t event);
  118. static bool waiting_buffer_has_anykey_pressed(void);
  119. static void waiting_buffer_scan_tap(void);
  120. static void debug_tapping_key(void);
  121. static void debug_waiting_buffer(void);
  122. /** \brief Action Tapping Process
  123. *
  124. * FIXME: Needs doc
  125. */
  126. void action_tapping_process(keyrecord_t record) {
  127. # ifdef SPECULATIVE_HOLD
  128. prev_speculative_mods = speculative_mods;
  129. if (record.event.pressed) {
  130. speculative_key_press(&record);
  131. }
  132. # endif // SPECULATIVE_HOLD
  133. if (process_tapping(&record)) {
  134. if (IS_EVENT(record.event)) {
  135. ac_dprintf("processed: ");
  136. debug_record(record);
  137. ac_dprintf("\n");
  138. }
  139. } else {
  140. if (!waiting_buffer_enq(record)) {
  141. // clear all in case of overflow.
  142. ac_dprintf("OVERFLOW: CLEAR ALL STATES\n");
  143. clear_keyboard();
  144. waiting_buffer_clear();
  145. tapping_key = (keyrecord_t){0};
  146. }
  147. }
  148. # ifdef SPECULATIVE_HOLD
  149. if (speculative_mods != prev_speculative_mods) {
  150. send_keyboard_report();
  151. }
  152. # endif // SPECULATIVE_HOLD
  153. // process waiting_buffer
  154. if (IS_EVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
  155. ac_dprintf("---- action_exec: process waiting_buffer -----\n");
  156. }
  157. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  158. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  159. ac_dprintf("processed: waiting_buffer[%u] =", waiting_buffer_tail);
  160. debug_record(waiting_buffer[waiting_buffer_tail]);
  161. ac_dprintf("\n\n");
  162. } else {
  163. break;
  164. }
  165. }
  166. if (IS_EVENT(record.event)) {
  167. ac_dprintf("\n");
  168. } else {
  169. # if defined(FLOW_TAP_TERM) || defined(SPECULATIVE_HOLD_FLOW_TERM)
  170. if (!flow_tap_expired && TIMER_DIFF_16(record.event.time, flow_tap_prev_time) >= INT16_MAX / 2) {
  171. flow_tap_expired = true;
  172. }
  173. # endif // defined(FLOW_TAP_TERM) || defined(SPECULATIVE_HOLD_FLOW_TERM)
  174. }
  175. }
  176. /* Some conditionally defined helper macros to keep process_tapping more
  177. * readable. The conditional definition of tapping_keycode and all the
  178. * conditional uses of it are hidden inside macros named TAP_...
  179. */
  180. # define TAP_DEFINE_KEYCODE const uint16_t tapping_keycode = get_record_keycode(&tapping_key, false)
  181. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  182. # ifdef RETRO_TAPPING_PER_KEY
  183. # define TAP_GET_RETRO_TAPPING(keyp) get_auto_shifted_key(tapping_keycode, keyp) && get_retro_tapping(tapping_keycode, &tapping_key)
  184. # else
  185. # define TAP_GET_RETRO_TAPPING(keyp) get_auto_shifted_key(tapping_keycode, keyp)
  186. # endif
  187. /* Used to extend TAPPING_TERM:
  188. * indefinitely if RETRO_SHIFT does not have a value
  189. * to RETRO_SHIFT if RETRO_SHIFT is set
  190. * for possibly retro shifted keys.
  191. */
  192. # define MAYBE_RETRO_SHIFTING(ev, keyp) (get_auto_shifted_key(tapping_keycode, keyp) && TAP_GET_RETRO_TAPPING(keyp) && ((RETRO_SHIFT + 0) == 0 || TIMER_DIFF_16((ev).time, tapping_key.event.time) < (RETRO_SHIFT + 0)))
  193. # define TAP_IS_LT IS_QK_LAYER_TAP(tapping_keycode)
  194. # define TAP_IS_MT IS_QK_MOD_TAP(tapping_keycode)
  195. # define TAP_IS_RETRO IS_RETRO(tapping_keycode)
  196. # else
  197. # define TAP_GET_RETRO_TAPPING(keyp) false
  198. # define MAYBE_RETRO_SHIFTING(ev, kp) false
  199. # define TAP_IS_LT false
  200. # define TAP_IS_MT false
  201. # define TAP_IS_RETRO false
  202. # endif
  203. # ifdef PERMISSIVE_HOLD_PER_KEY
  204. # define TAP_GET_PERMISSIVE_HOLD get_permissive_hold(tapping_keycode, &tapping_key)
  205. # elif defined(PERMISSIVE_HOLD)
  206. # define TAP_GET_PERMISSIVE_HOLD true
  207. # else
  208. # define TAP_GET_PERMISSIVE_HOLD false
  209. # endif
  210. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  211. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS get_hold_on_other_key_press(tapping_keycode, &tapping_key)
  212. # elif defined(HOLD_ON_OTHER_KEY_PRESS)
  213. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS true
  214. # else
  215. # define TAP_GET_HOLD_ON_OTHER_KEY_PRESS false
  216. # endif
  217. /** \brief Tapping
  218. *
  219. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  220. * (without interfering by typing other key)
  221. */
  222. /* return true when key event is processed or consumed. */
  223. bool process_tapping(keyrecord_t *keyp) {
  224. const keyevent_t event = keyp->event;
  225. # if defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM)
  226. if (!event.pressed) {
  227. const int8_t i = registered_tap_find(event.key);
  228. if (i != -1) {
  229. // If a tap-hold key was previously settled as tapped, set its
  230. // tap.count correspondingly on release.
  231. keyp->tap.count = 1;
  232. registered_taps_del_index(i);
  233. ac_dprintf("Found tap release for [%d]\n", i);
  234. debug_registered_taps();
  235. }
  236. }
  237. # endif // defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM)
  238. // state machine is in the "reset" state, no tapping key is to be
  239. // processed
  240. if (IS_NOEVENT(tapping_key.event)) {
  241. if (!IS_EVENT(event)) {
  242. // early return for tick events
  243. } else if (event.pressed && is_tap_record(keyp)) {
  244. // the currently pressed key is a tapping key, therefore transition
  245. // into the "pressed" tapping key state
  246. # if defined(FLOW_TAP_TERM)
  247. if (flow_tap_key_if_within_term(keyp, flow_tap_prev_time)) {
  248. return true;
  249. }
  250. # endif // defined(FLOW_TAP_TERM)
  251. ac_dprintf("Tapping: Start(Press tap key).\n");
  252. tapping_key = *keyp;
  253. process_record_tap_hint(&tapping_key);
  254. waiting_buffer_scan_tap();
  255. debug_tapping_key();
  256. } else {
  257. // the current key is just a regular key, pass it on for regular
  258. // processing
  259. process_record(keyp);
  260. }
  261. return true;
  262. }
  263. # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) || defined(PERMISSIVE_HOLD_PER_KEY) || defined(CHORDAL_HOLD) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
  264. TAP_DEFINE_KEYCODE;
  265. # endif
  266. // process "pressed" tapping key state
  267. if (tapping_key.event.pressed) {
  268. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event, keyp)) {
  269. if (IS_NOEVENT(event)) {
  270. // early return for tick events
  271. return true;
  272. }
  273. if (tapping_key.tap.count == 0) {
  274. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  275. // first tap!
  276. ac_dprintf("Tapping: First tap(0->1).\n");
  277. tapping_key.tap.count = 1;
  278. debug_tapping_key();
  279. process_record(&tapping_key);
  280. // copy tapping state
  281. keyp->tap = tapping_key.tap;
  282. # if defined(FLOW_TAP_TERM)
  283. // Now that tapping_key has settled as tapped, check whether
  284. // Flow Tap applies to following yet-unsettled keys.
  285. uint16_t prev_time = tapping_key.event.time;
  286. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  287. keyrecord_t *record = &waiting_buffer[waiting_buffer_tail];
  288. if (!record->event.pressed) {
  289. break;
  290. }
  291. const int16_t next_time = record->event.time;
  292. if (!is_tap_record(record)) {
  293. process_record(record);
  294. } else if (!flow_tap_key_if_within_term(record, prev_time)) {
  295. break;
  296. }
  297. prev_time = next_time;
  298. }
  299. debug_waiting_buffer();
  300. # endif // defined(FLOW_TAP_TERM)
  301. // enqueue
  302. return false;
  303. }
  304. # if defined(CHORDAL_HOLD)
  305. else if (is_mt_or_lt(tapping_keycode) && !event.pressed && waiting_buffer_typed(event) && !get_chordal_hold(tapping_keycode, &tapping_key, get_record_keycode(keyp, false), keyp)) {
  306. // Key release that is not a chord with the tapping key.
  307. // Settle the tapping key and any other pending tap-hold
  308. // keys preceding the press of this key as tapped.
  309. ac_dprintf("Tapping: End. Chord considered a tap\n");
  310. tapping_key.tap.count = 1;
  311. registered_taps_add(tapping_key.event.key);
  312. process_record(&tapping_key);
  313. tapping_key = (keyrecord_t){0};
  314. waiting_buffer_chordal_hold_taps_until(event.key);
  315. debug_registered_taps();
  316. debug_waiting_buffer();
  317. // enqueue
  318. return false;
  319. }
  320. # endif // CHORDAL_HOLD
  321. /* Process a key typed within TAPPING_TERM
  322. * This can register the key before settlement of tapping,
  323. * useful for long TAPPING_TERM but may prevent fast typing.
  324. */
  325. // clang-format off
  326. else if (
  327. !event.pressed && waiting_buffer_typed(event) &&
  328. (
  329. TAP_GET_PERMISSIVE_HOLD ||
  330. // Causes nested taps to not wait past TAPPING_TERM/RETRO_SHIFT
  331. // unnecessarily and fixes them for Layer Taps.
  332. TAP_GET_RETRO_TAPPING(keyp)
  333. )
  334. ) {
  335. // clang-format on
  336. ac_dprintf("Tapping: End. No tap. Interfered by typing key\n");
  337. process_record(&tapping_key);
  338. # if defined(CHORDAL_HOLD)
  339. uint8_t first_tap = waiting_buffer_find_chordal_hold_tap();
  340. ac_dprintf("first_tap = %u\n", first_tap);
  341. if (first_tap < WAITING_BUFFER_SIZE) {
  342. for (; waiting_buffer_tail != first_tap; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  343. ac_dprintf("Processing [%u]\n", waiting_buffer_tail);
  344. process_record(&waiting_buffer[waiting_buffer_tail]);
  345. }
  346. }
  347. waiting_buffer_chordal_hold_taps_until(event.key);
  348. debug_registered_taps();
  349. debug_waiting_buffer();
  350. # endif // CHORDAL_HOLD
  351. tapping_key = (keyrecord_t){0};
  352. debug_tapping_key();
  353. // enqueue
  354. return false;
  355. }
  356. /* Process release event of a key pressed before tapping starts
  357. * Without this unexpected repeating will occur with having fast repeating setting
  358. * https://github.com/tmk/tmk_keyboard/issues/60
  359. *
  360. * NOTE: This workaround causes events to process out of order,
  361. * e.g. in a rolled press of three tap-hold keys like
  362. *
  363. * "A down, B down, C down, A up, B up, C up"
  364. *
  365. * events are processed as
  366. *
  367. * "A down, B down, A up, B up, C down, C up"
  368. *
  369. * It seems incorrect to process keyp before the tapping key.
  370. * This workaround is old, from 2013. This might no longer
  371. * be needed for the original problem it was meant to address.
  372. */
  373. else if (!event.pressed && !waiting_buffer_typed(event)) {
  374. // Modifier/Layer should be retained till end of this tapping.
  375. action_t action = layer_switch_get_action(event.key);
  376. switch (action.kind.id) {
  377. case ACT_LMODS:
  378. case ACT_RMODS:
  379. if (action.key.mods && !action.key.code) return false;
  380. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  381. break;
  382. case ACT_LMODS_TAP:
  383. case ACT_RMODS_TAP:
  384. if (action.key.mods && keyp->tap.count == 0) return false;
  385. if (IS_MODIFIER_KEYCODE(action.key.code)) return false;
  386. break;
  387. case ACT_LAYER_TAP:
  388. case ACT_LAYER_TAP_EXT:
  389. switch (action.layer_tap.code) {
  390. case 0 ...(OP_TAP_TOGGLE - 1):
  391. case OP_ON_OFF:
  392. case OP_OFF_ON:
  393. case OP_SET_CLEAR:
  394. return false;
  395. }
  396. break;
  397. }
  398. // Release of key should be process immediately.
  399. ac_dprintf("Tapping: release event of a key pressed before tapping\n");
  400. process_record(keyp);
  401. return true;
  402. } else {
  403. // set interrupted flag when other key pressed during tapping
  404. if (event.pressed) {
  405. tapping_key.tap.interrupted = true;
  406. # if defined(CHORDAL_HOLD)
  407. if (is_mt_or_lt(tapping_keycode) && !get_chordal_hold(tapping_keycode, &tapping_key, get_record_keycode(keyp, false), keyp)) {
  408. // In process_action(), HOLD_ON_OTHER_KEY_PRESS
  409. // will revert interrupted events to holds, so
  410. // this needs to be set false.
  411. tapping_key.tap.interrupted = false;
  412. if (!is_tap_record(keyp)) {
  413. ac_dprintf("Tapping: End. Chord considered a tap\n");
  414. tapping_key.tap.count = 1;
  415. registered_taps_add(tapping_key.event.key);
  416. debug_registered_taps();
  417. process_record(&tapping_key);
  418. tapping_key = (keyrecord_t){0};
  419. }
  420. } else
  421. # endif // CHORDAL_HOLD
  422. if (TAP_GET_HOLD_ON_OTHER_KEY_PRESS
  423. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  424. // Auto Shift cannot evaluate this early
  425. // Retro Shift uses the hold action for all nested taps even without HOLD_ON_OTHER_KEY_PRESS, so this is fine to skip
  426. && !(MAYBE_RETRO_SHIFTING(event, keyp) && get_auto_shifted_key(get_record_keycode(keyp, false), keyp))
  427. # endif
  428. ) {
  429. // Settle the tapping key as *held*, since
  430. // HOLD_ON_OTHER_KEY_PRESS is enabled for this key.
  431. ac_dprintf("Tapping: End. No tap. Interfered by pressed key\n");
  432. process_record(&tapping_key);
  433. # if defined(CHORDAL_HOLD)
  434. if (waiting_buffer_tail != waiting_buffer_head && is_tap_record(&waiting_buffer[waiting_buffer_tail])) {
  435. tapping_key = waiting_buffer[waiting_buffer_tail];
  436. // Pop tail from the queue.
  437. waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE;
  438. debug_waiting_buffer();
  439. } else
  440. # endif // CHORDAL_HOLD
  441. {
  442. tapping_key = (keyrecord_t){0};
  443. }
  444. debug_tapping_key();
  445. # if defined(CHORDAL_HOLD)
  446. waiting_buffer_process_regular();
  447. # endif // CHORDAL_HOLD
  448. }
  449. }
  450. // enqueue
  451. return false;
  452. }
  453. }
  454. // tap_count > 0
  455. else {
  456. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  457. ac_dprintf("Tapping: Tap release(%u)\n", tapping_key.tap.count);
  458. keyp->tap = tapping_key.tap;
  459. process_record(keyp);
  460. tapping_key = *keyp;
  461. debug_tapping_key();
  462. return true;
  463. } else if (is_tap_record(keyp) && event.pressed) {
  464. if (tapping_key.tap.count > 1) {
  465. ac_dprintf("Tapping: Start new tap with releasing last tap(>1).\n");
  466. // unregister key
  467. process_record(&(keyrecord_t){
  468. .tap = tapping_key.tap,
  469. .event.key = tapping_key.event.key,
  470. .event.time = event.time,
  471. .event.pressed = false,
  472. .event.type = tapping_key.event.type,
  473. # ifdef COMBO_ENABLE
  474. .keycode = tapping_key.keycode,
  475. # endif
  476. });
  477. } else {
  478. ac_dprintf("Tapping: Start while last tap(1).\n");
  479. }
  480. tapping_key = *keyp;
  481. waiting_buffer_scan_tap();
  482. debug_tapping_key();
  483. return true;
  484. } else {
  485. ac_dprintf("Tapping: key event while last tap(>0).\n");
  486. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  487. retroshift_swap_times();
  488. # endif
  489. process_record(keyp);
  490. return true;
  491. }
  492. }
  493. }
  494. // after TAPPING_TERM
  495. else {
  496. if (tapping_key.tap.count == 0) {
  497. ac_dprintf("Tapping: End. Timeout. Not tap(0): ");
  498. debug_event(event);
  499. ac_dprintf("\n");
  500. process_record(&tapping_key);
  501. tapping_key = (keyrecord_t){0};
  502. debug_tapping_key();
  503. return false;
  504. } else {
  505. if (IS_NOEVENT(event)) {
  506. return true;
  507. }
  508. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  509. ac_dprintf("Tapping: End. last timeout tap release(>0).");
  510. keyp->tap = tapping_key.tap;
  511. process_record(keyp);
  512. tapping_key = (keyrecord_t){0};
  513. return true;
  514. } else if (is_tap_record(keyp) && event.pressed) {
  515. if (tapping_key.tap.count > 1) {
  516. ac_dprintf("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  517. // unregister key
  518. process_record(&(keyrecord_t){
  519. .tap = tapping_key.tap,
  520. .event.key = tapping_key.event.key,
  521. .event.time = event.time,
  522. .event.pressed = false,
  523. .event.type = tapping_key.event.type,
  524. # ifdef COMBO_ENABLE
  525. .keycode = tapping_key.keycode,
  526. # endif
  527. });
  528. } else {
  529. ac_dprintf("Tapping: Start while last timeout tap(1).\n");
  530. }
  531. tapping_key = *keyp;
  532. waiting_buffer_scan_tap();
  533. debug_tapping_key();
  534. return true;
  535. } else {
  536. ac_dprintf("Tapping: key event while last timeout tap(>0).\n");
  537. process_record(keyp);
  538. return true;
  539. }
  540. }
  541. }
  542. }
  543. // process "released" tapping key state
  544. else {
  545. if (WITHIN_TAPPING_TERM(event) || MAYBE_RETRO_SHIFTING(event, keyp)) {
  546. if (IS_NOEVENT(event)) {
  547. // early return for tick events
  548. return true;
  549. }
  550. if (event.pressed) {
  551. if (IS_TAPPING_RECORD(keyp)) {
  552. if (WITHIN_QUICK_TAP_TERM(event) && !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  553. // sequential tap.
  554. keyp->tap = tapping_key.tap;
  555. if (keyp->tap.count < 15) keyp->tap.count += 1;
  556. ac_dprintf("Tapping: Tap press(%u)\n", keyp->tap.count);
  557. process_record(keyp);
  558. tapping_key = *keyp;
  559. debug_tapping_key();
  560. return true;
  561. }
  562. // FIX: start new tap again
  563. tapping_key = *keyp;
  564. return true;
  565. } else if (is_tap_record(keyp)) {
  566. // Sequential tap can be interfered with other tap key.
  567. # if defined(FLOW_TAP_TERM)
  568. if (flow_tap_key_if_within_term(keyp, flow_tap_prev_time)) {
  569. tapping_key = (keyrecord_t){0};
  570. debug_tapping_key();
  571. return true;
  572. }
  573. # endif // defined(FLOW_TAP_TERM)
  574. ac_dprintf("Tapping: Start with interfering other tap.\n");
  575. tapping_key = *keyp;
  576. waiting_buffer_scan_tap();
  577. debug_tapping_key();
  578. return true;
  579. } else {
  580. // should none in buffer
  581. // FIX: interrupted when other key is pressed
  582. tapping_key.tap.interrupted = true;
  583. process_record(keyp);
  584. return true;
  585. }
  586. } else {
  587. ac_dprintf("Tapping: other key just after tap.\n");
  588. process_record(keyp);
  589. return true;
  590. }
  591. } else {
  592. // Timeout - reset state machine.
  593. ac_dprintf("Tapping: End(Timeout after releasing last tap): ");
  594. debug_event(event);
  595. ac_dprintf("\n");
  596. tapping_key = (keyrecord_t){0};
  597. debug_tapping_key();
  598. return false;
  599. }
  600. }
  601. }
  602. /** \brief Waiting buffer enq
  603. *
  604. * FIXME: Needs docs
  605. */
  606. bool waiting_buffer_enq(keyrecord_t record) {
  607. if (IS_NOEVENT(record.event)) {
  608. return true;
  609. }
  610. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  611. ac_dprintf("waiting_buffer_enq: Over flow.\n");
  612. return false;
  613. }
  614. waiting_buffer[waiting_buffer_head] = record;
  615. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  616. ac_dprintf("waiting_buffer_enq: ");
  617. debug_waiting_buffer();
  618. return true;
  619. }
  620. /** \brief Waiting buffer clear
  621. *
  622. * FIXME: Needs docs
  623. */
  624. void waiting_buffer_clear(void) {
  625. waiting_buffer_head = 0;
  626. waiting_buffer_tail = 0;
  627. }
  628. /** \brief Waiting buffer typed
  629. *
  630. * FIXME: Needs docs
  631. */
  632. bool waiting_buffer_typed(keyevent_t event) {
  633. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  634. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  635. return true;
  636. }
  637. }
  638. return false;
  639. }
  640. /** \brief Waiting buffer has anykey pressed
  641. *
  642. * FIXME: Needs docs
  643. */
  644. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  645. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  646. if (waiting_buffer[i].event.pressed) return true;
  647. }
  648. return false;
  649. }
  650. /** \brief Scan buffer for tapping
  651. *
  652. * FIXME: Needs docs
  653. */
  654. void waiting_buffer_scan_tap(void) {
  655. // early return if:
  656. // - tapping already is settled
  657. // - invalid state: tapping_key released && tap.count == 0
  658. if ((tapping_key.tap.count > 0) || !tapping_key.event.pressed) {
  659. return;
  660. }
  661. # if (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT))
  662. TAP_DEFINE_KEYCODE;
  663. # endif
  664. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  665. keyrecord_t *candidate = &waiting_buffer[i];
  666. // clang-format off
  667. if (IS_EVENT(candidate->event) && KEYEQ(candidate->event.key, tapping_key.event.key) && !candidate->event.pressed && (
  668. WITHIN_TAPPING_TERM(waiting_buffer[i].event) || MAYBE_RETRO_SHIFTING(waiting_buffer[i].event, &tapping_key)
  669. )) {
  670. // clang-format on
  671. tapping_key.tap.count = 1;
  672. candidate->tap.count = 1;
  673. process_record(&tapping_key);
  674. ac_dprintf("waiting_buffer_scan_tap: found at [%u]\n", i);
  675. debug_waiting_buffer();
  676. return;
  677. }
  678. }
  679. }
  680. # ifdef SPECULATIVE_HOLD
  681. static void debug_speculative_keys(void) {
  682. ac_dprintf("mods = { ");
  683. for (int8_t i = 0; i < num_speculative_keys; ++i) {
  684. ac_dprintf("%02X ", speculative_keys[i].mods);
  685. }
  686. ac_dprintf("}, keys = { ");
  687. for (int8_t i = 0; i < num_speculative_keys; ++i) {
  688. ac_dprintf("%02X%02X ", speculative_keys[i].key.row, speculative_keys[i].key.col);
  689. }
  690. ac_dprintf("}\n");
  691. }
  692. // Find key in speculative_keys. Returns num_speculative_keys if not found.
  693. static int8_t speculative_keys_find(keypos_t key) {
  694. uint8_t i;
  695. for (i = 0; i < num_speculative_keys; ++i) {
  696. if (KEYEQ(speculative_keys[i].key, key)) {
  697. break;
  698. }
  699. }
  700. return i;
  701. }
  702. static void speculative_key_press(keyrecord_t *record) {
  703. if (num_speculative_keys >= SPECULATIVE_KEYS_SIZE) { // Overflow!
  704. ac_dprintf("SPECULATIVE KEYS OVERFLOW: IGNORING EVENT\n");
  705. return; // Don't trigger: speculative_keys is full.
  706. }
  707. if (speculative_keys_find(record->event.key) < num_speculative_keys) {
  708. return; // Don't trigger: key is already in speculative_keys.
  709. }
  710. # ifdef SPECULATIVE_HOLD_FLOW_TERM
  711. if (!flow_tap_expired && TIMER_DIFF_16(record->event.time, flow_tap_prev_time) <= SPECULATIVE_HOLD_FLOW_TERM) {
  712. return; // Don't trigger: within flow term of previous key.
  713. }
  714. # endif // SPECULATIVE_HOLD_FLOW_TERM
  715. const uint16_t keycode = get_record_keycode(record, false);
  716. if (!IS_QK_MOD_TAP(keycode)) {
  717. return; // Don't trigger: not a mod-tap key.
  718. }
  719. uint8_t mods = mod_config(QK_MOD_TAP_GET_MODS(keycode));
  720. const uint8_t active_mods = get_mods() | speculative_mods;
  721. # ifdef SPECULATIVE_HOLD_ONE_KEY
  722. if (active_mods != 0) {
  723. return; // Don't trigger: some mod is already active.
  724. }
  725. # endif // SPECULATIVE_HOLD_ONE_KEY
  726. if ((mods & 0x10) != 0) { // Unpack 5-bit mods to 8-bit representation.
  727. mods <<= 4;
  728. }
  729. if ((~active_mods & mods) == 0) {
  730. return; // Don't trigger: mods are already active.
  731. }
  732. // Don't do Speculative Hold when there are non-speculated buffered events,
  733. // since that could result in sending keys out of order.
  734. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  735. if (!waiting_buffer[i].tap.speculated) {
  736. return;
  737. }
  738. }
  739. if (get_speculative_hold(keycode, record)) {
  740. record->tap.speculated = true;
  741. speculative_mods |= mods;
  742. // Remember the keypos and mods associated with this key.
  743. speculative_keys[num_speculative_keys] = (speculative_key_t){
  744. .key = record->event.key,
  745. .mods = mods,
  746. };
  747. ++num_speculative_keys;
  748. ac_dprintf("Speculative Hold: ");
  749. debug_speculative_keys();
  750. }
  751. }
  752. uint8_t get_speculative_mods(void) {
  753. return speculative_mods;
  754. }
  755. __attribute__((weak)) bool get_speculative_hold(uint16_t keycode, keyrecord_t *record) {
  756. const uint8_t mods = mod_config(QK_MOD_TAP_GET_MODS(keycode));
  757. return (mods & (MOD_LCTL | MOD_LSFT)) == (mods & (MOD_HYPR));
  758. }
  759. void speculative_key_settled(keyrecord_t *record) {
  760. if (num_speculative_keys == 0) {
  761. return; // Early return when there are no active speculative keys.
  762. }
  763. uint8_t i = speculative_keys_find(record->event.key);
  764. const uint16_t keycode = get_record_keycode(record, false);
  765. if (IS_QK_MOD_TAP(keycode) && record->tap.count == 0) { // MT hold press.
  766. if (i < num_speculative_keys) {
  767. --num_speculative_keys;
  768. const uint8_t cleared_mods = speculative_keys[i].mods;
  769. if (num_speculative_keys) {
  770. speculative_mods &= ~cleared_mods;
  771. // Don't call send_keyboard_report() here; allow default
  772. // handling to reapply the mod before the next report.
  773. // Remove the ith entry from speculative_keys.
  774. for (uint8_t j = i; j < num_speculative_keys; ++j) {
  775. speculative_keys[j] = speculative_keys[j + 1];
  776. }
  777. } else {
  778. speculative_mods = 0;
  779. }
  780. ac_dprintf("Speculative Hold: settled %02x, ", cleared_mods);
  781. debug_speculative_keys();
  782. }
  783. } else { // Tap press event; cancel speculatively-held mod.
  784. if (i >= num_speculative_keys) {
  785. i = 0;
  786. }
  787. // Clear mods for the ith key and all keys that follow.
  788. uint8_t cleared_mods = 0;
  789. for (uint8_t j = i; j < num_speculative_keys; ++j) {
  790. cleared_mods |= speculative_keys[j].mods;
  791. }
  792. num_speculative_keys = i; // Remove ith and following entries.
  793. if ((prev_speculative_mods & cleared_mods) != 0) {
  794. # ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE
  795. neutralize_flashing_modifiers(get_mods() | prev_speculative_mods);
  796. # endif // DUMMY_MOD_NEUTRALIZER_KEYCODE
  797. }
  798. if (num_speculative_keys) {
  799. speculative_mods &= ~cleared_mods;
  800. } else {
  801. speculative_mods = 0;
  802. }
  803. send_keyboard_report();
  804. wait_ms(TAP_CODE_DELAY);
  805. ac_dprintf("Speculative Hold: canceled %02x, ", cleared_mods);
  806. debug_speculative_keys();
  807. }
  808. }
  809. # endif // SPECULATIVE_HOLD
  810. # if defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM)
  811. static void registered_taps_add(keypos_t key) {
  812. if (num_registered_taps >= REGISTERED_TAPS_SIZE) {
  813. ac_dprintf("TAPS OVERFLOW: CLEAR ALL STATES\n");
  814. clear_keyboard();
  815. num_registered_taps = 0;
  816. }
  817. registered_taps[num_registered_taps] = key;
  818. ++num_registered_taps;
  819. }
  820. static int8_t registered_tap_find(keypos_t key) {
  821. for (int8_t i = 0; i < num_registered_taps; ++i) {
  822. if (KEYEQ(registered_taps[i], key)) {
  823. return i;
  824. }
  825. }
  826. return -1;
  827. }
  828. static void registered_taps_del_index(uint8_t i) {
  829. if (i < num_registered_taps) {
  830. --num_registered_taps;
  831. if (i < num_registered_taps) {
  832. registered_taps[i] = registered_taps[num_registered_taps];
  833. }
  834. }
  835. }
  836. static void debug_registered_taps(void) {
  837. ac_dprintf("registered_taps = { ");
  838. for (int8_t i = 0; i < num_registered_taps; ++i) {
  839. ac_dprintf("%02X%02X ", registered_taps[i].row, registered_taps[i].col);
  840. }
  841. ac_dprintf("}\n");
  842. }
  843. # endif // defined(CHORDAL_HOLD) || defined(FLOW_TAP_TERM)
  844. # ifdef CHORDAL_HOLD
  845. __attribute__((weak)) bool get_chordal_hold(uint16_t tap_hold_keycode, keyrecord_t *tap_hold_record, uint16_t other_keycode, keyrecord_t *other_record) {
  846. return get_chordal_hold_default(tap_hold_record, other_record);
  847. }
  848. bool get_chordal_hold_default(keyrecord_t *tap_hold_record, keyrecord_t *other_record) {
  849. if (tap_hold_record->event.type != KEY_EVENT || other_record->event.type != KEY_EVENT) {
  850. return true; // Return true on combos or other non-key events.
  851. }
  852. char tap_hold_hand = chordal_hold_handedness(tap_hold_record->event.key);
  853. if (tap_hold_hand == '*') {
  854. return true;
  855. }
  856. char other_hand = chordal_hold_handedness(other_record->event.key);
  857. return other_hand == '*' || tap_hold_hand != other_hand;
  858. }
  859. __attribute__((weak)) char chordal_hold_handedness(keypos_t key) {
  860. return (char)pgm_read_byte(&chordal_hold_layout[key.row][key.col]);
  861. }
  862. static uint8_t waiting_buffer_find_chordal_hold_tap(void) {
  863. keyrecord_t *prev = &tapping_key;
  864. uint16_t prev_keycode = get_record_keycode(&tapping_key, false);
  865. uint8_t first_tap = WAITING_BUFFER_SIZE;
  866. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  867. keyrecord_t *cur = &waiting_buffer[i];
  868. const uint16_t cur_keycode = get_record_keycode(cur, false);
  869. if (!cur->event.pressed || !is_mt_or_lt(prev_keycode)) {
  870. break;
  871. } else if (get_chordal_hold(prev_keycode, prev, cur_keycode, cur)) {
  872. first_tap = i; // Track one index past the latest hold.
  873. }
  874. prev = cur;
  875. prev_keycode = cur_keycode;
  876. }
  877. return first_tap;
  878. }
  879. static void waiting_buffer_chordal_hold_taps_until(keypos_t key) {
  880. while (waiting_buffer_tail != waiting_buffer_head) {
  881. keyrecord_t *record = &waiting_buffer[waiting_buffer_tail];
  882. ac_dprintf("waiting_buffer_chordal_hold_taps_until: processing [%u]\n", waiting_buffer_tail);
  883. if (record->event.pressed && is_tap_record(record)) {
  884. record->tap.count = 1;
  885. registered_taps_add(record->event.key);
  886. }
  887. process_record(record);
  888. waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE;
  889. if (KEYEQ(key, record->event.key) && record->event.pressed) {
  890. break;
  891. }
  892. }
  893. }
  894. static void waiting_buffer_process_regular(void) {
  895. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  896. if (is_tap_record(&waiting_buffer[waiting_buffer_tail])) {
  897. break; // Stop once a tap-hold key event is reached.
  898. }
  899. ac_dprintf("waiting_buffer_process_regular: processing [%u]\n", waiting_buffer_tail);
  900. process_record(&waiting_buffer[waiting_buffer_tail]);
  901. }
  902. debug_waiting_buffer();
  903. }
  904. # endif // CHORDAL_HOLD
  905. # if defined(FLOW_TAP_TERM) || defined(SPECULATIVE_HOLD_FLOW_TERM)
  906. void flow_tap_update_last_event(keyrecord_t *record) {
  907. const uint16_t keycode = get_record_keycode(record, false);
  908. // Don't update while a tap-hold key is unsettled.
  909. if (record->tap.count == 0 && (waiting_buffer_tail != waiting_buffer_head || (tapping_key.event.pressed && tapping_key.tap.count == 0))) {
  910. return;
  911. }
  912. // Ignore releases of modifiers and held layer switches.
  913. if (!record->event.pressed) {
  914. switch (keycode) {
  915. case MODIFIER_KEYCODE_RANGE:
  916. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  917. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  918. # ifndef NO_ACTION_ONESHOT // Ignore one-shot keys.
  919. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
  920. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  921. # endif // NO_ACTION_ONESHOT
  922. # ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys.
  923. case QK_TRI_LAYER_LOWER:
  924. case QK_TRI_LAYER_UPPER:
  925. # endif // TRI_LAYER_ENABLE
  926. return;
  927. case QK_MODS ... QK_MODS_MAX:
  928. if (QK_MODS_GET_BASIC_KEYCODE(keycode) == KC_NO) {
  929. return;
  930. }
  931. break;
  932. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  933. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  934. if (record->tap.count == 0) {
  935. return;
  936. }
  937. break;
  938. }
  939. }
  940. flow_tap_prev_keycode = keycode;
  941. flow_tap_prev_time = record->event.time;
  942. flow_tap_expired = false;
  943. }
  944. # endif // defined(FLOW_TAP_TERM) || defined(SPECULATIVE_HOLD_FLOW_TERM)
  945. # ifdef FLOW_TAP_TERM
  946. static bool flow_tap_key_if_within_term(keyrecord_t *record, uint16_t prev_time) {
  947. const uint16_t idle_time = TIMER_DIFF_16(record->event.time, prev_time);
  948. if (flow_tap_expired || idle_time >= 500) {
  949. return false;
  950. }
  951. const uint16_t keycode = get_record_keycode(record, false);
  952. if (is_mt_or_lt(keycode)) {
  953. uint16_t term = get_flow_tap_term(keycode, record, flow_tap_prev_keycode);
  954. if (term > 500) {
  955. term = 500;
  956. }
  957. if (idle_time < term) {
  958. debug_event(record->event);
  959. ac_dprintf(" within flow tap term (%u < %u) considered a tap\n", idle_time, term);
  960. record->tap.count = 1;
  961. registered_taps_add(record->event.key);
  962. debug_registered_taps();
  963. process_record(record);
  964. return true;
  965. }
  966. }
  967. return false;
  968. }
  969. // Checks both flow_tap_expired flag and elapsed time to determine
  970. // if the key is within the flow tap term.
  971. bool within_flow_tap_term(uint16_t keycode, keyrecord_t *record) {
  972. uint16_t term = get_flow_tap_term(keycode, record, flow_tap_prev_keycode);
  973. return !flow_tap_expired && TIMER_DIFF_16(record->event.time, flow_tap_prev_time) <= term;
  974. }
  975. // By default, enable Flow Tap for the keys in the main alphas area and Space.
  976. // This should work reasonably even if the layout is remapped on the host to an
  977. // alt layout or international layout (e.g. Dvorak or AZERTY), where these same
  978. // key positions are mostly used for typing letters.
  979. __attribute__((weak)) bool is_flow_tap_key(uint16_t keycode) {
  980. if ((get_mods() & (MOD_MASK_CG | MOD_BIT_LALT)) != 0) {
  981. return false; // Disable Flow Tap on hotkeys.
  982. }
  983. switch (get_tap_keycode(keycode)) {
  984. case KC_SPC:
  985. case KC_A ... KC_Z:
  986. case KC_DOT:
  987. case KC_COMM:
  988. case KC_SCLN:
  989. case KC_SLSH:
  990. return true;
  991. }
  992. return false;
  993. }
  994. __attribute__((weak)) uint16_t get_flow_tap_term(uint16_t keycode, keyrecord_t *record, uint16_t prev_keycode) {
  995. if (is_flow_tap_key(keycode) && is_flow_tap_key(prev_keycode)) {
  996. return FLOW_TAP_TERM;
  997. }
  998. return 0;
  999. }
  1000. # endif // FLOW_TAP_TERM
  1001. /** \brief Logs tapping key if ACTION_DEBUG is enabled. */
  1002. static void debug_tapping_key(void) {
  1003. ac_dprintf("TAPPING_KEY=");
  1004. debug_record(tapping_key);
  1005. ac_dprintf("\n");
  1006. }
  1007. /** \brief Logs waiting buffer if ACTION_DEBUG is enabled. */
  1008. static void debug_waiting_buffer(void) {
  1009. ac_dprintf("{");
  1010. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  1011. ac_dprintf(" [%u]=", i);
  1012. debug_record(waiting_buffer[i]);
  1013. }
  1014. ac_dprintf("}\n");
  1015. }
  1016. #endif