]> git.gir.st - tmk_keyboard.git/blob - common/action_tapping.c
Merge pull request #44 from Wraul/phantom_fix
[tmk_keyboard.git] / common / action_tapping.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include "action.h"
4 #include "action_tapping.h"
5 #include "timer.h"
6
7 #ifdef DEBUG_ACTION
8 #include "debug.h"
9 #else
10 #include "nodebug.h"
11 #endif
12
13 #ifndef NO_ACTION_TAPPING
14
15 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
16 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
17 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
18 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
19 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
20
21
22 static keyrecord_t tapping_key = {};
23 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
24 static uint8_t waiting_buffer_head = 0;
25 static uint8_t waiting_buffer_tail = 0;
26
27 static bool process_tapping(keyrecord_t *record);
28 static bool waiting_buffer_enq(keyrecord_t record);
29 static void waiting_buffer_clear(void);
30 #if TAPPING_TERM >= 500
31 static bool waiting_buffer_typed(keyevent_t event);
32 #endif
33 static bool waiting_buffer_has_anykey_pressed(void);
34 static void waiting_buffer_scan_tap(void);
35 static void debug_tapping_key(void);
36 static void debug_waiting_buffer(void);
37
38
39 void action_tapping_process(keyrecord_t record)
40 {
41 if (process_tapping(&record)) {
42 if (!IS_NOEVENT(record.event)) {
43 debug("processed: "); debug_record(record); debug("\n");
44 }
45 } else {
46 if (!waiting_buffer_enq(record)) {
47 // clear all in case of overflow.
48 debug("OVERFLOW: CLEAR ALL STATES\n");
49 clear_keyboard();
50 waiting_buffer_clear();
51 tapping_key = (keyrecord_t){};
52 }
53 }
54
55 // process waiting_buffer
56 if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
57 debug("---- action_exec: process waiting_buffer -----\n");
58 }
59 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
60 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
61 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
62 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
63 } else {
64 break;
65 }
66 }
67 if (!IS_NOEVENT(record.event)) {
68 debug("\n");
69 }
70 }
71
72
73 /* Tapping
74 *
75 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
76 * (without interfering by typing other key)
77 */
78 /* return true when key event is processed or consumed. */
79 bool process_tapping(keyrecord_t *keyp)
80 {
81 keyevent_t event = keyp->event;
82
83 // if tapping
84 if (IS_TAPPING_PRESSED()) {
85 if (WITHIN_TAPPING_TERM(event)) {
86 if (tapping_key.tap.count == 0) {
87 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
88 // first tap!
89 debug("Tapping: First tap(0->1).\n");
90 tapping_key.tap.count = 1;
91 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
92 debug_tapping_key();
93 process_action(&tapping_key);
94
95 // enqueue
96 keyp->tap = tapping_key.tap;
97 return false;
98 }
99 #if TAPPING_TERM >= 500
100 /* This can prevent from typing some tap keys in a row at a time. */
101 else if (!event.pressed && waiting_buffer_typed(event)) {
102 // other key typed. not tap.
103 debug("Tapping: End. No tap. Interfered by typing key\n");
104 process_action(&tapping_key);
105 tapping_key = (keyrecord_t){};
106 debug_tapping_key();
107
108 // enqueue
109 return false;
110 }
111 #endif
112 else {
113 // other key events shall be enq'd till tapping state settles.
114 return false;
115 }
116 }
117 // tap_count > 0
118 else {
119 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
120 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
121 keyp->tap = tapping_key.tap;
122 process_action(keyp);
123 tapping_key = *keyp;
124 debug_tapping_key();
125 return true;
126 }
127 else if (is_tap_key(keyp->event.key) && event.pressed) {
128 if (tapping_key.tap.count > 1) {
129 debug("Tapping: Start new tap with releasing last tap(>1).\n");
130 // unregister key
131 process_action(&(keyrecord_t){
132 .tap = tapping_key.tap,
133 .event.key = tapping_key.event.key,
134 .event.time = event.time,
135 .event.pressed = false
136 });
137 } else {
138 debug("Tapping: Start while last tap(1).\n");
139 }
140 tapping_key = *keyp;
141 waiting_buffer_scan_tap();
142 debug_tapping_key();
143 return true;
144 }
145 else {
146 if (!IS_NOEVENT(keyp->event)) {
147 debug("Tapping: key event while last tap(>0).\n");
148 }
149 process_action(keyp);
150 return true;
151 }
152 }
153 }
154 // after TAPPING_TERM
155 else {
156 if (tapping_key.tap.count == 0) {
157 debug("Tapping: End. Timeout. Not tap(0): ");
158 debug_event(event); debug("\n");
159 process_action(&tapping_key);
160 tapping_key = (keyrecord_t){};
161 debug_tapping_key();
162 return false;
163 } else {
164 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
165 debug("Tapping: End. last timeout tap release(>0).");
166 keyp->tap = tapping_key.tap;
167 process_action(keyp);
168 tapping_key = (keyrecord_t){};
169 return true;
170 }
171 else if (is_tap_key(keyp->event.key) && event.pressed) {
172 if (tapping_key.tap.count > 1) {
173 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
174 // unregister key
175 process_action(&(keyrecord_t){
176 .tap = tapping_key.tap,
177 .event.key = tapping_key.event.key,
178 .event.time = event.time,
179 .event.pressed = false
180 });
181 } else {
182 debug("Tapping: Start while last timeout tap(1).\n");
183 }
184 tapping_key = *keyp;
185 waiting_buffer_scan_tap();
186 debug_tapping_key();
187 return true;
188 }
189 else {
190 if (!IS_NOEVENT(keyp->event)) {
191 debug("Tapping: key event while last timeout tap(>0).\n");
192 }
193 process_action(keyp);
194 return true;
195 }
196 }
197 }
198 } else if (IS_TAPPING_RELEASED()) {
199 if (WITHIN_TAPPING_TERM(event)) {
200 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
201 // sequential tap.
202 keyp->tap = tapping_key.tap;
203 keyp->tap.count += 1;
204 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
205 process_action(keyp);
206 tapping_key = *keyp;
207 debug_tapping_key();
208 return true;
209 } else if (event.pressed && is_tap_key(event.key)) {
210 // Sequential tap can be interfered with other tap key.
211 debug("Tapping: Start with interfering other tap.\n");
212 tapping_key = *keyp;
213 waiting_buffer_scan_tap();
214 debug_tapping_key();
215 return true;
216 } else {
217 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
218 process_action(keyp);
219 return true;
220 }
221 } else {
222 // timeout. no sequential tap.
223 debug("Tapping: End(Timeout after releasing last tap): ");
224 debug_event(event); debug("\n");
225 tapping_key = (keyrecord_t){};
226 debug_tapping_key();
227 return false;
228 }
229 }
230 // not tapping satate
231 else {
232 if (event.pressed && is_tap_key(event.key)) {
233 debug("Tapping: Start(Press tap key).\n");
234 tapping_key = *keyp;
235 waiting_buffer_scan_tap();
236 debug_tapping_key();
237 return true;
238 } else {
239 process_action(keyp);
240 return true;
241 }
242 }
243 }
244
245
246 /*
247 * Waiting buffer
248 */
249 bool waiting_buffer_enq(keyrecord_t record)
250 {
251 if (IS_NOEVENT(record.event)) {
252 return true;
253 }
254
255 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
256 debug("waiting_buffer_enq: Over flow.\n");
257 return false;
258 }
259
260 waiting_buffer[waiting_buffer_head] = record;
261 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
262
263 debug("waiting_buffer_enq: "); debug_waiting_buffer();
264 return true;
265 }
266
267 void waiting_buffer_clear(void)
268 {
269 waiting_buffer_head = 0;
270 waiting_buffer_tail = 0;
271 }
272
273 #if TAPPING_TERM >= 500
274 bool waiting_buffer_typed(keyevent_t event)
275 {
276 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
277 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
278 return true;
279 }
280 }
281 return false;
282 }
283 #endif
284
285 bool waiting_buffer_has_anykey_pressed(void)
286 {
287 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
288 if (waiting_buffer[i].event.pressed) return true;
289 }
290 return false;
291 }
292
293 /* scan buffer for tapping */
294 void waiting_buffer_scan_tap(void)
295 {
296 // tapping already is settled
297 if (tapping_key.tap.count > 0) return;
298 // invalid state: tapping_key released && tap.count == 0
299 if (!tapping_key.event.pressed) return;
300
301 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
302 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
303 !waiting_buffer[i].event.pressed &&
304 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
305 tapping_key.tap.count = 1;
306 waiting_buffer[i].tap.count = 1;
307 process_action(&tapping_key);
308
309 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
310 debug_waiting_buffer();
311 return;
312 }
313 }
314 }
315
316
317 /*
318 * debug print
319 */
320 static void debug_tapping_key(void)
321 {
322 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
323 }
324
325 static void debug_waiting_buffer(void)
326 {
327 debug("{ ");
328 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
329 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
330 }
331 debug("}\n");
332 }
333
334 #endif
Imprint / Impressum