Skip to content

Commit

Permalink
Move toUnsigned() to a Binary util class
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Aug 3, 2022
1 parent d23b3e8 commit a964825
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
15 changes: 15 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Binary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.genymobile.scrcpy;

public final class Binary {
private Binary() {
// not instantiable
}

public static int toUnsigned(short value) {
return value & 0xffff;
}

public static int toUnsigned(byte value) {
return value & 0xff;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private ControlMessage parseInjectKeycode() {
if (buffer.remaining() < INJECT_KEYCODE_PAYLOAD_LENGTH) {
return null;
}
int action = toUnsigned(buffer.get());
int action = Binary.toUnsigned(buffer.get());
int keycode = buffer.getInt();
int repeat = buffer.getInt();
int metaState = buffer.getInt();
Expand Down Expand Up @@ -136,11 +136,11 @@ private ControlMessage parseInjectTouchEvent() {
if (buffer.remaining() < INJECT_TOUCH_EVENT_PAYLOAD_LENGTH) {
return null;
}
int action = toUnsigned(buffer.get());
int action = Binary.toUnsigned(buffer.get());
long pointerId = buffer.getLong();
Position position = readPosition(buffer);
// 16 bits fixed-point
int pressureInt = toUnsigned(buffer.getShort());
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);
int buttons = buffer.getInt();
Expand All @@ -162,15 +162,15 @@ private ControlMessage parseBackOrScreenOnEvent() {
if (buffer.remaining() < BACK_OR_SCREEN_ON_LENGTH) {
return null;
}
int action = toUnsigned(buffer.get());
int action = Binary.toUnsigned(buffer.get());
return ControlMessage.createBackOrScreenOn(action);
}

private ControlMessage parseGetClipboard() {
if (buffer.remaining() < GET_CLIPBOARD_LENGTH) {
return null;
}
int copyKey = toUnsigned(buffer.get());
int copyKey = Binary.toUnsigned(buffer.get());
return ControlMessage.createGetClipboard(copyKey);
}

Expand Down Expand Up @@ -198,16 +198,8 @@ private ControlMessage parseSetScreenPowerMode() {
private static Position readPosition(ByteBuffer buffer) {
int x = buffer.getInt();
int y = buffer.getInt();
int screenWidth = toUnsigned(buffer.getShort());
int screenHeight = toUnsigned(buffer.getShort());
int screenWidth = Binary.toUnsigned(buffer.getShort());
int screenHeight = Binary.toUnsigned(buffer.getShort());
return new Position(x, y, screenWidth, screenHeight);
}

private static int toUnsigned(short value) {
return value & 0xffff;
}

private static int toUnsigned(byte value) {
return value & 0xff;
}
}

0 comments on commit a964825

Please sign in to comment.