How can I get the system resolution under the linux system, including mainstream distributions (fedora, ubuntu...): I know through searching information that I can call the api of linux: XDisplayWidth, but I don't know how to call it
The x11 crate has binding for the C API as x11::xlib::XDisplayWidth
. See https://crates.io/crates/x11
Thank you very much, does macos have a similar library
I don't know, I don't use mac. But if you have the name of a C library try searching for it on crates.io, most probably someone has already done the bindings.
ok,thanks
This function doesn't seem to be called because I can't get the parameters he needs
It needs a pointer to the Display
and the screen number, as described here: https://x.org/releases/current/doc/libX11/libX11/libX11.html#Image_Format_Functions_and_Macros
What exact error are you getting?
With the xlib
feature enabled for the x11
crate:
[dependencies]
x11 = { version = "2.21.0", features = ["xlib"] }
this code works for me:
use x11::xlib::{XOpenDisplay, XDefaultScreen, XDisplayWidth};
fn main() {
let d = unsafe { XOpenDisplay(std::ptr::null()) };
let s = unsafe { XDefaultScreen(d) };
let w = unsafe { XDisplayWidth(d, s) };
println!("{w}");
}
For something that deals with more than just raw pointers, and that is already cross-platform, the winit crate might work better for you.