Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

72
vendor/raw-window-handle/src/android.rs vendored Normal file
View File

@@ -0,0 +1,72 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for Android.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AndroidDisplayHandle {}
impl AndroidDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::AndroidDisplayHandle;
/// let handle = AndroidDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create an Android-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::android();
/// do_something(handle);
/// ```
pub fn android() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(AndroidDisplayHandle::new().into()) }
}
}
/// Raw window handle for Android NDK.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AndroidNdkWindowHandle {
/// A pointer to an `ANativeWindow`.
pub a_native_window: NonNull<c_void>,
}
impl AndroidNdkWindowHandle {
/// Create a new handle to an `ANativeWindow`.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::AndroidNdkWindowHandle;
/// # type ANativeWindow = ();
/// #
/// let ptr: NonNull<ANativeWindow>;
/// # ptr = NonNull::from(&());
/// let handle = AndroidNdkWindowHandle::new(ptr.cast());
/// ```
pub fn new(a_native_window: NonNull<c_void>) -> Self {
Self { a_native_window }
}
}

111
vendor/raw-window-handle/src/appkit.rs vendored Normal file
View File

@@ -0,0 +1,111 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for AppKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AppKitDisplayHandle {}
impl AppKitDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::AppKitDisplayHandle;
/// let handle = AppKitDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create an AppKit-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::appkit();
/// do_something(handle);
/// ```
pub fn appkit() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(AppKitDisplayHandle::new().into()) }
}
}
/// Raw window handle for AppKit.
///
/// Note that `NSView` can only be accessed from the main thread of the
/// application. This struct is `!Send` and `!Sync` to help with ensuring
/// that.
///
/// # Example
///
/// Getting the view from a [`WindowHandle`][crate::WindowHandle].
///
/// ```no_run
/// # fn inner() {
/// #![cfg(target_os = "macos")]
/// # #[cfg(requires_objc2)]
/// use objc2_app_kit::NSView;
/// # #[cfg(requires_objc2)]
/// use objc2_foundation::is_main_thread;
/// # #[cfg(requires_objc2)]
/// use objc2::rc::Id;
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
///
/// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
/// # handle = unimplemented!();
/// match handle.as_raw() {
/// # #[cfg(requires_objc2)]
/// RawWindowHandle::AppKit(handle) => {
/// assert!(is_main_thread(), "can only access AppKit handles on the main thread");
/// let ns_view = handle.ns_view.as_ptr();
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
/// // that the `AppKitWindowHandle` contains a valid pointer to an
/// // `NSView`.
/// // Unwrap is fine, since the pointer came from `NonNull`.
/// let ns_view: Id<NSView> = unsafe { Id::retain(ns_view.cast()) }.unwrap();
/// // Do something with the NSView here, like getting the `NSWindow`
/// let ns_window = ns_view.window().expect("view was not installed in a window");
/// }
/// handle => unreachable!("unknown handle {handle:?} for platform"),
/// }
/// # }
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AppKitWindowHandle {
/// A pointer to an `NSView` object.
pub ns_view: NonNull<c_void>,
}
impl AppKitWindowHandle {
/// Create a new handle to a view.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::AppKitWindowHandle;
/// # type NSView = ();
/// #
/// let view: &NSView;
/// # view = &();
/// let handle = AppKitWindowHandle::new(NonNull::from(view).cast());
/// ```
pub fn new(ns_view: NonNull<c_void>) -> Self {
Self { ns_view }
}
}

282
vendor/raw-window-handle/src/borrowed.rs vendored Normal file
View File

