1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 22:08:08 +00:00

fix(core): fix mouse event processing in the simulator

[no changelog]
This commit is contained in:
cepetr 2024-01-29 13:58:11 +01:00 committed by cepetr
parent 6e1cb9ff8c
commit ecd4465ab3

View File

@ -32,54 +32,88 @@ extern int sdl_display_res_x, sdl_display_res_y;
extern int sdl_touch_offset_x, sdl_touch_offset_y; extern int sdl_touch_offset_x, sdl_touch_offset_y;
static bool _touch_detected = false; static bool _touch_detected = false;
static int _touch_x = 0;
static int _touch_y = 0;
static bool is_inside_display(int x, int y) {
return (x >= sdl_touch_offset_x && y >= sdl_touch_offset_y &&
x - sdl_touch_offset_x < sdl_display_res_x &&
y - sdl_touch_offset_y < sdl_display_res_y);
}
uint32_t touch_read(void) { uint32_t touch_read(void) {
emulator_poll_events(); emulator_poll_events();
SDL_Event event; SDL_Event event;
SDL_PumpEvents(); SDL_PumpEvents();
if (SDL_PollEvent(&event) > 0) {
int ev_x = 0;
int ev_y = 0;
int ev_type = 0;
while (SDL_PollEvent(&event) > 0) {
switch (event.type) { switch (event.type) {
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION: if (is_inside_display(event.button.x, event.button.y)) {
case SDL_MOUSEBUTTONUP: { ev_x = event.button.x - sdl_touch_offset_x;
const int x = event.button.x - sdl_touch_offset_x; ev_y = event.button.y - sdl_touch_offset_y;
const int y = event.button.y - sdl_touch_offset_y; ev_type = TOUCH_START;
if (x < 0 || y < 0 || x >= sdl_display_res_x || }
y >= sdl_display_res_y) {
if (event.motion.state) { break;
const int clamp_x =
(x < 0) case SDL_MOUSEBUTTONUP:
? 0 if (_touch_detected) {
: ((x >= sdl_display_res_x) ? sdl_display_res_x - 1 : x); if (is_inside_display(event.button.x, event.button.y)) {
const int clamp_y = ev_x = event.button.x - sdl_touch_offset_x;
(y < 0) ev_y = event.button.y - sdl_touch_offset_y;
? 0
: ((y >= sdl_display_res_y) ? sdl_display_res_y - 1 : y);
return TOUCH_END | touch_pack_xy(clamp_x, clamp_y);
} else { } else {
break; // use last valid coordinates
ev_x = _touch_x;
ev_y = _touch_y;
}
ev_type = TOUCH_END;
}
break;
case SDL_MOUSEMOTION:
if (is_inside_display(event.motion.x, event.motion.y)) {
ev_x = event.motion.x - sdl_touch_offset_x;
ev_y = event.motion.y - sdl_touch_offset_y;
if (_touch_detected) {
ev_type = TOUCH_MOVE;
} else if (event.motion.state != 0) { // button pressed ?
// simulate TOUCH_START
ev_type = TOUCH_START;
}
} else {
if (_touch_detected) {
// use last valid coordinates and simulate TOUCH_END
ev_x = _touch_x;
ev_y = _touch_y;
ev_type = TOUCH_END;
} }
} }
switch (event.type) { break;
case SDL_MOUSEBUTTONDOWN: }
_touch_detected = true;
return TOUCH_START | touch_pack_xy(x, y); if (ev_type != 0) {
case SDL_MOUSEMOTION: _touch_x = ev_x;
// remove other SDL_MOUSEMOTION events from queue _touch_y = ev_y;
SDL_FlushEvent(SDL_MOUSEMOTION);
if (event.motion.state) { if (ev_type == TOUCH_START) {
return TOUCH_MOVE | touch_pack_xy(x, y); _touch_detected = true;
} break;
break; }
case SDL_MOUSEBUTTONUP:
_touch_detected = false; if (ev_type == TOUCH_END) {
return TOUCH_END | touch_pack_xy(x, y); _touch_detected = false;
}
break; break;
} }
} }
} }
return 0;
return ev_type | touch_pack_xy(ev_x, ev_y);
} }
void touch_init(void) {} void touch_init(void) {}