Skip to content

Commit

Permalink
Make the device acknowledge device clipboard
Browse files Browse the repository at this point in the history
If the client provided a sequence number on SET_CLIPBOARD request, make
the device send back an acknowledgement once the clipboard is set.

PR #2814 <#2814>
  • Loading branch information
rom1v committed Nov 23, 2021
1 parent 2a0730e commit 41abe02
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public final class ControlMessage {
public static final int TYPE_SET_SCREEN_POWER_MODE = 10;
public static final int TYPE_ROTATE_DEVICE = 11;

public static final long SEQUENCE_INVALID = 0;

private int type;
private String text;
private int metaState; // KeyEvent.META_*
Expand Down
5 changes: 5 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ private void handleEvent() throws IOException {
}
break;
case ControlMessage.TYPE_SET_CLIPBOARD:
long sequence = msg.getSequence();
setClipboard(msg.getText(), msg.getPaste());
if (sequence != ControlMessage.SEQUENCE_INVALID) {
// Acknowledgement requested
sender.pushAckClipboard(sequence);
}
break;
case ControlMessage.TYPE_SET_SCREEN_POWER_MODE:
if (device.supportsInputEvents()) {
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/DeviceMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public final class DeviceMessage {
public static final int TYPE_CLIPBOARD = 0;
public static final int TYPE_ACK_CLIPBOARD = 1;

public static final long SEQUENCE_INVALID = ControlMessage.SEQUENCE_INVALID;

private int type;
private String text;
private long sequence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public final class DeviceMessageSender {

private String clipboardText;

private long ack;

public DeviceMessageSender(DesktopConnection connection) {
this.connection = connection;
}
Expand All @@ -17,18 +19,34 @@ public synchronized void pushClipboardText(String text) {
notify();
}

public synchronized void pushAckClipboard(long sequence) {
ack = sequence;
notify();
}

public void loop() throws IOException, InterruptedException {
while (true) {
String text;
long sequence;
synchronized (this) {
while (clipboardText == null) {
while (ack == DeviceMessage.SEQUENCE_INVALID && clipboardText == null) {
wait();
}
text = clipboardText;
clipboardText = null;

sequence = ack;
ack = DeviceMessage.SEQUENCE_INVALID;
}

if (sequence != DeviceMessage.SEQUENCE_INVALID) {
DeviceMessage event = DeviceMessage.createAckClipboard(sequence);
connection.sendDeviceMessage(event);
}
if (text != null) {
DeviceMessage event = DeviceMessage.createClipboard(text);
connection.sendDeviceMessage(event);
}
DeviceMessage event = DeviceMessage.createClipboard(text);
connection.sendDeviceMessage(event);
}
}
}

0 comments on commit 41abe02

Please sign in to comment.