From 44cf05a61ba4529bd47ca7584caba7e16bbf4dc1 Mon Sep 17 00:00:00 2001 From: Logan McGrath <1755866+lmcgrath@users.noreply.github.com> Date: Tue, 28 Jan 2020 21:38:53 -0800 Subject: [PATCH] tests and docs --- README.md | 16 ++++++++ changelog.md | 3 ++ src/sdl2/raw_window_handle.rs | 4 +- tests/raw_window_handle.rs | 70 +++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/raw_window_handle.rs diff --git a/README.md b/README.md index 8cffaf4a..88d355a0 100644 --- a/README.md +++ b/README.md @@ -543,6 +543,22 @@ fn main() { ``` +# Support for raw-window-handle + +`raw-window-handle` can be enabled using the feature name: + +```toml +[dependencies.sdl2] +version = "0.32" +features = ["raw-window-handle"] +``` + +### sdl2 with raw-window-handle on macOS: + +On macOS the `RawWindowHandle.ns_view` field is returned null. Libraries consuming the `RawWindowHandle` (such as +`wgpu-rs`) should determine a sane default for `ns_view`. If they do not, please file an issue with the associated +project. + # When things go wrong Rust, and Rust-SDL2, are both still heavily in development, and you may run into teething issues when using this. Before panicking, check that you're using diff --git a/changelog.md b/changelog.md index bd54b134..44c3d97e 100644 --- a/changelog.md +++ b/changelog.md @@ -27,6 +27,9 @@ Ignore unknown bits in `SDL_Keysym`'s `mod` field (key modifiers) when construct [PR #898](https://github.com/Rust-SDL2/rust-sdl2/pull/898): Implements `TryFrom` for `PixelFormat` +[PR #962](https://github.com/Rust-SDL2/rust-sdl2/pull/962): +Added `raw-window-handle` support for Windows, Linux (X11 and Wayland) and macOS. + ### v0.32.2 [PR #868](https://github.com/Rust-SDL2/rust-sdl2/pull/868): diff --git a/src/sdl2/raw_window_handle.rs b/src/sdl2/raw_window_handle.rs index b76af8bc..6dccae7b 100644 --- a/src/sdl2/raw_window_handle.rs +++ b/src/sdl2/raw_window_handle.rs @@ -1,9 +1,9 @@ extern crate raw_window_handle; use self::raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; -use crate::{sys::SDL_Window, video::Window as VideoWindow}; +use crate::{sys::SDL_Window, video::Window}; -unsafe impl HasRawWindowHandle for VideoWindow { +unsafe impl HasRawWindowHandle for Window { fn raw_window_handle(&self) -> RawWindowHandle { use self::SDL_SYSWM_TYPE::*; let mut wm_info = SDL_SysWMinfo::default(); diff --git a/tests/raw_window_handle.rs b/tests/raw_window_handle.rs new file mode 100644 index 00000000..d75ef720 --- /dev/null +++ b/tests/raw_window_handle.rs @@ -0,0 +1,70 @@ +#[cfg(feature = "raw-window-handle")] +mod raw_window_handle_test { + extern crate raw_window_handle; + extern crate sdl2; + + use self::raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; + use self::sdl2::video::Window; + + #[cfg(target_os = "windows")] + #[test] + fn get_windows_handle() { + let window = new_hidden_window(); + match window.raw_window_handle() { + RawWindowHandle::Windows(windows_handle) => { + assert_ne!(windows_handle.hwnd, 0 as *mut libc::c_void); + println!("Successfully received Windows RawWindowHandle!"); + }, + x => assert!(false, "Received wrong RawWindowHandle type for Windows: {:?}", x), + } + } + + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd", + ))] + #[test] + fn get_linux_handle() { + let window = new_hidden_window(); + match window.raw_window_handle() { + RawWindowHandle::Xlib(x11_handle) => { + assert_ne!(x11_handle.window, 0, "Window for X11 should not be 0"); + assert_ne!(x11_handle.display, 0 as *mut libc::c_void, "Display for X11 should not be null"); + println!("Successfully received linux X11 RawWindowHandle!"); + }, + RawWindowHandle::Wayland(wayland_handle) => { + assert_ne!(wayland_handle.surface, 0 as *mut libc::c_void, "Surface for Wayland should not be null"); + assert_ne!(wayland_handle.display, 0 as *mut libc::c_void, "Display for Wayland should not be null"); + println!("Successfully received linux Wayland RawWindowHandle!"); + }, + x => assert!(false, "Received wrong RawWindowHandle type for linux: {:?}", x), + } + } + + #[cfg(target_os = "macos")] + #[test] + fn get_macos_handle() { + let window = new_hidden_window(); + match window.raw_window_handle() { + RawWindowHandle::MacOS(macos_handle) => { + assert_ne!(macos_handle.ns_window, 0 as *mut libc::c_void, "ns_window should not be null"); + assert_eq!(macos_handle.ns_view, 0 as *mut libc::c_void, "nw_view should be null"); + println!("Successfully received macOS RawWindowHandle!"); + }, + x => assert!(false, "Received wrong RawWindowHandle type for macOS: {:?}", x), + }; + } + + pub fn new_hidden_window() -> Window { + let context = sdl2::init().unwrap(); + let video_subsystem = context.video().unwrap(); + video_subsystem + .window("Hello, World!", 800, 600) + .hidden() + .build() + .unwrap() + } +}