Skip to content

Commit 625fde7

Browse files
committed
Create X11 windows with a depth of 32-bits
This should fix the inability to create OpenGL contexts with alpha channels in raw-gl-context, but it seems like that still needs a bit more work.
1 parent a801001 commit 625fde7

1 file changed

Lines changed: 50 additions & 13 deletions

File tree

src/x11/window.rs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,39 @@ impl Window {
205205

206206
let window_info = WindowInfo::from_logical_size(options.size, scaling);
207207

208+
// The window need to have a depth of 32 so so we can create graphics contexts with alpha
209+
// buffers
210+
let mut depth = xcb::COPY_FROM_PARENT as u8;
211+
let mut visual = xcb::COPY_FROM_PARENT as u32;
212+
'match_visual: for candidate_depth in screen.allowed_depths() {
213+
if candidate_depth.depth() != 32 {
214+
continue;
215+
}
216+
217+
for candidate_visual in candidate_depth.visuals() {
218+
if candidate_visual.class() == xcb::VISUAL_CLASS_TRUE_COLOR as u8 {
219+
depth = candidate_depth.depth();
220+
visual = candidate_visual.visual_id();
221+
break 'match_visual;
222+
}
223+
}
224+
}
225+
226+
// For this 32-bith depth to work, you also need to define a color map and set a border
227+
// pixel: https://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n818
228+
let colormap = xcb_connection.conn.generate_id();
229+
xcb::create_colormap(
230+
&xcb_connection.conn,
231+
xcb::COLORMAP_ALLOC_NONE as u8,
232+
colormap,
233+
screen.root(),
234+
visual,
235+
);
236+
208237
let window_id = xcb_connection.conn.generate_id();
209238
xcb::create_window_checked(
210239
&xcb_connection.conn,
211-
xcb::COPY_FROM_PARENT as u8,
240+
depth,
212241
window_id,
213242
parent_id,
214243
0, // x coordinate of the new window
@@ -217,18 +246,26 @@ impl Window {
217246
window_info.physical_size().height as u16, // window height
218247
0, // window border
219248
xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
220-
if parent.is_some() { xcb::COPY_FROM_PARENT as u32 } else { screen.root_visual() },
221-
&[(
222-
xcb::CW_EVENT_MASK,
223-
xcb::EVENT_MASK_EXPOSURE
224-
| xcb::EVENT_MASK_POINTER_MOTION
225-
| xcb::EVENT_MASK_BUTTON_PRESS
226-
| xcb::EVENT_MASK_BUTTON_RELEASE
227-
| xcb::EVENT_MASK_KEY_PRESS
228-
| xcb::EVENT_MASK_KEY_RELEASE
229-
| xcb::EVENT_MASK_STRUCTURE_NOTIFY,
230-
)],
231-
).request_check().unwrap();
249+
visual,
250+
&[
251+
(
252+
xcb::CW_EVENT_MASK,
253+
xcb::EVENT_MASK_EXPOSURE
254+
| xcb::EVENT_MASK_POINTER_MOTION
255+
| xcb::EVENT_MASK_BUTTON_PRESS
256+
| xcb::EVENT_MASK_BUTTON_RELEASE
257+
| xcb::EVENT_MASK_KEY_PRESS
258+
| xcb::EVENT_MASK_KEY_RELEASE
259+
| xcb::EVENT_MASK_STRUCTURE_NOTIFY,
260+
),
261+
// As mentioend above, these two values are needed to be able to create a window
262+
// with a dpeth of 32-bits when the parent window has a different depth
263+
(xcb::CW_COLORMAP, colormap),
264+
(xcb::CW_BORDER_PIXEL, 0),
265+
],
266+
)
267+
.request_check()
268+
.unwrap();
232269

233270
xcb::map_window(&xcb_connection.conn, window_id);
234271

0 commit comments

Comments
 (0)