-
Notifications
You must be signed in to change notification settings - Fork 8
Clipboard Synchronization
Clipboard content synchronizes bidirectionally over the scrcpy 4.1 control socket, not ADB Shell. Text is transferred as raw UTF-8 so quotes, newlines, Chinese, Emoji, and command characters are never interpreted by a shell.
-
Ctrl+Vreads the local clipboard and sendsSET_CLIPBOARD(paste=true). The server pastes after setting the target clipboard; the client does not send a delayedKEYCODE_PASTE. - One basic ASCII character keeps the
INJECT_TEXTpath. Non-ASCII, newline/tab, multi-character commits, or content over 300 UTF-8 bytes uses clipboard. - Every message enters the ordered control sender. Clipboard is non-droppable, and pending MOVE values are flushed according to the ordering barrier first.
SET_CLIPBOARD wire format:
| Field | Bytes | Value |
|---|---|---|
| type | 1 | 9 |
| sequence | 8 |
-1, no ACK requested |
| paste | 1 |
1 means paste after setting |
| length | 4 | UTF-8 byte count, big-endian |
| text | length | Raw UTF-8 |
Length uses UTF-8 bytes, never String.length, and content receives no Shell, JSON, or Base64 escaping.
A separate receiver coroutine continuously reads the control socket:
-
CLIPBOARD: validate length, read exact UTF-8, and write to the local clipboard on the main thread. -
ACK_CLIPBOARD: fully consume the 8-byte sequence. -
UHID_OUTPUT: fully consume id, length, and payload so subsequent message boundaries remain aligned.
Sending and receiving cannot share one single-thread loop because blocking reads would stop control writes. Session stop cancels both directions and closes the socket; an old receiver must never read a new session.
The scrcpy control-message limit is 1 << 18 (262,144) bytes:
- The outgoing fixed header is 14 bytes, leaving 262,130 UTF-8 bytes for text.
- The incoming clipboard header is 5 bytes, leaving 262,139 UTF-8 bytes for text.
Oversized output fails explicitly instead of truncating a multibyte character. Input validates declared length before allocation. Ordinary messages use a reusable 512-byte buffer; an already encoded large clipboard byte array writes directly to the socket to avoid another large copy.
Large text occupies the ordered control socket while it is written, so later keys and touch barriers wait. Do not split it into interleavable fragments; this message has no scrcpy reassembly semantics.
- Logs record direction, UTF-8 length, and failure class only, never content.
- Content is not written to event history, cache, or persistent storage.
- Android background-clipboard restrictions and paste refusal from protected fields are platform or application security boundaries.
- An unknown device-message type or invalid length ends the current read instead of interpreting a misaligned stream.
- Never reintroduce a Shell fallback; it restores escaping, length, and command-injection risks.
-
ScrcpyClipboardProtocolTest: Chinese, special characters, Emoji/ZWJ, control characters, exact limit, and oversize rejection. -
ScrcpyDeviceMessageReaderTest: clipboard, ACK, and UHID sequences plus pre-allocation length checks. -
KeyboardClipboardRoutingTest: ASCII/clipboard routing and the 300-byteINJECT_TEXTboundary. - Device coverage: normal fields, WebView, terminal, vendor apps, large text, reverse sync, and protected fields.
On a server upgrade, recheck message types, field order, maximum length, sequence/ACK semantics, and UHID payload, updating protocol tests first.
- Overview / 总览
- Quick Start / 快速开始
- Connection & Pairing / 连接设备与配对
- Session Management / 会话管理
- Remote Control / 远程控制
- Settings / 设置
- Management Features / 管理功能
- FAQ / 常见问题
- Overview / 总览
- Technical Index / 技术文档索引
- Audit Checklist / 初始化与更新清单
- Conventions / 文档维护约定
- Architecture / 架构
- Module Map / 模块地图与边界
- Runtime Path / 运行时主链路
- Session State / 会话状态与事件
- Session Lifecycle / 会话配置与连接生命周期
- ADB, USB & Wireless / ADB、USB 与无线
- ADB Lifecycle / ADB 连接生命周期
- Pairing / 设备配对与无线调试
- Codecs / 编码与解码
- Remote Interaction / 远程交互性能与悬浮菜单
- Clipboard / 剪贴板同步
- Management / 管理功能
- Events & Shell / 事件、监控与 Shell
- Logging / 日志系统与维护
- Troubleshooting / 分层排障方法
- UI Design / UI 设计系统
- Bilingual Copy / 双语与文案
- Remote UI Analysis / 远程 UI 布局分析
- Engineering Rules / 工程与验证规则
- External Research / 外部研究与取舍
Technical topics appear here only after their page-level audit is complete. / 技术主题仅在完成逐页审核后加入侧边栏。