This commit is contained in:
Simonx22 2025-12-15 20:48:52 -07:00 committed by GitHub
commit 733502365e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 75 deletions

View File

@ -6,6 +6,7 @@ import android.content.Context
import android.hardware.input.InputManager
import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.VibrationEffect
import android.os.Vibrator
@ -17,7 +18,6 @@ import androidx.annotation.Keep
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.dolphinemu.dolphinemu.DolphinApplication
import org.dolphinemu.dolphinemu.utils.LooperThread
import java.util.concurrent.atomic.AtomicBoolean
/**
@ -26,9 +26,9 @@ import java.util.concurrent.atomic.AtomicBoolean
*/
object ControllerInterface {
private var inputDeviceListener: InputDeviceListener? = null
private lateinit var looperThread: LooperThread
private var handlerThread: HandlerThread? = null
private var inputStateUpdatePending = AtomicBoolean(false)
private val inputStateUpdatePending = AtomicBoolean(false)
private val inputStateVersion = MutableLiveData(0)
private val devicesVersion = MutableLiveData(0)
@ -81,9 +81,7 @@ object ControllerInterface {
}
private external fun dispatchSensorEventNative(
deviceQualifier: String,
axisName: String,
value: Float
deviceQualifier: String, axisName: String, value: Float
): Boolean
/**
@ -94,9 +92,7 @@ object ControllerInterface {
* @param suspended Whether the sensor is now suspended.
*/
external fun notifySensorSuspendedState(
deviceQualifier: String,
axisNames: Array<String>,
suspended: Boolean
deviceQualifier: String, axisNames: Array<String>, suspended: Boolean
)
/**
@ -132,15 +128,18 @@ object ControllerInterface {
@Keep
@JvmStatic
private fun registerInputDeviceListener() {
looperThread = LooperThread("Hotplug thread")
looperThread.start()
if (inputDeviceListener == null) {
handlerThread = HandlerThread("Hotplug thread").apply { start() }
val thread = requireNotNull(handlerThread) { "HandlerThread is not available" }
val im = DolphinApplication.getAppContext()
.getSystemService(Context.INPUT_SERVICE) as InputManager?
.getSystemService(Context.INPUT_SERVICE) as InputManager
val looper = requireNotNull(thread.looper) {
"HandlerThread looper is not available"
}
inputDeviceListener = InputDeviceListener()
im!!.registerInputDeviceListener(inputDeviceListener, Handler(looperThread.looper))
im.registerInputDeviceListener(inputDeviceListener, Handler(looper))
}
}
@ -149,10 +148,12 @@ object ControllerInterface {
private fun unregisterInputDeviceListener() {
if (inputDeviceListener != null) {
val im = DolphinApplication.getAppContext()
.getSystemService(Context.INPUT_SERVICE) as InputManager?
.getSystemService(Context.INPUT_SERVICE) as InputManager
im!!.unregisterInputDeviceListener(inputDeviceListener)
im.unregisterInputDeviceListener(inputDeviceListener)
inputDeviceListener = null
handlerThread?.quitSafely()
handlerThread = null
}
}
@ -172,8 +173,9 @@ object ControllerInterface {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = DolphinApplication.getAppContext()
.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager?
if (vibratorManager != null)
if (vibratorManager != null) {
return DolphinVibratorManagerPassthrough(vibratorManager)
}
}
val vibrator = DolphinApplication.getAppContext()
.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

View File

@ -1,58 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.utils;
import android.os.Looper;
public class LooperThread extends Thread
{
private Looper mLooper;
public LooperThread()
{
super();
}
public LooperThread(String name)
{
super(name);
}
@Override
public void run()
{
Looper.prepare();
synchronized (this)
{
mLooper = Looper.myLooper();
notifyAll();
}
Looper.loop();
}
public Looper getLooper()
{
if (!isAlive())
{
throw new IllegalStateException();
}
synchronized (this)
{
while (mLooper == null)
{
try
{
wait();
}
catch (InterruptedException ignored)
{
}
}
}
return mLooper;
}
}