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
|
|
|
|
2017-11-09 22:06:58 +00:00
|
|
|
#include "InputCommon/ControllerInterface/SDL/SDL.h"
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2017-11-09 22:06:58 +00:00
|
|
|
#include <thread>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2017-11-09 22:06:58 +00:00
|
|
|
#include <SDL_events.h>
|
|
|
|
|
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/ScopeGuard.h"
|
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"
|
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
|
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::SDL
|
2010-04-02 02:48:24 +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)
|
|
|
|
return SDL_JoystickNameForIndex(index);
|
|
|
|
#else
|
|
|
|
return SDL_JoystickName(index);
|
|
|
|
#endif
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2017-11-09 22:06:58 +00:00
|
|
|
static void OpenAndAddDevice(int index)
|
2013-10-29 05:23:17 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
SDL_Joystick* const dev = SDL_JoystickOpen(index);
|
2017-11-09 22:06:58 +00:00
|
|
|
if (dev)
|
2013-04-15 02:53:10 +00:00
|
|
|
{
|
2017-11-09 22:06:58 +00:00
|
|
|
auto js = std::make_shared<Joystick>(dev, index);
|
|
|
|
// only add if it has some inputs/outputs
|
|
|
|
if (!js->Inputs().empty() || !js->Outputs().empty())
|
|
|
|
g_controller_interface.AddDevice(std::move(js));
|
2015-01-19 22:05:21 +00:00
|
|
|
}
|
2017-11-09 22:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
static Common::Event s_init_event;
|
|
|
|
static Uint32 s_stop_event_type;
|
|
|
|
static Uint32 s_populate_event_type;
|
|
|
|
static Common::Event s_populated_event;
|
|
|
|
static std::thread s_hotplug_thread;
|
|
|
|
|
|
|
|
static bool HandleEventAndContinue(const SDL_Event& e)
|
|
|
|
{
|
|
|
|
if (e.type == SDL_JOYDEVICEADDED)
|
2015-01-19 22:05:21 +00:00
|
|
|
{
|
2017-11-09 22:06:58 +00:00
|
|
|
OpenAndAddDevice(e.jdevice.which);
|
2015-01-19 22:05:21 +00:00
|
|
|
}
|
2017-11-09 22:06:58 +00:00
|
|
|
else if (e.type == SDL_JOYDEVICEREMOVED)
|
|
|
|
{
|
|
|
|
g_controller_interface.RemoveDevice([&e](const auto* device) {
|
|
|
|
const Joystick* joystick = dynamic_cast<const Joystick*>(device);
|
|
|
|
return joystick && SDL_JoystickInstanceID(joystick->GetSDLJoystick()) == e.jdevice.which;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (e.type == s_populate_event_type)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < SDL_NumJoysticks(); ++i)
|
|
|
|
OpenAndAddDevice(i);
|
|
|
|
s_populated_event.Set();
|
|
|
|
}
|
|
|
|
else if (e.type == s_stop_event_type)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
#if !SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK) != 0)
|
|
|
|
ERROR_LOG(SERIALINTERFACE, "SDL failed to initialize");
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
s_hotplug_thread = std::thread([] {
|
|
|
|
Common::ScopeGuard quit_guard([] {
|
|
|
|
// TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
|
|
|
|
SDL_Quit();
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
Common::ScopeGuard init_guard([] { s_init_event.Set(); });
|
|
|
|
|
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) != 0)
|
|
|
|
{
|
|
|
|
ERROR_LOG(SERIALINTERFACE, "SDL failed to initialize");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
const Uint32 custom_events_start = SDL_RegisterEvents(2);
|
2017-11-09 22:06:58 +00:00
|
|
|
if (custom_events_start == static_cast<Uint32>(-1))
|
|
|
|
{
|
|
|
|
ERROR_LOG(SERIALINTERFACE, "SDL failed to register custom events");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s_stop_event_type = custom_events_start;
|
|
|
|
s_populate_event_type = custom_events_start + 1;
|
|
|
|
|
|
|
|
// Drain all of the events and add the initial joysticks before returning. Otherwise, the
|
|
|
|
// individual joystick events as well as the custom populate event will be handled _after_
|
|
|
|
// ControllerInterface::Init/RefreshDevices has cleared its list of devices, resulting in
|
|
|
|
// duplicate devices.
|
|
|
|
SDL_Event e;
|
|
|
|
while (SDL_PollEvent(&e) != 0)
|
|
|
|
{
|
|
|
|
if (!HandleEventAndContinue(e))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Event e;
|
|
|
|
while (SDL_WaitEvent(&e) != 0)
|
|
|
|
{
|
|
|
|
if (!HandleEventAndContinue(e))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
s_init_event.Wait();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeInit()
|
|
|
|
{
|
|
|
|
#if !SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
SDL_Quit();
|
|
|
|
#else
|
|
|
|
if (!s_hotplug_thread.joinable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_Event stop_event{s_stop_event_type};
|
|
|
|
SDL_PushEvent(&stop_event);
|
|
|
|
|
|
|
|
s_hotplug_thread.join();
|
|
|
|
#endif
|
2016-10-16 20:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PopulateDevices()
|
|
|
|
{
|
2017-11-09 22:06:58 +00:00
|
|
|
#if !SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
if (!SDL_WasInit(SDL_INIT_JOYSTICK))
|
2016-10-16 20:39:05 +00:00
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-01-19 22:05:21 +00:00
|
|
|
for (int i = 0; i < SDL_NumJoysticks(); ++i)
|
2017-11-09 22:06:58 +00:00
|
|
|
OpenAndAddDevice(i);
|
|
|
|
#else
|
|
|
|
if (!s_hotplug_thread.joinable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_Event populate_event{s_populate_event_type};
|
|
|
|
SDL_PushEvent(&populate_event);
|
|
|
|
|
|
|
|
s_populated_event.Wait();
|
|
|
|
#endif
|
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)
|
2017-11-09 22:04:31 +00:00
|
|
|
: m_joystick(joystick), m_name(StripSpaces(GetJoystickName(sdl_index)))
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-06 03:52:11 +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
|
|
|
|
// checking the name is probably good (and hacky) enough
|
2013-04-15 02:53:10 +00:00
|
|
|
// but I'll double check with the num of buttons/axes
|
2010-06-06 06:28:18 +00:00
|
|
|
std::string lcasename = GetName();
|
|
|
|
std::transform(lcasename.begin(), lcasename.end(), lcasename.begin(), tolower);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if ((std::string::npos != lcasename.find("xbox 360")) &&
|
|
|
|
(10 == SDL_JoystickNumButtons(joystick)) && (5 == SDL_JoystickNumAxes(joystick)) &&
|
|
|
|
(1 == SDL_JoystickNumHats(joystick)) && (0 == SDL_JoystickNumBalls(joystick)))
|
2010-06-06 06:28:18 +00:00
|
|
|
{
|
|
|
|
// this device won't be used
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-06 03:52:11 +00:00
|
|
|
|
2014-04-16 04:02:17 +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;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
// get buttons
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 i = 0; i != SDL_JoystickNumButtons(m_joystick); ++i)
|
|
|
|
AddInput(new Button(i, m_joystick));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
// get hats
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 i = 0; i != SDL_JoystickNumHats(m_joystick); ++i)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
// each hat gets 4 input instances associated with it, (up down left right)
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 d = 0; d != 4; ++d)
|
|
|
|
AddInput(new Hat(i, m_joystick, d));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
// get axes
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 i = 0; i != SDL_JoystickNumAxes(m_joystick); ++i)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
// each axis gets a negative and a positive input instance associated with it
|
2013-01-17 21:41:18 +00:00
|
|
|
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
|
|
|
|
m_haptic = SDL_HapticOpenFromJoystick(m_joystick);
|
2019-01-08 16:25:13 +00:00
|
|
|
if (!m_haptic)
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
const unsigned int supported_effects = SDL_HapticQuery(m_haptic);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
// Disable autocenter:
|
|
|
|
if (supported_effects & SDL_HAPTIC_AUTOCENTER)
|
|
|
|
SDL_HapticSetAutocenter(m_haptic, 0);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
// Constant
|
|
|
|
if (supported_effects & SDL_HAPTIC_CONSTANT)
|
|
|
|
AddOutput(new ConstantEffect(m_haptic));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
// Ramp
|
|
|
|
if (supported_effects & SDL_HAPTIC_RAMP)
|
|
|
|
AddOutput(new RampEffect(m_haptic));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
// Periodic
|
|
|
|
for (auto waveform :
|
|
|
|
{SDL_HAPTIC_SINE, SDL_HAPTIC_TRIANGLE, SDL_HAPTIC_SAWTOOTHUP, SDL_HAPTIC_SAWTOOTHDOWN})
|
|
|
|
{
|
|
|
|
if (supported_effects & waveform)
|
|
|
|
AddOutput(new PeriodicEffect(m_haptic, waveform));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
// LeftRight
|
|
|
|
if (supported_effects & SDL_HAPTIC_LEFTRIGHT)
|
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
AddOutput(new LeftRightEffect(m_haptic, LeftRightEffect::Motor::Strong));
|
|
|
|
AddOutput(new LeftRightEffect(m_haptic, LeftRightEffect::Motor::Weak));
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::~Joystick()
|
|
|
|
{
|
|
|
|
#ifdef USE_SDL_HAPTIC
|
2011-03-14 01:20:11 +00:00
|
|
|
if (m_haptic)
|
2013-10-29 05:23:17 +00:00
|
|
|
{
|
2010-04-02 02:48:24 +00:00
|
|
|
// stop/destroy all effects
|
2011-03-14 01:20:11 +00:00
|
|
|
SDL_HapticStopAll(m_haptic);
|
2010-04-02 02:48:24 +00:00
|
|
|
// close haptic first
|
2011-03-14 01:20:11 +00:00
|
|
|
SDL_HapticClose(m_haptic);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// close joystick
|
2011-03-14 01:20:11 +00:00
|
|
|
SDL_JoystickClose(m_joystick);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_SDL_HAPTIC
|
2019-01-08 16:25:13 +00:00
|
|
|
void Joystick::HapticEffect::UpdateEffect()
|
2014-11-13 08:49:18 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
if (m_effect.type != DISABLED_EFFECT_TYPE)
|
2014-11-13 08:49:18 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
if (m_id < 0)
|
|
|
|
{
|
|
|
|
// Upload and try to play the effect.
|
|
|
|
m_id = SDL_HapticNewEffect(m_haptic, &m_effect);
|
|
|
|
|
|
|
|
if (m_id >= 0)
|
|
|
|
SDL_HapticRunEffect(m_haptic, m_id, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Effect is already playing. Update parameters.
|
|
|
|
SDL_HapticUpdateEffect(m_haptic, m_id, &m_effect);
|
|
|
|
}
|
2014-11-13 08:49:18 +00:00
|
|
|
}
|
2019-01-08 16:25:13 +00:00
|
|
|
else if (m_id >= 0)
|
2014-11-13 08:49:18 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
// Stop and remove the effect.
|
2014-11-13 08:49:18 +00:00
|
|
|
SDL_HapticStopEffect(m_haptic, m_id);
|
|
|
|
SDL_HapticDestroyEffect(m_haptic, m_id);
|
|
|
|
m_id = -1;
|
|
|
|
}
|
2019-01-08 16:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::HapticEffect::HapticEffect(SDL_Haptic* haptic) : m_haptic(haptic)
|
|
|
|
{
|
|
|
|
// FYI: type is set within UpdateParameters.
|
|
|
|
m_effect.type = DISABLED_EFFECT_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::HapticEffect::~HapticEffect()
|
|
|
|
{
|
|
|
|
m_effect.type = DISABLED_EFFECT_TYPE;
|
|
|
|
UpdateEffect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::HapticEffect::SetDirection(SDL_HapticDirection* dir)
|
|
|
|
{
|
|
|
|
// Left direction (for wheels)
|
|
|
|
dir->type = SDL_HAPTIC_CARTESIAN;
|
|
|
|
dir->dir[0] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::ConstantEffect::ConstantEffect(SDL_Haptic* haptic) : HapticEffect(haptic)
|
|
|
|
{
|
|
|
|
m_effect.constant = {};
|
|
|
|
SetDirection(&m_effect.constant.direction);
|
|
|
|
m_effect.constant.length = RUMBLE_LENGTH_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::RampEffect::RampEffect(SDL_Haptic* haptic) : HapticEffect(haptic)
|
|
|
|
{
|
|
|
|
m_effect.ramp = {};
|
|
|
|
SetDirection(&m_effect.ramp.direction);
|
|
|
|
m_effect.ramp.length = RUMBLE_LENGTH_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Joystick::PeriodicEffect::PeriodicEffect(SDL_Haptic* haptic, u16 waveform)
|
|
|
|
: HapticEffect(haptic), m_waveform(waveform)
|
|
|
|
{
|
|
|
|
m_effect.periodic = {};
|
|
|
|
SetDirection(&m_effect.periodic.direction);
|
|
|
|
m_effect.periodic.length = RUMBLE_LENGTH_MS;
|
|
|
|
m_effect.periodic.period = RUMBLE_PERIOD_MS;
|
|
|
|
m_effect.periodic.offset = 0;
|
|
|
|
m_effect.periodic.phase = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:13:32 +00:00
|
|
|
Joystick::LeftRightEffect::LeftRightEffect(SDL_Haptic* haptic, Motor motor)
|
|
|
|
: HapticEffect(haptic), m_motor(motor)
|
2019-01-08 16:25:13 +00:00
|
|
|
{
|
|
|
|
m_effect.leftright = {};
|
|
|
|
m_effect.leftright.length = RUMBLE_LENGTH_MS;
|
2014-11-13 08:49:18 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
std::string Joystick::ConstantEffect::GetName() const
|
|
|
|
{
|
|
|
|
return "Constant";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::RampEffect::GetName() const
|
|
|
|
{
|
|
|
|
return "Ramp";
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
std::string Joystick::PeriodicEffect::GetName() const
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
switch (m_waveform)
|
|
|
|
{
|
|
|
|
case SDL_HAPTIC_SINE:
|
|
|
|
return "Sine";
|
|
|
|
case SDL_HAPTIC_TRIANGLE:
|
|
|
|
return "Triangle";
|
|
|
|
case SDL_HAPTIC_SAWTOOTHUP:
|
|
|
|
return "Sawtooth Up";
|
|
|
|
case SDL_HAPTIC_SAWTOOTHDOWN:
|
|
|
|
return "Sawtooth Down";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::LeftRightEffect::GetName() const
|
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
return (Motor::Strong == m_motor) ? "Strong" : "Weak";
|
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
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
// Maximum force value for all SDL effects:
|
|
|
|
constexpr s16 MAX_FORCE_VALUE = 0x7fff;
|
|
|
|
|
|
|
|
if (UpdateParameters(s16(state * MAX_FORCE_VALUE)))
|
2013-04-15 02:53:10 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
UpdateEffect();
|
2013-04-15 02:53:10 +00:00
|
|
|
}
|
2015-01-11 11:42:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
bool Joystick::ConstantEffect::UpdateParameters(s16 value)
|
2015-01-11 11:42:30 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
s16& level = m_effect.constant.level;
|
|
|
|
const s16 old_level = level;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
level = value;
|
|
|
|
|
|
|
|
m_effect.type = level ? SDL_HAPTIC_CONSTANT : DISABLED_EFFECT_TYPE;
|
|
|
|
return level != old_level;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2015-01-06 19:17:43 +00:00
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
bool Joystick::RampEffect::UpdateParameters(s16 value)
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
s16& level = m_effect.ramp.start;
|
|
|
|
const s16 old_level = level;
|
|
|
|
|
|
|
|
level = value;
|
|
|
|
// FYI: Setting end to same as start is odd,
|
|
|
|
// but so is using Ramp effects for rumble simulation.
|
|
|
|
m_effect.ramp.end = level;
|
|
|
|
|
|
|
|
m_effect.type = level ? SDL_HAPTIC_RAMP : DISABLED_EFFECT_TYPE;
|
|
|
|
return level != old_level;
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
bool Joystick::PeriodicEffect::UpdateParameters(s16 value)
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
s16& level = m_effect.periodic.magnitude;
|
|
|
|
const s16 old_level = level;
|
|
|
|
|
|
|
|
level = value;
|
|
|
|
|
|
|
|
m_effect.type = level ? m_waveform : DISABLED_EFFECT_TYPE;
|
|
|
|
return level != old_level;
|
2015-01-06 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 16:25:13 +00:00
|
|
|
bool Joystick::LeftRightEffect::UpdateParameters(s16 value)
|
2015-01-06 19:17:43 +00:00
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
u16& level = (Motor::Strong == m_motor) ? m_effect.leftright.large_magnitude :
|
|
|
|
m_effect.leftright.small_magnitude;
|
2019-01-08 16:25:13 +00:00
|
|
|
const u16 old_level = level;
|
|
|
|
|
|
|
|
level = value;
|
|
|
|
|
|
|
|
m_effect.type = level ? SDL_HAPTIC_LEFTRIGHT : DISABLED_EFFECT_TYPE;
|
|
|
|
return level != old_level;
|
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()
|
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
// TODO: Don't call this for every Joystick, only once per ControllerInterface::UpdateInput()
|
2015-06-08 01:43:39 +00:00
|
|
|
SDL_JoystickUpdate();
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
std::string Joystick::GetName() const
|
|
|
|
{
|
2017-11-09 22:04:31 +00:00
|
|
|
return m_name;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::GetSource() const
|
|
|
|
{
|
|
|
|
return "SDL";
|
|
|
|
}
|
|
|
|
|
2017-11-09 22:06:58 +00:00
|
|
|
SDL_Joystick* Joystick::GetSDLJoystick() const
|
|
|
|
{
|
|
|
|
return m_joystick;
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
std::string Joystick::Button::GetName() const
|
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
return "Button " + std::to_string(m_index);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::Axis::GetName() const
|
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
return "Axis " + std::to_string(m_index) + (m_range < 0 ? '-' : '+');
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Joystick::Hat::GetName() const
|
|
|
|
{
|
2019-01-08 16:25:13 +00:00
|
|
|
return "Hat " + std::to_string(m_index) + ' ' + "NESW"[m_direction];
|
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
|
|
|
{
|
2011-03-14 01:20:11 +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
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
return 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
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return (SDL_JoystickGetHat(m_js, m_index) & (1 << m_direction)) > 0;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::SDL
|