From 8110bb2773b6c841029a51eca7971f42a36f480c Mon Sep 17 00:00:00 2001 From: srdusr <99972264+srdusr@users.noreply.github.com> Date: Tue, 2 Apr 2024 00:58:00 +0200 Subject: Rewrite srdwm in Rust: working X11 and Wayland backends, Lua config The C++ prototype (moved to legacy-cpp/) was mostly a design skeleton: X11 and Windows backends were partially real, Wayland created the wlroots object graph but never wired a single event listener, macOS was stub except monitor enumeration, and the Lua engine's srd.bind() stored a key-combo string but never the actual closure. See docs/PRIOR_ART.md for the full audit. This replaces it with a Cargo workspace: - srdwm-core: platform-independent window/workspace/monitor state, a real master-stack tiling layout, and SmartPlacement grid/cascade/ snap-to-edge placement - fixing several bugs in the C++ version (hardcoded 2-column grid, cascade that never cascaded, snap-to-edge that always returned a fixed rect). 35 unit tests. - srdwm-config: the srd Lua API via mlua, implementing the surface docs/DEFAULTS.md always documented but the C++ engine never actually built (srd.window.close()/focus(direction), srd.workspace.next(), real keybinding closures, require("srd") support). 10 unit tests. - srdwm-x11: a real reparenting WM with a drawn title bar (buttons, drag, resize), verified live under Xephyr - frame placement and client offset match srdwm-core's computed geometry exactly, and the decoration renders correctly on screen. - srdwm-wayland: a from-scratch smithay compositor (the C++ version had nothing working to port from) - runs via the winit backend, tracks xdg-shell toplevels through the same WindowManager and hit-testing code X11 uses, verified to start/render/run without crashing. Decorations are solid-color (no text yet); see docs/IMPLEMENTATION_STATUS.md for exact scope. - srdwm-windows / srdwm-macos: structured, cfg-gated designs informed by komorebi/glazewm and yabai/AeroSpace respectively (see docs/PRIOR_ART.md), honestly marked as unbuilt/unverified since this sandbox has no Windows or macOS target. --- crates/macos/src/lib.rs | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 crates/macos/src/lib.rs (limited to 'crates/macos/src') diff --git a/crates/macos/src/lib.rs b/crates/macos/src/lib.rs new file mode 100644 index 0000000..ee61d53 --- /dev/null +++ b/crates/macos/src/lib.rs @@ -0,0 +1,146 @@ +//! macOS backend for srdwm. +//! +//! Like `srdwm-windows`, this crate is kept in the workspace so it +//! type-checks on every target, but its `cfg(target_os = "macos")` bodies +//! have never actually been built or run (this sandbox only has the +//! `x86_64-unknown-linux-gnu` target installed) - treat everything here as a +//! design sketch, not verified code. +//! +//! macOS gives no public API to draw a custom titlebar/border on another +//! process's window, so (per the legacy C++ design, see docs/PRIOR_ART.md) +//! srdwm cannot *decorate* foreign windows the way the X11/Wayland backends +//! do. The plan carried over from the legacy code: +//! +//! 1. Real window control (move/resize/focus) goes through the +//! Accessibility API (`AXUIElementSetAttributeValue` with +//! `kAXPositionAttribute`/`kAXSizeAttribute`), which requires the user to +//! grant Accessibility permission (`AXIsProcessTrustedWithOptions`). +//! 2. "Full title bar" parity is approximated with a separate, borderless, +//! click-through **overlay window** drawn on top of the target window, +//! reusing the same `srdwm_core::window::ResizeEdge` hit-testing as the +//! other backends, repositioned every time the tracked window moves. The +//! legacy C++ only stubbed this (`create_overlay_window` logged and did +//! nothing); it is *not* implemented here either - it needs an `NSWindow` +//! (via `objc2-app-kit` or similar) and per-window position polling or an +//! AX notification observer, which is a substantial enough piece of work +//! that faking it would be worse than leaving it as a documented TODO. +//! 3. Closing a window isn't a single AX attribute - it requires finding the +//! window's close-button `AXUIElement` (`kAXCloseButtonAttribute`) and +//! performing `kAXPressAction` on it. Also left as TODO for the same reason. +//! +//! What *is* implemented for real below: monitor enumeration via +//! `CGGetActiveDisplayList`/`CGDisplayBounds` (stable, well-documented +//! low-level Core Graphics C API), which was also the one genuinely working +//! piece of the legacy macOS backend. + +use srdwm_core::{Monitor, Rect, Window, WindowId}; +use srdwm_platform::{Platform, PlatformError, PlatformKind, Result}; + +pub struct MacOsPlatform; + +impl MacOsPlatform { + pub fn new() -> Result { + #[cfg(target_os = "macos")] + { + Ok(Self) + } + #[cfg(not(target_os = "macos"))] + { + Err(PlatformError::Unsupported("srdwm-macos was not compiled for a macOS target")) + } + } +} + +impl Platform for MacOsPlatform { + fn kind(&self) -> PlatformKind { + PlatformKind::MacOS + } + + fn poll_events(&mut self) -> Result> { + // TODO: CGEventTap (kCGSessionEventTap) for global key/mouse input, + // as the legacy backend set up but never actually forwarded into its + // event queue. + Err(PlatformError::Unsupported("poll_events")) + } + + #[cfg(target_os = "macos")] + fn monitors(&mut self) -> Result> { + use core_graphics::display::{CGDisplay, CGMainDisplayID}; + + let ids = CGDisplay::active_displays().map_err(|e| PlatformError::Other(format!("CGGetActiveDisplayList failed: {e}")))?; + let main_id = unsafe { CGMainDisplayID() }; + let monitors = ids + .into_iter() + .enumerate() + .map(|(i, id)| { + let display = CGDisplay::new(id); + let bounds = display.bounds(); + let mut m = Monitor::new( + i as u32, + format!("display-{id}"), + Rect::new(bounds.origin.x as i32, bounds.origin.y as i32, bounds.size.width as u32, bounds.size.height as u32), + ); + m.primary = id == main_id; + m + }) + .collect(); + Ok(monitors) + } + #[cfg(not(target_os = "macos"))] + fn monitors(&mut self) -> Result> { + Err(PlatformError::Unsupported("monitors")) + } + + fn apply_geometry(&mut self, _window: WindowId, _geometry: Rect) -> Result<()> { + // TODO: AXUIElementSetAttributeValue(kAXPositionAttribute / kAXSizeAttribute). + Err(PlatformError::Unsupported("apply_geometry (needs AXUIElement + Accessibility permission)")) + } + + fn set_title(&mut self, _window: WindowId, _title: &str) -> Result<()> { + Err(PlatformError::Unsupported("set_title (macOS windows are titled by their owning app, not the WM)")) + } + + fn focus(&mut self, _window: WindowId) -> Result<()> { + // TODO: AXUIElementSetAttributeValue(kAXFrontmostAttribute, true) on + // the owning application, or CGWindowListCopyWindowInfo + activate. + Err(PlatformError::Unsupported("focus")) + } + + fn minimize(&mut self, _window: WindowId) -> Result<()> { + // TODO: AXUIElementSetAttributeValue(kAXMinimizedAttribute, true). + Err(PlatformError::Unsupported("minimize")) + } + + fn restore(&mut self, _window: WindowId) -> Result<()> { + Err(PlatformError::Unsupported("restore")) + } + + fn close(&mut self, _window: WindowId) -> Result<()> { + // TODO: find kAXCloseButtonAttribute then AXUIElementPerformAction(kAXPressAction). + Err(PlatformError::Unsupported("close")) + } + + fn set_decorated(&mut self, _window: WindowId, _decorated: bool) -> Result<()> { + Err(PlatformError::Unsupported("set_decorated (needs the overlay-window design, see module docs)")) + } + + fn set_border_color(&mut self, _window: WindowId, _rgb: (u8, u8, u8)) -> Result<()> { + Err(PlatformError::Unsupported("set_border_color (needs the overlay-window design, see module docs)")) + } + + fn set_border_width(&mut self, _window: WindowId, _width: u32) -> Result<()> { + Err(PlatformError::Unsupported("set_border_width (needs the overlay-window design, see module docs)")) + } + + fn redraw_decoration(&mut self, _window: WindowId, _win: &Window, _focused: bool) -> Result<()> { + Err(PlatformError::Unsupported("redraw_decoration (needs the overlay-window design, see module docs)")) + } + + fn grab_keyboard(&mut self) -> Result<()> { + Err(PlatformError::Unsupported("grab_keyboard (needs a CGEventTap + Input Monitoring permission)")) + } + + fn ungrab_keyboard(&mut self) -> Result<()> { + Err(PlatformError::Unsupported("ungrab_keyboard")) + } +} -- cgit v1.2.3