action_tapping.c 15 KB

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