Keyboard firmwares for Atmel AVR and Cortex-M
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 14KB

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