2 #include "action_tapping.h"
4 static uint16_t last_td
;
5 static int8_t highest_td
= -1;
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
;
10 if (state
->count
== 1) {
11 register_code16 (pair
->kc1
);
12 } else if (state
->count
== 2) {
13 register_code16 (pair
->kc2
);
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
;
20 if (state
->count
== 1) {
21 unregister_code16 (pair
->kc1
);
22 } else if (state
->count
== 2) {
23 unregister_code16 (pair
->kc2
);
27 static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t
*state
,
29 qk_tap_dance_user_fn_t fn
)
36 static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t
*action
)
38 _process_tap_dance_action_fn (&action
->state
, action
->user_data
, action
->fn
.on_each_tap
);
41 static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t
*action
)
43 if (action
->state
.finished
)
45 action
->state
.finished
= true;
46 _process_tap_dance_action_fn (&action
->state
, action
->user_data
, action
->fn
.on_dance_finished
);
49 static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t
*action
)
51 _process_tap_dance_action_fn (&action
->state
, action
->user_data
, action
->fn
.on_reset
);
54 bool process_tap_dance(uint16_t keycode
, keyrecord_t
*record
) {
55 uint16_t idx
= keycode
- QK_TAP_DANCE
;
56 qk_tap_dance_action_t
*action
;
58 if (last_td
&& last_td
!= keycode
) {
59 (&tap_dance_actions
[last_td
- QK_TAP_DANCE
])->state
.interrupted
= true;
63 case QK_TAP_DANCE
... QK_TAP_DANCE_MAX
:
64 if ((int16_t)idx
> highest_td
)
66 action
= &tap_dance_actions
[idx
];
68 action
->state
.keycode
= keycode
;
69 action
->state
.pressed
= record
->event
.pressed
;
70 if (record
->event
.pressed
) {
71 action
->state
.count
++;
72 action
->state
.timer
= timer_read();
74 if (last_td
&& last_td
!= keycode
) {
75 qk_tap_dance_action_t
*paction
= &tap_dance_actions
[last_td
- QK_TAP_DANCE
];
76 paction
->state
.interrupted
= true;
77 process_tap_dance_action_on_dance_finished (paction
);
78 reset_tap_dance (&paction
->state
);
86 if (!record
->event
.pressed
)
92 for (int i
= 0; i
<= highest_td
; i
++) {
93 action
= &tap_dance_actions
[i
];
94 if (action
->state
.count
== 0)
96 action
->state
.interrupted
= true;
97 process_tap_dance_action_on_dance_finished (action
);
98 reset_tap_dance (&action
->state
);
106 void matrix_scan_tap_dance () {
107 if (highest_td
== -1)
110 for (int i
= 0; i
<= highest_td
; i
++) {
111 qk_tap_dance_action_t
*action
= &tap_dance_actions
[i
];
113 if (action
->state
.count
&& timer_elapsed (action
->state
.timer
) > TAPPING_TERM
) {
114 process_tap_dance_action_on_dance_finished (action
);
115 reset_tap_dance (&action
->state
);
120 void reset_tap_dance (qk_tap_dance_state_t
*state
) {
121 qk_tap_dance_action_t
*action
;
126 action
= &tap_dance_actions
[state
->keycode
- QK_TAP_DANCE
];
128 process_tap_dance_action_on_reset (action
);
131 state
->interrupted
= false;
132 state
->finished
= false;