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 void waiting_buffer_scan_tap(void);
34 static void debug_tapping_key(void);
35 static void debug_waiting_buffer(void);
38 void action_tapping_process(keyrecord_t record)
40 if (process_tapping(&record)) {
41 if (!IS_NOEVENT(record.event)) {
42 debug("processed: "); debug_record(record); debug("\n");
45 if (!waiting_buffer_enq(record)) {
46 // clear all in case of overflow.
47 debug("OVERFLOW: CLEAR ALL STATES\n");
49 waiting_buffer_clear();
50 tapping_key = (keyrecord_t){};
54 // process waiting_buffer
55 if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
56 debug("---- action_exec: process waiting_buffer -----\n");
58 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
59 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
60 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
61 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
66 if (!IS_NOEVENT(record.event)) {
74 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
75 * (without interfering by typing other key)
77 /* return true when key event is processed or consumed. */
78 bool process_tapping(keyrecord_t *keyp)
80 keyevent_t event = keyp->event;
83 if (IS_TAPPING_PRESSED()) {
84 if (WITHIN_TAPPING_TERM(event)) {
85 if (tapping_key.tap.count == 0) {
86 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
88 debug("Tapping: First tap(0->1).\n");
89 tapping_key.tap.count = 1;
91 process_action(&tapping_key);
94 keyp->tap = tapping_key.tap;
98 #if TAPPING_TERM >= 500
99 /* Process a key typed within TAPPING_TERM
100 * This can register the key before settlement of tapping,
101 * useful for long TAPPING_TERM but may prevent fast typing.
103 else if (IS_RELEASED(event) && waiting_buffer_typed(event)) {
104 debug("Tapping: End. No tap. Interfered by typing key\n");
105 process_action(&tapping_key);
106 tapping_key = (keyrecord_t){};
112 /* Process release event of a key pressed before tapping starts
113 * Without this unexpected repeating will occur with having fast repeating setting
114 * https://github.com/tmk/tmk_keyboard/issues/60
116 else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) {
117 // Modifier should be retained till end of this tapping.
118 action_t action = layer_switch_get_action(event.key);
119 switch (action.kind.id) {
122 if (action.key.mods && !action.key.code) return false;
123 if (IS_MOD(action.key.code)) return false;
127 if (action.key.mods && keyp->tap.count == 0) return false;
128 if (IS_MOD(action.key.code)) return false;
131 // Release of key should be process immediately.
132 debug("Tapping: release event of a key pressed before tapping\n");
133 process_action(keyp);
137 // set interrupted flag when other key preesed during tapping
139 tapping_key.tap.interrupted = true;
147 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
148 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
149 keyp->tap = tapping_key.tap;
150 process_action(keyp);
155 else if (is_tap_key(event.key) && event.pressed) {
156 if (tapping_key.tap.count > 1) {
157 debug("Tapping: Start new tap with releasing last tap(>1).\n");
159 process_action(&(keyrecord_t){
160 .tap = tapping_key.tap,
161 .event.key = tapping_key.event.key,
162 .event.time = event.time,
163 .event.pressed = false
166 debug("Tapping: Start while last tap(1).\n");
169 waiting_buffer_scan_tap();
174 if (!IS_NOEVENT(event)) {
175 debug("Tapping: key event while last tap(>0).\n");
177 process_action(keyp);
182 // after TAPPING_TERM
184 if (tapping_key.tap.count == 0) {
185 debug("Tapping: End. Timeout. Not tap(0): ");
186 debug_event(event); debug("\n");
187 process_action(&tapping_key);
188 tapping_key = (keyrecord_t){};
192 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
193 debug("Tapping: End. last timeout tap release(>0).");
194 keyp->tap = tapping_key.tap;
195 process_action(keyp);
196 tapping_key = (keyrecord_t){};
199 else if (is_tap_key(event.key) && event.pressed) {
200 if (tapping_key.tap.count > 1) {
201 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
203 process_action(&(keyrecord_t){
204 .tap = tapping_key.tap,
205 .event.key = tapping_key.event.key,
206 .event.time = event.time,
207 .event.pressed = false
210 debug("Tapping: Start while last timeout tap(1).\n");
213 waiting_buffer_scan_tap();
218 if (!IS_NOEVENT(event)) {
219 debug("Tapping: key event while last timeout tap(>0).\n");
221 process_action(keyp);
226 } else if (IS_TAPPING_RELEASED()) {
227 if (WITHIN_TAPPING_TERM(event)) {
229 if (IS_TAPPING_KEY(event.key)) {
230 if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
232 keyp->tap = tapping_key.tap;
233 if (keyp->tap.count < 15) keyp->tap.count += 1;
234 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
235 process_action(keyp);
240 // FIX: start new tap again
244 } else if (is_tap_key(event.key)) {
245 // Sequential tap can be interfered with other tap key.
246 debug("Tapping: Start with interfering other tap.\n");
248 waiting_buffer_scan_tap();
252 // should none in buffer
253 // FIX: interrupted when other key is pressed
254 tapping_key.tap.interrupted = true;
255 process_action(keyp);
259 if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
260 process_action(keyp);
264 // FIX: process_aciton here?
265 // timeout. no sequential tap.
266 debug("Tapping: End(Timeout after releasing last tap): ");
267 debug_event(event); debug("\n");
268 tapping_key = (keyrecord_t){};
275 if (event.pressed && is_tap_key(event.key)) {
276 debug("Tapping: Start(Press tap key).\n");
278 waiting_buffer_scan_tap();
282 process_action(keyp);
292 bool waiting_buffer_enq(keyrecord_t record)
294 if (IS_NOEVENT(record.event)) {
298 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
299 debug("waiting_buffer_enq: Over flow.\n");
303 waiting_buffer[waiting_buffer_head] = record;
304 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
306 debug("waiting_buffer_enq: "); debug_waiting_buffer();
310 void waiting_buffer_clear(void)
312 waiting_buffer_head = 0;
313 waiting_buffer_tail = 0;
316 bool waiting_buffer_typed(keyevent_t event)
318 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
319 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
326 /* scan buffer for tapping */
327 void waiting_buffer_scan_tap(void)
329 // tapping already is settled
330 if (tapping_key.tap.count > 0) return;
331 // invalid state: tapping_key released && tap.count == 0
332 if (!tapping_key.event.pressed) return;
334 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
335 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
336 !waiting_buffer[i].event.pressed &&
337 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
338 tapping_key.tap.count = 1;
339 waiting_buffer[i].tap.count = 1;
340 process_action(&tapping_key);
342 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
343 debug_waiting_buffer();
353 static void debug_tapping_key(void)
355 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
358 static void debug_waiting_buffer(void)
361 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
362 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");