aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-03-29 00:56:34 +0900
committertmk <nobody@nowhere>2013-03-29 02:46:05 +0900
commitc69f7e106922cecaf1aee8453e449dd7e2832c7a (patch)
tree2c5ea79dcd580a014d10b142f44b5191d35a12c5
parent366c75979535c27e10332a4842851fa7f91b2f7e (diff)
downloadqmk_firmware-c69f7e106922cecaf1aee8453e449dd7e2832c7a.tar.gz
qmk_firmware-c69f7e106922cecaf1aee8453e449dd7e2832c7a.zip
Add action_tapping.[ch] for refactoring
-rw-r--r--common.mk1
-rw-r--r--common/action.c368
-rw-r--r--common/action.h27
-rw-r--r--common/action_tapping.c330
-rw-r--r--common/action_tapping.h40
5 files changed, 397 insertions, 369 deletions
diff --git a/common.mk b/common.mk
index fa31b3c46..be9f289c9 100644
--- a/common.mk
+++ b/common.mk
@@ -2,6 +2,7 @@ COMMON_DIR = common
2SRC += $(COMMON_DIR)/host.c \ 2SRC += $(COMMON_DIR)/host.c \
3 $(COMMON_DIR)/keyboard.c \ 3 $(COMMON_DIR)/keyboard.c \
4 $(COMMON_DIR)/action.c \ 4 $(COMMON_DIR)/action.c \
5 $(COMMON_DIR)/action_tapping.c \
5 $(COMMON_DIR)/action_oneshot.c \ 6 $(COMMON_DIR)/action_oneshot.c \
6 $(COMMON_DIR)/action_macro.c \ 7 $(COMMON_DIR)/action_macro.c \
7 $(COMMON_DIR)/layer_switch.c \ 8 $(COMMON_DIR)/layer_switch.c \
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
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along 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
33static void process_action(keyrecord_t *record);
34static void debug_event(keyevent_t event);
35static void debug_record(keyrecord_t record);
36static 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. */
53static 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
68static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
69/* point to empty cell to enq */
70static uint8_t waiting_buffer_head = 0;
71/* point to the oldest data cell to deq */
72static uint8_t waiting_buffer_tail = 0;
73
74
75static bool process_tapping(keyrecord_t *record);
76static bool waiting_buffer_enq(keyrecord_t record);
77static void waiting_buffer_clear(void);
78#if TAPPING_TERM >= 500
79static bool waiting_buffer_typed(keyevent_t event);
80#endif
81static void waiting_buffer_scan_tap(void);
82static void debug_tapping_key(void);
83static void debug_waiting_buffer(void);
84#endif
85
86
87
88void action_exec(keyevent_t event) 31void 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
135static void process_action(keyrecord_t *record) 50void 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 */
777static void debug_event(keyevent_t event) 692void 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
784static void debug_record(keyrecord_t record) 699void 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
790static void debug_action(action_t action) 708void 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. */
822static 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 */
992static 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
1010static void waiting_buffer_clear(void)
1011{
1012 waiting_buffer_head = 0;
1013 waiting_buffer_tail = 0;
1014}
1015
1016#if TAPPING_TERM >= 500
1017static 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
1028bool 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 */
1036static 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 */
1062static void debug_tapping_key(void)
1063{
1064 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1065}
1066
1067static 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
diff --git a/common/action.h b/common/action.h
index ead917983..a6cb45384 100644
--- a/common/action.h
+++ b/common/action.h
@@ -17,25 +17,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
17#ifndef ACTION_H 17#ifndef ACTION_H
18#define ACTION_H 18#define ACTION_H
19 19
20#include <stdint.h>
21#include <stdbool.h>
20#include "keyboard.h" 22#include "keyboard.h"
21#include "keycode.h" 23#include "keycode.h"
22#include "action_macro.h" 24#include "action_macro.h"
23 25
24 26
25/* Struct to record event and tap count */ 27typedef struct {
26typedef union { 28 keyevent_t event;
29#ifndef NO_ACTION_TAPPING
30 /* tapping count and state */
27 struct { 31 struct {
28 bool interrupted :1; 32 bool interrupted :1;
29 bool reserved2 :1; 33 bool reserved2 :1;
30 bool reserved1 :1; 34 bool reserved1 :1;
31 bool reserved0 :1; 35 bool reserved0 :1;
32 uint8_t count :4; 36 uint8_t count :4;
33 }; 37 } tap;
34} tap_t; 38#endif
35
36typedef struct {
37 keyevent_t event;
38 tap_t tap;
39} keyrecord_t; 39} keyrecord_t;
40 40
41/* Action struct. 41/* Action struct.
@@ -99,9 +99,8 @@ const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t op
99/* user defined special function */ 99/* user defined special function */
100void action_function(keyrecord_t *record, uint8_t id, uint8_t opt); 100void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
101 101
102/* 102/* Utilities for actions. */
103 * Utilities for actions. 103void process_action(keyrecord_t *record);
104 */
105void register_code(uint8_t code); 104void register_code(uint8_t code);
106void unregister_code(uint8_t code); 105void unregister_code(uint8_t code);
107void add_mods(uint8_t mods); 106void add_mods(uint8_t mods);
@@ -112,7 +111,11 @@ void clear_keyboard_but_mods(void);
112bool sending_anykey(void); 111bool sending_anykey(void);
113void layer_switch(uint8_t new_layer); 112void layer_switch(uint8_t new_layer);
114bool is_tap_key(key_t key); 113bool is_tap_key(key_t key);
115bool waiting_buffer_has_anykey_pressed(void); 114
115/* debug */
116void debug_event(keyevent_t event);
117void debug_record(keyrecord_t record);
118void debug_action(action_t action);
116 119
117 120
118 121
diff --git a/common/action_tapping.c b/common/action_tapping.c
new file mode 100644
index 000000000..abb0bf518
--- /dev/null
+++ b/common/action_tapping.c
@@ -0,0 +1,330 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include "action.h"
4#include "action_tapping.h"
5#include "timer.h"
6#include "debug.h"
7
8
9#ifndef NO_ACTION_TAPPING
10
11#define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
12#define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
13#define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
14#define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
15#define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
16
17
18static keyrecord_t tapping_key = {};
19static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
20static uint8_t waiting_buffer_head = 0;
21static uint8_t waiting_buffer_tail = 0;
22
23static bool process_tapping(keyrecord_t *record);
24static bool waiting_buffer_enq(keyrecord_t record);
25static void waiting_buffer_clear(void);
26#if TAPPING_TERM >= 500
27static bool waiting_buffer_typed(keyevent_t event);
28#endif
29static bool waiting_buffer_has_anykey_pressed(void);
30static void waiting_buffer_scan_tap(void);
31static void debug_tapping_key(void);
32static void debug_waiting_buffer(void);
33
34
35void action_tapping_process(keyrecord_t record)
36{
37 if (process_tapping(&record)) {
38 if (!IS_NOEVENT(record.event)) {
39 debug("processed: "); debug_record(record); debug("\n");
40 }
41 } else {
42 if (!waiting_buffer_enq(record)) {
43 // clear all in case of overflow.
44 debug("OVERFLOW: CLEAR ALL STATES\n");
45 clear_keyboard();
46 waiting_buffer_clear();
47 tapping_key = (keyrecord_t){};
48 }
49 }
50
51 // process waiting_buffer
52 if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
53 debug("---- action_exec: process waiting_buffer -----\n");
54 }
55 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
56 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
57 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
58 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
59 } else {
60 break;
61 }
62 }
63 if (!IS_NOEVENT(record.event)) {
64 debug("\n");
65 }
66}
67
68
69/* Tapping
70 *
71 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
72 * (without interfering by typing other key)
73 */
74/* return true when key event is processed or consumed. */
75bool process_tapping(keyrecord_t *keyp)
76{
77 keyevent_t event = keyp->event;
78
79 // if tapping
80 if (IS_TAPPING_PRESSED()) {
81 if (WITHIN_TAPPING_TERM(event)) {
82 if (tapping_key.tap.count == 0) {
83 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
84 // first tap!
85 debug("Tapping: First tap(0->1).\n");
86 tapping_key.tap.count = 1;
87 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
88 debug_tapping_key();
89 process_action(&tapping_key);
90
91 // enqueue
92 keyp->tap = tapping_key.tap;
93 return false;
94 }
95#if TAPPING_TERM >= 500
96 /* This can prevent from typing some tap keys in a row at a time. */
97 else if (!event.pressed && waiting_buffer_typed(event)) {
98 // other key typed. not tap.
99 debug("Tapping: End. No tap. Interfered by typing key\n");
100 process_action(&tapping_key);
101 tapping_key = (keyrecord_t){};
102 debug_tapping_key();
103
104 // enqueue
105 return false;
106 }
107#endif
108 else {
109 // other key events shall be enq'd till tapping state settles.
110 return false;
111 }
112 }
113 // tap_count > 0
114 else {
115 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
116 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
117 keyp->tap = tapping_key.tap;
118 process_action(keyp);
119 tapping_key = *keyp;
120 debug_tapping_key();
121 return true;
122 }
123 else if (is_tap_key(keyp->event.key) && event.pressed) {
124 if (tapping_key.tap.count > 1) {
125 debug("Tapping: Start new tap with releasing last tap(>1).\n");
126 // unregister key
127 process_action(&(keyrecord_t){
128 .tap = tapping_key.tap,
129 .event.key = tapping_key.event.key,
130 .event.time = event.time,
131 .event.pressed = false
132 });
133 } else {
134 debug("Tapping: Start while last tap(1).\n");
135 }
136 tapping_key = *keyp;
137 waiting_buffer_scan_tap();
138 debug_tapping_key();
139 return true;
140 }
141 else {
142 if (!IS_NOEVENT(keyp->event)) {
143 debug("Tapping: key event while last tap(>0).\n");
144 }
145 process_action(keyp);
146 return true;
147 }
148 }
149 }
150 // after TAPPING_TERM
151 else {
152 if (tapping_key.tap.count == 0) {
153 debug("Tapping: End. Timeout. Not tap(0): ");
154 debug_event(event); debug("\n");
155 process_action(&tapping_key);
156 tapping_key = (keyrecord_t){};
157 debug_tapping_key();
158 return false;
159 } else {
160 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
161 debug("Tapping: End. last timeout tap release(>0).");
162 keyp->tap = tapping_key.tap;
163 process_action(keyp);
164 tapping_key = (keyrecord_t){};
165 return true;
166 }
167 else if (is_tap_key(keyp->event.key) && event.pressed) {
168 if (tapping_key.tap.count > 1) {
169 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
170 // unregister key
171 process_action(&(keyrecord_t){
172 .tap = tapping_key.tap,
173 .event.key = tapping_key.event.key,
174 .event.time = event.time,
175 .event.pressed = false
176 });
177 } else {
178 debug("Tapping: Start while last timeout tap(1).\n");
179 }
180 tapping_key = *keyp;
181 waiting_buffer_scan_tap();
182 debug_tapping_key();
183 return true;
184 }
185 else {
186 if (!IS_NOEVENT(keyp->event)) {
187 debug("Tapping: key event while last timeout tap(>0).\n");
188 }
189 process_action(keyp);
190 return true;
191 }
192 }
193 }
194 } else if (IS_TAPPING_RELEASED()) {
195 if (WITHIN_TAPPING_TERM(event)) {
196 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
197 // sequential tap.
198 keyp->tap = tapping_key.tap;
199 keyp->tap.count += 1;
200 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
201 process_action(keyp);
202 tapping_key = *keyp;
203 debug_tapping_key();
204 return true;
205 } else if (event.pressed && is_tap_key(event.key)) {
206 // Sequential tap can be interfered with other tap key.
207 debug("Tapping: Start with interfering other tap.\n");
208 tapping_key = *keyp;
209 waiting_buffer_scan_tap();
210 debug_tapping_key();
211 return true;
212 } else {
213 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
214 process_action(keyp);
215 return true;
216 }
217 } else {
218 // timeout. no sequential tap.
219 debug("Tapping: End(Timeout after releasing last tap): ");
220 debug_event(event); debug("\n");
221 tapping_key = (keyrecord_t){};
222 debug_tapping_key();
223 return false;
224 }
225 }
226 // not tapping satate
227 else {
228 if (event.pressed && is_tap_key(event.key)) {
229 debug("Tapping: Start(Press tap key).\n");
230 tapping_key = *keyp;
231 waiting_buffer_scan_tap();
232 debug_tapping_key();
233 return true;
234 } else {
235 process_action(keyp);
236 return true;
237 }
238 }
239}
240
241
242/*
243 * Waiting buffer
244 */
245bool waiting_buffer_enq(keyrecord_t record)
246{
247 if (IS_NOEVENT(record.event)) {
248 return true;
249 }
250
251 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
252 debug("waiting_buffer_enq: Over flow.\n");
253 return false;
254 }
255
256 waiting_buffer[waiting_buffer_head] = record;
257 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
258
259 debug("waiting_buffer_enq: "); debug_waiting_buffer();
260 return true;
261}
262
263void waiting_buffer_clear(void)
264{
265 waiting_buffer_head = 0;
266 waiting_buffer_tail = 0;
267}
268
269#if TAPPING_TERM >= 500
270bool waiting_buffer_typed(keyevent_t event)
271{
272 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
273 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
274 return true;
275 }
276 }
277 return false;
278}
279#endif
280
281bool waiting_buffer_has_anykey_pressed(void)
282{
283 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
284 if (waiting_buffer[i].event.pressed) return true;
285 }
286 return false;
287}
288
289/* scan buffer for tapping */
290void waiting_buffer_scan_tap(void)
291{
292 // tapping already is settled
293 if (tapping_key.tap.count > 0) return;
294 // invalid state: tapping_key released && tap.count == 0
295 if (!tapping_key.event.pressed) return;
296
297 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
298 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
299 !waiting_buffer[i].event.pressed &&
300 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
301 tapping_key.tap.count = 1;
302 waiting_buffer[i].tap.count = 1;
303 process_action(&tapping_key);
304
305 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
306 debug_waiting_buffer();
307 return;
308 }
309 }
310}
311
312
313/*
314 * debug print
315 */
316static void debug_tapping_key(void)
317{
318 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
319}
320
321static void debug_waiting_buffer(void)
322{
323 debug("{ ");
324 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
325 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
326 }
327 debug("}\n");
328}
329
330#endif
diff --git a/common/action_tapping.h b/common/action_tapping.h
new file mode 100644
index 000000000..c9f09f576
--- /dev/null
+++ b/common/action_tapping.h
@@ -0,0 +1,40 @@
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#ifndef ACTION_TAPPING_H
18#define ACTION_TAPPING_H
19
20
21#ifndef NO_ACTION_TAPPING
22
23/* period of tapping(ms) */
24#ifndef TAPPING_TERM
25#define TAPPING_TERM 200
26#endif
27
28/* tap count needed for toggling a feature */
29#ifndef TAPPING_TOGGLE
30#define TAPPING_TOGGLE 5
31#endif
32
33#define WAITING_BUFFER_SIZE 8
34
35
36void action_tapping_process(keyrecord_t record);
37
38#endif
39
40#endif