Skip to main content

smithay_client_toolkit/
background_effect.rs

1use wayland_client::{
2    globals::GlobalList, protocol::wl_surface, Connection, Dispatch, QueueHandle, WEnum,
3};
4use wayland_protocols::ext::background_effect::v1::client::{
5    ext_background_effect_manager_v1, ext_background_effect_surface_v1,
6};
7
8use crate::{dispatch2::Dispatch2, error::GlobalError, globals::GlobalData, registry::GlobalProxy};
9
10#[derive(Debug)]
11pub struct BackgroundEffectState {
12    manager: GlobalProxy<ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1>,
13    capabilities: Option<ext_background_effect_manager_v1::Capability>,
14}
15
16impl BackgroundEffectState {
17    pub fn new<D>(globals: &GlobalList, qh: &QueueHandle<D>) -> Self
18    where
19        D: Dispatch<ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1, GlobalData>
20            + 'static,
21    {
22        let manager = GlobalProxy::from(globals.bind(qh, 1..=1, GlobalData));
23        Self { manager, capabilities: None }
24    }
25
26    /// Capabilities advertised by the compositor.
27    ///
28    /// Returns `None` if the compositor has not yet advertised capabilities.
29    pub fn capabilities(&self) -> Option<ext_background_effect_manager_v1::Capability> {
30        self.capabilities
31    }
32
33    /// Get `ext_background_effect_surface_v1` for a given `wl_surface`.
34    ///
35    /// Returns error if `ext_background_effect_manager_v1` global is not present.
36    pub fn get_background_effect<D>(
37        &self,
38        surface: &wl_surface::WlSurface,
39        qh: &QueueHandle<D>,
40    ) -> Result<ext_background_effect_surface_v1::ExtBackgroundEffectSurfaceV1, GlobalError>
41    where
42        D: Dispatch<ext_background_effect_surface_v1::ExtBackgroundEffectSurfaceV1, GlobalData>
43            + 'static,
44    {
45        Ok(self.manager.get()?.get_background_effect(surface, qh, GlobalData))
46    }
47
48    /// The `ext_background_effect_manager_v1` global, if any.
49    pub fn ext_background_effect_manager_v1(
50        &self,
51    ) -> Result<&ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1, GlobalError> {
52        self.manager.get()
53    }
54}
55
56pub trait BackgroundEffectHandler {
57    fn background_effect_state(&mut self) -> &mut BackgroundEffectState;
58
59    /// Compositor has advertised background effect capabilities.
60    ///
61    /// Call [`BackgroundEffectState::capabilities`] to access capabilities.
62    fn update_capabilities(&mut self);
63}
64
65impl<D> Dispatch2<ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1, D> for GlobalData
66where
67    D: BackgroundEffectHandler,
68{
69    fn event(
70        &self,
71        data: &mut D,
72        _manager: &ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1,
73        event: ext_background_effect_manager_v1::Event,
74        _conn: &Connection,
75        _qh: &QueueHandle<D>,
76    ) {
77        match event {
78            ext_background_effect_manager_v1::Event::Capabilities { flags } => {
79                let flags = match flags {
80                    WEnum::Value(value) => value,
81                    WEnum::Unknown(value) => {
82                        ext_background_effect_manager_v1::Capability::from_bits_retain(value)
83                    }
84                };
85                data.background_effect_state().capabilities = Some(flags);
86                data.update_capabilities();
87            }
88            _ => unreachable!(),
89        }
90    }
91}
92
93impl<D> Dispatch2<ext_background_effect_surface_v1::ExtBackgroundEffectSurfaceV1, D>
94    for GlobalData
95{
96    fn event(
97        &self,
98        _data: &mut D,
99        _surface: &ext_background_effect_surface_v1::ExtBackgroundEffectSurfaceV1,
100        _event: ext_background_effect_surface_v1::Event,
101        _conn: &Connection,
102        _qh: &QueueHandle<D>,
103    ) {
104        unreachable!()
105    }
106}