From 85e53f64ca5c617f7de996d4f24088ff6da691da Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Fri, 27 Nov 2026 17:58:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(sender):=20=E4=B8=B2=E6=B5=81=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E8=A1=A5=E9=BD=90=E9=94=AE=E7=9B=98/=E5=8F=B3?= =?UTF-8?q?=E9=94=AE/=E6=BB=9A=E8=BD=AE=E4=B8=8E=E5=8F=8C=E5=90=91?= =?UTF-8?q?=E5=89=AA=E8=B4=B4=E6=9D=BF=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - video_window 处理右/中键(WM_RBUTTON/MBUTTON)与滚轮(WM_MOUSEWHEEL), 并通过 ttstd/video 的 mouse / wheel 方法通道交给 Dart - 键盘走 WM_KEYDOWN/UP 报 Windows 虚拟键码,Dart 侧映射成 Android keycode; Esc 仍保留为关窗,Alt 组合键旁路转发不吞掉 - 右键→返回、中键→多任务(buttons 位 bit1/bit2),滚轮按「deltaY 正=向下滚」取反 - 本机剪贴板每 700ms 轮询比较后下发 msg_type=4,被控端上行内容写入本机剪贴板; 两端都记住最近一次同步的文本,避免来回回送 - 控制栏「发送剪贴板」改为读取本机剪贴板实时内容(原先发的是固定占位字符串) --- flutter_sender/lib/core/bridge.dart | 32 ++++ .../lib/features/control/control_toolbar.dart | 6 +- flutter_sender/lib/main.dart | 143 ++++++++++++++++++ .../windows/runner/video_texture.cpp | 49 ++++++ .../windows/runner/video_window.cpp | 93 ++++++++++++ flutter_sender/windows/runner/video_window.h | 24 +++ 6 files changed, 345 insertions(+), 2 deletions(-) diff --git a/flutter_sender/lib/core/bridge.dart b/flutter_sender/lib/core/bridge.dart index c000b6e..7b5aab3 100644 --- a/flutter_sender/lib/core/bridge.dart +++ b/flutter_sender/lib/core/bridge.dart @@ -37,6 +37,15 @@ class SenderBridge { /// x/y 是**画面像素坐标**(编码分辨率),由被控端换算成真实屏幕坐标。 void Function(int action, int x, int y)? onVideoPointer; + /// 串流窗口里的右键/中键:(button, action, x, y),button 1=右键 2=中键。 + void Function(int button, int action, int x, int y)? onVideoMouseButton; + + /// 串流窗口里的滚轮:(deltaX, deltaY),正数=向上/向右。 + void Function(int deltaX, int deltaY)? onVideoWheel; + + /// 串流窗口里的键盘:(action, vk, modifiers),vk 是 Windows 虚拟键码。 + void Function(int action, int vk, int modifiers)? onVideoKey; + /// 取一条被控端上行的控制事件(投屏应答 / 剪贴板),没有则返回 null。 /// /// 事件形如 `{"type":8,"action":2,"text":""}`,`type` 是 msgType。 @@ -54,6 +63,29 @@ class SenderBridge { final y = (args['y'] as num?)?.toInt(); if (action == null || x == null || y == null) return; onVideoPointer?.call(action, x, y); + case 'mouse': + final args = call.arguments; + if (args is! Map) return; + final button = (args['button'] as num?)?.toInt(); + final action = (args['action'] as num?)?.toInt(); + final x = (args['x'] as num?)?.toInt(); + final y = (args['y'] as num?)?.toInt(); + if (button == null || action == null || x == null || y == null) return; + onVideoMouseButton?.call(button, action, x, y); + case 'wheel': + final args = call.arguments; + if (args is! Map) return; + onVideoWheel?.call( + (args['deltaX'] as num?)?.toInt() ?? 0, + (args['deltaY'] as num?)?.toInt() ?? 0, + ); + case 'key': + final args = call.arguments; + if (args is! Map) return; + final action = (args['action'] as num?)?.toInt(); + final vk = (args['vk'] as num?)?.toInt(); + if (action == null || vk == null) return; + onVideoKey?.call(action, vk, (args['modifiers'] as num?)?.toInt() ?? 0); } } diff --git a/flutter_sender/lib/features/control/control_toolbar.dart b/flutter_sender/lib/features/control/control_toolbar.dart index 3cb4f23..0703dde 100644 --- a/flutter_sender/lib/features/control/control_toolbar.dart +++ b/flutter_sender/lib/features/control/control_toolbar.dart @@ -56,14 +56,16 @@ class ControlToolbar extends StatelessWidget { _Action( icon: Icons.mouse, label: '滚轮向下', + // 协议约定 deltaY 正数 = 向下滚(与真实滚轮事件一致)。 enabled: enabled, - onPressed: () => onEvent('wheel', {'deltaX': 0, 'deltaY': -1, 'buttons': 0}), + onPressed: () => onEvent('wheel', {'deltaX': 0, 'deltaY': 1, 'buttons': 0}), ), _Action( icon: Icons.content_paste, label: '发送剪贴板', enabled: enabled, - onPressed: () => onEvent('clipboard', {'text': 'TTSTD Connect'}), + // 不带文本:由调用方读本机剪贴板实时内容再下发。 + onPressed: () => onEvent('clipboard', const {}), ), ], ), diff --git a/flutter_sender/lib/main.dart b/flutter_sender/lib/main.dart index 7e236c7..6e1f2f5 100644 --- a/flutter_sender/lib/main.dart +++ b/flutter_sender/lib/main.dart @@ -52,6 +52,7 @@ class _HomePageState extends State { Timer? _discoveryTimer; Timer? _videoStatsTimer; Timer? _eventTimer; + Timer? _clipboardTimer; List _devices = const []; DeviceInfo? _selected; @@ -73,6 +74,8 @@ class _HomePageState extends State { DateTime? _lastFrameAt; /// 因关窗而停流只下发一次 stopMirror。 bool _stopSentForWindowClose = false; + /// 最近一次同步过的剪贴板文本(收/发都记),用来避免两端来回回送同一条内容。 + String? _lastClipboardText; static const Duration streamingIdleTimeout = Duration(seconds: 2); String? _nativeError; @@ -90,6 +93,10 @@ class _HomePageState extends State { _bridge.onVideoWindowClosed = _onStreamWindowClosed; // 串流窗口里的鼠标点击/拖拽 → 触摸控制帧。 _bridge.onVideoPointer = _onVideoPointer; + // 右键/中键、滚轮、键盘 → 对应的控制帧。 + _bridge.onVideoMouseButton = _onVideoMouseButton; + _bridge.onVideoWheel = _onVideoWheel; + _bridge.onVideoKey = _onVideoKey; _diagnosticLog.start(); _diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}'); _bridge.watchPhase().listen((phase) { @@ -120,6 +127,11 @@ class _HomePageState extends State { const Duration(milliseconds: 250), (_) => _drainInboundEvents(), ); + // 本机剪贴板 → 被控端:Windows 侧没有可用的剪贴板变更通知,只能轮询比较。 + _clipboardTimer = Timer.periodic( + const Duration(milliseconds: 700), + (_) => _syncLocalClipboard(), + ); _refreshVideoTextureId(); // 自测/演示用开关:TTSTD_DEMO_AUTOSTART=1 时启动即开启本机监听。 if (Platform.environment['TTSTD_DEMO_AUTOSTART'] == '1') { @@ -136,6 +148,7 @@ class _HomePageState extends State { _discoveryTimer?.cancel(); _videoStatsTimer?.cancel(); _eventTimer?.cancel(); + _clipboardTimer?.cancel(); _diagnosticLog.dispose(); _bridge.stopDemoServer(); _bridge.stopLocalSignaling(); @@ -232,10 +245,50 @@ class _HomePageState extends State { /// 被控端上行的剪贴板内容 → 写入本机剪贴板。 Future _applyRemoteClipboard(String text) async { if (text.isEmpty || !_settings.clipboardSync) return; + // 先记住这条内容,否则本机剪贴板轮询会把它当成「本机新复制」再发回去。 + _lastClipboardText = text; await Clipboard.setData(ClipboardData(text: text)); _diagnosticLog.write('clipboard from peer applied (${text.length} chars)'); } + /// 本机剪贴板变化 → 下发被控端。 + /// + /// Windows 没有可订阅的剪贴板变更事件,只能定时读一次再和上次内容比较; + /// 内容与 [_lastClipboardText] 相同说明是刚同步过来的,不再回送。 + Future _syncLocalClipboard() async { + if (!mounted || _nativeCore == null) return; + if (!_phase.isConnected || !_settings.clipboardSync) return; + final ClipboardData? data; + try { + data = await Clipboard.getData(Clipboard.kTextPlain); + } on Object { + // 剪贴板被别的程序占用(Office 等的延迟渲染)时读会抛错,下一轮再试。 + return; + } + final text = data?.text; + if (text == null || text.isEmpty || text == _lastClipboardText) return; + _lastClipboardText = text; + _bridge.sendControl('clipboard', {'text': text}); + _diagnosticLog.write('clipboard -> peer (${text.length} chars)'); + } + + /// 手动发送一次本机剪贴板内容(控制栏「发送剪贴板」按钮)。 + Future _sendLocalClipboard() async { + if (!_phase.isConnected) { + _snack('请先连接被控端'); + return; + } + final data = await Clipboard.getData(Clipboard.kTextPlain); + final text = data?.text; + if (text == null || text.isEmpty) { + _snack('本机剪贴板为空'); + return; + } + _lastClipboardText = text; + _bridge.sendControl('clipboard', {'text': text}); + _snack('已发送剪贴板内容(${text.length} 字)'); + } + /// 把本机会话偏好下发给被控端(msgType=9 会话配置帧)。 /// /// 必须在请求投屏之前下发:被控端按该配置决定编码格式、码率、音频与剪贴板同步。 @@ -379,6 +432,47 @@ class _HomePageState extends State { }); } + /// 串流窗口里的右键/中键 → 鼠标控制帧。 + /// + /// 被控端把右键映射成「返回」、中键映射成「多任务」,符合桌面操作习惯。 + void _onVideoMouseButton(int button, int action, int x, int y) { + if (!_phase.isConnected) return; + final buttons = switch (button) { + 1 => 0x02, // bit1 = 右键 + 2 => 0x04, // bit2 = 中键 + _ => 0x01, // bit0 = 左键 + }; + _sendControl('mouse', { + 'action': action, + 'x': x, + 'y': y, + 'buttons': buttons, + }); + } + + /// 串流窗口里的滚轮 → 滚轮控制帧(被控端用屏幕中心作为滚动起点)。 + void _onVideoWheel(int deltaX, int deltaY) { + if (!_phase.isConnected) return; + if (deltaX == 0 && deltaY == 0) return; + _sendControl('wheel', { + 'deltaX': deltaX, + 'deltaY': deltaY, + 'buttons': 0, + }); + } + + /// 串流窗口里的键盘 → 按键控制帧。 + void _onVideoKey(int action, int vk, int modifiers) { + if (!_phase.isConnected) return; + final keycode = _androidKeycodeFromVk(vk); + if (keycode == null) return; + _sendControl('key', { + 'action': action, + 'keycode': keycode, + 'modifiers': modifiers, + }); + } + void _snack(String message) { if (!mounted) return; ScaffoldMessenger.of(context) @@ -631,6 +725,11 @@ class _HomePageState extends State { } void _sendControl(String type, Map payload) { + // 控制栏的「发送剪贴板」:payload 不带文本时读本机剪贴板实时内容。 + if (type == 'clipboard' && payload['text'] == null) { + unawaited(_sendLocalClipboard()); + return; + } _bridge.sendControl(type, payload); // 只有一次性动作才弹提示:触摸/鼠标/按键/滚轮是连续事件(拖拽时每秒几十条), // 每条都弹 SnackBar 会把 Flutter UI 线程压满,串流反而更卡。 @@ -896,3 +995,47 @@ class _HomePageState extends State { ); } } + +/// Windows 虚拟键码(VK_*)→ Android KeyEvent keycode;不支持的键返回 null。 +/// +/// 只覆盖常见键:字母/数字/编辑键/方向键/功能键与标点。中文等文本输入依赖输入法, +/// 注入按键无法替代,属于已知限制。 +int? _androidKeycodeFromVk(int vk) { + if (vk >= 0x41 && vk <= 0x5A) return 29 + (vk - 0x41); // A-Z + if (vk >= 0x30 && vk <= 0x39) return 7 + (vk - 0x30); // 0-9 + if (vk >= 0x70 && vk <= 0x7B) return 131 + (vk - 0x70); // F1-F12 + return switch (vk) { + 0x08 => 67, // Backspace + 0x09 => 61, // Tab + 0x0D => 66, // Enter + 0x10 => 59, // Shift → KEYCODE_SHIFT_LEFT + 0x11 => 113, // Ctrl → KEYCODE_CTRL_LEFT + 0x12 => 57, // Alt → KEYCODE_ALT_LEFT + 0x14 => 115, // CapsLock + 0x1B => 111, // Escape + 0x20 => 62, // Space + 0x21 => 92, // PageUp + 0x22 => 93, // PageDown + 0x23 => 123, // End + 0x24 => 122, // Home + 0x25 => 21, // Left + 0x26 => 19, // Up + 0x27 => 22, // Right + 0x28 => 20, // Down + 0x2D => 124, // Insert + 0x2E => 112, // Delete + 0x5B || 0x5C => 117, // Win → KEYCODE_META_LEFT + 0xBA => 74, // 分号 + 0xBB => 70, // 等号 + 0xBC => 55, // 逗号 + 0xBD => 69, // 减号 + 0xBE => 56, // 句点 + 0xBF => 76, // 斜杠 + 0xC0 => 68, // 反引号 + 0xDB => 71, // 左方括号 + 0xDC => 73, // 反斜杠 + 0xDD => 72, // 右方括号 + 0xDE => 75, // 单引号 + _ => null, + }; +} diff --git a/flutter_sender/windows/runner/video_texture.cpp b/flutter_sender/windows/runner/video_texture.cpp index e623115..ed84b27 100644 --- a/flutter_sender/windows/runner/video_texture.cpp +++ b/flutter_sender/windows/runner/video_texture.cpp @@ -855,6 +855,52 @@ class VideoTexturePlugin : public flutter::Plugin { std::make_unique(map)); }); + // 右键/中键:button 1=右键 2=中键,Dart 侧映射成 Android 返回/多任务。 + video_window_.SetMouseButtonHandler( + [this](int button, int action, int x, int y) { + if (channel_ == nullptr) { + return; + } + flutter::EncodableMap map; + map[flutter::EncodableValue("button")] = + flutter::EncodableValue(button); + map[flutter::EncodableValue("action")] = + flutter::EncodableValue(action); + map[flutter::EncodableValue("x")] = flutter::EncodableValue(x); + map[flutter::EncodableValue("y")] = flutter::EncodableValue(y); + channel_->InvokeMethod("mouse", + std::make_unique(map)); + }); + + // 滚轮:没有指针坐标,被控端用屏幕中心兜底。 + video_window_.SetWheelHandler([this](int delta_x, int delta_y) { + if (channel_ == nullptr) { + return; + } + flutter::EncodableMap map; + map[flutter::EncodableValue("deltaX")] = + flutter::EncodableValue(delta_x); + map[flutter::EncodableValue("deltaY")] = + flutter::EncodableValue(delta_y); + channel_->InvokeMethod("wheel", + std::make_unique(map)); + }); + + // 键盘:只报 Windows 虚拟键码,Android keycode 的映射留在 Dart 侧。 + video_window_.SetKeyboardHandler([this](int action, int vk, + int modifiers) { + if (channel_ == nullptr) { + return; + } + flutter::EncodableMap map; + map[flutter::EncodableValue("action")] = flutter::EncodableValue(action); + map[flutter::EncodableValue("vk")] = flutter::EncodableValue(vk); + map[flutter::EncodableValue("modifiers")] = + flutter::EncodableValue(modifiers); + channel_->InvokeMethod("key", + std::make_unique(map)); + }); + worker_ = std::thread([this, shared_frames]() { RunWorker(shared_frames); }); } @@ -862,6 +908,9 @@ class VideoTexturePlugin : public flutter::Plugin { // 先摘掉回调,避免析构期间 DestroyWindow 触发 InvokeMethod。 video_window_.SetCloseHandler(nullptr); video_window_.SetInputHandler(nullptr); + video_window_.SetMouseButtonHandler(nullptr); + video_window_.SetWheelHandler(nullptr); + video_window_.SetKeyboardHandler(nullptr); video_window_.Close(); stop_.store(true); if (worker_.joinable()) { diff --git a/flutter_sender/windows/runner/video_window.cpp b/flutter_sender/windows/runner/video_window.cpp index 7645189..f9b0cd7 100644 --- a/flutter_sender/windows/runner/video_window.cpp +++ b/flutter_sender/windows/runner/video_window.cpp @@ -150,6 +150,18 @@ void VideoWindow::SetInputHandler(InputHandler handler) { input_handler_ = std::move(handler); } +void VideoWindow::SetMouseButtonHandler(MouseButtonHandler handler) { + mouse_button_handler_ = std::move(handler); +} + +void VideoWindow::SetWheelHandler(WheelHandler handler) { + wheel_handler_ = std::move(handler); +} + +void VideoWindow::SetKeyboardHandler(KeyboardHandler handler) { + keyboard_handler_ = std::move(handler); +} + void VideoWindow::OnFrameAvailable() { if (!open_.load()) { return; @@ -254,12 +266,54 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam, } return 0; } + case WM_RBUTTONDOWN: + case WM_RBUTTONUP: + case WM_MBUTTONDOWN: + case WM_MBUTTONUP: { + // 右键 = 手机上的「返回」,中键 = 「多任务」(映射在 Dart 侧做,这里只报按键)。 + const bool right = message == WM_RBUTTONDOWN || message == WM_RBUTTONUP; + const bool down = message == WM_RBUTTONDOWN || message == WM_MBUTTONDOWN; + // 右键拖动时不想选中画面,按下前先抢焦点。 + if (down) { + SetFocus(hwnd); + } + DispatchMouseButtonAt(right ? 1 : 2, down ? 0 : 1, + POINT{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}); + return 0; + } + case WM_MOUSEWHEEL: { + // 一格 = WHEEL_DELTA(120)。Win32 正数表示向前滚(看到更早的内容), + // 协议里 deltaY 正数表示「向下滚」(手指上滑,看到更后面的内容),所以取反。 + const int steps = -GET_WHEEL_DELTA_WPARAM(wparam) / WHEEL_DELTA; + if (steps == 0) { + return 0; + } + WheelHandler handler = wheel_handler_; + if (handler) { + handler(0, steps); + } + return 0; + } case WM_KEYDOWN: if (wparam == VK_ESCAPE) { // 关窗即停止投屏(由 close_handler_ 通知 Flutter)。 SendMessageW(hwnd, WM_CLOSE, 0, 0); return 0; } + DispatchKey(0, static_cast(wparam)); + return 0; + case WM_KEYUP: + DispatchKey(1, static_cast(wparam)); + return 0; + case WM_SYSKEYDOWN: + // Alt 组合键(Alt+F4 关窗等)仍要交给系统,先转发一份再放行。 + if (wparam == VK_F4) { + break; + } + DispatchKey(0, static_cast(wparam)); + break; + case WM_SYSKEYUP: + DispatchKey(1, static_cast(wparam)); break; case WM_CLOSE: DestroyWindow(hwnd); @@ -452,6 +506,45 @@ void VideoWindow::DispatchPointerAt(int action, POINT point) { handler(action, x, y); } +void VideoWindow::DispatchMouseButtonAt(int button, int action, POINT point) { + last_point_ = point; + MouseButtonHandler handler = mouse_button_handler_; + if (!handler) { + return; + } + int x = 0; + int y = 0; + if (!ClientToFrame(point, &x, &y)) { + return; + } + handler(button, action, x, y); +} + +void VideoWindow::DispatchKey(int action, int vk) { + KeyboardHandler handler = keyboard_handler_; + if (!handler) { + return; + } + handler(action, vk, CurrentModifiers()); +} + +int VideoWindow::CurrentModifiers() const { + int modifiers = 0; + if ((GetKeyState(VK_CONTROL) & 0x8000) != 0) { + modifiers |= 0x01; + } + if ((GetKeyState(VK_MENU) & 0x8000) != 0) { + modifiers |= 0x02; + } + if ((GetKeyState(VK_SHIFT) & 0x8000) != 0) { + modifiers |= 0x04; + } + if (((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x8000) != 0) { + modifiers |= 0x08; + } + return modifiers; +} + void VideoWindow::ApplyAspectToWindow(double aspect) { if (hwnd_ == nullptr || aspect <= 0.0) { return; diff --git a/flutter_sender/windows/runner/video_window.h b/flutter_sender/windows/runner/video_window.h index 2d91383..535f2f2 100644 --- a/flutter_sender/windows/runner/video_window.h +++ b/flutter_sender/windows/runner/video_window.h @@ -27,6 +27,14 @@ class VideoWindow { // 画面内鼠标操作回调,坐标已换算成画面像素(0..frame_width-1)。 // action:0=按下 1=抬起 2=移动 3=取消,语义与协议 TouchEvent.action 一致。 using InputHandler = std::function; + // 右键/中键回调。button:1=右键 2=中键;action:0=按下 1=抬起; + // 坐标已换算成画面像素(黑边上的点击会被丢弃)。 + using MouseButtonHandler = std::function; + // 滚轮回调。deltaY 正=向下滚(手指上滑),与协议 WheelEvent 的约定一致。 + using WheelHandler = std::function; + // 键盘回调。action:0=按下 1=抬起;vk 是 Windows 虚拟键码(由 Dart 侧映射成 + // Android keycode);modifiers:bit0=ctrl bit1=alt bit2=shift bit3=meta。 + using KeyboardHandler = std::function; VideoWindow(); ~VideoWindow(); @@ -48,6 +56,15 @@ class VideoWindow { // 设置/清除画面鼠标回调;传 nullptr 清除。 void SetInputHandler(InputHandler handler); + // 设置/清除右键/中键回调;传 nullptr 清除。 + void SetMouseButtonHandler(MouseButtonHandler handler); + + // 设置/清除滚轮回调;传 nullptr 清除。 + void SetWheelHandler(WheelHandler handler); + + // 设置/清除键盘回调;传 nullptr 清除。 + void SetKeyboardHandler(KeyboardHandler handler); + bool IsOpen() const { return open_.load(); } private: @@ -62,6 +79,10 @@ class VideoWindow { // 客户区坐标 -> 画面像素;返回 false 表示点在黑边上或还没有画面。 bool ClientToFrame(POINT point, int* x, int* y) const; void DispatchPointerAt(int action, POINT point); + void DispatchMouseButtonAt(int button, int action, POINT point); + void DispatchKey(int action, int vk); + // 当前修饰键状态(按协议位定义拼装)。 + int CurrentModifiers() const; // 离屏缓冲:整帧画完再一次性贴到窗口,避免「已填黑、还没拉伸」的中间态被合成。 void EnsureBackBuffer(HDC reference, int width, int height); void ReleaseBackBuffer(); @@ -72,6 +93,9 @@ class VideoWindow { FrameProvider provider_; CloseHandler close_handler_; InputHandler input_handler_; + MouseButtonHandler mouse_button_handler_; + WheelHandler wheel_handler_; + KeyboardHandler keyboard_handler_; // 双缓冲用的内存 DC 与位图,尺寸跟随客户区。只在窗口线程读写。 HDC back_dc_ = nullptr;