diff --git a/moonlight-common/src/com/limelight/nvstream/input/ControllerPacket.java b/moonlight-common/src/com/limelight/nvstream/input/ControllerPacket.java index f52bdc5f..fc3078cc 100644 --- a/moonlight-common/src/com/limelight/nvstream/input/ControllerPacket.java +++ b/moonlight-common/src/com/limelight/nvstream/input/ControllerPacket.java @@ -46,6 +46,17 @@ public class ControllerPacket extends InputPacket { public static final short PACKET_LENGTH = PAYLOAD_LENGTH + InputPacket.HEADER_LENGTH; + // This is the highest value that is read as zero on the PC + public static final short ABS_LS_OFFSET = 7000; + public static final short ABS_RS_OFFSET = 7000; + + public static final double ABS_LS_SCALE_FACTOR = 1 - (ABS_LS_OFFSET / 32768.0); + public static final double ABS_RS_SCALE_FACTOR = 1 - (ABS_RS_OFFSET / 32768.0); + + // Set this flag if you want ControllerPacket to handle scaling for you + // Note: You MUST properly handle deadzones to use this flag + public static boolean enableAxisScaling = false; + short buttonFlags; byte leftTrigger; byte rightTrigger; @@ -63,10 +74,51 @@ public class ControllerPacket extends InputPacket { this.buttonFlags = buttonFlags; this.leftTrigger = leftTrigger; this.rightTrigger = rightTrigger; - this.leftStickX = leftStickX; - this.leftStickY = leftStickY; - this.rightStickX = rightStickX; - this.rightStickY = rightStickY; + this.leftStickX = scaleLeftStickAxis(leftStickX); + this.leftStickY = scaleLeftStickAxis(leftStickY); + this.rightStickX = scaleRightStickAxis(rightStickX); + this.rightStickY = scaleRightStickAxis(rightStickY); + } + + private static short scaleAxis(int axisValue, short offset, double factor) { + // Exit quit if it's zero + if (axisValue == 0) { + return 0; + } + + // Remember the sign and remove it from the value + int sign = axisValue < 0 ? -1 : 1; + axisValue = Math.abs(axisValue); + + // Scale the initial value + axisValue = (int)(axisValue * factor); + + // Add the offset + axisValue += offset; + + // Correct the value if it's over the limit + if (axisValue > 32767) { + axisValue = 32767; + } + + // Restore sign and return + return (short)(sign * axisValue); + } + + private static short scaleLeftStickAxis(short axisValue) { + if (enableAxisScaling) { + axisValue = scaleAxis(axisValue, ABS_LS_OFFSET, ABS_LS_SCALE_FACTOR); + } + + return axisValue; + } + + private static short scaleRightStickAxis(short axisValue) { + if (enableAxisScaling) { + axisValue = scaleAxis(axisValue, ABS_RS_OFFSET, ABS_RS_SCALE_FACTOR); + } + + return axisValue; } @Override