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
This commit is contained in:
1 parent
5bff9df3fc
commit
dc4fd792f6
12 files changed
+704
-276
No files matched your search
@@ -26,6 +26,7 @@
|
||||
#include <map>
|
||||
#include <cmath>
|
||||
#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<int> g_previewInFlight(0);
|
||||
|
||||
static void ScreenPreviewThread(ScreenPreviewReq req)
|
||||
{
|
||||
// 抓屏/缩放全程可能在几十毫秒量级,跑在独立线程里,不阻塞接收线程
|
||||
struct InFlightGuard {
|
||||
~InFlightGuard() { g_previewInFlight.fetch_sub(1); }
|
||||
} guard;
|
||||
|
||||
std::vector<unsigned char> 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<char> 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");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user