]> git.gir.st - tmk_keyboard.git/blob - common/action.c
Add NO_ACTION_FUNCTION config option
[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 #ifndef NO_ACTION_MACRO
692 case ACT_MACRO:
693 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
694 break;
695 #endif
696 case ACT_COMMAND:
697 break;
698 #ifndef NO_ACTION_FUNCTION
699 case ACT_FUNCTION:
700 action_function(record, action.func.id, action.func.opt);
701 break;
702 #endif
703 default:
704 break;
705 }
706 }
707
708 #ifndef NO_ACTION_TAPPING
709 /* Tapping
710 *
711 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
712 * (without interfering by typing other key)
713 */
714 /* return true when key event is processed or consumed. */
715 static bool process_tapping(keyrecord_t *keyp)
716 {
717 keyevent_t event = keyp->event;
718
719 // if tapping
720 if (IS_TAPPING_PRESSED()) {
721 if (WITHIN_TAPPING_TERM(event)) {
722 if (tapping_key.tap.count == 0) {
723 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
724 // first tap!
725 debug("Tapping: First tap(0->1).\n");
726 tapping_key.tap.count = 1;
727 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
728 debug_tapping_key();
729 process_action(&tapping_key);
730
731 // enqueue
732 keyp->tap = tapping_key.tap;
733 return false;
734 }
735 #if TAPPING_TERM >= 500
736 /* This can prevent from typing some tap keys in a row at a time. */
737 else if (!event.pressed && waiting_buffer_typed(event)) {
738 // other key typed. not tap.
739 debug("Tapping: End. No tap. Interfered by typing key\n");
740 process_action(&tapping_key);
741 tapping_key = (keyrecord_t){};
742 debug_tapping_key();
743
744 // enqueue
745 return false;
746 }
747 #endif
748 else {
749 // other key events shall be enq'd till tapping state settles.
750 return false;
751 }
752 }
753 // tap_count > 0
754 else {
755 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
756 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
757 keyp->tap = tapping_key.tap;
758 process_action(keyp);
759 tapping_key = *keyp;
760 debug_tapping_key();
761 return true;
762 }
763 else if (is_tap_key(keyp->event.key) && event.pressed) {
764 if (tapping_key.tap.count > 1) {
765 debug("Tapping: Start new tap with releasing last tap(>1).\n");
766 // unregister key
767 process_action(&(keyrecord_t){
768 .tap = tapping_key.tap,
769 .event.key = tapping_key.event.key,
770 .event.time = event.time,
771 .event.pressed = false
772 });
773 } else {
774 debug("Tapping: Start while last tap(1).\n");
775 }
776 tapping_key = *keyp;
777 waiting_buffer_scan_tap();
778 debug_tapping_key();
779 return true;
780 }
781 else {
782 if (!IS_NOEVENT(keyp->event)) {
783 debug("Tapping: key event while last tap(>0).\n");
784 }
785 process_action(keyp);
786 return true;
787 }
788 }
789 }
790 // after TAPPING_TERM
791 else {
792 if (tapping_key.tap.count == 0) {
793 debug("Tapping: End. Timeout. Not tap(0): ");
794 debug_event(event); debug("\n");
795 process_action(&tapping_key);
796 tapping_key = (keyrecord_t){};
797 debug_tapping_key();
798 return false;
799 } else {
800 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
801 debug("Tapping: End. last timeout tap release(>0).");
802 keyp->tap = tapping_key.tap;
803 process_action(keyp);
804 tapping_key = (keyrecord_t){};
805 return true;
806 }
807 else if (is_tap_key(keyp->event.key) && event.pressed) {
808 if (tapping_key.tap.count > 1) {
809 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
810 // unregister key
811 process_action(&(keyrecord_t){
812 .tap = tapping_key.tap,
813 .event.key = tapping_key.event.key,
814 .event.time = event.time,
815 .event.pressed = false
816 });
817 } else {
818 debug("Tapping: Start while last timeout tap(1).\n");
819 }
820 tapping_key = *keyp;
821 waiting_buffer_scan_tap();
822 debug_tapping_key();
823 return true;
824 }
825 else {
826 if (!IS_NOEVENT(keyp->event)) {
827 debug("Tapping: key event while last timeout tap(>0).\n");
828 }
829 process_action(keyp);
830 return true;
831 }
832 }
833 }
834 } else if (IS_TAPPING_RELEASED()) {
835 if (WITHIN_TAPPING_TERM(event)) {
836 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
837 // sequential tap.
838 keyp->tap = tapping_key.tap;
839 keyp->tap.count += 1;
840 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
841 process_action(keyp);
842 tapping_key = *keyp;
843 debug_tapping_key();
844 return true;
845 } else if (event.pressed && is_tap_key(event.key)) {
846 // Sequential tap can be interfered with other tap key.
847 debug("Tapping: Start with interfering other tap.\n");
848 tapping_key = *keyp;
849 waiting_buffer_scan_tap();
850 debug_tapping_key();
851 return true;
852 } else {
853 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
854 process_action(keyp);
855 return true;
856 }
857 } else {
858 // timeout. no sequential tap.
859 debug("Tapping: End(Timeout after releasing last tap): ");
860 debug_event(event); debug("\n");
861 tapping_key = (keyrecord_t){};
862 debug_tapping_key();
863 return false;
864 }
865 }
866 // not tapping satate
867 else {
868 if (event.pressed && is_tap_key(event.key)) {
869 debug("Tapping: Start(Press tap key).\n");
870 tapping_key = *keyp;
871 waiting_buffer_scan_tap();
872 debug_tapping_key();
873 return true;
874 } else {
875 process_action(keyp);
876 return true;
877 }
878 }
879 }
880
881 /* scan buffer for tapping */
882 static void waiting_buffer_scan_tap(void)
883 {
884 // tapping already is settled
885 if (tapping_key.tap.count > 0) return;
886 // invalid state: tapping_key released && tap.count == 0
887 if (!tapping_key.event.pressed) return;
888
889 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
890 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
891 !waiting_buffer[i].event.pressed &&
892 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
893 tapping_key.tap.count = 1;
894 waiting_buffer[i].tap.count = 1;
895 process_action(&tapping_key);
896
897 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
898 debug_waiting_buffer();
899 return;
900 }
901 }
902 }
903 #endif
904
905
906
907 /*
908 * Utilities for actions.
909 */
910 void register_code(uint8_t code)
911 {
912 if (code == KC_NO) {
913 return;
914 }
915 #ifdef CAPSLOCK_LOCKING_ENABLE
916 else if (KC_LOCKING_CAPS == code) {
917 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
918 // Resync: ignore if caps lock already is on
919 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
920 #endif
921 host_add_key(KC_CAPSLOCK);
922 host_send_keyboard_report();
923 host_del_key(KC_CAPSLOCK);
924 host_send_keyboard_report();
925 }
926 #endif
927 else if IS_KEY(code) {
928 // TODO: should push command_proc out of this block?
929 if (command_proc(code)) return;
930
931 #ifndef NO_ACTION_TAPPING
932 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
933 uint8_t tmp_mods = host_get_mods();
934 host_add_mods(oneshot_state.mods);
935 host_add_key(code);
936 host_send_keyboard_report();
937
938 host_set_mods(tmp_mods);
939 oneshot_state.ready = false;
940 } else
941 #endif
942 {
943 host_add_key(code);
944 host_send_keyboard_report();
945 }
946 }
947 else if IS_MOD(code) {
948 host_add_mods(MOD_BIT(code));
949 host_send_keyboard_report();
950 }
951 }
952
953 void unregister_code(uint8_t code)
954 {
955 if (code == KC_NO) {
956 return;
957 }
958 #ifdef CAPSLOCK_LOCKING_ENABLE
959 else if (KC_LOCKING_CAPS == code) {
960 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
961 // Resync: ignore if caps lock already is off
962 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
963 #endif
964 host_add_key(KC_CAPSLOCK);
965 host_send_keyboard_report();
966 host_del_key(KC_CAPSLOCK);
967 host_send_keyboard_report();
968 }
969 #endif
970 else if IS_KEY(code) {
971 host_del_key(code);
972 host_send_keyboard_report();
973 }
974 else if IS_MOD(code) {
975 host_del_mods(MOD_BIT(code));
976 host_send_keyboard_report();
977 }
978 }
979
980 void add_mods(uint8_t mods)
981 {
982 if (mods) {
983 host_add_mods(mods);
984 host_send_keyboard_report();
985 }
986 }
987
988 void del_mods(uint8_t mods)
989 {
990 if (mods) {
991 host_del_mods(mods);
992 host_send_keyboard_report();
993 }
994 }
995
996 void set_mods(uint8_t mods)
997 {
998 host_set_mods(mods);
999 host_send_keyboard_report();
1000 }
1001
1002 void clear_keyboard(void)
1003 {
1004 host_clear_mods();
1005 clear_keyboard_but_mods();
1006 }
1007
1008 void clear_keyboard_but_mods(void)
1009 {
1010 host_clear_keys();
1011 host_send_keyboard_report();
1012 #ifdef MOUSEKEY_ENABLE
1013 mousekey_clear();
1014 mousekey_send();
1015 #endif
1016 #ifdef EXTRAKEY_ENABLE
1017 host_system_send(0);
1018 host_consumer_send(0);
1019 #endif
1020 }
1021
1022 bool sending_anykey(void)
1023 {
1024 return (host_has_anykey() || host_mouse_in_use() ||
1025 host_last_sysytem_report() || host_last_consumer_report());
1026 }
1027
1028 bool is_tap_key(key_t key)
1029 {
1030 action_t action = layer_switch_get_action(key);
1031
1032 switch (action.kind.id) {
1033 case ACT_LMODS_TAP:
1034 case ACT_RMODS_TAP:
1035 return true;
1036 case ACT_KEYMAP:
1037 case ACT_OVERLAY:
1038 switch (action.layer.code) {
1039 case 0x04 ... 0xEF: /* tap key */
1040 case OP_INV:
1041 return true;
1042 default:
1043 return false;
1044 }
1045 case ACT_MACRO:
1046 case ACT_FUNCTION:
1047 if (action.func.opt & FUNC_TAP) { return true; }
1048 return false;
1049 }
1050 return false;
1051 }
1052
1053
1054 /*
1055 * debug print
1056 */
1057 static void debug_event(keyevent_t event)
1058 {
1059 debug_hex16((event.key.row<<8) | event.key.col);
1060 if (event.pressed) debug("d("); else debug("u(");
1061 debug_dec(event.time); debug(")");
1062 }
1063 static void debug_record(keyrecord_t record)
1064 {
1065 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1066 if (record.tap.interrupted) debug("-");
1067 }
1068 static void debug_action(action_t action)
1069 {
1070 switch (action.kind.id) {
1071 case ACT_LMODS: debug("ACT_LMODS"); break;
1072 case ACT_RMODS: debug("ACT_RMODS"); break;
1073 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1074 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1075 case ACT_USAGE: debug("ACT_USAGE"); break;
1076 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1077 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1078 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1079 case ACT_MACRO: debug("ACT_MACRO"); break;
1080 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1081 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1082 default: debug("UNKNOWN"); break;
1083 }
1084 debug("[");
1085 debug_hex4(action.kind.param>>8);
1086 debug(":");
1087 debug_hex8(action.kind.param & 0xff);
1088 debug("]");
1089 }
1090 #ifndef NO_ACTION_TAPPING
1091 static void debug_tapping_key(void)
1092 {
1093 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1094 }
1095 static void debug_waiting_buffer(void)
1096 {
1097 debug("{ ");
1098 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1099 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
1100 }
1101 debug("}\n");
1102 }
1103 #endif
Imprint / Impressum