4 #include "action_tapping.h"
9 #ifndef NO_ACTION_TAPPING
11 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
12 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
13 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
14 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
15 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
18 static keyrecord_t tapping_key
= {};
19 static keyrecord_t waiting_buffer
[WAITING_BUFFER_SIZE
] = {};
20 static uint8_t waiting_buffer_head
= 0;
21 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 #if TAPPING_TERM >= 500
27 static bool waiting_buffer_typed(keyevent_t event
);
29 static bool waiting_buffer_has_anykey_pressed(void);
30 static void waiting_buffer_scan_tap(void);
31 static void debug_tapping_key(void);
32 static void debug_waiting_buffer(void);
35 void action_tapping_process(keyrecord_t record
)
37 if (process_tapping(&record
)) {
38 if (!IS_NOEVENT(record
.event
)) {
39 debug("processed: "); debug_record(record
); debug("\n");
42 if (!waiting_buffer_enq(record
)) {
43 // clear all in case of overflow.
44 debug("OVERFLOW: CLEAR ALL STATES\n");
46 waiting_buffer_clear();
47 tapping_key
= (keyrecord_t
){};
51 // process waiting_buffer
52 if (!IS_NOEVENT(record
.event
) && waiting_buffer_head
!= waiting_buffer_tail
) {
53 debug("---- action_exec: process waiting_buffer -----\n");
55 for (; waiting_buffer_tail
!= waiting_buffer_head
; waiting_buffer_tail
= (waiting_buffer_tail
+ 1) % WAITING_BUFFER_SIZE
) {
56 if (process_tapping(&waiting_buffer
[waiting_buffer_tail
])) {
57 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail
); debug("] = ");
58 debug_record(waiting_buffer
[waiting_buffer_tail
]); debug("\n\n");
63 if (!IS_NOEVENT(record
.event
)) {
71 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
72 * (without interfering by typing other key)
74 /* return true when key event is processed or consumed. */
75 bool process_tapping(keyrecord_t
*keyp
)
77 keyevent_t event
= keyp
->event
;
80 if (IS_TAPPING_PRESSED()) {
81 if (WITHIN_TAPPING_TERM(event
)) {
82 if (tapping_key
.tap
.count
== 0) {
83 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
85 debug("Tapping: First tap(0->1).\n");
86 tapping_key
.tap
.count
= 1;
87 tapping_key
.tap
.interrupted
= (waiting_buffer_has_anykey_pressed() ? true : false);
89 process_action(&tapping_key
);
92 keyp
->tap
= tapping_key
.tap
;
95 #if TAPPING_TERM >= 500
96 /* This can prevent from typing some tap keys in a row at a time. */
97 else if (!event
.pressed
&& waiting_buffer_typed(event
)) {
98 // other key typed. not tap.
99 debug("Tapping: End. No tap. Interfered by typing key\n");
100 process_action(&tapping_key
);
101 tapping_key
= (keyrecord_t
){};
109 // other key events shall be enq'd till tapping state settles.
115 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
116 debug("Tapping: Tap release("); debug_dec(tapping_key
.tap
.count
); debug(")\n");
117 keyp
->tap
= tapping_key
.tap
;
118 process_action(keyp
);
123 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
124 if (tapping_key
.tap
.count
> 1) {
125 debug("Tapping: Start new tap with releasing last tap(>1).\n");
127 process_action(&(keyrecord_t
){
128 .tap
= tapping_key
.tap
,
129 .event
.key
= tapping_key
.event
.key
,
130 .event
.time
= event
.time
,
131 .event
.pressed
= false
134 debug("Tapping: Start while last tap(1).\n");
137 waiting_buffer_scan_tap();
142 if (!IS_NOEVENT(keyp
->event
)) {
143 debug("Tapping: key event while last tap(>0).\n");
145 process_action(keyp
);
150 // after TAPPING_TERM
152 if (tapping_key
.tap
.count
== 0) {
153 debug("Tapping: End. Timeout. Not tap(0): ");
154 debug_event(event
); debug("\n");
155 process_action(&tapping_key
);
156 tapping_key
= (keyrecord_t
){};
160 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
161 debug("Tapping: End. last timeout tap release(>0).");
162 keyp
->tap
= tapping_key
.tap
;
163 process_action(keyp
);
164 tapping_key
= (keyrecord_t
){};
167 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
168 if (tapping_key
.tap
.count
> 1) {
169 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
171 process_action(&(keyrecord_t
){
172 .tap
= tapping_key
.tap
,
173 .event
.key
= tapping_key
.event
.key
,
174 .event
.time
= event
.time
,
175 .event
.pressed
= false
178 debug("Tapping: Start while last timeout tap(1).\n");
181 waiting_buffer_scan_tap();
186 if (!IS_NOEVENT(keyp
->event
)) {
187 debug("Tapping: key event while last timeout tap(>0).\n");
189 process_action(keyp
);
194 } else if (IS_TAPPING_RELEASED()) {
195 if (WITHIN_TAPPING_TERM(event
)) {
196 if (tapping_key
.tap
.count
> 0 && IS_TAPPING_KEY(event
.key
) && event
.pressed
) {
198 keyp
->tap
= tapping_key
.tap
;
199 keyp
->tap
.count
+= 1;
200 debug("Tapping: Tap press("); debug_dec(keyp
->tap
.count
); debug(")\n");
201 process_action(keyp
);
205 } else if (event
.pressed
&& is_tap_key(event
.key
)) {
206 // Sequential tap can be interfered with other tap key.
207 debug("Tapping: Start with interfering other tap.\n");
209 waiting_buffer_scan_tap();
213 if (!IS_NOEVENT(keyp
->event
)) debug("Tapping: other key just after tap.\n");
214 process_action(keyp
);
218 // timeout. no sequential tap.
219 debug("Tapping: End(Timeout after releasing last tap): ");
220 debug_event(event
); debug("\n");
221 tapping_key
= (keyrecord_t
){};
226 // not tapping satate
228 if (event
.pressed
&& is_tap_key(event
.key
)) {
229 debug("Tapping: Start(Press tap key).\n");
231 waiting_buffer_scan_tap();
235 process_action(keyp
);
245 bool waiting_buffer_enq(keyrecord_t record
)
247 if (IS_NOEVENT(record
.event
)) {
251 if ((waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
== waiting_buffer_tail
) {
252 debug("waiting_buffer_enq: Over flow.\n");
256 waiting_buffer
[waiting_buffer_head
] = record
;
257 waiting_buffer_head
= (waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
;
259 debug("waiting_buffer_enq: "); debug_waiting_buffer();
263 void waiting_buffer_clear(void)
265 waiting_buffer_head
= 0;
266 waiting_buffer_tail
= 0;
269 #if TAPPING_TERM >= 500
270 bool waiting_buffer_typed(keyevent_t event
)
272 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
273 if (KEYEQ(event
.key
, waiting_buffer
[i
].event
.key
) && event
.pressed
!= waiting_buffer
[i
].event
.pressed
) {
281 bool waiting_buffer_has_anykey_pressed(void)
283 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
284 if (waiting_buffer
[i
].event
.pressed
) return true;
289 /* scan buffer for tapping */
290 void waiting_buffer_scan_tap(void)
292 // tapping already is settled
293 if (tapping_key
.tap
.count
> 0) return;
294 // invalid state: tapping_key released && tap.count == 0
295 if (!tapping_key
.event
.pressed
) return;
297 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
298 if (IS_TAPPING_KEY(waiting_buffer
[i
].event
.key
) &&
299 !waiting_buffer
[i
].event
.pressed
&&
300 WITHIN_TAPPING_TERM(waiting_buffer
[i
].event
)) {
301 tapping_key
.tap
.count
= 1;
302 waiting_buffer
[i
].tap
.count
= 1;
303 process_action(&tapping_key
);
305 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i
); debug("]\n");
306 debug_waiting_buffer();
316 static void debug_tapping_key(void)
318 debug("TAPPING_KEY="); debug_record(tapping_key
); debug("\n");
321 static void debug_waiting_buffer(void)
324 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
325 debug("["); debug_dec(i
); debug("]="); debug_record(waiting_buffer
[i
]); debug(" ");