Got the same functionality out of Rust, only prettier, and learned an important lesson about how much more Rust cares that the info you handled is, in fact, *fully* handled.

This commit is contained in:
Elf M. Sternberg 2022-04-18 17:26:10 -07:00
parent 0af01c5e0d
commit e8bd037f61
1 changed files with 22 additions and 16 deletions

View File

@ -1,3 +1,5 @@
use xcb::Xid;
extern crate libc; extern crate libc;
extern crate xcb; extern crate xcb;
@ -22,9 +24,20 @@ fn rotation_map(rotation: xcb::randr::Rotation) -> &'static str {
fn display_one_output( fn display_one_output(
conn: &xcb::Connection, conn: &xcb::Connection,
output: &xcb::randr::GetOutputInfoReply, cookie: xcb::randr::GetOutputInfoCookie,
timestamp: xcb::x::Timestamp, timestamp: xcb::x::Timestamp,
) { ) {
let output = conn.wait_for_reply(cookie).unwrap();
if !(output.connection() == xcb::randr::Connection::Connected) {
return;
}
println!("Display: {}", std::str::from_utf8(output.name()).unwrap());
if output.crtc().resource_id() == 0 {
println!("(no monitor found)\n");
return;
}
let crtc = conn let crtc = conn
.wait_for_reply(conn.send_request(&xcb::randr::GetCrtcInfo { .wait_for_reply(conn.send_request(&xcb::randr::GetCrtcInfo {
crtc: output.crtc(), crtc: output.crtc(),
@ -33,7 +46,7 @@ fn display_one_output(
.unwrap(); .unwrap();
println!( println!(
"x: {}, y: {}, W×H: {}×{}, status: {}, rotation: {}", "x: {}, y: {}, W×H: {}×{}, status: {}, rotation: {}\n",
crtc.x(), crtc.x(),
crtc.y(), crtc.y(),
crtc.width(), crtc.width(),
@ -48,22 +61,15 @@ fn display_outputs(
screen: &xcb::randr::GetScreenResourcesReply, screen: &xcb::randr::GetScreenResourcesReply,
timestamp: xcb::x::Timestamp, timestamp: xcb::x::Timestamp,
) { ) {
let output_cookies = screen let output_cookies = screen.outputs().iter().map(|output| {
.outputs() conn.send_request(&xcb::randr::GetOutputInfo {
.iter() output: *output,
.map(|output| { config_timestamp: timestamp,
conn.send_request(&xcb::randr::GetOutputInfo {
output: *output,
config_timestamp: timestamp,
})
}) })
.collect::<Vec<_>>(); });
for cookie in output_cookies.into_iter() { for cookie in output_cookies {
let reply = conn.wait_for_reply(cookie).unwrap(); display_one_output(conn, cookie, timestamp)
if reply.connection() == xcb::randr::Connection::Connected {
display_one_output(conn, &reply, timestamp)
}
} }
} }