gc_adapter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <mutex>
  6. #include <stop_token>
  7. #include <thread>
  8. #include "input_common/input_engine.h"
  9. struct libusb_context;
  10. struct libusb_device;
  11. struct libusb_device_handle;
  12. namespace InputCommon {
  13. class LibUSBContext;
  14. class LibUSBDeviceHandle;
  15. class GCAdapter : public InputCommon::InputEngine {
  16. public:
  17. explicit GCAdapter(const std::string input_engine_);
  18. ~GCAdapter();
  19. Input::VibrationError SetRumble(const PadIdentifier& identifier,
  20. const Input::VibrationStatus vibration) override;
  21. /// Used for automapping features
  22. std::vector<Common::ParamPackage> GetInputDevices() const override;
  23. ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
  24. AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
  25. std::string GetUIName(const Common::ParamPackage& params) const override;
  26. private:
  27. enum class PadButton {
  28. Undefined = 0x0000,
  29. ButtonLeft = 0x0001,
  30. ButtonRight = 0x0002,
  31. ButtonDown = 0x0004,
  32. ButtonUp = 0x0008,
  33. TriggerZ = 0x0010,
  34. TriggerR = 0x0020,
  35. TriggerL = 0x0040,
  36. ButtonA = 0x0100,
  37. ButtonB = 0x0200,
  38. ButtonX = 0x0400,
  39. ButtonY = 0x0800,
  40. ButtonStart = 0x1000,
  41. };
  42. enum class PadAxes : u8 {
  43. StickX,
  44. StickY,
  45. SubstickX,
  46. SubstickY,
  47. TriggerLeft,
  48. TriggerRight,
  49. Undefined,
  50. };
  51. enum class ControllerTypes {
  52. None,
  53. Wired,
  54. Wireless,
  55. };
  56. struct GCController {
  57. ControllerTypes type = ControllerTypes::None;
  58. PadIdentifier identifier{};
  59. bool enable_vibration = false;
  60. u8 rumble_amplitude{};
  61. std::array<u8, 6> axis_origin{};
  62. u8 reset_origin_counter{};
  63. };
  64. using AdapterPayload = std::array<u8, 37>;
  65. void UpdatePadType(std::size_t port, ControllerTypes pad_type);
  66. void UpdateControllers(const AdapterPayload& adapter_payload);
  67. void UpdateStateButtons(std::size_t port, u8 b1, u8 b2);
  68. void UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload);
  69. void AdapterInputThread(std::stop_token stop_token);
  70. void AdapterScanThread(std::stop_token stop_token);
  71. bool IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size);
  72. /// For use in initialization, querying devices to find the adapter
  73. bool Setup();
  74. /// Returns true if we successfully gain access to GC Adapter
  75. bool CheckDeviceAccess();
  76. /// Captures GC Adapter endpoint address
  77. /// Returns true if the endpoint was set correctly
  78. bool GetGCEndpoint(libusb_device* device);
  79. /// Returns true if there is a device connected to port
  80. bool DeviceConnected(std::size_t port) const;
  81. /// For shutting down, clear all data, join all threads, release usb
  82. void Reset();
  83. void UpdateVibrations();
  84. // Updates vibration state of all controllers
  85. void SendVibrations();
  86. std::unique_ptr<LibUSBDeviceHandle> usb_adapter_handle;
  87. std::array<GCController, 4> pads;
  88. std::jthread adapter_input_thread;
  89. std::jthread adapter_scan_thread;
  90. bool restart_scan_thread{};
  91. std::unique_ptr<LibUSBContext> libusb_ctx;
  92. u8 input_endpoint{0};
  93. u8 output_endpoint{0};
  94. u8 input_error_counter{0};
  95. u8 output_error_counter{0};
  96. int vibration_counter{0};
  97. bool rumble_enabled{true};
  98. bool vibration_changed{true};
  99. };
  100. } // namespace InputCommon