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