diff --git a/server/2015Remote/ScreenSpyDlg.cpp b/server/2015Remote/ScreenSpyDlg.cpp index f98e08b..203e6d4 100644 --- a/server/2015Remote/ScreenSpyDlg.cpp +++ b/server/2015Remote/ScreenSpyDlg.cpp @@ -1415,6 +1415,31 @@ VOID CScreenSpyDlg::DrawFirstScreen(void) PostMessage(WM_PAINT);//触发WM_PAINT消息 } +// 把远程光标的位置/类型同步给 Web 客户端。抓屏画面本身不含光标(GDI/DXGI 都不合成), +// 非控制态的浏览器靠这条消息在画面上自己画一个光标。 +// 类型变化 → 立即发(force=true); +// 位置变化 → 50ms 节流; +// 完全静止 → 每秒重发一次,让刚打开页面的观察者也能立刻看到光标位置 +// (否则要等远程鼠标下一次移动)。 +void CScreenSpyDlg::BroadcastCursorToWeb(bool force) +{ + if (!m_bIsWebSession || !WebService().IsRunning()) return; + ULONGLONG now = GetTickCount64(); + ULONGLONG since = now - m_nLastCursorBroadcast; + bool reshaped = force || m_bCursorIndex != m_bBroadcastCursorIndex; + bool moved = m_ClientCursorPos.x != m_ptBroadcastCursor.x || + m_ClientCursorPos.y != m_ptBroadcastCursor.y; + if (!reshaped) { + if (since < 50) return; // 位置变化节流 + if (!moved && since < 1000) return; // 静止时每秒兜底一次 + } + m_nLastCursorBroadcast = now; + m_bBroadcastCursorIndex = m_bCursorIndex; + m_ptBroadcastCursor = m_ClientCursorPos; + WebService().BroadcastCursor(m_ClientID, m_bCursorIndex, + (int)m_ClientCursorPos.x, (int)m_ClientCursorPos.y); +} + VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) { //该函数不是直接画到屏幕上,而是更新一下变化部分的屏幕数据然后调用 @@ -1452,10 +1477,6 @@ VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) m_bCursorIndex = m_ContextObject->InDeCompressedBuffer.GetBuffer(2+sizeof(POINT))[0]; if (bOldCursorIndex != m_bCursorIndex) { bChange = TRUE; - // 通知 Web 客户端光标变化 (只有 Web 会话的对话框才广播) - if (m_bIsWebSession && WebService().IsRunning()) { - WebService().BroadcastCursor(m_ClientID, m_bCursorIndex); - } if (m_bIsCtrl && !m_bIsTraceCursor) {//替换指定窗口所属类的WNDCLASSEX结构 HCURSOR cursor; if (m_bCursorIndex == 254) { // -2: 使用自定义光标 @@ -1473,6 +1494,8 @@ VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) #endif } } + // 通知 Web 客户端光标位置/类型(内部按类型变化/位置节流/每秒兜底决定是否真发) + BroadcastCursorToWeb(false); // 屏幕是否变化 if (NextScreenLength > 0) { @@ -1638,11 +1661,9 @@ VOID CScreenSpyDlg::DrawScrollFrame() m_bCursorIndex = m_ContextObject->InDeCompressedBuffer.GetBuffer(2 + sizeof(POINT))[0]; if (bOldCursorIndex != m_bCursorIndex) { bChange = TRUE; - // 通知 Web 客户端光标变化 (只有 Web 会话的对话框才广播) - if (m_bIsWebSession && WebService().IsRunning()) { - WebService().BroadcastCursor(m_ClientID, m_bCursorIndex); - } } + // 通知 Web 客户端光标位置/类型(内部按类型变化/位置节流/每秒兜底决定是否真发) + BroadcastCursorToWeb(false); // 读取滚动参数 LPBYTE buffer = m_ContextObject->InDeCompressedBuffer.GetBuffer(ulHeadLength); diff --git a/server/2015Remote/ScreenSpyDlg.h b/server/2015Remote/ScreenSpyDlg.h index 9e75470..0511ddc 100644 --- a/server/2015Remote/ScreenSpyDlg.h +++ b/server/2015Remote/ScreenSpyDlg.h @@ -218,6 +218,10 @@ public: bool IsWebSession() const { return m_bIsWebSession.load(); } void SetWebSession(bool isWeb) { m_bIsWebSession.store(isWeb); } + // 把远程光标的位置/类型同步给 Web 客户端(非控制态由浏览器自己画光标)。 + // force=true 用于类型变化,跳过节流立即发送。 + void BroadcastCursorToWeb(bool force); + VOID SendNext(void); VOID OnReceiveComplete(); HDC m_hFullDC; @@ -264,6 +268,9 @@ public: std::atomic m_bIsWebSession{false}; // True if this dialog was created by Web request (atomic for thread safety) std::string m_strSaveNotice; // 截图保存路径提示 ULONGLONG m_nSaveNoticeTime = 0; // 截图提示开始时间 + ULONGLONG m_nLastCursorBroadcast = 0; // 上次向 Web 广播光标的时间(50ms 节流) + BYTE m_bBroadcastCursorIndex = 0; // 上次广播的光标类型 + POINT m_ptBroadcastCursor = {0, 0}; // 上次广播的光标位置 BOOL m_bUsingFRP = FALSE; // 文件接收进度对话框(用于 Linux Ctrl+C -> 服务端 Ctrl+V) diff --git a/server/2015Remote/WebService.cpp b/server/2015Remote/WebService.cpp index ae022e9..151ee4d 100644 --- a/server/2015Remote/WebService.cpp +++ b/server/2015Remote/WebService.cpp @@ -2344,7 +2344,7 @@ void CWebService::NotifyAudioState(uint64_t device_id, bool enabled) { } } -void CWebService::BroadcastCursor(uint64_t device_id, uint8_t cursor_index) { +void CWebService::BroadcastCursor(uint64_t device_id, uint8_t cursor_index, int x, int y) { if (m_bStopping) return; // Build JSON message @@ -2352,6 +2352,9 @@ void CWebService::BroadcastCursor(uint64_t device_id, uint8_t cursor_index) { res["cmd"] = "cursor"; res["id"] = std::to_string(device_id); res["index"] = cursor_index; + // 位置是发送位图(即浏览器 canvas)坐标系;老页面忽略这两个字段。 + res["x"] = x; + res["y"] = y; Json::StreamWriterBuilder builder; builder["indentation"] = ""; diff --git a/server/2015Remote/WebService.h b/server/2015Remote/WebService.h index b9a9f7a..01c6cbb 100644 --- a/server/2015Remote/WebService.h +++ b/server/2015Remote/WebService.h @@ -167,8 +167,11 @@ public: // clients watching this device and caches it for newcomers. void NotifyAudioState(uint64_t device_id, bool enabled); - // Cursor change notification (called from ScreenSpyDlg) - void BroadcastCursor(uint64_t device_id, uint8_t cursor_index); + // Cursor notification (called from ScreenSpyDlg): the shape index plus the + // position in the transmitted frame's coordinate space. The browser draws + // the remote cursor itself while it is not in control mode — the captured + // frames never contain one. + void BroadcastCursor(uint64_t device_id, uint8_t cursor_index, int x, int y); // 远程剪贴板文本回传通知(called from ScreenSpyDlg,payload 为 UTF-8) void NotifyClipboardText(uint64_t device_id, const std::string& text); diff --git a/server/go/cmd/main.go b/server/go/cmd/main.go index 39fd0e7..ac23f72 100644 --- a/server/go/cmd/main.go +++ b/server/go/cmd/main.go @@ -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 探测器。浏览器侧用同样方式 diff --git a/server/go/cmd/screen_keyframe_test.go b/server/go/cmd/screen_keyframe_test.go index d5a7f44..36fe08e 100644 --- a/server/go/cmd/screen_keyframe_test.go +++ b/server/go/cmd/screen_keyframe_test.go @@ -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. diff --git a/server/go/devicehistory/devicehistory.go b/server/go/devicehistory/devicehistory.go index f612808..745103b 100644 --- a/server/go/devicehistory/devicehistory.go +++ b/server/go/devicehistory/devicehistory.go @@ -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. diff --git a/server/go/hub/hub.go b/server/go/hub/hub.go index c9f3d0a..fafa2fe 100644 --- a/server/go/hub/hub.go +++ b/server/go/hub/hub.go @@ -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) } } diff --git a/server/go/hub/hub_test.go b/server/go/hub/hub_test.go index 56a6590..eec3945 100644 --- a/server/go/hub/hub_test.go +++ b/server/go/hub/hub_test.go @@ -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) + } +} diff --git a/server/go/web/ws.go b/server/go/web/ws.go index 6d7737b..2521369 100644 --- a/server/go/web/ws.go +++ b/server/go/web/ws.go @@ -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() diff --git a/server/go/web/ws_cursor_test.go b/server/go/web/ws_cursor_test.go new file mode 100644 index 0000000..1ba0033 --- /dev/null +++ b/server/go/web/ws_cursor_test.go @@ -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: + } +} diff --git a/server/web/index.html b/server/web/index.html index c166f45..de63aac 100644 --- a/server/web/index.html +++ b/server/web/index.html @@ -2648,8 +2648,12 @@ isAndroidRemote = msg.client_type === 'APK'; updateAndroidNavbar(); // Refresh cursor overlay and immersive layout - document.getElementById('cursor-overlay').classList.toggle( - 'active', controlEnabled && isTouchDevice && !isAndroidRemote); + if (controlEnabled) { + document.getElementById('cursor-overlay').classList.toggle( + 'active', isTouchDevice && !isAndroidRemote); + } else { + renderRemoteCursor(); + } if (isTouchDevice) updateUIForOrientation(); } initDecoder(msg.width, msg.height); @@ -2662,8 +2666,18 @@ // Update remote cursor visual: native CSS cursor on desktop, // overlay variant on touch. 254=custom (unsupported), 255=unsupported. currentCursorIndex = msg.index; + remoteCursor.index = msg.index; + // 服务端每帧带上光标位置(帧坐标 == canvas 坐标)。老版本服务端 + // 只发 index,此时 known 保持 false,退回原来的表现。 + if (typeof msg.x === 'number' && typeof msg.y === 'number') { + remoteCursor.x = msg.x; + remoteCursor.y = msg.y; + remoteCursor.known = true; + } if (controlEnabled) { applyRemoteCursor(msg.index); + } else { + renderRemoteCursor(); } break; } @@ -2778,6 +2792,8 @@ // Update input shortcuts position after canvas resize requestAnimationFrame(updateInputShortcutsPosition); + // 分辨率变了:非控制态的远程光标按新画布重新定位 + requestAnimationFrame(renderRemoteCursor); // Windows/Linux/macOS clients send bottom-up H.264 (BMP convention) → flip. // Android MediaCodec sends top-down H.264 → no flip needed. @@ -5193,6 +5209,11 @@ ]; let currentCursorIndex = 1; // Default: arrow + // 远程真实光标位置(服务端随每帧回报)。抓屏画面本身不含光标,所以非控制态 + // 由浏览器自己画一个。known=false 表示服务端还没上报过位置(老版本服务端), + // 这时退回浏览器自己的指针,保持原有观感。 + let remoteCursor = { x: 0, y: 0, index: 1, known: false }; + // IDC_* index -> CSS class on .cursor-overlay (touch-mode visual cursor). // Indices not listed (0,2,4,6,8,14, custom) fall back to default arrow. const cursorOverlayClassMap = { @@ -5226,6 +5247,28 @@ } } + // 非控制态的光标渲染:把远程光标画在它真正所在的位置上,同时藏起浏览器 + // 自己的箭头,否则屏幕上会同时出现两个指针。控制态不走这里——桌面端本地 + // 指针本身就是远程光标,触摸端由 updateCursorOverlay 画触控光标。 + function renderRemoteCursor() { + if (controlEnabled) return; + const canvas = document.getElementById('screen-canvas'); + const overlay = document.getElementById('cursor-overlay'); + if (!canvas || !overlay) return; + // 屏幕墙(以及没打开远程桌面时)不画:多路光标会落在同一个固定层上。 + const screenPage = document.getElementById('screen-page'); + const onScreen = !!screenPage && screenPage.classList.contains('active'); + // Android 直连模式没有指针概念(手指即指针),不画。 + const show = onScreen && remoteCursor.known && !isAndroidRemote; + canvas.style.cursor = show ? 'none' : 'default'; + overlay.className = 'cursor-overlay' + (show ? ' active' : ''); + const cls = cursorOverlayClassMap[remoteCursor.index]; + if (show) { + if (cls) overlay.classList.add(cls); + positionCursorOverlay(remoteCursor.x, remoteCursor.y); + } + } + // Floating toolbar state let toolbarVisible = false; let toolbarHideTimer = null; @@ -5481,17 +5524,18 @@ // Cursor handling const canvas = document.getElementById('screen-canvas'); const cursorOverlay = document.getElementById('cursor-overlay'); - // Touch: hide native cursor and show our overlay (touchpad mode). - // Desktop: native CSS cursor is set by applyRemoteCursor below. - if (controlEnabled && isTouchDevice) { - canvas.style.cursor = 'none'; - } else if (!controlEnabled) { - canvas.style.cursor = 'default'; - } - // Android direct mode: no cursor overlay (finger IS the pointer) - cursorOverlay.classList.toggle('active', controlEnabled && isTouchDevice && !isAndroidRemote); if (controlEnabled) { + // Touch: hide native cursor and show our overlay (touchpad mode). + // Desktop: native CSS cursor is set by applyRemoteCursor below. + if (isTouchDevice) { + canvas.style.cursor = 'none'; + } + // Android direct mode: no cursor overlay (finger IS the pointer) + cursorOverlay.classList.toggle('active', isTouchDevice && !isAndroidRemote); applyRemoteCursor(currentCursorIndex); + } else { + // 非控制态:藏起本地指针,只留远程真实光标 + renderRemoteCursor(); } // Sync the keyboard lock: in fullscreen + control mode, ESC and F11 // must be passed through to the remote OS instead of triggering the @@ -5505,9 +5549,16 @@ // Only used on touch devices (touchpad mode) function updateCursorOverlay(canvasX, canvasY) { if (!controlEnabled || !isTouchDevice) return; + positionCursorOverlay(canvasX, canvasY); + } + + // Place the overlay at a canvas coordinate, applying the zoom/pan + // transform. Shared by the touch control cursor and the non-control + // remote cursor. + function positionCursorOverlay(canvasX, canvasY) { const canvas = document.getElementById('screen-canvas'); const cursorOverlay = document.getElementById('cursor-overlay'); - + if (!canvas || !cursorOverlay) return; // Get canvas base position (without transform, use container + offset) // offsetLeft/offsetTop are layout positions, unaffected by CSS transform const container = document.querySelector('.canvas-container'); @@ -5679,6 +5730,8 @@ canvas.style.transformOrigin = zoomState.originX + '% ' + zoomState.originY + '%'; canvas.style.transform = 'scale(' + zoomState.scale + ') translate(' + zoomState.translateX + 'px, ' + zoomState.translateY + 'px)'; } + // 非控制态:缩放/平移后远程光标要跟着画面走 + if (!controlEnabled) renderRemoteCursor(); } function getPinchDistance(touches) { @@ -6634,6 +6687,7 @@ // Strip any cursor-* variant on the overlay, leaving the bare default arrow. document.getElementById('cursor-overlay').className = 'cursor-overlay'; currentCursorIndex = 1; // Reset to default arrow + remoteCursor = { x: 0, y: 0, index: 1, known: false }; // 切设备后位置作废 // Reset zoom state zoomState.scale = 1;