Skip to content

Commit d0c859f

Browse files
committed
Simplify creation of the colormap associated to the discovered visual
1 parent 4fd042c commit d0c859f

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/x11/visual_info.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ pub(super) struct WindowVisualConfig {
1212

1313
pub visual_depth: u8,
1414
pub visual_id: Visualid,
15-
pub is_copy_from_parent: bool,
15+
pub color_map: Option<Colormap>,
1616
}
1717

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

2626
// SAFETY: TODO
@@ -29,24 +29,24 @@ impl WindowVisualConfig {
2929
}
3030
.expect("Could not fetch framebuffer config");
3131

32-
Self {
32+
Ok(Self {
3333
fb_config: Some(fb_config),
3434
visual_depth: window_config.depth,
3535
visual_id: window_config.visual,
36-
is_copy_from_parent: false,
37-
}
36+
color_map: Some(create_color_map(connection, window_config.visual)?),
37+
})
3838
}
3939

40-
pub fn find_best_visual_config(connection: &XcbConnection) -> Self {
40+
pub fn find_best_visual_config(connection: &XcbConnection) -> Result<Self, Box<dyn Error>> {
4141
match find_visual_for_depth(connection.screen(), 32) {
42-
None => Self::copy_from_parent(),
43-
Some(visual_id) => Self {
42+
None => Ok(Self::copy_from_parent()),
43+
Some(visual_id) => Ok(Self {
4444
#[cfg(feature = "opengl")]
4545
fb_config: None,
4646
visual_id,
4747
visual_depth: 32,
48-
is_copy_from_parent: false,
49-
},
48+
color_map: Some(create_color_map(connection, visual_id)?),
49+
}),
5050
}
5151
}
5252

@@ -56,29 +56,25 @@ impl WindowVisualConfig {
5656
fb_config: None,
5757
visual_depth: COPY_FROM_PARENT as u8,
5858
visual_id: COPY_FROM_PARENT,
59-
is_copy_from_parent: true,
59+
color_map: None,
6060
}
6161
}
62+
}
6263

63-
// For this 32-bit depth to work, you also need to define a color map and set a border
64-
// pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818
65-
pub fn create_color_map(
66-
&self, connection: &XcbConnection,
67-
) -> Result<Option<Colormap>, Box<dyn Error>> {
68-
if self.is_copy_from_parent {
69-
return Ok(None);
70-
}
71-
72-
let colormap = connection.conn2.generate_id()?;
73-
connection.conn2.create_colormap(
74-
ColormapAlloc::NONE,
75-
colormap,
76-
connection.screen().root,
77-
self.visual_id,
78-
)?;
64+
// For this 32-bit depth to work, you also need to define a color map and set a border
65+
// pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818
66+
pub fn create_color_map(
67+
connection: &XcbConnection, visual_id: Visualid,
68+
) -> Result<Colormap, Box<dyn Error>> {
69+
let colormap = connection.conn2.generate_id()?;
70+
connection.conn2.create_colormap(
71+
ColormapAlloc::NONE,
72+
colormap,
73+
connection.screen().root,
74+
visual_id,
75+
)?;
7976

80-
Ok(Some(colormap))
81-
}
77+
Ok(colormap)
8278
}
8379

8480
fn find_visual_for_depth(screen: &Screen, depth: u8) -> Option<Visualid> {

src/x11/window.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,10 @@ impl<'a> Window<'a> {
207207

208208
#[cfg(feature = "opengl")]
209209
let visual_info =
210-
WindowVisualConfig::find_best_visual_config_for_gl(&xcb_connection, options.gl_config);
210+
WindowVisualConfig::find_best_visual_config_for_gl(&xcb_connection, options.gl_config)?;
211211

212212
#[cfg(not(feature = "opengl"))]
213-
let visual_info = WindowVisualConfig::find_best_visual_config(&xcb_connection);
214-
215-
let color_map = visual_info.create_color_map(&xcb_connection)?;
213+
let visual_info = WindowVisualConfig::find_best_visual_config(&xcb_connection)?;
216214

217215
let window_id = xcb_connection.conn2.generate_id()?;
218216
xcb_connection.conn2.create_window(
@@ -240,7 +238,7 @@ impl<'a> Window<'a> {
240238
)
241239
// As mentioned above, these two values are needed to be able to create a window
242240
// with a depth of 32-bits when the parent window has a different depth
243-
.colormap(color_map)
241+
.colormap(visual_info.color_map)
244242
.border_pixel(0),
245243
)?;
246244
xcb_connection.conn2.map_window(window_id)?;

0 commit comments

Comments
 (0)