| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <atomic> |
| 8 |
#include <chrono> |
| 9 |
#include <memory> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include "Core/HW/WiimoteCommon/DataReport.h" |
| 13 |
#include "Core/HW/WiimoteCommon/WiimoteReport.h" |
| 14 |
#include "Core/HW/WiimoteEmu/Camera.h" |
| 15 |
#include "Core/HW/WiimoteEmu/Extension/Classic.h" |
| 16 |
#include "Core/HW/WiimoteEmu/Extension/Nunchuk.h" |
| 17 |
#include "Core/HW/WiimoteEmu/MotionPlus.h" |
| 18 |
#include "Core/HW/WiimoteReal/WiimoteReal.h" |
| 19 |
#include "InputCommon/ControllerInterface/CoreDevice.h" |
| 20 |
|
| 21 |
namespace ciface::WiimoteController |
| 22 |
{ |
| 23 |
using namespace WiimoteCommon; |
| 24 |
|
| 25 |
void AddDevice(std::unique_ptr<WiimoteReal::Wiimote>); |
| 26 |
void ReleaseDevices(std::optional<u32> count = std::nullopt); |
| 27 |
|
| 28 |
class Device final : public Core::Device |
| 29 |
{ |
| 30 |
public: |
| 31 |
Device(std::unique_ptr<WiimoteReal::Wiimote> wiimote); |
| 32 |
~Device() override; |
| 33 |
|
| 34 |
std::string GetName() const override; |
| 35 |
std::string GetSource() const override; |
| 36 |
int GetSortPriority() const override; |
| 37 |
|
| 38 |
Core::DeviceRemoval UpdateInput() override; |
| 39 |
|
| 40 |
private: |
| 41 |
using Clock = std::chrono::steady_clock; |
| 42 |
|
| 43 |
enum class ExtensionID |
| 44 |
{ |
| 45 |
Nunchuk, |
| 46 |
Classic, |
| 47 |
Unsupported, |
| 48 |
}; |
| 49 |
|
| 50 |
enum class Checksum |
| 51 |
{ |
| 52 |
Good, |
| 53 |
Bad, |
| 54 |
}; |
| 55 |
|
| 56 |
class MotionPlusState |
| 57 |
{ |
| 58 |
public: |
| 59 |
void SetCalibrationData(const WiimoteEmu::MotionPlus::CalibrationData&); |
| 60 |
void ProcessData(const WiimoteEmu::MotionPlus::DataFormat&); |
| 61 |
|
| 62 |
using PassthroughMode = WiimoteEmu::MotionPlus::PassthroughMode; |
| 63 |
|
| 64 |
|
| 65 |
std::optional<PassthroughMode> current_mode; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
std::optional<bool> passthrough_port; |
| 70 |
|
| 71 |
Common::Vec3 gyro_data = {}; |
| 72 |
|
| 73 |
std::optional<WiimoteEmu::MotionPlus::CalibrationBlocks> calibration; |
| 74 |
}; |
| 75 |
|
| 76 |
struct NunchukState |
| 77 |
{ |
| 78 |
using CalibrationData = WiimoteEmu::Nunchuk::CalibrationData; |
| 79 |
|
| 80 |
void SetCalibrationData(const CalibrationData&, Checksum); |
| 81 |
void ProcessData(const WiimoteEmu::Nunchuk::DataFormat&); |
| 82 |
|
| 83 |
Common::Vec2 stick = {}; |
| 84 |
Common::Vec3 accel = {}; |
| 85 |
|
| 86 |
u8 buttons = 0; |
| 87 |
|
| 88 |
struct Calibration |
| 89 |
{ |
| 90 |
Calibration(); |
| 91 |
|
| 92 |
CalibrationData::AccelCalibration accel; |
| 93 |
CalibrationData::StickCalibration stick; |
| 94 |
}; |
| 95 |
|
| 96 |
std::optional<Calibration> calibration; |
| 97 |
}; |
| 98 |
|
| 99 |
struct ClassicState |
| 100 |
{ |
| 101 |
using CalibrationData = WiimoteEmu::Classic::CalibrationData; |
| 102 |
|
| 103 |
void SetCalibrationData(const CalibrationData&, Checksum); |
| 104 |
void ProcessData(const WiimoteEmu::Classic::DataFormat&); |
| 105 |
|
| 106 |
std::array<Common::Vec2, 2> sticks = {}; |
| 107 |
std::array<float, 2> triggers = {}; |
| 108 |
|
| 109 |
u16 buttons = 0; |
| 110 |
|
| 111 |
struct Calibration |
| 112 |
{ |
| 113 |
Calibration(); |
| 114 |
|
| 115 |
CalibrationData::StickCalibration left_stick; |
| 116 |
CalibrationData::StickCalibration right_stick; |
| 117 |
|
| 118 |
CalibrationData::TriggerCalibration left_trigger; |
| 119 |
CalibrationData::TriggerCalibration right_trigger; |
| 120 |
}; |
| 121 |
|
| 122 |
std::optional<Calibration> calibration; |
| 123 |
}; |
| 124 |
|
| 125 |
struct IRState |
| 126 |
{ |
| 127 |
static u32 GetDesiredIRSensitivity(); |
| 128 |
|
| 129 |
void ProcessData(const DataReportManipulator& manipulator); |
| 130 |
bool IsFullyConfigured() const; |
| 131 |
|
| 132 |
u32 current_sensitivity = u32(-1); |
| 133 |
bool enabled = false; |
| 134 |
bool mode_set = false; |
| 135 |
|
| 136 |
|
| 137 |
Common::Vec2 center_position = {}; |
| 138 |
|
| 139 |
float distance = 0; |
| 140 |
|
| 141 |
bool is_hidden = true; |
| 142 |
|
| 143 |
std::array<Common::Vec2, 4> raw_ir_object_position; |
| 144 |
std::array<float, 4> raw_ir_object_size; |
| 145 |
}; |
| 146 |
|
| 147 |
class ReportHandler |
| 148 |
{ |
| 149 |
public: |
| 150 |
enum class HandlerResult |
| 151 |
{ |
| 152 |
Handled, |
| 153 |
NotHandled, |
| 154 |
}; |
| 155 |
|
| 156 |
ReportHandler(Clock::time_point expired_time); |
| 157 |
|
| 158 |
template <typename R, typename T> |
| 159 |
void AddHandler(std::function<R(const T&)>); |
| 160 |
|
| 161 |
HandlerResult TryToHandleReport(const WiimoteReal::Report& report); |
| 162 |
|
| 163 |
bool IsExpired() const; |
| 164 |
|
| 165 |
private: |
| 166 |
const Clock::time_point m_expired_time; |
| 167 |
std::vector<std::function<HandlerResult(const WiimoteReal::Report& report)>> m_callbacks; |
| 168 |
}; |
| 169 |
|
| 170 |
using AckReportHandler = std::function<ReportHandler::HandlerResult(const InputReportAck& reply)>; |
| 171 |
|
| 172 |
static AckReportHandler MakeAckHandler(OutputReportID report_id, |
| 173 |
std::function<void(WiimoteCommon::ErrorCode)> callback); |
| 174 |
|
| 175 |
|
| 176 |
void ProcessInputReport(WiimoteReal::Report& report); |
| 177 |
void ProcessMotionPlusExtensionData(const u8* data, u32 size); |
| 178 |
void ProcessNormalExtensionData(const u8* data, u32 size); |
| 179 |
void ProcessExtensionEvent(bool connected); |
| 180 |
void ProcessExtensionID(u8 id_0, u8 id_4, u8 id_5); |
| 181 |
void ProcessStatusReport(const InputReportStatus&); |
| 182 |
|
| 183 |
void RunTasks(); |
| 184 |
|
| 185 |
bool IsPerformingTask() const; |
| 186 |
|
| 187 |
template <typename T> |
| 188 |
void QueueReport(T&& report, std::function<void(ErrorCode)> ack_callback = {}); |
| 189 |
|
| 190 |
template <typename... T> |
| 191 |
void AddReportHandler(T&&... callbacks); |
| 192 |
|
| 193 |
using ReadResponse = std::optional<std::vector<u8>>; |
| 194 |
|
| 195 |
void ReadData(AddressSpace space, u8 slave, u16 address, u16 size, |
| 196 |
std::function<void(ReadResponse)> callback); |
| 197 |
|
| 198 |
void AddReadDataReplyHandler(AddressSpace space, u8 slave, u16 address, u16 size, |
| 199 |
std::vector<u8> starting_data, |
| 200 |
std::function<void(ReadResponse)> callback); |
| 201 |
|
| 202 |
template <typename T = std::initializer_list<u8>, typename C> |
| 203 |
void WriteData(AddressSpace space, u8 slave, u16 address, T&& data, C&& callback); |
| 204 |
|
| 205 |
void ReadActiveExtensionID(); |
| 206 |
void SetIRSensitivity(u32 level); |
| 207 |
void ConfigureSpeaker(); |
| 208 |
void ConfigureIRCamera(); |
| 209 |
|
| 210 |
u8 GetDesiredLEDValue() const; |
| 211 |
|
| 212 |
void TriggerMotionPlusModeChange(); |
| 213 |
void TriggerMotionPlusCalibration(); |
| 214 |
|
| 215 |
bool IsMotionPlusStateKnown() const; |
| 216 |
bool IsMotionPlusActive() const; |
| 217 |
bool IsMotionPlusInDesiredMode() const; |
| 218 |
|
| 219 |
bool IsWaitingForMotionPlus() const; |
| 220 |
void WaitForMotionPlus(); |
| 221 |
void HandleMotionPlusNonResponse(); |
| 222 |
|
| 223 |
void UpdateRumble(); |
| 224 |
void UpdateOrientation(); |
| 225 |
void UpdateExtensionNumberInput(); |
| 226 |
|
| 227 |
std::unique_ptr<WiimoteReal::Wiimote> m_wiimote; |
| 228 |
|
| 229 |
|
| 230 |
DataReportManipulator::CoreData m_core_data = {}; |
| 231 |
|
| 232 |
|
| 233 |
Common::Vec3 m_accel_data = {}; |
| 234 |
std::optional<AccelCalibrationData::Calibration> m_accel_calibration; |
| 235 |
|
| 236 |
|
| 237 |
Common::Vec3 m_rotation_inputs = {}; |
| 238 |
|
| 239 |
MotionPlusState m_mplus_state = {}; |
| 240 |
NunchukState m_nunchuk_state = {}; |
| 241 |
ClassicState m_classic_state = {}; |
| 242 |
IRState m_ir_state = {}; |
| 243 |
|
| 244 |
|
| 245 |
Clock::time_point m_mplus_wait_time = Clock::now(); |
| 246 |
|
| 247 |
|
| 248 |
std::optional<MotionPlusState::PassthroughMode> m_mplus_desired_mode; |
| 249 |
|
| 250 |
|
| 251 |
Clock::time_point m_status_outdated_time = Clock::now(); |
| 252 |
float m_battery = 0; |
| 253 |
u8 m_leds = 0; |
| 254 |
|
| 255 |
bool m_speaker_configured = false; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
std::optional<bool> m_extension_port; |
| 260 |
|
| 261 |
|
| 262 |
std::optional<ExtensionID> m_extension_id; |
| 263 |
|
| 264 |
|
| 265 |
bool m_rumble = false; |
| 266 |
|
| 267 |
|
| 268 |
std::atomic<ControlState> m_rumble_level; |
| 269 |
Clock::time_point m_last_rumble_change = Clock::now(); |
| 270 |
|
| 271 |
|
| 272 |
InputReportID m_reporting_mode = InputReportID::ReportDisabled; |
| 273 |
|
| 274 |
|
| 275 |
WiimoteEmu::ExtensionNumber m_extension_number_input = WiimoteEmu::ExtensionNumber::NONE; |
| 276 |
bool m_mplus_attached_input = false; |
| 277 |
|
| 278 |
|
| 279 |
std::list<ReportHandler> m_report_handlers; |
| 280 |
|
| 281 |
|
| 282 |
Common::Quaternion m_orientation = Common::Quaternion::Identity(); |
| 283 |
Clock::time_point m_last_report_time = Clock::now(); |
| 284 |
}; |
| 285 |
|
| 286 |
} |