uzor-framework 1.5.23

uzor app engine: one kernel of single-writer engines driven by a typed input bus
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! The transport-agnostic RPC core.
//!
//! Request: `{"id": <any>, "method": "<name>", "params": {...}}`.
//! Response: `{"id": <same>, "ok": true, "result": ...}` or
//! `{"id": <same>, "ok": false, "error": {"code": "...", "message": "..."}}`.
//! Codes and messages are plain English and stable; they are not
//! translated.

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

use base64::Engine as _;
use serde_json::{json, Map, Value};
use uzor::a11y::{query::role_name, A11yNode, QueryError, Selector};
use uzor::input::{ModifierKeys, MouseButton};
use uzor::types::Rect;
use crate::automation::{AutoCmd, AutoError, AutoReply, AutoTree, AutomationClient, ShotSource};
use crate::types::ids::WindowId;
use crate::{ImeInput, InputEvent, KeyInput, KeyState, Point, PointerInput, WheelDelta, WheelInput};

use super::keys;
use uzor_devtools::events::EventLog;

pub use uzor_devtools::{METHODS, PROTOCOL_VERSION};


/// How long one host round trip may take.
const CALL_TIMEOUT: Duration = Duration::from_secs(10);
/// How long `focus` / `type` wait for the clicked widget to take the focus.
const FOCUS_WAIT: Duration = Duration::from_secs(2);

/// An RPC failure: a stable code and an English message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RpcError {
    /// Stable code.
    pub code: &'static str,
    /// Human-readable detail.
    pub message: String,
}

impl RpcError {
    fn new(code: &'static str, message: impl Into<String>) -> Self {
        Self { code, message: message.into() }
    }
    fn bad(message: impl Into<String>) -> Self {
        Self::new("bad_request", message)
    }
}

impl From<AutoError> for RpcError {
    fn from(e: AutoError) -> Self {
        Self::new(e.code(), e.to_string())
    }
}

impl From<QueryError> for RpcError {
    fn from(e: QueryError) -> Self {
        let code = match e {
            QueryError::Parse(_) => "bad_selector",
            QueryError::NotFound => "not_found",
            QueryError::Ambiguous(_) => "ambiguous",
        };
        Self::new(code, e.to_string())
    }
}

type R<T> = Result<T, RpcError>;

/// The RPC core: decodes requests, drives the app through its automation
/// client, encodes responses, and records an event per call.
pub struct Dispatcher {
    app: String,
    client: AutomationClient,
    events: EventLog,
}

impl Dispatcher {
    /// A dispatcher for `client`; `app` names the app in `ping`.
    pub fn new(app: &str, client: AutomationClient) -> Self {
        Self {
            app: app.to_string(),
            client,
            events: EventLog::new(),
        }
    }

    /// One JSON request in, one JSON response out. Never panics on input.
    pub fn handle_json(&self, request: &str) -> String {
        let parsed: Result<Value, _> = serde_json::from_str(request);
        let (id, out, method) = match parsed {
            Err(e) => (Value::Null, Err(RpcError::bad(format!("request is not JSON: {e}"))), String::new()),
            Ok(v) => {
                let id = v.get("id").cloned().unwrap_or(Value::Null);
                match v.get("method").and_then(Value::as_str) {
                    None => (id, Err(RpcError::bad("missing \"method\"")), String::new()),
                    Some(m) => {
                        let params = v.get("params").cloned().unwrap_or(Value::Object(Map::new()));
                        if !params.is_object() {
                            (id, Err(RpcError::bad("\"params\" must be an object")), m.to_string())
                        } else {
                            (id, self.call(m, &params), m.to_string())
                        }
                    }
                }
            }
        };
        let ok = out.is_ok();
        let resp = match out {
            Ok(result) => json!({"id": id, "ok": true, "result": result}),
            Err(e) => json!({"id": id, "ok": false, "error": {"code": e.code, "message": e.message}}),
        };
        if !method.is_empty() {
            let mut ev = json!({"type": "rpc", "method": method, "ok": ok});
            if let Some(err) = resp.get("error").and_then(|e| e.get("code")) {
                ev["error"] = err.clone();
            }
            self.emit(ev);
        }
        resp.to_string()
    }

    /// Append an event (`seq` and `t_ms` are filled in).
    pub fn emit(&self, event: Value) {
        self.events.emit(event);
    }

