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