1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
package org.yuzu.yuzu_emu.utils
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import org.yuzu.yuzu_emu.features.input.NativeInput
import org.yuzu.yuzu_emu.features.input.YuzuInputOverlayDevice
import org.yuzu.yuzu_emu.features.input.YuzuPhysicalDevice
object InputHandler {
var androidControllers = mapOf<Int, YuzuPhysicalDevice>()
var registeredControllers = mutableListOf<ParamPackage>()
fun dispatchKeyEvent(event: KeyEvent): Boolean {
val action = when (event.action) {
KeyEvent.ACTION_DOWN -> NativeInput.ButtonState.PRESSED
KeyEvent.ACTION_UP -> NativeInput.ButtonState.RELEASED
else -> return false
}
var controllerData = androidControllers[event.device.controllerNumber]
if (controllerData == null) {
updateControllerData()
controllerData = androidControllers[event.device.controllerNumber] ?: return false
}
NativeInput.onGamePadButtonEvent(
controllerData.getGUID(),
controllerData.getPort(),
event.keyCode,
action
)
return true
}
fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
val controllerData =
androidControllers[event.device.controllerNumber] ?: return false
event.device.motionRanges.forEach {
NativeInput.onGamePadAxisEvent(
controllerData.getGUID(),
controllerData.getPort(),
it.axis,
event.getAxisValue(it.axis)
)
}
return true
}
fun getDevices(): Map<Int, YuzuPhysicalDevice> {
val gameControllerDeviceIds = mutableMapOf<Int, YuzuPhysicalDevice>()
val deviceIds = InputDevice.getDeviceIds()
var port = 0
val inputSettings = NativeConfig.getInputSettings(true)
deviceIds.forEach { deviceId ->
InputDevice.getDevice(deviceId)?.apply {
// Verify that the device has gamepad buttons, control sticks, or both.
if (sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD ||
sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
) {
if (!gameControllerDeviceIds.contains(controllerNumber)) {
gameControllerDeviceIds[controllerNumber] = YuzuPhysicalDevice(
this,
port,
inputSettings[port].useSystemVibrator
)
}
port++
}
}
}
return gameControllerDeviceIds
}
fun updateControllerData() {
androidControllers = getDevices()
androidControllers.forEach {
NativeInput.registerController(it.value)
}
// Register the input overlay on a dedicated port for all player 1 vibrations
NativeInput.registerController(YuzuInputOverlayDevice(androidControllers.isEmpty(), 100))
registeredControllers.clear()
NativeInput.getInputDevices().forEach {
registeredControllers.add(ParamPackage(it))
}
registeredControllers.sortBy { it.get("port", 0) }
}
fun InputDevice.getGUID(): String = String.format("%016x%016x", productId, vendorId)
}
|