summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJouke Witteveen <j.witteveen@gmail.com>2022-04-16 20:24:09 +0200
committerGitHub <noreply@github.com>2022-04-16 11:24:09 -0700
commit8f585153c470b07bb0c529ff49b39ef45f68d37e (patch)
tree5441986fa041cd2d966b800af3f82d69b55e4d58
parentcad0af09a8a280d918b726eb472c86065dc5c079 (diff)
Add GET_TAPPING_TERM macro to reduce duplicate code (#16681)
* Add GET_TAPPING_TERM macro to reduce duplicate code The macro gives the right tapping term depending on whether per-key tapping terms and/or dynamic tapping terms are enabled. Unnecessary function calls and variable resolution are avoided. Fixes #16472. * Use GET_TAPPING_TERM for Cirque trackpads Co-authored-by: Stefan Kerkmann <karlk90@pm.me>
-rw-r--r--docs/tap_hold.md2
-rw-r--r--quantum/action_tapping.c23
-rw-r--r--quantum/action_tapping.h8
-rw-r--r--quantum/pointing_device_drivers.c14
-rw-r--r--quantum/process_keycode/process_auto_shift.c7
-rw-r--r--quantum/process_keycode/process_space_cadet.c7
-rw-r--r--quantum/process_keycode/process_tap_dance.c6
7 files changed, 24 insertions, 43 deletions
diff --git a/docs/tap_hold.md b/docs/tap_hold.md
index 39fa84a9f3..601aef0493 100644
--- a/docs/tap_hold.md
+++ b/docs/tap_hold.md
@@ -112,7 +112,7 @@ uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
112} 112}
113``` 113```
114 114
115The reason being that `TAPPING_TERM` is a macro that expands to a constant integer and thus cannot be changed at runtime whereas `g_tapping_term` is a variable whose value can be changed at runtime. If you want, you can temporarily enable `DYNAMIC_TAPPING_TERM_ENABLE` to find a suitable tapping term value and then disable that feature and revert back to using the classic syntax for per-key tapping term settings. 115The reason being that `TAPPING_TERM` is a macro that expands to a constant integer and thus cannot be changed at runtime whereas `g_tapping_term` is a variable whose value can be changed at runtime. If you want, you can temporarily enable `DYNAMIC_TAPPING_TERM_ENABLE` to find a suitable tapping term value and then disable that feature and revert back to using the classic syntax for per-key tapping term settings. In case you need to access the tapping term from elsewhere in your code, you can use the `GET_TAPPING_TERM(keycode, record)` macro. This macro will expand to whatever is the appropriate access pattern given the current configuration.
116 116
117## Tap-Or-Hold Decision Modes 117## Tap-Or-Hold Decision Modes
118 118
diff --git a/quantum/action_tapping.c b/quantum/action_tapping.c
index e436619428..3c8b5678b7 100644
--- a/quantum/action_tapping.c
+++ b/quantum/action_tapping.c
@@ -24,17 +24,20 @@
24# else 24# else
25# define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode) 25# define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode)
26# endif 26# endif
27# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < GET_TAPPING_TERM(get_record_keycode(&tapping_key, false), &tapping_key))
27 28
29# ifdef DYNAMIC_TAPPING_TERM_ENABLE
28uint16_t g_tapping_term = TAPPING_TERM; 30uint16_t g_tapping_term = TAPPING_TERM;
31# endif
29 32
33# ifdef TAPPING_TERM_PER_KEY
30__attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { 34__attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
35# ifdef DYNAMIC_TAPPING_TERM_ENABLE
31 return g_tapping_term; 36 return g_tapping_term;
37# else
38 return TAPPING_TERM;
39# endif
32} 40}
33
34# ifdef TAPPING_TERM_PER_KEY
35# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_record_keycode(&tapping_key, false), &tapping_key))
36# else
37# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < g_tapping_term)
38# endif 41# endif
39 42
40# ifdef TAPPING_FORCE_HOLD_PER_KEY 43# ifdef TAPPING_FORCE_HOLD_PER_KEY
@@ -165,15 +168,7 @@ bool process_tapping(keyrecord_t *keyp) {
165 else if ( 168 else if (
166 ( 169 (
167 ( 170 (
168 ( 171 GET_TAPPING_TERM(tapping_keycode, &tapping_key) >= 500
169# ifdef TAPPING_TERM_PER_KEY
170 get_tapping_term(tapping_keycode, &tapping_key)
171# else
172 g_tapping_term
173# endif
174 >= 500
175 )
176
177# ifdef PERMISSIVE_HOLD_PER_KEY 172# ifdef PERMISSIVE_HOLD_PER_KEY
178 || get_permissive_hold(tapping_keycode, &tapping_key) 173 || get_permissive_hold(tapping_keycode, &tapping_key)
179# elif defined(PERMISSIVE_HOLD) 174# elif defined(PERMISSIVE_HOLD)
diff --git a/quantum/action_tapping.h b/quantum/action_tapping.h
index b2feb6850c..9b64c93120 100644
--- a/quantum/action_tapping.h
+++ b/quantum/action_tapping.h
@@ -44,3 +44,11 @@ bool get_retro_tapping(uint16_t keycode, keyrecord_t *record);
44#ifdef DYNAMIC_TAPPING_TERM_ENABLE 44#ifdef DYNAMIC_TAPPING_TERM_ENABLE
45extern uint16_t g_tapping_term; 45extern uint16_t g_tapping_term;
46#endif 46#endif
47
48#ifdef TAPPING_TERM_PER_KEY
49# define GET_TAPPING_TERM(keycode, record) get_tapping_term(keycode, record)
50#elif defined(DYNAMIC_TAPPING_TERM_ENABLE)
51# define GET_TAPPING_TERM(keycode, record) g_tapping_term
52#else
53# define GET_TAPPING_TERM(keycode, record) (TAPPING_TERM)
54#endif
diff --git a/quantum/pointing_device_drivers.c b/quantum/pointing_device_drivers.c
index b8ef6e67e5..11cbf6594e 100644
--- a/quantum/pointing_device_drivers.c
+++ b/quantum/pointing_device_drivers.c
@@ -98,17 +98,9 @@ const pointing_device_driver_t pointing_device_driver = {
98// clang-format on 98// clang-format on
99#elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi) 99#elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
100# ifndef CIRQUE_PINNACLE_TAPPING_TERM 100# ifndef CIRQUE_PINNACLE_TAPPING_TERM
101# ifdef TAPPING_TERM_PER_KEY 101# include "action.h"
102# include "action.h" 102# include "action_tapping.h"
103# include "action_tapping.h" 103# define CIRQUE_PINNACLE_TAPPING_TERM GET_TAPPING_TERM(KC_BTN1, &(keyrecord_t){})
104# define CIRQUE_PINNACLE_TAPPING_TERM get_tapping_term(KC_BTN1, &(keyrecord_t){})
105# else
106# ifdef TAPPING_TERM
107# define CIRQUE_PINNACLE_TAPPING_TERM TAPPING_TERM
108# else
109# define CIRQUE_PINNACLE_TAPPING_TERM 200
110# endif
111# endif
112# endif 104# endif
113# ifndef CIRQUE_PINNACLE_TOUCH_DEBOUNCE 105# ifndef CIRQUE_PINNACLE_TOUCH_DEBOUNCE
114# define CIRQUE_PINNACLE_TOUCH_DEBOUNCE (CIRQUE_PINNACLE_TAPPING_TERM * 8) 106# define CIRQUE_PINNACLE_TOUCH_DEBOUNCE (CIRQUE_PINNACLE_TAPPING_TERM * 8)
diff --git a/quantum/process_keycode/process_auto_shift.c b/quantum/process_keycode/process_auto_shift.c
index 2150edd7b2..e6a7c01f2a 100644
--- a/quantum/process_keycode/process_auto_shift.c
+++ b/quantum/process_keycode/process_auto_shift.c
@@ -182,12 +182,7 @@ static bool autoshift_press(uint16_t keycode, uint16_t now, keyrecord_t *record)
182# endif 182# endif
183 ) && 183 ) &&
184# endif 184# endif
185 TIMER_DIFF_16(now, autoshift_time) < 185 TIMER_DIFF_16(now, autoshift_time) < GET_TAPPING_TERM(autoshift_lastkey, record)
186# ifdef TAPPING_TERM_PER_KEY
187 get_tapping_term(autoshift_lastkey, record)
188# else
189 TAPPING_TERM
190# endif
191 ) { 186 ) {
192 // clang-format on 187 // clang-format on
193 // Allow a tap-then-hold for keyrepeat. 188 // Allow a tap-then-hold for keyrepeat.
diff --git a/quantum/process_keycode/process_space_cadet.c b/quantum/process_keycode/process_space_cadet.c
index 46b2648c35..0997e7b7f3 100644
--- a/quantum/process_keycode/process_space_cadet.c
+++ b/quantum/process_keycode/process_space_cadet.c
@@ -93,12 +93,7 @@ void perform_space_cadet(keyrecord_t *record, uint16_t sc_keycode, uint8_t holdM
93 register_mods(MOD_BIT(holdMod)); 93 register_mods(MOD_BIT(holdMod));
94 } 94 }
95 } else { 95 } else {
96#ifdef TAPPING_TERM_PER_KEY 96 if (sc_last == holdMod && timer_elapsed(sc_timer) < GET_TAPPING_TERM(sc_keycode, record)) {
97 if (sc_last == holdMod && timer_elapsed(sc_timer) < get_tapping_term(sc_keycode, record))
98#else
99 if (sc_last == holdMod && timer_elapsed(sc_timer) < TAPPING_TERM)
100#endif
101 {
102 if (holdMod != tapMod) { 97 if (holdMod != tapMod) {
103 if (IS_MOD(holdMod)) { 98 if (IS_MOD(holdMod)) {
104 unregister_mods(MOD_BIT(holdMod)); 99 unregister_mods(MOD_BIT(holdMod));
diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c
index e99119b2ae..db8df5f870 100644
--- a/quantum/process_keycode/process_tap_dance.c
+++ b/quantum/process_keycode/process_tap_dance.c
@@ -174,11 +174,7 @@ void tap_dance_task() {
174 if (action->custom_tapping_term > 0) { 174 if (action->custom_tapping_term > 0) {
175 tap_user_defined = action->custom_tapping_term; 175 tap_user_defined = action->custom_tapping_term;
176 } else { 176 } else {
177#ifdef TAPPING_TERM_PER_KEY 177 tap_user_defined = GET_TAPPING_TERM(action->state.keycode, &(keyrecord_t){});
178 tap_user_defined = get_tapping_term(action->state.keycode, &(keyrecord_t){});
179#else
180 tap_user_defined = TAPPING_TERM;
181#endif
182 } 178 }
183 if (action->state.count && timer_elapsed(action->state.timer) > tap_user_defined) { 179 if (action->state.count && timer_elapsed(action->state.timer) > tap_user_defined) {
184 process_tap_dance_action_on_dance_finished(action); 180 process_tap_dance_action_on_dance_finished(action);