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