action_tapping.c 15 KB

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