| 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::process::{Child, Command, Stdio}; |
| 32 | use std::time::Duration; |
| 33 | |
| 34 | use x11rb::connection::Connection; |
| 35 | use x11rb::protocol::xproto::{ConnectionExt as _, CreateWindowAux, WindowClass}; |
| 36 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 37 | |
| 38 | use super::*; |
| 39 | |
| 40 | struct Server(Child); |
| 41 | |
| 42 | impl Drop for Server { |
| 43 | fn drop(&mut self) { |
| 44 | let _ = self.0.kill(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn start_server(display: &str) -> Option<Server> { |
| 49 | let child = Command::new("Xvfb") |
| 50 | .args([display, "-screen", "0", "320x200x24", "-nolisten", "tcp", "-noreset"]) |
| 51 | .stdout(Stdio::null()) |
| 52 | .stderr(Stdio::null()) |
| 53 | .spawn() |
| 54 | .ok()?; |
| 55 | let server = Server(child); |
| 56 | for _ in 0..50 { |
| 57 | if RustConnection::connect(Some(display)).is_ok() { |
| 58 | return Some(server); |
| 59 | } |
| 60 | std::thread::sleep(Duration::from_millis(100)); |
| 61 | } |
| 62 | None |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn the_border_window_gets_an_empty_input_region() { |
| 67 | let display = ":87"; |
| 68 | let Some(_server) = start_server(display) else { |
| 69 | return; |
| 70 | }; |
| 71 | let (connection, screen_number) = RustConnection::connect(Some(display)).unwrap(); |
| 72 | let root = connection.setup().roots[screen_number].root; |
| 73 | let window = connection.generate_id().unwrap(); |
| 74 | connection |
| 75 | .create_window(COPY_DEPTH_FROM_PARENT, window, root, 0, 0, 100, 100, 0, WindowClass::INPUT_OUTPUT, 0, &CreateWindowAux::new()) |
| 76 | .unwrap(); |
| 77 | connection.flush().unwrap(); |
| 78 | let before = connection.shape_get_rectangles(window, SK::INPUT).unwrap().reply().unwrap(); |
| 79 | assert_eq!(before.rectangles.len(), 1); |
| 80 | empty_input_region(Some(display), window).unwrap(); |
| 81 | let after = connection.shape_get_rectangles(window, SK::INPUT).unwrap().reply().unwrap(); |
| 82 | assert!(after.rectangles.is_empty()); |
| 83 | } |
| 84 | } |