Skip to content

Commit

Permalink
fix(core/emulator): make sure SDL keyboard and quit events are always…
Browse files Browse the repository at this point in the history
… processed

fixes #973
  • Loading branch information
matejcik committed May 5, 2022
1 parent 692ea14 commit 7ac3958
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions core/.changelog.d/973.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_(Emulator)_ Emulator window will always react to shutdown events, even while waiting for USB packets.
26 changes: 26 additions & 0 deletions core/embed/unix/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down Expand Up @@ -110,6 +111,31 @@ error_shutdown(const char *line1, const char *line2, const char *line3,

void hal_delay(uint32_t ms) { usleep(1000 * ms); }

static void handle_key_event(const SDL_Event *event) {
if (event->key.repeat) {
return;
}
switch (event->key.keysym.sym) {
case SDLK_ESCAPE:
__shutdown();
return;
case SDLK_p:
display_save("emu");
return;
}
}

void emulator_poll_events(void) {
SDL_Event event;
SDL_PumpEvents();
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYUP, SDL_KEYUP) > 0) {
handle_key_event(&event);
}
if (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_QUIT, SDL_QUIT) > 0) {
__shutdown();
}
}

uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];

void collect_hw_entropy(void) { memzero(HW_ENTROPY_DATA, HW_ENTROPY_LEN); }
1 change: 1 addition & 0 deletions core/embed/unix/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))

void hal_delay(uint32_t ms);
void emulator_poll_events(void);

void collect_hw_entropy(void);
#define HW_ENTROPY_LEN (12 + 32)
Expand Down
2 changes: 2 additions & 0 deletions core/embed/unix/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ typedef unsigned int mp_uint_t; // must be pointer size
do { \
extern void mp_handle_pending(bool); \
mp_handle_pending(true); \
extern void emulator_poll_events(void); \
emulator_poll_events(); \
mp_hal_delay_us(500); \
} while (0);

Expand Down
25 changes: 0 additions & 25 deletions core/embed/unix/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@
extern void __shutdown(void);
extern const char *display_save(const char *prefix);

static bool handle_emulator_events(const SDL_Event *event) {
switch (event->type) {
case SDL_KEYUP:
if (event->key.repeat) {
break;
}
switch (event->key.keysym.sym) {
case SDLK_ESCAPE:
__shutdown();
return true;
case SDLK_p:
display_save("emu");
return true;
}
break;
case SDL_QUIT:
__shutdown();
return true;
}
return false;
}

#if defined TREZOR_MODEL_T

#include "touch.h"
Expand All @@ -57,9 +35,6 @@ uint32_t touch_read(void) {
SDL_Event event;
SDL_PumpEvents();
if (SDL_PollEvent(&event) > 0) {
if (handle_emulator_events(&event)) {
return 0;
}
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:
Expand Down

0 comments on commit 7ac3958

Please sign in to comment.