    /// Events with `seq > since`, waiting up to `wait` for the first one.
    pub fn events_since(&self, since: u64, wait: Duration) -> Vec<Value> {
        self.events.since(since, wait)
    }

    fn call(&self, method: &str, p: &Value) -> R<Value> {
        match method {
            "ping" => Ok(json!({
                "pong": true,
                "app": self.app,
                "pid": std::process::id(),
                "protocol": PROTOCOL_VERSION,
                "methods": METHODS,
            })),
            "windows" => self.windows(),
            "dump-tree" => self.dump_tree(p),
            "click" => self.click(p),
            "hover" => self.hover(p),
            "focus" => self.focus(p),
            "type" => self.type_text(p),
            "key" => self.key(p),
            "scroll" => self.scroll(p),
            "screenshot" => self.screenshot(p),
            "state" => self.state(),
            other => Err(RpcError::new("unknown_method", format!("unknown method '{other}'; try one of: {}", METHODS.join(", ")))),
        }
    }

    fn host(&self, cmd: AutoCmd) -> R<AutoReply> {
        Ok(self.client.call(cmd, CALL_TIMEOUT)?)
    }

    fn input(&self, win: Option<WindowId>, events: Vec<InputEvent>) -> R<()> {
        match self.host(AutoCmd::Input { win, events })? {
            AutoReply::InputDone { .. } => Ok(()),
            other => Err(RpcError::new("failed", format!("unexpected host reply {other:?}"))),
        }
    }

    fn tree(&self, win: Option<WindowId>) -> R<AutoTree> {
        match self.host(AutoCmd::Tree { win })? {
            AutoReply::Tree(t) => Ok(t),
            other => Err(RpcError::new("failed", format!("unexpected host reply {other:?}"))),
        }
    }

    fn windows(&self) -> R<Value> {
        match self.host(AutoCmd::Windows)? {
            AutoReply::Windows(ws) => Ok(Value::Array(
                ws.iter()
                    .map(|w| json!({"id": w.id.get(), "key": w.key, "viewport": rect_json(w.viewport), "dpr": w.dpr, "focused": w.focused}))
                    .collect(),
            )),
            other => Err(RpcError::new("failed", format!("unexpected host reply {other:?}"))),
        }
    }

    fn dump_tree(&self, p: &Value) -> R<Value> {
        let t = self.tree(window_param(p)?)?;
        let nodes: Vec<Value> = t.a11y.nodes().iter().map(node_json).collect();
        let mut out = json!({
            "window": t.window.get(),
            "focused": t.focused.as_ref().map(|w| w.as_str()),
            "hovered": t.hovered.as_ref().map(|w| w.as_str()),
            "nodes": nodes,
        });
        if p.get("widgets").and_then(Value::as_bool).unwrap_or(false) {
            out["widgets"] = Value::Array(
                t.widgets
                    .iter()
                    .map(|w| {
                        json!({"id": w.id.as_str(), "kind": format!("{:?}", w.kind), "rect": rect_json(w.rect), "parent": w.parent.as_ref().map(|p| p.as_str())})
                    })
                    .collect(),
            );
        }
        Ok(out)
    }

