Skip to content

Commit

Permalink
updates for raw-window-handle
Browse files Browse the repository at this point in the history
  • Loading branch information
HackerFoo committed Aug 14, 2022
1 parent 13722dd commit 0b3230a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }
# Used for close_on_esc
bevy_input = { path = "../bevy_input", version = "0.9.0-dev" }
raw-window-handle = "0.4.2"
raw-window-handle = "0.5"

# other

Expand Down
29 changes: 22 additions & 7 deletions crates/bevy_window/src/raw_window_handle.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, HasRawDisplayHandle, RawDisplayHandle};

/// A wrapper over [`RawWindowHandle`] that allows us to safely pass it across threads.
///
/// Depending on the platform, the underlying pointer-containing handle cannot be used on all threads,
/// and so we cannot simply make it (or any type that has a safe operation to get a [`RawWindowHandle`])
/// thread-safe.
#[derive(Debug, Clone)]
pub struct RawWindowHandleWrapper(RawWindowHandle);
pub struct RawWindowHandleWrapper {
window_handle: RawWindowHandle,
display_handle: RawDisplayHandle,
}

impl RawWindowHandleWrapper {
pub(crate) fn new(handle: RawWindowHandle) -> Self {
Self(handle)
pub(crate) fn new(window_handle: RawWindowHandle, display_handle: RawDisplayHandle) -> Self {
Self { window_handle, display_handle }
}

/// Returns a [`HasRawWindowHandle`] impl, which exposes [`RawWindowHandle`].
Expand All @@ -20,7 +23,10 @@ impl RawWindowHandleWrapper {
/// Some platforms have constraints on where/how this handle can be used. For example, some platforms don't support doing window
/// operations off of the main thread. The caller must ensure the [`RawWindowHandle`] is only used in valid contexts.
pub unsafe fn get_handle(&self) -> ThreadLockedRawWindowHandleWrapper {
ThreadLockedRawWindowHandleWrapper(self.0)
ThreadLockedRawWindowHandleWrapper {
window_handle: self.window_handle,
display_handle: self.display_handle,
}
}
}

Expand All @@ -39,7 +45,10 @@ unsafe impl Sync for RawWindowHandleWrapper {}
/// This can only be constructed via the [`RawWindowHandleWrapper::get_handle()`] method;
/// be sure to read the safety docs there about platform-specific limitations.
/// In many cases, this should only be constructed on the main thread.
pub struct ThreadLockedRawWindowHandleWrapper(RawWindowHandle);
pub struct ThreadLockedRawWindowHandleWrapper {
window_handle: RawWindowHandle,
display_handle: RawDisplayHandle,
}

// SAFETY: the caller has validated that this is a valid context to get RawWindowHandle
// as otherwise an instance of this type could not have been constructed
Expand All @@ -49,6 +58,12 @@ pub struct ThreadLockedRawWindowHandleWrapper(RawWindowHandle);
// and so exposing a safe method to get a `RawWindowHandle` directly would be UB.
unsafe impl HasRawWindowHandle for ThreadLockedRawWindowHandleWrapper {
fn raw_window_handle(&self) -> RawWindowHandle {
self.0
self.window_handle
}
}

unsafe impl HasRawDisplayHandle for ThreadLockedRawWindowHandleWrapper {
fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
self.display_handle
}
}
5 changes: 3 additions & 2 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy_ecs::system::Resource;
use bevy_math::{DVec2, IVec2, UVec2, Vec2};
use bevy_reflect::{FromReflect, Reflect};
use bevy_utils::{tracing::warn, Uuid};
use raw_window_handle::RawWindowHandle;
use raw_window_handle::{RawWindowHandle, RawDisplayHandle};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Reflect, FromReflect)]
#[reflect_value(PartialEq, Hash)]
Expand Down Expand Up @@ -320,6 +320,7 @@ impl Window {
scale_factor: f64,
position: Option<IVec2>,
raw_window_handle: RawWindowHandle,
raw_display_handle: RawDisplayHandle,
) -> Self {
Window {
id,
Expand All @@ -339,7 +340,7 @@ impl Window {
cursor_grab_mode: window_descriptor.cursor_grab_mode,
cursor_icon: CursorIcon::Default,
physical_cursor_position: None,
raw_window_handle: RawWindowHandleWrapper::new(raw_window_handle),
raw_window_handle: RawWindowHandleWrapper::new(raw_window_handle, raw_display_handle),
focused: true,
mode: window_descriptor.mode,
canvas: window_descriptor.canvas.clone(),
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ bevy_window = { path = "../bevy_window", version = "0.9.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }

# other
winit = { version = "0.26.0", default-features = false }
winit = { version = "0.27.0", default-features = false }
approx = { version = "0.5.0", default-features = false }
raw-window-handle = "0.4.2"
raw-window-handle = "0.5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = { version = "0.26.0", default-features = false }
winit = { version = "0.27.0", default-features = false }
wasm-bindgen = { version = "0.2" }
web-sys = "0.3"
crossbeam-channel = "0.5"
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_math::IVec2;
use bevy_utils::{tracing::warn, HashMap};
use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode};
use raw_window_handle::HasRawWindowHandle;
use raw_window_handle::{HasRawWindowHandle, HasRawDisplayHandle};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition};

use crate::winit_grab_mode;
Expand Down Expand Up @@ -190,6 +190,7 @@ impl WinitWindows {
let inner_size = winit_window.inner_size();
let scale_factor = winit_window.scale_factor();
let raw_window_handle = winit_window.raw_window_handle();
let raw_display_handle = winit_window.raw_display_handle();
self.windows.insert(winit_window.id(), winit_window);
Window::new(
window_id,
Expand All @@ -199,6 +200,7 @@ impl WinitWindows {
scale_factor,
position,
raw_window_handle,
raw_display_handle,
)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/input/mouse_grab.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Demonstrates how to grab and hide the mouse cursor.

use bevy::prelude::*;
use bevy::{prelude::*, window::CursorGrabMode};

fn main() {
App::new()
Expand All @@ -19,10 +19,10 @@ fn grab_mouse(
let window = windows.get_primary_mut().unwrap();
if mouse.just_pressed(MouseButton::Left) {
window.set_cursor_visibility(false);
window.set_cursor_lock_mode(true);
window.set_cursor_grab_mode(CursorGrabMode::Locked);
}
if key.just_pressed(KeyCode::Escape) {
window.set_cursor_visibility(true);
window.set_cursor_lock_mode(false);
window.set_cursor_grab_mode(CursorGrabMode::None);
}
}
10 changes: 8 additions & 2 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Illustrates how to change window settings and shows how to affect
//! the mouse pointer in various ways.

use bevy::{prelude::*, window::PresentMode};
use bevy::{
prelude::*,
window::{CursorGrabMode, PresentMode},
};

fn main() {
App::new()
Expand Down Expand Up @@ -32,7 +35,10 @@ fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
fn toggle_cursor(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
let window = windows.primary_mut();
if input.just_pressed(KeyCode::Space) {
window.set_cursor_lock_mode(!window.cursor_locked());
window.set_cursor_grab_mode(match window.cursor_grab_mode() {
CursorGrabMode::None => CursorGrabMode::Locked,
_ => CursorGrabMode::None,
});
window.set_cursor_visibility(!window.cursor_visible());
}
}
Expand Down

0 comments on commit 0b3230a

Please sign in to comment.