Fix bit shift which is beyond int size(16bit)
[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
28
29 /* default layer indicates base layer */
30 uint8_t default_layer = 0;
31 /* current layer indicates active layer at this time */
32 uint8_t current_layer = 0;
33
34
35 static void process_action(keyrecord_t *record);
36 static bool process_tapping(keyrecord_t *record);
37 static void waiting_buffer_scan_tap(void);
38
39 static void debug_event(keyevent_t event);
40 static void debug_record(keyrecord_t record);
41 static void debug_action(action_t action);
42 static void debug_tapping_key(void);
43 static void debug_waiting_buffer(void);
44
45
46 /*
47 * Tapping
48 */
49 /* period of tapping(ms) */
50 #ifndef TAPPING_TERM
51 #define TAPPING_TERM 200
52 #endif
53
54 /* tap count needed for toggling a feature */
55 #ifndef TAPPING_TOGGLE
56 #define TAPPING_TOGGLE 5
57 #endif
58
59 /* stores a key event of current tap. */
60 static keyrecord_t tapping_key = {};
61
62 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
63 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
64 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
65 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
66 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
67
68
69 /*
70 * Waiting buffer
71 *
72 * stores key events waiting for settling current tap.
73 */
74 #define WAITING_BUFFER_SIZE 8
75 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
76
77 /* point to empty cell to enq */
78 static uint8_t waiting_buffer_head = 0;
79
80 /* point to the oldest data cell to deq */
81 static uint8_t waiting_buffer_tail = 0;
82
83 static bool waiting_buffer_enq(keyrecord_t record)
84 {
85 if (IS_NOEVENT(record.event)) {
86 return true;
87 }
88
89 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
90 debug("waiting_buffer_enq: Over flow.\n");
91 return false;
92 }
93
94 waiting_buffer[waiting_buffer_head] = record;
95 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
96
97 debug("waiting_buffer_enq: "); debug_waiting_buffer();
98 return true;
99 }
100
101 static void waiting_buffer_clear(void)
102 {
103 waiting_buffer_head = 0;
104 waiting_buffer_tail = 0;
105 }
106
107 #if TAPPING_TERM >= 500
108 static bool waiting_buffer_typed(keyevent_t event)
109 {
110 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
111 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
112 return true;
113 }
114 }
115 return false;
116 }
117 #endif
118
119 bool waiting_buffer_has_anykey_pressed(void)
120 {
121 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
122 if (waiting_buffer[i].event.pressed) return true;
123 }
124 return false;
125 }
126
127
128 /* Oneshot modifier
129 *
130 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
131 * Solution: Oneshot modifier have its effect on only one key coming next.
132 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
133 *
134 * Hold: works as normal modifier.
135 * Tap: one shot modifier.
136 * 2 Tap: cancel one shot modifier.
137 * 5-Tap: toggles enable/disable oneshot feature.
138 */
139 static struct {
140 uint8_t mods;
141 uint8_t time;
142 bool ready;
143 bool disabled;
144 } oneshot_state;
145
146 static void oneshot_start(uint8_t mods, uint16_t time)
147 {
148 oneshot_state.mods = mods;
149 oneshot_state.time = time;
150 oneshot_state.ready = true;
151 }
152
153 static void oneshot_cancel(void)
154 {
155 oneshot_state.mods = 0;
156 oneshot_state.time = 0;
157 oneshot_state.ready = false;
158 }
159
160 static void oneshot_toggle(void)
161 {
162 oneshot_state.disabled = !oneshot_state.disabled;
163 }
164
165
166
167 void action_exec(keyevent_t event)
168 {
169 if (!IS_NOEVENT(event)) {
170 debug("\n---- action_exec: start -----\n");
171 debug("EVENT: "); debug_event(event); debug("\n");
172 }
173
174 keyrecord_t record = { .event = event };
175
176 // pre-process on tapping
177 if (process_tapping(&record)) {
178 if (!IS_NOEVENT(record.event)) {
179 debug("processed: "); debug_record(record); debug("\n");
180 }
181 } else {
182 // enqueue
183 if (!waiting_buffer_enq(record)) {
184 // clear all in case of overflow.
185 debug("OVERFLOW: CLEAR ALL STATES\n");
186 clear_keyboard();
187 waiting_buffer_clear();
188 tapping_key = (keyrecord_t){};
189 }
190 }
191
192 // process waiting_buffer
193 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
194 debug("---- action_exec: process waiting_buffer -----\n");
195 }
196
197 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
198 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
199 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
200 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
201 } else {
202 break;
203 }
204 }
205 if (!IS_NOEVENT(event)) {
206 debug("\n");
207 }
208 }
209
210 static action_t get_action(key_t key)
211 {
212 action_t action = action_for_key(current_layer, key);
213
214 /* Transparently use default layer */
215 if (action.code == ACTION_TRANSPARENT) {
216 // TODO: layer stacking
217 action = action_for_key(default_layer, key);
218 debug("TRNASPARENT: "); debug_hex16(action.code); debug("\n");
219 }
220 return action;
221 }
222
223 static void process_action(keyrecord_t *record)
224 {
225 keyevent_t event = record->event;
226 uint8_t tap_count = record->tap_count;
227
228 if (IS_NOEVENT(event)) { return; }
229
230 action_t action = get_action(event.key);
231 debug("ACTION: "); debug_action(action); debug("\n");
232
233 switch (action.kind.id) {
234 /* Key and Mods */
235 case ACT_LMODS:
236 case ACT_RMODS:
237 {
238 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
239 action.key.mods<<4;
240 if (event.pressed) {
241 uint8_t tmp_mods = host_get_mods();
242 if (mods) {
243 host_add_mods(mods);
244 host_send_keyboard_report();
245 }
246 register_code(action.key.code);
247 if (mods && action.key.code) {
248 host_set_mods(tmp_mods);
249 host_send_keyboard_report();
250 }
251 } else {
252 if (mods && !action.key.code) {
253 host_del_mods(mods);
254 host_send_keyboard_report();
255 }
256 unregister_code(action.key.code);
257 }
258 }
259 break;
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 by holding.
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
336 /* other HID usage */
337 case ACT_USAGE:
338 #ifdef EXTRAKEY_ENABLE
339 switch (action.usage.page) {
340 case PAGE_SYSTEM:
341 if (event.pressed) {
342 host_system_send(action.usage.code);
343 } else {
344 host_system_send(0);
345 }
346 break;
347 case PAGE_CONSUMER:
348 if (event.pressed) {
349 host_consumer_send(action.usage.code);
350 } else {
351 host_consumer_send(0);
352 }
353 break;
354 }
355 #endif
356 break;
357
358 /* Mouse key */
359 case ACT_MOUSEKEY:
360 #ifdef MOUSEKEY_ENABLE
361 if (event.pressed) {
362 mousekey_on(action.key.code);
363 mousekey_send();
364 } else {
365 mousekey_off(action.key.code);
366 mousekey_send();
367 }
368 #endif
369 break;
370
371 /* Layer key */
372 case ACT_LAYER:
373 switch (action.layer.code) {
374 case LAYER_MOMENTARY: /* momentary */
375 if (event.pressed) {
376 layer_switch(action.layer.val);
377 }
378 else {
379 // NOTE: This is needed by legacy keymap support
380 layer_switch(default_layer);
381 }
382 break;
383 case LAYER_ON_PRESS:
384 if (event.pressed) {
385 layer_switch(action.layer.val);
386 }
387 break;
388 case LAYER_ON_RELEASE:
389 if (!event.pressed) {
390 layer_switch(action.layer.val);
391 }
392 break;
393 case LAYER_DEFAULT: /* default layer */
394 switch (action.layer.val) {
395 case DEFAULT_ON_BOTH:
396 layer_switch(default_layer);
397 break;
398 case DEFAULT_ON_PRESS:
399 if (event.pressed) {
400 layer_switch(default_layer);
401 }
402 break;
403 case DEFAULT_ON_RELEASE:
404 if (!event.pressed) {
405 layer_switch(default_layer);
406 }
407 break;
408 }
409 break;
410 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
411 if (event.pressed) {
412 if (tap_count < TAPPING_TOGGLE) {
413 layer_switch(action.layer.val);
414 }
415 } else {
416 if (tap_count >= TAPPING_TOGGLE) {
417 debug("LAYER_PRESSED: tap toggle.\n");
418 layer_switch(action.layer.val);
419 }
420 }
421 break;
422 case LAYER_CHANGE_DEFAULT: /* change default layer */
423 if (event.pressed) {
424 default_layer = action.layer.val;
425 layer_switch(default_layer);
426 }
427 break;
428 default: /* switch layer on hold and key on tap*/
429 if (event.pressed) {
430 if (tap_count > 0) {
431 debug("LAYER_PRESSED: Tap: register_code\n");
432 register_code(action.layer.code);
433 } else {
434 debug("LAYER_PRESSED: No tap: layer_switch\n");
435 layer_switch(action.layer.val);
436 }
437 } else {
438 if (tap_count > 0) {
439 debug("LAYER_PRESSED: Tap: unregister_code\n");
440 unregister_code(action.layer.code);
441 } else {
442 //debug("LAYER_PRESSED: No tap: NO ACTION\n");
443 // NOTE: This is needed by legacy keymap support
444 debug("LAYER_PRESSED: No tap: return to default layer\n");
445 layer_switch(default_layer);
446 }
447 }
448 break;
449 }
450 break;
451 case ACT_LAYER_BIT:
452 switch (action.layer.code) {
453 case LAYER_MOMENTARY: /* momentary */
454 if (event.pressed) {
455 layer_switch(current_layer ^ action.layer.val);
456 } else {
457 layer_switch(current_layer ^ action.layer.val);
458 }
459 break;
460 case LAYER_ON_PRESS:
461 if (event.pressed) {
462 layer_switch(current_layer ^ action.layer.val);
463 }
464 break;
465 case LAYER_ON_RELEASE:
466 if (!event.pressed) {
467 layer_switch(current_layer ^ action.layer.val);
468 }
469 break;
470 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
471 if (event.pressed) {
472 if (tap_count < TAPPING_TOGGLE) {
473 debug("LAYER_BIT: tap toggle(press).\n");
474 layer_switch(current_layer ^ action.layer.val);
475 }
476 } else {
477 if (tap_count <= TAPPING_TOGGLE) {
478 debug("LAYER_BIT: tap toggle(release).\n");
479 layer_switch(current_layer ^ action.layer.val);
480 }
481 }
482 break;
483 case 0xFF:
484 // change default layer
485 if (event.pressed) {
486 default_layer = current_layer ^ action.layer.val;
487 layer_switch(default_layer);
488 } else {
489 default_layer = current_layer ^ action.layer.val;
490 layer_switch(default_layer);
491 }
492 break;
493 default:
494 // with tap key
495 if (event.pressed) {
496 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
497 debug("LAYER_BIT: Tap: register_code\n");
498 register_code(action.layer.code);
499 } else {
500 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
501 layer_switch(current_layer ^ action.layer.val);
502 }
503 } else {
504 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
505 debug("LAYER_BIT: Tap: unregister_code\n");
506 unregister_code(action.layer.code);
507 } else {
508 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
509 layer_switch(current_layer ^ action.layer.val);
510 }
511 }
512 break;
513 }
514 break;
515
516 /* Extentions */
517 case ACT_MACRO:
518 // TODO
519 break;
520 case ACT_COMMAND:
521 break;
522 case ACT_FUNCTION:
523 action_function(record, action.func.id, action.func.opt);
524 break;
525 default:
526 break;
527 }
528 }
529
530 /* Tapping
531 *
532 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
533 * (without interfering by typing other key)
534 */
535 /* return true when key event is processed or consumed. */
536 static bool process_tapping(keyrecord_t *keyp)
537 {
538 keyevent_t event = keyp->event;
539
540 // if tapping
541 if (IS_TAPPING_PRESSED()) {
542 if (WITHIN_TAPPING_TERM(event)) {
543 if (tapping_key.tap_count == 0) {
544 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
545 // first tap!
546 debug("Tapping: First tap(0->1).\n");
547 tapping_key.tap_count = 1;
548 debug_tapping_key();
549 process_action(&tapping_key);
550
551 // enqueue
552 keyp->tap_count = tapping_key.tap_count;
553 return false;
554 }
555 #if TAPPING_TERM >= 500
556 /* This can prevent from typing some tap keys in a row at a time. */
557 else if (!event.pressed && waiting_buffer_typed(event)) {
558 // other key typed. not tap.
559 debug("Tapping: End. No tap. Interfered by typing key\n");
560 process_action(&tapping_key);
561 tapping_key = (keyrecord_t){};
562 debug_tapping_key();
563
564 // enqueue
565 return false;
566 }
567 #endif
568 else {
569 // other key events shall be enq'd till tapping state settles.
570 return false;
571 }
572 }
573 // tap_count > 0
574 else {
575 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
576 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
577 keyp->tap_count = tapping_key.tap_count;
578 process_action(keyp);
579 tapping_key = *keyp;
580 debug_tapping_key();
581 return true;
582 }
583 else if (is_tap_key(keyp->event.key) && event.pressed) {
584 if (tapping_key.tap_count > 1) {
585 debug("Tapping: Start new tap with releasing last tap(>1).\n");
586 // unregister key
587 process_action(&(keyrecord_t){
588 .tap_count = tapping_key.tap_count,
589 .event.key = tapping_key.event.key,
590 .event.time = event.time,
591 .event.pressed = false
592 });
593 } else {
594 debug("Tapping: Start while last tap(1).\n");
595 }
596 tapping_key = *keyp;
597 waiting_buffer_scan_tap();
598 debug_tapping_key();
599 return true;
600 }
601 else {
602 if (!IS_NOEVENT(keyp->event)) {
603 debug("Tapping: key event while last tap(>0).\n");
604 }
605 process_action(keyp);
606 return true;
607 }
608 }
609 }
610 // after TAPPING_TERM
611 else {
612 if (tapping_key.tap_count == 0) {
613 debug("Tapping: End. Timeout. Not tap(0): ");
614 debug_event(event); debug("\n");
615 process_action(&tapping_key);
616 tapping_key = (keyrecord_t){};
617 debug_tapping_key();
618 return false;
619 } else {
620 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
621 debug("Tapping: End. last timeout tap release(>0).");
622 keyp->tap_count = tapping_key.tap_count;
623 process_action(keyp);
624 tapping_key = (keyrecord_t){};
625 return true;
626 }
627 else if (is_tap_key(keyp->event.key) && event.pressed) {
628 if (tapping_key.tap_count > 1) {
629 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
630 // unregister key
631 process_action(&(keyrecord_t){
632 .tap_count = tapping_key.tap_count,
633 .event.key = tapping_key.event.key,
634 .event.time = event.time,
635 .event.pressed = false
636 });
637 } else {
638 debug("Tapping: Start while last timeout tap(1).\n");
639 }
640 tapping_key = *keyp;
641 waiting_buffer_scan_tap();
642 debug_tapping_key();
643 return true;
644 }
645 else {
646 if (!IS_NOEVENT(keyp->event)) {
647 debug("Tapping: key event while last timeout tap(>0).\n");
648 }
649 process_action(keyp);
650 return true;
651 }
652 }
653 }
654 } else if (IS_TAPPING_RELEASED()) {
655 if (WITHIN_TAPPING_TERM(event)) {
656 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
657 // sequential tap.
658 keyp->tap_count = tapping_key.tap_count + 1;
659 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
660 process_action(keyp);
661 tapping_key = *keyp;
662 debug_tapping_key();
663 return true;
664 } else if (event.pressed && is_tap_key(event.key)) {
665 // Sequential tap can be interfered with other tap key.
666 debug("Tapping: Start with interfering other tap.\n");
667 tapping_key = *keyp;
668 waiting_buffer_scan_tap();
669 debug_tapping_key();
670 return true;
671 } else {
672 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
673 process_action(keyp);
674 return true;
675 }
676 } else {
677 // timeout. no sequential tap.
678 debug("Tapping: End(Timeout after releasing last tap): ");
679 debug_event(event); debug("\n");
680 tapping_key = (keyrecord_t){};
681 debug_tapping_key();
682 return false;
683 }
684 }
685 // not tapping satate
686 else {
687 if (event.pressed && is_tap_key(event.key)) {
688 debug("Tapping: Start(Press tap key).\n");
689 tapping_key = *keyp;
690 waiting_buffer_scan_tap();
691 debug_tapping_key();
692 return true;
693 } else {
694 process_action(keyp);
695 return true;
696 }
697 }
698 }
699
700 /* scan buffer for tapping */
701 static void waiting_buffer_scan_tap(void)
702 {
703 // tapping already is settled
704 if (tapping_key.tap_count > 0) return;
705 // invalid state: tapping_key released && tap_count == 0
706 if (!tapping_key.event.pressed) return;
707
708 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
709 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
710 !waiting_buffer[i].event.pressed &&
711 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
712 tapping_key.tap_count = 1;
713 waiting_buffer[i].tap_count = 1;
714 process_action(&tapping_key);
715
716 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
717 debug_waiting_buffer();
718 return;
719 }
720 }
721 }
722
723
724
725 /*
726 * Utilities for actions.
727 */
728 void register_code(uint8_t code)
729 {
730 if (code == KC_NO) {
731 return;
732 }
733 else if IS_KEY(code) {
734 // TODO: should push command_proc out of this block?
735 if (command_proc(code)) return;
736
737 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
738 uint8_t tmp_mods = host_get_mods();
739 host_add_mods(oneshot_state.mods);
740 host_add_key(code);
741 host_send_keyboard_report();
742
743 host_set_mods(tmp_mods);
744 oneshot_state.ready = false;
745 } else {
746 host_add_key(code);
747 host_send_keyboard_report();
748 }
749 }
750 else if IS_MOD(code) {
751 host_add_mods(MOD_BIT(code));
752 host_send_keyboard_report();
753 }
754 }
755
756 void unregister_code(uint8_t code)
757 {
758 if IS_KEY(code) {
759 host_del_key(code);
760 host_send_keyboard_report();
761 }
762 else if IS_MOD(code) {
763 host_del_mods(MOD_BIT(code));
764 host_send_keyboard_report();
765 }
766 }
767
768 void add_mods(uint8_t mods)
769 {
770 if (mods) {
771 host_add_mods(mods);
772 host_send_keyboard_report();
773 }
774 }
775
776 void del_mods(uint8_t mods)
777 {
778 if (mods) {
779 host_del_mods(mods);
780 host_send_keyboard_report();
781 }
782 }
783
784 void set_mods(uint8_t mods)
785 {
786 host_set_mods(mods);
787 host_send_keyboard_report();
788 }
789
790 void clear_keyboard(void)
791 {
792 host_clear_mods();
793 clear_keyboard_but_mods();
794 }
795
796 void clear_keyboard_but_mods(void)
797 {
798 host_clear_keys();
799 host_send_keyboard_report();
800 #ifdef MOUSEKEY_ENABLE
801 mousekey_clear();
802 mousekey_send();
803 #endif
804 #ifdef EXTRAKEY_ENABLE
805 host_system_send(0);
806 host_consumer_send(0);
807 #endif
808 }
809
810 bool sending_anykey(void)
811 {
812 return (host_has_anykey() || host_mouse_in_use() ||
813 host_last_sysytem_report() || host_last_consumer_report());
814 }
815
816 void layer_switch(uint8_t new_layer)
817 {
818 if (current_layer != new_layer) {
819 debug("Layer Switch: "); debug_hex(current_layer);
820 debug(" -> "); debug_hex(new_layer); debug("\n");
821
822 current_layer = new_layer;
823 clear_keyboard_but_mods(); // To avoid stuck keys
824 // NOTE: update mods with full scan of matrix? if modifier changes between layers
825 }
826 }
827
828 bool is_tap_key(key_t key)
829 {
830 action_t action = get_action(key);
831
832 switch (action.kind.id) {
833 case ACT_LMODS_TAP:
834 case ACT_RMODS_TAP:
835 return true;
836 case ACT_LAYER:
837 case ACT_LAYER_BIT:
838 switch (action.layer.code) {
839 case LAYER_MOMENTARY:
840 case LAYER_ON_PRESS:
841 case LAYER_ON_RELEASE:
842 case LAYER_DEFAULT:
843 return false;
844 case LAYER_TAP_TOGGLE:
845 default: /* tap key */
846 return true;
847 }
848 return false;
849 case ACT_FUNCTION:
850 if (action.func.opt & FUNC_TAP) { return true; }
851 return false;
852 }
853 return false;
854 }
855
856
857 /*
858 * debug print
859 */
860 static void debug_event(keyevent_t event)
861 {
862 debug_hex16((event.key.row<<8) | event.key.col);
863 if (event.pressed) debug("d("); else debug("u(");
864 debug_dec(event.time); debug(")");
865 }
866 static void debug_record(keyrecord_t record)
867 {
868 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
869 }
870 static void debug_action(action_t action)
871 {
872 switch (action.kind.id) {
873 case ACT_LMODS: debug("ACT_LMODS"); break;
874 case ACT_RMODS: debug("ACT_RMODS"); break;
875 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
876 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
877 case ACT_USAGE: debug("ACT_USAGE"); break;
878 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
879 case ACT_LAYER: debug("ACT_LAYER"); break;
880 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
881 case ACT_MACRO: debug("ACT_MACRO"); break;
882 case ACT_COMMAND: debug("ACT_COMMAND"); break;
883 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
884 default: debug("UNKNOWN"); break;
885 }
886 debug("[");
887 debug_hex4(action.kind.param>>8);
888 debug(":");
889 debug_hex8(action.kind.param & 0xff);
890 debug("]");
891 }
892 static void debug_tapping_key(void)
893 {
894 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
895 }
896 static void debug_waiting_buffer(void)
897 {
898 debug("{ ");
899 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
900 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
901 }
902 debug("}\n");
903 }
Imprint / Impressum