]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.c
Move Atreus50 to hardwired directory and update README
[qmk_firmware.git] / quantum / process_keycode / process_tap_dance.c
1 #include "quantum.h"
2 #include "action_tapping.h"
3
4 static uint16_t last_td;
5 static int8_t highest_td = -1;
6
7 void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) {
8   qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
9
10   if (state->count == 1) {
11     register_code16 (pair->kc1);
12   } else if (state->count == 2) {
13     register_code16 (pair->kc2);
14   }
15 }
16
17 void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) {
18   qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
19
20   if (state->count == 1) {
21     unregister_code16 (pair->kc1);
22   } else if (state->count == 2) {
23     unregister_code16 (pair->kc2);
24   }
25 }
26
27 static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
28                                                  void *user_data,
29                                                  qk_tap_dance_user_fn_t fn)
30 {
31   if (fn) {
32     fn(state, user_data);
33   }
34 }
35
36 static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t *action)
37 {
38   _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_each_tap);
39 }
40
41 static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t *action)
42 {
43   if (action->state.finished)
44     return;
45   action->state.finished = true;
46   add_mods(action->state.oneshot_mods);
47   send_keyboard_report();
48   _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_dance_finished);
49 }
50
51 static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t *action)
52 {
53   _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_reset);
54   del_mods(action->state.oneshot_mods);
55   send_keyboard_report();
56 }
57
58 bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
59   uint16_t idx = keycode - QK_TAP_DANCE;
60   qk_tap_dance_action_t *action;
61
62   if (last_td && last_td != keycode) {
63     (&tap_dance_actions[last_td - QK_TAP_DANCE])->state.interrupted = true;
64   }
65
66   switch(keycode) {
67   case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
68     if ((int16_t)idx > highest_td)
69       highest_td = idx;
70     action = &tap_dance_actions[idx];
71
72     action->state.pressed = record->event.pressed;
73     if (record->event.pressed) {
74       action->state.keycode = keycode;
75       action->state.count++;
76       action->state.timer = timer_read();
77       action->state.oneshot_mods = get_oneshot_mods();
78       process_tap_dance_action_on_each_tap (action);
79
80       if (last_td && last_td != keycode) {
81         qk_tap_dance_action_t *paction = &tap_dance_actions[last_td - QK_TAP_DANCE];
82         paction->state.interrupted = true;
83         process_tap_dance_action_on_dance_finished (paction);
84         reset_tap_dance (&paction->state);
85       }
86
87       last_td = keycode;
88     }
89
90     break;
91
92   default:
93     if (!record->event.pressed)
94       return true;
95
96     if (highest_td == -1)
97       return true;
98
99     for (int i = 0; i <= highest_td; i++) {
100       action = &tap_dance_actions[i];
101       if (action->state.count == 0)
102         continue;
103       action->state.interrupted = true;
104       process_tap_dance_action_on_dance_finished (action);
105       reset_tap_dance (&action->state);
106     }
107     break;
108   }
109
110   return true;
111 }
112
113 void matrix_scan_tap_dance () {
114   if (highest_td == -1)
115     return;
116
117 for (int i = 0; i <= highest_td; i++) {
118     qk_tap_dance_action_t *action = &tap_dance_actions[i];
119
120     if (action->state.count && timer_elapsed (action->state.timer) > TAPPING_TERM) {
121       process_tap_dance_action_on_dance_finished (action);
122       reset_tap_dance (&action->state);
123     }
124   }
125 }
126
127 void reset_tap_dance (qk_tap_dance_state_t *state) {
128   qk_tap_dance_action_t *action;
129
130   if (state->pressed)
131     return;
132
133   action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
134
135   process_tap_dance_action_on_reset (action);
136
137   state->count = 0;
138   state->interrupted = false;
139   state->finished = false;
140   last_td = 0;
141 }