]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.h
Do some cleanup for the API
[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   uint16_t keycode;
29   uint16_t timer;
30   bool interrupted;
31   bool pressed;
32   bool finished;
33 } qk_tap_dance_state_t;
34
35 #define TD(n) (QK_TAP_DANCE + n)
36
37 typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data);
38
39 typedef struct
40 {
41   struct {
42     qk_tap_dance_user_fn_t on_each_tap;
43     qk_tap_dance_user_fn_t on_dance_finished;
44     qk_tap_dance_user_fn_t on_reset;
45   } fn;
46   qk_tap_dance_state_t state;
47   uint16_t custom_tapping_term;
48   void *user_data;
49 } qk_tap_dance_action_t;
50
51 typedef struct
52 {
53   uint16_t kc1;
54   uint16_t kc2;
55 } qk_tap_dance_pair_t;
56
57 typedef struct
58 {
59   uint16_t kc;
60   uint8_t layer;
61 } qk_tap_dance_dual_role_t;
62
63 #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \
64     .fn = { NULL, qk_tap_dance_pair_finished, qk_tap_dance_pair_reset }, \
65     .user_data = (void *)&((qk_tap_dance_pair_t) { kc1, kc2 }),  \
66   }
67
68 #define ACTION_TAP_DANCE_DUAL_ROLE(kc, layer) { \
69     .fn = { NULL, qk_tap_dance_dual_role_finished, qk_tap_dance_dual_role_reset }, \
70     .user_data = (void *)&((qk_tap_dance_dual_role_t) { kc, layer }), \
71   }
72
73 #define ACTION_TAP_DANCE_FN(user_fn) {  \
74     .fn = { NULL, user_fn, NULL }, \
75     .user_data = NULL, \
76   }
77
78 #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset) { \
79     .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset }, \
80     .user_data = NULL, \
81   }
82
83 #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) { \
84     .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset }, \
85     .user_data = NULL, \
86     .custom_tapping_term = tap_specific_tapping_term, \
87   }
88
89 extern qk_tap_dance_action_t tap_dance_actions[];
90
91 /* To be used internally */
92
93 bool process_tap_dance(uint16_t keycode, keyrecord_t *record);
94 void matrix_scan_tap_dance (void);
95 void reset_tap_dance (qk_tap_dance_state_t *state);
96
97 void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data);
98 void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data);
99
100 void qk_tap_dance_dual_role_finished (qk_tap_dance_state_t *state, void *user_data);
101 void qk_tap_dance_dual_role_reset (qk_tap_dance_state_t *state, void *user_data);
102
103 #else
104
105 #define TD(n) KC_NO
106
107 #endif
108
109 #endif