From c4c7069a8a0b4ffc5de8758c2aa154118449aa62 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Mon, 17 May 2010 17:19:31 +0000 Subject: Fix FS#11280 SDL docs say SDL_PumpEvent (implicitely called by SDL_Poll/WaitEvent) may only be called from the thread that initializes the video subsystem, apparently because Windows requires that. So create an (or bring it back) SDL thread (with preemtive behavior) to read the event queue for buttons and initialize the video subsystem. I'd probably would have done that anyway because it enables an interrupt-like method to read them (no polling). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26113 a1c6a512-1295-4272-9138-f99709370657 --- firmware/target/hosted/sdl/system-sdl.c | 71 +++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 7 deletions(-) (limited to 'firmware/target/hosted/sdl/system-sdl.c') diff --git a/firmware/target/hosted/sdl/system-sdl.c b/firmware/target/hosted/sdl/system-sdl.c index 693e8d1b57..3d67de425b 100644 --- a/firmware/target/hosted/sdl/system-sdl.c +++ b/firmware/target/hosted/sdl/system-sdl.c @@ -70,14 +70,37 @@ void sys_poweroff(void) SDL_Quit(); } -void system_init(void) +/* + * Button read loop */ +void gui_message_loop(void); + +/* + * This callback let's the main thread run again after SDL has been initialized + **/ +static uint32_t cond_signal(uint32_t interval, void *param) +{ + (void)interval; + SDL_cond *c = (SDL_cond*)param; + /* remove timer, CondSignal returns 0 on success */ + return SDL_CondSignal(c); +} + +/* + * This thread will read the buttons in an interrupt like fashion, and + * also initializes SDL_INIT_VIDEO and the surfaces + * + * it must be done in the same thread (at least on windows) because events only + * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO + * + * This is an SDL thread and relies on preemptive behavoir of the host + **/ +static int sdl_event_thread(void * param) { + SDL_InitSubSystem(SDL_INIT_VIDEO); + SDL_Surface *picture_surface; int width, height; - if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)) - panicf("%s", SDL_GetError()); - /* Try and load the background image. If it fails go without */ if (background) { picture_surface = SDL_LoadBMP("UI256.bmp"); @@ -122,10 +145,43 @@ void system_init(void) sim_lcd_remote_init(); #endif - if (background && picture_surface != NULL) { + if (background && picture_surface != NULL) SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL); - SDL_UpdateRect(gui_surface, 0, 0, 0, 0); - } + + /* calling SDL_CondSignal() right away here doesn't work reliably so + * post-pone it a bit */ + SDL_AddTimer(100, cond_signal, param); + /* + * finally enter the button loop */ + while(1) + gui_message_loop(); + + return 0; +} + + +void system_init(void) +{ + SDL_cond *c; + SDL_mutex *m; + if (SDL_Init(SDL_INIT_TIMER)) + panicf("%s", SDL_GetError()); + atexit(SDL_Quit); + + c = SDL_CreateCond(); + m = SDL_CreateMutex(); + + SDL_CreateThread(sdl_event_thread, c); + + /* Lock mutex and wait for sdl_event_thread to run so that it can + * initialize the surfaces and video subsystem needed for SDL events */ + SDL_LockMutex(m); + SDL_CondWait(c, m); + SDL_UnlockMutex(m); + + /* cleanup */ + SDL_DestroyCond(c); + SDL_DestroyMutex(m); } void system_exception_wait(void) @@ -138,6 +194,7 @@ void system_reboot(void) sim_thread_exception_wait(); } + void sys_handle_argv(int argc, char *argv[]) { if (argc >= 1) -- cgit v1.2.3