From 4010724e13492acbddc027124d1786c134943e38 Mon Sep 17 00:00:00 2001 From: sandmor Date: Sat, 21 Aug 2021 22:12:18 -0500 Subject: [PATCH 1/4] find transparent in x11 --- src/platform_impl/linux/x11/window.rs | 57 +++++++++++++++++++-------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 9f3d573695..940c137fba 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -8,6 +8,7 @@ use std::{ ptr, slice, sync::Arc, }; +use x11_dl::xlib::TrueColor; use libc; use mio_misc::channel::Sender; @@ -180,6 +181,32 @@ impl UnownedWindow { }; // creating + let (depth, visual) = match pl_attribs.visual_infos { + Some(vi) => (vi.depth, vi.visual), + None if window_attrs.transparent == true => { + // Find a suitable visual + let mut vinfo = MaybeUninit::uninit(); + if unsafe { + (xconn.xlib.XMatchVisualInfo)( + xconn.display, + screen_id, + 32, + TrueColor, + vinfo.as_mut_ptr(), + ) + } != 0 + { + let vinfo = unsafe { vinfo.assume_init() }; + (vinfo.depth, vinfo.visual) + } else { + // There is not a visual that matches the criteria of being `TrueColor` and 32 bits depth, + // and panic or error is not appropiate so just ignore it and continue + (ffi::CopyFromParent, ffi::CopyFromParent as *mut ffi::Visual) + } + } + _ => (ffi::CopyFromParent, ffi::CopyFromParent as *mut ffi::Visual), + }; + let mut set_win_attr = { let mut swa: ffi::XSetWindowAttributes = unsafe { mem::zeroed() }; swa.colormap = if let Some(vi) = pl_attribs.visual_infos { @@ -187,6 +214,8 @@ impl UnownedWindow { let visual = vi.visual; (xconn.xlib.XCreateColormap)(xconn.display, root, visual, ffi::AllocNone) } + } else if window_attrs.transparent { + unsafe { (xconn.xlib.XCreateColormap)(xconn.display, root, visual, ffi::AllocNone) } } else { 0 }; @@ -210,6 +239,11 @@ impl UnownedWindow { window_attributes |= ffi::CWOverrideRedirect; } + xconn + .sync_with_server() + .map_err(|x_err| os_error!(OsError::XError(x_err))) + .unwrap(); + // finally creating the window let xwindow = unsafe { (xconn.xlib.XCreateWindow)( @@ -220,28 +254,19 @@ impl UnownedWindow { dimensions.0 as c_uint, dimensions.1 as c_uint, 0, - match pl_attribs.visual_infos { - Some(vi) => vi.depth, - None => ffi::CopyFromParent, - }, + depth, ffi::InputOutput as c_uint, - // TODO: If window wants transparency and `visual_infos` is None, - // we need to find our own visual which has an `alphaMask` which - // is > 0, like we do in glutin. - // - // It is non obvious which masks, if any, we should pass to - // `XGetVisualInfo`. winit doesn't receive any info about what - // properties the user wants. Users should consider choosing the - // visual themselves as glutin does. - match pl_attribs.visual_infos { - Some(vi) => vi.visual, - None => ffi::CopyFromParent as *mut ffi::Visual, - }, + visual, window_attributes, &mut set_win_attr, ) }; + xconn + .sync_with_server() + .map_err(|x_err| os_error!(OsError::XError(x_err))) + .unwrap(); + let mut window = UnownedWindow { xconn: Arc::clone(xconn), xwindow, From d17789ff187d71b6322abdcda19c359a056f4722 Mon Sep 17 00:00:00 2001 From: sandmor Date: Sat, 21 Aug 2021 22:19:09 -0500 Subject: [PATCH 2/4] remove debug hooks --- src/platform_impl/linux/x11/window.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 940c137fba..f2e686e70c 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -239,11 +239,6 @@ impl UnownedWindow { window_attributes |= ffi::CWOverrideRedirect; } - xconn - .sync_with_server() - .map_err(|x_err| os_error!(OsError::XError(x_err))) - .unwrap(); - // finally creating the window let xwindow = unsafe { (xconn.xlib.XCreateWindow)( @@ -262,11 +257,6 @@ impl UnownedWindow { ) }; - xconn - .sync_with_server() - .map_err(|x_err| os_error!(OsError::XError(x_err))) - .unwrap(); - let mut window = UnownedWindow { xconn: Arc::clone(xconn), xwindow, From 786f7516a643b47b5d3d7d70ee94a2344312786d Mon Sep 17 00:00:00 2001 From: sandmor Date: Sat, 21 Aug 2021 22:32:01 -0500 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 623cd1ee61..6605956a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- On X11, select an appropriate visual for transparency if is requested - On Wayland and X11, fix diagonal window resize cursor orientation. - On macOS, drop the event callback before exiting. - On Android, implement `Window::request_redraw` From 81cd2b8c440039c9639b57cd4ade9bf749061d7c Mon Sep 17 00:00:00 2001 From: sandmor Date: Sun, 22 Aug 2021 13:11:26 -0500 Subject: [PATCH 4/4] polish and failed to find visual warning --- src/platform_impl/linux/x11/window.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index f2e686e70c..247a289bc3 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -181,30 +181,29 @@ impl UnownedWindow { }; // creating - let (depth, visual) = match pl_attribs.visual_infos { - Some(vi) => (vi.depth, vi.visual), + let (visual, depth) = match pl_attribs.visual_infos { + Some(vi) => (vi.visual, vi.depth), None if window_attrs.transparent == true => { // Find a suitable visual let mut vinfo = MaybeUninit::uninit(); - if unsafe { + let vinfo_initialized = unsafe { (xconn.xlib.XMatchVisualInfo)( xconn.display, screen_id, 32, TrueColor, vinfo.as_mut_ptr(), - ) - } != 0 - { + ) != 0 + }; + if vinfo_initialized { let vinfo = unsafe { vinfo.assume_init() }; - (vinfo.depth, vinfo.visual) + (vinfo.visual, vinfo.depth) } else { - // There is not a visual that matches the criteria of being `TrueColor` and 32 bits depth, - // and panic or error is not appropiate so just ignore it and continue - (ffi::CopyFromParent, ffi::CopyFromParent as *mut ffi::Visual) + debug!("Could not set transparency, because XMatchVisualInfo returned zero for the required parameters"); + (ffi::CopyFromParent as *mut ffi::Visual, ffi::CopyFromParent) } } - _ => (ffi::CopyFromParent, ffi::CopyFromParent as *mut ffi::Visual), + _ => (ffi::CopyFromParent as *mut ffi::Visual, ffi::CopyFromParent), }; let mut set_win_attr = {