From 44f1c2010c545556a27426e0102002085019b0c9 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 2 Jan 2025 17:37:57 +0100 Subject: [PATCH] Android: Detect GCAdapter disconnection using BroadcastReceiver This lets us get rid of the Reset call in ProcessInputPayload, which was causing us some threading headaches (see 74ed5e5532). Instead we handle disconnection in the same way as the libusb implementation does. --- Source/Core/InputCommon/GCAdapter.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index b02306806d..fa847d8c8d 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -134,10 +134,9 @@ 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_init_mutex; -#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION +static std::mutex s_read_mutex; +#if GCADAPTER_USE_ANDROID_IMPLEMENTATION static std::mutex s_write_mutex; #endif @@ -359,6 +358,8 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_utils_GCAdapter_onAdapterDisconnected(JNIEnv* env, jclass) { INFO_LOG_FMT(CONTROLLERINTERFACE, "GC adapter disconnected"); + if (s_detected) + Reset(CalledFromReadThread::No); } } #endif @@ -422,6 +423,7 @@ static void ScanThreadFunc() if (!s_detected && UseAdapter() && env->CallStaticBooleanMethod(s_adapter_class, is_usb_device_available_func)) { + std::lock_guard lk(s_init_mutex); Setup(); } @@ -737,10 +739,10 @@ void Shutdown() static void Reset(CalledFromReadThread called_from_read_thread) { -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION std::unique_lock lock(s_init_mutex, std::defer_lock); if (!lock.try_lock()) return; +#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION if (s_status != AdapterStatus::Detected) return; #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION @@ -836,9 +838,6 @@ void ProcessInputPayload(const u8* data, std::size_t size) // This can occur for a few frames on initialization. ERROR_LOG_FMT(CONTROLLERINTERFACE, "error reading payload (size: {}, type: {:02x})", size, data[0]); -#if GCADAPTER_USE_ANDROID_IMPLEMENTATION - Reset(CalledFromReadThread::Yes); -#endif } else {