2022-12-20 17:34:33 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <span>
|
|
|
|
#include <thread>
|
|
|
|
#include <SDL_hidapi.h>
|
|
|
|
|
|
|
|
#include "input_common/input_engine.h"
|
|
|
|
|
|
|
|
namespace InputCommon::Joycon {
|
|
|
|
using SerialNumber = std::array<u8, 15>;
|
|
|
|
struct Battery;
|
|
|
|
struct Color;
|
|
|
|
struct MotionData;
|
2023-06-17 03:57:21 +00:00
|
|
|
struct TagInfo;
|
2023-01-28 02:35:51 +00:00
|
|
|
enum class ControllerType : u8;
|
2022-12-28 21:21:12 +00:00
|
|
|
enum class IrsResolution;
|
2022-12-20 17:34:33 +00:00
|
|
|
class JoyconDriver;
|
|
|
|
} // namespace InputCommon::Joycon
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
class Joycons final : public InputCommon::InputEngine {
|
|
|
|
public:
|
|
|
|
explicit Joycons(const std::string& input_engine_);
|
|
|
|
|
|
|
|
~Joycons();
|
|
|
|
|
|
|
|
bool IsVibrationEnabled(const PadIdentifier& identifier) override;
|
2022-12-26 17:11:01 +00:00
|
|
|
Common::Input::DriverResult SetVibration(
|
2022-12-20 17:34:33 +00:00
|
|
|
const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override;
|
|
|
|
|
2022-12-26 17:11:01 +00:00
|
|
|
Common::Input::DriverResult SetLeds(const PadIdentifier& identifier,
|
|
|
|
const Common::Input::LedStatus& led_status) override;
|
2022-12-20 17:34:33 +00:00
|
|
|
|
2022-12-28 21:21:12 +00:00
|
|
|
Common::Input::DriverResult SetCameraFormat(const PadIdentifier& identifier,
|
2022-12-26 17:11:01 +00:00
|
|
|
Common::Input::CameraFormat camera_format) override;
|
2022-12-20 17:34:33 +00:00
|
|
|
|
2023-06-17 03:57:21 +00:00
|
|
|
Common::Input::NfcState SupportsNfc(const PadIdentifier& identifier) const override;
|
|
|
|
Common::Input::NfcState StartNfcPolling(const PadIdentifier& identifier) override;
|
|
|
|
Common::Input::NfcState StopNfcPolling(const PadIdentifier& identifier) override;
|
|
|
|
Common::Input::NfcState ReadAmiiboData(const PadIdentifier& identifier,
|
|
|
|
std::vector<u8>& out_data) override;
|
|
|
|
Common::Input::NfcState WriteNfcData(const PadIdentifier& identifier,
|
2022-12-20 17:34:33 +00:00
|
|
|
const std::vector<u8>& data) override;
|
2023-06-17 03:57:21 +00:00
|
|
|
Common::Input::NfcState ReadMifareData(const PadIdentifier& identifier,
|
|
|
|
const Common::Input::MifareRequest& request,
|
|
|
|
Common::Input::MifareRequest& out_data) override;
|
|
|
|
Common::Input::NfcState WriteMifareData(const PadIdentifier& identifier,
|
|
|
|
const Common::Input::MifareRequest& request) override;
|
2022-12-20 17:34:33 +00:00
|
|
|
|
2022-12-26 17:11:01 +00:00
|
|
|
Common::Input::DriverResult SetPollingMode(
|
2022-12-20 17:34:33 +00:00
|
|
|
const PadIdentifier& identifier, const Common::Input::PollingMode polling_mode) override;
|
|
|
|
|
|
|
|
/// Used for automapping features
|
|
|
|
std::vector<Common::ParamPackage> GetInputDevices() const override;
|
|
|
|
ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
|
|
|
|
AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
|
|
|
|
MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override;
|
|
|
|
Common::Input::ButtonNames GetUIName(const Common::ParamPackage& params) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr std::size_t MaxSupportedControllers = 8;
|
|
|
|
|
|
|
|
/// For shutting down, clear all data, join all threads, release usb devices
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
/// Registers controllers, clears all data and starts the scan thread
|
|
|
|
void Setup();
|
|
|
|
|
2023-03-12 03:10:38 +00:00
|
|
|
/// Actively searches for new devices
|
2022-12-20 17:34:33 +00:00
|
|
|
void ScanThread(std::stop_token stop_token);
|
|
|
|
|
|
|
|
/// Returns true if device is valid and not registered
|
|
|
|
bool IsDeviceNew(SDL_hid_device_info* device_info) const;
|
|
|
|
|
|
|
|
/// Tries to connect to the new device
|
|
|
|
void RegisterNewDevice(SDL_hid_device_info* device_info);
|
|
|
|
|
|
|
|
/// Returns the next free handle
|
|
|
|
std::shared_ptr<Joycon::JoyconDriver> GetNextFreeHandle(Joycon::ControllerType type) const;
|
|
|
|
|
|
|
|
void OnBatteryUpdate(std::size_t port, Joycon::ControllerType type, Joycon::Battery value);
|
|
|
|
void OnColorUpdate(std::size_t port, Joycon::ControllerType type, const Joycon::Color& value);
|
|
|
|
void OnButtonUpdate(std::size_t port, Joycon::ControllerType type, int id, bool value);
|
|
|
|
void OnStickUpdate(std::size_t port, Joycon::ControllerType type, int id, f32 value);
|
|
|
|
void OnMotionUpdate(std::size_t port, Joycon::ControllerType type, int id,
|
|
|
|
const Joycon::MotionData& value);
|
|
|
|
void OnRingConUpdate(f32 ring_data);
|
2023-05-16 04:44:50 +00:00
|
|
|
void OnAmiiboUpdate(std::size_t port, Joycon::ControllerType type,
|
2023-06-17 03:57:21 +00:00
|
|
|
const Joycon::TagInfo& amiibo_data);
|
2022-12-28 21:21:12 +00:00
|
|
|
void OnCameraUpdate(std::size_t port, const std::vector<u8>& camera_data,
|
|
|
|
Joycon::IrsResolution format);
|
2022-12-20 17:34:33 +00:00
|
|
|
|
|
|
|
/// Returns a JoyconHandle corresponding to a PadIdentifier
|
|
|
|
std::shared_ptr<Joycon::JoyconDriver> GetHandle(PadIdentifier identifier) const;
|
|
|
|
|
2023-01-14 06:36:03 +00:00
|
|
|
/// Returns a PadIdentifier corresponding to the port number and joycon type
|
2022-12-20 17:34:33 +00:00
|
|
|
PadIdentifier GetIdentifier(std::size_t port, Joycon::ControllerType type) const;
|
|
|
|
|
2023-01-14 06:36:03 +00:00
|
|
|
/// Returns a ParamPackage corresponding to the port number and joycon type
|
|
|
|
Common::ParamPackage GetParamPackage(std::size_t port, Joycon::ControllerType type) const;
|
|
|
|
|
2022-12-20 17:34:33 +00:00
|
|
|
std::string JoyconName(std::size_t port) const;
|
|
|
|
|
|
|
|
Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const;
|
|
|
|
|
|
|
|
/// Returns the name of the device in text format
|
|
|
|
std::string JoyconName(Joycon::ControllerType type) const;
|
|
|
|
|
2023-06-28 06:20:38 +00:00
|
|
|
Common::Input::NfcState TranslateDriverResult(Common::Input::DriverResult result) const;
|
2023-06-17 03:57:21 +00:00
|
|
|
|
2022-12-20 17:34:33 +00:00
|
|
|
std::jthread scan_thread;
|
|
|
|
|
|
|
|
// Joycon types are split by type to ease supporting dualjoycon configurations
|
|
|
|
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> left_joycons{};
|
|
|
|
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> right_joycons{};
|
2023-02-10 01:59:12 +00:00
|
|
|
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> pro_controller{};
|
2022-12-20 17:34:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace InputCommon
|