linux x11 resolution

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2023-02-11 19:04:33 +08:00
parent 91a2a5b56e
commit 18a66749a1
7 changed files with 180 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
use std::ffi::CString;
use std::ptr;
use std::rc::Rc;
@@ -64,6 +65,7 @@ impl Iterator for DisplayIter {
if inner.rem != 0 {
unsafe {
let data = &*inner.data;
let name = get_atom_name(self.server.raw(), data.name);
let display = Display::new(
self.server.clone(),
@@ -75,6 +77,7 @@ impl Iterator for DisplayIter {
h: data.height,
},
root,
name,
);
xcb_randr_monitor_info_next(inner);
@@ -91,3 +94,30 @@ impl Iterator for DisplayIter {
}
}
}
fn get_atom_name(conn: *mut xcb_connection_t, atom: xcb_atom_t) -> String {
let empty = "".to_owned();
if atom == 0 {
return empty;
}
unsafe {
let mut e: xcb_generic_error_t = std::mem::zeroed();
let reply = xcb_get_atom_name_reply(
conn,
xcb_get_atom_name(conn, atom),
&mut ((&mut e) as *mut xcb_generic_error_t) as _,
);
if reply == std::ptr::null() {
return empty;
}
let length = xcb_get_atom_name_name_length(reply);
let name = xcb_get_atom_name_name(reply);
let mut v = vec![0u8; length as _];
std::ptr::copy_nonoverlapping(name as _, v.as_mut_ptr(), length as _);
libc::free(reply as *mut _);
if let Ok(s) = CString::new(v) {
return s.to_string_lossy().to_string();
}
empty
}
}