]> git.gir.st - tmk_keyboard.git/blob - common/action.c
Fix action of system and consumer usage.
[tmk_keyboard.git] / common / action.c
1 #include "host.h"
2 #include "timer.h"
3 //#include "keymap.h"
4 #include "keycode.h"
5 #include "keyboard.h"
6 #include "mousekey.h"
7 #include "command.h"
8 #include "util.h"
9 #include "debug.h"
10 #include "action.h"
11
12 #define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0)
13 #define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0)
14 #define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0)
15
16
17 /*
18 *
19 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
20 * -----------+------------------------------------------------------------------
21 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
22 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
23 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
24 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
25 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
26 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
27 * |
28 * Delay |- - IDLE(L+) IDLE(L+,Ps)
29 * Magic Key |COMMAND*5
30 *
31 * *1: ignore Fn if other key is down.
32 * *2: register Fnk if any key is pressing
33 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
34 * *4: if no keys registered to host
35 * *5: unregister all keys
36 * *6: only if no keys down
37 * *7: ignore Fn because Fnk key and stored key are down.
38 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
39 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
40 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
41 *
42 * States:
43 * IDLE: No key is down except modifiers
44 * DELAYING: delay layer switch after pressing Fn with alt keycode
45 * WAITING: key is pressed during DELAYING
46 *
47 * Events:
48 * Fn: Fn key without alternative keycode
49 * Fnk: Fn key with alternative keycode
50 * -: ignore
51 * Delay: layer switch delay term is elapsed
52 *
53 * Actions:
54 * Rk: register key
55 * Uk: unregister key
56 * Rf: register Fn(alt keycode)
57 * Uf: unregister Fn(alt keycode)
58 * Rs: register stored key
59 * Us: unregister stored key
60 * Sk: Store key(waiting Key)
61 * Sf: Store Fn(delayed Fn)
62 * Ps: Process stored key
63 * Ps: Process key
64 * Is: Interpret stored keys in current layer
65 * L+: Switch to new layer(*unregister* all keys but modifiers)
66 * L-: Switch back to last layer(*unregister* all keys but modifiers)
67 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
68 */
69
70
71 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
72 #define NEXT(state) do { \
73 Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
74 kbdstate = state; \
75 Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
76 } while (0)
77
78
79 static kbdstate_t kbdstate = IDLE;
80 static uint8_t fn_state_bits = 0;
81 static keyrecord_t delayed_fn = {};
82 static keyrecord_t waiting_key = {};
83
84 static const char *state_str(kbdstate_t state)
85 {
86 if (state == IDLE) return PSTR("IDLE");
87 if (state == DELAYING) return PSTR("DELAYING");
88 if (state == WAITING) return PSTR("WAITING");
89 if (state == PRESSING) return PSTR("PRESSING");
90 return PSTR("UNKNOWN");
91 }
92 static bool anykey_sent_to_host(void)
93 {
94 return (host_has_anykey() || host_mouse_in_use() ||
95 host_last_sysytem_report() || host_last_consumer_report());
96 }
97
98
99
100 /*
101 static void layer_switch_on(uint8_t code);
102 static void layer_switch_off(uint8_t code);
103 static void key_action(uint8_t code, keyevent_t event);
104 static void key_pressed(uint8_t code, keyevent_t event);
105 static void key_released(uint8_t code, keyevent_t event);
106 static void mod_pressed(uint8_t code, keyevent_t event);
107 static void mod_released(uint8_t code, keyevent_t event);
108 */
109
110 static void register_code(uint8_t code);
111 static void unregister_code(uint8_t code);
112 static void register_mods(uint8_t mods);
113 static void unregister_mods(uint8_t mods);
114 static void clear_keyboard(void);
115 static void clear_keyboard_but_mods(void);
116 static void layer_switch(uint8_t new_layer);
117
118
119 /* tap */
120 #define TAP_TIME 200
121 static keyevent_t last_event = {};
122 static uint16_t last_event_time = 0;
123 static uint8_t tap_count = 0;
124
125 /* layer */
126 uint8_t default_layer = 0;
127 uint8_t current_layer = 0;
128 uint8_t waiting_layer = 0;
129
130
131 void action_exec(action_t action, keyevent_t event)
132 {
133 /* count tap when key is up */
134 if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) {
135 if (!event.pressed) tap_count++;
136 } else {
137 tap_count = 0;
138 }
139
140 debug("action: "); debug_hex16(action.code); debug("\n");
141 debug("kind.id: "); debug_hex(action.kind.id); debug("\n");
142 debug("kind.param: "); debug_hex16(action.kind.param); debug("\n");
143 debug("key.code: "); debug_hex(action.key.code); debug("\n");
144 debug("key.mods: "); debug_hex(action.key.mods); debug("\n");
145
146 switch (action.kind.id) {
147 case ACT_LMODS:
148 if (event.pressed) {
149 register_mods(action.key.mods);
150 register_code(action.key.code);
151 } else {
152 unregister_code(action.key.code);
153 unregister_mods(action.key.mods);
154 }
155 break;
156 case ACT_RMODS:
157 if (event.pressed) {
158 register_mods(action.key.mods<<4);
159 register_code(action.key.code);
160 } else {
161 unregister_code(action.key.code);
162 unregister_mods(action.key.mods<<4);
163 }
164 break;
165 case ACT_LAYER:
166 switch (action.layer_key.code) {
167 case 0x00: // Momentary switch
168 // TODO: history of layer switch
169 if (event.pressed) {
170 layer_switch(action.layer_key.layer);
171 } else {
172 layer_switch(default_layer);
173 }
174 break;
175 case 0x01: // Oneshot switch
176 // TODO:
177 break;
178 case 0x02: // reserved
179 case 0x03: // reserved
180 break;
181 case 0xF0 ... 0xF7: // Tap to enable/disable
182 case 0xF8 ... 0xFF: // Tap to toggle layer
183 // TODO:
184 break;
185 default: // with keycode for tap
186 debug("tap: "); debug_hex(tap_count); debug("\n");
187 // TODO: layer switch
188 // TODO: in case tap is interrupted by other key
189
190
191 if (event.pressed) {
192 // when any key down
193 if (host_has_anykey()) {
194 if (tap_count == 0)
195 register_code(action.layer_key.code);
196 } else {
197 }
198
199 if (tap_count == 0) {
200 if (host_has_anykey()) {
201 register_code(action.layer_key.code);
202 } else {
203 waiting_layer = action.layer_key.layer;
204 }
205 }
206 // register key when press after a tap
207 if (tap_count > 0) {
208 register_code(action.layer_key.code);
209 }
210 } else {
211 // type key after tap
212 if (tap_count == 1) {
213 register_code(action.layer_key.code);
214 }
215 unregister_code(action.layer_key.code);
216 }
217 break;
218 }
219 break;
220 case ACT_USAGE:
221 #ifdef EXTRAKEY_ENABLE
222 switch (action.usage.page) {
223 case ACTION_USAGE_PAGE_SYSTEM:
224 if (event.pressed) {
225 host_system_send(action.usage.code);
226 } else {
227 host_system_send(0);
228 }
229 break;
230 case ACTION_USAGE_PAGE_CONSUMER:
231 if (event.pressed) {
232 host_consumer_send(action.usage.code);
233 } else {
234 host_consumer_send(0);
235 }
236 break;
237 }
238 #endif
239 break;
240 case ACT_MOUSEKEY:
241 #ifdef MOUSEKEY_ENABLE
242 if (event.pressed) {
243 mousekey_on(action.key.code);
244 mousekey_send();
245 } else {
246 mousekey_off(action.key.code);
247 mousekey_send();
248 }
249 #endif
250 break;
251 case ACT_LMOD_TAP:
252 case ACT_RMOD_TAP:
253 case ACT_MACRO:
254 case ACT_COMMAND:
255 case ACT_FUNCTION:
256 default:
257 break;
258 }
259
260 /* last event */
261 last_event = event;
262 last_event_time = timer_read();
263 }
264
265
266 #if 0
267 /* Key Action */
268 inline
269 static void key_action(uint8_t code, keyevent_t event)
270 {
271 if (event.pressed)
272 key_pressed(code, event);
273 else
274 key_released(code, event);
275 }
276
277 void fn_action(uint8_t code, keyevent_t event)
278 {
279 }
280
281 /* Key */
282 inline static void key_pressed(uint8_t code, keyevent_t event)
283 {
284 uint8_t tmp_mods;
285 switch (kbdstate) {
286 case IDLE:
287 register_code(code);
288 NEXT(PRESSING);
289 break;
290 case PRESSING:
291 register_code(code);
292 break;
293 case DELAYING:
294 waiting_key = (keyrecord_t) {
295 .event = event,
296 .code = code,
297 .mods = keyboard_report->mods,
298 .time = timer_read()
299 };
300 NEXT(WAITING);
301 break;
302 case WAITING:
303 // play back key stroke
304 tmp_mods = keyboard_report->mods;
305 host_set_mods(delayed_fn.mods);
306 register_code(delayed_fn.code);
307 host_set_mods(waiting_key.mods);
308 register_code(waiting_key.code);
309 host_set_mods(tmp_mods);
310 register_code(code);
311 NEXT(IDLE);
312 break;
313 }
314 }
315 inline static void key_released(uint8_t code, keyevent_t event)
316 {
317 uint8_t tmp_mods;
318 switch (kbdstate) {
319 case IDLE:
320 unregister_code(code);
321 break;
322 case PRESSING:
323 unregister_code(code);
324 if (!anykey_sent_to_host())
325 NEXT(IDLE);
326 break;
327 case DELAYING:
328 unregister_code(code);
329 break;
330 case WAITING:
331 if (code == waiting_key.code) {
332 layer_switch_on(delayed_fn.code);
333 NEXT(IDLE);
334 // process waiting_key
335 tmp_mods = keyboard_report->mods;
336 host_set_mods(waiting_key.mods);
337 keymap_process_event(waiting_key.event);
338 host_set_mods(tmp_mods);
339 keymap_process_event(event);
340 } else {
341 unregister_code(code);
342 }
343 break;
344 }
345 }
346
347 /* layer switch momentary */
348 inline static void layerkey_pressed(uint8_t code, keyevent_t event)
349 {
350 uint8_t tmp_mods;
351 switch (kbdstate) {
352 case IDLE:
353 layer_switch_on(code);
354 break;
355 case PRESSING:
356 // ignore
357 break;
358 case DELAYING:
359 waiting_key = (keyrecord_t) {
360 .event = event,
361 .code = code,
362 .mods = keyboard_report->mods,
363 .time = timer_read()
364 };
365 NEXT(WAITING);
366 break;
367 case WAITING:
368 tmp_mods = keyboard_report->mods;
369 host_set_mods(delayed_fn.mods);
370 register_code(delayed_fn.code);
371 host_set_mods(waiting_key.mods);
372 register_code(waiting_key.code);
373 host_set_mods(tmp_mods);
374 if (kind == FN_DOWN) {
375 // ignore Fn
376 } else if (kind == FNK_DOWN) {
377 register_code(code);
378 } else if (kind == KEY_DOWN) {
379 register_code(code);
380 }
381 NEXT(IDLE);
382 break;
383 }
384 }
385 inline static void layerkey_released(uint8_t code, keyevent_t event)
386 {
387 switch (kbdstate) {
388 case IDLE:
389 layer_switch_off(code);
390 break;
391 case PRESSING:
392 case DELAYING:
393 case WAITING:
394 if (layer_switch_off(code))
395 NEXT(IDLE);
396 break;
397 }
398 }
399 #endif
400
401
402 static void register_code(uint8_t code)
403 {
404 if (code == KC_NO) {
405 return;
406 }
407 else if IS_KEY(code) {
408 // TODO: should push command_proc out of this block?
409 if (!command_proc(code)) {
410 host_add_key(code);
411 host_send_keyboard_report();
412 }
413 }
414 else if IS_MOD(code) {
415 host_add_mods(MOD_BIT(code));
416 host_send_keyboard_report();
417 }
418 }
419
420 static void unregister_code(uint8_t code)
421 {
422 if IS_KEY(code) {
423 host_del_key(code);
424 host_send_keyboard_report();
425 }
426 else if IS_MOD(code) {
427 host_del_mods(MOD_BIT(code));
428 host_send_keyboard_report();
429 }
430 }
431
432 static void register_mods(uint8_t mods)
433 {
434 if (!mods) return;
435 host_add_mods(mods);
436 host_send_keyboard_report();
437 }
438
439 static void unregister_mods(uint8_t mods)
440 {
441 if (!mods) return;
442 host_del_mods(mods);
443 host_send_keyboard_report();
444 }
445
446 static void clear_keyboard(void)
447 {
448 host_clear_mods();
449 clear_keyboard_but_mods();
450 }
451
452 static void clear_keyboard_but_mods(void)
453 {
454 host_clear_keys();
455 host_send_keyboard_report();
456 #ifdef MOUSEKEY_ENABLE
457 mousekey_clear();
458 mousekey_send();
459 #endif
460 #ifdef EXTRAKEY_ENABLE
461 host_system_send(0);
462 host_consumer_send(0);
463 #endif
464 }
465
466 static void layer_switch(uint8_t new_layer)
467 {
468 if (current_layer != new_layer) {
469 Kdebug("Layer Switch: "); Kdebug_hex(current_layer);
470 Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
471
472 current_layer = new_layer;
473 clear_keyboard_but_mods(); // To avoid stuck keys
474 }
475 }
Imprint / Impressum