action_tapping.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #include "keymap_common.h"
  9. #ifdef DEBUG_ACTION
  10. # include "debug.h"
  11. #else
  12. # include "nodebug.h"
  13. #endif
  14. #ifndef NO_ACTION_TAPPING
  15. # define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
  16. # define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  17. # define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  18. # define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  19. #ifndef COMBO_ENABLE
  20. # define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)))
  21. #else
  22. # define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode)
  23. #endif
  24. __attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { return 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) < 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. static keyrecord_t tapping_key = {};
  40. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  41. static uint8_t waiting_buffer_head = 0;
  42. static uint8_t waiting_buffer_tail = 0;
  43. static bool process_tapping(keyrecord_t *record);
  44. static bool waiting_buffer_enq(keyrecord_t record);
  45. static void waiting_buffer_clear(void);
  46. static bool waiting_buffer_typed(keyevent_t event);
  47. static bool waiting_buffer_has_anykey_pressed(void);
  48. static void waiting_buffer_scan_tap(void);
  49. static void debug_tapping_key(void);
  50. static void debug_waiting_buffer(void);
  51. /* Convert record into usable keycode via the contained event. */
  52. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
  53. #ifdef COMBO_ENABLE
  54. if (record->keycode) { return record->keycode; }
  55. #endif
  56. return get_event_keycode(record->event, update_layer_cache);
  57. }
  58. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  59. * retains the correct keycode after a layer change, if the key is still pressed.
  60. * "update_layer_cache" is to ensure that it only updates the layer cache when
  61. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  62. * from triggering properly.
  63. */
  64. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  65. const keypos_t key = event.key;
  66. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  67. /* TODO: Use store_or_get_action() or a similar function. */
  68. if (!disable_action_cache) {
  69. uint8_t layer;
  70. if (event.pressed && update_layer_cache) {
  71. layer = layer_switch_get_layer(key);
  72. update_source_layers_cache(key, layer);
  73. } else {
  74. layer = read_source_layers_cache(key);
  75. }
  76. return keymap_key_to_keycode(layer, key);
  77. }
  78. #endif
  79. return keymap_key_to_keycode(layer_switch_get_layer(key), key);
  80. }
  81. /** \brief Action Tapping Process
  82. *
  83. * FIXME: Needs doc
  84. */
  85. void action_tapping_process(keyrecord_t record) {
  86. if (process_tapping(&record)) {
  87. if (!IS_NOEVENT(record.event)) {
  88. debug("processed: ");
  89. debug_record(record);
  90. debug("\n");
  91. }
  92. } else {
  93. if (!waiting_buffer_enq(record)) {
  94. // clear all in case of overflow.
  95. debug("OVERFLOW: CLEAR ALL STATES\n");
  96. clear_keyboard();
  97. waiting_buffer_clear();
  98. tapping_key = (keyrecord_t){};
  99. }
  100. }
  101. // process waiting_buffer
  102. if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
  103. debug("---- action_exec: process waiting_buffer -----\n");
  104. }
  105. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  106. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  107. debug("processed: waiting_buffer[");
  108. debug_dec(waiting_buffer_tail);
  109. debug("] = ");
  110. debug_record(waiting_buffer[waiting_buffer_tail]);
  111. debug("\n\n");
  112. } else {
  113. break;
  114. }
  115. }
  116. if (!IS_NOEVENT(record.event)) {
  117. debug("\n");
  118. }
  119. }
  120. /** \brief Tapping
  121. *
  122. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  123. * (without interfering by typing other key)
  124. */
  125. /* return true when key event is processed or consumed. */
  126. bool process_tapping(keyrecord_t *keyp) {
  127. keyevent_t event = keyp->event;
  128. // if tapping
  129. if (IS_TAPPING_PRESSED()) {
  130. if (WITHIN_TAPPING_TERM(event)) {
  131. if (tapping_key.tap.count == 0) {
  132. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  133. // first tap!
  134. debug("Tapping: First tap(0->1).\n");
  135. tapping_key.tap.count = 1;
  136. debug_tapping_key();
  137. process_record(&tapping_key);
  138. // copy tapping state
  139. keyp->tap = tapping_key.tap;
  140. // enqueue
  141. return false;
  142. }
  143. /* Process a key typed within TAPPING_TERM
  144. * This can register the key before settlement of tapping,
  145. * useful for long TAPPING_TERM but may prevent fast typing.
  146. */
  147. # if defined(TAPPING_TERM_PER_KEY) || (TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) || defined(PERMISSIVE_HOLD_PER_KEY)
  148. else if (((
  149. # ifdef TAPPING_TERM_PER_KEY
  150. get_tapping_term(get_record_keycode(&tapping_key, false), keyp)
  151. # else
  152. TAPPING_TERM
  153. # endif
  154. >= 500)
  155. # ifdef PERMISSIVE_HOLD_PER_KEY
  156. || get_permissive_hold(get_record_keycode(&tapping_key, false), keyp)
  157. # elif defined(PERMISSIVE_HOLD)
  158. || true
  159. # endif
  160. ) &&
  161. IS_RELEASED(event) && waiting_buffer_typed(event)) {
  162. debug("Tapping: End. No tap. Interfered by typing key\n");
  163. process_record(&tapping_key);
  164. tapping_key = (keyrecord_t){};
  165. debug_tapping_key();
  166. // enqueue
  167. return false;
  168. }
  169. # endif
  170. /* Process release event of a key pressed before tapping starts
  171. * Without this unexpected repeating will occur with having fast repeating setting
  172. * https://github.com/tmk/tmk_keyboard/issues/60
  173. */
  174. else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) {
  175. // Modifier should be retained till end of this tapping.
  176. action_t action = layer_switch_get_action(event.key);
  177. switch (action.kind.id) {
  178. case ACT_LMODS:
  179. case ACT_RMODS:
  180. if (action.key.mods && !action.key.code) return false;
  181. if (IS_MOD(action.key.code)) return false;
  182. break;
  183. case ACT_LMODS_TAP:
  184. case ACT_RMODS_TAP:
  185. if (action.key.mods && keyp->tap.count == 0) return false;
  186. if (IS_MOD(action.key.code)) return false;
  187. break;
  188. }
  189. // Release of key should be process immediately.
  190. debug("Tapping: release event of a key pressed before tapping\n");
  191. process_record(keyp);
  192. return true;
  193. } else {
  194. // set interrupted flag when other key preesed during tapping
  195. if (event.pressed) {
  196. tapping_key.tap.interrupted = true;
  197. # if defined(HOLD_ON_OTHER_KEY_PRESS) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
  198. # if defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
  199. if (get_hold_on_other_key_press(get_record_keycode(&tapping_key, false), keyp))
  200. # endif
  201. {
  202. debug("Tapping: End. No tap. Interfered by pressed key\n");
  203. process_record(&tapping_key);
  204. tapping_key = (keyrecord_t){};
  205. debug_tapping_key();
  206. // enqueue
  207. return false;
  208. }
  209. # endif
  210. }
  211. // enqueue
  212. return false;
  213. }
  214. }
  215. // tap_count > 0
  216. else {
  217. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  218. debug("Tapping: Tap release(");
  219. debug_dec(tapping_key.tap.count);
  220. debug(")\n");
  221. keyp->tap = tapping_key.tap;
  222. process_record(keyp);
  223. tapping_key = *keyp;
  224. debug_tapping_key();
  225. return true;
  226. } else if (is_tap_record(keyp) && event.pressed) {
  227. if (tapping_key.tap.count > 1) {
  228. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  229. // unregister key
  230. process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false,
  231. #ifdef COMBO_ENABLE
  232. .keycode = tapping_key.keycode,
  233. #endif
  234. });
  235. } else {
  236. debug("Tapping: Start while last tap(1).\n");
  237. }
  238. tapping_key = *keyp;
  239. waiting_buffer_scan_tap();
  240. debug_tapping_key();
  241. return true;
  242. } else {
  243. if (!IS_NOEVENT(event)) {
  244. debug("Tapping: key event while last tap(>0).\n");
  245. }
  246. process_record(keyp);
  247. return true;
  248. }
  249. }
  250. }
  251. // after TAPPING_TERM
  252. else {
  253. if (tapping_key.tap.count == 0) {
  254. debug("Tapping: End. Timeout. Not tap(0): ");
  255. debug_event(event);
  256. debug("\n");
  257. process_record(&tapping_key);
  258. tapping_key = (keyrecord_t){};
  259. debug_tapping_key();
  260. return false;
  261. } else {
  262. if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
  263. debug("Tapping: End. last timeout tap release(>0).");
  264. keyp->tap = tapping_key.tap;
  265. process_record(keyp);
  266. tapping_key = (keyrecord_t){};
  267. return true;
  268. } else if (is_tap_record(keyp) && event.pressed) {
  269. if (tapping_key.tap.count > 1) {
  270. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  271. // unregister key
  272. process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false,
  273. #ifdef COMBO_ENABLE
  274. .keycode = tapping_key.keycode,
  275. #endif
  276. });
  277. } else {
  278. debug("Tapping: Start while last timeout 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 timeout tap(>0).\n");
  287. }
  288. process_record(keyp);
  289. return true;
  290. }
  291. }
  292. }
  293. } else if (IS_TAPPING_RELEASED()) {
  294. if (WITHIN_TAPPING_TERM(event)) {
  295. if (event.pressed) {
  296. if (IS_TAPPING_RECORD(keyp)) {
  297. //# ifndef TAPPING_FORCE_HOLD
  298. # if !defined(TAPPING_FORCE_HOLD) || defined(TAPPING_FORCE_HOLD_PER_KEY)
  299. if (
  300. # ifdef TAPPING_FORCE_HOLD_PER_KEY
  301. !get_tapping_force_hold(get_record_keycode(&tapping_key, false), keyp) &&
  302. # endif
  303. !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  304. // sequential tap.
  305. keyp->tap = tapping_key.tap;
  306. if (keyp->tap.count < 15) keyp->tap.count += 1;
  307. debug("Tapping: Tap press(");
  308. debug_dec(keyp->tap.count);
  309. debug(")\n");
  310. process_record(keyp);
  311. tapping_key = *keyp;
  312. debug_tapping_key();
  313. return true;
  314. }
  315. # endif
  316. // FIX: start new tap again
  317. tapping_key = *keyp;
  318. return true;
  319. } else if (is_tap_record(keyp)) {
  320. // Sequential tap can be interfered with other tap key.
  321. debug("Tapping: Start with interfering other tap.\n");
  322. tapping_key = *keyp;
  323. waiting_buffer_scan_tap();
  324. debug_tapping_key();
  325. return true;
  326. } else {
  327. // should none in buffer
  328. // FIX: interrupted when other key is pressed
  329. tapping_key.tap.interrupted = true;
  330. process_record(keyp);
  331. return true;
  332. }
  333. } else {
  334. if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
  335. process_record(keyp);
  336. return true;
  337. }
  338. } else {
  339. // FIX: process_action here?
  340. // timeout. no sequential tap.
  341. debug("Tapping: End(Timeout after releasing last tap): ");
  342. debug_event(event);
  343. debug("\n");
  344. tapping_key = (keyrecord_t){};
  345. debug_tapping_key();
  346. return false;
  347. }
  348. }
  349. // not tapping state
  350. else {
  351. if (event.pressed && is_tap_record(keyp)) {
  352. debug("Tapping: Start(Press tap key).\n");
  353. tapping_key = *keyp;
  354. process_record_tap_hint(&tapping_key);
  355. waiting_buffer_scan_tap();
  356. debug_tapping_key();
  357. return true;
  358. } else {
  359. process_record(keyp);
  360. return true;
  361. }
  362. }
  363. }
  364. /** \brief Waiting buffer enq
  365. *
  366. * FIXME: Needs docs
  367. */
  368. bool waiting_buffer_enq(keyrecord_t record) {
  369. if (IS_NOEVENT(record.event)) {
  370. return true;
  371. }
  372. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  373. debug("waiting_buffer_enq: Over flow.\n");
  374. return false;
  375. }
  376. waiting_buffer[waiting_buffer_head] = record;
  377. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  378. debug("waiting_buffer_enq: ");
  379. debug_waiting_buffer();
  380. return true;
  381. }
  382. /** \brief Waiting buffer clear
  383. *
  384. * FIXME: Needs docs
  385. */
  386. void waiting_buffer_clear(void) {
  387. waiting_buffer_head = 0;
  388. waiting_buffer_tail = 0;
  389. }
  390. /** \brief Waiting buffer typed
  391. *
  392. * FIXME: Needs docs
  393. */
  394. bool waiting_buffer_typed(keyevent_t event) {
  395. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  396. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  397. return true;
  398. }
  399. }
  400. return false;
  401. }
  402. /** \brief Waiting buffer has anykey pressed
  403. *
  404. * FIXME: Needs docs
  405. */
  406. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  407. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  408. if (waiting_buffer[i].event.pressed) return true;
  409. }
  410. return false;
  411. }
  412. /** \brief Scan buffer for tapping
  413. *
  414. * FIXME: Needs docs
  415. */
  416. void waiting_buffer_scan_tap(void) {
  417. // tapping already is settled
  418. if (tapping_key.tap.count > 0) return;
  419. // invalid state: tapping_key released && tap.count == 0
  420. if (!tapping_key.event.pressed) return;
  421. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  422. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && !waiting_buffer[i].event.pressed && WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  423. tapping_key.tap.count = 1;
  424. waiting_buffer[i].tap.count = 1;
  425. process_record(&tapping_key);
  426. debug("waiting_buffer_scan_tap: found at [");
  427. debug_dec(i);
  428. debug("]\n");
  429. debug_waiting_buffer();
  430. return;
  431. }
  432. }
  433. }
  434. /** \brief Tapping key debug print
  435. *
  436. * FIXME: Needs docs
  437. */
  438. static void debug_tapping_key(void) {
  439. debug("TAPPING_KEY=");
  440. debug_record(tapping_key);
  441. debug("\n");
  442. }
  443. /** \brief Waiting buffer debug print
  444. *
  445. * FIXME: Needs docs
  446. */
  447. static void debug_waiting_buffer(void) {
  448. debug("{ ");
  449. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  450. debug("[");
  451. debug_dec(i);
  452. debug("]=");
  453. debug_record(waiting_buffer[i]);
  454. debug(" ");
  455. }
  456. debug("}\n");
  457. }
  458. #endif