1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

refactor(emulator): improve SDL event polling logic

[no changelog]
This commit is contained in:
cepetr 2024-10-03 13:23:56 +02:00 committed by cepetr
parent d062a54929
commit 12a9dca5e1
11 changed files with 65 additions and 72 deletions

View File

@ -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',

View File

@ -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',

View File

@ -1,6 +1,8 @@
#include <stdio.h>
#include <unistd.h>
#include <SDL.h>
#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();

View File

@ -8,7 +8,6 @@
extern uint8_t *FIRMWARE_START;
void emulator_poll_events(void);
__attribute__((noreturn)) void jump_to(uint32_t address);
#endif

View File

@ -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);

View File

@ -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) {

View File

@ -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:

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <SDL.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#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);
}

View File

@ -1,2 +0,0 @@
void emulator_poll_events(void);

View File

@ -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;

View File

@ -24,6 +24,8 @@
* THE SOFTWARE.
*/
#include <SDL.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
@ -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