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 cb847988..3dc85a7e 100644 --- a/src/core/input_device.c +++ b/src/core/input_device.c @@ -14,7 +14,7 @@ #include #ifdef EMULATOR_BUILD -#include +#include "SDLaccess.h" #endif #include "defines.h" @@ -492,7 +492,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); @@ -549,7 +551,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 } @@ -570,6 +575,10 @@ void input_device_init() { } } app_state_push(APP_STATE_MAINMENU); +#else + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("Error initializing SDL: %s\n", SDL_GetError()); + } #endif pthread_create(&input_device_pid, NULL, thread_input_device, NULL); } diff --git a/src/core/main.c b/src/core/main.c index 1573960c..8f30c062 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -11,6 +11,11 @@ #include #include +#ifdef EMULATOR_BUILD +#include "SDLaccess.h" +SDL_mutex *global_sdl_mutex; +#endif + #include "bmi270/accel_gyro.h" #include "core/app_state.h" #include "core/common.hh" @@ -140,6 +145,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) { + LOGE("Failed to create an SDL mutex!"); + } +#endif + // 1. Recall configuration settings_init(); settings_load(); diff --git a/src/ui/ui_porting.c b/src/ui/ui_porting.c index 15b2336b..e69a2c86 100644 --- a/src/ui/ui_porting.c +++ b/src/ui/ui_porting.c @@ -10,7 +10,7 @@ #include "lvgl/lvgl.h" #ifdef EMULATOR_BUILD -#include +#include "SDLaccess.h" static void *fb1, *fb2; SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; @@ -55,6 +55,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 +77,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 +129,13 @@ 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); + } else { + LOGI("SDL already initialised."); + } + 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 +147,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));