From 185569778c062f52bdd87137f74dc0d4483b74cb Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 2 Jan 2025 17:37:57 +0100 Subject: 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(-) (limited to 'Source/Core/InputCommon/GCAdapter.cpp') diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 2da4cf3f15..38c8220737 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(); } @@ -735,10 +737,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 @@ -834,9 +836,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 { -- cgit v1.2.3