emoon / rust_minifb

Cross platfrom window and framebuffer crate for Rust
MIT License
1k stars 95 forks source link

Add getter for window position. #276

Closed AndreasOM closed 2 years ago

AndreasOM commented 2 years ago

For a current project I wanted to store the layout of windows on the screen to reuse for the next session.

I added get_position to the window. It is tested on macos, and windows - with a single display connected. I created a stub for linux, which always returns 0,0. It compiles, but is untested due to lack of a system to test on.

It will need some rework for multi display systems, but I don't have one to do that. I tried to keep the formatting, and style in line with the original.

xkevio commented 2 years ago

This works for me under X11. Sadly, I don't think there is a way to find it under Wayland or RedoxOS.

#[inline]
pub fn get_position(&self) -> (isize, isize) {
    let (x, y);
    let (mut nx, mut ny) = (0, 0);

    // Create dummy window for child_return value
    let mut dummy_window: MaybeUninit<Window> = MaybeUninit::uninit();
    let mut attributes: MaybeUninit<XWindowAttributes> = MaybeUninit::uninit();

    unsafe {
        let root = (self.d.lib.XDefaultRootWindow)(self.d.display);

        (self.d.lib.XGetWindowAttributes)(self.d.display, self.handle, attributes.as_mut_ptr());
        x = attributes.assume_init().x;
        y = attributes.assume_init().y;

        (self.d.lib.XTranslateCoordinates)(
            self.d.display,
            self.handle,
            root,
            x,
            y,
            &mut nx,
            &mut ny,
            dummy_window.as_mut_ptr() as *mut u64,
        );
    }

    (nx as isize, ny as isize)
}
emoon commented 2 years ago

Thanks both of you!

Yeah, as for Wayland I looked at glfw and they also don't support it https://github.com/glfw/glfw/blob/master/src/wl_window.c#L880-L887 so I think we just have to live with returning (0, 0) on those platforms.

@AndreasOM can you bring in the changes (above) from @xkevio into the same PR?

AndreasOM commented 2 years ago

Will be a bit busy the next few days, but should find some time on the weekend. Will be a blind add of the linux part though.

AndreasOM commented 2 years ago

Ah, well, skipped my coffee break, and just did it.

No promises it works, but it compiles.

emoon commented 2 years ago

Thanks! I will merge it and give it a try :)

emoon commented 2 years ago

I tested this today and it works just fine under Linux/X11 :)