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);
36 /* period of tapping(ms) */
38 #define TAPPING_TERM 200
41 /* tap count needed for toggling a feature */
42 #ifndef TAPPING_TOGGLE
43 #define TAPPING_TOGGLE 5
46 /* stores a key event of current tap. */
47 static keyrecord_t tapping_key = {};
49 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
50 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
51 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
52 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
53 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
59 * stores key events waiting for settling current tap.
61 #define WAITING_BUFFER_SIZE 8
62 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
64 /* point to empty cell to enq */
65 static uint8_t waiting_buffer_head = 0;
67 /* point to the oldest data cell to deq */
68 static uint8_t waiting_buffer_tail = 0;
70 static bool waiting_buffer_enq(keyrecord_t record)
72 if (IS_NOEVENT(record.event)) {
76 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
77 debug("waiting_buffer_enq: Over flow.\n");
81 debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = ");
82 debug_hex16(record.event.key.raw); debug("\n");
84 waiting_buffer[waiting_buffer_head] = record;
85 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
89 static keyrecord_t waiting_buffer_deq(void)
91 if (waiting_buffer_head == waiting_buffer_tail) {
92 return (keyrecord_t){};
94 uint8_t last_tail = waiting_buffer_tail;
95 waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE;
96 return waiting_buffer[last_tail];
98 static bool waiting_buffer_is_empty(void)
100 return (waiting_buffer_head == waiting_buffer_tail);
103 static void waiting_buffer_clear(void)
105 waiting_buffer_head = 0;
106 waiting_buffer_tail = 0;
108 static bool waiting_buffer_typed(keyevent_t event)
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) {
117 static bool waiting_buffer_has_anykey_pressed(void)
119 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
120 if (waiting_buffer[i].event.pressed) return true;
128 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
129 * Solution: Oneshot modifier have its effect on only one key coming next.
130 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
132 * Hold: works as normal modifier.
133 * Tap: one shot modifier.
134 * 2 Tap: cancel one shot modifier.
135 * 5-Tap: toggles enable/disable oneshot feature.
143 static void oneshot_start(uint8_t mods, uint16_t time)
145 oneshot_state.mods = mods;
146 oneshot_state.time = time;
147 oneshot_state.ready = true;
149 static void oneshot_cancel(void)
151 oneshot_state.mods = 0;
152 oneshot_state.time = 0;
153 oneshot_state.ready = false;
155 static void oneshot_toggle(void)
157 oneshot_state.disabled = !oneshot_state.disabled;
162 void action_exec(keyevent_t event)
164 if (!IS_NOEVENT(event)) {
166 debug_hex16(event.time); debug(": ");
167 debug_hex16(event.key.raw);
169 if (event.pressed) debug("down"); else debug("up");
173 keyrecord_t record = { .event = event };
175 // pre-process on tapping
176 if (process_tapping(&record)) {
177 if (!IS_NOEVENT(record.event)) debug("processed.\n");
179 if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
181 if (!waiting_buffer_enq(record)) {
182 // clear all in case of overflow.
184 waiting_buffer_clear();
185 tapping_key = (keyrecord_t){};
189 // process waiting_buffer
190 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
191 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
192 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
193 debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
200 static void process_action(keyrecord_t *record)
202 keyevent_t event = record->event;
203 uint8_t tap_count = record->tap_count;
205 if (IS_NOEVENT(event)) { return; }
207 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
208 debug("action: "); debug_hex16(action.code);
209 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
211 switch (action.kind.id) {
216 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
219 uint8_t tmp_mods = host_get_mods();
222 host_send_keyboard_report();
224 register_code(action.key.code);
225 if (mods && action.key.code) {
226 host_set_mods(tmp_mods);
227 host_send_keyboard_report();
230 if (mods && !action.key.code) {
232 host_send_keyboard_report();
234 unregister_code(action.key.code);
241 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
243 switch (action.layer.code) {
247 if (tap_count == 0) {
248 debug("MODS_TAP: Oneshot: add_mods\n");
251 else if (tap_count == 1) {
252 debug("MODS_TAP: Oneshot: start\n");
253 oneshot_start(mods, event.time);
255 else if (tap_count == 5) {
256 debug("MODS_TAP: Oneshot: toggle\n");
260 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
261 // double tap cancels oneshot and works as normal modifier.
266 if (tap_count == 0) {
267 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
268 // cancel oneshot by holding.
272 else if (tap_count == 1) {
273 debug("MODS_TAP: Oneshot: del_mods\n");
278 debug("MODS_TAP: Oneshot: del_mods\n");
287 if (waiting_buffer_has_anykey_pressed()) {
288 debug("MODS_TAP: Tap: Cancel: add_mods\n");
289 // ad hoc: set 0 to cancel tap
290 record->tap_count = 0;
293 debug("MODS_TAP: Tap: register_code\n");
294 register_code(action.key.code);
297 debug("MODS_TAP: No tap: add_mods\n");
302 debug("MODS_TAP: Tap: unregister_code\n");
303 unregister_code(action.key.code);
305 debug("MODS_TAP: No tap: add_mods\n");
314 /* other HID usage */
316 #ifdef EXTRAKEY_ENABLE
317 switch (action.usage.page) {
318 case ACTION_USAGE_PAGE_SYSTEM:
320 host_system_send(action.usage.code);
325 case ACTION_USAGE_PAGE_CONSUMER:
327 host_consumer_send(action.usage.code);
329 host_consumer_send(0);
338 #ifdef MOUSEKEY_ENABLE
340 mousekey_on(action.key.code);
343 mousekey_off(action.key.code);
350 case ACT_LAYER_PRESSED:
351 // layer action when pressed
352 switch (action.layer.code) {
355 layer_switch(action.layer.opt);
361 if (tap_count < TAPPING_TOGGLE) {
362 layer_switch(action.layer.opt);
365 if (tap_count >= TAPPING_TOGGLE) {
366 debug("LAYER_PRESSED: tap toggle.\n");
367 layer_switch(action.layer.opt);
372 // change default layer
374 default_layer = action.layer.opt;
375 layer_switch(default_layer);
382 debug("LAYER_PRESSED: Tap: register_code\n");
383 register_code(action.layer.code);
385 debug("LAYER_PRESSED: No tap: layer_switch\n");
386 layer_switch(action.layer.opt);
390 debug("LAYER_PRESSED: Tap: unregister_code\n");
391 unregister_code(action.layer.code);
393 debug("LAYER_PRESSED: No tap: NO ACTION\n");
399 case ACT_LAYER_RELEASED:
400 switch (action.layer.code) {
402 if (!event.pressed) {
403 layer_switch(action.layer.opt);
409 if (tap_count >= TAPPING_TOGGLE) {
410 debug("LAYER_RELEASED: tap toggle.\n");
411 layer_switch(action.layer.opt);
414 if (tap_count < TAPPING_TOGGLE) {
415 layer_switch(action.layer.opt);
420 // change default layer
421 if (!event.pressed) {
422 default_layer = action.layer.opt;
423 layer_switch(default_layer);
430 debug("LAYER_RELEASED: Tap: register_code\n");
431 register_code(action.layer.code);
433 debug("LAYER_RELEASED: No tap: NO ACTION\n");
437 debug("LAYER_RELEASED: Tap: unregister_code\n");
438 unregister_code(action.layer.code);
440 debug("LAYER_RELEASED: No tap: layer_switch\n");
441 layer_switch(action.layer.opt);
448 switch (action.layer.code) {
451 layer_switch(current_layer | action.layer.opt);
453 layer_switch(current_layer & ~action.layer.opt);
459 if (tap_count < TAPPING_TOGGLE) {
460 debug("LAYER_BIT: tap toggle(press).\n");
461 layer_switch(current_layer | action.layer.opt);
464 if (tap_count < TAPPING_TOGGLE) {
465 debug("LAYER_BIT: tap toggle(release).\n");
466 layer_switch(current_layer & ~action.layer.opt);
468 debug("LAYER_BIT: tap toggle.\n");
469 layer_switch(current_layer | action.layer.opt);
474 // change default layer
476 default_layer = current_layer | action.layer.opt;
477 layer_switch(default_layer);
479 default_layer = current_layer & ~action.layer.opt;
480 layer_switch(default_layer);
486 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
487 debug("LAYER_BIT: Tap: register_code\n");
488 register_code(action.layer.code);
490 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
491 layer_switch(current_layer | action.layer.opt);
494 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
495 debug("LAYER_BIT: Tap: unregister_code\n");
496 unregister_code(action.layer.code);
498 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
499 layer_switch(current_layer & ~action.layer.opt);
506 switch (action.layer.opt) {
508 // set default layer when pressed
509 switch (action.layer.code) {
512 layer_switch(default_layer);
518 if (tap_count < TAPPING_TOGGLE) {
519 layer_switch(default_layer);
522 if (tap_count >= TAPPING_TOGGLE) {
523 debug("LAYER_EXT_PRESSED: tap toggle.\n");
524 layer_switch(default_layer);
529 // change default layer
531 default_layer = current_layer;
532 layer_switch(default_layer);
539 debug("LAYER_EXT_PRESSED: Tap: register_code\n");
540 register_code(action.layer.code);
542 debug("LAYER_EXT_PRESSED: No tap: layer_switch\n");
543 layer_switch(default_layer);
547 debug("LAYER_EXT_PRESSED: Tap: unregister_code\n");
548 unregister_code(action.layer.code);
550 debug("LAYER_EXT_PRESSED: No tap: NO ACTION\n");
557 // set default layer when released
558 switch (action.layer.code) {
560 if (!event.pressed) {
561 layer_switch(default_layer);
567 if (tap_count >= TAPPING_TOGGLE) {
568 debug("LAYER_EXT_RELEASED: tap toggle.\n");
569 layer_switch(default_layer);
572 if (tap_count < TAPPING_TOGGLE) {
573 layer_switch(default_layer);
578 // change default layer
579 if (!event.pressed) {
580 default_layer = current_layer;
581 layer_switch(default_layer);
588 debug("LAYER_EXT_RELEASED: Tap: register_code\n");
589 register_code(action.layer.code);
591 debug("LAYER_EXT_RELEASED: No tap: NO ACTION\n");
595 debug("LAYER_EXT_RELEASED: Tap: unregister_code\n");
596 unregister_code(action.layer.code);
598 debug("LAYER_EXT_RELEASED: No tap: layer_switch\n");
599 layer_switch(default_layer);
615 action_call_function(event, action.func.id);
624 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM
625 * without interfaring by typing other key.
627 /* return true when key event is processed. */
628 static bool process_tapping(keyrecord_t *keyp)
630 keyevent_t event = keyp->event;
633 if (IS_TAPPING_PRESSED()) {
634 if (WITHIN_TAPPING_TERM(event)) {
635 if (tapping_key.tap_count == 0) {
636 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
638 debug("Tapping: First tap.\n");
639 tapping_key.tap_count = 1;
640 process_action(&tapping_key);
643 keyp->tap_count = tapping_key.tap_count;
645 } else if (!event.pressed && waiting_buffer_typed(event)) {
646 // other key typed. not tap.
647 debug("Tapping: End(No tap. Interfered by typing key).\n");
648 process_action(&tapping_key);
649 tapping_key = (keyrecord_t){};
654 // other key events shall be stored till tapping state settles.
658 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
659 keyp->tap_count = tapping_key.tap_count;
660 debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
664 else if (is_tap_key(keyp->event.key) && event.pressed) {
665 debug("Tapping: Start with forcing to release last tap.\n");
666 process_action(&(keyrecord_t){
667 .tap_count = tapping_key.tap_count,
668 .event.key = tapping_key.event.key,
669 .event.time = event.time,
670 .event.pressed = false
676 if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
677 process_action(keyp);
682 // not within TAPPING_TERM
684 if (tapping_key.tap_count == 0) {
686 debug("Tapping: End. Not tap(time out).\n");
687 process_action(&tapping_key);
688 tapping_key = (keyrecord_t){};
691 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
692 debug("Tapping: End. tap release.");
693 keyp->tap_count = tapping_key.tap_count;
694 process_action(keyp);
695 tapping_key = (keyrecord_t){};
698 // other key after tap time out.
699 process_action(keyp);
704 } else if (IS_TAPPING_RELEASED()) {
705 if (WITHIN_TAPPING_TERM(event)) {
706 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
708 keyp->tap_count = tapping_key.tap_count + 1;
709 debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
710 process_action(keyp);
713 } else if (event.pressed && is_tap_key(event.key)) {
714 // Sequential tap can be interfered with other tap key.
715 debug("Tapping: Start with interfering other tap.\n");
719 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
720 process_action(keyp);
724 // timeout. no sequential tap.
725 debug("Tapping: End(Time out after releasing last tap).\n");
726 tapping_key = (keyrecord_t){};
727 process_action(keyp);
731 if (event.pressed && is_tap_key(event.key)) {
732 debug("Tapping: Start(Press tap key).\n");
736 process_action(keyp);
745 * Utilities for actions.
747 void register_code(uint8_t code)
752 else if IS_KEY(code) {
753 // TODO: should push command_proc out of this block?
754 if (command_proc(code)) return;
756 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
757 uint8_t tmp_mods = host_get_mods();
758 host_add_mods(oneshot_state.mods);
760 host_send_keyboard_report();
762 host_set_mods(tmp_mods);
763 oneshot_state.ready = false;
766 host_send_keyboard_report();
769 else if IS_MOD(code) {
770 host_add_mods(MOD_BIT(code));
771 host_send_keyboard_report();
775 void unregister_code(uint8_t code)
779 host_send_keyboard_report();
781 else if IS_MOD(code) {
782 host_del_mods(MOD_BIT(code));
783 host_send_keyboard_report();
787 void add_mods(uint8_t mods)
791 host_send_keyboard_report();
795 void del_mods(uint8_t mods)
799 host_send_keyboard_report();
803 void set_mods(uint8_t mods)
806 host_send_keyboard_report();
809 void clear_keyboard(void)
812 clear_keyboard_but_mods();
815 void clear_keyboard_but_mods(void)
818 host_send_keyboard_report();
819 #ifdef MOUSEKEY_ENABLE
823 #ifdef EXTRAKEY_ENABLE
825 host_consumer_send(0);
829 bool sending_anykey(void)
831 return (host_has_anykey() || host_mouse_in_use() ||
832 host_last_sysytem_report() || host_last_consumer_report());
835 void layer_switch(uint8_t new_layer)
837 if (current_layer != new_layer) {
838 debug("Layer Switch: "); debug_hex(current_layer);
839 debug(" -> "); debug_hex(new_layer); debug("\n");
841 current_layer = new_layer;
842 clear_keyboard_but_mods(); // To avoid stuck keys
843 // NOTE: update mods with full scan of matrix? if modifier changes between layers
847 bool is_tap_key(key_t key)
849 action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
850 switch (action.kind.id) {
854 case ACT_LAYER_PRESSED:
856 switch (action.layer.code) {
866 if (action.func.opt & 0x1) {