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.

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