From 7f9c8f0bc42a589091c59979dcbf2a895ae0b5ec Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sun, 23 Jun 2024 09:24:50 -0700 Subject: [PATCH] m: Review comments Signed-off-by: John Nunley --- examples/window.rs | 2 +- src/platform_impl/linux/x11/util/cursor.rs | 71 +++++++++++----------- src/platform_impl/linux/x11/xdisplay.rs | 11 ++++ 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index 11f324713c..70e5b197aa 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -130,7 +130,7 @@ impl Application { #[allow(unused_mut)] let mut window_attributes = Window::default_attributes() .with_title("Winit window") - .with_transparent(true) + //.with_transparent(true) .with_window_icon(Some(self.icon.clone())); #[cfg(any(x11_platform, wayland_platform))] diff --git a/src/platform_impl/linux/x11/util/cursor.rs b/src/platform_impl/linux/x11/util/cursor.rs index 7ffc2bb298..c244a33ca1 100644 --- a/src/platform_impl/linux/x11/util/cursor.rs +++ b/src/platform_impl/linux/x11/util/cursor.rs @@ -33,48 +33,52 @@ impl XConnection { &self, width: u16, height: u16, - depth: u8, hotspot_x: u16, hotspot_y: u16, image: &[u8], ) -> Result { - // Create a pixap for the default root window. + // Create a pixmap for the default root window. let root = self.default_root().root; - let pixmap = self.xcb_connection().generate_id()?; - self.xcb_connection().create_pixmap(depth, pixmap, root, width, height)?.ignore_error(); - let pixmap_guard = CallOnDrop(|| { - self.xcb_connection().free_pixmap(pixmap).map(|r| r.ignore_error()).ok(); - }); + let pixmap = + xproto::PixmapWrapper::create_pixmap(self.xcb_connection(), 32, root, width, height)?; // Create a GC to draw with. - let gc = self.xcb_connection().generate_id()?; - self.xcb_connection().create_gc(gc, pixmap, &Default::default())?.ignore_error(); - let gc_guard = CallOnDrop(|| { - self.xcb_connection().free_gc(gc).map(|r| r.ignore_error()).ok(); - }); + let gc = xproto::GcontextWrapper::create_gc( + self.xcb_connection(), + pixmap.pixmap(), + &Default::default(), + )?; // Draw the data into it. - let format = - if depth == 1 { xproto::ImageFormat::XY_PIXMAP } else { xproto::ImageFormat::Z_PIXMAP }; self.xcb_connection() - .put_image(format, pixmap, gc, width, height, 0, 0, 0, depth, image)? + .put_image( + xproto::ImageFormat::Z_PIXMAP, + pixmap.pixmap(), + gc.gcontext(), + width, + height, + 0, + 0, + 0, + 32, + image, + )? .ignore_error(); - drop(gc_guard); + drop(gc); // Create the XRender picture. - let picture = self.xcb_connection().generate_id()?; - self.xcb_connection() - .render_create_picture(picture, pixmap, self.find_argb32_format(), &Default::default())? - .check()?; - let _picture_guard = CallOnDrop(|| { - self.xcb_connection().render_free_picture(picture).map(|r| r.ignore_error()).ok(); - }); - drop(pixmap_guard); + let picture = render::PictureWrapper::create_picture( + self.xcb_connection(), + pixmap.pixmap(), + self.find_argb32_format(), + &Default::default(), + )?; + drop(pixmap); // Create the cursor. let cursor = self.xcb_connection().generate_id()?; self.xcb_connection() - .render_create_cursor(cursor, picture, hotspot_x, hotspot_y)? + .render_create_cursor(cursor, picture.picture(), hotspot_x, hotspot_y)? .check()?; Ok(cursor) @@ -104,7 +108,7 @@ impl XConnection { } fn create_empty_cursor(&self) -> Result { - self.create_cursor_from_image(1, 1, 32, 0, 0, &[0, 0, 0, 0]) + self.create_cursor_from_image(1, 1, 0, 0, &[0, 0, 0, 0]) } fn get_cursor(&self, cursor: Option) -> Result { @@ -181,7 +185,14 @@ impl CustomCursor { ) -> CustomCursor { // Reverse RGBA order to BGRA. cursor.0.rgba.chunks_mut(4).for_each(|chunk| { + let chunk: &mut [u8; 4] = chunk.try_into().unwrap(); chunk[0..3].reverse(); + + // Byteswap if we need to. + if event_loop.xconn.needs_endian_swap() { + let value = u32::from_ne_bytes(*chunk).swap_bytes(); + *chunk = value.to_ne_bytes(); + } }); let cursor = event_loop @@ -189,7 +200,6 @@ impl CustomCursor { .create_cursor_from_image( cursor.0.width, cursor.0.height, - 32, cursor.0.hotspot_x, cursor.0.hotspot_y, &cursor.0.rgba, @@ -217,10 +227,3 @@ impl Default for SelectedCursor { SelectedCursor::Named(Default::default()) } } - -struct CallOnDrop(F); -impl Drop for CallOnDrop { - fn drop(&mut self) { - (self.0)(); - } -} diff --git a/src/platform_impl/linux/x11/xdisplay.rs b/src/platform_impl/linux/x11/xdisplay.rs index bf1b4cbb78..f26de0db1e 100644 --- a/src/platform_impl/linux/x11/xdisplay.rs +++ b/src/platform_impl/linux/x11/xdisplay.rs @@ -273,6 +273,17 @@ impl XConnection { pub fn render_formats(&self) -> &render::QueryPictFormatsReply { &self.render_formats } + + /// Do we need to do an endian swap? + #[inline] + pub fn needs_endian_swap(&self) -> bool { + #[cfg(target_endian = "big")] + let endian = xproto::ImageOrder::MSB_FIRST; + #[cfg(not(target_endian = "big"))] + let endian = xproto::ImageOrder::LSB_FIRST; + + self.xcb_connection().setup().image_byte_order != endian + } } impl fmt::Debug for XConnection {