]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.c
tap-dance: Support for holding keys
[qmk_firmware.git] / quantum / process_keycode / process_tap_dance.c
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   if (fn) {
26     fn(state);
27   }
28 }
29
30 void process_tap_dance_action_on_each_tap (uint16_t keycode)
31 {
32   uint16_t idx = keycode - QK_TAP_DANCE;
33   qk_tap_dance_action_t action;
34
35   action = tap_dance_actions[idx];
36
37   switch (action.type) {
38   case QK_TAP_DANCE_TYPE_FN:
39     _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_each_tap);
40     break;
41
42   default:
43     break;
44   }
45 }
46
47 void process_tap_dance_action_on_dance_finished (uint16_t keycode)
48 {
49   uint16_t idx = keycode - QK_TAP_DANCE;
50   qk_tap_dance_action_t action;
51
52   action = tap_dance_actions[idx];
53
54   switch (action.type) {
55   case QK_TAP_DANCE_TYPE_PAIR:
56     _process_tap_dance_action_pair (&qk_tap_dance_state,
57                                     action.pair.kc1, action.pair.kc2);
58     break;
59   case QK_TAP_DANCE_TYPE_FN:
60     _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_dance_finished);
61     break;
62
63   default:
64     break;
65   }
66 }
67
68 bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
69   bool r = true;
70
71   switch(keycode) {
72   case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
73     process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode);
74     if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) {
75       process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode);
76     } else if (qk_tap_dance_state.active && qk_tap_dance_state.pressed) {
77       reset_tap_dance (&qk_tap_dance_state);
78     } else {
79       r = false;
80     }
81
82     qk_tap_dance_state.active = true;
83     qk_tap_dance_state.pressed = record->event.pressed;
84     if (record->event.pressed) {
85       qk_tap_dance_state.keycode = keycode;
86       qk_tap_dance_state.timer = timer_read ();
87       qk_tap_dance_state.count++;
88     }
89     break;
90
91   default:
92     if (qk_tap_dance_state.keycode) {
93       // if we are here, the tap dance was interrupted by a different key
94       process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode);
95       process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode);
96       reset_tap_dance (&qk_tap_dance_state);
97       qk_tap_dance_state.active = false;
98     }
99     break;
100   }
101
102   return r;
103 }
104
105 void matrix_scan_tap_dance () {
106   if (qk_tap_dance_state.active && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) {
107     // if we are here, the tap dance was timed out
108     process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode);
109     reset_tap_dance (&qk_tap_dance_state);
110   }
111 }
112
113 void reset_tap_dance (qk_tap_dance_state_t *state) {
114   uint16_t idx = state->keycode - QK_TAP_DANCE;
115   qk_tap_dance_action_t action;
116
117   if (state->pressed)
118     return;
119
120   action = tap_dance_actions[idx];
121   switch (action.type) {
122   case QK_TAP_DANCE_TYPE_FN:
123     if (action.fn.on_reset) {
124       action.fn.on_reset(state);
125     }
126     break;
127
128   default:
129     break;
130   }
131
132   state->keycode = 0;
133   state->count = 0;
134   state->active = false;
135 }