dim_screen/
surface.rs

1use log::debug;
2use smithay_client_toolkit::{
3    reexports::{
4        client::{protocol::wl_output::WlOutput, QueueHandle},
5        protocols::wp::viewporter::client::wp_viewport::WpViewport,
6    },
7    shell::{wlr_layer::LayerSurface, WaylandSurface},
8};
9
10use crate::{buffer::BufferType, consts::INIT_SIZE, DimData};
11
12pub struct DimSurface {
13    first_configure: bool,
14    width: u32,
15    height: u32,
16    buffer: BufferType,
17    viewport: WpViewport,
18    layer: LayerSurface,
19    output: WlOutput,
20}
21
22impl DimSurface {
23    pub fn new(
24        _qh: &QueueHandle<DimData>,
25        buffer: BufferType,
26        viewport: WpViewport,
27        layer: LayerSurface,
28        output: WlOutput,
29    ) -> Self {
30        Self {
31            first_configure: true,
32            width: INIT_SIZE,
33            height: INIT_SIZE,
34            buffer,
35            viewport,
36            layer,
37            output,
38        }
39    }
40
41    pub fn draw(&mut self, _qh: &QueueHandle<DimData>) {
42        debug!("Requesting draw");
43        if !self.first_configure {
44            // we only need to draw once as it is a static color
45            return;
46        }
47
48        let wl_buffer = match &self.buffer {
49            BufferType::Wl(wl_buffer) => wl_buffer,
50            BufferType::Shared(buffer) => buffer.wl_buffer(),
51        };
52
53        self.layer.wl_surface().attach(Some(wl_buffer), 0, 0);
54        self.layer.commit();
55
56        debug!("Drawn");
57    }
58
59    pub fn width(&self) -> u32 {
60        self.height
61    }
62
63    pub fn height(&self) -> u32 {
64        self.width
65    }
66
67    pub fn first_configure(&self) -> bool {
68        self.first_configure
69    }
70
71    pub fn output(&self) -> &WlOutput {
72        &self.output
73    }
74
75    pub fn layer(&self) -> &LayerSurface {
76        &self.layer
77    }
78
79    pub fn set_first_configure(&mut self, value: bool) {
80        self.first_configure = value;
81    }
82
83    pub fn set_size(&mut self, width: u32, height: u32) {
84        self.width = width;
85        self.height = height;
86    }
87
88    pub fn viewport_mut(&mut self) -> &mut WpViewport {
89        &mut self.viewport
90    }
91}
92
93impl Drop for DimSurface {
94    fn drop(&mut self) {
95        self.viewport.destroy();
96        if let BufferType::Wl(buffer) = &mut self.buffer {
97            buffer.destroy();
98        }
99    }
100}