From 3848ce86f118496467ebd91b40dbf571e2a29e24 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 24 Jul 2022 16:00:26 +0200 Subject: [PATCH] Extract conversion from u16 fixed-point to float PR #3369 --- .../src/main/java/com/genymobile/scrcpy/Binary.java | 12 ++++++++++++ .../com/genymobile/scrcpy/ControlMessageReader.java | 5 +---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Binary.java b/server/src/main/java/com/genymobile/scrcpy/Binary.java index 1a096de9..db946664 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Binary.java +++ b/server/src/main/java/com/genymobile/scrcpy/Binary.java @@ -12,4 +12,16 @@ public final class Binary { public static int toUnsigned(byte value) { return value & 0xff; } + + /** + * Convert unsigned 16-bit fixed-point to a float between 0 and 1 + * + * @param value encoded value + * @return Float value between 0 and 1 + */ + public static float u16FixedPointToFloat(short value) { + int unsignedShort = Binary.toUnsigned(value); + // 0x1p16f is 2^16 as float + return unsignedShort == 0xffff ? 1f : (unsignedShort / 0x1p16f); + } } diff --git a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java index 07ef1f7c..b2d500c2 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java +++ b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java @@ -139,10 +139,7 @@ public class ControlMessageReader { int action = Binary.toUnsigned(buffer.get()); long pointerId = buffer.getLong(); Position position = readPosition(buffer); - // 16 bits fixed-point - int pressureInt = Binary.toUnsigned(buffer.getShort()); - // convert it to a float between 0 and 1 (0x1p16f is 2^16 as float) - float pressure = pressureInt == 0xffff ? 1f : (pressureInt / 0x1p16f); + float pressure = Binary.u16FixedPointToFloat(buffer.getShort()); int buttons = buffer.getInt(); return ControlMessage.createInjectTouchEvent(action, pointerId, position, pressure, buttons); }