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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::io;
use std::sync::{Arc, Mutex};
use shell::{create_shell_surface, Event, ShellSurface};
use surface::{create_surface, SurfaceUserData};
use wayland_client::protocol::{
wl_compositor, wl_data_device_manager, wl_display, wl_registry, wl_shell, wl_shm,
wl_subcompositor, wl_surface,
};
use wayland_client::{EventQueue, GlobalEvent, GlobalManager, NewProxy};
use wayland_protocols::unstable::xdg_decoration::v1::client::zxdg_decoration_manager_v1;
use wayland_protocols::unstable::xdg_shell::v6::client::zxdg_shell_v6;
use wayland_protocols::xdg_shell::client::xdg_wm_base;
pub enum Shell {
Xdg(xdg_wm_base::XdgWmBase),
Zxdg(zxdg_shell_v6::ZxdgShellV6),
Wl(wl_shell::WlShell),
}
impl Shell {
pub fn needs_configure(&self) -> bool {
match *self {
Shell::Xdg(_) => true,
Shell::Zxdg(_) => true,
Shell::Wl(_) => false,
}
}
}
pub struct Environment {
pub manager: GlobalManager,
pub compositor: wl_compositor::WlCompositor,
pub subcompositor: wl_subcompositor::WlSubcompositor,
pub shell: Shell,
pub shm: wl_shm::WlShm,
pub data_device_manager: wl_data_device_manager::WlDataDeviceManager,
pub outputs: ::output::OutputMgr,
pub decorations_mgr: Option<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>,
shm_formats: Arc<Mutex<Vec<wl_shm::Format>>>,
surfaces: Arc<Mutex<Vec<wl_surface::WlSurface>>>,
}
impl Environment {
pub fn from_display(
display: &wl_display::WlDisplay,
evq: &mut EventQueue,
) -> io::Result<Environment> {
Environment::from_display_with_cb(display, evq, |_, _| {})
}
pub fn from_display_with_cb<Impl>(
display: &wl_display::WlDisplay,
evq: &mut EventQueue,
mut cb: Impl,
) -> io::Result<Environment>
where
Impl: FnMut(GlobalEvent, wl_registry::WlRegistry) + 'static,
{
let outputs = ::output::OutputMgr::new();
let outputs2 = outputs.clone();
let surfaces: Arc<Mutex<Vec<wl_surface::WlSurface>>> = Arc::new(Mutex::new(Vec::new()));
let surfaces2 = surfaces.clone();
let display_wrapper = display
.as_ref()
.make_wrapper(&evq.get_token())
.unwrap()
.into();
let manager = GlobalManager::new_with_cb(&display_wrapper, move |event, registry| {
match event {
GlobalEvent::New {
id,
ref interface,
version,
} => {
if let "wl_output" = &interface[..] {
outputs2.new_output(id, version, ®istry)
}
}
GlobalEvent::Removed { id, ref interface } => {
if let "wl_output" = &interface[..] {
let output = outputs2
.find_id(id, |output, _info| output.clone())
.unwrap();
for surface in &*surfaces2.lock().unwrap() {
surface
.as_ref()
.user_data::<Mutex<SurfaceUserData>>()
.expect("Surface was not created with create_surface.")
.lock()
.unwrap()
.leave(&output, surface.clone())
}
outputs2.output_removed(id)
}
}
}
cb(event, registry);
});
evq.sync_roundtrip()?;
evq.sync_roundtrip()?;
let compositor = manager
.instantiate_range(1, 4, NewProxy::implement_dummy)
.expect("Server didn't advertise `wl_compositor`?!");
let subcompositor = manager
.instantiate_range(1, 1, NewProxy::implement_dummy)
.expect("Server didn't advertise `wl_subcompositor`?!");
let shm_formats = Arc::new(Mutex::new(Vec::new()));
let shm_formats2 = shm_formats.clone();
let shm = manager
.instantiate_range(1, 1, |shm| {
shm.implement_closure(
move |evt, _| {
if let wl_shm::Event::Format { format } = evt {
shm_formats2.lock().unwrap().push(format);
}
},
(),
)
})
.expect("Server didn't advertise `wl_shm`?!");
let data_device_manager = manager
.instantiate_range(1, 3, NewProxy::implement_dummy)
.expect("Server didn't advertise `wl_data_device_manager`?!");
let shell = if let Ok(wm_base) =
manager.instantiate_exact(1, |wm_base: NewProxy<xdg_wm_base::XdgWmBase>| {
wm_base.implement_closure(
|evt, shell| {
if let xdg_wm_base::Event::Ping { serial } = evt {
shell.pong(serial)
}
},
(),
)
}) {
Shell::Xdg(wm_base)
} else if let Ok(xdg_shell) =
manager.instantiate_exact(1, |xdg_shell: NewProxy<zxdg_shell_v6::ZxdgShellV6>| {
xdg_shell.implement_closure(
|evt, shell| {
if let zxdg_shell_v6::Event::Ping { serial } = evt {
shell.pong(serial);
}
},
(),
)
})
{
Shell::Zxdg(xdg_shell)
} else if let Ok(wl_shell) = manager.instantiate_exact(1, NewProxy::implement_dummy) {
Shell::Wl(wl_shell)
} else {
panic!("Server didn't advertise neither `xdg_wm_base` nor `wl_shell`?!");
};
let decorations_mgr = if let Shell::Xdg(_) = shell {
manager.instantiate_exact(1, NewProxy::implement_dummy).ok()
} else {
None
};
evq.sync_roundtrip()?;
Ok(Environment {
manager,
compositor,
subcompositor,
shell,
shm,
shm_formats,
data_device_manager,
decorations_mgr,
outputs,
surfaces,
})
}
pub fn shm_formats(&self) -> Vec<wl_shm::Format> {
self.shm_formats.lock().unwrap().clone()
}
pub fn create_surface<F>(&self, dpi_change: F) -> wl_surface::WlSurface
where
F: FnMut(i32, wl_surface::WlSurface) + Send + 'static,
{
let surface = create_surface(&self, Box::new(dpi_change));
self.surfaces.lock().unwrap().push(surface.clone());
surface
}
pub fn create_shell_surface<Impl>(
&self,
surface: &wl_surface::WlSurface,
shell_impl: Impl,
) -> Box<ShellSurface>
where
Impl: FnMut(Event) + Send + 'static,
{
create_shell_surface(&self.shell, surface, shell_impl)
}
}