]> git.gir.st - tmk_keyboard.git/blob - common/action_tapping.c
Merge branch 'macro_mediakey'(Fix issue #42)
[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 debug_tapping_key();
92 process_action(&tapping_key);
93
94 // copy tapping state
95 keyp->tap = tapping_key.tap;
96 // enqueue
97 return false;
98 }
99 #if TAPPING_TERM >= 500
100 /* This can settle mod/fn state fast but may prevent from typing fast. */
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 // set interrupted flag when other key preesed during tapping
114 if (event.pressed) {
115 tapping_key.tap.interrupted = true;
116 }
117 // enqueue
118 return false;
119 }
120 }
121 // tap_count > 0
122 else {
123 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
124 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
125 keyp->tap = tapping_key.tap;
126 process_action(keyp);
127 tapping_key = *keyp;
128 debug_tapping_key();
129 return true;
130 }
131 else if (is_tap_key(event.key) && event.pressed) {
132 if (tapping_key.tap.count > 1) {
133 debug("Tapping: Start new tap with releasing last tap(>1).\n");
134 // unregister key
135 process_action(&(keyrecord_t){
136 .tap = tapping_key.tap,
137 .event.key = tapping_key.event.key,
138 .event.time = event.time,
139 .event.pressed = false
140 });
141 } else {
142 debug("Tapping: Start while last tap(1).\n");
143 }
144 tapping_key = *keyp;
145 waiting_buffer_scan_tap();
146 debug_tapping_key();
147 return true;
148 }
149 else {
150 if (!IS_NOEVENT(event)) {
151 debug("Tapping: key event while last tap(>0).\n");
152 }
153 process_action(keyp);
154 return true;
155 }
156 }
157 }
158 // after TAPPING_TERM
159 else {
160 if (tapping_key.tap.count == 0) {
161 debug("Tapping: End. Timeout. Not tap(0): ");
162 debug_event(event); debug("\n");
163 process_action(&tapping_key);
164 tapping_key = (keyrecord_t){};
165 debug_tapping_key();
166 return false;
167 } else {
168 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
169 debug("Tapping: End. last timeout tap release(>0).");
170 keyp->tap = tapping_key.tap;
171 process_action(keyp);
172 tapping_key = (keyrecord_t){};
173 return true;
174 }
175 else if (is_tap_key(event.key) && event.pressed) {
176 if (tapping_key.tap.count > 1) {
177 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
178 // unregister key
179 process_action(&(keyrecord_t){
180 .tap = tapping_key.tap,
181 .event.key = tapping_key.event.key,
182 .event.time = event.time,
183 .event.pressed = false
184 });
185 } else {
186 debug("Tapping: Start while last timeout tap(1).\n");
187 }
188 tapping_key = *keyp;
189 waiting_buffer_scan_tap();
190 debug_tapping_key();
191 return true;
192 }
193 else {
194 if (!IS_NOEVENT(event)) {
195 debug("Tapping: key event while last timeout tap(>0).\n");
196 }
197 process_action(keyp);
198 return true;
199 }
200 }
201 }
202 } else if (IS_TAPPING_RELEASED()) {
203 if (WITHIN_TAPPING_TERM(event)) {
204 if (event.pressed) {
205 if (IS_TAPPING_KEY(event.key)) {
206 if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
207 // sequential tap.
208 keyp->tap = tapping_key.tap;
209 keyp->tap.count += 1;
210 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
211 process_action(keyp);
212 tapping_key = *keyp;
213 debug_tapping_key();
214 return true;
215 } else {
216 // FIX: start new tap again
217 tapping_key = *keyp;
218 return true;
219 }
220 } else if (is_tap_key(event.key)) {
221 // Sequential tap can be interfered with other tap key.
222 debug("Tapping: Start with interfering other tap.\n");
223 tapping_key = *keyp;
224 waiting_buffer_scan_tap();
225 debug_tapping_key();
226 return true;
227 } else {
228 // should none in buffer
229 // FIX: interrupted when other key is pressed
230 tapping_key.tap.interrupted = true;
231 process_action(keyp);
232 return true;
233 }
234 } else {
235 if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
236 process_action(keyp);
237 return true;
238 }
239 } else {
240 // FIX: process_aciton here?
241 // timeout. no sequential tap.
242 debug("Tapping: End(Timeout after releasing last tap): ");
243 debug_event(event); debug("\n");
244 tapping_key = (keyrecord_t){};
245 debug_tapping_key();
246 return false;
247 }
248 }
249 // not tapping state
250 else {
251 if (event.pressed && is_tap_key(event.key)) {
252 debug("Tapping: Start(Press tap key).\n");
253 tapping_key = *keyp;
254 waiting_buffer_scan_tap();
255 debug_tapping_key();
256 return true;
257 } else {
258 process_action(keyp);
259 return true;
260 }
261 }
262 }
263
264
265 /*
266 * Waiting buffer
267 */
268 bool waiting_buffer_enq(keyrecord_t record)
269 {
270 if (IS_NOEVENT(record.event)) {
271 return true;
272 }
273
274 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
275 debug("waiting_buffer_enq: Over flow.\n");
276 return false;
277 }
278
279 waiting_buffer[waiting_buffer_head] = record;
280 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
281
282 debug("waiting_buffer_enq: "); debug_waiting_buffer();
283 return true;
284 }
285
286 void waiting_buffer_clear(void)
287 {
288 waiting_buffer_head = 0;
289 waiting_buffer_tail = 0;
290 }
291
292 #if TAPPING_TERM >= 500
293 bool waiting_buffer_typed(keyevent_t event)
294 {
295 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
296 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
297 return true;
298 }
299 }
300 return false;
301 }
302 #endif
303
304 bool waiting_buffer_has_anykey_pressed(void)
305 {
306 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
307 if (waiting_buffer[i].event.pressed) return true;
308 }
309 return false;
310 }
311
312 /* scan buffer for tapping */
313 void waiting_buffer_scan_tap(void)
314 {
315 // tapping already is settled
316 if (tapping_key.tap.count > 0) return;
317 // invalid state: tapping_key released && tap.count == 0
318 if (!tapping_key.event.pressed) return;
319
320 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
321 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
322 !waiting_buffer[i].event.pressed &&
323 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
324 tapping_key.tap.count = 1;
325 waiting_buffer[i].tap.count = 1;
326 process_action(&tapping_key);
327
328 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
329 debug_waiting_buffer();
330 return;
331 }
332 }
333 }
334
335
336 /*
337 * debug print
338 */
339 static void debug_tapping_key(void)
340 {
341 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
342 }
343
344 static void debug_waiting_buffer(void)
345 {
346 debug("{ ");
347 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
348 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
349 }
350 debug("}\n");
351 }
352
353 #endif
Imprint / Impressum