linux: port from x11rb to xcb and hook up RawWindowHandle

This commit is contained in:
Dzmitry Malyshau
2024-02-01 21:40:54 -08:00
parent aed363d3c7
commit 7f8c64aa6c
6 changed files with 161 additions and 167 deletions
+89 -68
View File
@@ -1,28 +1,19 @@
use super::BladeRenderer;
use crate::{
px, AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, BladeAtlas, Bounds, KeyDownEvent,
Keystroke, LinuxDisplay, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput,
PlatformInputHandler, PlatformWindow, Point, Size, TileId, WindowAppearance, WindowBounds,
WindowOptions, WmAtoms,
AnyWindowHandle, BladeAtlas, LinuxDisplay, Pixels, PlatformDisplay, PlatformInputHandler,
PlatformWindow, Point, Size, WindowAppearance, WindowBounds, WindowOptions, XcbAtoms,
};
use collections::HashMap;
use parking_lot::Mutex;
use std::{
rc::{Rc, Weak},
ffi::c_void,
rc::Rc,
sync::{self, Arc},
};
use x11rb::{
connection::Connection as _,
protocol::xproto::{
AtomEnum, ConnectionExt as _, CreateWindowAux, EventMask, PropMode, WindowClass,
},
rust_connection::RustConnection,
wrapper::ConnectionExt as _,
};
use xcb::{x, Xid as _};
pub(crate) struct LinuxWindowState {
display: Rc<dyn PlatformDisplay>,
x11_window: u32,
x_window: x::Window,
window_bounds: WindowBounds,
content_size: Size<Pixels>,
sprite_atlas: Arc<BladeAtlas>,
@@ -33,29 +24,53 @@ pub(crate) type LinuxWindowStatePtr = Arc<Mutex<LinuxWindowState>>;
#[derive(Clone)]
pub(crate) struct LinuxWindow(pub(crate) LinuxWindowStatePtr);
struct RawWindow {
connection: *mut c_void,
screen_id: i32,
window_id: u32,
}
unsafe impl raw_window_handle::HasRawWindowHandle for RawWindow {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
let mut wh = raw_window_handle::XcbWindowHandle::empty();
wh.window = self.window_id;
wh.into()
}
}
unsafe impl raw_window_handle::HasRawDisplayHandle for RawWindow {
fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
let mut dh = raw_window_handle::XcbDisplayHandle::empty();
dh.connection = self.connection;
dh.screen = self.screen_id;
dh.into()
}
}
impl LinuxWindowState {
pub fn new_ptr(
options: WindowOptions,
handle: AnyWindowHandle,
x11_connection: &RustConnection,
x11_main_screen_index: usize,
x11_window: u32,
atoms: &WmAtoms,
xcb_connection: &xcb::Connection,
x_main_screen_index: i32,
x_window: x::Window,
atoms: &XcbAtoms,
) -> LinuxWindowStatePtr {
let x11_screen_index = options
let x_screen_index = options
.display_id
.map_or(x11_main_screen_index, |did| did.0 as usize);
let screen = &x11_connection.setup().roots[x11_screen_index];
.map_or(x_main_screen_index, |did| did.0 as i32);
let screen = xcb_connection
.get_setup()
.roots()
.nth(x_screen_index as usize)
.unwrap();
let win_aux = CreateWindowAux::new()
.event_mask(
EventMask::EXPOSURE | EventMask::STRUCTURE_NOTIFY | EventMask::POINTER_MOTION,
)
.background_pixel(screen.white_pixel);
let xcb_values = [
x::Cw::BackPixel(screen.white_pixel()),
x::Cw::EventMask(x::EventMask::EXPOSURE | x::EventMask::KEY_PRESS),
];
let (bound_x, bound_y, bound_width, bound_height) = match options.bounds {
WindowBounds::Fullscreen | WindowBounds::Maximized => {
(0, 0, screen.width_in_pixels, screen.height_in_pixels)
(0, 0, screen.width_in_pixels(), screen.height_in_pixels())
}
WindowBounds::Fixed(bounds) => (
bounds.origin.x.0 as i16,
@@ -65,61 +80,67 @@ impl LinuxWindowState {
),
};
x11_connection
.create_window(
x11rb::COPY_DEPTH_FROM_PARENT,
x11_window,
screen.root,
bound_x,
bound_y,
bound_width,
bound_height,
0,
WindowClass::INPUT_OUTPUT,
0,
&win_aux,
)
.unwrap();
xcb_connection.send_request(&x::CreateWindow {
depth: x::COPY_FROM_PARENT as u8,
wid: x_window,
parent: screen.root(),
x: bound_x,
y: bound_y,
width: bound_width,
height: bound_height,
border_width: 0,
class: x::WindowClass::InputOutput,
visual: screen.root_visual(),
value_list: &xcb_values,
});
if let Some(titlebar) = options.titlebar {
if let Some(title) = titlebar.title {
x11_connection
.change_property8(
PropMode::REPLACE,
x11_window,
AtomEnum::WM_NAME,
AtomEnum::STRING,
title.as_bytes(),
)
.unwrap();
xcb_connection.send_request(&x::ChangeProperty {
mode: x::PropMode::Replace,
window: x_window,
property: x::ATOM_WM_NAME,
r#type: x::ATOM_STRING,
data: title.as_bytes(),
});
}
}
x11_connection
.change_property32(
PropMode::REPLACE,
x11_window,
atoms.protocols,
AtomEnum::ATOM,
&[atoms.delete_window],
)
xcb_connection
.send_and_check_request(&x::ChangeProperty {
mode: x::PropMode::Replace,
window: x_window,
property: atoms.wm_protocols,
r#type: x::ATOM_ATOM,
data: &[atoms.wm_del_window],
})
.unwrap();
x11_connection.map_window(x11_window).unwrap();
x11_connection.flush().unwrap();
xcb_connection.send_request(&x::MapWindow { window: x_window });
xcb_connection.flush().unwrap();
let raw_window = RawWindow {
connection: as_raw_xcb_connection::AsRawXcbConnection::as_raw_xcb_connection(
xcb_connection,
) as *mut _,
screen_id: x_screen_index,
window_id: x_window.resource_id(),
};
let gpu = Arc::new(
unsafe {
blade::Context::init(blade::ContextDesc {
validation: cfg!(debug_assertions),
capture: false,
})
blade::Context::init_windowed(
&raw_window,
blade::ContextDesc {
validation: cfg!(debug_assertions),
capture: false,
},
)
}
.unwrap(),
);
Arc::new(Mutex::new(Self {
display: Rc::new(LinuxDisplay::new(x11_connection, x11_screen_index)),
x11_window,
display: Rc::new(LinuxDisplay::new(xcb_connection, x_screen_index)),
x_window,
window_bounds: options.bounds,
content_size: Size {
width: Pixels(bound_width as f32),