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 xcb;
@ -22,9 +24,20 @@ fn rotation_map(rotation: xcb::randr::Rotation) -> &'static str {
fn display_one_output(
conn: &xcb::Connection,
output: &xcb::randr::GetOutputInfoReply,
cookie: xcb::randr::GetOutputInfoCookie,
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
.wait_for_reply(conn.send_request(&xcb::randr::GetCrtcInfo {
crtc: output.crtc(),
@ -33,7 +46,7 @@ fn display_one_output(
.unwrap();
println!(
"x: {}, y: {}, W×H: {}×{}, status: {}, rotation: {}",
"x: {}, y: {}, W×H: {}×{}, status: {}, rotation: {}\n",
crtc.x(),
crtc.y(),
crtc.width(),
@ -48,22 +61,15 @@ fn display_outputs(
screen: &xcb::randr::GetScreenResourcesReply,
timestamp: xcb::x::Timestamp,
) {
let output_cookies = screen
.outputs()
.iter()
.map(|output| {
let output_cookies = screen.outputs().iter().map(|output| {
conn.send_request(&xcb::randr::GetOutputInfo {
output: *output,
config_timestamp: timestamp,
})
})
.collect::<Vec<_>>();
});
for cookie in output_cookies.into_iter() {
let reply = conn.wait_for_reply(cookie).unwrap();
if reply.connection() == xcb::randr::Connection::Connected {
display_one_output(conn, &reply, timestamp)
}
for cookie in output_cookies {
display_one_output(conn, cookie, timestamp)
}
}