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
145
146
147
148
149
150
151
152
153
154
use std::ops::Range;
use crate::{
protocol::wl_registry, ConnectionHandle, DelegateDispatch, DelegateDispatchBase, Dispatch,
Proxy, QueueHandle,
};
#[derive(Debug)]
pub struct GlobalDescription {
pub name: u32,
pub interface: String,
pub version: u32,
}
#[derive(Debug)]
pub struct GlobalList {
globals: Vec<GlobalDescription>,
}
impl DelegateDispatchBase<wl_registry::WlRegistry> for GlobalList {
type UserData = ();
}
impl<D> DelegateDispatch<wl_registry::WlRegistry, D> for GlobalList
where
D: Dispatch<wl_registry::WlRegistry, UserData = ()> + AsMut<GlobalList>,
{
fn event(
handle: &mut D,
_: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &mut crate::ConnectionHandle,
_: &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 {
type UserData = ();
#[inline]
fn event(
&mut self,
proxy: &wl_registry::WlRegistry,
event: wl_registry::Event,
data: &Self::UserData,
connhandle: &mut ConnectionHandle,
qhandle: &QueueHandle<Self>,
) {
<Self as DelegateDispatch<wl_registry::WlRegistry, Self>>::event(
self, proxy, event, data, connhandle, qhandle,
)
}
}
impl Default for GlobalList {
fn default() -> Self {
GlobalList::new()
}
}
impl GlobalList {
pub fn new() -> GlobalList {
GlobalList { globals: Vec::new() }
}
pub fn list(&self) -> &[GlobalDescription] {
&self.globals
}
pub fn bind<I: Proxy + 'static, D: Dispatch<I> + 'static>(
&self,
conn: &mut ConnectionHandle<'_>,
qh: &QueueHandle<D>,
registry: &wl_registry::WlRegistry,
version: Range<u32>,
user_data: <D as Dispatch<I>>::UserData,
) -> 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, D>(conn, 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 })
}
}
#[derive(Debug, thiserror::Error)]
pub enum BindError {
#[error("Requested global was not advertized by the server: {interface}")]
MissingGlobal {
interface: &'static str,
},
#[error("Global {interface} has version {got}, which is outside of the requested range ({requested:?})")]
WrongVersion {
interface: &'static str,
requested: Range<u32>,
got: u32,
},
}