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