3
3
#[ cfg( target_os = "macos" ) ]
4
4
#[ macro_use]
5
5
extern crate objc;
6
+ extern crate core;
6
7
7
8
#[ cfg( target_os = "windows" ) ]
8
9
mod win32;
@@ -19,40 +20,44 @@ mod error;
19
20
20
21
pub use error:: SoftBufferError ;
21
22
22
- use raw_window_handle:: { HasRawWindowHandle , RawWindowHandle } ;
23
+ use raw_window_handle:: { HasRawDisplayHandle , HasRawWindowHandle , RawDisplayHandle , RawWindowHandle } ;
23
24
24
25
/// An instance of this struct contains the platform-specific data that must be managed in order to
25
26
/// write to a window on that platform. This struct owns the window that this data corresponds to
26
27
/// to ensure safety, as that data must be destroyed before the window itself is destroyed. You may
27
28
/// 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 > {
29
30
window : W ,
30
31
graphics_context_impl : Box < dyn GraphicsContextImpl > ,
31
32
}
32
33
33
- impl < W : HasRawWindowHandle > GraphicsContext < W > {
34
+ impl < W : HasRawWindowHandle + HasRawDisplayHandle > GraphicsContext < W > {
34
35
/// Creates a new instance of this struct, consuming the given window.
35
36
///
36
37
/// # Safety
37
38
///
38
39
/// - Ensure that the passed object is valid to draw a 2D buffer to
39
40
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) {
42
45
#[ 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 ) ?) ,
44
47
#[ 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 ) ?) ,
46
49
#[ 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) ?) ,
48
51
#[ 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) ?) ,
50
53
#[ 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 {
53
56
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
56
61
} ) ,
57
62
} ;
58
63
@@ -122,7 +127,7 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
122
127
}
123
128
}
124
129
125
- impl < W : HasRawWindowHandle > AsRef < W > for GraphicsContext < W > {
130
+ impl < W : HasRawWindowHandle + HasRawDisplayHandle > AsRef < W > for GraphicsContext < W > {
126
131
/// Equivalent to [`self.window()`](Self::window()).
127
132
#[ inline]
128
133
fn as_ref ( & self ) -> & W {
@@ -145,6 +150,28 @@ fn window_handle_type_name(handle: &RawWindowHandle) -> &'static str {
145
150
RawWindowHandle :: AppKit ( _) => "AppKit" ,
146
151
RawWindowHandle :: Orbital ( _) => "Orbital" ,
147
152
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" ,
148
175
_ => "Unknown Name" , //don't completely fail to compile if there is a new raw window handle type that's added at some point
149
176
}
150
177
}
0 commit comments