tony / oxwm

Branch: d7af0b4
Switch branches/tags
master
tonybtw Updated for commit message requests and added floating (normie layout) and bind to toggle layouts. 0335582 2025-10-25
110 lines · 2.9 KB Raw Blame History
1 use crate::errors::WmError;
2 use x11rb::protocol::xinerama::ConnectionExt as _;
3 use x11rb::protocol::xproto::{Screen, Window};
4 use x11rb::rust_connection::RustConnection;
5
6 type WmResult<T> = Result<T, WmError>;
7
8 #[derive(Debug, Clone)]
9 pub struct Monitor {
10 pub x: i32,
11 pub y: i32,
12 pub width: u32,
13 pub height: u32,
14 pub selected_tags: u32,
15 pub focused_window: Option<Window>,
16 }
17
18 impl Monitor {
19 pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
20 Self {
21 x,
22 y,
23 width,
24 height,
25 selected_tags: 1,
26 focused_window: None,
27 }
28 }
29
30 pub fn contains_point(&self, x: i32, y: i32) -> bool {
31 x >= self.x
32 && x < self.x + self.width as i32
33 && y >= self.y
34 && y < self.y + self.height as i32
35 }
36 }
37
38 pub fn detect_monitors(
39 connection: &RustConnection,
40 screen: &Screen,
41 _root: Window,
42 ) -> WmResult<Vec<Monitor>> {
43 let fallback_monitors = || {
44 vec![Monitor::new(
45 0,
46 0,
47 screen.width_in_pixels as u32,
48 screen.height_in_pixels as u32,
49 )]
50 };
51
52 let mut monitors = Vec::<Monitor>::new();
53
54 let xinerama_active = connection
55 .xinerama_is_active()
56 .ok()
57 .and_then(|cookie| cookie.reply().ok())
58 .map_or(false, |reply| reply.state != 0);
59
60 if xinerama_active {
61 let xinerama_cookie = match connection.xinerama_query_screens() {
62 Ok(cookie) => cookie,
63 Err(_) => return Ok(fallback_monitors()),
64 };
65
66 let xinerama_reply = match xinerama_cookie.reply() {
67 Ok(reply) => reply,
68 Err(_) => return Ok(fallback_monitors()),
69 };
70
71 for screen_info in &xinerama_reply.screen_info {
72 let has_valid_dimensions = screen_info.width > 0 && screen_info.height > 0;
73 if !has_valid_dimensions {
74 continue;
75 }
76
77 let x_position = screen_info.x_org as i32;
78 let y_position = screen_info.y_org as i32;
79 let width_in_pixels = screen_info.width as u32;
80 let height_in_pixels = screen_info.height as u32;
81
82 let is_duplicate_monitor = monitors.iter().any(|monitor| {
83 monitor.x == x_position
84 && monitor.y == y_position
85 && monitor.width == width_in_pixels
86 && monitor.height == height_in_pixels
87 });
88
89 if !is_duplicate_monitor {
90 monitors.push(Monitor::new(
91 x_position,
92 y_position,
93 width_in_pixels,
94 height_in_pixels,
95 ));
96 }
97 }
98 }
99
100 if monitors.is_empty() {
101 monitors = fallback_monitors();
102 }
103
104 monitors.sort_by(|a, b| match a.y.cmp(&b.y) {
105 std::cmp::Ordering::Equal => a.x.cmp(&b.x),
106 other => other,
107 });
108
109 Ok(monitors)
110 }