Extract conversion from fixed-point u16 to float

So that it can be reused for other fields.

PR #3369 <https://github.com/Genymobile/scrcpy/pull/3369>
This commit is contained in:
Romain Vimont 2022-07-24 16:00:26 +02:00
commit 72ff957e9a

View file

@ -139,10 +139,7 @@ public class ControlMessageReader {
int action = toUnsigned(buffer.get());
long pointerId = buffer.getLong();
Position position = readPosition(buffer);
// 16 bits fixed-point
int pressureInt = 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 = fixedPointToFloat(buffer.getShort());
int buttons = buffer.getInt();
return ControlMessage.createInjectTouchEvent(action, pointerId, position, pressure, buttons);
}
@ -210,4 +207,10 @@ public class ControlMessageReader {
private static int toUnsigned(byte value) {
return value & 0xff;
}
private static float fixedPointToFloat(short value) {
int unsignedShort = toUnsigned(value);
// convert 16 bits fixed-point to a float between 0 and 1 (0x1p16f is 2^16 as float)
return unsignedShort == 0xffff ? 1f : (unsignedShort / 0x1p16f);
}
}