1
0
Fork 0

Merge pull request #134 from greatest-ape/raw-window-handle-0.5

Upgrade to raw-window-handle version 0.5
This commit is contained in:
Robbert van der Helm 2023-10-07 15:12:01 +02:00 committed by GitHub
commit 54af726498
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 40 deletions

View file

@ -20,7 +20,7 @@ opengl = ["uuid", "x11/glx"]
[dependencies] [dependencies]
keyboard-types = { version = "0.6.1", default-features = false } keyboard-types = { version = "0.6.1", default-features = false }
raw-window-handle = "0.4.2" raw-window-handle = "0.5"
[target.'cfg(target_os="linux")'.dependencies] [target.'cfg(target_os="linux")'.dependencies]
xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] } xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] }

View file

@ -1,8 +1,6 @@
use std::ffi::{c_void, CString}; use std::ffi::{c_void, CString};
use std::os::raw::{c_int, c_ulong}; use std::os::raw::{c_int, c_ulong};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use x11::glx; use x11::glx;
use x11::xlib; use x11::xlib;
@ -80,20 +78,12 @@ impl GlContext {
/// ///
/// Use [Self::get_fb_config_and_visual] to create both of these things. /// Use [Self::get_fb_config_and_visual] to create both of these things.
pub unsafe fn create( pub unsafe fn create(
parent: &impl HasRawWindowHandle, config: FbConfig, window: c_ulong, display: *mut xlib::_XDisplay, config: FbConfig,
) -> Result<GlContext, GlError> { ) -> Result<GlContext, GlError> {
let handle = if let RawWindowHandle::Xlib(handle) = parent.raw_window_handle() { if display.is_null() {
handle
} else {
return Err(GlError::InvalidWindowHandle);
};
if handle.display.is_null() {
return Err(GlError::InvalidWindowHandle); return Err(GlError::InvalidWindowHandle);
} }
let display = handle.display as *mut xlib::_XDisplay;
errors::XErrorHandler::handle(display, |error_handler| { errors::XErrorHandler::handle(display, |error_handler| {
#[allow(non_snake_case)] #[allow(non_snake_case)]
let glXCreateContextAttribsARB: GlXCreateContextAttribsARB = { let glXCreateContextAttribsARB: GlXCreateContextAttribsARB = {
@ -144,13 +134,13 @@ impl GlContext {
return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed)); return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed));
} }
let res = glx::glXMakeCurrent(display, handle.window, context); let res = glx::glXMakeCurrent(display, window, context);
error_handler.check()?; error_handler.check()?;
if res == 0 { if res == 0 {
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
} }
glXSwapIntervalEXT(display, handle.window, config.gl_config.vsync as i32); glXSwapIntervalEXT(display, window, config.gl_config.vsync as i32);
error_handler.check()?; error_handler.check()?;
if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 { if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 {
@ -158,7 +148,7 @@ impl GlContext {
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
} }
Ok(GlContext { window: handle.window, display, context }) Ok(GlContext { window, display, context })
}) })
} }

View file

