joycon_driver.h 5.3 KB

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