2012-09-28 22:38:42 +02:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2016-01-10 04:33:01 +01:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 13:40:32 +01:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
|
|
|
* Copyright (C) 2014-2017 - Higor Euripedes
|
2017-12-11 23:55:31 -08:00
|
|
|
*
|
2012-09-28 22:38:42 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
2016-09-05 18:31:32 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-09-06 00:56:00 +02:00
|
|
|
#include <compat/strl.h>
|
|
|
|
|
2012-09-28 22:38:42 +02:00
|
|
|
#include "SDL.h"
|
2016-09-05 18:31:32 +02:00
|
|
|
|
|
|
|
#include "../input_driver.h"
|
2017-01-13 05:00:13 +01:00
|
|
|
|
2016-12-01 20:38:20 +01:00
|
|
|
#include "../../tasks/tasks_internal.h"
|
2015-11-23 12:03:38 +01:00
|
|
|
#include "../../verbosity.h"
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2014-08-11 15:47:36 -03:00
|
|
|
typedef struct _sdl_joypad
|
2012-09-28 22:38:42 +02:00
|
|
|
{
|
|
|
|
SDL_Joystick *joypad;
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
2014-08-15 21:22:54 -03:00
|
|
|
SDL_GameController *controller;
|
2014-08-11 15:47:36 -03:00
|
|
|
SDL_Haptic *haptic;
|
2015-04-03 01:19:51 +02:00
|
|
|
int rumble_effect; /* -1 = not initialized, -2 = error/unsupported */
|
2014-08-11 15:47:36 -03:00
|
|
|
#endif
|
2012-09-28 22:38:42 +02:00
|
|
|
unsigned num_axes;
|
|
|
|
unsigned num_buttons;
|
|
|
|
unsigned num_hats;
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
unsigned num_balls;
|
|
|
|
#endif
|
|
|
|
} sdl_joypad_t;
|
|
|
|
|
2020-08-03 17:31:22 +02:00
|
|
|
/* TODO/FIXME - static globals */
|
2015-01-05 01:58:00 +01:00
|
|
|
static sdl_joypad_t sdl_pads[MAX_USERS];
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
2020-08-03 17:31:22 +02:00
|
|
|
static bool g_has_haptic = false;
|
2014-08-11 15:47:36 -03:00
|
|
|
#endif
|
|
|
|
|
2017-01-13 05:00:13 +01:00
|
|
|
static const char *sdl_joypad_name(unsigned pad)
|
2014-08-15 21:22:54 -03:00
|
|
|
{
|
2017-01-13 05:00:13 +01:00
|
|
|
if (pad >= MAX_USERS)
|
|
|
|
return NULL;
|
|
|
|
|
2014-08-15 21:22:54 -03:00
|
|
|
#ifdef HAVE_SDL2
|
2017-01-13 05:00:13 +01:00
|
|
|
if (sdl_pads[pad].controller)
|
|
|
|
return SDL_GameControllerNameForIndex(pad);
|
|
|
|
return SDL_JoystickNameForIndex(pad);
|
2014-08-15 21:22:54 -03:00
|
|
|
#else
|
2017-01-13 05:00:13 +01:00
|
|
|
return SDL_JoystickName(pad);
|
2014-08-15 21:22:54 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
static uint8_t sdl_pad_get_button(sdl_joypad_t *pad, unsigned button)
|
2014-08-15 21:22:54 -03:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SDL2
|
2015-04-03 02:37:10 +02:00
|
|
|
/* TODO: see if a LUT like xinput_joypad.c's button_index_to_bitmap_code is needed. */
|
2014-08-15 21:22:54 -03:00
|
|
|
if (pad->controller)
|
2014-09-06 22:14:09 -03:00
|
|
|
return SDL_GameControllerGetButton(pad->controller, (SDL_GameControllerButton)button);
|
2014-08-15 21:22:54 -03:00
|
|
|
#endif
|
2014-08-28 18:09:55 +02:00
|
|
|
return SDL_JoystickGetButton(pad->joypad, button);
|
2014-08-15 21:22:54 -03:00
|
|
|
}
|
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
static uint8_t sdl_pad_get_hat(sdl_joypad_t *pad, unsigned hat)
|
2014-08-15 21:22:54 -03:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
if (pad->controller)
|
2015-04-03 01:19:51 +02:00
|
|
|
return sdl_pad_get_button(pad, hat);
|
2014-08-15 21:22:54 -03:00
|
|
|
#endif
|
2014-08-28 18:09:55 +02:00
|
|
|
return SDL_JoystickGetHat(pad->joypad, hat);
|
2014-08-15 21:22:54 -03:00
|
|
|
}
|
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
static int16_t sdl_pad_get_axis(sdl_joypad_t *pad, unsigned axis)
|
2014-08-15 21:22:54 -03:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
/* TODO: see if a rarch <-> sdl translation is needed. */
|
|
|
|
if (pad->controller)
|
2014-09-06 22:14:09 -03:00
|
|
|
return SDL_GameControllerGetAxis(pad->controller, (SDL_GameControllerAxis)axis);
|
2014-08-15 21:22:54 -03:00
|
|
|
#endif
|
2014-08-28 18:09:55 +02:00
|
|
|
return SDL_JoystickGetAxis(pad->joypad, axis);
|
2014-08-15 21:22:54 -03:00
|
|
|
}
|
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
static void sdl_pad_connect(unsigned id)
|
2014-08-11 15:47:36 -03:00
|
|
|
{
|
2015-04-03 01:19:51 +02:00
|
|
|
sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[id];
|
|
|
|
bool success = false;
|
|
|
|
int32_t product = 0;
|
|
|
|
int32_t vendor = 0;
|
2014-08-15 21:22:54 -03:00
|
|
|
|
|
|
|
#ifdef HAVE_SDL2
|
2014-11-29 22:11:35 +01:00
|
|
|
SDL_JoystickGUID guid;
|
2019-07-16 14:39:31 +02:00
|
|
|
uint16_t *guid_ptr = NULL;
|
2014-10-01 22:50:42 -03:00
|
|
|
|
2014-08-15 21:22:54 -03:00
|
|
|
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 15:47:36 -03:00
|
|
|
{
|
2014-08-15 21:22:54 -03: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 15:47:36 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-01 22:50:42 -03:00
|
|
|
#ifdef HAVE_SDL2
|
2015-04-03 01:19:51 +02:00
|
|
|
guid = SDL_JoystickGetGUID(pad->joypad);
|
|
|
|
guid_ptr = (uint16_t*)guid.data;
|
2014-10-01 22:50:42 -03:00
|
|
|
#ifdef __linux
|
2015-04-03 01:19:51 +02:00
|
|
|
vendor = guid_ptr[2];
|
|
|
|
product = guid_ptr[4];
|
2014-10-01 22:50:42 -03:00
|
|
|
#elif _WIN32
|
2015-04-03 01:19:51 +02:00
|
|
|
vendor = guid_ptr[0];
|
|
|
|
product = guid_ptr[1];
|
2014-10-01 22:50:42 -03:00
|
|
|
#endif
|
|
|
|
#endif
|
2016-12-01 18:57:44 +01:00
|
|
|
|
2019-07-16 15:28:22 +02:00
|
|
|
input_autoconfigure_connect(
|
2017-01-13 05:00:13 +01:00
|
|
|
sdl_joypad_name(id),
|
2016-12-31 07:43:34 +01:00
|
|
|
NULL,
|
|
|
|
sdl_joypad.ident,
|
|
|
|
id,
|
|
|
|
vendor,
|
2019-07-16 15:28:22 +02:00
|
|
|
product);
|
2014-08-15 22:18:46 -03:00
|
|
|
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
2014-08-15 21:22:54 -03:00
|
|
|
if (pad->controller)
|
2017-06-06 16:16:44 -04:00
|
|
|
{
|
|
|
|
/* SDL_GameController internally supports all axis/button IDs, even if
|
|
|
|
* the controller's mapping does not have a binding for it.
|
|
|
|
*
|
|
|
|
* So, we can claim to support all axes/buttons, and when we try to poll
|
|
|
|
* an unbound ID, SDL simply returns the correct unpressed value.
|
|
|
|
*
|
|
|
|
* Note that, in addition to 0 trackballs, we also have 0 hats. This is
|
|
|
|
* because the d-pad is in the button list, as the last 4 enum entries.
|
|
|
|
*
|
|
|
|
* -flibit
|
|
|
|
*/
|
|
|
|
pad->num_axes = SDL_CONTROLLER_AXIS_MAX;
|
|
|
|
pad->num_buttons = SDL_CONTROLLER_BUTTON_MAX;
|
|
|
|
pad->num_hats = 0;
|
|
|
|
pad->num_balls = 0;
|
|
|
|
|
2020-06-11 04:06:33 +02:00
|
|
|
/* SDL Device supports Game Controller API. */
|
2017-06-06 16:16:44 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2014-08-11 15:47:36 -03:00
|
|
|
|
2020-08-03 17:31:22 +02:00
|
|
|
pad->haptic = NULL;
|
|
|
|
|
|
|
|
if (g_has_haptic)
|
|
|
|
{
|
|
|
|
pad->haptic = SDL_HapticOpenFromJoystick(pad->joypad);
|
2014-08-11 15:47:36 -03:00
|
|
|
|
2020-08-03 17:31:22 +02:00
|
|
|
if (!pad->haptic)
|
|
|
|
RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n",
|
|
|
|
id, SDL_GetError());
|
|
|
|
}
|
2014-08-11 15:47:36 -03:00
|
|
|
|
|
|
|
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;
|
2015-04-14 05:40:36 +02:00
|
|
|
RARCH_WARN("[SDL]: Device #%u does not support rumble.\n", id);
|
2014-08-11 15:47:36 -03:00
|
|
|
}
|
|
|
|
}
|
2017-06-06 16:16:44 -04:00
|
|
|
#else
|
2014-08-11 15:47:36 -03:00
|
|
|
pad->num_axes = SDL_JoystickNumAxes(pad->joypad);
|
|
|
|
pad->num_buttons = SDL_JoystickNumButtons(pad->joypad);
|
|
|
|
pad->num_hats = SDL_JoystickNumHats(pad->joypad);
|
|
|
|
#endif
|
|
|
|
}
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
static void sdl_pad_disconnect(unsigned id)
|
2014-08-11 15:47:36 -03:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SDL2
|
2014-10-27 15:45:18 +01:00
|
|
|
if (sdl_pads[id].haptic)
|
|
|
|
SDL_HapticClose(sdl_pads[id].haptic);
|
2014-08-11 15:47:36 -03:00
|
|
|
|
2014-10-27 15:45:18 +01:00
|
|
|
if (sdl_pads[id].controller)
|
2014-08-15 21:22:54 -03:00
|
|
|
{
|
2014-10-27 15:45:18 +01:00
|
|
|
SDL_GameControllerClose(sdl_pads[id].controller);
|
2016-12-01 18:52:34 +01:00
|
|
|
input_autoconfigure_disconnect(id, sdl_joypad.ident);
|
2014-08-15 21:22:54 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2014-10-27 15:45:18 +01:00
|
|
|
if (sdl_pads[id].joypad)
|
2014-08-11 15:47:36 -03:00
|
|
|
{
|
2014-10-27 15:45:18 +01:00
|
|
|
SDL_JoystickClose(sdl_pads[id].joypad);
|
2016-12-01 18:52:34 +01:00
|
|
|
input_autoconfigure_disconnect(id, sdl_joypad.ident);
|
2014-08-11 15:47:36 -03:00
|
|
|
}
|
|
|
|
|
2014-10-27 15:45:18 +01:00
|
|
|
memset(&sdl_pads[id], 0, sizeof(sdl_pads[id]));
|
2014-08-11 15:47:36 -03:00
|
|
|
}
|
2012-09-28 22:38:42 +02:00
|
|
|
|
|
|
|
static void sdl_joypad_destroy(void)
|
|
|
|
{
|
2013-10-22 21:26:33 +02:00
|
|
|
unsigned i;
|
2020-08-03 17:31:22 +02:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
int subsystem = SDL_INIT_GAMECONTROLLER;
|
|
|
|
#else
|
|
|
|
int subsystem = SDL_INIT_JOYSTICK;
|
|
|
|
#endif
|
2015-01-05 01:58:00 +01:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2015-04-03 01:19:51 +02:00
|
|
|
sdl_pad_disconnect(i);
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2020-08-03 17:31:22 +02:00
|
|
|
SDL_QuitSubSystem(subsystem);
|
2014-10-27 15:45:18 +01:00
|
|
|
memset(sdl_pads, 0, sizeof(sdl_pads));
|
2012-09-28 22:38:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 02:30:47 +02:00
|
|
|
static void *sdl_joypad_init(void *data)
|
2012-09-28 22:38:42 +02:00
|
|
|
{
|
2015-04-03 01:19:51 +02:00
|
|
|
unsigned i, num_sticks;
|
2020-08-03 17:31:22 +02:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
int subsystem = SDL_INIT_GAMECONTROLLER;
|
|
|
|
#else
|
|
|
|
int subsystem = SDL_INIT_JOYSTICK;
|
|
|
|
#endif
|
2015-06-03 18:22:54 +02:00
|
|
|
|
2014-08-15 19:25:27 -03:00
|
|
|
if (SDL_WasInit(0) == 0)
|
|
|
|
{
|
2020-08-03 17:31:22 +02:00
|
|
|
if (SDL_Init(subsystem) < 0)
|
2020-09-22 02:30:47 +02:00
|
|
|
return NULL;
|
2014-08-15 19:25:27 -03:00
|
|
|
}
|
2020-08-03 17:31:22 +02:00
|
|
|
else if (SDL_InitSubSystem(subsystem) < 0)
|
2020-09-22 02:30:47 +02:00
|
|
|
return NULL;
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2014-08-15 19:21:16 -03:00
|
|
|
#if HAVE_SDL2
|
2014-08-11 17:37:53 -03:00
|
|
|
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 15:47:36 -03:00
|
|
|
#endif
|
|
|
|
|
2014-10-27 15:45:18 +01:00
|
|
|
memset(sdl_pads, 0, sizeof(sdl_pads));
|
2014-08-15 21:22:54 -03:00
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
num_sticks = SDL_NumJoysticks();
|
2015-01-05 01:58:00 +01:00
|
|
|
if (num_sticks > MAX_USERS)
|
|
|
|
num_sticks = MAX_USERS;
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2013-10-22 21:26:33 +02:00
|
|
|
for (i = 0; i < num_sticks; i++)
|
2015-04-03 01:19:51 +02:00
|
|
|
sdl_pad_connect(i);
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2014-08-11 17:37:53 -03:00
|
|
|
#ifndef HAVE_SDL2
|
|
|
|
/* quit if no joypad is detected. */
|
|
|
|
num_sticks = 0;
|
2015-01-05 01:58:00 +01:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-10-27 15:45:18 +01:00
|
|
|
if (sdl_pads[i].joypad)
|
2014-08-11 17:37:53 -03:00
|
|
|
num_sticks++;
|
|
|
|
|
|
|
|
if (num_sticks == 0)
|
|
|
|
goto error;
|
|
|
|
#endif
|
|
|
|
|
2020-09-22 02:30:47 +02:00
|
|
|
return (void*)-1;
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2014-08-11 17:37:53 -03:00
|
|
|
#ifndef HAVE_SDL2
|
2012-09-28 22:38:42 +02:00
|
|
|
error:
|
|
|
|
sdl_joypad_destroy();
|
2020-09-22 02:30:47 +02:00
|
|
|
|
|
|
|
return NULL;
|
2014-08-11 17:37:53 -03:00
|
|
|
#endif
|
2012-09-28 22:38:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:18:12 +02:00
|
|
|
static int16_t sdl_joypad_button_state(
|
|
|
|
sdl_joypad_t *pad,
|
|
|
|
unsigned port, uint16_t joykey)
|
|
|
|
{
|
2020-07-19 18:51:05 +02:00
|
|
|
unsigned hat_dir = GET_HAT_DIR(joykey);
|
2020-07-19 03:18:12 +02:00
|
|
|
/* Check hat. */
|
|
|
|
if (hat_dir)
|
|
|
|
{
|
|
|
|
uint8_t dir;
|
2020-07-19 18:51:05 +02:00
|
|
|
uint16_t hat = GET_HAT(joykey);
|
2020-07-19 03:18:12 +02:00
|
|
|
|
|
|
|
if (hat >= pad->num_hats)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dir = sdl_pad_get_hat(pad, hat);
|
|
|
|
|
|
|
|
switch (hat_dir)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* hat requested and no hat button down */
|
|
|
|
}
|
|
|
|
else if (joykey < pad->num_buttons)
|
|
|
|
return sdl_pad_get_button(pad, joykey);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:07:57 +02:00
|
|
|
static int16_t sdl_joypad_button(unsigned port, uint16_t joykey)
|
2012-09-28 22:38:42 +02:00
|
|
|
{
|
2020-07-18 19:51:14 +02:00
|
|
|
sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[port];
|
2016-12-11 08:44:55 +01:00
|
|
|
if (!pad || !pad->joypad)
|
2020-07-18 18:07:57 +02:00
|
|
|
return 0;
|
2020-07-18 19:51:14 +02:00
|
|
|
if (port >= DEFAULT_MAX_PADS)
|
|
|
|
return 0;
|
2020-07-19 03:18:12 +02:00
|
|
|
return sdl_joypad_button_state(pad, port, joykey);
|
2012-09-28 22:38:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:18:12 +02:00
|
|
|
static int16_t sdl_joypad_axis_state(
|
|
|
|
sdl_joypad_t *pad,
|
|
|
|
unsigned port, uint32_t joyaxis)
|
2012-09-28 22:38:42 +02:00
|
|
|
{
|
|
|
|
if (AXIS_NEG_GET(joyaxis) < pad->num_axes)
|
|
|
|
{
|
2020-07-19 18:51:05 +02:00
|
|
|
int16_t val = sdl_pad_get_axis(pad, AXIS_NEG_GET(joyaxis));
|
2020-07-19 03:18:12 +02:00
|
|
|
/* -0x8000 can cause trouble if we later abs() it. */
|
2020-07-19 18:51:05 +02:00
|
|
|
if (val < -0x7fff)
|
|
|
|
return -0x7fff;
|
|
|
|
else if (val < 0)
|
|
|
|
return val;
|
2012-09-28 22:38:42 +02:00
|
|
|
}
|
|
|
|
else if (AXIS_POS_GET(joyaxis) < pad->num_axes)
|
|
|
|
{
|
2020-07-19 18:51:05 +02:00
|
|
|
int16_t val = sdl_pad_get_axis(pad, AXIS_POS_GET(joyaxis));
|
|
|
|
if (val > 0)
|
|
|
|
return val;
|
2012-09-28 22:38:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 18:51:05 +02:00
|
|
|
return 0;
|
2012-09-28 22:38:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:18:12 +02:00
|
|
|
static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis)
|
|
|
|
{
|
|
|
|
sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[port];
|
|
|
|
if (!pad || !pad->joypad)
|
|
|
|
return false;
|
|
|
|
return sdl_joypad_axis_state(pad, port, joyaxis);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int16_t sdl_joypad_state(
|
|
|
|
rarch_joypad_info_t *joypad_info,
|
|
|
|
const struct retro_keybind *binds,
|
|
|
|
unsigned port)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
int16_t ret = 0;
|
2020-07-29 05:31:23 +02:00
|
|
|
uint16_t port_idx = joypad_info->joy_idx;
|
|
|
|
sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[port_idx];
|
2020-07-19 03:18:12 +02:00
|
|
|
|
|
|
|
if (!pad || !pad->joypad)
|
|
|
|
return 0;
|
2020-07-29 05:31:23 +02:00
|
|
|
if (port_idx >= DEFAULT_MAX_PADS)
|
2020-07-19 03:18:12 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
|
|
|
{
|
|
|
|
/* Auto-binds are per joypad, not per user. */
|
|
|
|
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
|
|
|
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
|
|
|
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
|
|
|
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
|
|
|
if (
|
|
|
|
(uint16_t)joykey != NO_BTN
|
2020-07-29 05:31:23 +02:00
|
|
|
&& sdl_joypad_button_state(pad, port_idx, (uint16_t)joykey)
|
2020-07-19 03:18:12 +02:00
|
|
|
)
|
|
|
|
ret |= ( 1 << i);
|
|
|
|
else if (joyaxis != AXIS_NONE &&
|
2020-07-29 05:31:23 +02:00
|
|
|
((float)abs(sdl_joypad_axis_state(pad, port_idx, joyaxis))
|
2020-07-19 03:18:12 +02:00
|
|
|
/ 0x8000) > joypad_info->axis_threshold)
|
|
|
|
ret |= (1 << i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-28 22:38:42 +02:00
|
|
|
static void sdl_joypad_poll(void)
|
|
|
|
{
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
SDL_PumpEvents();
|
2015-04-03 01:19:51 +02:00
|
|
|
|
|
|
|
while (SDL_PeepEvents(&event, 1,
|
|
|
|
SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0)
|
2014-08-11 15:47:36 -03:00
|
|
|
{
|
2015-04-03 01:19:51 +02:00
|
|
|
switch (event.type)
|
2014-08-11 15:47:36 -03:00
|
|
|
{
|
2015-04-03 01:19:51 +02:00
|
|
|
case SDL_JOYDEVICEADDED:
|
|
|
|
sdl_pad_connect(event.jdevice.which);
|
|
|
|
break;
|
|
|
|
case SDL_JOYDEVICEREMOVED:
|
|
|
|
sdl_pad_disconnect(event.jdevice.which);
|
|
|
|
break;
|
2014-08-11 15:47:36 -03:00
|
|
|
}
|
|
|
|
}
|
2019-01-01 17:11:16 -08:00
|
|
|
|
|
|
|
SDL_FlushEvents(SDL_JOYAXISMOTION, SDL_CONTROLLERDEVICEREMAPPED);
|
2014-08-11 15:47:36 -03:00
|
|
|
#else
|
2012-09-28 22:38:42 +02:00
|
|
|
SDL_JoystickUpdate();
|
2014-08-11 15:47:36 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
static bool sdl_joypad_set_rumble(unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
|
|
|
|
{
|
2016-12-11 07:28:33 +01:00
|
|
|
SDL_HapticEffect efx;
|
2015-06-26 18:35:35 +02:00
|
|
|
sdl_joypad_t *joypad = (sdl_joypad_t*)&sdl_pads[pad];
|
|
|
|
|
2014-08-11 15:47:36 -03:00
|
|
|
memset(&efx, 0, sizeof(efx));
|
|
|
|
|
|
|
|
if (!joypad->joypad || !joypad->haptic)
|
|
|
|
return false;
|
|
|
|
|
2015-04-03 01:19:51 +02:00
|
|
|
efx.type = SDL_HAPTIC_LEFTRIGHT;
|
|
|
|
efx.leftright.type = SDL_HAPTIC_LEFTRIGHT;
|
2014-08-11 15:47:36 -03:00
|
|
|
efx.leftright.length = 5000;
|
|
|
|
|
|
|
|
switch (effect)
|
|
|
|
{
|
2015-04-03 01:19:51 +02: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 15:47:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (joypad->rumble_effect == -1)
|
|
|
|
{
|
2014-10-27 15:45:18 +01:00
|
|
|
joypad->rumble_effect = SDL_HapticNewEffect(sdl_pads[pad].haptic, &efx);
|
2014-08-11 15:47:36 -03: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 22:38:42 +02:00
|
|
|
}
|
2014-08-11 15:47:36 -03:00
|
|
|
#endif
|
2012-09-28 22:38:42 +02:00
|
|
|
|
2012-09-29 21:57:03 +02:00
|
|
|
static bool sdl_joypad_query_pad(unsigned pad)
|
|
|
|
{
|
2015-01-05 01:58:00 +01:00
|
|
|
return pad < MAX_USERS && sdl_pads[pad].joypad;
|
2012-09-29 21:57:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 16:37:59 +02:00
|
|
|
input_device_driver_t sdl_joypad = {
|
2012-09-28 22:38:42 +02:00
|
|
|
sdl_joypad_init,
|
2012-09-29 21:57:03 +02:00
|
|
|
sdl_joypad_query_pad,
|
2012-09-28 22:38:42 +02:00
|
|
|
sdl_joypad_destroy,
|
|
|
|
sdl_joypad_button,
|
2020-07-19 03:18:12 +02:00
|
|
|
sdl_joypad_state,
|
2015-02-15 01:57:29 +01:00
|
|
|
NULL,
|
2012-09-28 22:38:42 +02:00
|
|
|
sdl_joypad_axis,
|
|
|
|
sdl_joypad_poll,
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
sdl_joypad_set_rumble,
|
|
|
|
#else
|
2013-09-25 22:59:05 +02:00
|
|
|
NULL,
|
2014-08-11 15:47:36 -03:00
|
|
|
#endif
|
2013-04-26 13:25:40 +02:00
|
|
|
sdl_joypad_name,
|
2014-08-11 15:47:36 -03:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
"sdl2",
|
|
|
|
#else
|
|
|
|
"sdl"
|
|
|
|
#endif
|
2012-09-28 22:38:42 +02:00
|
|
|
};
|