From 988473b25295e71b844d2a82ee9f2415f0b2892d Mon Sep 17 00:00:00 2001 From: Zan <69921455+InZan17@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:08:10 +0200 Subject: [PATCH 1/3] Add screen_mouse_position field + get function --- src/lib.rs | 6 ++++++ src/native.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 939758b5..a42fb722 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -259,6 +259,12 @@ pub mod window { d.screen_position } + #[cfg(any(target_os = "windows", target_os = "linux"))] + pub fn get_screen_mouse_position() -> (i32, i32) { + let d = native_display().lock().unwrap(); + d.screen_mouse_position + } + pub fn set_fullscreen(fullscreen: bool) { let d = native_display().lock().unwrap(); d.native_requests diff --git a/src/native.rs b/src/native.rs index 88640a55..eacda21c 100644 --- a/src/native.rs +++ b/src/native.rs @@ -11,6 +11,7 @@ pub(crate) struct NativeDisplayData { pub screen_width: i32, pub screen_height: i32, pub screen_position: (u32, u32), + pub screen_mouse_position: (i32, i32), pub dpi_scale: f32, pub high_dpi: bool, pub quit_requested: bool, @@ -43,6 +44,7 @@ impl NativeDisplayData { screen_width, screen_height, screen_position: (0, 0), + screen_mouse_position: (0, 0), dpi_scale: 1., high_dpi: false, quit_requested: false, From 411780f73a9f26bbc807eff50188a13aba1bf375 Mon Sep 17 00:00:00 2001 From: Zan <69921455+InZan17@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:09:03 +0200 Subject: [PATCH 2/3] Update screen_mouse_position on Linux X11 --- src/native/linux_x11.rs | 29 +++++++++++++++++++++++++++++ src/native/linux_x11/libx11.rs | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/native/linux_x11.rs b/src/native/linux_x11.rs index 12aeea03..a98cc68d 100644 --- a/src/native/linux_x11.rs +++ b/src/native/linux_x11.rs @@ -188,6 +188,31 @@ impl X11Display { } } + unsafe fn update_screen_mouse_position(&mut self) { + let mut root_return: Window = 0; + let mut child_return: Window = 0; + let mut root_x_return: libc::c_int = 0; + let mut root_y_return: libc::c_int = 0; + let mut win_x_return: libc::c_int = 0; + let mut win_y_return: libc::c_int = 0; + let mut mask_return: libc::c_uint = 0; + + (self.libx11.XQueryPointer)( + self.display, + self.window, + &mut root_return as *mut _, + &mut child_return as *mut _, + &mut root_x_return as *mut _, + &mut root_y_return as *mut _, + &mut win_x_return as *mut _, + &mut win_y_return as *mut _, + &mut mask_return as *mut _, + ); + + let mut d = crate::native_display().try_lock().unwrap(); + d.screen_mouse_position = (root_x_return, root_y_return); + } + // TODO: right now it just exits early if fullscreen is false. // should be able to able to go back from fullscreen to windowed instead unsafe fn set_fullscreen(&mut self, window: Window, fullscreen: bool) { @@ -428,6 +453,8 @@ where } glx.make_current(display.display, glx_window, glx_context); + display.update_screen_mouse_position(); + let mut count = (display.libx11.XPending)(display.display); let block_on_wait = conf.platform.blocking_event_loop && !display.update_requested; if block_on_wait { @@ -539,6 +566,8 @@ where display.process_request(request); } + display.update_screen_mouse_position(); + let mut count = (display.libx11.XPending)(display.display); let block_on_wait = conf.platform.blocking_event_loop && !display.update_requested; if block_on_wait { diff --git a/src/native/linux_x11/libx11.rs b/src/native/linux_x11/libx11.rs index 8000b342..5be98515 100644 --- a/src/native/linux_x11/libx11.rs +++ b/src/native/linux_x11/libx11.rs @@ -918,6 +918,17 @@ pub type XrmDestroyDatabase = unsafe extern "C" fn(_: XrmDatabase); pub type XrmGetStringDatabase = unsafe extern "C" fn(_: *const libc::c_char) -> XrmDatabase; pub type XkbSetDetectableAutoRepeat = unsafe extern "C" fn(_: *mut Display, _: libc::c_int, _: *mut libc::c_int) -> libc::c_int; +pub type XQueryPointer = unsafe extern "C" fn( + _: *mut Display, + _: Window, + _: *mut Window, + _: *mut Window, + _: *mut libc::c_int, + _: *mut libc::c_int, + _: *mut libc::c_int, + _: *mut libc::c_int, + _: *mut libc::c_uint, +) -> libc::c_int; pub type XQueryExtension = unsafe extern "C" fn( _: *mut Display, _: *const libc::c_char, @@ -1033,6 +1044,7 @@ pub struct LibX11 { pub XrmDestroyDatabase: XrmDestroyDatabase, pub XrmGetStringDatabase: XrmGetStringDatabase, pub XkbSetDetectableAutoRepeat: XkbSetDetectableAutoRepeat, + pub XQueryPointer: XQueryPointer, pub XQueryExtension: XQueryExtension, pub XConvertSelection: XConvertSelection, pub XSetSelectionOwner: XSetSelectionOwner, @@ -1088,6 +1100,7 @@ impl LibX11 { XkbSetDetectableAutoRepeat: module .get_symbol("XkbSetDetectableAutoRepeat") .unwrap(), + XQueryPointer: module.get_symbol("XQueryPointer").unwrap(), XQueryExtension: module.get_symbol("XQueryExtension").unwrap(), XConvertSelection: module.get_symbol("XConvertSelection").unwrap(), XSetSelectionOwner: module.get_symbol("XSetSelectionOwner").unwrap(), From 4cd4c041c0da88d5529a664f0aece2f68d9fb345 Mon Sep 17 00:00:00 2001 From: Zan <69921455+InZan17@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:18:27 +0200 Subject: [PATCH 3/3] Update screen_mouse_position on Windows --- src/native/windows.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/native/windows.rs b/src/native/windows.rs index a420e820..52922fab 100644 --- a/src/native/windows.rs +++ b/src/native/windows.rs @@ -795,6 +795,14 @@ impl WindowsDisplay { false } + unsafe fn update_screen_mouse_position(&mut self) { + let mut point: POINT = std::mem::zeroed(); + if GetCursorPos(&mut point as *mut _) != 0 { + let mut d = crate::native_display().lock().unwrap(); + d.screen_mouse_position = (point.x, point.y); + } + } + unsafe fn init_dpi(&mut self, high_dpi: bool) { self.dpi_aware = high_dpi; // get dpi scale factor for main monitor @@ -926,6 +934,8 @@ where display.process_request(request); } + display.update_screen_mouse_position(); + let mut dispatch_message = |mut msg: MSG| { if msg.message == WM_QUIT { done = true;