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