action_tapping.c 15 KB

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