Use X11 FFI in Rust

This commit is contained in:
Alex Kotov 2022-09-09 18:08:31 +04:00
parent 4dc0c65125
commit 2fbe9f00d0
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 27 additions and 18 deletions

View file

@ -9,4 +9,4 @@ name = "winproto"
crate-type = ["staticlib"] crate-type = ["staticlib"]
[dependencies] [dependencies]
x11rb = "0.10.1" x11 = "2.20.0"

View file

@ -1,22 +1,31 @@
use x11rb::atom_manager; use std::os::raw::*;
use std::ptr::null;
atom_manager! { use x11::xlib::{self, Display};
pub AtomCollection: AtomCollectionCookie {
WM_PROTOCOLS,
WM_DELETE_WINDOW,
WM_STATE,
WM_TAKE_FOCUS,
_NET_ACTIVE_WINDOW, struct Xbase {
_NET_SUPPORTED, program_title: String,
_NET_WM_NAME, x_display: *mut Display,
_NET_WM_STATE, x_screen: c_int,
_NET_SUPPORTING_WM_CHECK, x_root: c_ulong,
_NET_WM_STATE_FULLSCREEN, }
_NET_WM_WINDOW_TYPE,
_NET_WM_WINDOW_TYPE_DIALOG,
_NET_CLIENT_LIST,
UTF8_STRING, impl Xbase {
fn new(program_title: String) -> Result<Self, Box<dyn std::error::Error>> {
unsafe {
if xlib::XSupportsLocale() == 0 {
return Err("no locale support in X".into());
}
let x_display = xlib::XOpenDisplay(null());
if x_display.is_null() {
return Err("cannot open X display".into());
}
let x_screen = xlib::XDefaultScreen(x_display);
let x_root = xlib::XRootWindow(x_display, x_screen);
Ok(Self { program_title, x_display, x_screen, x_root })
}
} }
} }