diff --git a/core/SConscript.bootloader_emu b/core/SConscript.bootloader_emu index 789f27b428..161850796f 100644 --- a/core/SConscript.bootloader_emu +++ b/core/SConscript.bootloader_emu @@ -119,7 +119,6 @@ SOURCE_BOOTLOADER = [ SOURCE_TREZORHAL = [ 'embed/trezorhal/unix/bootutils.c', - 'embed/trezorhal/unix/common.c', 'embed/trezorhal/unix/flash.c', 'embed/trezorhal/unix/flash_otp.c', 'embed/trezorhal/unix/mpu.c', diff --git a/core/SConscript.unix b/core/SConscript.unix index 5f6840e3ca..69c995d76d 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -392,7 +392,6 @@ SOURCE_MICROPYTHON = [ SOURCE_UNIX = [ 'embed/trezorhal/unix/bootutils.c', - 'embed/trezorhal/unix/common.c', 'embed/trezorhal/unix/entropy.c', 'embed/trezorhal/unix/flash_otp.c', 'embed/trezorhal/unix/flash.c', diff --git a/core/embed/bootloader/emulator.c b/core/embed/bootloader/emulator.c index f0b09f8302..b0fec6514e 100644 --- a/core/embed/bootloader/emulator.c +++ b/core/embed/bootloader/emulator.c @@ -1,6 +1,8 @@ #include #include +#include + #include TREZOR_BOARD #include "bootargs.h" #include "bootui.h" @@ -20,9 +22,6 @@ uint8_t *FIRMWARE_START = 0; -// used in fw emulator to raise python exception on exit -void __attribute__((noreturn)) main_clean_exit() { exit(3); } - int bootloader_main(void); // assuming storage is single subarea @@ -89,7 +88,31 @@ bool load_firmware(const char *filename, uint8_t *hash) { return true; } +static int sdl_event_filter(void *userdata, SDL_Event *event) { + switch (event->type) { + case SDL_QUIT: + exit(3); + return 0; + case SDL_KEYUP: + if (event->key.repeat) { + return 0; + } + switch (event->key.keysym.sym) { + case SDLK_ESCAPE: + exit(3); + return 0; + case SDLK_p: + display_save("emu"); + return 0; + } + break; + } + return 1; +} + __attribute__((noreturn)) int main(int argc, char **argv) { + SDL_SetEventFilter(sdl_event_filter, NULL); + display_init(DISPLAY_RESET_CONTENT); flash_init(); flash_otp_init(); diff --git a/core/embed/bootloader/emulator.h b/core/embed/bootloader/emulator.h index d54b07d9da..ee589acb55 100644 --- a/core/embed/bootloader/emulator.h +++ b/core/embed/bootloader/emulator.h @@ -8,7 +8,6 @@ extern uint8_t *FIRMWARE_START; -void emulator_poll_events(void); __attribute__((noreturn)) void jump_to(uint32_t address); #endif diff --git a/core/embed/bootloader/main.c b/core/embed/bootloader/main.c index 392e269344..e19b29deb1 100644 --- a/core/embed/bootloader/main.c +++ b/core/embed/bootloader/main.c @@ -79,6 +79,7 @@ #include "version_check.h" #ifdef TREZOR_EMULATOR +#include "SDL.h" #include "emulator.h" #else #include "compiler_traits.h" @@ -149,7 +150,9 @@ static usb_result_t bootloader_usb_loop(const vendor_header *const vhdr, for (;;) { #ifdef TREZOR_EMULATOR - emulator_poll_events(); + // Ensures that SDL events are processed. This prevents the emulator from + // freezing when the user interacts with the window. + SDL_PumpEvents(); #endif int r = usb_webusb_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, USB_TIMEOUT); diff --git a/core/embed/extmod/modtrezorio/modtrezorio-poll.h b/core/embed/extmod/modtrezorio/modtrezorio-poll.h index a5f1bc89c2..c07904e560 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio-poll.h +++ b/core/embed/extmod/modtrezorio/modtrezorio-poll.h @@ -24,6 +24,10 @@ #include "display.h" #include "embed/extmod/trezorobj.h" +#ifdef TREZOR_EMULATOR +#include "SDL.h" +#endif + #define USB_DATA_IFACE (253) #define BUTTON_IFACE (254) #define TOUCH_IFACE (255) @@ -78,7 +82,10 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, const mp_uint_t mode = i & 0xFF00; #if defined TREZOR_EMULATOR - emulator_poll_events(); + // Ensures that SDL events are processed even if the ifaces list + // contains only USB interfaces. This prevents the emulator from + // freezing when the user interacts with the window. + SDL_PumpEvents(); #endif if (false) { diff --git a/core/embed/trezorhal/unix/button.c b/core/embed/trezorhal/unix/button.c index 9ab6731e0a..01a096ff26 100644 --- a/core/embed/trezorhal/unix/button.c +++ b/core/embed/trezorhal/unix/button.c @@ -32,7 +32,6 @@ char button_state_right(void) { return last_right; } uint32_t button_read(void) { SDL_Event event; - SDL_PumpEvents(); if (SDL_PollEvent(&event) > 0) { switch (event.type) { case SDL_KEYDOWN: diff --git a/core/embed/trezorhal/unix/common.c b/core/embed/trezorhal/unix/common.c deleted file mode 100644 index de06ef6dee..0000000000 --- a/core/embed/trezorhal/unix/common.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "display.h" -#include "memzero.h" - -void __attribute__((noreturn)) main_clean_exit(); - -static int SDLCALL emulator_event_filter(void *userdata, SDL_Event *event) { - switch (event->type) { - case SDL_QUIT: - main_clean_exit(); - return 0; - case SDL_KEYUP: - if (event->key.repeat) { - return 0; - } - switch (event->key.keysym.sym) { - case SDLK_ESCAPE: - main_clean_exit(); - return 0; - case SDLK_p: - display_save("emu"); - return 0; - } - break; - } - return 1; -} - -void emulator_poll_events(void) { - SDL_PumpEvents(); - SDL_FilterEvents(emulator_event_filter, NULL); -} diff --git a/core/embed/trezorhal/unix/platform.h b/core/embed/trezorhal/unix/platform.h deleted file mode 100644 index 0cab1411c1..0000000000 --- a/core/embed/trezorhal/unix/platform.h +++ /dev/null @@ -1,2 +0,0 @@ - -void emulator_poll_events(void); diff --git a/core/embed/trezorhal/unix/touch.c b/core/embed/trezorhal/unix/touch.c index 6c4f34f5c3..5b8e24c8d9 100644 --- a/core/embed/trezorhal/unix/touch.c +++ b/core/embed/trezorhal/unix/touch.c @@ -251,9 +251,7 @@ uint32_t touch_get_event(void) { return TOUCH_END | touch_pack_xy(driver->last_x, driver->last_y); } - emulator_poll_events(); SDL_Event event; - SDL_PumpEvents(); int ev_x = 0; int ev_y = 0; diff --git a/core/embed/unix/main.c b/core/embed/unix/main.c index aa8b285b0c..b079632097 100644 --- a/core/embed/unix/main.c +++ b/core/embed/unix/main.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include + #include #include #include @@ -414,7 +416,7 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) { // Inject SystemExit exception. This is primarily needed by prof.py to run the // atexit() handler. -void __attribute__((noreturn)) main_clean_exit() { +static void __attribute__((noreturn)) main_clean_exit() { const int status = 3; fflush(stdout); fflush(stderr); @@ -471,6 +473,28 @@ reimport: return 0; } +static int sdl_event_filter(void *userdata, SDL_Event *event) { + switch (event->type) { + case SDL_QUIT: + main_clean_exit(); + return 0; + case SDL_KEYUP: + if (event->key.repeat) { + return 0; + } + switch (event->key.keysym.sym) { + case SDLK_ESCAPE: + main_clean_exit(); + return 0; + case SDLK_p: + display_save("emu"); + return 0; + } + break; + } + return 1; +} + MP_NOINLINE int main_(int argc, char **argv) { #ifdef SIGPIPE // Do not raise SIGPIPE, instead return EPIPE. Otherwise, e.g. writing @@ -492,6 +516,8 @@ MP_NOINLINE int main_(int argc, char **argv) { system_init(&rsod_panic_handler); + SDL_SetEventFilter(sdl_event_filter, NULL); + display_init(DISPLAY_RESET_CONTENT); #if USE_TOUCH