Add UHID_DESTROY control message

This message will be sent on gamepad disconnection.

Contrary to keyboard and mouse devices, which are registered once and
unregistered when scrcpy exists, each physical gamepad is mapped with
its own HID id, and they can be plugged and unplugged dynamically.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
This commit is contained in:
Romain Vimont 2024-09-06 23:08:08 +02:00
commit 64a25f6e9d
6 changed files with 69 additions and 3 deletions

View file

@ -21,7 +21,8 @@ public final class ControlMessage {
public static final int TYPE_ROTATE_DEVICE = 11;
public static final int TYPE_UHID_CREATE = 12;
public static final int TYPE_UHID_INPUT = 13;
public static final int TYPE_OPEN_HARD_KEYBOARD_SETTINGS = 14;
public static final int TYPE_UHID_DESTROY = 14;
public static final int TYPE_OPEN_HARD_KEYBOARD_SETTINGS = 15;
public static final long SEQUENCE_INVALID = 0;
@ -146,6 +147,13 @@ public final class ControlMessage {
return msg;
}
public static ControlMessage createUhidDestroy(int id) {
ControlMessage msg = new ControlMessage();
msg.type = TYPE_UHID_DESTROY;
msg.id = id;
return msg;
}
public int getType() {
return type;
}

View file

@ -51,6 +51,8 @@ public class ControlMessageReader {
return parseUhidCreate();
case ControlMessage.TYPE_UHID_INPUT:
return parseUhidInput();
case ControlMessage.TYPE_UHID_DESTROY:
return parseUhidDestroy();
default:
throw new ControlProtocolException("Unknown event type: " + type);
}
@ -142,6 +144,11 @@ public class ControlMessageReader {
return ControlMessage.createUhidInput(id, data);
}
private ControlMessage parseUhidDestroy() throws IOException {
int id = dis.readUnsignedShort();
return ControlMessage.createUhidDestroy(id);
}
private Position parsePosition() throws IOException {
int x = dis.readInt();
int y = dis.readInt();

View file

@ -362,6 +362,24 @@ public class ControlMessageReaderTest {
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseUhidDestroy() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(ControlMessage.TYPE_UHID_DESTROY);
dos.writeShort(42); // id
byte[] packet = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
ControlMessageReader reader = new ControlMessageReader(bis);
ControlMessage event = reader.read();
Assert.assertEquals(ControlMessage.TYPE_UHID_DESTROY, event.getType());
Assert.assertEquals(42, event.getId());
Assert.assertEquals(-1, bis.read()); // EOS
}
@Test
public void testParseOpenHardKeyboardSettings() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();