Feature: Draw the remote cursor in the web desktop out of control mode

Screen frames are captured without the pointer composed in (GDI SRCCOPY,
DXGI), so browsers only ever knew the cursor's shape index and could show a
cursor just in control mode, where the local pointer stands in for the remote
one. Both servers now relay the cursor position that every per-frame header
already carries: shape changes go out immediately, position changes at most
every 50 ms, and an unchanged cursor is re-sent once a second so a viewer that
joins while the pointer is idle still learns where it is.

The page hides the local pointer and draws the remote cursor at its true
position while out of control mode; entering control mode restores exactly the
old behaviour (remote-shaped CSS cursor on desktop, touchpad pointer on touch).
A server that never sends x/y leaves the page on its previous fallback.

The C++ server broadcasts from the per-frame path, so header-only frames keep a
static desktop's cursor fresh; the Go hub dedups and throttles before fanning
out. Tests cover the throttle/refresh rules and the relayed payload.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-26 17:44:13 +01:00
1 parent 6236ed1eb4
commit bfb17d0d25
12 files changed
+322 -65

No files matched your search

+37 -14
View File
@@ -73,11 +73,15 @@ type Device struct {
screenHeight int
lastKeyframe []byte // fully-packed WS binary packet of the most recent IDR
// Cursor index dedup: cursors arrive on every frame (~30 Hz) but only
// rarely change. Suppress duplicates so the WS doesn't carry redundant
// JSON messages.
// Cursor dedup/throttle: cursors arrive on every frame (~30 Hz) and the
// position changes most frames. An index change is published immediately
// (the shape matters visually); a position-only change is rate-limited so
// the WS doesn't carry 30 JSON messages per second per viewer.
cursorSeen bool
lastCursorIndex byte
lastCursorX int
lastCursorY int
cursorPubAt time.Time
// Terminal session state — at most one web terminal per device (MVP
// constraint shared with the C++ server). All three fields are
@@ -186,10 +190,11 @@ type EventHandler interface {
// OnResolutionChange fires when a screen session starts (TOKEN_BITMAPINFO)
// or whenever the device reports a new screen geometry mid-stream.
OnResolutionChange(deviceID string, width, height int)
// OnCursorChange fires when the device's foreground cursor index changes.
// Duplicates (same index as the previous frame) are filtered out by the
// hub before reaching subscribers.
OnCursorChange(deviceID string, index byte)
// OnCursorChange fires when the device's foreground cursor changes shape or
// moves. x/y are in the captured frame's coordinate space (== browser canvas
// space). The hub filters duplicates and rate-limits position-only changes
// before reaching subscribers.
OnCursorChange(deviceID string, index byte, x, y int)
// OnClipboardText delivers the device's clipboard text (already UTF-8),
// captured in response to a browser "clipboard_get" request.
OnClipboardText(deviceID string, text string)
@@ -536,25 +541,43 @@ func deviceToInfo(d *Device) DeviceInfo {
}
}
// cursorMinInterval rate-limits position-only cursor updates. Shape changes
// bypass it; pure movement is published at most this often.
const cursorMinInterval = 50 * time.Millisecond
// cursorRefreshInterval re-sends an unchanged cursor at least this often so a
// viewer that joins while the pointer is idle still learns where it is (it
// would otherwise wait for the next mouse movement).
const cursorRefreshInterval = time.Second
// PublishCursor notifies subscribers when the device reports a new cursor
// index. Repeated identical indices are suppressed so the WS isn't spammed
// with per-frame cursor JSON. No-op for unknown devices.
func (h *Hub) PublishCursor(deviceID string, index byte) {
// position/shape. x/y are in the captured frame's coordinate space. Identical
// reports are suppressed and position-only changes are throttled, so the WS
// isn't spammed with per-frame cursor JSON; an idle cursor is refreshed once a
// second for late joiners. No-op for unknown devices.
func (h *Hub) PublishCursor(deviceID string, index byte, x, y int) {
h.mu.Lock()
d, ok := h.devices[deviceID]
if !ok {
h.mu.Unlock()
return
}
if d.cursorSeen && d.lastCursorIndex == index {
h.mu.Unlock()
return
moved := d.lastCursorX != x || d.lastCursorY != y
reshaped := !d.cursorSeen || d.lastCursorIndex != index
if !reshaped {
since := time.Since(d.cursorPubAt)
if since < cursorMinInterval || (!moved && since < cursorRefreshInterval) {
h.mu.Unlock()
return
}
}
d.cursorSeen = true
d.lastCursorIndex = index
d.lastCursorX, d.lastCursorY = x, y
d.cursorPubAt = time.Now()
h.mu.Unlock()
for _, s := range h.snapshotSubscribers() {
s.OnCursorChange(deviceID, index)
s.OnCursorChange(deviceID, index, x, y)
}
}