Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ What changed in each release, one line per change. Versions follow [Semantic Ver

- `iphone_tap` takes `count` (1–10): the taps go in one gesture, about 0.1 s apart, so a double tap or several quick taps work; separate calls are about 0.5 s apart. An older daemon ignores it and taps once.
- `iphone_batch` takes `one_gesture`: a batch of coordinate taps runs as one gesture, about twice as fast for pressing keys on a keyboard.
- `iphone_swipe` takes `hold`: hold still at the start before moving, to drag home-screen icons and other things that need a long press first. An older daemon ignores it and swipes as before.

### Improvements

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- `iphone_tap` 加 `count`(1~10):在一个手势里连点,每下间隔约 0.1 秒,双击、短时间内连点几下的入口都能点到;分开调用每下约隔 0.5 秒。旧版本守护进程不认这个参数,只点一下。
- `iphone_batch` 新增 `one_gesture`:全是坐标点击的一批步骤放进一个手势里点完,在键盘上逐个按键快一倍左右。
- `iphone_swipe` 新增 `hold`:先在起点按住几秒再移动,用来拖动主屏幕图标等要先长按才拿得起来的东西。旧版本守护进程会忽略它,照常滑动。

### 改进

Expand Down
3 changes: 2 additions & 1 deletion apps/iphone-control/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ fn action(name: &str, b: &Value) -> Option<Request> {
from: (num("x1")?, num("y1")?),
to: (num("x2")?, num("y2")?),
seconds: num("seconds")?.clamp(0.05, 3.0),
hold: 0.0,
},
"type" => {
Request::TypeText { text: text("text")?, submit: b.get("submit").and_then(Value::as_bool).unwrap_or(false) }
Expand Down Expand Up @@ -2013,7 +2014,7 @@ mod tests {
assert_eq!(action("tap", &json!({"x": 10.0, "y": 20.5})), Some(Request::Tap { x: 10.0, y: 20.5, count: 1 }));
assert_eq!(
action("swipe", &json!({"x1": 1, "y1": 2, "x2": 3, "y2": 4, "seconds": 9})),
Some(Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 3.0 })
Some(Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 3.0, hold: 0.0 })
);
assert_eq!(
action("type", &json!({"text": "hi"})),
Expand Down
32 changes: 29 additions & 3 deletions crates/ictl-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,9 @@ impl Daemon {
})
}
Request::LongPress { x, y, seconds } => self.long_press(g, x, y, seconds, view).await,
Request::Swipe { from, to, seconds } => self.swipe(g, from, to, seconds, view, watched).await,
Request::Swipe { from, to, seconds, hold } => {
self.swipe(g, crate::tools::Stroke { from, to, seconds, hold }, view, watched).await
}
Request::TypeText { text, submit } => self.type_text(g, &text, submit).await,
Request::PressButton { button } => self.press_button(g, &button, watched).await,
Request::Touch { phase, x, y } => {
Expand Down Expand Up @@ -1506,7 +1508,20 @@ fn check_request(req: &Request) -> Result<(), String> {
_ => {}
}
let seconds = match req {
Request::LongPress { seconds, .. } | Request::Swipe { seconds, .. } => *seconds,
Request::LongPress { seconds, .. } => *seconds,
Request::Swipe { seconds, hold, .. } => {
if !hold.is_finite() || *hold < 0.0 {
return Err(ictl_l10n::trf!("hold 要是不小于 0 的秒数", "hold must be a number of seconds, 0 or more"));
}
// 按住加移动一共多久:HID 两段都要逐个发采样。seconds 自己的范围下面照常查,负数不能靠 hold 凑过去
if seconds.is_finite() && *seconds >= 0.0 && seconds + hold > MAX_GESTURE_SECONDS {
return Err(ictl_l10n::trf!(
"seconds 加 hold 不能超过 {MAX_GESTURE_SECONDS} 秒",
"seconds plus hold must be at most {MAX_GESTURE_SECONDS} s"
));
}
*seconds
}
_ => return Ok(()),
};
if !seconds.is_finite() || !(0.0..=MAX_GESTURE_SECONDS).contains(&seconds) {
Expand Down Expand Up @@ -1846,8 +1861,19 @@ pub(crate) mod tests {

#[test]
fn gesture_seconds_are_bounded() {
let swipe = |s| Request::Swipe { from: (0.0, 0.0), to: (1.0, 1.0), seconds: s };
let swipe = |s| Request::Swipe { from: (0.0, 0.0), to: (1.0, 1.0), seconds: s, hold: 0.0 };
assert!(check_request(&swipe(0.3)).is_ok());
// a held drag counts the hold too, and the hold can't be negative
let held = |s, h| Request::Swipe { from: (0.0, 0.0), to: (1.0, 1.0), seconds: s, hold: h };
assert!(check_request(&held(0.7, 0.6)).is_ok());
assert!(check_request(&held(5.0, 5.0)).is_ok());
let too_long = check_request(&held(5.0, 5.5)).unwrap_err();
assert!(too_long.contains("hold"), "{too_long}");
// a negative seconds isn't made up for by the hold
assert!(check_request(&held(-0.5, 1.0)).is_err());
assert!(check_request(&held(0.3, 20.0)).is_err());
assert!(check_request(&held(0.7, -0.1)).is_err());
assert!(check_request(&held(0.7, f64::NAN)).is_err());
assert!(check_request(&swipe(1e6)).is_err());
assert!(check_request(&swipe(-1.0)).is_err());
assert!(check_request(&swipe(f64::NAN)).is_err());
Expand Down
2 changes: 1 addition & 1 deletion crates/ictl-daemon/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ mod tests {
let r = ts.handle(&req("iphone_tap", json!({"x": 5, "y": 6})), Some(&t.token), None, &mut None).await;
assert_eq!(r, Some(Response::text("2")));
t.thought("点一下");
let n = ts.auto_step(&t, &Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3 });
let n = ts.auto_step(&t, &Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 });
assert_eq!(n, 3);
let (log, _, _) = t.subscribe();
let steps: Vec<&Value> = log.iter().filter(|e| e.event == "step").map(|e| &e.data).collect();
Expand Down
23 changes: 17 additions & 6 deletions crates/ictl-daemon/src/tools/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
use super::*;
use crate::protocol::MAX_TAP_COUNT;

/// 一次滑动([`Daemon::swipe`]):从 `from` 用 `seconds` 秒移到 `to`;`hold` 秒大于 0 时先在起点按住再移动
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Stroke {
pub from: (f64, f64),
pub to: (f64, f64),
pub seconds: f64,
pub hold: f64,
}

impl Daemon {
/// A stale pointer must neither unlock nor act on a new display. Unlock can itself
/// rotate the foreground app, so take the final measurement after it completes.
Expand Down Expand Up @@ -313,16 +322,18 @@ impl Daemon {
pub(crate) async fn swipe(
&self,
g: &mut Inner,
from: (f64, f64),
to: (f64, f64),
seconds: f64,
stroke: Stroke,
view: Option<&str>,
watched: bool,
) -> crate::error::Result<String> {
let Stroke { from, to, seconds, hold } = stroke;
self.prepare_coordinates(g, view, true).await?;
let desc = format!("({},{}) → ({},{})", fmt_g(from.0), fmt_g(from.1), fmt_g(to.0), fmt_g(to.1));
let mut desc = format!("({},{}) → ({},{})", fmt_g(from.0), fmt_g(from.1), fmt_g(to.0), fmt_g(to.1));
if hold > 0.0 {
desc = ictl_l10n::trf!("{desc},先按住 {} 秒", "{desc}, held {} s first", fmt_g(hold));
}
if let Some(w) = self.wda(g) {
match w.swipe_into_polling(from, to, (seconds * 1000.0) as u64).await {
match w.swipe_into_polling(from, to, (hold * 1000.0) as u64, (seconds * 1000.0) as u64).await {
// 滑动请求不等惯性滚动(等的话设置不对能卡十几秒),这里等列表停下再返回:
// 列表还在滚时下一次点击只会让它停住,点不到东西
// someone watches the screen ([`crate::protocol::WATCHED`]): they see when it stops
Expand All @@ -349,7 +360,7 @@ impl Daemon {
self.hid_do(g, move |h| {
Box::pin(async move {
h.rotation = rot;
h.drag(from, to, seconds).await
if hold > 0.0 { h.held_drag(from, to, hold, seconds).await } else { h.drag(from, to, seconds).await }
})
})
.await?;
Expand Down
8 changes: 7 additions & 1 deletion crates/ictl-daemon/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod launch;
mod native;
mod observe;

pub(crate) use input::Stroke;

use std::time::{Duration, Instant};

use base64::Engine;
Expand Down Expand Up @@ -553,7 +555,11 @@ mod tests {
let down = |e: &str| e.contains("没有 HID 后备路线") || e.contains("no HID fallback");
let tap = d.tap(&mut g, 20., 50., 1, None).await.unwrap_err().to_string();
assert!(down(&tap), "{tap}");
let swipe = d.swipe(&mut g, (20., 50.), (20., 300.), 0.3, None, false).await.unwrap_err().to_string();
let swipe = d
.swipe(&mut g, Stroke { from: (20., 50.), to: (20., 300.), seconds: 0.3, hold: 0.0 }, None, false)
.await
.unwrap_err()
.to_string();
assert!(down(&swipe), "{swipe}");
// a finger: the same, and the page may still send the whole gesture once WDA is back
match d.touch(&mut g, TouchPhase::Down, 20., 50., None).await {
Expand Down
21 changes: 21 additions & 0 deletions crates/ictl-device/src/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ impl HidSession {
.await
}

/// 按住再拖:按下后在起点按住 `hold` 秒(和 [`Self::long_press`] 一样每 100 毫秒重报一次位置),不抬起,接着
/// 像 [`Self::drag`] 一样移到终点抬起。拖动主屏幕图标、列表排序这类要先长按才能拖的
pub async fn held_drag(&mut self, from: (f64, f64), to: (f64, f64), hold: f64, seconds: f64) -> Result<()> {
let (x1, y1) = self.hid_point(from);
let (x2, y2) = self.hid_point(to);
let held = Duration::from_secs_f64(hold.clamp(0.0, 60.0));
let steps = ((seconds * 1000.0 / 20.0).round() as u32).max(10);
let d = held + Duration::from_secs_f64(seconds.max(0.0) + 10.0);
limit(ictl_l10n::tr!("HID 按住拖动", "HID held drag"), d, async {
let end = tokio::time::Instant::now() + held;
self.touch.send_touchscreen(TOUCHSCREEN_STATE_CONTACT, x1, y1, None).await?;
while tokio::time::Instant::now() < end {
tokio::time::sleep(Duration::from_millis(100).min(end - tokio::time::Instant::now())).await;
self.touch.send_touchscreen(TOUCHSCREEN_STATE_CONTACT, x1, y1, None).await?;
}
// drag() starts with a contact at (x1, y1): the finger stays down from the hold into the move
Ok(self.touch.drag(x1, y1, x2, y2, steps, 20).await?)
})
.await
}

/// One sample of a finger that follows the user's pointer (the web viewer): `contact` true puts the finger at the
/// point (the first one touches down, later ones move it), false lifts it there. iOS works out taps, long presses
/// and the speed of a swipe from these samples and their timing, the same way as from a real finger
Expand Down
22 changes: 19 additions & 3 deletions crates/ictl-mcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ WDA 不可用时改用 CoreDevice HID(iOS 27 起;结果里注明「经 HID」)
"from_y": num(ictl_l10n::tr!("起点 y(点)", "start y (points)")),
"to_x": num(ictl_l10n::tr!("终点 x(点)", "end x (points)")),
"to_y": num(ictl_l10n::tr!("终点 y(点)", "end y (points)")),
"seconds": {"type": "number", "default": 0.3, "description": ictl_l10n::tr!("滑动用时(秒),默认 0.3;越小越快、惯性越大", "swipe duration (seconds), default 0.3; smaller is faster with more momentum")}
"seconds": {"type": "number", "default": 0.3, "description": ictl_l10n::tr!("滑动用时(秒),默认 0.3;越小越快、惯性越大", "swipe duration (seconds), default 0.3; smaller is faster with more momentum")},
"hold": {"type": "number", "default": 0, "minimum": 0, "description": ictl_l10n::tr!("先在起点按住几秒再移动,默认 0(普通滑动)。拖动要先长按才拿得起来的东西时用,例如主屏幕图标、可排序的列表项、文件:hold 0.6~1,seconds 0.7 左右。少于 0.5 秒通常拿不起来,只会滑动或翻页", "seconds to hold still at the start before moving, default 0 (a plain swipe). For dragging things that have to be long-pressed before they can be picked up, such as home-screen icons, reorderable list rows or files: hold 0.6-1, seconds about 0.7. Under 0.5 s usually doesn't pick anything up and just swipes or pages")}
})),
&["from_x", "from_y", "to_x", "to_y"],
),
Expand Down Expand Up @@ -668,6 +669,7 @@ pub fn to_request(name: &str, args: &Map<String, Value>) -> Result<Request, Stri
from: (a.f("from_x")?, a.f("from_y")?),
to: (a.f("to_x")?, a.f("to_y")?),
seconds: a.f_or("seconds", 0.3)?,
hold: a.f_or("hold", 0.0)?,
},
"iphone_type_text" => Request::TypeText { text: a.s("text")?, submit: a.b_or("submit", false)? },
"iphone_press_button" => Request::PressButton { button: a.s("button")? },
Expand Down Expand Up @@ -1250,6 +1252,20 @@ mod tests {
with_lang(Lang::Zh, || assert!(has_cjk(instructions())));
}

/// hold defaults to 0 (a plain swipe, same request as before) and is passed through when given
#[test]
fn swipe_hold_defaults_to_zero() {
let plain = to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4}))).unwrap();
assert_eq!(plain, Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 });
let held = json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4, "seconds": 0.7, "hold": 0.6});
let held = to_request("iphone_swipe", &args(held)).unwrap();
assert_eq!(held, Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.7, hold: 0.6 });
assert!(
to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4, "hold": "x"})))
.is_err()
);
}

