From 2a0730ee9bacc8df92c7c38ce83a735182d685e0 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 20 Nov 2021 12:10:09 +0100 Subject: [PATCH] Add device clipboard set acknowledgement Add a device message type so that the device could send acknowledgements for SET_CLIPBOARD requests. PR #2814 --- app/src/device_msg.c | 6 ++++++ app/src/device_msg.h | 4 ++++ app/src/receiver.c | 3 +++ app/tests/test_device_msg_deserialize.c | 15 ++++++++++++++ .../com/genymobile/scrcpy/DeviceMessage.java | 13 ++++++++++++ .../scrcpy/DeviceMessageWriter.java | 6 +++++- .../scrcpy/DeviceMessageWriterTest.java | 20 +++++++++++++++++++ 7 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/src/device_msg.c b/app/src/device_msg.c index 827f4213..4163b9fc 100644 --- a/app/src/device_msg.c +++ b/app/src/device_msg.c @@ -1,5 +1,6 @@ #include "device_msg.h" +#include #include #include @@ -34,6 +35,11 @@ device_msg_deserialize(const unsigned char *buf, size_t len, msg->clipboard.text = text; return 5 + clipboard_len; } + case DEVICE_MSG_TYPE_ACK_CLIPBOARD: { + uint64_t sequence = buffer_read64be(&buf[1]); + msg->ack_clipboard.sequence = sequence; + return 9; + } default: LOGW("Unknown device message type: %d", (int) msg->type); return -1; // error, we cannot recover diff --git a/app/src/device_msg.h b/app/src/device_msg.h index 888d9216..a0c989e3 100644 --- a/app/src/device_msg.h +++ b/app/src/device_msg.h @@ -13,6 +13,7 @@ enum device_msg_type { DEVICE_MSG_TYPE_CLIPBOARD, + DEVICE_MSG_TYPE_ACK_CLIPBOARD, }; struct device_msg { @@ -21,6 +22,9 @@ struct device_msg { struct { char *text; // owned, to be freed by free() } clipboard; + struct { + uint64_t sequence; + } ack_clipboard; }; }; diff --git a/app/src/receiver.c b/app/src/receiver.c index b5cf9b39..52e6034d 100644 --- a/app/src/receiver.c +++ b/app/src/receiver.c @@ -37,6 +37,9 @@ process_msg(struct device_msg *msg) { SDL_SetClipboardText(msg->clipboard.text); break; } + case DEVICE_MSG_TYPE_ACK_CLIPBOARD: + // TODO + break; } } diff --git a/app/tests/test_device_msg_deserialize.c b/app/tests/test_device_msg_deserialize.c index 3427d640..835096c0 100644 --- a/app/tests/test_device_msg_deserialize.c +++ b/app/tests/test_device_msg_deserialize.c @@ -47,11 +47,26 @@ static void test_deserialize_clipboard_big(void) { device_msg_destroy(&msg); } +static void test_deserialize_ack_set_clipboard(void) { + const unsigned char input[] = { + DEVICE_MSG_TYPE_ACK_CLIPBOARD, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence + }; + + struct device_msg msg; + ssize_t r = device_msg_deserialize(input, sizeof(input), &msg); + assert(r == 9); + + assert(msg.type == DEVICE_MSG_TYPE_ACK_CLIPBOARD); + assert(msg.ack_clipboard.sequence == UINT64_C(0x0102030405060708)); +} + int main(int argc, char *argv[]) { (void) argc; (void) argv; test_deserialize_clipboard(); test_deserialize_clipboard_big(); + test_deserialize_ack_set_clipboard(); return 0; } diff --git a/server/src/main/java/com/genymobile/scrcpy/DeviceMessage.java b/server/src/main/java/com/genymobile/scrcpy/DeviceMessage.java index c6eebd38..2e333e3f 100644 --- a/server/src/main/java/com/genymobile/scrcpy/DeviceMessage.java +++ b/server/src/main/java/com/genymobile/scrcpy/DeviceMessage.java @@ -3,9 +3,11 @@ package com.genymobile.scrcpy; public final class DeviceMessage { public static final int TYPE_CLIPBOARD = 0; + public static final int TYPE_ACK_CLIPBOARD = 1; private int type; private String text; + private long sequence; private DeviceMessage() { } @@ -17,6 +19,13 @@ public final class DeviceMessage { return event; } + public static DeviceMessage createAckClipboard(long sequence) { + DeviceMessage event = new DeviceMessage(); + event.type = TYPE_ACK_CLIPBOARD; + event.sequence = sequence; + return event; + } + public int getType() { return type; } @@ -24,4 +33,8 @@ public final class DeviceMessage { public String getText() { return text; } + + public long getSequence() { + return sequence; + } } diff --git a/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java b/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java index 15d91a35..bcd8d206 100644 --- a/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java +++ b/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java @@ -15,7 +15,7 @@ public class DeviceMessageWriter { public void writeTo(DeviceMessage msg, OutputStream output) throws IOException { buffer.clear(); - buffer.put((byte) DeviceMessage.TYPE_CLIPBOARD); + buffer.put((byte) msg.getType()); switch (msg.getType()) { case DeviceMessage.TYPE_CLIPBOARD: String text = msg.getText(); @@ -25,6 +25,10 @@ public class DeviceMessageWriter { buffer.put(raw, 0, len); output.write(rawBuffer, 0, buffer.position()); break; + case DeviceMessage.TYPE_ACK_CLIPBOARD: + buffer.putLong(msg.getSequence()); + output.write(rawBuffer, 0, buffer.position()); + break; default: Ln.w("Unknown device message: " + msg.getType()); break; diff --git a/server/src/test/java/com/genymobile/scrcpy/DeviceMessageWriterTest.java b/server/src/test/java/com/genymobile/scrcpy/DeviceMessageWriterTest.java index 88bf2af9..7b917d33 100644 --- a/server/src/test/java/com/genymobile/scrcpy/DeviceMessageWriterTest.java +++ b/server/src/test/java/com/genymobile/scrcpy/DeviceMessageWriterTest.java @@ -32,4 +32,24 @@ public class DeviceMessageWriterTest { Assert.assertArrayEquals(expected, actual); } + + @Test + public void testSerializeAckSetClipboard() throws IOException { + DeviceMessageWriter writer = new DeviceMessageWriter(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeByte(DeviceMessage.TYPE_ACK_CLIPBOARD); + dos.writeLong(0x0102030405060708L); + + byte[] expected = bos.toByteArray(); + + DeviceMessage msg = DeviceMessage.createAckClipboard(0x0102030405060708L); + bos = new ByteArrayOutputStream(); + writer.writeTo(msg, bos); + + byte[] actual = bos.toByteArray(); + + Assert.assertArrayEquals(expected, actual); + } }