diff options
| author | 2020-06-23 17:37:15 -0400 | |
|---|---|---|
| committer | 2020-06-23 17:37:15 -0400 | |
| commit | 743e1f02a06187164d55f4208b8e85742abd4498 (patch) | |
| tree | 6eeeaddbc7cc3dac770c1d2efada1f4e68394939 /src/input_common/gcadapter/gc_adapter.cpp | |
| parent | Fix deallocation of GC Adapter (diff) | |
| download | yuzu-743e1f02a06187164d55f4208b8e85742abd4498.tar.gz yuzu-743e1f02a06187164d55f4208b8e85742abd4498.tar.xz yuzu-743e1f02a06187164d55f4208b8e85742abd4498.zip | |
cleanup check access, read, and factory GetNextInput funcs. Use size rather than magic number
Diffstat (limited to 'src/input_common/gcadapter/gc_adapter.cpp')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 169 |
1 files changed, 76 insertions, 93 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 887cde263..9437d628f 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -21,57 +21,38 @@ Adapter::Adapter() { | |||
| 21 | StartScanThread(); | 21 | StartScanThread(); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | GCPadStatus Adapter::CheckStatus(int port, const std::array<u8, 37>& adapter_payload) { | 24 | GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { |
| 25 | GCPadStatus pad = {}; | 25 | GCPadStatus pad = {}; |
| 26 | bool get_origin = false; | 26 | bool get_origin = false; |
| 27 | 27 | ||
| 28 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); | 28 | ControllerTypes type = ControllerTypes(adapter_payload[1 + (9 * port)] >> 4); |
| 29 | if (type != ControllerTypes::None) | 29 | if (type != ControllerTypes::None) { |
| 30 | get_origin = true; | 30 | get_origin = true; |
| 31 | } | ||
| 31 | 32 | ||
| 32 | adapter_controllers_status[port] = type; | 33 | adapter_controllers_status[port] = type; |
| 33 | 34 | ||
| 34 | if (adapter_controllers_status[port] != ControllerTypes::None) { | 35 | constexpr std::array<PadButton, 8> b1_buttons{ |
| 35 | u8 b1 = adapter_payload[1 + (9 * port) + 1]; | 36 | PAD_BUTTON_A, PAD_BUTTON_B, PAD_BUTTON_X, PAD_BUTTON_Y, |
| 36 | u8 b2 = adapter_payload[1 + (9 * port) + 2]; | 37 | PAD_BUTTON_LEFT, PAD_BUTTON_RIGHT, PAD_BUTTON_DOWN, PAD_BUTTON_UP}; |
| 37 | 38 | ||
| 38 | if (b1 & (1 << 0)) { | 39 | constexpr std::array<PadButton, 4> b2_buttons{PAD_BUTTON_START, PAD_TRIGGER_Z, PAD_TRIGGER_R, |
| 39 | pad.button |= PAD_BUTTON_A; | 40 | PAD_TRIGGER_L}; |
| 40 | } | ||
| 41 | if (b1 & (1 << 1)) { | ||
| 42 | pad.button |= PAD_BUTTON_B; | ||
| 43 | } | ||
| 44 | if (b1 & (1 << 2)) { | ||
| 45 | pad.button |= PAD_BUTTON_X; | ||
| 46 | } | ||
| 47 | if (b1 & (1 << 3)) { | ||
| 48 | pad.button |= PAD_BUTTON_Y; | ||
| 49 | } | ||
| 50 | 41 | ||
| 51 | if (b1 & (1 << 4)) { | 42 | if (adapter_controllers_status[port] != ControllerTypes::None) { |
| 52 | pad.button |= PAD_BUTTON_LEFT; | 43 | const u8 b1 = adapter_payload[1 + (9 * port) + 1]; |
| 53 | } | 44 | const u8 b2 = adapter_payload[1 + (9 * port) + 2]; |
| 54 | if (b1 & (1 << 5)) { | ||
| 55 | pad.button |= PAD_BUTTON_RIGHT; | ||
| 56 | } | ||
| 57 | if (b1 & (1 << 6)) { | ||
| 58 | pad.button |= PAD_BUTTON_DOWN; | ||
| 59 | } | ||
| 60 | if (b1 & (1 << 7)) { | ||
| 61 | pad.button |= PAD_BUTTON_UP; | ||
| 62 | } | ||
| 63 | 45 | ||
| 64 | if (b2 & (1 << 0)) { | 46 | for (int i = 0; i < b1_buttons.size(); i++) { |
| 65 | pad.button |= PAD_BUTTON_START; | 47 | if (b1 & (1 << i)) { |
| 66 | } | 48 | pad.button |= b1_buttons[i]; |
| 67 | if (b2 & (1 << 1)) { | 49 | } |
| 68 | pad.button |= PAD_TRIGGER_Z; | ||
| 69 | } | ||
| 70 | if (b2 & (1 << 2)) { | ||
| 71 | pad.button |= PAD_TRIGGER_R; | ||
| 72 | } | 50 | } |
| 73 | if (b2 & (1 << 3)) { | 51 | |
| 74 | pad.button |= PAD_TRIGGER_L; | 52 | for (int j = 0; j < b2_buttons.size(); j++) { |
| 53 | if (b2 & (1 << j)) { | ||
| 54 | pad.button |= b2_buttons[j]; | ||
| 55 | } | ||
| 75 | } | 56 | } |
| 76 | 57 | ||
| 77 | if (get_origin) { | 58 | if (get_origin) { |
| @@ -112,65 +93,65 @@ void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { | |||
| 112 | void Adapter::Read() { | 93 | void Adapter::Read() { |
| 113 | LOG_INFO(Input, "GC Adapter Read() thread started"); | 94 | LOG_INFO(Input, "GC Adapter Read() thread started"); |
| 114 | 95 | ||
| 115 | int payload_size_in, payload_size; | 96 | int payload_size_in, payload_size_copy; |
| 116 | std::array<u8, 37> adapter_payload; | 97 | std::array<u8, 37> adapter_payload; |
| 117 | std::array<u8, 37> controller_payload_copy; | 98 | std::array<u8, 37> adapter_payload_copy; |
| 118 | std::array<GCPadStatus, 4> pad; | 99 | std::array<GCPadStatus, 4> pads; |
| 119 | 100 | ||
| 120 | while (adapter_thread_running) { | 101 | while (adapter_thread_running) { |
| 121 | libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), | 102 | libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), |
| 122 | sizeof(adapter_payload), &payload_size_in, 32); | 103 | sizeof(adapter_payload), &payload_size_in, 32); |
| 123 | payload_size = 0; | 104 | payload_size_copy = 0; |
| 124 | { | 105 | { |
| 125 | std::lock_guard<std::mutex> lk(s_mutex); | 106 | std::lock_guard<std::mutex> lk(s_mutex); |
| 126 | std::copy(std::begin(adapter_payload), std::end(adapter_payload), | 107 | std::copy(std::begin(adapter_payload), std::end(adapter_payload), |
| 127 | std::begin(controller_payload_copy)); | 108 | std::begin(adapter_payload_copy)); |
| 128 | payload_size = payload_size_in; | 109 | payload_size_copy = payload_size_in; |
| 129 | } | 110 | } |
| 130 | 111 | ||
| 131 | if (payload_size != sizeof(controller_payload_copy) || | 112 | if (payload_size_copy != sizeof(adapter_payload_copy) || |
| 132 | controller_payload_copy[0] != LIBUSB_DT_HID) { | 113 | adapter_payload_copy[0] != LIBUSB_DT_HID) { |
| 133 | // TODO: It might be worthwhile to Shutdown GC Adapter if we encounter errors here | 114 | // TODO: It might be worthwhile to Shutdown GC Adapter if we encounter errors here |
| 134 | LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, | 115 | LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size_copy, |
| 135 | controller_payload_copy[0]); | 116 | adapter_payload_copy[0]); |
| 136 | } else { | 117 | } else { |
| 137 | for (int port = 0; port < 4; port++) { | 118 | for (int port = 0; port < pads.size(); port++) { |
| 138 | pad[port] = CheckStatus(port, controller_payload_copy); | 119 | pads[port] = GetPadStatus(port, adapter_payload_copy); |
| 139 | } | 120 | } |
| 140 | } | 121 | } |
| 141 | for (int port = 0; port < 4; port++) { | 122 | for (int port = 0; port < pads.size(); port++) { |
| 142 | if (DeviceConnected(port) && configuring) { | 123 | if (DeviceConnected(port) && configuring) { |
| 143 | if (pad[port].button != PAD_GET_ORIGIN) { | 124 | if (pads[port].button != PAD_GET_ORIGIN) { |
| 144 | pad_queue[port].Push(pad[port]); | 125 | pad_queue[port].Push(pads[port]); |
| 145 | } | 126 | } |
| 146 | 127 | ||
| 147 | // Accounting for a threshold here because of some controller variance | 128 | // Accounting for a threshold here because of some controller variance |
| 148 | if (pad[port].stick_x > pad[port].MAIN_STICK_CENTER_X + pad[port].THRESHOLD || | 129 | if (pads[port].stick_x > pads[port].MAIN_STICK_CENTER_X + pads[port].THRESHOLD || |
| 149 | pad[port].stick_x < pad[port].MAIN_STICK_CENTER_X - pad[port].THRESHOLD) { | 130 | pads[port].stick_x < pads[port].MAIN_STICK_CENTER_X - pads[port].THRESHOLD) { |
| 150 | pad[port].axis = GCAdapter::PadAxes::StickX; | 131 | pads[port].axis = GCAdapter::PadAxes::StickX; |
| 151 | pad[port].axis_value = pad[port].stick_x; | 132 | pads[port].axis_value = pads[port].stick_x; |
| 152 | pad_queue[port].Push(pad[port]); | 133 | pad_queue[port].Push(pads[port]); |
| 153 | } | 134 | } |
| 154 | if (pad[port].stick_y > pad[port].MAIN_STICK_CENTER_Y + pad[port].THRESHOLD || | 135 | if (pads[port].stick_y > pads[port].MAIN_STICK_CENTER_Y + pads[port].THRESHOLD || |
| 155 | pad[port].stick_y < pad[port].MAIN_STICK_CENTER_Y - pad[port].THRESHOLD) { | 136 | pads[port].stick_y < pads[port].MAIN_STICK_CENTER_Y - pads[port].THRESHOLD) { |
| 156 | pad[port].axis = GCAdapter::PadAxes::StickY; | 137 | pads[port].axis = GCAdapter::PadAxes::StickY; |
| 157 | pad[port].axis_value = pad[port].stick_y; | 138 | pads[port].axis_value = pads[port].stick_y; |
| 158 | pad_queue[port].Push(pad[port]); | 139 | pad_queue[port].Push(pads[port]); |
| 159 | } | 140 | } |
| 160 | if (pad[port].substick_x > pad[port].C_STICK_CENTER_X + pad[port].THRESHOLD || | 141 | if (pads[port].substick_x > pads[port].C_STICK_CENTER_X + pads[port].THRESHOLD || |
| 161 | pad[port].substick_x < pad[port].C_STICK_CENTER_X - pad[port].THRESHOLD) { | 142 | pads[port].substick_x < pads[port].C_STICK_CENTER_X - pads[port].THRESHOLD) { |
| 162 | pad[port].axis = GCAdapter::PadAxes::SubstickX; | 143 | pads[port].axis = GCAdapter::PadAxes::SubstickX; |
| 163 | pad[port].axis_value = pad[port].substick_x; | 144 | pads[port].axis_value = pads[port].substick_x; |
| 164 | pad_queue[port].Push(pad[port]); | 145 | pad_queue[port].Push(pads[port]); |
| 165 | } | 146 | } |
| 166 | if (pad[port].substick_y > pad[port].C_STICK_CENTER_Y + pad[port].THRESHOLD || | 147 | if (pads[port].substick_y > pads[port].C_STICK_CENTER_Y + pads[port].THRESHOLD || |
| 167 | pad[port].substick_y < pad[port].C_STICK_CENTER_Y - pad[port].THRESHOLD) { | 148 | pads[port].substick_y < pads[port].C_STICK_CENTER_Y - pads[port].THRESHOLD) { |
| 168 | pad[port].axis = GCAdapter::PadAxes::SubstickY; | 149 | pads[port].axis = GCAdapter::PadAxes::SubstickY; |
| 169 | pad[port].axis_value = pad[port].substick_y; | 150 | pads[port].axis_value = pads[port].substick_y; |
| 170 | pad_queue[port].Push(pad[port]); | 151 | pad_queue[port].Push(pads[port]); |
| 171 | } | 152 | } |
| 172 | } | 153 | } |
| 173 | PadToState(pad[port], state[port]); | 154 | PadToState(pads[port], state[port]); |
| 174 | } | 155 | } |
| 175 | std::this_thread::yield(); | 156 | std::this_thread::yield(); |
| 176 | } | 157 | } |
| @@ -215,11 +196,11 @@ void Adapter::Setup() { | |||
| 215 | 196 | ||
| 216 | libusb_device** devs; // pointer to list of connected usb devices | 197 | libusb_device** devs; // pointer to list of connected usb devices |
| 217 | 198 | ||
| 218 | int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices | 199 | const int cnt = libusb_get_device_list(libusb_ctx, &devs); // get the list of devices |
| 219 | 200 | ||
| 220 | for (int i = 0; i < cnt; i++) { | 201 | for (int i = 0; i < cnt; i++) { |
| 221 | if (CheckDeviceAccess(devs[i])) { | 202 | if (CheckDeviceAccess(devs[i])) { |
| 222 | // GC Adapter found, registering it | 203 | // GC Adapter found and accessible, registering it |
| 223 | GetGCEndpoint(devs[i]); | 204 | GetGCEndpoint(devs[i]); |
| 224 | break; | 205 | break; |
| 225 | } | 206 | } |
| @@ -228,10 +209,11 @@ void Adapter::Setup() { | |||
| 228 | 209 | ||
| 229 | bool Adapter::CheckDeviceAccess(libusb_device* device) { | 210 | bool Adapter::CheckDeviceAccess(libusb_device* device) { |
| 230 | libusb_device_descriptor desc; | 211 | libusb_device_descriptor desc; |
| 231 | int ret = libusb_get_device_descriptor(device, &desc); | 212 | const int get_descriptor_error = libusb_get_device_descriptor(device, &desc); |
| 232 | if (ret) { | 213 | if (get_descriptor_error) { |
| 233 | // could not acquire the descriptor, no point in trying to use it. | 214 | // could not acquire the descriptor, no point in trying to use it. |
| 234 | LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", ret); | 215 | LOG_ERROR(Input, "libusb_get_device_descriptor failed with error: %d", |
| 216 | get_descriptor_error); | ||
| 235 | return false; | 217 | return false; |
| 236 | } | 218 | } |
| 237 | 219 | ||
| @@ -239,35 +221,36 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { | |||
| 239 | // This isn’t the device we are looking for. | 221 | // This isn’t the device we are looking for. |
| 240 | return false; | 222 | return false; |
| 241 | } | 223 | } |
| 242 | ret = libusb_open(device, &usb_adapter_handle); | 224 | const int open_error = libusb_open(device, &usb_adapter_handle); |
| 243 | 225 | ||
| 244 | if (ret == LIBUSB_ERROR_ACCESS) { | 226 | if (open_error == LIBUSB_ERROR_ACCESS) { |
| 245 | LOG_ERROR(Input, "Yuzu can not gain access to this device: ID %04X:%04X.", desc.idVendor, | 227 | LOG_ERROR(Input, "Yuzu can not gain access to this device: ID %04X:%04X.", desc.idVendor, |
| 246 | desc.idProduct); | 228 | desc.idProduct); |
| 247 | return false; | 229 | return false; |
| 248 | } | 230 | } |
| 249 | if (ret) { | 231 | if (open_error) { |
| 250 | LOG_ERROR(Input, "libusb_open failed to open device with error = %d", ret); | 232 | LOG_ERROR(Input, "libusb_open failed to open device with error = %d", open_error); |
| 251 | return false; | 233 | return false; |
| 252 | } | 234 | } |
| 253 | 235 | ||
| 254 | ret = libusb_kernel_driver_active(usb_adapter_handle, 0); | 236 | int kernel_driver_error = libusb_kernel_driver_active(usb_adapter_handle, 0); |
| 255 | if (ret == 1) { | 237 | if (kernel_driver_error == 1) { |
| 256 | ret = libusb_detach_kernel_driver(usb_adapter_handle, 0); | 238 | kernel_driver_error = libusb_detach_kernel_driver(usb_adapter_handle, 0); |
| 257 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { | 239 | if (kernel_driver_error != 0 && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) { |
| 258 | LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", ret); | 240 | LOG_ERROR(Input, "libusb_detach_kernel_driver failed with error = %d", |
| 241 | kernel_driver_error); | ||
| 259 | } | 242 | } |
| 260 | } | 243 | } |
| 261 | 244 | ||
| 262 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) { | 245 | if (kernel_driver_error && kernel_driver_error != LIBUSB_ERROR_NOT_SUPPORTED) { |
| 263 | libusb_close(usb_adapter_handle); | 246 | libusb_close(usb_adapter_handle); |
| 264 | usb_adapter_handle = nullptr; | 247 | usb_adapter_handle = nullptr; |
| 265 | return false; | 248 | return false; |
| 266 | } | 249 | } |
| 267 | 250 | ||
| 268 | ret = libusb_claim_interface(usb_adapter_handle, 0); | 251 | const int interface_claim_error = libusb_claim_interface(usb_adapter_handle, 0); |
| 269 | if (ret) { | 252 | if (interface_claim_error) { |
| 270 | LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", ret); | 253 | LOG_ERROR(Input, "libusb_claim_interface failed with error = %d", interface_claim_error); |
| 271 | libusb_close(usb_adapter_handle); | 254 | libusb_close(usb_adapter_handle); |
| 272 | usb_adapter_handle = nullptr; | 255 | usb_adapter_handle = nullptr; |
| 273 | return false; | 256 | return false; |