]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.h
ab20ea04ed361d7a6675f9ddba291afc106692b5
[qmk_firmware.git] / quantum / process_keycode / process_tap_dance.h
1 /* Copyright 2016 Jack Humbert
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #ifndef PROCESS_TAP_DANCE_H
17 #define PROCESS_TAP_DANCE_H
18
19 #ifdef TAP_DANCE_ENABLE
20
21 #include <stdbool.h>
22 #include <inttypes.h>
23
24 typedef struct
25 {
26   uint8_t count;
27   uint8_t oneshot_mods;
28   uint8_t weak_mods;
29   uint16_t keycode;
30   uint16_t timer;
31   bool interrupted;
32   bool pressed;
33   bool finished;
34 } qk_tap_dance_state_t;
35
36 #define TD(n) (QK_TAP_DANCE + n)
37
38 typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data);
39
40 typedef struct
41 {
42   struct {
43     qk_tap_dance_user_fn_t on_each_tap;
44     qk_tap_dance_user_fn_t on_dance_finished;
45     qk_tap_dance_user_fn_t on_reset;
46   } fn;
47   qk_tap_dance_state_t state;
48   uint16_t custom_tapping_term;
49   void *user_data;
50 } qk_tap_dance_action_t;
51
52 typedef struct
53 {
54   uint16_t kc1;
55   uint16_t kc2;
56 } qk_tap_dance_pair_t;
57
58 typedef struct
59 {
60   uint16_t kc;
61   uint8_t layer;
62 } qk_tap_dance_dual_role_t;
63
64 #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \
65     .fn = { NULL, qk_tap_dance_pair_finished, qk_tap_dance_pair_reset }, \
66     .user_data = (void *)&((qk_tap_dance_pair_t) { kc1, kc2 }),  \
67   }
68
69 #define ACTION_TAP_DANCE_DUAL_ROLE(kc, layer) { \
70     .fn = { NULL, qk_tap_dance_dual_role_finished, qk_tap_dance_dual_role_reset }, \
71     .user_data = (void *)&((qk_tap_dance_dual_role_t) { kc, layer }), \
72   }
73
74 #define ACTION_TAP_DANCE_FN(user_fn) {  \
75     .fn = { NULL, user_fn, NULL }, \
76     .user_data = NULL, \
77   }
78
79 #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset) { \
80     .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset }, \
81     .user_data = NULL, \
82   }
83
84 #define ACTION_TAP_DANCE_FN_ADVANCED_TIME(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset, tap_specific_tapping_term) { \
85     .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset }, \
86     .user_data = NULL, \
87     .custom_tapping_term = tap_specific_tapping_term, \
88   }
89
90 extern qk_tap_dance_action_t tap_dance_actions[];
91
92 /* To be used internally */
93
94 bool process_tap_dance(uint16_t keycode, keyrecord_t *record);
95 void matrix_scan_tap_dance (void);
96 void reset_tap_dance (qk_tap_dance_state_t *state);
97
98 void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data);
99 void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data);
100
101 void qk_tap_dance_dual_role_finished (qk_tap_dance_state_t *state, void *user_data);
102 void qk_tap_dance_dual_role_reset (qk_tap_dance_state_t *state, void *user_data);
103
104 #else
105
106 #define TD(n) KC_NO
107
108 #endif
109
110 #endif