2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
29 static bool process_tapping(keyrecord_t *record);
30 static void process_action(keyrecord_t *record);
32 static void debug_event(keyevent_t event)
34 debug_hex16(event.key.raw);
35 if (event.pressed) debug("d("); else debug("u(");
36 debug_dec(event.time); debug(")");
38 static void debug_record(keyrecord_t record)
40 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
42 static void debug_action(action_t action)
44 switch (action.kind.id) {
45 case ACT_LMODS: debug("ACT_LMODS"); break;
46 case ACT_RMODS: debug("ACT_RMODS"); break;
47 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
48 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
49 case ACT_USAGE: debug("ACT_USAGE"); break;
50 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
51 case ACT_LAYER_PRESSED: debug("ACT_LAYER_PRESSED"); break;
52 case ACT_LAYER_RELEASED: debug("ACT_LAYER_RELEASED"); break;
53 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
54 case ACT_LAYER_EXT: debug("ACT_LAYER_EXT"); break;
55 case ACT_MACRO: debug("ACT_MACRO"); break;
56 case ACT_COMMAND: debug("ACT_COMMAND"); break;
57 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
58 default: debug("UNKNOWN"); break;
61 debug_hex4(action.kind.param>>8);
63 debug_hex8(action.kind.param & 0xff);
71 /* period of tapping(ms) */
73 #define TAPPING_TERM 200
76 /* tap count needed for toggling a feature */
77 #ifndef TAPPING_TOGGLE
78 #define TAPPING_TOGGLE 5
81 /* stores a key event of current tap. */
82 static keyrecord_t tapping_key = {};
84 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
85 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
86 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
87 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
88 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
94 * stores key events waiting for settling current tap.
96 #define WAITING_BUFFER_SIZE 8
97 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
99 /* point to empty cell to enq */
100 static uint8_t waiting_buffer_head = 0;
102 /* point to the oldest data cell to deq */
103 static uint8_t waiting_buffer_tail = 0;
105 static void debug_waiting_buffer(void)
108 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
109 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
113 static bool waiting_buffer_enq(keyrecord_t record)
115 if (IS_NOEVENT(record.event)) {
119 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
120 debug("waiting_buffer_enq: Over flow.\n");
124 waiting_buffer[waiting_buffer_head] = record;
125 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
127 debug("waiting_buffer_enq: "); debug_waiting_buffer();
131 static keyrecord_t waiting_buffer_deq(void)
133 if (waiting_buffer_head == waiting_buffer_tail) {
134 return (keyrecord_t){};
136 uint8_t last_tail = waiting_buffer_tail;
137 waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE;
138 return waiting_buffer[last_tail];
140 static bool waiting_buffer_is_empty(void)
142 return (waiting_buffer_head == waiting_buffer_tail);
145 static void waiting_buffer_clear(void)
147 waiting_buffer_head = 0;
148 waiting_buffer_tail = 0;
150 static bool waiting_buffer_typed(keyevent_t event)
152 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
153 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
159 static bool waiting_buffer_has_anykey_pressed(void)
161 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
162 if (waiting_buffer[i].event.pressed) return true;
170 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
171 * Solution: Oneshot modifier have its effect on only one key coming next.
172 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
174 * Hold: works as normal modifier.
175 * Tap: one shot modifier.
176 * 2 Tap: cancel one shot modifier.
177 * 5-Tap: toggles enable/disable oneshot feature.
185 static void oneshot_start(uint8_t mods, uint16_t time)
187 oneshot_state.mods = mods;
188 oneshot_state.time = time;
189 oneshot_state.ready = true;
191 static void oneshot_cancel(void)
193 oneshot_state.mods = 0;
194 oneshot_state.time = 0;
195 oneshot_state.ready = false;
197 static void oneshot_toggle(void)
199 oneshot_state.disabled = !oneshot_state.disabled;
204 void action_exec(keyevent_t event)
206 if (!IS_NOEVENT(event)) {
207 debug("\n---- action_exec: start -----\n");
208 debug("EVENT: "); debug_event(event); debug("\n");
211 keyrecord_t record = { .event = event };
213 // pre-process on tapping
214 if (process_tapping(&record)) {
215 if (!IS_NOEVENT(record.event)) {
216 debug("processed: "); debug_record(record); debug("\n");
220 if (!waiting_buffer_enq(record)) {
221 // clear all in case of overflow.
223 waiting_buffer_clear();
224 tapping_key = (keyrecord_t){};
228 // process waiting_buffer
229 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
230 debug("---- action_exec: process waiting_buffer -----\n");
232 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
233 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
234 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
235 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
240 if (!IS_NOEVENT(event)) {
245 static void process_action(keyrecord_t *record)
247 keyevent_t event = record->event;
248 uint8_t tap_count = record->tap_count;
250 if (IS_NOEVENT(event)) { return; }
252 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
253 //debug("action: "); debug_hex16(action.code); if (event.pressed) debug("d\n"); else debug("u\n");
254 debug("ACTION: "); debug_action(action); debug("\n");
256 switch (action.kind.id) {
261 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
264 uint8_t tmp_mods = host_get_mods();
267 host_send_keyboard_report();
269 register_code(action.key.code);
270 if (mods && action.key.code) {
271 host_set_mods(tmp_mods);
272 host_send_keyboard_report();
275 if (mods && !action.key.code) {
277 host_send_keyboard_report();
279 unregister_code(action.key.code);
286 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
288 switch (action.layer.code) {
292 if (tap_count == 0) {
293 debug("MODS_TAP: Oneshot: add_mods\n");
296 else if (tap_count == 1) {
297 debug("MODS_TAP: Oneshot: start\n");
298 oneshot_start(mods, event.time);
300 else if (tap_count == 5) {
301 debug("MODS_TAP: Oneshot: toggle\n");
305 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
306 // double tap cancels oneshot and works as normal modifier.
311 if (tap_count == 0) {
312 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
313 // cancel oneshot by holding.
317 else if (tap_count == 1) {
318 debug("MODS_TAP: Oneshot: del_mods\n");
323 debug("MODS_TAP: Oneshot: del_mods\n");
332 if (waiting_buffer_has_anykey_pressed()) {
333 debug("MODS_TAP: Tap: Cancel: add_mods\n");
334 // ad hoc: set 0 to cancel tap
335 record->tap_count = 0;
338 debug("MODS_TAP: Tap: register_code\n");
339 register_code(action.key.code);
342 debug("MODS_TAP: No tap: add_mods\n");
347 debug("MODS_TAP: Tap: unregister_code\n");
348 unregister_code(action.key.code);
350 debug("MODS_TAP: No tap: add_mods\n");
359 /* other HID usage */
361 #ifdef EXTRAKEY_ENABLE
362 switch (action.usage.page) {
363 case ACTION_USAGE_PAGE_SYSTEM:
365 host_system_send(action.usage.code);
370 case ACTION_USAGE_PAGE_CONSUMER:
372 host_consumer_send(action.usage.code);
374 host_consumer_send(0);
383 #ifdef MOUSEKEY_ENABLE
385 mousekey_on(action.key.code);
388 mousekey_off(action.key.code);
395 case ACT_LAYER_PRESSED:
396 // layer action when pressed
397 switch (action.layer.code) {
400 layer_switch(action.layer.opt);
406 if (tap_count < TAPPING_TOGGLE) {
407 layer_switch(action.layer.opt);
410 if (tap_count >= TAPPING_TOGGLE) {
411 debug("LAYER_PRESSED: tap toggle.\n");
412 layer_switch(action.layer.opt);
417 // change default layer
419 default_layer = action.layer.opt;
420 layer_switch(default_layer);
427 debug("LAYER_PRESSED: Tap: register_code\n");
428 register_code(action.layer.code);
430 debug("LAYER_PRESSED: No tap: layer_switch\n");
431 layer_switch(action.layer.opt);
435 debug("LAYER_PRESSED: Tap: unregister_code\n");
436 unregister_code(action.layer.code);
438 debug("LAYER_PRESSED: No tap: NO ACTION\n");
444 case ACT_LAYER_RELEASED:
445 switch (action.layer.code) {
447 if (!event.pressed) {
448 layer_switch(action.layer.opt);
454 if (tap_count >= TAPPING_TOGGLE) {
455 debug("LAYER_RELEASED: tap toggle.\n");
456 layer_switch(action.layer.opt);
459 if (tap_count < TAPPING_TOGGLE) {
460 layer_switch(action.layer.opt);
465 // change default layer
466 if (!event.pressed) {
467 default_layer = action.layer.opt;
468 layer_switch(default_layer);
475 debug("LAYER_RELEASED: Tap: register_code\n");
476 register_code(action.layer.code);
478 debug("LAYER_RELEASED: No tap: NO ACTION\n");
482 debug("LAYER_RELEASED: Tap: unregister_code\n");
483 unregister_code(action.layer.code);
485 debug("LAYER_RELEASED: No tap: layer_switch\n");
486 layer_switch(action.layer.opt);
493 switch (action.layer.code) {
496 layer_switch(current_layer | action.layer.opt);
498 layer_switch(current_layer & ~action.layer.opt);
504 if (tap_count < TAPPING_TOGGLE) {
505 debug("LAYER_BIT: tap toggle(press).\n");
506 layer_switch(current_layer | action.layer.opt);
509 if (tap_count < TAPPING_TOGGLE) {
510 debug("LAYER_BIT: tap toggle(release).\n");
511 layer_switch(current_layer & ~action.layer.opt);
513 debug("LAYER_BIT: tap toggle.\n");
514 layer_switch(current_layer | action.layer.opt);
519 // change default layer
521 default_layer = current_layer | action.layer.opt;
522 layer_switch(default_layer);
524 default_layer = current_layer & ~action.layer.opt;
525 layer_switch(default_layer);
531 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
532 debug("LAYER_BIT: Tap: register_code\n");
533 register_code(action.layer.code);
535 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
536 layer_switch(current_layer | action.layer.opt);
539 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
540 debug("LAYER_BIT: Tap: unregister_code\n");
541 unregister_code(action.layer.code);
543 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
544 layer_switch(current_layer & ~action.layer.opt);
551 switch (action.layer.opt) {
553 // set default layer when pressed
554 switch (action.layer.code) {
557 layer_switch(default_layer);
563 if (tap_count < TAPPING_TOGGLE) {
564 layer_switch(default_layer);
567 if (tap_count >= TAPPING_TOGGLE) {
568 debug("LAYER_EXT_PRESSED: tap toggle.\n");
569 layer_switch(default_layer);
574 // change default layer
576 default_layer = current_layer;
577 layer_switch(default_layer);
584 debug("LAYER_EXT_PRESSED: Tap: register_code\n");
585 register_code(action.layer.code);
587 debug("LAYER_EXT_PRESSED: No tap: layer_switch\n");
588 layer_switch(default_layer);
592 debug("LAYER_EXT_PRESSED: Tap: unregister_code\n");
593 unregister_code(action.layer.code);
595 debug("LAYER_EXT_PRESSED: No tap: NO ACTION\n");
602 // set default layer when released
603 switch (action.layer.code) {
605 if (!event.pressed) {
606 layer_switch(default_layer);
612 if (tap_count >= TAPPING_TOGGLE) {
613 debug("LAYER_EXT_RELEASED: tap toggle.\n");
614 layer_switch(default_layer);
617 if (tap_count < TAPPING_TOGGLE) {
618 layer_switch(default_layer);
623 // change default layer
624 if (!event.pressed) {
625 default_layer = current_layer;
626 layer_switch(default_layer);
633 debug("LAYER_EXT_RELEASED: Tap: register_code\n");
634 register_code(action.layer.code);
636 debug("LAYER_EXT_RELEASED: No tap: NO ACTION\n");
640 debug("LAYER_EXT_RELEASED: Tap: unregister_code\n");
641 unregister_code(action.layer.code);
643 debug("LAYER_EXT_RELEASED: No tap: layer_switch\n");
644 layer_switch(default_layer);
660 action_call_function(event, action.func.id);
669 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM
670 * without interfaring by typing other key.
672 /* return true when key event is processed. */
673 static bool process_tapping(keyrecord_t *keyp)
675 keyevent_t event = keyp->event;
678 if (IS_TAPPING_PRESSED()) {
679 if (WITHIN_TAPPING_TERM(event)) {
680 if (tapping_key.tap_count == 0) {
681 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
683 tapping_key.tap_count = 1;
684 debug("Tapping: First tap: tapping_key="); debug_record(tapping_key); debug("\n");
685 process_action(&tapping_key);
688 keyp->tap_count = tapping_key.tap_count;
690 } else if (!event.pressed && waiting_buffer_typed(event)) {
691 // other key typed. not tap.
692 debug("Tapping: End. No tap. Interfered by typing key: tapping_key={}\n");
693 process_action(&tapping_key);
694 tapping_key = (keyrecord_t){};
699 // other key events shall be stored till tapping state settles.
705 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
706 keyp->tap_count = tapping_key.tap_count;
707 debug("Tapping: Tap release: tapping_key="); debug_record(*keyp); debug("\n");
711 else if (is_tap_key(keyp->event.key) && event.pressed) {
712 debug("Tapping: Start with forcing to release last tap: tapping_key=");
713 debug_record(*keyp); debug("\n");
714 // TODO: need only when tap > 1?
715 process_action(&(keyrecord_t){
716 .tap_count = tapping_key.tap_count,
717 .event.key = tapping_key.event.key,
718 .event.time = event.time,
719 .event.pressed = false
725 if (!IS_NOEVENT(keyp->event)) {
726 debug("Tapping: key event while tap: tapping_key=");
727 debug_record(tapping_key); debug("\n");
729 process_action(keyp);
734 // not within TAPPING_TERM
736 if (tapping_key.tap_count == 0) {
738 debug("Tapping: End. Not tap(time out): tapping_key={}: "); debug_record(*keyp); debug("\n");
739 process_action(&tapping_key);
740 tapping_key = (keyrecord_t){};
743 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
744 debug("Tapping: End. tap release.");
745 keyp->tap_count = tapping_key.tap_count;
746 process_action(keyp);
747 tapping_key = (keyrecord_t){};
750 // other key after tap time out.
751 process_action(keyp);
756 } else if (IS_TAPPING_RELEASED()) {
757 if (WITHIN_TAPPING_TERM(event)) {
758 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
760 keyp->tap_count = tapping_key.tap_count + 1;
761 debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
762 process_action(keyp);
765 } else if (event.pressed && is_tap_key(event.key)) {
766 // Sequential tap can be interfered with other tap key.
767 debug("Tapping: Start with interfering other tap.\n");
771 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
772 process_action(keyp);
776 // timeout. no sequential tap.
777 debug("Tapping: End(Time out after releasing last tap).\n");
778 tapping_key = (keyrecord_t){};
779 process_action(keyp);
783 if (event.pressed && is_tap_key(event.key)) {
784 debug("Tapping: Start(Press tap key).\n");
786 debug("tapping_key="); debug_record(*keyp); debug("\n");
789 process_action(keyp);
798 * Utilities for actions.
800 void register_code(uint8_t code)
805 else if IS_KEY(code) {
806 // TODO: should push command_proc out of this block?
807 if (command_proc(code)) return;
809 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
810 uint8_t tmp_mods = host_get_mods();
811 host_add_mods(oneshot_state.mods);
813 host_send_keyboard_report();
815 host_set_mods(tmp_mods);
816 oneshot_state.ready = false;
819 host_send_keyboard_report();
822 else if IS_MOD(code) {
823 host_add_mods(MOD_BIT(code));
824 host_send_keyboard_report();
828 void unregister_code(uint8_t code)
832 host_send_keyboard_report();
834 else if IS_MOD(code) {
835 host_del_mods(MOD_BIT(code));
836 host_send_keyboard_report();
840 void add_mods(uint8_t mods)
844 host_send_keyboard_report();
848 void del_mods(uint8_t mods)
852 host_send_keyboard_report();
856 void set_mods(uint8_t mods)
859 host_send_keyboard_report();
862 void clear_keyboard(void)
865 clear_keyboard_but_mods();
868 void clear_keyboard_but_mods(void)
871 host_send_keyboard_report();
872 #ifdef MOUSEKEY_ENABLE
876 #ifdef EXTRAKEY_ENABLE
878 host_consumer_send(0);
882 bool sending_anykey(void)
884 return (host_has_anykey() || host_mouse_in_use() ||
885 host_last_sysytem_report() || host_last_consumer_report());
888 void layer_switch(uint8_t new_layer)
890 if (current_layer != new_layer) {
891 debug("Layer Switch: "); debug_hex(current_layer);
892 debug(" -> "); debug_hex(new_layer); debug("\n");
894 current_layer = new_layer;
895 clear_keyboard_but_mods(); // To avoid stuck keys
896 // NOTE: update mods with full scan of matrix? if modifier changes between layers
900 bool is_tap_key(key_t key)
902 action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
903 switch (action.kind.id) {
907 case ACT_LAYER_PRESSED:
909 switch (action.layer.code) {
919 if (action.func.opt & 0x1) {