From a9a9fdd9e903d47ec83aa7385e7be54871abbf0b Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 11 Mar 2024 02:36:07 -0500 Subject: [PATCH] InputCommon: Add Android InputBackend class. --- .../ControllerInterface/Android/Android.cpp | 30 +++++++++++++++---- .../ControllerInterface/Android/Android.h | 7 ++--- .../ControllerInterface.cpp | 10 +------ 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp b/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp index 50858cd3de..69f0e5b5ec 100644 --- a/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp @@ -23,6 +23,7 @@ #include "InputCommon/ControllerInterface/ControllerInterface.h" +#include "InputCommon/ControllerInterface/InputBackend.h" #include "jni/AndroidCommon/AndroidCommon.h" #include "jni/AndroidCommon/IDCache.h" #include "jni/Input/CoreDevice.h" @@ -444,6 +445,23 @@ std::shared_ptr FindDevice(jint device_id) namespace ciface::Android { +class InputBackend final : public ciface::InputBackend +{ +public: + using ciface::InputBackend::InputBackend; + ~InputBackend(); + void PopulateDevices() override; + +private: + void AddDevice(JNIEnv* env, int device_id); + void AddSensorDevice(JNIEnv* env); +}; + +std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface) +{ + return std::make_unique(controller_interface); +} + class AndroidInput : public Core::Device::Input { public: @@ -885,7 +903,7 @@ void Init() s_controller_interface_register_input_device_listener); } -void Shutdown() +InputBackend::~InputBackend() { JNIEnv* env = IDCache::GetEnvForThread(); @@ -903,7 +921,7 @@ void Shutdown() env->DeleteGlobalRef(s_keycodes_array); } -static void AddDevice(JNIEnv* env, int device_id) +void InputBackend::AddDevice(JNIEnv* env, int device_id) { jobject input_device = env->CallStaticObjectMethod(s_input_device_class, s_input_device_get_device, device_id); @@ -921,7 +939,7 @@ static void AddDevice(JNIEnv* env, int device_id) if (device->Inputs().empty() && device->Outputs().empty()) return; - g_controller_interface.AddDevice(device); + GetControllerInterface().AddDevice(device); Core::DeviceQualifier qualifier; qualifier.FromDevice(device.get()); @@ -936,7 +954,7 @@ static void AddDevice(JNIEnv* env, int device_id) env->DeleteLocalRef(j_qualifier); } -static void AddSensorDevice(JNIEnv* env) +void InputBackend::AddSensorDevice(JNIEnv* env) { // Device sensors (accelerometer, etc.) aren't associated with any Android InputDevice. // Create an otherwise empty Dolphin input device so that they have somewhere to live. @@ -946,7 +964,7 @@ static void AddSensorDevice(JNIEnv* env) if (device->Inputs().empty() && device->Outputs().empty()) return; - g_controller_interface.AddDevice(device); + GetControllerInterface().AddDevice(device); Core::DeviceQualifier qualifier; qualifier.FromDevice(device.get()); @@ -959,7 +977,7 @@ static void AddSensorDevice(JNIEnv* env) env->DeleteLocalRef(j_qualifier); } -void PopulateDevices() +void InputBackend::PopulateDevices() { INFO_LOG_FMT(CONTROLLERINTERFACE, "Android populating devices"); diff --git a/Source/Core/InputCommon/ControllerInterface/Android/Android.h b/Source/Core/InputCommon/ControllerInterface/Android/Android.h index 8b8478d7ac..e63ac4f2da 100644 --- a/Source/Core/InputCommon/ControllerInterface/Android/Android.h +++ b/Source/Core/InputCommon/ControllerInterface/Android/Android.h @@ -3,11 +3,10 @@ #pragma once +#include "InputCommon/ControllerInterface/InputBackend.h" + namespace ciface::Android { -void Init(); -void Shutdown(); - -void PopulateDevices(); +std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface); } // namespace ciface::Android diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index b82cba645e..ab266292bc 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -71,7 +71,7 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi) m_input_backends.emplace_back(ciface::SDL::CreateInputBackend(this)); #endif #ifdef CIFACE_USE_ANDROID - ciface::Android::Init(); + m_input_backends.emplace_back(ciface::Android::CreateInputBackend(this)); #endif #ifdef CIFACE_USE_EVDEV m_input_backends.emplace_back(ciface::evdev::CreateInputBackend(this)); @@ -157,10 +157,6 @@ void ControllerInterface::RefreshDevices(RefreshReason reason) // do it async, to not risk the emulated controllers default config loading not finding a default // device. -#ifdef CIFACE_USE_ANDROID - ciface::Android::PopulateDevices(); -#endif - for (auto& backend : m_input_backends) backend->PopulateDevices(); @@ -199,10 +195,6 @@ void ControllerInterface::Shutdown() // Update control references so shared_ptrs are freed up BEFORE we shutdown the backends. ClearDevices(); -#ifdef CIFACE_USE_ANDROID - ciface::Android::Shutdown(); -#endif - // Empty the container of input backends to deconstruct and deinitialize them. m_input_backends.clear();