@@ -0,0 +1,282 @@
//! Borrowable window handles based on the ones in this crate.
//!
//! These should be 100% safe to pass around and use, no possibility of dangling or invalidity.
use core::borrow::Borrow;
use core::fmt;
use core::marker::PhantomData;
use crate::{HandleError, RawDisplayHandle, RawWindowHandle};
/// A display that acts as a wrapper around a display handle.
///
/// Objects that implement this trait should be able to return a [`DisplayHandle`] for the display
/// that they are associated with. This handle should last for the lifetime of the object, and should
/// return an error if the application is inactive.
///
/// Implementors of this trait will be windowing systems, like [`winit`] and [`sdl2`]. These windowing
/// systems should implement this trait on types that represent the top-level display server. It
/// should be implemented by tying the lifetime of the [`DisplayHandle`] to the lifetime of the
/// display object.
///
/// Users of this trait will include graphics libraries, like [`wgpu`] and [`glutin`]. These APIs
/// should be generic over a type that implements `HasDisplayHandle`, and should use the
/// [`DisplayHandle`] type to access the display handle.
///
/// Note that these requirements are not enforced on `HasDisplayHandle`, rather, they are enforced on the
/// constructors of [`DisplayHandle`]. This is because the `HasDisplayHandle` trait is safe to implement.
///
/// [`winit`]: https://crates.io/crates/winit
/// [`sdl2`]: https://crates.io/crates/sdl2
/// [`wgpu`]: https://crates.io/crates/wgpu
/// [`glutin`]: https://crates.io/crates/glutin
pub trait HasDisplayHandle {
/// Get a handle to the display controller of the windowing system.
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>;
}
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for &H {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
(**self).display_handle()
}
}
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for &mut H {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
(**self).display_handle()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::boxed::Box<H> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
(**self).display_handle()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::rc::Rc<H> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
(**self).display_handle()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::sync::Arc<H> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
(**self).display_handle()
}
}
/// The handle to the display controller of the windowing system.
///
/// This is the primary return type of the [`HasDisplayHandle`] trait. It is guaranteed to contain
/// a valid platform-specific display handle for its lifetime.
#[repr(transparent)]
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
pub struct DisplayHandle<'a> {
raw: RawDisplayHandle,
_marker: PhantomData<&'a ()>,
}
impl fmt::Debug for DisplayHandle<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("DisplayHandle").field(&self.raw).finish()
}
}
impl<'a> DisplayHandle<'a> {
/// Create a `DisplayHandle` from a [`RawDisplayHandle`].
///
/// # Safety
///
/// Users can safely assume that non-`null`/`0` fields are valid handles, and it is up to the
/// implementer of this trait to ensure that condition is upheld.
///
/// Despite that qualification, implementors should still make a best-effort attempt to fill in all
/// available fields. If an implementation doesn't, and a downstream user needs the field, it should
/// try to derive the field from other fields the implementer *does* provide via whatever methods the
/// platform provides.
///
/// It is not possible to invalidate a [`DisplayHandle`] on any platform without additional unsafe code.
pub unsafe fn borrow_raw(raw: RawDisplayHandle) -> Self {
Self {
raw,
_marker: PhantomData,
}
}
/// Get the underlying raw display handle.
pub fn as_raw(&self) -> RawDisplayHandle {
self.raw
}
}
impl AsRef<RawDisplayHandle> for DisplayHandle<'_> {
fn as_ref(&self) -> &RawDisplayHandle {
&self.raw
}
}
impl Borrow<RawDisplayHandle> for DisplayHandle<'_> {
fn borrow(&self) -> &RawDisplayHandle {
&self.raw
}
}
impl From<DisplayHandle<'_>> for RawDisplayHandle {
fn from(handle: DisplayHandle<'_>) -> Self {
handle.raw
}
}
impl<'a> HasDisplayHandle for DisplayHandle<'a> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
Ok(*self)
}
}
/// A handle to a window.
///
/// Objects that implement this trait should be able to return a [`WindowHandle`] for the window
/// that they are associated with. This handle should last for the lifetime of the object, and should
/// return an error if the application is inactive.
///
/// Implementors of this trait will be windowing systems, like [`winit`] and [`sdl2`]. These windowing
/// systems should implement this trait on types that represent windows.
///
/// Users of this trait will include graphics libraries, like [`wgpu`] and [`glutin`]. These APIs
/// should be generic over a type that implements `HasWindowHandle`, and should use the
/// [`WindowHandle`] type to access the window handle. The window handle should be acquired and held
/// while the window is being used, in order to ensure that the window is not deleted while it is in
/// use.
///
/// [`winit`]: https://crates.io/crates/winit
/// [`sdl2`]: https://crates.io/crates/sdl2
/// [`wgpu`]: https://crates.io/crates/wgpu
/// [`glutin`]: https://crates.io/crates/glutin
pub trait HasWindowHandle {
/// Get a handle to the window.
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError>;
}
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for &H {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
(**self).window_handle()
}
}
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for &mut H {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
(**self).window_handle()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::boxed::Box<H> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
(**self).window_handle()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::rc::Rc<H> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
(**self).window_handle()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::sync::Arc<H> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
(**self).window_handle()
}
}
/// The handle to a window.
///
/// This is the primary return type of the [`HasWindowHandle`] trait. All *pointers* within this type
/// are guaranteed to be valid and not dangling for the lifetime of the handle. This excludes window IDs
/// like XIDs and the window ID for web platforms. See the documentation on the [`HasWindowHandle`]
/// trait for more information about these safety requirements.
///
/// This handle is guaranteed to be safe and valid.
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
pub struct WindowHandle<'a> {
raw: RawWindowHandle,
_marker: PhantomData<&'a ()>,
}
impl fmt::Debug for WindowHandle<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("WindowHandle").field(&self.raw).finish()
}
}
impl<'a> WindowHandle<'a> {
/// Borrow a `WindowHandle` from a [`RawWindowHandle`].
///
/// # Safety
///
/// Users can safely assume that non-`null`/`0` fields are valid handles, and it is up to the
/// implementer of this trait to ensure that condition is upheld.
///
/// Despite that qualification, implementers should still make a best-effort attempt to fill in all
/// available fields. If an implementation doesn't, and a downstream user needs the field, it should
/// try to derive the field from other fields the implementer *does* provide via whatever methods the
/// platform provides.
///
/// Note that this guarantee only applies to *pointers*, and not any window ID types in the handle.
/// This includes Window IDs (XIDs) from X11 and the window ID for web platforms. There is no way for
/// Rust to enforce any kind of invariant on these types, since:
///
/// - For all three listed platforms, it is possible for safe code in the same process to delete
/// the window.
/// - For X11, it is possible for code in a different process to delete the window. In fact, it is
/// possible for code on a different *machine* to delete the window.
///
/// It is *also* possible for the window to be replaced with another, valid-but-different window. User
/// code should be aware of this possibility, and should be ready to soundly handle the possible error
/// conditions that can arise from this.
pub unsafe fn borrow_raw(raw: RawWindowHandle) -> Self {
Self {
raw,
_marker: PhantomData,
}
}
/// Get the underlying raw window handle.
pub fn as_raw(&self) -> RawWindowHandle {
self.raw.clone()
}
}
impl AsRef<RawWindowHandle> for WindowHandle<'_> {
fn as_ref(&self) -> &RawWindowHandle {
&self.raw
}
}
impl Borrow<RawWindowHandle> for WindowHandle<'_> {
fn borrow(&self) -> &RawWindowHandle {
&self.raw
}
}
impl From<WindowHandle<'_>> for RawWindowHandle {
fn from(handle: WindowHandle<'_>) -> Self {
handle.raw
}
}
impl HasWindowHandle for WindowHandle<'_> {
fn window_handle(&self) -> Result<Self, HandleError> {
Ok(*self)
}
}

79
vendor/raw-window-handle/src/haiku.rs vendored Normal file
View File

@@ -0,0 +1,79 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for Haiku.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HaikuDisplayHandle {}
impl HaikuDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::HaikuDisplayHandle;
/// let handle = HaikuDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create an Haiku-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::haiku();
/// do_something(handle);
/// ```
pub fn haiku() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(HaikuDisplayHandle::new().into()) }
}
}
/// Raw window handle for Haiku.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HaikuWindowHandle {
/// A pointer to a BWindow object
pub b_window: NonNull<c_void>,
/// A pointer to a BDirectWindow object that might be null
pub b_direct_window: Option<NonNull<c_void>>,
}
impl HaikuWindowHandle {
/// Create a new handle to a window.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::HaikuWindowHandle;
/// # type BWindow = ();
/// #
/// let b_window: NonNull<BWindow>;
/// # b_window = NonNull::from(&());
/// let mut handle = HaikuWindowHandle::new(b_window.cast());
/// // Optionally set `b_direct_window`.
/// handle.b_direct_window = None;
/// ```
pub fn new(b_window: NonNull<c_void>) -> Self {
Self {
b_window,
b_direct_window: None,
}
}
}

