Merge pull request #869 from heuripedes/master

SDL fixes and improvements
This commit is contained in:
Twinaphex 2014-08-12 00:53:56 +02:00
commit e087072f7d
13 changed files with 276 additions and 694 deletions

View File

@ -214,8 +214,8 @@ ifeq ($(HAVE_SDL), 1)
endif
ifeq ($(HAVE_SDL2), 1)
OBJ += gfx/sdl2_gfx.o input/sdl2_input.o input/sdl2_joypad.o audio/sdl_audio.o
JOYCONFIG_OBJ += input/sdl2_joypad.o
OBJ += gfx/sdl2_gfx.o input/sdl_input.o input/sdl_joypad.o audio/sdl_audio.o
JOYCONFIG_OBJ += input/sdl_joypad.o
JOYCONFIG_LIBS += $(SDL2_LIBS)
DEFINES += $(SDL2_CFLAGS) $(BSD_LOCAL_INC)
LIBS += $(SDL2_LIBS)

View File

@ -132,8 +132,8 @@ ifeq ($(HAVE_MENU_COMMON), 1)
endif
ifeq ($(HAVE_SDL2), 1)
OBJ += gfx/sdl2_gfx.o input/sdl2_input.o input/sdl2_joypad.o audio/sdl_audio.o
JOBJ += input/sdl2_joypad.o
OBJ += gfx/sdl2_gfx.o input/sdl_input.o input/sdl_joypad.o audio/sdl_audio.o
JOBJ += input/sdl_joypad.o
LIBS += -lSDL2
JLIBS += -lSDL2
DEFINES += -ISDL2 -DHAVE_SDL2

View File

@ -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));

View File

