From 16e0d24e1765f7157979f39188c9fa44d0ec3daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Mon, 11 Aug 2014 17:37:53 -0300 Subject: [PATCH] (SDL*) Properly initialize SDL library/subsystems --- audio/sdl_audio.c | 4 +++- gfx/sdl2_gfx.c | 5 ++++- gfx/sdl_gfx.c | 5 ++++- input/sdl_joypad.c | 39 +++++++++++++++++++++++++++++++-------- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/audio/sdl_audio.c b/audio/sdl_audio.c index e302e41f41..e1bd5ab5ee 100644 --- a/audio/sdl_audio.c +++ b/audio/sdl_audio.c @@ -60,7 +60,9 @@ static inline int find_num_frames(int rate, int latency) static void *sdl_audio_init(const char *device, unsigned rate, unsigned latency) { (void)device; - if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) + if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_AUDIO) < 0) + return NULL; + else if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) return NULL; sdl_audio_t *sdl = (sdl_audio_t*)calloc(1, sizeof(*sdl)); diff --git a/gfx/sdl2_gfx.c b/gfx/sdl2_gfx.c index 1b14cf7335..7a1c56a17a 100644 --- a/gfx/sdl2_gfx.c +++ b/gfx/sdl2_gfx.c @@ -385,7 +385,10 @@ static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **inp int i; - SDL_InitSubSystem(SDL_INIT_VIDEO); + if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_VIDEO) < 0) + return NULL; + else if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + return NULL; sdl2_video_t *vid = (sdl2_video_t*)calloc(1, sizeof(*vid)); if (!vid) diff --git a/gfx/sdl_gfx.c b/gfx/sdl_gfx.c index 7195f1d9e8..7bb7c7247f 100644 --- a/gfx/sdl_gfx.c +++ b/gfx/sdl_gfx.c @@ -207,7 +207,10 @@ static void *sdl_gfx_init(const video_info_t *video, const input_driver_t **inpu XInitThreads(); #endif - SDL_InitSubSystem(SDL_INIT_VIDEO); + if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_VIDEO) < 0) + return NULL; + else if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + return NULL; sdl_video_t *vid = (sdl_video_t*)calloc(1, sizeof(*vid)); if (!vid) diff --git a/input/sdl_joypad.c b/input/sdl_joypad.c index a20de56641..3d8e909aa1 100644 --- a/input/sdl_joypad.c +++ b/input/sdl_joypad.c @@ -34,7 +34,7 @@ typedef struct _sdl_joypad static sdl_joypad_t g_pads[MAX_PLAYERS]; #ifdef HAVE_SDL2 -static bool has_haptic; +static bool g_has_haptic; #endif static void sdl_joypad_connect(int id) @@ -56,9 +56,9 @@ static void sdl_joypad_connect(int id) #ifdef HAVE_SDL2 - pad->haptic = has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL; + pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL; - if (has_haptic && !pad->haptic) + if (g_has_haptic && !pad->haptic) RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n", id, SDL_GetError()); @@ -124,13 +124,23 @@ static void sdl_joypad_destroy(void) static bool sdl_joypad_init(void) { unsigned i; - if (SDL_Init(SDL_INIT_JOYSTICK) < 0) + if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_JOYSTICK) < 0) + return false; + else if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) return false; -#if 0 // TODO: Add SDL_GameController support. - if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) - RARCH_LOG("[SDL]: Failed to initialize game controller interface: %s\n", - SDL_GetError()); +#if HAS_SDL2 + // TODO: Add SDL_GameController support. + //if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) + // RARCH_LOG("[SDL]: Failed to initialize game controller interface: %s\n", + // SDL_GetError()); + + g_has_haptic = false; + if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) + RARCH_WARN("[SDL]: Failed to initialize haptic device support: %s\n", + SDL_GetError()); + else + g_has_haptic = true; #endif unsigned num_sticks = SDL_NumJoysticks(); @@ -140,11 +150,24 @@ static bool sdl_joypad_init(void) for (i = 0; i < num_sticks; i++) sdl_joypad_connect(i); +#ifndef HAVE_SDL2 + /* quit if no joypad is detected. */ + num_sticks = 0; + for (i = 0; i < MAX_PLAYERS; i++) + if (g_pads[i].joypad) + num_sticks++; + + if (num_sticks == 0) + goto error; +#endif + return true; +#ifndef HAVE_SDL2 error: sdl_joypad_destroy(); return false; +#endif } static bool sdl_joypad_button(unsigned port, uint16_t joykey)