linux x11 rgb565 capture (#8580)

Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
21pages
2024-07-03 14:20:41 +08:00
committed by GitHub
parent 6d2e985593
commit d00582e929
7 changed files with 165 additions and 45 deletions
+21 -6
View File
@@ -2,6 +2,7 @@ use std::ffi::CString;
use std::ptr;
use std::rc::Rc;
use crate::Pixfmt;
use hbb_common::libc;
use super::ffi::*;
@@ -66,7 +67,7 @@ impl Iterator for DisplayIter {
unsafe {
let data = &*inner.data;
let name = get_atom_name(self.server.raw(), data.name);
let pixfmt = get_pixfmt(self.server.raw(), root).unwrap_or(Pixfmt::BGRA);
let display = Display::new(
self.server.clone(),
data.primary != 0,
@@ -78,6 +79,7 @@ impl Iterator for DisplayIter {
},
root,
name,
pixfmt,
);
xcb_randr_monitor_info_next(inner);
@@ -102,11 +104,7 @@ fn get_atom_name(conn: *mut xcb_connection_t, atom: xcb_atom_t) -> String {
}
unsafe {
let mut e: *mut xcb_generic_error_t = std::ptr::null_mut();
let reply = xcb_get_atom_name_reply(
conn,
xcb_get_atom_name(conn, atom),
&mut e as _,
);
let reply = xcb_get_atom_name_reply(conn, xcb_get_atom_name(conn, atom), &mut e as _);
if reply == std::ptr::null() {
return empty;
}
@@ -121,3 +119,20 @@ fn get_atom_name(conn: *mut xcb_connection_t, atom: xcb_atom_t) -> String {
empty
}
}
unsafe fn get_pixfmt(conn: *mut xcb_connection_t, root: xcb_window_t) -> Option<Pixfmt> {
let geo_cookie = xcb_get_geometry_unchecked(conn, root);
let geo = xcb_get_geometry_reply(conn, geo_cookie, ptr::null_mut());
if geo.is_null() {
return None;
}
let depth = (*geo).depth;
libc::free(geo as _);
// now only support little endian
// https://github.com/FFmpeg/FFmpeg/blob/a9c05eb657d0d05f3ac79fe9973581a41b265a5e/libavdevice/xcbgrab.c#L519
match depth {
16 => Some(Pixfmt::RGB565LE),
32 => Some(Pixfmt::BGRA),
_ => None,
}
}