Skip to content

Commit

Permalink
Merge pull request #428 from eliasrenman/feature/fix-emulator-bug-seg…
Browse files Browse the repository at this point in the history
…-fault

Feature/fix emulator bug seg fault #391
  • Loading branch information
ligenxxxx authored Jul 27, 2024
2 parents 210e5e9 + f910ef0 commit 904b6a3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/core/SDLaccess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _SDLaccess_H
#define _SDLaccess_H

#include <SDL2/SDL.h>

extern SDL_mutex *global_sdl_mutex;

#endif
13 changes: 11 additions & 2 deletions src/core/input_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <minIni.h>

#ifdef EMULATOR_BUILD
#include <SDL2/SDL.h>
#include "SDLaccess.h"
#endif

#include "defines.h"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
Expand All @@ -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);
}
12 changes: 12 additions & 0 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#include <lvgl/lvgl.h>
#include <minIni.h>

#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"
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions src/ui/ui_porting.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "lvgl/lvgl.h"

#ifdef EMULATOR_BUILD
#include <SDL2/SDL.h>
#include "SDLaccess.h"
static void *fb1, *fb2;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down

0 comments on commit 904b6a3

Please sign in to comment.