Feature: Add window info RPC for foreground checks and window activation

Add a capability-gated main-connection RPC (COMMAND_MCP_WINDOW_INFO=179 /
TOKEN_MCP_WINDOW_INFO=180) that returns visible top-level windows with UTF-8
titles, screen rectangles, foreground flag and z-order, so MCP can verify
focus before injecting input. New tool get_foreground_window; host_list_windows
switches to the richer fields; host_control_window and operate_window gain
action=activate; remote_keyboard and remote_mouse gain expect_hwnd,
require_foreground and report_foreground. Old clients keep the previous
behavior: the RPC is refused up front via CLIENT_CAP_WINDOW_INFO.

The client caps a response at 96 windows (about 101 KB, under MAX_SEND_BUFFER).
Signal the cut-off in the mode byte (0x80) instead of silently dropping windows:
host_list_windows now reports truncated=true and says the list is incomplete,
and activate verification reports state=unknown with reason=list_truncated
rather than claiming the window is gone. expect_hwnd is checked only after the
session is validated, so an invalid session_id no longer burns a window RPC and
no longer reports a window error for a session that does not exist. Nullable
output fields (foreground, window) are declared as ["object","null"] so strict
clients do not reject responses.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-10 15:09:24 +01:00
1 parent c7e23f750d
commit b2c0d81396
18 files changed
+1298 -68

No files matched your search

+90
View File
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "McpInternal.h"
#include "context.h" // context 接口(GetClientID/IsLogin)
#include "McpServer.h" // 窗口信息 RPC 复用主连接的挂起/等待机制
#include "2015RemoteDlg.h" // CMy2015RemoteDlg 成员(m_HostList/m_cs)+ WM_SHOWERRORMSG
#include "LangManager.h" // _TR
@@ -179,6 +180,94 @@ bool IsWindowsClient(context* ctx) {
return !(t == "LNX" || t == "MAC" || t == "APK");
}
// ===== 窗口信息 RPC =====
Json::Value WindowEntryToJson(const WindowEntry& e) {
Json::Value item(Json::objectValue);
item["hwnd"] = (Json::UInt)e.hwnd;
item["pid"] = (Json::UInt)e.pid;
item["title"] = e.title;
item["left"] = e.left;
item["top"] = e.top;
item["right"] = e.right;
item["bottom"] = e.bottom;
item["width"] = e.right - e.left;
item["height"] = e.bottom - e.top;
item["visible"] = (e.flags & WINDOWINFO_VISIBLE) != 0;
item["minimized"] = (e.flags & WINDOWINFO_MINIMIZED) != 0;
item["maximized"] = (e.flags & WINDOWINFO_MAXIMIZED) != 0;
item["isForeground"] = (e.flags & WINDOWINFO_FOREGROUND) != 0;
item["zOrder"] = e.zorder;
return item;
}
int FetchWindowInfo(context* ctx, uint64_t devId, const char* tool, int timeoutMs,
std::vector<WindowEntry>& out, std::string& errMsg, bool* truncated) {
out.clear();
errMsg.clear();
if (truncated) *truncated = false;
if (!ctx) {
errMsg = "host context unavailable";
return 2;
}
if (!ctx->SupportsWindowInfo()) {
errMsg = "host client does not support window info (old client)";
return 4;
}
CMcpServer& mcp = McpServer();
if (!mcp.BeginPending(devId, tool ? tool : "window_info")) {
errMsg = "device busy: another request is pending for this host";
return 1;
}
BYTE req[2] = { (BYTE)COMMAND_MCP_WINDOW_INFO, 0 }; // mode 0 = 可见顶层窗口
if (!ctx->Send2Client(req, sizeof(req))) {
mcp.ClearPending(devId);
errMsg = "failed to send window info request";
return 2;
}
std::vector<BYTE> data;
if (!mcp.WaitPending(devId, data, timeoutMs > 0 ? timeoutMs : mcp.ToolTimeoutMs())) {
errMsg = "timeout waiting for window info";
return 2;
}
if (data.size() < 4 || data[0] != (BYTE)TOKEN_MCP_WINDOW_INFO) {
errMsg = "invalid window info response";
return 3;
}
// 模式字节最高位 = 客户端枚举到的可见窗口超过单包上限,返回的 count 条不完整。
if (truncated) *truncated = (data[1] & 0x80) != 0;
const size_t count = (size_t)data[2] | ((size_t)data[3] << 8);
if (data.size() < 4 + count * sizeof(WINDOWINFO_MCP)) {
errMsg = "truncated window info response";
return 3;
}
out.reserve(count);
for (size_t i = 0; i < count; ++i) {
WINDOWINFO_MCP rec;
memcpy(&rec, data.data() + 4 + i * sizeof(WINDOWINFO_MCP), sizeof(rec));
WindowEntry e;
e.hwnd = rec.hwnd;
e.pid = rec.pid;
e.left = rec.left;
e.top = rec.top;
e.right = rec.right;
e.bottom = rec.bottom;
e.flags = rec.flags;
e.zorder = rec.zorder;
char title[sizeof(rec.title) + 1] = { 0 };
memcpy(title, rec.title, sizeof(rec.title)); // 客户端保证 NUL 填充;再兜底截断
e.title = title;
out.push_back(std::move(e));
}
return 0;
}
size_t BoundedStrlen(const char* p, size_t avail) {
size_t n = 0;
while (n < avail && p[n]) ++n;
@@ -397,6 +486,7 @@ const ToolHints kToolHints[] = {
// 已落盘的记录),故按「可写、可破坏」登记。
{ "get_keylog", false, true, false, true },
{ "get_screenshot", true, false, true, true },
{ "get_foreground_window", true, false, true, true },
{ "list_registry", true, false, true, true },
{ "download_file", true, false, true, true }, // 只读主机(写的是主控本机目录)
{ "search_file", true, false, true, true },