From e8bd037f612c6e39799375a35ca3694a6d759f6b Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Mon, 18 Apr 2022 17:26:10 -0700 Subject: [PATCH] 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. --- xcb_read_rs/src/main.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/xcb_read_rs/src/main.rs b/xcb_read_rs/src/main.rs index 9ad2105..04bd67a 100644 --- a/xcb_read_rs/src/main.rs +++ b/xcb_read_rs/src/main.rs @@ -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| { - conn.send_request(&xcb::randr::GetOutputInfo { - output: *output, - config_timestamp: timestamp, - }) + let output_cookies = screen.outputs().iter().map(|output| { + conn.send_request(&xcb::randr::GetOutputInfo { + output: *output, + config_timestamp: timestamp, }) - .collect::>(); + }); - 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) } }