summaryrefslogtreecommitdiff
path: root/src/input_common/gcadapter/gc_adapter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/gcadapter/gc_adapter.cpp')
-rw-r--r--src/input_common/gcadapter/gc_adapter.cpp350
1 files changed, 350 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..d42261d61
--- /dev/null
+++ b/src/input_common/gcadapter/gc_adapter.cpp
@@ -0,0 +1,350 @@
1// Copyright 2014 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4//*
5#include "common/logging/log.h"
6#include "common/threadsafe_queue.h"
7#include "input_common/gcadapter/gc_adapter.h"
8
9Common::SPSCQueue<GCPadStatus> pad_queue[4];
10struct GCState state[4];
11
12namespace GCAdapter {
13
14static libusb_device_handle* usb_adapter_handle = nullptr;
15static u8 adapter_controllers_status[4] = {
16 ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE,
17 ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE};
18
19static std::mutex s_mutex;
20
21static std::thread adapter_input_thread;
22static bool adapter_thread_running;
23
24static std::mutex initialization_mutex;
25static std::thread detect_thread;
26static bool detect_thread_running = false;
27
28static libusb_context* libusb_ctx;
29
30static u8 input_endpoint = 0;
31
32static bool configuring = false;
33
34GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) {
35 GCPadStatus pad = {};
36 bool get_origin = false;
37
38 u8 type = adapter_payload[1 + (9 * port)] >> 4;
39 if (type)
40 get_origin = true;
41
42 adapter_controllers_status[port] = type;
43
44 if (adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE) {
45 u8 b1 = adapter_payload[1 + (9 * port) + 1];
46 u8 b2 = adapter_payload[1 + (9 * port) + 2];
47
48 if (b1 & (1 << 0))
49 pad.button |= PAD_BUTTON_A;
50 if (b1 & (1 << 1))
51 pad.button |= PAD_BUTTON_B;
52 if (b1 & (1 << 2))
53 pad.button |= PAD_BUTTON_X;
54 if (b1 & (1 << 3))
55 pad.button |= PAD_BUTTON_Y;
56
57 if (b1 & (1 << 4))
58 pad.button |= PAD_BUTTON_LEFT;
59 if (b1 & (1 << 5))
60 pad.button |= PAD_BUTTON_RIGHT;
61 if (b1 & (1 << 6))
62 pad.button |= PAD_BUTTON_DOWN;
63 if (b1 & (1 << 7))
64 pad.button |= PAD_BUTTON_UP;
65
66 if (b2 & (1 << 0))
67 pad.button |= PAD_BUTTON_START;
68 if (b2 & (1 << 1))
69 pad.button |= PAD_TRIGGER_Z;
70 if (b2 & (1 << 2))
71 pad.button |= PAD_TRIGGER_R;
72 if (b2 & (1 << 3))
73 pad.button |= PAD_TRIGGER_L;
74
75 if (get_origin)
76 pad.button |= PAD_GET_ORIGIN;
77
78 pad.stickX = adapter_payload[1 + (9 * port) + 3];
79 pad.stickY = adapter_payload[1 + (9 * port) + 4];
80 pad.substickX = adapter_payload[1 + (9 * port) + 5];
81 pad.substickY = adapter_payload[1 + (9 * port) + 6];
82 pad.triggerLeft = adapter_payload[1 + (9 * port) + 7];
83 pad.triggerRight = adapter_payload[1 + (9 * port) + 8];
84 }
85 return pad;
86}
87
88void PadToState(GCPadStatus pad, GCState& state) {
89 //std::lock_guard lock{s_mutex};
90 state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A);
91 state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B);
92 state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X);
93 state.buttons.insert_or_assign(PAD_BUTTON_Y, pad.button & PAD_BUTTON_Y);
94 state.buttons.insert_or_assign(PAD_BUTTON_LEFT, pad.button & PAD_BUTTON_LEFT);
95 state.buttons.insert_or_assign(PAD_BUTTON_RIGHT, pad.button & PAD_BUTTON_RIGHT);
96 state.buttons.insert_or_assign(PAD_BUTTON_DOWN, pad.button & PAD_BUTTON_DOWN);
97 state.buttons.insert_or_assign(PAD_BUTTON_UP, pad.button & PAD_BUTTON_UP);
98 state.buttons.insert_or_assign(PAD_BUTTON_START, pad.button & PAD_BUTTON_START);
99 state.buttons.insert_or_assign(PAD_TRIGGER_Z, pad.button & PAD_TRIGGER_Z);
100 state.buttons.insert_or_assign(PAD_TRIGGER_L, pad.button & PAD_TRIGGER_L);
101 state.buttons.insert_or_assign(PAD_TRIGGER_R, pad.button & PAD_TRIGGER_R);
102 state.axes.insert_or_assign(STICK_X, pad.stickX);
103 state.axes.insert_or_assign(STICK_Y, pad.stickY);
104 state.axes.insert_or_assign(SUBSTICK_X, pad.substickX);
105 state.axes.insert_or_assign(SUBSTICK_Y, pad.substickY);
106 state.axes.insert_or_assign(TRIGGER_LEFT, pad.triggerLeft);
107 state.axes.insert_or_assign(TRIGGER_RIGHT, pad.triggerRight);
108}
109
110static void Read() {
111 LOG_INFO(Input, "GC Adapter Read() thread started");
112
113 int payload_size_in;
114 u8 adapter_payload[37];
115 while (adapter_thread_running) {
116 libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload,
117 sizeof(adapter_payload), &payload_size_in, 32);
118
119 int payload_size = 0;
120 u8 controller_payload_copy[37];
121
122 {
123 std::lock_guard<std::mutex> lk(s_mutex);
124 std::copy(std::begin(adapter_payload), std::end(adapter_payload),
125 std::begin(controller_payload_copy));
126 payload_size = payload_size_in;
127 }
128
129 GCPadStatus pad[4];
130 if (payload_size != sizeof(controller_payload_copy) ||
131 controller_payload_copy[0] != LIBUSB_DT_HID) {
132 LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size,
133 controller_payload_copy[0]);
134 } else {
135 for (int i = 0; i < 4; i++)
136 pad[i] = CheckStatus(i, controller_payload_copy);
137 }
138 for (int port = 0; port < 4; port++) {
139 if (DeviceConnected(port) && configuring) {
140 if (pad[port].button != PAD_GET_ORIGIN)
141 pad_queue[port].Push(pad[port]);
142
143 // Accounting for a threshold here because of some controller variance
144 if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD ||
145 pad[port].stickX < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) {
146 pad[port].axis_which = STICK_X;
147 pad[port].axis_value = pad[port].stickX;
148 pad_queue[port].Push(pad[port]);
149 }
150 if (pad[port].stickY > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD ||
151 pad[port].stickY < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) {
152 pad[port].axis_which = STICK_Y;
153 pad[port].axis_value = pad[port].stickY;
154 pad_queue[port].Push(pad[port]);
155 }
156 if (pad[port].substickX > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD ||
157 pad[port].substickX < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) {
158 pad[port].axis_which = SUBSTICK_X;
159 pad[port].axis_value = pad[port].substickX;
160 pad_queue[port].Push(pad[port]);
161 }
162 if (pad[port].substickY > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD ||
163 pad[port].substickY < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) {
164 pad[port].axis_which = SUBSTICK_Y;
165 pad[port].axis_value = pad[port].substickY;
166 pad_queue[port].Push(pad[port]);
167 }
168 }
169 PadToState(pad[port], state[port]);
170 }
171 std::this_thread::yield();
172 }
173}
174
175static void ScanThreadFunc() {
176 LOG_INFO(Input, "GC Adapter scanning thread started");
177
178 while (detect_thread_running) {
179 if (usb_adapter_handle == nullptr) {
180 std::lock_guard<std::mutex> lk(initialization_mutex);
181 Setup();
182 }
183 Sleep(500);
184 }
185}
186
187void Init() {
188
189 if (usb_adapter_handle != nullptr)
190 return;
191 LOG_INFO(Input, "GC Adapter Initialization started");
192
193 current_status = NO_ADAPTER_DETECTED;
194 libusb_init(&libusb_ctx);
195
196 StartScanThread();
197}
198
199void StartScanThread() {
200 if (detect_thread_running)
201 return;
202 if (!libusb_ctx)
203 return;
204
205 detect_thread_running = true;
206 detect_thread = std::thread(ScanThreadFunc);
207}
208
209void StopScanThread() {
210 detect_thread.join();
211}
212
213static void Setup() {
214 // Reset the error status in case the adapter gets unplugged
215 if (current_status < 0)
216 current_status = NO_ADAPTER_DETECTED;
217
218 for (int i = 0; i < 4; i++)
219 adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE;
220
221 libusb_device** devs; // pointer to list of connected usb devices
222
223 int cnt = libusb_get_device_list(libusb_ctx, &devs); //get the list of devices
224
225 for (int i = 0; i < cnt; i++) {
226 if (CheckDeviceAccess(devs[i])) {
227 // GC Adapter found, registering it
228 GetGCEndpoint(devs[i]);
229 break;
230 }
231 }
232}
233
234static bool CheckDeviceAccess(libusb_device* device) {
235 libusb_device_descriptor desc;
236 int ret = libusb_get_device_descriptor(device, &desc);
237 if (ret) {
238 // could not acquire the descriptor, no point in trying to use it.
239 LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", ret);
240 return false;
241 }
242
243 if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
244 // This isn’t the device we are looking for.
245 return false;
246 }
247 ret = libusb_open(device, &usb_adapter_handle);
248
249 if (ret == LIBUSB_ERROR_ACCESS) {
250 LOG_ERROR(Input,
251 "Yuzu can not gain access to this device: ID %04X:%04X.",
252 desc.idVendor, desc.idProduct);
253 return false;
254 }
255 if (ret) {
256 LOG_ERROR(Input, "libusb_open failed to open device with error = %d", ret);
257 return false;
258 }
259
260 ret = libusb_kernel_driver_active(usb_adapter_handle, 0);
261 if (ret == 1) {
262 ret = libusb_detach_kernel_driver(usb_adapter_handle, 0);
263 if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED)
264 LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret);
265 }
266
267 if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) {
268 libusb_close(usb_adapter_handle);
269 usb_adapter_handle = nullptr;
270 return false;
271 }
272
273 ret = libusb_claim_interface(usb_adapter_handle, 0);
274 if (ret) {
275 LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", ret);
276 libusb_close(usb_adapter_handle);
277 usb_adapter_handle = nullptr;
278 return false;
279 }
280
281 return true;
282}
283
284static void GetGCEndpoint(libusb_device* device) {
285 libusb_config_descriptor* config = nullptr;
286 libusb_get_config_descriptor(device, 0, &config);
287 for (u8 ic = 0; ic < config->bNumInterfaces; ic++) {
288 const libusb_interface* interfaceContainer = &config->interface[ic];
289 for (int i = 0; i < interfaceContainer->num_altsetting; i++) {
290 const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
291 for (u8 e = 0; e < interface->bNumEndpoints; e++) {
292 const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
293 if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN)
294 input_endpoint = endpoint->bEndpointAddress;
295 }
296 }
297 }
298
299 adapter_thread_running = true;
300 current_status = ADAPTER_DETECTED;
301
302 adapter_input_thread = std::thread(Read); // Read input
303}
304
305void Shutdown() {
306 StopScanThread();
307 Reset();
308
309 current_status = NO_ADAPTER_DETECTED;
310}
311
312static void Reset() {
313 std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock);
314 if (!lock.try_lock())
315 return;
316 if (current_status != ADAPTER_DETECTED)
317 return;
318
319 if (adapter_thread_running)
320 adapter_input_thread.join();
321
322 for (int i = 0; i < 4; i++)
323 adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE;
324
325 current_status = NO_ADAPTER_DETECTED;
326
327 if (usb_adapter_handle) {
328 libusb_release_interface(usb_adapter_handle, 0);
329 libusb_close(usb_adapter_handle);
330 usb_adapter_handle = nullptr;
331 }
332}
333
334bool DeviceConnected(int port) {
335 return adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE;
336}
337
338void ResetDeviceType(int port) {
339 adapter_controllers_status[port] = ControllerTypes::CONTROLLER_NONE;
340}
341
342void BeginConfiguration() {
343 configuring = true;
344}
345
346void EndConfiguration() {
347 configuring = false;
348}
349
350} // end of namespace GCAdapter