diff --git a/Cargo.lock b/Cargo.lock index 10353347..41534752 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3767,6 +3767,11 @@ dependencies = [ name = "komorebi-e2e-agent" version = "0.2.0-dev" dependencies = [ + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-application-services", + "objc2-core-foundation", + "objc2-foundation 0.3.2", "serde", "serde_json_lenient", "windows 0.62.2", @@ -3916,19 +3921,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "komorebi-macos-e2e" -version = "0.2.0-dev" -dependencies = [ - "objc2 0.6.5", - "objc2-app-kit 0.3.2", - "objc2-application-services", - "objc2-core-foundation", - "objc2-foundation 0.3.2", - "serde", - "serde_json_lenient", -] - [[package]] name = "komorebi-protocol" version = "0.2.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 9a5532ec..b792e765 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,6 @@ members = [ "crates/komorebi-linux", "crates/komorebi-macos", "crates/komorebi-macos-core", - "crates/komorebi-macos-e2e", "crates/komorebi-hotkeys", "crates/komorebi-version", "komorebi-recorder", diff --git a/crates/komorebi-e2e-agent/Cargo.toml b/crates/komorebi-e2e-agent/Cargo.toml index e9a78ae0..a1999d0c 100644 --- a/crates/komorebi-e2e-agent/Cargo.toml +++ b/crates/komorebi-e2e-agent/Cargo.toml @@ -11,3 +11,40 @@ serde_json = { workspace = true } [target.'cfg(windows)'.dependencies] windows = { workspace = true } + +[target.'cfg(target_os = "macos")'.dependencies] +objc2 = { version = "0.6", default-features = false, features = ["std"] } +objc2-app-kit = { version = "0.3", default-features = false, features = [ + "std", + "NSApplication", + "NSResponder", + "NSWorkspace", + "NSRunningApplication", + "NSScreen", + "libc", +] } +objc2-application-services = { version = "0.3", default-features = false, features = [ + "std", + "HIServices", + "AXUIElement", + "AXValue", + "AXError", + "libc", + "Accessibility", +] } +objc2-core-foundation = { version = "0.3", default-features = false, features = [ + "std", + "CFString", + "CFArray", + "CFNumber", + "CFCGTypes", +] } +objc2-foundation = { version = "0.3", default-features = false, features = [ + "std", + "NSString", + "NSGeometry", + "NSArray", + "NSDate", + "NSRunLoop", + "NSEnumerator", +] } diff --git a/crates/komorebi-macos-e2e/src/bin/agent.rs b/crates/komorebi-e2e-agent/src/macos.rs similarity index 55% rename from crates/komorebi-macos-e2e/src/bin/agent.rs rename to crates/komorebi-e2e-agent/src/macos.rs index 916d003a..a7e95284 100644 --- a/crates/komorebi-macos-e2e/src/bin/agent.rs +++ b/crates/komorebi-e2e-agent/src/macos.rs @@ -1,31 +1,15 @@ -//! The guest half of the e2e suite on macOS. +//! The verbs of the macOS agent: the windows and displays of the logged-in GUI session. //! -//! It has to run inside the logged-in GUI session, and be started by a program that holds the -//! Accessibility and Screen Recording grants (the golden image starts it from a Terminal window), -//! because the daemon it starts, and the windows it reads, are attributed to that program. The -//! host talks to it over TCP, on the guest's own address. The protocol is the one of the Windows -//! agent (`komorebi-e2e-agent`): one request is one line of JSON, `{"id": 1, "verb": "...", ...}`, -//! answered by one line, `{"id": 1, "ok": ...}` or `{"id": 1, "error": "..."}`, and `put_file` and -//! `get_file` move `size` raw bytes after the line. -//! -//! The agent serves one connection at a time on the main thread, because AppKit (`NSScreen`) is -//! for the main thread. It injects no input. +//! It has to run inside that session, and be started by a program that holds the Accessibility and +//! Screen Recording grants (the golden image starts it from a Terminal window), because the daemon +//! it starts, and the windows it reads, are attributed to that program. //! //! The windows it describes are the ones the accessibility API gives, as the daemon sees them; -//! their frames are in points, with the origin at the top left of the main display, as the -//! daemon's are. A frame is `{left, top, right, bottom}` where `right` and `bottom` are the width -//! and the height, the shape komorebi itself prints. +//! their frames are in points, with the origin at the top left of the main display, as the daemon's +//! are. AppKit (`NSScreen`) is for the main thread, so one connection is served at a time, on it. +//! The agent injects no input. -#![cfg(target_os = "macos")] - -use std::collections::HashMap; -use std::io::BufRead; -use std::io::BufReader; -use std::io::Read; -use std::io::Write; use std::net::TcpListener; -use std::net::TcpStream; -use std::process::Child; use std::process::Command; use std::process::Stdio; use std::ptr::NonNull; @@ -50,40 +34,25 @@ use objc2_core_foundation::Type; use objc2_foundation::NSDate; use objc2_foundation::NSDefaultRunLoopMode; use objc2_foundation::NSRunLoop; -use serde::Deserialize; use serde_json::Value; use serde_json::json; -const DEFAULT_PORT: u16 = 7879; +use crate::protocol::Mode; +use crate::protocol::Request; +use crate::protocol::Verbs; +use crate::protocol::frame_json; /// How long a call to an application that does not answer holds the agent. const AX_TIMEOUT: f32 = 2.0; -#[derive(Deserialize)] -struct Request { - id: Value, - verb: String, - #[serde(default)] - args: Vec, - #[serde(default)] - path: String, - #[serde(default)] - size: u64, - #[serde(default)] - line: String, - #[serde(default)] - title: String, - #[serde(default)] - pid: u32, - #[serde(default)] - x: f64, - #[serde(default)] - y: f64, - #[serde(default)] - env: HashMap, - /// a file that gets what a spawned program prints, standard output and error - #[serde(default)] - out: Option, +/// A frame as the protocol has it, from points. +fn frame_json_f(x: f64, y: f64, width: f64, height: f64) -> Value { + frame_json( + x.round() as i64, + y.round() as i64, + width.round() as i64, + height.round() as i64, + ) } fn copy_attribute(element: &AXUIElement, attribute: &str) -> Option> { @@ -132,14 +101,6 @@ fn frame_of(element: &AXUIElement) -> Option { Some(rect) } -fn rect_json(x: f64, y: f64, width: f64, height: f64) -> Value { - // the shape komorebi itself prints: right and bottom are width and height - json!({ - "left": x.round() as i64, "top": y.round() as i64, - "right": width.round() as i64, "bottom": height.round() as i64, - }) -} - /// A window of a regular application, with what the agent needs to act on it. struct Window { pid: i32, @@ -211,7 +172,7 @@ fn describe(window: &Window) -> Value { "pid": window.pid, "role": copy_attribute::(&window.element, "AXRole").map(|r| r.to_string()), "subrole": copy_attribute::(&window.element, "AXSubrole").map(|r| r.to_string()), - "frame": rect_json(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height), + "frame": frame_json_f(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height), "minimized": minimized, "focused": focused, }) @@ -232,7 +193,7 @@ fn displays(mtm: MainThreadMarker) -> Vec { }; let main_height = main.frame().size.height; let flip = |rect: CGRect| { - rect_json( + frame_json_f( rect.origin.x, main_height - (rect.origin.y + rect.size.height), rect.size.width, @@ -300,151 +261,52 @@ fn move_to(title: &str, x: f64, y: f64) -> Result { } } -fn answer(stream: &mut TcpStream, id: &Value, result: Result) { - let line = match result { - Ok(ok) => json!({"id": id, "ok": ok}), - Err(error) => json!({"id": id, "error": error}), - }; - let _ = writeln!(stream, "{line}"); -} - -fn exec(args: &[String], env: &HashMap) -> Result { - let (program, rest) = args.split_first().ok_or("exec wants a program")?; - let out = Command::new(program) - .args(rest) - .envs(env) - .stdin(Stdio::null()) - .output() - .map_err(|e| format!("{program}: {e}"))?; - Ok(json!({ - "status": out.status.code(), - "stdout": String::from_utf8_lossy(&out.stdout), - "stderr": String::from_utf8_lossy(&out.stderr), - })) -} - -/// Starts a program that keeps running and returns at once. The agent keeps the child, so that -/// `status` can say how it ended and it is not left a zombie, and keeps the write end of its -/// standard input open: the test window leaves when its input closes, and `send` writes to it. -fn spawn( - args: &[String], - env: &HashMap, - out: &Option, - children: &mut HashMap, -) -> Result { - let (program, rest) = args.split_first().ok_or("spawn wants a program")?; - let mut command = Command::new(program); - command.args(rest).envs(env).stdin(Stdio::piped()); - match out { - Some(path) => { - let file = std::fs::File::create(path).map_err(|e| format!("{path}: {e}"))?; - let again = file.try_clone().map_err(|e| format!("{path}: {e}"))?; - command.stdout(file).stderr(again); - } - None => { - command.stdout(Stdio::null()).stderr(Stdio::null()); - } - } - let child = command.spawn().map_err(|e| format!("{program}: {e}"))?; - let pid = child.id(); - children.insert(pid, child); - Ok(json!({"pid": pid})) -} - -fn status(pid: u32, children: &mut HashMap) -> Result { - let child = children - .get_mut(&pid) - .ok_or_else(|| format!("{pid} was not started by this agent"))?; - match child.try_wait() { - Ok(Some(status)) => Ok(json!({"running": false, "code": status.code()})), - Ok(None) => Ok(json!({"running": true})), - Err(e) => Err(e.to_string()), - } -} - -/// Writes a line to the standard input of a program `spawn` started. -fn send(pid: u32, line: &str, children: &mut HashMap) -> Result { - let input = children - .get_mut(&pid) - .ok_or_else(|| format!("{pid} was not started by this agent"))? - .stdin - .as_mut() - .ok_or_else(|| format!("{pid} has no standard input"))?; - writeln!(input, "{line}") - .and_then(|()| input.flush()) - .map(|()| json!(true)) - .map_err(|e| format!("{pid}: {e}")) -} - -fn serve( - stream: TcpStream, +struct MacosVerbs { mtm: MainThreadMarker, - children: &mut HashMap, -) -> std::io::Result<()> { - let mut writer = stream.try_clone()?; - let mut reader = BufReader::new(stream); - let mut line = String::new(); - loop { - line.clear(); - if reader.read_line(&mut line)? == 0 { - return Ok(()); - } - let req: Request = match serde_json::from_str(&line) { - Ok(req) => req, - Err(e) => { - answer(&mut writer, &Value::Null, Err(format!("bad request: {e}"))); - continue; +} + +impl Verbs for MacosVerbs { + fn prepare(&self, command: &mut Command, mode: Mode) { + match mode { + Mode::Exec => { + command.stdin(Stdio::null()); } - }; - let result = match req.verb.as_str() { - "ping" => Ok(json!("pong")), - "exec" => exec(&req.args, &req.env), - "spawn" => spawn(&req.args, &req.env, &req.out, children), - "status" => status(req.pid, children), - "send" => send(req.pid, &req.line, children), + // the standard input stays open, because the test window leaves when it closes, and + // `send` writes to it; what the program prints goes nowhere unless `out` names a file + Mode::Spawn => { + command + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::null()); + } + } + } + + fn handle(&mut self, request: &Request) -> Option> { + Some(match request.verb.as_str() { "os_windows" => Ok(Value::Array(windows().iter().map(describe).collect())), - "displays" => Ok(Value::Array(displays(mtm))), - "activate" => activate(&req.title), - "minimize" => find(&req.title) + "displays" => Ok(Value::Array(displays(self.mtm))), + "activate" => activate(&request.title), + "minimize" => find(&request.title) .and_then(|w| set_bool(&w.element, "AXMinimized", true)) .map(|()| json!(true)), - "close" => close(&req.title), - "move" => move_to(&req.title, req.x, req.y), - "put_file" => { - let mut bytes = vec![0u8; req.size as usize]; - reader.read_exact(&mut bytes)?; - std::fs::write(&req.path, bytes) - .map(|()| json!(req.size)) - .map_err(|e| format!("{}: {e}", req.path)) - } - "get_file" => match std::fs::read(&req.path) { - Ok(bytes) => { - answer(&mut writer, &req.id, Ok(json!({"size": bytes.len()}))); - writer.write_all(&bytes)?; - continue; - } - Err(e) => Err(format!("{}: {e}", req.path)), - }, - other => Err(format!("unknown verb {other}")), - }; - answer(&mut writer, &req.id, result); + "close" => close(&request.title), + "move" => move_to(&request.title, request.x, request.y), + _ => return None, + }) } } -fn main() -> std::io::Result<()> { +pub fn run() -> std::io::Result<()> { let mtm = MainThreadMarker::new().expect("main runs on the main thread"); // the window server connection that NSScreen needs, with no Dock icon and no window let application = NSApplication::sharedApplication(mtm); application.setActivationPolicy(NSApplicationActivationPolicy::Accessory); - let port = std::env::var("KOMOREBI_E2E_AGENT_PORT") - .ok() - .and_then(|p| p.parse().ok()) - .unwrap_or(DEFAULT_PORT); - let listener = TcpListener::bind(("0.0.0.0", port))?; - let mut children = HashMap::new(); + let listener = TcpListener::bind(("0.0.0.0", crate::protocol::port()))?; + let mut verbs = MacosVerbs { mtm }; for stream in listener.incoming() { - if let Err(e) = serve(stream?, mtm, &mut children) { + if let Err(e) = crate::protocol::serve(stream?, &mut verbs) { eprintln!("connection ended: {e}"); } } diff --git a/crates/komorebi-e2e-agent/src/main.rs b/crates/komorebi-e2e-agent/src/main.rs index 58f8565b..d54b7a53 100644 --- a/crates/komorebi-e2e-agent/src/main.rs +++ b/crates/komorebi-e2e-agent/src/main.rs @@ -7,6 +7,8 @@ // no console window on Windows: it would be a window for the daemon to tile #![cfg_attr(windows, windows_subsystem = "windows")] +#[cfg(target_os = "macos")] +mod macos; mod protocol; #[cfg(windows)] mod windows; @@ -16,8 +18,13 @@ fn main() -> std::io::Result<()> { windows::run() } -#[cfg(not(windows))] +#[cfg(target_os = "macos")] +fn main() -> std::io::Result<()> { + macos::run() +} + +#[cfg(not(any(windows, target_os = "macos")))] fn main() { - eprintln!("komorebi-e2e-agent runs on Windows only"); + eprintln!("komorebi-e2e-agent runs on Windows and macOS only"); std::process::exit(1); } diff --git a/crates/komorebi-e2e-agent/src/protocol.rs b/crates/komorebi-e2e-agent/src/protocol.rs index 327862cc..bbdcce64 100644 --- a/crates/komorebi-e2e-agent/src/protocol.rs +++ b/crates/komorebi-e2e-agent/src/protocol.rs @@ -62,7 +62,19 @@ pub struct Request { pub line: String, /// A window handle, on a platform that has them (Windows). #[serde(default)] + #[cfg_attr(not(windows), allow(dead_code))] pub hwnd: isize, + /// A window's title, on a platform that finds a window by it (macOS), and a place on the + /// desktop for `move`. + #[serde(default)] + #[cfg_attr(not(target_os = "macos"), allow(dead_code))] + pub title: String, + #[serde(default)] + #[cfg_attr(not(target_os = "macos"), allow(dead_code))] + pub x: f64, + #[serde(default)] + #[cfg_attr(not(target_os = "macos"), allow(dead_code))] + pub y: f64, } /// Why a command is being prepared: it runs to its end, or it keeps running. diff --git a/crates/komorebi-macos-e2e/Cargo.toml b/crates/komorebi-macos-e2e/Cargo.toml deleted file mode 100644 index f383f599..00000000 --- a/crates/komorebi-macos-e2e/Cargo.toml +++ /dev/null @@ -1,53 +0,0 @@ -[package] -name = "komorebi-macos-e2e" -version.workspace = true -edition = "2024" -description = "The guest half of the e2e suite on macOS: an agent that answers the host, and a window to tile" -publish = false - -[[bin]] -name = "komorebi-macos-e2e-agent" -path = "src/bin/agent.rs" - -[target.'cfg(target_os = "macos")'.dependencies] -serde = { workspace = true } -serde_json = { workspace = true } -objc2 = { version = "0.6", default-features = false, features = ["std"] } -objc2-app-kit = { version = "0.3", default-features = false, features = [ - "std", - "NSApplication", - "NSWindow", - "NSView", - "NSColor", - "NSResponder", - "NSWorkspace", - "NSRunningApplication", - "NSScreen", - "NSGraphics", - "libc", -] } -objc2-application-services = { version = "0.3", default-features = false, features = [ - "std", - "HIServices", - "AXUIElement", - "AXValue", - "AXError", - "libc", - "Accessibility", -] } -objc2-core-foundation = { version = "0.3", default-features = false, features = [ - "std", - "CFString", - "CFArray", - "CFNumber", - "CFCGTypes", -] } -objc2-foundation = { version = "0.3", default-features = false, features = [ - "std", - "NSString", - "NSGeometry", - "NSArray", - "NSDate", - "NSRunLoop", - "NSEnumerator", -] } diff --git a/nix/workspace-src.nix b/nix/workspace-src.nix index 5e6a434c..09f6c9e2 100644 --- a/nix/workspace-src.nix +++ b/nix/workspace-src.nix @@ -47,10 +47,7 @@ let "crates/komorebi-linux" "crates/komorebi-hotkeys" ]; - macosOnly = [ - "crates/komorebi-macos" - "crates/komorebi-macos-e2e" - ]; + macosOnly = [ "crates/komorebi-macos" ]; # crates that no Linux package depends on, but whose tests need no macOS and so run in `linux-nextest`: # the FFI-free part of the macOS daemon. They are not in linux/root-crates.json (that list is the @@ -59,7 +56,6 @@ let linuxNeutralTests = [ "crates/komorebi-macos-core" ]; windowsOnly = [ "crates/komorebi-windows" - "crates/komorebi-e2e-agent" "komorebi-gui" "komorebic-no-console" "komorebi-shortcuts"