Skip to content

Commit 34d774d

Browse files
committed
refactor(behaviors): Replacing global-quick-tap with min-prior-ms
Detaching the global-quick-tap functionality from quick-tap. This makes way for two improvements: 1. This functionality can be added to combos under a unified name 'min-prior-ms'. 2. This allows users to set a lower term for the 'global-quick-tap' (typically ~100ms), and a higher term for the regular quick-tap (typically ~200ms) This deprecates the global-quick-tap option, however if it is set, the quick-tap-ms value will be copied to min-prior-ms.
1 parent 3d2bd01 commit 34d774d

34 files changed

+59
-52
lines changed

app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ properties:
2020
default: -1
2121
quick_tap_ms: # deprecated
2222
type: int
23-
global-quick-tap:
23+
global-quick-tap: # deprecated
2424
type: boolean
25+
min-prior-ms:
26+
type: int
27+
default: -1
2528
flavor:
2629
type: string
2730
required: false

app/src/behaviors/behavior_hold_tap.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ enum decision_moment {
4949
HT_OTHER_KEY_DOWN,
5050
HT_OTHER_KEY_UP,
5151
HT_TIMER_EVENT,
52-
HT_QUICK_TAP,
52+
HT_IMMEDIATE_TAP,
5353
};
5454

5555
struct behavior_hold_tap_config {
5656
int tapping_term_ms;
5757
char *hold_behavior_dev;
5858
char *tap_behavior_dev;
5959
int quick_tap_ms;
60-
bool global_quick_tap;
60+
int min_prior_ms;
6161
enum flavor flavor;
6262
bool retro_tap;
6363
int32_t hold_trigger_key_positions_len;
@@ -96,7 +96,9 @@ struct last_tapped {
9696
int64_t timestamp;
9797
};
9898

99-
struct last_tapped last_tapped = {INT32_MIN, INT64_MIN};
99+
// Set time stamp to large negative number initially for test suites, but not
100+
// int64 min since it will overflow if -1 is added
101+
struct last_tapped last_tapped = {INT32_MIN, INT32_MIN};
100102

101103
static void store_last_tapped(int64_t timestamp) {
102104
if (timestamp > last_tapped.timestamp) {
@@ -110,11 +112,12 @@ static void store_last_hold_tapped(struct active_hold_tap *hold_tap) {
110112
last_tapped.timestamp = hold_tap->timestamp;
111113
}
112114

113-
static bool is_quick_tap(struct active_hold_tap *hold_tap) {
114-
if (hold_tap->config->global_quick_tap || last_tapped.position == hold_tap->position) {
115-
return (last_tapped.timestamp + hold_tap->config->quick_tap_ms) > hold_tap->timestamp;
115+
static bool is_immediate_tap(struct active_hold_tap *hold_tap) {
116+
if ((last_tapped.timestamp + hold_tap->config->min_prior_ms) > hold_tap->timestamp) {
117+
return true;
116118
} else {
117-
return false;
119+
return (last_tapped.position == hold_tap->position) &&
120+
(last_tapped.timestamp + hold_tap->config->quick_tap_ms) > hold_tap->timestamp;
118121
}
119122
}
120123

@@ -247,7 +250,7 @@ static void decide_balanced(struct active_hold_tap *hold_tap, enum decision_mome
247250
case HT_TIMER_EVENT:
248251
hold_tap->status = STATUS_HOLD_TIMER;
249252
return;
250-
case HT_QUICK_TAP:
253+
case HT_IMMEDIATE_TAP:
251254
hold_tap->status = STATUS_TAP;
252255
return;
253256
default:
@@ -263,7 +266,7 @@ static void decide_tap_preferred(struct active_hold_tap *hold_tap, enum decision
263266
case HT_TIMER_EVENT:
264267
hold_tap->status = STATUS_HOLD_TIMER;
265268
return;
266-
case HT_QUICK_TAP:
269+
case HT_IMMEDIATE_TAP:
267270
hold_tap->status = STATUS_TAP;
268271
return;
269272
default:
@@ -283,7 +286,7 @@ static void decide_tap_unless_interrupted(struct active_hold_tap *hold_tap,
283286
case HT_TIMER_EVENT:
284287
hold_tap->status = STATUS_TAP;
285288
return;
286-
case HT_QUICK_TAP:
289+
case HT_IMMEDIATE_TAP:
287290
hold_tap->status = STATUS_TAP;
288291
return;
289292
default:
@@ -302,7 +305,7 @@ static void decide_hold_preferred(struct active_hold_tap *hold_tap, enum decisio
302305
case HT_TIMER_EVENT:
303306
hold_tap->status = STATUS_HOLD_TIMER;
304307
return;
305-
case HT_QUICK_TAP:
308+
case HT_IMMEDIATE_TAP:
306309
hold_tap->status = STATUS_TAP;
307310
return;
308311
default:
@@ -348,8 +351,8 @@ static inline const char *decision_moment_str(enum decision_moment decision_mome
348351
return "other-key-down";
349352
case HT_OTHER_KEY_UP:
350353
return "other-key-up";
351-
case HT_QUICK_TAP:
352-
return "quick-tap";
354+
case HT_IMMEDIATE_TAP:
355+
return "immediate";
353356
case HT_TIMER_EVENT:
354357
return "timer";
355358
default:
@@ -527,8 +530,8 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
527530
LOG_DBG("%d new undecided hold_tap", event.position);
528531
undecided_hold_tap = hold_tap;
529532

530-
if (is_quick_tap(hold_tap)) {
531-
decide_hold_tap(hold_tap, HT_QUICK_TAP);
533+
if (is_immediate_tap(hold_tap)) {
534+
decide_hold_tap(hold_tap, HT_IMMEDIATE_TAP);
532535
}
533536

534537
// if this behavior was queued we have to adjust the timer to only
@@ -696,7 +699,8 @@ static int behavior_hold_tap_init(const struct device *dev) {
696699
.hold_behavior_dev = DT_LABEL(DT_INST_PHANDLE_BY_IDX(n, bindings, 0)), \
697700
.tap_behavior_dev = DT_LABEL(DT_INST_PHANDLE_BY_IDX(n, bindings, 1)), \
698701
.quick_tap_ms = DT_INST_PROP(n, quick_tap_ms), \
699-
.global_quick_tap = DT_INST_PROP(n, global_quick_tap), \
702+
.min_prior_ms = DT_INST_PROP(n, global_quick_tap) ? DT_INST_PROP(n, quick_tap_ms) \
703+
: DT_INST_PROP(n, min_prior_ms), \
700704
.flavor = DT_ENUM_IDX(DT_DRV_INST(n), flavor), \
701705
.retro_tap = DT_INST_PROP(n, retro_tap), \
702706
.hold_trigger_key_positions = DT_INST_PROP(n, hold_trigger_key_positions), \

app/tests/hold-tap/balanced/5-quick-tap/keycode_events.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
44
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
55
ht_binding_released: 0 cleaning up hold-tap
66
ht_binding_pressed: 0 new undecided hold_tap
7-
ht_decide: 0 decided tap (balanced decision moment quick-tap)
7+
ht_decide: 0 decided tap (balanced decision moment immediate)
88
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
99
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
1010
ht_binding_released: 0 cleaning up hold-tap

app/tests/hold-tap/balanced/8-global-quick-tap/1-basic/keycode_events.snapshot renamed to app/tests/hold-tap/balanced/8-min-prior-ms/1-basic/keycode_events.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
44
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
55
ht_binding_released: 0 cleaning up hold-tap
66
ht_binding_pressed: 0 new undecided hold_tap
7-
ht_decide: 0 decided tap (balanced decision moment quick-tap)
7+
ht_decide: 0 decided tap (balanced decision moment immediate)
88
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
99
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
1010
ht_binding_released: 0 cleaning up hold-tap
@@ -17,7 +17,7 @@ kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
1717
ht_binding_released: 0 cleaning up hold-tap
1818
kp_pressed: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
1919
ht_binding_pressed: 0 new undecided hold_tap
20-
ht_decide: 0 decided tap (balanced decision moment quick-tap)
20+
ht_decide: 0 decided tap (balanced decision moment immediate)
2121
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
2222
kp_released: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
2323
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00

app/tests/hold-tap/tap-preferred/8-global-quick-tap/1-basic/native_posix_64.keymap renamed to app/tests/hold-tap/balanced/8-min-prior-ms/1-basic/native_posix_64.keymap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
&kscan {
77
events = <
88
/* tap */
9-
ZMK_MOCK_PRESS(0,0,10)
10-
ZMK_MOCK_RELEASE(0,0,10)
11-
/* normal quick tap */
9+
ZMK_MOCK_PRESS(0,0,10)
10+
ZMK_MOCK_RELEASE(0,0,250)
11+
/* quick tap */
1212
ZMK_MOCK_PRESS(0,0,400)
1313
ZMK_MOCK_RELEASE(0,0,400)
1414
/* hold */
1515
ZMK_MOCK_PRESS(0,0,400)
1616
ZMK_MOCK_PRESS(1,0,10)
1717
ZMK_MOCK_RELEASE(1,0,10)
1818
ZMK_MOCK_RELEASE(0,0,400)
19-
/* global quick tap */
20-
ZMK_MOCK_PRESS(1,0,10)
19+
/* min prior term */
20+
ZMK_MOCK_PRESS(1,0,100)
2121
ZMK_MOCK_PRESS(0,0,400)
2222
ZMK_MOCK_RELEASE(1,0,10)
2323
ZMK_MOCK_RELEASE(0,0,10)

app/tests/hold-tap/balanced/8-global-quick-tap/behavior_keymap.dtsi renamed to app/tests/hold-tap/balanced/8-min-prior-ms/behavior_keymap.dtsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
flavor = "balanced";
1212
tapping-term-ms = <300>;
1313
quick-tap-ms = <300>;
14+
min-prior-ms = <100>;
1415
bindings = <&kp>, <&kp>;
15-
global-quick-tap;
1616
};
1717
};
1818

0 commit comments

Comments
 (0)