Skip to content

Commit

Permalink
refactor(behaviors): Replacing global-quick-tap with min-prior-ms
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrewjrae committed Jul 18, 2022
1 parent 3d2bd01 commit 34d774d
Show file tree
Hide file tree
Showing 34 changed files with 59 additions and 52 deletions.
5 changes: 4 additions & 1 deletion app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ properties:
default: -1
quick_tap_ms: # deprecated
type: int
global-quick-tap:
global-quick-tap: # deprecated
type: boolean
min-prior-ms:
type: int
default: -1
flavor:
type: string
required: false
Expand Down
36 changes: 20 additions & 16 deletions app/src/behaviors/behavior_hold_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ enum decision_moment {
HT_OTHER_KEY_DOWN,
HT_OTHER_KEY_UP,
HT_TIMER_EVENT,
HT_QUICK_TAP,
HT_IMMEDIATE_TAP,
};

struct behavior_hold_tap_config {
int tapping_term_ms;
char *hold_behavior_dev;
char *tap_behavior_dev;
int quick_tap_ms;
bool global_quick_tap;
int min_prior_ms;
enum flavor flavor;
bool retro_tap;
int32_t hold_trigger_key_positions_len;
Expand Down Expand Up @@ -96,7 +96,9 @@ struct last_tapped {
int64_t timestamp;
};

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

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

static bool is_quick_tap(struct active_hold_tap *hold_tap) {
if (hold_tap->config->global_quick_tap || last_tapped.position == hold_tap->position) {
return (last_tapped.timestamp + hold_tap->config->quick_tap_ms) > hold_tap->timestamp;
static bool is_immediate_tap(struct active_hold_tap *hold_tap) {
if ((last_tapped.timestamp + hold_tap->config->min_prior_ms) > hold_tap->timestamp) {
return true;
} else {
return false;
return (last_tapped.position == hold_tap->position) &&
(last_tapped.timestamp + hold_tap->config->quick_tap_ms) > hold_tap->timestamp;
}
}

Expand Down Expand Up @@ -247,7 +250,7 @@ static void decide_balanced(struct active_hold_tap *hold_tap, enum decision_mome
case HT_TIMER_EVENT:
hold_tap->status = STATUS_HOLD_TIMER;
return;
case HT_QUICK_TAP:
case HT_IMMEDIATE_TAP:
hold_tap->status = STATUS_TAP;
return;
default:
Expand All @@ -263,7 +266,7 @@ static void decide_tap_preferred(struct active_hold_tap *hold_tap, enum decision
case HT_TIMER_EVENT:
hold_tap->status = STATUS_HOLD_TIMER;
return;
case HT_QUICK_TAP:
case HT_IMMEDIATE_TAP:
hold_tap->status = STATUS_TAP;
return;
default:
Expand All @@ -283,7 +286,7 @@ static void decide_tap_unless_interrupted(struct active_hold_tap *hold_tap,
case HT_TIMER_EVENT:
hold_tap->status = STATUS_TAP;
return;
case HT_QUICK_TAP:
case HT_IMMEDIATE_TAP:
hold_tap->status = STATUS_TAP;
return;
default:
Expand All @@ -302,7 +305,7 @@ static void decide_hold_preferred(struct active_hold_tap *hold_tap, enum decisio
case HT_TIMER_EVENT:
hold_tap->status = STATUS_HOLD_TIMER;
return;
case HT_QUICK_TAP:
case HT_IMMEDIATE_TAP:
hold_tap->status = STATUS_TAP;
return;
default:
Expand Down Expand Up @@ -348,8 +351,8 @@ static inline const char *decision_moment_str(enum decision_moment decision_mome
return "other-key-down";
case HT_OTHER_KEY_UP:
return "other-key-up";
case HT_QUICK_TAP:
return "quick-tap";
case HT_IMMEDIATE_TAP:
return "immediate";
case HT_TIMER_EVENT:
return "timer";
default:
Expand Down Expand Up @@ -527,8 +530,8 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
LOG_DBG("%d new undecided hold_tap", event.position);
undecided_hold_tap = hold_tap;

if (is_quick_tap(hold_tap)) {
decide_hold_tap(hold_tap, HT_QUICK_TAP);
if (is_immediate_tap(hold_tap)) {
decide_hold_tap(hold_tap, HT_IMMEDIATE_TAP);
}

// if this behavior was queued we have to adjust the timer to only
Expand Down Expand Up @@ -696,7 +699,8 @@ static int behavior_hold_tap_init(const struct device *dev) {
.hold_behavior_dev = DT_LABEL(DT_INST_PHANDLE_BY_IDX(n, bindings, 0)), \
.tap_behavior_dev = DT_LABEL(DT_INST_PHANDLE_BY_IDX(n, bindings, 1)), \
.quick_tap_ms = DT_INST_PROP(n, quick_tap_ms), \
.global_quick_tap = DT_INST_PROP(n, global_quick_tap), \
.min_prior_ms = DT_INST_PROP(n, global_quick_tap) ? DT_INST_PROP(n, quick_tap_ms) \
: DT_INST_PROP(n, min_prior_ms), \
.flavor = DT_ENUM_IDX(DT_DRV_INST(n), flavor), \
.retro_tap = DT_INST_PROP(n, retro_tap), \
.hold_trigger_key_positions = DT_INST_PROP(n, hold_trigger_key_positions), \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (balanced decision moment quick-tap)
ht_decide: 0 decided tap (balanced decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (balanced decision moment quick-tap)
ht_decide: 0 decided tap (balanced decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Expand All @@ -17,7 +17,7 @@ kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
kp_pressed: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (balanced decision moment quick-tap)
ht_decide: 0 decided tap (balanced decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
&kscan {
events = <
/* tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
/* normal quick tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,250)
/* quick tap */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(0,0,400)
/* hold */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,400)
/* global quick tap */
ZMK_MOCK_PRESS(1,0,10)
/* min prior term */
ZMK_MOCK_PRESS(1,0,100)
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
flavor = "balanced";
tapping-term-ms = <300>;
quick-tap-ms = <300>;
min-prior-ms = <100>;
bindings = <&kp>, <&kp>;
global-quick-tap;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (hold-preferred decision moment quick-tap)
ht_decide: 0 decided tap (hold-preferred decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (hold-preferred decision moment quick-tap)
ht_decide: 0 decided tap (hold-preferred decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Expand All @@ -17,7 +17,7 @@ kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
kp_pressed: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (hold-preferred decision moment quick-tap)
ht_decide: 0 decided tap (hold-preferred decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
&kscan {
events = <
/* tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
/* normal quick tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,250)
/* quick tap */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(0,0,400)
/* hold */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,400)
/* global quick tap */
ZMK_MOCK_PRESS(1,0,10)
/* min prior term */
ZMK_MOCK_PRESS(1,0,100)
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
flavor = "hold-preferred";
tapping-term-ms = <300>;
quick-tap-ms = <300>;
min-prior-ms = <100>;
bindings = <&kp>, <&kp>;
global-quick-tap;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (tap-preferred decision moment quick-tap)
ht_decide: 0 decided tap (tap-preferred decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (tap-preferred decision moment quick-tap)
ht_decide: 0 decided tap (tap-preferred decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Expand All @@ -17,7 +17,7 @@ kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
kp_pressed: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (tap-preferred decision moment quick-tap)
ht_decide: 0 decided tap (tap-preferred decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
events = <
/* tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
/* normal quick tap */
ZMK_MOCK_RELEASE(0,0,250)
/* quick tap */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(0,0,400)
/* hold */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,400)
/* global quick tap */
ZMK_MOCK_PRESS(1,0,10)
/* min prior term */
ZMK_MOCK_PRESS(1,0,100)
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
flavor = "tap-preferred";
tapping-term-ms = <300>;
quick-tap-ms = <300>;
min-prior-ms = <100>;
bindings = <&kp>, <&kp>;
global-quick-tap;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (tap-unless-interrupted decision moment quick-tap)
ht_decide: 0 decided tap (tap-unless-interrupted decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (tap-unless-interrupted decision moment quick-tap)
ht_decide: 0 decided tap (tap-unless-interrupted decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
Expand All @@ -17,7 +17,7 @@ kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
ht_binding_released: 0 cleaning up hold-tap
kp_pressed: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
ht_binding_pressed: 0 new undecided hold_tap
ht_decide: 0 decided tap (tap-unless-interrupted decision moment quick-tap)
ht_decide: 0 decided tap (tap-unless-interrupted decision moment immediate)
kp_pressed: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x07 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x09 implicit_mods 0x00 explicit_mods 0x00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
&kscan {
events = <
/* tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
/* normal quick tap */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,250)
/* quick tap */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(0,0,400)
/* hold */
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,400)
/* global quick tap */
ZMK_MOCK_PRESS(1,0,10)
/* min prior term */
ZMK_MOCK_PRESS(1,0,100)
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,0,10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
flavor = "tap-unless-interrupted";
tapping-term-ms = <300>;
quick-tap-ms = <300>;
min-prior-ms = <100>;
bindings = <&kp>, <&kp>;
global-quick-tap;
};
};

Expand Down

0 comments on commit 34d774d

Please sign in to comment.