Skip to content

Commit

Permalink
Merge pull request #134 from greatest-ape/raw-window-handle-0.5
Browse files Browse the repository at this point in the history
Upgrade to raw-window-handle version 0.5
  • Loading branch information
robbert-vdh authored Oct 7, 2023
2 parents 1d9806d + 0df4348 commit 54af726
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ opengl = ["uuid", "x11/glx"]

[dependencies]
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]
xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] }
Expand Down
20 changes: 5 additions & 15 deletions src/gl/x11.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::ffi::{c_void, CString};
use std::os::raw::{c_int, c_ulong};

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};

use x11::glx;
use x11::xlib;

Expand Down Expand Up @@ -80,20 +78,12 @@ impl GlContext {
///
/// Use [Self::get_fb_config_and_visual] to create both of these things.
pub unsafe fn create(
parent: &impl HasRawWindowHandle, config: FbConfig,
window: c_ulong, display: *mut xlib::_XDisplay, config: FbConfig,
) -> Result<GlContext, GlError> {
let handle = if let RawWindowHandle::Xlib(handle) = parent.raw_window_handle() {
handle
} else {
return Err(GlError::InvalidWindowHandle);
};

if handle.display.is_null() {
if display.is_null() {
return Err(GlError::InvalidWindowHandle);
}

let display = handle.display as *mut xlib::_XDisplay;

errors::XErrorHandler::handle(display, |error_handler| {
#[allow(non_snake_case)]
let glXCreateContextAttribsARB: GlXCreateContextAttribsARB = {
Expand Down Expand Up @@ -144,21 +134,21 @@ impl GlContext {
return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed));
}

let res = glx::glXMakeCurrent(display, handle.window, context);
let res = glx::glXMakeCurrent(display, window, context);
error_handler.check()?;
if res == 0 {
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()?;

if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 {
error_handler.check()?;
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
}

Ok(GlContext { window: handle.window, display, context })
Ok(GlContext { window, display, context })
})
}

Expand Down
17 changes: 13 additions & 4 deletions src/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use keyboard_types::KeyboardEvent;

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::{
Event, EventStatus, Size, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions,
Expand Down Expand Up @@ -62,7 +65,7 @@ unsafe impl HasRawWindowHandle for WindowHandle {
}
}

RawWindowHandle::AppKit(AppKitHandle::empty())
RawWindowHandle::AppKit(AppKitWindowHandle::empty())
}
}

Expand Down Expand Up @@ -345,7 +348,7 @@ impl Window {

#[cfg(feature = "opengl")]
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_view = ns_view as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::AppKit(handle) };
Expand Down Expand Up @@ -476,14 +479,20 @@ unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle {
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_view = self.ns_view as *mut c_void;

RawWindowHandle::AppKit(handle)
}
}

unsafe impl HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::AppKit(AppKitDisplayHandle::empty())
}
}

pub fn copy_to_clipboard(string: &str) {
unsafe {
let pb = NSPasteboard::generalPasteboard(nil);
Expand Down
19 changes: 14 additions & 5 deletions src/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use std::os::windows::ffi::OsStrExt;
use std::ptr::null_mut;
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;

Expand Down Expand Up @@ -87,12 +90,12 @@ impl WindowHandle {
unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle {
if let Some(hwnd) = self.hwnd {
let mut handle = Win32Handle::empty();
let mut handle = Win32WindowHandle::empty();
handle.hwnd = hwnd as *mut c_void;

RawWindowHandle::Win32(handle)
} else {
RawWindowHandle::Win32(Win32Handle::empty())
RawWindowHandle::Win32(Win32WindowHandle::empty())
}
}
}
Expand Down Expand Up @@ -661,7 +664,7 @@ impl Window<'_> {

#[cfg(feature = "opengl")]
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;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Win32(handle) };

Expand Down Expand Up @@ -781,13 +784,19 @@ impl Window<'_> {

unsafe impl HasRawWindowHandle for Window<'_> {
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;

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) {
todo!()
}
10 changes: 9 additions & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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::window_open_options::WindowOpenOptions;
Expand Down Expand Up @@ -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 {
fn raw_window_handle(&self) -> RawWindowHandle {
self.handle
Expand Down
41 changes: 27 additions & 14 deletions src/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::sync::Arc;
use std::thread;
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::StructPtr;

Expand All @@ -19,10 +22,7 @@ use crate::{
use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods};

#[cfg(feature = "opengl")]
use crate::{
gl::{platform, GlContext},
window::RawWindowHandleWrapper,
};
use crate::gl::{platform, GlContext};

pub struct WindowHandle {
raw_window_handle: Option<RawWindowHandle>,
Expand Down Expand Up @@ -57,7 +57,7 @@ unsafe impl HasRawWindowHandle for WindowHandle {
}
}

RawWindowHandle::Xlib(XlibHandle::empty())
RawWindowHandle::Xlib(XlibWindowHandle::empty())
}
}

Expand Down Expand Up @@ -96,6 +96,7 @@ pub struct Window {
xcb_connection: XcbConnection,
window_id: u32,
window_info: WindowInfo,
visual_id: u32,
// FIXME: There's all this mouse cursor logic but it's never actually used, is this correct?
mouse_cursor: MouseCursor,

Expand Down Expand Up @@ -323,13 +324,11 @@ impl Window {
// compared to when raw-gl-context was a separate crate.
#[cfg(feature = "opengl")]
let gl_context = fb_config.map(|fb_config| {
let mut handle = XlibHandle::empty();
handle.window = window_id as c_ulong;
handle.display = xcb_connection.conn.get_raw_dpy() as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Xlib(handle) };
let window = window_id as c_ulong;
let display = xcb_connection.conn.get_raw_dpy();

// 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");
GlContext::new(context)
});
Expand All @@ -338,6 +337,7 @@ impl Window {
xcb_connection,
window_id,
window_info,
visual_id: visual,
mouse_cursor: MouseCursor::default(),

frame_interval: Duration::from_millis(15),
Expand Down Expand Up @@ -686,14 +686,27 @@ impl Window {

unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = XlibHandle::empty();
handle.window = self.window_id as c_ulong;
handle.display = self.xcb_connection.conn.get_raw_dpy() as *mut c_void;
let mut handle = XlibWindowHandle::empty();

handle.window = self.window_id.into();
handle.visual_id = self.visual_id.into();

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 {
match id {
1 => MouseButton::Left,
Expand Down

0 comments on commit 54af726

Please sign in to comment.