Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.winlator.inputcontrols.ControlElement
import com.winlator.inputcontrols.ControlsProfile
import com.winlator.inputcontrols.ExternalController
import com.winlator.inputcontrols.ExternalControllerBinding
import com.winlator.inputcontrols.JoyConSupport
import com.winlator.math.Mathf
import com.winlator.xserver.XServer
import java.util.Timer
Expand Down Expand Up @@ -113,37 +114,38 @@ class PhysicalControllerHandler(
*/
fun onKeyEvent(event: KeyEvent): Boolean {
if (profile != null && event.repeatCount == 0) {
val keyCode = JoyConSupport.remapKeyCode(event.device, event)
if (radialMenuPressed && !isRadialMenuOpenerDevice(event.deviceId)) return true
val controller = profile?.getController(event.deviceId)
if (controller != null) {
val controllerBinding = controller.getControllerBinding(event.keyCode)
val controllerBinding = controller.getControllerBinding(keyCode)
if (radialMenuPressed && controllerBinding?.bindingCombo?.bindings?.contains(Binding.OPEN_RADIAL_MENU) == true) {
if (event.keyCode == radialMenuOpenerKeyCode ||
if (keyCode == radialMenuOpenerKeyCode ||
radialMenuOpenerKeyCode == KeyEvent.KEYCODE_UNKNOWN
) {
handleInputEvent(
controllerBinding.bindingCombo,
event.action == KeyEvent.ACTION_DOWN,
sourceKeyCode = event.keyCode,
sourceKeyCode = keyCode,
sourceDeviceId = event.deviceId,
sourceController = controller,
)
return true
}
handleRadialMenuNavigationKey(event)
handleRadialMenuNavigationKey(event, keyCode)
return true
}
if (radialMenuPressed && handleRadialMenuNavigationKey(event)) {
if (radialMenuPressed && handleRadialMenuNavigationKey(event, keyCode)) {
return true
}

if (controllerBinding != null) {
// Some controllers emit BOTH a digital KeyEvent for L2/R2 and an analog axis value in MotionEvent.
// If this physical key is mapped to a virtual trigger AND the device exposes trigger axes,
// ignore the KeyEvent to avoid an initial "full press" spike. MotionEvent will provide the analog value.
if ((event.keyCode == KeyEvent.KEYCODE_BUTTON_L2 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R2) &&
if ((keyCode == KeyEvent.KEYCODE_BUTTON_L2 || keyCode == KeyEvent.KEYCODE_BUTTON_R2) &&
controllerBinding.bindingCombo.bindings.any { it == Binding.GAMEPAD_BUTTON_L2 || it == Binding.GAMEPAD_BUTTON_R2 } &&
deviceHasTriggerAxis(event.device, event.keyCode)
deviceHasTriggerAxis(event.device, keyCode)
) {
return true
}
Expand All @@ -154,7 +156,7 @@ class PhysicalControllerHandler(
controllerBinding.bindingCombo,
event.action == KeyEvent.ACTION_DOWN,
offset,
sourceKeyCode = event.keyCode,
sourceKeyCode = keyCode,
sourceDeviceId = event.deviceId,
sourceController = controller,
)
Expand Down Expand Up @@ -839,9 +841,9 @@ class PhysicalControllerHandler(
return radialMenuOpenerDeviceId == UNKNOWN_DEVICE_ID || deviceId == radialMenuOpenerDeviceId
}

private fun handleRadialMenuNavigationKey(event: KeyEvent): Boolean {
private fun handleRadialMenuNavigationKey(event: KeyEvent, keyCode: Int): Boolean {
if (event.action != KeyEvent.ACTION_DOWN) {
return when (event.keyCode) {
return when (keyCode) {
KeyEvent.KEYCODE_DPAD_UP,
KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_DPAD_LEFT,
Expand All @@ -850,7 +852,7 @@ class PhysicalControllerHandler(
else -> false
}
}
val vector = when (event.keyCode) {
val vector = when (keyCode) {
KeyEvent.KEYCODE_DPAD_UP -> 0f to -1f
KeyEvent.KEYCODE_DPAD_DOWN -> 0f to 1f
KeyEvent.KEYCODE_DPAD_LEFT -> -1f to 0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1599,8 +1599,9 @@ fun XServerScreen(
) {
winHandler.refreshControllerMappingsForHotplug()
}
val assignedSlot = ControllerManager.getInstance().getSlotForDevice(it.event.device.id)
if (assignedSlot > 0) {
val routeDirectly = ControllerManager.getInstance()
.shouldRouteDirectlyToWinHandler(it.event.device.id)
if (routeDirectly) {
handled = winHandler.onKeyEvent(it.event)
} else {
winHandler.setCurrentController(it.event.device.id)
Expand Down Expand Up @@ -1651,8 +1652,9 @@ fun XServerScreen(
if (isGamepad && it.event != null) {
val winHandler = xServerView!!.getxServer().winHandler
ControllerManager.getInstance().noteGamepadActivity(it.event)
val assignedSlot = ControllerManager.getInstance().getSlotForDevice(it.event.device.id)
if (assignedSlot > 0) {
val routeDirectly = ControllerManager.getInstance()
.shouldRouteDirectlyToWinHandler(it.event.device.id)
if (routeDirectly) {
handled = winHandler.onGenericMotionEvent(it.event)
} else {
winHandler.setCurrentController(it.event.device.id)
Expand Down
Loading