From b33fbc08061e89d405c48f8c21fe4d57d8df2bc2 Mon Sep 17 00:00:00 2001 From: Osspial Date: Wed, 16 Oct 2019 22:37:53 -0400 Subject: [PATCH] Add raw-window-handle support to Winit 0.19 and bump version (#1225) * Add raw-window-handle support to Winit 0.19 and bump version * Hopefully fix linux and ios builds --- CHANGELOG.md | 8 ++++++++ Cargo.toml | 3 ++- src/lib.rs | 1 + src/platform/ios/mod.rs | 10 ++++++++++ src/platform/linux/mod.rs | 8 ++++++++ src/platform/linux/wayland/window.rs | 9 +++++++++ src/platform/linux/x11/window.rs | 10 ++++++++++ src/platform/macos/window.rs | 11 +++++++++++ src/platform/windows/window.rs | 11 +++++++++++ src/window.rs | 6 ++++++ 10 files changed, 76 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f59c6f41..b4840b090 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Unreleased +# Version 0.19.4 (2019-10-16) + +- Add support for `raw-window-handle` 0.3. + +# Version 0.19.3 (2019-08-26) + +- Update parking_lot version. + # Version 0.19.2 (2019-07-29) - On X11, fix sanity check which checks that a monitor's reported width and height (in millimeters) are non-zero when calculating the DPI factor. diff --git a/Cargo.toml b/Cargo.toml index 6dbbf726a..c9a4e355e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "winit" -version = "0.19.3" +version = "0.19.4" authors = ["The winit contributors", "Pierre Krieger "] description = "Cross-platform window creation library." keywords = ["windowing"] @@ -22,6 +22,7 @@ libc = "0.2" log = "0.4" image = { version = "0.21", optional = true } serde = { version = "1", optional = true, features = ["serde_derive"] } +raw-window-handle = "0.3" [target.'cfg(target_os = "android")'.dependencies.android_glue] version = "0.2" diff --git a/src/lib.rs b/src/lib.rs index d4b615781..209b57d0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,6 +120,7 @@ extern crate parking_lot; extern crate percent_encoding; #[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] extern crate smithay_client_toolkit as sctk; +extern crate raw_window_handle; pub(crate) use dpi::*; // TODO: Actually change the imports throughout the codebase. pub use events::*; diff --git a/src/platform/ios/mod.rs b/src/platform/ios/mod.rs index 8bc679152..1cfd48dfb 100644 --- a/src/platform/ios/mod.rs +++ b/src/platform/ios/mod.rs @@ -60,6 +60,7 @@ #![cfg(target_os = "ios")] +use raw_window_handle::{ios::IOSHandle, RawWindowHandle}; use std::{fmt, mem, ptr}; use std::cell::RefCell; use std::collections::VecDeque; @@ -527,6 +528,15 @@ impl Window { pub fn id(&self) -> WindowId { WindowId } + + pub fn raw_window_handle(&self) -> RawWindowHandle { + let handle = IOSHandle { + ui_window: self.get_uiwindow() as *mut _, + ui_view: self.get_uiview() as *mut _, + ..IOSHandle::empty() + }; + RawWindowHandle::IOS(handle) + } } fn create_delegate_class() { diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index d74188ad0..eb4f40f36 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -7,6 +7,7 @@ use std::os::raw::*; use std::sync::Arc; use parking_lot::Mutex; +use raw_window_handle::RawWindowHandle; use sctk::reexports::client::ConnectError; use { @@ -379,6 +380,13 @@ impl Window { &Window::Wayland(ref window) => MonitorId::Wayland(window.get_primary_monitor()), } } + + pub fn raw_window_handle(&self) -> RawWindowHandle { + match self { + &Window::X(ref window) => RawWindowHandle::Xlib(window.raw_window_handle()), + &Window::Wayland(ref window) => RawWindowHandle::Wayland(window.raw_window_handle()), + } + } } unsafe extern "C" fn x_error_callback( diff --git a/src/platform/linux/wayland/window.rs b/src/platform/linux/wayland/window.rs index 417572270..3afe87acd 100644 --- a/src/platform/linux/wayland/window.rs +++ b/src/platform/linux/wayland/window.rs @@ -1,3 +1,4 @@ +use raw_window_handle::unix::WaylandHandle; use std::collections::VecDeque; use std::sync::{Arc, Mutex, Weak}; @@ -300,6 +301,14 @@ impl Window { pub fn get_primary_monitor(&self) -> MonitorId { get_primary_monitor(&self.outputs) } + + pub fn raw_window_handle(&self) -> WaylandHandle { + WaylandHandle { + surface: self.get_surface().c_ptr() as *mut _, + display: self.get_display().c_ptr() as *mut _, + ..WaylandHandle::empty() + } + } } impl Drop for Window { diff --git a/src/platform/linux/x11/window.rs b/src/platform/linux/x11/window.rs index 6d69b61cd..f59c990e6 100644 --- a/src/platform/linux/x11/window.rs +++ b/src/platform/linux/x11/window.rs @@ -1,3 +1,4 @@ +use raw_window_handle::unix::XlibHandle; use std::{cmp, env, mem}; use std::ffi::CString; use std::os::raw::*; @@ -1217,4 +1218,13 @@ impl UnownedWindow { #[inline] pub fn id(&self) -> WindowId { WindowId(self.xwindow) } + + #[inline] + pub fn raw_window_handle(&self) -> XlibHandle { + XlibHandle { + window: self.xwindow, + display: self.xconn.display as _, + ..XlibHandle::empty() + } + } } diff --git a/src/platform/macos/window.rs b/src/platform/macos/window.rs index 1f82a9360..8f4a1ef70 100644 --- a/src/platform/macos/window.rs +++ b/src/platform/macos/window.rs @@ -1,3 +1,4 @@ +use raw_window_handle::{macos::MacOSHandle, RawWindowHandle}; use std; use std::cell::{Cell, RefCell}; use std::f64; @@ -1266,6 +1267,16 @@ impl Window2 { self::get_current_monitor(*self.window) } } + + #[inline] + pub fn raw_window_handle(&self) -> RawWindowHandle { + let handle = MacOSHandle { + ns_window: self.get_nswindow(), + ns_view: self.get_nsview(), + ..MacOSHandle::empty() + }; + RawWindowHandle::MacOS(handle) + } } // Convert the `cocoa::base::id` associated with a window to a usize to use as a unique identifier diff --git a/src/platform/windows/window.rs b/src/platform/windows/window.rs index fc7256900..4a93133f2 100644 --- a/src/platform/windows/window.rs +++ b/src/platform/windows/window.rs @@ -35,6 +35,8 @@ use platform::platform::raw_input::register_all_mice_and_keyboards_for_raw_input use platform::platform::util; use platform::platform::window_state::{CursorFlags, SavedWindow, WindowFlags, WindowState}; +use raw_window_handle::{windows::WindowsHandle, RawWindowHandle}; + /// The Win32 implementation of the main `Window` object. pub struct Window { /// Main handle for the window. @@ -268,6 +270,15 @@ impl Window { self.window.0 } + #[inline] + pub fn raw_window_handle(&self) -> RawWindowHandle { + let handle = WindowsHandle { + hwnd: self.window.0 as *mut _, + ..WindowsHandle::empty() + }; + RawWindowHandle::Windows(handle) + } + #[inline] pub fn set_cursor(&self, cursor: MouseCursor) { self.window_state.lock().unwrap().mouse.cursor = cursor; diff --git a/src/window.rs b/src/window.rs index 50471ec2e..a57a40b8f 100644 --- a/src/window.rs +++ b/src/window.rs @@ -435,6 +435,12 @@ impl Window { } } +unsafe impl raw_window_handle::HasRawWindowHandle for Window { + fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { + self.window.raw_window_handle() + } +} + /// An iterator for the list of available monitors. // Implementation note: we retrieve the list once, then serve each element by one by one. // This may change in the future.