/// one_gesture 把全是坐标点击的批量并成一个 TapSequence;混了别的步骤就报错,一步都不执行
#[test]
fn one_gesture_turns_taps_into_one_sequence() {
Expand Down Expand Up @@ -1295,7 +1311,7 @@ mod tests {

#[test]
fn alert_check_after_actions_but_not_buttons_or_reads() {
assert!(wants_alert_check(&Request::Swipe { from: (0.0, 0.0), to: (1.0, 1.0), seconds: 0.3 }));
assert!(wants_alert_check(&Request::Swipe { from: (0.0, 0.0), to: (1.0, 1.0), seconds: 0.3, hold: 0.0 }));
assert!(wants_alert_check(&Request::LaunchApp { app: "微信".into() }));
assert!(!wants_alert_check(&Request::PressButton { button: "home".into() }));
assert!(!wants_alert_check(&Request::TerminateApp { app: "微信".into() }));
Expand Down Expand Up @@ -1367,7 +1383,7 @@ mod tests {
);
assert_eq!(
to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4}))).unwrap(),
Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3 }
Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 }
);
assert_eq!(
to_request("iphone_long_press", &args(json!({"x": 1, "y": 2}))).unwrap(),
Expand Down
16 changes: 14 additions & 2 deletions crates/ictl-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub fn default_socket() -> PathBuf {
}
}

