Merge pull request #889 from heuripedes/sdl2-improvements

SDL2 improvements
This commit is contained in:
Twinaphex 2014-08-19 22:27:24 +02:00
commit db10f4ed8c
4 changed files with 135 additions and 50 deletions

View File

@ -60,8 +60,11 @@ static inline int find_num_frames(int rate, int latency)
static void *sdl_audio_init(const char *device, unsigned rate, unsigned latency) static void *sdl_audio_init(const char *device, unsigned rate, unsigned latency)
{ {
(void)device; (void)device;
if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_AUDIO) < 0) if (SDL_WasInit(0) == 0)
return NULL; {
if (SDL_Init(SDL_INIT_AUDIO) < 0)
return NULL;
}
else if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) else if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
return NULL; return NULL;

View File

@ -89,6 +89,8 @@ static bool set_direct_pix_conv(struct scaler_ctx *ctx)
ctx->direct_pixconv = conv_argb8888_abgr8888; ctx->direct_pixconv = conv_argb8888_abgr8888;
else if (ctx->in_fmt == SCALER_FMT_YUYV && ctx->out_fmt == SCALER_FMT_ARGB8888) else if (ctx->in_fmt == SCALER_FMT_YUYV && ctx->out_fmt == SCALER_FMT_ARGB8888)
ctx->direct_pixconv = conv_yuyv_argb8888; ctx->direct_pixconv = conv_yuyv_argb8888;
else if (ctx->in_fmt == SCALER_FMT_RGBA4444 && ctx->out_fmt == SCALER_FMT_ARGB8888)
ctx->direct_pixconv = conv_rgba4444_argb8888;
else else
return false; return false;

View File

