events: Remove unique events for top/bottom
What changed, and why it matters
This commit is a straightforward internal code cleanup in the BitBox02 hardware wallet's touch/gesture handling system. It merges separate 'top' and 'bottom' touch events into generic events, carrying the source slider as data instead. There is no security-relevant change: the same gestures are detected, the same components react to them, and no new behavior or vulnerability is introduced.
No security action required. Treat as normal refactoring/technical-debt reduction. Standard code review and regression testing for touch UI behavior is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors the event model so that gesture events are no longer duplicated per slider (top/bottom). A new event_slider_data_t struct embeds slider_location_t source alongside position/diff/velocity, and event_t now stores this struct by value rather than a const void* pointer. All consumers (button, confirm_gesture, icon_button, keyboard_switch, label, left/right arrows, trinary_input_char) are updated to check event->data.source and use the renamed generic event IDs. The change is behavior-preserving: every previous top/bottom-specific branch is replaced by an equivalent source check.
Changed components
src/touch/gestures.csrc/touch/gestures.hsrc/ui/event.hsrc/ui/event_handler.csrc/ui/components/button.csrc/ui/components/confirm_gesture.csrc/ui/components/icon_button.csrc/ui/components/keyboard_switch.csrc/ui/components/label.csrc/ui/components/left_arrow.csrc/ui/components/right_arrow.csrc/ui/components/trinary_input_char.cInspect captured patch +102 / −166
diff --git a/src/touch/gestures.c b/src/touch/gestures.c
index f23ca1e..3450cd8 100644
--- a/src/touch/gestures.c
+++ b/src/touch/gestures.c
@@ -144,18 +144,6 @@ static void _reset_state(void)
memset(_state, 0, sizeof(_state));
}
-/**
- * Prepares the gestures data to be sent with an emitted event
- */
-static void _collect_gestures_data(
- gestures_detection_state_t* state,
- gestures_slider_data_t* slider_data)
-{
- slider_data->position = state->position_current;
- slider_data->diff = state->position_current - state->position_start;
- slider_data->velocity = state->velocity_sum;
-}
-
/********************************** GESTURE DETECTION **********************************/
static bool _is_continuous_tap(uint8_t location)
@@ -194,59 +182,63 @@ static void _gesture_emit_event(uint8_t id, slider_location_t location)
if (!_released_since_new_screen) {
return;
}
- gestures_slider_data_t slider_data;
- _collect_gestures_data(&_state[location], &slider_data);
- event_t event = {.data = &slider_data, .id = id};
+ event_slider_data_t slider_data = {
+ .source = location,
+ .position = _state[location].position_current,
+ .diff = _state[location].position_current - _state[location].position_start,
+ .velocity = _state[location].velocity_sum,
+ };
+ event_t event = {.id = id, .data = slider_data};
emit_event(&event);
}
static void _emit_continuous_slide_event(void)
{
if (_is_continuous_slide(top_slider)) {
- _gesture_emit_event(EVENT_TOP_SLIDE, top_slider);
+ _gesture_emit_event(EVENT_SLIDE, top_slider);
}
if (_is_continuous_slide(bottom_slider)) {
- _gesture_emit_event(EVENT_BOTTOM_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_TOP_SLIDE_RELEASED, top_slider);
+ _gesture_emit_event(EVENT_SLIDE_RELEASED, top_slider);
}
if (_is_slide_released(bottom_slider)) {
- _gesture_emit_event(EVENT_BOTTOM_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_TOP_LONG_TAP, top_slider);
+ _gesture_emit_event(EVENT_LONG_TAP, top_slider);
}
if (_is_long_tap_release(bottom_slider)) {
- _gesture_emit_event(EVENT_BOTTOM_LONG_TAP, 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_TOP_SHORT_TAP, top_slider);
+ _gesture_emit_event(EVENT_SHORT_TAP, top_slider);
}
if (_is_tap_release(bottom_slider)) {
- _gesture_emit_event(EVENT_BOTTOM_SHORT_TAP, 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_TOP_CONTINUOUS_TAP, top_slider);
+ _gesture_emit_event(EVENT_CONTINUOUS_TAP, top_slider);
}
if (_is_continuous_tap(bottom_slider)) {
- _gesture_emit_event(EVENT_BOTTOM_CONTINUOUS_TAP, bottom_slider);
+ _gesture_emit_event(EVENT_CONTINUOUS_TAP, bottom_slider);
}
}
diff --git a/src/touch/gestures.h b/src/touch/gestures.h
index 4719810..c6bc771 100644
--- a/src/touch/gestures.h
+++ b/src/touch/gestures.h
@@ -33,12 +33,6 @@
#define SLIDER_POSITION_ONE_THIRD (MAX_SLIDER_POS / 3)
#define SLIDER_POSITION_TWO_THIRD (MAX_SLIDER_POS / 3 * 2)
-typedef struct {
- int16_t diff;
- uint16_t position;
- int32_t velocity;
-} gestures_slider_data_t;
-
/**
* Detects a gestures and calls the respective callback.
* @param[in] reset The flag indicates whether the gesture history should be
diff --git a/src/ui/components/button.c b/src/ui/components/button.c
index 8564882..307110d 100644
--- a/src/ui/components/button.c
+++ b/src/ui/components/button.c
@@ -59,18 +59,13 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
button_data_t* data = (button_data_t*)component->data;
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
- if (data->span_over_slider) {
- if (event->id ==
- ((data->location == top_slider) ? EVENT_TOP_SHORT_TAP : EVENT_BOTTOM_SHORT_TAP)) {
+ if (event->id == EVENT_SHORT_TAP && event->data.source == data->location) {
+ if (data->span_over_slider) {
data->callback(component);
- }
- } else {
- if (event->id ==
- ((data->location == top_slider) ? EVENT_TOP_SHORT_TAP : EVENT_BOTTOM_SHORT_TAP) &&
- slider_data->position >= component->position.left * MAX_SLIDER_POS / SCREEN_WIDTH &&
- slider_data->position <= (component->position.left + component->dimension.width) *
- MAX_SLIDER_POS / SCREEN_WIDTH) {
+ } else if (
+ event->data.position >= component->position.left * MAX_SLIDER_POS / SCREEN_WIDTH &&
+ event->data.position <= (component->position.left + component->dimension.width) *
+ MAX_SLIDER_POS / SCREEN_WIDTH) {
data->callback(component);
}
}
diff --git a/src/ui/components/confirm_gesture.c b/src/ui/components/confirm_gesture.c
index cfd650a..81fbbd8 100644
--- a/src/ui/components/confirm_gesture.c
+++ b/src/ui/components/confirm_gesture.c
@@ -100,41 +100,32 @@ static void _render(component_t* component)
*/
static void _on_event(const event_t* event, component_t* component)
{
- // Avoid type casting to unknown events
- if (event->id != EVENT_TOP_SLIDE_RELEASED && event->id != EVENT_BOTTOM_SLIDE_RELEASED &&
- event->id != EVENT_TOP_CONTINUOUS_TAP && event->id != EVENT_BOTTOM_CONTINUOUS_TAP &&
- event->id != EVENT_TOP_SHORT_TAP && event->id != EVENT_BOTTOM_SHORT_TAP &&
- event->id != EVENT_TOP_LONG_TAP && event->id != EVENT_BOTTOM_LONG_TAP &&
- event->id != EVENT_TOP_SLIDE && event->id != EVENT_BOTTOM_SLIDE) {
- return;
- }
confirm_data_t* data = (confirm_data_t*)component->data;
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
switch (event->id) {
- case EVENT_TOP_SLIDE_RELEASED:
- data->active_top = false;
- break;
- case EVENT_TOP_CONTINUOUS_TAP:
- if (slider_data->position > SLIDER_POSITION_TWO_THIRD &&
- slider_data->position <= MAX_SLIDER_POS) {
- data->active_top = true;
+ case EVENT_SLIDE_RELEASED:
+ if (event->data.source == top_slider) {
+ data->active_top = false;
+ } else {
+ data->active_bottom = false;
}
break;
- case EVENT_TOP_SHORT_TAP:
- data->active_top = false;
- break;
- case EVENT_BOTTOM_SLIDE_RELEASED:
- data->active_bottom = false;
- break;
- case EVENT_BOTTOM_CONTINUOUS_TAP:
- if (slider_data->position > SLIDER_POSITION_TWO_THIRD &&
- slider_data->position <= MAX_SLIDER_POS) {
- data->active_bottom = true;
+ case EVENT_CONTINUOUS_TAP:
+ if (event->data.position > SLIDER_POSITION_TWO_THIRD &&
+ event->data.position <= MAX_SLIDER_POS) {
+ if (event->data.source == top_slider) {
+ data->active_top = true;
+ } else {
+ data->active_bottom = true;
+ }
}
break;
- case EVENT_BOTTOM_SHORT_TAP:
- data->active_bottom = false;
+ case EVENT_SHORT_TAP:
+ if (event->data.source == top_slider) {
+ data->active_top = false;
+ } else {
+ data->active_bottom = false;
+ }
break;
default:
break;
diff --git a/src/ui/components/icon_button.c b/src/ui/components/icon_button.c
index f08a65c..9ea9fec 100644
--- a/src/ui/components/icon_button.c
+++ b/src/ui/components/icon_button.c
@@ -106,16 +106,9 @@ static void _on_event(const event_t* event, component_t* component)
// Return if the slider event is on the wrong slider
switch (event->id) {
- case EVENT_TOP_SHORT_TAP:
- case EVENT_TOP_CONTINUOUS_TAP:
- if (data->location != top_slider) {
- data->active = false;
- return;
- }
- break;
- case EVENT_BOTTOM_SHORT_TAP:
- case EVENT_BOTTOM_CONTINUOUS_TAP:
- if (data->location != bottom_slider) {
+ case EVENT_SHORT_TAP:
+ case EVENT_CONTINUOUS_TAP:
+ if (event->data.source != data->location) {
data->active = false;
return;
}
@@ -127,17 +120,16 @@ static void _on_event(const event_t* event, component_t* component)
// Return if the slider position is away from the button
// Only slider events reach here, so ok to typescast event->data
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
switch (data->type) {
case ICON_BUTTON_CHECK:
case ICON_BUTTON_NEXT:
- if (slider_data->position < SLIDER_POSITION_TWO_THIRD) {
+ if (event->data.position < SLIDER_POSITION_TWO_THIRD) {
data->active = false;
return;
}
break;
case ICON_BUTTON_CROSS:
- if (slider_data->position >= SLIDER_POSITION_ONE_THIRD) {
+ if (event->data.position >= SLIDER_POSITION_ONE_THIRD) {
data->active = false;
return;
}
@@ -151,8 +143,7 @@ static void _on_event(const event_t* event, component_t* component)
// Call the callback on short tap
switch (event->id) {
- case EVENT_TOP_SHORT_TAP:
- case EVENT_BOTTOM_SHORT_TAP:
+ case EVENT_SHORT_TAP:
if (data->callback) {
data->callback(data->user_data);
}
diff --git a/src/ui/components/keyboard_switch.c b/src/ui/components/keyboard_switch.c
index 4c6b56d..99a75f4 100644
--- a/src/ui/components/keyboard_switch.c
+++ b/src/ui/components/keyboard_switch.c
@@ -90,18 +90,19 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
keyboard_switch_data_t* ks_data = (keyboard_switch_data_t*)component->data;
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
switch (event->id) {
- case EVENT_TOP_CONTINUOUS_TAP:
- if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD &&
- slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
+ case EVENT_CONTINUOUS_TAP:
+ if (event->data.source == top_slider && ks_data->location == top_slider &&
+ event->data.position > SLIDER_POSITION_ONE_THIRD &&
+ event->data.position <= SLIDER_POSITION_TWO_THIRD) {
ks_data->active = true;
break;
}
/* FALLTHROUGH */
- case EVENT_TOP_SHORT_TAP:
- if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD &&
- slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
+ case EVENT_SHORT_TAP:
+ if (event->data.source == top_slider && ks_data->location == top_slider &&
+ event->data.position > SLIDER_POSITION_ONE_THIRD &&
+ event->data.position <= SLIDER_POSITION_TWO_THIRD) {
ks_data->active = false;
switch (ks_data->mode) {
case LOWER_CASE:
diff --git a/src/ui/components/label.c b/src/ui/components/label.c
index 0af9f8e..ae0bb23 100644
--- a/src/ui/components/label.c
+++ b/src/ui/components/label.c
@@ -138,36 +138,35 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
data_t* data = (data_t*)component->data;
+ if (event->data.source != bottom_slider) {
+ return;
+ }
if (data->scrollable) {
switch (event->id) {
- case EVENT_BOTTOM_SLIDE: {
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
+ case EVENT_SLIDE: {
// Variable scroll speed
int16_t margin = SCREEN_WIDTH / 5;
- data->slider_position_diff += SIGMOID(slider_data->velocity);
+ data->slider_position_diff += SIGMOID(event->data.velocity);
data->text_position = data->text_position_last + (int16_t)data->slider_position_diff;
data->text_position = MIN(
component->dimension.width / 2 + margin,
MAX(-margin - component->dimension.width / 2 + SCREEN_WIDTH, data->text_position));
- data->slider_position = slider_data->position;
+ data->slider_position = event->data.position;
data->slider_is_touched = true;
data->slider_was_touched = true;
break;
}
-
- case EVENT_BOTTOM_SLIDE_RELEASED:
+ case EVENT_SLIDE_RELEASED:
data->text_position_last = data->text_position;
data->slider_position_diff = 0;
data->slider_is_touched = false;
break;
- case EVENT_BOTTOM_CONTINUOUS_TAP: {
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
- data->slider_position = slider_data->position;
+ case EVENT_CONTINUOUS_TAP:
+ data->slider_position = event->data.position;
data->slider_is_touched = true;
data->slider_was_touched = true;
break;
- }
default:
break;
}
diff --git a/src/ui/components/left_arrow.c b/src/ui/components/left_arrow.c
index e9c9053..80b670e 100644
--- a/src/ui/components/left_arrow.c
+++ b/src/ui/components/left_arrow.c
@@ -60,17 +60,12 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
left_arrow_data_t* data = (left_arrow_data_t*)component->data;
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
+ if (event->data.source != data->location) {
+ return;
+ }
switch (event->id) {
- case EVENT_TOP_SHORT_TAP:
- case EVENT_BOTTOM_SHORT_TAP:
- if (data->location == top_slider && event->id == EVENT_BOTTOM_SHORT_TAP) {
- break;
- }
- if (data->location == bottom_slider && event->id == EVENT_TOP_SHORT_TAP) {
- break;
- }
- if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) {
+ case EVENT_SHORT_TAP:
+ if (event->data.position <= SLIDER_POSITION_ONE_THIRD) {
data->active = false;
if (data->callback) {
data->callback(data->user_data);
@@ -78,15 +73,8 @@ static void _on_event(const event_t* event, component_t* component)
break;
}
/* FALLTHROUGH */
- case EVENT_TOP_CONTINUOUS_TAP:
- case EVENT_BOTTOM_CONTINUOUS_TAP:
- if (data->location == top_slider && event->id == EVENT_BOTTOM_CONTINUOUS_TAP) {
- break;
- }
- if (data->location == bottom_slider && event->id == EVENT_TOP_CONTINUOUS_TAP) {
- break;
- }
- if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) {
+ case EVENT_CONTINUOUS_TAP:
+ if (event->data.position <= SLIDER_POSITION_ONE_THIRD) {
data->active = true;
break;
}
diff --git a/src/ui/components/right_arrow.c b/src/ui/components/right_arrow.c
index 15b637f..72ddaf6 100644
--- a/src/ui/components/right_arrow.c
+++ b/src/ui/components/right_arrow.c
@@ -60,18 +60,13 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
right_arrow_data_t* data = (right_arrow_data_t*)component->data;
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
+ if (event->data.source != data->location) {
+ return;
+ }
switch (event->id) {
- case EVENT_TOP_SHORT_TAP:
- case EVENT_BOTTOM_SHORT_TAP:
- if (data->location == top_slider && event->id == EVENT_BOTTOM_SHORT_TAP) {
- break;
- }
- if (data->location == bottom_slider && event->id == EVENT_TOP_SHORT_TAP) {
- break;
- }
- if (slider_data->position > SLIDER_POSITION_TWO_THIRD &&
- slider_data->position <= MAX_SLIDER_POS) {
+ case EVENT_SHORT_TAP:
+ if (event->data.position > SLIDER_POSITION_TWO_THIRD &&
+ event->data.position <= MAX_SLIDER_POS) {
data->active = false;
if (data->callback) {
data->callback(data->user_data);
@@ -79,17 +74,9 @@ static void _on_event(const event_t* event, component_t* component)
break;
}
/* FALLTHROUGH */
- case EVENT_TOP_CONTINUOUS_TAP:
- case EVENT_BOTTOM_CONTINUOUS_TAP:
- if (data->location == top_slider && event->id == EVENT_BOTTOM_CONTINUOUS_TAP) {
- break;
- }
- if (data->location == bottom_slider && event->id == EVENT_TOP_CONTINUOUS_TAP) {
- break;
- }
-
- if (slider_data->position > SLIDER_POSITION_TWO_THIRD &&
- slider_data->position <= MAX_SLIDER_POS) {
+ case EVENT_CONTINUOUS_TAP:
+ if (event->data.position > SLIDER_POSITION_TWO_THIRD &&
+ event->data.position <= MAX_SLIDER_POS) {
data->active = true;
break;
}
diff --git a/src/ui/components/trinary_input_char.c b/src/ui/components/trinary_input_char.c
index 305ffbe..e20dd64 100644
--- a/src/ui/components/trinary_input_char.c
+++ b/src/ui/components/trinary_input_char.c
@@ -95,15 +95,14 @@ static void _alphabet_selected(component_t* component, const char* alphabet)
static void _on_event(const event_t* event, component_t* component)
{
- if (event->id != EVENT_BOTTOM_SHORT_TAP) {
+ if (!(event->id == EVENT_SHORT_TAP && event->data.source == bottom_slider)) {
return;
}
data_t* data = (data_t*)component->data;
- const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
const char* selected_alphabet;
- if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) {
+ if (event->data.position <= SLIDER_POSITION_ONE_THIRD) {
selected_alphabet = data->left_alphabet;
- } else if (slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
+ } else if (event->data.position <= SLIDER_POSITION_TWO_THIRD) {
selected_alphabet = data->middle_alphabet;
} else {
selected_alphabet = data->right_alphabet;
diff --git a/src/ui/event.h b/src/ui/event.h
index 1ea6f94..cb1e3fd 100644
--- a/src/ui/event.h
+++ b/src/ui/event.h
@@ -15,24 +15,27 @@
#ifndef _EVENT_H_
#define _EVENT_H_
+#include <screen.h>
#include <stdint.h>
+typedef struct {
+ slider_location_t source;
+ int16_t diff;
+ uint16_t position;
+ int32_t velocity;
+} event_slider_data_t;
+
enum {
- EVENT_BOTTOM_SLIDE,
- EVENT_TOP_SLIDE,
- EVENT_BOTTOM_SLIDE_RELEASED,
- EVENT_TOP_SLIDE_RELEASED,
- EVENT_TOP_CONTINUOUS_TAP,
- EVENT_BOTTOM_CONTINUOUS_TAP,
- EVENT_TOP_LONG_TAP,
- EVENT_BOTTOM_LONG_TAP,
- EVENT_TOP_SHORT_TAP,
- EVENT_BOTTOM_SHORT_TAP,
+ EVENT_SLIDE,
+ EVENT_SLIDE_RELEASED,
+ EVENT_CONTINUOUS_TAP,
+ EVENT_LONG_TAP,
+ EVENT_SHORT_TAP,
};
typedef struct {
- const void* data;
uint8_t id;
+ event_slider_data_t data;
} event_t;
#endif
diff --git a/src/ui/event_handler.c b/src/ui/event_handler.c
index 1200463..25039c2 100644
--- a/src/ui/event_handler.c
+++ b/src/ui/event_handler.c
@@ -47,16 +47,12 @@ void emit_event(const event_t* event)
//
// If the screensaver is not active, we reset the timer with any touch interaction.
switch (event->id) {
- case EVENT_TOP_SHORT_TAP:
- case EVENT_BOTTOM_SHORT_TAP:
- case EVENT_BOTTOM_SLIDE_RELEASED:
- case EVENT_TOP_SLIDE_RELEASED:
+ case EVENT_SHORT_TAP:
+ case EVENT_SLIDE_RELEASED:
screen_saver_reset();
break;
- case EVENT_TOP_SLIDE:
- case EVENT_BOTTOM_SLIDE:
- case EVENT_TOP_CONTINUOUS_TAP:
- case EVENT_BOTTOM_CONTINUOUS_TAP:
+ case EVENT_SLIDE:
+ case EVENT_CONTINUOUS_TAP:
if (screen_saver_get() == NULL) {
screen_saver_reset();
}
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.