diff --git a/src/x11/visual_info.rs b/src/x11/visual_info.rs index de94ee5..99a250b 100644 --- a/src/x11/visual_info.rs +++ b/src/x11/visual_info.rs @@ -12,7 +12,7 @@ pub(super) struct WindowVisualConfig { pub visual_depth: u8, pub visual_id: Visualid, - pub is_copy_from_parent: bool, + pub color_map: Option, } // TODO: make visual negotiation actually check all of a visual's parameters @@ -20,7 +20,7 @@ impl WindowVisualConfig { #[cfg(feature = "opengl")] pub fn find_best_visual_config_for_gl( connection: &XcbConnection, gl_config: Option, - ) -> Self { + ) -> Result> { let Some(gl_config) = gl_config else { return Self::find_best_visual_config(connection) }; // SAFETY: TODO @@ -29,24 +29,24 @@ impl WindowVisualConfig { } .expect("Could not fetch framebuffer config"); - Self { + Ok(Self { fb_config: Some(fb_config), visual_depth: window_config.depth, visual_id: window_config.visual, - is_copy_from_parent: false, - } + color_map: Some(create_color_map(connection, window_config.visual)?), + }) } - pub fn find_best_visual_config(connection: &XcbConnection) -> Self { + pub fn find_best_visual_config(connection: &XcbConnection) -> Result> { match find_visual_for_depth(connection.screen(), 32) { - None => Self::copy_from_parent(), - Some(visual_id) => Self { + None => Ok(Self::copy_from_parent()), + Some(visual_id) => Ok(Self { #[cfg(feature = "opengl")] fb_config: None, visual_id, visual_depth: 32, - is_copy_from_parent: false, - }, + color_map: Some(create_color_map(connection, visual_id)?), + }), } } @@ -56,29 +56,25 @@ impl WindowVisualConfig { fb_config: None, visual_depth: COPY_FROM_PARENT as u8, visual_id: COPY_FROM_PARENT, - is_copy_from_parent: true, + color_map: None, } } +} - // For this 32-bit depth to work, you also need to define a color map and set a border - // pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818 - pub fn create_color_map( - &self, connection: &XcbConnection, - ) -> Result, Box> { - if self.is_copy_from_parent { - return Ok(None); - } - - let colormap = connection.conn2.generate_id()?; - connection.conn2.create_colormap( - ColormapAlloc::NONE, - colormap, - connection.screen().root, - self.visual_id, - )?; +// For this 32-bit depth to work, you also need to define a color map and set a border +// pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818 +pub fn create_color_map( + connection: &XcbConnection, visual_id: Visualid, +) -> Result> { + let colormap = connection.conn2.generate_id()?; + connection.conn2.create_colormap( + ColormapAlloc::NONE, + colormap, + connection.screen().root, + visual_id, + )?; - Ok(Some(colormap)) - } + Ok(colormap) } fn find_visual_for_depth(screen: &Screen, depth: u8) -> Option { diff --git a/src/x11/window.rs b/src/x11/window.rs index f0f7539..7bbc9cc 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -207,12 +207,10 @@ impl<'a> Window<'a> { #[cfg(feature = "opengl")] let visual_info = - WindowVisualConfig::find_best_visual_config_for_gl(&xcb_connection, options.gl_config); + WindowVisualConfig::find_best_visual_config_for_gl(&xcb_connection, options.gl_config)?; #[cfg(not(feature = "opengl"))] - let visual_info = WindowVisualConfig::find_best_visual_config(&xcb_connection); - - let color_map = visual_info.create_color_map(&xcb_connection)?; + let visual_info = WindowVisualConfig::find_best_visual_config(&xcb_connection)?; let window_id = xcb_connection.conn2.generate_id()?; xcb_connection.conn2.create_window( @@ -240,7 +238,7 @@ impl<'a> Window<'a> { ) // As mentioned above, these two values are needed to be able to create a window // with a depth of 32-bits when the parent window has a different depth - .colormap(color_map) + .colormap(visual_info.color_map) .border_pixel(0), )?; xcb_connection.conn2.map_window(window_id)?;