#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; }