]> git.gir.st - tmk_keyboard.git/blob - common/action.c
Clean action.c
[tmk_keyboard.git] / common / action.c
1 /*
2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "host.h"
18 #include "timer.h"
19 #include "keymap.h"
20 #include "keycode.h"
21 #include "keyboard.h"
22 #include "mousekey.h"
23 #include "command.h"
24 #include "util.h"
25 #include "debug.h"
26 #include "led.h"
27 #include "layer_switch.h"
28 #include "action_oneshot.h"
29 #include "action_macro.h"
30 #include "action.h"
31
32
33 static void process_action(keyrecord_t *record);
34 static void debug_event(keyevent_t event);
35 static void debug_record(keyrecord_t record);
36 static void debug_action(action_t action);
37
38 #ifndef NO_ACTION_TAPPING
39 /*
40 * Tapping
41 */
42 /* period of tapping(ms) */
43 #ifndef TAPPING_TERM
44 #define TAPPING_TERM 200
45 #endif
46
47 /* tap count needed for toggling a feature */
48 #ifndef TAPPING_TOGGLE
49 #define TAPPING_TOGGLE 5
50 #endif
51
52 /* stores a key event of current tap. */
53 static keyrecord_t tapping_key = {};
54
55 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
56 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
57 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
58 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
59 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
60
61
62 /*
63 * Waiting buffer
64 *
65 * stores key events waiting for settling current tap.
66 */
67 #define WAITING_BUFFER_SIZE 8
68 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
69 /* point to empty cell to enq */
70 static uint8_t waiting_buffer_head = 0;
71 /* point to the oldest data cell to deq */
72 static uint8_t waiting_buffer_tail = 0;
73
74
75 static bool process_tapping(keyrecord_t *record);
76 static bool waiting_buffer_enq(keyrecord_t record);
77 static void waiting_buffer_clear(void);
78 #if TAPPING_TERM >= 500
79 static bool waiting_buffer_typed(keyevent_t event);
80 #endif
81 static void waiting_buffer_scan_tap(void);
82 static void debug_tapping_key(void);
83 static void debug_waiting_buffer(void);
84 #endif
85
86
87
88 void action_exec(keyevent_t event)
89 {
90 if (!IS_NOEVENT(event)) {
91 debug("\n---- action_exec: start -----\n");
92 debug("EVENT: "); debug_event(event); debug("\n");
93 }
94
95 keyrecord_t record = { .event = event };
96
97 #ifndef NO_ACTION_TAPPING
98 if (process_tapping(&record)) {
99 if (!IS_NOEVENT(record.event)) {
100 debug("processed: "); debug_record(record); debug("\n");
101 }
102 } else {
103 if (!waiting_buffer_enq(record)) {
104 // clear all in case of overflow.
105 debug("OVERFLOW: CLEAR ALL STATES\n");
106 clear_keyboard();
107 waiting_buffer_clear();
108 tapping_key = (keyrecord_t){};
109 }
110 }
111
112 // process waiting_buffer
113 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
114 debug("---- action_exec: process waiting_buffer -----\n");
115 }
116 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
117 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
118 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
119 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
120 } else {
121 break;
122 }
123 }
124 if (!IS_NOEVENT(event)) {
125 debug("\n");
126 }
127 #else
128 process_action(&record);
129 if (!IS_NOEVENT(record.event)) {
130 debug("processed: "); debug_record(record); debug("\n");
131 }
132 #endif
133 }
134
135 static void process_action(keyrecord_t *record)
136 {
137 keyevent_t event = record->event;
138 uint8_t tap_count = record->tap.count;
139
140 if (IS_NOEVENT(event)) { return; }
141
142 action_t action = layer_switch_get_action(event.key);
143 debug("ACTION: "); debug_action(action);
144 debug(" overlays: "); overlay_debug();
145 debug(" keymaps: "); keymap_debug();
146 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
147
148 switch (action.kind.id) {
149 /* Key and Mods */
150 case ACT_LMODS:
151 case ACT_RMODS:
152 {
153 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
154 action.key.mods<<4;
155 if (event.pressed) {
156 uint8_t tmp_mods = host_get_mods();
157 if (mods) {
158 host_add_mods(mods);
159 host_send_keyboard_report();
160 }
161 register_code(action.key.code);
162 if (mods && action.key.code) {
163 host_set_mods(tmp_mods);
164 host_send_keyboard_report();
165 }
166 } else {
167 if (mods && !action.key.code) {
168 host_del_mods(mods);
169 host_send_keyboard_report();
170 }
171 unregister_code(action.key.code);
172 }
173 }
174 break;
175 #ifndef NO_ACTION_TAPPING
176 case ACT_LMODS_TAP:
177 case ACT_RMODS_TAP:
178 {
179 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
180 action.key.mods<<4;
181 switch (action.layer.code) {
182 #ifndef NO_ACTION_ONESHOT
183 case 0x00:
184 // Oneshot modifier
185 if (event.pressed) {
186 if (tap_count == 0) {
187 debug("MODS_TAP: Oneshot: add_mods\n");
188 add_mods(mods);
189 }
190 else if (tap_count == 1) {
191 debug("MODS_TAP: Oneshot: start\n");
192 oneshot_start(mods);
193 }
194 else if (tap_count == TAPPING_TOGGLE) {
195 debug("MODS_TAP: Oneshot: toggle\n");
196 oneshot_toggle();
197 }
198 else {
199 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
200 // double tap cancels oneshot and works as normal modifier.
201 oneshot_cancel();
202 add_mods(mods);
203 }
204 } else {
205 if (tap_count == 0) {
206 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
207 // cancel oneshot on hold
208 oneshot_cancel();
209 del_mods(mods);
210 }
211 else if (tap_count == 1) {
212 debug("MODS_TAP: Oneshot: del_mods\n");
213 // retain Oneshot
214 del_mods(mods);
215 }
216 else {
217 debug("MODS_TAP: Oneshot: del_mods\n");
218 // cancel Mods
219 del_mods(mods);
220 }
221 }
222 break;
223 #endif
224 default:
225 if (event.pressed) {
226 if (tap_count > 0) {
227 if (waiting_buffer_has_anykey_pressed()) {
228 debug("MODS_TAP: Tap: Cancel: add_mods\n");
229 // ad hoc: set 0 to cancel tap
230 record->tap.count = 0;
231 add_mods(mods);
232 } else {
233 debug("MODS_TAP: Tap: register_code\n");
234 register_code(action.key.code);
235 }
236 } else {
237 debug("MODS_TAP: No tap: add_mods\n");
238 add_mods(mods);
239 }
240 } else {
241 if (tap_count > 0) {
242 debug("MODS_TAP: Tap: unregister_code\n");
243 unregister_code(action.key.code);
244 } else {
245 debug("MODS_TAP: No tap: add_mods\n");
246 del_mods(mods);
247 }
248 }
249 break;
250 }
251 }
252 break;
253 #endif
254 #ifdef EXTRAKEY_ENABLE
255 /* other HID usage */
256 case ACT_USAGE:
257 switch (action.usage.page) {
258 case PAGE_SYSTEM:
259 if (event.pressed) {
260 host_system_send(action.usage.code);
261 } else {
262 host_system_send(0);
263 }
264 break;
265 case PAGE_CONSUMER:
266 if (event.pressed) {
267 host_consumer_send(action.usage.code);
268 } else {
269 host_consumer_send(0);
270 }
271 break;
272 }
273 break;
274 #endif
275 #ifdef MOUSEKEY_ENABLE
276 /* Mouse key */
277 case ACT_MOUSEKEY:
278 if (event.pressed) {
279 mousekey_on(action.key.code);
280 mousekey_send();
281 } else {
282 mousekey_off(action.key.code);
283 mousekey_send();
284 }
285 break;
286 #endif
287 #ifndef NO_ACTION_KEYMAP
288 case ACT_KEYMAP:
289 switch (action.layer.code) {
290 /* Keymap clear */
291 case OP_RESET:
292 switch (action.layer.val & 0x03) {
293 case 0:
294 // NOTE: reserved
295 overlay_clear();
296 keymap_clear();
297 break;
298 case ON_PRESS:
299 if (event.pressed) {
300 overlay_clear();
301 keymap_clear();
302 }
303 break;
304 case ON_RELEASE:
305 if (!event.pressed) {
306 overlay_clear();
307 keymap_clear();
308 }
309 break;
310 case ON_BOTH:
311 overlay_clear();
312 keymap_clear();
313 break;
314 /* NOTE: 4-7 rserved */
315 }
316 break;
317 /* Keymap Reset default layer */
318 case (OP_RESET | ON_PRESS):
319 if (event.pressed) {
320 default_layer_set(action.layer.val);
321 }
322 break;
323 case (OP_RESET | ON_RELEASE):
324 if (!event.pressed) {
325 default_layer_set(action.layer.val);
326 }
327 break;
328 case (OP_RESET | ON_BOTH):
329 default_layer_set(action.layer.val);
330 break;
331
332 /* Keymap Bit invert */
333 case OP_INV:
334 /* with tap toggle */
335 if (event.pressed) {
336 if (tap_count < TAPPING_TOGGLE) {
337 debug("KEYMAP_INV: tap toggle(press).\n");
338 keymap_invert(action.layer.val);
339 }
340 } else {
341 if (tap_count <= TAPPING_TOGGLE) {
342 debug("KEYMAP_INV: tap toggle(release).\n");
343 keymap_invert(action.layer.val);
344 }
345 }
346 break;
347 case (OP_INV | ON_PRESS):
348 if (event.pressed) {
349 keymap_invert(action.layer.val);
350 }
351 break;
352 case (OP_INV | ON_RELEASE):
353 if (!event.pressed) {
354 keymap_invert(action.layer.val);
355 }
356 break;
357 case (OP_INV | ON_BOTH):
358 keymap_invert(action.layer.val);
359 break;
360
361 /* Keymap Bit on */
362 case OP_ON:
363 if (event.pressed) {
364 keymap_on(action.layer.val);
365 } else {
366 keymap_off(action.layer.val);
367 }
368 break;
369 case (OP_ON | ON_PRESS):
370 if (event.pressed) {
371 keymap_on(action.layer.val);
372 }
373 break;
374 case (OP_ON | ON_RELEASE):
375 if (!event.pressed) {
376 keymap_on(action.layer.val);
377 }
378 break;
379 case (OP_ON | ON_BOTH):
380 keymap_on(action.layer.val);
381 break;
382
383 /* Keymap Bit off */
384 case OP_OFF:
385 if (event.pressed) {
386 keymap_off(action.layer.val);
387 } else {
388 keymap_on(action.layer.val);
389 }
390 break;
391 case (OP_OFF | ON_PRESS):
392 if (event.pressed) {
393 keymap_off(action.layer.val);
394 }
395 break;
396 case (OP_OFF | ON_RELEASE):
397 if (!event.pressed) {
398 keymap_off(action.layer.val);
399 }
400 break;
401 case (OP_OFF | ON_BOTH):
402 keymap_off(action.layer.val);
403 break;
404
405 /* Keymap Bit set */
406 case OP_SET:
407 if (event.pressed) {
408 keymap_set(action.layer.val);
409 } else {
410 keymap_clear();
411 }
412 break;
413 case (OP_SET | ON_PRESS):
414 if (event.pressed) {
415 keymap_set(action.layer.val);
416 }
417 break;
418 case (OP_SET | ON_RELEASE):
419 if (!event.pressed) {
420 keymap_set(action.layer.val);
421 }
422 break;
423 case (OP_SET | ON_BOTH):
424 keymap_set(action.layer.val);
425 break;
426
427 /* Keymap Bit invert with tap key */
428 default:
429 if (event.pressed) {
430 if (tap_count > 0) {
431 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
432 register_code(action.layer.code);
433 } else {
434 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
435 keymap_on(action.layer.val);
436 }
437 } else {
438 if (tap_count > 0) {
439 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
440 unregister_code(action.layer.code);
441 } else {
442 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
443 keymap_off(action.layer.val);
444 }
445 }
446 break;
447 }
448 break;
449 #endif
450 #ifndef NO_ACTION_OVERLAY
451 case ACT_OVERLAY:
452 switch (action.layer.code) {
453 // Overlay Invert bit4
454 case OP_INV4 | 0:
455 if (action.layer.val == 0) {
456 // NOTE: reserved for future use
457 overlay_clear();
458 } else {
459 overlay_set(overlay_stat ^ action.layer.val);
460 }
461 break;
462 case OP_INV4 | 1:
463 if (action.layer.val == 0) {
464 // on pressed
465 if (event.pressed) overlay_clear();
466 } else {
467 overlay_set(overlay_stat ^ action.layer.val<<4);
468 }
469 break;
470 case OP_INV4 | 2:
471 if (action.layer.val == 0) {
472 // on released
473 if (!event.pressed) overlay_clear();
474 } else {
475 overlay_set(overlay_stat ^ action.layer.val<<8);
476 }
477 break;
478 case OP_INV4 | 3:
479 if (action.layer.val == 0) {
480 // on both
481 overlay_clear();
482 } else {
483 overlay_set(overlay_stat ^ action.layer.val<<12);
484 }
485 break;
486
487 /* Overlay Bit invert */
488 case OP_INV:
489 /* with tap toggle */
490 if (event.pressed) {
491 if (tap_count < TAPPING_TOGGLE) {
492 debug("OVERLAY_INV: tap toggle(press).\n");
493 overlay_invert(action.layer.val);
494 }
495 } else {
496 if (tap_count <= TAPPING_TOGGLE) {
497 debug("OVERLAY_INV: tap toggle(release).\n");
498 overlay_invert(action.layer.val);
499 }
500 }
501 break;
502 case (OP_INV | ON_PRESS):
503 if (event.pressed) {
504 overlay_invert(action.layer.val);
505 }
506 break;
507 case (OP_INV | ON_RELEASE):
508 if (!event.pressed) {
509 overlay_invert(action.layer.val);
510 }
511 break;
512 case (OP_INV | ON_BOTH):
513 overlay_invert(action.layer.val);
514 break;
515
516 /* Overlay Bit on */
517 case OP_ON:
518 if (event.pressed) {
519 overlay_on(action.layer.val);
520 } else {
521 overlay_off(action.layer.val);
522 }
523 break;
524 case (OP_ON | ON_PRESS):
525 if (event.pressed) {
526 overlay_on(action.layer.val);
527 }
528 break;
529 case (OP_ON | ON_RELEASE):
530 if (!event.pressed) {
531 overlay_on(action.layer.val);
532 }
533 break;
534 case (OP_ON | ON_BOTH):
535 overlay_on(action.layer.val);
536 break;
537
538 /* Overlay Bit off */
539 case OP_OFF:
540 if (event.pressed) {
541 overlay_off(action.layer.val);
542 } else {
543 overlay_on(action.layer.val);
544 }
545 break;
546 case (OP_OFF | ON_PRESS):
547 if (event.pressed) {
548 overlay_off(action.layer.val);
549 }
550 break;
551 case (OP_OFF | ON_RELEASE):
552 if (!event.pressed) {
553 overlay_off(action.layer.val);
554 }
555 break;
556 case (OP_OFF | ON_BOTH):
557 overlay_off(action.layer.val);
558 break;
559
560 /* Overlay Bit set */
561 case OP_SET:
562 if (event.pressed) {
563 overlay_move(action.layer.val);
564 } else {
565 overlay_clear();
566 }
567 break;
568 case (OP_SET | ON_PRESS):
569 if (event.pressed) {
570 overlay_move(action.layer.val);
571 }
572 break;
573 case (OP_SET | ON_RELEASE):
574 if (!event.pressed) {
575 overlay_move(action.layer.val);
576 }
577 break;
578 case (OP_SET | ON_BOTH):
579 overlay_move(action.layer.val);
580 break;
581
582 /* Overlay Bit invert with tap key */
583 default:
584 if (event.pressed) {
585 if (tap_count > 0) {
586 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
587 register_code(action.layer.code);
588 } else {
589 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
590 overlay_on(action.layer.val);
591 }
592 } else {
593 if (tap_count > 0) {
594 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
595 unregister_code(action.layer.code);
596 } else {
597 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
598 overlay_off(action.layer.val);
599 }
600 }
601 break;
602 }
603 break;
604 #endif
605 /* Extentions */
606 #ifndef NO_ACTION_MACRO
607 case ACT_MACRO:
608 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
609 break;
610 #endif
611 case ACT_COMMAND:
612 break;
613 #ifndef NO_ACTION_FUNCTION
614 case ACT_FUNCTION:
615 action_function(record, action.func.id, action.func.opt);
616 break;
617 #endif
618 default:
619 break;
620 }
621 }
622
623
624
625
626 /*
627 * Utilities for actions.
628 */
629 void register_code(uint8_t code)
630 {
631 if (code == KC_NO) {
632 return;
633 }
634 #ifdef CAPSLOCK_LOCKING_ENABLE
635 else if (KC_LOCKING_CAPS == code) {
636 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
637 // Resync: ignore if caps lock already is on
638 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
639 #endif
640 host_add_key(KC_CAPSLOCK);
641 host_send_keyboard_report();
642 host_del_key(KC_CAPSLOCK);
643 host_send_keyboard_report();
644 }
645 #endif
646 else if IS_KEY(code) {
647 // TODO: should push command_proc out of this block?
648 if (command_proc(code)) return;
649
650 #ifndef NO_ACTION_ONESHOT
651 if (oneshot_state.mods && !oneshot_state.disabled) {
652 uint8_t tmp_mods = host_get_mods();
653 host_add_mods(oneshot_state.mods);
654
655 host_add_key(code);
656 host_send_keyboard_report();
657
658 host_set_mods(tmp_mods);
659 oneshot_cancel();
660 } else
661 #endif
662 {
663 host_add_key(code);
664 host_send_keyboard_report();
665 }
666 }
667 else if IS_MOD(code) {
668 host_add_mods(MOD_BIT(code));
669 host_send_keyboard_report();
670 }
671 }
672
673 void unregister_code(uint8_t code)
674 {
675 if (code == KC_NO) {
676 return;
677 }
678 #ifdef CAPSLOCK_LOCKING_ENABLE
679 else if (KC_LOCKING_CAPS == code) {
680 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
681 // Resync: ignore if caps lock already is off
682 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
683 #endif
684 host_add_key(KC_CAPSLOCK);
685 host_send_keyboard_report();
686 host_del_key(KC_CAPSLOCK);
687 host_send_keyboard_report();
688 }
689 #endif
690 else if IS_KEY(code) {
691 host_del_key(code);
692 host_send_keyboard_report();
693 }
694 else if IS_MOD(code) {
695 host_del_mods(MOD_BIT(code));
696 host_send_keyboard_report();
697 }
698 }
699
700 void add_mods(uint8_t mods)
701 {
702 if (mods) {
703 host_add_mods(mods);
704 host_send_keyboard_report();
705 }
706 }
707
708 void del_mods(uint8_t mods)
709 {
710 if (mods) {
711 host_del_mods(mods);
712 host_send_keyboard_report();
713 }
714 }
715
716 void set_mods(uint8_t mods)
717 {
718 host_set_mods(mods);
719 host_send_keyboard_report();
720 }
721
722 void clear_keyboard(void)
723 {
724 host_clear_mods();
725 clear_keyboard_but_mods();
726 }
727
728 void clear_keyboard_but_mods(void)
729 {
730 host_clear_keys();
731 host_send_keyboard_report();
732 #ifdef MOUSEKEY_ENABLE
733 mousekey_clear();
734 mousekey_send();
735 #endif
736 #ifdef EXTRAKEY_ENABLE
737 host_system_send(0);
738 host_consumer_send(0);
739 #endif
740 }
741
742 bool sending_anykey(void)
743 {
744 return (host_has_anykey() || host_mouse_in_use() ||
745 host_last_sysytem_report() || host_last_consumer_report());
746 }
747
748 bool is_tap_key(key_t key)
749 {
750 action_t action = layer_switch_get_action(key);
751
752 switch (action.kind.id) {
753 case ACT_LMODS_TAP:
754 case ACT_RMODS_TAP:
755 return true;
756 case ACT_KEYMAP:
757 case ACT_OVERLAY:
758 switch (action.layer.code) {
759 case 0x04 ... 0xEF: /* tap key */
760 case OP_INV:
761 return true;
762 default:
763 return false;
764 }
765 case ACT_MACRO:
766 case ACT_FUNCTION:
767 if (action.func.opt & FUNC_TAP) { return true; }
768 return false;
769 }
770 return false;
771 }
772
773
774 /*
775 * debug print
776 */
777 static void debug_event(keyevent_t event)
778 {
779 debug_hex16((event.key.row<<8) | event.key.col);
780 if (event.pressed) debug("d("); else debug("u(");
781 debug_dec(event.time); debug(")");
782 }
783
784 static void debug_record(keyrecord_t record)
785 {
786 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
787 if (record.tap.interrupted) debug("-");
788 }
789
790 static void debug_action(action_t action)
791 {
792 switch (action.kind.id) {
793 case ACT_LMODS: debug("ACT_LMODS"); break;
794 case ACT_RMODS: debug("ACT_RMODS"); break;
795 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
796 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
797 case ACT_USAGE: debug("ACT_USAGE"); break;
798 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
799 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
800 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
801 case ACT_MACRO: debug("ACT_MACRO"); break;
802 case ACT_COMMAND: debug("ACT_COMMAND"); break;
803 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
804 default: debug("UNKNOWN"); break;
805 }
806 debug("[");
807 debug_hex4(action.kind.param>>8);
808 debug(":");
809 debug_hex8(action.kind.param & 0xff);
810 debug("]");
811 }
812
813
814
815 #ifndef NO_ACTION_TAPPING
816 /* Tapping
817 *
818 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
819 * (without interfering by typing other key)
820 */
821 /* return true when key event is processed or consumed. */
822 static bool process_tapping(keyrecord_t *keyp)
823 {
824 keyevent_t event = keyp->event;
825
826 // if tapping
827 if (IS_TAPPING_PRESSED()) {
828 if (WITHIN_TAPPING_TERM(event)) {
829 if (tapping_key.tap.count == 0) {
830 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
831 // first tap!
832 debug("Tapping: First tap(0->1).\n");
833 tapping_key.tap.count = 1;
834 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
835 debug_tapping_key();
836 process_action(&tapping_key);
837
838 // enqueue
839 keyp->tap = tapping_key.tap;
840 return false;
841 }
842 #if TAPPING_TERM >= 500
843 /* This can prevent from typing some tap keys in a row at a time. */
844 else if (!event.pressed && waiting_buffer_typed(event)) {
845 // other key typed. not tap.
846 debug("Tapping: End. No tap. Interfered by typing key\n");
847 process_action(&tapping_key);
848 tapping_key = (keyrecord_t){};
849 debug_tapping_key();
850
851 // enqueue
852 return false;
853 }
854 #endif
855 else {
856 // other key events shall be enq'd till tapping state settles.
857 return false;
858 }
859 }
860 // tap_count > 0
861 else {
862 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
863 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
864 keyp->tap = tapping_key.tap;
865 process_action(keyp);
866 tapping_key = *keyp;
867 debug_tapping_key();
868 return true;
869 }
870 else if (is_tap_key(keyp->event.key) && event.pressed) {
871 if (tapping_key.tap.count > 1) {
872 debug("Tapping: Start new tap with releasing last tap(>1).\n");
873 // unregister key
874 process_action(&(keyrecord_t){
875 .tap = tapping_key.tap,
876 .event.key = tapping_key.event.key,
877 .event.time = event.time,
878 .event.pressed = false
879 });
880 } else {
881 debug("Tapping: Start while last tap(1).\n");
882 }
883 tapping_key = *keyp;
884 waiting_buffer_scan_tap();
885 debug_tapping_key();
886 return true;
887 }
888 else {
889 if (!IS_NOEVENT(keyp->event)) {
890 debug("Tapping: key event while last tap(>0).\n");
891 }
892 process_action(keyp);
893 return true;
894 }
895 }
896 }
897 // after TAPPING_TERM
898 else {
899 if (tapping_key.tap.count == 0) {
900 debug("Tapping: End. Timeout. Not tap(0): ");
901 debug_event(event); debug("\n");
902 process_action(&tapping_key);
903 tapping_key = (keyrecord_t){};
904 debug_tapping_key();
905 return false;
906 } else {
907 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
908 debug("Tapping: End. last timeout tap release(>0).");
909 keyp->tap = tapping_key.tap;
910 process_action(keyp);
911 tapping_key = (keyrecord_t){};
912 return true;
913 }
914 else if (is_tap_key(keyp->event.key) && event.pressed) {
915 if (tapping_key.tap.count > 1) {
916 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
917 // unregister key
918 process_action(&(keyrecord_t){
919 .tap = tapping_key.tap,
920 .event.key = tapping_key.event.key,
921 .event.time = event.time,
922 .event.pressed = false
923 });
924 } else {
925 debug("Tapping: Start while last timeout tap(1).\n");
926 }
927 tapping_key = *keyp;
928 waiting_buffer_scan_tap();
929 debug_tapping_key();
930 return true;
931 }
932 else {
933 if (!IS_NOEVENT(keyp->event)) {
934 debug("Tapping: key event while last timeout tap(>0).\n");
935 }
936 process_action(keyp);
937 return true;
938 }
939 }
940 }
941 } else if (IS_TAPPING_RELEASED()) {
942 if (WITHIN_TAPPING_TERM(event)) {
943 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
944 // sequential tap.
945 keyp->tap = tapping_key.tap;
946 keyp->tap.count += 1;
947 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
948 process_action(keyp);
949 tapping_key = *keyp;
950 debug_tapping_key();
951 return true;
952 } else if (event.pressed && is_tap_key(event.key)) {
953 // Sequential tap can be interfered with other tap key.
954 debug("Tapping: Start with interfering other tap.\n");
955 tapping_key = *keyp;
956 waiting_buffer_scan_tap();
957 debug_tapping_key();
958 return true;
959 } else {
960 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
961 process_action(keyp);
962 return true;
963 }
964 } else {
965 // timeout. no sequential tap.
966 debug("Tapping: End(Timeout after releasing last tap): ");
967 debug_event(event); debug("\n");
968 tapping_key = (keyrecord_t){};
969 debug_tapping_key();
970 return false;
971 }
972 }
973 // not tapping satate
974 else {
975 if (event.pressed && is_tap_key(event.key)) {
976 debug("Tapping: Start(Press tap key).\n");
977 tapping_key = *keyp;
978 waiting_buffer_scan_tap();
979 debug_tapping_key();
980 return true;
981 } else {
982 process_action(keyp);
983 return true;
984 }
985 }
986 }
987
988
989 /*
990 * Waiting buffer
991 */
992 static bool waiting_buffer_enq(keyrecord_t record)
993 {
994 if (IS_NOEVENT(record.event)) {
995 return true;
996 }
997
998 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
999 debug("waiting_buffer_enq: Over flow.\n");
1000 return false;
1001 }
1002
1003 waiting_buffer[waiting_buffer_head] = record;
1004 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
1005
1006 debug("waiting_buffer_enq: "); debug_waiting_buffer();
1007 return true;
1008 }
1009
1010 static void waiting_buffer_clear(void)
1011 {
1012 waiting_buffer_head = 0;
1013 waiting_buffer_tail = 0;
1014 }
1015
1016 #if TAPPING_TERM >= 500
1017 static bool waiting_buffer_typed(keyevent_t event)
1018 {
1019 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1020 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
1021 return true;
1022 }
1023 }
1024 return false;
1025 }
1026 #endif
1027
1028 bool waiting_buffer_has_anykey_pressed(void)
1029 {
1030 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1031 if (waiting_buffer[i].event.pressed) return true;
1032 }
1033 return false;
1034 }
1035 /* scan buffer for tapping */
1036 static void waiting_buffer_scan_tap(void)
1037 {
1038 // tapping already is settled
1039 if (tapping_key.tap.count > 0) return;
1040 // invalid state: tapping_key released && tap.count == 0
1041 if (!tapping_key.event.pressed) return;
1042
1043 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1044 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
1045 !waiting_buffer[i].event.pressed &&
1046 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
1047 tapping_key.tap.count = 1;
1048 waiting_buffer[i].tap.count = 1;
1049 process_action(&tapping_key);
1050
1051 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
1052 debug_waiting_buffer();
1053 return;
1054 }
1055 }
1056 }
1057
1058
1059 /*
1060 * debug print
1061 */
1062 static void debug_tapping_key(void)
1063 {
1064 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1065 }
1066
1067 static void debug_waiting_buffer(void)
1068 {
1069 debug("{ ");
1070 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1071 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
1072 }
1073 debug("}\n");
1074 }
1075 #endif
Imprint / Impressum