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): how Home and locking work on each device is kept across restarts (#107)

Qinghe Lib27d8f6c831a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a0866f6..b0339ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ All notable changes to this project are documented here. The format is based on
## [Unreleased]
+### Fixed
+
+- The way Home and locking work on each device is kept in `state.json` instead of only in memory. After the daemon restarted, or another device was controlled meanwhile, the first Home on a device where IOHID's Home doesn't work tried it, waited 2 s for the home screen and pressed again: 4.4 s instead of 0.8 on an iPad mini 5.
+
## [0.2.6] - 2026-11-14
### Added
diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md
index 42ca53b..7d9798e 100644
--- a/CHANGELOG.zh-CN.md
+++ b/CHANGELOG.zh-CN.md
@@ -6,6 +6,10 @@
## [Unreleased]
+### 修复
+
+- 每台设备按 Home 和锁屏该用哪种方式,现在记在 `state.json` 里,不再只放在内存。原来守护进程重启或中途控制过别的设备后,在 IOHID 的 Home 不起作用的设备上,第一次按 Home 会先用 IOHID 按,等 2 秒没回到主屏幕再补按一次:iPad mini 5 上要 4.4 秒,平时 0.8 秒。
+
## [0.2.6] - 2026-11-14
### 新增
diff --git a/crates/ictl-daemon/src/core.rs b/crates/ictl-daemon/src/core.rs
index 71d79c0..4281bb3 100644
--- a/crates/ictl-daemon/src/core.rs
+++ b/crates/ictl-daemon/src/core.rs
@@ -66,6 +66,27 @@ pub struct Persisted {
/// 不重新启动;关掉实验开关后据此结束它(不然它一直占着 8100 端口,新 runner 的 HTTP 服务起不来)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub detached_wda: Option<DetachedWda>,
+ /// Which way Home and locking work on each device, by UDID ([`Self::ways_for`]). Kept here, not only in memory:
+ /// after a restart or switching devices the first Home on an iPad mini 5 tried the way that doesn't work there,
+ /// waited 2 s for the home screen, then pressed again (4.4 s instead of 0.8)
+ #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
+ pub button_ways: std::collections::BTreeMap<String, ButtonWays>,
+}
+
+/// Which way the buttons work on a device, learned when one way didn't and the other did. Unknown is `false` both
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ButtonWays {
+ /// Home through XCTest first: IOHID's didn't get to the home screen and XCTest's did (IOHID's Home hardly ever works
+ /// on an iPhone 18 Pro Max / iOS 27.2.1, nor from an app on an iPad mini 5 / iPadOS 26.7.3; an iPhone 12 mini is the
+ /// other way round). See `tools::keys::press_home`
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub home_via_xctest: bool,
+ /// Lock with WDA's `/wda/lock` first: the power key through IOHID didn't lock and WDA did. On an iPhone 15 Pro Max
+ /// (iOS 27.0.1) the power key doesn't work (the call hangs about 5 s, then 3 s more before falling back, about 10 s
+ /// a lock), `/wda/lock` takes about 2 s; an iPhone 18 Pro Max (iOS 27.2.1) is the other way round. See
+ /// `tools::keys::lock_order`
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub lock_via_wda: bool,
}
/// 后台启动的 WDA 进程
@@ -113,6 +134,26 @@ impl Persisted {
changed
}
+ /// How the buttons work on `udid` (`None`: no device), as learned so far
+ pub fn ways_for(&self, udid: Option<&str>) -> ButtonWays {
+ udid.and_then(|u| self.button_ways.iter().find(|(k, _)| k.eq_ignore_ascii_case(u)).map(|(_, w)| *w))
+ .unwrap_or_default()
+ }
+
+ /// Changes what is known of `udid`'s buttons with `f`; whether anything changed (then save)
+ pub fn record_ways(&mut self, udid: &str, f: impl FnOnce(&mut ButtonWays)) -> bool {
+ let key =
+ self.button_ways.keys().find(|k| k.eq_ignore_ascii_case(udid)).cloned().unwrap_or_else(|| udid.to_owned());
+ let before = self.button_ways.get(&key).copied().unwrap_or_default();
+ let mut now = before;
+ f(&mut now);
+ if now == before {
+ return false;
+ }
+ self.button_ways.insert(key, now);
+ true
+ }
+
/// 记下这次重签的结果:新字段,加上旧版本认的中文整句
pub fn record_resign(&mut self, at: u64, error: Option<String>) {
let when = crate::eventlog::format_utc(at);
@@ -318,13 +359,6 @@ pub(crate) struct PerDevice {
pub ddi_failures: u32,
/// 最近一次经本进程锁屏的时间(自动解锁要等锁屏过渡走完,见 [`crate::tools::LOCK_SETTLE`])
pub last_lock: Option<Instant>,
- /// 这台手机上 Home 先用 XCTest 的:上一次 IOHID 的没回到主屏、XCTest 的回到了(iPhone 18 Pro Max /
- /// iOS 27.2.1 上 IOHID 的 Home 基本不起作用)。iPhone 12 mini 正相反,所以按设备记,见 `tools::keys::press_home`
- pub home_via_xctest: bool,
- /// 这台手机上锁屏先用 WDA 的 `/wda/lock`:上一次经 IOHID 短按电源键没锁上、WDA 的锁上了。iPhone 15 Pro Max
- /// (iOS 27.0.1)上短按电源键不起作用(调用卡约 5 秒,再等 3 秒才退回,一次锁屏约 10 秒),`/wda/lock` 约 2 秒;
- /// iPhone 18 Pro Max(iOS 27.2.1)正相反,所以按设备记,见 `tools::keys::lock_order`
- pub lock_via_wda: bool,
/// The orientation of the finger the web viewer is moving (`Request::Touch`), worked out when it touched down
pub touch_rotation: Option<Rotation>,
pub apps: HashMap<bool, Vec<AppInfo>>,
diff --git a/crates/ictl-daemon/src/tools/keys.rs b/crates/ictl-daemon/src/tools/keys.rs
index 1493ba9..54b127d 100644
--- a/crates/ictl-daemon/src/tools/keys.rs
+++ b/crates/ictl-daemon/src/tools/keys.rs
@@ -22,7 +22,7 @@ impl Daemon {
Button::VolumeDown => "volumeDown",
};
self.ensure_unlocked(g).await?;
- let xctest_first = g.dev.home_via_xctest;
+ let xctest_first = ways(g).home_via_xctest;
if let Some(w) = self.wda(g) {
// Home 在系统弹窗(定位、通知权限等)挡着时不生效,WDA 却照样返回成功。弹窗属于 SpringBoard,
// 看前台 App 发现不了,要单独查;按之前查(App 静止时约 0.4 秒,按下后动画中查要好几秒)
@@ -59,7 +59,7 @@ impl Daemon {
));
}
if let Some(via_xctest) = home_worked {
- g.dev.home_via_xctest = via_xctest;
+ self.learn(g, |w| w.home_via_xctest = via_xctest);
}
match r {
Ok(text) => return Ok(text),
@@ -72,7 +72,7 @@ impl Daemon {
}
pub(super) async fn lock_screen(&self, g: &mut Inner) -> crate::error::Result<String> {
- let via_wda = g.dev.lock_via_wda;
+ let via_wda = ways(g).lock_via_wda;
// Ok(way):用 way 锁上了;Err:WDA 两种办法没锁上的原因(WDA 不可用时为空),退到 HID
let outcome = match self.wda(g) {
Some(w) => {
@@ -122,19 +122,32 @@ impl Daemon {
)
});
}
- g.dev.lock_via_wda = wda;
+ self.learn(g, |w| w.lock_via_wda = wda);
return self.locked_now(g);
}
self.hid_do(g, |h| Box::pin(h.press(Button::Lock))).await?;
self.locked_now(g)
}
+ /// Records how a button works on the current device, in `state.json` ([`crate::core::Persisted::button_ways`])
+ fn learn(&self, g: &mut Inner, f: impl FnOnce(&mut crate::core::ButtonWays)) {
+ let Some(udid) = g.device.as_ref().map(|d| d.udid.clone()) else { return };
+ if g.persisted.record_ways(&udid, f) {
+ g.persisted.save(&self.cfg.state_file());
+ }
+ }
+
pub(super) fn locked_now(&self, g: &mut Inner) -> crate::error::Result<String> {
g.dev.last_lock = Some(Instant::now());
Ok(ictl_l10n::tr!("已锁屏", "Locked").into())
}
}
+/// What is known of the current device's buttons ([`crate::core::Persisted::ways_for`])
+fn ways(g: &Inner) -> crate::core::ButtonWays {
+ g.persisted.ways_for(g.device.as_ref().map(|d| d.udid.as_str()))
+}
+
/// 按 Home,等主屏真的到前台再说「已按」(紧接着查状态时前台不再是旧 App)。
/// 两种按法各有设备不认:XCTest 的 home 在 iPhone 12 mini / iOS 26.4.1 上不起作用却报成功;IOHID 的 AC Home
/// 在 iPhone 18 Pro Max / iOS 27.2.1 上基本不起作用(按住 0.05~0.5 秒都一样)。所以默认先 IOHID,
@@ -205,7 +218,7 @@ pub(super) enum LockWay {
}
/// 先试哪种:默认先经 IOHID 短按电源键(iPhone 18 Pro Max 上 `/wda/lock` 按住 0.5 秒不认,很慢),
-/// 这台设备上记过 `lock_via_wda` 就先用 WDA 的(见 [`crate::core::PerDevice::lock_via_wda`])。
+/// 这台设备上记过 `lock_via_wda` 就先用 WDA 的(见 [`crate::core::ButtonWays::lock_via_wda`])。
/// 先试的那种不灵,另一种照样会试,所以记错了(比如系统升级后)也只是慢一次
pub(super) fn lock_order(via_wda: bool) -> [LockWay; 2] {
if via_wda { [LockWay::Wda, LockWay::PowerKey] } else { [LockWay::PowerKey, LockWay::Wda] }
diff --git a/crates/ictl-daemon/src/tools/mod.rs b/crates/ictl-daemon/src/tools/mod.rs
index 3d46244..f89368e 100644
--- a/crates/ictl-daemon/src/tools/mod.rs
+++ b/crates/ictl-daemon/src/tools/mod.rs
@@ -376,7 +376,18 @@ mod tests {
fn lock_order_follows_what_worked_on_this_device() {
assert_eq!(lock_order(false), [LockWay::PowerKey, LockWay::Wda]);
assert_eq!(lock_order(true), [LockWay::Wda, LockWay::PowerKey]);
- assert!(!crate::core::PerDevice::default().lock_via_wda, "a new device starts with the power key");
+ let mut p = crate::core::Persisted::default();
+ assert!(!p.ways_for(Some("A")).lock_via_wda, "a new device starts with the power key");
+ assert!(p.record_ways("A", |w| w.lock_via_wda = true));
+ assert!(!p.record_ways("a", |w| w.lock_via_wda = true), "the same device, nothing new");
+ assert!(
+ p.ways_for(Some("a")).lock_via_wda && !p.ways_for(Some("B")).lock_via_wda && !p.ways_for(None).lock_via_wda
+ );
+ // kept in state.json; an older one without it reads as nothing known
+ let back: crate::core::Persisted = serde_json::from_str(&serde_json::to_string(&p).unwrap()).unwrap();
+ assert_eq!(back.ways_for(Some("A")), p.ways_for(Some("A")));
+ let old: crate::core::Persisted = serde_json::from_str(r#"{"team_id":null,"selected_udid":null}"#).unwrap();
+ assert!(old.button_ways.is_empty());
}
#[tokio::test]