2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-12 17:15:16 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2019-02-02 20:25:48 +00:00
|
|
|
#include <cmath>
|
2014-02-01 22:20:35 +00:00
|
|
|
#include <memory>
|
2016-07-14 15:45:59 +00:00
|
|
|
#include <mutex>
|
2010-06-13 09:26:00 +00:00
|
|
|
#include <string>
|
2019-02-02 20:25:48 +00:00
|
|
|
#include <type_traits>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <vector>
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2020-01-22 00:32:03 +00:00
|
|
|
#include "Common/BitUtils.h"
|
2018-03-30 19:42:26 +00:00
|
|
|
#include "Common/Common.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/IniFile.h"
|
2020-07-05 03:19:26 +00:00
|
|
|
#include "Common/MathUtil.h"
|
2018-12-30 22:06:29 +00:00
|
|
|
#include "InputCommon/ControlReference/ExpressionParser.h"
|
2021-03-21 19:27:00 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
|
2020-09-15 11:34:41 +00:00
|
|
|
#include "InputCommon/ControllerInterface/CoreDevice.h"
|
2017-04-04 19:37:31 +00:00
|
|
|
|
|
|
|
class ControllerInterface;
|
2023-10-01 16:35:01 +00:00
|
|
|
class InputConfig;
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
constexpr const char* DIRECTION_UP = _trans("Up");
|
|
|
|
constexpr const char* DIRECTION_DOWN = _trans("Down");
|
|
|
|
constexpr const char* DIRECTION_LEFT = _trans("Left");
|
|
|
|
constexpr const char* DIRECTION_RIGHT = _trans("Right");
|
|
|
|
|
|
|
|
constexpr const char* named_directions[] = {DIRECTION_UP, DIRECTION_DOWN, DIRECTION_LEFT,
|
|
|
|
DIRECTION_RIGHT};
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
class ControlReference;
|
|
|
|
|
2017-02-09 03:15:43 +00:00
|
|
|
namespace ControllerEmu
|
2011-11-10 08:27:33 +00:00
|
|
|
{
|
2017-02-09 03:15:43 +00:00
|
|
|
class ControlGroup;
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2020-01-22 00:32:03 +00:00
|
|
|
// Represents calibration data found on Wii Remotes + extensions with a zero and a max value.
|
|
|
|
// (e.g. accelerometer data)
|
|
|
|
// Bits of precision specified to handle common situation of differing precision in the actual data.
|
|
|
|
template <typename T, size_t Bits>
|
|
|
|
struct TwoPointCalibration
|
|
|
|
{
|
|
|
|
TwoPointCalibration() = default;
|
|
|
|
TwoPointCalibration(const T& zero_, const T& max_) : zero{zero_}, max{max_} {}
|
|
|
|
|
2020-07-05 03:19:26 +00:00
|
|
|
// Sanity check is that max and zero are not equal.
|
|
|
|
constexpr bool IsSane() const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_arithmetic_v<T>)
|
|
|
|
{
|
|
|
|
return max != zero;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return std::equal(std::begin(max.data), std::end(max.data), std::begin(zero.data),
|
|
|
|
std::not_equal_to<>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 00:32:03 +00:00
|
|
|
static constexpr size_t BITS_OF_PRECISION = Bits;
|
|
|
|
|
|
|
|
T zero;
|
|
|
|
T max;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Represents calibration data with a min, zero, and max value. (e.g. joystick data)
|
|
|
|
template <typename T, size_t Bits>
|
|
|
|
struct ThreePointCalibration
|
|
|
|
{
|
|
|
|
ThreePointCalibration() = default;
|
|
|
|
ThreePointCalibration(const T& min_, const T& zero_, const T& max_)
|
|
|
|
: min{min_}, zero{zero_}, max{max_}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-05 03:19:26 +00:00
|
|
|
// Sanity check is that min and max are on opposite sides of the zero value.
|
|
|
|
constexpr bool IsSane() const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_arithmetic_v<T>)
|
|
|
|
{
|
|
|
|
return MathUtil::Sign(zero - min) * MathUtil::Sign(zero - max) == -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i != std::size(zero.data); ++i)
|
|
|
|
{
|
|
|
|
if (MathUtil::Sign(zero.data[i] - min.data[i]) *
|
|
|
|
MathUtil::Sign(zero.data[i] - max.data[i]) !=
|
|
|
|
-1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 00:32:03 +00:00
|
|
|
static constexpr size_t BITS_OF_PRECISION = Bits;
|
|
|
|
|
|
|
|
T min;
|
|
|
|
T zero;
|
|
|
|
T max;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Represents a raw/uncalibrated N-dimensional value of input data. (e.g. Joystick X and Y)
|
|
|
|
// A normalized value can be calculated with a provided {Two,Three}PointCalibration.
|
|
|
|
// Values are adjusted with mismatched bits of precision.
|
|
|
|
// Underlying type may be an unsigned type or a a Common::TVecN<> of an unsigned type.
|
|
|
|
template <typename T, size_t Bits>
|
|
|
|
struct RawValue
|
|
|
|
{
|
2022-09-23 22:48:26 +00:00
|
|
|
constexpr RawValue() = default;
|
|
|
|
constexpr explicit RawValue(const T& value_) : value{value_} {}
|
2020-01-22 00:32:03 +00:00
|
|
|
|
|
|
|
static constexpr size_t BITS_OF_PRECISION = Bits;
|
|
|
|
|
|
|
|
T value;
|
|
|
|
|
2022-09-23 22:48:47 +00:00
|
|
|
constexpr bool operator==(const RawValue& other) const = default;
|
|
|
|
|
2020-01-22 00:32:03 +00:00
|
|
|
template <typename OtherT, size_t OtherBits>
|
|
|
|
auto GetNormalizedValue(const TwoPointCalibration<OtherT, OtherBits>& calibration) const
|
|
|
|
{
|
|
|
|
const auto value_expansion =
|
|
|
|
std::max(0, int(calibration.BITS_OF_PRECISION) - int(BITS_OF_PRECISION));
|
|
|
|
|
|
|
|
const auto calibration_expansion =
|
|
|
|
std::max(0, int(BITS_OF_PRECISION) - int(calibration.BITS_OF_PRECISION));
|
|
|
|
|
|
|
|
const auto calibration_zero = ExpandValue(calibration.zero, calibration_expansion) * 1.f;
|
|
|
|
const auto calibration_max = ExpandValue(calibration.max, calibration_expansion) * 1.f;
|
|
|
|
|
|
|
|
// Multiplication by 1.f to floatify either a scalar or a Vec.
|
|
|
|
return (ExpandValue(value, value_expansion) * 1.f - calibration_zero) /
|
|
|
|
(calibration_max - calibration_zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OtherT, size_t OtherBits>
|
|
|
|
auto GetNormalizedValue(const ThreePointCalibration<OtherT, OtherBits>& calibration) const
|
|
|
|
{
|
|
|
|
const auto value_expansion =
|
|
|
|
std::max(0, int(calibration.BITS_OF_PRECISION) - int(BITS_OF_PRECISION));
|
|
|
|
|
|
|
|
const auto calibration_expansion =
|
|
|
|
std::max(0, int(BITS_OF_PRECISION) - int(calibration.BITS_OF_PRECISION));
|
|
|
|
|
|
|
|
const auto calibration_min = ExpandValue(calibration.min, calibration_expansion) * 1.f;
|
|
|
|
const auto calibration_zero = ExpandValue(calibration.zero, calibration_expansion) * 1.f;
|
|
|
|
const auto calibration_max = ExpandValue(calibration.max, calibration_expansion) * 1.f;
|
|
|
|
|
|
|
|
const auto use_max = calibration.zero < value;
|
|
|
|
|
|
|
|
// Multiplication by 1.f to floatify either a scalar or a Vec.
|
|
|
|
return (ExpandValue(value, value_expansion) * 1.f - calibration_zero) /
|
|
|
|
(use_max * 1.f * (calibration_max - calibration_zero) +
|
|
|
|
!use_max * 1.f * (calibration_zero - calibration_min));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OtherT>
|
|
|
|
static OtherT ExpandValue(OtherT value, size_t bits)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_arithmetic_v<OtherT>)
|
|
|
|
{
|
|
|
|
return Common::ExpandValue(value, bits);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i != std::size(value.data); ++i)
|
|
|
|
value.data[i] = Common::ExpandValue(value.data[i], bits);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-09 03:15:43 +00:00
|
|
|
class EmulatedController
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-02-09 03:15:43 +00:00
|
|
|
virtual ~EmulatedController();
|
2019-05-01 22:25:48 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
virtual std::string GetName() const = 0;
|
2019-05-01 22:25:48 +00:00
|
|
|
virtual std::string GetDisplayName() const;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-10-01 16:35:01 +00:00
|
|
|
virtual InputConfig* GetConfig() const = 0;
|
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
virtual void LoadDefaults(const ControllerInterface& ciface);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
virtual void LoadConfig(Common::IniFile::Section* sec, const std::string& base = "");
|
|
|
|
virtual void SaveConfig(Common::IniFile::Section* sec, const std::string& base = "");
|
2017-11-04 21:08:26 +00:00
|
|
|
|
2017-02-08 00:25:27 +00:00
|
|
|
bool IsDefaultDeviceConnected() const;
|
2017-11-04 21:08:26 +00:00
|
|
|
const ciface::Core::DeviceQualifier& GetDefaultDevice() const;
|
|
|
|
void SetDefaultDevice(const std::string& device);
|
|
|
|
void SetDefaultDevice(ciface::Core::DeviceQualifier devq);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-03-21 19:27:00 +00:00
|
|
|
void SetInputOverrideFunction(InputOverrideFunction override_func);
|
|
|
|
void ClearInputOverrideFunction();
|
|
|
|
|
2017-02-11 05:29:22 +00:00
|
|
|
void UpdateReferences(const ControllerInterface& devi);
|
2018-12-30 22:06:29 +00:00
|
|
|
void UpdateSingleControlReference(const ControllerInterface& devi, ControlReference* ref);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
// This returns a lock that should be held before calling State() on any control
|
|
|
|
// references and GetState(), by extension. This prevents a race condition
|
|
|
|
// which happens while handling a hotplug event because a control reference's State()
|
|
|
|
// could be called before we have finished updating the reference.
|
2020-02-23 15:34:31 +00:00
|
|
|
[[nodiscard]] static std::unique_lock<std::recursive_mutex> GetStateLock();
|
2021-03-11 22:01:18 +00:00
|
|
|
const ciface::ExpressionParser::ControlEnvironment::VariableContainer&
|
|
|
|
GetExpressionVariables() const;
|
|
|
|
|
|
|
|
// Resets the values while keeping the list.
|
|
|
|
void ResetExpressionVariables();
|
2016-07-14 15:45:59 +00:00
|
|
|
|
2014-02-01 22:20:35 +00:00
|
|
|
std::vector<std::unique_ptr<ControlGroup>> groups;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
// Maps a float from -1.0..+1.0 to an integer in the provided range.
|
2019-02-02 20:25:48 +00:00
|
|
|
template <typename T, typename F>
|
|
|
|
static T MapFloat(F input_value, T zero_value, T neg_1_value = std::numeric_limits<T>::min(),
|
|
|
|
T pos_1_value = std::numeric_limits<T>::max())
|
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>(), "T is only sane for int types.");
|
|
|
|
static_assert(std::is_floating_point<F>(), "F is only sane for float types.");
|
|
|
|
|
2022-09-22 16:30:32 +00:00
|
|
|
static_assert(std::numeric_limits<long long>::min() <= std::numeric_limits<T>::min() &&
|
|
|
|
std::numeric_limits<long long>::max() >= std::numeric_limits<T>::max(),
|
|
|
|
"long long is not a superset of T. use of std::llround is not sane.");
|
2019-02-02 20:25:48 +00:00
|
|
|
|
|
|
|
// Here we round when converting from float to int.
|
|
|
|
// After applying our deadzone, resizing, and reshaping math
|
|
|
|
// we sometimes have a near-zero value which is slightly negative. (e.g. -0.0001)
|
|
|
|
// Casting would round down but rounding will yield our "zero_value".
|
|
|
|
|
|
|
|
if (input_value > 0)
|
2022-09-22 16:30:32 +00:00
|
|
|
return T(std::llround((pos_1_value - zero_value) * input_value + zero_value));
|
2019-02-02 20:25:48 +00:00
|
|
|
else
|
2022-09-22 16:30:32 +00:00
|
|
|
return T(std::llround((zero_value - neg_1_value) * input_value + zero_value));
|
2019-02-02 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
// The inverse of the function above.
|
|
|
|
// Maps an integer in the provided range to a float in the range -1.0..1.0.
|
|
|
|
template <typename F, typename T>
|
|
|
|
static F MapToFloat(T input_value, T zero_value, T neg_1_value = std::numeric_limits<T>::min(),
|
|
|
|
T pos_1_value = std::numeric_limits<T>::max())
|
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>(), "T is only sane for int types.");
|
|
|
|
static_assert(std::is_floating_point<F>(), "F is only sane for float types.");
|
|
|
|
|
|
|
|
if (input_value >= zero_value)
|
|
|
|
return F(input_value - zero_value) / F(pos_1_value - zero_value);
|
|
|
|
else
|
|
|
|
return -F(zero_value - input_value) / F(zero_value - neg_1_value);
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
protected:
|
2021-03-11 22:01:18 +00:00
|
|
|
// TODO: Wiimote attachments actually end up using their parent controller value for this,
|
|
|
|
// so theirs won't be used (and thus shouldn't even exist).
|
2018-12-30 22:06:29 +00:00
|
|
|
ciface::ExpressionParser::ControlEnvironment::VariableContainer m_expression_vars;
|
|
|
|
|
2021-03-21 19:27:00 +00:00
|
|
|
InputOverrideFunction m_input_override_function;
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
void UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env);
|
|
|
|
|
2017-11-04 21:08:26 +00:00
|
|
|
private:
|
|
|
|
ciface::Core::DeviceQualifier m_default_device;
|
2017-02-08 00:25:27 +00:00
|
|
|
bool m_default_device_is_connected{false};
|
2010-06-03 18:05:08 +00:00
|
|
|
};
|
2017-02-09 03:15:43 +00:00
|
|
|
} // namespace ControllerEmu
|