Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools
Rust ai-agentautomationclaudeclaude-codedockeriosiphonelinuxmcpmcp-servermodel-context-protocolrustwebdriveragent
fix(setup): menu keys already received are read, however late (#120)
Qinghe Liabfd3fa66872
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0e2e36b..8e6a1ff 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -18,6 +18,7 @@ All notable changes to this project are documented here. The format is based on### Fixed+- An arrow key in the setup guide's menus (`iphone-control setup`) could come out as a letter on a busy machine: when the program was descheduled for more than 250 ms in the middle of reading the key, the rest of its bytes, though already there, were dropped and the last one taken as typed (↓ as "B"). Bytes already received are now always read.- A tap or swipe a minute or more after the last one no longer waits for the screen size to be measured again first: that takes WDA 0.95 s on an iPad mini 5 (iPadOS 26.7.3), where the first tap after a pause took 1.7 s instead of 0.9. The size is now measured again in the background while nobody acts (only Display Zoom changes it), and on rotation as before.- A control page whose H.264 or the phone's own video didn't show a first frame within 9 s (the viewer or daemon restarting, the encoder starting up) used MJPEG until it was reloaded; it now tries again after 30 s, 1 min and 2 min. A picture the viewer said isn't available still isn't tried again.- A swipe on the control page through WDA no longer waits for the list to stop before answering (reading the element tree again and again, up to 2.5 s): the person at the page sees it. On an iPad mini 5 a swipe took 3.1 s, now 1.6–2.3 s. Agents (MCP, CLI) still wait, so their next tap doesn't land on a moving list.diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.mdindex fb680d9..25cce17 100644--- a/CHANGELOG.zh-CN.md+++ b/CHANGELOG.zh-CN.md@@ -18,6 +18,7 @@### 修复+- 引导(`iphone-control setup`)菜单里的方向键在机器很忙时可能变成字母:读按键读到一半,程序被挂起超过 250 毫秒,后面已经到了的字节会被丢掉,最后一个当成输入的字符(↓ 变成「B」)。现在已经收到的字节一定会读。- 隔了一分钟以上的点按、滑动不再先等重新读屏幕尺寸:iPad mini 5(iPadOS 26.7.3)上 WDA 读一次要 0.95 秒,停一会儿后的第一次点按要 1.7 秒,平时 0.9 秒。尺寸现在在没人操作时于后台重读(只有「显示缩放」会改变它),转屏时照旧马上重读。- 控制台页面的 H.264 或手机自己的视频 9 秒内没出第一帧时(控制台服务或守护进程正在重启、编码器正在启动),原来一直用 MJPEG 直到刷新页面;现在 30 秒、1 分钟、2 分钟后各再试一次。控制台服务明确说不可用的画面仍然不再试。- 控制台上经 WDA 的滑动不再等列表停下才返回(原来要反复读元素树,最多 2.5 秒):页面前的人看得见。iPad mini 5 上一次滑动从 3.1 秒降到 1.6–2.3 秒。Agent(MCP、命令行)仍然等,免得下一次点击落在还在滚的列表上。diff --git a/apps/iphone-control/src/guide/terminal.rs b/apps/iphone-control/src/guide/terminal.rsindex f0e0fc8..f0faeee 100644--- a/apps/iphone-control/src/guide/terminal.rs+++ b/apps/iphone-control/src/guide/terminal.rs@@ -67,15 +67,16 @@ impl Drop for MenuSignals {}}+/// The next byte, waiting until `deadline` at most. A byte already there is taken even after the deadline: the bytes+/// of a key arrive together, and this process may be descheduled between two of them (a busy macOS CI runner took+/// longer than [`SEQUENCE_WAIT`] between the `[` and the `B` of ↓, which then came out as the letter B)fn read_byte(input: &mut std::fs::File, deadline: Instant) -> io::Result<Option<u8>> {loop {if MENU_SIGNAL.load(Ordering::Relaxed) != 0 {return Err(io::ErrorKind::Interrupted.into());}+ // zero past the deadline: select then only checks what is therelet remaining = deadline.saturating_duration_since(Instant::now());- if remaining.is_zero() {- return Ok(None);- }// select works for terminal descriptors on macOS as well as Linux.let fd = input.as_raw_fd();if fd < 0 || fd as usize >= libc::FD_SETSIZE {@@ -83,7 +84,7 @@ fn read_byte(input: &mut std::fs::File, deadline: Instant) -> io::Result<Option<}let mut timeout = libc::timeval {tv_sec: remaining.as_secs() as libc::time_t,- tv_usec: remaining.subsec_micros().max(1) as libc::suseconds_t,+ tv_usec: remaining.subsec_micros() as libc::suseconds_t,};// SAFETY: fd is live and checked against FD_SETSIZE; the initialized// set and timeout remain valid for the duration of select.@@ -656,4 +657,27 @@ mod tests {Err(error) => panic!("menu error: {error}"),}}++ /// A pipe: reading end as the menu's input, writing end+ fn pipe() -> (std::fs::File, std::fs::File) {+ use std::os::fd::FromRawFd;+ let mut fds = [0; 2];+ // SAFETY: pipe fills both descriptors, each owned by one File from here on+ assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);+ unsafe { (std::fs::File::from_raw_fd(fds[0]), std::fs::File::from_raw_fd(fds[1])) }+ }++ /// The rest of a key that is already there is read even when this process got to it after [`SEQUENCE_WAIT`]+ #[test]+ fn bytes_already_there_are_read_after_the_deadline() {+ use std::io::Write;+ let (mut input, mut keys) = pipe();+ let past = Instant::now() - SEQUENCE_WAIT;+ assert_eq!(read_byte(&mut input, past).unwrap(), None, "nothing there: no wait");+ keys.write_all(b"\x1b[B").unwrap();+ assert_eq!(read_byte(&mut input, past).unwrap(), Some(0x1b));+ assert_eq!(read_byte(&mut input, past).unwrap(), Some(b'['));+ assert_eq!(read_byte(&mut input, past).unwrap(), Some(b'B'));+ assert_eq!(read_byte(&mut input, past).unwrap(), None);+ }}diff --git a/tests/terminal.py b/tests/terminal.pyindex bc27cb9..de56757 100644--- a/tests/terminal.py+++ b/tests/terminal.py@@ -53,11 +53,15 @@ class Session:def expect(self, text):target = text.encode()deadline = time.monotonic() + 8+ exited = Nonewhile target not in self.output and time.monotonic() < deadline:self.pump()if self.proc.poll() is not None:- self.pump(0)- break+ # What the process wrote just before it exited can reach this side of the PTY a little later (macOS):+ # one more read without waiting missed MENU_CANCELLED now and then+ exited = exited or time.monotonic()+ if time.monotonic() - exited > 1:+ breakassert target in self.output, (text, self.output.decode(errors="replace"))def wait_for_raw(self):