linux: port from x11rb to xcb and hook up RawWindowHandle

This commit is contained in:
Dzmitry Malyshau 2024-01-28 21:50:58 -08:00
parent aed363d3c7
commit 7f8c64aa6c
6 changed files with 161 additions and 167 deletions

View file

@ -36,6 +36,7 @@ env_logger = { version = "0.9", optional = true }
etagere = "0.2" etagere = "0.2"
futures.workspace = true futures.workspace = true
gpui_macros = { path = "../gpui_macros" } gpui_macros = { path = "../gpui_macros" }
font-kit = { git = "https://github.com/zed-industries/font-kit", rev = "d97147f" }
image = "0.23" image = "0.23"
itertools = "0.10" itertools = "0.10"
lazy_static.workspace = true lazy_static.workspace = true
@ -48,7 +49,7 @@ parking_lot.workspace = true
pathfinder_geometry = "0.5" pathfinder_geometry = "0.5"
postage.workspace = true postage.workspace = true
rand.workspace = true rand.workspace = true
raw-window-handle = "0.6.0" raw-window-handle = "0.5.0"
refineable.workspace = true refineable.workspace = true
resvg = "0.14" resvg = "0.14"
schemars.workspace = true schemars.workspace = true
@ -96,4 +97,5 @@ objc = "0.2"
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
flume = "0.11" flume = "0.11"
x11rb = "0.13" xcb = { version = "1.3", features = ["as-raw-xcb-connection"] }
as-raw-xcb-connection = "1"

View file

@ -5,7 +5,6 @@ use crate::{
}; };
use anyhow::Result; use anyhow::Result;
use collections::FxHashMap; use collections::FxHashMap;
use derive_more::{Deref, DerefMut};
use etagere::BucketedAtlasAllocator; use etagere::BucketedAtlasAllocator;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, sync::Arc};

View file

