4 #include "action_tapping.h"
13 #ifndef NO_ACTION_TAPPING
15 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
16 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
17 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
18 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
19 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
22 static keyrecord_t tapping_key
= {};
23 static keyrecord_t waiting_buffer
[WAITING_BUFFER_SIZE
] = {};
24 static uint8_t waiting_buffer_head
= 0;
25 static uint8_t waiting_buffer_tail
= 0;
27 static bool process_tapping(keyrecord_t
*record
);
28 static bool waiting_buffer_enq(keyrecord_t record
);
29 static void waiting_buffer_clear(void);
30 static bool waiting_buffer_typed(keyevent_t event
);
31 static bool waiting_buffer_has_anykey_pressed(void);
32 static void waiting_buffer_scan_tap(void);
33 static void debug_tapping_key(void);
34 static void debug_waiting_buffer(void);
37 void action_tapping_process(keyrecord_t record
)
39 if (process_tapping(&record
)) {
40 if (!IS_NOEVENT(record
.event
)) {
41 debug("processed: "); debug_record(record
); debug("\n");
44 if (!waiting_buffer_enq(record
)) {
45 // clear all in case of overflow.
46 debug("OVERFLOW: CLEAR ALL STATES\n");
48 waiting_buffer_clear();
49 tapping_key
= (keyrecord_t
){};
53 // process waiting_buffer
54 if (!IS_NOEVENT(record
.event
) && waiting_buffer_head
!= waiting_buffer_tail
) {
55 debug("---- action_exec: process waiting_buffer -----\n");
57 for (; waiting_buffer_tail
!= waiting_buffer_head
; waiting_buffer_tail
= (waiting_buffer_tail
+ 1) % WAITING_BUFFER_SIZE
) {
58 if (process_tapping(&waiting_buffer
[waiting_buffer_tail
])) {
59 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail
); debug("] = ");
60 debug_record(waiting_buffer
[waiting_buffer_tail
]); debug("\n\n");
65 if (!IS_NOEVENT(record
.event
)) {
73 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
74 * (without interfering by typing other key)
76 /* return true when key event is processed or consumed. */
77 bool process_tapping(keyrecord_t
*keyp
)
79 keyevent_t event
= keyp
->event
;
82 if (IS_TAPPING_PRESSED()) {
83 if (WITHIN_TAPPING_TERM(event
)) {
84 if (tapping_key
.tap
.count
== 0) {
85 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
87 debug("Tapping: First tap(0->1).\n");
88 tapping_key
.tap
.count
= 1;
90 process_action(&tapping_key
);
93 keyp
->tap
= tapping_key
.tap
;
97 #if TAPPING_TERM >= 500
98 /* This can settle mod/fn state fast but may prevent from typing fast. */
99 else if (!event
.pressed
&& waiting_buffer_typed(event
)) {
100 // other key typed. not tap.
101 debug("Tapping: End. No tap. Interfered by typing key\n");
102 process_action(&tapping_key
);
103 tapping_key
= (keyrecord_t
){};
110 /* release a key pressed before tapping */
111 else if (!event
.pressed
&& !waiting_buffer_typed(event
)) {
112 /* Unexpected repeating occurs unless this event is processed immedately. */
113 debug("Tapping: release a key pressed before tapping\n");
114 process_action(keyp
);
118 // set interrupted flag when other key preesed during tapping
120 tapping_key
.tap
.interrupted
= true;
128 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
129 debug("Tapping: Tap release("); debug_dec(tapping_key
.tap
.count
); debug(")\n");
130 keyp
->tap
= tapping_key
.tap
;
131 process_action(keyp
);
136 else if (is_tap_key(event
.key
) && event
.pressed
) {
137 if (tapping_key
.tap
.count
> 1) {
138 debug("Tapping: Start new tap with releasing last tap(>1).\n");
140 process_action(&(keyrecord_t
){
141 .tap
= tapping_key
.tap
,
142 .event
.key
= tapping_key
.event
.key
,
143 .event
.time
= event
.time
,
144 .event
.pressed
= false
147 debug("Tapping: Start while last tap(1).\n");
150 waiting_buffer_scan_tap();
155 if (!IS_NOEVENT(event
)) {
156 debug("Tapping: key event while last tap(>0).\n");
158 process_action(keyp
);
163 // after TAPPING_TERM
165 if (tapping_key
.tap
.count
== 0) {
166 debug("Tapping: End. Timeout. Not tap(0): ");
167 debug_event(event
); debug("\n");
168 process_action(&tapping_key
);
169 tapping_key
= (keyrecord_t
){};
173 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
174 debug("Tapping: End. last timeout tap release(>0).");
175 keyp
->tap
= tapping_key
.tap
;
176 process_action(keyp
);
177 tapping_key
= (keyrecord_t
){};
180 else if (is_tap_key(event
.key
) && event
.pressed
) {
181 if (tapping_key
.tap
.count
> 1) {
182 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
184 process_action(&(keyrecord_t
){
185 .tap
= tapping_key
.tap
,
186 .event
.key
= tapping_key
.event
.key
,
187 .event
.time
= event
.time
,
188 .event
.pressed
= false
191 debug("Tapping: Start while last timeout tap(1).\n");
194 waiting_buffer_scan_tap();
199 if (!IS_NOEVENT(event
)) {
200 debug("Tapping: key event while last timeout tap(>0).\n");
202 process_action(keyp
);
207 } else if (IS_TAPPING_RELEASED()) {
208 if (WITHIN_TAPPING_TERM(event
)) {
210 if (IS_TAPPING_KEY(event
.key
)) {
211 if (!tapping_key
.tap
.interrupted
&& tapping_key
.tap
.count
> 0) {
213 keyp
->tap
= tapping_key
.tap
;
214 if (keyp
->tap
.count
< 15) keyp
->tap
.count
+= 1;
215 debug("Tapping: Tap press("); debug_dec(keyp
->tap
.count
); debug(")\n");
216 process_action(keyp
);
221 // FIX: start new tap again
225 } else if (is_tap_key(event
.key
)) {
226 // Sequential tap can be interfered with other tap key.
227 debug("Tapping: Start with interfering other tap.\n");
229 waiting_buffer_scan_tap();
233 // should none in buffer
234 // FIX: interrupted when other key is pressed
235 tapping_key
.tap
.interrupted
= true;
236 process_action(keyp
);
240 if (!IS_NOEVENT(event
)) debug("Tapping: other key just after tap.\n");
241 process_action(keyp
);
245 // FIX: process_aciton here?
246 // timeout. no sequential tap.
247 debug("Tapping: End(Timeout after releasing last tap): ");
248 debug_event(event
); debug("\n");
249 tapping_key
= (keyrecord_t
){};
256 if (event
.pressed
&& is_tap_key(event
.key
)) {
257 debug("Tapping: Start(Press tap key).\n");
259 waiting_buffer_scan_tap();
263 process_action(keyp
);
273 bool waiting_buffer_enq(keyrecord_t record
)
275 if (IS_NOEVENT(record
.event
)) {
279 if ((waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
== waiting_buffer_tail
) {
280 debug("waiting_buffer_enq: Over flow.\n");
284 waiting_buffer
[waiting_buffer_head
] = record
;
285 waiting_buffer_head
= (waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
;
287 debug("waiting_buffer_enq: "); debug_waiting_buffer();
291 void waiting_buffer_clear(void)
293 waiting_buffer_head
= 0;
294 waiting_buffer_tail
= 0;
297 bool waiting_buffer_typed(keyevent_t event
)
299 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
300 if (KEYEQ(event
.key
, waiting_buffer
[i
].event
.key
) && event
.pressed
!= waiting_buffer
[i
].event
.pressed
) {
307 bool waiting_buffer_has_anykey_pressed(void)
309 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
310 if (waiting_buffer
[i
].event
.pressed
) return true;
315 /* scan buffer for tapping */
316 void waiting_buffer_scan_tap(void)
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;
323 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
324 if (IS_TAPPING_KEY(waiting_buffer
[i
].event
.key
) &&
325 !waiting_buffer
[i
].event
.pressed
&&
326 WITHIN_TAPPING_TERM(waiting_buffer
[i
].event
)) {
327 tapping_key
.tap
.count
= 1;
328 waiting_buffer
[i
].tap
.count
= 1;
329 process_action(&tapping_key
);
331 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i
); debug("]\n");
332 debug_waiting_buffer();
342 static void debug_tapping_key(void)
344 debug("TAPPING_KEY="); debug_record(tapping_key
); debug("\n");
347 static void debug_waiting_buffer(void)
350 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
351 debug("["); debug_dec(i
); debug("]="); debug_record(waiting_buffer
[i
]); debug(" ");