@ -32,8 +32,6 @@
#include "config.h" #include "config.h"
#endif #endif
#include "SDL2/SDL_syswm.h"
typedef struct sdl2_tex typedef struct sdl2_tex
{ {
SDL_Texture *tex; SDL_Texture *tex;
@ -369,8 +367,11 @@ static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **inp
int i; int i;
if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_VIDEO) < 0) if (SDL_WasInit(0) == 0)
return NULL; {
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return NULL;
}
else if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) else if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
return NULL; return NULL;
@ -378,19 +379,19 @@ static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **inp
if (!vid) if (!vid)
return NULL; return NULL;
// RARCH_LOG("[SDL]: supported video drivers (change with $SDL_VIDEODRIVER):\n"); RARCH_LOG("[SDL]: supported video drivers (change with $SDL_VIDEODRIVER):\n");
// for (i = 0; i < SDL_GetNumVideoDrivers(); ++i) for (i = 0; i < SDL_GetNumVideoDrivers(); ++i)
// { {
// RARCH_LOG("\t%s\n", SDL_GetVideoDriver(i)); RARCH_LOG("\t%s\n", SDL_GetVideoDriver(i));
// } }
RARCH_LOG("[SDL]: Available displays:\n"); RARCH_LOG("[SDL]: Available displays:\n");
for(i = 0; i < SDL_GetNumVideoDisplays(); ++i) for(i = 0; i < SDL_GetNumVideoDisplays(); ++i)
{ {
SDL_DisplayMode mode; SDL_DisplayMode mode;
if (SDL_GetCurrentDisplayMode(i, &mode) == 0) if (SDL_GetCurrentDisplayMode(i, &mode) <= 0)
RARCH_LOG("\tDisplay #%i mode: unknown.\n", i); RARCH_LOG("\tDisplay #%i mode: unknown.\n", i);
else else
RARCH_LOG("\tDisplay #%i mode: %ix%i@%ihz.\n", i, mode.w, mode.h, RARCH_LOG("\tDisplay #%i mode: %ix%i@%ihz.\n", i, mode.w, mode.h,

View File

@ -21,6 +21,7 @@ typedef struct _sdl_joypad
{ {
SDL_Joystick *joypad; SDL_Joystick *joypad;
#ifdef HAVE_SDL2 #ifdef HAVE_SDL2
SDL_GameController *controller;
SDL_Haptic *haptic; SDL_Haptic *haptic;
int rumble_effect; // -1 = not initialized, -2 = error/unsupported int rumble_effect; // -1 = not initialized, -2 = error/unsupported
#endif #endif
@ -32,30 +33,103 @@ typedef struct _sdl_joypad
#endif #endif
} sdl_joypad_t; } sdl_joypad_t;
#ifdef HAVE_SDL2
const int g_subsystem = SDL_INIT_GAMECONTROLLER;
#else
const int g_subsystem = SDL_INIT_JOYSTICK;
#endif
static sdl_joypad_t g_pads[MAX_PLAYERS]; static sdl_joypad_t g_pads[MAX_PLAYERS];
#ifdef HAVE_SDL2 #ifdef HAVE_SDL2
static bool g_has_haptic; static bool g_has_haptic;
#endif #endif
static void sdl_joypad_connect(int id) static const char* pad_name(unsigned id)
{
#ifdef HAVE_SDL2
if (g_pads[id].controller)
return SDL_GameControllerNameForIndex(id);
else
return SDL_JoystickNameForIndex(id);
#else
return SDL_JoystickName(id);
#endif
}
static uint8_t pad_get_button(sdl_joypad_t *pad, unsigned button)
{
#ifdef HAVE_SDL2
/* TODO: see if a LUT like winxinput_joypad.c's button_index_to_bitmap_code is needed. */
if (pad->controller)
return SDL_GameControllerGetButton(pad->controller, button);
else
#endif
return SDL_JoystickGetButton(pad->joypad, button);
}
static uint8_t pad_get_hat(sdl_joypad_t *pad, unsigned hat)
{
#ifdef HAVE_SDL2
if (pad->controller)
return pad_get_button(pad, hat);
else
#endif
return SDL_JoystickGetHat(pad->joypad, hat);
}
static int16_t pad_get_axis(sdl_joypad_t *pad, unsigned axis)
{
#ifdef HAVE_SDL2
/* TODO: see if a rarch <-> sdl translation is needed. */
if (pad->controller)
return SDL_GameControllerGetAxis(pad->controller, axis);
else
#endif
return SDL_JoystickGetAxis(pad->joypad, axis);
}
static void pad_connect(unsigned id)
{ {
sdl_joypad_t *pad = &g_pads[id]; sdl_joypad_t *pad = &g_pads[id];
pad->joypad = SDL_JoystickOpen(id); bool success = false;
if (!pad->joypad)
#ifdef HAVE_SDL2
if (SDL_IsGameController(id))
{ {
RARCH_ERR("[SDL]: Couldn't open SDL joystick #%u.\n", id); pad->controller = SDL_GameControllerOpen(id);
pad->joypad = SDL_GameControllerGetJoystick(pad->controller);
success = pad->joypad != NULL && pad->controller != NULL;
}
else
#endif
{
pad->joypad = SDL_JoystickOpen(id);
success = pad->joypad != NULL;
}
if (!success)
{
RARCH_ERR("[SDL]: Couldn't open joystick #%u: %s.\n", id, SDL_GetError());
if (pad->joypad)
SDL_JoystickClose(pad->joypad);
pad->joypad = NULL;
return; return;
} }
RARCH_LOG("[SDL]: Joypad #%u connected: %s.\n", strlcpy(g_settings.input.device_names[id], pad_name(id), sizeof(g_settings.input.device_names[id]));
#ifdef HAVE_SDL2 input_config_autoconfigure_joypad(id, pad_name(id), sdl_joypad.ident);
id, SDL_JoystickName(pad->joypad));
#else
id, SDL_JoystickName(id));
#endif
RARCH_LOG("[SDL]: Joypad #%u connected: %s.\n", id, pad_name(id));
#ifdef HAVE_SDL2 #ifdef HAVE_SDL2
if (pad->controller)
RARCH_LOG("[SDL]: Joypad #%u supports game controller api.\n", id);
pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL; pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL;
if (g_has_haptic && !pad->haptic) if (g_has_haptic && !pad->haptic)
@ -95,19 +169,27 @@ static void sdl_joypad_connect(int id)
#endif #endif
} }
static void sdl_joypad_disconnect(int id) static void pad_disconnect(unsigned id)
{ {
#ifdef HAVE_SDL2 #ifdef HAVE_SDL2
if (g_pads[id].haptic) if (g_pads[id].haptic)
SDL_HapticClose(g_pads[id].haptic); SDL_HapticClose(g_pads[id].haptic);
#endif
if (g_pads[id].controller)
{
SDL_GameControllerClose(g_pads[id].controller);
RARCH_LOG("[SDL]: Joypad #%u disconnected.\n", id);
}
else
#endif
if (g_pads[id].joypad) if (g_pads[id].joypad)
{ {
SDL_JoystickClose(g_pads[id].joypad); SDL_JoystickClose(g_pads[id].joypad);
RARCH_LOG("[SDL]: Joypad #%u disconnected.\n", id); RARCH_LOG("[SDL]: Joypad #%u disconnected.\n", id);
} }
g_settings.input.device_names[id][0] = '\0';
memset(&g_pads[id], 0, sizeof(g_pads[id])); memset(&g_pads[id], 0, sizeof(g_pads[id]));
} }
@ -115,26 +197,25 @@ static void sdl_joypad_destroy(void)
{ {
unsigned i; unsigned i;
for (i = 0; i < MAX_PLAYERS; i++) for (i = 0; i < MAX_PLAYERS; i++)
sdl_joypad_disconnect(i); pad_disconnect(i);
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); SDL_QuitSubSystem(g_subsystem);
memset(g_pads, 0, sizeof(g_pads)); memset(g_pads, 0, sizeof(g_pads));
} }
static bool sdl_joypad_init(void) static bool sdl_joypad_init(void)
{ {
unsigned i; unsigned i;
if (SDL_WasInit(0) == 0 && SDL_Init(SDL_INIT_JOYSTICK) < 0)
return false; if (SDL_WasInit(0) == 0)
else if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
if (SDL_Init(g_subsystem) < 0)
return false;
}
else if (SDL_InitSubSystem(g_subsystem) < 0)
return false; return false;
#if HAS_SDL2 #if HAVE_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; g_has_haptic = false;
if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0)
RARCH_WARN("[SDL]: Failed to initialize haptic device support: %s\n", RARCH_WARN("[SDL]: Failed to initialize haptic device support: %s\n",
@ -143,12 +224,14 @@ static bool sdl_joypad_init(void)
g_has_haptic = true; g_has_haptic = true;
#endif #endif
memset(g_pads, 0, sizeof(g_pads));
unsigned num_sticks = SDL_NumJoysticks(); unsigned num_sticks = SDL_NumJoysticks();
if (num_sticks > MAX_PLAYERS) if (num_sticks > MAX_PLAYERS)
num_sticks = MAX_PLAYERS; num_sticks = MAX_PLAYERS;
for (i = 0; i < num_sticks; i++) for (i = 0; i < num_sticks; i++)
sdl_joypad_connect(i); pad_connect(i);
#ifndef HAVE_SDL2 #ifndef HAVE_SDL2
/* quit if no joypad is detected. */ /* quit if no joypad is detected. */
@ -175,7 +258,7 @@ static bool sdl_joypad_button(unsigned port, uint16_t joykey)
if (joykey == NO_BTN) if (joykey == NO_BTN)
return false; return false;
const sdl_joypad_t *pad = &g_pads[port]; sdl_joypad_t *pad = &g_pads[port];
if (!pad->joypad) if (!pad->joypad)
return false; return false;
@ -186,7 +269,7 @@ static bool sdl_joypad_button(unsigned port, uint16_t joykey)
if (hat >= pad->num_hats) if (hat >= pad->num_hats)
return false; return false;
Uint8 dir = SDL_JoystickGetHat(pad->joypad, hat); Uint8 dir = pad_get_hat(pad, hat);
switch (GET_HAT_DIR(joykey)) switch (GET_HAT_DIR(joykey))
{ {
case HAT_UP_MASK: case HAT_UP_MASK:
@ -203,7 +286,7 @@ static bool sdl_joypad_button(unsigned port, uint16_t joykey)
} }
else // Check the button else // Check the button
{ {
if (joykey < pad->num_buttons && SDL_JoystickGetButton(pad->joypad, joykey)) if (joykey < pad->num_buttons && pad_get_button(pad, joykey))
return true; return true;
return false; return false;
@ -215,14 +298,14 @@ static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis)
if (joyaxis == AXIS_NONE) if (joyaxis == AXIS_NONE)
return 0; return 0;
const sdl_joypad_t *pad = &g_pads[port]; sdl_joypad_t *pad = &g_pads[port];
if (!pad->joypad) if (!pad->joypad)
return false; return false;
Sint16 val = 0; int16_t val = 0;
if (AXIS_NEG_GET(joyaxis) < pad->num_axes) if (AXIS_NEG_GET(joyaxis) < pad->num_axes)
{ {
val = SDL_JoystickGetAxis(pad->joypad, AXIS_NEG_GET(joyaxis)); val = pad_get_axis(pad, AXIS_NEG_GET(joyaxis));
if (val > 0) if (val > 0)
val = 0; val = 0;
@ -231,7 +314,7 @@ static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis)
} }
else if (AXIS_POS_GET(joyaxis) < pad->num_axes) else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
{ {
val = SDL_JoystickGetAxis(pad->joypad, AXIS_POS_GET(joyaxis)); val = pad_get_axis(pad, AXIS_POS_GET(joyaxis));
if (val < 0) if (val < 0)
val = 0; val = 0;
@ -250,11 +333,11 @@ static void sdl_joypad_poll(void)
{ {
if (event.type == SDL_JOYDEVICEADDED) if (event.type == SDL_JOYDEVICEADDED)
{ {
sdl_joypad_connect(event.jdevice.which); pad_connect(event.jdevice.which);
} }
else if (event.type == SDL_JOYDEVICEREMOVED) else if (event.type == SDL_JOYDEVICEREMOVED)
{ {
sdl_joypad_disconnect(event.jdevice.which); pad_disconnect(event.jdevice.which);
} }
} }
#else #else
@ -322,11 +405,7 @@ static const char *sdl_joypad_name(unsigned pad)
if (pad >= MAX_PLAYERS) if (pad >= MAX_PLAYERS)
return NULL; return NULL;
#ifdef HAVE_SDL2 return pad_name(pad);
return SDL_JoystickName(g_pads[pad].joypad);
#else
return SDL_JoystickName(pad);
#endif
} }
const rarch_joypad_driver_t sdl_joypad = { const rarch_joypad_driver_t sdl_joypad = {