@ -1,25 +1,24 @@
use crate::{point, size, Bounds, DisplayId, GlobalPixels, PlatformDisplay, Size}; use crate::{Bounds, DisplayId, GlobalPixels, PlatformDisplay, Size};
use anyhow::Result; use anyhow::Result;
use uuid::Uuid; use uuid::Uuid;
use x11rb::{connection::Connection as _, rust_connection::RustConnection};
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct LinuxDisplay { pub(crate) struct LinuxDisplay {
x11_screen_index: usize, x_screen_index: i32,
bounds: Bounds<GlobalPixels>, bounds: Bounds<GlobalPixels>,
uuid: Uuid, uuid: Uuid,
} }
impl LinuxDisplay { impl LinuxDisplay {
pub(crate) fn new(xc: &RustConnection, x11_screen_index: usize) -> Self { pub(crate) fn new(xc: &xcb::Connection, x_screen_index: i32) -> Self {
let screen = &xc.setup().roots[x11_screen_index]; let screen = xc.get_setup().roots().nth(x_screen_index as usize).unwrap();
Self { Self {
x11_screen_index, x_screen_index,
bounds: Bounds { bounds: Bounds {
origin: Default::default(), origin: Default::default(),
size: Size { size: Size {
width: GlobalPixels(screen.width_in_pixels as f32), width: GlobalPixels(screen.width_in_pixels() as f32),
height: GlobalPixels(screen.height_in_pixels as f32), height: GlobalPixels(screen.height_in_pixels() as f32),
}, },
}, },
uuid: Uuid::from_bytes([0; 16]), uuid: Uuid::from_bytes([0; 16]),
@ -29,7 +28,7 @@ impl LinuxDisplay {
impl PlatformDisplay for LinuxDisplay { impl PlatformDisplay for LinuxDisplay {
fn id(&self) -> DisplayId { fn id(&self) -> DisplayId {
DisplayId(self.x11_screen_index as u32) DisplayId(self.x_screen_index as u32)
} }
fn uuid(&self) -> Result<Uuid> { fn uuid(&self) -> Result<Uuid> {

View file

@ -19,48 +19,28 @@ use std::{
time::Duration, time::Duration,
}; };
use time::UtcOffset; use time::UtcOffset;
use x11rb::{ use xcb::{x, Xid as _};
connection::Connection as _,
protocol::{
xproto::{Atom, ConnectionExt as _},
Event,
},
rust_connection::RustConnection,
};
pub(crate) struct LinuxPlatform(Mutex<LinuxPlatformState>); xcb::atoms_struct! {
#[derive(Debug)]
pub(crate) struct WmAtoms { pub(crate) struct XcbAtoms {
pub protocols: Atom, pub wm_protocols => b"WM_PROTOCOLS",
pub delete_window: Atom, pub wm_del_window => b"WM_DELETE_WINDOW",
} wm_state => b"_NET_WM_STATE",
wm_state_maxv => b"_NET_WM_STATE_MAXIMIZED_VERT",
impl WmAtoms { wm_state_maxh => b"_NET_WM_STATE_MAXIMIZED_HORZ",
fn new(x11_connection: &RustConnection) -> Self {
Self {
protocols: x11_connection
.intern_atom(false, b"WM_PROTOCOLS")
.unwrap()
.reply()
.unwrap()
.atom,
delete_window: x11_connection
.intern_atom(false, b"WM_DELETE_WINDOW")
.unwrap()
.reply()
.unwrap()
.atom,
}
} }
} }
pub(crate) struct LinuxPlatform(Mutex<LinuxPlatformState>);
pub(crate) struct LinuxPlatformState { pub(crate) struct LinuxPlatformState {
x11_connection: RustConnection, xcb_connection: xcb::Connection,
x11_root_index: usize, x_root_index: i32,
atoms: WmAtoms, atoms: XcbAtoms,
background_executor: BackgroundExecutor, background_executor: BackgroundExecutor,
foreground_executor: ForegroundExecutor, foreground_executor: ForegroundExecutor,
windows: HashMap<u32, LinuxWindowStatePtr>, windows: HashMap<x::Window, LinuxWindowStatePtr>,
text_system: Arc<LinuxTextSystem>, text_system: Arc<LinuxTextSystem>,
} }
@ -72,14 +52,14 @@ impl Default for LinuxPlatform {
impl LinuxPlatform { impl LinuxPlatform {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
let (x11_connection, x11_root_index) = x11rb::connect(None).unwrap(); let (xcb_connection, x_root_index) = xcb::Connection::connect(None).unwrap();
let atoms = WmAtoms::new(&x11_connection); let atoms = XcbAtoms::intern_all(&xcb_connection).unwrap();
let dispatcher = Arc::new(LinuxDispatcher::new()); let dispatcher = Arc::new(LinuxDispatcher::new());
Self(Mutex::new(LinuxPlatformState { Self(Mutex::new(LinuxPlatformState {
x11_connection, xcb_connection,
x11_root_index, x_root_index,
atoms, atoms,
background_executor: BackgroundExecutor::new(dispatcher.clone()), background_executor: BackgroundExecutor::new(dispatcher.clone()),
foreground_executor: ForegroundExecutor::new(dispatcher), foreground_executor: ForegroundExecutor::new(dispatcher),
@ -105,54 +85,44 @@ impl Platform for LinuxPlatform {
fn run(&self, on_finish_launching: Box<dyn FnOnce()>) { fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
on_finish_launching(); on_finish_launching();
let mut need_repaint = HashSet::<u32>::default(); let mut need_repaint = HashSet::<x::Window>::default();
while !self.0.lock().windows.is_empty() { while !self.0.lock().windows.is_empty() {
let event = self.0.lock().x11_connection.wait_for_event().unwrap(); let event = self.0.lock().xcb_connection.wait_for_event().unwrap();
let mut event_option = Some(event); match event {
while let Some(event) = event_option { xcb::Event::X(x::Event::ClientMessage(ev)) => {
match event { if let x::ClientMessageData::Data32([atom, ..]) = ev.data() {
Event::Expose(event) => {
if event.count == 0 {
need_repaint.insert(event.window);
}
}
Event::ConfigureNotify(event) => {
let lock = self.0.lock();
let mut window = lock.windows[&event.window].lock();
window.resize(event.width, event.height);
}
Event::MotionNotify(_event) => {
//mouse_position = (event.event_x, event.event_y);
//need_repaint.insert(event.window);
}
Event::MapNotify(_) => {}
Event::ClientMessage(event) => {
let mut lock = self.0.lock(); let mut lock = self.0.lock();
let data = event.data.as_data32(); if atom == lock.atoms.wm_del_window.resource_id() {
if data[0] == lock.atoms.delete_window { // window "x" button clicked by user, we gracefully exit
{ {
let mut window = lock.windows[&event.window].lock(); let mut window = lock.windows[&ev.window()].lock();
window.destroy(); window.destroy();
} }
lock.windows.remove(&event.window); lock.windows.remove(&ev.window());
break;
} }
} }
Event::Error(error) => {
log::error!("X11 error {:?}", error);
}
_ => {}
} }
_ => {} /*
let lock = self.0.lock(); Event::Expose(event) => {
event_option = lock.x11_connection.poll_for_event().unwrap(); if event.count == 0 {
need_repaint.insert(event.window);
}
}
Event::ConfigureNotify(event) => {
let lock = self.0.lock();
let mut window = lock.windows[&event.window].lock();
window.resize(event.width, event.height);
}
_ => {}*/
} }
for x11_window in need_repaint.drain() { for x_window in need_repaint.drain() {
let lock = self.0.lock(); let lock = self.0.lock();
let mut window = lock.windows[&x11_window].lock(); let mut window = lock.windows[&x_window].lock();
window.paint(); window.paint();
lock.x11_connection.flush().unwrap(); lock.xcb_connection.flush();
} }
} }
} }
@ -171,10 +141,13 @@ impl Platform for LinuxPlatform {
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> { fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
let lock = self.0.lock(); let lock = self.0.lock();
let setup = lock.x11_connection.setup(); let setup = lock.xcb_connection.get_setup();
(0..setup.roots.len()) setup
.map(|id| { .roots()
Rc::new(LinuxDisplay::new(&lock.x11_connection, id)) as Rc<dyn PlatformDisplay> .enumerate()
.map(|(root_id, _)| {
Rc::new(LinuxDisplay::new(&lock.xcb_connection, root_id as i32))
as Rc<dyn PlatformDisplay>
}) })
.collect() .collect()
} }
@ -182,8 +155,8 @@ impl Platform for LinuxPlatform {
fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> { fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
let lock = self.0.lock(); let lock = self.0.lock();
Some(Rc::new(LinuxDisplay::new( Some(Rc::new(LinuxDisplay::new(
&lock.x11_connection, &lock.xcb_connection,
id.0 as usize, id.0 as i32,
))) )))
} }
@ -197,17 +170,17 @@ impl Platform for LinuxPlatform {
options: WindowOptions, options: WindowOptions,
) -> Box<dyn PlatformWindow> { ) -> Box<dyn PlatformWindow> {
let mut lock = self.0.lock(); let mut lock = self.0.lock();
let win_id = lock.x11_connection.generate_id().unwrap(); let x_window = lock.xcb_connection.generate_id();
let window_ptr = LinuxWindowState::new_ptr( let window_ptr = LinuxWindowState::new_ptr(
options, options,
handle, handle,
&lock.x11_connection, &lock.xcb_connection,
lock.x11_root_index, lock.x_root_index,
win_id, x_window,
&lock.atoms, &lock.atoms,
); );
lock.windows.insert(win_id, window_ptr.clone()); lock.windows.insert(x_window, window_ptr.clone());
Box::new(LinuxWindow(window_ptr)) Box::new(LinuxWindow(window_ptr))
} }

View file

@ -15,7 +15,7 @@ use font_kit::{
}; };
use parking_lot::RwLock; use parking_lot::RwLock;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::sync::Arc; use std::{borrow::Cow};
pub(crate) struct LinuxTextSystem(RwLock<LinuxTextSystemState>); pub(crate) struct LinuxTextSystem(RwLock<LinuxTextSystemState>);
@ -54,7 +54,7 @@ impl Default for LinuxTextSystem {
#[allow(unused)] #[allow(unused)]
impl PlatformTextSystem for LinuxTextSystem { impl PlatformTextSystem for LinuxTextSystem {
fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> Result<()> { fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
Ok(()) //TODO Ok(()) //TODO
} }
fn all_font_names(&self) -> Vec<String> { fn all_font_names(&self) -> Vec<String> {

View file

@ -1,28 +1,19 @@
use super::BladeRenderer; use super::BladeRenderer;
use crate::{ use crate::{
px, AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, BladeAtlas, Bounds, KeyDownEvent, AnyWindowHandle, BladeAtlas, LinuxDisplay, Pixels, PlatformDisplay, PlatformInputHandler,
Keystroke, LinuxDisplay, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput, PlatformWindow, Point, Size, WindowAppearance, WindowBounds, WindowOptions, XcbAtoms,
PlatformInputHandler, PlatformWindow, Point, Size, TileId, WindowAppearance, WindowBounds,
WindowOptions, WmAtoms,
}; };
use collections::HashMap;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
rc::{Rc, Weak}, ffi::c_void,
rc::Rc,
sync::{self, Arc}, sync::{self, Arc},
}; };
use x11rb::{ use xcb::{x, Xid as _};
connection::Connection as _,
protocol::xproto::{
AtomEnum, ConnectionExt as _, CreateWindowAux, EventMask, PropMode, WindowClass,
},
rust_connection::RustConnection,
wrapper::ConnectionExt as _,
};
pub(crate) struct LinuxWindowState { pub(crate) struct LinuxWindowState {
display: Rc<dyn PlatformDisplay>, display: Rc<dyn PlatformDisplay>,
x11_window: u32, x_window: x::Window,
window_bounds: WindowBounds, window_bounds: WindowBounds,
content_size: Size<Pixels>, content_size: Size<Pixels>,
sprite_atlas: Arc<BladeAtlas>, sprite_atlas: Arc<BladeAtlas>,
@ -33,29 +24,53 @@ pub(crate) type LinuxWindowStatePtr = Arc<Mutex<LinuxWindowState>>;
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct LinuxWindow(pub(crate) LinuxWindowStatePtr); 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 { impl LinuxWindowState {
pub fn new_ptr( pub fn new_ptr(
options: WindowOptions, options: WindowOptions,
handle: AnyWindowHandle, handle: AnyWindowHandle,
x11_connection: &RustConnection, xcb_connection: &xcb::Connection,
x11_main_screen_index: usize, x_main_screen_index: i32,
x11_window: u32, x_window: x::Window,
atoms: &WmAtoms, atoms: &XcbAtoms,
) -> LinuxWindowStatePtr { ) -> LinuxWindowStatePtr {
let x11_screen_index = options let x_screen_index = options
.display_id .display_id
.map_or(x11_main_screen_index, |did| did.0 as usize); .map_or(x_main_screen_index, |did| did.0 as i32);
let screen = &x11_connection.setup().roots[x11_screen_index]; let screen = xcb_connection
.get_setup()
.roots()
.nth(x_screen_index as usize)
.unwrap();
let win_aux = CreateWindowAux::new() let xcb_values = [
.event_mask( x::Cw::BackPixel(screen.white_pixel()),
EventMask::EXPOSURE | EventMask::STRUCTURE_NOTIFY | EventMask::POINTER_MOTION, x::Cw::EventMask(x::EventMask::EXPOSURE | x::EventMask::KEY_PRESS),
) ];
.background_pixel(screen.white_pixel);
let (bound_x, bound_y, bound_width, bound_height) = match options.bounds { let (bound_x, bound_y, bound_width, bound_height) = match options.bounds {
WindowBounds::Fullscreen | WindowBounds::Maximized => { 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) => ( WindowBounds::Fixed(bounds) => (
bounds.origin.x.0 as i16, bounds.origin.x.0 as i16,
@ -65,61 +80,67 @@ impl LinuxWindowState {
), ),
}; };
x11_connection xcb_connection.send_request(&x::CreateWindow {
.create_window( depth: x::COPY_FROM_PARENT as u8,
x11rb::COPY_DEPTH_FROM_PARENT, wid: x_window,
x11_window, parent: screen.root(),
screen.root, x: bound_x,
bound_x, y: bound_y,
bound_y, width: bound_width,
bound_width, height: bound_height,
bound_height, border_width: 0,
0, class: x::WindowClass::InputOutput,
WindowClass::INPUT_OUTPUT, visual: screen.root_visual(),
0, value_list: &xcb_values,
&win_aux, });
)
.unwrap();
if let Some(titlebar) = options.titlebar { if let Some(titlebar) = options.titlebar {
if let Some(title) = titlebar.title { if let Some(title) = titlebar.title {
x11_connection xcb_connection.send_request(&x::ChangeProperty {
.change_property8( mode: x::PropMode::Replace,
PropMode::REPLACE, window: x_window,
x11_window, property: x::ATOM_WM_NAME,
AtomEnum::WM_NAME, r#type: x::ATOM_STRING,
AtomEnum::STRING, data: title.as_bytes(),
title.as_bytes(), });
)
.unwrap();
} }
} }
x11_connection xcb_connection
.change_property32( .send_and_check_request(&x::ChangeProperty {
PropMode::REPLACE, mode: x::PropMode::Replace,
x11_window, window: x_window,
atoms.protocols, property: atoms.wm_protocols,
AtomEnum::ATOM, r#type: x::ATOM_ATOM,
&[atoms.delete_window], data: &[atoms.wm_del_window],
) })
.unwrap(); .unwrap();
x11_connection.map_window(x11_window).unwrap(); xcb_connection.send_request(&x::MapWindow { window: x_window });
x11_connection.flush().unwrap(); 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( let gpu = Arc::new(
unsafe { unsafe {
blade::Context::init(blade::ContextDesc { blade::Context::init_windowed(
validation: cfg!(debug_assertions), &raw_window,
capture: false, blade::ContextDesc {
}) validation: cfg!(debug_assertions),
capture: false,
},
)
} }
.unwrap(), .unwrap(),
); );
Arc::new(Mutex::new(Self { Arc::new(Mutex::new(Self {
display: Rc::new(LinuxDisplay::new(x11_connection, x11_screen_index)), display: Rc::new(LinuxDisplay::new(xcb_connection, x_screen_index)),
x11_window, x_window,
window_bounds: options.bounds, window_bounds: options.bounds,
content_size: Size { content_size: Size {
width: Pixels(bound_width as f32), width: Pixels(bound_width as f32),