Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

action_tapping.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "action.h"
  4. #include "action_tapping.h"
  5. #include "timer.h"
  6. #ifdef DEBUG_ACTION
  7. #include "debug.h"
  8. #else
  9. #include "nodebug.h"
  10. #endif
  11. #ifndef NO_ACTION_TAPPING
  12. #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
  13. #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  14. #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  15. #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  16. #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
  17. static keyrecord_t tapping_key = {};
  18. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  19. static uint8_t waiting_buffer_head = 0;
  20. static uint8_t waiting_buffer_tail = 0;
  21. static bool process_tapping(keyrecord_t *record);
  22. static bool waiting_buffer_enq(keyrecord_t record);
  23. static void waiting_buffer_clear(void);
  24. #if TAPPING_TERM >= 500
  25. static bool waiting_buffer_typed(keyevent_t event);
  26. #endif
  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. /* This can settle mod/fn state fast but may prevent from typing fast. */
  88. else if (!event.pressed && waiting_buffer_typed(event)) {
  89. // other key typed. not tap.
  90. debug("Tapping: End. No tap. Interfered by typing key\n");
  91. process_action(&tapping_key);
  92. tapping_key = (keyrecord_t){};
  93. debug_tapping_key();
  94. // enqueue
  95. return false;
  96. }
  97. #endif
  98. else {
  99. // set interrupted flag when other key preesed during tapping
  100. if (event.pressed) {
  101. tapping_key.tap.interrupted = true;
  102. }
  103. // enqueue
  104. return false;
  105. }
  106. }
  107. // tap_count > 0
  108. else {
  109. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  110. debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
  111. keyp->tap = tapping_key.tap;
  112. process_action(keyp);
  113. tapping_key = *keyp;
  114. debug_tapping_key();
  115. return true;
  116. }
  117. else if (is_tap_key(event.key) && event.pressed) {
  118. if (tapping_key.tap.count > 1) {
  119. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  120. // unregister key
  121. process_action(&(keyrecord_t){
  122. .tap = tapping_key.tap,
  123. .event.key = tapping_key.event.key,
  124. .event.time = event.time,
  125. .event.pressed = false
  126. });
  127. } else {
  128. debug("Tapping: Start while last tap(1).\n");
  129. }
  130. tapping_key = *keyp;
  131. waiting_buffer_scan_tap();
  132. debug_tapping_key();
  133. return true;
  134. }
  135. else {
  136. if (!IS_NOEVENT(event)) {
  137. debug("Tapping: key event while last tap(>0).\n");
  138. }
  139. process_action(keyp);
  140. return true;
  141. }
  142. }
  143. }
  144. // after TAPPING_TERM
  145. else {
  146. if (tapping_key.tap.count == 0) {
  147. debug("Tapping: End. Timeout. Not tap(0): ");
  148. debug_event(event); debug("\n");
  149. process_action(&tapping_key);
  150. tapping_key = (keyrecord_t){};
  151. debug_tapping_key();
  152. return false;
  153. } else {
  154. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  155. debug("Tapping: End. last timeout tap release(>0).");
  156. keyp->tap = tapping_key.tap;
  157. process_action(keyp);
  158. tapping_key = (keyrecord_t){};
  159. return true;
  160. }
  161. else if (is_tap_key(event.key) && event.pressed) {
  162. if (tapping_key.tap.count > 1) {
  163. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  164. // unregister key
  165. process_action(&(keyrecord_t){
  166. .tap = tapping_key.tap,
  167. .event.key = tapping_key.event.key,
  168. .event.time = event.time,
  169. .event.pressed = false
  170. });
  171. } else {
  172. debug("Tapping: Start while last timeout tap(1).\n");
  173. }
  174. tapping_key = *keyp;
  175. waiting_buffer_scan_tap();
  176. debug_tapping_key();
  177. return true;
  178. }
  179. else {
  180. if (!IS_NOEVENT(event)) {
  181. debug("Tapping: key event while last timeout tap(>0).\n");
  182. }
  183. process_action(keyp);
  184. return true;
  185. }
  186. }
  187. }
  188. } else if (IS_TAPPING_RELEASED()) {
  189. if (WITHIN_TAPPING_TERM(event)) {
  190. if (event.pressed) {
  191. if (IS_TAPPING_KEY(event.key)) {
  192. if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  193. // sequential tap.
  194. keyp->tap = tapping_key.tap;
  195. keyp->tap.count += 1;
  196. debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
  197. process_action(keyp);
  198. tapping_key = *keyp;
  199. debug_tapping_key();
  200. return true;
  201. } else {
  202. // FIX: start new tap again
  203. tapping_key = *keyp;
  204. return true;
  205. }
  206. } else if (is_tap_key(event.key)) {
  207. // Sequential tap can be interfered with other tap key.
  208. debug("Tapping: Start with interfering other tap.\n");
  209. tapping_key = *keyp;
  210. waiting_buffer_scan_tap();
  211. debug_tapping_key();
  212. return true;
  213. } else {
  214. // should none in buffer
  215. // FIX: interrupted when other key is pressed
  216. tapping_key.tap.interrupted = true;
  217. process_action(keyp);
  218. return true;
  219. }
  220. } else {
  221. if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
  222. process_action(keyp);
  223. return true;
  224. }
  225. } else {
  226. // FIX: process_aciton here?
  227. // timeout. no sequential tap.
  228. debug("Tapping: End(Timeout after releasing last tap): ");
  229. debug_event(event); debug("\n");
  230. tapping_key = (keyrecord_t){};
  231. debug_tapping_key();
  232. return false;
  233. }
  234. }
  235. // not tapping state
  236. else {
  237. if (event.pressed && is_tap_key(event.key)) {
  238. debug("Tapping: Start(Press tap key).\n");
  239. tapping_key = *keyp;
  240. waiting_buffer_scan_tap();
  241. debug_tapping_key();
  242. return true;
  243. } else {
  244. process_action(keyp);
  245. return true;
  246. }
  247. }
  248. }
  249. /*
  250. * Waiting buffer
  251. */
  252. bool waiting_buffer_enq(keyrecord_t record)
  253. {
  254. if (IS_NOEVENT(record.event)) {
  255. return true;
  256. }
  257. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  258. debug("waiting_buffer_enq: Over flow.\n");
  259. return false;
  260. }
  261. waiting_buffer[waiting_buffer_head] = record;
  262. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  263. debug("waiting_buffer_enq: "); debug_waiting_buffer();
  264. return true;
  265. }
  266. void waiting_buffer_clear(void)
  267. {
  268. waiting_buffer_head = 0;
  269. waiting_buffer_tail = 0;
  270. }
  271. #if TAPPING_TERM >= 500
  272. bool waiting_buffer_typed(keyevent_t event)
  273. {
  274. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  275. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. #endif
  282. bool waiting_buffer_has_anykey_pressed(void)
  283. {
  284. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  285. if (waiting_buffer[i].event.pressed) return true;
  286. }
  287. return false;
  288. }
  289. /* scan buffer for tapping */
  290. void waiting_buffer_scan_tap(void)
  291. {
  292. // tapping already is settled
  293. if (tapping_key.tap.count > 0) return;
  294. // invalid state: tapping_key released && tap.count == 0
  295. if (!tapping_key.event.pressed) return;
  296. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  297. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  298. !waiting_buffer[i].event.pressed &&
  299. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  300. tapping_key.tap.count = 1;
  301. waiting_buffer[i].tap.count = 1;
  302. process_action(&tapping_key);
  303. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  304. debug_waiting_buffer();
  305. return;
  306. }
  307. }
  308. }
  309. /*
  310. * debug print
  311. */
  312. static void debug_tapping_key(void)
  313. {
  314. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  315. }
  316. static void debug_waiting_buffer(void)
  317. {
  318. debug("{ ");
  319. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  320. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  321. }
  322. debug("}\n");
  323. }
  324. #endif