@ -17,7 +17,10 @@ use keyboard_types::KeyboardEvent;
use objc::{msg_send, runtime::Object, sel, sel_impl}; use objc::{msg_send, runtime::Object, sel, sel_impl};
use raw_window_handle::{AppKitHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{
AppKitDisplayHandle, AppKitWindowHandle, HasRawDisplayHandle, HasRawWindowHandle,
RawDisplayHandle, RawWindowHandle,
};
use crate::{ use crate::{
Event, EventStatus, Size, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions, Event, EventStatus, Size, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions,
@ -62,7 +65,7 @@ unsafe impl HasRawWindowHandle for WindowHandle {
} }
} }
RawWindowHandle::AppKit(AppKitHandle::empty()) RawWindowHandle::AppKit(AppKitWindowHandle::empty())
} }
} }
@ -345,7 +348,7 @@ impl Window {
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
fn create_gl_context(ns_window: Option<id>, ns_view: id, config: GlConfig) -> GlContext { fn create_gl_context(ns_window: Option<id>, ns_view: id, config: GlConfig) -> GlContext {
let mut handle = AppKitHandle::empty(); let mut handle = AppKitWindowHandle::empty();
handle.ns_window = ns_window.unwrap_or(ptr::null_mut()) as *mut c_void; handle.ns_window = ns_window.unwrap_or(ptr::null_mut()) as *mut c_void;
handle.ns_view = ns_view as *mut c_void; handle.ns_view = ns_view as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::AppKit(handle) }; let handle = RawWindowHandleWrapper { handle: RawWindowHandle::AppKit(handle) };
@ -476,7 +479,7 @@ unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle { fn raw_window_handle(&self) -> RawWindowHandle {
let ns_window = self.ns_window.unwrap_or(ptr::null_mut()) as *mut c_void; let ns_window = self.ns_window.unwrap_or(ptr::null_mut()) as *mut c_void;
let mut handle = AppKitHandle::empty(); let mut handle = AppKitWindowHandle::empty();
handle.ns_window = ns_window; handle.ns_window = ns_window;
handle.ns_view = self.ns_view as *mut c_void; handle.ns_view = self.ns_view as *mut c_void;
@ -484,6 +487,12 @@ unsafe impl HasRawWindowHandle for Window {
} }
} }
unsafe impl HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::AppKit(AppKitDisplayHandle::empty())
}
}
pub fn copy_to_clipboard(string: &str) { pub fn copy_to_clipboard(string: &str) {
unsafe { unsafe {
let pb = NSPasteboard::generalPasteboard(nil); let pb = NSPasteboard::generalPasteboard(nil);

View file

@ -26,7 +26,10 @@ use std::os::windows::ffi::OsStrExt;
use std::ptr::null_mut; use std::ptr::null_mut;
use std::rc::Rc; use std::rc::Rc;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, Win32Handle}; use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, Win32WindowHandle,
WindowsDisplayHandle,
};
const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1; const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1;
@ -87,12 +90,12 @@ impl WindowHandle {
unsafe impl HasRawWindowHandle for WindowHandle { unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle { fn raw_window_handle(&self) -> RawWindowHandle {
if let Some(hwnd) = self.hwnd { if let Some(hwnd) = self.hwnd {
let mut handle = Win32Handle::empty(); let mut handle = Win32WindowHandle::empty();
handle.hwnd = hwnd as *mut c_void; handle.hwnd = hwnd as *mut c_void;
RawWindowHandle::Win32(handle) RawWindowHandle::Win32(handle)
} else { } else {
RawWindowHandle::Win32(Win32Handle::empty()) RawWindowHandle::Win32(Win32WindowHandle::empty())
} }
} }
} }
@ -661,7 +664,7 @@ impl Window<'_> {
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
let gl_context: Option<GlContext> = options.gl_config.map(|gl_config| { let gl_context: Option<GlContext> = options.gl_config.map(|gl_config| {
let mut handle = Win32Handle::empty(); let mut handle = Win32WindowHandle::empty();
handle.hwnd = hwnd as *mut c_void; handle.hwnd = hwnd as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Win32(handle) }; let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Win32(handle) };
@ -781,13 +784,19 @@ impl Window<'_> {
unsafe impl HasRawWindowHandle for Window<'_> { unsafe impl HasRawWindowHandle for Window<'_> {
fn raw_window_handle(&self) -> RawWindowHandle { fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = Win32Handle::empty(); let mut handle = Win32WindowHandle::empty();
handle.hwnd = self.state.hwnd as *mut c_void; handle.hwnd = self.state.hwnd as *mut c_void;
RawWindowHandle::Win32(handle) RawWindowHandle::Win32(handle)
} }
} }
unsafe impl HasRawDisplayHandle for Window<'_> {
fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::Windows(WindowsDisplayHandle::empty())
}
}
pub fn copy_to_clipboard(data: &str) { pub fn copy_to_clipboard(data: &str) {
todo!() todo!()
} }

View file

