Skip to content

Commit

Permalink
fixup! feat(core/emu): handle arrow buttons as swipes
Browse files Browse the repository at this point in the history
  • Loading branch information
obrusvit committed Mar 21, 2024
1 parent f248d62 commit bedeb8a
Showing 1 changed file with 67 additions and 60 deletions.
127 changes: 67 additions & 60 deletions core/embed/trezorhal/unix/touch/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ static const int _btn_swipe_length = 60;
// finished.
typedef enum {
IDLE,
MOUSE_ACTIVE,
MOUSE_OUTSIDE,
BUTTON_SWIPE_INITIATED,
MOUSE_DOWN_INSIDE,
MOUSE_DOWN_OUTSIDE,
BUTTON_SWIPE_LEFT_INITIATED,
BUTTON_SWIPE_RIGHT_INITIATED,
BUTTON_SWIPE_UP_INITIATED,
BUTTON_SWIPE_DOWN_INITIATED,
BUTTON_SWIPE_COMPLETED
} touch_input_state_t;

Expand All @@ -60,6 +63,13 @@ static bool is_inside_display(int x, int y) {
y - sdl_touch_offset_y < sdl_display_res_y;
}

static bool is_button_swipe_initiated() {
return input_state == BUTTON_SWIPE_LEFT_INITIATED ||
input_state == BUTTON_SWIPE_RIGHT_INITIATED ||
input_state == BUTTON_SWIPE_UP_INITIATED ||
input_state == BUTTON_SWIPE_DOWN_INITIATED;
}

