summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/gc_adapter.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/input_common/drivers/gc_adapter.cpp (renamed from src/input_common/gcadapter/gc_adapter.cpp)419
1 files changed, 198 insertions, 221 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/drivers/gc_adapter.cpp
index a2f1bb67c..6721ba4f7 100644
--- a/src/input_common/gcadapter/gc_adapter.cpp
+++ b/src/input_common/drivers/gc_adapter.cpp
@@ -2,47 +2,103 @@
2// Licensed under GPLv2+ 2// Licensed under GPLv2+
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <chrono> 5#include <fmt/format.h>
6#include <thread>
7
8#include <libusb.h> 6#include <libusb.h>
9 7
10#include "common/logging/log.h" 8#include "common/logging/log.h"
11#include "common/param_package.h" 9#include "common/param_package.h"
12#include "common/settings_input.h" 10#include "common/settings_input.h"
13#include "input_common/gcadapter/gc_adapter.h" 11#include "common/thread.h"
12#include "input_common/drivers/gc_adapter.h"
13
14namespace InputCommon {
15
16class LibUSBContext {
17public:
18 explicit LibUSBContext() {
19 init_result = libusb_init(&ctx);
20 }
21
22 ~LibUSBContext() {
23 libusb_exit(ctx);
24 }
25
26 LibUSBContext& operator=(const LibUSBContext&) = delete;
27 LibUSBContext(const LibUSBContext&) = delete;
28
29 LibUSBContext& operator=(LibUSBContext&&) noexcept = delete;
30 LibUSBContext(LibUSBContext&&) noexcept = delete;
31
32 [[nodiscard]] int InitResult() const noexcept {
33 return init_result;
34 }
35
36 [[nodiscard]] libusb_context* get() noexcept {
37 return ctx;
38 }
39
40private:
41 libusb_context* ctx;
42 int init_result{};
43};
44
45class LibUSBDeviceHandle {
46public:
47 explicit LibUSBDeviceHandle(libusb_context* ctx, uint16_t vid, uint16_t pid) noexcept {
48 handle = libusb_open_device_with_vid_pid(ctx, vid, pid);
49 }
50
51 ~LibUSBDeviceHandle() noexcept {
52 if (handle) {
53 libusb_release_interface(handle, 1);
54 libusb_close(handle);
55 }
56 }
14 57
15namespace GCAdapter { 58 LibUSBDeviceHandle& operator=(const LibUSBDeviceHandle&) = delete;
59 LibUSBDeviceHandle(const LibUSBDeviceHandle&) = delete;
16 60
17Adapter::Adapter() { 61 LibUSBDeviceHandle& operator=(LibUSBDeviceHandle&&) noexcept = delete;
18 if (usb_adapter_handle != nullptr) { 62 LibUSBDeviceHandle(LibUSBDeviceHandle&&) noexcept = delete;
63
64 [[nodiscard]] libusb_device_handle* get() noexcept {
65 return handle;
66 }
67
68private:
69 libusb_device_handle* handle{};
70};
71
72GCAdapter::GCAdapter(const std::string input_engine_) : InputEngine(input_engine_) {
73 if (usb_adapter_handle) {
19 return; 74 return;
20 } 75 }
21 LOG_INFO(Input, "GC Adapter Initialization started"); 76 LOG_INFO(Input, "GC Adapter Initialization started");
22 77
23 const int init_res = libusb_init(&libusb_ctx); 78 libusb_ctx = std::make_unique<LibUSBContext>();
79 const int init_res = libusb_ctx->InitResult();
24 if (init_res == LIBUSB_SUCCESS) { 80 if (init_res == LIBUSB_SUCCESS) {
25 adapter_scan_thread = std::thread(&Adapter::AdapterScanThread, this); 81 adapter_scan_thread =
82 std::jthread([this](std::stop_token stop_token) { AdapterScanThread(stop_token); });
26 } else { 83 } else {
27 LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res); 84 LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res);
28 } 85 }
29} 86}
30 87
31Adapter::~Adapter() { 88GCAdapter::~GCAdapter() {
32 Reset(); 89 Reset();
33} 90}
34 91
35void Adapter::AdapterInputThread() { 92void GCAdapter::AdapterInputThread(std::stop_token stop_token) {
36 LOG_DEBUG(Input, "GC Adapter input thread started"); 93 LOG_DEBUG(Input, "GC Adapter input thread started");
94 Common::SetCurrentThreadName("yuzu:input:GCAdapter");
37 s32 payload_size{}; 95 s32 payload_size{};
38 AdapterPayload adapter_payload{}; 96 AdapterPayload adapter_payload{};
39 97
40 if (adapter_scan_thread.joinable()) { 98 adapter_scan_thread = {};
41 adapter_scan_thread.join();
42 }
43 99
44 while (adapter_input_thread_running) { 100 while (!stop_token.stop_requested()) {
45 libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), 101 libusb_interrupt_transfer(usb_adapter_handle->get(), input_endpoint, adapter_payload.data(),
46 static_cast<s32>(adapter_payload.size()), &payload_size, 16); 102 static_cast<s32>(adapter_payload.size()), &payload_size, 16);
47 if (IsPayloadCorrect(adapter_payload, payload_size)) { 103 if (IsPayloadCorrect(adapter_payload, payload_size)) {
48 UpdateControllers(adapter_payload); 104 UpdateControllers(adapter_payload);
@@ -52,19 +108,20 @@ void Adapter::AdapterInputThread() {
52 } 108 }
53 109
54 if (restart_scan_thread) { 110 if (restart_scan_thread) {
55 adapter_scan_thread = std::thread(&Adapter::AdapterScanThread, this); 111 adapter_scan_thread =
112 std::jthread([this](std::stop_token token) { AdapterScanThread(token); });
56 restart_scan_thread = false; 113 restart_scan_thread = false;
57 } 114 }
58} 115}
59 116
60bool Adapter::IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size) { 117bool GCAdapter::IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size) {
61 if (payload_size != static_cast<s32>(adapter_payload.size()) || 118 if (payload_size != static_cast<s32>(adapter_payload.size()) ||
62 adapter_payload[0] != LIBUSB_DT_HID) { 119 adapter_payload[0] != LIBUSB_DT_HID) {
63 LOG_DEBUG(Input, "Error reading payload (size: {}, type: {:02x})", payload_size, 120 LOG_DEBUG(Input, "Error reading payload (size: {}, type: {:02x})", payload_size,
64 adapter_payload[0]); 121 adapter_payload[0]);
65 if (input_error_counter++ > 20) { 122 if (input_error_counter++ > 20) {
66 LOG_ERROR(Input, "GC adapter timeout, Is the adapter connected?"); 123 LOG_ERROR(Input, "GC adapter timeout, Is the adapter connected?");
67 adapter_input_thread_running = false; 124 adapter_input_thread.request_stop();
68 restart_scan_thread = true; 125 restart_scan_thread = true;
69 } 126 }
70 return false; 127 return false;
@@ -74,7 +131,7 @@ bool Adapter::IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payloa
74 return true; 131 return true;
75} 132}
76 133
77void Adapter::UpdateControllers(const AdapterPayload& adapter_payload) { 134void GCAdapter::UpdateControllers(const AdapterPayload& adapter_payload) {
78 for (std::size_t port = 0; port < pads.size(); ++port) { 135 for (std::size_t port = 0; port < pads.size(); ++port) {
79 const std::size_t offset = 1 + (9 * port); 136 const std::size_t offset = 1 + (9 * port);
80 const auto type = static_cast<ControllerTypes>(adapter_payload[offset] >> 4); 137 const auto type = static_cast<ControllerTypes>(adapter_payload[offset] >> 4);
@@ -84,23 +141,21 @@ void Adapter::UpdateControllers(const AdapterPayload& adapter_payload) {
84 const u8 b2 = adapter_payload[offset + 2]; 141 const u8 b2 = adapter_payload[offset + 2];
85 UpdateStateButtons(port, b1, b2); 142 UpdateStateButtons(port, b1, b2);
86 UpdateStateAxes(port, adapter_payload); 143 UpdateStateAxes(port, adapter_payload);
87 if (configuring) {
88 UpdateYuzuSettings(port);
89 }
90 } 144 }
91 } 145 }
92} 146}
93 147
94void Adapter::UpdatePadType(std::size_t port, ControllerTypes pad_type) { 148void GCAdapter::UpdatePadType(std::size_t port, ControllerTypes pad_type) {
95 if (pads[port].type == pad_type) { 149 if (pads[port].type == pad_type) {
96 return; 150 return;
97 } 151 }
98 // Device changed reset device and set new type 152 // Device changed reset device and set new type
99 ResetDevice(port); 153 pads[port] = {};
100 pads[port].type = pad_type; 154 pads[port].type = pad_type;
101} 155}
102 156
103void Adapter::UpdateStateButtons(std::size_t port, u8 b1, u8 b2) { 157void GCAdapter::UpdateStateButtons(std::size_t port, [[maybe_unused]] u8 b1,
158 [[maybe_unused]] u8 b2) {
104 if (port >= pads.size()) { 159 if (port >= pads.size()) {
105 return; 160 return;
106 } 161 }
@@ -116,25 +171,21 @@ void Adapter::UpdateStateButtons(std::size_t port, u8 b1, u8 b2) {
116 PadButton::TriggerR, 171 PadButton::TriggerR,
117 PadButton::TriggerL, 172 PadButton::TriggerL,
118 }; 173 };
119 pads[port].buttons = 0; 174
120 for (std::size_t i = 0; i < b1_buttons.size(); ++i) { 175 for (std::size_t i = 0; i < b1_buttons.size(); ++i) {
121 if ((b1 & (1U << i)) != 0) { 176 const bool button_status = (b1 & (1U << i)) != 0;
122 pads[port].buttons = 177 const int button = static_cast<int>(b1_buttons[i]);
123 static_cast<u16>(pads[port].buttons | static_cast<u16>(b1_buttons[i])); 178 SetButton(pads[port].identifier, button, button_status);
124 pads[port].last_button = b1_buttons[i];
125 }
126 } 179 }
127 180
128 for (std::size_t j = 0; j < b2_buttons.size(); ++j) { 181 for (std::size_t j = 0; j < b2_buttons.size(); ++j) {
129 if ((b2 & (1U << j)) != 0) { 182 const bool button_status = (b2 & (1U << j)) != 0;
130 pads[port].buttons = 183 const int button = static_cast<int>(b2_buttons[j]);
131 static_cast<u16>(pads[port].buttons | static_cast<u16>(b2_buttons[j])); 184 SetButton(pads[port].identifier, button, button_status);
132 pads[port].last_button = b2_buttons[j];
133 }
134 } 185 }
135} 186}
136 187
137void Adapter::UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload) { 188void GCAdapter::UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload) {
138 if (port >= pads.size()) { 189 if (port >= pads.size()) {
139 return; 190 return;
140 } 191 }
@@ -155,134 +206,70 @@ void Adapter::UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_pa
155 pads[port].axis_origin[index] = axis_value; 206 pads[port].axis_origin[index] = axis_value;
156 pads[port].reset_origin_counter++; 207 pads[port].reset_origin_counter++;
157 } 208 }
158 pads[port].axis_values[index] = 209 const f32 axis_status = (axis_value - pads[port].axis_origin[index]) / 110.0f;
159 static_cast<s16>(axis_value - pads[port].axis_origin[index]); 210 SetAxis(pads[port].identifier, static_cast<int>(index), axis_status);
160 }
161}
162
163void Adapter::UpdateYuzuSettings(std::size_t port) {
164 if (port >= pads.size()) {
165 return;
166 }
167
168 constexpr u8 axis_threshold = 50;
169 GCPadStatus pad_status = {.port = port};
170
171 if (pads[port].buttons != 0) {
172 pad_status.button = pads[port].last_button;
173 pad_queue.Push(pad_status);
174 }
175
176 // Accounting for a threshold here to ensure an intentional press
177 for (std::size_t i = 0; i < pads[port].axis_values.size(); ++i) {
178 const s16 value = pads[port].axis_values[i];
179
180 if (value > axis_threshold || value < -axis_threshold) {
181 pad_status.axis = static_cast<PadAxes>(i);
182 pad_status.axis_value = value;
183 pad_status.axis_threshold = axis_threshold;
184 pad_queue.Push(pad_status);
185 }
186 }
187}
188
189void Adapter::UpdateVibrations() {
190 // Use 8 states to keep the switching between on/off fast enough for
191 // a human to not notice the difference between switching from on/off
192 // More states = more rumble strengths = slower update time
193 constexpr u8 vibration_states = 8;
194
195 vibration_counter = (vibration_counter + 1) % vibration_states;
196
197 for (GCController& pad : pads) {
198 const bool vibrate = pad.rumble_amplitude > vibration_counter;
199 vibration_changed |= vibrate != pad.enable_vibration;
200 pad.enable_vibration = vibrate;
201 }
202 SendVibrations();
203}
204
205void Adapter::SendVibrations() {
206 if (!rumble_enabled || !vibration_changed) {
207 return;
208 }
209 s32 size{};
210 constexpr u8 rumble_command = 0x11;
211 const u8 p1 = pads[0].enable_vibration;
212 const u8 p2 = pads[1].enable_vibration;
213 const u8 p3 = pads[2].enable_vibration;
214 const u8 p4 = pads[3].enable_vibration;
215 std::array<u8, 5> payload = {rumble_command, p1, p2, p3, p4};
216 const int err = libusb_interrupt_transfer(usb_adapter_handle, output_endpoint, payload.data(),
217 static_cast<s32>(payload.size()), &size, 16);
218 if (err) {
219 LOG_DEBUG(Input, "Adapter libusb write failed: {}", libusb_error_name(err));
220 if (output_error_counter++ > 5) {
221 LOG_ERROR(Input, "GC adapter output timeout, Rumble disabled");
222 rumble_enabled = false;
223 }
224 return;
225 } 211 }
226 output_error_counter = 0;
227 vibration_changed = false;
228}
229
230bool Adapter::RumblePlay(std::size_t port, u8 amplitude) {
231 pads[port].rumble_amplitude = amplitude;
232
233 return rumble_enabled;
234} 212}
235 213
236void Adapter::AdapterScanThread() { 214void GCAdapter::AdapterScanThread(std::stop_token stop_token) {
237 adapter_scan_thread_running = true; 215 Common::SetCurrentThreadName("yuzu:input:ScanGCAdapter");
238 adapter_input_thread_running = false; 216 usb_adapter_handle = nullptr;
239 if (adapter_input_thread.joinable()) { 217 pads = {};
240 adapter_input_thread.join(); 218 while (!stop_token.stop_requested() && !Setup()) {
241 } 219 std::this_thread::sleep_for(std::chrono::seconds(2));
242 ClearLibusbHandle();
243 ResetDevices();
244 while (adapter_scan_thread_running && !adapter_input_thread_running) {
245 Setup();
246 std::this_thread::sleep_for(std::chrono::seconds(1));
247 } 220 }
248} 221}
249 222
250void Adapter::Setup() { 223bool GCAdapter::Setup() {
251 usb_adapter_handle = libusb_open_device_with_vid_pid(libusb_ctx, 0x057e, 0x0337); 224 constexpr u16 nintendo_vid = 0x057e;
252 225 constexpr u16 gc_adapter_pid = 0x0337;
253 if (usb_adapter_handle == NULL) { 226 usb_adapter_handle =
254 return; 227 std::make_unique<LibUSBDeviceHandle>(libusb_ctx->get(), nintendo_vid, gc_adapter_pid);
228 if (!usb_adapter_handle->get()) {
229 return false;
255 } 230 }
256 if (!CheckDeviceAccess()) { 231 if (!CheckDeviceAccess()) {
257 ClearLibusbHandle(); 232 usb_adapter_handle = nullptr;
258 return; 233 return false;
259 } 234 }
260 235
261 libusb_device* device = libusb_get_device(usb_adapter_handle); 236 libusb_device* const device = libusb_get_device(usb_adapter_handle->get());
262 237
263 LOG_INFO(Input, "GC adapter is now connected"); 238 LOG_INFO(Input, "GC adapter is now connected");
264 // GC Adapter found and accessible, registering it 239 // GC Adapter found and accessible, registering it
265 if (GetGCEndpoint(device)) { 240 if (GetGCEndpoint(device)) {
266 adapter_scan_thread_running = false;
267 adapter_input_thread_running = true;
268 rumble_enabled = true; 241 rumble_enabled = true;
269 input_error_counter = 0; 242 input_error_counter = 0;
270 output_error_counter = 0; 243 output_error_counter = 0;
271 adapter_input_thread = std::thread(&Adapter::AdapterInputThread, this); 244
245 std::size_t port = 0;
246 for (GCController& pad : pads) {
247 pad.identifier = {
248 .guid = Common::UUID{""},
249 .port = port++,
250 .pad = 0,
251 };
252 PreSetController(pad.identifier);
253 }
254
255 adapter_input_thread =
256 std::jthread([this](std::stop_token stop_token) { AdapterInputThread(stop_token); });
257 return true;
272 } 258 }
259 return false;
273} 260}
274 261
275bool Adapter::CheckDeviceAccess() { 262bool GCAdapter::CheckDeviceAccess() {
276 // This fixes payload problems from offbrand GCAdapters 263 // This fixes payload problems from offbrand GCAdapters
277 const s32 control_transfer_error = 264 const s32 control_transfer_error =
278 libusb_control_transfer(usb_adapter_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 1000); 265 libusb_control_transfer(usb_adapter_handle->get(), 0x21, 11, 0x0001, 0, nullptr, 0, 1000);
279 if (control_transfer_error < 0) { 266 if (control_transfer_error < 0) {
280 LOG_ERROR(Input, "libusb_control_transfer failed with error= {}", control_transfer_error); 267 LOG_ERROR(Input, "libusb_control_transfer failed with error= {}", control_transfer_error);
281 } 268 }
282 269
283 s32 kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle, 0); 270 s32 kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle->get(), 0);
284 if (kernel_driver_error == 1) { 271 if (kernel_driver_error == 1) {
285 kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle, 0); 272 kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle->get(), 0);
286 if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) { 273 if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
287 LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}", 274 LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = {}",
288 kernel_driver_error); 275 kernel_driver_error);
@@ -290,15 +277,13 @@ bool Adapter::CheckDeviceAccess() {
290 } 277 }
291 278
292 if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) { 279 if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) {
293 libusb_close(usb_adapter_handle);
294 usb_adapter_handle = nullptr; 280 usb_adapter_handle = nullptr;
295 return false; 281 return false;
296 } 282 }
297 283
298 const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0); 284 const int interface_claim_error = libusb_claim_interface(usb_adapter_handle->get(), 0);
299 if (interface_claim_error) { 285 if (interface_claim_error) {
300 LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error); 286 LOG_ERROR(Input, "libusb_claim_interface failed with error = {}", interface_claim_error);
301 libusb_close(usb_adapter_handle);
302 usb_adapter_handle = nullptr; 287 usb_adapter_handle = nullptr;
303 return false; 288 return false;
304 } 289 }
@@ -306,7 +291,7 @@ bool Adapter::CheckDeviceAccess() {
306 return true; 291 return true;
307} 292}
308 293
309bool Adapter::GetGCEndpoint(libusb_device* device) { 294bool GCAdapter::GetGCEndpoint(libusb_device* device) {
310 libusb_config_descriptor* config = nullptr; 295 libusb_config_descriptor* config = nullptr;
311 const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config); 296 const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config);
312 if (config_descriptor_return != LIBUSB_SUCCESS) { 297 if (config_descriptor_return != LIBUSB_SUCCESS) {
@@ -332,68 +317,83 @@ bool Adapter::GetGCEndpoint(libusb_device* device) {
332 // This transfer seems to be responsible for clearing the state of the adapter 317 // This transfer seems to be responsible for clearing the state of the adapter
333 // Used to clear the "busy" state of when the device is unexpectedly unplugged 318 // Used to clear the "busy" state of when the device is unexpectedly unplugged
334 unsigned char clear_payload = 0x13; 319 unsigned char clear_payload = 0x13;
335 libusb_interrupt_transfer(usb_adapter_handle, output_endpoint, &clear_payload, 320 libusb_interrupt_transfer(usb_adapter_handle->get(), output_endpoint, &clear_payload,
336 sizeof(clear_payload), nullptr, 16); 321 sizeof(clear_payload), nullptr, 16);
337 return true; 322 return true;
338} 323}
339 324
340void Adapter::JoinThreads() { 325bool GCAdapter::SetRumble(const PadIdentifier& identifier, const Input::VibrationStatus vibration) {
341 restart_scan_thread = false; 326 const auto mean_amplitude = (vibration.low_amplitude + vibration.high_amplitude) * 0.5f;
342 adapter_input_thread_running = false; 327 const auto processed_amplitude =
343 adapter_scan_thread_running = false; 328 static_cast<u8>((mean_amplitude + std::pow(mean_amplitude, 0.3f)) * 0.5f * 0x8);
344
345 if (adapter_scan_thread.joinable()) {
346 adapter_scan_thread.join();
347 }
348 329
349 if (adapter_input_thread.joinable()) { 330 pads[identifier.port].rumble_amplitude = processed_amplitude;
350 adapter_input_thread.join(); 331 return rumble_enabled;
351 }
352} 332}
353 333
354void Adapter::ClearLibusbHandle() { 334void GCAdapter::UpdateVibrations() {
355 if (usb_adapter_handle) { 335 // Use 8 states to keep the switching between on/off fast enough for
356 libusb_release_interface(usb_adapter_handle, 1); 336 // a human to feel different vibration strenght
357 libusb_close(usb_adapter_handle); 337 // More states == more rumble strengths == slower update time
358 usb_adapter_handle = nullptr; 338 constexpr u8 vibration_states = 8;
339
340 vibration_counter = (vibration_counter + 1) % vibration_states;
341
342 for (GCController& pad : pads) {
343 const bool vibrate = pad.rumble_amplitude > vibration_counter;
344 vibration_changed |= vibrate != pad.enable_vibration;
345 pad.enable_vibration = vibrate;
359 } 346 }
347 SendVibrations();
360} 348}
361 349
362void Adapter::ResetDevices() { 350void GCAdapter::SendVibrations() {
363 for (std::size_t i = 0; i < pads.size(); ++i) { 351 if (!rumble_enabled || !vibration_changed) {
364 ResetDevice(i); 352 return;
353 }
354 s32 size{};
355 constexpr u8 rumble_command = 0x11;
356 const u8 p1 = pads[0].enable_vibration;
357 const u8 p2 = pads[1].enable_vibration;
358 const u8 p3 = pads[2].enable_vibration;
359 const u8 p4 = pads[3].enable_vibration;
360 std::array<u8, 5> payload = {rumble_command, p1, p2, p3, p4};
361 const int err =
362 libusb_interrupt_transfer(usb_adapter_handle->get(), output_endpoint, payload.data(),
363 static_cast<s32>(payload.size()), &size, 16);
364 if (err) {
365 LOG_DEBUG(Input, "Adapter libusb write failed: {}", libusb_error_name(err));
366 if (output_error_counter++ > 5) {
367 LOG_ERROR(Input, "GC adapter output timeout, Rumble disabled");
368 rumble_enabled = false;
369 }
370 return;
365 } 371 }
372 output_error_counter = 0;
373 vibration_changed = false;
366} 374}
367 375
368void Adapter::ResetDevice(std::size_t port) { 376bool GCAdapter::DeviceConnected(std::size_t port) const {
369 pads[port].type = ControllerTypes::None; 377 return pads[port].type != ControllerTypes::None;
370 pads[port].enable_vibration = false;
371 pads[port].rumble_amplitude = 0;
372 pads[port].buttons = 0;
373 pads[port].last_button = PadButton::Undefined;
374 pads[port].axis_values.fill(0);
375 pads[port].reset_origin_counter = 0;
376} 378}
377 379
378void Adapter::Reset() { 380void GCAdapter::Reset() {
379 JoinThreads(); 381 adapter_scan_thread = {};
380 ClearLibusbHandle(); 382 adapter_input_thread = {};
381 ResetDevices(); 383 usb_adapter_handle = nullptr;
382 384 pads = {};
383 if (libusb_ctx) { 385 libusb_ctx = nullptr;
384 libusb_exit(libusb_ctx);
385 }
386} 386}
387 387
388std::vector<Common::ParamPackage> Adapter::GetInputDevices() const { 388std::vector<Common::ParamPackage> GCAdapter::GetInputDevices() const {
389 std::vector<Common::ParamPackage> devices; 389 std::vector<Common::ParamPackage> devices;
390 for (std::size_t port = 0; port < pads.size(); ++port) { 390 for (std::size_t port = 0; port < pads.size(); ++port) {
391 if (!DeviceConnected(port)) { 391 if (!DeviceConnected(port)) {
392 continue; 392 continue;
393 } 393 }
394 std::string name = fmt::format("Gamecube Controller {}", port + 1); 394 const std::string name = fmt::format("Gamecube Controller {}", port + 1);
395 devices.emplace_back(Common::ParamPackage{ 395 devices.emplace_back(Common::ParamPackage{
396 {"class", "gcpad"}, 396 {"engine", "gcpad"},
397 {"display", std::move(name)}, 397 {"display", std::move(name)},
398 {"port", std::to_string(port)}, 398 {"port", std::to_string(port)},
399 }); 399 });
@@ -401,8 +401,7 @@ std::vector<Common::ParamPackage> Adapter::GetInputDevices() const {
401 return devices; 401 return devices;
402} 402}
403 403
404InputCommon::ButtonMapping Adapter::GetButtonMappingForDevice( 404ButtonMapping GCAdapter::GetButtonMappingForDevice(const Common::ParamPackage& params) {
405 const Common::ParamPackage& params) const {
406 // This list is missing ZL/ZR since those are not considered buttons. 405 // This list is missing ZL/ZR since those are not considered buttons.
407 // We will add those afterwards 406 // We will add those afterwards
408 // This list also excludes any button that can't be really mapped 407 // This list also excludes any button that can't be really mapped
@@ -425,7 +424,7 @@ InputCommon::ButtonMapping Adapter::GetButtonMappingForDevice(
425 return {}; 424 return {};
426 } 425 }
427 426
428 InputCommon::ButtonMapping mapping{}; 427 ButtonMapping mapping{};
429 for (const auto& [switch_button, gcadapter_button] : switch_to_gcadapter_button) { 428 for (const auto& [switch_button, gcadapter_button] : switch_to_gcadapter_button) {
430 Common::ParamPackage button_params({{"engine", "gcpad"}}); 429 Common::ParamPackage button_params({{"engine", "gcpad"}});
431 button_params.Set("port", params.Get("port", 0)); 430 button_params.Set("port", params.Get("port", 0));
@@ -434,30 +433,30 @@ InputCommon::ButtonMapping Adapter::GetButtonMappingForDevice(
434 } 433 }
435 434
436 // Add the missing bindings for ZL/ZR 435 // Add the missing bindings for ZL/ZR
437 static constexpr std::array<std::pair<Settings::NativeButton::Values, PadAxes>, 2> 436 static constexpr std::array<std::tuple<Settings::NativeButton::Values, PadButton, PadAxes>, 2>
438 switch_to_gcadapter_axis = { 437 switch_to_gcadapter_axis = {
439 std::pair{Settings::NativeButton::ZL, PadAxes::TriggerLeft}, 438 std::tuple{Settings::NativeButton::ZL, PadButton::TriggerL, PadAxes::TriggerLeft},
440 {Settings::NativeButton::ZR, PadAxes::TriggerRight}, 439 {Settings::NativeButton::ZR, PadButton::TriggerR, PadAxes::TriggerRight},
441 }; 440 };
442 for (const auto& [switch_button, gcadapter_axis] : switch_to_gcadapter_axis) { 441 for (const auto& [switch_button, gcadapter_buton, gcadapter_axis] : switch_to_gcadapter_axis) {
443 Common::ParamPackage button_params({{"engine", "gcpad"}}); 442 Common::ParamPackage button_params({{"engine", "gcpad"}});
444 button_params.Set("port", params.Get("port", 0)); 443 button_params.Set("port", params.Get("port", 0));
445 button_params.Set("button", static_cast<s32>(PadButton::Stick)); 444 button_params.Set("button", static_cast<s32>(gcadapter_buton));
446 button_params.Set("axis", static_cast<s32>(gcadapter_axis)); 445 button_params.Set("axis", static_cast<s32>(gcadapter_axis));
447 button_params.Set("threshold", 0.5f); 446 button_params.Set("threshold", 0.5f);
447 button_params.Set("range", 1.9f);
448 button_params.Set("direction", "+"); 448 button_params.Set("direction", "+");
449 mapping.insert_or_assign(switch_button, std::move(button_params)); 449 mapping.insert_or_assign(switch_button, std::move(button_params));
450 } 450 }
451 return mapping; 451 return mapping;
452} 452}
453 453
454InputCommon::AnalogMapping Adapter::GetAnalogMappingForDevice( 454AnalogMapping GCAdapter::GetAnalogMappingForDevice(const Common::ParamPackage& params) {
455 const Common::ParamPackage& params) const {
456 if (!params.Has("port")) { 455 if (!params.Has("port")) {
457 return {}; 456 return {};
458 } 457 }
459 458
460 InputCommon::AnalogMapping mapping = {}; 459 AnalogMapping mapping = {};
461 Common::ParamPackage left_analog_params; 460 Common::ParamPackage left_analog_params;
462 left_analog_params.Set("engine", "gcpad"); 461 left_analog_params.Set("engine", "gcpad");
463 left_analog_params.Set("port", params.Get("port", 0)); 462 left_analog_params.Set("port", params.Get("port", 0));
@@ -473,34 +472,12 @@ InputCommon::AnalogMapping Adapter::GetAnalogMappingForDevice(
473 return mapping; 472 return mapping;
474} 473}
475 474
476bool Adapter::DeviceConnected(std::size_t port) const { 475std::string GCAdapter::GetUIName(const Common::ParamPackage& params) const {
477 return pads[port].type != ControllerTypes::None; 476 if (params.Has("button")) {
478} 477 return fmt::format("Button {}", params.Get("button", 0));
479 478 }
480void Adapter::BeginConfiguration() {
481 pad_queue.Clear();
482 configuring = true;
483}
484
485void Adapter::EndConfiguration() {
486 pad_queue.Clear();
487 configuring = false;
488}
489
490Common::SPSCQueue<GCPadStatus>& Adapter::GetPadQueue() {
491 return pad_queue;
492}
493
494const Common::SPSCQueue<GCPadStatus>& Adapter::GetPadQueue() const {
495 return pad_queue;
496}
497
498GCController& Adapter::GetPadState(std::size_t port) {
499 return pads.at(port);
500}
501 479
502const GCController& Adapter::GetPadState(std::size_t port) const { 480 return "Bad GC Adapter";
503 return pads.at(port);
504} 481}
505 482
506} // namespace GCAdapter 483} // namespace InputCommon