]> git.gir.st - tmk_keyboard.git/blob - common/action.c
Change: 0 means default_layer in current_layer now
[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 * Layer stack
168 */
169 #define LAYER_STACK_SIZE 8
170 typedef struct {
171 uint8_t layer:4;
172 uint8_t next:3;
173 bool used;
174 } layer_item_t;
175
176 static uint8_t top_layer = 0;
177 // [0] is sentinel and not used. [0] is null item.
178 static layer_item_t layer_stack[LAYER_STACK_SIZE] = {};
179
180 static bool layer_push(uint8_t layer)
181 {
182 for (uint8_t i = 1; i < LAYER_STACK_SIZE; i++) {
183 if (!layer_stack[i].used) {
184 layer_stack[i] = (layer_item_t){ .layer = layer,
185 .next = top_layer,
186 .used = true };
187 top_layer = i;
188 return true;
189 }
190 }
191 return false;
192 }
193 static bool layer_pop(void)
194 {
195 if (layer_stack[top_layer].used) {
196 uint8_t popped = top_layer;
197 top_layer = layer_stack[popped].next;
198 layer_stack[popped] = (layer_item_t){};
199 return true;
200 }
201 return false;
202 }
203 static bool layer_remove(uint8_t layer)
204 {
205 if (layer_stack[top_layer].used && layer_stack[top_layer].layer == layer) {
206 layer_pop();
207 debug("layer_remove: top_layer\n");
208 return true;
209 }
210
211 for (uint8_t i = top_layer; layer_stack[i].used; i = layer_stack[i].next) {
212 debug("layer_remove: ["); debug_dec(i); debug("]");
213 debug_dec(layer_stack[i].layer); debug("\n");
214 uint8_t removed = layer_stack[i].next;
215 if (layer_stack[removed].used && layer_stack[removed].layer == layer) {
216 layer_stack[i].next = layer_stack[removed].next;
217 layer_stack[removed] = (layer_item_t){};
218 debug("layer_remove: removed.\n");
219 return true;
220 }
221 }
222 return false;
223 }
224 static bool layer_remove_then_push(uint8_t layer)
225 {
226 layer_remove(layer);
227 return layer_push(layer);
228 }
229 static bool layer_remove_or_push(uint8_t layer)
230 {
231 return (layer_remove(layer)) || layer_push(layer);
232 }
233 static void debug_layer_stack(void)
234 {
235 debug("layer_stack: ");
236 layer_item_t item = layer_stack[top_layer];
237 while (item.used) {
238 debug_dec(item.layer);
239 debug("["); debug_dec(item.next); debug("]");
240 item = layer_stack[item.next];
241 }
242 debug("\n");
243 }
244
245
246 void action_exec(keyevent_t event)
247 {
248 if (!IS_NOEVENT(event)) {
249 debug("\n---- action_exec: start -----\n");
250 debug("EVENT: "); debug_event(event); debug("\n");
251 }
252
253 keyrecord_t record = { .event = event };
254
255 // pre-process on tapping
256 if (process_tapping(&record)) {
257 if (!IS_NOEVENT(record.event)) {
258 debug("processed: "); debug_record(record); debug("\n");
259 }
260 } else {
261 // enqueue
262 if (!waiting_buffer_enq(record)) {
263 // clear all in case of overflow.
264 debug("OVERFLOW: CLEAR ALL STATES\n");
265 clear_keyboard();
266 waiting_buffer_clear();
267 tapping_key = (keyrecord_t){};
268 }
269 }
270
271 // process waiting_buffer
272 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
273 debug("---- action_exec: process waiting_buffer -----\n");
274 }
275
276 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
277 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
278 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
279 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
280 } else {
281 break;
282 }
283 }
284 if (!IS_NOEVENT(event)) {
285 debug("\n");
286 }
287 }
288
289 static action_t get_action(key_t key)
290 {
291 action_t action;
292 action.code = ACTION_NO;
293
294 /* layer stack */
295 for (layer_item_t i = layer_stack[top_layer]; i.used; i = layer_stack[i.next]) {
296 action = action_for_key(i.layer, key);
297 if (action.code != ACTION_TRANSPARENT) {
298 debug_layer_stack();
299 debug("layer_stack: used. "); debug_dec(i.layer); debug("\n");
300 return action;
301 }
302 debug("layer_stack: through. "); debug_dec(i.layer); debug("\n");
303 }
304
305 /* current layer: 0 means default layer */
306 if (current_layer) {
307 action = action_for_key(current_layer, key);
308 if (action.code != ACTION_TRANSPARENT) {
309 debug("current layer: used. "); debug_dec(current_layer); debug("\n");
310 return action;
311 }
312 }
313
314 /* default layer */
315 debug("default layer: used. \n");
316 action = action_for_key(default_layer, key);
317 return action;
318 }
319
320 static void process_action(keyrecord_t *record)
321 {
322 keyevent_t event = record->event;
323 uint8_t tap_count = record->tap_count;
324
325 if (IS_NOEVENT(event)) { return; }
326
327 action_t action = get_action(event.key);
328 debug("ACTION: "); debug_action(action); debug("\n");
329
330 switch (action.kind.id) {
331 /* Key and Mods */
332 case ACT_LMODS:
333 case ACT_RMODS:
334 {
335 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
336 action.key.mods<<4;
337 if (event.pressed) {
338 uint8_t tmp_mods = host_get_mods();
339 if (mods) {
340 host_add_mods(mods);
341 host_send_keyboard_report();
342 }
343 register_code(action.key.code);
344 if (mods && action.key.code) {
345 host_set_mods(tmp_mods);
346 host_send_keyboard_report();
347 }
348 } else {
349 if (mods && !action.key.code) {
350 host_del_mods(mods);
351 host_send_keyboard_report();
352 }
353 unregister_code(action.key.code);
354 }
355 }
356 break;
357 case ACT_LMODS_TAP:
358 case ACT_RMODS_TAP:
359 {
360 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
361 action.key.mods<<4;
362 switch (action.layer.code) {
363 case 0x00:
364 // Oneshot modifier
365 if (event.pressed) {
366 if (tap_count == 0) {
367 debug("MODS_TAP: Oneshot: add_mods\n");
368 add_mods(mods);
369 }
370 else if (tap_count == 1) {
371 debug("MODS_TAP: Oneshot: start\n");
372 oneshot_start(mods, event.time);
373 }
374 else if (tap_count == TAPPING_TOGGLE) {
375 debug("MODS_TAP: Oneshot: toggle\n");
376 oneshot_toggle();
377 }
378 else {
379 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
380 // double tap cancels oneshot and works as normal modifier.
381 oneshot_cancel();
382 add_mods(mods);
383 }
384 } else {
385 if (tap_count == 0) {
386 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
387 // cancel oneshot on hold
388 oneshot_cancel();
389 del_mods(mods);
390 }
391 else if (tap_count == 1) {
392 debug("MODS_TAP: Oneshot: del_mods\n");
393 // retain Oneshot
394 del_mods(mods);
395 }
396 else {
397 debug("MODS_TAP: Oneshot: del_mods\n");
398 // cancel Mods
399 del_mods(mods);
400 }
401 }
402 break;
403 default:
404 if (event.pressed) {
405 if (tap_count > 0) {
406 if (waiting_buffer_has_anykey_pressed()) {
407 debug("MODS_TAP: Tap: Cancel: add_mods\n");
408 // ad hoc: set 0 to cancel tap
409 record->tap_count = 0;
410 add_mods(mods);
411 } else {
412 debug("MODS_TAP: Tap: register_code\n");
413 register_code(action.key.code);
414 }
415 } else {
416 debug("MODS_TAP: No tap: add_mods\n");
417 add_mods(mods);
418 }
419 } else {
420 if (tap_count > 0) {
421 debug("MODS_TAP: Tap: unregister_code\n");
422 unregister_code(action.key.code);
423 } else {
424 debug("MODS_TAP: No tap: add_mods\n");
425 del_mods(mods);
426 }
427 }
428 break;
429 }
430 }
431 break;
432
433 /* other HID usage */
434 case ACT_USAGE:
435 #ifdef EXTRAKEY_ENABLE
436 switch (action.usage.page) {
437 case PAGE_SYSTEM:
438 if (event.pressed) {
439 host_system_send(action.usage.code);
440 } else {
441 host_system_send(0);
442 }
443 break;
444 case PAGE_CONSUMER:
445 if (event.pressed) {
446 host_consumer_send(action.usage.code);
447 } else {
448 host_consumer_send(0);
449 }
450 break;
451 }
452 #endif
453 break;
454
455 /* Mouse key */
456 case ACT_MOUSEKEY:
457 #ifdef MOUSEKEY_ENABLE
458 if (event.pressed) {
459 mousekey_on(action.key.code);
460 mousekey_send();
461 } else {
462 mousekey_off(action.key.code);
463 mousekey_send();
464 }
465 #endif
466 break;
467
468 /* Layer key */
469 case ACT_LAYER:
470 switch (action.layer.code) {
471 case LAYER_MOMENTARY: /* momentary */
472 if (event.pressed) {
473 layer_switch(action.layer.val);
474 }
475 else {
476 // NOTE: This is needed by legacy keymap support
477 layer_switch(0);
478 }
479 break;
480 case LAYER_ON_PRESS:
481 if (event.pressed) {
482 layer_switch(action.layer.val);
483 }
484 break;
485 case LAYER_ON_RELEASE:
486 if (!event.pressed) {
487 layer_switch(action.layer.val);
488 }
489 break;
490 case LAYER_ON_BOTH:
491 layer_switch(action.layer.val);
492 break;
493 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
494 if (event.pressed) {
495 if (tap_count < TAPPING_TOGGLE) {
496 layer_switch(action.layer.val);
497 }
498 } else {
499 if (tap_count >= TAPPING_TOGGLE) {
500 debug("LAYER_PRESSED: tap toggle.\n");
501 layer_switch(action.layer.val);
502 }
503 }
504 break;
505 case LAYER_SET_DEFAULT_ON_PRESS:
506 if (event.pressed) {
507 default_layer = action.layer.val;
508 layer_switch(0);
509 }
510 break;
511 case LAYER_SET_DEFAULT_ON_RELEASE:
512 if (!event.pressed) {
513 default_layer = action.layer.val;
514 layer_switch(0);
515 }
516 break;
517 case LAYER_SET_DEFAULT_ON_BOTH:
518 default_layer = action.layer.val;
519 layer_switch(0);
520 break;
521 default:
522 /* tap key */
523 if (event.pressed) {
524 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
525 debug("LAYER_SET: Tap: register_code\n");
526 register_code(action.layer.code);
527 } else {
528 debug("LAYER_SET: No tap: layer_set(on press)\n");
529 layer_switch(action.layer.val);
530 }
531 } else {
532 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
533 debug("LAYER_SET: Tap: unregister_code\n");
534 unregister_code(action.layer.code);
535 } else {
536 // NOTE: This is needed by legacy keymap support
537 debug("LAYER_SET: No tap: return to default layer(on release)\n");
538 layer_switch(0);
539 }
540 }
541 break;
542 }
543 break;
544 case ACT_LAYER_BIT:
545 switch (action.layer.code) {
546 case LAYER_MOMENTARY: /* momentary */
547 if (event.pressed) {
548 layer_switch(current_layer | action.layer.val);
549 } else {
550 layer_switch(current_layer & ~action.layer.val);
551 }
552 break;
553 case LAYER_ON_PRESS:
554 if (event.pressed) {
555 layer_switch(current_layer ^ action.layer.val);
556 }
557 break;
558 case LAYER_ON_RELEASE:
559 if (!event.pressed) {
560 layer_switch(current_layer ^ action.layer.val);
561 }
562 break;
563 case LAYER_ON_BOTH:
564 layer_switch(current_layer ^ action.layer.val);
565 break;
566 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
567 if (event.pressed) {
568 if (tap_count < TAPPING_TOGGLE) {
569 debug("LAYER_BIT: tap toggle(press).\n");
570 layer_switch(current_layer ^ action.layer.val);
571 }
572 } else {
573 if (tap_count <= TAPPING_TOGGLE) {
574 debug("LAYER_BIT: tap toggle(release).\n");
575 layer_switch(current_layer ^ action.layer.val);
576 }
577 }
578 break;
579 case LAYER_SET_DEFAULT_ON_PRESS:
580 if (event.pressed) {
581 default_layer = default_layer ^ action.layer.val;
582 layer_switch(0);
583 }
584 break;
585 case LAYER_SET_DEFAULT_ON_RELEASE:
586 if (!event.pressed) {
587 default_layer = default_layer ^ action.layer.val;
588 layer_switch(0);
589 }
590 break;
591 case LAYER_SET_DEFAULT_ON_BOTH:
592 default_layer = default_layer ^ action.layer.val;
593 layer_switch(0);
594 break;
595 default:
596 // tap key
597 if (event.pressed) {
598 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
599 debug("LAYER_BIT: Tap: register_code\n");
600 register_code(action.layer.code);
601 } else {
602 debug("LAYER_BIT: No tap: layer_bit(on press)\n");
603 layer_switch(current_layer ^ action.layer.val);
604 }
605 } else {
606 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
607 debug("LAYER_BIT: Tap: unregister_code\n");
608 unregister_code(action.layer.code);
609 } else {
610 debug("LAYER_BIT: No tap: layer_bit(on release)\n");
611 layer_switch(current_layer ^ action.layer.val);
612 }
613 }
614 break;
615 }
616 break;
617 case ACT_LAYER_STACK:
618 switch (action.layer.code) {
619 case LAYER_MOMENTARY: /* momentary */
620 if (event.pressed) {
621 layer_remove_then_push(action.layer.val);
622 debug_layer_stack();
623 } else {
624 layer_remove(action.layer.val);
625 debug_layer_stack();
626 }
627 break;
628 case LAYER_ON_PRESS:
629 if (event.pressed) {
630 layer_remove_or_push(action.layer.val);
631 debug_layer_stack();
632 }
633 break;
634 case LAYER_ON_RELEASE:
635 if (!event.pressed) {
636 layer_remove_or_push(action.layer.val);
637 debug_layer_stack();
638 }
639 break;
640 case LAYER_ON_BOTH:
641 layer_remove_or_push(action.layer.val);
642 debug_layer_stack();
643 break;
644 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
645 if (event.pressed) {
646 if (tap_count < TAPPING_TOGGLE) {
647 debug("LAYER_STACK: tap toggle(press).\n");
648 layer_remove_or_push(action.layer.val);
649 debug_layer_stack();
650 }
651 } else {
652 if (tap_count <= TAPPING_TOGGLE) {
653 debug("LAYER_STACK: tap toggle(release).\n");
654 layer_remove_or_push(action.layer.val);
655 debug_layer_stack();
656 }
657 }
658 break;
659 default:
660 // tap key
661 if (event.pressed) {
662 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
663 debug("LAYER_STACK: Tap: register_code\n");
664 register_code(action.layer.code);
665 } else {
666 debug("LAYER_STACK: No tap: layer_stack(on press)\n");
667 layer_remove_or_push(action.layer.val);
668 debug_layer_stack();
669 }
670 } else {
671 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
672 debug("LAYER_STACK: Tap: unregister_code\n");
673 unregister_code(action.layer.code);
674 } else {
675 debug("LAYER_STACK: No tap: layer_stack(on release)\n");
676 layer_remove_or_push(action.layer.val);
677 debug_layer_stack();
678 }
679 }
680 break;
681 }
682 break;
683
684 /* Extentions */
685 case ACT_MACRO:
686 // TODO
687 break;
688 case ACT_COMMAND:
689 break;
690 case ACT_FUNCTION:
691 action_function(record, action.func.id, action.func.opt);
692 break;
693 default:
694 break;
695 }
696 }
697
698 /* Tapping
699 *
700 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
701 * (without interfering by typing other key)
702 */
703 /* return true when key event is processed or consumed. */
704 static bool process_tapping(keyrecord_t *keyp)
705 {
706 keyevent_t event = keyp->event;
707
708 // if tapping
709 if (IS_TAPPING_PRESSED()) {
710 if (WITHIN_TAPPING_TERM(event)) {
711 if (tapping_key.tap_count == 0) {
712 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
713 // first tap!
714 debug("Tapping: First tap(0->1).\n");
715 tapping_key.tap_count = 1;
716 debug_tapping_key();
717 process_action(&tapping_key);
718
719 // enqueue
720 keyp->tap_count = tapping_key.tap_count;
721 return false;
722 }
723 #if TAPPING_TERM >= 500
724 /* This can prevent from typing some tap keys in a row at a time. */
725 else if (!event.pressed && waiting_buffer_typed(event)) {
726 // other key typed. not tap.
727 debug("Tapping: End. No tap. Interfered by typing key\n");
728 process_action(&tapping_key);
729 tapping_key = (keyrecord_t){};
730 debug_tapping_key();
731
732 // enqueue
733 return false;
734 }
735 #endif
736 else {
737 // other key events shall be enq'd till tapping state settles.
738 return false;
739 }
740 }
741 // tap_count > 0
742 else {
743 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
744 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
745 keyp->tap_count = tapping_key.tap_count;
746 process_action(keyp);
747 tapping_key = *keyp;
748 debug_tapping_key();
749 return true;
750 }
751 else if (is_tap_key(keyp->event.key) && event.pressed) {
752 if (tapping_key.tap_count > 1) {
753 debug("Tapping: Start new tap with releasing last tap(>1).\n");
754 // unregister key
755 process_action(&(keyrecord_t){
756 .tap_count = tapping_key.tap_count,
757 .event.key = tapping_key.event.key,
758 .event.time = event.time,
759 .event.pressed = false
760 });
761 } else {
762 debug("Tapping: Start while last tap(1).\n");
763 }
764 tapping_key = *keyp;
765 waiting_buffer_scan_tap();
766 debug_tapping_key();
767 return true;
768 }
769 else {
770 if (!IS_NOEVENT(keyp->event)) {
771 debug("Tapping: key event while last tap(>0).\n");
772 }
773 process_action(keyp);
774 return true;
775 }
776 }
777 }
778 // after TAPPING_TERM
779 else {
780 if (tapping_key.tap_count == 0) {
781 debug("Tapping: End. Timeout. Not tap(0): ");
782 debug_event(event); debug("\n");
783 process_action(&tapping_key);
784 tapping_key = (keyrecord_t){};
785 debug_tapping_key();
786 return false;
787 } else {
788 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
789 debug("Tapping: End. last timeout tap release(>0).");
790 keyp->tap_count = tapping_key.tap_count;
791 process_action(keyp);
792 tapping_key = (keyrecord_t){};
793 return true;
794 }
795 else if (is_tap_key(keyp->event.key) && event.pressed) {
796 if (tapping_key.tap_count > 1) {
797 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
798 // unregister key
799 process_action(&(keyrecord_t){
800 .tap_count = tapping_key.tap_count,
801 .event.key = tapping_key.event.key,
802 .event.time = event.time,
803 .event.pressed = false
804 });
805 } else {
806 debug("Tapping: Start while last timeout tap(1).\n");
807 }
808 tapping_key = *keyp;
809 waiting_buffer_scan_tap();
810 debug_tapping_key();
811 return true;
812 }
813 else {
814 if (!IS_NOEVENT(keyp->event)) {
815 debug("Tapping: key event while last timeout tap(>0).\n");
816 }
817 process_action(keyp);
818 return true;
819 }
820 }
821 }
822 } else if (IS_TAPPING_RELEASED()) {
823 if (WITHIN_TAPPING_TERM(event)) {
824 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
825 // sequential tap.
826 keyp->tap_count = tapping_key.tap_count + 1;
827 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
828 process_action(keyp);
829 tapping_key = *keyp;
830 debug_tapping_key();
831 return true;
832 } else if (event.pressed && is_tap_key(event.key)) {
833 // Sequential tap can be interfered with other tap key.
834 debug("Tapping: Start with interfering other tap.\n");
835 tapping_key = *keyp;
836 waiting_buffer_scan_tap();
837 debug_tapping_key();
838 return true;
839 } else {
840 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
841 process_action(keyp);
842 return true;
843 }
844 } else {
845 // timeout. no sequential tap.
846 debug("Tapping: End(Timeout after releasing last tap): ");
847 debug_event(event); debug("\n");
848 tapping_key = (keyrecord_t){};
849 debug_tapping_key();
850 return false;
851 }
852 }
853 // not tapping satate
854 else {
855 if (event.pressed && is_tap_key(event.key)) {
856 debug("Tapping: Start(Press tap key).\n");
857 tapping_key = *keyp;
858 waiting_buffer_scan_tap();
859 debug_tapping_key();
860 return true;
861 } else {
862 process_action(keyp);
863 return true;
864 }
865 }
866 }
867
868 /* scan buffer for tapping */
869 static void waiting_buffer_scan_tap(void)
870 {
871 // tapping already is settled
872 if (tapping_key.tap_count > 0) return;
873 // invalid state: tapping_key released && tap_count == 0
874 if (!tapping_key.event.pressed) return;
875
876 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
877 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
878 !waiting_buffer[i].event.pressed &&
879 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
880 tapping_key.tap_count = 1;
881 waiting_buffer[i].tap_count = 1;
882 process_action(&tapping_key);
883
884 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
885 debug_waiting_buffer();
886 return;
887 }
888 }
889 }
890
891
892
893 /*
894 * Utilities for actions.
895 */
896 void register_code(uint8_t code)
897 {
898 if (code == KC_NO) {
899 return;
900 }
901 else if IS_KEY(code) {
902 // TODO: should push command_proc out of this block?
903 if (command_proc(code)) return;
904
905 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
906 uint8_t tmp_mods = host_get_mods();
907 host_add_mods(oneshot_state.mods);
908 host_add_key(code);
909 host_send_keyboard_report();
910
911 host_set_mods(tmp_mods);
912 oneshot_state.ready = false;
913 } else {
914 host_add_key(code);
915 host_send_keyboard_report();
916 }
917 }
918 else if IS_MOD(code) {
919 host_add_mods(MOD_BIT(code));
920 host_send_keyboard_report();
921 }
922 }
923
924 void unregister_code(uint8_t code)
925 {
926 if IS_KEY(code) {
927 host_del_key(code);
928 host_send_keyboard_report();
929 }
930 else if IS_MOD(code) {
931 host_del_mods(MOD_BIT(code));
932 host_send_keyboard_report();
933 }
934 }
935
936 void add_mods(uint8_t mods)
937 {
938 if (mods) {
939 host_add_mods(mods);
940 host_send_keyboard_report();
941 }
942 }
943
944 void del_mods(uint8_t mods)
945 {
946 if (mods) {
947 host_del_mods(mods);
948 host_send_keyboard_report();
949 }
950 }
951
952 void set_mods(uint8_t mods)
953 {
954 host_set_mods(mods);
955 host_send_keyboard_report();
956 }
957
958 void clear_keyboard(void)
959 {
960 host_clear_mods();
961 clear_keyboard_but_mods();
962 }
963
964 void clear_keyboard_but_mods(void)
965 {
966 host_clear_keys();
967 host_send_keyboard_report();
968 #ifdef MOUSEKEY_ENABLE
969 mousekey_clear();
970 mousekey_send();
971 #endif
972 #ifdef EXTRAKEY_ENABLE
973 host_system_send(0);
974 host_consumer_send(0);
975 #endif
976 }
977
978 bool sending_anykey(void)
979 {
980 return (host_has_anykey() || host_mouse_in_use() ||
981 host_last_sysytem_report() || host_last_consumer_report());
982 }
983
984 void layer_switch(uint8_t new_layer)
985 {
986 if (current_layer != new_layer) {
987 debug("Layer Switch: "); debug_hex(current_layer);
988 debug(" -> "); debug_hex(new_layer); debug("\n");
989
990 current_layer = new_layer;
991 clear_keyboard_but_mods(); // To avoid stuck keys
992 // NOTE: update mods with full scan of matrix? if modifier changes between layers
993 }
994 }
995
996 bool is_tap_key(key_t key)
997 {
998 action_t action = get_action(key);
999
1000 switch (action.kind.id) {
1001 case ACT_LMODS_TAP:
1002 case ACT_RMODS_TAP:
1003 return true;
1004 case ACT_LAYER:
1005 case ACT_LAYER_BIT:
1006 switch (action.layer.code) {
1007 case LAYER_MOMENTARY:
1008 case LAYER_ON_PRESS:
1009 case LAYER_ON_RELEASE:
1010 case LAYER_ON_BOTH:
1011 case LAYER_SET_DEFAULT_ON_PRESS:
1012 case LAYER_SET_DEFAULT_ON_RELEASE:
1013 case LAYER_SET_DEFAULT_ON_BOTH:
1014 return false;
1015 case LAYER_TAP_TOGGLE:
1016 default: /* tap key */
1017 return true;
1018 }
1019 return false;
1020 case ACT_FUNCTION:
1021 if (action.func.opt & FUNC_TAP) { return true; }
1022 return false;
1023 }
1024 return false;
1025 }
1026
1027
1028 /*
1029 * debug print
1030 */
1031 static void debug_event(keyevent_t event)
1032 {
1033 debug_hex16((event.key.row<<8) | event.key.col);
1034 if (event.pressed) debug("d("); else debug("u(");
1035 debug_dec(event.time); debug(")");
1036 }
1037 static void debug_record(keyrecord_t record)
1038 {
1039 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
1040 }
1041 static void debug_action(action_t action)
1042 {
1043 switch (action.kind.id) {
1044 case ACT_LMODS: debug("ACT_LMODS"); break;
1045 case ACT_RMODS: debug("ACT_RMODS"); break;
1046 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1047 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1048 case ACT_USAGE: debug("ACT_USAGE"); break;
1049 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1050 case ACT_LAYER: debug("ACT_LAYER"); break;
1051 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
1052 case ACT_LAYER_STACK: debug("ACT_LAYER_STACK"); break;
1053 case ACT_MACRO: debug("ACT_MACRO"); break;
1054 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1055 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1056 default: debug("UNKNOWN"); break;
1057 }
1058 debug("[");
1059 debug_hex4(action.kind.param>>8);
1060 debug(":");
1061 debug_hex8(action.kind.param & 0xff);
1062 debug("]");
1063 }
1064 static void debug_tapping_key(void)
1065 {
1066 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1067 }
1068 static void debug_waiting_buffer(void)
1069 {
1070 debug("{ ");
1071 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1072 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
1073 }
1074 debug("}\n");
1075 }
Imprint / Impressum