@ -152,12 +152,9 @@ static const input_driver_t *input_drivers[] = {
#if defined(SN_TARGET_PSP2) || defined(PSP)
&input_psp,
#endif
#ifdef HAVE_SDL
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
&input_sdl,
#endif
#ifdef HAVE_SDL2
&input_sdl2,
#endif
#ifdef HAVE_DINPUT
&input_dinput,
#endif

View File

@ -628,7 +628,6 @@ extern const video_driver_t video_omap;
extern const video_driver_t video_exynos;
extern const input_driver_t input_android;
extern const input_driver_t input_sdl;
extern const input_driver_t input_sdl2;
extern const input_driver_t input_dinput;
extern const input_driver_t input_x;
extern const input_driver_t input_wayland;

View File

@ -208,7 +208,7 @@ static void sdl2_gfx_set_handles(sdl2_video_t *vid)
{
driver.display_type = RARCH_DISPLAY_WIN32;
driver.video_display = 0;
driver.video_window = (uintptr_t)info.window;
driver.video_window = (uintptr_t)info.info.window;
}
#elif defined(HAVE_X11)
SDL_SysWMinfo info;
@ -331,12 +331,11 @@ static void sdl_refresh_input_size(sdl2_video_t *vid, bool menu, bool rgb32,
{
switch (g_extern.system.pix_fmt)
{
case RETRO_PIXEL_FORMAT_0RGB1555:
format = SDL_PIXELFORMAT_ARGB1555;
break;
case RETRO_PIXEL_FORMAT_XRGB8888:
format = SDL_PIXELFORMAT_ARGB8888;
break;
case RETRO_PIXEL_FORMAT_0RGB1555:
/* this assumes the frontend will convert the input to RGB565 */
case RETRO_PIXEL_FORMAT_RGB565:
format = SDL_PIXELFORMAT_RGB565;
break;
@ -386,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)
@ -450,11 +452,11 @@ static void *sdl2_gfx_init(const video_info_t *video, const input_driver_t **inp
if (input && input_data)
{
void *sdl2_input = input_sdl2.init();
if (sdl2_input)
void *sdl_input = input_sdl.init();
if (sdl_input)
{
*input = &input_sdl2;
*input_data = sdl2_input;
*input = &input_sdl;
*input_data = sdl_input;
}
else
{

View File

@ -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)

View File

@ -74,12 +74,9 @@ static const rarch_joypad_driver_t *joypad_drivers[] = {
#ifdef ANDROID
&android_joypad,
#endif
#ifdef HAVE_SDL
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
&sdl_joypad,
#endif
#ifdef HAVE_SDL2
&sdl2_joypad,
#endif
#ifdef __MACH__
&apple_joypad,
#endif
@ -468,7 +465,7 @@ const struct rarch_key_map rarch_key_map_sdl[] = {
#endif
#ifdef HAVE_SDL2
const struct rarch_key_map rarch_key_map_sdl2[] = {
const struct rarch_key_map rarch_key_map_sdl[] = {
{ SDL_SCANCODE_LEFT, RETROK_LEFT },
{ SDL_SCANCODE_RIGHT, RETROK_RIGHT },
{ SDL_SCANCODE_UP, RETROK_UP },

View File

@ -104,7 +104,6 @@ extern const rarch_joypad_driver_t linuxraw_joypad;
extern const rarch_joypad_driver_t udev_joypad;
extern const rarch_joypad_driver_t winxinput_joypad; // Named as such to avoid confusion with xb1/360 port code
extern const rarch_joypad_driver_t sdl_joypad;
extern const rarch_joypad_driver_t sdl2_joypad;
extern const rarch_joypad_driver_t ps3_joypad;
extern const rarch_joypad_driver_t psp_joypad;
extern const rarch_joypad_driver_t xdk_joypad;

View File

@ -1,324 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "../driver.h"
#include "SDL.h"
#include "../gfx/gfx_context.h"
#include "../boolean.h"
#include "../general.h"
#include <stdint.h>
#include <stdlib.h>
#include "../libretro.h"
#include "input_common.h"
typedef struct sdl2_input
{
const rarch_joypad_driver_t *joypad;
int mouse_x, mouse_y;
int mouse_abs_x, mouse_abs_y;
int mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd, mouse_wl, mouse_wr;
} sdl2_input_t;
static void *sdl2_input_init(void)
{
input_init_keyboard_lut(rarch_key_map_sdl2);
sdl2_input_t *sdl = (sdl2_input_t*)calloc(1, sizeof(*sdl));
if (!sdl)
return NULL;
sdl->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
return sdl;
}
static bool sdl2_key_pressed(int key)
{
if (key >= RETROK_LAST)
return false;
int sym = input_translate_rk_to_keysym((enum retro_key)key);
int num_keys;
const uint8_t *keymap = SDL_GetKeyboardState(&num_keys);
if (sym < 0 || sym >= num_keys)
return false;
return keymap[sym];
}
static bool sdl2_is_pressed(sdl2_input_t *sdl, unsigned port_num, const struct retro_keybind *binds, unsigned key)
{
if (sdl2_key_pressed(binds[key].key))
return true;
return input_joypad_pressed(sdl->joypad, port_num, binds, key);
}
static int16_t sdl2_analog_pressed(sdl2_input_t *sdl, const struct retro_keybind *binds,
unsigned index, unsigned id)
{
unsigned id_minus = 0;
unsigned id_plus = 0;
input_conv_analog_id_to_bind_id(index, id, &id_minus, &id_plus);
int16_t pressed_minus = sdl2_key_pressed(binds[id_minus].key) ? -0x7fff : 0;
int16_t pressed_plus = sdl2_key_pressed(binds[id_plus].key) ? 0x7fff : 0;
return pressed_plus + pressed_minus;
}
static bool sdl2_bind_button_pressed(void *data, int key)
{
const struct retro_keybind *binds = g_settings.input.binds[0];
if (key >= 0 && key < RARCH_BIND_LIST_END)
return sdl2_is_pressed((sdl2_input_t*)data, 0, binds, key);
else
return false;
}
static int16_t sdl2_joypad_device_state(sdl2_input_t *sdl, const struct retro_keybind **binds_,
unsigned port_num, unsigned id)
{
const struct retro_keybind *binds = binds_[port_num];
if (id < RARCH_BIND_LIST_END)
return binds[id].valid && sdl2_is_pressed(sdl, port_num, binds, id);
else
return 0;
}
static int16_t sdl2_analog_device_state(sdl2_input_t *sdl, const struct retro_keybind **binds,
unsigned port_num, unsigned index, unsigned id)
{
int16_t ret = sdl2_analog_pressed(sdl, binds[port_num], index, id);
if (!ret)
ret = input_joypad_analog(sdl->joypad, port_num, index, id, binds[port_num]);
return ret;
}
static int16_t sdl2_keyboard_device_state(sdl2_input_t *sdl, unsigned id)
{
return sdl2_key_pressed(id);
}
static int16_t sdl2_mouse_device_state(sdl2_input_t *sdl, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_LEFT:
return sdl->mouse_l;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
return sdl->mouse_r;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
return sdl->mouse_wu;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
return sdl->mouse_wd;
case RETRO_DEVICE_ID_MOUSE_X:
return sdl->mouse_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return sdl->mouse_y;
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return sdl->mouse_m;
default:
return 0;
}
}
static int16_t sdl2_pointer_device_state(sdl2_input_t *sdl, unsigned index, unsigned id, bool screen)
{
if (index != 0)
return 0;
int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0;
bool valid = input_translate_coord_viewport(sdl->mouse_abs_x, sdl->mouse_abs_y,
&res_x, &res_y, &res_screen_x, &res_screen_y);
if (!valid)
return 0;
if (screen)
{
res_x = res_screen_x;
res_y = res_screen_y;
}
bool inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
if (!inside)
return 0;
switch (id)
{
case RETRO_DEVICE_ID_POINTER_X:
return res_x;
case RETRO_DEVICE_ID_POINTER_Y:
return res_y;
case RETRO_DEVICE_ID_POINTER_PRESSED:
return sdl->mouse_l;
default:
return 0;
}
}
static int16_t sdl2_lightgun_device_state(sdl2_input_t *sdl, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_LIGHTGUN_X:
return sdl->mouse_x;
case RETRO_DEVICE_ID_LIGHTGUN_Y:
return sdl->mouse_y;
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
return sdl->mouse_l;
case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
return sdl->mouse_m;
case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
return sdl->mouse_r;
case RETRO_DEVICE_ID_LIGHTGUN_START:
return sdl->mouse_m && sdl->mouse_r;
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
return sdl->mouse_m && sdl->mouse_l;
default:
return 0;
}
}
static int16_t sdl2_input_state(void *data_, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned index, unsigned id)
{
sdl2_input_t *data = (sdl2_input_t*)data_;
switch (device)
{
case RETRO_DEVICE_JOYPAD:
return sdl2_joypad_device_state(data, binds, port, id);
case RETRO_DEVICE_ANALOG:
return sdl2_analog_device_state(data, binds, port, index, id);
case RETRO_DEVICE_MOUSE:
return sdl2_mouse_device_state(data, id);
case RETRO_DEVICE_POINTER:
case RARCH_DEVICE_POINTER_SCREEN:
return sdl2_pointer_device_state(data, index, id, device == RARCH_DEVICE_POINTER_SCREEN);
case RETRO_DEVICE_KEYBOARD:
return sdl2_keyboard_device_state(data, id);
case RETRO_DEVICE_LIGHTGUN:
return sdl2_lightgun_device_state(data, id);
default:
return 0;
}
}
static void sdl2_input_free(void *data)
{
if (!data)
return;
// Flush out all pending events.
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
sdl2_input_t *sdl = (sdl2_input_t*)data;
if (sdl->joypad)
sdl->joypad->destroy();
free(data);
}
static void sdl2_grab_mouse(void *data, bool state)
{
sdl2_input_t *sdl = (sdl2_input_t*)data;
if (driver.video == &video_sdl2)
{
/* first member of sdl2_video_t is the window */
struct temp{
SDL_Window *w;
};
SDL_SetWindowGrab(((struct temp*)driver.video_data)->w,
state ? SDL_TRUE : SDL_FALSE);
}
}
static bool sdl2_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength)
{
sdl2_input_t *sdl = (sdl2_input_t*)data;
return input_joypad_set_rumble(sdl->joypad, port, effect, strength);
}
static const rarch_joypad_driver_t *sdl2_get_joypad_driver(void *data)
{
sdl2_input_t *sdl = (sdl2_input_t*)data;
return sdl->joypad;
}
static void sdl2_input_poll(void *data)
{
sdl2_input_t *sdl = (sdl2_input_t*)data;
SDL_Event event;
SDL_PumpEvents();
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEWHEEL, SDL_MOUSEWHEEL) > 0)
{
if (event.type == SDL_MOUSEWHEEL)
{
sdl->mouse_wu = event.wheel.y < 0;
sdl->mouse_wd = event.wheel.y > 0;
sdl->mouse_wl = event.wheel.x < 0;
sdl->mouse_wr = event.wheel.x > 0;
break;
}
}
if (sdl->joypad)
sdl->joypad->poll();
uint8_t btn = SDL_GetRelativeMouseState(&sdl->mouse_x, &sdl->mouse_y);
SDL_GetMouseState(&sdl->mouse_abs_x, &sdl->mouse_abs_y);
sdl->mouse_l = SDL_BUTTON(SDL_BUTTON_LEFT) & btn ? 1 : 0;
sdl->mouse_r = SDL_BUTTON(SDL_BUTTON_RIGHT) & btn ? 1 : 0;
sdl->mouse_m = SDL_BUTTON(SDL_BUTTON_MIDDLE) & btn ? 1 : 0;
}
static uint64_t sdl2_get_capabilities(void *data)
{
uint64_t caps = 0;
caps |= (1 << RETRO_DEVICE_JOYPAD);
caps |= (1 << RETRO_DEVICE_MOUSE);
caps |= (1 << RETRO_DEVICE_KEYBOARD);
caps |= (1 << RETRO_DEVICE_LIGHTGUN);
caps |= (1 << RETRO_DEVICE_POINTER);
caps |= (1 << RETRO_DEVICE_ANALOG);
return caps;
}
const input_driver_t input_sdl2 = {
sdl2_input_init,
sdl2_input_poll,
sdl2_input_state,
sdl2_bind_button_pressed,
sdl2_input_free,
NULL,
NULL,
NULL,
sdl2_get_capabilities,
NULL,
"sdl2",
sdl2_grab_mouse,
sdl2_set_rumble,
sdl2_get_joypad_driver,
};

View File

@ -1,315 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "input_common.h"
#include "SDL.h"
#include "../general.h"
typedef struct _sdl2_joypad
{
SDL_Joystick *joypad;
SDL_Haptic *haptic;
int rumble_effect; // -1 = not initialized, -2 = error/unsupported
unsigned num_axes;
unsigned num_buttons;
unsigned num_hats;
unsigned num_balls;
} sdl2_joypad_t;
static sdl2_joypad_t g_pads[MAX_PLAYERS];
static bool has_haptic;
static void sdl2_joypad_destroy(void)
{
unsigned i;
for (i = 0; i < MAX_PLAYERS; i++)
{
}
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
memset(g_pads, 0, sizeof(g_pads));
}
static void sdl2_joypad_connect(int id)
{
sdl2_joypad_t *pad = &g_pads[id];
pad->joypad = SDL_JoystickOpen(id);
if (!pad->joypad)
{
RARCH_ERR("[SDL]: Couldn't open SDL joystick #%u.\n", id);
return;
}
RARCH_LOG("[SDL]: Joypad #%u connected: %s.\n",
id, SDL_JoystickName(pad->joypad));
pad->haptic = has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL;
if (has_haptic && !pad->haptic)
RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n",
id, SDL_GetError());
pad->rumble_effect = -1;
if (pad->haptic)
{
SDL_HapticEffect efx;
efx.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.large_magnitude = efx.leftright.small_magnitude = 0x4000;
efx.leftright.length = 5000;
if (SDL_HapticEffectSupported(pad->haptic, &efx) == SDL_FALSE)
{
pad->rumble_effect = -2;
RARCH_WARN("[SDL]: Joypad #%u does not support rumble.\n", id);
}
}
pad->num_axes = SDL_JoystickNumAxes(pad->joypad);
pad->num_buttons = SDL_JoystickNumButtons(pad->joypad);
pad->num_hats = SDL_JoystickNumHats(pad->joypad);
pad->num_balls = SDL_JoystickNumBalls(pad->joypad);
RARCH_LOG("[SDL]: Joypad #%u has: %u axes, %u buttons, %u hats and %u trackballs.\n",
id, pad->num_axes, pad->num_buttons, pad->num_hats, pad->num_balls);
}
static void sdl2_joypad_disconnect(int id)
{
RARCH_LOG("[SDL]: Joypad #%u disconnected.\n", id);
if (g_pads[id].haptic)
SDL_HapticClose(g_pads[id].haptic);
if (g_pads[id].joypad)
SDL_JoystickClose(g_pads[id].joypad);
memset(&g_pads[id], 0, sizeof(g_pads[id]));
}
static bool sdl2_joypad_init(void)
{
unsigned i;
has_haptic = false;
if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
{
RARCH_WARN("[SDL]: Failed to initialize joystick interface: %s\n",
SDL_GetError());
return false;
}
SDL_JoystickEventState(SDL_ENABLE);
// 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 (SDL_Init(SDL_INIT_HAPTIC) < 0)
RARCH_WARN("[SDL]: Failed to initialize haptic device support: %s\n",
SDL_GetError());
else
has_haptic = true;
unsigned num_sticks = SDL_NumJoysticks();
if (num_sticks > MAX_PLAYERS)
num_sticks = MAX_PLAYERS;
for (i = 0; i < num_sticks; i++)
{
sdl2_joypad_connect(i);
}
num_sticks = 0;
for (i = 0; i < MAX_PLAYERS; i++)
{
if (g_pads[i].joypad)
num_sticks++;
}
if (num_sticks == 0)
goto error;
return true;
error:
sdl2_joypad_destroy();
return false;
}
static bool sdl2_joypad_button(unsigned port, uint16_t joykey)
{
if (joykey == NO_BTN)
return false;
const sdl2_joypad_t *pad = &g_pads[port];
if (!pad->joypad)
return false;
// Check hat.
if (GET_HAT_DIR(joykey))
{
uint16_t hat = GET_HAT(joykey);
if (hat >= pad->num_hats)
return false;
Uint8 dir = SDL_JoystickGetHat(pad->joypad, hat);
switch (GET_HAT_DIR(joykey))
{
case HAT_UP_MASK:
return dir & SDL_HAT_UP;
case HAT_DOWN_MASK:
return dir & SDL_HAT_DOWN;
case HAT_LEFT_MASK:
return dir & SDL_HAT_LEFT;
case HAT_RIGHT_MASK:
return dir & SDL_HAT_RIGHT;
default:
return false;
}
}
else // Check the button
{
if (joykey < pad->num_buttons && SDL_JoystickGetButton(pad->joypad, joykey))
return true;
return false;
}
}
static int16_t sdl2_joypad_axis(unsigned port, uint32_t joyaxis)
{
if (joyaxis == AXIS_NONE)
return 0;
const sdl2_joypad_t *pad = &g_pads[port];
if (!pad->joypad)
return false;
Sint16 val = 0;
if (AXIS_NEG_GET(joyaxis) < pad->num_axes)
{
val = SDL_JoystickGetAxis(pad->joypad, AXIS_NEG_GET(joyaxis));
if (val > 0)
val = 0;
else if (val < -0x7fff) // -0x8000 can cause trouble if we later abs() it.
val = -0x7fff;
}
else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
{
val = SDL_JoystickGetAxis(pad->joypad, AXIS_POS_GET(joyaxis));
if (val < 0)
val = 0;
}
return val;
}
static void sdl2_joypad_poll(void)
{
SDL_Event event;
SDL_PumpEvents();
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0)
{
if (event.type == SDL_JOYDEVICEADDED)
{
sdl2_joypad_connect(event.jdevice.which);
}
else if (event.type == SDL_JOYDEVICEREMOVED)
{
sdl2_joypad_disconnect(event.jdevice.which);
}
}
}
static bool sdl2_joypad_set_rumble(unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
{
SDL_HapticEffect efx;
memset(&efx, 0, sizeof(efx));
sdl2_joypad_t *joypad = &g_pads[pad];
if (!joypad->joypad || !joypad->haptic)
return false;
efx.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.length = 5000;
switch (effect)
{
case RETRO_RUMBLE_STRONG: efx.leftright.large_magnitude = strength; break;
case RETRO_RUMBLE_WEAK: efx.leftright.small_magnitude = strength; break;
default: return false;
}
if (joypad->rumble_effect == -1)
{
joypad->rumble_effect = SDL_HapticNewEffect(g_pads[pad].haptic, &efx);
if (joypad->rumble_effect < 0)
{
RARCH_WARN("[SDL]: Failed to create rumble effect for joypad %u: %s\n",
pad, SDL_GetError());
joypad->rumble_effect = -2;
return false;
}
}
else if (joypad->rumble_effect >= 0)
SDL_HapticUpdateEffect(joypad->haptic, joypad->rumble_effect, &efx);
if (joypad->rumble_effect < 0)
return false;
if (SDL_HapticRunEffect(joypad->haptic, joypad->rumble_effect, 1) < 0)
{
RARCH_WARN("[SDL]: Failed to set rumble effect on joypad %u: %s\n",
pad, SDL_GetError());
return false;
}
return true;
}
static bool sdl2_joypad_query_pad(unsigned pad)
{
return pad < MAX_PLAYERS && g_pads[pad].joypad;
}
static const char *sdl2_joypad_name(unsigned pad)
{
if (pad >= MAX_PLAYERS)
return NULL;
return SDL_JoystickName(g_pads[pad].joypad);
}
const rarch_joypad_driver_t sdl2_joypad = {
sdl2_joypad_init,
sdl2_joypad_query_pad,
sdl2_joypad_destroy,
sdl2_joypad_button,
sdl2_joypad_axis,
sdl2_joypad_poll,
sdl2_joypad_set_rumble,
sdl2_joypad_name,
"sdl2",
};

View File

@ -30,7 +30,7 @@ typedef struct sdl_input
int mouse_x, mouse_y;
int mouse_abs_x, mouse_abs_y;
int mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd;
int mouse_l, mouse_r, mouse_m, mouse_wu, mouse_wd, mouse_wl, mouse_wr;
} sdl_input_t;
static void *sdl_input_init(void)
@ -41,6 +41,8 @@ static void *sdl_input_init(void)
return NULL;
sdl->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
RARCH_LOG("[SDL]: Input driver initialized.\n");
return sdl;
}
@ -52,7 +54,12 @@ static bool sdl_key_pressed(int key)
int sym = input_translate_rk_to_keysym((enum retro_key)key);
int num_keys;
Uint8 *keymap = SDL_GetKeyState(&num_keys);
const uint8_t *keymap;
#if HAVE_SDL2
keymap = SDL_GetKeyboardState(&num_keys);
#else
keymap = SDL_GetKeyState(&num_keys);
#endif
if (sym < 0 || sym >= num_keys)
return false;
@ -224,8 +231,12 @@ static void sdl_input_free(void *data)
return;
// Flush out all pending events.
#ifdef HAVE_SDL2
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
#else
SDL_Event event;
while (SDL_PollEvent(&event));
#endif
sdl_input_t *sdl = (sdl_input_t*)data;
@ -235,6 +246,23 @@ static void sdl_input_free(void *data)
free(data);
}
#ifdef HAVE_SDL2
static void sdl_grab_mouse(void *data, bool state)
{
sdl_input_t *sdl = (sdl_input_t*)data;
if (driver.video == &video_sdl2)
{
/* first member of sdl2_video_t is the window */
struct temp{
SDL_Window *w;
};
SDL_SetWindowGrab(((struct temp*)driver.video_data)->w,
state ? SDL_TRUE : SDL_FALSE);
}
}
#endif
static bool sdl_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength)
{
sdl_input_t *sdl = (sdl_input_t*)data;
@ -254,8 +282,10 @@ static void sdl_poll_mouse(sdl_input_t *sdl)
sdl->mouse_l = SDL_BUTTON(SDL_BUTTON_LEFT) & btn ? 1 : 0;
sdl->mouse_r = SDL_BUTTON(SDL_BUTTON_RIGHT) & btn ? 1 : 0;
sdl->mouse_m = SDL_BUTTON(SDL_BUTTON_MIDDLE) & btn ? 1 : 0;
#ifndef HAVE_SDL2
sdl->mouse_wu = SDL_BUTTON(SDL_BUTTON_WHEELUP) & btn ? 1 : 0;
sdl->mouse_wd = SDL_BUTTON(SDL_BUTTON_WHEELDOWN) & btn ? 1 : 0;
#endif
}
static void sdl_input_poll(void *data)
@ -266,6 +296,21 @@ static void sdl_input_poll(void *data)
if (sdl->joypad)
sdl->joypad->poll();
sdl_poll_mouse(sdl);
#ifdef HAVE_SDL2
SDL_Event event;
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEWHEEL, SDL_MOUSEWHEEL) > 0)
{
if (event.type == SDL_MOUSEWHEEL)
{
sdl->mouse_wu = event.wheel.y < 0;
sdl->mouse_wd = event.wheel.y > 0;
sdl->mouse_wl = event.wheel.x < 0;
sdl->mouse_wr = event.wheel.x > 0;
break;
}
}
#endif
}
static uint64_t sdl_get_capabilities(void *data)
@ -293,8 +338,13 @@ const input_driver_t input_sdl = {
NULL,
sdl_get_capabilities,
NULL,
#ifdef HAVE_SDL2
"sdl2",
sdl_grab_mouse,
#else
"sdl",
NULL,
#endif
sdl_set_rumble,
sdl_get_joypad_driver,
};

