Skip to content

Commit d30d325

Browse files
committed
Updated winit to 0.27.2 for examples and raw-window-handle to 0.5.0
1 parent bef3c52 commit d30d325

File tree

8 files changed

+72
-41
lines changed

8 files changed

+72
-41
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exclude = ["examples"]
1515

1616
[dependencies]
1717
thiserror = "1.0.30"
18-
raw-window-handle = "0.4.2"
18+
raw-window-handle = "0.5.0"
1919

2020
[target.'cfg(target_os = "linux")'.dependencies]
2121
tempfile = "3.3.0"
@@ -41,7 +41,7 @@ features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElemen
4141

4242
[dev-dependencies]
4343
instant = "0.1.12"
44-
winit = "0.26.1"
44+
winit = "0.27.2"
4545

4646
[dev-dependencies.image]
4747
version = "0.23.14"

src/cg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{GraphicsContextImpl, SoftBufferError};
2-
use raw_window_handle::{HasRawWindowHandle, AppKitHandle};
2+
use raw_window_handle::{HasRawWindowHandle, AppKitWindowHandle};
33
use core_graphics::base::{kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault};
44
use core_graphics::color_space::CGColorSpace;
55
use core_graphics::data_provider::CGDataProvider;
@@ -17,7 +17,7 @@ pub struct CGImpl {
1717
}
1818

