diff options
Diffstat (limited to 'common/action.c')
| -rw-r--r-- | common/action.c | 368 |
1 files changed, 11 insertions, 357 deletions
diff --git a/common/action.c b/common/action.c index c218808ab..07a3a64d6 100644 --- a/common/action.c +++ b/common/action.c | |||
| @@ -15,76 +15,19 @@ 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/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ | 16 | */ |
| 17 | #include "host.h" | 17 | #include "host.h" |
| 18 | #include "timer.h" | ||
| 19 | #include "keymap.h" | ||
| 20 | #include "keycode.h" | 18 | #include "keycode.h" |
| 21 | #include "keyboard.h" | 19 | #include "keyboard.h" |
| 22 | #include "mousekey.h" | 20 | #include "mousekey.h" |
| 23 | #include "command.h" | 21 | #include "command.h" |
| 24 | #include "util.h" | ||
| 25 | #include "debug.h" | 22 | #include "debug.h" |
| 26 | #include "led.h" | 23 | #include "led.h" |
| 27 | #include "layer_switch.h" | 24 | #include "layer_switch.h" |
| 25 | #include "action_tapping.h" | ||
| 28 | #include "action_oneshot.h" | 26 | #include "action_oneshot.h" |
| 29 | #include "action_macro.h" | 27 | #include "action_macro.h" |
| 30 | #include "action.h" | 28 | #include "action.h" |
| 31 | 29 | ||
| 32 | 30 | ||
| 33 | static void process_action(keyrecord_t *record); | ||
| 34 | static void debug_event(keyevent_t event); | ||
| 35 | static void debug_record(keyrecord_t record); | ||
| 36 | static void debug_action(action_t action); | ||
| 37 | |||
| 38 | #ifndef NO_ACTION_TAPPING | ||
| 39 | /* | ||
| 40 | * Tapping | ||
| 41 | */ | ||
| 42 | /* period of tapping(ms) */ | ||
| 43 | #ifndef TAPPING_TERM | ||
| 44 | #define TAPPING_TERM 200 | ||
| 45 | #endif | ||
| 46 | |||
| 47 | /* tap count needed for toggling a feature */ | ||
| 48 | #ifndef TAPPING_TOGGLE | ||
| 49 | #define TAPPING_TOGGLE 5 | ||
| 50 | #endif | ||
| 51 | |||
| 52 | /* stores a key event of current tap. */ | ||
| 53 | static keyrecord_t tapping_key = {}; | ||
| 54 | |||
| 55 | #define IS_TAPPING() !IS_NOEVENT(tapping_key.event) | ||
| 56 | #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) | ||
| 57 | #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) | ||
| 58 | #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) | ||
| 59 | #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) | ||
| 60 | |||
| 61 | |||
| 62 | /* | ||
| 63 | * Waiting buffer | ||
| 64 | * | ||
| 65 | * stores key events waiting for settling current tap. | ||
| 66 | */ | ||
| 67 | #define WAITING_BUFFER_SIZE 8 | ||
| 68 | static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {}; | ||
| 69 | /* point to empty cell to enq */ | ||
| 70 | static uint8_t waiting_buffer_head = 0; | ||
| 71 | /* point to the oldest data cell to deq */ | ||
| 72 | static uint8_t waiting_buffer_tail = 0; | ||
| 73 | |||
| 74 | |||
| 75 | static bool process_tapping(keyrecord_t *record); | ||
| 76 | static bool waiting_buffer_enq(keyrecord_t record); | ||
| 77 | static void waiting_buffer_clear(void); | ||
| 78 | #if TAPPING_TERM >= 500 | ||
| 79 | static bool waiting_buffer_typed(keyevent_t event); | ||
| 80 | #endif | ||
| 81 | static void waiting_buffer_scan_tap(void); | ||
| 82 | static void debug_tapping_key(void); | ||
| 83 | static void debug_waiting_buffer(void); | ||
| 84 | #endif | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | void action_exec(keyevent_t event) | 31 | void action_exec(keyevent_t event) |
| 89 | { | 32 | { |
| 90 | if (!IS_NOEVENT(event)) { | 33 | if (!IS_NOEVENT(event)) { |
| @@ -95,35 +38,7 @@ void action_exec(keyevent_t event) | |||
| 95 | keyrecord_t record = { .event = event }; | 38 | keyrecord_t record = { .event = event }; |
| 96 | 39 | ||
| 97 | #ifndef NO_ACTION_TAPPING | 40 | #ifndef NO_ACTION_TAPPING |
| 98 | if (process_tapping(&record)) { | 41 | action_tapping_process(record); |
| 99 | if (!IS_NOEVENT(record.event)) { | ||
| 100 | debug("processed: "); debug_record(record); debug("\n"); | ||
| 101 | } | ||
| 102 | } else { | ||
| 103 | if (!waiting_buffer_enq(record)) { | ||
| 104 | // clear all in case of overflow. | ||
| 105 | debug("OVERFLOW: CLEAR ALL STATES\n"); | ||
| 106 | clear_keyboard(); | ||
| 107 | waiting_buffer_clear(); | ||
| 108 | tapping_key = (keyrecord_t){}; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | // process waiting_buffer | ||
| 113 | if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) { | ||
| 114 | debug("---- action_exec: process waiting_buffer -----\n"); | ||
| 115 | } | ||
| 116 | for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { | ||
| 117 | if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { | ||
| 118 | debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); | ||
| 119 | debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n"); | ||
| 120 | } else { | ||
| 121 | break; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | if (!IS_NOEVENT(event)) { | ||
| 125 | debug("\n"); | ||
| 126 | } | ||
| 127 | #else | 42 | #else |
| 128 | process_action(&record); | 43 | process_action(&record); |
| 129 | if (!IS_NOEVENT(record.event)) { | 44 | if (!IS_NOEVENT(record.event)) { |
| @@ -132,7 +47,7 @@ void action_exec(keyevent_t event) | |||
| 132 | #endif | 47 | #endif |
| 133 | } | 48 | } |
| 134 | 49 | ||
| 135 | static void process_action(keyrecord_t *record) | 50 | void process_action(keyrecord_t *record) |
| 136 | { | 51 | { |
| 137 | keyevent_t event = record->event; | 52 | keyevent_t event = record->event; |
| 138 | uint8_t tap_count = record->tap.count; | 53 | uint8_t tap_count = record->tap.count; |
| @@ -224,7 +139,7 @@ static void process_action(keyrecord_t *record) | |||
| 224 | default: | 139 | default: |
| 225 | if (event.pressed) { | 140 | if (event.pressed) { |
| 226 | if (tap_count > 0) { | 141 | if (tap_count > 0) { |
| 227 | if (waiting_buffer_has_anykey_pressed()) { | 142 | if (record->tap.interrupted) { |
| 228 | debug("MODS_TAP: Tap: Cancel: add_mods\n"); | 143 | debug("MODS_TAP: Tap: Cancel: add_mods\n"); |
| 229 | // ad hoc: set 0 to cancel tap | 144 | // ad hoc: set 0 to cancel tap |
| 230 | record->tap.count = 0; | 145 | record->tap.count = 0; |
| @@ -774,20 +689,23 @@ bool is_tap_key(key_t key) | |||
| 774 | /* | 689 | /* |
| 775 | * debug print | 690 | * debug print |
| 776 | */ | 691 | */ |
| 777 | static void debug_event(keyevent_t event) | 692 | void debug_event(keyevent_t event) |
| 778 | { | 693 | { |
| 779 | debug_hex16((event.key.row<<8) | event.key.col); | 694 | debug_hex16((event.key.row<<8) | event.key.col); |
| 780 | if (event.pressed) debug("d("); else debug("u("); | 695 | if (event.pressed) debug("d("); else debug("u("); |
| 781 | debug_dec(event.time); debug(")"); | 696 | debug_dec(event.time); debug(")"); |
| 782 | } | 697 | } |
| 783 | 698 | ||
| 784 | static void debug_record(keyrecord_t record) | 699 | void debug_record(keyrecord_t record) |
| 785 | { | 700 | { |
| 786 | debug_event(record.event); debug(":"); debug_dec(record.tap.count); | 701 | debug_event(record.event); |
| 702 | #ifndef NO_ACTION_TAPPING | ||
| 703 | debug(":"); debug_dec(record.tap.count); | ||
| 787 | if (record.tap.interrupted) debug("-"); | 704 | if (record.tap.interrupted) debug("-"); |
| 705 | #endif | ||
| 788 | } | 706 | } |
| 789 | 707 | ||
| 790 | static void debug_action(action_t action) | 708 | void debug_action(action_t action) |
| 791 | { | 709 | { |
| 792 | switch (action.kind.id) { | 710 | switch (action.kind.id) { |
| 793 | case ACT_LMODS: debug("ACT_LMODS"); break; | 711 | case ACT_LMODS: debug("ACT_LMODS"); break; |
| @@ -809,267 +727,3 @@ static void debug_action(action_t action) | |||
| 809 | debug_hex8(action.kind.param & 0xff); | 727 | debug_hex8(action.kind.param & 0xff); |
| 810 | debug("]"); | 728 | debug("]"); |
| 811 | } | 729 | } |
| 812 | |||
| 813 | |||
| 814 | |||
| 815 | #ifndef NO_ACTION_TAPPING | ||
| 816 | /* Tapping | ||
| 817 | * | ||
| 818 | * Rule: Tap key is typed(pressed and released) within TAPPING_TERM. | ||
| 819 | * (without interfering by typing other key) | ||
| 820 | */ | ||
| 821 | /* return true when key event is processed or consumed. */ | ||
| 822 | static bool process_tapping(keyrecord_t *keyp) | ||
| 823 | { | ||
| 824 | keyevent_t event = keyp->event; | ||
| 825 | |||
| 826 | // if tapping | ||
| 827 | if (IS_TAPPING_PRESSED()) { | ||
| 828 | if (WITHIN_TAPPING_TERM(event)) { | ||
| 829 | if (tapping_key.tap.count == 0) { | ||
| 830 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | ||
| 831 | // first tap! | ||
| 832 | debug("Tapping: First tap(0->1).\n"); | ||
| 833 | tapping_key.tap.count = 1; | ||
| 834 | tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false); | ||
| 835 | debug_tapping_key(); | ||
| 836 | process_action(&tapping_key); | ||
| 837 | |||
| 838 | // enqueue | ||
| 839 | keyp->tap = tapping_key.tap; | ||
| 840 | return false; | ||
| 841 | } | ||
| 842 | #if TAPPING_TERM >= 500 | ||
| 843 | /* This can prevent from typing some tap keys in a row at a time. */ | ||
| 844 | else if (!event.pressed && waiting_buffer_typed(event)) { | ||
| 845 | // other key typed. not tap. | ||
| 846 | debug("Tapping: End. No tap. Interfered by typing key\n"); | ||
| 847 | process_action(&tapping_key); | ||
| 848 | tapping_key = (keyrecord_t){}; | ||
| 849 | debug_tapping_key(); | ||
| 850 | |||
| 851 | // enqueue | ||
| 852 | return false; | ||
| 853 | } | ||
| 854 | #endif | ||
| 855 | else { | ||
| 856 | // other key events shall be enq'd till tapping state settles. | ||
| 857 | return false; | ||
| 858 | } | ||
| 859 | } | ||
| 860 | // tap_count > 0 | ||
| 861 | else { | ||
| 862 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | ||
| 863 | debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n"); | ||
| 864 | keyp->tap = tapping_key.tap; | ||
| 865 | process_action(keyp); | ||
| 866 | tapping_key = *keyp; | ||
| 867 | debug_tapping_key(); | ||
| 868 | return true; | ||
| 869 | } | ||
| 870 | else if (is_tap_key(keyp->event.key) && event.pressed) { | ||
| 871 | if (tapping_key.tap.count > 1) { | ||
| 872 | debug("Tapping: Start new tap with releasing last tap(>1).\n"); | ||
| 873 | // unregister key | ||
| 874 | process_action(&(keyrecord_t){ | ||
| 875 | .tap = tapping_key.tap, | ||
| 876 | .event.key = tapping_key.event.key, | ||
| 877 | .event.time = event.time, | ||
| 878 | .event.pressed = false | ||
| 879 | }); | ||
| 880 | } else { | ||
| 881 | debug("Tapping: Start while last tap(1).\n"); | ||
| 882 | } | ||
| 883 | tapping_key = *keyp; | ||
| 884 | waiting_buffer_scan_tap(); | ||
| 885 | debug_tapping_key(); | ||
| 886 | return true; | ||
| 887 | } | ||
| 888 | else { | ||
| 889 | if (!IS_NOEVENT(keyp->event)) { | ||
| 890 | debug("Tapping: key event while last tap(>0).\n"); | ||
| 891 | } | ||
| 892 | process_action(keyp); | ||
| 893 | return true; | ||
| 894 | } | ||
| 895 | } | ||
| 896 | } | ||
| 897 | // after TAPPING_TERM | ||
| 898 | else { | ||
| 899 | if (tapping_key.tap.count == 0) { | ||
| 900 | debug("Tapping: End. Timeout. Not tap(0): "); | ||
| 901 | debug_event(event); debug("\n"); | ||
| 902 | process_action(&tapping_key); | ||
| 903 | tapping_key = (keyrecord_t){}; | ||
| 904 | debug_tapping_key(); | ||
| 905 | return false; | ||
| 906 | } else { | ||
| 907 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | ||
| 908 | debug("Tapping: End. last timeout tap release(>0)."); | ||
| 909 | keyp->tap = tapping_key.tap; | ||
| 910 | process_action(keyp); | ||
| 911 | tapping_key = (keyrecord_t){}; | ||
| 912 | return true; | ||
| 913 | } | ||
| 914 | else if (is_tap_key(keyp->event.key) && event.pressed) { | ||
| 915 | if (tapping_key.tap.count > 1) { | ||
| 916 | debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); | ||
| 917 | // unregister key | ||
| 918 | process_action(&(keyrecord_t){ | ||
| 919 | .tap = tapping_key.tap, | ||
| 920 | .event.key = tapping_key.event.key, | ||
| 921 | .event.time = event.time, | ||
| 922 | .event.pressed = false | ||
| 923 | }); | ||
| 924 | } else { | ||
| 925 | debug("Tapping: Start while last timeout tap(1).\n"); | ||
| 926 | } | ||
| 927 | tapping_key = *keyp; | ||
| 928 | waiting_buffer_scan_tap(); | ||
| 929 | debug_tapping_key(); | ||
| 930 | return true; | ||
| 931 | } | ||
| 932 | else { | ||
| 933 | if (!IS_NOEVENT(keyp->event)) { | ||
| 934 | debug("Tapping: key event while last timeout tap(>0).\n"); | ||
| 935 | } | ||
| 936 | process_action(keyp); | ||
| 937 | return true; | ||
| 938 | } | ||
| 939 | } | ||
| 940 | } | ||
| 941 | } else if (IS_TAPPING_RELEASED()) { | ||
| 942 | if (WITHIN_TAPPING_TERM(event)) { | ||
| 943 | if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { | ||
| 944 | // sequential tap. | ||
| 945 | keyp->tap = tapping_key.tap; | ||
| 946 | keyp->tap.count += 1; | ||
| 947 | debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n"); | ||
| 948 | process_action(keyp); | ||
| 949 | tapping_key = *keyp; | ||
| 950 | debug_tapping_key(); | ||
| 951 | return true; | ||
| 952 | } else if (event.pressed && is_tap_key(event.key)) { | ||
| 953 | // Sequential tap can be interfered with other tap key. | ||
| 954 | debug("Tapping: Start with interfering other tap.\n"); | ||
| 955 | tapping_key = *keyp; | ||
| 956 | waiting_buffer_scan_tap(); | ||
| 957 | debug_tapping_key(); | ||
| 958 | return true; | ||
| 959 | } else { | ||
| 960 | if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n"); | ||
| 961 | process_action(keyp); | ||
| 962 | return true; | ||
| 963 | } | ||
| 964 | } else { | ||
| 965 | // timeout. no sequential tap. | ||
| 966 | debug("Tapping: End(Timeout after releasing last tap): "); | ||
| 967 | debug_event(event); debug("\n"); | ||
| 968 | tapping_key = (keyrecord_t){}; | ||
| 969 | debug_tapping_key(); | ||
| 970 | return false; | ||
| 971 | } | ||
| 972 | } | ||
| 973 | // not tapping satate | ||
| 974 | else { | ||
| 975 | if (event.pressed && is_tap_key(event.key)) { | ||
| 976 | debug("Tapping: Start(Press tap key).\n"); | ||
| 977 | tapping_key = *keyp; | ||
| 978 | waiting_buffer_scan_tap(); | ||
| 979 | debug_tapping_key(); | ||
| 980 | return true; | ||
| 981 | } else { | ||
| 982 | process_action(keyp); | ||
| 983 | return true; | ||
| 984 | } | ||
| 985 | } | ||
| 986 | } | ||
| 987 | |||
| 988 | |||
| 989 | /* | ||
| 990 | * Waiting buffer | ||
| 991 | */ | ||
| 992 | static bool waiting_buffer_enq(keyrecord_t record) | ||
| 993 | { | ||
| 994 | if (IS_NOEVENT(record.event)) { | ||
| 995 | return true; | ||
| 996 | } | ||
| 997 | |||
| 998 | if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) { | ||
| 999 | debug("waiting_buffer_enq: Over flow.\n"); | ||
| 1000 | return false; | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | waiting_buffer[waiting_buffer_head] = record; | ||
| 1004 | waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; | ||
| 1005 | |||
| 1006 | debug("waiting_buffer_enq: "); debug_waiting_buffer(); | ||
| 1007 | return true; | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | static void waiting_buffer_clear(void) | ||
| 1011 | { | ||
| 1012 | waiting_buffer_head = 0; | ||
| 1013 | waiting_buffer_tail = 0; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | #if TAPPING_TERM >= 500 | ||
| 1017 | static bool waiting_buffer_typed(keyevent_t event) | ||
| 1018 | { | ||
| 1019 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 1020 | if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { | ||
| 1021 | return true; | ||
| 1022 | } | ||
| 1023 | } | ||
| 1024 | return false; | ||
| 1025 | } | ||
| 1026 | #endif | ||
| 1027 | |||
| 1028 | bool waiting_buffer_has_anykey_pressed(void) | ||
| 1029 | { | ||
| 1030 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 1031 | if (waiting_buffer[i].event.pressed) return true; | ||
| 1032 | } | ||
| 1033 | return false; | ||
| 1034 | } | ||
| 1035 | /* scan buffer for tapping */ | ||
| 1036 | static void waiting_buffer_scan_tap(void) | ||
| 1037 | { | ||
| 1038 | // tapping already is settled | ||
| 1039 | if (tapping_key.tap.count > 0) return; | ||
| 1040 | // invalid state: tapping_key released && tap.count == 0 | ||
| 1041 | if (!tapping_key.event.pressed) return; | ||
| 1042 | |||
| 1043 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 1044 | if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && | ||
| 1045 | !waiting_buffer[i].event.pressed && | ||
| 1046 | WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { | ||
| 1047 | tapping_key.tap.count = 1; | ||
| 1048 | waiting_buffer[i].tap.count = 1; | ||
| 1049 | process_action(&tapping_key); | ||
| 1050 | |||
| 1051 | debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n"); | ||
| 1052 | debug_waiting_buffer(); | ||
| 1053 | return; | ||
| 1054 | } | ||
| 1055 | } | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | |||
| 1059 | /* | ||
| 1060 | * debug print | ||
| 1061 | */ | ||
| 1062 | static void debug_tapping_key(void) | ||
| 1063 | { | ||
| 1064 | debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n"); | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | static void debug_waiting_buffer(void) | ||
| 1068 | { | ||
| 1069 | debug("{ "); | ||
| 1070 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | ||
| 1071 | debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" "); | ||
| 1072 | } | ||
| 1073 | debug("}\n"); | ||
| 1074 | } | ||
| 1075 | #endif | ||