@ -1,6 +1,8 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use crate::event::{Event, EventStatus}; use crate::event::{Event, EventStatus};
use crate::window_open_options::WindowOpenOptions; use crate::window_open_options::WindowOpenOptions;
@ -129,6 +131,12 @@ unsafe impl<'a> HasRawWindowHandle for Window<'a> {
} }
} }
unsafe impl<'a> HasRawDisplayHandle for Window<'a> {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.window.raw_display_handle()
}
}
unsafe impl HasRawWindowHandle for RawWindowHandleWrapper { unsafe impl HasRawWindowHandle for RawWindowHandleWrapper {
fn raw_window_handle(&self) -> RawWindowHandle { fn raw_window_handle(&self) -> RawWindowHandle {
self.handle self.handle

View file

@ -6,7 +6,10 @@ use std::sync::Arc;
use std::thread; use std::thread;
use std::time::*; use std::time::*;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, XlibHandle}; use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, XlibDisplayHandle,
XlibWindowHandle,
};
use xcb::ffi::xcb_screen_t; use xcb::ffi::xcb_screen_t;
use xcb::StructPtr; use xcb::StructPtr;
@ -19,10 +22,7 @@ use crate::{
use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods}; use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods};
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
use crate::{ use crate::gl::{platform, GlContext};
gl::{platform, GlContext},
window::RawWindowHandleWrapper,
};
pub struct WindowHandle { pub struct WindowHandle {
raw_window_handle: Option<RawWindowHandle>, raw_window_handle: Option<RawWindowHandle>,
@ -57,7 +57,7 @@ unsafe impl HasRawWindowHandle for WindowHandle {
} }
} }
RawWindowHandle::Xlib(XlibHandle::empty()) RawWindowHandle::Xlib(XlibWindowHandle::empty())
} }
} }
@ -96,6 +96,7 @@ pub struct Window {
xcb_connection: XcbConnection, xcb_connection: XcbConnection,
window_id: u32, window_id: u32,
window_info: WindowInfo, window_info: WindowInfo,
visual_id: u32,
// FIXME: There's all this mouse cursor logic but it's never actually used, is this correct? // FIXME: There's all this mouse cursor logic but it's never actually used, is this correct?
mouse_cursor: MouseCursor, mouse_cursor: MouseCursor,
@ -323,13 +324,11 @@ impl Window {
// compared to when raw-gl-context was a separate crate. // compared to when raw-gl-context was a separate crate.
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
let gl_context = fb_config.map(|fb_config| { let gl_context = fb_config.map(|fb_config| {
let mut handle = XlibHandle::empty(); let window = window_id as c_ulong;
handle.window = window_id as c_ulong; let display = xcb_connection.conn.get_raw_dpy();
handle.display = xcb_connection.conn.get_raw_dpy() as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Xlib(handle) };
// Because of the visual negotation we had to take some extra steps to create this context // Because of the visual negotation we had to take some extra steps to create this context
let context = unsafe { platform::GlContext::create(&handle, fb_config) } let context = unsafe { platform::GlContext::create(window, display, fb_config) }
.expect("Could not create OpenGL context"); .expect("Could not create OpenGL context");
GlContext::new(context) GlContext::new(context)
}); });
@ -338,6 +337,7 @@ impl Window {
xcb_connection, xcb_connection,
window_id, window_id,
window_info, window_info,
visual_id: visual,
mouse_cursor: MouseCursor::default(), mouse_cursor: MouseCursor::default(),
frame_interval: Duration::from_millis(15), frame_interval: Duration::from_millis(15),
@ -686,14 +686,27 @@ impl Window {
unsafe impl HasRawWindowHandle for Window { unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle { fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = XlibHandle::empty(); let mut handle = XlibWindowHandle::empty();
handle.window = self.window_id as c_ulong;
handle.display = self.xcb_connection.conn.get_raw_dpy() as *mut c_void; handle.window = self.window_id.into();
handle.visual_id = self.visual_id.into();
RawWindowHandle::Xlib(handle) RawWindowHandle::Xlib(handle)
} }
} }
unsafe impl HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> RawDisplayHandle {
let display = self.xcb_connection.conn.get_raw_dpy();
let mut handle = XlibDisplayHandle::empty();
handle.display = display as *mut c_void;
handle.screen = unsafe { x11::xlib::XDefaultScreen(display) };
RawDisplayHandle::Xlib(handle)
}
}
fn mouse_id(id: u8) -> MouseButton { fn mouse_id(id: u8) -> MouseButton {
match id { match id {
1 => MouseButton::Left, 1 => MouseButton::Left,