From 1e75b951a0a567a0e9620bd49fef96826b5d3d70 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sat, 16 Dec 2023 16:40:22 -0800 Subject: [PATCH] Fix eminent compile errors Signed-off-by: John Nunley --- Cargo.toml | 4 --- glutin-winit/src/lib.rs | 33 ++++++++++++++++------- glutin-winit/src/window.rs | 2 -- glutin/src/api/cgl/config.rs | 2 +- glutin/src/api/cgl/context.rs | 9 ++++--- glutin/src/api/cgl/display.rs | 4 +-- glutin/src/api/cgl/surface.rs | 25 +++++++++-------- glutin/src/api/egl/config.rs | 51 +++++++++++++---------------------- glutin/src/api/egl/display.rs | 36 ++++++++++++------------- glutin/src/api/egl/surface.rs | 40 +++++---------------------- glutin/src/api/glx/config.rs | 4 +-- glutin/src/api/glx/display.rs | 38 +++++++++++++------------- glutin/src/api/glx/surface.rs | 4 +-- glutin/src/api/wgl/config.rs | 10 +++---- glutin/src/api/wgl/context.rs | 6 ++--- glutin/src/api/wgl/display.rs | 32 ++++++++++------------ glutin/src/api/wgl/mod.rs | 6 ++--- glutin/src/api/wgl/surface.rs | 10 +++---- glutin_examples/src/lib.rs | 15 +++++------ 19 files changed, 146 insertions(+), 185 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d17e415149..277b23a41e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,3 @@ members = [ "glutin_wgl_sys", "glutin_gles2_sys", ] - -[patch.crates-io] -raw-window-handle = { git = "https://github.com/rust-windowing/raw-window-handle.git", branch = "notgull/next" } -winit = { git = "https://github.com/forkgull/winit.git", branch = "rwh-0.6" } diff --git a/glutin-winit/src/lib.rs b/glutin-winit/src/lib.rs index 0c3632a340..282b4ac449 100644 --- a/glutin-winit/src/lib.rs +++ b/glutin-winit/src/lib.rs @@ -20,13 +20,12 @@ use glutin::display::{Display, DisplayApiPreference}; use glutin::platform::x11::X11GlConfigExt; use glutin::prelude::*; -use raw_window_handle::{HasDisplayHandle, WindowHandle}; #[cfg(wgl_backend)] use raw_window_handle::HasWindowHandle; +use raw_window_handle::{DisplayHandle, HasDisplayHandle, WindowHandle}; -use raw_window_handle::{HasRawDisplayHandle, RawWindowHandle}; use winit::error::OsError; -use winit::event_loop::{EventLoopWindowTarget, OwnedDisplayHandle}; +use winit::event_loop::EventLoopWindowTarget; use winit::window::{Window, WindowBuilder}; #[cfg(glx_backend)] @@ -87,16 +86,20 @@ impl DisplayBuilder { /// **WGL:** - [`WindowBuilder`] **must** be passed in /// [`Self::with_window_builder`] if modern OpenGL(ES) is desired, /// otherwise only builtin functions like `glClear` will be available. + /// + /// # Safety + /// + /// The `Config` must not outlive the `EventLoop`. pub fn build( mut self, window_target: &EventLoopWindowTarget, template_builder: ConfigTemplateBuilder>, config_picker: Picker, - ) -> Result<(Option, Config), Box> + ) -> Result<(Option, Config>), Box> where Picker: FnOnce( - Box> + '_>, - ) -> Config, + Box>> + '_>, + ) -> Config>, { // XXX with WGL backend window should be created first. #[cfg(wgl_backend)] @@ -111,7 +114,9 @@ impl DisplayBuilder { #[cfg(not(wgl_backend))] let raw_window_handle = None; - let gl_display = create_display(window_target, self.preference, raw_window_handle)?; + // SAFETY: Will not outlive the event loop. + let gl_display = + unsafe { create_display(window_target, self.preference, raw_window_handle)? }; // XXX the native window must be passed to config picker when WGL is used // otherwise very limited OpenGL features will be supported. @@ -140,11 +145,16 @@ impl DisplayBuilder { } } -fn create_display( +/// Create the actual display. +/// +/// # Safety +/// +/// The `Display` must not outlive the `EventLoop`. +unsafe fn create_display( window_target: &EventLoopWindowTarget, _api_preference: ApiPreference, _raw_window_handle: Option>, -) -> Result, Box> { +) -> Result>, Box> { #[cfg(egl_backend)] let _preference = DisplayApiPreference::Egl; @@ -173,7 +183,10 @@ fn create_display( ApiPreference::FallbackEgl => DisplayApiPreference::WglThenEgl(_raw_window_handle), }; - Ok(Display::new(window_target.owned_display_handle(), _preference)?) + // SAFETY: Does not outlive the event loop. + let display_handle = + unsafe { DisplayHandle::borrow_raw(window_target.display_handle()?.as_raw()) }; + Ok(Display::new(display_handle, _preference)?) } /// Finalize [`Window`] creation by applying the options from the [`Config`], be diff --git a/glutin-winit/src/window.rs b/glutin-winit/src/window.rs index 3c3f2b42f6..594dc3e9c8 100644 --- a/glutin-winit/src/window.rs +++ b/glutin-winit/src/window.rs @@ -1,5 +1,3 @@ -use std::num::NonZeroU32; - use glutin::context::PossiblyCurrentContext; use glutin::surface::{ GlSurface, ResizeableSurface, Surface, SurfaceAttributes, SurfaceAttributesBuilder, diff --git a/glutin/src/api/cgl/config.rs b/glutin/src/api/cgl/config.rs index 1aba23c66a..24cf7550e5 100644 --- a/glutin/src/api/cgl/config.rs +++ b/glutin/src/api/cgl/config.rs @@ -12,7 +12,7 @@ use icrate::AppKit::{ NSOpenGLPFATripleBuffer, NSOpenGLPixelFormatAttribute, NSOpenGLProfileVersion3_2Core, NSOpenGLProfileVersion4_1Core, NSOpenGLProfileVersionLegacy, }; -use objc2::rc::{Id, Shared}; +use objc2::rc::Id; use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; use crate::config::{ diff --git a/glutin/src/api/cgl/context.rs b/glutin/src/api/cgl/context.rs index 9780f41001..6fde39bf9c 100644 --- a/glutin/src/api/cgl/context.rs +++ b/glutin/src/api/cgl/context.rs @@ -225,8 +225,9 @@ impl ContextInner { self.raw.makeCurrentContext(); let view = &surface.ns_view; + let raw = &self.raw; MainThreadMarker::run_on_main(|mtm| unsafe { - self.raw.setView(Some(view.get(mtm))); + raw.setView(Some(view.get(mtm))); }); Ok(()) @@ -249,7 +250,8 @@ impl ContextInner { } pub(crate) fn update(&self) { - MainThreadMarker::run_on_main(|_| self.raw.update()); + let raw = &self.raw; + MainThreadMarker::run_on_main(|_| raw.update()); } pub(crate) fn flush_buffer(&self) -> Result<()> { @@ -260,8 +262,9 @@ impl ContextInner { } pub(crate) fn is_view_current(&self, view: &MainThreadBound>) -> bool { + let raw = &self.raw; MainThreadMarker::run_on_main(|mtm| { - self.raw.view(mtm).expect("context to have a current view") == *view.get(mtm) + raw.view(mtm).expect("context to have a current view") == *view.get(mtm) }) } diff --git a/glutin/src/api/cgl/display.rs b/glutin/src/api/cgl/display.rs index 33da596748..90e75ad40e 100644 --- a/glutin/src/api/cgl/display.rs +++ b/glutin/src/api/cgl/display.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use core_foundation::base::TCFType; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; use core_foundation::string::CFString; -use raw_window_handle::{HasDisplayHandle, HasRawDisplayHandle, HasWindowHandle, RawDisplayHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle}; use crate::config::ConfigTemplate; use crate::display::{AsRawDisplay, DisplayFeatures, RawDisplay}; @@ -35,7 +35,7 @@ impl Clone for Display { impl Display { /// Create CGL display. pub fn new(display: D) -> Result { - match display.display_handle()?.raw_display_handle()? { + match display.display_handle()?.as_raw() { RawDisplayHandle::AppKit(..) => Ok(Display { display: Arc::new(display) }), _ => Err(ErrorKind::NotSupported("provided native display is not supported").into()), } diff --git a/glutin/src/api/cgl/surface.rs b/glutin/src/api/cgl/surface.rs index 4181c38127..6172ea89f4 100644 --- a/glutin/src/api/cgl/surface.rs +++ b/glutin/src/api/cgl/surface.rs @@ -1,12 +1,13 @@ //! Wrapper around `NSView`. use std::fmt; +use std::marker::PhantomData; use std::num::NonZeroU32; use icrate::AppKit::{NSView, NSWindow}; use icrate::Foundation::{MainThreadBound, MainThreadMarker}; use objc2::rc::Id; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use crate::config::GetGlConfig; use crate::display::GetGlDisplay; @@ -43,7 +44,7 @@ impl Display { config: &Config, surface_attributes: SurfaceAttributes>, ) -> Result>> { - let native_window = match surface_attributes.ty.0.window_handle()?.raw_window_handle()? { + let native_window = match surface_attributes.ty.0.window_handle()?.as_raw() { RawWindowHandle::AppKit(window) => window, _ => { return Err( @@ -58,21 +59,23 @@ impl Display { // SAFETY: Validity of the view and window is ensured by caller // This function makes sure the window is non null. - let ns_view = if let Some(ns_view) = unsafe { Id::retain(native_window.ns_view.cast()) } { + let ns_view: Id = if let Some(ns_view) = + unsafe { Id::retain(native_window.ns_view.as_ptr().cast()) } + { ns_view } else { return Err(ErrorKind::NotSupported("ns_view of provided native window is nil").into()); }; - let ns_view = MainThreadBound::new(ns_view, mtm); - - let ns_window = - if let Some(ns_window) = unsafe { Id::retain(native_window.ns_window.cast()) } { - ns_window - } else { + let ns_window = match unsafe { ns_view.window() } { + Some(window) => window, + None => { return Err( ErrorKind::NotSupported("ns_window of provided native window is nil").into() - ); - }; + ) + }, + }; + + let ns_view = MainThreadBound::new(ns_view, mtm); let ns_window = MainThreadBound::new(ns_window, mtm); let surface = Surface { diff --git a/glutin/src/api/egl/config.rs b/glutin/src/api/egl/config.rs index d136d93547..2e07cffac9 100644 --- a/glutin/src/api/egl/config.rs +++ b/glutin/src/api/egl/config.rs @@ -5,7 +5,7 @@ use std::ops::Deref; use std::sync::Arc; use std::{fmt, mem}; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use glutin_egl_sys::egl; use glutin_egl_sys::egl::types::{EGLConfig, EGLint}; @@ -28,6 +28,8 @@ impl Display { &self, template: ConfigTemplate, ) -> Result> + '_>> { + use raw_window_handle::{XcbWindowHandle, XlibWindowHandle}; + let mut config_attributes = Vec::::new(); // Add color buffer type. @@ -184,7 +186,7 @@ impl Display { } let raw_handle = - template._native_window.map(|w| w.window_handle()?.raw_window_handle()).transpose()?; + template._native_window.map(|w| w.window_handle().map(|w| w.as_raw())).transpose()?; let configs = found_configs .into_iter() .map(move |raw| { @@ -198,11 +200,14 @@ impl Display { // XXX This can't be done by passing visual in the EGL attributes // when calling `eglChooseConfig` since the visual is ignored. match raw_handle { - Some(RawWindowHandle::Xcb(xcb)) if xcb.visual_id > 0 => { - xcb.visual_id as u32 == config.native_visual() - }, - Some(RawWindowHandle::Xlib(xlib)) if xlib.visual_id > 0 => { - xlib.visual_id as u32 == config.native_visual() + Some(RawWindowHandle::Xcb(XcbWindowHandle { + visual_id: Some(visual_id), + .. + })) => visual_id.get() == config.native_visual(), + Some(RawWindowHandle::Xlib(XlibWindowHandle { visual_id, .. })) + if visual_id > 0 => + { + visual_id as u32 == config.native_visual() }, _ => true, } @@ -350,18 +355,8 @@ impl GlConfig for Config { #[cfg(any(wayland_platform, x11_platform))] fn supports_transparency(&self) -> Option { - use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle}; - match self - .inner - .display - .inner - ._native_display - .as_ref()? - .display_handle() - .ok()? - .raw_display_handle() - .ok()? - { + use raw_window_handle::RawDisplayHandle; + match self.inner.display.inner._native_display.as_ref()?.display_handle().ok()?.as_raw() { #[cfg(x11_platform)] RawDisplayHandle::Xlib(_) | RawDisplayHandle::Xcb(_) => { self.x11_visual().map(|visual| visual.supports_transparency()) @@ -409,21 +404,13 @@ impl AsRawConfig for Config { #[cfg(x11_platform)] impl X11GlConfigExt for Config { fn x11_visual(&self) -> Option { - use raw_window_handle::HasRawDisplayHandle; - match self - .inner - .display - .inner - ._native_display - .as_ref()? - .display_handle() - .ok()? - .raw_display_handle() - .ok()? - { + match self.inner.display.inner._native_display.as_ref()?.display_handle().ok()?.as_raw() { raw_window_handle::RawDisplayHandle::Xlib(display_handle) => unsafe { let xid = self.native_visual(); - X11VisualInfo::from_xid(display_handle.display as *mut _, xid as _) + X11VisualInfo::from_xid( + display_handle.display.map_or(std::ptr::null_mut(), |d| d.as_ptr() as *mut _), + xid as _, + ) }, _ => None, } diff --git a/glutin/src/api/egl/display.rs b/glutin/src/api/egl/display.rs index 6a261f1b12..df08cd7102 100644 --- a/glutin/src/api/egl/display.rs +++ b/glutin/src/api/egl/display.rs @@ -13,7 +13,7 @@ use glutin_egl_sys::egl::types::{EGLAttrib, EGLDisplay, EGLint}; use once_cell::sync::OnceCell; -use raw_window_handle::{HasDisplayHandle, HasRawDisplayHandle, HasWindowHandle, RawDisplayHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle}; use crate::config::ConfigTemplate; use crate::context::Version; @@ -251,12 +251,12 @@ impl Display { let extensions = CLIENT_EXTENSIONS.get().unwrap(); let mut attrs = Vec::::with_capacity(5); - let (platform, mut display) = match display.display_handle()?.raw_display_handle()? { + let (platform, display) = match display.display_handle()?.as_raw() { #[cfg(wayland_platform)] RawDisplayHandle::Wayland(handle) if extensions.contains("EGL_KHR_platform_wayland") => { - (egl::PLATFORM_WAYLAND_KHR, handle.display) + (egl::PLATFORM_WAYLAND_KHR, Some(handle.display)) }, #[cfg(x11_platform)] RawDisplayHandle::Xlib(handle) if extensions.contains("EGL_KHR_platform_x11") => { @@ -265,10 +265,10 @@ impl Display { (egl::PLATFORM_X11_KHR, handle.display) }, RawDisplayHandle::Gbm(handle) if extensions.contains("EGL_KHR_platform_gbm") => { - (egl::PLATFORM_GBM_KHR, handle.gbm_device) + (egl::PLATFORM_GBM_KHR, Some(handle.gbm_device)) }, RawDisplayHandle::Android(_) if extensions.contains("EGL_KHR_platform_android") => { - (egl::PLATFORM_ANDROID_KHR, egl::DEFAULT_DISPLAY as *mut _) + (egl::PLATFORM_ANDROID_KHR, None) }, _ => { return Err( @@ -278,9 +278,7 @@ impl Display { }; // Be explicit here. - if display.is_null() { - display = egl::DEFAULT_DISPLAY as *mut _; - } + let display = display.map_or(egl::DEFAULT_DISPLAY as *mut _, |x| x.as_ptr()); // Push at the end so we can pop it on failure let mut has_display_reference = extensions.contains("EGL_KHR_display_reference"); @@ -327,12 +325,12 @@ impl Display { let mut attrs = Vec::::with_capacity(5); let mut legacy = false; - let (platform, mut display) = match display.display_handle()?.raw_display_handle()? { + let (platform, display) = match display.display_handle()?.as_raw() { #[cfg(wayland_platform)] RawDisplayHandle::Wayland(handle) if extensions.contains("EGL_EXT_platform_wayland") => { - (egl::PLATFORM_WAYLAND_EXT, handle.display) + (egl::PLATFORM_WAYLAND_EXT, Some(handle.display)) }, #[cfg(x11_platform)] RawDisplayHandle::Xlib(handle) if extensions.contains("EGL_EXT_platform_x11") => { @@ -350,12 +348,12 @@ impl Display { (egl::PLATFORM_XCB_EXT, handle.connection) }, RawDisplayHandle::Gbm(handle) if extensions.contains("EGL_MESA_platform_gbm") => { - (egl::PLATFORM_GBM_MESA, handle.gbm_device) + (egl::PLATFORM_GBM_MESA, Some(handle.gbm_device)) }, RawDisplayHandle::Windows(..) if extensions.contains("EGL_ANGLE_platform_angle") => { // Only CreateWindowSurface appears to work with Angle. legacy = true; - (egl::PLATFORM_ANGLE_ANGLE, egl::DEFAULT_DISPLAY as *mut _) + (egl::PLATFORM_ANGLE_ANGLE, None) }, _ => { return Err( @@ -365,9 +363,7 @@ impl Display { }; // Be explicit here. - if display.is_null() { - display = egl::DEFAULT_DISPLAY as *mut _; - } + let display = display.map_or(egl::DEFAULT_DISPLAY as *mut _, |x| x.as_ptr()); // Push at the end so we can pop it on failure let mut has_display_reference = extensions.contains("EGL_KHR_display_reference"); @@ -416,10 +412,12 @@ impl Display { } fn get_display(egl: &Egl, display: &D) -> Result { - let mut display = match display.display_handle()?.raw_display_handle()? { - RawDisplayHandle::Gbm(handle) => handle.gbm_device, + let mut display = match display.display_handle()?.as_raw() { + RawDisplayHandle::Gbm(handle) => handle.gbm_device.as_ptr(), #[cfg(x11_platform)] - RawDisplayHandle::Xlib(handle) => handle.display, + RawDisplayHandle::Xlib(handle) => { + handle.display.map_or(egl::DEFAULT_DISPLAY as *mut _, |x| x.as_ptr()) + }, RawDisplayHandle::Android(_) => egl::DEFAULT_DISPLAY as *mut _, _ => { return Err( @@ -606,7 +604,7 @@ pub(crate) struct DisplayInner { pub(crate) _native_display: Option, } -impl DisplayInner { +impl DisplayInner { fn uses_display_reference(&self) -> bool { if !CLIENT_EXTENSIONS.get().unwrap().contains("EGL_KHR_display_reference") { return false; diff --git a/glutin/src/api/egl/surface.rs b/glutin/src/api/egl/surface.rs index dd4d4bdcd6..5dd8b7bd4f 100644 --- a/glutin/src/api/egl/surface.rs +++ b/glutin/src/api/egl/surface.rs @@ -5,7 +5,7 @@ use std::{ffi, fmt}; use glutin_egl_sys::egl; use glutin_egl_sys::egl::types::{EGLAttrib, EGLSurface, EGLint}; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; #[cfg(wayland_platform)] use wayland_sys::{egl::*, ffi_dispatch}; @@ -165,7 +165,7 @@ impl Display { let native_window = NativeWindow::new( surface_attributes.inner.width.unwrap(), surface_attributes.inner.height.unwrap(), - &surface_attributes.ty.0.window_handle()?.raw_window_handle()?, + &surface_attributes.ty.0.window_handle()?.as_raw(), )?; // XXX Window surface is using `EGLAttrib` and not `EGLint`. @@ -470,14 +470,10 @@ impl NativeWindow { let native_window = match raw_window_handle { #[cfg(wayland_platform)] RawWindowHandle::Wayland(window_handle) => unsafe { - if window_handle.surface.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - let ptr = ffi_dispatch!( wayland_egl_handle(), wl_egl_window_create, - window_handle.surface.cast(), + window_handle.surface.as_ptr().cast(), _width.get() as _, _height.get() as _ ); @@ -495,37 +491,15 @@ impl NativeWindow { Self::Xlib(window_handle.window as _) }, #[cfg(x11_platform)] - RawWindowHandle::Xcb(window_handle) => { - if window_handle.window == 0 { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Xcb(window_handle.window as _) - }, + RawWindowHandle::Xcb(window_handle) => Self::Xcb(window_handle.window.get() as _), #[cfg(android_platform)] RawWindowHandle::AndroidNdk(window_handle) => { - if window_handle.a_native_window.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Android(window_handle.a_native_window) + Self::Android(window_handle.a_native_window.as_ptr()) }, #[cfg(windows)] - RawWindowHandle::Win32(window_handle) => { - if window_handle.hwnd.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Win32(window_handle.hwnd as _) - }, + RawWindowHandle::Win32(window_handle) => Self::Win32(window_handle.hwnd.get() as _), #[cfg(free_unix)] - RawWindowHandle::Gbm(window_handle) => { - if window_handle.gbm_surface.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Gbm(window_handle.gbm_surface) - }, + RawWindowHandle::Gbm(window_handle) => Self::Gbm(window_handle.gbm_surface.as_ptr()), _ => { return Err( ErrorKind::NotSupported("provided native window is not supported").into() diff --git a/glutin/src/api/glx/config.rs b/glutin/src/api/glx/config.rs index 091ff7edfc..562b9dc4e3 100644 --- a/glutin/src/api/glx/config.rs +++ b/glutin/src/api/glx/config.rs @@ -7,7 +7,7 @@ use std::{fmt, slice}; use glutin_glx_sys::glx::types::GLXFBConfig; use glutin_glx_sys::{glx, glx_extra}; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use crate::config::{ Api, AsRawConfig, ColorBufferType, ConfigSurfaceTypes, ConfigTemplate, GlConfig, RawConfig, @@ -97,7 +97,7 @@ impl Display { // Add visual if was provided. if let Some(RawWindowHandle::Xlib(window)) = - template._native_window.map(|w| w.window_handle()?.raw_window_handle()).transpose()? + template._native_window.map(|w| w.window_handle().map(|w| w.as_raw())).transpose()? { if window.visual_id > 0 { config_attributes.push(glx::VISUAL_ID as c_int); diff --git a/glutin/src/api/glx/display.rs b/glutin/src/api/glx/display.rs index 6afb1a6900..0d3586ee73 100644 --- a/glutin/src/api/glx/display.rs +++ b/glutin/src/api/glx/display.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use glutin_glx_sys::glx; use glutin_glx_sys::glx::types::Display as GLXDisplay; -use raw_window_handle::{HasDisplayHandle, HasRawDisplayHandle, HasWindowHandle, RawDisplayHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle}; use crate::config::ConfigTemplate; use crate::context::Version; @@ -49,24 +49,24 @@ impl Display { error_hook_registrar: XlibErrorHookRegistrar, ) -> DisplayResult { // Don't load GLX when unsupported platform was requested. - let (display, screen) = - match display_handle.display_handle().and_then(|w| w.raw_display_handle()) { - Ok(RawDisplayHandle::Xlib(handle)) => { - if handle.display.is_null() { - return Err((ErrorKind::BadDisplay, display_handle).into()); - } - - (GlxDisplay(handle.display as *mut _), handle.screen as i32) - }, - Ok(_) => { - return Err(( - ErrorKind::NotSupported("provided native display isn't supported"), - display_handle, - ) - .into()) - }, - Err(e) => return Err((crate::error::Error::from(e), display_handle).into()), - }; + let (display, screen) = match display_handle.display_handle().map(|w| w.as_raw()) { + Ok(RawDisplayHandle::Xlib(handle)) => { + let display = match handle.display { + Some(display) => display.as_ptr(), + None => return Err((ErrorKind::BadDisplay, display_handle).into()), + }; + + (GlxDisplay(display as *mut _), handle.screen as i32) + }, + Ok(_) => { + return Err(( + ErrorKind::NotSupported("provided native display isn't supported"), + display_handle, + ) + .into()) + }, + Err(e) => return Err((crate::error::Error::from(e), display_handle).into()), + }; let glx = match GLX.as_ref() { Some(glx) => glx, diff --git a/glutin/src/api/glx/surface.rs b/glutin/src/api/glx/surface.rs index 1c10b7c578..fd1a5a11c3 100644 --- a/glutin/src/api/glx/surface.rs +++ b/glutin/src/api/glx/surface.rs @@ -7,7 +7,7 @@ use std::os::raw::{c_int, c_uint}; use glutin_glx_sys::glx::types::GLXWindow; use glutin_glx_sys::{glx, glx_extra}; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use crate::config::GetGlConfig; use crate::display::{DisplayFeatures, GetGlDisplay}; @@ -110,7 +110,7 @@ impl Display { config: &Config, surface_attributes: SurfaceAttributes>, ) -> Result>> { - let window = match surface_attributes.ty.0.window_handle()?.raw_window_handle()? { + let window = match surface_attributes.ty.0.window_handle()?.as_raw() { RawWindowHandle::Xlib(window_handle) => { if window_handle.window == 0 { return Err(ErrorKind::BadNativeWindow.into()); diff --git a/glutin/src/api/wgl/config.rs b/glutin/src/api/wgl/config.rs index e65e187a2d..9f02032e73 100644 --- a/glutin/src/api/wgl/config.rs +++ b/glutin/src/api/wgl/config.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use std::{fmt, iter}; use glutin_wgl_sys::wgl_extra; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use windows_sys::Win32::Graphics::Gdi::{self as gdi, HDC}; use windows_sys::Win32::Graphics::OpenGL::{self as gl, PIXELFORMATDESCRIPTOR}; @@ -35,10 +35,10 @@ impl Display { let hwnd = match template ._native_window .as_ref() - .map(|w| w.window_handle()?.raw_window_handle()) + .map(|w| w.window_handle().map(|w| w.as_raw())) .transpose()? { - Some(RawWindowHandle::Win32(window_handle)) => window_handle.hwnd as _, + Some(RawWindowHandle::Win32(window_handle)) => window_handle.hwnd.get() as _, _ => 0, }; let hdc = unsafe { gdi::GetDC(hwnd) }; @@ -307,8 +307,8 @@ impl Config { /// /// The `raw_window_handle` should point to a valid value. pub fn apply_on_native_window(&self, raw_window_handle: impl HasWindowHandle) -> Result<()> { - let hdc = match raw_window_handle.window_handle()?.raw_window_handle()? { - RawWindowHandle::Win32(window) => unsafe { gdi::GetDC(window.hwnd as _) }, + let hdc = match raw_window_handle.window_handle()?.as_raw() { + RawWindowHandle::Win32(window) => unsafe { gdi::GetDC(window.hwnd.get() as _) }, _ => return Err(ErrorKind::BadNativeWindow.into()), }; diff --git a/glutin/src/api/wgl/context.rs b/glutin/src/api/wgl/context.rs index 534df389c3..67ab6f396b 100644 --- a/glutin/src/api/wgl/context.rs +++ b/glutin/src/api/wgl/context.rs @@ -8,7 +8,7 @@ use std::os::raw::c_int; use glutin_wgl_sys::wgl::types::HGLRC; use glutin_wgl_sys::{wgl, wgl_extra}; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use windows_sys::Win32::Graphics::Gdi::{self as gdi, HDC}; use crate::config::GetGlConfig; @@ -35,12 +35,12 @@ impl Display { let hdc = match context_attributes ._window .as_ref() - .map(|w| w.window_handle()?.raw_window_handle()) + .map(|w| w.window_handle().map(|w| w.as_raw())) .transpose()? { Some(RawWindowHandle::Win32(window)) => unsafe { let _ = config.apply_on_native_window(context_attributes._window.as_ref().unwrap()); - gdi::GetDC(window.hwnd as _) + gdi::GetDC(window.hwnd.get() as _) }, _ => config.inner.hdc, }; diff --git a/glutin/src/api/wgl/display.rs b/glutin/src/api/wgl/display.rs index 987f981e1c..eb3136c880 100644 --- a/glutin/src/api/wgl/display.rs +++ b/glutin/src/api/wgl/display.rs @@ -7,10 +7,7 @@ use std::os::windows::ffi::OsStrExt; use std::sync::Arc; use glutin_wgl_sys::wgl; -use raw_window_handle::{ - HasDisplayHandle, HasRawDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawDisplayHandle, - RawWindowHandle, -}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle}; use windows_sys::Win32::Foundation::HMODULE; use windows_sys::Win32::Graphics::Gdi::HDC; use windows_sys::Win32::System::LibraryLoader as dll_loader; @@ -67,7 +64,7 @@ impl Display { }}; } - match leap!(display.display_handle().and_then(|r| r.raw_display_handle())) { + match leap!(display.display_handle().and_then(|r| r.display_handle().map(|r| r.as_raw()))) { RawDisplayHandle::Windows(..) => {}, _ => { return Err(( @@ -78,28 +75,27 @@ impl Display { }, }; - let name = - OsStr::new("opengl32.dll").encode_wide().chain(Some(0).into_iter()).collect::>(); + let name = OsStr::new("opengl32.dll").encode_wide().chain(Some(0)).collect::>(); let lib_opengl32 = unsafe { dll_loader::LoadLibraryW(name.as_ptr()) }; if lib_opengl32 == 0 { return Err((ErrorKind::NotFound, display).into()); } // In case native window was provided init extra functions. - let (wgl_extra, client_extensions) = match leap!(native_window - .map(|w| w.window_handle()?.raw_window_handle()) - .transpose()) - { - Some(RawWindowHandle::Win32(window)) => unsafe { - let (wgl_extra, client_extensions) = - match super::load_extra_functions(window.hinstance as _, window.hwnd as _) { + let (wgl_extra, client_extensions) = + match leap!(native_window.map(|w| w.window_handle().map(|w| w.as_raw())).transpose()) { + Some(RawWindowHandle::Win32(window)) => unsafe { + let (wgl_extra, client_extensions) = match super::load_extra_functions( + window.hinstance.map_or(0, |i| i.get()), + window.hwnd.get() as _, + ) { Ok(x) => x, Err(e) => return Err((e, display).into()), }; - (Some(wgl_extra), client_extensions) - }, - _ => (None, HashSet::new()), - }; + (Some(wgl_extra), client_extensions) + }, + _ => (None, HashSet::new()), + }; let features = Self::extract_display_features(&client_extensions); diff --git a/glutin/src/api/wgl/mod.rs b/glutin/src/api/wgl/mod.rs index 1d4de18eba..3839a1904a 100644 --- a/glutin/src/api/wgl/mod.rs +++ b/glutin/src/api/wgl/mod.rs @@ -74,8 +74,7 @@ unsafe fn load_extra_functions( class }; - let class_name = - OsStr::new("WglDummy Window").encode_wide().chain(Some(0).into_iter()).collect::>(); + let class_name = OsStr::new("WglDummy Window").encode_wide().chain(Some(0)).collect::>(); class.cbSize = mem::size_of::() as _; class.lpszClassName = class_name.as_ptr(); @@ -89,8 +88,7 @@ unsafe fn load_extra_functions( // This dummy wnidow should match the real one enough to get the same OpenGL // driver. - let title = - OsStr::new("dummy window").encode_wide().chain(Some(0).into_iter()).collect::>(); + let title = OsStr::new("dummy window").encode_wide().chain(Some(0)).collect::>(); let ex_style = wm::WS_EX_APPWINDOW; let style = wm::WS_POPUP | wm::WS_CLIPSIBLINGS | wm::WS_CLIPCHILDREN; diff --git a/glutin/src/api/wgl/surface.rs b/glutin/src/api/wgl/surface.rs index 776d0c3536..5a3793bb2f 100644 --- a/glutin/src/api/wgl/surface.rs +++ b/glutin/src/api/wgl/surface.rs @@ -4,7 +4,7 @@ use std::io::Error as IoError; use std::num::NonZeroU32; use std::{fmt, mem}; -use raw_window_handle::{HasDisplayHandle, HasRawWindowHandle, HasWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use windows_sys::Win32::Foundation::{HWND, RECT}; use windows_sys::Win32::Graphics::Gdi::HDC; use windows_sys::Win32::Graphics::{Gdi as gdi, OpenGL as gl}; @@ -46,14 +46,10 @@ impl Display { config: &Config, surface_attributes: SurfaceAttributes>, ) -> Result>> { - let hwnd = match surface_attributes.ty.0.window_handle()?.raw_window_handle()? { + let hwnd = match surface_attributes.ty.0.window_handle()?.as_raw() { RawWindowHandle::Win32(window_handle) => { - if window_handle.hwnd.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - let _ = config.apply_on_native_window(&surface_attributes.ty.0); - window_handle.hwnd as HWND + window_handle.hwnd.get() as HWND }, _ => { return Err( diff --git a/glutin_examples/src/lib.rs b/glutin_examples/src/lib.rs index 97624dba13..1e73571a43 100644 --- a/glutin_examples/src/lib.rs +++ b/glutin_examples/src/lib.rs @@ -45,14 +45,13 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box accum.num_samples() { config