api: make OwnedDisplayHandle wrap an opaque type

This will help in case we want to linger the event loop during drop
to prevent use after free in the consumers code.
This commit is contained in:
Kirill Chibisov 2024-11-13 15:29:05 +03:00 committed by GitHub
parent f781e13166
commit 59b1eb5410
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 149 additions and 177 deletions

View file

@ -28,13 +28,13 @@ use crate::event::{DeviceId, Event, StartCause, WindowEvent};
use crate::event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider,
OwnedDisplayHandle as RootOwnedDisplayHandle,
OwnedDisplayHandle as CoreOwnedDisplayHandle,
};
use crate::platform::pump_events::PumpStatus;
use crate::platform_impl::common::xkb::Context;
use crate::platform_impl::platform::min_timeout;
use crate::platform_impl::x11::window::Window;
use crate::platform_impl::{OwnedDisplayHandle, PlatformCustomCursor};
use crate::platform_impl::PlatformCustomCursor;
use crate::window::{
CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow,
WindowAttributes, WindowId,
@ -648,20 +648,6 @@ impl ActiveEventLoop {
.expect_then_ignore_error("Failed to update device event filter");
}
pub fn raw_display_handle_rwh_06(
&self,
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
let display_handle = rwh_06::XlibDisplayHandle::new(
// SAFETY: display will never be null
Some(
std::ptr::NonNull::new(self.xconn.display as *mut _)
.expect("X11 display should never be null"),
),
self.xconn.default_screen_index() as c_int,
);
Ok(display_handle.into())
}
pub(crate) fn clear_exit(&self) {
self.exit.set(None)
}
@ -739,9 +725,8 @@ impl RootActiveEventLoop for ActiveEventLoop {
self.exit.get().is_some()
}
fn owned_display_handle(&self) -> RootOwnedDisplayHandle {
let handle = OwnedDisplayHandle::X(self.x_connection().clone());
RootOwnedDisplayHandle { platform: handle }
fn owned_display_handle(&self) -> CoreOwnedDisplayHandle {
CoreOwnedDisplayHandle::new(self.x_connection().clone())
}
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
@ -751,8 +736,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
impl rwh_06::HasDisplayHandle for ActiveEventLoop {
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
let raw = self.raw_display_handle_rwh_06()?;
unsafe { Ok(rwh_06::DisplayHandle::borrow_raw(raw)) }
self.xconn.display_handle()
}
}

View file

@ -1,9 +1,11 @@
use std::collections::HashMap;
use std::error::Error;
use std::ffi::c_int;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard};
use std::{fmt, ptr};
use rwh_06::HasDisplayHandle;
use x11rb::connection::Connection;
use x11rb::protocol::randr::ConnectionExt as _;
use x11rb::protocol::render;
@ -62,6 +64,13 @@ pub struct XConnection {
pub cursor_cache: Mutex<HashMap<Option<CursorIcon>, xproto::Cursor>>,
}
impl HasDisplayHandle for XConnection {
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
let raw = self.raw_display_handle()?;
unsafe { Ok(rwh_06::DisplayHandle::borrow_raw(raw)) }
}
}
unsafe impl Send for XConnection {}
unsafe impl Sync for XConnection {}
@ -284,6 +293,19 @@ impl XConnection {
self.xcb_connection().setup().image_byte_order != endian
}
pub fn raw_display_handle(&self) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
let display_handle = rwh_06::XlibDisplayHandle::new(
// SAFETY: display will never be null
Some(
std::ptr::NonNull::new(self.display as *mut _)
.expect("X11 display should never be null"),
),
self.default_screen_index() as c_int,
);
Ok(display_handle.into())
}
}
impl fmt::Debug for XConnection {