| 1 | use gpui_kit::Window; |
| 2 | use raw_window_handle::{HasWindowHandle, RawWindowHandle}; |
| 3 | use x11rb::protocol::shape::{ConnectionExt as _, SK, SO}; |
| 4 | use x11rb::protocol::xproto::ClipOrdering; |
| 5 | use x11rb::rust_connection::RustConnection; |
| 6 | |
| 7 | fn x11_window_id(window: &Window) -> Option<u32> { |
| 8 | match HasWindowHandle::window_handle(window).ok()?.as_raw() { |
| 9 | RawWindowHandle::Xcb(handle) => Some(handle.window.get()), |
| 10 | RawWindowHandle::Xlib(handle) => u32::try_from(handle.window).ok(), |
| 11 | _ => None, |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | pub fn empty_input_region(display: Option<&str>, window: u32) -> Result<(), Box<dyn std::error::Error>> { |
| 16 | let (connection, _) = RustConnection::connect(display)?; |
| 17 | connection |
| 18 | .shape_rectangles(SO::SET, SK::INPUT, ClipOrdering::UNSORTED, window, 0, 0, &[])? |
| 19 | .check()?; |
| 20 | Ok(()) |
| 21 | } |
| 22 | |
| 23 | pub fn make_input_transparent(window: &Window) { |
| 24 | if let Some(id) = x11_window_id(window) { |
| 25 | let _ = empty_input_region(None, id); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | #[cfg(test)] |
| 30 | mod tests { |
| 31 | use std::io::{BufRead, BufReader}; |
| 32 | use std::process::{Child, Command, Stdio}; |
| 33 | use std::sync::mpsc; |
| 34 | use std::time::Duration; |
| 35 | |
| 36 | use x11rb::connection::Connection; |
| 37 | use x11rb::protocol::xproto::{ConnectionExt as _, CreateWindowAux, WindowClass}; |
| 38 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 39 | |
| 40 | use super::*; |
| 41 | |
| 42 | const SERVER_START_TIMEOUT: Duration = Duration::from_secs(5); |
| 43 | |
| 44 | struct Server(Child); |
| 45 | |
| 46 | impl Drop for Server { |
| 47 | fn drop(&mut self) { |
| 48 | // SIGKILL leaves /tmp/.X<n>-lock behind, and the next run then cannot claim that display. |
| 49 | let _ = Command::new("kill").arg(self.0.id().to_string()).status(); |
| 50 | let _ = self.0.wait(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn start_server() -> Option<(Server, String)> { |
| 55 | let mut child = Command::new("Xvfb") |
| 56 | .args(["-displayfd", "1", "-screen", "0", "320x200x24", "-nolisten", "tcp", "-noreset"]) |
| 57 | .stdout(Stdio::piped()) |
| 58 | .stderr(Stdio::null()) |
| 59 | .spawn() |
| 60 | .ok()?; |
| 61 | let stdout = child.stdout.take()?; |
| 62 | let server = Server(child); |
| 63 | let (sender, receiver) = mpsc::channel(); |
| 64 | std::thread::spawn(move || { |
| 65 | let mut number = String::new(); |
| 66 | let _ = BufReader::new(stdout).read_line(&mut number); |
| 67 | let _ = sender.send(number); |
| 68 | }); |
| 69 | let number = receiver.recv_timeout(SERVER_START_TIMEOUT).ok()?; |
| 70 | let display = format!(":{}", number.trim()); |
| 71 | RustConnection::connect(Some(&display)).ok()?; |
| 72 | Some((server, display)) |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn the_border_window_gets_an_empty_input_region() { |
| 77 | let Some((_server, display)) = start_server() else { |
| 78 | return; |
| 79 | }; |
| 80 | let (connection, screen_number) = RustConnection::connect(Some(&display)).unwrap(); |
| 81 | let root = connection.setup().roots[screen_number].root; |
| 82 | let window = connection.generate_id().unwrap(); |
| 83 | connection |
| 84 | .create_window(COPY_DEPTH_FROM_PARENT, window, root, 0, 0, 100, 100, 0, WindowClass::INPUT_OUTPUT, 0, &CreateWindowAux::new()) |
| 85 | .unwrap(); |
| 86 | connection.flush().unwrap(); |
| 87 | let before = connection.shape_get_rectangles(window, SK::INPUT).unwrap().reply().unwrap(); |
| 88 | assert_eq!(before.rectangles.len(), 1); |
| 89 | empty_input_region(Some(&display), window).unwrap(); |
| 90 | let after = connection.shape_get_rectangles(window, SK::INPUT).unwrap().reply().unwrap(); |
| 91 | assert!(after.rectangles.is_empty()); |
| 92 | } |
| 93 | } |