action_tapping.c 17 KB

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