You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
14 KiB

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