4 #include "action_layer.h"
5 #include "action_tapping.h"
15 #ifndef NO_ACTION_TAPPING
17 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
18 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
19 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
20 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
21 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
24 static keyrecord_t tapping_key
= {};
25 static keyrecord_t waiting_buffer
[WAITING_BUFFER_SIZE
] = {};
26 static uint8_t waiting_buffer_head
= 0;
27 static uint8_t waiting_buffer_tail
= 0;
29 static bool process_tapping(keyrecord_t
*record
);
30 static bool waiting_buffer_enq(keyrecord_t record
);
31 static void waiting_buffer_clear(void);
32 static bool waiting_buffer_typed(keyevent_t event
);
33 static bool waiting_buffer_has_anykey_pressed(void);
34 static void waiting_buffer_scan_tap(void);
35 static void debug_tapping_key(void);
36 static void debug_waiting_buffer(void);
39 void action_tapping_process(keyrecord_t record
)
41 if (process_tapping(&record
)) {
42 if (!IS_NOEVENT(record
.event
)) {
43 debug("processed: "); debug_record(record
); debug("\n");
46 if (!waiting_buffer_enq(record
)) {
47 // clear all in case of overflow.
48 debug("OVERFLOW: CLEAR ALL STATES\n");
50 waiting_buffer_clear();
51 tapping_key
= (keyrecord_t
){};
55 // process waiting_buffer
56 if (!IS_NOEVENT(record
.event
) && waiting_buffer_head
!= waiting_buffer_tail
) {
57 debug("---- action_exec: process waiting_buffer -----\n");
59 for (; waiting_buffer_tail
!= waiting_buffer_head
; waiting_buffer_tail
= (waiting_buffer_tail
+ 1) % WAITING_BUFFER_SIZE
) {
60 if (process_tapping(&waiting_buffer
[waiting_buffer_tail
])) {
61 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail
); debug("] = ");
62 debug_record(waiting_buffer
[waiting_buffer_tail
]); debug("\n\n");
67 if (!IS_NOEVENT(record
.event
)) {
75 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
76 * (without interfering by typing other key)
78 /* return true when key event is processed or consumed. */
79 bool process_tapping(keyrecord_t
*keyp
)
81 keyevent_t event
= keyp
->event
;
84 if (IS_TAPPING_PRESSED()) {
85 if (WITHIN_TAPPING_TERM(event
)) {
86 if (tapping_key
.tap
.count
== 0) {
87 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
89 debug("Tapping: First tap(0->1).\n");
90 tapping_key
.tap
.count
= 1;
92 process_action(&tapping_key
);
95 keyp
->tap
= tapping_key
.tap
;
99 #if TAPPING_TERM >= 500
100 /* Process a key typed within TAPPING_TERM
101 * This can register the key before settlement of tapping,
102 * useful for long TAPPING_TERM but may prevent fast typing.
104 else if (IS_RELEASED(event
) && waiting_buffer_typed(event
)) {
105 debug("Tapping: End. No tap. Interfered by typing key\n");
106 process_action(&tapping_key
);
107 tapping_key
= (keyrecord_t
){};
113 /* Process release event of a key pressed before tapping starts
114 * Without this unexpected repeating will occur with having fast repeating setting
115 * https://github.com/tmk/tmk_keyboard/issues/60
117 else if (IS_RELEASED(event
) && !waiting_buffer_typed(event
)) {
118 // Modifier should be retained till end of this tapping.
119 action_t action
= layer_switch_get_action(event
.key
);
120 switch (action
.kind
.id
) {
123 if (action
.key
.mods
&& !action
.key
.code
) return false;
124 if (IS_MOD(action
.key
.code
)) return false;
128 if (action
.key
.mods
&& keyp
->tap
.count
== 0) return false;
129 if (IS_MOD(action
.key
.code
)) return false;
132 // Release of key should be process immediately.
133 debug("Tapping: release event of a key pressed before tapping\n");
134 process_action(keyp
);
138 // set interrupted flag when other key preesed during tapping
140 tapping_key
.tap
.interrupted
= true;
148 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
149 debug("Tapping: Tap release("); debug_dec(tapping_key
.tap
.count
); debug(")\n");
150 keyp
->tap
= tapping_key
.tap
;
151 process_action(keyp
);
156 else if (is_tap_key(event
.key
) && event
.pressed
) {
157 if (tapping_key
.tap
.count
> 1) {
158 debug("Tapping: Start new tap with releasing last tap(>1).\n");
160 process_action(&(keyrecord_t
){
161 .tap
= tapping_key
.tap
,
162 .event
.key
= tapping_key
.event
.key
,
163 .event
.time
= event
.time
,
164 .event
.pressed
= false
167 debug("Tapping: Start while last tap(1).\n");
170 waiting_buffer_scan_tap();
175 if (!IS_NOEVENT(event
)) {
176 debug("Tapping: key event while last tap(>0).\n");
178 process_action(keyp
);
183 // after TAPPING_TERM
185 if (tapping_key
.tap
.count
== 0) {
186 debug("Tapping: End. Timeout. Not tap(0): ");
187 debug_event(event
); debug("\n");
188 process_action(&tapping_key
);
189 tapping_key
= (keyrecord_t
){};
193 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
194 debug("Tapping: End. last timeout tap release(>0).");
195 keyp
->tap
= tapping_key
.tap
;
196 process_action(keyp
);
197 tapping_key
= (keyrecord_t
){};
200 else if (is_tap_key(event
.key
) && event
.pressed
) {
201 if (tapping_key
.tap
.count
> 1) {
202 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
204 process_action(&(keyrecord_t
){
205 .tap
= tapping_key
.tap
,
206 .event
.key
= tapping_key
.event
.key
,
207 .event
.time
= event
.time
,
208 .event
.pressed
= false
211 debug("Tapping: Start while last timeout tap(1).\n");
214 waiting_buffer_scan_tap();
219 if (!IS_NOEVENT(event
)) {
220 debug("Tapping: key event while last timeout tap(>0).\n");
222 process_action(keyp
);
227 } else if (IS_TAPPING_RELEASED()) {
228 if (WITHIN_TAPPING_TERM(event
)) {
230 if (IS_TAPPING_KEY(event
.key
)) {
231 if (!tapping_key
.tap
.interrupted
&& tapping_key
.tap
.count
> 0) {
233 keyp
->tap
= tapping_key
.tap
;
234 if (keyp
->tap
.count
< 15) keyp
->tap
.count
+= 1;
235 debug("Tapping: Tap press("); debug_dec(keyp
->tap
.count
); debug(")\n");
236 process_action(keyp
);
241 // FIX: start new tap again
245 } else if (is_tap_key(event
.key
)) {
246 // Sequential tap can be interfered with other tap key.
247 debug("Tapping: Start with interfering other tap.\n");
249 waiting_buffer_scan_tap();
253 // should none in buffer
254 // FIX: interrupted when other key is pressed
255 tapping_key
.tap
.interrupted
= true;
256 process_action(keyp
);
260 if (!IS_NOEVENT(event
)) debug("Tapping: other key just after tap.\n");
261 process_action(keyp
);
265 // FIX: process_aciton here?
266 // timeout. no sequential tap.
267 debug("Tapping: End(Timeout after releasing last tap): ");
268 debug_event(event
); debug("\n");
269 tapping_key
= (keyrecord_t
){};
276 if (event
.pressed
&& is_tap_key(event
.key
)) {
277 debug("Tapping: Start(Press tap key).\n");
279 waiting_buffer_scan_tap();
283 process_action(keyp
);
293 bool waiting_buffer_enq(keyrecord_t record
)
295 if (IS_NOEVENT(record
.event
)) {
299 if ((waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
== waiting_buffer_tail
) {
300 debug("waiting_buffer_enq: Over flow.\n");
304 waiting_buffer
[waiting_buffer_head
] = record
;
305 waiting_buffer_head
= (waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
;
307 debug("waiting_buffer_enq: "); debug_waiting_buffer();
311 void waiting_buffer_clear(void)
313 waiting_buffer_head
= 0;
314 waiting_buffer_tail
= 0;
317 bool waiting_buffer_typed(keyevent_t event
)
319 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
320 if (KEYEQ(event
.key
, waiting_buffer
[i
].event
.key
) && event
.pressed
!= waiting_buffer
[i
].event
.pressed
) {
327 bool waiting_buffer_has_anykey_pressed(void)
329 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
330 if (waiting_buffer
[i
].event
.pressed
) return true;
335 /* scan buffer for tapping */
336 void waiting_buffer_scan_tap(void)
338 // tapping already is settled
339 if (tapping_key
.tap
.count
> 0) return;
340 // invalid state: tapping_key released && tap.count == 0
341 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_action(&tapping_key
);
351 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i
); debug("]\n");
352 debug_waiting_buffer();
362 static void debug_tapping_key(void)
364 debug("TAPPING_KEY="); debug_record(tapping_key
); debug("\n");
367 static void debug_waiting_buffer(void)
370 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
371 debug("["); debug_dec(i
); debug("]="); debug_record(waiting_buffer
[i
]); debug(" ");