action_tapping.c 17 KB

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