diff options
Diffstat (limited to 'src/input_common/gcadapter')
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 350 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.h | 116 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.cpp | 310 | ||||
| -rw-r--r-- | src/input_common/gcadapter/gc_poller.h | 59 |
4 files changed, 835 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 | |||
| 9 | Common::SPSCQueue<GCPadStatus> pad_queue[4]; | ||
| 10 | struct GCState state[4]; | ||
| 11 | |||
| 12 | namespace GCAdapter { | ||
| 13 | |||
| 14 | static libusb_device_handle* usb_adapter_handle = nullptr; | ||
| 15 | static u8 adapter_controllers_status[4] = { | ||
| 16 | ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, | ||
| 17 | ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; | ||
| 18 | |||
| 19 | static std::mutex s_mutex; | ||
| 20 | |||
| 21 | static std::thread adapter_input_thread; | ||
| 22 | static bool adapter_thread_running; | ||
| 23 | |||
| 24 | static std::mutex initialization_mutex; | ||
| 25 | static std::thread detect_thread; | ||
| 26 | static bool detect_thread_running = false; | ||
| 27 | |||
| 28 | static libusb_context* libusb_ctx; | ||
| 29 | |||
| 30 | static u8 input_endpoint = 0; | ||
| 31 | |||
| 32 | static bool configuring = false; | ||
| 33 | |||
| 34 | GCPadStatus 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 | |||
| 88 | void 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 | |||
| 110 | static 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 | |||
| 175 | static 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 | |||
| 187 | void 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 | |||
| 199 | void 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 | |||
| 209 | void StopScanThread() { | ||
| 210 | detect_thread.join(); | ||
| 211 | } | ||
| 212 | |||
| 213 | static 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 | |||
| 234 | static 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 | |||
| 284 | static 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 | |||
| 305 | void Shutdown() { | ||
| 306 | StopScanThread(); | ||
| 307 | Reset(); | ||
| 308 | |||
| 309 | current_status = NO_ADAPTER_DETECTED; | ||
| 310 | } | ||
| 311 | |||
| 312 | static 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 | |||
| 334 | bool DeviceConnected(int port) { | ||
| 335 | return adapter_controllers_status[port] != ControllerTypes::CONTROLLER_NONE; | ||
| 336 | } | ||
| 337 | |||
| 338 | void ResetDeviceType(int port) { | ||
| 339 | adapter_controllers_status[port] = ControllerTypes::CONTROLLER_NONE; | ||
| 340 | } | ||
| 341 | |||
| 342 | void BeginConfiguration() { | ||
| 343 | configuring = true; | ||
| 344 | } | ||
| 345 | |||
| 346 | void EndConfiguration() { | ||
| 347 | configuring = false; | ||
| 348 | } | ||
| 349 | |||
| 350 | } // end of namespace GCAdapter | ||
diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h new file mode 100644 index 000000000..9b02d1382 --- /dev/null +++ b/src/input_common/gcadapter/gc_adapter.h | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | #pragma once | ||
| 2 | #include <algorithm> | ||
| 3 | #include <libusb.h> | ||
| 4 | #include <mutex> | ||
| 5 | #include <functional> | ||
| 6 | #include "common/common_types.h" | ||
| 7 | |||
| 8 | |||
| 9 | enum { | ||
| 10 | PAD_USE_ORIGIN = 0x0080, | ||
| 11 | PAD_GET_ORIGIN = 0x2000, | ||
| 12 | PAD_ERR_STATUS = 0x8000, | ||
| 13 | }; | ||
| 14 | |||
| 15 | enum PadButton { | ||
| 16 | PAD_BUTTON_LEFT = 0x0001, | ||
| 17 | PAD_BUTTON_RIGHT = 0x0002, | ||
| 18 | PAD_BUTTON_DOWN = 0x0004, | ||
| 19 | PAD_BUTTON_UP = 0x0008, | ||
| 20 | PAD_TRIGGER_Z = 0x0010, | ||
| 21 | PAD_TRIGGER_R = 0x0020, | ||
| 22 | PAD_TRIGGER_L = 0x0040, | ||
| 23 | PAD_BUTTON_A = 0x0100, | ||
| 24 | PAD_BUTTON_B = 0x0200, | ||
| 25 | PAD_BUTTON_X = 0x0400, | ||
| 26 | PAD_BUTTON_Y = 0x0800, | ||
| 27 | PAD_BUTTON_START = 0x1000, | ||
| 28 | // Below is for compatibility with "AxisButton" type | ||
| 29 | PAD_STICK = 0x2000, | ||
| 30 | |||
| 31 | }; | ||
| 32 | |||
| 33 | enum PadAxes { STICK_X, STICK_Y, SUBSTICK_X, SUBSTICK_Y, TRIGGER_LEFT, TRIGGER_RIGHT }; | ||
| 34 | |||
| 35 | struct GCPadStatus { | ||
| 36 | u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits | ||
| 37 | u8 stickX; // 0 <= stickX <= 255 | ||
| 38 | u8 stickY; // 0 <= stickY <= 255 | ||
| 39 | u8 substickX; // 0 <= substickX <= 255 | ||
| 40 | u8 substickY; // 0 <= substickY <= 255 | ||
| 41 | u8 triggerLeft; // 0 <= triggerLeft <= 255 | ||
| 42 | u8 triggerRight; // 0 <= triggerRight <= 255 | ||
| 43 | bool isConnected{true}; | ||
| 44 | |||
| 45 | static const u8 MAIN_STICK_CENTER_X = 0x80; | ||
| 46 | static const u8 MAIN_STICK_CENTER_Y = 0x80; | ||
| 47 | static const u8 MAIN_STICK_RADIUS = 0x7f; | ||
| 48 | static const u8 C_STICK_CENTER_X = 0x80; | ||
| 49 | static const u8 C_STICK_CENTER_Y = 0x80; | ||
| 50 | static const u8 C_STICK_RADIUS = 0x7f; | ||
| 51 | |||
| 52 | static const u8 TRIGGER_CENTER = 20; | ||
| 53 | static const u8 THRESHOLD = 10; | ||
| 54 | u8 port; | ||
| 55 | u8 axis_which = 255; | ||
| 56 | u8 axis_value = 255; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct GCState { | ||
| 60 | std::unordered_map<int, bool> buttons; | ||
| 61 | std::unordered_map<int, u16> axes; | ||
| 62 | }; | ||
| 63 | |||
| 64 | |||
| 65 | namespace GCAdapter { | ||
| 66 | enum ControllerTypes { | ||
| 67 | CONTROLLER_NONE = 0, | ||
| 68 | CONTROLLER_WIRED = 1, | ||
| 69 | CONTROLLER_WIRELESS = 2 | ||
| 70 | }; | ||
| 71 | |||
| 72 | enum { | ||
| 73 | NO_ADAPTER_DETECTED = 0, | ||
| 74 | ADAPTER_DETECTED = 1, | ||
| 75 | }; | ||
| 76 | |||
| 77 | // Current adapter status: detected/not detected/in error (holds the error code) | ||
| 78 | static int current_status = NO_ADAPTER_DETECTED; | ||
| 79 | |||
| 80 | GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); | ||
| 81 | /// Initialize the GC Adapter capture and read sequence | ||
| 82 | void Init(); | ||
| 83 | |||
| 84 | /// Close the adapter read thread and release the adapter | ||
| 85 | void Shutdown(); | ||
| 86 | |||
| 87 | /// Begin scanning for the GC Adapter. | ||
| 88 | void StartScanThread(); | ||
| 89 | |||
| 90 | /// Stop scanning for the adapter | ||
| 91 | void StopScanThread(); | ||
| 92 | |||
| 93 | /// Returns true if there is a device connected to port | ||
| 94 | bool DeviceConnected(int port); | ||
| 95 | |||
| 96 | /// Resets status of device connected to port | ||
| 97 | void ResetDeviceType(int port); | ||
| 98 | |||
| 99 | /// Returns true if we successfully gain access to GC Adapter | ||
| 100 | bool CheckDeviceAccess(libusb_device* device); | ||
| 101 | |||
| 102 | /// Captures GC Adapter endpoint address, | ||
| 103 | void GetGCEndpoint(libusb_device* device); | ||
| 104 | |||
| 105 | /// For shutting down, clear all data, join all threads, release usb | ||
| 106 | void Reset(); | ||
| 107 | |||
| 108 | /// For use in initialization, querying devices to find the adapter | ||
| 109 | void Setup(); | ||
| 110 | |||
| 111 | /// Used for polling | ||
| 112 | void BeginConfiguration(); | ||
| 113 | |||
| 114 | void EndConfiguration(); | ||
| 115 | |||
| 116 | } // end of namespace GCAdapter | ||
diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp new file mode 100644 index 000000000..772bd8890 --- /dev/null +++ b/src/input_common/gcadapter/gc_poller.cpp | |||
| @@ -0,0 +1,310 @@ | |||
| 1 | #include <atomic> | ||
| 2 | #include <list> | ||
| 3 | #include <mutex> | ||
| 4 | #include <utility> | ||
| 5 | #include "input_common/gcadapter/gc_poller.h" | ||
| 6 | #include "input_common/gcadapter/gc_adapter.h" | ||
| 7 | #include "common/threadsafe_queue.h" | ||
| 8 | |||
| 9 | // Using extern as to avoid multply defined symbols. | ||
| 10 | extern Common::SPSCQueue<GCPadStatus> pad_queue[4]; | ||
| 11 | extern struct GCState state[4]; | ||
| 12 | |||
| 13 | namespace InputCommon { | ||
| 14 | |||
| 15 | class GCButton final : public Input::ButtonDevice { | ||
| 16 | public: | ||
| 17 | explicit GCButton(int port_, int button_, int axis_) | ||
| 18 | : port(port_), button(button_) { | ||
| 19 | } | ||
| 20 | |||
| 21 | ~GCButton() override; | ||
| 22 | |||
| 23 | bool GetStatus() const override { | ||
| 24 | return state[port].buttons.at(button); | ||
| 25 | } | ||
| 26 | |||
| 27 | private: | ||
| 28 | const int port; | ||
| 29 | const int button; | ||
| 30 | }; | ||
| 31 | |||
| 32 | class GCAxisButton final : public Input::ButtonDevice { | ||
| 33 | public: | ||
| 34 | explicit GCAxisButton(int port_, int axis_, float threshold_, | ||
| 35 | bool trigger_if_greater_) | ||
| 36 | : port(port_), axis(axis_), threshold(threshold_), | ||
| 37 | trigger_if_greater(trigger_if_greater_) { | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | bool GetStatus() const override { | ||
| 42 | const float axis_value = (state[port].axes.at(axis) - 128.0f) / 128.0f; | ||
| 43 | if (trigger_if_greater) { | ||
| 44 | return axis_value > 0.10f; //TODO(ameerj) : Fix threshold. | ||
| 45 | } | ||
| 46 | return axis_value < -0.10f; | ||
| 47 | } | ||
| 48 | |||
| 49 | private: | ||
| 50 | const int port; | ||
| 51 | const int axis; | ||
| 52 | float threshold; | ||
| 53 | bool trigger_if_greater; | ||
| 54 | }; | ||
| 55 | |||
| 56 | GCButtonFactory::GCButtonFactory() { | ||
| 57 | GCAdapter::Init(); | ||
| 58 | } | ||
| 59 | |||
| 60 | GCButton::~GCButton() { | ||
| 61 | GCAdapter::Shutdown(); | ||
| 62 | } | ||
| 63 | |||
| 64 | std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { | ||
| 65 | int button_id = params.Get("button", 0); | ||
| 66 | int port = params.Get("port", 0); | ||
| 67 | // For Axis buttons, used by the binary sticks. | ||
| 68 | if (params.Has("axis")) { | ||
| 69 | const int axis = params.Get("axis", 0); | ||
| 70 | const float threshold = params.Get("threshold", 0.5f); | ||
| 71 | const std::string direction_name = params.Get("direction", ""); | ||
| 72 | bool trigger_if_greater; | ||
| 73 | if (direction_name == "+") { | ||
| 74 | trigger_if_greater = true; | ||
| 75 | } else if (direction_name == "-") { | ||
| 76 | trigger_if_greater = false; | ||
| 77 | } else { | ||
| 78 | trigger_if_greater = true; | ||
| 79 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 80 | } | ||
| 81 | return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater); | ||
| 82 | } | ||
| 83 | |||
| 84 | std::unique_ptr<GCButton> button = | ||
| 85 | std::make_unique<GCButton>(port, button_id, params.Get("axis", 0)); | ||
| 86 | return std::move(button); | ||
| 87 | } | ||
| 88 | |||
| 89 | Common::ParamPackage GCButtonFactory::GetNextInput() { | ||
| 90 | Common::ParamPackage params; | ||
| 91 | GCPadStatus pad; | ||
| 92 | for (int i = 0; i < 4; i++) { | ||
| 93 | while (pad_queue[i].Pop(pad)) { | ||
| 94 | // This while loop will break on the earliest detected button | ||
| 95 | params.Set("engine", "gcpad"); | ||
| 96 | params.Set("port", i); | ||
| 97 | // I was debating whether to keep these verbose for ease of reading | ||
| 98 | // or to use a while loop shifting the bits to test and set the value. | ||
| 99 | if (pad.button & PAD_BUTTON_A) { | ||
| 100 | params.Set("button", PAD_BUTTON_A); | ||
| 101 | break; | ||
| 102 | } | ||
| 103 | if (pad.button & PAD_BUTTON_B) { | ||
| 104 | params.Set("button", PAD_BUTTON_B); | ||
| 105 | break; | ||
| 106 | } | ||
| 107 | if (pad.button & PAD_BUTTON_X) { | ||
| 108 | params.Set("button", PAD_BUTTON_X); | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | if (pad.button & PAD_BUTTON_Y) { | ||
| 112 | params.Set("button", PAD_BUTTON_Y); | ||
| 113 | break; | ||
| 114 | } | ||
| 115 | if (pad.button & PAD_BUTTON_DOWN) { | ||
| 116 | params.Set("button", PAD_BUTTON_DOWN); | ||
| 117 | break; | ||
| 118 | } | ||
| 119 | if (pad.button & PAD_BUTTON_LEFT) { | ||
| 120 | params.Set("button", PAD_BUTTON_LEFT); | ||
| 121 | break; | ||
| 122 | } | ||
| 123 | if (pad.button & PAD_BUTTON_RIGHT) { | ||
| 124 | params.Set("button", PAD_BUTTON_RIGHT); | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | if (pad.button & PAD_BUTTON_UP) { | ||
| 128 | params.Set("button", PAD_BUTTON_UP); | ||
| 129 | break; | ||
| 130 | } | ||
| 131 | if (pad.button & PAD_TRIGGER_L) { | ||
| 132 | params.Set("button", PAD_TRIGGER_L); | ||
| 133 | break; | ||
| 134 | } | ||
| 135 | if (pad.button & PAD_TRIGGER_R) { | ||
| 136 | params.Set("button", PAD_TRIGGER_R); | ||
| 137 | break; | ||
| 138 | } | ||
| 139 | if (pad.button & PAD_TRIGGER_Z) { | ||
| 140 | params.Set("button", PAD_TRIGGER_Z); | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | if (pad.button & PAD_BUTTON_START) { | ||
| 144 | params.Set("button", PAD_BUTTON_START); | ||
| 145 | break; | ||
| 146 | } | ||
| 147 | // For Axis button implementation | ||
| 148 | if (pad.axis_which != 255) { | ||
| 149 | params.Set("axis", pad.axis_which); | ||
| 150 | params.Set("button", PAD_STICK); | ||
| 151 | if (pad.axis_value > 128) { | ||
| 152 | params.Set("direction", "+"); | ||
| 153 | params.Set("threshold", "0.5"); | ||
| 154 | } else { | ||
| 155 | params.Set("direction", "-"); | ||
| 156 | params.Set("threshold", "-0.5"); | ||
| 157 | } | ||
| 158 | break; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | return params; | ||
| 163 | } | ||
| 164 | |||
| 165 | void GCButtonFactory::BeginConfiguration() { | ||
| 166 | polling = true; | ||
| 167 | for (int i = 0; i < 4; i++) | ||
| 168 | pad_queue[i].Clear(); | ||
| 169 | GCAdapter::BeginConfiguration(); | ||
| 170 | } | ||
| 171 | |||
| 172 | void GCButtonFactory::EndConfiguration() { | ||
| 173 | polling = false; | ||
| 174 | |||
| 175 | for (int i = 0; i < 4; i++) | ||
| 176 | pad_queue[i].Clear(); | ||
| 177 | GCAdapter::EndConfiguration(); | ||
| 178 | } | ||
| 179 | |||
| 180 | class GCAnalog final : public Input::AnalogDevice { | ||
| 181 | public: | ||
| 182 | GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_) | ||
| 183 | : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) { | ||
| 184 | } | ||
| 185 | |||
| 186 | float GetAxis(int axis) const { | ||
| 187 | std::lock_guard lock{mutex}; | ||
| 188 | // division is not by a perfect 128 to account for some variance in center location | ||
| 189 | // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range [20-230] | ||
| 190 | return (state[port].axes.at(axis) - 128.0f) / 95.0f; | ||
| 191 | } | ||
| 192 | |||
| 193 | std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { | ||
| 194 | float x = GetAxis(axis_x); | ||
| 195 | float y = GetAxis(axis_y); | ||
| 196 | |||
| 197 | // Make sure the coordinates are in the unit circle, | ||
| 198 | // otherwise normalize it. | ||
| 199 | float r = x * x + y * y; | ||
| 200 | if (r > 1.0f) { | ||
| 201 | r = std::sqrt(r); | ||
| 202 | x /= r; | ||
| 203 | y /= r; | ||
| 204 | } | ||
| 205 | |||
| 206 | return std::make_tuple(x, y); | ||
| 207 | } | ||
| 208 | |||
| 209 | std::tuple<float, float> GetStatus() const override { | ||
| 210 | const auto [x, y] = GetAnalog(axis_x, axis_y); | ||
| 211 | const float r = std::sqrt((x * x) + (y * y)); | ||
| 212 | if (r > deadzone) { | ||
| 213 | return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), | ||
| 214 | y / r * (r - deadzone) / (1 - deadzone)); | ||
| 215 | } | ||
| 216 | return std::make_tuple<float, float>(0.0f, 0.0f); | ||
| 217 | } | ||
| 218 | |||
| 219 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | ||
| 220 | const auto [x, y] = GetStatus(); | ||
| 221 | const float directional_deadzone = 0.4f; | ||
| 222 | switch (direction) { | ||
| 223 | case Input::AnalogDirection::RIGHT: | ||
| 224 | return x > directional_deadzone; | ||
| 225 | case Input::AnalogDirection::LEFT: | ||
| 226 | return x < -directional_deadzone; | ||
| 227 | case Input::AnalogDirection::UP: | ||
| 228 | return y > directional_deadzone; | ||
| 229 | case Input::AnalogDirection::DOWN: | ||
| 230 | return y < -directional_deadzone; | ||
| 231 | } | ||
| 232 | return false; | ||
| 233 | } | ||
| 234 | |||
| 235 | private: | ||
| 236 | const int port; | ||
| 237 | const int axis_x; | ||
| 238 | const int axis_y; | ||
| 239 | const float deadzone; | ||
| 240 | mutable std::mutex mutex; | ||
| 241 | }; | ||
| 242 | |||
| 243 | |||
| 244 | /// An analog device factory that creates analog devices from GC Adapter | ||
| 245 | GCAnalogFactory::GCAnalogFactory() {}; | ||
| 246 | |||
| 247 | |||
| 248 | /** | ||
| 249 | * Creates analog device from joystick axes | ||
| 250 | * @param params contains parameters for creating the device: | ||
| 251 | * - "port": the nth gcpad on the adapter | ||
| 252 | * - "axis_x": the index of the axis to be bind as x-axis | ||
| 253 | * - "axis_y": the index of the axis to be bind as y-axis | ||
| 254 | */ | ||
| 255 | std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) { | ||
| 256 | const std::string guid = params.Get("guid", "0"); | ||
| 257 | const int port = params.Get("port", 0); | ||
| 258 | const int axis_x = params.Get("axis_x", 0); | ||
| 259 | const int axis_y = params.Get("axis_y", 1); | ||
| 260 | const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); | ||
| 261 | |||
| 262 | return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone); | ||
| 263 | } | ||
| 264 | |||
| 265 | void GCAnalogFactory::BeginConfiguration() { | ||
| 266 | polling = true; | ||
| 267 | for (int i = 0; i < 4; i++) | ||
| 268 | pad_queue[i].Clear(); | ||
| 269 | GCAdapter::BeginConfiguration(); | ||
| 270 | } | ||
| 271 | |||
| 272 | void GCAnalogFactory::EndConfiguration() { | ||
| 273 | polling = false; | ||
| 274 | for (int i = 0; i < 4; i++) | ||
| 275 | pad_queue[i].Clear(); | ||
| 276 | GCAdapter::EndConfiguration(); | ||
| 277 | } | ||
| 278 | |||
| 279 | Common::ParamPackage GCAnalogFactory::GetNextInput() { | ||
| 280 | GCPadStatus pad; | ||
| 281 | for (int i = 0; i < 4; i++) { | ||
| 282 | while (pad_queue[i].Pop(pad)) { | ||
| 283 | if (pad.axis_which == 255 || std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { | ||
| 284 | continue; | ||
| 285 | } | ||
| 286 | // An analog device needs two axes, so we need to store the axis for later and wait for | ||
| 287 | // a second SDL event. The axes also must be from the same joystick. | ||
| 288 | const int axis = pad.axis_which; | ||
| 289 | if (analog_x_axis == -1) { | ||
| 290 | analog_x_axis = axis; | ||
| 291 | controller_number = i; | ||
| 292 | } else if (analog_y_axis == -1 && analog_x_axis != axis && controller_number == i) { | ||
| 293 | analog_y_axis = axis; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | Common::ParamPackage params; | ||
| 298 | if (analog_x_axis != -1 && analog_y_axis != -1) { | ||
| 299 | params.Set("engine", "gcpad"); | ||
| 300 | params.Set("port", controller_number); | ||
| 301 | params.Set("axis_x", analog_x_axis); | ||
| 302 | params.Set("axis_y", analog_y_axis); | ||
| 303 | analog_x_axis = -1; | ||
| 304 | analog_y_axis = -1; | ||
| 305 | controller_number = -1; | ||
| 306 | return params; | ||
| 307 | } | ||
| 308 | return params; | ||
| 309 | } | ||
| 310 | } // namespace InputCommon | ||
diff --git a/src/input_common/gcadapter/gc_poller.h b/src/input_common/gcadapter/gc_poller.h new file mode 100644 index 000000000..d115b1d2a --- /dev/null +++ b/src/input_common/gcadapter/gc_poller.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <memory> | ||
| 4 | #include "core/frontend/input.h" | ||
| 5 | |||
| 6 | namespace InputCommon { | ||
| 7 | |||
| 8 | |||
| 9 | /** | ||
| 10 | * A button device factory representing a gcpad. It receives gcpad events and forward them | ||
| 11 | * to all button devices it created. | ||
| 12 | */ | ||
| 13 | class GCButtonFactory final : public Input::Factory<Input::ButtonDevice> { | ||
| 14 | public: | ||
| 15 | GCButtonFactory(); | ||
| 16 | |||
| 17 | /** | ||
| 18 | * Creates a button device from a button press | ||
| 19 | * @param params contains parameters for creating the device: | ||
| 20 | * - "code": the code of the key to bind with the button | ||
| 21 | */ | ||
| 22 | std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override; | ||
| 23 | |||
| 24 | Common::ParamPackage GetNextInput(); | ||
| 25 | |||
| 26 | /// For device input configuration/polling | ||
| 27 | void BeginConfiguration(); | ||
| 28 | void EndConfiguration(); | ||
| 29 | |||
| 30 | bool IsPolling() { | ||
| 31 | return polling; | ||
| 32 | } | ||
| 33 | |||
| 34 | private: | ||
| 35 | bool polling = false; | ||
| 36 | }; | ||
| 37 | |||
| 38 | /// An analog device factory that creates analog devices from GC Adapter | ||
| 39 | class GCAnalogFactory final : public Input::Factory<Input::AnalogDevice> { | ||
| 40 | public: | ||
| 41 | GCAnalogFactory(); | ||
| 42 | std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override; | ||
| 43 | Common::ParamPackage GetNextInput(); | ||
| 44 | |||
| 45 | /// For device input configuration/polling | ||
| 46 | void BeginConfiguration(); | ||
| 47 | void EndConfiguration(); | ||
| 48 | |||
| 49 | bool IsPolling() { | ||
| 50 | return polling; | ||
| 51 | } | ||
| 52 | |||
| 53 | private: | ||
| 54 | int analog_x_axis = -1; | ||
| 55 | int analog_y_axis = -1; | ||
| 56 | int controller_number = -1; | ||
| 57 | bool polling = false; | ||
| 58 | }; | ||
| 59 | } // namespace InputCommon | ||