View File

@ -17,24 +17,105 @@
#include "SDL.h"
#include "../general.h"
struct sdl_joypad
typedef struct _sdl_joypad
{
SDL_Joystick *joypad;
#ifdef HAVE_SDL2
SDL_Haptic *haptic;
int rumble_effect; // -1 = not initialized, -2 = error/unsupported
#endif
unsigned num_axes;
unsigned num_buttons;
unsigned num_hats;
};
#ifdef HAVE_SDL2
unsigned num_balls;
#endif
} sdl_joypad_t;
static struct sdl_joypad g_pads[MAX_PLAYERS];
static sdl_joypad_t g_pads[MAX_PLAYERS];
#ifdef HAVE_SDL2
static bool g_has_haptic;
#endif
static void sdl_joypad_connect(int id)
{
sdl_joypad_t *pad = &g_pads[id];
pad->joypad = SDL_JoystickOpen(id);
if (!pad->joypad)
{
RARCH_ERR("[SDL]: Couldn't open SDL joystick #%u.\n", id);
return;
}
RARCH_LOG("[SDL]: Joypad #%u connected: %s.\n",
#ifdef HAVE_SDL2
id, SDL_JoystickName(pad->joypad));
#else
id, SDL_JoystickName(id));
#endif
#ifdef HAVE_SDL2
pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL;
if (g_has_haptic && !pad->haptic)
RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n",
id, SDL_GetError());
pad->rumble_effect = -1;
if (pad->haptic)
{
SDL_HapticEffect efx;
efx.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.large_magnitude = efx.leftright.small_magnitude = 0x4000;
efx.leftright.length = 5000;
if (SDL_HapticEffectSupported(pad->haptic, &efx) == SDL_FALSE)
{
pad->rumble_effect = -2;
RARCH_WARN("[SDL]: Joypad #%u does not support rumble.\n", id);
}
}
#endif
pad->num_axes = SDL_JoystickNumAxes(pad->joypad);
pad->num_buttons = SDL_JoystickNumButtons(pad->joypad);
pad->num_hats = SDL_JoystickNumHats(pad->joypad);
#ifdef HAVE_SDL2
pad->num_balls = SDL_JoystickNumBalls(pad->joypad);
RARCH_LOG("[SDL]: Joypad #%u has: %u axes, %u buttons, %u hats and %u trackballs.\n",
id, pad->num_axes, pad->num_buttons, pad->num_hats, pad->num_balls);
#else
RARCH_LOG("[SDL]: Joypad #%u has: %u axes, %u buttons, %u hats.\n",
id, pad->num_axes, pad->num_buttons, pad->num_hats);
#endif
}
static void sdl_joypad_disconnect(int id)
{
#ifdef HAVE_SDL2
if (g_pads[id].haptic)
SDL_HapticClose(g_pads[id].haptic);
#endif
if (g_pads[id].joypad)
{
SDL_JoystickClose(g_pads[id].joypad);
RARCH_LOG("[SDL]: Joypad #%u disconnected.\n", id);
}
memset(&g_pads[id], 0, sizeof(g_pads[id]));
}
static void sdl_joypad_destroy(void)
{
unsigned i;
for (i = 0; i < MAX_PLAYERS; i++)
{
if (g_pads[i].joypad)
SDL_JoystickClose(g_pads[i].joypad);
}
sdl_joypad_disconnect(i);
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
memset(g_pads, 0, sizeof(g_pads));
@ -43,38 +124,50 @@ 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 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();
if (num_sticks > MAX_PLAYERS)
num_sticks = MAX_PLAYERS;
for (i = 0; i < num_sticks; i++)
{
struct sdl_joypad *pad = &g_pads[i];
pad->joypad = SDL_JoystickOpen(i);
if (!pad->joypad)
{
RARCH_ERR("Couldn't open SDL joystick #%u.\n", i);
goto error;
}
sdl_joypad_connect(i);
RARCH_LOG("Opened Joystick: %s (#%u).\n",
SDL_JoystickName(i), 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++;
pad->num_axes = SDL_JoystickNumAxes(pad->joypad);
pad->num_buttons = SDL_JoystickNumButtons(pad->joypad);
pad->num_hats = SDL_JoystickNumHats(pad->joypad);
RARCH_LOG("Joypad has: %u axes, %u buttons, %u hats.\n",
pad->num_axes, pad->num_buttons, pad->num_hats);
}
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)
@ -82,7 +175,7 @@ static bool sdl_joypad_button(unsigned port, uint16_t joykey)
if (joykey == NO_BTN)
return false;
const struct sdl_joypad *pad = &g_pads[port];
const sdl_joypad_t *pad = &g_pads[port];
if (!pad->joypad)
return false;
@ -122,7 +215,7 @@ static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis)
if (joyaxis == AXIS_NONE)
return 0;
const struct sdl_joypad *pad = &g_pads[port];
const sdl_joypad_t *pad = &g_pads[port];
if (!pad->joypad)
return false;
@ -149,9 +242,76 @@ static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis)
static void sdl_joypad_poll(void)
{
#ifdef HAVE_SDL2
SDL_Event event;
SDL_PumpEvents();
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0)
{
if (event.type == SDL_JOYDEVICEADDED)
{
sdl_joypad_connect(event.jdevice.which);
}
else if (event.type == SDL_JOYDEVICEREMOVED)
{
sdl_joypad_disconnect(event.jdevice.which);
}
}
#else
SDL_JoystickUpdate();
#endif
}
#ifdef HAVE_SDL2
static bool sdl_joypad_set_rumble(unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
{
SDL_HapticEffect efx;
memset(&efx, 0, sizeof(efx));
sdl_joypad_t *joypad = &g_pads[pad];
if (!joypad->joypad || !joypad->haptic)
return false;
efx.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.length = 5000;
switch (effect)
{
case RETRO_RUMBLE_STRONG: efx.leftright.large_magnitude = strength; break;
case RETRO_RUMBLE_WEAK: efx.leftright.small_magnitude = strength; break;
default: return false;
}
if (joypad->rumble_effect == -1)
{
joypad->rumble_effect = SDL_HapticNewEffect(g_pads[pad].haptic, &efx);
if (joypad->rumble_effect < 0)
{
RARCH_WARN("[SDL]: Failed to create rumble effect for joypad %u: %s\n",
pad, SDL_GetError());
joypad->rumble_effect = -2;
return false;
}
}
else if (joypad->rumble_effect >= 0)
SDL_HapticUpdateEffect(joypad->haptic, joypad->rumble_effect, &efx);
if (joypad->rumble_effect < 0)
return false;
if (SDL_HapticRunEffect(joypad->haptic, joypad->rumble_effect, 1) < 0)
{
RARCH_WARN("[SDL]: Failed to set rumble effect on joypad %u: %s\n",
pad, SDL_GetError());
return false;
}
return true;
}
#endif
static bool sdl_joypad_query_pad(unsigned pad)
{
return pad < MAX_PLAYERS && g_pads[pad].joypad;
@ -162,7 +322,11 @@ static const char *sdl_joypad_name(unsigned pad)
if (pad >= MAX_PLAYERS)
return NULL;
#ifdef HAVE_SDL2
return SDL_JoystickName(g_pads[pad].joypad);
#else
return SDL_JoystickName(pad);
#endif
}
const rarch_joypad_driver_t sdl_joypad = {
@ -172,8 +336,16 @@ const rarch_joypad_driver_t sdl_joypad = {
sdl_joypad_button,
sdl_joypad_axis,
sdl_joypad_poll,
#ifdef HAVE_SDL2
sdl_joypad_set_rumble,
#else
NULL,
#endif
sdl_joypad_name,
"sdl",
#ifdef HAVE_SDL2
"sdl2",
#else
"sdl"
#endif
};