Skip to content

Commit

Permalink
m: Review comments
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Jun 23, 2024
1 parent f19993a commit 7f9c8f0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
71 changes: 37 additions & 34 deletions src/platform_impl/linux/x11/util/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,52 @@ impl XConnection {
&self,
width: u16,
height: u16,
depth: u8,
hotspot_x: u16,
hotspot_y: u16,
image: &[u8],
) -> Result<xproto::Cursor, X11Error> {
// 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)
Expand Down Expand Up @@ -104,7 +108,7 @@ impl XConnection {
}

fn create_empty_cursor(&self) -> Result<xproto::Cursor, X11Error> {
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<CursorIcon>) -> Result<xproto::Cursor, X11Error> {
Expand Down Expand Up @@ -181,15 +185,21 @@ 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
.xconn
.create_cursor_from_image(
cursor.0.width,
cursor.0.height,
32,
cursor.0.hotspot_x,
cursor.0.hotspot_y,
&cursor.0.rgba,
Expand Down Expand Up @@ -217,10 +227,3 @@ impl Default for SelectedCursor {
SelectedCursor::Named(Default::default())
}
}

struct CallOnDrop<F: FnMut()>(F);
impl<F: FnMut()> Drop for CallOnDrop<F> {
fn drop(&mut self) {
(self.0)();
}
}
11 changes: 11 additions & 0 deletions src/platform_impl/linux/x11/xdisplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7f9c8f0

Please sign in to comment.