1919
impl CGImpl {
20-
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
20+
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitWindowHandle) -> Result<Self, SoftBufferError<W>> {
2121
let view = handle.ns_view as id;
2222
let layer = CALayer::new();
2323
let subview: id = NSView::alloc(nil).initWithFrame_(view.frame());

src/error.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use std::error::Error;
2-
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
2+
use raw_window_handle::{HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
33
use thiserror::Error;
44

55
#[derive(Error, Debug)]
66
pub enum SoftBufferError<W: HasRawWindowHandle> {
77
#[error(
8-
"The provided window returned an unsupported platform: {human_readable_platform_name}."
8+
"The provided window returned an unsupported platform: {human_readable_window_platform_name}, {human_readable_display_platform_name}."
99
)]
1010
UnsupportedPlatform {
1111
window: W,
12-
human_readable_platform_name: &'static str,
13-
handle: RawWindowHandle,
12+
human_readable_window_platform_name: &'static str,
13+
human_readable_display_platform_name: &'static str,
14+
window_handle: RawWindowHandle,
15+
display_handle: RawDisplayHandle
1416
},
1517
#[error("Platform error")]
1618
PlatformError(Option<String>, Option<Box<dyn Error>>)

src/lib.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[cfg(target_os = "macos")]
44
#[macro_use]
55
extern crate objc;
6+
extern crate core;
67

78
#[cfg(target_os = "windows")]
89
mod win32;
@@ -19,40 +20,44 @@ mod error;
1920

2021
pub use error::SoftBufferError;
2122

22-
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
23+
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
2324

2425
/// An instance of this struct contains the platform-specific data that must be managed in order to
2526
/// write to a window on that platform. This struct owns the window that this data corresponds to
2627
/// to ensure safety, as that data must be destroyed before the window itself is destroyed. You may
2728
/// access the underlying window via [`window`](Self::window) and [`window_mut`](Self::window_mut).
28-
pub struct GraphicsContext<W: HasRawWindowHandle> {
29+
pub struct GraphicsContext<W: HasRawWindowHandle + HasRawDisplayHandle> {
2930
window: W,
3031
graphics_context_impl: Box<dyn GraphicsContextImpl>,
3132
}
3233

33-
impl<W: HasRawWindowHandle> GraphicsContext<W> {
34+
impl<W: HasRawWindowHandle + HasRawDisplayHandle> GraphicsContext<W> {
3435
/// Creates a new instance of this struct, consuming the given window.
3536
///
3637
/// # Safety
3738
///
3839
/// - Ensure that the passed object is valid to draw a 2D buffer to
3940
pub unsafe fn new(window: W) -> Result<Self, SoftBufferError<W>> {
40-
let raw_handle = window.raw_window_handle();
41-
let imple: Box<dyn GraphicsContextImpl> = match raw_handle {
41+
let raw_window_handle = window.raw_window_handle();
42+
let raw_display_handle = window.raw_display_handle();
43+
44+
let imple: Box<dyn GraphicsContextImpl> = match (raw_window_handle, raw_display_handle) {
4245
#[cfg(target_os = "linux")]
43-
RawWindowHandle::Xlib(xlib_handle) => Box::new(x11::X11Impl::new(xlib_handle)?),
46+
(RawWindowHandle::Xlib(xlib_window_handle), RawDisplayHandle::Xlib(xlib_display_handle)) => Box::new(x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?),
4447
#[cfg(target_os = "linux")]
45-
RawWindowHandle::Wayland(wayland_handle) => Box::new(wayland::WaylandImpl::new(wayland_handle)?),
48+
(RawWindowHandle::Wayland(wayland_window_handle), RawDisplayHandle::Wayland(wayland_display_handle)) => Box::new(wayland::WaylandImpl::new(wayland_window_handle, wayland_display_handle)?),
4649
#[cfg(target_os = "windows")]
47-
RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)?),
50+
(RawWindowHandle::Win32(win32_handle), _) => Box::new(win32::Win32Impl::new(&win32_handle)?),
4851
#[cfg(target_os = "macos")]
49-
RawWindowHandle::AppKit(appkit_handle) => Box::new(cg::CGImpl::new(appkit_handle)?),
52+
(RawWindowHandle::AppKit(appkit_handle), _) => Box::new(cg::CGImpl::new(appkit_handle)?),
5053
#[cfg(target_arch = "wasm32")]
51-
RawWindowHandle::Web(web_handle) => Box::new(web::WebImpl::new(web_handle)?),
52-
unimplemented_handle_type => return Err(SoftBufferError::UnsupportedPlatform {
54+
(RawWindowHandle::Web(web_handle), _) => Box::new(web::WebImpl::new(web_handle)?),
55+
(unimplemented_window_handle, unimplemented_display_handle) => return Err(SoftBufferError::UnsupportedPlatform {
5356
window,
54-
human_readable_platform_name: window_handle_type_name(&unimplemented_handle_type),
55-
handle: unimplemented_handle_type,
57+
human_readable_window_platform_name: window_handle_type_name(&unimplemented_window_handle),
58+
human_readable_display_platform_name: display_handle_type_name(&unimplemented_display_handle),
59+
window_handle: unimplemented_window_handle,
60+
display_handle: unimplemented_display_handle
5661
}),
5762
};
5863

@@ -122,7 +127,7 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
122127
}
123128
}
124129

125-
impl<W: HasRawWindowHandle> AsRef<W> for GraphicsContext<W> {
130+
impl<W: HasRawWindowHandle + HasRawDisplayHandle> AsRef<W> for GraphicsContext<W> {
126131
/// Equivalent to [`self.window()`](Self::window()).
127132
#[inline]
128133
fn as_ref(&self) -> &W {
@@ -145,6 +150,28 @@ fn window_handle_type_name(handle: &RawWindowHandle) -> &'static str {
145150
RawWindowHandle::AppKit(_) => "AppKit",
146151
RawWindowHandle::Orbital(_) => "Orbital",
147152
RawWindowHandle::UiKit(_) => "UiKit",
153+
RawWindowHandle::Xcb(_) => "XCB",
154+
RawWindowHandle::Drm(_) => "DRM",
155+
RawWindowHandle::Gbm(_) => "GBM",
156+
RawWindowHandle::Haiku(_) => "Haiku",
157+
_ => "Unknown Name", //don't completely fail to compile if there is a new raw window handle type that's added at some point
158+
}
159+
}
160+
161+
fn display_handle_type_name(handle: &RawDisplayHandle) -> &'static str {
162+
match handle {
163+
RawDisplayHandle::Xlib(_) => "Xlib",
164+
RawDisplayHandle::Web(_) => "Web",
165+
RawDisplayHandle::Wayland(_) => "Wayland",
166+
RawDisplayHandle::AppKit(_) => "AppKit",
167+
RawDisplayHandle::Orbital(_) => "Orbital",
168+
RawDisplayHandle::UiKit(_) => "UiKit",
169+
RawDisplayHandle::Xcb(_) => "XCB",
170+
RawDisplayHandle::Drm(_) => "DRM",
171+
RawDisplayHandle::Gbm(_) => "GBM",
172+
RawDisplayHandle::Haiku(_) => "Haiku",
173+
RawDisplayHandle::Windows(_) => "Windows",
174+
RawDisplayHandle::Android(_) => "Android",
148175
_ => "Unknown Name", //don't completely fail to compile if there is a new raw window handle type that's added at some point
149176
}
150177
}

src/wayland.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use raw_window_handle::{HasRawWindowHandle, WaylandHandle};
1+
use raw_window_handle::{HasRawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle};
22
use tempfile::tempfile;
33
use wayland_client::{Display, sys::client::wl_display, GlobalManager, protocol::{wl_shm::WlShm, wl_buffer::WlBuffer, wl_surface::WlSurface}, Main, Proxy, EventQueue};
44
use crate::{GraphicsContextImpl, SoftBufferError, error::unwrap};
@@ -20,15 +20,15 @@ struct WaylandBuffer{
2020

2121
impl WaylandImpl {
2222

23-
pub unsafe fn new<W: HasRawWindowHandle>(handle: WaylandHandle) -> Result<Self, SoftBufferError<W>> {
24-
let display = Display::from_external_display(handle.display as *mut wl_display);
23+
pub unsafe fn new<W: HasRawWindowHandle>(window_handle: WaylandWindowHandle, display_handle: WaylandDisplayHandle) -> Result<Self, SoftBufferError<W>> {
24+
let display = Display::from_external_display(display_handle.display as *mut wl_display);
2525
let mut event_queue = display.create_event_queue();
2626
let attached_display = (*display).clone().attach(event_queue.token());
2727
let globals = GlobalManager::new(&attached_display);
2828
unwrap(event_queue.sync_roundtrip(&mut (), |_, _, _| unreachable!()), "Failed to make round trip to server")?;
2929
let shm = unwrap(globals.instantiate_exact::<WlShm>(1), "Failed to instantiate Wayland Shm")?;
3030
let tempfile = unwrap(tempfile(), "Failed to create temporary file to store buffer.")?;
31-
let surface = Proxy::from_c_ptr(handle.surface as _).into();
31+
let surface = Proxy::from_c_ptr(window_handle.surface as _).into();
3232
Ok(Self{
3333
_event_queue: event_queue,
3434
surface, shm, tempfile,

src/web.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use raw_window_handle::HasRawWindowHandle;
2-
use raw_window_handle::WebHandle;
2+
use raw_window_handle::WebWindowHandle;
33
use wasm_bindgen::Clamped;
44
use wasm_bindgen::JsCast;
55
use web_sys::CanvasRenderingContext2d;
@@ -15,7 +15,7 @@ pub struct WebImpl {
1515
}
1616

1717
impl WebImpl {
18-
pub fn new<W: HasRawWindowHandle>(handle: WebHandle) -> Result<Self, SoftBufferError<W>> {
18+
pub fn new<W: HasRawWindowHandle>(handle: WebWindowHandle) -> Result<Self, SoftBufferError<W>> {
1919
let canvas: HtmlCanvasElement = web_sys::window()
2020
.ok_or_else(|| {
2121
SoftBufferError::PlatformError(

src/win32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{GraphicsContextImpl, SoftBufferError};
2-
use raw_window_handle::{HasRawWindowHandle, Win32Handle};
2+
use raw_window_handle::{HasRawWindowHandle, Win32WindowHandle};
33
use std::os::raw::c_int;
44
use winapi::shared::windef::{HDC, HWND};
55
use winapi::um::wingdi::{StretchDIBits, BITMAPINFOHEADER, BI_BITFIELDS, RGBQUAD};
@@ -19,7 +19,7 @@ struct BitmapInfo {
1919
}
2020

2121
impl Win32Impl {
22-
pub unsafe fn new<W: HasRawWindowHandle>(handle: &Win32Handle) -> Result<Self, crate::SoftBufferError<W>> {
22+
pub unsafe fn new<W: HasRawWindowHandle>(handle: &Win32WindowHandle) -> Result<Self, crate::SoftBufferError<W>> {
2323
let dc = GetDC(handle.hwnd as HWND);
2424

2525
if dc.is_null(){

src/x11.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
use crate::{GraphicsContextImpl, SoftBufferError};
2-
use raw_window_handle::{HasRawWindowHandle, XlibHandle};
2+
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, XlibDisplayHandle, XlibWindowHandle};
33
use std::os::raw::{c_char, c_uint};
44
use x11_dl::xlib::{Display, Visual, Xlib, ZPixmap, GC};
55

66
pub struct X11Impl {
7-
handle: XlibHandle,
7+
window_handle: XlibWindowHandle,
8+
display_handle: XlibDisplayHandle,
89
lib: Xlib,
910
gc: GC,
1011
visual: *mut Visual,
1112
depth: i32,
1213
}
1314

1415
impl X11Impl {
15-
pub unsafe fn new<W: HasRawWindowHandle>(handle: XlibHandle) -> Result<Self, SoftBufferError<W>> {
16+
pub unsafe fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(window_handle: XlibWindowHandle, display_handle: XlibDisplayHandle) -> Result<Self, SoftBufferError<W>> {
1617
let lib = match Xlib::open() {
1718
Ok(lib) => lib,
1819
Err(e) => return Err(SoftBufferError::PlatformError(Some("Failed to open Xlib".into()), Some(Box::new(e))))
1920
};
20-
let screen = (lib.XDefaultScreen)(handle.display as *mut Display);
21-
let gc = (lib.XDefaultGC)(handle.display as *mut Display, screen);
22-
let visual = (lib.XDefaultVisual)(handle.display as *mut Display, screen);
23-
let depth = (lib.XDefaultDepth)(handle.display as *mut Display, screen);
21+
let screen = (lib.XDefaultScreen)(display_handle.display as *mut Display);
22+
let gc = (lib.XDefaultGC)(display_handle.display as *mut Display, screen);
23+
let visual = (lib.XDefaultVisual)(display_handle.display as *mut Display, screen);
24+
let depth = (lib.XDefaultDepth)(display_handle.display as *mut Display, screen);
2425

2526
Ok(
2627
Self {
27-
handle,
28+
window_handle,
29+
display_handle,
2830
lib,
2931
gc,
3032
visual,
@@ -38,7 +40,7 @@ impl GraphicsContextImpl for X11Impl {
3840
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
3941
//create image
4042
let image = (self.lib.XCreateImage)(
41-
self.handle.display as *mut Display,
43+
self.display_handle.display as *mut Display,
4244
self.visual,
4345
self.depth as u32,
4446
ZPixmap,
@@ -52,8 +54,8 @@ impl GraphicsContextImpl for X11Impl {
5254

5355
//push image to window
5456
(self.lib.XPutImage)(
55-
self.handle.display as *mut Display,
56-
self.handle.window,
57+
self.display_handle.display as *mut Display,
58+
self.window_handle.window,
5759
self.gc,
5860
image,
5961
0,

0 commit comments

Comments
 (0)