    /// Where a pointer command lands: a selector's centre or explicit x/y.
    fn target(&self, p: &Value) -> R<(Option<WindowId>, Option<String>, Point)> {
        let win = window_param(p)?;
        if let Some(sel) = p.get("selector").and_then(Value::as_str) {
            let t = self.tree(win)?;
            let (id, r) = resolve(&t, sel)?;
            return Ok((Some(t.window), Some(id), Point::new(r.x + r.width / 2.0, r.y + r.height / 2.0)));
        }
        match (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64)) {
            (Some(x), Some(y)) => Ok((win, None, Point::new(x, y))),
            _ => Err(RpcError::bad("give \"selector\", or \"x\" and \"y\"")),
        }
    }

    fn hover(&self, p: &Value) -> R<Value> {
        let (win, id, pos) = self.target(p)?;
        self.input(win, vec![InputEvent::Pointer(PointerInput::Moved { pos, mods: ModifierKeys::default() })])?;
        self.input(win, vec![])?;
        Ok(json!({"id": id, "x": pos.x, "y": pos.y}))
    }

    fn click(&self, p: &Value) -> R<Value> {
        let (win, id, pos) = self.target(p)?;
        let button = match p.get("button").and_then(Value::as_str).unwrap_or("left") {
            "left" => MouseButton::Left,
            "right" => MouseButton::Right,
            "middle" => MouseButton::Middle,
            other => return Err(RpcError::bad(format!("unknown button '{other}'"))),
        };
        let count = p.get("count").and_then(Value::as_u64).unwrap_or(1).clamp(1, 3);
        let mods = ModifierKeys::default();
        self.input(win, vec![InputEvent::Pointer(PointerInput::Moved { pos, mods })])?;
        for _ in 0..count {
            self.input(win, vec![InputEvent::Pointer(PointerInput::Down { pos, button, mods })])?;
            self.input(win, vec![InputEvent::Pointer(PointerInput::Up { pos, button, mods })])?;
        }
        // The app reads clicks on its next compose; one more turn lets it act.
        self.input(win, vec![])?;
        Ok(json!({"id": id, "x": pos.x, "y": pos.y}))
    }

    fn focus(&self, p: &Value) -> R<Value> {
        if p.get("selector").and_then(Value::as_str).is_none() {
            return Err(RpcError::bad("give \"selector\""));
        }
        let (win, id) = self.click_and_focus(p)?;
        Ok(json!({"id": id, "window": win.map(|w| w.get()), "focused": true}))
    }

    /// Click the selector's widget and wait until it holds the focus. Apps may move the focus
    /// a frame or two after the click (a focus command), so this polls with empty input turns.
    fn click_and_focus(&self, p: &Value) -> R<(Option<WindowId>, String)> {
        let (win, id, _) = self.target(p)?;
        let id = id.unwrap_or_default();
        self.click(p)?;
        let deadline = Instant::now() + FOCUS_WAIT;
        loop {
            let t = self.tree(win)?;
            if t.focused.as_ref().map(|f| f.as_str()) == Some(id.as_str()) {
                return Ok((win, id));
            }
            if Instant::now() >= deadline {
                return Err(RpcError::new("not_focused", format!("'{id}' did not take the focus after a click")));
            }
            std::thread::sleep(Duration::from_millis(16));
            self.input(win, vec![])?;
        }
    }

    fn type_text(&self, p: &Value) -> R<Value> {
        let text = p.get("text").and_then(Value::as_str).ok_or_else(|| RpcError::bad("give \"text\""))?;
        let win = if p.get("selector").is_some() {
            self.click_and_focus(p)?.0
        } else {
            window_param(p)?
        };
        let mut events = Vec::new();
        if p.get("ime").and_then(Value::as_bool).unwrap_or(false) {
            events.push(InputEvent::Ime(ImeInput::Commit(text.to_string())));
        } else {
            for c in text.chars() {
                let code = keys::code_for_char(c);
                let t = (!matches!(c, '\n' | '\r' | '\t')).then(|| c.to_string());
                events.push(InputEvent::Key(KeyInput { code, text: t, state: KeyState::Down, mods: ModifierKeys::default() }));
                events.push(InputEvent::Key(KeyInput { code, text: None, state: KeyState::Up, mods: ModifierKeys::default() }));
            }
        }
        self.input(win, events)?;
        self.input(win, vec![])?;
        Ok(json!({"chars": text.chars().count()}))
    }

    fn key(&self, p: &Value) -> R<Value> {
        let spec = p.get("key").and_then(Value::as_str).ok_or_else(|| RpcError::bad("give \"key\", e.g. \"Enter\" or \"Ctrl+A\""))?;
        let chord = keys::parse(spec).map_err(RpcError::bad)?;
        let win = window_param(p)?;
        let repeat = p.get("repeat").and_then(Value::as_u64).unwrap_or(1).clamp(1, 100);
        let mut events = Vec::new();
        for _ in 0..repeat {
            events.push(InputEvent::Key(KeyInput { code: chord.code, text: chord.text.clone(), state: KeyState::Down, mods: chord.mods }));
            events.push(InputEvent::Key(KeyInput { code: chord.code, text: None, state: KeyState::Up, mods: chord.mods }));
        }
        self.input(win, events)?;
        self.input(win, vec![])?;
        Ok(json!({"key": spec}))
    }

    fn scroll(&self, p: &Value) -> R<Value> {
        let (win, id, pos) = self.target(p)?;
        let dx = p.get("dx").and_then(Value::as_f64).unwrap_or(0.0);
        let dy = p.get("dy").and_then(Value::as_f64).unwrap_or(0.0);
        let delta = match p.get("unit").and_then(Value::as_str).unwrap_or("px") {
            "px" | "pixels" => WheelDelta::Pixels { x: dx, y: dy },
            "lines" => WheelDelta::Lines { x: dx, y: dy },
            other => return Err(RpcError::bad(format!("unknown unit '{other}' (px or lines)"))),
        };
        let mods = ModifierKeys::default();
        self.input(win, vec![InputEvent::Pointer(PointerInput::Moved { pos, mods })])?;
        self.input(win, vec![InputEvent::Wheel(WheelInput { pos, delta, mods })])?;
        self.input(win, vec![])?;
        Ok(json!({"id": id, "x": pos.x, "y": pos.y}))
    }

    fn screenshot(&self, p: &Value) -> R<Value> {
        let source = match p.get("source").and_then(Value::as_str).unwrap_or("auto") {
            "auto" => ShotSource::Auto,
            "window" => ShotSource::Window,
            "offscreen" => ShotSource::Offscreen,
            other => return Err(RpcError::bad(format!("unknown source '{other}' (auto, window, offscreen)"))),
        };
        match self.host(AutoCmd::Screenshot { win: window_param(p)?, source })? {
            AutoReply::Shot(s) => Ok(json!({
                "window": s.window.get(),
                "width": s.width,
                "height": s.height,
                "source": s.source,
                "png_base64": base64::engine::general_purpose::STANDARD.encode(&s.png),
            })),
            other => Err(RpcError::new("failed", format!("unexpected host reply {other:?}"))),
        }
    }

    fn state(&self) -> R<Value> {
        match self.host(AutoCmd::AppState)? {
            AutoReply::State(None) => Ok(Value::Null),
            AutoReply::State(Some(s)) => serde_json::from_str(&s).map_err(|e| RpcError::new("failed", format!("app state is not JSON: {e}"))),
            other => Err(RpcError::new("failed", format!("unexpected host reply {other:?}"))),
        }
    }
}

