Peter's keyboard firmware (QMK) https://qmk.fm/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action_tapping.c 15KB

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