Skip to main content

output_info/
output_info.rs

1use std::ffi::CString;
2
3use wayrs_client::global::GlobalExt;
4use wayrs_client::protocol::wl_output::{self, WlOutput};
5use wayrs_client::{Connection, EventCtx, IoMode};
6
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
35
36struct State {
37    outputs: Vec<(WlOutput, OutputInfo)>,
38}
39
40#[derive(Debug, Default)]
41struct OutputInfo {
42    done: bool,
43    name: Option<CString>,
44    desc: Option<CString>,
45    scale: Option<i32>,
46    mode: Option<String>,
47}
48
49fn wl_output_cb(ctx: EventCtx<State, WlOutput>) {
50    let output = &mut ctx
51        .state
52        .outputs
53        .iter_mut()
54        .find(|o| o.0 == ctx.proxy)
55        .unwrap()
56        .1;
57    match ctx.event {
58        wl_output::Event::Geometry(_) => (),
59        wl_output::Event::Mode(mode) => {
60            output.mode = Some(format!(
61                "{}x{} @ {}Hz",
62                mode.width,
63                mode.height,
64                mode.refresh as f64 * 1e-3
65            ))
66        }
67        wl_output::Event::Done => output.done = true,
68        wl_output::Event::Scale(scale) => output.scale = Some(scale),
69        wl_output::Event::Name(name) => output.name = Some(name),
70        wl_output::Event::Description(desc) => output.desc = Some(desc),
71        _ => (),
72    }
73}