display.rs

 1use anyhow::Result;
 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 Some(screen) = xcb.setup().roots.get(x_screen_index) else {
21            return Err(anyhow::anyhow!(
22                "No screen found with index {}",
23                x_screen_index
24            ));
25        };
26        Ok(Self {
27            x_screen_index,
28            bounds: Bounds {
29                origin: Default::default(),
30                size: Size {
31                    width: px(screen.width_in_pixels as f32 / scale_factor),
32                    height: px(screen.height_in_pixels as f32 / scale_factor),
33                },
34            },
35            uuid: Uuid::from_bytes([0; 16]),
36        })
37    }
38}
39
40impl PlatformDisplay for X11Display {
41    fn id(&self) -> DisplayId {
42        DisplayId(self.x_screen_index as u32)
43    }
44
45    fn uuid(&self) -> Result<Uuid> {
46        Ok(self.uuid)
47    }
48
49    fn bounds(&self) -> Bounds<Pixels> {
50        self.bounds
51    }
52}