diff options
Diffstat (limited to 'quantum/process_keycode/process_tap_dance.c')
-rw-r--r-- | quantum/process_keycode/process_tap_dance.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c new file mode 100644 index 000000000..9b172e1b6 --- /dev/null +++ b/quantum/process_keycode/process_tap_dance.c | |||
@@ -0,0 +1,90 @@ | |||
1 | #include "quantum.h" | ||
2 | |||
3 | static qk_tap_dance_state_t qk_tap_dance_state; | ||
4 | |||
5 | static void _process_tap_dance_action_pair (qk_tap_dance_state_t *state, | ||
6 | uint16_t kc1, uint16_t kc2) { | ||
7 | uint16_t kc; | ||
8 | |||
9 | if (state->count == 0) | ||
10 | return; | ||
11 | |||
12 | kc = (state->count == 1) ? kc1 : kc2; | ||
13 | |||
14 | register_code (kc); | ||
15 | unregister_code (kc); | ||
16 | |||
17 | if (state->count >= 2) { | ||
18 | reset_tap_dance (state); | ||
19 | } | ||
20 | } | ||
21 | |||
22 | static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state, | ||
23 | qk_tap_dance_user_fn_t fn) | ||
24 | { | ||
25 | fn(state); | ||
26 | } | ||
27 | |||
28 | void process_tap_dance_action (uint16_t keycode) | ||
29 | { | ||
30 | uint16_t idx = keycode - QK_TAP_DANCE; | ||
31 | qk_tap_dance_action_t action; | ||
32 | |||
33 | action = tap_dance_actions[idx]; | ||
34 | |||
35 | switch (action.type) { | ||
36 | case QK_TAP_DANCE_TYPE_PAIR: | ||
37 | _process_tap_dance_action_pair (&qk_tap_dance_state, | ||
38 | action.pair.kc1, action.pair.kc2); | ||
39 | break; | ||
40 | case QK_TAP_DANCE_TYPE_FN: | ||
41 | _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn); | ||
42 | break; | ||
43 | |||
44 | default: | ||
45 | break; | ||
46 | } | ||
47 | } | ||
48 | |||
49 | bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { | ||
50 | bool r = true; | ||
51 | |||
52 | switch(keycode) { | ||
53 | case QK_TAP_DANCE ... QK_TAP_DANCE_MAX: | ||
54 | if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) { | ||
55 | process_tap_dance_action (qk_tap_dance_state.keycode); | ||
56 | } else { | ||
57 | r = false; | ||
58 | } | ||
59 | |||
60 | if (record->event.pressed) { | ||
61 | qk_tap_dance_state.keycode = keycode; | ||
62 | qk_tap_dance_state.timer = timer_read (); | ||
63 | qk_tap_dance_state.count++; | ||
64 | } | ||
65 | break; | ||
66 | |||
67 | default: | ||
68 | if (qk_tap_dance_state.keycode) { | ||
69 | process_tap_dance_action (qk_tap_dance_state.keycode); | ||
70 | |||
71 | reset_tap_dance (&qk_tap_dance_state); | ||
72 | } | ||
73 | break; | ||
74 | } | ||
75 | |||
76 | return r; | ||
77 | } | ||
78 | |||
79 | void matrix_scan_tap_dance () { | ||
80 | if (qk_tap_dance_state.keycode && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) { | ||
81 | process_tap_dance_action (qk_tap_dance_state.keycode); | ||
82 | |||
83 | reset_tap_dance (&qk_tap_dance_state); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | void reset_tap_dance (qk_tap_dance_state_t *state) { | ||
88 | state->keycode = 0; | ||
89 | state->count = 0; | ||
90 | } | ||