summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt3
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt53
2 files changed, 50 insertions, 6 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt
index 0eda27f7d..f37875ffe 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt
@@ -64,6 +64,8 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener {
64 private var motionTimestamp: Long = 0 64 private var motionTimestamp: Long = 0
65 private var flipMotionOrientation: Boolean = false 65 private var flipMotionOrientation: Boolean = false
66 66
67 private var controllerIds = InputHandler.getGameControllerIds()
68
67 private val actionPause = "ACTION_EMULATOR_PAUSE" 69 private val actionPause = "ACTION_EMULATOR_PAUSE"
68 private val actionPlay = "ACTION_EMULATOR_PLAY" 70 private val actionPlay = "ACTION_EMULATOR_PLAY"
69 private val actionMute = "ACTION_EMULATOR_MUTE" 71 private val actionMute = "ACTION_EMULATOR_MUTE"
@@ -155,6 +157,7 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener {
155 super.onResume() 157 super.onResume()
156 nfcReader.startScanning() 158 nfcReader.startScanning()
157 startMotionSensorListener() 159 startMotionSensorListener()
160 InputHandler.updateControllerIds()
158 161
159 buildPictureInPictureParams() 162 buildPictureInPictureParams()
160 } 163 }
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt
index fec40e27d..fc6a8b5cb 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt
@@ -3,17 +3,24 @@
3 3
4package org.yuzu.yuzu_emu.utils 4package org.yuzu.yuzu_emu.utils
5 5
6import android.view.InputDevice
6import android.view.KeyEvent 7import android.view.KeyEvent
7import android.view.MotionEvent 8import android.view.MotionEvent
8import kotlin.math.sqrt 9import kotlin.math.sqrt
9import org.yuzu.yuzu_emu.NativeLibrary 10import org.yuzu.yuzu_emu.NativeLibrary
10 11
11object InputHandler { 12object InputHandler {
13 private var controllerIds = getGameControllerIds()
14
12 fun initialize() { 15 fun initialize() {
13 // Connect first controller 16 // Connect first controller
14 NativeLibrary.onGamePadConnectEvent(getPlayerNumber(NativeLibrary.Player1Device)) 17 NativeLibrary.onGamePadConnectEvent(getPlayerNumber(NativeLibrary.Player1Device))
15 } 18 }
16 19
20 fun updateControllerIds() {
21 controllerIds = getGameControllerIds()
22 }
23
17 fun dispatchKeyEvent(event: KeyEvent): Boolean { 24 fun dispatchKeyEvent(event: KeyEvent): Boolean {
18 val button: Int = when (event.device.vendorId) { 25 val button: Int = when (event.device.vendorId) {
19 0x045E -> getInputXboxButtonKey(event.keyCode) 26 0x045E -> getInputXboxButtonKey(event.keyCode)
@@ -35,7 +42,7 @@ object InputHandler {
35 } 42 }
36 43
37 return NativeLibrary.onGamePadButtonEvent( 44 return NativeLibrary.onGamePadButtonEvent(
38 getPlayerNumber(event.device.controllerNumber), 45 getPlayerNumber(event.device.controllerNumber, event.deviceId),
39 button, 46 button,
40 action 47 action
41 ) 48 )
@@ -58,9 +65,14 @@ object InputHandler {
58 return true 65 return true
59 } 66 }
60 67
61 private fun getPlayerNumber(index: Int): Int { 68 private fun getPlayerNumber(index: Int, deviceId: Int = -1): Int {
69 var deviceIndex = index
70 if (deviceId != -1) {
71 deviceIndex = controllerIds[deviceId]!!
72 }
73
62 // TODO: Joycons are handled as different controllers. Find a way to merge them. 74 // TODO: Joycons are handled as different controllers. Find a way to merge them.
63 return when (index) { 75 return when (deviceIndex) {
64 2 -> NativeLibrary.Player2Device 76 2 -> NativeLibrary.Player2Device
65 3 -> NativeLibrary.Player3Device 77 3 -> NativeLibrary.Player3Device
66 4 -> NativeLibrary.Player4Device 78 4 -> NativeLibrary.Player4Device
@@ -238,7 +250,7 @@ object InputHandler {
238 } 250 }
239 251
240 private fun setGenericAxisInput(event: MotionEvent, axis: Int) { 252 private fun setGenericAxisInput(event: MotionEvent, axis: Int) {
241 val playerNumber = getPlayerNumber(event.device.controllerNumber) 253 val playerNumber = getPlayerNumber(event.device.controllerNumber, event.deviceId)
242 254
243 when (axis) { 255 when (axis) {
244 MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> 256 MotionEvent.AXIS_X, MotionEvent.AXIS_Y ->
@@ -297,7 +309,7 @@ object InputHandler {
297 309
298 private fun setJoyconAxisInput(event: MotionEvent, axis: Int) { 310 private fun setJoyconAxisInput(event: MotionEvent, axis: Int) {
299 // Joycon support is half dead. Right joystick doesn't work 311 // Joycon support is half dead. Right joystick doesn't work
300 val playerNumber = getPlayerNumber(event.device.controllerNumber) 312 val playerNumber = getPlayerNumber(event.device.controllerNumber, event.deviceId)
301 313
302 when (axis) { 314 when (axis) {
303 MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> 315 MotionEvent.AXIS_X, MotionEvent.AXIS_Y ->
@@ -325,7 +337,7 @@ object InputHandler {
325 } 337 }
326 338
327 private fun setRazerAxisInput(event: MotionEvent, axis: Int) { 339 private fun setRazerAxisInput(event: MotionEvent, axis: Int) {
328 val playerNumber = getPlayerNumber(event.device.controllerNumber) 340 val playerNumber = getPlayerNumber(event.device.controllerNumber, event.deviceId)
329 341
330 when (axis) { 342 when (axis) {
331 MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> 343 MotionEvent.AXIS_X, MotionEvent.AXIS_Y ->
@@ -362,4 +374,33 @@ object InputHandler {
362 ) 374 )
363 } 375 }
364 } 376 }
377
378 fun getGameControllerIds(): Map<Int, Int> {
379 val gameControllerDeviceIds = mutableMapOf<Int, Int>()
380 val deviceIds = InputDevice.getDeviceIds()
381 var controllerSlot = 1
382 deviceIds.forEach { deviceId ->
383 InputDevice.getDevice(deviceId)?.apply {
384 // Don't over-assign controllers
385 if (controllerSlot >= 8) {
386 return gameControllerDeviceIds
387 }
388
389 // Verify that the device has gamepad buttons, control sticks, or both.
390 if (sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD ||
391 sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
392 ) {
393 // This device is a game controller. Store its device ID.
394 if (deviceId and id and vendorId and productId != 0) {
395 // Additionally filter out devices that have no ID
396 gameControllerDeviceIds
397 .takeIf { !it.contains(deviceId) }
398 ?.put(deviceId, controllerSlot)
399 controllerSlot++
400 }
401 }
402 }
403 }
404 return gameControllerDeviceIds
405 }
365} 406}