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,4 +1,4 @@
use crate::{x11, common::TraitCapturer};
use crate::{common::TraitCapturer, x11};
use std::{io, ops, time::Duration};
pub struct Capturer(x11::Capturer);
@@ -90,6 +90,6 @@ impl Display {
}
pub fn name(&self) -> String {
"".to_owned()
self.0.name()
}
}

View File

@@ -9,6 +9,7 @@ pub struct Display {
default: bool,
rect: Rect,
root: xcb_window_t,
name: String,
}
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
@@ -25,12 +26,14 @@ impl Display {
default: bool,
rect: Rect,
root: xcb_window_t,
name: String,
) -> Display {
Display {
server,
default,
rect,
root,
name,
}
}
@@ -52,4 +55,8 @@ impl Display {
pub fn root(&self) -> xcb_window_t {
self.root
}
pub fn name(&self) -> String {
self.name.clone()
}
}

View File

@@ -65,6 +65,21 @@ extern "C" {
) -> xcb_randr_monitor_info_iterator_t;
pub fn xcb_randr_monitor_info_next(i: *mut xcb_randr_monitor_info_iterator_t);
pub fn xcb_get_atom_name(
c: *mut xcb_connection_t,
atom: xcb_atom_t,
) -> xcb_get_atom_name_cookie_t;
pub fn xcb_get_atom_name_reply(
c: *mut xcb_connection_t,
cookie: xcb_get_atom_name_cookie_t,
e: *mut *mut xcb_generic_error_t,
) -> *const xcb_get_atom_name_reply_t;
pub fn xcb_get_atom_name_name(reply: *const xcb_get_atom_name_request_t) -> *const u8;
pub fn xcb_get_atom_name_name_length(reply: *const xcb_get_atom_name_reply_t) -> i32;
}
pub const XCB_IMAGE_FORMAT_Z_PIXMAP: u8 = 2;
@@ -78,6 +93,9 @@ pub type xcb_timestamp_t = u32;
pub type xcb_colormap_t = u32;
pub type xcb_shm_seg_t = u32;
pub type xcb_drawable_t = u32;
pub type xcb_get_atom_name_cookie_t = u32;
pub type xcb_get_atom_name_reply_t = u32;
pub type xcb_get_atom_name_request_t = xcb_get_atom_name_reply_t;
#[repr(C)]
pub struct xcb_setup_t {

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
}
}