#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "device/gamepad/nintendo_controller.h"
#include <algorithm>
#include <utility>
#include "base/functional/bind.h"
#include "base/ranges/algorithm.h"
#include "base/task/single_thread_task_runner.h"
#include "device/gamepad/gamepad_data_fetcher.h"
#include "device/gamepad/gamepad_id_list.h"
namespace device {
namespace {
const uint16_t kVendorNintendo = …;
const uint16_t kProductSwitchChargingGrip = …;
const size_t kSwitchProMaxOutputReportSizeBytesUsb = …;
const size_t kSwitchProMaxOutputReportSizeBytesBluetooth = …;
const size_t kMaxInputReportSizeBytes = …;
const char kProductNameSwitchCompositeDevice[] = …;
const uint8_t kReportIdOutput01 = …;
const uint8_t kReportIdOutput10 = …;
const uint8_t kReportIdInput21 = …;
const uint8_t kReportIdInput30 = …;
const uint8_t kUsbReportIdOutput80 = …;
const uint8_t kUsbReportIdInput81 = …;
const uint8_t kSubTypeRequestMac = …;
const uint8_t kSubTypeHandshake = …;
const uint8_t kSubTypeBaudRate = …;
const uint8_t kSubTypeDisableUsbTimeout = …;
const uint8_t kSubTypeEnableUsbTimeout = …;
const uint8_t kSubCommandSetInputReportMode = …;
const uint8_t kSubCommandReadSpi = …;
const uint8_t kSubCommandSetPlayerLights = …;
const uint8_t kSubCommand33 = …;
const uint8_t kSubCommandEnableImu = …;
const uint8_t kSubCommandSetImuSensitivity = …;
const uint8_t kSubCommandEnableVibration = …;
const uint16_t kSpiImuCalibrationAddress = …;
const size_t kSpiImuCalibrationSize = …;
const uint16_t kSpiAnalogStickCalibrationAddress = …;
const size_t kSpiAnalogStickCalibrationSize = …;
const uint16_t kSpiImuHorizontalOffsetsAddress = …;
const size_t kSpiImuHorizontalOffsetsSize = …;
const uint16_t kSpiAnalogStickParametersAddress = …;
const size_t kSpiAnalogStickParametersSize = …;
const size_t kSubCommandDataOffset = …;
const size_t kSpiDataOffset = …;
const uint8_t kUsbDeviceTypeChargingGripNoDevice = …;
const uint8_t kUsbDeviceTypeChargingGripJoyConL = …;
const uint8_t kUsbDeviceTypeChargingGripJoyConR = …;
const uint8_t kUsbDeviceTypeProController = …;
const base::TimeDelta kTimeoutDuration = …;
const size_t kMaxRetryCount = …;
const size_t kMaxVibrationEffectDurationMillis = …;
const uint8_t kGyroSensitivity2000Dps = …;
const uint8_t kAccelerometerSensitivity8G = …;
const uint8_t kGyroPerformance208Hz = …;
const uint8_t kAccelerometerFilterBandwidth100Hz = …;
const uint8_t kPlayerLightPattern1 = …;
const uint16_t kCalBogusValue = …;
const uint16_t kCalDefaultDeadzone = …;
const uint16_t kCalDefaultMin = …;
const uint16_t kCalDefaultCenter = …;
const uint16_t kCalDefaultMax = …;
const double kVibrationFrequencyStrongRumble = …;
const double kVibrationFrequencyWeakRumble = …;
const double kVibrationAmplitudeStrongRumbleMax = …;
const double kVibrationAmplitudeWeakRumbleMax = …;
const int kVibrationFrequencyHzMin = …;
const int kVibrationFrequencyHzMax = …;
const int kVibrationAmplitudeMax = …;
struct VibrationFrequency { … } kVibrationFrequency[] = …;
const size_t kVibrationFrequencySize = …;
struct VibrationAmplitude { … } kVibrationAmplitude[]{ … };
const size_t kVibrationAmplitudeSize = …;
enum SWITCH_BUTTON_INDICES { … };
#pragma pack(push, 1)
struct UsbInputReport81 { … };
#pragma pack(pop)
static_assert …;
#pragma pack(push, 1)
struct MacAddressReport { … };
#pragma pack(pop)
static_assert …;
#pragma pack(push, 1)
struct ControllerData { … };
#pragma pack(pop)
static_assert …;
#pragma pack(push, 1)
struct ControllerDataReport { … };
#pragma pack(pop)
static_assert …;
#pragma pack(push, 1)
struct SpiReadReport { … };
#pragma pack(pop)
static_assert …;
void UnpackShorts(uint8_t byte0,
uint8_t byte1,
uint8_t byte2,
uint16_t* short1,
uint16_t* short2) { … }
uint64_t UnpackSwitchMacAddress(const uint8_t* data) { … }
void UnpackSwitchAnalogStickParameters(
const uint8_t* data,
NintendoController::SwitchCalibrationData& cal) { … }
void UnpackSwitchImuCalibration(
const uint8_t* data,
NintendoController::SwitchCalibrationData& cal) { … }
void UnpackSwitchImuHorizontalOffsets(
const uint8_t* data,
NintendoController::SwitchCalibrationData& cal) { … }
void UnpackSwitchAnalogStickCalibration(
const uint8_t* data,
NintendoController::SwitchCalibrationData& cal) { … }
void UnpackSwitchImuData(const uint8_t* data,
NintendoController::SwitchImuData* imu_data) { … }
bool ApplyDeadZone(uint16_t& x,
uint16_t& y,
uint16_t x_center,
uint16_t y_center,
uint16_t dead_zone) { … }
double NormalizeAndClampAxis(int value, int min, int max) { … }
bool UpdateGamepadFromControllerData(
const ControllerData& data,
const NintendoController::SwitchCalibrationData& cal,
Gamepad& pad) { … }
void UpdateButtonForLeftSide(const Gamepad& src_pad,
Gamepad& dst_pad,
size_t button_index,
bool horizontal) { … }
void UpdateButtonForRightSide(const Gamepad& src_pad,
Gamepad& dst_pad,
size_t button_index,
bool horizontal) { … }
void UpdateAxisForLeftSide(const Gamepad& src_pad,
Gamepad& dst_pad,
size_t axis_index,
bool horizontal) { … }
void UpdateAxisForRightSide(const Gamepad& src_pad,
Gamepad& dst_pad,
size_t axis_index,
bool horizontal) { … }
void FrequencyToHex(float frequency,
float amplitude,
uint16_t* hf,
uint8_t* lf,
uint8_t* hf_amp,
uint16_t* lf_amp) { … }
GamepadBusType BusTypeFromDeviceInfo(const mojom::HidDeviceInfo* device_info) { … }
}
NintendoController::SwitchCalibrationData::SwitchCalibrationData() = default;
NintendoController::SwitchCalibrationData::~SwitchCalibrationData() = default;
NintendoController::SwitchImuData::SwitchImuData() = default;
NintendoController::SwitchImuData::~SwitchImuData() = default;
NintendoController::NintendoController(int source_id,
GamepadBusType bus_type,
mojom::HidDeviceInfoPtr device_info,
mojom::HidManager* hid_manager)
: … { … }
NintendoController::NintendoController(
int source_id,
std::unique_ptr<NintendoController> composite1,
std::unique_ptr<NintendoController> composite2,
mojom::HidManager* hid_manager)
: … { … }
NintendoController::~NintendoController() = default;
std::unique_ptr<NintendoController> NintendoController::Create(
int source_id,
mojom::HidDeviceInfoPtr device_info,
mojom::HidManager* hid_manager) { … }
std::unique_ptr<NintendoController> NintendoController::CreateComposite(
int source_id,
std::unique_ptr<NintendoController> composite1,
std::unique_ptr<NintendoController> composite2,
mojom::HidManager* hid_manager) { … }
bool NintendoController::IsNintendoController(GamepadId gamepad_id) { … }
std::vector<std::unique_ptr<NintendoController>>
NintendoController::Decompose() { … }
void NintendoController::Open(base::OnceClosure device_ready_closure) { … }
GamepadHand NintendoController::GetGamepadHand() const { … }
bool NintendoController::IsUsable() const { … }
bool NintendoController::HasGuid(const std::string& guid) const { … }
GamepadStandardMappingFunction NintendoController::GetMappingFunction() const { … }
void NintendoController::InitializeGamepadState(bool has_standard_mapping,
Gamepad& pad) const { … }
void NintendoController::UpdatePadConnected() { … }
void NintendoController::UpdateGamepadState(Gamepad& pad) const { … }
void NintendoController::UpdateLeftGamepadState(Gamepad& pad,
bool horizontal) const { … }
void NintendoController::UpdateRightGamepadState(Gamepad& pad,
bool horizontal) const { … }
void NintendoController::Connect(mojom::HidManager::ConnectCallback callback) { … }
void NintendoController::OnConnect(
mojo::PendingRemote<mojom::HidConnection> connection) { … }
void NintendoController::StartInitSequence() { … }
void NintendoController::FinishInitSequence() { … }
void NintendoController::FailInitSequence() { … }
void NintendoController::HandleInputReport(
uint8_t report_id,
const std::vector<uint8_t>& report_bytes) { … }
void NintendoController::HandleUsbInputReport81(
const std::vector<uint8_t>& report_bytes) { … }
void NintendoController::HandleInputReport21(
const std::vector<uint8_t>& report_bytes) { … }
void NintendoController::HandleInputReport30(
const std::vector<uint8_t>& report_bytes) { … }
void NintendoController::ContinueInitSequence(
uint8_t report_id,
const std::vector<uint8_t>& report_bytes) { … }
void NintendoController::MakeInitSequenceRequests(InitializationState state) { … }
void NintendoController::SubCommand(uint8_t sub_command,
const std::vector<uint8_t>& bytes) { … }
void NintendoController::RequestMacAddress() { … }
void NintendoController::RequestHandshake() { … }
void NintendoController::RequestBaudRate() { … }
void NintendoController::RequestSubCommand33() { … }
void NintendoController::RequestVibration(double left_frequency,
double left_magnitude,
double right_frequency,
double right_magnitude) { … }
void NintendoController::RequestEnableUsbTimeout(bool enable) { … }
void NintendoController::RequestEnableImu(bool enable) { … }
void NintendoController::RequestEnableVibration(bool enable) { … }
void NintendoController::RequestSetPlayerLights(uint8_t light_pattern) { … }
void NintendoController::RequestSetImuSensitivity(
uint8_t gyro_sensitivity,
uint8_t accelerometer_sensitivity,
uint8_t gyro_performance_rate,
uint8_t accelerometer_filter_bandwidth) { … }
void NintendoController::RequestSetInputReportMode(uint8_t mode) { … }
void NintendoController::ReadSpi(uint16_t address, size_t length) { … }
void NintendoController::RequestImuCalibration() { … }
void NintendoController::RequestHorizontalOffsets() { … }
void NintendoController::RequestAnalogCalibration() { … }
void NintendoController::RequestAnalogParameters() { … }
void NintendoController::ReadInputReport() { … }
void NintendoController::OnReadInputReport(
bool success,
uint8_t report_id,
const std::optional<std::vector<uint8_t>>& report_bytes) { … }
void NintendoController::WriteOutputReport(
uint8_t report_id,
const std::vector<uint8_t>& report_bytes,
bool expect_reply) { … }
void NintendoController::OnWriteOutputReport(bool success) { … }
void NintendoController::DoShutdown() { … }
void NintendoController::SetVibration(
mojom::GamepadEffectParametersPtr params) { … }
double NintendoController::GetMaxEffectDurationMillis() { … }
void NintendoController::ArmTimeout() { … }
void NintendoController::CancelTimeout() { … }
void NintendoController::OnTimeout() { … }
base::WeakPtr<AbstractHapticGamepad> NintendoController::GetWeakPtr() { … }
}