Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/native/linux_x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/native/linux_x11/libx11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 10 additions & 0 deletions src/native/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down