]> git.gir.st - tmk_keyboard.git/blob - common/action.c
Add NO_ACTION_KEYMAP 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 #ifndef NO_ACTION_KEYMAP
373 case ACT_KEYMAP:
374 switch (action.layer.code) {
375 /* Keymap clear */
376 case OP_RESET:
377 switch (action.layer.val & 0x03) {
378 case 0:
379 // NOTE: reserved
380 overlay_clear();
381 keymap_clear();
382 break;
383 case ON_PRESS:
384 if (event.pressed) {
385 overlay_clear();
386 keymap_clear();
387 }
388 break;
389 case ON_RELEASE:
390 if (!event.pressed) {
391 overlay_clear();
392 keymap_clear();
393 }
394 break;
395 case ON_BOTH:
396 overlay_clear();
397 keymap_clear();
398 break;
399 /* NOTE: 4-7 rserved */
400 }
401 break;
402 /* Keymap Reset default layer */
403 case (OP_RESET | ON_PRESS):
404 if (event.pressed) {
405 default_layer_set(action.layer.val);
406 }
407 break;
408 case (OP_RESET | ON_RELEASE):
409 if (!event.pressed) {
410 default_layer_set(action.layer.val);
411 }
412 break;
413 case (OP_RESET | ON_BOTH):
414 default_layer_set(action.layer.val);
415 break;
416
417 /* Keymap Bit invert */
418 case OP_INV:
419 /* with tap toggle */
420 if (event.pressed) {
421 if (tap_count < TAPPING_TOGGLE) {
422 debug("KEYMAP_INV: tap toggle(press).\n");
423 keymap_invert(action.layer.val);
424 }
425 } else {
426 if (tap_count <= TAPPING_TOGGLE) {
427 debug("KEYMAP_INV: tap toggle(release).\n");
428 keymap_invert(action.layer.val);
429 }
430 }
431 break;
432 case (OP_INV | ON_PRESS):
433 if (event.pressed) {
434 keymap_invert(action.layer.val);
435 }
436 break;
437 case (OP_INV | ON_RELEASE):
438 if (!event.pressed) {
439 keymap_invert(action.layer.val);
440 }
441 break;
442 case (OP_INV | ON_BOTH):
443 keymap_invert(action.layer.val);
444 break;
445
446 /* Keymap Bit on */
447 case OP_ON:
448 if (event.pressed) {
449 keymap_on(action.layer.val);
450 } else {
451 keymap_off(action.layer.val);
452 }
453 break;
454 case (OP_ON | ON_PRESS):
455 if (event.pressed) {
456 keymap_on(action.layer.val);
457 }
458 break;
459 case (OP_ON | ON_RELEASE):
460 if (!event.pressed) {
461 keymap_on(action.layer.val);
462 }
463 break;
464 case (OP_ON | ON_BOTH):
465 keymap_on(action.layer.val);
466 break;
467
468 /* Keymap Bit off */
469 case OP_OFF:
470 if (event.pressed) {
471 keymap_off(action.layer.val);
472 } else {
473 keymap_on(action.layer.val);
474 }
475 break;
476 case (OP_OFF | ON_PRESS):
477 if (event.pressed) {
478 keymap_off(action.layer.val);
479 }
480 break;
481 case (OP_OFF | ON_RELEASE):
482 if (!event.pressed) {
483 keymap_off(action.layer.val);
484 }
485 break;
486 case (OP_OFF | ON_BOTH):
487 keymap_off(action.layer.val);
488 break;
489
490 /* Keymap Bit set */
491 case OP_SET:
492 if (event.pressed) {
493 keymap_set(action.layer.val);
494 } else {
495 keymap_clear();
496 }
497 break;
498 case (OP_SET | ON_PRESS):
499 if (event.pressed) {
500 keymap_set(action.layer.val);
501 }
502 break;
503 case (OP_SET | ON_RELEASE):
504 if (!event.pressed) {
505 keymap_set(action.layer.val);
506 }
507 break;
508 case (OP_SET | ON_BOTH):
509 keymap_set(action.layer.val);
510 break;
511
512 /* Keymap Bit invert with tap key */
513 default:
514 if (event.pressed) {
515 if (tap_count > 0) {
516 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
517 register_code(action.layer.code);
518 } else {
519 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
520 keymap_on(action.layer.val);
521 }
522 } else {
523 if (tap_count > 0) {
524 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
525 unregister_code(action.layer.code);
526 } else {
527 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
528 keymap_off(action.layer.val);
529 }
530 }
531 break;
532 }
533 break;
534 #endif
535
536 #ifndef NO_ACTION_OVERLAY
537 case ACT_OVERLAY:
538 switch (action.layer.code) {
539 // Overlay Invert bit4
540 case OP_INV4 | 0:
541 if (action.layer.val == 0) {
542 // NOTE: reserved for future use
543 overlay_clear();
544 } else {
545 overlay_set(overlay_stat ^ action.layer.val);
546 }
547 break;
548 case OP_INV4 | 1:
549 if (action.layer.val == 0) {
550 // on pressed
551 if (event.pressed) overlay_clear();
552 } else {
553 overlay_set(overlay_stat ^ action.layer.val<<4);
554 }
555 break;
556 case OP_INV4 | 2:
557 if (action.layer.val == 0) {
558 // on released
559 if (!event.pressed) overlay_clear();
560 } else {
561 overlay_set(overlay_stat ^ action.layer.val<<8);
562 }
563 break;
564 case OP_INV4 | 3:
565 if (action.layer.val == 0) {
566 // on both
567 overlay_clear();
568 } else {
569 overlay_set(overlay_stat ^ action.layer.val<<12);
570 }
571 break;
572
573 /* Overlay Bit invert */
574 case OP_INV:
575 /* with tap toggle */
576 if (event.pressed) {
577 if (tap_count < TAPPING_TOGGLE) {
578 debug("OVERLAY_INV: tap toggle(press).\n");
579 overlay_invert(action.layer.val);
580 }
581 } else {
582 if (tap_count <= TAPPING_TOGGLE) {
583 debug("OVERLAY_INV: tap toggle(release).\n");
584 overlay_invert(action.layer.val);
585 }
586 }
587 break;
588 case (OP_INV | ON_PRESS):
589 if (event.pressed) {
590 overlay_invert(action.layer.val);
591 }
592 break;
593 case (OP_INV | ON_RELEASE):
594 if (!event.pressed) {
595 overlay_invert(action.layer.val);
596 }
597 break;
598 case (OP_INV | ON_BOTH):
599 overlay_invert(action.layer.val);
600 break;
601
602 /* Overlay Bit on */
603 case OP_ON:
604 if (event.pressed) {
605 overlay_on(action.layer.val);
606 } else {
607 overlay_off(action.layer.val);
608 }
609 break;
610 case (OP_ON | ON_PRESS):
611 if (event.pressed) {
612 overlay_on(action.layer.val);
613 }
614 break;
615 case (OP_ON | ON_RELEASE):
616 if (!event.pressed) {
617 overlay_on(action.layer.val);
618 }
619 break;
620 case (OP_ON | ON_BOTH):
621 overlay_on(action.layer.val);
622 break;
623
624 /* Overlay Bit off */
625 case OP_OFF:
626 if (event.pressed) {
627 overlay_off(action.layer.val);
628 } else {
629 overlay_on(action.layer.val);
630 }
631 break;
632 case (OP_OFF | ON_PRESS):
633 if (event.pressed) {
634 overlay_off(action.layer.val);
635 }
636 break;
637 case (OP_OFF | ON_RELEASE):
638 if (!event.pressed) {
639 overlay_off(action.layer.val);
640 }
641 break;
642 case (OP_OFF | ON_BOTH):
643 overlay_off(action.layer.val);
644 break;
645
646 /* Overlay Bit set */
647 case OP_SET:
648 if (event.pressed) {
649 overlay_move(action.layer.val);
650 } else {
651 overlay_clear();
652 }
653 break;
654 case (OP_SET | ON_PRESS):
655 if (event.pressed) {
656 overlay_move(action.layer.val);
657 }
658 break;
659 case (OP_SET | ON_RELEASE):
660 if (!event.pressed) {
661 overlay_move(action.layer.val);
662 }
663 break;
664 case (OP_SET | ON_BOTH):
665 overlay_move(action.layer.val);
666 break;
667
668 /* Overlay Bit invert with tap key */
669 default:
670 if (event.pressed) {
671 if (tap_count > 0) {
672 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
673 register_code(action.layer.code);
674 } else {
675 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
676 overlay_on(action.layer.val);
677 }
678 } else {
679 if (tap_count > 0) {
680 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
681 unregister_code(action.layer.code);
682 } else {
683 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
684 overlay_off(action.layer.val);
685 }
686 }
687 break;
688 }
689 break;
690 #endif
691
692 /* Extentions */
693 #ifndef NO_ACTION_MACRO
694 case ACT_MACRO:
695 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
696 break;
697 #endif
698 case ACT_COMMAND:
699 break;
700 #ifndef NO_ACTION_FUNCTION
701 case ACT_FUNCTION:
702 action_function(record, action.func.id, action.func.opt);
703 break;
704 #endif
705 default:
706 break;
707 }
708 }
709
710 #ifndef NO_ACTION_TAPPING
711 /* Tapping
712 *
713 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
714 * (without interfering by typing other key)
715 */
716 /* return true when key event is processed or consumed. */
717 static bool process_tapping(keyrecord_t *keyp)
718 {
719 keyevent_t event = keyp->event;
720
721 // if tapping
722 if (IS_TAPPING_PRESSED()) {
723 if (WITHIN_TAPPING_TERM(event)) {
724 if (tapping_key.tap.count == 0) {
725 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
726 // first tap!
727 debug("Tapping: First tap(0->1).\n");
728 tapping_key.tap.count = 1;
729 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
730 debug_tapping_key();
731 process_action(&tapping_key);
732
733 // enqueue
734 keyp->tap = tapping_key.tap;
735 return false;
736 }
737 #if TAPPING_TERM >= 500
738 /* This can prevent from typing some tap keys in a row at a time. */
739 else if (!event.pressed && waiting_buffer_typed(event)) {
740 // other key typed. not tap.
741 debug("Tapping: End. No tap. Interfered by typing key\n");
742 process_action(&tapping_key);
743 tapping_key = (keyrecord_t){};
744 debug_tapping_key();
745
746 // enqueue
747 return false;
748 }
749 #endif
750 else {
751 // other key events shall be enq'd till tapping state settles.
752 return false;
753 }
754 }
755 // tap_count > 0
756 else {
757 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
758 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
759 keyp->tap = tapping_key.tap;
760 process_action(keyp);
761 tapping_key = *keyp;
762 debug_tapping_key();
763 return true;
764 }
765 else if (is_tap_key(keyp->event.key) && event.pressed) {
766 if (tapping_key.tap.count > 1) {
767 debug("Tapping: Start new tap with releasing last tap(>1).\n");
768 // unregister key
769 process_action(&(keyrecord_t){
770 .tap = tapping_key.tap,
771 .event.key = tapping_key.event.key,
772 .event.time = event.time,
773 .event.pressed = false
774 });
775 } else {
776 debug("Tapping: Start while last tap(1).\n");
777 }
778 tapping_key = *keyp;
779 waiting_buffer_scan_tap();
780 debug_tapping_key();
781 return true;
782 }
783 else {
784 if (!IS_NOEVENT(keyp->event)) {
785 debug("Tapping: key event while last tap(>0).\n");
786 }
787 process_action(keyp);
788 return true;
789 }
790 }
791 }
792 // after TAPPING_TERM
793 else {
794 if (tapping_key.tap.count == 0) {
795 debug("Tapping: End. Timeout. Not tap(0): ");
796 debug_event(event); debug("\n");
797 process_action(&tapping_key);
798 tapping_key = (keyrecord_t){};
799 debug_tapping_key();
800 return false;
801 } else {
802 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
803 debug("Tapping: End. last timeout tap release(>0).");
804 keyp->tap = tapping_key.tap;
805 process_action(keyp);
806 tapping_key = (keyrecord_t){};
807 return true;
808 }
809 else if (is_tap_key(keyp->event.key) && event.pressed) {
810 if (tapping_key.tap.count > 1) {
811 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
812 // unregister key
813 process_action(&(keyrecord_t){
814 .tap = tapping_key.tap,
815 .event.key = tapping_key.event.key,
816 .event.time = event.time,
817 .event.pressed = false
818 });
819 } else {
820 debug("Tapping: Start while last timeout tap(1).\n");
821 }
822 tapping_key = *keyp;
823 waiting_buffer_scan_tap();
824 debug_tapping_key();
825 return true;
826 }
827 else {
828 if (!IS_NOEVENT(keyp->event)) {
829 debug("Tapping: key event while last timeout tap(>0).\n");
830 }
831 process_action(keyp);
832 return true;
833 }
834 }
835 }
836 } else if (IS_TAPPING_RELEASED()) {
837 if (WITHIN_TAPPING_TERM(event)) {
838 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
839 // sequential tap.
840 keyp->tap = tapping_key.tap;
841 keyp->tap.count += 1;
842 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
843 process_action(keyp);
844 tapping_key = *keyp;
845 debug_tapping_key();
846 return true;
847 } else if (event.pressed && is_tap_key(event.key)) {
848 // Sequential tap can be interfered with other tap key.
849 debug("Tapping: Start with interfering other tap.\n");
850 tapping_key = *keyp;
851 waiting_buffer_scan_tap();
852 debug_tapping_key();
853 return true;
854 } else {
855 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
856 process_action(keyp);
857 return true;
858 }
859 } else {
860 // timeout. no sequential tap.
861 debug("Tapping: End(Timeout after releasing last tap): ");
862 debug_event(event); debug("\n");
863 tapping_key = (keyrecord_t){};
864 debug_tapping_key();
865 return false;
866 }
867 }
868 // not tapping satate
869 else {
870 if (event.pressed && is_tap_key(event.key)) {
871 debug("Tapping: Start(Press tap key).\n");
872 tapping_key = *keyp;
873 waiting_buffer_scan_tap();
874 debug_tapping_key();
875 return true;
876 } else {
877 process_action(keyp);
878 return true;
879 }
880 }
881 }
882
883 /* scan buffer for tapping */
884 static void waiting_buffer_scan_tap(void)
885 {
886 // tapping already is settled
887 if (tapping_key.tap.count > 0) return;
888 // invalid state: tapping_key released && tap.count == 0
889 if (!tapping_key.event.pressed) return;
890
891 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
892 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
893 !waiting_buffer[i].event.pressed &&
894 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
895 tapping_key.tap.count = 1;
896 waiting_buffer[i].tap.count = 1;
897 process_action(&tapping_key);
898
899 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
900 debug_waiting_buffer();
901 return;
902 }
903 }
904 }
905 #endif
906
907
908
909 /*
910 * Utilities for actions.
911 */
912 void register_code(uint8_t code)
913 {
914 if (code == KC_NO) {
915 return;
916 }
917 #ifdef CAPSLOCK_LOCKING_ENABLE
918 else if (KC_LOCKING_CAPS == code) {
919 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
920 // Resync: ignore if caps lock already is on
921 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
922 #endif
923 host_add_key(KC_CAPSLOCK);
924 host_send_keyboard_report();
925 host_del_key(KC_CAPSLOCK);
926 host_send_keyboard_report();
927 }
928 #endif
929 else if IS_KEY(code) {
930 // TODO: should push command_proc out of this block?
931 if (command_proc(code)) return;
932
933 #ifndef NO_ACTION_TAPPING
934 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
935 uint8_t tmp_mods = host_get_mods();
936 host_add_mods(oneshot_state.mods);
937 host_add_key(code);
938 host_send_keyboard_report();
939
940 host_set_mods(tmp_mods);
941 oneshot_state.ready = false;
942 } else
943 #endif
944 {
945 host_add_key(code);
946 host_send_keyboard_report();
947 }
948 }
949 else if IS_MOD(code) {
950 host_add_mods(MOD_BIT(code));
951 host_send_keyboard_report();
952 }
953 }
954
955 void unregister_code(uint8_t code)
956 {
957 if (code == KC_NO) {
958 return;
959 }
960 #ifdef CAPSLOCK_LOCKING_ENABLE
961 else if (KC_LOCKING_CAPS == code) {
962 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
963 // Resync: ignore if caps lock already is off
964 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
965 #endif
966 host_add_key(KC_CAPSLOCK);
967 host_send_keyboard_report();
968 host_del_key(KC_CAPSLOCK);
969 host_send_keyboard_report();
970 }
971 #endif
972 else if IS_KEY(code) {
973 host_del_key(code);
974 host_send_keyboard_report();
975 }
976 else if IS_MOD(code) {
977 host_del_mods(MOD_BIT(code));
978 host_send_keyboard_report();
979 }
980 }
981
982 void add_mods(uint8_t mods)
983 {
984 if (mods) {
985 host_add_mods(mods);
986 host_send_keyboard_report();
987 }
988 }
989
990 void del_mods(uint8_t mods)
991 {
992 if (mods) {
993 host_del_mods(mods);
994 host_send_keyboard_report();
995 }
996 }
997
998 void set_mods(uint8_t mods)
999 {
1000 host_set_mods(mods);
1001 host_send_keyboard_report();
1002 }
1003
1004 void clear_keyboard(void)
1005 {
1006 host_clear_mods();
1007 clear_keyboard_but_mods();
1008 }
1009
1010 void clear_keyboard_but_mods(void)
1011 {
1012 host_clear_keys();
1013 host_send_keyboard_report();
1014 #ifdef MOUSEKEY_ENABLE
1015 mousekey_clear();
1016 mousekey_send();
1017 #endif
1018 #ifdef EXTRAKEY_ENABLE
1019 host_system_send(0);
1020 host_consumer_send(0);
1021 #endif
1022 }
1023
1024 bool sending_anykey(void)
1025 {
1026 return (host_has_anykey() || host_mouse_in_use() ||
1027 host_last_sysytem_report() || host_last_consumer_report());
1028 }
1029
1030 bool is_tap_key(key_t key)
1031 {
1032 action_t action = layer_switch_get_action(key);
1033
1034 switch (action.kind.id) {
1035 case ACT_LMODS_TAP:
1036 case ACT_RMODS_TAP:
1037 return true;
1038 case ACT_KEYMAP:
1039 case ACT_OVERLAY:
1040 switch (action.layer.code) {
1041 case 0x04 ... 0xEF: /* tap key */
1042 case OP_INV:
1043 return true;
1044 default:
1045 return false;
1046 }
1047 case ACT_MACRO:
1048 case ACT_FUNCTION:
1049 if (action.func.opt & FUNC_TAP) { return true; }
1050 return false;
1051 }
1052 return false;
1053 }
1054
1055
1056 /*
1057 * debug print
1058 */
1059 static void debug_event(keyevent_t event)
1060 {
1061 debug_hex16((event.key.row<<8) | event.key.col);
1062 if (event.pressed) debug("d("); else debug("u(");
1063 debug_dec(event.time); debug(")");
1064 }
1065 static void debug_record(keyrecord_t record)
1066 {
1067 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1068 if (record.tap.interrupted) debug("-");
1069 }
1070 static void debug_action(action_t action)
1071 {
1072 switch (action.kind.id) {
1073 case ACT_LMODS: debug("ACT_LMODS"); break;
1074 case ACT_RMODS: debug("ACT_RMODS"); break;
1075 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1076 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1077 case ACT_USAGE: debug("ACT_USAGE"); break;
1078 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1079 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1080 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1081 case ACT_MACRO: debug("ACT_MACRO"); break;
1082 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1083 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1084 default: debug("UNKNOWN"); break;
1085 }
1086 debug("[");
1087 debug_hex4(action.kind.param>>8);
1088 debug(":");
1089 debug_hex8(action.kind.param & 0xff);
1090 debug("]");
1091 }
1092 #ifndef NO_ACTION_TAPPING
1093 static void debug_tapping_key(void)
1094 {
1095 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1096 }
1097 static void debug_waiting_buffer(void)
1098 {
1099 debug("{ ");
1100 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1101 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
1102 }
1103 debug("}\n");
1104 }
1105 #endif
Imprint / Impressum