action_tapping.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. # 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)) >= 500) &&
  112. # endif
  113. # ifdef PERMISSIVE_HOLD_PER_KEY
  114. !get_permissive_hold(get_event_keycode(tapping_key.event), keyp) &&
  115. # endif
  116. IS_RELEASED(event) && waiting_buffer_typed(event))
  117. {
  118. debug("Tapping: End. No tap. Interfered by typing key\n");
  119. process_record(&tapping_key);
  120. tapping_key = (keyrecord_t){};
  121. debug_tapping_key();
  122. // enqueue
  123. return false;
  124. }
  125. # endif
  126. /* Process release event of a key pressed before tapping starts
  127. * Without this unexpected repeating will occur with having fast repeating setting
  128. * https://github.com/tmk/tmk_keyboard/issues/60
  129. */
  130. else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) {
  131. // Modifier should be retained till end of this tapping.
  132. action_t action = layer_switch_get_action(event.key);
  133. switch (action.kind.id) {
  134. case ACT_LMODS:
  135. case ACT_RMODS:
  136. if (action.key.mods && !action.key.code) return false;
  137. if (IS_MOD(action.key.code)) return false;
  138. break;
  139. case ACT_LMODS_TAP:
  140. case ACT_RMODS_TAP:
  141. if (action.key.mods && keyp->tap.count == 0) return false;
  142. if (IS_MOD(action.key.code)) return false;
  143. break;
  144. }
  145. // Release of key should be process immediately.
  146. debug("Tapping: release event of a key pressed before tapping\n");
  147. process_record(keyp);
  148. return true;
  149. } else {
  150. // set interrupted flag when other key preesed during tapping
  151. if (event.pressed) {
  152. tapping_key.tap.interrupted = true;
  153. }
  154. // enqueue
  155. return false;
  156. }
  157. }
  158. // tap_count > 0
  159. else {
  160. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  161. debug("Tapping: Tap release(");
  162. debug_dec(tapping_key.tap.count);
  163. debug(")\n");
  164. keyp->tap = tapping_key.tap;
  165. process_record(keyp);
  166. tapping_key = *keyp;
  167. debug_tapping_key();
  168. return true;
  169. } else if (is_tap_key(event.key) && event.pressed) {
  170. if (tapping_key.tap.count > 1) {
  171. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  172. // unregister key
  173. process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false});
  174. } else {
  175. debug("Tapping: Start while last tap(1).\n");
  176. }
  177. tapping_key = *keyp;
  178. waiting_buffer_scan_tap();
  179. debug_tapping_key();
  180. return true;
  181. } else {
  182. if (!IS_NOEVENT(event)) {
  183. debug("Tapping: key event while last tap(>0).\n");
  184. }
  185. process_record(keyp);
  186. return true;
  187. }
  188. }
  189. }
  190. // after TAPPING_TERM
  191. else {
  192. if (tapping_key.tap.count == 0) {
  193. debug("Tapping: End. Timeout. Not tap(0): ");
  194. debug_event(event);
  195. debug("\n");
  196. process_record(&tapping_key);
  197. tapping_key = (keyrecord_t){};
  198. debug_tapping_key();
  199. return false;
  200. } else {
  201. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  202. debug("Tapping: End. last timeout tap release(>0).");
  203. keyp->tap = tapping_key.tap;
  204. process_record(keyp);
  205. tapping_key = (keyrecord_t){};
  206. return true;
  207. } else if (is_tap_key(event.key) && event.pressed) {
  208. if (tapping_key.tap.count > 1) {
  209. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  210. // unregister key
  211. process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false});
  212. } else {
  213. debug("Tapping: Start while last timeout tap(1).\n");
  214. }
  215. tapping_key = *keyp;
  216. waiting_buffer_scan_tap();
  217. debug_tapping_key();
  218. return true;
  219. } else {
  220. if (!IS_NOEVENT(event)) {
  221. debug("Tapping: key event while last timeout tap(>0).\n");
  222. }
  223. process_record(keyp);
  224. return true;
  225. }
  226. }
  227. }
  228. } else if (IS_TAPPING_RELEASED()) {
  229. if (WITHIN_TAPPING_TERM(event)) {
  230. if (event.pressed) {
  231. if (IS_TAPPING_KEY(event.key)) {
  232. //# ifndef TAPPING_FORCE_HOLD
  233. # if !defined(TAPPING_FORCE_HOLD) || defined(TAPPING_FORCE_HOLD_PER_KEY)
  234. if (
  235. # ifdef TAPPING_FORCE_HOLD_PER_KEY
  236. !get_tapping_force_hold(get_event_keycode(tapping_key.event), keyp) &&
  237. # endif
  238. !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
  239. // sequential tap.
  240. keyp->tap = tapping_key.tap;
  241. if (keyp->tap.count < 15) keyp->tap.count += 1;
  242. debug("Tapping: Tap press(");
  243. debug_dec(keyp->tap.count);
  244. debug(")\n");
  245. process_record(keyp);
  246. tapping_key = *keyp;
  247. debug_tapping_key();
  248. return true;
  249. }
  250. # endif
  251. // FIX: start new tap again
  252. tapping_key = *keyp;
  253. return true;
  254. } else if (is_tap_key(event.key)) {
  255. // Sequential tap can be interfered with other tap key.
  256. debug("Tapping: Start with interfering other tap.\n");
  257. tapping_key = *keyp;
  258. waiting_buffer_scan_tap();
  259. debug_tapping_key();
  260. return true;
  261. } else {
  262. // should none in buffer
  263. // FIX: interrupted when other key is pressed
  264. tapping_key.tap.interrupted = true;
  265. process_record(keyp);
  266. return true;
  267. }
  268. } else {
  269. if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
  270. process_record(keyp);
  271. return true;
  272. }
  273. } else {
  274. // FIX: process_action here?
  275. // timeout. no sequential tap.
  276. debug("Tapping: End(Timeout after releasing last tap): ");
  277. debug_event(event);
  278. debug("\n");
  279. tapping_key = (keyrecord_t){};
  280. debug_tapping_key();
  281. return false;
  282. }
  283. }
  284. // not tapping state
  285. else {
  286. if (event.pressed && is_tap_key(event.key)) {
  287. debug("Tapping: Start(Press tap key).\n");
  288. tapping_key = *keyp;
  289. process_record_tap_hint(&tapping_key);
  290. waiting_buffer_scan_tap();
  291. debug_tapping_key();
  292. return true;
  293. } else {
  294. process_record(keyp);
  295. return true;
  296. }
  297. }
  298. }
  299. /** \brief Waiting buffer enq
  300. *
  301. * FIXME: Needs docs
  302. */
  303. bool waiting_buffer_enq(keyrecord_t record) {
  304. if (IS_NOEVENT(record.event)) {
  305. return true;
  306. }
  307. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  308. debug("waiting_buffer_enq: Over flow.\n");
  309. return false;
  310. }
  311. waiting_buffer[waiting_buffer_head] = record;
  312. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  313. debug("waiting_buffer_enq: ");
  314. debug_waiting_buffer();
  315. return true;
  316. }
  317. /** \brief Waiting buffer clear
  318. *
  319. * FIXME: Needs docs
  320. */
  321. void waiting_buffer_clear(void) {
  322. waiting_buffer_head = 0;
  323. waiting_buffer_tail = 0;
  324. }
  325. /** \brief Waiting buffer typed
  326. *
  327. * FIXME: Needs docs
  328. */
  329. bool waiting_buffer_typed(keyevent_t event) {
  330. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  331. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. /** \brief Waiting buffer has anykey pressed
  338. *
  339. * FIXME: Needs docs
  340. */
  341. __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
  342. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  343. if (waiting_buffer[i].event.pressed) return true;
  344. }
  345. return false;
  346. }
  347. /** \brief Scan buffer for tapping
  348. *
  349. * FIXME: Needs docs
  350. */
  351. void waiting_buffer_scan_tap(void) {
  352. // tapping already is settled
  353. if (tapping_key.tap.count > 0) return;
  354. // invalid state: tapping_key released && tap.count == 0
  355. if (!tapping_key.event.pressed) return;
  356. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  357. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && !waiting_buffer[i].event.pressed && WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  358. tapping_key.tap.count = 1;
  359. waiting_buffer[i].tap.count = 1;
  360. process_record(&tapping_key);
  361. debug("waiting_buffer_scan_tap: found at [");
  362. debug_dec(i);
  363. debug("]\n");
  364. debug_waiting_buffer();
  365. return;
  366. }
  367. }
  368. }
  369. /** \brief Tapping key debug print
  370. *
  371. * FIXME: Needs docs
  372. */
  373. static void debug_tapping_key(void) {
  374. debug("TAPPING_KEY=");
  375. debug_record(tapping_key);
  376. debug("\n");
  377. }
  378. /** \brief Waiting buffer debug print
  379. *
  380. * FIXME: Needs docs
  381. */
  382. static void debug_waiting_buffer(void) {
  383. debug("{ ");
  384. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  385. debug("[");
  386. debug_dec(i);
  387. debug("]=");
  388. debug_record(waiting_buffer[i]);
  389. debug(" ");
  390. }
  391. debug("}\n");
  392. }
  393. #endif