gestures: Simplify some repetitive code
What changed, and why it matters
This commit is a straightforward code cleanup in the touch gesture handling of the BitBox02 hardware wallet. It replaces five nearly identical two-line functions with a small lookup table and a loop. The order of one gesture event constant was moved in the enum so the loop can iterate over all event types. There is no security-relevant change: the same checks are performed in the same order and the same events are emitted under the same conditions.
No security action required. Treat as a normal maintainability/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors gesture event emission in src/touch/gestures.c. Previously, five static helper functions each tested top_slider and bottom_slider with a specific predicate and emitted the corresponding event. The patch introduces a function-pointer array indexed by event ID and a single loop that calls the predicate for each slider and emits the event if true. To make the array indexing contiguous, src/ui/event.h reorders EVENT_CONTINUOUS_TAP to after EVENT_SHORT_TAP and adds EVENT_ENUM_MAX. The predicates and emission logic are unchanged; only boilerplate is removed.
Changed components
src/touch/gestures.csrc/ui/event.hInspect captured patch +19 / −55
diff --git a/src/touch/gestures.c b/src/touch/gestures.c
index 3450cd8..640bf6d 100644
--- a/src/touch/gestures.c
+++ b/src/touch/gestures.c
@@ -146,6 +146,8 @@ static void _reset_state(void)
/********************************** GESTURE DETECTION **********************************/
+typedef bool (*gesture_detect_fn)(uint8_t location);
+
static bool _is_continuous_tap(uint8_t location)
{
return _state[location].max_slide_travel < TAP_SLIDE_TOLERANCE &&
@@ -192,55 +194,13 @@ static void _gesture_emit_event(uint8_t id, slider_location_t location)
emit_event(&event);
}
-static void _emit_continuous_slide_event(void)
-{
- if (_is_continuous_slide(top_slider)) {
- _gesture_emit_event(EVENT_SLIDE, top_slider);
- }
- if (_is_continuous_slide(bottom_slider)) {
- _gesture_emit_event(EVENT_SLIDE, bottom_slider);
- }
-}
-
-static void _emit_slide_release_event(void)
-{
- if (_is_slide_released(top_slider)) {
- _gesture_emit_event(EVENT_SLIDE_RELEASED, top_slider);
- }
- if (_is_slide_released(bottom_slider)) {
- _gesture_emit_event(EVENT_SLIDE_RELEASED, bottom_slider);
- }
-}
-
-static void _emit_long_tap_event(void)
-{
- if (_is_long_tap_release(top_slider)) {
- _gesture_emit_event(EVENT_LONG_TAP, top_slider);
- }
- if (_is_long_tap_release(bottom_slider)) {
- _gesture_emit_event(EVENT_LONG_TAP, bottom_slider);
- }
-}
-
-static void _emit_short_tap_event(void)
-{
- if (_is_tap_release(top_slider)) {
- _gesture_emit_event(EVENT_SHORT_TAP, top_slider);
- }
- if (_is_tap_release(bottom_slider)) {
- _gesture_emit_event(EVENT_SHORT_TAP, bottom_slider);
- }
-}
-
-static void _emit_continuous_tap_event(void)
-{
- if (_is_continuous_tap(top_slider)) {
- _gesture_emit_event(EVENT_CONTINUOUS_TAP, top_slider);
- }
- if (_is_continuous_tap(bottom_slider)) {
- _gesture_emit_event(EVENT_CONTINUOUS_TAP, bottom_slider);
- }
-}
+static gesture_detect_fn _emit_event_detect_fns[EVENT_ENUM_MAX] = {
+ [EVENT_SLIDE] = _is_continuous_slide,
+ [EVENT_SLIDE_RELEASED] = _is_slide_released,
+ [EVENT_LONG_TAP] = _is_long_tap_release,
+ [EVENT_SHORT_TAP] = _is_tap_release,
+ [EVENT_CONTINUOUS_TAP] = _is_continuous_tap,
+};
/********************************** MEASURE, DETECT and CALLBACK **********************************/
@@ -263,11 +223,14 @@ static void _measure_and_emit(void)
}
if (gesture_detected) {
- _emit_continuous_slide_event();
- _emit_slide_release_event();
- _emit_long_tap_event();
- _emit_short_tap_event();
- _emit_continuous_tap_event();
+ for (int event_idx = 0; event_idx < EVENT_ENUM_MAX; event_idx++) {
+ if (_emit_event_detect_fns[event_idx](top_slider)) {
+ _gesture_emit_event(event_idx, top_slider);
+ }
+ if (_emit_event_detect_fns[event_idx](bottom_slider)) {
+ _gesture_emit_event(event_idx, bottom_slider);
+ }
+ }
}
bool both_sliders_released_or_inactive = true;
diff --git a/src/ui/event.h b/src/ui/event.h
index cb1e3fd..8e8cf92 100644
--- a/src/ui/event.h
+++ b/src/ui/event.h
@@ -28,9 +28,10 @@ typedef struct {
enum {
EVENT_SLIDE,
EVENT_SLIDE_RELEASED,
- EVENT_CONTINUOUS_TAP,
EVENT_LONG_TAP,
EVENT_SHORT_TAP,
+ EVENT_CONTINUOUS_TAP,
+ EVENT_ENUM_MAX, // MAX must always be last, indicates the number of items in the enumration
};
typedef struct {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.