Files
SimpleRemoter/linux/X11Loader.h
T
yuanyuanxianganddeepseek-flash 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
2026-10-29 18:12:15 +01:00

280 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
// X11 运行时加载(编译期不依赖 X11 开发包,运行时 dlopen libX11.so)
//
// 从 ScreenHandler.h 抽出:远程桌面(ScreenHandler)和屏幕预览(ScreenPreview)
// 都要抓屏,共用这套类型定义与加载器,避免两份 dlsym 表各自漂移。
#include <dlfcn.h>
// X11 类型前向声明(避免 #include <X11/Xlib.h>)
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;
};