mirror of
https://git.eden-emu.dev/eden-emu/eden
synced 2026-04-10 01:08:56 +02:00
[android] Refined physical controller automatic detection
This commit is contained in:
parent
88124a4cd7
commit
5427867bee
2 changed files with 58 additions and 12 deletions
|
|
@ -404,8 +404,7 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener, InputManager
|
||||||
val isPhysicalKeyboard = event.source and InputDevice.SOURCE_KEYBOARD == InputDevice.SOURCE_KEYBOARD &&
|
val isPhysicalKeyboard = event.source and InputDevice.SOURCE_KEYBOARD == InputDevice.SOURCE_KEYBOARD &&
|
||||||
event.device?.isVirtual == false
|
event.device?.isVirtual == false
|
||||||
|
|
||||||
val isControllerInput = event.source and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK ||
|
val isControllerInput = InputHandler.isPhysicalGameController(event.device)
|
||||||
event.source and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD
|
|
||||||
|
|
||||||
if (!isControllerInput &&
|
if (!isControllerInput &&
|
||||||
event.source and InputDevice.SOURCE_MOUSE != InputDevice.SOURCE_MOUSE &&
|
event.source and InputDevice.SOURCE_MOUSE != InputDevice.SOURCE_MOUSE &&
|
||||||
|
|
@ -426,8 +425,7 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener, InputManager
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
|
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
|
||||||
val isControllerInput = event.source and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK ||
|
val isControllerInput = InputHandler.isPhysicalGameController(event.device)
|
||||||
event.source and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD
|
|
||||||
|
|
||||||
if (!isControllerInput &&
|
if (!isControllerInput &&
|
||||||
event.source and InputDevice.SOURCE_KEYBOARD != InputDevice.SOURCE_KEYBOARD &&
|
event.source and InputDevice.SOURCE_KEYBOARD != InputDevice.SOURCE_KEYBOARD &&
|
||||||
|
|
@ -461,10 +459,7 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener, InputManager
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isGameController(deviceId: Int): Boolean {
|
private fun isGameController(deviceId: Int): Boolean {
|
||||||
val device = InputDevice.getDevice(deviceId) ?: return false
|
return InputHandler.isPhysicalGameController(InputDevice.getDevice(deviceId))
|
||||||
val sources = device.sources
|
|
||||||
return sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD ||
|
|
||||||
sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onInputDeviceAdded(deviceId: Int) {
|
override fun onInputDeviceAdded(deviceId: Int) {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,60 @@ object InputHandler {
|
||||||
var androidControllers = mapOf<Int, YuzuPhysicalDevice>()
|
var androidControllers = mapOf<Int, YuzuPhysicalDevice>()
|
||||||
var registeredControllers = mutableListOf<ParamPackage>()
|
var registeredControllers = mutableListOf<ParamPackage>()
|
||||||
|
|
||||||
|
private val controllerButtons = intArrayOf(
|
||||||
|
KeyEvent.KEYCODE_BUTTON_A,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_B,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_X,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_Y,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_L1,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_R1,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_L2,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_R2,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_THUMBL,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_THUMBR,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_START,
|
||||||
|
KeyEvent.KEYCODE_BUTTON_SELECT,
|
||||||
|
KeyEvent.KEYCODE_DPAD_UP,
|
||||||
|
KeyEvent.KEYCODE_DPAD_DOWN,
|
||||||
|
KeyEvent.KEYCODE_DPAD_LEFT,
|
||||||
|
KeyEvent.KEYCODE_DPAD_RIGHT
|
||||||
|
)
|
||||||
|
|
||||||
|
private val controllerAxes = intArrayOf(
|
||||||
|
MotionEvent.AXIS_X,
|
||||||
|
MotionEvent.AXIS_Y,
|
||||||
|
MotionEvent.AXIS_Z,
|
||||||
|
MotionEvent.AXIS_RX,
|
||||||
|
MotionEvent.AXIS_RY,
|
||||||
|
MotionEvent.AXIS_RZ,
|
||||||
|
MotionEvent.AXIS_HAT_X,
|
||||||
|
MotionEvent.AXIS_HAT_Y,
|
||||||
|
MotionEvent.AXIS_LTRIGGER,
|
||||||
|
MotionEvent.AXIS_RTRIGGER
|
||||||
|
)
|
||||||
|
|
||||||
|
fun isPhysicalGameController(device: InputDevice?): Boolean {
|
||||||
|
device ?: return false
|
||||||
|
|
||||||
|
if (device.isVirtual) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val sources = device.sources
|
||||||
|
val hasControllerSource =
|
||||||
|
sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD ||
|
||||||
|
sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
|
||||||
|
if (!hasControllerSource) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val hasControllerButtons = device.hasKeys(*controllerButtons).any { it }
|
||||||
|
val hasControllerAxes = device.motionRanges.any { range ->
|
||||||
|
controllerAxes.contains(range.axis)
|
||||||
|
}
|
||||||
|
return hasControllerButtons || hasControllerAxes
|
||||||
|
}
|
||||||
|
|
||||||
fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
||||||
val action = when (event.action) {
|
val action = when (event.action) {
|
||||||
KeyEvent.ACTION_DOWN -> NativeInput.ButtonState.PRESSED
|
KeyEvent.ACTION_DOWN -> NativeInput.ButtonState.PRESSED
|
||||||
|
|
@ -57,10 +111,7 @@ object InputHandler {
|
||||||
val inputSettings = NativeConfig.getInputSettings(true)
|
val inputSettings = NativeConfig.getInputSettings(true)
|
||||||
deviceIds.forEach { deviceId ->
|
deviceIds.forEach { deviceId ->
|
||||||
InputDevice.getDevice(deviceId)?.apply {
|
InputDevice.getDevice(deviceId)?.apply {
|
||||||
// Verify that the device has gamepad buttons, control sticks, or both.
|
if (isPhysicalGameController(this)) {
|
||||||
if (sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD ||
|
|
||||||
sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
|
|
||||||
) {
|
|
||||||
if (!gameControllerDeviceIds.contains(controllerNumber)) {
|
if (!gameControllerDeviceIds.contains(controllerNumber)) {
|
||||||
gameControllerDeviceIds[controllerNumber] = YuzuPhysicalDevice(
|
gameControllerDeviceIds[controllerNumber] = YuzuPhysicalDevice(
|
||||||
this,
|
this,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue