From 04f113b46bcd6534e00a0150a89845290f594e13 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 14 Jul 2024 12:05:27 -0700 Subject: [PATCH] wgpu -> 0.20 This compiles, but when I run it on X11 I get a fully transparent window. I have not tried to debug this yet. --- Cargo.lock | 174 ++++++++++++++++----------- wezterm-gui/Cargo.toml | 2 +- wezterm-gui/src/scripting/mod.rs | 1 + wezterm-gui/src/termwindow/webgpu.rs | 35 +++--- window/Cargo.toml | 3 +- window/src/os/macos/window.rs | 37 +++--- window/src/os/wayland/window.rs | 43 ++++--- window/src/os/windows/window.rs | 44 ++++--- window/src/os/x11/window.rs | 53 ++++---- window/src/os/x_and_wayland.rs | 18 +-- 10 files changed, 237 insertions(+), 173 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64f3bec91d6..55d9e40e8ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -906,10 +906,35 @@ dependencies = [ ] [[package]] -name = "com-rs" -version = "0.2.1" +name = "com" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e17887fd17353b65b1b2ef1c526c83e26cd72e74f598a8dc1bee13a48f3d9f6" +dependencies = [ + "com_macros", +] + +[[package]] +name = "com_macros" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d375883580a668c7481ea6631fc1a8863e33cc335bf56bfad8d7e6d4b04b13a5" +dependencies = [ + "com_macros_support", + "proc-macro2", + "syn 1.0.109", +] + +[[package]] +name = "com_macros_support" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf43edc576402991846b093a7ca18a3477e0ef9c588cde84964b5d3e43016642" +checksum = "ad899a1087a9296d5644792d7cb72b8e34c1bec8e7d4fbc002230169a6e8710c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] [[package]] name = "concurrent-queue" @@ -1163,9 +1188,9 @@ checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" [[package]] name = "d3d12" -version = "0.7.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e16e44ab292b1dddfdaf7be62cfd8877df52f2f3fde5858d95bab606be259f20" +checksum = "b28bfe653d79bd16c77f659305b195b82bb5ce0c0eb2a4846b82ddbd77586813" dependencies = [ "bitflags 2.6.0", "libloading 0.8.4", @@ -1309,6 +1334,15 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "document-features" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" +dependencies = [ + "litrs", +] + [[package]] name = "downcast-rs" version = "1.2.1" @@ -2055,23 +2089,22 @@ dependencies = [ [[package]] name = "gpu-allocator" -version = "0.23.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40fe17c8a05d60c38c0a4e5a3c802f2f1ceb66b76c67d96ffb34bef0475a7fad" +checksum = "6f56f6318968d03c18e1bcf4857ff88c61157e9da8e47c5f29055d60e1228884" dependencies = [ - "backtrace", "log", "presser", "thiserror", "winapi", - "windows 0.51.1", + "windows 0.52.0", ] [[package]] name = "gpu-descriptor" -version = "0.2.4" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc11df1ace8e7e564511f53af41f3e42ddc95b56fd07b3f4445d2a6048bc682c" +checksum = "9c08c1f623a8d0b722b8b99f821eb0ba672a1618f0d3b16ddbee1cedd2dd8557" dependencies = [ "bitflags 2.6.0", "gpu-descriptor-types", @@ -2080,9 +2113,9 @@ dependencies = [ [[package]] name = "gpu-descriptor-types" -version = "0.1.2" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bf0b36e6f090b7e1d8a4b49c0cb81c1f8376f72198c65dd3ad9ff3556b8b78c" +checksum = "fdf242682df893b86f33a73828fb09ca4b2d3bb6cc95249707fc684d27484b91" dependencies = [ "bitflags 2.6.0", ] @@ -2161,14 +2194,14 @@ dependencies = [ [[package]] name = "hassle-rs" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1397650ee315e8891a0df210707f0fc61771b0cc518c3023896064c5407cb3b0" +checksum = "af2a7e73e1f34c48da31fb668a907f250794837e08faa144fd24f0b8b741e890" dependencies = [ - "bitflags 1.3.2", - "com-rs", + "bitflags 2.6.0", + "com", "libc", - "libloading 0.7.4", + "libloading 0.8.4", "thiserror", "widestring", "winapi", @@ -2387,7 +2420,7 @@ dependencies = [ "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows-core 0.52.0", + "windows-core", ] [[package]] @@ -2607,6 +2640,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + [[package]] name = "jobserver" version = "0.1.31" @@ -2900,6 +2939,12 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "litrs" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" + [[package]] name = "lock_api" version = "0.4.12" @@ -3058,9 +3103,9 @@ dependencies = [ [[package]] name = "metal" -version = "0.27.0" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c43f73953f8cbe511f021b58f18c3ce1c3d1ae13fe953293e13345bf83217f25" +checksum = "5637e166ea14be6063a3f8ba5ccb9a4159df7d8f6d61c02fc3d480b1f90dcfcb" dependencies = [ "bitflags 2.6.0", "block", @@ -3219,10 +3264,11 @@ dependencies = [ [[package]] name = "naga" -version = "0.14.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae585df4b6514cf8842ac0f1ab4992edc975892704835b549cf818dc0191249e" +checksum = "e536ae46fcab0876853bd4a632ede5df4b1c2527a58f6c5a4150fe86be858231" dependencies = [ + "arrayvec", "bit-set", "bitflags 2.6.0", "codespan-reporting", @@ -3272,6 +3318,15 @@ dependencies = [ "tempfile", ] +[[package]] +name = "ndk-sys" +version = "0.5.0+25.2.9519653" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c196769dd60fd4f363e11d948139556a344e79d451aeb2fa2fd040738ef7691" +dependencies = [ + "jni-sys", +] + [[package]] name = "new_debug_unreachable" version = "1.0.6" @@ -3482,16 +3537,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" dependencies = [ "malloc_buf", - "objc_exception", -] - -[[package]] -name = "objc_exception" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad970fb455818ad6cba4c122ad012fae53ae8b4795f86378bce65e4f6bab2ca4" -dependencies = [ - "cc", ] [[package]] @@ -4208,9 +4253,9 @@ dependencies = [ [[package]] name = "raw-window-handle" -version = "0.5.2" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" +checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" [[package]] name = "rayon" @@ -4978,12 +5023,11 @@ dependencies = [ [[package]] name = "spirv" -version = "0.2.0+1.5.4" +version = "0.3.0+sdk-1.3.268.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "246bfa38fe3db3f1dfc8ca5a2cdeb7348c78be2112740cc0ec8ef18b6d94f830" +checksum = "eda41003dc44290527a59b13432d4a0379379fa074b70174882adfbdfd917844" dependencies = [ - "bitflags 1.3.2", - "num-traits", + "bitflags 2.6.0", ] [[package]] @@ -5977,6 +6021,7 @@ checksum = "269c04f203640d0da2092d1b8d89a2d081714ae3ac2f1b53e99f205740517198" dependencies = [ "cc", "downcast-rs", + "raw-window-handle", "rustix 0.38.34", "scoped-tls", "smallvec", @@ -6076,9 +6121,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.64" +version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" dependencies = [ "js-sys", "wasm-bindgen", @@ -6540,13 +6585,14 @@ dependencies = [ [[package]] name = "wgpu" -version = "0.18.0" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30e7d227c9f961f2061c26f4cb0fbd4df0ef37e056edd0931783599d6c94ef24" +checksum = "90e37c7b9921b75dfd26dd973fdcbce36f13dfa6e2dc82aece584e0ed48c355c" dependencies = [ "arrayvec", "cfg-if", - "flume", + "cfg_aliases 0.1.1", + "document-features", "js-sys", "log", "naga", @@ -6565,16 +6611,20 @@ dependencies = [ [[package]] name = "wgpu-core" -version = "0.18.1" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef91c1d62d1e9e81c79e600131a258edf75c9531cbdbde09c44a011a47312726" +checksum = "d50819ab545b867d8a454d1d756b90cd5f15da1f2943334ca314af10583c9d39" dependencies = [ "arrayvec", "bit-vec", "bitflags 2.6.0", + "cfg_aliases 0.1.1", "codespan-reporting", + "document-features", + "indexmap 2.2.6", "log", "naga", + "once_cell", "parking_lot 0.12.3", "profiling", "raw-window-handle", @@ -6588,9 +6638,9 @@ dependencies = [ [[package]] name = "wgpu-hal" -version = "0.18.1" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84ecc802da3eb67b4cf3dd9ea6fe45bbb47ef13e6c49c5c3240868a9cc6cdd9" +checksum = "172e490a87295564f3fcc0f165798d87386f6231b04d4548bca458cbbfd63222" dependencies = [ "android_system_properties", "arrayvec", @@ -6598,6 +6648,7 @@ dependencies = [ "bit-set", "bitflags 2.6.0", "block", + "cfg_aliases 0.1.1", "core-graphics-types", "d3d12", "glow", @@ -6613,6 +6664,7 @@ dependencies = [ "log", "metal", "naga", + "ndk-sys", "objc", "once_cell", "parking_lot 0.12.3", @@ -6631,9 +6683,9 @@ dependencies = [ [[package]] name = "wgpu-types" -version = "0.18.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d5ed5f0edf0de351fe311c53304986315ce866f394a2e6df0c4b3c70774bcdd" +checksum = "1353d9a46bff7f955a680577f34c69122628cc2076e1d6f3a9be6ef00ae793ef" dependencies = [ "bitflags 2.6.0", "js-sys", @@ -6746,6 +6798,7 @@ dependencies = [ "thiserror", "tiny-skia", "url", + "wayland-backend", "wayland-client", "wayland-egl", "wayland-protocols", @@ -6788,35 +6841,16 @@ dependencies = [ "windows_x86_64_msvc 0.33.0", ] -[[package]] -name = "windows" -version = "0.51.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" -dependencies = [ - "windows-core 0.51.1", - "windows-targets 0.48.5", -] - [[package]] name = "windows" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" dependencies = [ - "windows-core 0.52.0", + "windows-core", "windows-targets 0.52.6", ] -[[package]] -name = "windows-core" -version = "0.51.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" -dependencies = [ - "windows-targets 0.48.5", -] - [[package]] name = "windows-core" version = "0.52.0" diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.toml index e4be8916871..36104406e1d 100644 --- a/wezterm-gui/Cargo.toml +++ b/wezterm-gui/Cargo.toml @@ -105,7 +105,7 @@ wezterm-open-url = { path = "../wezterm-open-url" } wezterm-ssh = { path = "../wezterm-ssh" } wezterm-term = { path = "../term", features=["use_serde"] } wezterm-toast-notification = { path = "../wezterm-toast-notification" } -wgpu = "0.18" +wgpu = "0.20" window = { path = "../window" } window-funcs = { path = "../lua-api-crates/window-funcs" } diff --git a/wezterm-gui/src/scripting/mod.rs b/wezterm-gui/src/scripting/mod.rs index bcb447b22bc..8867be45d57 100644 --- a/wezterm-gui/src/scripting/mod.rs +++ b/wezterm-gui/src/scripting/mod.rs @@ -88,6 +88,7 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { }); let gpus: Vec = instance .enumerate_adapters(backends) + .into_iter() .map(|adapter| { let info = adapter.get_info(); crate::termwindow::webgpu::adapter_info_to_gpu_info(info) diff --git a/wezterm-gui/src/termwindow/webgpu.rs b/wezterm-gui/src/termwindow/webgpu.rs index 204259de715..c3d308b1a26 100644 --- a/wezterm-gui/src/termwindow/webgpu.rs +++ b/wezterm-gui/src/termwindow/webgpu.rs @@ -6,7 +6,8 @@ use std::sync::Arc; use wgpu::util::DeviceExt; use window::bitmaps::Texture2d; use window::raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle, + RawWindowHandle, WindowHandle, }; use window::{BitmapImage, Dimensions, Rect, Window}; @@ -23,7 +24,7 @@ pub struct ShaderUniform { pub struct WebGpuState { pub adapter_info: wgpu::AdapterInfo, pub downlevel_caps: wgpu::DownlevelCapabilities, - pub surface: wgpu::Surface, + pub surface: wgpu::Surface<'static>, pub device: wgpu::Device, pub queue: Arc, pub config: RefCell, @@ -44,21 +45,21 @@ pub struct RawHandlePair { impl RawHandlePair { fn new(window: &Window) -> Self { Self { - window: window.raw_window_handle(), - display: window.raw_display_handle(), + window: window.window_handle().expect("window handle").as_raw(), + display: window.display_handle().expect("display handle").as_raw(), } } } -unsafe impl HasRawWindowHandle for RawHandlePair { - fn raw_window_handle(&self) -> RawWindowHandle { - self.window +impl HasWindowHandle for RawHandlePair { + fn window_handle(&self) -> Result { + unsafe { Ok(WindowHandle::borrow_raw(self.window)) } } } -unsafe impl HasRawDisplayHandle for RawHandlePair { - fn raw_display_handle(&self) -> RawDisplayHandle { - self.display +impl HasDisplayHandle for RawHandlePair { + fn display_handle(&self) -> Result { + unsafe { Ok(DisplayHandle::borrow_raw(self.display)) } } } @@ -194,6 +195,7 @@ fn compute_compatibility_list( ) -> Vec { instance .enumerate_adapters(backends) + .into_iter() .map(|a| { let info = adapter_info_to_gpu_info(a.get_info()); let compatible = a.is_surface_supported(&surface); @@ -226,7 +228,9 @@ impl WebGpuState { backends, ..Default::default() }); - let surface = unsafe { instance.create_surface(&handle)? }; + let surface = unsafe { + instance.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::from_window(&handle)?)? + }; let mut adapter: Option = None; @@ -316,10 +320,10 @@ impl WebGpuState { let (device, queue) = adapter .request_device( &wgpu::DeviceDescriptor { - features: wgpu::Features::empty(), + required_features: wgpu::Features::empty(), // WebGL doesn't support all of wgpu's features, so if // we're building for the web we'll have to disable some. - limits: if cfg!(target_arch = "wasm32") { + required_limits: if cfg!(target_arch = "wasm32") { wgpu::Limits::downlevel_webgl2_defaults() } else { wgpu::Limits::downlevel_defaults() @@ -374,6 +378,7 @@ impl WebGpuState { wgpu::CompositeAlphaMode::Auto }, view_formats, + desired_maximum_frame_latency: 2, }; surface.configure(&device, &config); @@ -454,6 +459,7 @@ impl WebGpuState { module: &shader, entry_point: "vs_main", buffers: &[Vertex::desc()], + compilation_options: wgpu::PipelineCompilationOptions::default(), }, fragment: Some(wgpu::FragmentState { module: &shader, @@ -463,6 +469,7 @@ impl WebGpuState { blend: Some(wgpu::BlendState::ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL, })], + compilation_options: wgpu::PipelineCompilationOptions::default(), }), primitive: wgpu::PrimitiveState { @@ -528,7 +535,7 @@ impl WebGpuState { #[cfg(windows)] RawWindowHandle::Win32(h) => { let mut rect = unsafe { std::mem::zeroed() }; - unsafe { winapi::um::winuser::GetClientRect(h.hwnd as _, &mut rect) }; + unsafe { winapi::um::winuser::GetClientRect(h.hwnd.get() as _, &mut rect) }; dims.pixel_width = (rect.right - rect.left) as usize; dims.pixel_height = (rect.bottom - rect.top) as usize; } diff --git a/window/Cargo.toml b/window/Cargo.toml index 3cfa26dc9b6..8da9eb1d4b6 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -36,7 +36,7 @@ line_drawing = "0.8" log = "0.4" metrics = "0.23" promise = { path = "../promise" } -raw-window-handle = "0.5" +raw-window-handle = "0.6" resize = "0.5" serde = {version="1.0", features = ["rc", "derive"]} tiny-skia = "0.11" @@ -82,6 +82,7 @@ zbus = "4.2" zvariant = "4.0" smithay-client-toolkit = {version = "0.19", default-features=false, optional=true} +wayland-backend = {version="0.3.5", features=["client_system", "rwh_06"]} wayland-protocols = {version="0.32", optional=true} wayland-client = {version="0.31", optional=true} wayland-egl = {version="0.32", optional=true} diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 7576e72b637..b0657dfbd18 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -39,13 +39,14 @@ use objc::runtime::{Class, Object, Protocol, Sel}; use objc::*; use promise::Future; use raw_window_handle::{ - AppKitDisplayHandle, AppKitWindowHandle, HasRawDisplayHandle, HasRawWindowHandle, - RawDisplayHandle, RawWindowHandle, + AppKitDisplayHandle, AppKitWindowHandle, DisplayHandle, HandleError, HasDisplayHandle, + HasWindowHandle, RawDisplayHandle, RawWindowHandle, WindowHandle, }; use std::any::Any; use std::cell::RefCell; use std::ffi::c_void; use std::path::PathBuf; +use std::ptr::NonNull; use std::rc::Rc; use std::str::FromStr; use std::time::Instant; @@ -661,18 +662,21 @@ impl Window { } } -unsafe impl HasRawDisplayHandle for Window { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) +impl HasDisplayHandle for Window { + fn display_handle(&self) -> Result { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::AppKit( + AppKitDisplayHandle::new(), + ))) + } } } -unsafe impl HasRawWindowHandle for Window { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = AppKitWindowHandle::empty(); - handle.ns_window = self.ns_window as *mut _; - handle.ns_view = self.ns_view as *mut _; - RawWindowHandle::AppKit(handle) +impl HasWindowHandle for Window { + fn window_handle(&self) -> Result { + let mut handle = + AppKitWindowHandle::new(NonNull::new(self.ns_view as *mut _).expect("non-null")); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::AppKit(handle))) } } } @@ -865,18 +869,13 @@ impl WindowOps for Window { _config: &ConfigHandle, window_state: WindowState, ) -> anyhow::Result> { - let raw = self.raw_window_handle(); - // We implement this method primarily to provide Notch-avoidance for // systems with a notch. // We only need this for non-native full screen mode. - let native_full_screen = match raw { - RawWindowHandle::AppKit(raw) => { - let style_mask = unsafe { NSWindow::styleMask(raw.ns_window as *mut Object) }; - style_mask.contains(NSWindowStyleMask::NSFullScreenWindowMask) - } - _ => false, + let native_full_screen = { + let style_mask = unsafe { NSWindow::styleMask(self.ns_window) }; + style_mask.contains(NSWindowStyleMask::NSFullScreenWindowMask) }; let border_dimensions = diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index 254f42dea78..62149dc3cd5 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -6,6 +6,7 @@ use std::io::Read; use std::num::NonZeroU32; use std::os::fd::AsRawFd; use std::path::PathBuf; +use std::ptr::NonNull; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -16,8 +17,8 @@ use async_trait::async_trait; use config::ConfigHandle; use promise::{Future, Promise}; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, - WaylandDisplayHandle, WaylandWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawWindowHandle, + WaylandWindowHandle, WindowHandle, }; use smithay_client_toolkit::compositor::{CompositorHandler, SurfaceData, SurfaceDataExt}; use smithay_client_toolkit::data_device_manager::ReadPipe; @@ -1377,26 +1378,35 @@ impl SurfaceDataExt for SurfaceUserData { } } -unsafe impl HasRawWindowHandle for WaylandWindowInner { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = WaylandWindowHandle::empty(); - let surface = self.surface(); - handle.surface = surface.id().as_ptr() as *mut _; - RawWindowHandle::Wayland(handle) +impl HasDisplayHandle for WaylandWindowInner { + fn display_handle(&self) -> Result { + let conn = WaylandConnection::get().unwrap().wayland(); + let backend = conn.connection.backend(); + let handle = backend.display_handle()?; + Ok(unsafe { DisplayHandle::borrow_raw(handle.as_raw()) }) + } +} + +impl HasWindowHandle for WaylandWindowInner { + fn window_handle(&self) -> Result { + let handle = WaylandWindowHandle::new( + NonNull::new(self.surface().id().as_ptr() as _).expect("non-null"), + ); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Wayland(handle))) } } } -unsafe impl HasRawDisplayHandle for WaylandWindow { - fn raw_display_handle(&self) -> RawDisplayHandle { - let mut handle = WaylandDisplayHandle::empty(); +impl HasDisplayHandle for WaylandWindow { + fn display_handle(&self) -> Result { let conn = WaylandConnection::get().unwrap().wayland(); - handle.display = conn.connection.backend().display_ptr() as *mut _; - RawDisplayHandle::Wayland(handle) + let backend = conn.connection.backend(); + let handle = backend.display_handle()?; + Ok(unsafe { DisplayHandle::borrow_raw(handle.as_raw()) }) } } -unsafe impl HasRawWindowHandle for WaylandWindow { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for WaylandWindow { + fn window_handle(&self) -> Result { let conn = Connection::get().expect("raw_window_handle only callable on main thread"); let handle = conn .wayland() @@ -1404,6 +1414,7 @@ unsafe impl HasRawWindowHandle for WaylandWindow { .expect("window handle invalid!?"); let inner = handle.borrow(); - inner.raw_window_handle() + let handle = inner.window_handle()?; + unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) } } } diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 58053344991..21dfa522d88 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -13,8 +13,8 @@ use config::{ConfigHandle, ImePreeditRendering, SystemBackdrop}; use lazy_static::lazy_static; use promise::Future; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, Win32WindowHandle, - WindowsDisplayHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle, + RawWindowHandle, Win32WindowHandle, WindowHandle, WindowsDisplayHandle, }; use shared_library::shared_library; use std::any::Any; @@ -23,6 +23,7 @@ use std::collections::HashMap; use std::convert::TryInto; use std::ffi::OsString; use std::io::{self, Error as IoError}; +use std::num::NonZeroIsize; use std::os::windows::ffi::OsStringExt; use std::path::PathBuf; use std::ptr::{null, null_mut}; @@ -200,18 +201,22 @@ fn callback_behavior() -> glium::debug::DebugCallbackBehavior { } } -unsafe impl HasRawDisplayHandle for WindowInner { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) +impl HasDisplayHandle for WindowInner { + fn display_handle(&self) -> Result { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Windows( + WindowsDisplayHandle::new(), + ))) + } } } -unsafe impl HasRawWindowHandle for WindowInner { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = Win32WindowHandle::empty(); - handle.hwnd = self.hwnd.0 as *mut _; - handle.hinstance = unsafe { GetModuleHandleW(null()) } as _; - RawWindowHandle::Win32(handle) +impl HasWindowHandle for WindowInner { + fn window_handle(&self) -> Result { + let mut handle = + Win32WindowHandle::new(NonZeroIsize::new(self.hwnd.0 as _).expect("non-zero")); + handle.hinstance = NonZeroIsize::new(unsafe { GetModuleHandleW(null()) } as _); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Win32(handle))) } } } @@ -729,19 +734,24 @@ impl WindowInner { } } -unsafe impl HasRawDisplayHandle for Window { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) +impl HasDisplayHandle for Window { + fn display_handle(&self) -> Result { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Windows( + WindowsDisplayHandle::new(), + ))) + } } } -unsafe impl HasRawWindowHandle for Window { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for Window { + fn window_handle(&self) -> Result { let conn = Connection::get().expect("raw_window_handle only callable on main thread"); let handle = conn.get_window(self.0).expect("window handle invalid!?"); let inner = handle.borrow(); - inner.raw_window_handle() + let handle = inner.window_handle()?; + unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) } } } diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 58ec4f8936f..e6bd87c750f 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -13,12 +13,14 @@ use async_trait::async_trait; use config::ConfigHandle; use promise::{Future, Promise}; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, - XcbWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle, + RawWindowHandle, WindowHandle, XcbDisplayHandle, XcbWindowHandle, }; use std::any::Any; use std::convert::TryInto; +use std::num::NonZeroU32; use std::path::PathBuf; +use std::ptr::NonNull; use std::rc::{Rc, Weak}; use std::sync::{Arc, Mutex}; use url::Url; @@ -134,24 +136,24 @@ impl Drop for XWindowInner { } } -unsafe impl HasRawDisplayHandle for XWindowInner { - fn raw_display_handle(&self) -> RawDisplayHandle { - let mut handle = XcbDisplayHandle::empty(); +impl HasDisplayHandle for XWindowInner { + fn display_handle(&self) -> Result, HandleError> { if let Some(conn) = self.conn.upgrade() { - handle.connection = conn.conn.get_raw_conn() as _; - handle.screen = conn.screen_num; + let handle = + XcbDisplayHandle::new(NonNull::new(conn.conn.get_raw_conn() as _), conn.screen_num); + unsafe { Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Xcb(handle))) } + } else { + Err(HandleError::Unavailable) } - - RawDisplayHandle::Xcb(handle) } } -unsafe impl HasRawWindowHandle for XWindowInner { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = XcbWindowHandle::empty(); - handle.window = self.child_id.resource_id(); - handle.visual_id = self.conn.upgrade().unwrap().visual.visual_id(); - RawWindowHandle::Xcb(handle) +impl HasWindowHandle for XWindowInner { + fn window_handle(&self) -> Result, HandleError> { + let mut handle = + XcbWindowHandle::new(NonZeroU32::new(self.window_id.resource_id()).expect("non-zero")); + handle.visual_id = NonZeroU32::new(self.conn.upgrade().unwrap().visual.visual_id()); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Xcb(handle))) } } } @@ -1909,29 +1911,28 @@ impl XWindowInner { } } -unsafe impl HasRawDisplayHandle for XWindow { - fn raw_display_handle(&self) -> RawDisplayHandle { +impl HasDisplayHandle for XWindow { + fn display_handle(&self) -> Result, HandleError> { let conn = Connection::get() - .expect("raw_window_handle only callable on main thread") + .expect("display_handle only callable on main thread") .x11(); - let mut handle = XcbDisplayHandle::empty(); - handle.connection = conn.get_raw_conn() as _; - handle.screen = conn.screen_num; + let handle = XcbDisplayHandle::new(NonNull::new(conn.get_raw_conn() as _), conn.screen_num); - RawDisplayHandle::Xcb(handle) + unsafe { Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Xcb(handle))) } } } -unsafe impl HasRawWindowHandle for XWindow { - fn raw_window_handle(&self) -> RawWindowHandle { - let conn = Connection::get().expect("raw_window_handle only callable on main thread"); +impl HasWindowHandle for XWindow { + fn window_handle(&self) -> Result, HandleError> { + let conn = Connection::get().expect("window_handle only callable on main thread"); let handle = conn .x11() .window_by_id(self.0) .expect("window handle invalid!?"); let inner = handle.lock().unwrap(); - inner.raw_window_handle() + let handle = inner.window_handle()?; + unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) } } } diff --git a/window/src/os/x_and_wayland.rs b/window/src/os/x_and_wayland.rs index d785feced80..b66d18d8201 100644 --- a/window/src/os/x_and_wayland.rs +++ b/window/src/os/x_and_wayland.rs @@ -16,7 +16,7 @@ use async_trait::async_trait; use config::ConfigHandle; use promise::*; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle, }; use std::any::Any; use std::rc::Rc; @@ -202,22 +202,22 @@ impl Window { } } -unsafe impl HasRawDisplayHandle for Window { - fn raw_display_handle(&self) -> RawDisplayHandle { +impl HasDisplayHandle for Window { + fn display_handle(&self) -> Result, HandleError> { match self { - Self::X11(x) => x.raw_display_handle(), + Self::X11(x) => x.display_handle(), #[cfg(feature = "wayland")] - Self::Wayland(w) => w.raw_display_handle(), + Self::Wayland(w) => w.display_handle(), } } } -unsafe impl HasRawWindowHandle for Window { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for Window { + fn window_handle(&self) -> Result, HandleError> { match self { - Self::X11(x) => x.raw_window_handle(), + Self::X11(x) => x.window_handle(), #[cfg(feature = "wayland")] - Self::Wayland(w) => w.raw_window_handle(), + Self::Wayland(w) => w.window_handle(), } } }