From b178c10e8b8f9b471a3e719089c68f77868ce898 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Wed, 22 May 2024 22:19:14 +0200 Subject: [PATCH] fix(core): prevent overloading app with touch events [no changelog] --- .../extmod/modtrezorio/modtrezorio-poll.h | 67 +++++++++++-------- core/embed/extmod/modtrezorio/modtrezorio.c | 2 + core/embed/trezorhal/stm32f4/touch/ft6x36.c | 7 +- core/embed/trezorhal/unix/touch/touch.c | 1 - 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/core/embed/extmod/modtrezorio/modtrezorio-poll.h b/core/embed/extmod/modtrezorio/modtrezorio-poll.h index 7f1feb54ec..77ffaa547a 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio-poll.h +++ b/core/embed/extmod/modtrezorio/modtrezorio-poll.h @@ -31,6 +31,7 @@ #define POLL_WRITE (0x0100) extern bool usb_connected_previously; +extern uint32_t last_touch_sample_time; /// package: trezorio.__init__ @@ -84,38 +85,46 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, } #if defined USE_TOUCH else if (iface == TOUCH_IFACE) { + const uint32_t evt = touch_read(); + if (evt) { - mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); - const uint32_t etype = (evt >> 24) & 0xFFU; // event type - const uint32_t ex = (evt >> 12) & 0xFFFU; // x position - const uint32_t ey = evt & 0xFFFU; // y position - uint32_t exr; // rotated x position - uint32_t eyr; // rotated y position - switch (display_orientation(-1)) { - case 90: - exr = ey; - eyr = DISPLAY_RESX - ex; - break; - case 180: - exr = DISPLAY_RESX - ex; - eyr = DISPLAY_RESY - ey; - break; - case 270: - exr = DISPLAY_RESY - ey; - eyr = ex; - break; - default: - exr = ex; - eyr = ey; - break; + // ignore TOUCH_MOVE events if they are too frequent + if ((evt & TOUCH_MOVE) == 0 || + (hal_ticks_ms() - last_touch_sample_time > 10)) { + last_touch_sample_time = hal_ticks_ms(); + + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); + const uint32_t etype = (evt >> 24) & 0xFFU; // event type + const uint32_t ex = (evt >> 12) & 0xFFFU; // x position + const uint32_t ey = evt & 0xFFFU; // y position + uint32_t exr; // rotated x position + uint32_t eyr; // rotated y position + switch (display_orientation(-1)) { + case 90: + exr = ey; + eyr = DISPLAY_RESX - ex; + break; + case 180: + exr = DISPLAY_RESX - ex; + eyr = DISPLAY_RESY - ey; + break; + case 270: + exr = DISPLAY_RESY - ey; + eyr = ex; + break; + default: + exr = ex; + eyr = ey; + break; + } + tuple->items[0] = MP_OBJ_NEW_SMALL_INT(etype); + tuple->items[1] = MP_OBJ_NEW_SMALL_INT(exr); + tuple->items[2] = MP_OBJ_NEW_SMALL_INT(eyr); + ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); + ret->items[1] = MP_OBJ_FROM_PTR(tuple); + return mp_const_true; } - tuple->items[0] = MP_OBJ_NEW_SMALL_INT(etype); - tuple->items[1] = MP_OBJ_NEW_SMALL_INT(exr); - tuple->items[2] = MP_OBJ_NEW_SMALL_INT(eyr); - ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); - ret->items[1] = MP_OBJ_FROM_PTR(tuple); - return mp_const_true; } } else if (iface == USB_DATA_IFACE) { bool usb_connected = usb_configured() == sectrue ? true : false; diff --git a/core/embed/extmod/modtrezorio/modtrezorio.c b/core/embed/extmod/modtrezorio/modtrezorio.c index b740a12dc9..a40ae96263 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio.c +++ b/core/embed/extmod/modtrezorio/modtrezorio.c @@ -35,6 +35,8 @@ // Whether USB data pins were connected on last check (USB configured) bool usb_connected_previously = true; +uint32_t last_touch_sample_time = 0; + #define CHECK_PARAM_RANGE(value, minimum, maximum) \ if (value < minimum || value > maximum) { \ mp_raise_ValueError(#value " is out of range"); \ diff --git a/core/embed/trezorhal/stm32f4/touch/ft6x36.c b/core/embed/trezorhal/stm32f4/touch/ft6x36.c index f2a7d96bce..eb2b4cf30a 100644 --- a/core/embed/trezorhal/stm32f4/touch/ft6x36.c +++ b/core/embed/trezorhal/stm32f4/touch/ft6x36.c @@ -266,7 +266,12 @@ uint32_t touch_read(void) { touching = 1; return TOUCH_START | xy; } else if ((number_of_touch_points == 1) && (event_flag == EVENT_CONTACT)) { - return TOUCH_MOVE | xy; + if (touching) { + return TOUCH_MOVE | xy; + } else { + touching = 1; + return TOUCH_START | xy; + } } else if ((number_of_touch_points == 0) && (event_flag == EVENT_LIFT_UP)) { touching = 0; return TOUCH_END | xy; diff --git a/core/embed/trezorhal/unix/touch/touch.c b/core/embed/trezorhal/unix/touch/touch.c index f0fc83072b..b90b418d91 100644 --- a/core/embed/trezorhal/unix/touch/touch.c +++ b/core/embed/trezorhal/unix/touch/touch.c @@ -211,7 +211,6 @@ uint32_t touch_read(void) { if (ev_type != 0) { _touch_x = ev_x; _touch_y = ev_y; - break; } } return ev_type | touch_pack_xy(ev_x, ev_y);