Skip to content

Commit

Permalink
Simplify creation of the colormap associated to the discovered visual
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Mar 27, 2024
1 parent 4fd042c commit d0c859f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
54 changes: 25 additions & 29 deletions src/x11/visual_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ pub(super) struct WindowVisualConfig {

pub visual_depth: u8,
pub visual_id: Visualid,
pub is_copy_from_parent: bool,
pub color_map: Option<Colormap>,
}

// TODO: make visual negotiation actually check all of a visual's parameters
impl WindowVisualConfig {
#[cfg(feature = "opengl")]
pub fn find_best_visual_config_for_gl(
connection: &XcbConnection, gl_config: Option<crate::gl::GlConfig>,
) -> Self {
) -> Result<Self, Box<dyn Error>> {
let Some(gl_config) = gl_config else { return Self::find_best_visual_config(connection) };

// SAFETY: TODO
Expand All @@ -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<Self, Box<dyn Error>> {
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)?),
}),
}
}

Expand All @@ -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<Option<Colormap>, Box<dyn Error>> {
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<Colormap, Box<dyn Error>> {
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<Visualid> {
Expand Down
8 changes: 3 additions & 5 deletions src/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit d0c859f

Please sign in to comment.