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)))
22 __attribute__((weak
)) uint16_t get_tapping_term(uint16_t keycode
) { return TAPPING_TERM
; }
24 # ifdef TAPPING_TERM_PER_KEY
25 # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event)))
27 # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
30 # ifdef TAPPING_FORCE_HOLD_PER_KEY
31 __attribute__((weak
)) bool get_tapping_force_hold(uint16_t keycode
, keyrecord_t
*record
) { return false; }
34 # ifdef PERMISSIVE_HOLD_PER_KEY
35 __attribute__((weak
)) bool get_permissive_hold(uint16_t keycode
, keyrecord_t
*record
) { return false; }
38 static keyrecord_t tapping_key
= {};
39 static keyrecord_t waiting_buffer
[WAITING_BUFFER_SIZE
] = {};
40 static uint8_t waiting_buffer_head
= 0;
41 static uint8_t waiting_buffer_tail
= 0;
43 static bool process_tapping(keyrecord_t
*record
);
44 static bool waiting_buffer_enq(keyrecord_t record
);
45 static void waiting_buffer_clear(void);
46 static bool waiting_buffer_typed(keyevent_t event
);
47 static bool waiting_buffer_has_anykey_pressed(void);
48 static void waiting_buffer_scan_tap(void);
49 static void debug_tapping_key(void);
50 static void debug_waiting_buffer(void);
52 /** \brief Action Tapping Process
56 void action_tapping_process(keyrecord_t record
) {
57 if (process_tapping(&record
)) {
58 if (!IS_NOEVENT(record
.event
)) {
64 if (!waiting_buffer_enq(record
)) {
65 // clear all in case of overflow.
66 debug("OVERFLOW: CLEAR ALL STATES\n");
68 waiting_buffer_clear();
69 tapping_key
= (keyrecord_t
){};
73 // process waiting_buffer
74 if (!IS_NOEVENT(record
.event
) && waiting_buffer_head
!= waiting_buffer_tail
) {
75 debug("---- action_exec: process waiting_buffer -----\n");
77 for (; waiting_buffer_tail
!= waiting_buffer_head
; waiting_buffer_tail
= (waiting_buffer_tail
+ 1) % WAITING_BUFFER_SIZE
) {
78 if (process_tapping(&waiting_buffer
[waiting_buffer_tail
])) {
79 debug("processed: waiting_buffer[");
80 debug_dec(waiting_buffer_tail
);
82 debug_record(waiting_buffer
[waiting_buffer_tail
]);
88 if (!IS_NOEVENT(record
.event
)) {
95 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
96 * (without interfering by typing other key)
98 /* return true when key event is processed or consumed. */
99 bool process_tapping(keyrecord_t
*keyp
) {
100 keyevent_t event
= keyp
->event
;
103 if (IS_TAPPING_PRESSED()) {
104 if (WITHIN_TAPPING_TERM(event
)) {
105 if (tapping_key
.tap
.count
== 0) {
106 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
108 debug("Tapping: First tap(0->1).\n");
109 tapping_key
.tap
.count
= 1;
111 process_record(&tapping_key
);
113 // copy tapping state
114 keyp
->tap
= tapping_key
.tap
;
118 /* Process a key typed within TAPPING_TERM
119 * This can register the key before settlement of tapping,
120 * useful for long TAPPING_TERM but may prevent fast typing.
122 # if defined(TAPPING_TERM_PER_KEY) || (TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) || defined(PERMISSIVE_HOLD_PER_KEY)
124 # ifdef TAPPING_TERM_PER_KEY
125 (get_tapping_term(get_event_keycode(tapping_key
.event
)) >= 500) &&
127 # ifdef PERMISSIVE_HOLD_PER_KEY
128 !get_permissive_hold(get_event_keycode(tapping_key
.event
), keyp
) &&
130 IS_RELEASED(event
) && waiting_buffer_typed(event
)) {
131 debug("Tapping: End. No tap. Interfered by typing key\n");
132 process_record(&tapping_key
);
133 tapping_key
= (keyrecord_t
){};
139 /* Process release event of a key pressed before tapping starts
140 * Without this unexpected repeating will occur with having fast repeating setting
141 * https://github.com/tmk/tmk_keyboard/issues/60
143 else if (IS_RELEASED(event
) && !waiting_buffer_typed(event
)) {
144 // Modifier should be retained till end of this tapping.
145 action_t action
= layer_switch_get_action(event
.key
);
146 switch (action
.kind
.id
) {
149 if (action
.key
.mods
&& !action
.key
.code
) return false;
150 if (IS_MOD(action
.key
.code
)) return false;
154 if (action
.key
.mods
&& keyp
->tap
.count
== 0) return false;
155 if (IS_MOD(action
.key
.code
)) return false;
158 // Release of key should be process immediately.
159 debug("Tapping: release event of a key pressed before tapping\n");
160 process_record(keyp
);
163 // set interrupted flag when other key preesed during tapping
165 tapping_key
.tap
.interrupted
= true;
173 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
174 debug("Tapping: Tap release(");
175 debug_dec(tapping_key
.tap
.count
);
177 keyp
->tap
= tapping_key
.tap
;
178 process_record(keyp
);
182 } else if (is_tap_key(event
.key
) && event
.pressed
) {
183 if (tapping_key
.tap
.count
> 1) {
184 debug("Tapping: Start new tap with releasing last tap(>1).\n");
186 process_record(&(keyrecord_t
){.tap
= tapping_key
.tap
, .event
.key
= tapping_key
.event
.key
, .event
.time
= event
.time
, .event
.pressed
= false});
188 debug("Tapping: Start while last tap(1).\n");
191 waiting_buffer_scan_tap();
195 if (!IS_NOEVENT(event
)) {
196 debug("Tapping: key event while last tap(>0).\n");
198 process_record(keyp
);
203 // after TAPPING_TERM
205 if (tapping_key
.tap
.count
== 0) {
206 debug("Tapping: End. Timeout. Not tap(0): ");
209 process_record(&tapping_key
);
210 tapping_key
= (keyrecord_t
){};
214 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
215 debug("Tapping: End. last timeout tap release(>0).");
216 keyp
->tap
= tapping_key
.tap
;
217 process_record(keyp
);
218 tapping_key
= (keyrecord_t
){};
220 } else if (is_tap_key(event
.key
) && event
.pressed
) {
221 if (tapping_key
.tap
.count
> 1) {
222 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
224 process_record(&(keyrecord_t
){.tap
= tapping_key
.tap
, .event
.key
= tapping_key
.event
.key
, .event
.time
= event
.time
, .event
.pressed
= false});
226 debug("Tapping: Start while last timeout tap(1).\n");
229 waiting_buffer_scan_tap();
233 if (!IS_NOEVENT(event
)) {
234 debug("Tapping: key event while last timeout tap(>0).\n");
236 process_record(keyp
);
241 } else if (IS_TAPPING_RELEASED()) {
242 if (WITHIN_TAPPING_TERM(event
)) {
244 if (IS_TAPPING_KEY(event
.key
)) {
245 //# ifndef TAPPING_FORCE_HOLD
246 # if !defined(TAPPING_FORCE_HOLD) || defined(TAPPING_FORCE_HOLD_PER_KEY)
248 # ifdef TAPPING_FORCE_HOLD_PER_KEY
249 !get_tapping_force_hold(get_event_keycode(tapping_key
.event
), keyp
) &&
251 !tapping_key
.tap
.interrupted
&& tapping_key
.tap
.count
> 0) {
253 keyp
->tap
= tapping_key
.tap
;
254 if (keyp
->tap
.count
< 15) keyp
->tap
.count
+= 1;
255 debug("Tapping: Tap press(");
256 debug_dec(keyp
->tap
.count
);
258 process_record(keyp
);
264 // FIX: start new tap again
267 } else if (is_tap_key(event
.key
)) {
268 // Sequential tap can be interfered with other tap key.
269 debug("Tapping: Start with interfering other tap.\n");
271 waiting_buffer_scan_tap();
275 // should none in buffer
276 // FIX: interrupted when other key is pressed
277 tapping_key
.tap
.interrupted
= true;
278 process_record(keyp
);
282 if (!IS_NOEVENT(event
)) debug("Tapping: other key just after tap.\n");
283 process_record(keyp
);
287 // FIX: process_action here?
288 // timeout. no sequential tap.
289 debug("Tapping: End(Timeout after releasing last tap): ");
292 tapping_key
= (keyrecord_t
){};
299 if (event
.pressed
&& is_tap_key(event
.key
)) {
300 debug("Tapping: Start(Press tap key).\n");
302 process_record_tap_hint(&tapping_key
);
303 waiting_buffer_scan_tap();
307 process_record(keyp
);
313 /** \brief Waiting buffer enq
317 bool waiting_buffer_enq(keyrecord_t record
) {
318 if (IS_NOEVENT(record
.event
)) {
322 if ((waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
== waiting_buffer_tail
) {
323 debug("waiting_buffer_enq: Over flow.\n");
327 waiting_buffer
[waiting_buffer_head
] = record
;
328 waiting_buffer_head
= (waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
;
330 debug("waiting_buffer_enq: ");
331 debug_waiting_buffer();
335 /** \brief Waiting buffer clear
339 void waiting_buffer_clear(void) {
340 waiting_buffer_head
= 0;
341 waiting_buffer_tail
= 0;
344 /** \brief Waiting buffer typed
348 bool waiting_buffer_typed(keyevent_t event
) {
349 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
350 if (KEYEQ(event
.key
, waiting_buffer
[i
].event
.key
) && event
.pressed
!= waiting_buffer
[i
].event
.pressed
) {
357 /** \brief Waiting buffer has anykey pressed
361 __attribute__((unused
)) bool waiting_buffer_has_anykey_pressed(void) {
362 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
363 if (waiting_buffer
[i
].event
.pressed
) return true;
368 /** \brief Scan buffer for tapping
372 void waiting_buffer_scan_tap(void) {
373 // tapping already is settled
374 if (tapping_key
.tap
.count
> 0) return;
375 // invalid state: tapping_key released && tap.count == 0
376 if (!tapping_key
.event
.pressed
) return;
378 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
379 if (IS_TAPPING_KEY(waiting_buffer
[i
].event
.key
) && !waiting_buffer
[i
].event
.pressed
&& WITHIN_TAPPING_TERM(waiting_buffer
[i
].event
)) {
380 tapping_key
.tap
.count
= 1;
381 waiting_buffer
[i
].tap
.count
= 1;
382 process_record(&tapping_key
);
384 debug("waiting_buffer_scan_tap: found at [");
387 debug_waiting_buffer();
393 /** \brief Tapping key debug print
397 static void debug_tapping_key(void) {
398 debug("TAPPING_KEY=");
399 debug_record(tapping_key
);
403 /** \brief Waiting buffer debug print
407 static void debug_waiting_buffer(void) {
409 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
413 debug_record(waiting_buffer
[i
]);