display.rs

 1use anyhow::Context as _;
 2use uuid::Uuid;
 3use x11rb::{connection::Connection as _, xcb_ffi::XCBConnection};
 4
 5use crate::{Bounds, DisplayId, Pixels, PlatformDisplay, Size, px};
 6
 7#[derive(Debug)]
 8pub(crate) struct X11Display {
 9    x_screen_index: usize,
10    bounds: Bounds<Pixels>,
11    uuid: Uuid,
12}
13
14impl X11Display {
15    pub(crate) fn new(
16        xcb: &XCBConnection,
17        scale_factor: f32,
18        x_screen_index: usize,
19    ) -> anyhow::Result<Self> {
20        let screen = xcb
21            .setup()
22            .roots
23            .get(x_screen_index)
24            .with_context(|| format!("No screen found with index {x_screen_index}"))?;
25        Ok(Self {
26            x_screen_index,
27            bounds: Bounds {
28                origin: Default::default(),
29                size: Size {
30                    width: px(screen.width_in_pixels as f32 / scale_factor),
31                    height: px(screen.height_in_pixels as f32 / scale_factor),
32                },
33            },
34            uuid: Uuid::from_bytes([0; 16]),
35        })
36    }
37}
38
39impl PlatformDisplay for X11Display {
40    fn id(&self) -> DisplayId {
41        DisplayId(self.x_screen_index as u32)
42    }
43
44    fn uuid(&self) -> anyhow::Result<Uuid> {
45        Ok(self.uuid)
46    }
47
48    fn bounds(&self) -> Bounds<Pixels> {
49        self.bounds
50    }
51}