From dd6592698e1c2934ad6d67756d25aac5e2df96ef Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 19 Apr 2022 14:41:13 -0700 Subject: [PATCH 01/14] GCAdapter: Merge GCAdapter.cpp and GCAdapter_Android.cpp This is mostly a brainless merge, #ifdef-ing anything that doesn't match between the two while preserving common logic. I didn't rename any variables (although similar ones do exist), but I did change one log that was ERROR on android and NOTICE elsewhere to just always be NOTICE. Further merging will follow. --- Source/Core/InputCommon/CMakeLists.txt | 10 +- Source/Core/InputCommon/GCAdapter.cpp | 302 ++++++++++++- Source/Core/InputCommon/GCAdapter_Android.cpp | 420 ------------------ 3 files changed, 292 insertions(+), 440 deletions(-) delete mode 100644 Source/Core/InputCommon/GCAdapter_Android.cpp diff --git a/Source/Core/InputCommon/CMakeLists.txt b/Source/Core/InputCommon/CMakeLists.txt index 910d64d8d4..06f959a91a 100644 --- a/Source/Core/InputCommon/CMakeLists.txt +++ b/Source/Core/InputCommon/CMakeLists.txt @@ -1,6 +1,8 @@ add_library(inputcommon DynamicInputTextureManager.cpp DynamicInputTextureManager.h + GCAdapter.cpp + GCAdapter.h ImageOperations.cpp ImageOperations.h InputConfig.cpp @@ -143,13 +145,7 @@ elseif(ANDROID) ) endif() -if(ANDROID) - target_sources(inputcommon PRIVATE GCAdapter_Android.cpp) -else() - target_sources(inputcommon PRIVATE - GCAdapter.cpp - GCAdapter.h - ) +if(NOT ANDROID) target_link_libraries(inputcommon PUBLIC ${LIBUSB_LIBRARIES}) endif() diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index f567335223..86a595aaf4 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -1,30 +1,49 @@ // Copyright 2014 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "InputCommon/GCAdapter.h" + +#ifndef ANDROID +#define GCADAPTER_USE_LIBUSB_IMPLEMENTATION true +#define GCADAPTER_USE_ANDROID_IMPLEMENTATION false +#else +#define GCADAPTER_USE_LIBUSB_IMPLEMENTATION false +#define GCADAPTER_USE_ANDROID_IMPLEMENTATION true +#endif + #include #include -#include #include #include +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION +#include +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +#include +#endif + #include "Common/Event.h" #include "Common/Flag.h" #include "Common/Logging/Log.h" -#include "Common/ScopeGuard.h" #include "Common/Thread.h" #include "Core/Config/MainSettings.h" -#include "Core/ConfigManager.h" #include "Core/Core.h" #include "Core/CoreTiming.h" #include "Core/HW/SI/SI.h" #include "Core/HW/SI/SI_Device.h" #include "Core/HW/SystemTimers.h" -#include "Core/LibusbUtils.h" -#include "Core/NetPlayProto.h" - -#include "InputCommon/GCAdapter.h" #include "InputCommon/GCPadStatus.h" +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION +#include "Common/ScopeGuard.h" +#include "Core/ConfigManager.h" +#include "Core/LibusbUtils.h" +#include "Core/NetPlayProto.h" +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +#include "jni/AndroidCommon/IDCache.h" +#endif + +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if defined(LIBUSB_API_VERSION) #define LIBUSB_API_VERSION_EXIST 1 #else @@ -33,15 +52,19 @@ #define LIBUSB_API_VERSION_ATLEAST(v) (LIBUSB_API_VERSION_EXIST && LIBUSB_API_VERSION >= (v)) #define LIBUSB_API_HAS_HOTPLUG LIBUSB_API_VERSION_ATLEAST(0x01000102) +#endif namespace GCAdapter { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static bool CheckDeviceAccess(libusb_device* device); static void AddGCAdapter(libusb_device* device); static void ResetRumbleLockNeeded(); +#endif static void Reset(); static void Setup(); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION enum { NO_ADAPTER_DETECTED = 0, @@ -51,11 +74,20 @@ enum // Current adapter status: detected/not detected/in error (holds the error code) static std::atomic s_status = NO_ADAPTER_DETECTED; static libusb_device_handle* s_handle = nullptr; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +// Java classes +static jclass s_adapter_class; + +static bool s_detected = false; +static int s_fd = 0; +#endif + static std::array s_controller_type = { ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; static std::array s_controller_rumble{}; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::mutex s_mutex; static u8 s_controller_payload[37]; static u8 s_controller_payload_swap[37]; @@ -70,8 +102,29 @@ static Common::Flag s_adapter_thread_running; static Common::Event s_rumble_data_available; static std::mutex s_init_mutex; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +// Input handling +static std::mutex s_read_mutex; +static std::array s_controller_payload; +static int s_controller_payload_size = {0}; + +// Output handling +static std::mutex s_write_mutex; +static u8 s_controller_write_payload[5]; +static std::atomic s_controller_write_payload_size{0}; + +// Adapter running thread +static std::thread s_read_adapter_thread; +static Common::Flag s_read_adapter_thread_running; + +static Common::Flag s_write_adapter_thread_running; +static Common::Event s_write_happened; +#endif + static std::thread s_adapter_detect_thread; static Common::Flag s_adapter_detect_thread_running; + +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static Common::Event s_hotplug_event; static std::function s_detect_callback; @@ -89,18 +142,22 @@ static std::unique_ptr s_libusb_context; static u8 s_endpoint_in = 0; static u8 s_endpoint_out = 0; +#endif static u64 s_last_init = 0; static std::optional s_config_callback_id = std::nullopt; static std::array s_config_si_device_type{}; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::array s_config_rumble_enabled{}; +#endif static void Read() { Common::SetCurrentThreadName("GCAdapter Read Thread"); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int payload_size = 0; while (s_adapter_thread_running.IsSet()) { @@ -118,12 +175,73 @@ static void Read() Common::YieldCPU(); } +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter read thread started"); + + bool first_read = true; + JNIEnv* env = IDCache::GetEnvForThread(); + + jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "controller_payload", "[B"); + jobject payload_object = env->GetStaticObjectField(s_adapter_class, payload_field); + auto* java_controller_payload = reinterpret_cast(&payload_object); + + // Get function pointers + jmethodID getfd_func = env->GetStaticMethodID(s_adapter_class, "GetFD", "()I"); + jmethodID input_func = env->GetStaticMethodID(s_adapter_class, "Input", "()I"); + jmethodID openadapter_func = env->GetStaticMethodID(s_adapter_class, "OpenAdapter", "()Z"); + + bool connected = env->CallStaticBooleanMethod(s_adapter_class, openadapter_func); + + if (connected) + { + s_write_adapter_thread_running.Set(true); + std::thread write_adapter_thread(Write); + + // Reset rumble once on initial reading + ResetRumble(); + + while (s_read_adapter_thread_running.IsSet()) + { + int read_size = env->CallStaticIntMethod(s_adapter_class, input_func); + + jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); + { + std::lock_guard lk(s_read_mutex); + std::copy(java_data, java_data + s_controller_payload.size(), s_controller_payload.begin()); + s_controller_payload_size = read_size; + } + env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); + + if (first_read) + { + first_read = false; + s_fd = env->CallStaticIntMethod(s_adapter_class, getfd_func); + } + + Common::YieldCPU(); + } + + // Terminate the write thread on leaving + if (s_write_adapter_thread_running.TestAndClear()) + { + s_controller_write_payload_size.store(0); + s_write_happened.Set(); // Kick the waiting event + write_adapter_thread.join(); + } + } + + s_fd = 0; + s_detected = false; + + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter read thread stopped"); +#endif } static void Write() { Common::SetCurrentThreadName("GCAdapter Write Thread"); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int size = 0; while (true) @@ -146,8 +264,44 @@ static void Write() ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", libusb_error_name(err)); } +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter write thread started"); + + JNIEnv* env = IDCache::GetEnvForThread(); + jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I"); + + while (s_write_adapter_thread_running.IsSet()) + { + s_write_happened.Wait(); + int write_size = s_controller_write_payload_size.load(); + if (write_size) + { + jbyteArray jrumble_array = env->NewByteArray(5); + jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr); + + { + std::lock_guard lk(s_write_mutex); + memcpy(jrumble, s_controller_write_payload, write_size); + } + + env->ReleaseByteArrayElements(jrumble_array, jrumble, 0); + int size = env->CallStaticIntMethod(s_adapter_class, output_func, jrumble_array); + // Netplay sends invalid data which results in size = 0x00. Ignore it. + if (size != write_size && size != 0x00) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "error writing rumble (size: {})", size); + Reset(); + } + } + + Common::YieldCPU(); + } + + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter write thread stopped"); +#endif } +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if LIBUSB_API_HAS_HOTPLUG static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event, void* user_data) @@ -173,12 +327,14 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl return 0; } #endif +#endif static void ScanThreadFunc() { Common::SetCurrentThreadName("GC Adapter Scanning Thread"); NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread started"); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if LIBUSB_API_HAS_HOTPLUG #ifndef __FreeBSD__ s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0; @@ -210,12 +366,28 @@ static void ScanThreadFunc() else Common::SleepCurrentThread(500); } +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + JNIEnv* env = IDCache::GetEnvForThread(); + + jmethodID queryadapter_func = env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z"); + + while (s_adapter_detect_thread_running.IsSet()) + { + if (!s_detected && UseAdapter() && + env->CallStaticBooleanMethod(s_adapter_class, queryadapter_func)) + Setup(); + Common::SleepCurrentThread(1000); + } +#endif + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread stopped"); } -void SetAdapterCallback(std::function func) +void SetAdapterCallback(std::function func) { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_detect_callback = func; +#endif } static void RefreshConfig() @@ -223,16 +395,23 @@ static void RefreshConfig() for (int i = 0; i < SerialInterface::MAX_SI_CHANNELS; ++i) { s_config_si_device_type[i] = Config::Get(Config::GetInfoForSIDevice(i)); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_config_rumble_enabled[i] = Config::Get(Config::GetInfoForAdapterRumble(i)); +#endif } } void Init() { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION if (s_handle != nullptr) return; s_libusb_context = std::make_unique(); +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + if (s_fd) + return; +#endif if (Core::GetState() != Core::State::Uninitialized && Core::GetState() != Core::State::Starting) { @@ -242,7 +421,14 @@ void Init() s_last_init = CoreTiming::GetTicks(); } +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_status = NO_ADAPTER_DETECTED; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + JNIEnv* env = IDCache::GetEnvForThread(); + + jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_GCAdapter"); + s_adapter_class = reinterpret_cast(env->NewGlobalRef(adapter_class)); +#endif if (!s_config_callback_id) s_config_callback_id = Config::AddConfigChangedCallback(RefreshConfig); @@ -256,8 +442,10 @@ void StartScanThread() { if (s_adapter_detect_thread_running.IsSet()) return; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION if (!s_libusb_context->IsValid()) return; +#endif s_adapter_detect_thread_running.Set(true); s_adapter_detect_thread = std::thread(ScanThreadFunc); } @@ -266,13 +454,16 @@ void StopScanThread() { if (s_adapter_detect_thread_running.TestAndClear()) { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_hotplug_event.Set(); +#endif s_adapter_detect_thread.join(); } } static void Setup() { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int prev_status = s_status; // Reset the error status in case the adapter gets unplugged @@ -294,8 +485,20 @@ static void Setup() if (s_status != ADAPTER_DETECTED && prev_status != s_status && s_detect_callback != nullptr) s_detect_callback(); +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + s_fd = 0; + s_detected = true; + + // Make sure the thread isn't in the middle of shutting down while starting a new one + if (s_read_adapter_thread_running.TestAndClear()) + s_read_adapter_thread.join(); + + s_read_adapter_thread_running.Set(true); + s_read_adapter_thread = std::thread(Read); +#endif } +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static bool CheckDeviceAccess(libusb_device* device) { libusb_device_descriptor desc; @@ -414,18 +617,23 @@ static void AddGCAdapter(libusb_device* device) s_detect_callback(); ResetRumbleLockNeeded(); } +#endif void Shutdown() { StopScanThread(); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if LIBUSB_API_HAS_HOTPLUG if (s_libusb_context->IsValid() && s_libusb_hotplug_enabled) libusb_hotplug_deregister_callback(*s_libusb_context, s_hotplug_handle); +#endif #endif Reset(); - s_libusb_context.reset(); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + s_libusb_context.reset(); s_status = NO_ADAPTER_DETECTED; +#endif if (s_config_callback_id) { @@ -436,6 +644,7 @@ void Shutdown() static void Reset() { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION std::unique_lock lock(s_init_mutex, std::defer_lock); if (!lock.try_lock()) return; @@ -448,9 +657,17 @@ static void Reset() s_adapter_input_thread.join(); s_adapter_output_thread.join(); } +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + if (!s_detected) + return; + + if (s_read_adapter_thread_running.TestAndClear()) + s_read_adapter_thread.join(); +#endif s_controller_type.fill(ControllerTypes::CONTROLLER_NONE); +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_status = NO_ADAPTER_DETECTED; if (s_handle) @@ -461,6 +678,11 @@ static void Reset() } if (s_detect_callback != nullptr) s_detect_callback(); +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + s_detected = false; + s_fd = 0; +#endif + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter detached"); } @@ -469,26 +691,42 @@ GCPadStatus Input(int chan) if (!UseAdapter()) return {}; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION if (s_handle == nullptr || s_status != ADAPTER_DETECTED) return {}; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + if (!s_detected || !s_fd) + return {}; +#endif int payload_size = 0; u8 controller_payload_copy[37]; { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION std::lock_guard lk(s_mutex); std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), std::begin(controller_payload_copy)); +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + std::lock_guard lk(s_read_mutex); + controller_payload_copy = s_controller_payload; +#endif payload_size = s_controller_payload_size; } GCPadStatus pad = {}; - if (payload_size != sizeof(controller_payload_copy) || - controller_payload_copy[0] != LIBUSB_DT_HID) + if (payload_size != sizeof(controller_payload_copy) +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + || controller_payload_copy[0] != LIBUSB_DT_HID +#endif + ) { // This can occur for a few frames on initialization. ERROR_LOG_FMT(CONTROLLERINTERFACE, "error reading payload (size: {}, type: {:02x})", payload_size, controller_payload_copy[0]); +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION + Reset(); +#endif } else { @@ -546,7 +784,10 @@ GCPadStatus Input(int chan) pad.triggerLeft = controller_payload_copy[1 + (9 * chan) + 7]; pad.triggerRight = controller_payload_copy[1 + (9 * chan) + 8]; } - else if (!Core::WantsDeterminism()) + else +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + if (!Core::WantsDeterminism()) +#endif { // This is a hack to prevent a desync due to SI devices // being different and returning different values. @@ -578,12 +819,23 @@ bool UseAdapter() void ResetRumble() { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION std::unique_lock lock(s_init_mutex, std::defer_lock); if (!lock.try_lock()) return; ResetRumbleLockNeeded(); +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + unsigned char rumble[5] = {0x11, 0, 0, 0, 0}; + { + std::lock_guard lk(s_write_mutex); + memcpy(s_controller_write_payload, rumble, 5); + s_controller_write_payload_size.store(5); + } + s_write_happened.Set(); +#endif } +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION // 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() @@ -603,23 +855,44 @@ static void ResetRumbleLockNeeded() INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset"); } +#endif void Output(int chan, u8 rumble_command) { - if (s_handle == nullptr || !UseAdapter() || !s_config_rumble_enabled[chan]) + if (!UseAdapter()) return; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + if (s_handle == nullptr || !s_config_rumble_enabled[chan]) + return; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + if (!s_detected || !s_fd) + return; +#endif + // 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; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_rumble_data_available.Set(); +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], + s_controller_rumble[2], s_controller_rumble[3]}; + { + std::lock_guard lk(s_write_mutex); + memcpy(s_controller_write_payload, rumble, 5); + s_controller_write_payload_size.store(5); + } + s_write_happened.Set(); +#endif } } bool IsDetected(const char** error_message) { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION if (s_status >= 0) { if (error_message) @@ -632,6 +905,9 @@ bool IsDetected(const char** error_message) *error_message = libusb_strerror(static_cast(s_status.load())); return false; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + return s_detected; +#endif } } // namespace GCAdapter diff --git a/Source/Core/InputCommon/GCAdapter_Android.cpp b/Source/Core/InputCommon/GCAdapter_Android.cpp deleted file mode 100644 index 499a9ece30..0000000000 --- a/Source/Core/InputCommon/GCAdapter_Android.cpp +++ /dev/null @@ -1,420 +0,0 @@ -// Copyright 2014 Dolphin Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "InputCommon/GCAdapter.h" - -#include -#include -#include -#include - -#include "Common/Event.h" -#include "Common/Flag.h" -#include "Common/Logging/Log.h" -#include "Common/Thread.h" -#include "Core/Config/MainSettings.h" -#include "Core/Core.h" -#include "Core/CoreTiming.h" -#include "Core/HW/SI/SI.h" -#include "Core/HW/SI/SI_Device.h" -#include "Core/HW/SystemTimers.h" - -#include "InputCommon/GCPadStatus.h" - -#include "jni/AndroidCommon/IDCache.h" - -namespace GCAdapter -{ -static void Setup(); -static void Reset(); - -// Java classes -static jclass s_adapter_class; - -static bool s_detected = false; -static int s_fd = 0; -static std::array s_controller_type = { - ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, - ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; -static u8 s_controller_rumble[4]; - -// Input handling -static std::mutex s_read_mutex; -static std::array s_controller_payload; -static int s_controller_payload_size{0}; - -// Output handling -static std::mutex s_write_mutex; -static u8 s_controller_write_payload[5]; -static std::atomic s_controller_write_payload_size{0}; - -// Adapter running thread -static std::thread s_read_adapter_thread; -static Common::Flag s_read_adapter_thread_running; - -static Common::Flag s_write_adapter_thread_running; -static Common::Event s_write_happened; - -// Adapter scanning thread -static std::thread s_adapter_detect_thread; -static Common::Flag s_adapter_detect_thread_running; - -static u64 s_last_init = 0; - -static std::optional s_config_callback_id = std::nullopt; -static std::array - s_config_si_device_type{}; - -static void RefreshConfig() -{ - for (int i = 0; i < SerialInterface::MAX_SI_CHANNELS; ++i) - s_config_si_device_type[i] = Config::Get(Config::GetInfoForSIDevice(i)); -} - -static void ScanThreadFunc() -{ - Common::SetCurrentThreadName("GC Adapter Scanning Thread"); - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread started"); - - JNIEnv* env = IDCache::GetEnvForThread(); - - jmethodID queryadapter_func = env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z"); - - while (s_adapter_detect_thread_running.IsSet()) - { - if (!s_detected && UseAdapter() && - env->CallStaticBooleanMethod(s_adapter_class, queryadapter_func)) - Setup(); - Common::SleepCurrentThread(1000); - } - - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread stopped"); -} - -static void Write() -{ - Common::SetCurrentThreadName("GC Adapter Write Thread"); - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter write thread started"); - - JNIEnv* env = IDCache::GetEnvForThread(); - jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I"); - - while (s_write_adapter_thread_running.IsSet()) - { - s_write_happened.Wait(); - int write_size = s_controller_write_payload_size.load(); - if (write_size) - { - jbyteArray jrumble_array = env->NewByteArray(5); - jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr); - - { - std::lock_guard lk(s_write_mutex); - memcpy(jrumble, s_controller_write_payload, write_size); - } - - env->ReleaseByteArrayElements(jrumble_array, jrumble, 0); - int size = env->CallStaticIntMethod(s_adapter_class, output_func, jrumble_array); - // Netplay sends invalid data which results in size = 0x00. Ignore it. - if (size != write_size && size != 0x00) - { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "error writing rumble (size: {})", size); - Reset(); - } - } - - Common::YieldCPU(); - } - - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter write thread stopped"); -} - -static void Read() -{ - Common::SetCurrentThreadName("GC Adapter Read Thread"); - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter read thread started"); - - bool first_read = true; - JNIEnv* env = IDCache::GetEnvForThread(); - - jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "controller_payload", "[B"); - jobject payload_object = env->GetStaticObjectField(s_adapter_class, payload_field); - auto* java_controller_payload = reinterpret_cast(&payload_object); - - // Get function pointers - jmethodID getfd_func = env->GetStaticMethodID(s_adapter_class, "GetFD", "()I"); - jmethodID input_func = env->GetStaticMethodID(s_adapter_class, "Input", "()I"); - jmethodID openadapter_func = env->GetStaticMethodID(s_adapter_class, "OpenAdapter", "()Z"); - - bool connected = env->CallStaticBooleanMethod(s_adapter_class, openadapter_func); - - if (connected) - { - s_write_adapter_thread_running.Set(true); - std::thread write_adapter_thread(Write); - - // Reset rumble once on initial reading - ResetRumble(); - - while (s_read_adapter_thread_running.IsSet()) - { - int read_size = env->CallStaticIntMethod(s_adapter_class, input_func); - - jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); - { - std::lock_guard lk(s_read_mutex); - std::copy(java_data, java_data + s_controller_payload.size(), s_controller_payload.begin()); - s_controller_payload_size = read_size; - } - env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); - - if (first_read) - { - first_read = false; - s_fd = env->CallStaticIntMethod(s_adapter_class, getfd_func); - } - - Common::YieldCPU(); - } - - // Terminate the write thread on leaving - if (s_write_adapter_thread_running.TestAndClear()) - { - s_controller_write_payload_size.store(0); - s_write_happened.Set(); // Kick the waiting event - write_adapter_thread.join(); - } - } - - s_fd = 0; - s_detected = false; - - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter read thread stopped"); -} - -void Init() -{ - if (s_fd) - return; - - if (Core::GetState() != Core::State::Uninitialized && Core::GetState() != Core::State::Starting) - { - if ((CoreTiming::GetTicks() - s_last_init) < SystemTimers::GetTicksPerSecond()) - return; - - s_last_init = CoreTiming::GetTicks(); - } - - JNIEnv* env = IDCache::GetEnvForThread(); - - jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_GCAdapter"); - s_adapter_class = reinterpret_cast(env->NewGlobalRef(adapter_class)); - - if (!s_config_callback_id) - s_config_callback_id = Config::AddConfigChangedCallback(RefreshConfig); - RefreshConfig(); - - if (UseAdapter()) - StartScanThread(); -} - -static void Setup() -{ - s_fd = 0; - s_detected = true; - - // Make sure the thread isn't in the middle of shutting down while starting a new one - if (s_read_adapter_thread_running.TestAndClear()) - s_read_adapter_thread.join(); - - s_read_adapter_thread_running.Set(true); - s_read_adapter_thread = std::thread(Read); -} - -static void Reset() -{ - if (!s_detected) - return; - - if (s_read_adapter_thread_running.TestAndClear()) - s_read_adapter_thread.join(); - - s_controller_type.fill(ControllerTypes::CONTROLLER_NONE); - - s_detected = false; - s_fd = 0; - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter detached"); -} - -void Shutdown() -{ - StopScanThread(); - Reset(); - - if (s_config_callback_id) - { - Config::RemoveConfigChangedCallback(*s_config_callback_id); - s_config_callback_id = std::nullopt; - } -} - -void StartScanThread() -{ - if (s_adapter_detect_thread_running.IsSet()) - return; - - 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(); -} - -GCPadStatus Input(int chan) -{ - if (!UseAdapter() || !s_detected || !s_fd) - return {}; - - int payload_size = 0; - std::array controller_payload_copy{}; - - { - std::lock_guard lk(s_read_mutex); - controller_payload_copy = s_controller_payload; - payload_size = s_controller_payload_size; - } - - GCPadStatus pad = {}; - if (payload_size != controller_payload_copy.size()) - { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "error reading payload (size: {}, 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) - { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "New device connected to Port {} of Type: {:02x}", - chan + 1, controller_payload_copy[1 + (9 * chan)]); - get_origin = true; - } - - s_controller_type[chan] = type; - - 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 - { - pad.button = PAD_ERR_STATUS; - } - } - - return pad; -} - -void Output(int chan, u8 rumble_command) -{ - if (!UseAdapter() || !s_detected || !s_fd) - 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]}; - { - std::lock_guard lk(s_write_mutex); - memcpy(s_controller_write_payload, rumble, 5); - s_controller_write_payload_size.store(5); - } - s_write_happened.Set(); - } -} - -bool IsDetected(const char** error_message) -{ - return s_detected; -} -bool DeviceConnected(int chan) -{ - return s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE; -} - -void ResetDeviceType(int chan) -{ - s_controller_type[chan] = ControllerTypes::CONTROLLER_NONE; -} - -bool UseAdapter() -{ - const auto& si_devices = s_config_si_device_type; - return std::any_of(std::begin(si_devices), std::end(si_devices), [](const auto device_type) { - return device_type == SerialInterface::SIDEVICE_WIIU_ADAPTER; - }); -} - -void ResetRumble() -{ - unsigned char rumble[5] = {0x11, 0, 0, 0, 0}; - { - std::lock_guard lk(s_write_mutex); - memcpy(s_controller_write_payload, rumble, 5); - s_controller_write_payload_size.store(5); - } - s_write_happened.Set(); -} - -void SetAdapterCallback(std::function func) -{ -} - -} // end of namespace GCAdapter From 36d4ee0939765cda58cbe1e11b986f2465ad80d7 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 19 Apr 2022 14:24:06 -0700 Subject: [PATCH 02/14] GCAdapter: Use std::array for controller read and write payloads --- Source/Core/InputCommon/GCAdapter.cpp | 67 +++++++++++++++------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 86a595aaf4..84381195fa 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -87,10 +87,14 @@ static std::array s_controller_type = { ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; static std::array s_controller_rumble{}; +constexpr size_t CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE = 37; +constexpr size_t CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE = 1; +constexpr size_t CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE = 5; + #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::mutex s_mutex; -static u8 s_controller_payload[37]; -static u8 s_controller_payload_swap[37]; +static std::array s_controller_payload; +static std::array s_controller_payload_swap; // Only access with s_mutex held! static int s_controller_payload_size = {0}; @@ -105,12 +109,12 @@ static std::mutex s_init_mutex; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION // Input handling static std::mutex s_read_mutex; -static std::array s_controller_payload; +static std::array s_controller_payload; static int s_controller_payload_size = {0}; // Output handling static std::mutex s_write_mutex; -static u8 s_controller_write_payload[5]; +static std::array s_controller_write_payload; static std::atomic s_controller_write_payload_size{0}; // Adapter running thread @@ -161,8 +165,8 @@ static void Read() int payload_size = 0; while (s_adapter_thread_running.IsSet()) { - int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, - sizeof(s_controller_payload_swap), &payload_size, 16); + int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); if (err) ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", libusb_error_name(err)); @@ -207,7 +211,8 @@ static void Read() jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); { std::lock_guard lk(s_read_mutex); - std::copy(java_data, java_data + s_controller_payload.size(), s_controller_payload.begin()); + std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, + s_controller_payload.begin()); s_controller_payload_size = read_size; } env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); @@ -251,15 +256,15 @@ static void Write() if (!s_adapter_thread_running.IsSet()) return; - u8 payload[5] = { + std::array payload = { 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3], }; - const int err = - libusb_interrupt_transfer(s_handle, s_endpoint_out, payload, sizeof(payload), &size, 16); + const int err = libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), + CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16); if (err != 0) ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", libusb_error_name(err)); @@ -276,12 +281,12 @@ static void Write() int write_size = s_controller_write_payload_size.load(); if (write_size) { - jbyteArray jrumble_array = env->NewByteArray(5); + jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr); { std::lock_guard lk(s_write_mutex); - memcpy(jrumble, s_controller_write_payload, write_size); + memcpy(jrumble, s_controller_write_payload.data(), write_size); } env->ReleaseByteArrayElements(jrumble_array, jrumble, 0); @@ -604,9 +609,10 @@ static void AddGCAdapter(libusb_device* device) } } - int tmp = 0; - unsigned char payload = 0x13; - libusb_interrupt_transfer(s_handle, s_endpoint_out, &payload, sizeof(payload), &tmp, 16); + int size = 0; + std::array payload = {0x13}; + libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), + CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16); s_adapter_thread_running.Set(true); s_adapter_input_thread = std::thread(Read); @@ -700,22 +706,20 @@ GCPadStatus Input(int chan) #endif int payload_size = 0; - u8 controller_payload_copy[37]; + std::array controller_payload_copy{}; { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION std::lock_guard lk(s_mutex); - std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), - std::begin(controller_payload_copy)); #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION std::lock_guard lk(s_read_mutex); - controller_payload_copy = s_controller_payload; #endif + controller_payload_copy = s_controller_payload; payload_size = s_controller_payload_size; } GCPadStatus pad = {}; - if (payload_size != sizeof(controller_payload_copy) + if (payload_size != CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION || controller_payload_copy[0] != LIBUSB_DT_HID #endif @@ -825,11 +829,11 @@ void ResetRumble() return; ResetRumbleLockNeeded(); #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - unsigned char rumble[5] = {0x11, 0, 0, 0, 0}; + std::array rumble = {0x11, 0, 0, 0, 0}; { std::lock_guard lk(s_write_mutex); - memcpy(s_controller_write_payload, rumble, 5); - s_controller_write_payload_size.store(5); + s_controller_write_payload = rumble; + s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); } s_write_happened.Set(); #endif @@ -847,11 +851,13 @@ static void ResetRumbleLockNeeded() 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]}; + std::array rumble = { + 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); + libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(), + CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16); INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset"); } @@ -878,12 +884,13 @@ void Output(int chan, u8 rumble_command) #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_rumble_data_available.Set(); #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], - s_controller_rumble[2], s_controller_rumble[3]}; + std::array rumble = { + 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], + s_controller_rumble[3]}; { std::lock_guard lk(s_write_mutex); - memcpy(s_controller_write_payload, rumble, 5); - s_controller_write_payload_size.store(5); + s_controller_write_payload = rumble; + s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); } s_write_happened.Set(); #endif From 682d86f4daaba99294f17d1534f3f98fd9bc2f25 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 19 Apr 2022 14:57:08 -0700 Subject: [PATCH 03/14] GCAdapter: Fix rumble enabled config on Android I believe the setting already existed in the UI; it just wasn't implemented in GCAdapter_Android.cpp. --- Source/Core/InputCommon/GCAdapter.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 84381195fa..d13f743817 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -153,9 +153,7 @@ static u64 s_last_init = 0; static std::optional s_config_callback_id = std::nullopt; static std::array s_config_si_device_type{}; -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::array s_config_rumble_enabled{}; -#endif static void Read() { @@ -400,9 +398,7 @@ static void RefreshConfig() for (int i = 0; i < SerialInterface::MAX_SI_CHANNELS; ++i) { s_config_si_device_type[i] = Config::Get(Config::GetInfoForSIDevice(i)); -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_config_rumble_enabled[i] = Config::Get(Config::GetInfoForAdapterRumble(i)); -#endif } } @@ -865,11 +861,11 @@ static void ResetRumbleLockNeeded() void Output(int chan, u8 rumble_command) { - if (!UseAdapter()) + if (!UseAdapter() || !s_config_rumble_enabled[chan]) return; #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - if (s_handle == nullptr || !s_config_rumble_enabled[chan]) + if (s_handle == nullptr) return; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION if (!s_detected || !s_fd) From 55922e6d17442723e57f7435b758c5450aa12f11 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 19 Apr 2022 15:21:23 -0700 Subject: [PATCH 04/14] GCAdapter: Convert ControllerType to an enum class --- Source/Core/InputCommon/GCAdapter.cpp | 30 ++++++++++++++++----------- Source/Core/InputCommon/GCAdapter.h | 6 ------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index d13f743817..25e14a7e5d 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -82,9 +82,15 @@ static bool s_detected = false; static int s_fd = 0; #endif -static std::array s_controller_type = { - ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, - ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; +enum class ControllerType : u8 +{ + None = 0, + Wired = 1, + Wireless = 2, +}; + +static std::array s_controller_type = { + ControllerType::None, ControllerType::None, ControllerType::None, ControllerType::None}; static std::array s_controller_rumble{}; constexpr size_t CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE = 37; @@ -471,7 +477,7 @@ static void Setup() if (s_status < 0) s_status = NO_ADAPTER_DETECTED; - s_controller_type.fill(ControllerTypes::CONTROLLER_NONE); + s_controller_type.fill(ControllerType::None); s_controller_rumble.fill(0); s_libusb_context->GetDeviceList([](libusb_device* device) { @@ -667,7 +673,7 @@ static void Reset() s_read_adapter_thread.join(); #endif - s_controller_type.fill(ControllerTypes::CONTROLLER_NONE); + s_controller_type.fill(ControllerType::None); #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_status = NO_ADAPTER_DETECTED; @@ -731,9 +737,9 @@ GCPadStatus Input(int chan) 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) + // TODO: What do the other bits here indicate? Does casting to an enum like this make sense? + const auto type = static_cast(controller_payload_copy[1 + (9 * chan)] >> 4); + if (type != ControllerType::None && s_controller_type[chan] == ControllerType::None) { NOTICE_LOG_FMT(CONTROLLERINTERFACE, "New device connected to Port {} of Type: {:02x}", chan + 1, controller_payload_copy[1 + (9 * chan)]); @@ -742,7 +748,7 @@ GCPadStatus Input(int chan) s_controller_type[chan] = type; - if (s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE) + if (s_controller_type[chan] != ControllerType::None) { u8 b1 = controller_payload_copy[1 + (9 * chan) + 1]; u8 b2 = controller_payload_copy[1 + (9 * chan) + 2]; @@ -801,12 +807,12 @@ GCPadStatus Input(int chan) bool DeviceConnected(int chan) { - return s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE; + return s_controller_type[chan] != ControllerType::None; } void ResetDeviceType(int chan) { - s_controller_type[chan] = ControllerTypes::CONTROLLER_NONE; + s_controller_type[chan] = ControllerType::None; } bool UseAdapter() @@ -874,7 +880,7 @@ void Output(int chan, u8 rumble_command) // 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_type[chan] != ControllerType::Wireless) { s_controller_rumble[chan] = rumble_command; #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION diff --git a/Source/Core/InputCommon/GCAdapter.h b/Source/Core/InputCommon/GCAdapter.h index 67c3f7b1cd..25b114a365 100644 --- a/Source/Core/InputCommon/GCAdapter.h +++ b/Source/Core/InputCommon/GCAdapter.h @@ -11,12 +11,6 @@ struct GCPadStatus; namespace GCAdapter { -enum ControllerTypes -{ - CONTROLLER_NONE = 0, - CONTROLLER_WIRED = 1, - CONTROLLER_WIRELESS = 2 -}; void Init(); void ResetRumble(); void Shutdown(); From 3ae775e5745ba847b2195022ebc107004c871dc7 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 19 Apr 2022 15:23:06 -0700 Subject: [PATCH 05/14] GCAdapter: Use determinism hack on Android This hack was added in 8f0cbefbe57762de92284ef151e984f52ec8d7e6, and the part of it in SI_DeviceGCAdapter is present on Android already, so I don't see any reason why this part doesn't apply to Android. --- Source/Core/InputCommon/GCAdapter.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 25e14a7e5d..6242ce3155 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -790,10 +790,7 @@ GCPadStatus Input(int chan) pad.triggerLeft = controller_payload_copy[1 + (9 * chan) + 7]; pad.triggerRight = controller_payload_copy[1 + (9 * chan) + 8]; } - else -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - if (!Core::WantsDeterminism()) -#endif + else if (!Core::WantsDeterminism()) { // This is a hack to prevent a desync due to SI devices // being different and returning different values. From 279888da8c3f827c718fe9d9421e00f08dc2440c Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 20 Apr 2022 15:31:40 -0700 Subject: [PATCH 06/14] GCAdapter: Remove unused includes --- Source/Core/Core/HW/SI/SI_DeviceGCAdapter.cpp | 1 - Source/Core/InputCommon/GCAdapter.cpp | 2 -- 2 files changed, 3 deletions(-) diff --git a/Source/Core/Core/HW/SI/SI_DeviceGCAdapter.cpp b/Source/Core/Core/HW/SI/SI_DeviceGCAdapter.cpp index e57cdd0c0b..f36375025d 100644 --- a/Source/Core/Core/HW/SI/SI_DeviceGCAdapter.cpp +++ b/Source/Core/Core/HW/SI/SI_DeviceGCAdapter.cpp @@ -8,7 +8,6 @@ #include "Common/CommonTypes.h" #include "Common/Swap.h" #include "Core/Config/MainSettings.h" -#include "Core/ConfigManager.h" #include "Core/Core.h" #include "Core/HW/GCPad.h" #include "Core/NetPlayProto.h" diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 6242ce3155..5ac8907d57 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -36,9 +36,7 @@ #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #include "Common/ScopeGuard.h" -#include "Core/ConfigManager.h" #include "Core/LibusbUtils.h" -#include "Core/NetPlayProto.h" #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #include "jni/AndroidCommon/IDCache.h" #endif From 27947046af4a1c234a63d8636637f8bb74372860 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 20 Apr 2022 17:21:05 -0700 Subject: [PATCH 07/14] GCAdapter: Harmonize read/write thread variable names --- Source/Core/InputCommon/GCAdapter.cpp | 56 ++++++++++++--------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 5ac8907d57..07b89ff619 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -95,38 +95,32 @@ constexpr size_t CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE = 37; constexpr size_t CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE = 1; constexpr size_t CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE = 5; -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION -static std::mutex s_mutex; static std::array s_controller_payload; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::array s_controller_payload_swap; +#endif // Only access with s_mutex held! static int s_controller_payload_size = {0}; -static std::thread s_adapter_input_thread; -static std::thread s_adapter_output_thread; -static Common::Flag s_adapter_thread_running; - -static Common::Event s_rumble_data_available; - -static std::mutex s_init_mutex; -#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION -// Input handling -static std::mutex s_read_mutex; -static std::array s_controller_payload; -static int s_controller_payload_size = {0}; - -// Output handling -static std::mutex s_write_mutex; +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION static std::array s_controller_write_payload; static std::atomic s_controller_write_payload_size{0}; +#endif -// Adapter running thread static std::thread s_read_adapter_thread; -static Common::Flag s_read_adapter_thread_running; - -static Common::Flag s_write_adapter_thread_running; +static std::thread s_write_adapter_thread; static Common::Event s_write_happened; + +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION +static std::mutex s_mutex; +static std::mutex s_init_mutex; +static Common::Flag s_adapter_thread_running; +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +static std::mutex s_read_mutex; +static std::mutex s_write_mutex; +static Common::Flag s_read_adapter_thread_running; +static Common::Flag s_write_adapter_thread_running; #endif static std::thread s_adapter_detect_thread; @@ -201,7 +195,7 @@ static void Read() if (connected) { s_write_adapter_thread_running.Set(true); - std::thread write_adapter_thread(Write); + s_write_adapter_thread = std::thread(Write); // Reset rumble once on initial reading ResetRumble(); @@ -253,7 +247,7 @@ static void Write() while (true) { - s_rumble_data_available.Wait(); + s_write_happened.Wait(); if (!s_adapter_thread_running.IsSet()) return; @@ -615,8 +609,8 @@ static void AddGCAdapter(libusb_device* device) CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16); s_adapter_thread_running.Set(true); - s_adapter_input_thread = std::thread(Read); - s_adapter_output_thread = std::thread(Write); + s_read_adapter_thread = std::thread(Read); + s_write_adapter_thread = std::thread(Write); s_status = ADAPTER_DETECTED; if (s_detect_callback != nullptr) @@ -659,9 +653,9 @@ static void Reset() if (s_adapter_thread_running.TestAndClear()) { - s_rumble_data_available.Set(); - s_adapter_input_thread.join(); - s_adapter_output_thread.join(); + s_write_happened.Set(); + s_read_adapter_thread.join(); + s_write_adapter_thread.join(); } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION if (!s_detected) @@ -878,9 +872,7 @@ void Output(int chan, u8 rumble_command) s_controller_type[chan] != ControllerType::Wireless) { s_controller_rumble[chan] = rumble_command; -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - s_rumble_data_available.Set(); -#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION std::array rumble = { 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3]}; @@ -889,8 +881,8 @@ void Output(int chan, u8 rumble_command) s_controller_write_payload = rumble; s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); } - s_write_happened.Set(); #endif + s_write_happened.Set(); } } From 749a4ad1ef785eb82e42cf083b6cd2511f40001c Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 20 Apr 2022 18:42:47 -0700 Subject: [PATCH 08/14] GCAdapter: Remove check on write size on android It was removed for non-android in 56239d1ae139998a7cde17ff02f0526ffb17f5ef, and android already uses a separate thread, so presumably this isn't needed anymore. --- Source/Core/InputCommon/GCAdapter.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 07b89ff619..0f52305816 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -286,13 +286,7 @@ static void Write() } env->ReleaseByteArrayElements(jrumble_array, jrumble, 0); - int size = env->CallStaticIntMethod(s_adapter_class, output_func, jrumble_array); - // Netplay sends invalid data which results in size = 0x00. Ignore it. - if (size != write_size && size != 0x00) - { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "error writing rumble (size: {})", size); - Reset(); - } + env->CallStaticIntMethod(s_adapter_class, output_func, jrumble_array); } Common::YieldCPU(); From 0fa92694d120e2a68666c597cf40fc94ca3d4b23 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 20 Apr 2022 18:52:04 -0700 Subject: [PATCH 09/14] GCAdapter: Exit early if the adapter fails to open on Android This is only so that indentation is consistent with the non-android code. --- Source/Core/InputCommon/GCAdapter.cpp | 66 +++++++++++++++------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 0f52305816..1a0844c42e 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -192,43 +192,49 @@ static void Read() bool connected = env->CallStaticBooleanMethod(s_adapter_class, openadapter_func); - if (connected) + if (!connected) { - s_write_adapter_thread_running.Set(true); - s_write_adapter_thread = std::thread(Write); + s_fd = 0; + s_detected = false; - // Reset rumble once on initial reading - ResetRumble(); + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter failed to open!"); + return; + } - while (s_read_adapter_thread_running.IsSet()) + s_write_adapter_thread_running.Set(true); + s_write_adapter_thread = std::thread(Write); + + // Reset rumble once on initial reading + ResetRumble(); + + while (s_read_adapter_thread_running.IsSet()) + { + int read_size = env->CallStaticIntMethod(s_adapter_class, input_func); + + jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); { - int read_size = env->CallStaticIntMethod(s_adapter_class, input_func); + std::lock_guard lk(s_read_mutex); + std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, + s_controller_payload.begin()); + s_controller_payload_size = read_size; + } + env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); - jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); - { - std::lock_guard lk(s_read_mutex); - std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, - s_controller_payload.begin()); - s_controller_payload_size = read_size; - } - env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); - - if (first_read) - { - first_read = false; - s_fd = env->CallStaticIntMethod(s_adapter_class, getfd_func); - } - - Common::YieldCPU(); + if (first_read) + { + first_read = false; + s_fd = env->CallStaticIntMethod(s_adapter_class, getfd_func); } - // Terminate the write thread on leaving - if (s_write_adapter_thread_running.TestAndClear()) - { - s_controller_write_payload_size.store(0); - s_write_happened.Set(); // Kick the waiting event - write_adapter_thread.join(); - } + Common::YieldCPU(); + } + + // Terminate the write thread on leaving + if (s_write_adapter_thread_running.TestAndClear()) + { + s_controller_write_payload_size.store(0); + s_write_happened.Set(); // Kick the waiting event + write_adapter_thread.join(); } s_fd = 0; From 9ec65baf46cc1c0707b1fedf01196423e9fb9b08 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 20 Apr 2022 21:36:07 -0700 Subject: [PATCH 10/14] GCAdapter: Have the read thread control the write thread This was done for Android in 6cc40b12357ae339abcb44517810af304bab77ea. --- Source/Core/InputCommon/GCAdapter.cpp | 106 +++++++++++--------------- 1 file changed, 43 insertions(+), 63 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 1a0844c42e..832315160a 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -61,6 +61,8 @@ static void ResetRumbleLockNeeded(); #endif static void Reset(); static void Setup(); +static void Read(); +static void Write(); #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION enum @@ -103,24 +105,21 @@ static std::array s_controller_payloa // Only access with s_mutex held! static int s_controller_payload_size = {0}; -#if GCADAPTER_USE_ANDROID_IMPLEMENTATION static std::array s_controller_write_payload; static std::atomic s_controller_write_payload_size{0}; -#endif static std::thread s_read_adapter_thread; +static Common::Flag s_read_adapter_thread_running; static std::thread s_write_adapter_thread; +static Common::Flag s_write_adapter_thread_running; static Common::Event s_write_happened; #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::mutex s_mutex; static std::mutex s_init_mutex; -static Common::Flag s_adapter_thread_running; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION static std::mutex s_read_mutex; static std::mutex s_write_mutex; -static Common::Flag s_read_adapter_thread_running; -static Common::Flag s_write_adapter_thread_running; #endif static std::thread s_adapter_detect_thread; @@ -156,28 +155,11 @@ static std::array s_config_rumble_enable static void Read() { Common::SetCurrentThreadName("GCAdapter Read Thread"); + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter read thread started"); #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int payload_size = 0; - while (s_adapter_thread_running.IsSet()) - { - int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), - CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); - if (err) - ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", - libusb_error_name(err)); - - { - std::lock_guard lk(s_mutex); - std::swap(s_controller_payload_swap, s_controller_payload); - s_controller_payload_size = payload_size; - } - - Common::YieldCPU(); - } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter read thread started"); - bool first_read = true; JNIEnv* env = IDCache::GetEnvForThread(); @@ -200,6 +182,7 @@ static void Read() NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter failed to open!"); return; } +#endif s_write_adapter_thread_running.Set(true); s_write_adapter_thread = std::thread(Write); @@ -209,6 +192,19 @@ static void Read() while (s_read_adapter_thread_running.IsSet()) { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); + if (err) + ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", + libusb_error_name(err)); + + { + std::lock_guard lk(s_mutex); + std::swap(s_controller_payload_swap, s_controller_payload); + s_controller_payload_size = payload_size; + } +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION int read_size = env->CallStaticIntMethod(s_adapter_class, input_func); jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); @@ -225,6 +221,7 @@ static void Read() first_read = false; s_fd = env->CallStaticIntMethod(s_adapter_class, getfd_func); } +#endif Common::YieldCPU(); } @@ -234,55 +231,45 @@ static void Read() { s_controller_write_payload_size.store(0); s_write_happened.Set(); // Kick the waiting event - write_adapter_thread.join(); + s_write_adapter_thread.join(); } +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION s_fd = 0; s_detected = false; - - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter read thread stopped"); #endif + + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter read thread stopped"); } static void Write() { Common::SetCurrentThreadName("GCAdapter Write Thread"); + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter write thread started"); #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int size = 0; - - while (true) - { - s_write_happened.Wait(); - - if (!s_adapter_thread_running.IsSet()) - return; - - std::array payload = { - 0x11, - s_controller_rumble[0], - s_controller_rumble[1], - s_controller_rumble[2], - s_controller_rumble[3], - }; - const int err = libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), - CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16); - if (err != 0) - ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", - libusb_error_name(err)); - } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter write thread started"); - JNIEnv* env = IDCache::GetEnvForThread(); jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I"); +#endif while (s_write_adapter_thread_running.IsSet()) { s_write_happened.Wait(); + int write_size = s_controller_write_payload_size.load(); if (write_size) { +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + const int err = libusb_interrupt_transfer( + s_handle, s_endpoint_out, s_controller_write_payload.data(), write_size, &size, 16); + if (err != 0) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", + libusb_error_name(err)); + } +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr); @@ -293,13 +280,13 @@ static void Write() env->ReleaseByteArrayElements(jrumble_array, jrumble, 0); env->CallStaticIntMethod(s_adapter_class, output_func, jrumble_array); +#endif } Common::YieldCPU(); } - NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter write thread stopped"); -#endif + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter write thread stopped"); } #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION @@ -608,9 +595,8 @@ static void AddGCAdapter(libusb_device* device) libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16); - s_adapter_thread_running.Set(true); + s_read_adapter_thread_running.Set(true); s_read_adapter_thread = std::thread(Read); - s_write_adapter_thread = std::thread(Write); s_status = ADAPTER_DETECTED; if (s_detect_callback != nullptr) @@ -650,20 +636,14 @@ static void Reset() return; if (s_status != ADAPTER_DETECTED) return; - - if (s_adapter_thread_running.TestAndClear()) - { - s_write_happened.Set(); - s_read_adapter_thread.join(); - s_write_adapter_thread.join(); - } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION if (!s_detected) return; +#endif if (s_read_adapter_thread_running.TestAndClear()) s_read_adapter_thread.join(); -#endif + // The read thread will close the write thread s_controller_type.fill(ControllerType::None); @@ -872,16 +852,16 @@ void Output(int chan, u8 rumble_command) s_controller_type[chan] != ControllerType::Wireless) { s_controller_rumble[chan] = rumble_command; -#if GCADAPTER_USE_ANDROID_IMPLEMENTATION std::array rumble = { 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3]}; { +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION std::lock_guard lk(s_write_mutex); +#endif s_controller_write_payload = rumble; s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); } -#endif s_write_happened.Set(); } } From cd9edeacda9bc816fdd29859b76da7d2fa6d7bd0 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Thu, 21 Apr 2022 12:03:58 -0700 Subject: [PATCH 11/14] GCAdapter: Merge Read logic --- Source/Core/InputCommon/GCAdapter.cpp | 34 +++++++++------------------ 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 832315160a..4376e0d3a9 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -98,9 +98,7 @@ constexpr size_t CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE = 1; constexpr size_t CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE = 5; static std::array s_controller_payload; -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION static std::array s_controller_payload_swap; -#endif // Only access with s_mutex held! static int s_controller_payload_size = {0}; @@ -114,11 +112,10 @@ static std::thread s_write_adapter_thread; static Common::Flag s_write_adapter_thread_running; static Common::Event s_write_happened; +static std::mutex s_read_mutex; #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION -static std::mutex s_mutex; static std::mutex s_init_mutex; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION -static std::mutex s_read_mutex; static std::mutex s_write_mutex; #endif @@ -157,9 +154,7 @@ static void Read() Common::SetCurrentThreadName("GCAdapter Read Thread"); NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter read thread started"); -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - int payload_size = 0; -#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION bool first_read = true; JNIEnv* env = IDCache::GetEnvForThread(); @@ -193,27 +188,24 @@ static void Read() while (s_read_adapter_thread_running.IsSet()) { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION + int payload_size = 0; int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); if (err) ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", libusb_error_name(err)); - +#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION + int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func); + jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); + std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, + s_controller_payload_swap.begin()); +#endif { - std::lock_guard lk(s_mutex); + std::lock_guard lk(s_read_mutex); std::swap(s_controller_payload_swap, s_controller_payload); s_controller_payload_size = payload_size; } -#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - int read_size = env->CallStaticIntMethod(s_adapter_class, input_func); - - jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); - { - std::lock_guard lk(s_read_mutex); - std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, - s_controller_payload.begin()); - s_controller_payload_size = read_size; - } +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); if (first_read) @@ -683,11 +675,7 @@ GCPadStatus Input(int chan) std::array controller_payload_copy{}; { -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - std::lock_guard lk(s_mutex); -#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION std::lock_guard lk(s_read_mutex); -#endif controller_payload_copy = s_controller_payload; payload_size = s_controller_payload_size; } From 0d8772ccbe13781d742a1251112823d4c0644ec0 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Thu, 2 Jun 2022 13:19:32 -0700 Subject: [PATCH 12/14] GCAdapter: Make local variables const where possible --- Source/Core/InputCommon/GCAdapter.cpp | 48 ++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 4376e0d3a9..7c8a89c533 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -156,18 +156,18 @@ static void Read() #if GCADAPTER_USE_ANDROID_IMPLEMENTATION bool first_read = true; - JNIEnv* env = IDCache::GetEnvForThread(); + JNIEnv* const env = IDCache::GetEnvForThread(); - jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "controller_payload", "[B"); + const jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "controller_payload", "[B"); jobject payload_object = env->GetStaticObjectField(s_adapter_class, payload_field); - auto* java_controller_payload = reinterpret_cast(&payload_object); + auto* const java_controller_payload = reinterpret_cast(&payload_object); // Get function pointers - jmethodID getfd_func = env->GetStaticMethodID(s_adapter_class, "GetFD", "()I"); - jmethodID input_func = env->GetStaticMethodID(s_adapter_class, "Input", "()I"); - jmethodID openadapter_func = env->GetStaticMethodID(s_adapter_class, "OpenAdapter", "()Z"); + const jmethodID getfd_func = env->GetStaticMethodID(s_adapter_class, "GetFD", "()I"); + const jmethodID input_func = env->GetStaticMethodID(s_adapter_class, "Input", "()I"); + const jmethodID openadapter_func = env->GetStaticMethodID(s_adapter_class, "OpenAdapter", "()Z"); - bool connected = env->CallStaticBooleanMethod(s_adapter_class, openadapter_func); + const bool connected = env->CallStaticBooleanMethod(s_adapter_class, openadapter_func); if (!connected) { @@ -189,14 +189,15 @@ static void Read() { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int payload_size = 0; - int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), - CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); + const int err = + libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); if (err) ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", libusb_error_name(err)); #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func); - jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); + const int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func); + jbyte* const java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, s_controller_payload_swap.begin()); #endif @@ -242,15 +243,15 @@ static void Write() #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int size = 0; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - JNIEnv* env = IDCache::GetEnvForThread(); - jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I"); + JNIEnv* const env = IDCache::GetEnvForThread(); + const jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I"); #endif while (s_write_adapter_thread_running.IsSet()) { s_write_happened.Wait(); - int write_size = s_controller_write_payload_size.load(); + const int write_size = s_controller_write_payload_size.load(); if (write_size) { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION @@ -262,8 +263,8 @@ static void Write() libusb_error_name(err)); } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); - jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr); + const jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); + jbyte* const jrumble = env->GetByteArrayElements(jrumble_array, nullptr); { std::lock_guard lk(s_write_mutex); @@ -347,9 +348,10 @@ static void ScanThreadFunc() Common::SleepCurrentThread(500); } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - JNIEnv* env = IDCache::GetEnvForThread(); + JNIEnv* const env = IDCache::GetEnvForThread(); - jmethodID queryadapter_func = env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z"); + const jmethodID queryadapter_func = + env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z"); while (s_adapter_detect_thread_running.IsSet()) { @@ -402,9 +404,9 @@ void Init() #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION s_status = NO_ADAPTER_DETECTED; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION - JNIEnv* env = IDCache::GetEnvForThread(); + JNIEnv* const env = IDCache::GetEnvForThread(); - jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_GCAdapter"); + const jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_GCAdapter"); s_adapter_class = reinterpret_cast(env->NewGlobalRef(adapter_class)); #endif @@ -442,7 +444,7 @@ void StopScanThread() static void Setup() { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - int prev_status = s_status; + const int prev_status = s_status; // Reset the error status in case the adapter gets unplugged if (s_status < 0) @@ -710,8 +712,8 @@ GCPadStatus Input(int chan) if (s_controller_type[chan] != ControllerType::None) { - u8 b1 = controller_payload_copy[1 + (9 * chan) + 1]; - u8 b2 = controller_payload_copy[1 + (9 * chan) + 2]; + const u8 b1 = controller_payload_copy[1 + (9 * chan) + 1]; + const u8 b2 = controller_payload_copy[1 + (9 * chan) + 2]; if (b1 & (1 << 0)) pad.button |= PAD_BUTTON_A; From 457fcbaf5e565ba851cbc91202b18d2319fb837c Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Thu, 2 Jun 2022 13:32:30 -0700 Subject: [PATCH 13/14] LibusbUtils: Create ErrorWrap --- Source/Core/Core/LibusbUtils.cpp | 20 +++++++++++++++++++- Source/Core/Core/LibusbUtils.h | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/LibusbUtils.cpp b/Source/Core/Core/LibusbUtils.cpp index 8ab2f40a55..dadad60a2d 100644 --- a/Source/Core/Core/LibusbUtils.cpp +++ b/Source/Core/Core/LibusbUtils.cpp @@ -23,7 +23,7 @@ public: Impl() { const int ret = libusb_init(&m_context); - ASSERT_MSG(IOS_USB, ret == LIBUSB_SUCCESS, "Failed to init libusb: {}", libusb_error_name(ret)); + ASSERT_MSG(IOS_USB, ret == LIBUSB_SUCCESS, "Failed to init libusb: {}", ErrorWrap(ret)); if (ret != LIBUSB_SUCCESS) return; @@ -118,4 +118,22 @@ ConfigDescriptor MakeConfigDescriptor(libusb_device* device, u8 config_num) #endif return {nullptr, [](auto) {}}; } + +const char* ErrorWrap::GetName() const +{ +#if defined(__LIBUSB__) + return libusb_error_name(m_error); +#else + return "__LIBUSB__ not defined"; +#endif +} + +const char* ErrorWrap::GetStrError() const +{ +#if defined(__LIBUSB__) + return libusb_strerror(static_cast(m_error)); +#else + return "__LIBUSB__ not defined"; +#endif +} } // namespace LibusbUtils diff --git a/Source/Core/Core/LibusbUtils.h b/Source/Core/Core/LibusbUtils.h index 3e15ed0155..aaa77de74b 100644 --- a/Source/Core/Core/LibusbUtils.h +++ b/Source/Core/Core/LibusbUtils.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include @@ -39,4 +40,28 @@ private: using ConfigDescriptor = UniquePtr; ConfigDescriptor MakeConfigDescriptor(libusb_device* device, u8 config_num = 0); + +// Wrapper for libusb_error to be used with fmt. Note that we can't create a fmt::formatter +// directly for libusb_error as it is a plain enum and most libusb functions actually return an +// int instead of a libusb_error. +struct ErrorWrap +{ + constexpr explicit ErrorWrap(int error) : m_error(error) {} + const int m_error; + + const char* GetStrError() const; + const char* GetName() const; +}; } // namespace LibusbUtils + +template <> +struct fmt::formatter +{ + constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); } + template + auto format(const LibusbUtils::ErrorWrap& wrap, FormatContext& ctx) const + { + return fmt::format_to(ctx.out(), "{} ({}: {})", wrap.GetStrError(), wrap.m_error, + wrap.GetName()); + } +}; From 6823b4d7a05b305f06a92144180a63c626a865ab Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Thu, 2 Jun 2022 13:56:17 -0700 Subject: [PATCH 14/14] GCAdapter: Use LibusbUtils::ErrorWrap in log messages --- Source/Core/InputCommon/GCAdapter.cpp | 120 +++++++++++++++++--------- 1 file changed, 81 insertions(+), 39 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 7c8a89c533..cf1b103f2f 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -189,12 +189,14 @@ static void Read() { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION int payload_size = 0; - const int err = + const int error = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); - if (err) - ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", - libusb_error_name(err)); + if (error != LIBUSB_SUCCESS) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "Read: libusb_interrupt_transfer failed: {}", + LibusbUtils::ErrorWrap(error)); + } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION const int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func); jbyte* const java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); @@ -255,12 +257,12 @@ static void Write() if (write_size) { #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION - const int err = libusb_interrupt_transfer( + const int error = libusb_interrupt_transfer( s_handle, s_endpoint_out, s_controller_write_payload.data(), write_size, &size, 16); - if (err != 0) + if (error != LIBUSB_SUCCESS) { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", - libusb_error_name(err)); + ERROR_LOG_FMT(CONTROLLERINTERFACE, "Write: libusb_interrupt_transfer failed: {}", + LibusbUtils::ErrorWrap(error)); } #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION const jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); @@ -322,15 +324,22 @@ static void ScanThreadFunc() #endif 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) + const int error = 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); + if (error == LIBUSB_SUCCESS) + { NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Using libUSB hotplug detection"); + } + else + { + s_libusb_hotplug_enabled = false; + ERROR_LOG_FMT(CONTROLLERINTERFACE, "Failed to add libUSB hotplug detection callback: {}", + LibusbUtils::ErrorWrap(error)); + } } #endif @@ -483,10 +492,11 @@ static bool CheckDeviceAccess(libusb_device* device) { libusb_device_descriptor desc; int ret = libusb_get_device_descriptor(device, &desc); - if (ret != 0) + if (ret != LIBUSB_SUCCESS) { // could not acquire the descriptor, no point in trying to use it. - ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_device_descriptor failed with error: {}", ret); + ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_device_descriptor failed: {}", + LibusbUtils::ErrorWrap(ret)); return false; } @@ -505,23 +515,23 @@ static bool CheckDeviceAccess(libusb_device* device) const u8 bus = libusb_get_bus_number(device); const u8 port = libusb_get_device_address(device); ret = libusb_open(device, &s_handle); - if (ret == LIBUSB_ERROR_ACCESS) + if (ret != LIBUSB_SUCCESS) { - ERROR_LOG_FMT( - CONTROLLERINTERFACE, - "Dolphin does not have access to this device: Bus {:03d} Device {:03d}: ID {:04X}:{:04X}.", - bus, port, desc.idVendor, desc.idProduct); - return false; - } - if (ret != 0) - { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_open failed to open device with error = {}", ret); + if (ret == LIBUSB_ERROR_ACCESS) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, + "Dolphin does not have access to this device: Bus {:03d} Device {:03d}: ID " + "{:04X}:{:04X}.", + bus, port, desc.idVendor, desc.idProduct); + } + ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_open failed to open device: {}", + LibusbUtils::ErrorWrap(ret)); return false; } bool detach_failed = false; ret = libusb_kernel_driver_active(s_handle, 0); - if (ret == 1) + if (ret == 1) // 1: kernel driver is active { // On macos detaching would fail without root or entitlement. // We assume user is using GCAdapterDriver and therefor don't want to detach anything @@ -530,14 +540,26 @@ static bool CheckDeviceAccess(libusb_device* device) detach_failed = ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND && ret != LIBUSB_ERROR_NOT_SUPPORTED; #endif if (detach_failed) - ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed with error: {}", ret); + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed: {}", + LibusbUtils::ErrorWrap(ret)); + } + } + else if (ret != 0) // 0: kernel driver is not active, but otherwise no error. + { + // Neither 0 nor 1 means an error occured. + ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_kernel_driver_active failed: {}", + LibusbUtils::ErrorWrap(ret)); } // This call makes Nyko-brand (and perhaps other) adapters work. // However it returns LIBUSB_ERROR_PIPE with Mayflash adapters. const int transfer = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 1000); if (transfer < 0) - WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_control_transfer failed with error: {}", transfer); + { + WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_control_transfer failed: {}", + LibusbUtils::ErrorWrap(transfer)); + } // this split is needed so that we don't avoid claiming the interface when // detaching the kernel driver is successful @@ -549,9 +571,10 @@ static bool CheckDeviceAccess(libusb_device* device) } ret = libusb_claim_interface(s_handle, 0); - if (ret != 0) + if (ret != LIBUSB_SUCCESS) { - ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_claim_interface failed with error: {}", ret); + ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_claim_interface failed: {}", + LibusbUtils::ErrorWrap(ret)); libusb_close(s_handle); s_handle = nullptr; return false; @@ -566,7 +589,11 @@ static bool CheckDeviceAccess(libusb_device* device) static void AddGCAdapter(libusb_device* device) { libusb_config_descriptor* config = nullptr; - libusb_get_config_descriptor(device, 0, &config); + if (const int error = libusb_get_config_descriptor(device, 0, &config); error != LIBUSB_SUCCESS) + { + WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_config_descriptor failed: {}", + LibusbUtils::ErrorWrap(error)); + } for (u8 ic = 0; ic < config->bNumInterfaces; ic++) { const libusb_interface* interfaceContainer = &config->interface[ic]; @@ -586,8 +613,13 @@ static void AddGCAdapter(libusb_device* device) int size = 0; std::array payload = {0x13}; - libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), - CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16); + const int error = libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), + CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16); + if (error != LIBUSB_SUCCESS) + { + WARN_LOG_FMT(CONTROLLERINTERFACE, "AddGCAdapter: libusb_interrupt_transfer failed: {}", + LibusbUtils::ErrorWrap(error)); + } s_read_adapter_thread_running.Set(true); s_read_adapter_thread = std::thread(Read); @@ -646,7 +678,12 @@ static void Reset() if (s_handle) { - libusb_release_interface(s_handle, 0); + const int error = libusb_release_interface(s_handle, 0); + if (error != LIBUSB_SUCCESS) + { + WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_release_interface failed: {}", + LibusbUtils::ErrorWrap(error)); + } libusb_close(s_handle); s_handle = nullptr; } @@ -817,8 +854,13 @@ static void ResetRumbleLockNeeded() s_controller_rumble[3]}; int size = 0; - libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(), - CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16); + const int error = libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(), + CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16); + if (error != LIBUSB_SUCCESS) + { + WARN_LOG_FMT(CONTROLLERINTERFACE, "ResetRumbleLockNeeded: libusb_interrupt_transfer failed: {}", + LibusbUtils::ErrorWrap(error)); + } INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset"); }