Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- **Breaking:** Rename `Web*` handles to `WasmBindgen*`, to make it clearer that these are specific to `wasm-bindgen`.
- Enabled `std` feature by default.
- Add owned window and display handles.
- Add ToyOS support. (#223)
- Improve documentation on AppKit and UIKit handles.

## 0.6.2 (2024-05-17)
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod haiku;
mod ohos;
mod owned;
mod redox;
mod toyos;
mod uikit;
mod wayland;
mod web;
Expand All @@ -67,6 +68,7 @@ pub use owned::{
SyncWindowHandle, WindowHandleVtable,
};
pub use redox::{OrbitalDisplayHandle, OrbitalWindowHandle};
pub use toyos::{ToyOsDisplayHandle, ToyOsWindowHandle};
pub use uikit::{UiKitDisplayHandle, UiKitWindowHandle};
pub use wayland::{WaylandDisplayHandle, WaylandWindowHandle};
pub use web::{
Expand Down Expand Up @@ -200,6 +202,11 @@ pub enum RawWindowHandle {
/// ## Availability Hints
/// This variant is used on HaikuOS.
Haiku(HaikuWindowHandle),
/// A raw window handle for ToyOS.
///
/// ## Availability Hints
/// This variant is used on ToyOS (`target_os = "toyos"`).
ToyOs(ToyOsWindowHandle),
}

/// A display server handle for a particular windowing system.
Expand Down Expand Up @@ -313,6 +320,11 @@ pub enum RawDisplayHandle {
/// ## Availability Hints
/// This variant is used on HaikuOS.
Haiku(HaikuDisplayHandle),
/// A raw display handle for ToyOS.
///
/// ## Availability Hints
/// This variant is used on ToyOS (`target_os = "toyos"`).
ToyOs(ToyOsDisplayHandle),
}

/// An error that can occur while fetching a display or window handle.
Expand Down Expand Up @@ -384,6 +396,7 @@ from_impl!(RawDisplayHandle, Windows, WindowsDisplayHandle);
from_impl!(RawDisplayHandle, WasmBindgen, WasmBindgenDisplay);
from_impl!(RawDisplayHandle, Android, AndroidDisplayHandle);
from_impl!(RawDisplayHandle, Haiku, HaikuDisplayHandle);
from_impl!(RawDisplayHandle, ToyOs, ToyOsDisplayHandle);

from_impl!(RawWindowHandle, UiKit, UiKitWindowHandle);
from_impl!(RawWindowHandle, AppKit, AppKitWindowHandle);
Expand All @@ -408,6 +421,7 @@ from_impl!(
);
from_impl!(RawWindowHandle, AndroidNdk, AndroidNdkWindowHandle);
from_impl!(RawWindowHandle, Haiku, HaikuWindowHandle);
from_impl!(RawWindowHandle, ToyOs, ToyOsWindowHandle);

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -443,6 +457,7 @@ mod tests {
assert_not_impl_any!(WasmBindgenDisplay: Send, Sync);
assert_impl_all!(AndroidDisplayHandle: Send, Sync);
assert_impl_all!(HaikuDisplayHandle: Send, Sync);
assert_impl_all!(ToyOsDisplayHandle: Send, Sync);

// TODO: Unsure if some of these should not actually be Send + Sync
assert_not_impl_any!(UiKitWindowHandle: Send, Sync);
Expand All @@ -462,6 +477,7 @@ mod tests {
assert_not_impl_any!(WasmBindgenOffscreenCanvasWindowHandle: Send, Sync);
assert_impl_all!(AndroidNdkWindowHandle: Send, Sync);
assert_impl_all!(HaikuWindowHandle: Send, Sync);
assert_impl_all!(ToyOsWindowHandle: Send, Sync);

assert_impl_all!(OwnedDisplayHandle: Unpin, UnwindSafe, RefUnwindSafe);
assert_not_impl_any!(OwnedDisplayHandle: Send, Sync);
Expand Down
92 changes: 92 additions & 0 deletions src/toyos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for ToyOS.
///
/// ## Thread Safety
///
/// The display is the ToyOS compositor, which clients reach through a per-window
/// IPC connection rather than through a shared connection object. Therefore this
/// type is `Send` and `Sync`.
///
/// Note that this type does not currently contain any ToyOS objects. This type
/// is kept as `Send` and `Sync` in preparation for such objects to be added to
/// this type.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ToyOsDisplayHandle {}

impl ToyOsDisplayHandle {
/// Create a new empty display handle.
///
///
/// # Example
///
/// ```
/// # use raw_window_handle::ToyOsDisplayHandle;
/// let handle = ToyOsDisplayHandle::new();
/// ```
pub fn new() -> Self {
Self {}
}
}

impl DisplayHandle<'static> {
/// Create a ToyOS-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::toyos();
/// do_something(handle);
/// ```
pub fn toyos() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(ToyOsDisplayHandle::new().into()) }
}
}

/// Raw window handle for ToyOS.
///
/// ## Thread Safety
///
/// The underlying window owns an IPC connection to the compositor and the
/// shared-memory framebuffer behind it, and calls on the window correspond to
/// reads and writes through that connection. Therefore this type is `Send` and
/// `Sync`. This means that this type can be sent to and used from other threads.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ToyOsWindowHandle {
/// A pointer to a ToyOS window object.
pub window: NonNull<c_void>,
}

unsafe impl Send for ToyOsWindowHandle {}
unsafe impl Sync for ToyOsWindowHandle {}

impl ToyOsWindowHandle {
/// Create a new handle to a window.
///
///
/// # Example
///
/// ```
/// # use core::ptr::NonNull;
/// # use raw_window_handle::ToyOsWindowHandle;
/// # type Window = ();
/// #
/// let window: NonNull<Window>;
/// # window = NonNull::from(&());
/// let mut handle = ToyOsWindowHandle::new(window.cast());
/// ```
pub fn new(window: NonNull<c_void>) -> Self {
Self { window }
}
}
Loading