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