action_tapping.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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)) uint16_t get_tapping_term(uint16_t keycode) { return TAPPING_TERM; }
  19. # ifdef TAPPING_TERM_PER_KEY
  20. # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event)))
  21. # else
  22. # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
  23. # endif
  24. static keyrecord_t tapping_key = {};
  25. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  26. static uint8_t waiting_buffer_head = 0;
  27. static uint8_t waiting_buffer_tail = 0;
  28. static bool process_tapping(keyrecord_t *record);
  29. static bool waiting_buffer_enq(keyrecord_t record);
  30. static void waiting_buffer_clear(void);
  31. static bool waiting_buffer_typed(keyevent_t event);
  32. static bool waiting_buffer_has_anykey_pressed(void);
  33. static void waiting_buffer_scan_tap(void);
  34. static void debug_tapping_key(void);
  35. static void debug_waiting_buffer(void);
  36. /** \brief Action Tapping Process
  37. *
  38. * FIXME: Needs doc
  39. */
  40. void action_tapping_process(keyrecord_t record) {
  41. if (process_tapping(&record)) {
  42. if (!IS_NOEVENT(record.event)) {
  43. debug("processed: ");
  44. debug_record(record);
  45. debug("\n");
  46. }
  47. } else {
  48. if (!waiting_buffer_enq(record)) {
  49. // clear all in case of overflow.
  50. debug("OVERFLOW: CLEAR ALL STATES\n");
  51. clear_keyboard();
  52. waiting_buffer_clear();
  53. tapping_key = (keyrecord_t){};
  54. }
  55. }
  56. // process waiting_buffer
  57. if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
  58. debug("---- action_exec: process waiting_buffer -----\n");
  59. }
  60. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  61. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  62. debug("processed: waiting_buffer[");
  63. debug_dec(waiting_buffer_tail);
  64. debug("] = ");
  65. debug_record(waiting_buffer[waiting_buffer_tail]);
  66. debug("\n\n");
  67. } else {
  68. break;
  69. }
  70. }
  71. if (!IS_NOEVENT(record.event)) {
  72. debug("\n");
  73. }
  74. }
  75. /** \brief Tapping
  76. *
  77. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  78. * (without interfering by typing other key)
  79. */
  80. /* return true when key event is processed or consumed. */
  81. bool process_tapping(keyrecord_t *keyp) {
  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) || (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. } else {
  141. // set interrupted flag when other key preesed during tapping
  142. if (event.pressed) {
  143. tapping_key.tap.interrupted = true;
  144. }
  145. // enqueue
  146. return false;
  147. }
  148. }
  149. // tap_count > 0
  150. else {
  151. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  152. debug("Tapping: Tap release(");
  153. debug_dec(tapping_key.tap.count);
  154. debug(")\n");
  155. keyp->tap = tapping_key.tap;
  156. process_record(keyp);
  157. tapping_key = *keyp;
  158. debug_tapping_key();
  159. return true;
  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){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false});
  165. } else {
  166. debug("Tapping: Start while last tap(1).\n");
  167. }
  168. tapping_key = *keyp;
  169. waiting_buffer_scan_tap();
  170. debug_tapping_key();
  171. return true;
  172. } else {
  173. if (!IS_NOEVENT(event)) {
  174. debug("Tapping: key event while last tap(>0).\n");
  175. }
  176. process_record(keyp);
  177. return true;
  178. }
  179. }
  180. }
  181. // after TAPPING_TERM
  182. else {
  183. if (tapping_key.tap.count == 0) {
  184. debug("Tapping: End. Timeout. Not tap(0): ");
  185. debug_event(event);
  186. debug("\n");
  187. process_record(&tapping_key);
  188. tapping_key = (keyrecord_t){};
  189. debug_tapping_key();
  190. return false;
  191. } else {
  192. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  193. debug("Tapping: End. last timeout tap release(>0).");
  194. keyp->tap = tapping_key.tap;
  195. process_record(keyp);
  196. tapping_key = (keyrecord_t){};
  197. return true;
  198. } else if (is_tap_key(event.key) && event.pressed) {
  199. if (tapping_key.tap.count > 1) {
  200. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  201. // unregister key
  202. process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false});
  203. } else {
  204. debug("Tapping: Start while last timeout tap(1).\n");
  205. }
  206. tapping_key = *keyp;
  207. waiting_buffer_scan_tap();
  208. debug_tapping_key();
  209. return true;
  210. } else {
  211. if (!IS_NOEVENT(event)) {
  212. debug("Tapping: key event while last timeout tap(>0).\n");
  213. }
  214. process_record(keyp);
  215. return true;
  216. }
  217. }
  218. }
  219. } else if (IS_TAPPING_RELEASED()) {
  220. if (WITHIN_TAPPING_TERM(event)) {
  221. if (event.pressed) {
  222. if (IS_TAPPING_KEY(event.key)) {
  223. # ifndef TAPPING_FORCE_HOLD
  224. if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  225. // sequential tap.
  226. keyp->tap = tapping_key.tap;
  227. if (keyp->tap.count < 15) keyp->tap.count += 1;
  228. debug("Tapping: Tap press(");
  229. debug_dec(keyp->tap.count);
  230. debug(")\n");
  231. process_record(keyp);
  232. tapping_key = *keyp;
  233. debug_tapping_key();
  234. return true;
  235. }
  236. # endif
  237. // FIX: start new tap again
  238. tapping_key = *keyp;
  239. return true;
  240. } else if (is_tap_key(event.key)) {
  241. // Sequential tap can be interfered with other tap key.
  242. debug("Tapping: Start with interfering other tap.\n");
  243. tapping_key = *keyp;
  244. waiting_buffer_scan_tap();
  245. debug_tapping_key();
  246. return true;
  247. } else {
  248. // should none in buffer
  249. // FIX: interrupted when other key is pressed
  250. tapping_key.tap.interrupted = true;
  251. process_record(keyp);
  252. return true;
  253. }
  254. } else {
  255. if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
  256. process_record(keyp);
  257. return true;
  258. }
  259. } else {
  260. // FIX: process_action here?
  261. // timeout. no sequential tap.
  262. debug("Tapping: End(Timeout after releasing last tap): ");
  263. debug_event(event);
  264. debug("\n");
  265. tapping_key = (keyrecord_t){};
  266. debug_tapping_key();
  267. return false;
  268. }
  269. }
  270. // not tapping state
  271. else {
  272. if (event.pressed && is_tap_key(event.key)) {
  273. debug("Tapping: Start(Press tap key).\n");
  274. tapping_key = *keyp;
  275. process_record_tap_hint(&tapping_key);
  276. waiting_buffer_scan_tap();
  277. debug_tapping_key();
  278. return true;
  279. } else {
  280. process_record(keyp);
  281. return true;
  282. }
  283. }
  284. }
  285. /** \brief Waiting buffer enq
  286. *
  287. * FIXME: Needs docs
  288. */
  289. bool waiting_buffer_enq(keyrecord_t record) {
  290. if (IS_NOEVENT(record.event)) {
  291. return true;
  292. }
  293. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  294. debug("waiting_buffer_enq: Over flow.\n");
  295. return false;
  296. }
  297. waiting_buffer[waiting_buffer_head] = record;
  298. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  299. debug("waiting_buffer_enq: ");
  300. debug_waiting_buffer();
  301. return true;
  302. }
  303. /** \brief Waiting buffer clear
  304. *
  305. * FIXME: Needs docs
  306. */
  307. void waiting_buffer_clear(void) {
  308. waiting_buffer_head = 0;
  309. waiting_buffer_tail = 0;
  310. }
  311. /** \brief Waiting buffer typed
  312. *
  313. * FIXME: Needs docs
  314. */
  315. bool waiting_buffer_typed(keyevent_t event) {
  316. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  317. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. /** \brief Waiting buffer has anykey pressed
  324. *
  325. * FIXME: Needs docs
  326. */
  327. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  328. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  329. if (waiting_buffer[i].event.pressed) return true;
  330. }
  331. return false;
  332. }
  333. /** \brief Scan buffer for tapping
  334. *
  335. * FIXME: Needs docs
  336. */
  337. void waiting_buffer_scan_tap(void) {
  338. // tapping already is settled
  339. if (tapping_key.tap.count > 0) return;
  340. // invalid state: tapping_key released && tap.count == 0
  341. if (!tapping_key.event.pressed) return;
  342. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  343. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && !waiting_buffer[i].event.pressed && WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  344. tapping_key.tap.count = 1;
  345. waiting_buffer[i].tap.count = 1;
  346. process_record(&tapping_key);
  347. debug("waiting_buffer_scan_tap: found at [");
  348. debug_dec(i);
  349. debug("]\n");
  350. debug_waiting_buffer();
  351. return;
  352. }
  353. }
  354. }
  355. /** \brief Tapping key debug print
  356. *
  357. * FIXME: Needs docs
  358. */
  359. static void debug_tapping_key(void) {
  360. debug("TAPPING_KEY=");
  361. debug_record(tapping_key);
  362. debug("\n");
  363. }
  364. /** \brief Waiting buffer debug print
  365. *
  366. * FIXME: Needs docs
  367. */
  368. static void debug_waiting_buffer(void) {
  369. debug("{ ");
  370. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  371. debug("[");
  372. debug_dec(i);
  373. debug("]=");
  374. debug_record(waiting_buffer[i]);
  375. debug(" ");
  376. }
  377. debug("}\n");
  378. }
  379. #endif