diff options
| author | Pierre Bourdon <delroth@gmail.com> | 2016-06-24 10:43:46 +0200 |
|---|---|---|
| committer | Pierre Bourdon <delroth@gmail.com> | 2016-06-24 10:43:46 +0200 |
| commit | 3570c7f03a2aa90aa634f96c0af1969af610f14d (patch) | |
| tree | 4252e03c0e853ba7cffe022937698c854ad0eaa9 /Source/Core/InputCommon/GCAdapter.cpp | |
| parent | 2115e8a4a6814e32107b5205ff5c95bbd3c6e99c (diff) | |
Reformat all the things. Have fun with merge conflicts.
Diffstat (limited to 'Source/Core/InputCommon/GCAdapter.cpp')
| -rw-r--r-- | Source/Core/InputCommon/GCAdapter.cpp | 749 |
1 files changed, 388 insertions, 361 deletions
diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index a5ccb771cc..b86b0aa4c5 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -7,14 +7,14 @@ #include <mutex> #include "Common/Flag.h" -#include "Common/Thread.h" #include "Common/Logging/Log.h" +#include "Common/Thread.h" #include "Core/ConfigManager.h" #include "Core/Core.h" #include "Core/CoreTiming.h" -#include "Core/NetPlayProto.h" #include "Core/HW/SI.h" #include "Core/HW/SystemTimers.h" +#include "Core/NetPlayProto.h" #include "InputCommon/GCAdapter.h" #include "InputCommon/GCPadStatus.h" @@ -29,7 +29,9 @@ static void Setup(); static bool s_detected = false; static libusb_device_handle* s_handle = nullptr; -static u8 s_controller_type[MAX_SI_CHANNELS] = { ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE }; +static u8 s_controller_type[MAX_SI_CHANNELS] = { + ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, + ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; static u8 s_controller_rumble[4]; static std::mutex s_mutex; @@ -61,455 +63,480 @@ static u64 s_last_init = 0; static void Read() { - int payload_size = 0; - while (s_adapter_thread_running.IsSet()) - { - libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, sizeof(s_controller_payload_swap), &payload_size, 16); - - { - std::lock_guard<std::mutex> lk(s_mutex); - std::swap(s_controller_payload_swap, s_controller_payload); - s_controller_payload_size.store(payload_size); - } - - Common::YieldCPU(); - } + int payload_size = 0; + while (s_adapter_thread_running.IsSet()) + { + libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, + sizeof(s_controller_payload_swap), &payload_size, 16); + + { + std::lock_guard<std::mutex> lk(s_mutex); + std::swap(s_controller_payload_swap, s_controller_payload); + s_controller_payload_size.store(payload_size); + } + + Common::YieldCPU(); + } } #if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 -static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event, void* user_data) +static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event, + void* user_data) { - if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) - { - if (s_handle == nullptr && CheckDeviceAccess(dev)) - { - std::lock_guard<std::mutex> lk(s_init_mutex); - AddGCAdapter(dev); - } - } - else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) - { - if (s_handle != nullptr && libusb_get_device(s_handle) == dev) - Reset(); - } - return 0; + if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) + { + if (s_handle == nullptr && CheckDeviceAccess(dev)) + { + std::lock_guard<std::mutex> lk(s_init_mutex); + AddGCAdapter(dev); + } + } + else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) + { + if (s_handle != nullptr && libusb_get_device(s_handle) == dev) + Reset(); + } + return 0; } #endif static void ScanThreadFunc() { - Common::SetCurrentThreadName("GC Adapter Scanning Thread"); - NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread started"); + Common::SetCurrentThreadName("GC Adapter Scanning Thread"); + NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread started"); #if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 - s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0; - if (s_libusb_hotplug_enabled) - { - if (libusb_hotplug_register_callback(s_libusb_context, (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback, nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS) - s_libusb_hotplug_enabled = false; - if (s_libusb_hotplug_enabled) - NOTICE_LOG(SERIALINTERFACE, "Using libUSB hotplug detection"); - } + s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0; + if (s_libusb_hotplug_enabled) + { + if (libusb_hotplug_register_callback( + s_libusb_context, (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), + LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback, + nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS) + s_libusb_hotplug_enabled = false; + if (s_libusb_hotplug_enabled) + NOTICE_LOG(SERIALINTERFACE, "Using libUSB hotplug detection"); + } #endif - while (s_adapter_detect_thread_running.IsSet()) - { - if (s_libusb_hotplug_enabled) - { - static timeval tv = {0, 500000}; - libusb_handle_events_timeout(s_libusb_context, &tv); - } - else - { - if (s_handle == nullptr) - { - std::lock_guard<std::mutex> lk(s_init_mutex); - Setup(); - if (s_detected && s_detect_callback != nullptr) - s_detect_callback(); - } - Common::SleepCurrentThread(500); - } - } - NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped"); + while (s_adapter_detect_thread_running.IsSet()) + { + if (s_libusb_hotplug_enabled) + { + static timeval tv = {0, 500000}; + libusb_handle_events_timeout(s_libusb_context, &tv); + } + else + { + if (s_handle == nullptr) + { + std::lock_guard<std::mutex> lk(s_init_mutex); + Setup(); + if (s_detected && s_detect_callback != nullptr) + s_detect_callback(); + } + Common::SleepCurrentThread(500); + } + } + NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped"); } void SetAdapterCallback(std::function<void(void)> func) { - s_detect_callback = func; + s_detect_callback = func; } void Init() { - if (s_handle != nullptr) - return; - - if (Core::GetState() != Core::CORE_UNINITIALIZED) - { - if ((CoreTiming::GetTicks() - s_last_init) < SystemTimers::GetTicksPerSecond()) - return; - - s_last_init = CoreTiming::GetTicks(); - } - - s_libusb_driver_not_supported = false; - - int ret = libusb_init(&s_libusb_context); - - if (ret) - { - ERROR_LOG(SERIALINTERFACE, "libusb_init failed with error: %d", ret); - s_libusb_driver_not_supported = true; - Shutdown(); - } - else - { - if (UseAdapter()) - StartScanThread(); - } + if (s_handle != nullptr) + return; + + if (Core::GetState() != Core::CORE_UNINITIALIZED) + { + if ((CoreTiming::GetTicks() - s_last_init) < SystemTimers::GetTicksPerSecond()) + return; + + s_last_init = CoreTiming::GetTicks(); + } + + s_libusb_driver_not_supported = false; + + int ret = libusb_init(&s_libusb_context); + + if (ret) + { + ERROR_LOG(SERIALINTERFACE, "libusb_init failed with error: %d", ret); + s_libusb_driver_not_supported = true; + Shutdown(); + } + else + { + if (UseAdapter()) + StartScanThread(); + } } void StartScanThread() { - if (s_adapter_detect_thread_running.IsSet()) - return; + if (s_adapter_detect_thread_running.IsSet()) + return; - s_adapter_detect_thread_running.Set(true); - s_adapter_detect_thread = std::thread(ScanThreadFunc); + s_adapter_detect_thread_running.Set(true); + s_adapter_detect_thread = std::thread(ScanThreadFunc); } void StopScanThread() { - if (s_adapter_detect_thread_running.TestAndClear()) - { - s_adapter_detect_thread.join(); - } + if (s_adapter_detect_thread_running.TestAndClear()) + { + s_adapter_detect_thread.join(); + } } static void Setup() { - libusb_device** list; - ssize_t cnt = libusb_get_device_list(s_libusb_context, &list); - - for (int i = 0; i < MAX_SI_CHANNELS; i++) - { - s_controller_type[i] = ControllerTypes::CONTROLLER_NONE; - s_controller_rumble[i] = 0; - } - - for (int d = 0; d < cnt; d++) - { - libusb_device* device = list[d]; - if (CheckDeviceAccess(device)) - { - // Only connect to a single adapter in case the user has multiple connected - AddGCAdapter(device); - break; - } - } - - libusb_free_device_list(list, 1); + libusb_device** list; + ssize_t cnt = libusb_get_device_list(s_libusb_context, &list); + + for (int i = 0; i < MAX_SI_CHANNELS; i++) + { + s_controller_type[i] = ControllerTypes::CONTROLLER_NONE; + s_controller_rumble[i] = 0; + } + + for (int d = 0; d < cnt; d++) + { + libusb_device* device = list[d]; + if (CheckDeviceAccess(device)) + { + // Only connect to a single adapter in case the user has multiple connected + AddGCAdapter(device); + break; + } + } + + libusb_free_device_list(list, 1); } static bool CheckDeviceAccess(libusb_device* device) { - int ret; - libusb_device_descriptor desc; - int dRet = libusb_get_device_descriptor(device, &desc); - if (dRet) - { - // could not acquire the descriptor, no point in trying to use it. - ERROR_LOG(SERIALINTERFACE, "libusb_get_device_descriptor failed with error: %d", dRet); - return false; - } - - if (desc.idVendor == 0x057e && desc.idProduct == 0x0337) - { - NOTICE_LOG(SERIALINTERFACE, "Found GC Adapter with Vendor: %X Product: %X Devnum: %d", desc.idVendor, desc.idProduct, 1); - - u8 bus = libusb_get_bus_number(device); - u8 port = libusb_get_device_address(device); - ret = libusb_open(device, &s_handle); - if (ret) - { - if (ret == LIBUSB_ERROR_ACCESS) - { - if (dRet) - { - ERROR_LOG(SERIALINTERFACE, "Dolphin does not have access to this device: Bus %03d Device %03d: ID ????:???? (couldn't get id).", - bus, - port - ); - } - else - { - ERROR_LOG(SERIALINTERFACE, "Dolphin does not have access to this device: Bus %03d Device %03d: ID %04X:%04X.", - bus, - port, - desc.idVendor, - desc.idProduct - ); - } - } - else - { - ERROR_LOG(SERIALINTERFACE, "libusb_open failed to open device with error = %d", ret); - if (ret == LIBUSB_ERROR_NOT_SUPPORTED) - s_libusb_driver_not_supported = true; - } - return false; - } - else if ((ret = libusb_kernel_driver_active(s_handle, 0)) == 1) - { - if ((ret = libusb_detach_kernel_driver(s_handle, 0)) && ret != LIBUSB_ERROR_NOT_SUPPORTED) - { - ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret); - } - } - // this split is needed so that we don't avoid claiming the interface when - // detaching the kernel driver is successful - if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) - { - return false; - } - else if ((ret = libusb_claim_interface(s_handle, 0))) - { - ERROR_LOG(SERIALINTERFACE, "libusb_claim_interface failed with error: %d", ret); - } - else - { - return true; - } - } - return false; + int ret; + libusb_device_descriptor desc; + int dRet = libusb_get_device_descriptor(device, &desc); + if (dRet) + { + // could not acquire the descriptor, no point in trying to use it. + ERROR_LOG(SERIALINTERFACE, "libusb_get_device_descriptor failed with error: %d", dRet); + return false; + } + + if (desc.idVendor == 0x057e && desc.idProduct == 0x0337) + { + NOTICE_LOG(SERIALINTERFACE, "Found GC Adapter with Vendor: %X Product: %X Devnum: %d", + desc.idVendor, desc.idProduct, 1); + + u8 bus = libusb_get_bus_number(device); + u8 port = libusb_get_device_address(device); + ret = libusb_open(device, &s_handle); + if (ret) + { + if (ret == LIBUSB_ERROR_ACCESS) + { + if (dRet) + { + ERROR_LOG(SERIALINTERFACE, "Dolphin does not have access to this device: Bus %03d Device " + "%03d: ID ????:???? (couldn't get id).", + bus, port); + } + else + { + ERROR_LOG( + SERIALINTERFACE, + "Dolphin does not have access to this device: Bus %03d Device %03d: ID %04X:%04X.", + bus, port, desc.idVendor, desc.idProduct); + } + } + else + { + ERROR_LOG(SERIALINTERFACE, "libusb_open failed to open device with error = %d", ret); + if (ret == LIBUSB_ERROR_NOT_SUPPORTED) + s_libusb_driver_not_supported = true; + } + return false; + } + else if ((ret = libusb_kernel_driver_active(s_handle, 0)) == 1) + { + if ((ret = libusb_detach_kernel_driver(s_handle, 0)) && ret != LIBUSB_ERROR_NOT_SUPPORTED) + { + ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret); + } + } + // this split is needed so that we don't avoid claiming the interface when + // detaching the kernel driver is successful + if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) + { + return false; + } + else if ((ret = libusb_claim_interface(s_handle, 0))) + { + ERROR_LOG(SERIALINTERFACE, "libusb_claim_interface failed with error: %d", ret); + } + else + { + return true; + } + } + return false; } static void AddGCAdapter(libusb_device* device) { - libusb_config_descriptor *config = nullptr; - libusb_get_config_descriptor(device, 0, &config); - for (u8 ic = 0; ic < config->bNumInterfaces; ic++) - { - const libusb_interface *interfaceContainer = &config->interface[ic]; - for (int i = 0; i < interfaceContainer->num_altsetting; i++) - { - const libusb_interface_descriptor *interface = &interfaceContainer->altsetting[i]; - for (u8 e = 0; e < interface->bNumEndpoints; e++) - { - const libusb_endpoint_descriptor *endpoint = &interface->endpoint[e]; - if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) - s_endpoint_in = endpoint->bEndpointAddress; - else - s_endpoint_out = endpoint->bEndpointAddress; - } - } - } - - int tmp = 0; - unsigned char payload = 0x13; - libusb_interrupt_transfer(s_handle, s_endpoint_out, &payload, sizeof(payload), &tmp, 16); - - s_adapter_thread_running.Set(true); - s_adapter_thread = std::thread(Read); - - s_detected = true; - if (s_detect_callback != nullptr) - s_detect_callback(); - ResetRumbleLockNeeded(); + libusb_config_descriptor* config = nullptr; + libusb_get_config_descriptor(device, 0, &config); + for (u8 ic = 0; ic < config->bNumInterfaces; ic++) + { + const libusb_interface* interfaceContainer = &config->interface[ic]; + for (int i = 0; i < interfaceContainer->num_altsetting; i++) + { + const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; + for (u8 e = 0; e < interface->bNumEndpoints; e++) + { + const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; + if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) + s_endpoint_in = endpoint->bEndpointAddress; + else + s_endpoint_out = endpoint->bEndpointAddress; + } + } + } + + int tmp = 0; + unsigned char payload = 0x13; + libusb_interrupt_transfer(s_handle, s_endpoint_out, &payload, sizeof(payload), &tmp, 16); + + s_adapter_thread_running.Set(true); + s_adapter_thread = std::thread(Read); + + s_detected = true; + if (s_detect_callback != nullptr) + s_detect_callback(); + ResetRumbleLockNeeded(); } void Shutdown() { - StopScanThread(); + StopScanThread(); #if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 - if (s_libusb_hotplug_enabled) - libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle); + if (s_libusb_hotplug_enabled) + libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle); #endif - Reset(); + Reset(); - if (s_libusb_context) - { - libusb_exit(s_libusb_context); - s_libusb_context = nullptr; - } + if (s_libusb_context) + { + libusb_exit(s_libusb_context); + s_libusb_context = nullptr; + } - s_libusb_driver_not_supported = false; + s_libusb_driver_not_supported = false; } static void Reset() { - std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock); - if (!lock.try_lock()) - return; - if (!s_detected) - return; - - if (s_adapter_thread_running.TestAndClear()) - { - s_adapter_thread.join(); - } - - for (int i = 0; i < MAX_SI_CHANNELS; i++) - s_controller_type[i] = ControllerTypes::CONTROLLER_NONE; - - s_detected = false; - - if (s_handle) - { - libusb_release_interface(s_handle, 0); - libusb_close(s_handle); - s_handle = nullptr; - } - if (s_detect_callback != nullptr) - s_detect_callback(); - NOTICE_LOG(SERIALINTERFACE, "GC Adapter detached"); + std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock); + if (!lock.try_lock()) + return; + if (!s_detected) + return; + + if (s_adapter_thread_running.TestAndClear()) + { + s_adapter_thread.join(); + } + + for (int i = 0; i < MAX_SI_CHANNELS; i++) + s_controller_type[i] = ControllerTypes::CONTROLLER_NONE; + + s_detected = false; + + if (s_handle) + { + libusb_release_interface(s_handle, 0); + libusb_close(s_handle); + s_handle = nullptr; + } + if (s_detect_callback != nullptr) + s_detect_callback(); + NOTICE_LOG(SERIALINTERFACE, "GC Adapter detached"); } void Input(int chan, GCPadStatus* pad) { - if (!UseAdapter()) - return; - - if (s_handle == nullptr || !s_detected) - return; - - int payload_size = 0; - u8 controller_payload_copy[37]; - - { - std::lock_guard<std::mutex> lk(s_mutex); - std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), std::begin(controller_payload_copy)); - payload_size = s_controller_payload_size.load(); - } - - if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) - { - INFO_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", payload_size, controller_payload_copy[0]); - Reset(); - } - else - { - bool get_origin = false; - u8 type = controller_payload_copy[1 + (9 * chan)] >> 4; - if (type != ControllerTypes::CONTROLLER_NONE && s_controller_type[chan] == ControllerTypes::CONTROLLER_NONE) - { - NOTICE_LOG(SERIALINTERFACE, "New device connected to Port %d of Type: %02x", chan + 1, controller_payload_copy[1 + (9 * chan)]); - get_origin = true; - } - - s_controller_type[chan] = type; - - memset(pad, 0, sizeof(*pad)); - if (s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE) - { - u8 b1 = controller_payload_copy[1 + (9 * chan) + 1]; - u8 b2 = controller_payload_copy[1 + (9 * chan) + 2]; - - if (b1 & (1 << 0)) pad->button |= PAD_BUTTON_A; - if (b1 & (1 << 1)) pad->button |= PAD_BUTTON_B; - if (b1 & (1 << 2)) pad->button |= PAD_BUTTON_X; - if (b1 & (1 << 3)) pad->button |= PAD_BUTTON_Y; - - if (b1 & (1 << 4)) pad->button |= PAD_BUTTON_LEFT; - if (b1 & (1 << 5)) pad->button |= PAD_BUTTON_RIGHT; - if (b1 & (1 << 6)) pad->button |= PAD_BUTTON_DOWN; - if (b1 & (1 << 7)) pad->button |= PAD_BUTTON_UP; - - if (b2 & (1 << 0)) pad->button |= PAD_BUTTON_START; - if (b2 & (1 << 1)) pad->button |= PAD_TRIGGER_Z; - if (b2 & (1 << 2)) pad->button |= PAD_TRIGGER_R; - if (b2 & (1 << 3)) pad->button |= PAD_TRIGGER_L; - - if (get_origin) pad->button |= PAD_GET_ORIGIN; - - pad->stickX = controller_payload_copy[1 + (9 * chan) + 3]; - pad->stickY = controller_payload_copy[1 + (9 * chan) + 4]; - pad->substickX = controller_payload_copy[1 + (9 * chan) + 5]; - pad->substickY = controller_payload_copy[1 + (9 * chan) + 6]; - pad->triggerLeft = controller_payload_copy[1 + (9 * chan) + 7]; - pad->triggerRight = controller_payload_copy[1 + (9 * chan) + 8]; - } - else if (!NetPlay::IsNetPlayRunning()) - { - // This is a hack to prevent a netplay desync due to SI devices - // being different and returning different values. - // The corresponding code in DeviceGCAdapter has the same check - pad->button = PAD_ERR_STATUS; - } - } + if (!UseAdapter()) + return; + + if (s_handle == nullptr || !s_detected) + return; + + int payload_size = 0; + u8 controller_payload_copy[37]; + + { + std::lock_guard<std::mutex> lk(s_mutex); + std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), + std::begin(controller_payload_copy)); + payload_size = s_controller_payload_size.load(); + } + + if (payload_size != sizeof(controller_payload_copy) || + controller_payload_copy[0] != LIBUSB_DT_HID) + { + INFO_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", payload_size, + controller_payload_copy[0]); + Reset(); + } + else + { + bool get_origin = false; + u8 type = controller_payload_copy[1 + (9 * chan)] >> 4; + if (type != ControllerTypes::CONTROLLER_NONE && + s_controller_type[chan] == ControllerTypes::CONTROLLER_NONE) + { + NOTICE_LOG(SERIALINTERFACE, "New device connected to Port %d of Type: %02x", chan + 1, + controller_payload_copy[1 + (9 * chan)]); + get_origin = true; + } + + s_controller_type[chan] = type; + + memset(pad, 0, sizeof(*pad)); + if (s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE) + { + u8 b1 = controller_payload_copy[1 + (9 * chan) + 1]; + u8 b2 = controller_payload_copy[1 + (9 * chan) + 2]; + + if (b1 & (1 << 0)) + pad->button |= PAD_BUTTON_A; + if (b1 & (1 << 1)) + pad->button |= PAD_BUTTON_B; + if (b1 & (1 << 2)) + pad->button |= PAD_BUTTON_X; + if (b1 & (1 << 3)) + pad->button |= PAD_BUTTON_Y; + + if (b1 & (1 << 4)) + pad->button |= PAD_BUTTON_LEFT; + if (b1 & (1 << 5)) + pad->button |= PAD_BUTTON_RIGHT; + if (b1 & (1 << 6)) + pad->button |= PAD_BUTTON_DOWN; + if (b1 & (1 << 7)) + pad->button |= PAD_BUTTON_UP; + + if (b2 & (1 << 0)) + pad->button |= PAD_BUTTON_START; + if (b2 & (1 << 1)) + pad->button |= PAD_TRIGGER_Z; + if (b2 & (1 << 2)) + pad->button |= PAD_TRIGGER_R; + if (b2 & (1 << 3)) + pad->button |= PAD_TRIGGER_L; + + if (get_origin) + pad->button |= PAD_GET_ORIGIN; + + pad->stickX = controller_payload_copy[1 + (9 * chan) + 3]; + pad->stickY = controller_payload_copy[1 + (9 * chan) + 4]; + pad->substickX = controller_payload_copy[1 + (9 * chan) + 5]; + pad->substickY = controller_payload_copy[1 + (9 * chan) + 6]; + pad->triggerLeft = controller_payload_copy[1 + (9 * chan) + 7]; + pad->triggerRight = controller_payload_copy[1 + (9 * chan) + 8]; + } + else if (!NetPlay::IsNetPlayRunning()) + { + // This is a hack to prevent a netplay desync due to SI devices + // being different and returning different values. + // The corresponding code in DeviceGCAdapter has the same check + pad->button = PAD_ERR_STATUS; + } + } } bool DeviceConnected(int chan) { - return s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE; + return s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE; } bool UseAdapter() { - return SConfig::GetInstance().m_SIDevice[0] == SIDEVICE_WIIU_ADAPTER || - SConfig::GetInstance().m_SIDevice[1] == SIDEVICE_WIIU_ADAPTER || - SConfig::GetInstance().m_SIDevice[2] == SIDEVICE_WIIU_ADAPTER || - SConfig::GetInstance().m_SIDevice[3] == SIDEVICE_WIIU_ADAPTER; + return SConfig::GetInstance().m_SIDevice[0] == SIDEVICE_WIIU_ADAPTER || + SConfig::GetInstance().m_SIDevice[1] == SIDEVICE_WIIU_ADAPTER || + SConfig::GetInstance().m_SIDevice[2] == SIDEVICE_WIIU_ADAPTER || + SConfig::GetInstance().m_SIDevice[3] == SIDEVICE_WIIU_ADAPTER; } void ResetRumble() { - std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock); - if (!lock.try_lock()) - return; - ResetRumbleLockNeeded(); + std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock); + if (!lock.try_lock()) + return; + ResetRumbleLockNeeded(); } // Needs to be called when s_init_mutex is locked in order to avoid // being called while the libusb state is being reset static void ResetRumbleLockNeeded() { - if (!UseAdapter() || (s_handle == nullptr || !s_detected)) - { - return; - } + if (!UseAdapter() || (s_handle == nullptr || !s_detected)) + { + return; + } - std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0); + std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0); - unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3]}; + unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], + s_controller_rumble[2], s_controller_rumble[3]}; - int size = 0; - libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16); + int size = 0; + libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16); - DEBUG_LOG(SERIALINTERFACE, "Rumble state reset"); + DEBUG_LOG(SERIALINTERFACE, "Rumble state reset"); } void Output(int chan, u8 rumble_command) { - if (s_handle == nullptr || !UseAdapter() || !SConfig::GetInstance().m_AdapterRumble[chan]) - return; - - // Skip over rumble commands if it has not changed or the controller is wireless - if (rumble_command != s_controller_rumble[chan] && s_controller_type[chan] != ControllerTypes::CONTROLLER_WIRELESS) - { - s_controller_rumble[chan] = rumble_command; - - unsigned char rumble[5] = { 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3] }; - int size = 0; - - libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16); - // Netplay sends invalid data which results in size = 0x00. Ignore it. - if (size != 0x05 && size != 0x00) - { - INFO_LOG(SERIALINTERFACE, "error writing rumble (size: %d)", size); - Reset(); - } - } + if (s_handle == nullptr || !UseAdapter() || !SConfig::GetInstance().m_AdapterRumble[chan]) + return; + + // Skip over rumble commands if it has not changed or the controller is wireless + if (rumble_command != s_controller_rumble[chan] && + s_controller_type[chan] != ControllerTypes::CONTROLLER_WIRELESS) + { + s_controller_rumble[chan] = rumble_command; + + unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], + s_controller_rumble[2], s_controller_rumble[3]}; + int size = 0; + + libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16); + // Netplay sends invalid data which results in size = 0x00. Ignore it. + if (size != 0x05 && size != 0x00) + { + INFO_LOG(SERIALINTERFACE, "error writing rumble (size: %d)", size); + Reset(); + } + } } bool IsDetected() { - return s_detected; + return s_detected; } bool IsDriverDetected() { - return !s_libusb_driver_not_supported; + return !s_libusb_driver_not_supported; } -} // end of namespace GCAdapter +} // end of namespace GCAdapter |
