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 /** \brief Action Tapping Process
43 void action_tapping_process(keyrecord_t record)
45 if (process_tapping(&record)) {
46 if (!IS_NOEVENT(record.event)) {
47 debug("processed: "); debug_record(record); debug("\n");
50 if (!waiting_buffer_enq(record)) {
51 // clear all in case of overflow.
52 debug("OVERFLOW: CLEAR ALL STATES\n");
54 waiting_buffer_clear();
55 tapping_key = (keyrecord_t){};
59 // process waiting_buffer
60 if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
61 debug("---- action_exec: process waiting_buffer -----\n");
63 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
64 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
65 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
66 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
71 if (!IS_NOEVENT(record.event)) {
79 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
80 * (without interfering by typing other key)
82 /* return true when key event is processed or consumed. */
83 bool process_tapping(keyrecord_t *keyp)
85 keyevent_t event = keyp->event;
88 if (IS_TAPPING_PRESSED()) {
89 if (WITHIN_TAPPING_TERM(event)) {
90 if (tapping_key.tap.count == 0) {
91 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
93 debug("Tapping: First tap(0->1).\n");
94 tapping_key.tap.count = 1;
96 process_record(&tapping_key);
99 keyp->tap = tapping_key.tap;
103 #if TAPPING_TERM >= 500 || defined PERMISSIVE_HOLD
104 /* Process a key typed within TAPPING_TERM
105 * This can register the key before settlement of tapping,
106 * useful for long TAPPING_TERM but may prevent fast typing.
108 else if (IS_RELEASED(event) && waiting_buffer_typed(event)) {
109 debug("Tapping: End. No tap. Interfered by typing key\n");
110 process_record(&tapping_key);
111 tapping_key = (keyrecord_t){};
117 /* Process release event of a key pressed before tapping starts
118 * Without this unexpected repeating will occur with having fast repeating setting
119 * https://github.com/tmk/tmk_keyboard/issues/60
121 else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) {
122 // Modifier should be retained till end of this tapping.
123 action_t action = layer_switch_get_action(event.key);
124 switch (action.kind.id) {
127 if (action.key.mods && !action.key.code) return false;
128 if (IS_MOD(action.key.code)) return false;
132 if (action.key.mods && keyp->tap.count == 0) return false;
133 if (IS_MOD(action.key.code)) return false;
136 // Release of key should be process immediately.
137 debug("Tapping: release event of a key pressed before tapping\n");
138 process_record(keyp);
142 // set interrupted flag when other key preesed during tapping
144 tapping_key.tap.interrupted = true;
152 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
153 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
154 keyp->tap = tapping_key.tap;
155 process_record(keyp);
160 else if (is_tap_key(event.key) && event.pressed) {
161 if (tapping_key.tap.count > 1) {
162 debug("Tapping: Start new tap with releasing last tap(>1).\n");
164 process_record(&(keyrecord_t){
165 .tap = tapping_key.tap,
166 .event.key = tapping_key.event.key,
167 .event.time = event.time,
168 .event.pressed = false
171 debug("Tapping: Start while last tap(1).\n");
174 waiting_buffer_scan_tap();
179 if (!IS_NOEVENT(event)) {
180 debug("Tapping: key event while last tap(>0).\n");
182 process_record(keyp);
187 // after TAPPING_TERM
189 if (tapping_key.tap.count == 0) {
190 debug("Tapping: End. Timeout. Not tap(0): ");
191 debug_event(event); debug("\n");
192 process_record(&tapping_key);
193 tapping_key = (keyrecord_t){};
197 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
198 debug("Tapping: End. last timeout tap release(>0).");
199 keyp->tap = tapping_key.tap;
200 process_record(keyp);
201 tapping_key = (keyrecord_t){};
204 else if (is_tap_key(event.key) && event.pressed) {
205 if (tapping_key.tap.count > 1) {
206 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
208 process_record(&(keyrecord_t){
209 .tap = tapping_key.tap,
210 .event.key = tapping_key.event.key,
211 .event.time = event.time,
212 .event.pressed = false
215 debug("Tapping: Start while last timeout tap(1).\n");
218 waiting_buffer_scan_tap();
223 if (!IS_NOEVENT(event)) {
224 debug("Tapping: key event while last timeout tap(>0).\n");
226 process_record(keyp);
231 } else if (IS_TAPPING_RELEASED()) {
232 if (WITHIN_TAPPING_TERM(event)) {
234 if (IS_TAPPING_KEY(event.key)) {
235 #ifndef TAPPING_FORCE_HOLD
236 if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
238 keyp->tap = tapping_key.tap;
239 if (keyp->tap.count < 15) keyp->tap.count += 1;
240 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
241 process_record(keyp);
247 // FIX: start new tap again
250 } else if (is_tap_key(event.key)) {
251 // Sequential tap can be interfered with other tap key.
252 debug("Tapping: Start with interfering other tap.\n");
254 waiting_buffer_scan_tap();
258 // should none in buffer
259 // FIX: interrupted when other key is pressed
260 tapping_key.tap.interrupted = true;
261 process_record(keyp);
265 if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
266 process_record(keyp);
270 // FIX: process_action here?
271 // timeout. no sequential tap.
272 debug("Tapping: End(Timeout after releasing last tap): ");
273 debug_event(event); debug("\n");
274 tapping_key = (keyrecord_t){};
281 if (event.pressed && is_tap_key(event.key)) {
282 debug("Tapping: Start(Press tap key).\n");
284 process_record_tap_hint(&tapping_key);
285 waiting_buffer_scan_tap();
289 process_record(keyp);
296 /** \brief Waiting buffer enq
300 bool waiting_buffer_enq(keyrecord_t record)
302 if (IS_NOEVENT(record.event)) {
306 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
307 debug("waiting_buffer_enq: Over flow.\n");
311 waiting_buffer[waiting_buffer_head] = record;
312 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
314 debug("waiting_buffer_enq: "); debug_waiting_buffer();
318 /** \brief Waiting buffer clear
322 void waiting_buffer_clear(void)
324 waiting_buffer_head = 0;
325 waiting_buffer_tail = 0;
328 /** \brief Waiting buffer typed
332 bool waiting_buffer_typed(keyevent_t event)
334 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
335 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
342 /** \brief Waiting buffer has anykey pressed
346 __attribute__((unused))
347 bool waiting_buffer_has_anykey_pressed(void)
349 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
350 if (waiting_buffer[i].event.pressed) return true;
355 /** \brief Scan buffer for tapping
359 void waiting_buffer_scan_tap(void)
361 // tapping already is settled
362 if (tapping_key.tap.count > 0) return;
363 // invalid state: tapping_key released && tap.count == 0
364 if (!tapping_key.event.pressed) return;
366 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
367 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
368 !waiting_buffer[i].event.pressed &&
369 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
370 tapping_key.tap.count = 1;
371 waiting_buffer[i].tap.count = 1;
372 process_record(&tapping_key);
374 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
375 debug_waiting_buffer();
382 /** \brief Tapping key debug print
386 static void debug_tapping_key(void)
388 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
391 /** \brief Waiting buffer debug print
395 static void debug_waiting_buffer(void)
398 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
399 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");