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
use std::sync::{Arc, Mutex};
use wayland_client::protocol::wl_output::{self, Event, RequestsTrait, WlOutput};
use wayland_client::{NewProxy, Proxy};
pub use wayland_client::protocol::wl_output::{Subpixel, Transform};
#[derive(Copy, Clone, Debug)]
pub struct Mode {
pub dimensions: (i32, i32),
pub refresh_rate: i32,
pub is_current: bool,
pub is_preferred: bool,
}
#[derive(Clone, Debug)]
pub struct OutputInfo {
pub model: String,
pub make: String,
pub location: (i32, i32),
pub physical_size: (i32, i32),
pub subpixel: Subpixel,
pub transform: Transform,
pub scale_factor: i32,
pub modes: Vec<Mode>,
}
impl OutputInfo {
fn new() -> OutputInfo {
OutputInfo {
model: String::new(),
make: String::new(),
location: (0, 0),
physical_size: (0, 0),
subpixel: Subpixel::Unknown,
transform: Transform::Normal,
scale_factor: 1,
modes: Vec::new(),
}
}
}
struct Inner {
outputs: Vec<(u32, Proxy<WlOutput>, OutputInfo)>,
pendings: Vec<(Proxy<WlOutput>, Event)>,
}
impl Inner {
fn merge(&mut self, output: &Proxy<WlOutput>) {
let info = match self.outputs
.iter_mut()
.find(|&&mut (_, ref o, _)| o.equals(output))
{
Some(&mut (_, _, ref mut info)) => info,
None => {
self.pendings.retain(|&(ref o, _)| o.is_alive());
return;
}
};
while let Some(idx) = self.pendings
.iter()
.position(|&(ref o, _)| o.equals(output))
{
let (_, event) = self.pendings.swap_remove(idx);
match event {
Event::Geometry {
x,
y,
physical_width,
physical_height,
subpixel,
model,
make,
transform,
} => {
info.location = (x, y);
info.physical_size = (physical_width, physical_height);
info.subpixel = subpixel;
info.transform = transform;
info.model = model;
info.make = make;
}
Event::Scale { factor } => {
info.scale_factor = factor;
}
Event::Done => {
unreachable!();
}
Event::Mode {
width,
height,
refresh,
flags,
} => {
let mut found = false;
if let Some(mode) = info.modes
.iter_mut()
.find(|m| m.dimensions == (width, height) && m.refresh_rate == refresh)
{
mode.is_preferred = flags.contains(wl_output::Mode::Preferred);
mode.is_current = flags.contains(wl_output::Mode::Current);
found = true;
}
if !found {
info.modes.push(Mode {
dimensions: (width, height),
refresh_rate: refresh,
is_preferred: flags.contains(wl_output::Mode::Preferred),
is_current: flags.contains(wl_output::Mode::Current),
})
}
}
}
}
}
}
#[derive(Clone)]
pub struct OutputMgr {
inner: Arc<Mutex<Inner>>,
}
impl OutputMgr {
pub(crate) fn new() -> OutputMgr {
OutputMgr {
inner: Arc::new(Mutex::new(Inner {
outputs: Vec::new(),
pendings: Vec::new(),
})),
}
}
pub(crate) fn new_output(&self, id: u32, output: NewProxy<WlOutput>) {
let inner = self.inner.clone();
let output = output.implement(move |event, output| {
let mut inner = inner.lock().unwrap();
if let Event::Done = event {
inner.merge(&output);
} else {
inner.pendings.push((output.clone(), event));
if output.version() < 2 {
inner.merge(&output);
}
}
});
self.inner
.lock()
.unwrap()
.outputs
.push((id, output, OutputInfo::new()));
}
pub(crate) fn output_removed(&self, id: u32) {
let mut inner = self.inner.lock().unwrap();
if let Some(idx) = inner.outputs.iter().position(|&(i, _, _)| i == id) {
let (_, output, _) = inner.outputs.swap_remove(idx);
inner.pendings.retain(|&(ref o, _)| !o.equals(&output));
if output.version() >= 3 {
output.release();
}
}
}
pub fn find_id<F, T>(&self, id: u32, f: F) -> Option<T>
where
F: FnOnce(&Proxy<wl_output::WlOutput>, &OutputInfo) -> T,
{
let inner = self.inner.lock().unwrap();
if let Some(&(_, ref proxy, ref info)) = inner.outputs.iter().find(|&&(i, _, _)| i == id) {
Some(f(proxy, info))
} else {
None
}
}
pub fn with_info<F, T>(&self, output: &Proxy<WlOutput>, f: F) -> Option<T>
where
F: FnOnce(u32, &OutputInfo) -> T,
{
let inner = self.inner.lock().unwrap();
if let Some(&(id, _, ref info)) = inner
.outputs
.iter()
.find(|&&(_, ref o, _)| o.equals(output))
{
Some(f(id, info))
} else {
None
}
}
pub fn with_all<F, T>(&self, f: F) -> T
where
F: FnOnce(&[(u32, Proxy<WlOutput>, OutputInfo)]) -> T,
{
let inner = self.inner.lock().unwrap();
f(&inner.outputs)
}
}