2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2013-06-17 00:07:10 +00:00
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Common/StringUtil.h"
|
2016-06-12 15:08:04 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/SDL/SDL.h"
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef _WIN32
|
2013-10-19 09:27:57 +00:00
|
|
|
#pragma comment(lib, "SDL2.lib")
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace SDL
|
|
|
|
{
|
2015-01-11 11:57:48 +00:00
|
|
|
// 10ms = 100Hz which homebrew docs very roughly imply is within WiiMote normal
|
|
|
|
// range, used for periodic haptic effects though often ignored by devices
|
2015-01-13 16:00:35 +00:00
|
|
|
static const u16 RUMBLE_PERIOD = 10;
|
2016-06-24 08:43:46 +00:00
|
|
|
static const u16 RUMBLE_LENGTH_MAX =
|
|
|
|
500; // ms: enough to span multiple frames at low FPS, but still finite
|
2015-01-11 11:57:48 +00:00
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static std::string GetJoystickName(int index)
|
2013-02-06 03:07:09 +00:00
|
|
|
{
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return SDL_JoystickNameForIndex(index);
|
2013-02-06 03:07:09 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
return SDL_JoystickName(index);
|
2013-02-06 03:07:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-12 15:08:04 +00:00
|
|
|
void Init()
|
2013-10-29 05:23:17 +00:00
|
|
|
{
|
2015-01-19 22:05:21 +00:00
|
|
|
#ifdef USE_SDL_HAPTIC
|
2016-06-24 08:43:46 +00:00
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) >= 0)
|
|
|
|
{
|
|
|
|
// Correctly initialized
|
|
|
|
}
|
|
|
|
else
|
2015-01-19 22:05:21 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
|
|
|
|
{
|
|
|
|
// Failed to initialize
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// joysticks
|
|
|
|
for (int i = 0; i < SDL_NumJoysticks(); ++i)
|
|
|
|
{
|
|
|
|
SDL_Joystick* dev = SDL_JoystickOpen(i);
|
|
|
|
if (dev)
|
|
|
|
{
|
2016-07-14 08:25:52 +00:00
|
|
|
auto js = std::make_shared<Joystick>(dev, i);
|
2016-06-24 08:43:46 +00:00
|
|
|
// only add if it has some inputs/outputs
|
|
|
|
if (js->Inputs().size() || js->Outputs().size())
|
2016-06-25 19:46:39 +00:00
|
|
|
g_controller_interface.AddDevice(std::move(js));
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 08:25:52 +00:00
|
|
|
Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index)
|
|
|
|
: m_joystick(joystick), m_sdl_index(sdl_index)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// really bad HACKS:
|
|
|
|
// to not use SDL for an XInput device
|
|
|
|
// too many people on the forums pick the SDL device and ask:
|
|
|
|
// "why don't my 360 gamepad triggers/rumble work correctly"
|
2010-06-06 06:28:18 +00:00
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
// checking the name is probably good (and hacky) enough
|
|
|
|
// but I'll double check with the num of buttons/axes
|
|
|
|
std::string lcasename = GetName();
|
|
|
|
std::transform(lcasename.begin(), lcasename.end(), lcasename.begin(), tolower);
|
|
|
|
|
|
|
|
if ((std::string::npos != lcasename.find("xbox 360")) &&
|
|
|
|
(10 == SDL_JoystickNumButtons(joystick)) && (5 == SDL_JoystickNumAxes(joystick)) &&
|
|
|
|
(1 == SDL_JoystickNumHats(joystick)) && (0 == SDL_JoystickNumBalls(joystick)))
|
|
|
|
{
|
|
|
|
// this device won't be used
|
|
|
|
return;
|
|
|
|
}
|
2010-06-06 06:28:18 +00:00
|
|
|
#endif
|
2010-06-06 03:52:11 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (SDL_JoystickNumButtons(joystick) > 255 || SDL_JoystickNumAxes(joystick) > 255 ||
|
|
|
|
SDL_JoystickNumHats(joystick) > 255 || SDL_JoystickNumBalls(joystick) > 255)
|
|
|
|
{
|
|
|
|
// This device is invalid, don't use it
|
|
|
|
// Some crazy devices(HP webcam 2100) end up as HID devices
|
|
|
|
// SDL tries parsing these as joysticks
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get buttons
|
|
|
|
for (u8 i = 0; i != SDL_JoystickNumButtons(m_joystick); ++i)
|
|
|
|
AddInput(new Button(i, m_joystick));
|
|
|
|
|
|
|
|
// get hats
|
|
|
|
for (u8 i = 0; i != SDL_JoystickNumHats(m_joystick); ++i)
|
|
|
|
{
|
|
|
|
// each hat gets 4 input instances associated with it, (up down left right)
|
|
|
|
for (u8 d = 0; d != 4; ++d)
|
|
|
|
AddInput(new Hat(i, m_joystick, d));
|
|
|
|
}
|
|
|
|
|
|
|
|
// get axes
|
|
|
|
for (u8 i = 0; i != SDL_JoystickNumAxes(m_joystick); ++i)
|
|
|
|
{
|
|
|
|
// each axis gets a negative and a positive input instance associated with it
|
|
|
|
AddAnalogInputs(new Axis(i, m_joystick, -32768), new Axis(i, m_joystick, 32767));
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
#ifdef USE_SDL_HAPTIC
|
2016-06-24 08:43:46 +00:00
|
|
|
// try to get supported ff effects
|
|
|
|
m_haptic = SDL_HapticOpenFromJoystick(m_joystick);
|
|
|
|
if (m_haptic)
|
|
|
|
{
|
|
|
|
// SDL_HapticSetGain( m_haptic, 1000 );
|
|
|
|
// SDL_HapticSetAutocenter( m_haptic, 0 );
|
|
|
|
|
|
|
|
const unsigned int supported_effects = SDL_HapticQuery(m_haptic);
|
|
|
|
|
|
|
|
// constant effect
|
|
|
|
if (supported_effects & SDL_HAPTIC_CONSTANT)
|
|
|
|
AddOutput(new ConstantEffect(m_haptic));
|
|
|
|
|
|
|
|
// ramp effect
|
|
|
|
if (supported_effects & SDL_HAPTIC_RAMP)
|
|
|
|
AddOutput(new RampEffect(m_haptic));
|
|
|
|
|
|
|
|
// sine effect
|
|
|
|
if (supported_effects & SDL_HAPTIC_SINE)
|
|
|
|
AddOutput(new SineEffect(m_haptic));
|
|
|
|
|
|
|
|
// triangle effect
|
|
|
|
if (supported_effects & SDL_HAPTIC_TRIANGLE)
|
|
|
|
AddOutput(new TriangleEffect(m_haptic));
|
|
|
|
|
|
|
|
// left-right effect
|
|
|
|
if (supported_effects & SDL_HAPTIC_LEFTRIGHT)
|
|
|
|
AddOutput(new LeftRightEffect(m_haptic));
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::~Joystick()
|
|
|
|
{
|
|
|
|
#ifdef USE_SDL_HAPTIC
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_haptic)
|
|
|
|
{
|
|
|
|
// stop/destroy all effects
|
|
|
|
SDL_HapticStopAll(m_haptic);
|
|
|
|
// close haptic first
|
|
|
|
SDL_HapticClose(m_haptic);
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// close joystick
|
|
|
|
SDL_JoystickClose(m_joystick);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_SDL_HAPTIC
|
2014-11-13 08:49:18 +00:00
|
|
|
void Joystick::HapticEffect::Update()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_id == -1 && m_effect.type > 0)
|
|
|
|
{
|
|
|
|
m_id = SDL_HapticNewEffect(m_haptic, &m_effect);
|
|
|
|
if (m_id > -1)
|
|
|
|
SDL_HapticRunEffect(m_haptic, m_id, 1);
|
|
|
|
}
|
|
|
|
else if (m_id > -1 && m_effect.type == 0)
|
|
|
|
{
|
|
|
|
SDL_HapticStopEffect(m_haptic, m_id);
|
|
|
|
SDL_HapticDestroyEffect(m_haptic, m_id);
|
|
|
|
m_id = -1;
|
|
|
|
}
|
|
|
|
else if (m_id > -1)
|
|
|
|
{
|
|
|
|
SDL_HapticUpdateEffect(m_haptic, m_id, &m_effect);
|
|
|
|
}
|
2014-11-13 08:49:18 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
std::string Joystick::ConstantEffect::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return "Constant";
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::RampEffect::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return "Ramp";
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 19:17:43 +00:00
|
|
|
std::string Joystick::SineEffect::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return "Sine";
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::TriangleEffect::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return "Triangle";
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::LeftRightEffect::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return "LeftRight";
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 12:37:06 +00:00
|
|
|
void Joystick::HapticEffect::SetState(ControlState state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
memset(&m_effect, 0, sizeof(m_effect));
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
SetSDLHapticEffect(state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this module uses type==0 to indicate 'off'
|
|
|
|
m_effect.type = 0;
|
|
|
|
}
|
|
|
|
Update();
|
2015-01-11 11:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::ConstantEffect::SetSDLHapticEffect(ControlState state)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_effect.type = SDL_HAPTIC_CONSTANT;
|
|
|
|
m_effect.constant.length = RUMBLE_LENGTH_MAX;
|
|
|
|
m_effect.constant.level = (Sint16)(state * 0x7FFF);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 15:17:29 +00:00
|
|
|
void Joystick::RampEffect::SetSDLHapticEffect(ControlState state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_effect.type = SDL_HAPTIC_RAMP;
|
|
|
|
m_effect.ramp.length = RUMBLE_LENGTH_MAX;
|
|
|
|
m_effect.ramp.start = (Sint16)(state * 0x7FFF);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2015-01-06 19:17:43 +00:00
|
|
|
|
2015-01-08 15:17:29 +00:00
|
|
|
void Joystick::SineEffect::SetSDLHapticEffect(ControlState state)
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_effect.type = SDL_HAPTIC_SINE;
|
|
|
|
m_effect.periodic.period = RUMBLE_PERIOD;
|
|
|
|
m_effect.periodic.magnitude = (Sint16)(state * 0x7FFF);
|
|
|
|
m_effect.periodic.offset = 0;
|
|
|
|
m_effect.periodic.phase = 18000;
|
|
|
|
m_effect.periodic.length = RUMBLE_LENGTH_MAX;
|
|
|
|
m_effect.periodic.delay = 0;
|
|
|
|
m_effect.periodic.attack_length = 0;
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 15:17:29 +00:00
|
|
|
void Joystick::TriangleEffect::SetSDLHapticEffect(ControlState state)
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_effect.type = SDL_HAPTIC_TRIANGLE;
|
|
|
|
m_effect.periodic.period = RUMBLE_PERIOD;
|
|
|
|
m_effect.periodic.magnitude = (Sint16)(state * 0x7FFF);
|
|
|
|
m_effect.periodic.offset = 0;
|
|
|
|
m_effect.periodic.phase = 18000;
|
|
|
|
m_effect.periodic.length = RUMBLE_LENGTH_MAX;
|
|
|
|
m_effect.periodic.delay = 0;
|
|
|
|
m_effect.periodic.attack_length = 0;
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 15:17:29 +00:00
|
|
|
void Joystick::LeftRightEffect::SetSDLHapticEffect(ControlState state)
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_effect.type = SDL_HAPTIC_LEFTRIGHT;
|
|
|
|
m_effect.leftright.length = RUMBLE_LENGTH_MAX;
|
|
|
|
// max ranges tuned to 'feel' similar in magnitude to triangle/sine on xbox360 controller
|
|
|
|
m_effect.leftright.large_magnitude = (Uint16)(state * 0x4000);
|
|
|
|
m_effect.leftright.small_magnitude = (Uint16)(state * 0xFFFF);
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-08 01:43:39 +00:00
|
|
|
void Joystick::UpdateInput()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// each joystick is doin this, o well
|
|
|
|
SDL_JoystickUpdate();
|
2015-06-08 01:43:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
std::string Joystick::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return StripSpaces(GetJoystickName(m_sdl_index));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::GetSource() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return "SDL";
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::Button::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "Button " << (int)m_index;
|
|
|
|
return ss.str();
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::Axis::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "Axis " << (int)m_index << (m_range < 0 ? '-' : '+');
|
|
|
|
return ss.str();
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::Hat::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
static char tmpstr[] = "Hat . .";
|
|
|
|
// I don't think more than 10 hats are supported
|
|
|
|
tmpstr[4] = (char)('0' + m_index);
|
|
|
|
tmpstr[6] = "NESW"[m_direction];
|
|
|
|
return tmpstr;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Joystick::Button::GetState() const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return SDL_JoystickGetButton(m_js, m_index);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Joystick::Axis::GetState() const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return std::max(0.0, ControlState(SDL_JoystickGetAxis(m_js, m_index)) / m_range);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState Joystick::Hat::GetState() const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return (SDL_JoystickGetHat(m_js, m_index) & (1 << m_direction)) > 0;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|