fn window_param(p: &Value) -> R<Option<WindowId>> {
    match p.get("window") {
        None | Some(Value::Null) => Ok(None),
        Some(v) => v
            .as_u64()
            .and_then(|n| u32::try_from(n).ok())
            .map(|n| Some(WindowId(n)))
            .ok_or_else(|| RpcError::bad("\"window\" must be a window id number")),
    }
}

/// A selector to (id, rect): the accessibility tree first; a bare `#id`
/// also finds widgets that have no accessibility role.
fn resolve(t: &AutoTree, sel: &str) -> R<(String, Rect)> {
    let parsed = Selector::parse(sel)?;
    match parsed.find_one(&t.a11y) {
        Ok(n) => Ok((n.id.as_str().to_string(), n.bounds)),
        Err(QueryError::NotFound) => {
            let bare = sel.strip_prefix('#').filter(|s| !s.contains(['[', ':', ' ']));
            match bare.and_then(|id| t.widgets.iter().rev().find(|w| w.id.as_str() == id)) {
                Some(w) => Ok((w.id.as_str().to_string(), w.rect)),
                None => Err(QueryError::NotFound.into()),
            }
        }
        Err(e) => Err(e.into()),
    }
}

fn rect_json(r: Rect) -> Value {
    json!({"x": r.x, "y": r.y, "w": r.width, "h": r.height})
}

fn node_json(n: &A11yNode) -> Value {
    let mut v = json!({
        "id": n.id.as_str(),
        "role": role_name(n.role),
        "name": n.name,
        "bounds": rect_json(n.bounds),
        "focused": n.focused,
        "disabled": n.disabled,
        "masked": n.masked,
    });
    if let Some(k) = n.kind {
        v["kind"] = json!(format!("{k:?}"));
    }
    if let Some(val) = &n.value {
        v["value"] = json!(val);
    }
    if let Some(c) = n.checked {
        v["checked"] = json!(c);
    }
    if !n.children.is_empty() {
        v["children"] = Value::Array(n.children.iter().map(|c| json!(c.as_str())).collect());
    }
    v
}

impl uzor_devtools::Rpc for Dispatcher {
    fn handle_json(&self, request: &str) -> String {
        Dispatcher::handle_json(self, request)
    }
    fn events_since(&self, since: u64, wait: Duration) -> Vec<Value> {
        Dispatcher::events_since(self, since, wait)
    }
}