Skip to content

Commit

Permalink
add get/set_window_position for Linux X11
Browse files Browse the repository at this point in the history
  • Loading branch information
InZan17 authored and not-fl3 committed Sep 15, 2024
1 parent 4bcc8d5 commit 27cade5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub mod window {

/// Get the position of the window.
/// TODO: implement for other platforms
#[cfg(target_os = "windows")]
#[cfg(any(target_os = "windows", target_os = "linux"))]
pub fn get_window_position() -> (u32, u32) {
let d = native_display().lock().unwrap();
d.screen_position
Expand Down
12 changes: 10 additions & 2 deletions src/native/linux_x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ impl X11Display {
}
22 => {
let mut d = crate::native_display().try_lock().unwrap();
let left = (*event).xconfigure.x;
let top = (*event).xconfigure.y;
d.screen_position = (left as _, top as _);
if (*event).xconfigure.width != d.screen_width
|| (*event).xconfigure.height != d.screen_height
{
Expand Down Expand Up @@ -263,6 +266,11 @@ impl X11Display {
(self.libx11.XFlush)(self.display);
}

/// Set the window position in screen coordinates.
unsafe fn set_window_position(&mut self, window: Window, new_x: i32, new_y: i32) {
(self.libx11.XMoveWindow)(self.display, window, new_x, new_y);
}

fn show_mouse(&mut self, shown: bool) {
unsafe {
if shown {
Expand Down Expand Up @@ -347,8 +355,8 @@ impl X11Display {
new_width,
new_height,
} => self.set_window_size(self.window, new_width as _, new_height as _),
SetWindowPosition { .. } => {
eprintln!("Not implemented for X11")
SetWindowPosition { new_x, new_y } => {
self.set_window_position(self.window, new_x as _, new_y as _)
}
SetFullscreen(fullscreen) => self.set_fullscreen(self.window, fullscreen),
ShowKeyboard(..) => {
Expand Down
4 changes: 4 additions & 0 deletions src/native/linux_x11/libx11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ pub type XLowerWindow = unsafe extern "C" fn(_: *mut Display, _: Window) -> libc
pub type XRaiseWindow = unsafe extern "C" fn(_: *mut Display, _: Window) -> libc::c_int;
pub type XResizeWindow =
unsafe extern "C" fn(_: *mut Display, _: Window, _: libc::c_int, _: libc::c_int) -> libc::c_int;
pub type XMoveWindow =
unsafe extern "C" fn(_: *mut Display, _: Window, _: libc::c_int, _: libc::c_int) -> libc::c_int;
pub type XPending = unsafe extern "C" fn(_: *mut Display) -> libc::c_int;
pub type XNextEvent = unsafe extern "C" fn(_: *mut Display, _: *mut XEvent) -> libc::c_int;
pub type XGetKeyboardMapping = unsafe extern "C" fn(
Expand Down Expand Up @@ -1013,6 +1015,7 @@ pub struct LibX11 {
pub XLowerWindow: XLowerWindow,
pub XRaiseWindow: XRaiseWindow,
pub XResizeWindow: XResizeWindow,
pub XMoveWindow: XMoveWindow,
pub XPending: XPending,
pub XNextEvent: XNextEvent,
pub XGetKeyboardMapping: XGetKeyboardMapping,
Expand Down Expand Up @@ -1065,6 +1068,7 @@ impl LibX11 {
XLowerWindow: module.get_symbol("XLowerWindow").unwrap(),
XRaiseWindow: module.get_symbol("XRaiseWindow").unwrap(),
XResizeWindow: module.get_symbol("XResizeWindow").unwrap(),
XMoveWindow: module.get_symbol("XMoveWindow").unwrap(),
XPending: module.get_symbol("XPending").unwrap(),
XNextEvent: module.get_symbol("XNextEvent").unwrap(),
XGetKeyboardMapping: module.get_symbol("XGetKeyboardMapping").unwrap(),
Expand Down

0 comments on commit 27cade5

Please sign in to comment.