499
vendor/raw-window-handle/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,499 @@
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::new_without_default)]
#![deny(unsafe_op_in_unsafe_fn)]
//! Interoperability library for Rust Windowing applications.
//!
//! This library provides standard types for accessing a window's platform-specific raw window
//! handle and platforms display handle. This does not provide any utilities for creating and
//! managing windows; instead, it provides a common interface that window creation libraries (e.g.
//! Winit, SDL) can use to easily talk with graphics libraries (e.g. gfx-hal).
//!
//! ## Safety guarantees
//!
//! Please see the docs of [`HasWindowHandle`] and [`HasDisplayHandle`].
//!
//! ## Platform handle initialization
//!
//! Each platform handle struct is purposefully non-exhaustive, so that additional fields may be
//! added without breaking backwards compatibility. Each struct provides an `empty` method that may
//! be used along with the struct update syntax to construct it. See each specific struct for
//! examples.
//!
//! ## Display Handles
//!
//! Some windowing systems use a separate display handle for some operations. The display usually
//! represents a connection to some display server, but it is not necessarily tied to a particular
//! window. See [`RawDisplayHandle`] for more details.
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
mod android;
mod appkit;
mod borrowed;
mod haiku;
mod ohos;
mod redox;
mod uikit;
mod unix;
mod web;
mod windows;
pub use android::{AndroidDisplayHandle, AndroidNdkWindowHandle};
pub use appkit::{AppKitDisplayHandle, AppKitWindowHandle};
pub use borrowed::{DisplayHandle, HasDisplayHandle, HasWindowHandle, WindowHandle};
pub use haiku::{HaikuDisplayHandle, HaikuWindowHandle};
pub use ohos::{OhosDisplayHandle, OhosNdkWindowHandle};
pub use redox::{OrbitalDisplayHandle, OrbitalWindowHandle};
pub use uikit::{UiKitDisplayHandle, UiKitWindowHandle};
pub use unix::{
DrmDisplayHandle, DrmWindowHandle, GbmDisplayHandle, GbmWindowHandle, WaylandDisplayHandle,
WaylandWindowHandle, XcbDisplayHandle, XcbWindowHandle, XlibDisplayHandle, XlibWindowHandle,
};
pub use web::{
WebCanvasWindowHandle, WebDisplayHandle, WebOffscreenCanvasWindowHandle, WebWindowHandle,
};
pub use windows::{Win32WindowHandle, WinRtWindowHandle, WindowsDisplayHandle};
use core::fmt;
/// Window that wraps around a raw window handle.
///
/// # Safety
///
/// Users can safely assume that pointers and non-zero fields are valid, and it is up to the
/// implementer of this trait to ensure that condition is upheld.
///
/// Despite that qualification, implementers should still make a best-effort attempt to fill in all
/// available fields. If an implementation doesn't, and a downstream user needs the field, it should
/// try to derive the field from other fields the implementer *does* provide via whatever methods the
/// platform provides.
///
/// The exact handles returned by `raw_window_handle` must remain consistent between multiple calls
/// to `raw_window_handle` as long as not indicated otherwise by platform specific events.
#[deprecated = "Use `HasWindowHandle` instead"]
pub unsafe trait HasRawWindowHandle {
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError>;
}
#[allow(deprecated)]
unsafe impl<T: HasWindowHandle + ?Sized> HasRawWindowHandle for T {
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError> {
self.window_handle().map(Into::into)
}
}
/// A window handle for a particular windowing system.
///
/// Each variant contains a struct with fields specific to that windowing system
/// (e.g. [`Win32WindowHandle`] will include a [HWND], [`WaylandWindowHandle`] uses [wl_surface],
/// etc.)
///
/// [HWND]: https://learn.microsoft.com/en-us/windows/win32/winmsg/about-windows#window-handle
/// [wl_surface]: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_surface
///
/// # Variant Availability
///
/// Note that all variants are present on all targets (none are disabled behind
/// `#[cfg]`s), but see the "Availability Hints" section on each variant for
/// some hints on where this variant might be expected.
///
/// Note that these "Availability Hints" are not normative. That is to say, a
/// [`HasWindowHandle`] implementor is completely allowed to return something
/// unexpected. (For example, it's legal for someone to return a
/// [`RawWindowHandle::Xlib`] on macOS, it would just be weird, and probably
/// requires something like XQuartz be used).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RawWindowHandle {
/// A raw window handle for UIKit (Apple's non-macOS windowing library).
///
/// ## Availability Hints
/// This variant is likely to be used on iOS, tvOS, (in theory) watchOS, and
/// Mac Catalyst (`$arch-apple-ios-macabi` targets, which can notably use
/// UIKit *or* AppKit), as these are the targets that (currently) support
/// UIKit.
UiKit(UiKitWindowHandle),
/// A raw window handle for AppKit.
///
/// ## Availability Hints
/// This variant is likely to be used on macOS, although Mac Catalyst
/// (`$arch-apple-ios-macabi` targets, which can notably use UIKit *or*
/// AppKit) can also use it despite being `target_os = "ios"`.
AppKit(AppKitWindowHandle),
/// A raw window handle for the Redox operating system.
///
/// ## Availability Hints
/// This variant is used by the Orbital Windowing System in the Redox
/// operating system.
Orbital(OrbitalWindowHandle),
/// A raw window handle for the OpenHarmony OS NDK
///
/// ## Availability Hints
/// This variant is used on OpenHarmony OS (`target_env = "ohos"`).
OhosNdk(OhosNdkWindowHandle),
/// A raw window handle for Xlib.
///
/// ## Availability Hints
/// This variant is likely to show up anywhere someone manages to get X11
/// working that Xlib can be built for, which is to say, most (but not all)
/// Unix systems.
Xlib(XlibWindowHandle),
/// A raw window handle for Xcb.
///
/// ## Availability Hints
/// This variant is likely to show up anywhere someone manages to get X11
/// working that XCB can be built for, which is to say, most (but not all)
/// Unix systems.
Xcb(XcbWindowHandle),
/// A raw window handle for Wayland.
///
/// ## Availability Hints
/// This variant should be expected anywhere Wayland works, which is
/// currently some subset of unix systems.
Wayland(WaylandWindowHandle),
/// A raw window handle for the Linux Kernel Mode Set/Direct Rendering Manager
///
/// ## Availability Hints
/// This variant is used on Linux when neither X nor Wayland are available
Drm(DrmWindowHandle),
/// A raw window handle for the Linux Generic Buffer Manager.
///
/// ## Availability Hints
/// This variant is present regardless of windowing backend and likely to be used with
/// EGL_MESA_platform_gbm or EGL_KHR_platform_gbm.
Gbm(GbmWindowHandle),
/// A raw window handle for Win32.
///
/// ## Availability Hints
/// This variant is used on Windows systems.
Win32(Win32WindowHandle),
/// A raw window handle for WinRT.
///
/// ## Availability Hints
/// This variant is used on Windows systems.
WinRt(WinRtWindowHandle),
/// A raw window handle for the Web.
///
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
Web(WebWindowHandle),
/// A raw window handle for a Web canvas registered via [`wasm-bindgen`].
///
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
///
/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
WebCanvas(WebCanvasWindowHandle),
/// A raw window handle for a Web offscreen canvas registered via [`wasm-bindgen`].
///
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
///
/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
WebOffscreenCanvas(WebOffscreenCanvasWindowHandle),
/// A raw window handle for Android NDK.
///
/// ## Availability Hints
/// This variant is used on Android targets.
AndroidNdk(AndroidNdkWindowHandle),
/// A raw window handle for Haiku.
///
/// ## Availability Hints
/// This variant is used on HaikuOS.
Haiku(HaikuWindowHandle),
}
/// Display that wraps around a raw display handle.
///
/// # Safety
///
/// Users can safely assume that pointers and non-zero fields are valid, and it is up to the
/// implementer of this trait to ensure that condition is upheld.
///
/// Despite that qualification, implementers should still make a best-effort attempt to fill in all
/// available fields. If an implementation doesn't, and a downstream user needs the field, it should
/// try to derive the field from other fields the implementer *does* provide via whatever methods the
/// platform provides.
///
/// The exact handles returned by `raw_display_handle` must remain consistent between multiple calls
/// to `raw_display_handle` as long as not indicated otherwise by platform specific events.
#[deprecated = "Use `HasDisplayHandle` instead"]
pub unsafe trait HasRawDisplayHandle {
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>;
}
#[allow(deprecated)]
unsafe impl<T: HasDisplayHandle + ?Sized> HasRawDisplayHandle for T {
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError> {
self.display_handle().map(Into::into)
}
}
/// A display server handle for a particular windowing system.
///
/// The display usually represents a connection to some display server, but it is not necessarily
/// tied to a particular window. Some APIs can use the display handle without ever creating a window
/// handle (e.g. offscreen rendering, headless event handling).
///
/// Each variant contains a struct with fields specific to that windowing system
/// (e.g. [`XlibDisplayHandle`] contains a [Display] connection to an X Server,
/// [`WaylandDisplayHandle`] uses [wl_display] to connect to a compositor). Not all windowing
/// systems have a separate display handle (or they haven't been implemented yet) and their variants
/// contain empty structs.
///
/// [Display]: https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Display_Functions
/// [wl_display]: https://wayland.freedesktop.org/docs/html/apb.html#Client-classwl__display
///
/// # Variant Availability
///
/// Note that all variants are present on all targets (none are disabled behind
/// `#[cfg]`s), but see the "Availability Hints" section on each variant for
/// some hints on where this variant might be expected.
///
/// Note that these "Availability Hints" are not normative. That is to say, a
/// [`HasDisplayHandle`] implementor is completely allowed to return something
/// unexpected. (For example, it's legal for someone to return a
/// [`RawDisplayHandle::Xlib`] on macOS, it would just be weird, and probably
/// requires something like XQuartz be used).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RawDisplayHandle {
/// A raw display handle for UIKit (Apple's non-macOS windowing library).
///
/// ## Availability Hints
/// This variant is likely to be used on iOS, tvOS, (in theory) watchOS, and
/// Mac Catalyst (`$arch-apple-ios-macabi` targets, which can notably use
/// UIKit *or* AppKit), as these are the targets that (currently) support
/// UIKit.
UiKit(UiKitDisplayHandle),
/// A raw display handle for AppKit.
///
/// ## Availability Hints
/// This variant is likely to be used on macOS, although Mac Catalyst
/// (`$arch-apple-ios-macabi` targets, which can notably use UIKit *or*
/// AppKit) can also use it despite being `target_os = "ios"`.
AppKit(AppKitDisplayHandle),
/// A raw display handle for the Redox operating system.
///
/// ## Availability Hints
/// This variant is used by the Orbital Windowing System in the Redox
/// operating system.
Orbital(OrbitalDisplayHandle),
/// A raw display handle for OpenHarmony OS NDK
///
/// ## Availability Hints
/// This variant is used on OpenHarmony OS (`target_env = "ohos"`).
Ohos(OhosDisplayHandle),
/// A raw display handle for Xlib.
///
/// ## Availability Hints
/// This variant is likely to show up anywhere someone manages to get X11
/// working that Xlib can be built for, which is to say, most (but not all)
/// Unix systems.
Xlib(XlibDisplayHandle),
/// A raw display handle for Xcb.
///
/// ## Availability Hints
/// This variant is likely to show up anywhere someone manages to get X11
/// working that XCB can be built for, which is to say, most (but not all)
/// Unix systems.
Xcb(XcbDisplayHandle),
/// A raw display handle for Wayland.
///
/// ## Availability Hints
/// This variant should be expected anywhere Wayland works, which is
/// currently some subset of unix systems.
Wayland(WaylandDisplayHandle),
/// A raw display handle for the Linux Kernel Mode Set/Direct Rendering Manager
///
/// ## Availability Hints
/// This variant is used on Linux when neither X nor Wayland are available
Drm(DrmDisplayHandle),
/// A raw display handle for the Linux Generic Buffer Manager.
///
/// ## Availability Hints
/// This variant is present regardless of windowing backend and likely to be used with
/// EGL_MESA_platform_gbm or EGL_KHR_platform_gbm.
Gbm(GbmDisplayHandle),
/// A raw display handle for Win32.
///
/// ## Availability Hints
/// This variant is used on Windows systems.
Windows(WindowsDisplayHandle),
/// A raw display handle for the Web.
///
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
Web(WebDisplayHandle),
/// A raw display handle for Android NDK.
///
/// ## Availability Hints
/// This variant is used on Android targets.
Android(AndroidDisplayHandle),
/// A raw display handle for Haiku.
///
/// ## Availability Hints
/// This variant is used on HaikuOS.
Haiku(HaikuDisplayHandle),
}
/// An error that can occur while fetching a display or window handle.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum HandleError {
/// The underlying handle cannot be represented using the types in this crate.
///
/// This may be returned if the underlying window system does not support any of the
/// representative C window handles in this crate. For instance, if you were using a pure Rust
/// library to set up X11 (like [`x11rb`]), you would not be able to use any of the
/// [`RawWindowHandle`] variants, as they all represent C types.
///
/// Another example would be a system that isn't supported by `raw-window-handle` yet,
/// like some game consoles.
///
/// In the event that this error is returned, you should try to use the underlying window
/// system's native API to get the handle you need.
///
/// [`x11rb`]: https://crates.io/crates/x11rb
NotSupported,
/// The underlying handle is not available.
///
/// In some cases the underlying window handle may become temporarily unusable. For example, on
/// Android, the native window pointer can arbitrarily be replaced or removed by the system. In
/// this case, returning a window handle would be disingenuous, as it would not be usable. A
/// similar situation can occur on Wayland for the layer shell windows.
///
/// In the event that this error is returned, you should wait until the handle becomes available
/// again.
Unavailable,
}
impl fmt::Display for HandleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotSupported => write!(
f,
"the underlying handle cannot be represented using the types in this crate"
),
Self::Unavailable => write!(f, "the underlying handle is not available"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for HandleError {}
macro_rules! from_impl {
($($to:ident, $enum:ident, $from:ty)*) => ($(
impl From<$from> for $to {
fn from(value: $from) -> Self {
$to::$enum(value)
}
}
)*)
}
from_impl!(RawDisplayHandle, UiKit, UiKitDisplayHandle);
from_impl!(RawDisplayHandle, AppKit, AppKitDisplayHandle);
from_impl!(RawDisplayHandle, Orbital, OrbitalDisplayHandle);
from_impl!(RawDisplayHandle, Ohos, OhosDisplayHandle);
from_impl!(RawDisplayHandle, Xlib, XlibDisplayHandle);
from_impl!(RawDisplayHandle, Xcb, XcbDisplayHandle);
from_impl!(RawDisplayHandle, Wayland, WaylandDisplayHandle);
from_impl!(RawDisplayHandle, Drm, DrmDisplayHandle);
from_impl!(RawDisplayHandle, Gbm, GbmDisplayHandle);
from_impl!(RawDisplayHandle, Windows, WindowsDisplayHandle);
from_impl!(RawDisplayHandle, Web, WebDisplayHandle);
from_impl!(RawDisplayHandle, Android, AndroidDisplayHandle);
from_impl!(RawDisplayHandle, Haiku, HaikuDisplayHandle);
from_impl!(RawWindowHandle, UiKit, UiKitWindowHandle);
from_impl!(RawWindowHandle, AppKit, AppKitWindowHandle);
from_impl!(RawWindowHandle, Orbital, OrbitalWindowHandle);
from_impl!(RawWindowHandle, OhosNdk, OhosNdkWindowHandle);
from_impl!(RawWindowHandle, Xlib, XlibWindowHandle);
from_impl!(RawWindowHandle, Xcb, XcbWindowHandle);
from_impl!(RawWindowHandle, Wayland, WaylandWindowHandle);
from_impl!(RawWindowHandle, Drm, DrmWindowHandle);
from_impl!(RawWindowHandle, Gbm, GbmWindowHandle);
from_impl!(RawWindowHandle, Win32, Win32WindowHandle);
from_impl!(RawWindowHandle, WinRt, WinRtWindowHandle);
from_impl!(RawWindowHandle, Web, WebWindowHandle);
from_impl!(RawWindowHandle, WebCanvas, WebCanvasWindowHandle);
from_impl!(
RawWindowHandle,
WebOffscreenCanvas,
WebOffscreenCanvasWindowHandle
);
from_impl!(RawWindowHandle, AndroidNdk, AndroidNdkWindowHandle);
from_impl!(RawWindowHandle, Haiku, HaikuWindowHandle);
#[cfg(test)]
mod tests {
use core::panic::{RefUnwindSafe, UnwindSafe};
use static_assertions::{assert_impl_all, assert_not_impl_any};
use super::*;
#[test]
fn auto_traits() {
assert_impl_all!(RawDisplayHandle: UnwindSafe, RefUnwindSafe, Unpin);
assert_not_impl_any!(RawDisplayHandle: Send, Sync);
assert_impl_all!(DisplayHandle<'_>: UnwindSafe, RefUnwindSafe, Unpin);
assert_not_impl_any!(DisplayHandle<'_>: Send, Sync);
assert_impl_all!(RawWindowHandle: UnwindSafe, RefUnwindSafe, Unpin);
assert_not_impl_any!(RawWindowHandle: Send, Sync);
assert_impl_all!(WindowHandle<'_>: UnwindSafe, RefUnwindSafe, Unpin);
assert_not_impl_any!(WindowHandle<'_>: Send, Sync);
assert_impl_all!(HandleError: Send, Sync, UnwindSafe, RefUnwindSafe, Unpin);
// TODO: Unsure if some of these should not actually be Send + Sync
assert_impl_all!(UiKitDisplayHandle: Send, Sync);
assert_impl_all!(AppKitDisplayHandle: Send, Sync);
assert_impl_all!(OrbitalDisplayHandle: Send, Sync);
assert_impl_all!(OhosDisplayHandle: Send, Sync);
assert_not_impl_any!(XlibDisplayHandle: Send, Sync);
assert_not_impl_any!(XcbDisplayHandle: Send, Sync);
assert_not_impl_any!(WaylandDisplayHandle: Send, Sync);
assert_impl_all!(DrmDisplayHandle: Send, Sync);
assert_not_impl_any!(GbmDisplayHandle: Send, Sync);
assert_impl_all!(WindowsDisplayHandle: Send, Sync);
assert_impl_all!(WebDisplayHandle: Send, Sync);
assert_impl_all!(AndroidDisplayHandle: Send, Sync);
assert_impl_all!(HaikuDisplayHandle: Send, Sync);
// TODO: Unsure if some of these should not actually be Send + Sync
assert_not_impl_any!(UiKitWindowHandle: Send, Sync);
assert_not_impl_any!(AppKitWindowHandle: Send, Sync);
assert_not_impl_any!(OrbitalWindowHandle: Send, Sync);
assert_not_impl_any!(OhosNdkWindowHandle: Send, Sync);
assert_impl_all!(XlibWindowHandle: Send, Sync);
assert_impl_all!(XcbWindowHandle: Send, Sync);
assert_not_impl_any!(WaylandWindowHandle: Send, Sync);
assert_impl_all!(DrmWindowHandle: Send, Sync);
assert_not_impl_any!(GbmWindowHandle: Send, Sync);
assert_impl_all!(Win32WindowHandle: Send, Sync);
assert_not_impl_any!(WinRtWindowHandle: Send, Sync);
assert_impl_all!(WebWindowHandle: Send, Sync);
assert_not_impl_any!(WebCanvasWindowHandle: Send, Sync);
assert_not_impl_any!(WebOffscreenCanvasWindowHandle: Send, Sync);
assert_not_impl_any!(AndroidNdkWindowHandle: Send, Sync);
assert_not_impl_any!(HaikuWindowHandle: Send, Sync);
}
#[allow(deprecated, unused)]
fn assert_object_safe(
_: &dyn HasRawWindowHandle,
_: &dyn HasRawDisplayHandle,
_: &dyn HasWindowHandle,
_: &dyn HasDisplayHandle,
) {
}
}

98
vendor/raw-window-handle/src/ohos.rs vendored Normal file
View File

@@ -0,0 +1,98 @@
//! [OpenHarmony] OS Window Handles
//!
//! ## Background
//!
//! Applications on [OpenHarmony] use [ArkUI] for defining their UI. Applications can use an
//! [XComponent] to render using native Code (e.g. Rust) via EGL.
//! Native code will receive a callback `OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)`
//! when the `XComponent` is created. The window argument has the type [`OHNativeWindow`] / `EGLNativeWindowType`.
//! The window can then be used to create a surface with
//! `eglCreateWindowSurface(eglDisplay_, eglConfig_, window, NULL)`
//!
//! [OpenHarmony]: https://gitee.com/openharmony/docs/blob/master/en/OpenHarmony-Overview.md
//! [ArkUI]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/ui/arkui-overview.md
//! [XComponent]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/ui/arkts-common-components-xcomponent.md
//! [`OHNativeWindow`]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-arkgraphics2d/_native_window.md
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for OpenHarmony.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OhosDisplayHandle {}
impl OhosDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::OhosDisplayHandle;
/// let handle = OhosDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create an OpenHarmony-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::ohos();
/// do_something(handle);
/// ```
pub fn ohos() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(OhosDisplayHandle::new().into()) }
}
}
/// Raw window handle for Ohos NDK.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OhosNdkWindowHandle {
pub native_window: NonNull<c_void>,
}
impl OhosNdkWindowHandle {
/// Create a new handle to an [`OHNativeWindow`] on OpenHarmony.
///
/// The handle will typically be created from an [`XComponent`], consult the
/// [native `XComponent` Guidelines] for more details.
///
/// [`XComponent`]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/ui/arkts-common-components-xcomponent.md
/// [native `XComponent` Guidelines]: https://gitee.com/openharmony/docs/blob/OpenHarmony-4.0-Release/en/application-dev/napi/xcomponent-guidelines.md
/// [`OHNativeWindow`]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-arkgraphics2d/_native_window.md
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use core::ffi::c_void;
/// # use raw_window_handle::OhosNdkWindowHandle;
/// # #[allow(non_camel_case_types)]
/// # type OH_NativeXComponent = ();
///
/// /// Called When the `XComponent` is created.
/// ///
/// /// See the [XComponent Guidelines](https://gitee.com/openharmony/docs/blob/OpenHarmony-4.0-Release/en/application-dev/napi/xcomponent-guidelines.md)
/// /// for more details
/// extern "C" fn on_surface_created_callback(component: *mut OH_NativeXComponent, window: *mut c_void) {
/// let handle = OhosNdkWindowHandle::new(NonNull::new(window).unwrap());
/// }
/// ```
pub fn new(native_window: NonNull<c_void>) -> Self {
Self { native_window }
}
}

74
vendor/raw-window-handle/src/redox.rs vendored Normal file
View File

@@ -0,0 +1,74 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for the Redox operating system.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OrbitalDisplayHandle {}
impl OrbitalDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::OrbitalDisplayHandle;
/// let handle = OrbitalDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create an Orbital-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::orbital();
/// do_something(handle);
/// ```
pub fn orbital() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(OrbitalDisplayHandle::new().into()) }
}
}
/// Raw window handle for the Redox operating system.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OrbitalWindowHandle {
/// A pointer to an orbclient window.
// TODO(madsmtm): I think this is a file descriptor, so perhaps it should
// actually use `std::os::fd::RawFd`, or some sort of integer instead?
pub window: NonNull<c_void>,
}
impl OrbitalWindowHandle {
/// Create a new handle to a window.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::OrbitalWindowHandle;
/// # type Window = ();
/// #
/// let window: NonNull<Window>;
/// # window = NonNull::from(&());
/// let mut handle = OrbitalWindowHandle::new(window.cast());
/// ```
pub fn new(window: NonNull<c_void>) -> Self {
Self { window }
}
}

