RetroArch/input/drivers_joypad/sdl_joypad.c

471 lines
11 KiB
C
Raw Normal View History

2012-09-28 20:38:42 +00:00
/* RetroArch - A frontend for libretro.
2015-01-07 16:46:50 +00:00
* Copyright (C) 2010-2015 - Hans-Kristian Arntzen
* Copyright (C) 2014-2015 - Higor Euripedes
2012-09-28 20:38:42 +00:00
*
* 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_autodetect.h"
2012-09-28 20:38:42 +00:00
#include "SDL.h"
#include "../../general.h"
2012-09-28 20:38:42 +00:00
2014-08-11 18:47:36 +00:00
typedef struct _sdl_joypad
2012-09-28 20:38:42 +00:00
{
SDL_Joystick *joypad;
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
SDL_GameController *controller;
2014-08-11 18:47:36 +00:00
SDL_Haptic *haptic;
2015-04-02 23:19:51 +00:00
int rumble_effect; /* -1 = not initialized, -2 = error/unsupported */
2014-08-11 18:47:36 +00:00
#endif
2012-09-28 20:38:42 +00:00
unsigned num_axes;
unsigned num_buttons;
unsigned num_hats;
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
unsigned num_balls;
#endif
} sdl_joypad_t;
#ifdef HAVE_SDL2
const int g_subsystem = SDL_INIT_GAMECONTROLLER;
#else
const int g_subsystem = SDL_INIT_JOYSTICK;
#endif
2015-01-05 00:58:00 +00:00
static sdl_joypad_t sdl_pads[MAX_USERS];
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
static bool g_has_haptic;
2014-08-11 18:47:36 +00:00
#endif
2015-04-02 23:19:51 +00:00
static const char* sdl_pad_name(unsigned id)
{
#ifdef HAVE_SDL2
if (sdl_pads[id].controller)
return SDL_GameControllerNameForIndex(id);
2014-08-28 16:09:55 +00:00
return SDL_JoystickNameForIndex(id);
#else
return SDL_JoystickName(id);
#endif
}
2015-04-02 23:19:51 +00:00
static uint8_t sdl_pad_get_button(sdl_joypad_t *pad, unsigned button)
{
#ifdef HAVE_SDL2
/* TODO: see if a LUT like xinput_joypad.c's button_index_to_bitmap_code is needed. */
if (pad->controller)
2014-09-07 01:14:09 +00:00
return SDL_GameControllerGetButton(pad->controller, (SDL_GameControllerButton)button);
#endif
2014-08-28 16:09:55 +00:00
return SDL_JoystickGetButton(pad->joypad, button);
}
2015-04-02 23:19:51 +00:00
static uint8_t sdl_pad_get_hat(sdl_joypad_t *pad, unsigned hat)
{
#ifdef HAVE_SDL2
if (pad->controller)
2015-04-02 23:19:51 +00:00
return sdl_pad_get_button(pad, hat);
#endif
2014-08-28 16:09:55 +00:00
return SDL_JoystickGetHat(pad->joypad, hat);
}
2015-04-02 23:19:51 +00:00
static int16_t sdl_pad_get_axis(sdl_joypad_t *pad, unsigned axis)
{
#ifdef HAVE_SDL2
/* TODO: see if a rarch <-> sdl translation is needed. */
if (pad->controller)
2014-09-07 01:14:09 +00:00
return SDL_GameControllerGetAxis(pad->controller, (SDL_GameControllerAxis)axis);
#endif
2014-08-28 16:09:55 +00:00
return SDL_JoystickGetAxis(pad->joypad, axis);
}
2015-04-02 23:19:51 +00:00
static void sdl_pad_connect(unsigned id)
2014-08-11 18:47:36 +00:00
{
2015-04-02 23:19:51 +00:00
sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[id];
bool success = false;
int32_t product = 0;
int32_t vendor = 0;
settings_t *settings = config_get_ptr();
2015-03-27 15:57:58 +00:00
autoconfig_params_t params = {{0}};
#ifdef HAVE_SDL2
SDL_JoystickGUID guid;
uint16_t *guid_ptr;
if (SDL_IsGameController(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)
2014-08-11 18:47:36 +00:00
{
RARCH_ERR("[SDL]: Couldn't open joystick #%u: %s.\n", id, SDL_GetError());
if (pad->joypad)
SDL_JoystickClose(pad->joypad);
pad->joypad = NULL;
2014-08-11 18:47:36 +00:00
return;
}
2015-04-02 23:19:51 +00:00
strlcpy(settings->input.device_names[id], sdl_pad_name(id), sizeof(settings->input.device_names[id]));
#ifdef HAVE_SDL2
2015-04-02 23:19:51 +00:00
guid = SDL_JoystickGetGUID(pad->joypad);
guid_ptr = (uint16_t*)guid.data;
#ifdef __linux
2015-04-02 23:19:51 +00:00
vendor = guid_ptr[2];
product = guid_ptr[4];
#elif _WIN32
2015-04-02 23:19:51 +00:00
vendor = guid_ptr[0];
product = guid_ptr[1];
#endif
#endif
2015-03-27 15:57:58 +00:00
params.idx = id;
2015-04-02 23:19:51 +00:00
strlcpy(params.name, sdl_pad_name(id), sizeof(params.name));
2015-03-27 15:57:58 +00:00
params.vid = vendor;
params.pid = product;
strlcpy(params.driver, sdl_joypad.ident, sizeof(params.driver));
input_config_autoconfigure_joypad(&params);
RARCH_LOG("[SDL]: Device #%u (%04x:%04x) connected: %s.\n", id, vendor,
2015-04-02 23:19:51 +00:00
product, sdl_pad_name(id));
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
if (pad->controller)
RARCH_LOG("[SDL]: Device #%u supports game controller api.\n", id);
2014-08-11 18:47:36 +00:00
pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL;
2014-08-11 18:47:36 +00:00
if (g_has_haptic && !pad->haptic)
2014-08-11 18:47:36 +00:00
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]: Device #%u does not support rumble.\n", id);
2014-08-11 18:47:36 +00:00
}
}
#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]: Device #%u has: %u axes, %u buttons, %u hats and %u trackballs.\n",
2014-08-11 18:47:36 +00:00
id, pad->num_axes, pad->num_buttons, pad->num_hats, pad->num_balls);
#else
RARCH_LOG("[SDL]: Device #%u has: %u axes, %u buttons, %u hats.\n",
2014-08-11 18:47:36 +00:00
id, pad->num_axes, pad->num_buttons, pad->num_hats);
#endif
}
2012-09-28 20:38:42 +00:00
2015-04-02 23:19:51 +00:00
static void sdl_pad_disconnect(unsigned id)
2014-08-11 18:47:36 +00:00
{
2015-03-20 21:41:15 +00:00
settings_t *settings = config_get_ptr();
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
if (sdl_pads[id].haptic)
SDL_HapticClose(sdl_pads[id].haptic);
2014-08-11 18:47:36 +00:00
if (sdl_pads[id].controller)
{
SDL_GameControllerClose(sdl_pads[id].controller);
input_config_autoconfigure_disconnect(id, sdl_joypad.ident);
}
else
#endif
if (sdl_pads[id].joypad)
2014-08-11 18:47:36 +00:00
{
SDL_JoystickClose(sdl_pads[id].joypad);
input_config_autoconfigure_disconnect(id, sdl_joypad.ident);
2014-08-11 18:47:36 +00:00
}
2015-03-20 21:41:15 +00:00
settings->input.device_names[id][0] = '\0';
memset(&sdl_pads[id], 0, sizeof(sdl_pads[id]));
2014-08-11 18:47:36 +00:00
}
2012-09-28 20:38:42 +00:00
static void sdl_joypad_destroy(void)
{
2013-10-22 19:26:33 +00:00
unsigned i;
2015-01-05 00:58:00 +00:00
for (i = 0; i < MAX_USERS; i++)
2015-04-02 23:19:51 +00:00
sdl_pad_disconnect(i);
2012-09-28 20:38:42 +00:00
SDL_QuitSubSystem(g_subsystem);
memset(sdl_pads, 0, sizeof(sdl_pads));
2012-09-28 20:38:42 +00:00
}
static bool sdl_joypad_init(void *data)
2012-09-28 20:38:42 +00:00
{
2015-04-02 23:19:51 +00:00
unsigned i, num_sticks;
(void)data;
if (SDL_WasInit(0) == 0)
{
if (SDL_Init(g_subsystem) < 0)
return false;
}
else if (SDL_InitSubSystem(g_subsystem) < 0)
2012-09-28 20:38:42 +00:00
return false;
#if HAVE_SDL2
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;
2014-08-11 18:47:36 +00:00
#endif
memset(sdl_pads, 0, sizeof(sdl_pads));
2015-04-02 23:19:51 +00:00
num_sticks = SDL_NumJoysticks();
2015-01-05 00:58:00 +00:00
if (num_sticks > MAX_USERS)
num_sticks = MAX_USERS;
2012-09-28 20:38:42 +00:00
2013-10-22 19:26:33 +00:00
for (i = 0; i < num_sticks; i++)
2015-04-02 23:19:51 +00:00
sdl_pad_connect(i);
2012-09-28 20:38:42 +00:00
#ifndef HAVE_SDL2
/* quit if no joypad is detected. */
num_sticks = 0;
2015-01-05 00:58:00 +00:00
for (i = 0; i < MAX_USERS; i++)
if (sdl_pads[i].joypad)
num_sticks++;
if (num_sticks == 0)
goto error;
#endif
2012-09-28 20:38:42 +00:00
return true;
#ifndef HAVE_SDL2
2012-09-28 20:38:42 +00:00
error:
sdl_joypad_destroy();
return false;
#endif
2012-09-28 20:38:42 +00:00
}
static bool sdl_joypad_button(unsigned port, uint16_t joykey)
{
2015-04-02 23:19:51 +00:00
sdl_joypad_t *pad = NULL;
2012-09-28 20:38:42 +00:00
if (joykey == NO_BTN)
return false;
2015-04-02 23:19:51 +00:00
pad = (sdl_joypad_t*)&sdl_pads[port];
2012-09-28 20:38:42 +00:00
if (!pad->joypad)
return false;
2015-04-02 23:19:51 +00:00
/* Check hat. */
2012-09-28 20:38:42 +00:00
if (GET_HAT_DIR(joykey))
{
2015-04-02 23:19:51 +00:00
uint8_t dir;
2012-09-28 20:38:42 +00:00
uint16_t hat = GET_HAT(joykey);
if (hat >= pad->num_hats)
return false;
2015-04-02 23:19:51 +00:00
dir = sdl_pad_get_hat(pad, hat);
2012-09-28 20:38:42 +00:00
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:
2014-08-28 16:09:55 +00:00
break;
2012-09-28 20:38:42 +00:00
}
return false;
}
2014-08-28 16:09:55 +00:00
2015-04-02 23:19:51 +00:00
/* Check the button */
if (joykey < pad->num_buttons && sdl_pad_get_button(pad, joykey))
2014-08-28 16:09:55 +00:00
return true;
return false;
2012-09-28 20:38:42 +00:00
}
static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis)
{
2015-06-26 16:35:35 +00:00
sdl_joypad_t *pad;
int16_t val;
2012-09-28 20:38:42 +00:00
if (joyaxis == AXIS_NONE)
return 0;
2015-06-26 16:35:35 +00:00
pad = (sdl_joypad_t*)&sdl_pads[port];
2012-09-28 20:38:42 +00:00
if (!pad->joypad)
return false;
2015-06-26 16:35:35 +00:00
val = 0;
2012-09-28 20:38:42 +00:00
if (AXIS_NEG_GET(joyaxis) < pad->num_axes)
{
2015-04-02 23:19:51 +00:00
val = sdl_pad_get_axis(pad, AXIS_NEG_GET(joyaxis));
2012-09-28 20:38:42 +00:00
if (val > 0)
val = 0;
2015-04-02 23:19:51 +00:00
else if (val < -0x7fff) /* -0x8000 can cause trouble if we later abs() it. */
2012-09-28 20:38:42 +00:00
val = -0x7fff;
}
else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
{
2015-04-02 23:19:51 +00:00
val = sdl_pad_get_axis(pad, AXIS_POS_GET(joyaxis));
2012-09-28 20:38:42 +00:00
if (val < 0)
val = 0;
}
return val;
}
static void sdl_joypad_poll(void)
{
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
SDL_Event event;
SDL_PumpEvents();
2015-04-02 23:19:51 +00:00
while (SDL_PeepEvents(&event, 1,
SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0)
2014-08-11 18:47:36 +00:00
{
2015-04-02 23:19:51 +00:00
switch (event.type)
2014-08-11 18:47:36 +00:00
{
2015-04-02 23:19:51 +00:00
case SDL_JOYDEVICEADDED:
sdl_pad_connect(event.jdevice.which);
break;
case SDL_JOYDEVICEREMOVED:
sdl_pad_disconnect(event.jdevice.which);
break;
2014-08-11 18:47:36 +00:00
}
}
#else
2012-09-28 20:38:42 +00:00
SDL_JoystickUpdate();
2014-08-11 18:47:36 +00:00
#endif
}
#ifdef HAVE_SDL2
static bool sdl_joypad_set_rumble(unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
{
2015-06-26 16:35:35 +00:00
sdl_joypad_t *joypad = (sdl_joypad_t*)&sdl_pads[pad];
2014-08-11 18:47:36 +00:00
SDL_HapticEffect efx;
memset(&efx, 0, sizeof(efx));
if (!joypad->joypad || !joypad->haptic)
return false;
2015-04-02 23:19:51 +00:00
efx.type = SDL_HAPTIC_LEFTRIGHT;
efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
2014-08-11 18:47:36 +00:00
efx.leftright.length = 5000;
switch (effect)
{
2015-04-02 23:19:51 +00:00
case RETRO_RUMBLE_STRONG:
efx.leftright.large_magnitude = strength;
break;
case RETRO_RUMBLE_WEAK:
efx.leftright.small_magnitude = strength;
break;
default:
return false;
2014-08-11 18:47:36 +00:00
}
if (joypad->rumble_effect == -1)
{
joypad->rumble_effect = SDL_HapticNewEffect(sdl_pads[pad].haptic, &efx);
2014-08-11 18:47:36 +00:00
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;
2012-09-28 20:38:42 +00:00
}
2014-08-11 18:47:36 +00:00
#endif
2012-09-28 20:38:42 +00:00
static bool sdl_joypad_query_pad(unsigned pad)
{
2015-01-05 00:58:00 +00:00
return pad < MAX_USERS && sdl_pads[pad].joypad;
}
static const char *sdl_joypad_name(unsigned pad)
{
2015-01-05 00:58:00 +00:00
if (pad >= MAX_USERS)
return NULL;
2015-04-02 23:19:51 +00:00
return sdl_pad_name(pad);
}
input_device_driver_t sdl_joypad = {
2012-09-28 20:38:42 +00:00
sdl_joypad_init,
sdl_joypad_query_pad,
2012-09-28 20:38:42 +00:00
sdl_joypad_destroy,
sdl_joypad_button,
2015-02-15 00:57:29 +00:00
NULL,
2012-09-28 20:38:42 +00:00
sdl_joypad_axis,
sdl_joypad_poll,
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
sdl_joypad_set_rumble,
#else
NULL,
2014-08-11 18:47:36 +00:00
#endif
sdl_joypad_name,
2014-08-11 18:47:36 +00:00
#ifdef HAVE_SDL2
"sdl2",
#else
"sdl"
#endif
2012-09-28 20:38:42 +00:00
};