1use std::cmp;
23use super::*;
45// Friendly neighborhood axis-aligned rectangle
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct AaRect {
8 x: i64,
9 y: i64,
10 width: i64,
11 height: i64,
12}
1314impl AaRect {
15pub fn new((x, y): (i32, i32), (width, height): (u32, u32)) -> Self {
16let (x, y) = (x as i64, y as i64);
17let (width, height) = (width as i64, height as i64);
18 AaRect { x, y, width, height }
19 }
2021pub fn contains_point(&self, x: i64, y: i64) -> bool {
22 x >= self.x && x <= self.x + self.width && y >= self.y && y <= self.y + self.height
23 }
2425pub fn get_overlapping_area(&self, other: &Self) -> i64 {
26let x_overlap = cmp::max(
270,
28 cmp::min(self.x + self.width, other.x + other.width) - cmp::max(self.x, other.x),
29 );
30let y_overlap = cmp::max(
310,
32 cmp::min(self.y + self.height, other.y + other.height) - cmp::max(self.y, other.y),
33 );
34 x_overlap * y_overlap
35 }
36}
3738#[derive(Debug, Clone)]
39pub struct FrameExtents {
40pub left: u32,
41pub right: u32,
42pub top: u32,
43pub bottom: u32,
44}
4546impl FrameExtents {
47pub fn new(left: u32, right: u32, top: u32, bottom: u32) -> Self {
48 FrameExtents { left, right, top, bottom }
49 }
5051pub fn from_border(border: u32) -> Self {
52Self::new(border, border, border, border)
53 }
54}
5556#[derive(Debug, Clone, PartialEq, Eq)]
57pub enum FrameExtentsHeuristicPath {
58 Supported,
59 UnsupportedNested,
60 UnsupportedBordered,
61}
6263#[derive(Debug, Clone)]
64pub struct FrameExtentsHeuristic {
65pub frame_extents: FrameExtents,
66pub heuristic_path: FrameExtentsHeuristicPath,
67}
6869impl FrameExtentsHeuristic {
70pub fn inner_pos_to_outer(&self, x: i32, y: i32) -> (i32, i32) {
71use self::FrameExtentsHeuristicPath::*;
72if self.heuristic_path != UnsupportedBordered {
73 (x - self.frame_extents.left as i32, y - self.frame_extents.top as i32)
74 } else {
75 (x, y)
76 }
77 }
7879pub fn inner_size_to_outer(&self, width: u32, height: u32) -> (u32, u32) {
80 (
81 width.saturating_add(
82self.frame_extents.left.saturating_add(self.frame_extents.right) as _
83),
84 height.saturating_add(
85self.frame_extents.top.saturating_add(self.frame_extents.bottom) as _
86),
87 )
88 }
89}
9091impl XConnection {
92// This is adequate for inner_position
93pub fn translate_coords(
94&self,
95 window: xproto::Window,
96 root: xproto::Window,
97 ) -> Result<xproto::TranslateCoordinatesReply, X11Error> {
98self.xcb_connection().translate_coordinates(window, root, 0, 0)?.reply().map_err(Into::into)
99 }
100101// This is adequate for inner_size
102pub fn get_geometry(
103&self,
104 window: xproto::Window,
105 ) -> Result<xproto::GetGeometryReply, X11Error> {
106self.xcb_connection().get_geometry(window)?.reply().map_err(Into::into)
107 }
108109fn get_frame_extents(&self, window: xproto::Window) -> Option<FrameExtents> {
110let atoms = self.atoms();
111let extents_atom = atoms[_NET_FRAME_EXTENTS];
112113if !hint_is_supported(extents_atom) {
114return None;
115 }
116117// Of the WMs tested, xmonad, i3, dwm, IceWM (1.3.x and earlier), and blackbox don't
118 // support this. As this is part of EWMH (Extended Window Manager Hints), it's likely to
119 // be unsupported by many smaller WMs.
120let extents: Option<Vec<u32>> = self
121.get_property(window, extents_atom, xproto::Atom::from(xproto::AtomEnum::CARDINAL))
122 .ok();
123124 extents.and_then(|extents| {
125if extents.len() >= 4 {
126Some(FrameExtents {
127 left: extents[0],
128 right: extents[1],
129 top: extents[2],
130 bottom: extents[3],
131 })
132 } else {
133None
134}
135 })
136 }
137138pub fn is_top_level(&self, window: xproto::Window, root: xproto::Window) -> Option<bool> {
139let atoms = self.atoms();
140let client_list_atom = atoms[_NET_CLIENT_LIST];
141142if !hint_is_supported(client_list_atom) {
143return None;
144 }
145146let client_list: Option<Vec<xproto::Window>> = self
147.get_property(root, client_list_atom, xproto::Atom::from(xproto::AtomEnum::WINDOW))
148 .ok();
149150 client_list.map(|client_list| client_list.contains(&(window as xproto::Window)))
151 }
152153fn get_parent_window(&self, window: xproto::Window) -> Result<xproto::Window, X11Error> {
154let parent = self.xcb_connection().query_tree(window)?.reply()?.parent;
155Ok(parent)
156 }
157158fn climb_hierarchy(
159&self,
160 window: xproto::Window,
161 root: xproto::Window,
162 ) -> Result<xproto::Window, X11Error> {
163let mut outer_window = window;
164loop {
165let candidate = self.get_parent_window(outer_window)?;
166if candidate == root {
167break;
168 }
169 outer_window = candidate;
170 }
171Ok(outer_window)
172 }
173174pub fn get_frame_extents_heuristic(
175&self,
176 window: xproto::Window,
177 root: xproto::Window,
178 ) -> FrameExtentsHeuristic {
179use self::FrameExtentsHeuristicPath::*;
180181// Position relative to root window.
182 // With rare exceptions, this is the position of a nested window. Cases where the window
183 // isn't nested are outlined in the comments throughout this function, but in addition to
184 // that, fullscreen windows often aren't nested.
185let (inner_y_rel_root, child) = {
186let coords = self
187.translate_coords(window, root)
188 .expect("Failed to translate window coordinates");
189 (coords.dst_y, coords.child)
190 };
191192let (width, height, border) = {
193let inner_geometry =
194self.get_geometry(window).expect("Failed to get inner window geometry");
195 (inner_geometry.width, inner_geometry.height, inner_geometry.border_width)
196 };
197198// The first condition is only false for un-nested windows, but isn't always false for
199 // un-nested windows. Mutter/Muffin/Budgie and Marco present a mysterious discrepancy:
200 // when y is on the range [0, 2] and if the window has been unfocused since being
201 // undecorated (or was undecorated upon construction), the first condition is true,
202 // requiring us to rely on the second condition.
203let nested = !(window == child || self.is_top_level(child, root) == Some(true));
204205// Hopefully the WM supports EWMH, allowing us to get exact info on the window frames.
206if let Some(mut frame_extents) = self.get_frame_extents(window) {
207// Mutter/Muffin/Budgie and Marco preserve their decorated frame extents when
208 // decorations are disabled, but since the window becomes un-nested, it's easy to
209 // catch.
210if !nested {
211 frame_extents = FrameExtents::new(0, 0, 0, 0);
212 }
213214// The difference between the nested window's position and the outermost window's
215 // position is equivalent to the frame size. In most scenarios, this is equivalent to
216 // manually climbing the hierarchy as is done in the case below. Here's a list of
217 // known discrepancies:
218 // * Mutter/Muffin/Budgie gives decorated windows a margin of 9px (only 7px on top) in
219 // addition to a 1px semi-transparent border. The margin can be easily observed by
220 // using a screenshot tool to get a screenshot of a selected window, and is presumably
221 // used for drawing drop shadows. Getting window geometry information via
222 // hierarchy-climbing results in this margin being included in both the position and
223 // outer size, so a window positioned at (0, 0) would be reported as having a position
224 // (-10, -8).
225 // * Compiz has a drop shadow margin just like Mutter/Muffin/Budgie, though it's 10px on
226 // all sides, and there's no additional border.
227 // * Enlightenment otherwise gets a y position equivalent to inner_y_rel_root. Without
228 // decorations, there's no difference. This is presumably related to Enlightenment's
229 // fairly unique concept of window position; it interprets positions given to
230 // XMoveWindow as a client area position rather than a position of the overall window.
231232FrameExtentsHeuristic { frame_extents, heuristic_path: Supported }
233 } else if nested {
234// If the position value we have is for a nested window used as the client area, we'll
235 // just climb up the hierarchy and get the geometry of the outermost window we're
236 // nested in.
237let outer_window =
238self.climb_hierarchy(window, root).expect("Failed to climb window hierarchy");
239let (outer_y, outer_width, outer_height) = {
240let outer_geometry =
241self.get_geometry(outer_window).expect("Failed to get outer window geometry");
242 (outer_geometry.y, outer_geometry.width, outer_geometry.height)
243 };
244245// Since we have the geometry of the outermost window and the geometry of the client
246 // area, we can figure out what's in between.
247let diff_x = outer_width.saturating_sub(width) as u32;
248let diff_y = outer_height.saturating_sub(height) as u32;
249let offset_y = inner_y_rel_root.saturating_sub(outer_y) as u32;
250251let left = diff_x / 2;
252let right = left;
253let top = offset_y;
254let bottom = diff_y.saturating_sub(offset_y);
255256let frame_extents = FrameExtents::new(left, right, top, bottom);
257 FrameExtentsHeuristic { frame_extents, heuristic_path: UnsupportedNested }
258 } else {
259// This is the case for xmonad and dwm, AKA the only WMs tested that supplied a
260 // border value. This is convenient, since we can use it to get an accurate frame.
261let frame_extents = FrameExtents::from_border(border.into());
262 FrameExtentsHeuristic { frame_extents, heuristic_path: UnsupportedBordered }
263 }
264 }
265}