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/core/src/geometry.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 crates/core/src/geometry.rs (limited to 'crates/core/src/geometry.rs') diff --git a/crates/core/src/geometry.rs b/crates/core/src/geometry.rs new file mode 100644 index 0000000..75feeef --- /dev/null +++ b/crates/core/src/geometry.rs @@ -0,0 +1,77 @@ +/// An axis-aligned rectangle in screen space, used for window and monitor bounds. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Rect { + pub x: i32, + pub y: i32, + pub width: u32, + pub height: u32, +} + +impl Rect { + pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self { + Self { x, y, width, height } + } + + pub fn right(&self) -> i32 { + self.x + self.width as i32 + } + + pub fn bottom(&self) -> i32 { + self.y + self.height as i32 + } + + pub fn contains_point(&self, x: i32, y: i32) -> bool { + x >= self.x && x < self.right() && y >= self.y && y < self.bottom() + } + + pub fn overlaps(&self, other: &Rect) -> bool { + !(self.right() <= other.x + || other.right() <= self.x + || self.bottom() <= other.y + || other.bottom() <= self.y) + } + + /// Shrinks the rect on all sides by `margin`, saturating at zero size. + pub fn inset(&self, margin: u32) -> Rect { + let m = margin as i32; + let width = self.width.saturating_sub(margin * 2); + let height = self.height.saturating_sub(margin * 2); + Rect { x: self.x + m, y: self.y + m, width, height } + } + + pub fn center(&self) -> (i32, i32) { + (self.x + self.width as i32 / 2, self.y + self.height as i32 / 2) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn overlap_detection_matches_aabb_semantics() { + let a = Rect::new(0, 0, 100, 100); + let b = Rect::new(50, 50, 100, 100); + let c = Rect::new(100, 100, 50, 50); // touches corner, should not overlap (half-open) + let d = Rect::new(200, 200, 10, 10); + + assert!(a.overlaps(&b)); + assert!(!a.overlaps(&c)); + assert!(!a.overlaps(&d)); + } + + #[test] + fn inset_shrinks_symmetrically() { + let r = Rect::new(0, 0, 100, 60); + let inset = r.inset(10); + assert_eq!(inset, Rect::new(10, 10, 80, 40)); + } + + #[test] + fn contains_point_is_half_open() { + let r = Rect::new(0, 0, 10, 10); + assert!(r.contains_point(0, 0)); + assert!(!r.contains_point(10, 10)); + assert!(r.contains_point(9, 9)); + } +} -- cgit v1.2.3