117
vendor/raw-window-handle/src/uikit.rs vendored Normal file
View File

@@ -0,0 +1,117 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for UIKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UiKitDisplayHandle {}
impl UiKitDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::UiKitDisplayHandle;
/// let handle = UiKitDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create a UiKit-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::uikit();
/// do_something(handle);
/// ```
pub fn uikit() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(UiKitDisplayHandle::new().into()) }
}
}
/// Raw window handle for UIKit.
///
/// Note that `UIView` can only be accessed from the main thread of the
/// application. This struct is `!Send` and `!Sync` to help with ensuring
/// that.
///
/// # Example
///
/// Getting the view from a [`WindowHandle`][crate::WindowHandle].
///
/// ```no_run
/// # fn inner() {
/// #![cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", target_os = "xros"))]
/// # #[cfg(requires_objc2)]
/// use objc2_foundation::is_main_thread;
/// # #[cfg(requires_objc2)]
/// use objc2::rc::Id;
/// # #[cfg(requires_objc2)]
/// use objc2_ui_kit::UIView;
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
///
/// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
/// # handle = unimplemented!();
/// match handle.as_raw() {
/// # #[cfg(requires_objc2)]
/// RawWindowHandle::UIKit(handle) => {
/// assert!(is_main_thread(), "can only access UIKit handles on the main thread");
/// let ui_view = handle.ui_view.as_ptr();
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
/// // that the `UiKitWindowHandle` contains a valid pointer to an
/// // `UIView`.
/// // Unwrap is fine, since the pointer came from `NonNull`.
/// let ui_view: Id<UIView> = unsafe { Id::retain(ui_view.cast()) }.unwrap();
/// // Do something with the UIView here.
/// }
/// handle => unreachable!("unknown handle {handle:?} for platform"),
/// }
/// # }
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UiKitWindowHandle {
/// A pointer to an `UIView` object.
pub ui_view: NonNull<c_void>,
/// A pointer to an `UIViewController` object, if the view has one.
pub ui_view_controller: Option<NonNull<c_void>>,
}
impl UiKitWindowHandle {
/// Create a new handle to a view.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::UiKitWindowHandle;
/// # type UIView = ();
/// #
/// let view: &UIView;
/// # view = &();
/// let mut handle = UiKitWindowHandle::new(NonNull::from(view).cast());
/// // Optionally set the view controller.
/// handle.ui_view_controller = None;
/// ```
pub fn new(ui_view: NonNull<c_void>) -> Self {
Self {
ui_view,
ui_view_controller: None,
}
}
}

