8b92f32c472bac4721c9c1cdec9bd02cb0d11235
1
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
dc4fd792f6 |
Feature: Implement the Linux screen preview response
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 |