1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! Helpers for listing and bindings globals

use std::ops::Range;

use crate::{protocol::wl_registry, Connection, DelegateDispatch, Dispatch, Proxy, QueueHandle};

/// Description of an advertized global
#[derive(Debug)]
pub struct GlobalDescription {
    /// identifier of this global
    pub name: u32,
    /// interface name
    pub interface: String,
    /// advertized version
    pub version: u32,
}

/// A helper to retrieve a list of globals and bind them
///
/// The `GlobalList` can be used as a [`Dispatch`](crate::Dispatch) target for the `wl_registry`. It
/// maintains a list of globals advertized by the compositor, and provides a way to bind according to
/// specified version requirements. It is an easy way to ensure at startup that the server advertized
/// all the globals your app needs, and bind them all at once.
#[derive(Debug)]
pub struct GlobalList {
    globals: Vec<GlobalDescription>,
}

impl<D> DelegateDispatch<wl_registry::WlRegistry, (), D> for GlobalList
where
    D: Dispatch<wl_registry::WlRegistry, ()> + AsMut<GlobalList>,
{
    fn event(
        handle: &mut D,
        _: &wl_registry::WlRegistry,
        event: wl_registry::Event,
        _: &(),
        _: &Connection,
        _: &crate::QueueHandle<D>,
    ) {
        let me = handle.as_mut();
        match event {
            wl_registry::Event::Global { name, interface, version } => {
                me.globals.push(GlobalDescription { name, interface, version });
            }
            wl_registry::Event::GlobalRemove { name } => {
                me.globals.retain(|desc| desc.name != name);
            }
        }
    }
}

impl AsMut<GlobalList> for GlobalList {
    fn as_mut(&mut self) -> &mut GlobalList {
        self
    }
}

impl Dispatch<wl_registry::WlRegistry, ()> for GlobalList {
    #[inline]
    fn event(
        &mut self,
        proxy: &wl_registry::WlRegistry,
        event: wl_registry::Event,
        data: &(),
        conn: &Connection,
        qhandle: &QueueHandle<Self>,
    ) {
        <Self as DelegateDispatch<wl_registry::WlRegistry, (), Self>>::event(
            self, proxy, event, data, conn, qhandle,
        )
    }
}

impl Default for GlobalList {
    fn default() -> Self {
        Self::new()
    }
}

impl GlobalList {
    /// Create a new `GLobalList`
    pub fn new() -> Self {
        Self { globals: Vec::new() }
    }

    /// Access the list of currently advertized globals
    pub fn list(&self) -> &[GlobalDescription] {
        &self.globals
    }

    /// Bind a global
    ///
    /// You can specify the requested interface as type parameter, and the version range. You
    /// also need to provide the user data value that will be set for the newly created object.
    pub fn bind<I: Proxy + 'static, U: Send + Sync + 'static, D: Dispatch<I, U> + 'static>(
        &self,
        qh: &QueueHandle<D>,
        registry: &wl_registry::WlRegistry,
        version: Range<u32>,
        user_data: U,
    ) -> Result<I, BindError> {
        for desc in &self.globals {
            if desc.interface != I::interface().name {
                continue;
            }

            if version.contains(&desc.version) {
                return Ok(registry
                    .bind::<I, U, D>(desc.name, desc.version, qh, user_data)
                    .expect("invalid wl_registry"));
            } else {
                return Err(BindError::WrongVersion {
                    interface: I::interface().name,
                    requested: version,
                    got: desc.version,
                });
            }
        }

        Err(BindError::MissingGlobal { interface: I::interface().name })
    }
}

/// Error when trying to bind a global
#[derive(Debug, thiserror::Error)]
pub enum BindError {
    /// The requested global was not advertized by the server
    #[error("Requested global was not advertized by the server: {interface}")]
    MissingGlobal {
        /// The requested interface
        interface: &'static str,
    },
    /// The version advertized by the server did not fit in the requested range
    #[error("Global {interface} has version {got}, which is outside of the requested range ({requested:?})")]
    WrongVersion {
        /// The requested interface
        interface: &'static str,
        /// The requested version range
        requested: Range<u32>,
        /// The advertized version
        got: u32,
    },
}