From ff96bf0c494927b3dbe3ecbff24a12ed5c664604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Olszewski?= Date: Sun, 28 Jan 2024 12:27:32 +0100 Subject: [PATCH] Fixing emulator bug #391 --- src/core/SDLaccess.h | 8 ++++++++ src/core/input_device.c | 11 ++++++++++- src/core/main.c | 19 ++++++++++++++++--- src/ui/ui_porting.c | 9 ++++++++- 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/core/SDLaccess.h diff --git a/src/core/SDLaccess.h b/src/core/SDLaccess.h new file mode 100644 index 00000000..58123a10 --- /dev/null +++ b/src/core/SDLaccess.h @@ -0,0 +1,8 @@ +#ifndef _SDLaccess_H +#define _SDLaccess_H + +#include + +extern SDL_mutex *global_sdl_mutex; + +#endif \ No newline at end of file diff --git a/src/core/input_device.c b/src/core/input_device.c index 182d3051..0b747f9a 100644 --- a/src/core/input_device.c +++ b/src/core/input_device.c @@ -14,6 +14,7 @@ #include #ifdef EMULATOR_BUILD +#include "SDLaccess.h" #include #endif @@ -482,7 +483,9 @@ static void *thread_input_device(void *ptr) { while (true) { SDL_Event event; - while (SDL_WaitEvent(&event)) { + SDL_LockMutex(global_sdl_mutex); + while (SDL_PollEvent(&event)) { + SDL_UnlockMutex(global_sdl_mutex); switch (event.type) { case SDL_QUIT: exit(0); @@ -539,7 +542,10 @@ static void *thread_input_device(void *ptr) { } break; } + SDL_LockMutex(global_sdl_mutex); } + SDL_UnlockMutex(global_sdl_mutex); + usleep(50000); // Sorry, this will break windows, but it's not like it is working now anyway :-( } #endif } @@ -561,5 +567,8 @@ void input_device_init() { } app_state_push(APP_STATE_MAINMENU); #endif + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("Error initializing SDL: %s\n", SDL_GetError()); + } pthread_create(&input_device_pid, NULL, thread_input_device, NULL); } diff --git a/src/core/main.c b/src/core/main.c index cf1b8d55..22018036 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -11,6 +11,12 @@ #include #include +#ifdef EMULATOR_BUILD +#include "SDLaccess.h" +#include +SDL_mutex *global_sdl_mutex; +#endif + #include "bmi270/accel_gyro.h" #include "core/app_state.h" #include "core/common.hh" @@ -23,6 +29,7 @@ #include "core/sleep_mode.h" #include "core/thread.h" #include "driver/TP2825.h" +#include "driver/beep.h" #include "driver/dm5680.h" #include "driver/esp32.h" #include "driver/fans.h" @@ -34,7 +41,6 @@ #include "driver/mcp3021.h" #include "driver/oled.h" #include "driver/rtc.h" -#include "driver/beep.h" #include "ui/page_power.h" #include "ui/page_scannow.h" #include "ui/page_source.h" @@ -141,6 +147,13 @@ void lvgl_init() { int main(int argc, char *argv[]) { pthread_mutex_init(&lvgl_mutex, NULL); +#ifdef EMULATOR_BUILD + global_sdl_mutex = SDL_CreateMutex(); + if (global_sdl_mutex == NULL) { + // Handle error: SDL_CreateMutex failed + } +#endif + // 1. Recall configuration settings_init(); settings_load(); @@ -188,10 +201,10 @@ int main(int argc, char *argv[]) { // 8. Synthetic counter for gif refresh gif_cnt = 0; - + // 8.1 set initial analog module power state Analog_Module_Power(0); - + // 10. Execute main loop g_init_done = 1; for (;;) { diff --git a/src/ui/ui_porting.c b/src/ui/ui_porting.c index 15b2336b..830eac86 100644 --- a/src/ui/ui_porting.c +++ b/src/ui/ui_porting.c @@ -10,6 +10,7 @@ #include "lvgl/lvgl.h" #ifdef EMULATOR_BUILD +#include "SDLaccess.h" #include static void *fb1, *fb2; SDL_Window *window = NULL; @@ -55,6 +56,7 @@ static void hdz_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_ fb_sync(&fbdev); #else + SDL_LockMutex(global_sdl_mutex); SDL_Rect src = { .x = 0, @@ -76,6 +78,7 @@ static void hdz_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_ src.h = dst.h; SDL_RenderCopy(renderer, texture, &src, &dst); SDL_RenderPresent(renderer); + SDL_UnlockMutex(global_sdl_mutex); #endif if (disp_orbit_state & ORBIT_FLUSH) { @@ -127,8 +130,11 @@ int lvgl_init_porting() { lv_disp_drv_init(&disp_drv); #else - SDL_InitSubSystem(SDL_INIT_VIDEO); + if (SDL_WasInit(SDL_INIT_VIDEO) == 0) { + SDL_InitSubSystem(SDL_INIT_VIDEO); + } + SDL_LockMutex(global_sdl_mutex); window = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DISP_HOR_RES_FHD, DISP_VER_RES_FHD, 0); @@ -140,6 +146,7 @@ int lvgl_init_porting() { SDL_TEXTUREACCESS_STREAMING, DRAW_HOR_RES_FHD, DRAW_VER_RES_FHD); + SDL_UnlockMutex(global_sdl_mutex); fb1 = malloc(DRAW_HOR_RES_FHD * DRAW_VER_RES_FHD * ((LV_COLOR_DEPTH + 7) / 8)); fb2 = malloc(DRAW_HOR_RES_FHD * DRAW_VER_RES_FHD * ((LV_COLOR_DEPTH + 7) / 8));