summaryrefslogtreecommitdiff
path: root/src/input_common/gcadapter/gc_adapter.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2020-07-04 10:05:59 -0400
committerGravatar GitHub2020-07-04 10:05:59 -0400
commit9f8e17cb185d0d769ab81ef8de906cef37947ea5 (patch)
tree0ae185ce3ef43ef9b085aae7b9ad5abb04e3d239 /src/input_common/gcadapter/gc_adapter.cpp
parentMerge pull request #4218 from ogniK5377/opus-external (diff)
parentFix merge conflicts? (diff)
downloadyuzu-9f8e17cb185d0d769ab81ef8de906cef37947ea5.tar.gz
yuzu-9f8e17cb185d0d769ab81ef8de906cef37947ea5.tar.xz
yuzu-9f8e17cb185d0d769ab81ef8de906cef37947ea5.zip
Merge pull request #4137 from ameerj/master
GC Adapter Implementation
Diffstat (limited to 'src/input_common/gcadapter/gc_adapter.cpp')
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp379
1 files changed, 379 insertions, 0 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp
new file mode 100644
index 000000000..b39d2a3fb
--- /dev/null
+++ b/src/input_common/gcadapter/gc_adapter.cpp
@@ -0,0 +1,379 @@
1// Copyright 2014 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include <chrono>
6#include <thread>
7#include "common/logging/log.h"
8#include "input_common/gcadapter/gc_adapter.h"
9
10namespace GCAdapter {
11
12/// Used to loop through and assign button in poller
13constexpr std::array<PadButton, 12> PadButtonArray{
14 PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN,
15 PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R,
16 PadButton::PAD_TRIGGER_L, PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B,
17 PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START,
18};
19
20Adapter::Adapter() {
21 if (usb_adapter_handle != nullptr) {
22 return;
23 }
24 LOG_INFO(Input, "GC Adapter Initialization started");
25
26 current_status = NO_ADAPTER_DETECTED;
27 libusb_init(&libusb_ctx);
28
29 StartScanThread();
30}
31
32GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) {
33 GCPadStatus pad = {};
34 bool get_origin = false;
35
36 ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4);
37 if (type != ControllerTypes::None) {
38 get_origin = true;
39 }
40
41 adapter_controllers_status[port] = type;
42
43 static constexpr std::array<PadButton, 8> b1_buttons{
44 PadButton::PAD_BUTTON_A, PadButton::PAD_BUTTON_B, PadButton::PAD_BUTTON_X,
45 PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT,
46 PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP,
47 };
48
49 static constexpr std::array<PadButton, 4> b2_buttons{
50 PadButton::PAD_BUTTON_START,
51 PadButton::PAD_TRIGGER_Z,
52 PadButton::PAD_TRIGGER_R,
53 PadButton::PAD_TRIGGER_L,
54 };
55
56 if (adapter_controllers_status[port] != ControllerTypes::None) {
57 const u8 b1 = adapter_payload[1 + (9 * port) + 1];
58 const u8 b2 = adapter_payload[1 + (9 * port) + 2];
59
60 for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
61 if ((b1 & (1U << i)) != 0) {
62 pad.button |= static_cast<u16>(b1_buttons[i]);
63 }
64 }
65
66 for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
67 if ((b2 & (1U << j)) != 0) {
68 pad.button |= static_cast<u16>(b2_buttons[j]);
69 }
70 }
71
72 if (get_origin) {
73 pad.button |= PAD_GET_ORIGIN;
74 }
75
76 pad.stick_x = adapter_payload[1 + (9 * port) + 3];
77 pad.stick_y = adapter_payload[1 + (9 * port) + 4];
78 pad.substick_x = adapter_payload[1 + (9 * port) + 5];
79 pad.substick_y = adapter_payload[1 + (9 * port) + 6];
80 pad.trigger_left = adapter_payload[1 + (9 * port) + 7];
81 pad.trigger_right = adapter_payload[1 + (9 * port) + 8];
82 }
83 return pad;
84}
85
86void Adapter::PadToState(const GCPadStatus& pad, GCState& state) {
87 for (const auto& button : PadButtonArray) {
88 const u16 button_value = static_cast<u16>(button);
89 state.buttons.insert_or_assign(button_value, pad.button & button_value);
90 }
91
92 state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickX), pad.stick_x);
93 state.axes.insert_or_assign(static_cast<u8>(PadAxes::StickY), pad.stick_y);
94 state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickX), pad.substick_x);
95 state.axes.insert_or_assign(static_cast<u8>(PadAxes::SubstickY), pad.substick_y);
96 state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerLeft), pad.trigger_left);
97 state.axes.insert_or_assign(static_cast<u8>(PadAxes::TriggerRight), pad.trigger_right);
98}
99
100void Adapter::Read() {
101 LOG_DEBUG(Input, "GC Adapter Read() thread started");
102
103 int payload_size_in, payload_size_copy;
104 std::array<u8, 37> adapter_payload;
105 std::array<u8, 37> adapter_payload_copy;
106 std::array<GCPadStatus, 4> pads;
107
108 while (adapter_thread_running) {
109 libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(),
110 sizeof(adapter_payload), &payload_size_in, 16);
111 payload_size_copy = 0;
112 // this mutex might be redundant?
113 {
114 std::lock_guard<std::mutex> lk(s_mutex);
115 std::copy(std::begin(adapter_payload), std::end(adapter_payload),
116 std::begin(adapter_payload_copy));
117 payload_size_copy = payload_size_in;
118 }
119
120 if (payload_size_copy != sizeof(adapter_payload_copy) ||
121 adapter_payload_copy[0] != LIBUSB_DT_HID) {
122 LOG_ERROR(Input, "error reading payload (size: {}, type: {:02x})", payload_size_copy,
123 adapter_payload_copy[0]);
124 adapter_thread_running = false; // error reading from adapter, stop reading.
125 break;
126 }
127 for (std::size_t port = 0; port < pads.size(); ++port) {
128 pads[port] = GetPadStatus(port, adapter_payload_copy);
129 if (DeviceConnected(port) && configuring) {
130 if (pads[port].button != PAD_GET_ORIGIN) {
131 pad_queue[port].Push(pads[port]);
132 }
133
134 // Accounting for a threshold here because of some controller variance
135 if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD ||
136 pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) {
137 pads[port].axis = GCAdapter::PadAxes::StickX;
138 pads[port].axis_value = pads[port].stick_x;
139 pad_queue[port].Push(pads[port]);
140 }
141 if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD ||
142 pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) {
143 pads[port].axis = GCAdapter::PadAxes::StickY;
144 pads[port].axis_value = pads[port].stick_y;
145 pad_queue[port].Push(pads[port]);
146 }
147 if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD ||
148 pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) {
149 pads[port].axis = GCAdapter::PadAxes::SubstickX;
150 pads[port].axis_value = pads[port].substick_x;
151 pad_queue[port].Push(pads[port]);
152 }
153 if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD ||
154 pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) {
155 pads[port].axis = GCAdapter::PadAxes::SubstickY;
156 pads[port].axis_value = pads[port].substick_y;
157 pad_queue[port].Push(pads[port]);
158 }
159 if (pads[port].trigger_left > pads[port].TRIGGER_THRESHOLD) {
160 pads[port].axis = GCAdapter::PadAxes::TriggerLeft;
161 pads[port].axis_value = pads[port].trigger_left;
162 pad_queue[port].Push(pads[port]);
163 }
164 if (pads[port].trigger_right > pads[port].TRIGGER_THRESHOLD) {
165 pads[port].axis = GCAdapter::PadAxes::TriggerRight;
166 pads[port].axis_value = pads[port].trigger_right;
167 pad_queue[port].Push(pads[port]);
168 }
169 }
170 PadToState(pads[port], state[port]);
171 }
172 std::this_thread::yield();
173 }
174}
175
176void Adapter::ScanThreadFunc() {
177 LOG_INFO(Input, "GC Adapter scanning thread started");
178
179 while (detect_thread_running) {
180 if (usb_adapter_handle == nullptr) {
181 std::lock_guard<std::mutex> lk(initialization_mutex);
182 Setup();
183 }
184 std::this_thread::sleep_for(std::chrono::milliseconds(500));
185 }
186}
187
188void Adapter::StartScanThread() {
189 if (detect_thread_running) {
190 return;
191 }
192 if (!libusb_ctx) {
193 return;
194 }
195
196 detect_thread_running = true;
197 detect_thread = std::thread([=] { ScanThreadFunc(); });
198}
199
200void Adapter::StopScanThread() {
201 detect_thread_running = false;
202 detect_thread.join();
203}
204
205void Adapter::Setup() {
206 // Reset the error status in case the adapter gets unplugged
207 if (current_status < 0) {
208 current_status = NO_ADAPTER_DETECTED;
209 }
210
211 adapter_controllers_status.fill(ControllerTypes::None);
212
213 // pointer to list of connected usb devices
214 libusb_device** devices;
215
216 // populate the list of devices, get the count
217 const std::size_t device_count = libusb_get_device_list(libusb_ctx, &devices);
218
219 for (std::size_t index = 0; index < device_count; ++index) {
220 if (CheckDeviceAccess(devices[index])) {
221 // GC Adapter found and accessible, registering it
222 GetGCEndpoint(devices[index]);
223 break;
224 }
225 }
226}
227
228bool Adapter::CheckDeviceAccess(libusb_device* device) {
229 libusb_device_descriptor desc;
230 const int get_descriptor_error = libusb_get_device_descriptor(device, &desc);
231 if (get_descriptor_error) {
232 // could not acquire the descriptor, no point in trying to use it.
233 LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: {}",
234 get_descriptor_error);
235 return false;
236 }
237
238 if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
239 // This isn't the device we are looking for.
240 return false;
241 }
242 const int open_error = libusb_open(device, &usb_adapter_handle);
243
244 if (open_error == LIBUSB_ERROR_ACCESS) {
245 LOG_ERROR(Input, "Yuzu can not gain access to this device: ID {:04X}:{:04X}.",
246 desc.idVendor, desc.idProduct);
247 return false;
248 }
249 if (open_error) {
250 LOG_ERROR(Input, "libusb_open failed to open device with error = {}", open_error);
251 return false;
252 }
253
254 int kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle, 0);
255 if (kernel_driver_error == 1) {
256 kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle, 0);
257 if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
258 LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}",
259 kernel_driver_error);
260 }
261 }
262
263 if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
264 libusb_close(usb_adapter_handle);
265 usb_adapter_handle = nullptr;
266 return false;
267 }
268
269 const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0);
270 if (interface_claim_error) {
271 LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error);
272 libusb_close(usb_adapter_handle);
273 usb_adapter_handle = nullptr;
274 return false;
275 }
276
277 return true;
278}
279
280void Adapter::GetGCEndpoint(libusb_device* device) {
281 libusb_config_descriptor* config = nullptr;
282 libusb_get_config_descriptor(device, 0, &config);
283 for (u8 ic = 0; ic < config->bNumInterfaces; ic++) {
284 const libusb_interface* interfaceContainer = &config->interface[ic];
285 for (int i = 0; i < interfaceContainer->num_altsetting; i++) {
286 const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
287 for (u8 e = 0; e < interface->bNumEndpoints; e++) {
288 const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
289 if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
290 input_endpoint = endpoint->bEndpointAddress;
291 } else {
292 output_endpoint = endpoint->bEndpointAddress;
293 }
294 }
295 }
296 }
297 // This transfer seems to be responsible for clearing the state of the adapter
298 // Used to clear the "busy" state of when the device is unexpectedly unplugged
299 unsigned char clear_payload = 0x13;
300 libusb_interrupt_transfer(usb_adapter_handle, output_endpoint, &clear_payload,
301 sizeof(clear_payload), nullptr, 16);
302
303 adapter_thread_running = true;
304 current_status = ADAPTER_DETECTED;
305 adapter_input_thread = std::thread([=] { Read(); }); // Read input
306}
307
308Adapter::~Adapter() {
309 StopScanThread();
310 Reset();
311}
312
313void Adapter::Reset() {
314 std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock);
315 if (!lock.try_lock()) {
316 return;
317 }
318 if (current_status != ADAPTER_DETECTED) {
319 return;
320 }
321
322 if (adapter_thread_running) {
323 adapter_thread_running = false;
324 }
325 adapter_input_thread.join();
326
327 adapter_controllers_status.fill(ControllerTypes::None);
328 current_status = NO_ADAPTER_DETECTED;
329
330 if (usb_adapter_handle) {
331 libusb_release_interface(usb_adapter_handle, 1);
332 libusb_close(usb_adapter_handle);
333 usb_adapter_handle = nullptr;
334 }
335
336 if (libusb_ctx) {
337 libusb_exit(libusb_ctx);
338 }
339}
340
341bool Adapter::DeviceConnected(int port) {
342 return adapter_controllers_status[port] != ControllerTypes::None;
343}
344
345void Adapter::ResetDeviceType(int port) {
346 adapter_controllers_status[port] = ControllerTypes::None;
347}
348
349void Adapter::BeginConfiguration() {
350 for (auto& pq : pad_queue) {
351 pq.Clear();
352 }
353 configuring = true;
354}
355
356void Adapter::EndConfiguration() {
357 for (auto& pq : pad_queue) {
358 pq.Clear();
359 }
360 configuring = false;
361}
362
363std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() {
364 return pad_queue;
365}
366
367const std::array<Common::SPSCQueue<GCPadStatus>, 4>& Adapter::GetPadQueue() const {
368 return pad_queue;
369}
370
371std::array<GCState, 4>& Adapter::GetPadState() {
372 return state;
373}
374
375const std::array<GCState, 4>& Adapter::GetPadState() const {
376 return state;
377}
378
379} // namespace GCAdapter