diff options
Diffstat (limited to 'src/input_common/helpers/joycon_driver.h')
| -rw-r--r-- | src/input_common/helpers/joycon_driver.h | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/input_common/helpers/joycon_driver.h b/src/input_common/helpers/joycon_driver.h new file mode 100644 index 000000000..c1e189fa5 --- /dev/null +++ b/src/input_common/helpers/joycon_driver.h | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <atomic> | ||
| 7 | #include <functional> | ||
| 8 | #include <mutex> | ||
| 9 | #include <span> | ||
| 10 | #include <thread> | ||
| 11 | |||
| 12 | #include "input_common/helpers/joycon_protocol/joycon_types.h" | ||
| 13 | |||
| 14 | namespace InputCommon::Joycon { | ||
| 15 | class CalibrationProtocol; | ||
| 16 | class GenericProtocol; | ||
| 17 | class IrsProtocol; | ||
| 18 | class NfcProtocol; | ||
| 19 | class JoyconPoller; | ||
| 20 | class RingConProtocol; | ||
| 21 | class RumbleProtocol; | ||
| 22 | |||
| 23 | class JoyconDriver final { | ||
| 24 | public: | ||
| 25 | explicit JoyconDriver(std::size_t port_); | ||
| 26 | |||
| 27 | ~JoyconDriver(); | ||
| 28 | |||
| 29 | DriverResult RequestDeviceAccess(SDL_hid_device_info* device_info); | ||
| 30 | DriverResult InitializeDevice(); | ||
| 31 | void Stop(); | ||
| 32 | |||
| 33 | bool IsConnected() const; | ||
| 34 | bool IsVibrationEnabled() const; | ||
| 35 | |||
| 36 | FirmwareVersion GetDeviceVersion() const; | ||
| 37 | Color GetDeviceColor() const; | ||
| 38 | std::size_t GetDevicePort() const; | ||
| 39 | ControllerType GetDeviceType() const; | ||
| 40 | ControllerType GetHandleDeviceType() const; | ||
| 41 | SerialNumber GetSerialNumber() const; | ||
| 42 | SerialNumber GetHandleSerialNumber() const; | ||
| 43 | |||
| 44 | DriverResult SetVibration(const VibrationValue& vibration); | ||
| 45 | DriverResult SetLedConfig(u8 led_pattern); | ||
| 46 | DriverResult SetIrsConfig(IrsMode mode_, IrsResolution format_); | ||
| 47 | DriverResult SetPasiveMode(); | ||
| 48 | DriverResult SetActiveMode(); | ||
| 49 | DriverResult SetIrMode(); | ||
| 50 | DriverResult SetNfcMode(); | ||
| 51 | DriverResult SetRingConMode(); | ||
| 52 | |||
| 53 | void SetCallbacks(const JoyconCallbacks& callbacks); | ||
| 54 | |||
| 55 | // Returns device type from hidapi handle | ||
| 56 | static DriverResult GetDeviceType(SDL_hid_device_info* device_info, | ||
| 57 | ControllerType& controller_type); | ||
| 58 | |||
| 59 | // Returns serial number from hidapi handle | ||
| 60 | static DriverResult GetSerialNumber(SDL_hid_device_info* device_info, | ||
| 61 | SerialNumber& serial_number); | ||
| 62 | |||
| 63 | private: | ||
| 64 | struct SupportedFeatures { | ||
| 65 | bool passive{}; | ||
| 66 | bool hidbus{}; | ||
| 67 | bool irs{}; | ||
| 68 | bool motion{}; | ||
| 69 | bool nfc{}; | ||
| 70 | bool vibration{}; | ||
| 71 | }; | ||
| 72 | |||
| 73 | /// Main thread, actively request new data from the handle | ||
| 74 | void InputThread(std::stop_token stop_token); | ||
| 75 | |||
| 76 | /// Called everytime a valid package arrives | ||
| 77 | void OnNewData(std::span<u8> buffer); | ||
| 78 | |||
| 79 | /// Updates device configuration to enable or disable features | ||
| 80 | DriverResult SetPollingMode(); | ||
| 81 | |||
| 82 | /// Returns true if input thread is valid and doesn't need to be stopped | ||
| 83 | bool IsInputThreadValid() const; | ||
| 84 | |||
| 85 | /// Returns true if the data should be interpreted. Otherwise the error counter is incremented | ||
| 86 | bool IsPayloadCorrect(int status, std::span<const u8> buffer); | ||
| 87 | |||
| 88 | /// Returns a list of supported features that can be enabled on this device | ||
| 89 | SupportedFeatures GetSupportedFeatures(); | ||
| 90 | |||
| 91 | // Protocol Features | ||
| 92 | std::unique_ptr<CalibrationProtocol> calibration_protocol; | ||
| 93 | std::unique_ptr<GenericProtocol> generic_protocol; | ||
| 94 | std::unique_ptr<IrsProtocol> irs_protocol; | ||
| 95 | std::unique_ptr<NfcProtocol> nfc_protocol; | ||
| 96 | std::unique_ptr<JoyconPoller> joycon_poller; | ||
| 97 | std::unique_ptr<RingConProtocol> ring_protocol; | ||
| 98 | std::unique_ptr<RumbleProtocol> rumble_protocol; | ||
| 99 | |||
| 100 | // Connection status | ||
| 101 | std::atomic<bool> is_connected{}; | ||
| 102 | u64 delta_time; | ||
| 103 | std::size_t error_counter{}; | ||
| 104 | std::shared_ptr<JoyconHandle> hidapi_handle; | ||
| 105 | std::chrono::time_point<std::chrono::steady_clock> last_update; | ||
| 106 | |||
| 107 | // External device status | ||
| 108 | bool starlink_connected{}; | ||
| 109 | bool ring_connected{}; | ||
| 110 | bool amiibo_detected{}; | ||
| 111 | bool is_ring_disabled_by_irs{}; | ||
| 112 | |||
| 113 | // Harware configuration | ||
| 114 | u8 leds{}; | ||
| 115 | ReportMode mode{}; | ||
| 116 | bool passive_enabled{}; // Low power mode, Ideal for multiple controllers at the same time | ||
| 117 | bool hidbus_enabled{}; // External device support | ||
| 118 | bool irs_enabled{}; // Infrared camera input | ||
| 119 | bool motion_enabled{}; // Enables motion input | ||
| 120 | bool nfc_enabled{}; // Enables Amiibo detection | ||
| 121 | bool vibration_enabled{}; // Allows vibrations | ||
| 122 | |||
| 123 | // Calibration data | ||
| 124 | GyroSensitivity gyro_sensitivity{}; | ||
| 125 | GyroPerformance gyro_performance{}; | ||
| 126 | AccelerometerSensitivity accelerometer_sensitivity{}; | ||
| 127 | AccelerometerPerformance accelerometer_performance{}; | ||
| 128 | JoyStickCalibration left_stick_calibration{}; | ||
| 129 | JoyStickCalibration right_stick_calibration{}; | ||
| 130 | MotionCalibration motion_calibration{}; | ||
| 131 | RingCalibration ring_calibration{}; | ||
| 132 | |||
| 133 | // Fixed joycon info | ||
| 134 | FirmwareVersion version{}; | ||
| 135 | Color color{}; | ||
| 136 | std::size_t port{}; | ||
| 137 | ControllerType device_type{}; // Device type reported by controller | ||
| 138 | ControllerType handle_device_type{}; // Device type reported by hidapi | ||
| 139 | SerialNumber serial_number{}; // Serial number reported by controller | ||
| 140 | SerialNumber handle_serial_number{}; // Serial number type reported by hidapi | ||
| 141 | SupportedFeatures supported_features{}; | ||
| 142 | |||
| 143 | // Thread related | ||
| 144 | mutable std::mutex mutex; | ||
| 145 | std::jthread input_thread; | ||
| 146 | bool input_thread_running{}; | ||
| 147 | bool disable_input_thread{}; | ||
| 148 | }; | ||
| 149 | |||
| 150 | } // namespace InputCommon::Joycon | ||