From 852a8a00273c9128efeed0217a8e5d3fcd8bf780 Mon Sep 17 00:00:00 2001 From: Lena Date: Sat, 1 Aug 2026 00:00:00 +0000 Subject: app: harden mirroring lifecycle and state --- .../java/invalid/lena/scrcpy/TouchGeometry.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 app/src/main/java/invalid/lena/scrcpy/TouchGeometry.java (limited to 'app/src/main/java/invalid/lena/scrcpy/TouchGeometry.java') diff --git a/app/src/main/java/invalid/lena/scrcpy/TouchGeometry.java b/app/src/main/java/invalid/lena/scrcpy/TouchGeometry.java new file mode 100644 index 0000000..5ee80d4 --- /dev/null +++ b/app/src/main/java/invalid/lena/scrcpy/TouchGeometry.java @@ -0,0 +1,46 @@ +package invalid.lena.scrcpy; + +// Publishes a target size and its matching on-screen viewport as one +// immutable value. A target resize invalidates the old viewport until the UI +// lays out the new generation. +final class TouchGeometry { + + static final class Snapshot { + final int targetW, targetH; + final int x, y, w, h; + + Snapshot(int targetW, int targetH, int x, int y, int w, int h) { + this.targetW = targetW; + this.targetH = targetH; + this.x = x; + this.y = y; + this.w = w; + this.h = h; + } + } + + private volatile Snapshot snapshot; + private long version; + private int targetW; + private int targetH; + + synchronized void setTargetSize(long nextVersion, int w, int h) { + if (nextVersion <= version) throw new IllegalArgumentException("stale target version"); + if (w <= 0 || h <= 0) throw new IllegalArgumentException("invalid target size"); + version = nextVersion; + targetW = w; + targetH = h; + snapshot = null; + } + + synchronized void setViewport(long expectedVersion, int x, int y, int w, int h) { + if (expectedVersion != version) return; + snapshot = version > 0 && targetW > 0 && targetH > 0 && w > 0 && h > 0 + ? new Snapshot(targetW, targetH, x, y, w, h) + : null; + } + + Snapshot snapshot() { + return snapshot; + } +} -- cgit v1.2.3