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

+28
View File
@@ -14,6 +14,7 @@
#include <string>
#include <cstdint>
#include <vector>
class CMy2015RemoteDlg;
class context;
@@ -79,6 +80,33 @@ bool ParseHostIdArg(const Json::Value& args, uint64_t& out, std::string& err);
// 在 m_HostList 中按 clientID 找在线主 context(锁内遍历 m_cs)。
context* FindMainContext(CMy2015RemoteDlg* parent, uint64_t id);
// ===== 窗口信息 RPC(CLIENT_CAP_WINDOW_INFO)=====
// 一条窗口记录(WINDOWINFO_MCP 的解码结果;标题已转 UTF-8)。
struct WindowEntry {
uint32_t hwnd = 0;
uint32_t pid = 0;
int32_t left = 0;
int32_t top = 0;
int32_t right = 0;
int32_t bottom = 0;
uint32_t flags = 0; // WINDOWINFO_*
int32_t zorder = -1; // 0 = 最顶层
std::string title; // UTF-8
};
// WindowEntry → 输出 JSON(hwnd/pid/title/left/top/right/bottom/width/height/visible/
// minimized/maximized/isForeground/zOrder)。机器管理与远程控制共用同一字段口径。
Json::Value WindowEntryToJson(const WindowEntry& e);
// 下发 COMMAND_MCP_WINDOW_INFO(主连接 RPC,不建子连接、不弹框),解析 TOKEN_MCP_WINDOW_INFO。
// timeoutMs <= 0 时取 McpServer().ToolTimeoutMs()。out 按客户端的 z 序返回。
// 返回:0=ok;1=设备忙;2=发送失败/超时;3=响应畸形;4=客户端未声明 CLIENT_CAP_WINDOW_INFO。
// truncated(可选):true 表示客户端还有更多可见窗口没装进这个包(单包上限 96 条)。
int FetchWindowInfo(context* ctx, uint64_t devId, const char* tool, int timeoutMs,
std::vector<WindowEntry>& out, std::string& errMsg,
bool* truncated = nullptr);
// 客户端是否为 Windows 形态(EXE/DLL/MEXE/SC/MDLL);LNX / MAC / APK 返回 false。
// 用于「只有 Windows 客户端实现了这个能力」的判断(注册表、窗口、服务等)。
bool IsWindowsClient(context* ctx);