action_tapping.c 15 KB

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