316
vendor/raw-window-handle/src/unix.rs vendored Normal file
View File

@@ -0,0 +1,316 @@
use core::ffi::{c_int, c_ulong, c_void};
use core::num::NonZeroU32;
use core::ptr::NonNull;
/// Raw display handle for Xlib.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct XlibDisplayHandle {
/// A pointer to an Xlib `Display`.
///
/// It is strongly recommended to set this value, however it may be set to
/// `None` to request the default display when using EGL.
pub display: Option<NonNull<c_void>>,
/// An X11 screen to use with this display handle.
///
/// Note, that X11 could have multiple screens, however
/// graphics APIs could work only with one screen at the time,
/// given that multiple screens usually reside on different GPUs.
pub screen: c_int,
}
impl XlibDisplayHandle {
/// Create a new handle to a display.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::XlibDisplayHandle;
/// #
/// let display: NonNull<c_void>;
/// let screen;
/// # display = NonNull::from(&()).cast();
/// # screen = 0;
/// let handle = XlibDisplayHandle::new(Some(display), screen);
/// ```
pub fn new(display: Option<NonNull<c_void>>, screen: c_int) -> Self {
Self { display, screen }
}
}
/// Raw window handle for Xlib.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct XlibWindowHandle {
/// An Xlib `Window`.
pub window: c_ulong,
/// An Xlib visual ID, or 0 if unknown.
pub visual_id: c_ulong,
}
impl XlibWindowHandle {
/// Create a new handle to a window.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_ulong;
/// # use raw_window_handle::XlibWindowHandle;
/// #
/// let window: c_ulong;
/// # window = 0;
/// let mut handle = XlibWindowHandle::new(window);
/// // Optionally set the visual ID.
/// handle.visual_id = 0;
/// ```
pub fn new(window: c_ulong) -> Self {
Self {
window,
visual_id: 0,
}
}
}
/// Raw display handle for Xcb.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct XcbDisplayHandle {
/// A pointer to an X server `xcb_connection_t`.
///
/// It is strongly recommended to set this value, however it may be set to
/// `None` to request the default display when using EGL.
pub connection: Option<NonNull<c_void>>,
/// An X11 screen to use with this display handle.
///
/// Note, that X11 could have multiple screens, however
/// graphics APIs could work only with one screen at the time,
/// given that multiple screens usually reside on different GPUs.
pub screen: c_int,
}
impl XcbDisplayHandle {
/// Create a new handle to a connection and screen.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::XcbDisplayHandle;
/// #
/// let connection: NonNull<c_void>;
/// let screen;
/// # connection = NonNull::from(&()).cast();
/// # screen = 0;
/// let handle = XcbDisplayHandle::new(Some(connection), screen);
/// ```
pub fn new(connection: Option<NonNull<c_void>>, screen: c_int) -> Self {
Self { connection, screen }
}
}
/// Raw window handle for Xcb.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct XcbWindowHandle {
/// An X11 `xcb_window_t`.
pub window: NonZeroU32, // Based on xproto.h
/// An X11 `xcb_visualid_t`.
pub visual_id: Option<NonZeroU32>,
}
impl XcbWindowHandle {
/// Create a new handle to a window.
///
///
/// # Example
///
/// ```
/// # use core::num::NonZeroU32;
/// # use raw_window_handle::XcbWindowHandle;
/// #
/// let window: NonZeroU32;
/// # window = NonZeroU32::new(1).unwrap();
/// let mut handle = XcbWindowHandle::new(window);
/// // Optionally set the visual ID.
/// handle.visual_id = None;
/// ```
pub fn new(window: NonZeroU32) -> Self {
Self {
window,
visual_id: None,
}
}
}
/// Raw display handle for Wayland.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WaylandDisplayHandle {
/// A pointer to a `wl_display`.
pub display: NonNull<c_void>,
}
impl WaylandDisplayHandle {
/// Create a new display handle.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::WaylandDisplayHandle;
/// #
/// let display: NonNull<c_void>;
/// # display = NonNull::from(&()).cast();
/// let handle = WaylandDisplayHandle::new(display);
/// ```
pub fn new(display: NonNull<c_void>) -> Self {
Self { display }
}
}
/// Raw window handle for Wayland.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WaylandWindowHandle {
/// A pointer to a `wl_surface`.
pub surface: NonNull<c_void>,
}
impl WaylandWindowHandle {
/// Create a new handle to a surface.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::WaylandWindowHandle;
/// #
/// let surface: NonNull<c_void>;
/// # surface = NonNull::from(&()).cast();
/// let handle = WaylandWindowHandle::new(surface);
/// ```
pub fn new(surface: NonNull<c_void>) -> Self {
Self { surface }
}
}
/// Raw display handle for the Linux Kernel Mode Set/Direct Rendering Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DrmDisplayHandle {
/// The drm file descriptor.
// TODO: Use `std::os::fd::RawFd`?
pub fd: i32,
}
impl DrmDisplayHandle {
/// Create a new handle to a file descriptor.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::DrmDisplayHandle;
/// #
/// let fd: i32;
/// # fd = 0;
/// let handle = DrmDisplayHandle::new(fd);
/// ```
pub fn new(fd: i32) -> Self {
Self { fd }
}
}
/// Raw window handle for the Linux Kernel Mode Set/Direct Rendering Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DrmWindowHandle {
/// The primary drm plane handle.
pub plane: u32,
}
impl DrmWindowHandle {
/// Create a new handle to a plane.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::DrmWindowHandle;
/// #
/// let plane: u32;
/// # plane = 0;
/// let handle = DrmWindowHandle::new(plane);
/// ```
pub fn new(plane: u32) -> Self {
Self { plane }
}
}
/// Raw display handle for the Linux Generic Buffer Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GbmDisplayHandle {
/// The gbm device.
pub gbm_device: NonNull<c_void>,
}
impl GbmDisplayHandle {
/// Create a new handle to a device.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::GbmDisplayHandle;
/// #
/// let ptr: NonNull<c_void>;
/// # ptr = NonNull::from(&()).cast();
/// let handle = GbmDisplayHandle::new(ptr);
/// ```
pub fn new(gbm_device: NonNull<c_void>) -> Self {
Self { gbm_device }
}
}
/// Raw window handle for the Linux Generic Buffer Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GbmWindowHandle {
/// The gbm surface.
pub gbm_surface: NonNull<c_void>,
}
impl GbmWindowHandle {
/// Create a new handle to a surface.
///
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::GbmWindowHandle;
/// #
/// let ptr: NonNull<c_void>;
/// # ptr = NonNull::from(&()).cast();
/// let handle = GbmWindowHandle::new(ptr);
/// ```
pub fn new(gbm_surface: NonNull<c_void>) -> Self {
Self { gbm_surface }
}
}

