]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.c
bab6bb81b36ef05fbbe60e3a7a084d266ef7d380
[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     if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) {
74       process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode);
75     } else {
76       r = false;
77     }
78
79     if (record->event.pressed) {
80       qk_tap_dance_state.keycode = keycode;
81       qk_tap_dance_state.timer = timer_read ();
82       qk_tap_dance_state.count++;
83       process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode);
84     }
85     break;
86
87   default:
88     if (qk_tap_dance_state.keycode) {
89       process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode);
90       process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode);
91       reset_tap_dance (&qk_tap_dance_state);
92     }
93     break;
94   }
95
96   return r;
97 }
98
99 void matrix_scan_tap_dance () {
100   if (qk_tap_dance_state.keycode && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) {
101     process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode);
102     reset_tap_dance (&qk_tap_dance_state);
103   }
104 }
105
106 void reset_tap_dance (qk_tap_dance_state_t *state) {
107   uint16_t idx = state->keycode - QK_TAP_DANCE;
108   qk_tap_dance_action_t action;
109
110   action = tap_dance_actions[idx];
111   switch (action.type) {
112   case QK_TAP_DANCE_TYPE_FN:
113     if (action.fn.on_reset) {
114       action.fn.on_reset(state);
115     }
116     break;
117
118   default:
119     break;
120   }
121
122   state->keycode = 0;
123   state->count = 0;
124 }