gc_adapter.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <functional>
  7. #include <mutex>
  8. #include <stop_token>
  9. #include <thread>
  10. #include <unordered_map>
  11. #include "common/common_types.h"
  12. #include "common/threadsafe_queue.h"
  13. #include "input_common/main.h"
  14. struct libusb_context;
  15. struct libusb_device;
  16. struct libusb_device_handle;
  17. namespace GCAdapter {
  18. class LibUSBContext;
  19. class LibUSBDeviceHandle;
  20. enum class PadButton {
  21. Undefined = 0x0000,
  22. ButtonLeft = 0x0001,
  23. ButtonRight = 0x0002,
  24. ButtonDown = 0x0004,
  25. ButtonUp = 0x0008,
  26. TriggerZ = 0x0010,
  27. TriggerR = 0x0020,
  28. TriggerL = 0x0040,
  29. ButtonA = 0x0100,
  30. ButtonB = 0x0200,
  31. ButtonX = 0x0400,
  32. ButtonY = 0x0800,
  33. ButtonStart = 0x1000,
  34. // Below is for compatibility with "AxisButton" type
  35. Stick = 0x2000,
  36. };
  37. enum class PadAxes : u8 {
  38. StickX,
  39. StickY,
  40. SubstickX,
  41. SubstickY,
  42. TriggerLeft,
  43. TriggerRight,
  44. Undefined,
  45. };
  46. enum class ControllerTypes {
  47. None,
  48. Wired,
  49. Wireless,
  50. };
  51. struct GCPadStatus {
  52. std::size_t port{};
  53. PadButton button{PadButton::Undefined}; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
  54. PadAxes axis{PadAxes::Undefined};
  55. s16 axis_value{};
  56. u8 axis_threshold{50};
  57. };
  58. struct GCController {
  59. ControllerTypes type = ControllerTypes::None;
  60. bool enable_vibration = false;
  61. u8 rumble_amplitude = 0;
  62. u16 buttons = 0;
  63. PadButton last_button = PadButton::Undefined;
  64. std::array<s16, 6> axis_values{};
  65. std::array<u8, 6> axis_origin{};
  66. u8 reset_origin_counter{};
  67. };
  68. class Adapter {
  69. public:
  70. Adapter();
  71. ~Adapter();
  72. /// Request a vibration for a controller
  73. bool RumblePlay(std::size_t port, u8 amplitude);
  74. /// Used for polling
  75. void BeginConfiguration();
  76. void EndConfiguration();
  77. Common::SPSCQueue<GCPadStatus>& GetPadQueue();
  78. const Common::SPSCQueue<GCPadStatus>& GetPadQueue() const;
  79. GCController& GetPadState(std::size_t port);
  80. const GCController& GetPadState(std::size_t port) const;
  81. /// Returns true if there is a device connected to port
  82. bool DeviceConnected(std::size_t port) const;
  83. /// Used for automapping features
  84. std::vector<Common::ParamPackage> GetInputDevices() const;
  85. InputCommon::ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) const;
  86. InputCommon::AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) const;
  87. private:
  88. using AdapterPayload = std::array<u8, 37>;
  89. void UpdatePadType(std::size_t port, ControllerTypes pad_type);
  90. void UpdateControllers(const AdapterPayload& adapter_payload);
  91. void UpdateYuzuSettings(std::size_t port);
  92. void UpdateStateButtons(std::size_t port, u8 b1, u8 b2);
  93. void UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload);
  94. void UpdateVibrations();
  95. void AdapterInputThread(std::stop_token stop_token);
  96. void AdapterScanThread(std::stop_token stop_token);
  97. bool IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size);
  98. // Updates vibration state of all controllers
  99. void SendVibrations();
  100. /// For use in initialization, querying devices to find the adapter
  101. bool Setup();
  102. /// Returns true if we successfully gain access to GC Adapter
  103. bool CheckDeviceAccess();
  104. /// Captures GC Adapter endpoint address
  105. /// Returns true if the endpoint was set correctly
  106. bool GetGCEndpoint(libusb_device* device);
  107. /// For shutting down, clear all data, join all threads, release usb
  108. void Reset();
  109. std::unique_ptr<LibUSBDeviceHandle> usb_adapter_handle;
  110. std::array<GCController, 4> pads;
  111. Common::SPSCQueue<GCPadStatus> pad_queue;
  112. std::jthread adapter_input_thread;
  113. std::jthread adapter_scan_thread;
  114. bool restart_scan_thread{};
  115. std::unique_ptr<LibUSBContext> libusb_ctx;
  116. u8 input_endpoint{0};
  117. u8 output_endpoint{0};
  118. u8 input_error_counter{0};
  119. u8 output_error_counter{0};
  120. int vibration_counter{0};
  121. bool configuring{false};
  122. bool rumble_enabled{true};
  123. bool vibration_changed{true};
  124. };
  125. } // namespace GCAdapter