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

+16 -5
View File
@@ -418,13 +418,24 @@ func (h *MyHandler) handleScreenFrame(ctx *connection.Context, data []byte, alwa
}
// data[0] is the token; the 11-byte header sits at data[1..12].
const skip = 1 + protocol.ScreenFrameHeaderLen
if len(data) <= skip {
if len(data) < skip {
return // truncated header
}
// Cursor position + index live in the per-frame header: [1]=algorithm,
// [2..9]=POINT (x,y int32 LE), [10]=cursor index. Publish before the heavy
// frame work so the browser sees cursor updates even if we drop the frame.
// The position is in the captured frame's coordinate space, which is also
// the browser canvas space — the overlay can draw it as-is.
//
// The client also sends header-only packets (no bitmap change) at the frame
// rate, so this keeps a static desktop's cursor fresh for non-controlling
// viewers; those packets carry no video and are dropped right after.
cursorX := int(int32(binary.LittleEndian.Uint32(data[2:6])))
cursorY := int(int32(binary.LittleEndian.Uint32(data[6:10])))
h.hub.PublishCursor(deviceID, data[10], cursorX, cursorY)
if len(data) == skip {
return
}
// Cursor index lives at the last byte of the small per-frame header
// (offset 1 + 1 + 8 = 10). Publish before the heavy frame work so the
// browser sees cursor updates even if we end up dropping frames later.
h.hub.PublishCursor(deviceID, data[10])
video := data[skip:]
// 按首字节嗅探 H.264 / AV1,分发到对应的 keyframe 探测器。浏览器侧用同样方式
+9 -9
View File
@@ -21,15 +21,15 @@ type recordedFrame struct {
func (r *frameRecorder) OnScreenFrame(deviceID string, packet []byte, isKeyframe bool) {
r.frames = append(r.frames, recordedFrame{deviceID, packet, isKeyframe})
}
func (r *frameRecorder) OnDeviceOnline(hub.DeviceInfo) {}
func (r *frameRecorder) OnDeviceOffline(string) {}
func (r *frameRecorder) OnDeviceUpdate(string, int, string) {}
func (r *frameRecorder) OnResolutionChange(string, int, int) {}
func (r *frameRecorder) OnCursorChange(string, byte) {}
func (r *frameRecorder) OnClipboardText(string, string) {}
func (r *frameRecorder) OnTerminalReady(string, bool) {}
func (r *frameRecorder) OnTerminalData(string, []byte) {}
func (r *frameRecorder) OnTerminalClosed(string, string) {}
func (r *frameRecorder) OnDeviceOnline(hub.DeviceInfo) {}
func (r *frameRecorder) OnDeviceOffline(string) {}
func (r *frameRecorder) OnDeviceUpdate(string, int, string) {}
func (r *frameRecorder) OnResolutionChange(string, int, int) {}
func (r *frameRecorder) OnCursorChange(string, byte, int, int) {}
func (r *frameRecorder) OnClipboardText(string, string) {}
func (r *frameRecorder) OnTerminalReady(string, bool) {}
func (r *frameRecorder) OnTerminalData(string, []byte) {}
func (r *frameRecorder) OnTerminalClosed(string, string) {}
// TestOnReceiveRelaysKeyframeToken guards the forced-IDR recovery path: when
// the browser asks for a keyframe, the client replies with TOKEN_KEYFRAME.
+8 -8
View File
@@ -193,14 +193,14 @@ func (s *Store) offlineAt(id string, at time.Time) {
}
// The remaining EventHandler methods are not relevant to device history.
func (s *Store) OnDeviceUpdate(string, int, string) {}
func (s *Store) OnScreenFrame(string, []byte, bool) {}
func (s *Store) OnResolutionChange(string, int, int) {}
func (s *Store) OnCursorChange(string, byte) {}
func (s *Store) OnClipboardText(string, string) {}
func (s *Store) OnTerminalReady(string, bool) {}
func (s *Store) OnTerminalData(string, []byte) {}
func (s *Store) OnTerminalClosed(string, string) {}
func (s *Store) OnDeviceUpdate(string, int, string) {}
func (s *Store) OnScreenFrame(string, []byte, bool) {}
func (s *Store) OnResolutionChange(string, int, int) {}
func (s *Store) OnCursorChange(string, byte, int, int) {}
func (s *Store) OnClipboardText(string, string) {}
func (s *Store) OnTerminalReady(string, bool) {}
func (s *Store) OnTerminalData(string, []byte) {}
func (s *Store) OnTerminalClosed(string, string) {}
// Offline returns the records whose IDs are not in online, newest last_seen
// first, converted to the hub's device payload shape with Online=false.
+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)
}
}
+75 -1
View File
@@ -58,6 +58,7 @@ type captureHandler struct {
online []string
offline []string
updates []string // formatted "id:rtt"
cursors [][3]int // recorded {index, x, y}
}
func (c *captureHandler) OnDeviceOnline(d DeviceInfo) {
@@ -82,7 +83,11 @@ func (c *captureHandler) OnScreenFrame(_ string, _ []byte, _ bool) {}
func (c *captureHandler) OnResolutionChange(_ string, _, _ int) {}
func (c *captureHandler) OnCursorChange(_ string, _ byte) {}
func (c *captureHandler) OnCursorChange(_ string, index byte, x, y int) {
c.mu.Lock()
c.cursors = append(c.cursors, [3]int{int(index), x, y})
c.mu.Unlock()
}
func (c *captureHandler) OnClipboardText(_ string, _ string) {}
@@ -225,3 +230,72 @@ func TestHubConcurrent(t *testing.T) {
t.Fatalf("after all unregisters: want 0, got %d", got)
}
}
// The web console draws the remote cursor itself while the user is not in
// control mode, so the hub must deliver the position reliably but without
// relaying every ~30 Hz frame to every viewer.
func TestPublishCursorDedupAndThrottle(t *testing.T) {
h := New()
c := &captureHandler{}
h.Subscribe(c)
h.Register(&Device{ID: "d", Name: "d"}, stubCtx())
// First report always goes out, even for the zero value.
h.PublishCursor("d", 1, 10, 20)
if len(c.cursors) != 1 || c.cursors[0] != [3]int{1, 10, 20} {
t.Fatalf("first publish: %+v", c.cursors)
}
// Identical report: suppressed.
h.PublishCursor("d", 1, 10, 20)
if len(c.cursors) != 1 {
t.Fatalf("identical report should be dropped: %+v", c.cursors)
}
// Position-only change inside the throttle window: suppressed.
h.PublishCursor("d", 1, 11, 21)
if len(c.cursors) != 1 {
t.Fatalf("position-only change should be throttled: %+v", c.cursors)
}
// Shape change bypasses the throttle.
h.PublishCursor("d", 5, 12, 22)
if len(c.cursors) != 2 || c.cursors[1] != [3]int{5, 12, 22} {
t.Fatalf("shape change should be immediate: %+v", c.cursors)
}
// After the window a position-only change is delivered again.
time.Sleep(cursorMinInterval + 10*time.Millisecond)
h.PublishCursor("d", 5, 13, 23)
if len(c.cursors) != 3 || c.cursors[2] != [3]int{5, 13, 23} {
t.Fatalf("post-window move: %+v", c.cursors)
}
// Unknown device: no event, no panic.
h.PublishCursor("ghost", 1, 0, 0)
if len(c.cursors) != 3 {
t.Fatalf("unknown device should not publish: %+v", c.cursors)
}
}
// A viewer joining while the pointer is idle must still learn its position:
// the hub re-sends an unchanged cursor once the refresh interval passes.
func TestPublishCursorRefreshesIdleState(t *testing.T) {
h := New()
c := &captureHandler{}
h.Subscribe(c)
h.Register(&Device{ID: "d", Name: "d"}, stubCtx())
h.PublishCursor("d", 3, 100, 200)
time.Sleep(cursorRefreshInterval / 2)
h.PublishCursor("d", 3, 100, 200) // inside the refresh window: dropped
if len(c.cursors) != 1 {
t.Fatalf("duplicate inside refresh window should be dropped: %+v", c.cursors)
}
time.Sleep(cursorRefreshInterval/2 + 50*time.Millisecond)
h.PublishCursor("d", 3, 100, 200) // stale: re-published for late joiners
if len(c.cursors) != 2 || c.cursors[1] != [3]int{3, 100, 200} {
t.Fatalf("idle refresh missing: %+v", c.cursors)
}
}
+8 -5
View File
@@ -351,15 +351,18 @@ func (h *wsHub) OnDeviceOffline(id string) {
}
}
// OnCursorChange relays the remote cursor index to every viewer of this
// device. The browser maps the index to a CSS cursor (desktop) or overlay
// SVG variant (touch). Hub already de-duplicates so we always have a real
// transition here.
func (h *wsHub) OnCursorChange(deviceID string, index byte) {
// OnCursorChange relays the remote cursor's shape and position to every viewer
// of this device. The browser draws the cursor itself while it is not in
// control mode (captured frames never contain one) and maps the index to a CSS
// cursor on desktop while in control mode. x/y are in frame/canvas space.
// Hub already de-duplicates and throttles, so we always have a real transition.
func (h *wsHub) OnCursorChange(deviceID string, index byte, x, y int) {
msg := mustJSON(map[string]any{
"cmd": "cursor",
"id": deviceID,
"index": index,
"x": x,
"y": y,
})
h.mu.RLock()
defer h.mu.RUnlock()
+58
View File
@@ -0,0 +1,58 @@
package web
import (
"encoding/json"
"testing"
)
// The non-controlling browser draws the remote cursor itself, so the relayed
// "cursor" message must carry the position, not just the shape index. Without
// x/y an older page still works (it ignores unknown fields), but the new
// overlay has nothing to place.
func TestOnCursorChangeCarriesPosition(t *testing.T) {
c := &wsClient{
send: make(chan wsMsg, 4),
closed: make(chan struct{}),
token: "tok",
watchIDs: map[string]struct{}{"42": {}},
}
h := &wsHub{clients: map[*wsClient]struct{}{c: {}}}
h.OnCursorChange("42", 5, 1280, 720)
var msg struct {
Cmd string `json:"cmd"`
ID string `json:"id"`
Index int `json:"index"`
X *int `json:"x"`
Y *int `json:"y"`
}
m := <-c.send
if m.binary {
t.Fatal("cursor message must be a text frame")
}
if err := json.Unmarshal(m.data, &msg); err != nil {
t.Fatalf("unmarshal %s: %v", m.data, err)
}
if msg.Cmd != "cursor" || msg.ID != "42" || msg.Index != 5 {
t.Fatalf("message = %+v", msg)
}
if msg.X == nil || msg.Y == nil || *msg.X != 1280 || *msg.Y != 720 {
t.Fatalf("position missing: x=%v y=%v (raw %s)", msg.X, msg.Y, m.data)
}
// A viewer not watching the device must not receive anything.
other := &wsClient{
send: make(chan wsMsg, 1),
closed: make(chan struct{}),
token: "tok",
watchIDs: map[string]struct{}{"7": {}},
}
h.clients[other] = struct{}{}
h.OnCursorChange("42", 5, 1, 1)
select {
case <-other.send:
t.Fatal("non-watching client received a cursor message")
default:
}
}