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))
23 uint16_t get_tapping_term(uint16_t keycode) {
27 #ifdef TAPPING_TERM_PER_KEY
28 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event)))
30 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
33 static keyrecord_t tapping_key = {};
34 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
35 static uint8_t waiting_buffer_head = 0;
36 static uint8_t waiting_buffer_tail = 0;
38 static bool process_tapping(keyrecord_t *record);
39 static bool waiting_buffer_enq(keyrecord_t record);
40 static void waiting_buffer_clear(void);
41 static bool waiting_buffer_typed(keyevent_t event);
42 static bool waiting_buffer_has_anykey_pressed(void);
43 static void waiting_buffer_scan_tap(void);
44 static void debug_tapping_key(void);
45 static void debug_waiting_buffer(void);
48 /** \brief Action Tapping Process
52 void action_tapping_process(keyrecord_t record)
54 if (process_tapping(&record)) {
55 if (!IS_NOEVENT(record.event)) {
56 debug("processed: "); debug_record(record); debug("\n");
59 if (!waiting_buffer_enq(record)) {
60 // clear all in case of overflow.
61 debug("OVERFLOW: CLEAR ALL STATES\n");
63 waiting_buffer_clear();
64 tapping_key = (keyrecord_t){};
68 // process waiting_buffer
69 if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
70 debug("---- action_exec: process waiting_buffer -----\n");
72 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
73 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
74 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
75 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
80 if (!IS_NOEVENT(record.event)) {
88 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
89 * (without interfering by typing other key)
91 /* return true when key event is processed or consumed. */
92 bool process_tapping(keyrecord_t *keyp)
94 keyevent_t event = keyp->event;
97 if (IS_TAPPING_PRESSED()) {
98 if (WITHIN_TAPPING_TERM(event)) {
99 if (tapping_key.tap.count == 0) {
100 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
102 debug("Tapping: First tap(0->1).\n");
103 tapping_key.tap.count = 1;
105 process_record(&tapping_key);
107 // copy tapping state
108 keyp->tap = tapping_key.tap;
112 /* Process a key typed within TAPPING_TERM
113 * This can register the key before settlement of tapping,
114 * useful for long TAPPING_TERM but may prevent fast typing.
116 #if defined(TAPPING_TERM_PER_KEY) || (!defined(PER_KEY_TAPPING_TERM) && TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD)
117 #ifdef TAPPING_TERM_PER_KEY
118 else if ( ( get_tapping_term(get_event_keycode(tapping_key.event)) >= 500) && IS_RELEASED(event) && waiting_buffer_typed(event))
120 else if ( IS_RELEASED(event) && waiting_buffer_typed(event))
123 debug("Tapping: End. No tap. Interfered by typing key\n");
124 process_record(&tapping_key);
125 tapping_key = (keyrecord_t){};
131 /* Process release event of a key pressed before tapping starts
132 * Without this unexpected repeating will occur with having fast repeating setting
133 * https://github.com/tmk/tmk_keyboard/issues/60
135 else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) {
136 // Modifier should be retained till end of this tapping.
137 action_t action = layer_switch_get_action(event.key);
138 switch (action.kind.id) {
141 if (action.key.mods && !action.key.code) return false;
142 if (IS_MOD(action.key.code)) return false;
146 if (action.key.mods && keyp->tap.count == 0) return false;
147 if (IS_MOD(action.key.code)) return false;
150 // Release of key should be process immediately.
151 debug("Tapping: release event of a key pressed before tapping\n");
152 process_record(keyp);
156 // set interrupted flag when other key preesed during tapping
158 tapping_key.tap.interrupted = true;
166 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
167 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
168 keyp->tap = tapping_key.tap;
169 process_record(keyp);
174 else if (is_tap_key(event.key) && event.pressed) {
175 if (tapping_key.tap.count > 1) {
176 debug("Tapping: Start new tap with releasing last tap(>1).\n");
178 process_record(&(keyrecord_t){
179 .tap = tapping_key.tap,
180 .event.key = tapping_key.event.key,
181 .event.time = event.time,
182 .event.pressed = false
185 debug("Tapping: Start while last tap(1).\n");
188 waiting_buffer_scan_tap();
193 if (!IS_NOEVENT(event)) {
194 debug("Tapping: key event while last tap(>0).\n");
196 process_record(keyp);
201 // after TAPPING_TERM
203 if (tapping_key.tap.count == 0) {
204 debug("Tapping: End. Timeout. Not tap(0): ");
205 debug_event(event); debug("\n");
206 process_record(&tapping_key);
207 tapping_key = (keyrecord_t){};
211 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
212 debug("Tapping: End. last timeout tap release(>0).");
213 keyp->tap = tapping_key.tap;
214 process_record(keyp);
215 tapping_key = (keyrecord_t){};
218 else if (is_tap_key(event.key) && event.pressed) {
219 if (tapping_key.tap.count > 1) {
220 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
222 process_record(&(keyrecord_t){
223 .tap = tapping_key.tap,
224 .event.key = tapping_key.event.key,
225 .event.time = event.time,
226 .event.pressed = false
229 debug("Tapping: Start while last timeout tap(1).\n");
232 waiting_buffer_scan_tap();
237 if (!IS_NOEVENT(event)) {
238 debug("Tapping: key event while last timeout tap(>0).\n");
240 process_record(keyp);
245 } else if (IS_TAPPING_RELEASED()) {
246 if (WITHIN_TAPPING_TERM(event)) {
248 if (IS_TAPPING_KEY(event.key)) {
249 #ifndef TAPPING_FORCE_HOLD
250 if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
252 keyp->tap = tapping_key.tap;
253 if (keyp->tap.count < 15) keyp->tap.count += 1;
254 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
255 process_record(keyp);
261 // FIX: start new tap again
264 } else if (is_tap_key(event.key)) {
265 // Sequential tap can be interfered with other tap key.
266 debug("Tapping: Start with interfering other tap.\n");
268 waiting_buffer_scan_tap();
272 // should none in buffer
273 // FIX: interrupted when other key is pressed
274 tapping_key.tap.interrupted = true;
275 process_record(keyp);
279 if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
280 process_record(keyp);
284 // FIX: process_action here?
285 // timeout. no sequential tap.
286 debug("Tapping: End(Timeout after releasing last tap): ");
287 debug_event(event); debug("\n");
288 tapping_key = (keyrecord_t){};
295 if (event.pressed && is_tap_key(event.key)) {
296 debug("Tapping: Start(Press tap key).\n");
298 process_record_tap_hint(&tapping_key);
299 waiting_buffer_scan_tap();
303 process_record(keyp);
310 /** \brief Waiting buffer enq
314 bool waiting_buffer_enq(keyrecord_t record)
316 if (IS_NOEVENT(record.event)) {
320 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
321 debug("waiting_buffer_enq: Over flow.\n");
325 waiting_buffer[waiting_buffer_head] = record;
326 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
328 debug("waiting_buffer_enq: "); debug_waiting_buffer();
332 /** \brief Waiting buffer clear
336 void waiting_buffer_clear(void)
338 waiting_buffer_head = 0;
339 waiting_buffer_tail = 0;
342 /** \brief Waiting buffer typed
346 bool waiting_buffer_typed(keyevent_t event)
348 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
349 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
356 /** \brief Waiting buffer has anykey pressed
360 __attribute__((unused))
361 bool waiting_buffer_has_anykey_pressed(void)
363 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
364 if (waiting_buffer[i].event.pressed) return true;
369 /** \brief Scan buffer for tapping
373 void waiting_buffer_scan_tap(void)
375 // tapping already is settled
376 if (tapping_key.tap.count > 0) return;
377 // invalid state: tapping_key released && tap.count == 0
378 if (!tapping_key.event.pressed) return;
380 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
381 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
382 !waiting_buffer[i].event.pressed &&
383 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
384 tapping_key.tap.count = 1;
385 waiting_buffer[i].tap.count = 1;
386 process_record(&tapping_key);
388 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
389 debug_waiting_buffer();
396 /** \brief Tapping key debug print
400 static void debug_tapping_key(void)
402 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
405 /** \brief Waiting buffer debug print
409 static void debug_waiting_buffer(void)
412 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
413 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");