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