feat(receiver): 授权开关与按需投屏,新增 LocalSend 风格文件服务

- 新增 Prefs 持久化 + BootReceiver 开机自启,主开关打开后进入应用/开机自动准备待命

- Service 拆为待命/投屏两阶段:待命只维持广播与控制通道,不建编码器、不创建 VirtualDisplay

- 新增控制帧 msgType=8 Mirror:主控端请求投屏,被控端弹窗确认并校验屏幕录制权限后才开流

- 新增 MirrorRequestActivity 透明确认页(通知兜底 + 30s 超时自动拒绝)

- 投屏按本机真实分辨率(不强制横屏)适配,旋转/分辨率变化自动重建采集

- 新增 LocalSend 风格 HTTP 文件服务(53317,含 info/browse/download/upload/mirror),目录做根路径越权校验,主界面提供存储与通知权限申请

- 控制通道 msgType=7 增加 op=list 目录浏览;Rust/Dart 同步支持投屏请求并加工具栏按钮
This commit is contained in:
tongtong committed 2026-11-27 09:07:31 +08:00
1 parent 3feddb3434
commit f87ef59d98
23 files changed
+1840 -274

No files matched your search

+24 -3
View File
@@ -1,8 +1,8 @@
use super::codec::{CodecError, ControlFrameCodec, ControlFrameHeader};
use super::events::{
ClipboardEvent, ControlEvent, ControlEventKind, KeyAction, KeyboardEvent, MouseAction,
MouseEvent, TouchAction, TouchEvent, WheelEvent, MSG_CLIPBOARD, MSG_KEY, MSG_MOUSE, MSG_PING,
MSG_PONG, MSG_TOUCH, MSG_WHEEL, PROTOCOL_VERSION,
ClipboardEvent, ControlEvent, ControlEventKind, KeyAction, KeyboardEvent, MirrorEvent,
MouseAction, MouseEvent, TouchAction, TouchEvent, WheelEvent, MSG_CLIPBOARD, MSG_KEY,
MSG_MIRROR, MSG_MOUSE, MSG_PING, MSG_PONG, MSG_TOUCH, MSG_WHEEL, PROTOCOL_VERSION,
};
#[derive(Debug, Default, Clone, Copy)]
@@ -51,6 +51,10 @@ impl ControlFrameCodec for BinaryCodec {
ControlEventKind::Ping(timestamp) | ControlEventKind::Pong(timestamp) => {
buffer.extend_from_slice(&timestamp.to_le_bytes());
}
ControlEventKind::Mirror(payload) => {
buffer.push(payload.action);
buffer.extend_from_slice(payload.message.as_bytes());
}
}
let payload_len = (buffer.len() - ControlFrameHeader::LEN) as u32;
@@ -149,6 +153,15 @@ fn decode_payload(msg_type: u8, payload: &[u8]) -> Result<ControlEventKind, Code
Ok(ControlEventKind::Pong(timestamp))
}
}
MSG_MIRROR => {
if payload.is_empty() {
return Err(CodecError::InvalidPayload("投屏动作负载至少 1 字节"));
}
let action = payload[0];
let message = String::from_utf8(payload[1..].to_vec())
.map_err(|_| CodecError::InvalidUtf8)?;
Ok(ControlEventKind::Mirror(MirrorEvent { action, message }))
}
other => Err(CodecError::UnknownMsgType(other)),
}
}
@@ -198,6 +211,14 @@ mod tests {
}));
assert_roundtrip(ControlEventKind::Ping(1_700_000_000_000));
assert_roundtrip(ControlEventKind::Pong(1_700_000_000_001));
assert_roundtrip(ControlEventKind::Mirror(MirrorEvent {
action: 0,
message: String::new(),
}));
assert_roundtrip(ControlEventKind::Mirror(MirrorEvent {
action: 5,
message: "无屏幕录制权限".to_string(),
}));
}
#[test]
+22
View File
@@ -8,6 +8,19 @@ pub const MSG_CLIPBOARD: u8 = 4;
pub const MSG_PING: u8 = 5;
pub const MSG_PONG: u8 = 6;
/// 投屏请求/应答。payload = `[action:u8]` + 可选 UTF-8 文本。
pub const MSG_MIRROR: u8 = 8;
/// 投屏控制动作,两端必须保持一致。
pub mod mirror_action {
pub const REQUEST: u8 = 0;
pub const STOP: u8 = 1;
pub const GRANTED: u8 = 2;
pub const DENIED: u8 = 3;
pub const STOPPED: u8 = 4;
pub const ERROR: u8 = 5;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TouchAction {
Down,
@@ -138,6 +151,13 @@ pub struct ClipboardEvent {
pub text: String,
}
/// 投屏请求/应答负载。`message` 用于 DENIED / ERROR 的原因说明。
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MirrorEvent {
pub action: u8,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ControlEventKind {
Touch(TouchEvent),
@@ -147,6 +167,7 @@ pub enum ControlEventKind {
Clipboard(ClipboardEvent),
Ping(u64),
Pong(u64),
Mirror(MirrorEvent),
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -173,6 +194,7 @@ impl ControlEvent {
ControlEventKind::Clipboard(_) => MSG_CLIPBOARD,
ControlEventKind::Ping(_) => MSG_PING,
ControlEventKind::Pong(_) => MSG_PONG,
ControlEventKind::Mirror(_) => MSG_MIRROR,
}
}
}
+7 -2
View File
@@ -14,8 +14,8 @@ use std::thread::{self, JoinHandle};
use std::time::Duration;
use crate::control::events::{
ClipboardEvent, KeyAction, KeyboardEvent, MouseAction, MouseEvent, TouchAction, TouchEvent,
WheelEvent,
ClipboardEvent, KeyAction, KeyboardEvent, MirrorEvent, MouseAction, MouseEvent, TouchAction,
TouchEvent, WheelEvent,
};
use crate::control::{ControlEvent, ControlEventKind};
@@ -284,6 +284,11 @@ pub fn ping_event(sequence: u16, timestamp: u64) -> ControlEvent {
ControlEvent::new(sequence, ControlEventKind::Ping(timestamp))
}
/// 构造投屏请求/停止事件(主控端 → 被控端)。
pub fn mirror_event(sequence: u16, action: u8, message: String) -> ControlEvent {
ControlEvent::new(sequence, ControlEventKind::Mirror(MirrorEvent { action, message }))
}
fn run_tcp(listener: TcpListener, shared: Arc<Shared>) {
while shared.running.load(Ordering::Relaxed) {
match listener.accept() {
+7
View File
@@ -300,6 +300,7 @@ pub unsafe extern "C" fn ttstd_control_decode(
ControlEventKind::Key(_) => "key",
ControlEventKind::Wheel(_) => "wheel",
ControlEventKind::Clipboard(_) => "clipboard",
ControlEventKind::Mirror(_) => "mirror",
};
let json = format!(
"{{\"sequence\":{},\"msgType\":{},\"kind\":\"{}\"}}",
@@ -358,6 +359,12 @@ pub unsafe extern "C" fn ttstd_demo_send_control(
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
]),
)),
// 8 = 投屏请求/停止:[action:u8] + 可选 UTF-8 说明
8 if !bytes.is_empty() => Some(crate::demo_server::mirror_event(
sequence,
bytes[0],
String::from_utf8_lossy(&bytes[1..]).to_string(),
)),
_ => None,
};
match event {