Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

fix(daemon): remember every device's screen size (#86)

Qinghe Lib2c119b2cdec
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2ae5e35..d272d8d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@ All notable changes to this project are documented here. The format is based on
### Fixed
+- With several devices, every device's screen size is remembered: only the last one was, so after switching to another device and back, until WDA came up (say, stuck at the "Enable UI Automation" passcode prompt) the iOS 27 HID fallback didn't know the size, and screenshots, keys and taps failed with "the screen size is unknown".
- Notification titles name the device: they always said "iPhone", also when an iPad needed its passcode, went offline or failed to re-sign, and with several devices you couldn't tell which. On an iPad the prompt is quoted as iOS shows it there ("Enter iPad passcode for XCTest"). `notify-test` samples use the controlled device.
- Checking a new version during an update (running its `--version`) could fail now and then with "Text file busy": another thread started a process while the new program was still open for writing. It now waits a moment and tries again.
- `task run`: a command that ended its task with `task finish` is no longer stopped on its way out with exit code 1. It now has 5 seconds to exit with its own code; if it is stopped after that, the exit code is 0 when the task completed.
diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md
index 3e2bc80..f901451 100644
--- a/CHANGELOG.zh-CN.md
+++ b/CHANGELOG.zh-CN.md
@@ -36,6 +36,7 @@
### 修复
+- 接着多台设备时,每台的屏幕尺寸都记下来:之前只记最后一台,切到另一台再切回来后,WDA 起来之前(比如卡在「Enable UI Automation」密码框)iOS 27 的 HID 后备路线不知道这台的尺寸,截图、按键、点击都报「不知道屏幕尺寸」。
- 通知标题写设备自己的名字:之前一律写「iPhone」,iPad 需要输入锁屏密码、离线、重签失败时也是,接着多台设备时看不出是哪一台。iPad 上的提示也改用系统弹窗的说法(「为 XCTest 输入 iPad 密码」)。`notify-test` 的样例用正在控制的设备。
- 升级时检查新程序(运行它的 `--version`)偶尔会因为「Text file busy」失败:同一进程的其他线程恰好在新程序还以写方式打开时启动了子进程。现在遇到这种情况会稍等重试。
- `task run`:命令自己用 `task finish` 结束任务后,不再在它正常退出时被立刻停掉、退出码变成 1。现在它有 5 秒自己退出,退出码就是它自己的;超时才停掉,这时任务完成则退出码为 0。
diff --git a/apps/iphone-control/src/viewer.rs b/apps/iphone-control/src/viewer.rs
index 943ad98..885f4b7 100644
--- a/apps/iphone-control/src/viewer.rs
+++ b/apps/iphone-control/src/viewer.rs
@@ -1403,8 +1403,7 @@ fn parse_element(line: &str) -> Option<Value> {
/// The recorded screen size, only if it belongs to `udid` (a size recorded by an older version, without
/// a device, is used as is, like the daemon does)
fn screen_of(p: &ictl_daemon::core::Persisted, udid: &str) -> Option<ictl_device::ScreenSize> {
- let same = p.screen_udid.as_deref().is_none_or(|u| u.eq_ignore_ascii_case(udid));
- p.screen.filter(|_| same).map(|s| s.portrait())
+ p.screen_for(Some(udid)).map(|s| s.portrait())
}
#[cfg(test)]
@@ -1919,6 +1918,32 @@ mod tests {
assert!(screen_of(&p, "BBB").is_some(), "older state without a device");
}
+ /// Switching to another device and back: the first one's size is still known (the HID fallback needs it while
+ /// its WDA is down)
+ #[test]
+ fn every_device_keeps_its_screen_size() {
+ let (phone, pad) = (
+ ictl_device::ScreenSize { width: 430.0, height: 932.0 },
+ ictl_device::ScreenSize { width: 768.0, height: 1024.0 },
+ );
+ let mut p = ictl_daemon::core::Persisted::default();
+ assert!(p.record_screen("PHONE", phone));
+ assert!(!p.record_screen("PHONE", phone), "nothing new: not saved again");
+ assert!(p.record_screen("PAD", pad));
+ assert_eq!(p.screen_for(Some("phone")), Some(phone), "after the switch, the phone's own");
+ assert_eq!(p.screen_for(Some("PAD")), Some(pad));
+ assert_eq!(p.screen_udid.as_deref(), Some("PAD"), "the last one stays where older versions look");
+ assert_eq!(p.screen_for(Some("OTHER")), None);
+ assert_eq!(p.screen_for(None), None);
+ // state.json from 0.2.3 (no `screens`) still reads, and an older version reads ours
+ let old: ictl_daemon::core::Persisted =
+ serde_json::from_str(r#"{"screen":{"width":430.0,"height":932.0},"screen_udid":"PHONE"}"#).unwrap();
+ assert_eq!(old.screen_for(Some("PHONE")), Some(phone));
+ let json = serde_json::to_value(&p).unwrap();
+ assert_eq!(json["screen_udid"], "PAD");
+ assert_eq!(json["screens"]["PHONE"]["width"], 430.0);
+ }
+
#[test]
fn parses_request_head_and_query() {
let h = head("GET /stream?t=1 HTTP/1.1\r\nHost: localhost:8200\r\n\r\n");
diff --git a/crates/ictl-daemon/src/core.rs b/crates/ictl-daemon/src/core.rs
index 77738dc..daab408 100644
--- a/crates/ictl-daemon/src/core.rs
+++ b/crates/ictl-daemon/src/core.rs
@@ -47,8 +47,12 @@ pub struct Persisted {
pub profile_expires: Option<u64>,
/// 设备竖放时的屏幕尺寸(WDA 不可用时 HID 换算坐标用;旧版本可能存的是横屏尺寸,用的时候都先转成竖放)
pub screen: Option<ScreenSize>,
- /// `screen` 是哪台设备的;换了设备就不能拿来给 HID 换算坐标
+ /// `screen` 是哪台设备的;换了设备就不能拿来给 HID 换算坐标。也是上次就绪的那台(自动选设备时先接着用它)
pub screen_udid: Option<String>,
+ /// Every device's screen size, by UDID: `screen` only holds the last one, so after switching devices and back
+ /// the HID fallback didn't know the first one's size until its WDA came up again ([`Self::screen_for`])
+ #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
+ pub screens: std::collections::BTreeMap<String, ScreenSize>,
/// 上次重签的结果,旧格式:拼好的中文整句。照旧写,旧版本(降级、旧 CLI)还读它;新版本显示时用
/// [`Self::resign_result`]
pub last_resign: Option<String>,
@@ -82,6 +86,33 @@ pub struct ResignRecord {
}
impl Persisted {
+ /// The recorded screen size of `udid` (`None`: no device): its own entry, else `screen` if it is this device's
+ /// (or from an older version that didn't say whose)
+ pub fn screen_for(&self, udid: Option<&str>) -> Option<ScreenSize> {
+ if let Some(u) = udid
+ && let Some(s) = self.screens.iter().find(|(k, _)| k.eq_ignore_ascii_case(u)).map(|(_, s)| *s)
+ {
+ return Some(s);
+ }
+ let same = match (&self.screen_udid, udid) {
+ (Some(s), Some(u)) => s.eq_ignore_ascii_case(u),
+ (Some(_), None) => false,
+ (None, _) => true,
+ };
+ self.screen.filter(|_| same)
+ }
+
+ /// Records `udid`'s screen size; whether anything changed (then save)
+ pub fn record_screen(&mut self, udid: &str, size: ScreenSize) -> bool {
+ let changed = self.screen != Some(size)
+ || self.screen_udid.as_deref() != Some(udid)
+ || self.screens.get(udid) != Some(&size);
+ self.screen = Some(size);
+ self.screen_udid = Some(udid.to_owned());
+ self.screens.insert(udid.to_owned(), size);
+ changed
+ }
+
/// 记下这次重签的结果:新字段,加上旧版本认的中文整句
pub fn record_resign(&mut self, at: u64, error: Option<String>) {
let when = crate::eventlog::format_utc(at);
diff --git a/crates/ictl-daemon/src/supervise.rs b/crates/ictl-daemon/src/supervise.rs
index a7f910f..81ae380 100644
--- a/crates/ictl-daemon/src/supervise.rs
+++ b/crates/ictl-daemon/src/supervise.rs
@@ -184,12 +184,11 @@ impl Daemon {
g.dev.last_rotation = Some((Instant::now(), r));
}
// Other clients use the last known size for HID even on WDA versions without /rotation.
- if let (Some(size), Some(device)) = (size, &g.device)
- && (g.persisted.screen != Some(size) || g.persisted.screen_udid.as_deref() != Some(&device.udid))
- {
- g.persisted.screen = Some(size);
- g.persisted.screen_udid = Some(device.udid.clone());
- g.persisted.save(&self.cfg.state_file());
+ if let (Some(size), Some(device)) = (size, &g.device) {
+ let udid = device.udid.clone();
+ if g.persisted.record_screen(&udid, size) {
+ g.persisted.save(&self.cfg.state_file());
+ }
}
let sample = size.zip(rotation);
if sample.is_none()
diff --git a/crates/ictl-daemon/src/tools.rs b/crates/ictl-daemon/src/tools.rs
index 0906a54..6122879 100644
--- a/crates/ictl-daemon/src/tools.rs
+++ b/crates/ictl-daemon/src/tools.rs
@@ -68,15 +68,9 @@ impl Daemon {
{
return Ok(s);
}
- // 记下的尺寸是别的设备的就不用(旧版本没记是哪台,照用)
- let same = match (&g.persisted.screen_udid, &g.device) {
- (Some(u), Some(d)) => *u == d.udid,
- (Some(_), None) => false,
- (None, _) => true,
- };
+ // 这台设备记下的尺寸(别的设备的不用;旧版本没记是哪台,照用)
g.persisted
- .screen
- .filter(|_| same)
+ .screen_for(g.device.as_ref().map(|d| d.udid.as_str()))
.or(self.cfg.screen_fallback)
.ok_or_else(|| ictl_l10n::tr!("WDA 不可用,也不知道屏幕尺寸;等 WDA 起来一次,或在 ~/.config/iphone-control/env 里设 SCREEN_W/SCREEN_H", "WDA is unavailable and the screen size is unknown; wait for WDA to start once, or set SCREEN_W/SCREEN_H in ~/.config/iphone-control/env").into())
}
diff --git a/docs/dev/regression-cases.md b/docs/dev/regression-cases.md
index 6b21a67..49e06e9 100644
--- a/docs/dev/regression-cases.md
+++ b/docs/dev/regression-cases.md
@@ -127,3 +127,4 @@
| 121 | 任务事件流(SSE)的一块数据断在中文字符中间时,先整体转成文字会把这个字变成乱码 | 在字节里找事件边界,只转换完整的事件 | 单测 `remote::reads_server_sent_events` |
| 122 | 升级时运行新程序的 `--version` 偶尔失败:`Text file busy`(同一进程的其他线程 fork 时,新程序还以写方式打开) | 遇到 `ETXTBSY` 等 50 毫秒重试,最多 20 次 | 单测 `the_candidate_runs_once_it_is_no_longer_open_for_writing`(修复前稳定复现) |
| 123 | iPad(iPadOS 26)重启后要输入密码启用 UI 自动化时,通知写的是「iPhone 需要输入锁屏密码」;所有通知标题都写死「iPhone」,多台设备时看不出是哪一台 | 标题用设备名(没有名字时用型号类别),密码提示按设备类别写系统弹窗的说法;离线通知用离线前记下的名字 | 单测 `a_notification_names_its_device`;真机 `notify-test pin`(控制 iPad,经本地假 Bark 截到标题) |
+| 124 | 切到另一台设备再切回 iPhone(iOS 27)后,WDA 卡在「Enable UI Automation」密码框期间,HID 后备路线的截图、按键都报「WDA 不可用,也不知道屏幕尺寸」:state.json 只记最后一台的尺寸 | 每台设备的尺寸按 UDID 记下(`screens`),`screen`/`screen_udid` 照旧写给旧版本 | 单测 `every_device_keeps_its_screen_size`;真机:iPhone 和 iPad 来回切换后,iPhone 的 WDA 还没起来时截图、按 Home |