From dc4fd792f6e7ae35d1f91fb3928134504f613a8c Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 29 Oct 2026 18:09:11 +0100 Subject: [PATCH] Feature: Implement the Linux screen preview response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Linux client now answers COMMAND_SCREEN_PREVIEW_REQ, so a host with an X11 desktop gets a thumbnail when it is double-clicked in the online list, a frame in the list's Preview column, and an image from the MCP get_screenshot tool. Previously only Windows clients advertised the capability and Linux hosts showed "预览不可用" after a four second wait. The client does not encode JPEG. Stock desktops ship libjpeg.so.8 whose struct layout is the distribution's ABI (JPEG_LIB_VERSION 80), while the in-tree jpeglib.h is libjpeg-turbo 3.1.1 (62), and libturbojpeg is not installed by default; dlopening the system library against that header fails with "Wrong JPEG library version: library is 80, caller expects 62". Instead the client sends raw pixels in a new screen preview format, SCREEN_PREVIEW_FMT_BGRX: top-down rows of B,G,R,X with no row padding, already scaled to the requested width. The server re-encodes to JPEG at the single ingress point, so the preview popup, the list thumbnail cache and the MCP tool keep consuming JPEG only. Capture follows the path ScreenHandler already uses: root window copied into an offscreen pixmap with an IncludeInferiors GC, then XGetImage, which avoids BadMatch under compositing window managers. The X11 type declarations and the dlopen loader moved out of ScreenHandler.h into linux/X11Loader.h so both users share one dlsym table. The full-size image is never materialized: a box filter reads straight from the XImage rows (honouring bytes_per_line) into the output buffer, scales to maxWidth without upscaling, and clamps the height to 1080 so a portrait 4K desktop cannot produce a 26 MB packet. Rows and columns come out even, which they are for JPEG anyway. The capability bit is now honest about the runtime. LOGIN_INFOR grows DefaultCaps()/SetCaps(), and the client recomputes the bits before every login and re-login: CLIENT_CAP_SCREEN_PREVIEW is set only when DISPLAY is present and the X server actually opens. A headless host therefore gets -32005 "Host does not support screen preview" from MCP instead of a timeout, and a host whose session appears after boot picks it up on the next reconnect. Requests are handled on a detached worker thread behind a single-flight guard (matching the Windows client), so a slow XGetImage never stalls the receive thread and a burst of requests degrades to "no preview" rather than queueing. If the worker thread cannot be created, the guard is released again, so one failure cannot leave every later request rejected as busy until the client restarts. The MCP design note claimed the capability was declared by Windows only, and the get_screenshot tool description said the same; both now say what the code does (Windows always, Linux when a display is reachable). Server side, NormalizeScreenPreviewPacket runs in MessageHandle before the MCP handoff and the PostMessage to the UI thread. It validates that width/height are within the values the server itself can ask for and that bytes equals width*height*4, converts with a stack Gdiplus::Bitmap over the packet's own pixels (no copy, and no DEBUG_NEW trouble), and rewrites the header to JPEG. The encoder output is read back by seeking the stream rather than GlobalSize, so the packet carries no heap-block padding after the EOI marker. Malformed packets are dropped; failure statuses pass through untouched so the existing code can report them. Tested against a Debug server with the WSL Ubuntu 24.04 build as the client. get_screenshot returned valid JPEGs at max_width 480 (480x142, 1.7 KB) and 1920 (1920x570, 17.9 KB), repeatably; the list showed a thumbnail for the GUI client and N/A for a second copy started without DISPLAY, which also answered -32005. The box filter and the BGRX to JPEG conversion were checked separately with quadrant fixtures: the browser over the synthetic B,G,R,X buffer recovers red/green/blue/white in the right channels and the downscale averages, handles non-integer ratios and row padding, and rejects 24bpp and upscaling. The captured frames come out black under WSLg because Xwayland keeps no root pixmap there; that is the compositor, not this code, and Xorg sessions capture normally. Co-Authored-By: deepseek-flash --- common/commands.h | 31 +++- docs/Mcp_Phase2_Design.md | 4 +- linux/CMakeLists.txt | 1 + linux/README.md | 2 + linux/ScreenHandler.h | 265 +------------------------- linux/ScreenPreview.cpp | 185 ++++++++++++++++++ linux/ScreenPreview.h | 24 +++ linux/X11Loader.h | 279 ++++++++++++++++++++++++++++ linux/main.cpp | 79 ++++++++ server/2015Remote/2015RemoteDlg.cpp | 102 +++++++++- server/2015Remote/2015RemoteDlg.h | 3 + server/2015Remote/McpServer.cpp | 5 +- 12 files changed, 704 insertions(+), 276 deletions(-) create mode 100644 linux/ScreenPreview.cpp create mode 100644 linux/ScreenPreview.h create mode 100644 linux/X11Loader.h diff --git a/common/commands.h b/common/commands.h index b589575..d9d4a09 100644 --- a/common/commands.h +++ b/common/commands.h @@ -465,13 +465,17 @@ static_assert(sizeof(ConnAuthAck) == 256, "ConnAuthAck must be exactly 256 by // 屏幕预览:服务端按双击在线主机触发,向客户端要一张缩略图(JPEG),与浮窗一起显示。 // 服务端依 ctx 最近心跳 Ping + RES_RESOLUTION 决定 maxWidth/quality 后下发;客户端 -// 主屏抓图 → 等比缩放 → JPEG 编码 → 回响应。format 字段 v1 锁 0=JPEG,预留 PNG/WebP。 +// 主屏抓图 → 等比缩放 → JPEG 编码 → 回响应。 +// +// format 字段:v1 只有 JPEG(0);Windows 客户端走这条路(GDI+ 编码)。Linux 客户端 +// 没有可依赖的 JPEG 编码库,改为回 BGRX(3) 原始像素,由服务端在 IO 线程统一编码成 +// JPEG——下游(浮窗/列表缩略图/MCP get_screenshot)因此只认 JPEG 一种格式。 #pragma pack(push, 1) struct ScreenPreviewReq { uint8_t cmd; // = COMMAND_SCREEN_PREVIEW_REQ uint16_t reqId; // 请求序号,用于丢弃过期响应 uint16_t maxWidth; // 服务端期望的目标宽度(客户端等比缩放,不强制) - uint8_t jpegQuality; // 1..100 + uint8_t jpegQuality; // 1..100(BGRX 客户端不编码,服务端按自己的档位重编) uint16_t reserved; }; // 总 8 字节 @@ -486,6 +490,9 @@ enum ScreenPreviewFormat { SCREEN_PREVIEW_FMT_JPEG = 0, SCREEN_PREVIEW_FMT_PNG = 1, // 预留 SCREEN_PREVIEW_FMT_WEBP = 2, // 预留 + // 原始像素:自上而下逐行、每像素 4 字节 B,G,R,X(X 填充为 0xFF),无行首对齐填充。 + // bytes 必须等于 width*height*4;客户端已缩放到 maxWidth 以内,不再二次缩放。 + SCREEN_PREVIEW_FMT_BGRX = 3, }; struct ScreenPreviewRspHeader { @@ -1107,9 +1114,15 @@ typedef struct LOGIN_INFOR { { memset(this, 0, sizeof(LOGIN_INFOR)); bToken = TOKEN_LOGIN; - // 能力位:声明客户端实际实现了的功能。SCREEN_PREVIEW 只在 Windows 客户端 - // 实现(依赖 GDI BitBlt + GDI+ JPEG),Linux/macOS 不声明,避免服务端发请求 - // 后等 4s 超时显示"预览不可用"。 + SetCaps(DefaultCaps()); + } + + // 本平台的默认能力位:声明客户端实际实现了的功能。 + // SCREEN_PREVIEW 依赖抓屏:Windows 恒为真(GDI BitBlt + GDI+ JPEG);Linux 要等 + // 运行时确认有可截图的 X11 会话,所以不在这里声明,由 main.cpp 探测后用 + // SetCaps(DefaultCaps() | CLIENT_CAP_SCREEN_PREVIEW) 追加。 + static unsigned int DefaultCaps() + { unsigned int caps = CLIENT_CAP_V2 | CLIENT_CAP_UTF8; #ifdef _WIN32 caps |= CLIENT_CAP_SCREEN_PREVIEW; @@ -1119,6 +1132,14 @@ typedef struct LOGIN_INFOR { // macOS 暂未接线(无全局主连接句柄),不声明——服务端就不会为它空等回执。 caps |= CLIENT_CAP_C2C_RECV_ACK; #endif + return caps; + } + + // 覆写能力位。服务端只从 moduleVersion 的 "版本-XXXX" 后缀解析能力位 + // (2015RemoteDlg.cpp AddList),因此改能力位必须重写该字段; + // LOGIN_INFOR 是与服务端共享的定长结构,不能再加成员来携带它。 + void SetCaps(unsigned int caps) + { sprintf_s(moduleVersion, "%s-%04X", DLL_VERSION, caps); } LOGIN_INFOR& Speed(unsigned long speed) diff --git a/docs/Mcp_Phase2_Design.md b/docs/Mcp_Phase2_Design.md index 5be8d17..d79c549 100644 --- a/docs/Mcp_Phase2_Design.md +++ b/docs/Mcp_Phase2_Design.md @@ -275,7 +275,7 @@ Web 是长连接,子链接可长驻;MCP 是**一次性 request/response**。 - 复用 MFC 预览的 RTT/FRP 自适应参数挑选 `ChooseScreenPreviewParams` + `SendScreenPreviewRequest`,不重复实现 `GetTargetQualityLevel`。 - **可选 `max_width` 参数**:缺省/0 沿用缩略图档位(最大 1024,4K 源屏高档 1280);`max_width`>0 覆盖宽度并钳制到客户端上限 `[64,1920]`(客户端 `CaptureAndEncodePreview` 硬钳制),供 AI 视觉/OCR 场景请求接近原分辨率(1080p 源屏传 1920 即原分辨率,4K 源屏最多 1920)。`jpegQuality` 仍沿用档位自适应值。 - **请求关联**:`ScreenPreviewReq.reqId` 提供关联;MCP 用独立 16 位 reqId 发生器(`NextPreviewReqId`,跳过 0)+ `SetPendingReqId` 登记期望值,`TakePreviewResponse` 只接受「挂起命中 + reqId 一致」的响应,过期/他途(MFC 预览并发)响应原样回落到 MFC 路径。 -- 客户端能力位 `CLIENT_CAP_SCREEN_PREVIEW`(0x0004,仅 Windows 客户端声明):非 Windows 主机返回 `-32005`「不支持」。 +- 客户端能力位 `CLIENT_CAP_SCREEN_PREVIEW`(0x0004):Windows 客户端恒声明;Linux 客户端在登录时探测到可用 X11 桌面才声明(无 `DISPLAY` 的服务器不声明)。未声明的主机返回 `-32005`「不支持」。 - 返回结构:`content[0] = {type:"image", data: base64(JPEG), mimeType:"image/jpeg"}` + `structuredContent.image = {mimeType,width,height,bytes}`(元数据,不含 base64,避免二次膨胀)。 **`list_files`(文件链路,一次性子链接,模式 A′)**: @@ -303,7 +303,7 @@ Web 是长连接,子链接可长驻;MCP 是**一次性 request/response**。 **验收 / 实机验证点(待构建后执行)**: - MFC 双击文件管理弹框仍正常(回归点);MFC 双击预览缩略图仍正常(回归点)。 -- MCP `get_screenshot` 返回 JPEG(base64 + 元数据),Windows 主机正常;非 Windows 主机(或未声明 `CLIENT_CAP_SCREEN_PREVIEW`)返回 `-32005`。 +- MCP `get_screenshot` 返回 JPEG(base64 + 元数据),Windows 主机正常;带 X11 桌面的 Linux 主机同样正常(客户端回 BGRA,服务端转 JPEG);未声明 `CLIENT_CAP_SCREEN_PREVIEW` 的主机返回 `-32005`。 - MCP `list_files`(无 `path`)返回 `drives[]`;`list_files(path="C:\\")` 返回 `files[]`(含目录/文件/大小/mtime),中文文件名无乱码;条目 >500 时截断为 500。 - 错误路径:缺 `id`/非数字 `id` → `-32602`;未知/离线主机 → `-32002`;同 host 并发 → `-32003`;发送失败 → `-32004`;超时 → `-32001`。 diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index 14c882f..4943377 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -26,6 +26,7 @@ include_directories(../compress) # 添加可执行文件 set(SOURCES main.cpp + ScreenPreview.cpp # 屏幕预览(缩略图)抓屏,见 ScreenPreview.h ../client/Buffer.cpp ../client/IOCPClient.cpp ../client/TlsChannelLinux.cpp # Linux 客户端单向 TLS(OpenSSL),见 client/TlsChannel.h diff --git a/linux/README.md b/linux/README.md index 23cf759..492404c 100644 --- a/linux/README.md +++ b/linux/README.md @@ -5,6 +5,7 @@ SimpleRemoter 的 Linux 客户端,支持远程桌面、远程终端、文件 ## 功能特性 - **远程桌面** - 实时屏幕截图传输,支持鼠标/键盘控制,支持多种压缩算法 +- **屏幕预览** - 服务端双击主机弹缩略图、在线列表缩略图、MCP `get_screenshot` - **远程终端** - 基于 PTY 的交互式 Shell - **文件管理** - 远程文件浏览、上传、下载 - **进程管理** - 查看和管理远程进程 @@ -25,6 +26,7 @@ SimpleRemoter 的 Linux 客户端,支持远程桌面、远程终端、文件 | 系统信息采集 | main.cpp | `LoginServer.cpp` | ✅ 完整 | | 守护进程 | daemonize() | Windows 服务 | ✅ 完整 | | 剪贴板同步 | `ClipboardHandler.h` | `ScreenManager.cpp` | ✅ 服务端↔Linux | +| 屏幕预览(缩略图) | `ScreenPreview.cpp` | `client/ScreenPreview.cpp` | ✅ 完整(X11 抓屏 → 缩放 → 原始 BGRA 上报,由服务端统一编码 JPEG;双击预览、列表缩略图、MCP `get_screenshot` 均可用。无 `DISPLAY` 时不声明能力位,服务端不会发请求) | | V2 文件传输 | `FileTransferV2.h` | `file_upload.h` | ✅ 服务端↔Linux(含 MCP `upload_file`/`download_file`:发送完成包带流式 SHA-256,路径不存在/无权限回 `TOKEN_DOWNLOAD_ERROR`) | | C2C 文件接收 | `FileTransferV2.h` | `file_upload.h` | ✅ Windows/MCP → Linux(目标目录由服务端 `dst_dir` 指定,落盘后回执) | | 配置持久化 | `LinuxConfig` | INI 文件 | ✅ 完整 | diff --git a/linux/ScreenHandler.h b/linux/ScreenHandler.h index 82fc4b1..48a86fe 100644 --- a/linux/ScreenHandler.h +++ b/linux/ScreenHandler.h @@ -5,6 +5,7 @@ #include "ClipboardHandler.h" #include "common/FileTransferV2.h" #include "X264Encoder.h" +#include "X11Loader.h" #include #include #include @@ -50,68 +51,6 @@ struct MSG64_LNX { }; #pragma pack(pop) -// X11 类型前向声明(避免 #include ) -typedef struct _XDisplay Display; -typedef unsigned long XID; -typedef XID Window; -typedef XID Drawable; -typedef XID Pixmap; -typedef struct _XGC *GC; - -struct XImage { - int width, height; - int xoffset; - int format; - char *data; - int byte_order; - int bitmap_unit; - int bitmap_bit_order; - int bitmap_pad; - int depth; - int bytes_per_line; - int bits_per_pixel; - unsigned long red_mask; - unsigned long green_mask; - unsigned long blue_mask; - // 后续字段省略,XDestroyImage 通过函数指针释放 - void *obdata; - struct funcs { - void *p[8]; // create_image, destroy_image, ... - } f; -}; - -// X11 错误处理(防止 BadMatch 等错误导致进程退出) -static int x11_error_handler(Display* dpy, void* evt) -{ - // 忽略所有 X11 错误,由调用方检查返回值 - return 0; -} - -// XCreateGC 需要的 XGCValues 结构体(布局与 Xlib 一致) -struct XGCValues_LNX { - int function; - unsigned long plane_mask; - unsigned long foreground; - unsigned long background; - int line_width; - int line_style; - int cap_style; - int join_style; - int fill_style; - int fill_rule; - int arc_mode; - unsigned long tile; // Pixmap = XID - unsigned long stipple; // Pixmap = XID - int ts_x_origin; - int ts_y_origin; - unsigned long font; // Font = XID - int subwindow_mode; -}; - -// X11 GC 常量 -#define GCSubwindowMode (1L<<15) -#define IncludeInferiors 1 - // ============== 屏幕算法常量 ============== // 常量定义已移至 commands.h: ALGORITHM_GRAY, ALGORITHM_DIFF, ALGORITHM_H264, ALGORITHM_RGB565 @@ -390,208 +329,6 @@ static unsigned long VKtoKeySym(unsigned int vk) } } -// XFixes cursor image structure (for cursor type detection) -struct XFixesCursorImage { - short x, y; - unsigned short width, height; - unsigned short xhot, yhot; - unsigned long cursor_serial; - unsigned long* pixels; - // Atom cursor_name; // Only in XFixes 2.0+ -}; - -// X11 函数指针类型 -typedef Display* (*fn_XOpenDisplay)(const char*); -typedef int (*fn_XCloseDisplay)(Display*); -typedef XImage* (*fn_XGetImage)(Display*, Drawable, int, int, unsigned int, unsigned int, unsigned long, int); -typedef int (*fn_XDestroyImage)(XImage*); -typedef int (*fn_XSetErrorHandler)(int (*)(Display*, void*)); -typedef Pixmap (*fn_XCreatePixmap)(Display*, Drawable, unsigned int, unsigned int, unsigned int); -typedef int (*fn_XFreePixmap)(Display*, Pixmap); -typedef GC (*fn_XCreateGC)(Display*, Drawable, unsigned long, void*); -typedef int (*fn_XFreeGC)(Display*, GC); -typedef int (*fn_XCopyArea)(Display*, Drawable, Drawable, GC, int, int, unsigned int, unsigned int, int, int); -typedef int (*fn_XDefaultDepth)(Display*, int); -typedef int (*fn_XSync)(Display*, int); -typedef unsigned long (*fn_XKeysymToKeycode)(Display*, unsigned long); -typedef int (*fn_XFlush)(Display*); -typedef int (*fn_XClearArea)(Display*, Window, int, int, unsigned int, unsigned int, int); -typedef int (*fn_XQueryPointer)(Display*, Window, Window*, Window*, int*, int*, int*, int*, unsigned int*); -typedef int (*fn_XFree)(void*); - -// XTest 扩展函数指针类型(用于模拟鼠标/键盘输入) -typedef int (*fn_XTestFakeMotionEvent)(Display*, int, int, int, unsigned long); -typedef int (*fn_XTestFakeButtonEvent)(Display*, unsigned int, int, unsigned long); -typedef int (*fn_XTestFakeKeyEvent)(Display*, unsigned int, int, unsigned long); - -// XFixes 扩展函数指针类型(用于光标类型检测) -typedef int (*fn_XFixesQueryExtension)(Display*, int*, int*); -typedef XFixesCursorImage* (*fn_XFixesGetCursorImage)(Display*); - -// X11 动态加载包装 -class X11Loader -{ -public: - fn_XOpenDisplay pXOpenDisplay; - fn_XCloseDisplay pXCloseDisplay; - fn_XGetImage pXGetImage; - fn_XDestroyImage pXDestroyImage; - - // Xlib 宏的替代:通过偏移读取 Display 内部结构 - // 这些函数通过 dlsym 获取 - typedef int (*fn_XDefaultScreen)(Display*); - typedef int (*fn_XDisplayWidth)(Display*, int); - typedef int (*fn_XDisplayHeight)(Display*, int); - typedef Window (*fn_XRootWindow)(Display*, int); - - fn_XDefaultScreen pXDefaultScreen; - fn_XDisplayWidth pXDisplayWidth; - fn_XDisplayHeight pXDisplayHeight; - fn_XRootWindow pXRootWindow; - - // Pixmap 相关(解决合成窗口管理器下 XGetImage BadMatch 问题) - fn_XSetErrorHandler pXSetErrorHandler; - fn_XCreatePixmap pXCreatePixmap; - fn_XFreePixmap pXFreePixmap; - fn_XCreateGC pXCreateGC; - fn_XFreeGC pXFreeGC; - fn_XCopyArea pXCopyArea; - fn_XDefaultDepth pXDefaultDepth; - fn_XSync pXSync; - fn_XKeysymToKeycode pXKeysymToKeycode; - fn_XFlush pXFlush; - fn_XClearArea pXClearArea; - fn_XQueryPointer pXQueryPointer; - fn_XFree pXFree; - - // XTest 扩展(用于模拟输入) - fn_XTestFakeMotionEvent pXTestFakeMotionEvent; - fn_XTestFakeButtonEvent pXTestFakeButtonEvent; - fn_XTestFakeKeyEvent pXTestFakeKeyEvent; - - // XFixes 扩展(用于光标类型检测) - fn_XFixesQueryExtension pXFixesQueryExtension; - fn_XFixesGetCursorImage pXFixesGetCursorImage; - - X11Loader() : m_handle(nullptr), m_xtst_handle(nullptr), m_xfixes_handle(nullptr) - { - pXOpenDisplay = nullptr; - pXCloseDisplay = nullptr; - pXGetImage = nullptr; - pXDestroyImage = nullptr; - pXDefaultScreen = nullptr; - pXDisplayWidth = nullptr; - pXDisplayHeight = nullptr; - pXRootWindow = nullptr; - pXSetErrorHandler = nullptr; - pXCreatePixmap = nullptr; - pXFreePixmap = nullptr; - pXCreateGC = nullptr; - pXFreeGC = nullptr; - pXCopyArea = nullptr; - pXDefaultDepth = nullptr; - pXSync = nullptr; - pXKeysymToKeycode = nullptr; - pXFlush = nullptr; - pXClearArea = nullptr; - pXQueryPointer = nullptr; - pXFree = nullptr; - pXTestFakeMotionEvent = nullptr; - pXTestFakeButtonEvent = nullptr; - pXTestFakeKeyEvent = nullptr; - pXFixesQueryExtension = nullptr; - pXFixesGetCursorImage = nullptr; - } - - bool Load() - { - m_handle = dlopen("libX11.so.6", RTLD_LAZY); - if (!m_handle) m_handle = dlopen("libX11.so", RTLD_LAZY); - if (!m_handle) return false; - - pXOpenDisplay = (fn_XOpenDisplay)dlsym(m_handle, "XOpenDisplay"); - pXCloseDisplay = (fn_XCloseDisplay)dlsym(m_handle, "XCloseDisplay"); - pXGetImage = (fn_XGetImage)dlsym(m_handle, "XGetImage"); - pXDestroyImage = (fn_XDestroyImage)dlsym(m_handle, "XDestroyImage"); - pXDefaultScreen = (fn_XDefaultScreen)dlsym(m_handle, "XDefaultScreen"); - pXDisplayWidth = (fn_XDisplayWidth)dlsym(m_handle, "XDisplayWidth"); - pXDisplayHeight = (fn_XDisplayHeight)dlsym(m_handle, "XDisplayHeight"); - pXRootWindow = (fn_XRootWindow)dlsym(m_handle, "XRootWindow"); - - // Pixmap 相关函数 - pXSetErrorHandler = (fn_XSetErrorHandler)dlsym(m_handle, "XSetErrorHandler"); - pXCreatePixmap = (fn_XCreatePixmap)dlsym(m_handle, "XCreatePixmap"); - pXFreePixmap = (fn_XFreePixmap)dlsym(m_handle, "XFreePixmap"); - pXCreateGC = (fn_XCreateGC)dlsym(m_handle, "XCreateGC"); - pXFreeGC = (fn_XFreeGC)dlsym(m_handle, "XFreeGC"); - pXCopyArea = (fn_XCopyArea)dlsym(m_handle, "XCopyArea"); - pXDefaultDepth = (fn_XDefaultDepth)dlsym(m_handle, "XDefaultDepth"); - pXSync = (fn_XSync)dlsym(m_handle, "XSync"); - pXKeysymToKeycode = (fn_XKeysymToKeycode)dlsym(m_handle, "XKeysymToKeycode"); - pXFlush = (fn_XFlush)dlsym(m_handle, "XFlush"); - pXClearArea = (fn_XClearArea)dlsym(m_handle, "XClearArea"); - pXQueryPointer = (fn_XQueryPointer)dlsym(m_handle, "XQueryPointer"); - pXFree = (fn_XFree)dlsym(m_handle, "XFree"); - - // 加载 XTest 扩展库(用于模拟鼠标/键盘输入) - m_xtst_handle = dlopen("libXtst.so.6", RTLD_LAZY); - if (!m_xtst_handle) m_xtst_handle = dlopen("libXtst.so", RTLD_LAZY); - if (m_xtst_handle) { - pXTestFakeMotionEvent = (fn_XTestFakeMotionEvent)dlsym(m_xtst_handle, "XTestFakeMotionEvent"); - pXTestFakeButtonEvent = (fn_XTestFakeButtonEvent)dlsym(m_xtst_handle, "XTestFakeButtonEvent"); - pXTestFakeKeyEvent = (fn_XTestFakeKeyEvent)dlsym(m_xtst_handle, "XTestFakeKeyEvent"); - } - - // 加载 XFixes 扩展库(用于光标类型检测) - m_xfixes_handle = dlopen("libXfixes.so.3", RTLD_LAZY); - if (!m_xfixes_handle) m_xfixes_handle = dlopen("libXfixes.so", RTLD_LAZY); - if (m_xfixes_handle) { - pXFixesQueryExtension = (fn_XFixesQueryExtension)dlsym(m_xfixes_handle, "XFixesQueryExtension"); - pXFixesGetCursorImage = (fn_XFixesGetCursorImage)dlsym(m_xfixes_handle, "XFixesGetCursorImage"); - } - - // 基本 X11 函数必须全部存在;XTest/XFixes 函数可选 - return pXOpenDisplay && pXCloseDisplay && pXGetImage && pXDestroyImage && - pXDefaultScreen && pXDisplayWidth && pXDisplayHeight && pXRootWindow && - pXSetErrorHandler && pXCreatePixmap && pXFreePixmap && - pXCreateGC && pXFreeGC && pXCopyArea && pXDefaultDepth && pXSync && - pXKeysymToKeycode && pXFlush; - } - - // 检查 XTest 扩展是否可用 - bool HasXTest() const - { - return pXTestFakeMotionEvent && pXTestFakeButtonEvent && pXTestFakeKeyEvent; - } - - // 检查 XFixes 扩展是否可用 - bool HasXFixes() const - { - return pXFixesGetCursorImage != nullptr; - } - - ~X11Loader() - { - if (m_xfixes_handle) { - dlclose(m_xfixes_handle); - m_xfixes_handle = nullptr; - } - if (m_xtst_handle) { - dlclose(m_xtst_handle); - m_xtst_handle = nullptr; - } - if (m_handle) { - dlclose(m_handle); - m_handle = nullptr; - } - } - -private: - void* m_handle; - void* m_xtst_handle; - void* m_xfixes_handle; -}; - class ScreenHandler : public IOCPManager { public: diff --git a/linux/ScreenPreview.cpp b/linux/ScreenPreview.cpp new file mode 100644 index 0000000..0633bbd --- /dev/null +++ b/linux/ScreenPreview.cpp @@ -0,0 +1,185 @@ +#include "ScreenPreview.h" +#include "X11Loader.h" +#include "common/commands.h" + +#include +#include + +namespace { + +// 一次抓屏独占一条 X 连接:Xlib 的 Display 不是线程安全的,预览在工作线程里跑, +// 不能复用 ScreenHandler 的那条连接;用完即关,避免长驻连接占着 X server 资源。 +struct XSession { + X11Loader x11; + Display* dpy = nullptr; + + bool Open() + { + // 没有 DISPLAY 必然是纯命令行/无桌面环境,连库都不必加载 + const char* display = getenv("DISPLAY"); + if (display == nullptr || display[0] == '\0') { + return false; + } + if (!x11.Load() || !x11.CanCapture()) { + return false; + } + // 与 ScreenHandler 一致:忽略 X11 协议错误,由返回值判断失败 + x11.pXSetErrorHandler(x11_error_handler); + dpy = x11.pXOpenDisplay(display); + return dpy != nullptr; + } + + ~XSession() + { + if (dpy) { + x11.pXCloseDisplay(dpy); + } + } +}; + +// 盒式滤波缩放,直接从 XImage 读到目标缓冲,不保留整屏中间副本。 +// 返回 false 表示像素格式不受支持(非 32bpp)。 +bool DownscaleBox(const XImage* img, int tw, int th, std::vector& out) +{ + if (img == nullptr || img->bits_per_pixel != 32 || img->bytes_per_line <= 0) { + return false; + } + const int sw = img->width; + const int sh = img->height; + if (sw <= 0 || sh <= 0 || tw <= 0 || th <= 0 || tw > sw || th > sh) { + return false; + } + + out.resize((size_t)tw * th * 4); + const unsigned char* base = (const unsigned char*)img->data; + for (int dy = 0; dy < th; dy++) { + // 目标行覆盖的源行区间 [sy0, sy1),区间非空因为 th <= sh + int sy0 = (int)((long long)dy * sh / th); + int sy1 = (int)((long long)(dy + 1) * sh / th); + if (sy1 <= sy0) sy1 = sy0 + 1; + + unsigned char* dst = out.data() + (size_t)dy * tw * 4; + for (int dx = 0; dx < tw; dx++) { + int sx0 = (int)((long long)dx * sw / tw); + int sx1 = (int)((long long)(dx + 1) * sw / tw); + if (sx1 <= sx0) sx1 = sx0 + 1; + + unsigned int sb = 0, sg = 0, sr = 0; + unsigned int count = 0; + for (int sy = sy0; sy < sy1; sy++) { + // X11 ZPixmap 32bpp 每像素 4 字节,字节序为 B,G,R,X(X 不用); + // 行尾可能有对齐填充,必须按 bytes_per_line 定位 + const unsigned char* row = base + (size_t)sy * img->bytes_per_line; + for (int sx = sx0; sx < sx1; sx++) { + const unsigned char* px = row + (size_t)sx * 4; + sb += px[0]; + sg += px[1]; + sr += px[2]; + count++; + } + } + + dst[0] = (unsigned char)(sb / count); + dst[1] = (unsigned char)(sg / count); + dst[2] = (unsigned char)(sr / count); + dst[3] = 0xFF; + dst += 4; + } + } + return true; +} + +} // namespace + +bool ScreenPreview::IsAvailable() +{ + XSession session; + if (!session.Open()) { + return false; + } + int screen = session.x11.pXDefaultScreen(session.dpy); + return session.x11.pXDisplayWidth(session.dpy, screen) > 0 && + session.x11.pXDisplayHeight(session.dpy, screen) > 0; +} + +int ScreenPreview::Capture(int maxWidth, std::vector& out, int& outW, int& outH) +{ + outW = 0; + outH = 0; + out.clear(); + + // 与 Windows 端一致:上限防滥用,下限防止 1 像素宽的退化请求 + if (maxWidth < 64) maxWidth = 64; + if (maxWidth > 1920) maxWidth = 1920; + + XSession session; + if (!session.Open()) { + return SCREEN_PREVIEW_NOT_SUPPORTED; + } + X11Loader& x11 = session.x11; + Display* dpy = session.dpy; + + int screen = x11.pXDefaultScreen(dpy); + int sw = x11.pXDisplayWidth(dpy, screen); + int sh = x11.pXDisplayHeight(dpy, screen); + if (sw <= 0 || sh <= 0) { + return SCREEN_PREVIEW_CAPTURE_FAILED; + } + + // 等比缩放到 maxWidth 以内(不放大);再把高度限制在 1080,避免竖屏 4K 这类 + // 极端宽高比把 BGRX 包撑到几十 MB(服务端只按宽度提要求,不管比例) + int tw = sw, th = sh; + if (tw > maxWidth) { + th = (int)((long long)sh * maxWidth / sw); + tw = maxWidth; + } + if (th > 1080) { + tw = (int)((long long)tw * 1080 / th); + th = 1080; + } + // 宽高取偶数,便于 JPEG 编码 + tw &= ~1; + th &= ~1; + if (tw < 2) tw = 2; + if (th < 2) th = 2; + if (tw > sw) tw = sw; + if (th > sh) th = sh; + + // root → 离屏 Pixmap → XGetImage:绕开合成窗口管理器下的 BadMatch + Window root = x11.pXRootWindow(dpy, screen); + int depth = x11.pXDefaultDepth(dpy, screen); + Pixmap pixmap = x11.pXCreatePixmap(dpy, root, (unsigned int)sw, (unsigned int)sh, (unsigned int)depth); + if (pixmap == 0) { + return SCREEN_PREVIEW_CAPTURE_FAILED; + } + + XGCValues_LNX gcv; + memset(&gcv, 0, sizeof(gcv)); + gcv.subwindow_mode = IncludeInferiors; // 连同子窗口内容一起抓 + GC gc = x11.pXCreateGC(dpy, root, GCSubwindowMode, &gcv); + if (gc == nullptr) { + x11.pXFreePixmap(dpy, pixmap); + return SCREEN_PREVIEW_CAPTURE_FAILED; + } + + int result = SCREEN_PREVIEW_CAPTURE_FAILED; + x11.pXCopyArea(dpy, root, pixmap, gc, 0, 0, (unsigned int)sw, (unsigned int)sh, 0, 0); + x11.pXSync(dpy, 0); // 等待拷贝完成 + + // AllPlanes = ~0UL, ZPixmap = 2 + XImage* img = x11.pXGetImage(dpy, pixmap, 0, 0, (unsigned int)sw, (unsigned int)sh, ~0UL, 2); + if (img != nullptr) { + if (DownscaleBox(img, tw, th, out)) { + outW = tw; + outH = th; + result = SCREEN_PREVIEW_OK; + } else { + out.clear(); + } + x11.pXDestroyImage(img); + } + + x11.pXFreeGC(dpy, gc); + x11.pXFreePixmap(dpy, pixmap); + return result; +} diff --git a/linux/ScreenPreview.h b/linux/ScreenPreview.h new file mode 100644 index 0000000..d6bb160 --- /dev/null +++ b/linux/ScreenPreview.h @@ -0,0 +1,24 @@ +#pragma once +// 屏幕预览(COMMAND_SCREEN_PREVIEW_REQ)—— Linux 端一次性抓屏 +// +// 与 Windows 端 client/ScreenPreview.h 对应,区别只有一处:Linux 没有可依赖的 +// JPEG 编码库(桌面版默认只带 libjpeg.so.8,其结构体布局随发行版 ABI 变化, +// 不适合 dlopen 后手搓),所以这里只做 抓屏 → 等比缩放 → BGRA 原始像素, +// 由服务端在 IO 线程统一编码成 JPEG(见 2015RemoteDlg.cpp 的预览包规范化)。 +// +// 抓屏路径与 ScreenHandler 相同:root window → 离屏 Pixmap(XCopyArea, +// IncludeInferiors)→ XGetImage,规避合成窗口管理器下的 BadMatch。 +#include +#include + +class ScreenPreview +{ +public: + // 本机当前能否截图(X11 库可加载 + 能打开 display + 主屏尺寸有效)。 + // 服务端按 CLIENT_CAP_SCREEN_PREVIEW 决定要不要下发预览请求,能力位就取自它。 + static bool IsAvailable(); + + // 抓主屏并等比缩放到 maxWidth 以内(不放大、高不超过 1080),返回 ScreenPreviewStatus。 + // out 为自上而下逐行、每像素 B,G,R,X 四字节(X=0xFF),无行首填充。 + static int Capture(int maxWidth, std::vector& out, int& outW, int& outH); +}; diff --git a/linux/X11Loader.h b/linux/X11Loader.h new file mode 100644 index 0000000..92cbfcf --- /dev/null +++ b/linux/X11Loader.h @@ -0,0 +1,279 @@ +#pragma once +// X11 运行时加载(编译期不依赖 X11 开发包,运行时 dlopen libX11.so) +// +// 从 ScreenHandler.h 抽出:远程桌面(ScreenHandler)和屏幕预览(ScreenPreview) +// 都要抓屏,共用这套类型定义与加载器,避免两份 dlsym 表各自漂移。 +#include + +// X11 类型前向声明(避免 #include ) +typedef struct _XDisplay Display; +typedef unsigned long XID; +typedef XID Window; +typedef XID Drawable; +typedef XID Pixmap; +typedef struct _XGC *GC; + +struct XImage { + int width, height; + int xoffset; + int format; + char *data; + int byte_order; + int bitmap_unit; + int bitmap_bit_order; + int bitmap_pad; + int depth; + int bytes_per_line; + int bits_per_pixel; + unsigned long red_mask; + unsigned long green_mask; + unsigned long blue_mask; + // 后续字段省略,XDestroyImage 通过函数指针释放 + void *obdata; + struct funcs { + void *p[8]; // create_image, destroy_image, ... + } f; +}; + +// X11 错误处理(防止 BadMatch 等错误导致进程退出) +static inline int x11_error_handler(Display* dpy, void* evt) +{ + // 忽略所有 X11 错误,由调用方检查返回值 + return 0; +} + +// XCreateGC 需要的 XGCValues 结构体(布局与 Xlib 一致) +struct XGCValues_LNX { + int function; + unsigned long plane_mask; + unsigned long foreground; + unsigned long background; + int line_width; + int line_style; + int cap_style; + int join_style; + int fill_style; + int fill_rule; + int arc_mode; + unsigned long tile; // Pixmap = XID + unsigned long stipple; // Pixmap = XID + int ts_x_origin; + int ts_y_origin; + unsigned long font; // Font = XID + int subwindow_mode; +}; + +// X11 GC 常量 +#define GCSubwindowMode (1L<<15) +#define IncludeInferiors 1 + +// XFixes cursor image structure (for cursor type detection) +struct XFixesCursorImage { + short x, y; + unsigned short width, height; + unsigned short xhot, yhot; + unsigned long cursor_serial; + unsigned long* pixels; + // Atom cursor_name; // Only in XFixes 2.0+ +}; + +// X11 函数指针类型 +typedef Display* (*fn_XOpenDisplay)(const char*); +typedef int (*fn_XCloseDisplay)(Display*); +typedef XImage* (*fn_XGetImage)(Display*, Drawable, int, int, unsigned int, unsigned int, unsigned long, int); +typedef int (*fn_XDestroyImage)(XImage*); +typedef int (*fn_XSetErrorHandler)(int (*)(Display*, void*)); +typedef Pixmap (*fn_XCreatePixmap)(Display*, Drawable, unsigned int, unsigned int, unsigned int); +typedef int (*fn_XFreePixmap)(Display*, Pixmap); +typedef GC (*fn_XCreateGC)(Display*, Drawable, unsigned long, void*); +typedef int (*fn_XFreeGC)(Display*, GC); +typedef int (*fn_XCopyArea)(Display*, Drawable, Drawable, GC, int, int, unsigned int, unsigned int, int, int); +typedef int (*fn_XDefaultDepth)(Display*, int); +typedef int (*fn_XSync)(Display*, int); +typedef unsigned long (*fn_XKeysymToKeycode)(Display*, unsigned long); +typedef int (*fn_XFlush)(Display*); +typedef int (*fn_XClearArea)(Display*, Window, int, int, unsigned int, unsigned int, int); +typedef int (*fn_XQueryPointer)(Display*, Window, Window*, Window*, int*, int*, int*, int*, unsigned int*); +typedef int (*fn_XFree)(void*); + +// XTest 扩展函数指针类型(用于模拟鼠标/键盘输入) +typedef int (*fn_XTestFakeMotionEvent)(Display*, int, int, int, unsigned long); +typedef int (*fn_XTestFakeButtonEvent)(Display*, unsigned int, int, unsigned long); +typedef int (*fn_XTestFakeKeyEvent)(Display*, unsigned int, int, unsigned long); + +// XFixes 扩展函数指针类型(用于光标类型检测) +typedef int (*fn_XFixesQueryExtension)(Display*, int*, int*); +typedef XFixesCursorImage* (*fn_XFixesGetCursorImage)(Display*); + +// X11 动态加载包装 +class X11Loader +{ +public: + fn_XOpenDisplay pXOpenDisplay; + fn_XCloseDisplay pXCloseDisplay; + fn_XGetImage pXGetImage; + fn_XDestroyImage pXDestroyImage; + + // Xlib 宏的替代:通过偏移读取 Display 内部结构 + // 这些函数通过 dlsym 获取 + typedef int (*fn_XDefaultScreen)(Display*); + typedef int (*fn_XDisplayWidth)(Display*, int); + typedef int (*fn_XDisplayHeight)(Display*, int); + typedef Window (*fn_XRootWindow)(Display*, int); + + fn_XDefaultScreen pXDefaultScreen; + fn_XDisplayWidth pXDisplayWidth; + fn_XDisplayHeight pXDisplayHeight; + fn_XRootWindow pXRootWindow; + + // Pixmap 相关(解决合成窗口管理器下 XGetImage BadMatch 问题) + fn_XSetErrorHandler pXSetErrorHandler; + fn_XCreatePixmap pXCreatePixmap; + fn_XFreePixmap pXFreePixmap; + fn_XCreateGC pXCreateGC; + fn_XFreeGC pXFreeGC; + fn_XCopyArea pXCopyArea; + fn_XDefaultDepth pXDefaultDepth; + fn_XSync pXSync; + fn_XKeysymToKeycode pXKeysymToKeycode; + fn_XFlush pXFlush; + fn_XClearArea pXClearArea; + fn_XQueryPointer pXQueryPointer; + fn_XFree pXFree; + + // XTest 扩展(用于模拟输入) + fn_XTestFakeMotionEvent pXTestFakeMotionEvent; + fn_XTestFakeButtonEvent pXTestFakeButtonEvent; + fn_XTestFakeKeyEvent pXTestFakeKeyEvent; + + // XFixes 扩展(用于光标类型检测) + fn_XFixesQueryExtension pXFixesQueryExtension; + fn_XFixesGetCursorImage pXFixesGetCursorImage; + + X11Loader() : m_handle(nullptr), m_xtst_handle(nullptr), m_xfixes_handle(nullptr) + { + pXOpenDisplay = nullptr; + pXCloseDisplay = nullptr; + pXGetImage = nullptr; + pXDestroyImage = nullptr; + pXDefaultScreen = nullptr; + pXDisplayWidth = nullptr; + pXDisplayHeight = nullptr; + pXRootWindow = nullptr; + pXSetErrorHandler = nullptr; + pXCreatePixmap = nullptr; + pXFreePixmap = nullptr; + pXCreateGC = nullptr; + pXFreeGC = nullptr; + pXCopyArea = nullptr; + pXDefaultDepth = nullptr; + pXSync = nullptr; + pXKeysymToKeycode = nullptr; + pXFlush = nullptr; + pXClearArea = nullptr; + pXQueryPointer = nullptr; + pXFree = nullptr; + pXTestFakeMotionEvent = nullptr; + pXTestFakeButtonEvent = nullptr; + pXTestFakeKeyEvent = nullptr; + pXFixesQueryExtension = nullptr; + pXFixesGetCursorImage = nullptr; + } + + bool Load() + { + m_handle = dlopen("libX11.so.6", RTLD_LAZY); + if (!m_handle) m_handle = dlopen("libX11.so", RTLD_LAZY); + if (!m_handle) return false; + + pXOpenDisplay = (fn_XOpenDisplay)dlsym(m_handle, "XOpenDisplay"); + pXCloseDisplay = (fn_XCloseDisplay)dlsym(m_handle, "XCloseDisplay"); + pXGetImage = (fn_XGetImage)dlsym(m_handle, "XGetImage"); + pXDestroyImage = (fn_XDestroyImage)dlsym(m_handle, "XDestroyImage"); + pXDefaultScreen = (fn_XDefaultScreen)dlsym(m_handle, "XDefaultScreen"); + pXDisplayWidth = (fn_XDisplayWidth)dlsym(m_handle, "XDisplayWidth"); + pXDisplayHeight = (fn_XDisplayHeight)dlsym(m_handle, "XDisplayHeight"); + pXRootWindow = (fn_XRootWindow)dlsym(m_handle, "XRootWindow"); + + // Pixmap 相关函数 + pXSetErrorHandler = (fn_XSetErrorHandler)dlsym(m_handle, "XSetErrorHandler"); + pXCreatePixmap = (fn_XCreatePixmap)dlsym(m_handle, "XCreatePixmap"); + pXFreePixmap = (fn_XFreePixmap)dlsym(m_handle, "XFreePixmap"); + pXCreateGC = (fn_XCreateGC)dlsym(m_handle, "XCreateGC"); + pXFreeGC = (fn_XFreeGC)dlsym(m_handle, "XFreeGC"); + pXCopyArea = (fn_XCopyArea)dlsym(m_handle, "XCopyArea"); + pXDefaultDepth = (fn_XDefaultDepth)dlsym(m_handle, "XDefaultDepth"); + pXSync = (fn_XSync)dlsym(m_handle, "XSync"); + pXKeysymToKeycode = (fn_XKeysymToKeycode)dlsym(m_handle, "XKeysymToKeycode"); + pXFlush = (fn_XFlush)dlsym(m_handle, "XFlush"); + pXClearArea = (fn_XClearArea)dlsym(m_handle, "XClearArea"); + pXQueryPointer = (fn_XQueryPointer)dlsym(m_handle, "XQueryPointer"); + pXFree = (fn_XFree)dlsym(m_handle, "XFree"); + + // 加载 XTest 扩展库(用于模拟鼠标/键盘输入) + m_xtst_handle = dlopen("libXtst.so.6", RTLD_LAZY); + if (!m_xtst_handle) m_xtst_handle = dlopen("libXtst.so", RTLD_LAZY); + if (m_xtst_handle) { + pXTestFakeMotionEvent = (fn_XTestFakeMotionEvent)dlsym(m_xtst_handle, "XTestFakeMotionEvent"); + pXTestFakeButtonEvent = (fn_XTestFakeButtonEvent)dlsym(m_xtst_handle, "XTestFakeButtonEvent"); + pXTestFakeKeyEvent = (fn_XTestFakeKeyEvent)dlsym(m_xtst_handle, "XTestFakeKeyEvent"); + } + + // 加载 XFixes 扩展库(用于光标类型检测) + m_xfixes_handle = dlopen("libXfixes.so.3", RTLD_LAZY); + if (!m_xfixes_handle) m_xfixes_handle = dlopen("libXfixes.so", RTLD_LAZY); + if (m_xfixes_handle) { + pXFixesQueryExtension = (fn_XFixesQueryExtension)dlsym(m_xfixes_handle, "XFixesQueryExtension"); + pXFixesGetCursorImage = (fn_XFixesGetCursorImage)dlsym(m_xfixes_handle, "XFixesGetCursorImage"); + } + + // 基本 X11 函数必须全部存在;XTest/XFixes 函数可选 + return pXOpenDisplay && pXCloseDisplay && pXGetImage && pXDestroyImage && + pXDefaultScreen && pXDisplayWidth && pXDisplayHeight && pXRootWindow && + pXSetErrorHandler && pXCreatePixmap && pXFreePixmap && + pXCreateGC && pXFreeGC && pXCopyArea && pXDefaultDepth && pXSync && + pXKeysymToKeycode && pXFlush; + } + + // 只关心抓屏的那一小部分函数是否齐备(ScreenPreview 用,不要求 XTest/XFixes) + bool CanCapture() const + { + return pXOpenDisplay && pXCloseDisplay && pXGetImage && pXDestroyImage && + pXDefaultScreen && pXDisplayWidth && pXDisplayHeight && pXRootWindow && + pXSetErrorHandler && pXCreatePixmap && pXFreePixmap && + pXCreateGC && pXFreeGC && pXCopyArea && pXDefaultDepth && pXSync; + } + + // 检查 XTest 扩展是否可用 + bool HasXTest() const + { + return pXTestFakeMotionEvent && pXTestFakeButtonEvent && pXTestFakeKeyEvent; + } + + // 检查 XFixes 扩展是否可用 + bool HasXFixes() const + { + return pXFixesGetCursorImage != nullptr; + } + + ~X11Loader() + { + if (m_xfixes_handle) { + dlclose(m_xfixes_handle); + m_xfixes_handle = nullptr; + } + if (m_xtst_handle) { + dlclose(m_xtst_handle); + m_xtst_handle = nullptr; + } + if (m_handle) { + dlclose(m_handle); + m_handle = nullptr; + } + } + +private: + void* m_handle; + void* m_xtst_handle; + void* m_xfixes_handle; +}; diff --git a/linux/main.cpp b/linux/main.cpp index 58755a3..4a5d59c 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -26,6 +26,7 @@ #include #include #include "ScreenHandler.h" +#include "ScreenPreview.h" #include "SystemManager.h" #include "common/FileManager.h" #include "ClipboardHandler.h" @@ -382,6 +383,53 @@ void* FileManagerThread(void* /*param*/) return NULL; } +// ============== 屏幕预览(对应 Windows 端 COMMAND_SCREEN_PREVIEW_REQ) ============== + +// 同一时刻只跑一个抓屏任务:列表缩略图是周期性刷新,服务端还可能重发/并发请求, +// 多余的直接丢弃,让服务端 4s 超时后按"预览不可用"降级(与 Windows 端限流一致)。 +static std::atomic g_previewInFlight(0); + +static void ScreenPreviewThread(ScreenPreviewReq req) +{ + // 抓屏/缩放全程可能在几十毫秒量级,跑在独立线程里,不阻塞接收线程 + struct InFlightGuard { + ~InFlightGuard() { g_previewInFlight.fetch_sub(1); } + } guard; + + std::vector pixels; + int w = 0, h = 0; + int st = ScreenPreview::Capture(req.maxWidth, pixels, w, h); + bool ok = (st == SCREEN_PREVIEW_OK) && !pixels.empty(); + if (!ok) { + pixels.clear(); + } + + uint32_t bytes = ok ? (uint32_t)pixels.size() : 0; + std::vector pkt(sizeof(ScreenPreviewRspHeader) + bytes); + ScreenPreviewRspHeader* hdr = (ScreenPreviewRspHeader*)pkt.data(); + memset(hdr, 0, sizeof(*hdr)); + hdr->token = TOKEN_SCREEN_PREVIEW_RSP; + hdr->reqId = req.reqId; + hdr->status = (uint8_t)st; + // 成功时发原始 BGRA,由服务端统一编码成 JPEG(Linux 无可靠的 JPEG 编码库); + // 失败时 format 无意义,填 JPEG 保持包头合法 + hdr->format = ok ? SCREEN_PREVIEW_FMT_BGRX : SCREEN_PREVIEW_FMT_JPEG; + hdr->width = (uint16_t)w; + hdr->height = (uint16_t)h; + hdr->bytes = bytes; + if (ok) { + memcpy(pkt.data() + sizeof(*hdr), pixels.data(), pixels.size()); + } + + // Send2Server 内部持锁,可在工作线程调用;进程退出/断线时不再回包 + if (g_pMainClient && g_pMainClient->IsConnected() && g_bExit == S_CLIENT_NORMAL) { + g_pMainClient->Send2Server(pkt.data(), (int)pkt.size()); + } + if (!ok) { + Mprintf("** Screen preview capture failed: status=%d ***\n", st); + } +} + // 信任锚已落盘/已删除:热重载 TLS 状态,必要时请求主循环重建连接。 // 只在传输方式真正变化(明文↔TLS)时重连;仅换证书(仍是 TLS)不打断现有会话, // 新锚从下一条新连接开始生效。 @@ -430,6 +478,22 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength) } else if (szBuffer[0] == COMMAND_LIST_DRIVE) { std::thread(FileManagerThread, nullptr).detach(); Mprintf("** [%p] Received 'LIST_DRIVE' command ***\n", user); + } else if (szBuffer[0] == COMMAND_SCREEN_PREVIEW_REQ) { + if (ulLength < sizeof(ScreenPreviewReq)) return TRUE; + ScreenPreviewReq req; + memcpy(&req, szBuffer, sizeof(req)); + // 抓屏可能耗几十毫秒,必须离开接收线程;超额请求直接丢弃(服务端会超时降级) + if (g_previewInFlight.fetch_add(1) >= 1) { + g_previewInFlight.fetch_sub(1); + return TRUE; + } + try { + std::thread(ScreenPreviewThread, req).detach(); + } catch (const std::exception& e) { + // 线程起不来时必须把在途计数还回去,否则后面所有预览请求都会被当成"忙"丢掉 + g_previewInFlight.fetch_sub(1); + Mprintf("** Screen preview thread failed: %s ***\n", e.what()); + } } else if (szBuffer[0] == CMD_HEARTBEAT_ACK) { if (ulLength >= 1 + sizeof(HeartbeatACK)) { HeartbeatACK* ack = (HeartbeatACK*)(szBuffer + 1); @@ -1115,6 +1179,18 @@ static void RefreshLoginTls(LOGIN_INFOR& info) info.szReserved[sizeof(info.szReserved) - 1] = '\0'; } +// 能力位(moduleVersion 的 "-%04X" 后缀)里唯一依赖运行环境的是屏幕预览: +// 只在本机当前真有可抓的 X11 桌面时才声明,无桌面的服务器不该被服务端要求截图。 +// 与 RefreshLoginTls 一样只在主线程登录前调用,每次重连都会重新判断。 +static void RefreshLoginCaps(LOGIN_INFOR& info) +{ + unsigned int caps = LOGIN_INFOR::DefaultCaps(); + if (ScreenPreview::IsAvailable()) { + caps |= CLIENT_CAP_SCREEN_PREVIEW; + } + info.SetCaps(caps); +} + // 用法: ./ghost [-d] [IP] [端口] // -d 后台守护进程模式 // 示例: ./ghost -d 192.168.1.100 6543 @@ -1296,6 +1372,8 @@ int main(int argc, char* argv[]) ClientAuth::OnNewConnection(); // 传输方式可能已被热切换(TLS 启/停),登录上报的 RES_TLS 必须反映本次连接。 RefreshLoginTls(logInfo); + // 屏幕预览能力取决于本机当前是否有可用桌面(daemon 启动时可能还没有 DISPLAY) + RefreshLoginCaps(logInfo); ClientObject->SendLoginInfo(logInfo.Speed(clock() - c)); // 新连接:把 ACK 看门狗喂到当前时间,避免循环刚进来就被误判为超时 @@ -1315,6 +1393,7 @@ int main(int argc, char* argv[]) } logInfo.szPCName[sizeof(logInfo.szPCName) - 1] = '\0'; RefreshLoginTls(logInfo); + RefreshLoginCaps(logInfo); ClientObject->SendLoginInfo(logInfo); Mprintf(">> Resent login info after group change\n"); } diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 8dadf14..8f6e4df 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -6852,15 +6852,19 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject) // (IO 线程不接触 UI;clientId 用于循环快照路由到对应窗口)。 // 不用 WPARAM 携带 ID:32 位 Windows 上 WPARAM 是 32 位,会截 64 位 clientID。 if (len < sizeof(ScreenPreviewRspHeader)) break; + // Linux 客户端回的是原始 BGRA,统一在这里转成 JPEG;畸形包直接丢弃 + std::vector previewPkt(szBuffer, szBuffer + len); + if (!NormalizeScreenPreviewPacket(previewPkt)) + break; // P2c:MCP 挂起时接管(屏幕预览 = 主连接 RPC,不能 CancelIO)。 // reqId 关联:命中 MCP 的 expectedReqId 才取走,避免误吞 MFC 预览在途响应。 uint64_t previewDevId = ContextObject->GetClientID(); - const ScreenPreviewRspHeader* previewHdr = reinterpret_cast(szBuffer); - if (McpServer().TakePreviewResponse(previewDevId, previewHdr->reqId, szBuffer, len)) + const ScreenPreviewRspHeader* previewHdr = reinterpret_cast(previewPkt.data()); + if (McpServer().TakePreviewResponse(previewDevId, previewHdr->reqId, previewPkt.data(), (ULONG)previewPkt.size())) break; auto* msg = new CMy2015RemoteDlg::PreviewRspMsg{ ContextObject->GetClientID(), - std::vector(szBuffer, szBuffer + len) + std::move(previewPkt) }; if (!g_2015RemoteDlg->PostMessageA(WM_PREVIEW_RESPONSE, 0, (LPARAM)msg)) { delete msg; @@ -9531,6 +9535,98 @@ void CMy2015RemoteDlg::ChooseScreenPreviewParams(context* ctx, WORD& maxWidth, B jpegQuality = q; } +namespace { + +// GDI+ 的 JPEG 编码器 CLSID(Windows 自带,进程内即可用;与 Bmp2Video 的同名逻辑互不影响) +bool FindJpegEncoderClsid(CLSID& clsid) +{ + UINT num = 0, size = 0; + if (Gdiplus::GetImageEncodersSize(&num, &size) != Gdiplus::Ok || size == 0) return false; + + std::vector buf(size); + Gdiplus::ImageCodecInfo* info = reinterpret_cast(buf.data()); + if (Gdiplus::GetImageEncoders(num, size, info) != Gdiplus::Ok) return false; + for (UINT i = 0; i < num; ++i) { + if (wcscmp(info[i].MimeType, L"image/jpeg") == 0) { + clsid = info[i].Clsid; + return true; + } + } + return false; +} + +} // namespace + +// 预览响应规范化:Windows 客户端直接回 JPEG,Linux 客户端没有可靠的 JPEG 编码库, +// 回的是原始 BGRA(SCREEN_PREVIEW_FMT_BGRX)。统一在 IO 线程入口转成 JPEG,下游 +// (浮窗 / 列表缩略图 / MCP get_screenshot)就只需要认识 JPEG 一种格式。 +// 返回 false 表示这个包畸形、不可用,调用方应直接丢弃。 +bool CMy2015RemoteDlg::NormalizeScreenPreviewPacket(std::vector& pkt) +{ + if (pkt.size() < sizeof(ScreenPreviewRspHeader)) return false; + ScreenPreviewRspHeader* hdr = reinterpret_cast(pkt.data()); + if (hdr->token != TOKEN_SCREEN_PREVIEW_RSP) return false; + if (hdr->format != SCREEN_PREVIEW_FMT_BGRX) return true; // JPEG / 预留格式:原样放行 + + // 抓屏失败的响应没有像素可转,原样放行,让下游按 status 报"预览不可用" + if (hdr->status != SCREEN_PREVIEW_OK) return true; + + // 尺寸与长度校验:请求侧 maxWidth 已限制在 [64,1920],高度按 16:10 上限放宽到 2160 + const int w = hdr->width, h = hdr->height; + if (w < 2 || h < 2 || w > 1920 || h > 2160) return false; + const size_t pixels = (size_t)w * h * 4; + if (hdr->bytes != pixels || pkt.size() < sizeof(*hdr) + pixels) return false; + + std::vector jpeg; + { + // Bitmap 直接引用包内像素(不复制),所以编码必须在 pkt 被 resize 之前、且 + // 在本作用域内完成析构。用栈对象:本 TU 的 `new` 被 MFC 重定义成 DEBUG_NEW, + // Gdiplus 的 operator new 不接受那种三参形式。 + Gdiplus::Bitmap bmp(w, h, w * 4, PixelFormat32bppRGB, pkt.data() + sizeof(*hdr)); + if (bmp.GetLastStatus() != Gdiplus::Ok) return false; + + CLSID clsid; + if (!FindJpegEncoderClsid(clsid)) return false; + + // Linux 端不上报质量(它不编码),这里跟随缩略图默认档 70 + Gdiplus::EncoderParameters params; + params.Count = 1; + params.Parameter[0].Guid = Gdiplus::EncoderQuality; + params.Parameter[0].Type = Gdiplus::EncoderParameterValueTypeLong; + params.Parameter[0].NumberOfValues = 1; + ULONG quality = 70; + params.Parameter[0].Value = &quality; + + IStream* stream = nullptr; + if (FAILED(::CreateStreamOnHGlobal(NULL, TRUE, &stream))) return false; + if (bmp.Save(stream, &clsid, ¶ms) == Gdiplus::Ok) { + // 取实际写入长度:GlobalSize 按堆块对齐,尾部填充字节不能算进 JPEG + LARGE_INTEGER zero = {}; + ULARGE_INTEGER end = {}, start = {}; + if (SUCCEEDED(stream->Seek(zero, STREAM_SEEK_END, &end)) && + end.QuadPart > 0 && end.QuadPart <= 16 * 1024 * 1024 && + SUCCEEDED(stream->Seek(zero, STREAM_SEEK_SET, &start))) { + jpeg.resize((size_t)end.QuadPart); + ULONG read = 0; + if (FAILED(stream->Read(jpeg.data(), (ULONG)jpeg.size(), &read)) || + read != jpeg.size()) { + jpeg.clear(); + } + } + } + stream->Release(); + } + if (jpeg.empty()) return false; + + // 用 JPEG 覆盖原像素区;宽高沿用客户端上报值(客户端就是按这两个尺寸缩放的) + pkt.resize(sizeof(*hdr) + jpeg.size()); + hdr = reinterpret_cast(pkt.data()); // resize 后重新取 + memcpy(pkt.data() + sizeof(*hdr), jpeg.data(), jpeg.size()); + hdr->format = SCREEN_PREVIEW_FMT_JPEG; + hdr->bytes = (uint32_t)jpeg.size(); + return true; +} + void CMy2015RemoteDlg::SendScreenPreviewRequest(context* ctx, WORD reqId, WORD maxWidth, BYTE jpegQuality) { if (!ctx) return; diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 0927526..ac4de37 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -621,6 +621,9 @@ public: void ChooseScreenPreviewParams(context* ctx, WORD& maxWidth, BYTE& jpegQuality) const; // 发起预览请求;reqId 应与 m_PreviewReqId 同步 void SendScreenPreviewRequest(context* ctx, WORD reqId, WORD maxWidth, BYTE jpegQuality); + // 预览响应规范化:Linux 客户端回原始 BGRA(SCREEN_PREVIEW_FMT_BGRX),在 IO 线程入口 + // 统一转成 JPEG,下游(浮窗/列表缩略图/MCP)只处理 JPEG。返回 false 表示畸形包,应丢弃。 + static bool NormalizeScreenPreviewPacket(std::vector& pkt); // 收到 TOKEN_SCREEN_PREVIEW_RSP(在主线程处理) afx_msg LRESULT OnPreviewResponse(WPARAM wParam, LPARAM lParam); afx_msg void OnOnlineUnauthorize(); diff --git a/server/2015Remote/McpServer.cpp b/server/2015Remote/McpServer.cpp index fd9bb52..e763ee7 100644 --- a/server/2015Remote/McpServer.cpp +++ b/server/2015Remote/McpServer.cpp @@ -1951,7 +1951,7 @@ std::string BuildToolsListResult(const Json::Value& id) { { Json::Value tool(Json::objectValue); tool["name"] = "get_screenshot"; - tool["description"] = u8"截取指定在线主机的一帧屏幕,返回 base64 编码的 JPEG 图片(image 对象含 mimeType/data/width/height)。仅 Windows 客户端支持(能力位 CLIENT_CAP_SCREEN_PREVIEW),主连接 RPC、一次性返回。"; + tool["description"] = u8"截取指定在线主机的一帧屏幕,返回 base64 编码的 JPEG 图片(image 对象含 mimeType/data/width/height)。支持 Windows 与带 X11 桌面的 Linux 客户端(能力位 CLIENT_CAP_SCREEN_PREVIEW),主连接 RPC、一次性返回。"; tool["inputSchema"] = BuildGetScreenshotInputSchema(); // { id } 必填 + max_width 可选 tool["outputSchema"] = BuildScreenshotOutputSchema(); @@ -2951,7 +2951,8 @@ std::string BuildGetScreenshot(const Json::Value& id, const Json::Value& args, C if (!ctx) return BuildError(id, -32002, "Host not found or offline: " + std::to_string(devId)); - // 能力位门槛:仅 Windows 客户端声明 CLIENT_CAP_SCREEN_PREVIEW(非 Windows 返回明确错误) + // 能力位门槛:只有能抓屏的客户端才声明 CLIENT_CAP_SCREEN_PREVIEW + // (Windows 恒声明;Linux 仅在登录时探测到可用 X11 桌面才声明),否则返回明确错误 if (!ctx->SupportsScreenPreview()) return BuildError(id, -32005, "Host does not support screen preview");