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