From 1c0490f9ec6b7ab42e8133ade45f6d55d9ad3a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 23 Nov 2022 22:30:33 +0100 Subject: [PATCH 01/11] Upgrade to raw-window-handle 0.5, impl HasRawDisplayHandle for macOS --- Cargo.toml | 2 +- src/macos/window.rs | 15 ++++++++++++--- src/window.rs | 10 +++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb23f4e5..a0387dc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/macos/window.rs b/src/macos/window.rs index 54046ddd..136d9ab2 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -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, @@ -62,7 +65,13 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } - RawWindowHandle::AppKit(AppKitHandle::empty()) + RawWindowHandle::AppKit(AppKitWindowHandle::empty()) + } +} + +unsafe impl HasRawDisplayHandle for WindowHandle { + fn raw_display_handle(&self) -> RawDisplayHandle { + RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) } } @@ -476,7 +485,7 @@ 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; diff --git a/src/window.rs b/src/window.rs index c0ef1ac8..d83f6cf2 100644 --- a/src/window.rs +++ b/src/window.rs @@ -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; @@ -48,6 +50,12 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } +unsafe impl HasRawDisplayHandle for WindowHandle { + fn raw_display_handle(&self) -> RawDisplayHandle { + self.window_handle.raw_display_handle() + } +} + pub trait WindowHandler { fn on_frame(&mut self, window: &mut Window); fn on_event(&mut self, window: &mut Window, event: Event) -> EventStatus; From 5c23c62030ab7adfc2a17b88d876e74dd2d0127e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 23 Nov 2022 22:40:25 +0100 Subject: [PATCH 02/11] Add dummy HasRawDisplayHandle impls for Windows and X11 --- src/win/window.rs | 27 ++++++++++++++++++--------- src/x11/window.rs | 17 +++++++++++++---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index 2af1abed..f8c61560 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -24,7 +24,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, + Win32WindowHandle, WindowsDisplayHandle, +}; const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1; @@ -84,16 +87,22 @@ 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) + RawWindowHandle::Windows(handle) } else { - RawWindowHandle::Win32(Win32Handle::empty()) + RawWindowHandle::Windows(Win32WindowHandle::empty()) } } } +unsafe impl HasRawDisplayHandle for WindowHandle { + fn raw_display_handle(&self) -> RawDisplayHandle { + RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) + } +} + struct ParentHandle { is_open: Rc>, } @@ -536,7 +545,7 @@ impl Window<'_> { B: Send + 'static, { let parent = match parent.raw_window_handle() { - RawWindowHandle::Win32(h) => h.hwnd as HWND, + RawWindowHandle::Windows(h) => h.hwnd as HWND, h => panic!("unsupported parent handle {:?}", h), }; @@ -644,9 +653,9 @@ impl Window<'_> { #[cfg(feature = "opengl")] let gl_context: Option = 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) }; + let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Windows(handle) }; GlContext::create(&handle, gl_config).expect("Could not create OpenGL context") }); @@ -757,10 +766,10 @@ 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) + RawWindowHandle::Windows(handle) } } diff --git a/src/x11/window.rs b/src/x11/window.rs index fcd90837..b6ba8b3b 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -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; @@ -57,7 +60,13 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } - RawWindowHandle::Xlib(XlibHandle::empty()) + RawWindowHandle::Xlib(XlibWindowHandle::empty()) + } +} + +unsafe impl HasRawDisplayHandle for WindowHandle { + fn raw_display_handle(&self) -> RawDisplayHandle { + RawDisplayHandle::Xlib(XlibDisplayHandle::empty()) } } @@ -323,7 +332,7 @@ 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(); + let mut handle = XlibWindowHandle::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) }; @@ -686,7 +695,7 @@ impl Window { unsafe impl HasRawWindowHandle for Window { 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; From eb94b4d7ab10e721512b0ec8c65ad383d73e5db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 23 Nov 2022 23:18:10 +0100 Subject: [PATCH 03/11] Impl HasRawDisplayHandle on Window, not WindowHandle --- src/macos/window.rs | 14 +++++++------- src/win/window.rs | 12 ++++++------ src/window.rs | 12 ++++++------ src/x11/window.rs | 12 ++++++------ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/macos/window.rs b/src/macos/window.rs index 136d9ab2..2587fa99 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -69,12 +69,6 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } -unsafe impl HasRawDisplayHandle for WindowHandle { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) - } -} - struct ParentHandle { _close_requested: Arc, is_open: Arc, @@ -354,7 +348,7 @@ impl Window { #[cfg(feature = "opengl")] fn create_gl_context(ns_window: Option, 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) }; @@ -493,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) { unsafe { let pb = NSPasteboard::generalPasteboard(nil); diff --git a/src/win/window.rs b/src/win/window.rs index f8c61560..02d2481f 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -97,12 +97,6 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } -unsafe impl HasRawDisplayHandle for WindowHandle { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) - } -} - struct ParentHandle { is_open: Rc>, } @@ -773,6 +767,12 @@ unsafe impl HasRawWindowHandle for Window<'_> { } } +unsafe impl HasRawDisplayHandle for Window<'_> { + fn raw_display_handle(&self) -> RawDisplayHandle { + RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) + } +} + pub fn copy_to_clipboard(data: &str) { todo!() } diff --git a/src/window.rs b/src/window.rs index d83f6cf2..f662990d 100644 --- a/src/window.rs +++ b/src/window.rs @@ -50,12 +50,6 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } -unsafe impl HasRawDisplayHandle for WindowHandle { - fn raw_display_handle(&self) -> RawDisplayHandle { - self.window_handle.raw_display_handle() - } -} - pub trait WindowHandler { fn on_frame(&mut self, window: &mut Window); fn on_event(&mut self, window: &mut Window, event: Event) -> EventStatus; @@ -137,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 diff --git a/src/x11/window.rs b/src/x11/window.rs index b6ba8b3b..b8f89bbc 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -64,12 +64,6 @@ unsafe impl HasRawWindowHandle for WindowHandle { } } -unsafe impl HasRawDisplayHandle for WindowHandle { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Xlib(XlibDisplayHandle::empty()) - } -} - struct ParentHandle { close_requested: Arc, is_open: Arc, @@ -703,6 +697,12 @@ unsafe impl HasRawWindowHandle for Window { } } +unsafe impl HasRawDisplayHandle for Window { + fn raw_display_handle(&self) -> RawDisplayHandle { + RawDisplayHandle::Xlib(XlibDisplayHandle::empty()) + } +} + fn mouse_id(id: u8) -> MouseButton { match id { 1 => MouseButton::Left, From 6f325841494d59cd40e521e71659de5c650dfc49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 23 Nov 2022 23:34:42 +0100 Subject: [PATCH 04/11] X11: set XlibWindowHandle.window and XlibDisplayhandle.display --- src/x11/window.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/x11/window.rs b/src/x11/window.rs index b8f89bbc..74ed3c95 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -690,8 +690,9 @@ impl Window { unsafe impl HasRawWindowHandle for Window { fn raw_window_handle(&self) -> RawWindowHandle { 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; + // FIXME: handle.visual_id? RawWindowHandle::Xlib(handle) } @@ -699,7 +700,12 @@ unsafe impl HasRawWindowHandle for Window { unsafe impl HasRawDisplayHandle for Window { fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Xlib(XlibDisplayHandle::empty()) + let mut handle = XlibDisplayHandle::empty(); + + handle.display = self.xcb_connection.conn.get_raw_dpy() as *mut c_void; + // FIXME: handle.screen? + + RawDisplayHandle::Xlib(handle) } } From 10c970a3298b564e48e1a758987a0aa4985b2119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 23 Nov 2022 23:52:19 +0100 Subject: [PATCH 05/11] Fix X11 OpenGL issue --- src/gl/x11.rs | 20 ++++++-------------- src/x11/window.rs | 13 ++++--------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/gl/x11.rs b/src/gl/x11.rs index f62d318a..df7ce0cb 100644 --- a/src/gl/x11.rs +++ b/src/gl/x11.rs @@ -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; @@ -80,19 +78,13 @@ 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 c_void, config: FbConfig, ) -> Result { - 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; + let display = display as *mut xlib::_XDisplay; errors::XErrorHandler::handle(display, |error_handler| { #[allow(non_snake_case)] @@ -144,13 +136,13 @@ 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 { @@ -158,7 +150,7 @@ impl GlContext { return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); } - Ok(GlContext { window: handle.window, display, context }) + Ok(GlContext { window, display, context }) }) } diff --git a/src/x11/window.rs b/src/x11/window.rs index 74ed3c95..ce33b711 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -22,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, @@ -326,13 +323,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 = XlibWindowHandle::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() as *mut c_void; // 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) }); From e35f3080f62cd7669b3a989e43584177e1ddcd21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Thu, 24 Nov 2022 00:07:55 +0100 Subject: [PATCH 06/11] Fix Windows compilation issue --- src/win/window.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index 02d2481f..3af522b3 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -26,7 +26,7 @@ use std::rc::Rc; use raw_window_handle::{ HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, Win32WindowHandle, - Win32WindowHandle, WindowsDisplayHandle, + WindowsDisplayHandle, }; const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1; @@ -90,9 +90,9 @@ unsafe impl HasRawWindowHandle for WindowHandle { let mut handle = Win32WindowHandle::empty(); handle.hwnd = hwnd as *mut c_void; - RawWindowHandle::Windows(handle) + RawWindowHandle::Win32(handle) } else { - RawWindowHandle::Windows(Win32WindowHandle::empty()) + RawWindowHandle::Win32(Win32WindowHandle::empty()) } } } @@ -539,7 +539,7 @@ impl Window<'_> { B: Send + 'static, { let parent = match parent.raw_window_handle() { - RawWindowHandle::Windows(h) => h.hwnd as HWND, + RawWindowHandle::Win32(h) => h.hwnd as HWND, h => panic!("unsupported parent handle {:?}", h), }; @@ -649,7 +649,7 @@ impl Window<'_> { let gl_context: Option = options.gl_config.map(|gl_config| { let mut handle = Win32WindowHandle::empty(); handle.hwnd = hwnd as *mut c_void; - let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Windows(handle) }; + let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Win32(handle) }; GlContext::create(&handle, gl_config).expect("Could not create OpenGL context") }); @@ -763,7 +763,7 @@ unsafe impl HasRawWindowHandle for Window<'_> { let mut handle = Win32WindowHandle::empty(); handle.hwnd = self.state.hwnd as *mut c_void; - RawWindowHandle::Windows(handle) + RawWindowHandle::Win32(handle) } } From 70f46a691d3df55d2398ab6a453fe2b0a0aa717d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Thu, 24 Nov 2022 19:02:08 +0100 Subject: [PATCH 07/11] X11: set XlibDisplayHandle.screen --- src/x11/window.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/x11/window.rs b/src/x11/window.rs index ce33b711..e644442f 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -685,9 +685,7 @@ impl Window { unsafe impl HasRawWindowHandle for Window { fn raw_window_handle(&self) -> RawWindowHandle { let mut handle = XlibWindowHandle::empty(); - handle.window = self.window_id as c_ulong; - // FIXME: handle.visual_id? RawWindowHandle::Xlib(handle) } @@ -695,10 +693,11 @@ unsafe impl HasRawWindowHandle for Window { 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 = self.xcb_connection.conn.get_raw_dpy() as *mut c_void; - // FIXME: handle.screen? + handle.display = display as *mut c_void; + handle.screen = unsafe { x11::xlib::XDefaultScreen(display) }; RawDisplayHandle::Xlib(handle) } From 4130502a2a3f668e06b69e1b948c3e8fd17b67d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 26 Nov 2022 14:38:17 +0100 Subject: [PATCH 08/11] X11 GlContext::create: change display arg to *mut xlib::XDisplay --- src/gl/x11.rs | 4 +--- src/x11/window.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/gl/x11.rs b/src/gl/x11.rs index df7ce0cb..5e90f8de 100644 --- a/src/gl/x11.rs +++ b/src/gl/x11.rs @@ -78,14 +78,12 @@ impl GlContext { /// /// Use [Self::get_fb_config_and_visual] to create both of these things. pub unsafe fn create( - window: c_ulong, display: *mut c_void, config: FbConfig, + window: c_ulong, display: *mut xlib::_XDisplay, config: FbConfig, ) -> Result { if display.is_null() { return Err(GlError::InvalidWindowHandle); } - let display = display as *mut xlib::_XDisplay; - errors::XErrorHandler::handle(display, |error_handler| { #[allow(non_snake_case)] let glXCreateContextAttribsARB: GlXCreateContextAttribsARB = { diff --git a/src/x11/window.rs b/src/x11/window.rs index e644442f..f080d4b0 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -324,7 +324,7 @@ impl Window { #[cfg(feature = "opengl")] let gl_context = fb_config.map(|fb_config| { let window = window_id as c_ulong; - let display = xcb_connection.conn.get_raw_dpy() as *mut c_void; + 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(window, display, fb_config) } From 0df43486ba43c53bb4e69775f35ef3f1569640b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 26 Nov 2022 14:47:42 +0100 Subject: [PATCH 09/11] X11: provide XlibWindowHandle.visual_id --- src/x11/window.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/x11/window.rs b/src/x11/window.rs index f080d4b0..7f6ceec4 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -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, @@ -336,6 +337,7 @@ impl Window { xcb_connection, window_id, window_info, + visual_id: visual, mouse_cursor: MouseCursor::default(), frame_interval: Duration::from_millis(15), @@ -685,7 +687,9 @@ impl Window { unsafe impl HasRawWindowHandle for Window { fn raw_window_handle(&self) -> RawWindowHandle { let mut handle = XlibWindowHandle::empty(); - handle.window = self.window_id as c_ulong; + + handle.window = self.window_id.into(); + handle.visual_id = self.visual_id.into(); RawWindowHandle::Xlib(handle) } From fd1a3a4d2eeb4dc5d6e2f7d793f7f28fa071a3a2 Mon Sep 17 00:00:00 2001 From: Fredemus Date: Mon, 2 Oct 2023 23:06:28 +0200 Subject: [PATCH 10/11] Add logic for CursorEntered/CursorLeft on x11 --- src/x11/window.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/x11/window.rs b/src/x11/window.rs index fcd90837..f9902e51 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -280,10 +280,12 @@ impl Window { | xcb::EVENT_MASK_BUTTON_RELEASE | xcb::EVENT_MASK_KEY_PRESS | xcb::EVENT_MASK_KEY_RELEASE - | xcb::EVENT_MASK_STRUCTURE_NOTIFY, + | xcb::EVENT_MASK_STRUCTURE_NOTIFY + | xcb::EVENT_MASK_ENTER_WINDOW + | xcb::EVENT_MASK_LEAVE_WINDOW, ), - // As mentioend above, these two values are needed to be able to create a window - // with a dpeth of 32-bits when the parent window has a different depth + // As mentioned above, these two values are needed to be able to create a window + // with a depth of 32-bits when the parent window has a different depth (xcb::CW_COLORMAP, colormap), (xcb::CW_BORDER_PIXEL, 0), ], @@ -609,6 +611,20 @@ impl Window { } } + // mouse entering the window + // TODO: might need to consider the window used in `xcb::cast_event::(&event)` + xcb::ENTER_NOTIFY => { + handler.on_event( + &mut crate::Window::new(self), + Event::Mouse(MouseEvent::CursorEntered), + ); + } + + xcb::LEAVE_NOTIFY => { + handler + .on_event(&mut crate::Window::new(self), Event::Mouse(MouseEvent::CursorLeft)); + } + xcb::BUTTON_PRESS => { let event = unsafe { xcb::cast_event::(&event) }; let detail = event.detail(); From 486bb1f0696e45f16ae9b95e5005348bf8ed9d90 Mon Sep 17 00:00:00 2001 From: Fredemus Date: Sat, 7 Oct 2023 16:23:12 +0200 Subject: [PATCH 11/11] add MouseMoved event when ENTER_NOTIFY happens --- src/x11/window.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/x11/window.rs b/src/x11/window.rs index f9902e51..08166c9e 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -611,13 +611,23 @@ impl Window { } } - // mouse entering the window - // TODO: might need to consider the window used in `xcb::cast_event::(&event)` xcb::ENTER_NOTIFY => { handler.on_event( &mut crate::Window::new(self), Event::Mouse(MouseEvent::CursorEntered), ); + // since no `MOTION_NOTIFY` event is generated when `ENTER_NOTIFY` is generated, + // we generate a CursorMoved as well, so the mouse position from here isn't lost + let event = unsafe { xcb::cast_event::(&event) }; + let physical_pos = PhyPoint::new(event.event_x() as i32, event.event_y() as i32); + let logical_pos = physical_pos.to_logical(&self.window_info); + handler.on_event( + &mut crate::Window::new(self), + Event::Mouse(MouseEvent::CursorMoved { + position: logical_pos, + modifiers: key_mods(event.state()), + }), + ); } xcb::LEAVE_NOTIFY => {