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 13KB

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