diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 9ad14dffad..5dcbffd87d 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -41,3 +41,4 @@ - Add `Window::default_attributes` to get default `WindowAttributes`. - `log` has been replaced with `tracing`. The old behavior can be emulated by setting the `log` feature on the `tracing` crate. - On Windows, confine cursor to center of window when grabbed and hidden. +- Add `ActiveEventLoop::cursor_position`. diff --git a/src/event_loop.rs b/src/event_loop.rs index 48455e3062..4a66c640f3 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -19,7 +19,8 @@ use std::time::{Duration, Instant}; use web_time::{Duration, Instant}; use crate::application::ApplicationHandler; -use crate::error::{EventLoopError, OsError}; +use crate::dpi::PhysicalPosition; +use crate::error::{EventLoopError, ExternalError, OsError}; use crate::window::{CustomCursor, CustomCursorSource, Window, WindowAttributes}; use crate::{event::Event, monitor::MonitorHandle, platform_impl}; @@ -490,6 +491,18 @@ impl ActiveEventLoop { platform: self.p.owned_display_handle(), } } + + /// Returns the current cursor position relative to the top-left hand corner of the desktop. + /// + /// See [`Window::outer_position`] for more information about the coordinates. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / Wayland / Orbital / Web**: Unsupported. + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + self.p.cursor_position() + } } #[cfg(feature = "rwh_06")] diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 08dbb6752d..205a1b5d3c 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -732,6 +732,11 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + pub fn cursor_position(&self) -> Result, error::ExternalError> { + Err(error::ExternalError::NotSupported( + error::NotSupportedError::new(), + )) + } } #[derive(Clone)] diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 27921c1bed..579b4e2791 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -17,7 +17,8 @@ use icrate::Foundation::{MainThreadMarker, NSString}; use objc2::ClassType; use crate::{ - error::EventLoopError, + dpi::PhysicalPosition, + error::{EventLoopError, ExternalError, NotSupportedError}, event::Event, event_loop::{ ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopClosed, @@ -95,6 +96,10 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } #[derive(Clone)] diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index bd5e606687..80cb74a555 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -937,6 +937,10 @@ impl ActiveEventLoop { fn exit_code(&self) -> Option { x11_or_wayland!(match self; Self(evlp) => evlp.exit_code()) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + x11_or_wayland!(match self; Self(evlp) => evlp.cursor_position()) + } } #[derive(Clone)] diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 36ce8f5638..ead00f1733 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -16,8 +16,8 @@ use sctk::reexports::client::globals; use sctk::reexports::client::{Connection, QueueHandle}; use crate::cursor::OnlyCursorImage; -use crate::dpi::LogicalSize; -use crate::error::{EventLoopError, OsError as RootOsError}; +use crate::dpi::{LogicalSize, PhysicalPosition}; +use crate::error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError}; use crate::event::{Event, InnerSizeWriter, StartCause, WindowEvent}; use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents}; use crate::platform::pump_events::PumpStatus; @@ -719,4 +719,8 @@ impl ActiveEventLoop { }) .into()) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 72b9a5c9e6..f3c30df358 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -28,7 +28,8 @@ use x11rb::protocol::xproto::{self, ConnectionExt as _}; use x11rb::x11_utils::X11Error as LogicalError; use x11rb::xcb_ffi::ReplyOrIdError; -use crate::error::{EventLoopError, OsError as RootOsError}; +use crate::dpi::PhysicalPosition; +use crate::error::{EventLoopError, ExternalError, OsError as RootOsError}; use crate::event::{Event, StartCause, WindowEvent}; use crate::event_loop::{ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents, EventLoopClosed}; use crate::platform::pump_events::PumpStatus; @@ -757,6 +758,12 @@ impl ActiveEventLoop { pub(crate) fn exit_code(&self) -> Option { self.exit.get() } + + pub fn cursor_position(&self) -> Result, ExternalError> { + self.xconn + .cursor_position(self.root) + .map_err(|x_err| ExternalError::Os(os_error!(OsError::XError(Arc::new(x_err))))) + } } impl EventLoopProxy { diff --git a/src/platform_impl/linux/x11/util/cursor.rs b/src/platform_impl/linux/x11/util/cursor.rs index f86907a464..f4a2da6b9c 100644 --- a/src/platform_impl/linux/x11/util/cursor.rs +++ b/src/platform_impl/linux/x11/util/cursor.rs @@ -7,6 +7,7 @@ use std::{ use x11rb::connection::Connection; +use crate::dpi::PhysicalPosition; use crate::{platform_impl::PlatformCustomCursorSource, window::CursorIcon}; use super::super::ActiveEventLoop; @@ -96,6 +97,14 @@ impl XConnection { self.xcb_connection().flush()?; Ok(()) } + + pub fn cursor_position( + &self, + window: xproto::Window, + ) -> Result, X11Error> { + let reply = self.xcb_connection().query_pointer(window)?.reply()?; + Ok((reply.root_x, reply.root_y).into()) + } } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 1e0e34ee37..f9daaebac0 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -18,9 +18,9 @@ use core_foundation::runloop::{ }; use icrate::AppKit::{ NSApplication, NSApplicationActivationPolicyAccessory, NSApplicationActivationPolicyProhibited, - NSApplicationActivationPolicyRegular, NSWindow, + NSApplicationActivationPolicyRegular, NSEvent, NSWindow, }; -use icrate::Foundation::{MainThreadMarker, NSObjectProtocol}; +use icrate::Foundation::{CGRect, CGSize, MainThreadMarker, NSObjectProtocol}; use objc2::{msg_send_id, ClassType}; use objc2::{ rc::{autoreleasepool, Id}, @@ -34,6 +34,8 @@ use super::{ monitor::{self, MonitorHandle}, observer::setup_control_flow_observers, }; +use crate::dpi::{LogicalPosition, PhysicalPosition}; +use crate::error::ExternalError; use crate::platform_impl::platform::cursor::CustomCursor; use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource}; use crate::{ @@ -146,6 +148,16 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + let point = unsafe { NSEvent::mouseLocation() }; + // The cursor is sizeless + let rect = CGRect::new(point, CGSize::new(0.0, 0.0)); + let point = monitor::flip_window_screen_coordinates(rect); + let point = LogicalPosition::new(point.x, point.y); + Ok(point.to_physical(monitor::primary_monitor().scale_factor())) + } } impl ActiveEventLoop { diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index 11216e1176..a42344f12e 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -15,7 +15,8 @@ use orbclient::{ use smol_str::SmolStr; use crate::{ - error::EventLoopError, + dpi::PhysicalPosition, + error::{EventLoopError, ExternalError, NotSupportedError}, event::{self, Ime, Modifiers, StartCause}, event_loop::{self, ControlFlow, DeviceEvents}, keyboard::{ @@ -860,6 +861,10 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } #[derive(Clone)] diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 3d23d9ce84..cdc2be33bb 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -14,6 +14,8 @@ use super::{ runner, window::WindowId, }; +use crate::dpi::PhysicalPosition; +use crate::error::{ExternalError, NotSupportedError}; use crate::event::{ DeviceId as RootDeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent, }; @@ -722,6 +724,10 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } #[derive(Clone)] diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index f7d91caa1b..4a688b6422 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -17,7 +17,7 @@ use std::{ time::{Duration, Instant}, }; -use crate::utils::Lazy; +use crate::{error::ExternalError, utils::Lazy}; use windows_sys::Win32::{ Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE, @@ -596,6 +596,10 @@ impl ActiveEventLoop { OwnedDisplayHandle } + pub fn cursor_position(&self) -> Result, ExternalError> { + util::cursor_position() + } + fn exit_code(&self) -> Option { self.runner_shared.exit_code() } diff --git a/src/platform_impl/windows/util.rs b/src/platform_impl/windows/util.rs index ae36bdaec6..4334b2bbed 100644 --- a/src/platform_impl/windows/util.rs +++ b/src/platform_impl/windows/util.rs @@ -13,7 +13,7 @@ use crate::utils::Lazy; use windows_sys::{ core::{HRESULT, PCWSTR}, Win32::{ - Foundation::{BOOL, HANDLE, HMODULE, HWND, RECT}, + Foundation::{BOOL, HANDLE, HMODULE, HWND, POINT, RECT}, Graphics::Gdi::{ClientToScreen, HMONITOR}, System::{ LibraryLoader::{GetProcAddress, LoadLibraryA}, @@ -26,17 +26,18 @@ use windows_sys::{ Pointer::{POINTER_INFO, POINTER_PEN_INFO, POINTER_TOUCH_INFO}, }, WindowsAndMessaging::{ - ClipCursor, GetClientRect, GetClipCursor, GetSystemMetrics, GetWindowPlacement, - GetWindowRect, IsIconic, ShowCursor, IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, - IDC_HAND, IDC_HELP, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, - IDC_SIZENWSE, IDC_SIZEWE, IDC_WAIT, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, - SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN, SW_MAXIMIZE, WINDOWPLACEMENT, + ClipCursor, GetClientRect, GetClipCursor, GetCursorPos, GetSystemMetrics, + GetWindowPlacement, GetWindowRect, IsIconic, ShowCursor, IDC_APPSTARTING, + IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM, IDC_NO, IDC_SIZEALL, + IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_WAIT, SM_CXVIRTUALSCREEN, + SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN, SW_MAXIMIZE, + WINDOWPLACEMENT, }, }, }, }; -use crate::window::CursorIcon; +use crate::{dpi::PhysicalPosition, error::ExternalError, window::CursorIcon}; pub fn encode_wide(string: impl AsRef) -> Vec { string.as_ref().encode_wide().chain(once(0)).collect() @@ -167,6 +168,13 @@ pub fn get_instance_handle() -> HMODULE { unsafe { &__ImageBase as *const _ as _ } } +pub fn cursor_position() -> Result, ExternalError> { + let mut pt = POINT { x: 0, y: 0 }; + win_to_err(unsafe { GetCursorPos(&mut pt) }) + .map(|_| (pt.x, pt.y).into()) + .map_err(|e| ExternalError::Os(os_error!(e))) +} + pub(crate) fn to_windows_cursor(cursor: CursorIcon) -> PCWSTR { match cursor { CursorIcon::Default => IDC_ARROW,