fn is_zero(v: &f64) -> bool {
*v == 0.0
}

/// [`Request::Tap`] 一次最多连点几下
pub const MAX_TAP_COUNT: u32 = 10;

Expand Down Expand Up @@ -113,10 +117,13 @@ pub enum Request {
y: f64,
seconds: f64,
},
/// `hold`:先在起点按住几秒再移动(拖动图标、列表排序),0 是普通滑动。旧版本守护进程不认识这个字段,当普通滑动做
Swipe {
from: (f64, f64),
to: (f64, f64),
seconds: f64,
#[serde(default, skip_serializing_if = "is_zero")]
hold: f64,
},
TypeText {
text: String,
Expand Down Expand Up @@ -934,9 +941,14 @@ mod tests {

#[test]
fn requests_round_trip_as_tagged_json() {
let r = Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3 };
let r = Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 };
let s = serde_json::to_string(&r).unwrap();
// a plain swipe is what it always was on the wire: older daemons and clients see no change
assert_eq!(s, r#"{"op":"swipe","from":[1.0,2.0],"to":[3.0,4.0],"seconds":0.3}"#);
assert_eq!(serde_json::from_str::<Request>(&s).unwrap(), r);
let r = Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.7, hold: 0.6 };
let s = serde_json::to_string(&r).unwrap();
assert!(s.starts_with(r#"{"op":"swipe""#), "{s}");
assert!(s.ends_with(r#""hold":0.6}"#), "{s}");
assert_eq!(serde_json::from_str::<Request>(&s).unwrap(), r);
let r = Request::TapSequence { points: vec![(60.0, 548.0), (23.0, 548.0)] };
let s = serde_json::to_string(&r).unwrap();
Expand Down
38 changes: 36 additions & 2 deletions crates/ictl-wda/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ pub fn swipe_steps(from: (f64, f64), to: (f64, f64), ms: u64) -> Vec<PointerStep
]
}

/// 按住再拖:按下后先原地按住 `hold_ms`,再用 `ms` 移到终点。按住要写成一段原地的 pointerMove,不能是 pause:
/// iPhone 12 mini(iOS 26.4.1)上主屏幕图标按 pause 按住 0.6~1 秒再移动,0/6 次拖得动(只当成滑动翻页),
/// 原地 move 0.5 秒再移动 3/3,0.3 秒 0/3。`hold_ms` 为 0 时就是 [`swipe_steps`]
pub fn drag_steps(from: (f64, f64), to: (f64, f64), hold_ms: u64, ms: u64) -> Vec<PointerStep> {
if hold_ms == 0 {
return swipe_steps(from, to, ms);
}
vec![
PointerStep::Move { x: from.0, y: from.1, ms: 0 },
PointerStep::Down,
PointerStep::Move { x: from.0, y: from.1, ms: hold_ms },
PointerStep::Move { x: to.0, y: to.1, ms },
PointerStep::Up,
]
}

/// 一个 actions 请求里依次点几个点,每个点一根手指:第 k 根手指先空等前面 k 个点按完(每个点占 5 个节拍:移动、按下、
/// 按住 `press_ms`、抬起、停 `gap_ms`),再移过去按下抬起。W3C 按节拍对齐各手指,所以这些点一个接一个、不重叠。
/// 不用一根手指移来移去:iPhone 17(iOS 26.5.2)上一根手指在一个手势里移到别的点再按,每个点都被识别两次
Expand Down Expand Up @@ -862,10 +878,11 @@ impl<T: Transport> WdaClient<T> {

/// Swipes and stays in polling mode for the caller, who ends it with [`Self::end_polling`]: restoring the settings
/// only to change them again right after would cost two round trips
pub async fn swipe_into_polling(&mut self, from: (f64, f64), to: (f64, f64), ms: u64) -> Result<()> {
/// `hold_ms` > 0 holds the finger still at `from` first ([`drag_steps`])
pub async fn swipe_into_polling(&mut self, from: (f64, f64), to: (f64, f64), hold_ms: u64, ms: u64) -> Result<()> {
self.baseline().await?;
self.set_idle_settings(IdleSettings::NO_COOL_OFF).await?;
let r = self.actions(&swipe_steps(from, to, ms)).await;
let r = self.actions(&drag_steps(from, to, hold_ms, ms)).await;
if r.is_err() {
self.end_polling().await;
}
Expand Down Expand Up @@ -1663,6 +1680,23 @@ mod tests {
assert_eq!(s[3], PointerStep::Move { x: 3.0, y: 4.0, ms: 300 });
}

/// 按住再拖:按下后原地 move 按住(不是 pause,pause 拖不动图标),再移到终点;不按住就是普通滑动
#[test]
fn a_held_drag_holds_with_a_move_in_place() {
let s = drag_steps((1.0, 2.0), (3.0, 4.0), 600, 700);
assert_eq!(
s,
[
PointerStep::Move { x: 1.0, y: 2.0, ms: 0 },
PointerStep::Down,
PointerStep::Move { x: 1.0, y: 2.0, ms: 600 },
PointerStep::Move { x: 3.0, y: 4.0, ms: 700 },
PointerStep::Up,
]
);
assert_eq!(drag_steps((1.0, 2.0), (3.0, 4.0), 0, 300), swipe_steps((1.0, 2.0), (3.0, 4.0), 300));
}

#[tokio::test]
async fn locked_checks_tree_until_unlocked_once_then_trusts_wda() {
let lock_xml = serde_json::to_string(include_str!("../tests/fixtures/lockscreen-ios27.xml")).unwrap();
Expand Down
Loading
Loading