diff options
| author | 2020-06-21 15:31:57 -0400 | |
|---|---|---|
| committer | 2020-06-21 15:31:57 -0400 | |
| commit | c94583d867fd909d8731ba50e085352aae0e6885 (patch) | |
| tree | 9c691c1382d3317f45ad09cb43a93f9223fe379b /src/input_common/gcadapter/gc_adapter.cpp | |
| parent | Cleanup after linter (diff) | |
| download | yuzu-c94583d867fd909d8731ba50e085352aae0e6885.tar.gz yuzu-c94583d867fd909d8731ba50e085352aae0e6885.tar.xz yuzu-c94583d867fd909d8731ba50e085352aae0e6885.zip | |
Clang Formatting
Diffstat (limited to 'src/input_common/gcadapter/gc_adapter.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 90 |
1 files changed, 57 insertions, 33 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index d42261d61..dc04116ce 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | // Copyright 2014 Dolphin Emulator Project | 1 | // Copyright 2014 Dolphin Emulator Project |
| 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 "common/logging/log.h" | 5 | #include "common/logging/log.h" |
| 6 | #include "common/threadsafe_queue.h" | 6 | #include "common/threadsafe_queue.h" |
| 7 | #include "input_common/gcadapter/gc_adapter.h" | 7 | #include "input_common/gcadapter/gc_adapter.h" |
| @@ -45,35 +45,48 @@ GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { | |||
| 45 | u8 b1 = adapter_payload[1 + (9 * port) + 1]; | 45 | u8 b1 = adapter_payload[1 + (9 * port) + 1]; |
| 46 | u8 b2 = adapter_payload[1 + (9 * port) + 2]; | 46 | u8 b2 = adapter_payload[1 + (9 * port) + 2]; |
| 47 | 47 | ||
| 48 | if (b1 & (1 << 0)) | 48 | if (b1 & (1 << 0)) { |
| 49 | pad.button |= PAD_BUTTON_A; | 49 | pad.button |= PAD_BUTTON_A; |
| 50 | if (b1 & (1 << 1)) | 50 | } |
| 51 | if (b1 & (1 << 1)) { | ||
| 51 | pad.button |= PAD_BUTTON_B; | 52 | pad.button |= PAD_BUTTON_B; |
| 52 | if (b1 & (1 << 2)) | 53 | } |
| 54 | if (b1 & (1 << 2)) { | ||
| 53 | pad.button |= PAD_BUTTON_X; | 55 | pad.button |= PAD_BUTTON_X; |
| 54 | if (b1 & (1 << 3)) | 56 | } |
| 57 | if (b1 & (1 << 3)) { | ||
| 55 | pad.button |= PAD_BUTTON_Y; | 58 | pad.button |= PAD_BUTTON_Y; |
| 59 | } | ||
| 56 | 60 | ||
| 57 | if (b1 & (1 << 4)) | 61 | if (b1 & (1 << 4)) { |
| 58 | pad.button |= PAD_BUTTON_LEFT; | 62 | pad.button |= PAD_BUTTON_LEFT; |
| 59 | if (b1 & (1 << 5)) | 63 | } |
| 64 | if (b1 & (1 << 5)) { | ||
| 60 | pad.button |= PAD_BUTTON_RIGHT; | 65 | pad.button |= PAD_BUTTON_RIGHT; |
| 61 | if (b1 & (1 << 6)) | 66 | } |
| 67 | if (b1 & (1 << 6)) { | ||
| 62 | pad.button |= PAD_BUTTON_DOWN; | 68 | pad.button |= PAD_BUTTON_DOWN; |
| 63 | if (b1 & (1 << 7)) | 69 | } |
| 70 | if (b1 & (1 << 7)) { | ||
| 64 | pad.button |= PAD_BUTTON_UP; | 71 | pad.button |= PAD_BUTTON_UP; |
| 72 | } | ||
| 65 | 73 | ||
| 66 | if (b2 & (1 << 0)) | 74 | if (b2 & (1 << 0)) { |
| 67 | pad.button |= PAD_BUTTON_START; | 75 | pad.button |= PAD_BUTTON_START; |
| 68 | if (b2 & (1 << 1)) | 76 | } |
| 77 | if (b2 & (1 << 1)) { | ||
| 69 | pad.button |= PAD_TRIGGER_Z; | 78 | pad.button |= PAD_TRIGGER_Z; |
| 70 | if (b2 & (1 << 2)) | 79 | } |
| 80 | if (b2 & (1 << 2)) { | ||
| 71 | pad.button |= PAD_TRIGGER_R; | 81 | pad.button |= PAD_TRIGGER_R; |
| 72 | if (b2 & (1 << 3)) | 82 | } |
| 83 | if (b2 & (1 << 3)) { | ||
| 73 | pad.button |= PAD_TRIGGER_L; | 84 | pad.button |= PAD_TRIGGER_L; |
| 85 | } | ||
| 74 | 86 | ||
| 75 | if (get_origin) | 87 | if (get_origin) { |
| 76 | pad.button |= PAD_GET_ORIGIN; | 88 | pad.button |= PAD_GET_ORIGIN; |
| 89 | } | ||
| 77 | 90 | ||
| 78 | pad.stickX = adapter_payload[1 + (9 * port) + 3]; | 91 | pad.stickX = adapter_payload[1 + (9 * port) + 3]; |
| 79 | pad.stickY = adapter_payload[1 + (9 * port) + 4]; | 92 | pad.stickY = adapter_payload[1 + (9 * port) + 4]; |
| @@ -86,7 +99,7 @@ GCPadStatus CheckStatus(int port, u8 adapter_payload[37]) { | |||
| 86 | } | 99 | } |
| 87 | 100 | ||
| 88 | void PadToState(GCPadStatus pad, GCState& state) { | 101 | void PadToState(GCPadStatus pad, GCState& state) { |
| 89 | //std::lock_guard lock{s_mutex}; | 102 | // std::lock_guard lock{s_mutex}; |
| 90 | state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); | 103 | 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); | 104 | 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); | 105 | state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); |
| @@ -125,7 +138,7 @@ static void Read() { | |||
| 125 | std::begin(controller_payload_copy)); | 138 | std::begin(controller_payload_copy)); |
| 126 | payload_size = payload_size_in; | 139 | payload_size = payload_size_in; |
| 127 | } | 140 | } |
| 128 | 141 | ||
| 129 | GCPadStatus pad[4]; | 142 | GCPadStatus pad[4]; |
| 130 | if (payload_size != sizeof(controller_payload_copy) || | 143 | if (payload_size != sizeof(controller_payload_copy) || |
| 131 | controller_payload_copy[0] != LIBUSB_DT_HID) { | 144 | controller_payload_copy[0] != LIBUSB_DT_HID) { |
| @@ -137,8 +150,9 @@ static void Read() { | |||
| 137 | } | 150 | } |
| 138 | for (int port = 0; port < 4; port++) { | 151 | for (int port = 0; port < 4; port++) { |
| 139 | if (DeviceConnected(port) && configuring) { | 152 | if (DeviceConnected(port) && configuring) { |
| 140 | if (pad[port].button != PAD_GET_ORIGIN) | 153 | if (pad[port].button != PAD_GET_ORIGIN) { |
| 141 | pad_queue[port].Push(pad[port]); | 154 | pad_queue[port].Push(pad[port]); |
| 155 | } | ||
| 142 | 156 | ||
| 143 | // Accounting for a threshold here because of some controller variance | 157 | // Accounting for a threshold here because of some controller variance |
| 144 | if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || | 158 | if (pad[port].stickX > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || |
| @@ -186,8 +200,9 @@ static void ScanThreadFunc() { | |||
| 186 | 200 | ||
| 187 | void Init() { | 201 | void Init() { |
| 188 | 202 | ||
| 189 | if (usb_adapter_handle != nullptr) | 203 | if (usb_adapter_handle != nullptr) { |
| 190 | return; | 204 | return; |
| 205 | } | ||
| 191 | LOG_INFO(Input, "GC Adapter Initialization started"); | 206 | LOG_INFO(Input, "GC Adapter Initialization started"); |
| 192 | 207 | ||
| 193 | current_status = NO_ADAPTER_DETECTED; | 208 | current_status = NO_ADAPTER_DETECTED; |
| @@ -197,10 +212,12 @@ void Init() { | |||
| 197 | } | 212 | } |
| 198 | 213 | ||
| 199 | void StartScanThread() { | 214 | void StartScanThread() { |
| 200 | if (detect_thread_running) | 215 | if (detect_thread_running) { |
| 201 | return; | 216 | return; |
| 202 | if (!libusb_ctx) | 217 | } |
| 218 | if (!libusb_ctx) { | ||
| 203 | return; | 219 | return; |
| 220 | } | ||
| 204 | 221 | ||
| 205 | detect_thread_running = true; | 222 | detect_thread_running = true; |
| 206 | detect_thread = std::thread(ScanThreadFunc); | 223 | detect_thread = std::thread(ScanThreadFunc); |
| @@ -212,15 +229,17 @@ void StopScanThread() { | |||
| 212 | 229 | ||
| 213 | static void Setup() { | 230 | static void Setup() { |
| 214 | // Reset the error status in case the adapter gets unplugged | 231 | // Reset the error status in case the adapter gets unplugged |
| 215 | if (current_status < 0) | 232 | if (current_status < 0) { |
| 216 | current_status = NO_ADAPTER_DETECTED; | 233 | current_status = NO_ADAPTER_DETECTED; |
| 234 | } | ||
| 217 | 235 | ||
| 218 | for (int i = 0; i < 4; i++) | 236 | for (int i = 0; i < 4; i++) { |
| 219 | adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; | 237 | adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; |
| 220 | 238 | } | |
| 239 | |||
| 221 | libusb_device** devs; // pointer to list of connected usb devices | 240 | libusb_device** devs; // pointer to list of connected usb devices |
| 222 | 241 | ||
| 223 | int cnt = libusb_get_device_list(libusb_ctx, &devs); //get the list of devices | 242 | int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices |
| 224 | 243 | ||
| 225 | for (int i = 0; i < cnt; i++) { | 244 | for (int i = 0; i < cnt; i++) { |
| 226 | if (CheckDeviceAccess(devs[i])) { | 245 | if (CheckDeviceAccess(devs[i])) { |
| @@ -247,9 +266,8 @@ static bool CheckDeviceAccess(libusb_device* device) { | |||
| 247 | ret = libusb_open(device, &usb_adapter_handle); | 266 | ret = libusb_open(device, &usb_adapter_handle); |
| 248 | 267 | ||
| 249 | if (ret == LIBUSB_ERROR_ACCESS) { | 268 | if (ret == LIBUSB_ERROR_ACCESS) { |
| 250 | LOG_ERROR(Input, | 269 | LOG_ERROR(Input, "Yuzu can not gain access to this device: ID %04X:%04X.", desc.idVendor, |
| 251 | "Yuzu can not gain access to this device: ID %04X:%04X.", | 270 | desc.idProduct); |
| 252 | desc.idVendor, desc.idProduct); | ||
| 253 | return false; | 271 | return false; |
| 254 | } | 272 | } |
| 255 | if (ret) { | 273 | if (ret) { |
| @@ -260,8 +278,9 @@ static bool CheckDeviceAccess(libusb_device* device) { | |||
| 260 | ret = libusb_kernel_driver_active(usb_adapter_handle, 0); | 278 | ret = libusb_kernel_driver_active(usb_adapter_handle, 0); |
| 261 | if (ret == 1) { | 279 | if (ret == 1) { |
| 262 | ret = libusb_detach_kernel_driver(usb_adapter_handle, 0); | 280 | ret = libusb_detach_kernel_driver(usb_adapter_handle, 0); |
| 263 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) | 281 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { |
| 264 | LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret); | 282 | LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret); |
| 283 | } | ||
| 265 | } | 284 | } |
| 266 | 285 | ||
| 267 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { | 286 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { |
| @@ -290,8 +309,9 @@ static void GetGCEndpoint(libusb_device* device) { | |||
| 290 | const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; | 309 | const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; |
| 291 | for (u8 e = 0; e < interface->bNumEndpoints; e++) { | 310 | for (u8 e = 0; e < interface->bNumEndpoints; e++) { |
| 292 | const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; | 311 | const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; |
| 293 | if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) | 312 | if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { |
| 294 | input_endpoint = endpoint->bEndpointAddress; | 313 | input_endpoint = endpoint->bEndpointAddress; |
| 314 | } | ||
| 295 | } | 315 | } |
| 296 | } | 316 | } |
| 297 | } | 317 | } |
| @@ -311,16 +331,20 @@ void Shutdown() { | |||
| 311 | 331 | ||
| 312 | static void Reset() { | 332 | static void Reset() { |
| 313 | std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock); | 333 | std::unique_lock<std::mutex> lock(initialization_mutex, std::defer_lock); |
| 314 | if (!lock.try_lock()) | 334 | if (!lock.try_lock()) { |
| 315 | return; | 335 | return; |
| 316 | if (current_status != ADAPTER_DETECTED) | 336 | } |
| 337 | if (current_status != ADAPTER_DETECTED) { | ||
| 317 | return; | 338 | return; |
| 339 | } | ||
| 318 | 340 | ||
| 319 | if (adapter_thread_running) | 341 | if (adapter_thread_running) { |
| 320 | adapter_input_thread.join(); | 342 | adapter_input_thread.join(); |
| 343 | } | ||
| 321 | 344 | ||
| 322 | for (int i = 0; i < 4; i++) | 345 | for (int i = 0; i < 4; i++) { |
| 323 | adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; | 346 | adapter_controllers_status[i] = ControllerTypes::CONTROLLER_NONE; |
| 347 | } | ||
| 324 | 348 | ||
| 325 | current_status = NO_ADAPTER_DETECTED; | 349 | current_status = NO_ADAPTER_DETECTED; |
| 326 | 350 | ||