gc_adapter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <thread>
  6. #include <libusb.h>
  7. #include "common/logging/log.h"
  8. #include "input_common/gcadapter/gc_adapter.h"
  9. namespace GCAdapter {
  10. /// Used to loop through and assign button in poller
  11. constexpr std::array<PadButton, 12> PadButtonArray{
  12. PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN,
  13. PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R,
  14. PadButton::PAD_TRIGGER_L, PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B,
  15. PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START,
  16. };
  17. Adapter::Adapter() {
  18. if (usb_adapter_handle != nullptr) {
  19. return;
  20. }
  21. LOG_INFO(Input, "GC Adapter Initialization started");
  22. const int init_res = libusb_init(&libusb_ctx);
  23. if (init_res == LIBUSB_SUCCESS) {
  24. Setup();
  25. } else {
  26. LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res);
  27. }
  28. }
  29. GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload) {
  30. GCPadStatus pad = {};
  31. const std::size_t offset = 1 + (9 * port);
  32. adapter_controllers_status[port] = static_cast<ControllerTypes>(adapter_payload[offset] >> 4);
  33. static constexpr std::array<PadButton, 8> b1_buttons{
  34. PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X,
  35. PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT,
  36. PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP,
  37. };
  38. static constexpr std::array<PadButton, 4> b2_buttons{
  39. PadButton::PAD_BUTTON_START,
  40. PadButton::PAD_TRIGGER_Z,
  41. PadButton::PAD_TRIGGER_R,
  42. PadButton::PAD_TRIGGER_L,
  43. };
  44. static constexpr std::array<PadAxes, 6> axes{
  45. PadAxes::StickX, PadAxes::StickY, PadAxes::SubstickX,
  46. PadAxes::SubstickY, PadAxes::TriggerLeft, PadAxes::TriggerRight,
  47. };
  48. if (adapter_controllers_status[port] == ControllerTypes::None && !get_origin[port]) {
  49. // Controller may have been disconnected, recalibrate if reconnected.
  50. get_origin[port] = true;
  51. }
  52. if (adapter_controllers_status[port] != ControllerTypes::None) {
  53. const u8 b1 = adapter_payload[offset + 1];
  54. const u8 b2 = adapter_payload[offset + 2];
  55. for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
  56. if ((b1 & (1U << i)) != 0) {
  57. pad.button |= static_cast<u16>(b1_buttons[i]);
  58. }
  59. }
  60. for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
  61. if ((b2 & (1U << j)) != 0) {
  62. pad.button |= static_cast<u16>(b2_buttons[j]);
  63. }
  64. }
  65. for (PadAxes axis : axes) {
  66. const std::size_t index = static_cast<std::size_t>(axis);
  67. pad.axis_values[index] = adapter_payload[offset + 3 + index];
  68. }
  69. if (get_origin[port]) {
  70. origin_status[port].axis_values = pad.axis_values;
  71. get_origin[port] = false;
  72. }
  73. }
  74. return pad;
  75. }
  76. void Adapter::PadToState(const GCPadStatus& pad, GCState& state) {
  77. for (const auto& button : PadButtonArray) {
  78. const u16 button_value = static_cast<u16>(button);
  79. state.buttons.insert_or_assign(button_value, pad.button & button_value);
  80. }
  81. for (size_t i = 0; i < pad.axis_values.size(); ++i) {
  82. state.axes.insert_or_assign(static_cast<u8>(i), pad.axis_values[i]);
  83. }
  84. }
  85. void Adapter::Read() {
  86. LOG_DEBUG(Input, "GC Adapter Read() thread started");
  87. int payload_size;
  88. std::array<u8, 37> adapter_payload;
  89. std::array<GCPadStatus, 4> pads;
  90. while (adapter_thread_running) {
  91. libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(),
  92. sizeof(adapter_payload), &payload_size, 16);
  93. if (payload_size != sizeof(adapter_payload) || adapter_payload[0] != LIBUSB_DT_HID) {
  94. LOG_ERROR(Input,
  95. "Error reading payload (size: {}, type: {:02x}) Is the adapter connected?",
  96. payload_size, adapter_payload[0]);
  97. adapter_thread_running = false; // error reading from adapter, stop reading.
  98. break;
  99. }
  100. for (std::size_t port = 0; port < pads.size(); ++port) {
  101. pads[port] = GetPadStatus(port, adapter_payload);
  102. if (DeviceConnected(port) && configuring) {
  103. if (pads[port].button != 0) {
  104. pad_queue[port].Push(pads[port]);
  105. }
  106. // Accounting for a threshold here to ensure an intentional press
  107. for (size_t i = 0; i < pads[port].axis_values.size(); ++i) {
  108. const u8 value = pads[port].axis_values[i];
  109. const u8 origin = origin_status[port].axis_values[i];
  110. if (value > origin + pads[port].THRESHOLD ||
  111. value < origin - pads[port].THRESHOLD) {
  112. pads[port].axis = static_cast<PadAxes>(i);
  113. pads[port].axis_value = pads[port].axis_values[i];
  114. pad_queue[port].Push(pads[port]);
  115. }
  116. }
  117. }
  118. PadToState(pads[port], state[port]);
  119. }
  120. std::this_thread::yield();
  121. }
  122. }
  123. void Adapter::Setup() {
  124. // Initialize all controllers as unplugged
  125. adapter_controllers_status.fill(ControllerTypes::None);
  126. // Initialize all ports to store axis origin values
  127. get_origin.fill(true);
  128. // pointer to list of connected usb devices
  129. libusb_device** devices{};
  130. // populate the list of devices, get the count
  131. const ssize_t device_count = libusb_get_device_list(libusb_ctx, &devices);
  132. if (device_count < 0) {
  133. LOG_ERROR(Input, "libusb_get_device_list failed with error: {}", device_count);
  134. return;
  135. }
  136. if (devices != nullptr) {
  137. for (std::size_t index = 0; index < static_cast<std::size_t>(device_count); ++index) {
  138. if (CheckDeviceAccess(devices[index])) {
  139. // GC Adapter found and accessible, registering it
  140. GetGCEndpoint(devices[index]);
  141. break;
  142. }
  143. }
  144. libusb_free_device_list(devices, 1);
  145. }
  146. }
  147. bool Adapter::CheckDeviceAccess(libusb_device* device) {
  148. libusb_device_descriptor desc;
  149. const int get_descriptor_error = libusb_get_device_descriptor(device, &desc);
  150. if (get_descriptor_error) {
  151. // could not acquire the descriptor, no point in trying to use it.
  152. LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: {}",
  153. get_descriptor_error);
  154. return false;
  155. }
  156. if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
  157. // This isn't the device we are looking for.
  158. return false;
  159. }
  160. const int open_error = libusb_open(device, &usb_adapter_handle);
  161. if (open_error == LIBUSB_ERROR_ACCESS) {
  162. LOG_ERROR(Input, "Yuzu can not gain access to this device: ID {:04X}:{:04X}.",
  163. desc.idVendor, desc.idProduct);
  164. return false;
  165. }
  166. if (open_error) {
  167. LOG_ERROR(Input, "libusb_open failed to open device with error = {}", open_error);
  168. return false;
  169. }
  170. int kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle, 0);
  171. if (kernel_driver_error == 1) {
  172. kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle, 0);
  173. if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
  174. LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}",
  175. kernel_driver_error);
  176. }
  177. }
  178. if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
  179. libusb_close(usb_adapter_handle);
  180. usb_adapter_handle = nullptr;
  181. return false;
  182. }
  183. const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0);
  184. if (interface_claim_error) {
  185. LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error);
  186. libusb_close(usb_adapter_handle);
  187. usb_adapter_handle = nullptr;
  188. return false;
  189. }
  190. return true;
  191. }
  192. void Adapter::GetGCEndpoint(libusb_device* device) {
  193. libusb_config_descriptor* config = nullptr;
  194. const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config);
  195. if (config_descriptor_return != LIBUSB_SUCCESS) {
  196. LOG_ERROR(Input, "libusb_get_config_descriptor failed with error = {}",
  197. config_descriptor_return);
  198. return;
  199. }
  200. for (u8 ic = 0; ic < config->bNumInterfaces; ic++) {
  201. const libusb_interface* interfaceContainer = &config->interface[ic];
  202. for (int i = 0; i < interfaceContainer->num_altsetting; i++) {
  203. const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
  204. for (u8 e = 0; e < interface->bNumEndpoints; e++) {
  205. const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
  206. if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
  207. input_endpoint = endpoint->bEndpointAddress;
  208. } else {
  209. output_endpoint = endpoint->bEndpointAddress;
  210. }
  211. }
  212. }
  213. }
  214. // This transfer seems to be responsible for clearing the state of the adapter
  215. // Used to clear the "busy" state of when the device is unexpectedly unplugged
  216. unsigned char clear_payload = 0x13;
  217. libusb_interrupt_transfer(usb_adapter_handle, output_endpoint, &clear_payload,
  218. sizeof(clear_payload), nullptr, 16);
  219. adapter_thread_running = true;
  220. adapter_input_thread = std::thread([=] { Read(); }); // Read input
  221. }
  222. Adapter::~Adapter() {
  223. Reset();
  224. }
  225. void Adapter::Reset() {
  226. if (adapter_thread_running) {
  227. adapter_thread_running = false;
  228. }
  229. adapter_input_thread.join();
  230. adapter_controllers_status.fill(ControllerTypes::None);
  231. get_origin.fill(true);
  232. if (usb_adapter_handle) {
  233. libusb_release_interface(usb_adapter_handle, 1);
  234. libusb_close(usb_adapter_handle);
  235. usb_adapter_handle = nullptr;
  236. }
  237. if (libusb_ctx) {
  238. libusb_exit(libusb_ctx);
  239. }
  240. }
  241. bool Adapter::DeviceConnected(std::size_t port) {
  242. return adapter_controllers_status[port] != ControllerTypes::None;
  243. }
  244. void Adapter::ResetDeviceType(std::size_t port) {
  245. adapter_controllers_status[port] = ControllerTypes::None;
  246. }
  247. void Adapter::BeginConfiguration() {
  248. get_origin.fill(true);
  249. for (auto& pq : pad_queue) {
  250. pq.Clear();
  251. }
  252. configuring = true;
  253. }
  254. void Adapter::EndConfiguration() {
  255. for (auto& pq : pad_queue) {
  256. pq.Clear();
  257. }
  258. configuring = false;
  259. }
  260. std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
  261. return pad_queue;
  262. }
  263. const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
  264. return pad_queue;
  265. }
  266. std::array<GCState, 4>& Adapter::GetPadState() {
  267. return state;
  268. }
  269. const std::array<GCState, 4>& Adapter::GetPadState() const {
  270. return state;
  271. }
  272. int Adapter::GetOriginValue(int port, int axis) const {
  273. return origin_status[port].axis_values[axis];
  274. }
  275. } // namespace GCAdapter