Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_screen_mouse_position function for Windows and Linux X11 #486

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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 @@ -160,7 +160,7 @@
unsafe {
#[cfg(target_arch = "x86_64")]
SetWindowLongPtrA(self.wnd, GWL_STYLE, win_style as _);
#[cfg(target_arch = "i686")]

Check warning on line 163 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition value: `i686`

Check warning on line 163 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition value: `i686`
SetWindowLong(self.wnd, GWL_STYLE, win_style as _);

if self.fullscreen {
Expand Down Expand Up @@ -795,6 +795,14 @@
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 @@ -824,7 +832,7 @@

fn process_request(&mut self, request: Request) {
use Request::*;
unsafe {

Check warning on line 835 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

unnecessary `unsafe` block

Check warning on line 835 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

unnecessary `unsafe` block
match request {
ScheduleUpdate => {
self.update_requested = true;
Expand All @@ -838,7 +846,7 @@
} => self.set_window_size(new_width as _, new_height as _),
SetWindowPosition { new_x, new_y } => self.set_window_position(new_x, new_y),
SetFullscreen(fullscreen) => self.set_fullscreen(fullscreen),
ShowKeyboard(show) => {

Check warning on line 849 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

unused variable: `show`

Check warning on line 849 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

unused variable: `show`
eprintln!("Not implemented for windows")
}
}
Expand Down Expand Up @@ -917,7 +925,7 @@

#[cfg(target_arch = "x86_64")]
SetWindowLongPtrA(wnd, GWLP_USERDATA, &mut display as *mut _ as isize);
#[cfg(target_arch = "i686")]

Check warning on line 928 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition value: `i686`

Check warning on line 928 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition value: `i686`
SetWindowLong(wnd, GWLP_USERDATA, &mut display as *mut _ as isize);

let mut done = false;
Expand All @@ -926,6 +934,8 @@
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
Loading