| 1 | use std::sync::OnceLock; |
| 2 | |
| 3 | use gpui_kit::Window; |
| 4 | use raw_window_handle::{HasWindowHandle, RawWindowHandle}; |
| 5 | use x11rb::connection::Connection as _; |
| 6 | use x11rb::protocol::shape::{ConnectionExt as _, SK, SO}; |
| 7 | use x11rb::protocol::xproto::{AtomEnum, ClipOrdering, ConnectionExt as _, PropMode}; |
| 8 | use x11rb::rust_connection::RustConnection; |
| 9 | use x11rb::wrapper::ConnectionExt as _; |
| 10 | |
| 11 | const WINDOW_ICON_SIZES: [u32; 3] = [32, 64, 128]; |
| 12 | |
| 13 | fn x11_window_id(window: &Window) -> Option<u32> { |
| 14 | match HasWindowHandle::window_handle(window).ok()?.as_raw() { |
| 15 | RawWindowHandle::Xcb(handle) => Some(handle.window.get()), |
| 16 | RawWindowHandle::Xlib(handle) => u32::try_from(handle.window).ok(), |
| 17 | _ => None, |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub fn empty_input_region(display: Option<&str>, window: u32) -> Result<(), Box<dyn std::error::Error>> { |
| 22 | let (connection, _) = RustConnection::connect(display)?; |
| 23 | connection |
| 24 | .shape_rectangles(SO::SET, SK::INPUT, ClipOrdering::UNSORTED, window, 0, 0, &[])? |
| 25 | .check()?; |
| 26 | Ok(()) |
| 27 | } |
| 28 | |
| 29 | pub fn make_input_transparent(window: &Window) { |
| 30 | if let Some(id) = x11_window_id(window) { |
| 31 | let _ = empty_input_region(None, id); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn compositor_running(display: Option<&str>) -> Result<bool, Box<dyn std::error::Error>> { |
| 36 | let (connection, screen_number) = RustConnection::connect(display)?; |
| 37 | let selection = format!("_NET_WM_CM_S{screen_number}"); |
| 38 | let atom = connection.intern_atom(false, selection.as_bytes())?.reply()?.atom; |
| 39 | let owner = connection.get_selection_owner(atom)?.reply()?.owner; |
| 40 | Ok(owner != x11rb::NONE) |
| 41 | } |
| 42 | |
| 43 | pub fn window_transparency_available(window: &Window) -> bool { |
| 44 | static COMPOSITED: OnceLock<bool> = OnceLock::new(); |
| 45 | match HasWindowHandle::window_handle(window).map(|handle| handle.as_raw()) { |
| 46 | Ok(RawWindowHandle::Xcb(_) | RawWindowHandle::Xlib(_)) => { |
| 47 | *COMPOSITED.get_or_init(|| compositor_running(None).unwrap_or(false)) |
| 48 | } |
| 49 | _ => true, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn window_icon_property(png: &[u8]) -> Option<Vec<u32>> { |
| 54 | let decoded = image::load_from_memory(png).ok()?; |
| 55 | let mut property = Vec::new(); |
| 56 | for size in WINDOW_ICON_SIZES { |
| 57 | let resized = decoded |
| 58 | .resize_exact(size, size, image::imageops::FilterType::Lanczos3) |
| 59 | .to_rgba8(); |
| 60 | property.extend([size, size]); |
| 61 | property.extend(resized.pixels().map(|pixel| { |
| 62 | let [red, green, blue, alpha] = pixel.0; |
| 63 | u32::from_le_bytes([blue, green, red, alpha]) |
| 64 | })); |
| 65 | } |
| 66 | Some(property) |
| 67 | } |
| 68 | |
| 69 | pub fn write_window_icon( |
| 70 | display: Option<&str>, |
| 71 | window: u32, |
| 72 | property: &[u32], |
| 73 | ) -> Result<(), Box<dyn std::error::Error>> { |
| 74 | let (connection, _) = RustConnection::connect(display)?; |
| 75 | let atom = connection.intern_atom(false, b"_NET_WM_ICON")?.reply()?.atom; |
| 76 | connection |
| 77 | .change_property32(PropMode::REPLACE, window, atom, AtomEnum::CARDINAL, property)? |
| 78 | .check()?; |
| 79 | connection.flush()?; |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | pub fn set_window_icon(window: &Window, png: &[u8]) { |
| 84 | if let (Some(id), Some(property)) = (x11_window_id(window), window_icon_property(png)) { |
| 85 | let _ = write_window_icon(None, id, &property); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[cfg(test)] |
| 90 | mod tests { |
| 91 | use std::io::{BufRead, BufReader}; |
| 92 | use std::process::{Child, Command, Stdio}; |
| 93 | use std::sync::mpsc; |
| 94 | use std::time::Duration; |
| 95 | |
| 96 | use x11rb::connection::Connection; |
| 97 | use x11rb::protocol::xproto::{CreateWindowAux, WindowClass}; |
| 98 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 99 | |
| 100 | use super::*; |
| 101 | |
| 102 | const SERVER_START_TIMEOUT: Duration = Duration::from_secs(5); |
| 103 | |
| 104 | struct Server(Child); |
| 105 | |
| 106 | impl Drop for Server { |
| 107 | fn drop(&mut self) { |
| 108 | // SIGKILL leaves /tmp/.X<n>-lock behind, and the next run then cannot claim that display. |
| 109 | let _ = Command::new("kill").arg(self.0.id().to_string()).status(); |
| 110 | let _ = self.0.wait(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn start_server() -> Option<(Server, String)> { |
| 115 | let mut child = Command::new("Xvfb") |
| 116 | .args(["-displayfd", "1", "-screen", "0", "320x200x24", "-nolisten", "tcp", "-noreset"]) |
| 117 | .stdout(Stdio::piped()) |
| 118 | .stderr(Stdio::null()) |
| 119 | .spawn() |
| 120 | .ok()?; |
| 121 | let stdout = child.stdout.take()?; |
| 122 | let server = Server(child); |
| 123 | let (sender, receiver) = mpsc::channel(); |
| 124 | std::thread::spawn(move || { |
| 125 | let mut number = String::new(); |
| 126 | let _ = BufReader::new(stdout).read_line(&mut number); |
| 127 | let _ = sender.send(number); |
| 128 | }); |
| 129 | let number = receiver.recv_timeout(SERVER_START_TIMEOUT).ok()?; |
| 130 | let display = format!(":{}", number.trim()); |
| 131 | RustConnection::connect(Some(&display)).ok()?; |
| 132 | Some((server, display)) |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn the_border_window_gets_an_empty_input_region() { |
| 137 | let Some((_server, display)) = start_server() else { |
| 138 | return; |
| 139 | }; |
| 140 | let (connection, screen_number) = RustConnection::connect(Some(&display)).unwrap(); |
| 141 | let root = connection.setup().roots[screen_number].root; |
| 142 | let window = connection.generate_id().unwrap(); |
| 143 | connection |
| 144 | .create_window(COPY_DEPTH_FROM_PARENT, window, root, 0, 0, 100, 100, 0, WindowClass::INPUT_OUTPUT, 0, &CreateWindowAux::new()) |
| 145 | .unwrap(); |
| 146 | connection.flush().unwrap(); |
| 147 | let before = connection.shape_get_rectangles(window, SK::INPUT).unwrap().reply().unwrap(); |
| 148 | assert_eq!(before.rectangles.len(), 1); |
| 149 | empty_input_region(Some(&display), window).unwrap(); |
| 150 | let after = connection.shape_get_rectangles(window, SK::INPUT).unwrap().reply().unwrap(); |
| 151 | assert!(after.rectangles.is_empty()); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn transparency_needs_a_compositor_owning_the_cm_selection() { |
| 156 | let Some((_server, display)) = start_server() else { |
| 157 | return; |
| 158 | }; |
| 159 | assert!(!compositor_running(Some(&display)).unwrap()); |
| 160 | let (connection, screen_number) = RustConnection::connect(Some(&display)).unwrap(); |
| 161 | let root = connection.setup().roots[screen_number].root; |
| 162 | let window = connection.generate_id().unwrap(); |
| 163 | connection |
| 164 | .create_window(COPY_DEPTH_FROM_PARENT, window, root, 0, 0, 1, 1, 0, WindowClass::INPUT_OUTPUT, 0, &CreateWindowAux::new()) |
| 165 | .unwrap(); |
| 166 | let selection = format!("_NET_WM_CM_S{screen_number}"); |
| 167 | let atom = connection.intern_atom(false, selection.as_bytes()).unwrap().reply().unwrap().atom; |
| 168 | connection.set_selection_owner(window, atom, x11rb::CURRENT_TIME).unwrap().check().unwrap(); |
| 169 | assert!(compositor_running(Some(&display)).unwrap()); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn the_window_icon_property_holds_every_size_in_argb() { |
| 174 | let png = crate::appearance::AppIcon::MicrosoftTeams.png(); |
| 175 | let property = window_icon_property(png).unwrap(); |
| 176 | let expected: usize = WINDOW_ICON_SIZES |
| 177 | .iter() |
| 178 | .map(|size| 2 + (size * size) as usize) |
| 179 | .sum(); |
| 180 | assert_eq!(property.len(), expected); |
| 181 | assert_eq!(&property[..2], &[32, 32]); |
| 182 | assert!(window_icon_property(b"not a png").is_none()); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn the_window_icon_lands_in_net_wm_icon() { |
| 187 | let Some((_server, display)) = start_server() else { |
| 188 | return; |
| 189 | }; |
| 190 | let (connection, screen_number) = RustConnection::connect(Some(&display)).unwrap(); |
| 191 | let root = connection.setup().roots[screen_number].root; |
| 192 | let window = connection.generate_id().unwrap(); |
| 193 | connection |
| 194 | .create_window(COPY_DEPTH_FROM_PARENT, window, root, 0, 0, 100, 100, 0, WindowClass::INPUT_OUTPUT, 0, &CreateWindowAux::new()) |
| 195 | .unwrap(); |
| 196 | connection.flush().unwrap(); |
| 197 | let property = window_icon_property(crate::appearance::AppIcon::RustyTeams.png()).unwrap(); |
| 198 | write_window_icon(Some(&display), window, &property).unwrap(); |
| 199 | let atom = connection.intern_atom(false, b"_NET_WM_ICON").unwrap().reply().unwrap().atom; |
| 200 | let reply = connection |
| 201 | .get_property(false, window, atom, AtomEnum::CARDINAL, 0, u32::MAX) |
| 202 | .unwrap() |
| 203 | .reply() |
| 204 | .unwrap(); |
| 205 | let stored: Vec<u32> = reply.value32().unwrap().collect(); |
| 206 | assert_eq!(stored, property); |
| 207 | } |
| 208 | } |