| 1 | use crate::errors::WmError; |
| 2 | use x11rb::protocol::randr::{self, 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 randr_version = match connection.randr_query_version(1, 2) { |
| 44 | Ok(cookie) => match cookie.reply() { |
| 45 | Ok(reply) => reply.major_version >= 1 && reply.minor_version >= 2, |
| 46 | Err(_) => false, |
| 47 | }, |
| 48 | Err(_) => false, |
| 49 | }; |
| 50 | |
| 51 | if !randr_version { |
| 52 | eprintln!("RandR 1.2+ not available, using single monitor"); |
| 53 | return Ok(vec![Monitor::new( |
| 54 | 0, |
| 55 | 0, |
| 56 | screen.width_in_pixels as u32, |
| 57 | screen.height_in_pixels as u32, |
| 58 | )]); |
| 59 | } |
| 60 | |
| 61 | let resources = match connection.randr_get_screen_resources(root) { |
| 62 | Ok(cookie) => match cookie.reply() { |
| 63 | Ok(res) => res, |
| 64 | Err(_) => { |
| 65 | eprintln!("Failed to get screen resources, using single monitor"); |
| 66 | return Ok(vec![Monitor::new( |
| 67 | 0, |
| 68 | 0, |
| 69 | screen.width_in_pixels as u32, |
| 70 | screen.height_in_pixels as u32, |
| 71 | )]); |
| 72 | } |
| 73 | }, |
| 74 | Err(_) => { |
| 75 | eprintln!("Failed to query screen resources, using single monitor"); |
| 76 | return Ok(vec![Monitor::new( |
| 77 | 0, |
| 78 | 0, |
| 79 | screen.width_in_pixels as u32, |
| 80 | screen.height_in_pixels as u32, |
| 81 | )]); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | let mut monitors = Vec::new(); |
| 86 | |
| 87 | for &output in &resources.outputs { |
| 88 | let output_info = match connection.randr_get_output_info(output, 0) { |
| 89 | Ok(cookie) => match cookie.reply() { |
| 90 | Ok(info) => info, |
| 91 | Err(_) => continue, |
| 92 | }, |
| 93 | Err(_) => continue, |
| 94 | }; |
| 95 | |
| 96 | if output_info.connection != randr::Connection::CONNECTED { |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | if output_info.crtc == 0 { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | let crtc_info = match connection.randr_get_crtc_info(output_info.crtc, 0) { |
| 105 | Ok(cookie) => match cookie.reply() { |
| 106 | Ok(info) => info, |
| 107 | Err(_) => continue, |
| 108 | }, |
| 109 | Err(_) => continue, |
| 110 | }; |
| 111 | |
| 112 | if crtc_info.width == 0 || crtc_info.height == 0 { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | monitors.push(Monitor::new( |
| 117 | crtc_info.x as i32, |
| 118 | crtc_info.y as i32, |
| 119 | crtc_info.width as u32, |
| 120 | crtc_info.height as u32, |
| 121 | )); |
| 122 | } |
| 123 | |
| 124 | if monitors.is_empty() { |
| 125 | eprintln!("No monitors detected via RandR, using full screen"); |
| 126 | monitors.push(Monitor::new( |
| 127 | 0, |
| 128 | 0, |
| 129 | screen.width_in_pixels as u32, |
| 130 | screen.height_in_pixels as u32, |
| 131 | )); |
| 132 | } |
| 133 | |
| 134 | monitors.sort_by(|a, b| { |
| 135 | let y_cmp = a.y.cmp(&b.y); |
| 136 | if y_cmp == std::cmp::Ordering::Equal { |
| 137 | a.x.cmp(&b.x) |
| 138 | } else { |
| 139 | y_cmp |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | Ok(monitors) |
| 144 | } |