action_tapping.c 16 KB

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