Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

windows-helper: coalesce native lane moves per UI drain

Alex Hughesa97ef05c9b1d
diff --git a/windows-helper/src/Lane.cs b/windows-helper/src/Lane.cs
index aad143c6..0e018d2e 100644
--- a/windows-helper/src/Lane.cs
+++ b/windows-helper/src/Lane.cs
@@ -123,6 +123,7 @@ namespace MacShare
case "driven": td_driven(U32(c, "route"), Json.Num(c, "speed", 1)); break;
case "hold": td_hold(Json.Bool(c, "on", false) ? 1 : 0); break;
case "fence":
+ Drain();
double fx, fy;
if (td_fence_apply(U32(c, "route"), U32(c, "mn"), Json.Num(c, "cx", 0), Json.Num(c, "cy", 0), out fx, out fy) == 1) inject(fx, fy);
break;
@@ -172,6 +173,7 @@ namespace MacShare
case "driven": td_lane_driven(lid, U32(c, "route"), Json.Num(c, "speed", 1)); break;
case "hold": td_lane_hold(lid, Json.Bool(c, "on", false) ? 1 : 0); break;
case "fence":
+ Drain();
double fx, fy;
if (td_lane_fence_apply(lid, U32(c, "route"), U32(c, "mn"), Json.Num(c, "cx", 0), Json.Num(c, "cy", 0), out fx, out fy) == 1) inject(fx, fy);
break;
@@ -238,32 +240,35 @@ namespace MacShare
ev["event"] = "laneEv";
ev["msg"] = msg;
if (lane > 0) ev["lane"] = (double)lane;
- emit(ev);
+ QueueEmit(ev);
break;
case TD_UP:
Dictionary<string, object> up = new Dictionary<string, object>();
up["event"] = "lane"; up["up"] = true; up["rtt"] = dx;
if (lane > 0) up["lane"] = (double)lane;
- emit(up);
+ QueueEmit(up);
break;
case TD_DOWN:
Dictionary<string, object> down = new Dictionary<string, object>();
down["event"] = "lane"; down["up"] = false;
if (lane > 0) down["lane"] = (double)lane;
- emit(down);
+ QueueEmit(down);
break;
}
}
catch (Exception) { /* never throw into the lane's thread */ }
}
- // The lane's moves to the UI thread without allocating: a fixed ring, and one posted message
- // to a message-only window per batch (not a delegate per move, as BeginInvoke would be).
- const int RING = 1024;
+ // The lane's moves to the UI thread without allocating: a fixed ring, one posted message per batch
+ // (not a delegate per move, as BeginInvoke would be). Each drain sums pending moves into one inject
+ // (mac-helper's Lane.swift). Lane events go through the same window after a drain so clicks keep
+ // the pointer they followed.
const uint WM_LANE = 0x8000 + 0x4C; // WM_APP + 'L'
- static readonly double[] ring = new double[RING * 2];
- static readonly object ringGate = new object();
- static int head, tail, posted;
+ const uint WM_LANE_EMIT = 0x8000 + 0x4D; // WM_APP + 'M'
+ static readonly LaneMoveRing moveRing = new LaneMoveRing();
+ static readonly List<Dictionary<string, object>> pendingEmit = new List<Dictionary<string, object>>();
+ static readonly object emitGate = new object();
+ static int posted;
static Sink sink;
static readonly IntPtr HWND_MESSAGE = new IntPtr(-3);
@@ -280,47 +285,47 @@ namespace MacShare
protected override void WndProc(ref Message m)
{
if (m.Msg == (int)WM_LANE) { Drain(); return; }
+ if (m.Msg == (int)WM_LANE_EMIT) { Drain(); FlushEmit(); return; }
base.WndProc(ref m);
}
}
static void Queue(double dx, double dy)
{
- lock (ringGate)
+ moveRing.Enqueue(dx, dy);
+ if (sink != null && Interlocked.Exchange(ref posted, 1) == 0) PostMessage(sink.Handle, WM_LANE, IntPtr.Zero, IntPtr.Zero);
+ }
+
+ static void QueueEmit(Dictionary<string, object> ev)
+ {
+ if (sink == null)
{
- if (head - tail < RING)
- {
- int i = (head % RING) * 2;
- ring[i] = dx;
- ring[i + 1] = dy;
- head++;
- }
- else // full (the UI thread is stuck): add to the newest, nothing is lost
- {
- int i = ((head - 1) % RING) * 2;
- ring[i] += dx;
- ring[i + 1] += dy;
- }
+ if (emit != null) emit(ev);
+ return;
}
- if (sink != null && Interlocked.Exchange(ref posted, 1) == 0) PostMessage(sink.Handle, WM_LANE, IntPtr.Zero, IntPtr.Zero);
+ lock (emitGate) pendingEmit.Add(ev);
+ PostMessage(sink.Handle, WM_LANE_EMIT, IntPtr.Zero, IntPtr.Zero);
}
- static void Drain()
+ static void FlushEmit()
{
- Interlocked.Exchange(ref posted, 0); // a move from now on posts again
- for (;;)
+ if (emit == null) return;
+ Dictionary<string, object>[] batch;
+ lock (emitGate)
{
- double dx, dy;
- lock (ringGate)
- {
- if (tail == head) return;
- int i = (tail % RING) * 2;
- dx = ring[i];
- dy = ring[i + 1];
- tail++;
- }
- inject(dx, dy);
+ if (pendingEmit.Count == 0) return;
+ batch = pendingEmit.ToArray();
+ pendingEmit.Clear();
}
+ for (int i = 0; i < batch.Length; i++) emit(batch[i]);
+ }
+
+ static void Drain()
+ {
+ Interlocked.Exchange(ref posted, 0); // a move from now on posts again
+ if (inject == null) return;
+ double dx, dy;
+ if (moveRing.TryDrainCoalesced(out dx, out dy)) inject(dx, dy);
}
}
}
diff --git a/windows-helper/src/LaneMoveRing.cs b/windows-helper/src/LaneMoveRing.cs
new file mode 100644
index 00000000..e056b32d
--- /dev/null
+++ b/windows-helper/src/LaneMoveRing.cs
@@ -0,0 +1,72 @@
+// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
+// Pending lane moves from the UDP thread: a fixed-capacity ring. When the UI thread drains, every
+// slot still queued is summed into one delta (as mac-helper's Lane.swift), so injection keeps up with
+// high-rate move streams.
+using System;
+
+namespace MacShare
+{
+ sealed class LaneMoveRing
+ {
+ public const int Capacity = 1024;
+
+ readonly double[] ring = new double[Capacity * 2];
+ readonly object gate = new object();
+ int head, tail;
+
+ public void Enqueue(double dx, double dy)
+ {
+ lock (gate)
+ {
+ if (head - tail < Capacity)
+ {
+ int i = (head % Capacity) * 2;
+ ring[i] = dx;
+ ring[i + 1] = dy;
+ head++;
+ }
+ else // full (the UI thread is stuck): add to the newest, nothing is lost
+ {
+ int i = ((head - 1) % Capacity) * 2;
+ ring[i] += dx;
+ ring[i + 1] += dy;
+ }
+ }
+ }
+
+ // One coalesced delta for everything still in the ring; false when empty.
+ public bool TryDrainCoalesced(out double dx, out double dy)
+ {
+ lock (gate)
+ {
+ if (tail == head)
+ {
+ dx = dy = 0;
+ return false;
+ }
+ dx = dy = 0;
+ while (tail != head)
+ {
+ int i = (tail % Capacity) * 2;
+ dx += ring[i];
+ dy += ring[i + 1];
+ tail++;
+ }
+ return dx != 0 || dy != 0;
+ }
+ }
+
+ public int Queued
+ {
+ get { lock (gate) return head - tail; }
+ }
+
+ public void Reset()
+ {
+ lock (gate)
+ {
+ head = tail = 0;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/windows-helper/test/LaneMoveRingTest.cs b/windows-helper/test/LaneMoveRingTest.cs
new file mode 100644
index 00000000..7a1e6838
--- /dev/null
+++ b/windows-helper/test/LaneMoveRingTest.cs
@@ -0,0 +1,51 @@
+// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
+using System;
+using MacShare;
+
+class LaneMoveRingTest
+{
+ static void Check(bool actual, bool expected, string name)
+ {
+ if (actual != expected) throw new Exception(name);
+ }
+
+ static void Near(double actual, double expected, string name)
+ {
+ if (Math.Abs(actual - expected) > 1e-9) throw new Exception(name + " got " + actual);
+ }
+
+ static void Main()
+ {
+ LaneMoveRing ring = new LaneMoveRing();
+ double dx, dy;
+
+ ring.Enqueue(1, 2);
+ ring.Enqueue(3, 4);
+ ring.Enqueue(-0.5, 1);
+ Check(ring.TryDrainCoalesced(out dx, out dy), true, "coalesce");
+ Near(dx, 3.5, "coalesce dx");
+ Near(dy, 7, "coalesce dy");
+ Check(ring.TryDrainCoalesced(out dx, out dy), false, "empty after drain");
+ if (ring.Queued != 0) throw new Exception("queue empty");
+
+ ring.Reset();
+ for (int i = 0; i < LaneMoveRing.Capacity + 16; i++) ring.Enqueue(1, -1);
+ Check(ring.TryDrainCoalesced(out dx, out dy), true, "overflow drain");
+ Near(dx, LaneMoveRing.Capacity + 16, "overflow dx");
+ Near(dy, -(LaneMoveRing.Capacity + 16), "overflow dy");
+ if (ring.Queued != 0) throw new Exception("overflow emptied");
+
+ ring.Reset();
+ ring.Enqueue(1e200, 0);
+ ring.Enqueue(1e200, 0);
+ Check(ring.TryDrainCoalesced(out dx, out dy), true, "large delta");
+ Near(dx, 2e200, "large dx");
+
+ ring.Reset();
+ ring.Enqueue(0, 0);
+ ring.Enqueue(0, 0);
+ Check(ring.TryDrainCoalesced(out dx, out dy), false, "zero-only");
+
+ Console.WriteLine("LaneMoveRing: passed");
+ }
+}
\ No newline at end of file