static void handle_mouse_events(SDL_Event event, int* ev_type, int* ev_x,
int* ev_y) {
bool inside_display = is_inside_display(event.button.x, event.button.y);
Expand All @@ -69,7 +79,7 @@ static void handle_mouse_events(SDL_Event event, int* ev_type, int* ev_x,
*ev_x = event.button.x - sdl_touch_offset_x;
*ev_y = event.button.y - sdl_touch_offset_y;
*ev_type = TOUCH_START;
input_state = MOUSE_ACTIVE;
input_state = MOUSE_DOWN_INSIDE;
}
break;

Expand All @@ -88,16 +98,17 @@ static void handle_mouse_events(SDL_Event event, int* ev_type, int* ev_x,
*ev_x = event.motion.x - sdl_touch_offset_x;
*ev_y = event.motion.y - sdl_touch_offset_y;
// simulate TOUCH_START if pressed in mouse returned on visible area
*ev_type = (input_state == MOUSE_OUTSIDE) ? TOUCH_START : TOUCH_MOVE;
input_state = MOUSE_ACTIVE;
*ev_type =
(input_state == MOUSE_DOWN_OUTSIDE) ? TOUCH_START : TOUCH_MOVE;
input_state = MOUSE_DOWN_INSIDE;
} else {
if (input_state == MOUSE_ACTIVE) {
if (input_state == MOUSE_DOWN_INSIDE) {
// use last valid coordinates and simulate TOUCH_END
*ev_x = _touch_x;
*ev_y = _touch_y;
*ev_type = TOUCH_END;
}
input_state = MOUSE_OUTSIDE;
input_state = MOUSE_DOWN_OUTSIDE;
}
}
break;
Expand All @@ -108,72 +119,68 @@ static void handle_button_events(SDL_Event event, int* ev_type, int* ev_x,
int* ev_y) {
// Handle arrow buttons to trigger a scroll movement by set length in the
// direction of the button
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.repeat == 1) {
// do not react when a button is held but the first event wasn't
// registered, e.g. when mouse movement was in progress in the moment of
// pressing the button
if (event.type == SDL_KEYDOWN && !event.key.repeat && !is_button_swipe_initiated()) {
switch (event.key.keysym.sym) {
case SDLK_LEFT:
*ev_x = _btn_swipe_begin;
*ev_y = sdl_display_res_y / 2;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_LEFT_INITIATED;
break;
}
switch (event.key.keysym.sym) {
case SDLK_LEFT:
*ev_x = _btn_swipe_begin;
*ev_y = sdl_display_res_y / 2;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_INITIATED;
break;
case SDLK_RIGHT:
*ev_x = sdl_display_res_x - _btn_swipe_begin;
*ev_y = sdl_display_res_y / 2;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_INITIATED;
break;
case SDLK_UP:
*ev_x = sdl_display_res_x / 2;
*ev_y = _btn_swipe_begin;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_INITIATED;
break;
case SDLK_DOWN:
*ev_x = sdl_display_res_x / 2;
*ev_y = sdl_display_res_y - _btn_swipe_begin;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_INITIATED;
break;
}
break;
case SDL_KEYUP:
if (input_state != BUTTON_SWIPE_INITIATED) {
case SDLK_RIGHT:
*ev_x = sdl_display_res_x - _btn_swipe_begin;
*ev_y = sdl_display_res_y / 2;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_RIGHT_INITIATED;
break;
}
switch (event.key.keysym.sym) {
case SDLK_LEFT:
case SDLK_UP:
*ev_x = sdl_display_res_x / 2;
*ev_y = _btn_swipe_begin;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_UP_INITIATED;
break;
case SDLK_DOWN:
*ev_x = sdl_display_res_x / 2;
*ev_y = sdl_display_res_y - _btn_swipe_begin;
*ev_type = TOUCH_START;
input_state = BUTTON_SWIPE_DOWN_INITIATED;
break;
}
} else if (event.type == SDL_KEYUP && input_state != IDLE) {
switch (event.key.keysym.sym) {
case SDLK_LEFT:
if (input_state == BUTTON_SWIPE_LEFT_INITIATED) {
*ev_x = _btn_swipe_begin + _btn_swipe_length;
*ev_y = sdl_display_res_y / 2;
*ev_type = TOUCH_MOVE;
input_state = BUTTON_SWIPE_COMPLETED;
break;
case SDLK_RIGHT:
}
break;
case SDLK_RIGHT:
if (input_state == BUTTON_SWIPE_RIGHT_INITIATED) {
*ev_x = sdl_display_res_x - _btn_swipe_begin - _btn_swipe_length;
*ev_y = sdl_display_res_y / 2;
*ev_type = TOUCH_MOVE;
input_state = BUTTON_SWIPE_COMPLETED;
break;
case SDLK_UP:
}
break;
case SDLK_UP:
if (input_state == BUTTON_SWIPE_UP_INITIATED) {
*ev_x = sdl_display_res_x / 2;
*ev_y = _btn_swipe_begin + _btn_swipe_length;
*ev_type = TOUCH_MOVE;
input_state = BUTTON_SWIPE_COMPLETED;
break;
case SDLK_DOWN:
}
break;
case SDLK_DOWN:
if (input_state == BUTTON_SWIPE_DOWN_INITIATED) {
*ev_x = sdl_display_res_x / 2;
*ev_y = sdl_display_res_y - _btn_swipe_begin - _btn_swipe_length;
*ev_type = TOUCH_MOVE;
input_state = BUTTON_SWIPE_COMPLETED;
break;
}
break;
}
break;
}
}
}

Expand All @@ -192,11 +199,11 @@ uint32_t touch_read(void) {
int ev_type = 0;

while (SDL_PollEvent(&event) > 0) {
if (input_state == IDLE || input_state == MOUSE_ACTIVE ||
input_state == MOUSE_OUTSIDE) {
if (input_state == IDLE || input_state == MOUSE_DOWN_INSIDE ||
input_state == MOUSE_DOWN_OUTSIDE) {
handle_mouse_events(event, &ev_type, &ev_x, &ev_y);
}
if (input_state == IDLE || input_state == BUTTON_SWIPE_INITIATED) {
if (input_state == IDLE || is_button_swipe_initiated()) {
handle_button_events(event, &ev_type, &ev_x, &ev_y);
}

Expand All @@ -214,7 +221,7 @@ void touch_power_on(void) {}
void touch_wait_until_ready(void) {}

uint32_t touch_is_detected(void) {
return input_state == MOUSE_ACTIVE || input_state == BUTTON_SWIPE_INITIATED;
return input_state == MOUSE_DOWN_INSIDE || is_button_swipe_initiated();
}

#endif
Expand Down

0 comments on commit bedeb8a

Please sign in to comment.