Feat(client): complete the macOS feature set (phase 0 and 1)

Phase 0 makes implementations that already existed reachable. macOS now
declares TERMINAL, SCREEN_CLIPBOARD, PROCESS and C2C_RECV_ACK; PROCESS is a
new bit and Linux declares it too. SystemManager was dead code -- its
constructor took a second argument and it did not derive IOCPManager, so the
sub-connection skeleton could never instantiate it -- and now matches the
createHandler/OnReceive contract, so COMMAND_SYSTEM really spawns a
sub-connection (proc_listpids, SIGKILL). DataProcess always gets a NULL user,
so main.mm keeps a g_pMainClient handle and the V2 C2C receiver finally
answers TOKEN_C2C_RECV_RESULT. The server asks for SupportsTerminal() on MAC
for exec_command / terminal_* and for SupportsProcess() for kill_process, so
an old build gets "client is too old" instead of a command nobody answers;
kToolPlatforms and mcp/tools.json follow. http_probe stays win/lnx: the
terminal works on macOS, but the probe uses GNU-only base64 -w.

Phase 1 is POSIX parity. ScreenPreview captures through CGDisplayCreateImage,
scales and ships BGRX for the server to encode, and the capability is only
declared when preflight or a real capture says the display is readable.
ActivityHistory samples the frontmost layer-0 window once a second and prints
the same records as Windows and Linux. COMMAND_SCREEN_KEYFRAME_REQ is now
consumed by Linux (x264 IDR) and macOS (VideoToolbox force keyframe): the
receive thread raises a flag, the capture thread -- which owns the encoder --
consumes it and puts it back if the frame never went out. The macOS quality
level persists in NSUserDefaults because main.mm rewrites config.conf.

Linux client and Debug x64 server rebuild green; check_platform_table PASS.
Not built or run on a Mac.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-14 13:30:50 +01:00
1 parent c2b59f5d4a
commit 06b93f7bdb
19 files changed
+890 -121

No files matched your search

+52 -3
View File
@@ -17,6 +17,22 @@
// Global client ID (calculated in main.mm)
extern uint64_t g_myClientID;
// 质量等级持久化:服务端下发 CMD_QUALITY_LEVEL 时带 persist 位,就是「下次开机还这样」。
// 存 NSUserDefaults 而不是 ~/.config/ghost/config.conf——后者由 main.mm 用内存 map 整体
// 重写(分组变更会触发),单独往同一个文件写键会被下一次写盘冲掉。
static NSString* const kQualityLevelKey = @"GhostQualityLevel";
// 读上次保存的等级。范围外的值(-2 关闭 / -1 自适应 / 越界)保留原值返回,
// 由 applyQualityLevel 决定怎么处理:只有 [0, QUALITY_COUNT) 才会套用配置表。
static int8_t loadPersistedQualityLevel()
{
NSNumber* saved = [[NSUserDefaults standardUserDefaults] objectForKey:kQualityLevelKey];
if (!saved) return (int8_t)QUALITY_GOOD;
int level = [saved intValue];
if (level < QUALITY_DISABLED || level >= QUALITY_COUNT) return (int8_t)QUALITY_GOOD;
return (int8_t)level;
}
ScreenHandler::ScreenHandler(IOCPClient* client)
: m_client(client)
, m_clientID(0)
@@ -45,6 +61,10 @@ ScreenHandler::ScreenHandler(IOCPClient* client)
// Cache color space (avoid per-frame creation)
m_colorSpace = CGColorSpaceCreateDeviceRGB();
// 上次保存的质量等级(服务端下发的 CMD_QUALITY_LEVEL 带 persist 位时写入)。
// 放在构造末尾按普通调用应用一次:此时 m_width==0,尺寸相关分支天然不触发。
applyQualityLevel(loadPersistedQualityLevel(), false);
// Initialize input handler for mouse/keyboard control
m_inputHandler = std::make_unique<InputHandler>();
if (m_inputHandler->init()) {
@@ -498,6 +518,12 @@ void ScreenHandler::OnReceive(uint8_t* data, ULONG size)
}
break;
case COMMAND_SCREEN_KEYFRAME_REQ:
// Web 屏幕墙恢复/新加入者请求:下一帧强制 IDR,立即重建参考链。
// 这里只置标志,由采集线程在下一帧消费(见 sendH264Frame 的调用点)。
m_keyframeRequested.store(true);
break;
case COMMAND_SCREEN_SET_CLIPBOARD:
// 服务端设置剪贴板: [cmd:1][text:N]
if (size > 1) {
@@ -626,8 +652,13 @@ void ScreenHandler::applyQualityLevel(int8_t level, bool persist)
{
m_qualityLevel = level;
// TODO: persist to config file if needed
(void)persist;
// 持久化(服务端 CMD_QUALITY_LEVEL 的第 3 字节)。下次启动构造时读回并应用,
// 与 Linux 端把 QualityLevel 写进 config 的语义一致。
if (persist) {
[[NSUserDefaults standardUserDefaults] setInteger:(NSInteger)level forKey:kQualityLevelKey];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@">>> Quality saved: level=%d", (int)level);
}
if (level == QUALITY_DISABLED) {
// Disabled mode: keep current settings
@@ -834,6 +865,18 @@ void ScreenHandler::sendDiffFrame()
void ScreenHandler::sendH264Frame(bool keyframe)
{
// 强制 IDR 请求在调用点已经 exchange 掉,只有真的把这一帧编出来才算消费成功。
// 中途任何提前返回(抓屏失败 / 没连接 / 编码器开不起来 / 编码失败)都把它还回去,
// 否则 Web 端要等编码器下一次自然 IDR(最长一个 GOP)才能解出画面。
struct RearmOnFailure {
std::atomic<bool>& flag;
bool force;
bool consumed;
RearmOnFailure(std::atomic<bool>& f, bool force_)
: flag(f), force(force_), consumed(false) {}
~RearmOnFailure() { if (force && !consumed) flag.store(true); }
} rearm(m_keyframeRequested, keyframe);
if (!captureScreen(m_currFrame)) return;
if (!m_client) return;
@@ -900,6 +943,7 @@ void ScreenHandler::sendH264Frame(bool keyframe)
memcpy(&packet[headerSize], encodedData, encodedSize);
m_client->Send2Server((char*)packet.data(), packet.size());
rearm.consumed = true;
}
uint32_t ScreenHandler::compareBitmap(const uint8_t* curr, const uint8_t* prev,
@@ -1214,8 +1258,12 @@ void ScreenHandler::captureLoop()
currentAlgo = algo;
if (algo == ALGORITHM_H264) {
// 这一帧本身就是关键帧:挂起的强制 IDR 请求顺手清掉,别下一帧再打一次
m_keyframeRequested.store(false);
sendH264Frame(true); // First H264 frame is keyframe
} else {
// 非 H264 时关键帧请求没有意义,顺手清掉,免得切回 H264 时白打一次 IDR
m_keyframeRequested.store(false);
if (m_h264Encoder) {
m_h264Encoder->close();
m_h264Encoder.reset();
@@ -1224,7 +1272,8 @@ void ScreenHandler::captureLoop()
}
} else {
if (algo == ALGORITHM_H264) {
sendH264Frame(false);
// 服务端的强制 IDR 请求在这里消费:下一帧就是关键帧。
sendH264Frame(m_keyframeRequested.exchange(false));
} else {
sendDiffFrame();
}