219
vendor/raw-window-handle/src/web.rs vendored Normal file
View File

@@ -0,0 +1,219 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for the Web.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WebDisplayHandle {}
impl WebDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::WebDisplayHandle;
/// let handle = WebDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create a Web-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::web();
/// do_something(handle);
/// ```
pub fn web() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(WebDisplayHandle::new().into()) }
}
}
/// Raw window handle for the Web.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WebWindowHandle {
/// An ID value inserted into the [data attributes] of the canvas element as '`raw-handle`'.
///
/// When accessing from JS, the attribute will automatically be called `rawHandle`.
///
/// Each canvas created by the windowing system should be assigned their own unique ID.
///
/// [data attributes]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
pub id: u32,
}
impl WebWindowHandle {
/// Create a new handle to a canvas element.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::WebWindowHandle;
/// #
/// let id: u32 = 0; // canvas.rawHandle;
/// let handle = WebWindowHandle::new(id);
/// ```
pub fn new(id: u32) -> Self {
Self { id }
}
}
/// Raw window handle for a Web canvas registered via [`wasm-bindgen`].
///
/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WebCanvasWindowHandle {
/// A pointer to the [`JsValue`] of an [`HtmlCanvasElement`].
///
/// Note: This uses [`c_void`] to avoid depending on `wasm-bindgen`
/// directly.
///
/// [`JsValue`]: https://docs.rs/wasm-bindgen/latest/wasm_bindgen/struct.JsValue.html
/// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html
//
// SAFETY: Not using `JsValue` is sound because `wasm-bindgen` guarantees
// that there's only one version of itself in any given binary, and hence
// we can't have a type-confusion where e.g. one library used `JsValue`
// from `v0.2` of `wasm-bindgen`, and another used `JsValue` from `v1.0`;
// the binary will simply fail to compile!
//
// Reference: TODO
pub obj: NonNull<c_void>,
}
impl WebCanvasWindowHandle {
/// Create a new handle from a pointer to [`HtmlCanvasElement`].
///
/// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::WebCanvasWindowHandle;
/// # type HtmlCanvasElement = ();
/// # type JsValue = ();
/// let canvas: &HtmlCanvasElement;
/// # canvas = &();
/// let value: &JsValue = &canvas; // Deref to `JsValue`
/// let obj: NonNull<c_void> = NonNull::from(value).cast();
/// let mut handle = WebCanvasWindowHandle::new(obj);
/// ```
pub fn new(obj: NonNull<c_void>) -> Self {
Self { obj }
}
}
#[cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2"))]
#[cfg_attr(
docsrs,
doc(cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2")))
)]
/// These implementations are only available when `wasm-bindgen-0-2` is enabled.
impl WebCanvasWindowHandle {
/// Create a new `WebCanvasWindowHandle` from a [`wasm_bindgen::JsValue`].
///
/// The `JsValue` should refer to a `HtmlCanvasElement`, and the lifetime
/// of the value should be at least as long as the lifetime of this.
pub fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self {
Self::new(NonNull::from(js_value).cast())
}
/// Convert to the underlying [`wasm_bindgen::JsValue`].
///
/// # Safety
///
/// The inner pointer must be valid. This is ensured if this handle was
/// borrowed from [`WindowHandle`][crate::WindowHandle].
pub unsafe fn as_wasm_bindgen_0_2(&self) -> &wasm_bindgen::JsValue {
unsafe { self.obj.cast().as_ref() }
}
}
/// Raw window handle for a Web offscreen canvas registered via
/// [`wasm-bindgen`].
///
/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WebOffscreenCanvasWindowHandle {
/// A pointer to the [`JsValue`] of an [`OffscreenCanvas`].
///
/// Note: This uses [`c_void`] to avoid depending on `wasm-bindgen`
/// directly.
///
/// [`JsValue`]: https://docs.rs/wasm-bindgen/latest/wasm_bindgen/struct.JsValue.html
/// [`OffscreenCanvas`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenCanvas.html
//
// SAFETY: See WebCanvasWindowHandle.
pub obj: NonNull<c_void>,
}
impl WebOffscreenCanvasWindowHandle {
/// Create a new handle from a pointer to an [`OffscreenCanvas`].
///
/// [`OffscreenCanvas`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenCanvas.html
///
/// # Example
///
/// ```
/// # use core::ffi::c_void;
/// # use core::ptr::NonNull;
/// # use raw_window_handle::WebOffscreenCanvasWindowHandle;
/// # type OffscreenCanvas = ();
/// # type JsValue = ();
/// let canvas: &OffscreenCanvas;
/// # canvas = &();
/// let value: &JsValue = &canvas; // Deref to `JsValue`
/// let obj: NonNull<c_void> = NonNull::from(value).cast();
/// let mut handle = WebOffscreenCanvasWindowHandle::new(obj);
/// ```
pub fn new(obj: NonNull<c_void>) -> Self {
Self { obj }
}
}
#[cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2"))]
#[cfg_attr(
docsrs,
doc(cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2")))
)]
/// These implementations are only available when `wasm-bindgen-0-2` is enabled.
impl WebOffscreenCanvasWindowHandle {
/// Create a new `WebOffscreenCanvasWindowHandle` from a
/// [`wasm_bindgen::JsValue`].
///
/// The `JsValue` should refer to a `HtmlCanvasElement`, and the lifetime
/// of the value should be at least as long as the lifetime of this.
pub fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self {
Self::new(NonNull::from(js_value).cast())
}
/// Convert to the underlying [`wasm_bindgen::JsValue`].
///
/// # Safety
///
/// The inner pointer must be valid. This is ensured if this handle was
/// borrowed from [`WindowHandle`][crate::WindowHandle].
pub unsafe fn as_wasm_bindgen_0_2(&self) -> &wasm_bindgen::JsValue {
unsafe { self.obj.cast().as_ref() }
}
}

117
vendor/raw-window-handle/src/windows.rs vendored Normal file
View File

@@ -0,0 +1,117 @@
use core::ffi::c_void;
use core::num::NonZeroIsize;
use core::ptr::NonNull;
use super::DisplayHandle;
/// Raw display handle for Windows.
///
/// It can be used regardless of Windows window backend.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WindowsDisplayHandle {}
impl WindowsDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::WindowsDisplayHandle;
/// let handle = WindowsDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}
impl DisplayHandle<'static> {
/// Create a Windows-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::windows();
/// do_something(handle);
/// ```
pub fn windows() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(WindowsDisplayHandle::new().into()) }
}
}
/// Raw window handle for Win32.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Win32WindowHandle {
/// A Win32 `HWND` handle.
pub hwnd: NonZeroIsize,
/// The `GWLP_HINSTANCE` associated with this type's `HWND`.
pub hinstance: Option<NonZeroIsize>,
}
impl Win32WindowHandle {
/// Create a new handle to a window.
///
/// # Safety
///
/// It is assumed that the Win32 handle belongs to the current thread. This
/// is necessary for the handle to be considered "valid" in all cases.
///
/// # Example
///
/// ```
/// # use core::num::NonZeroIsize;
/// # use raw_window_handle::Win32WindowHandle;
/// # struct HWND(isize);
/// #
/// let window: HWND;
/// # window = HWND(1);
/// let mut handle = Win32WindowHandle::new(NonZeroIsize::new(window.0).unwrap());
/// // Optionally set the GWLP_HINSTANCE.
/// # #[cfg(only_for_showcase)]
/// let hinstance = NonZeroIsize::new(unsafe { GetWindowLongPtrW(window, GWLP_HINSTANCE) }).unwrap();
/// # let hinstance = None;
/// handle.hinstance = hinstance;
/// ```
pub fn new(hwnd: NonZeroIsize) -> Self {
Self {
hwnd,
hinstance: None,
}
}
}
/// Raw window handle for WinRT.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WinRtWindowHandle {
/// A WinRT `CoreWindow` handle.
pub core_window: NonNull<c_void>,
}
impl WinRtWindowHandle {
/// Create a new handle to a window.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::WinRtWindowHandle;
/// # type CoreWindow = ();
/// #
/// let window: NonNull<CoreWindow>;
/// # window = NonNull::from(&());
/// let handle = WinRtWindowHandle::new(window.cast());
/// ```
pub fn new(core_window: NonNull<c_void>) -> Self {
Self { core_window }
}
}