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
266
267
//! Types related to `wl_output` handling

use std::sync::{Arc, Mutex};

use wayland_client::protocol::wl_output::{self, Event, RequestsTrait, WlOutput};
use wayland_client::protocol::wl_registry::{self, RequestsTrait as RegistryRequest};
use wayland_client::Proxy;

pub use wayland_client::protocol::wl_output::{Subpixel, Transform};

/// A possible mode for an output
#[derive(Copy, Clone, Debug)]
pub struct Mode {
    /// Number of pixels of this mode in format `(width, height)`
    ///
    /// for example `(1920, 1080)`
    pub dimensions: (i32, i32),
    /// Refresh rate for this mode, in mHz
    pub refresh_rate: i32,
    /// Whether this is the current mode for this output
    pub is_current: bool,
    /// Whether this is the preferred mode for this output
    pub is_preferred: bool,
}

#[derive(Clone, Debug)]
/// Compiled information about an output
pub struct OutputInfo {
    /// The model name of this output as advertised by the server
    pub model: String,
    /// The make name of this output as advertised by the server
    pub make: String,
    /// Location of the top-left corner of this output in compositor
    /// space
    ///
    /// Note that the compositor may decide to always report (0,0) if
    /// it decides clients are not allowed to know this information.
    pub location: (i32, i32),
    /// Physical dimensions of this output, in unspecified units
    pub physical_size: (i32, i32),
    /// The subpixel layout for this output
    pub subpixel: Subpixel,
    /// The current transformation applied to this output
    ///
    /// You can pre-render your buffers taking this information
    /// into account and advertising it via `wl_buffer.set_tranform`
    /// for better performances.
    pub transform: Transform,
    /// The scaling factor of this output
    ///
    /// Any buffer whose scaling factor does not match the one
    /// of the output it is displayed on will be rescaled accordingly.
    ///
    /// For example, a buffer of scaling factor 1 will be doubled in
    /// size if the output scaling factor is 2.
    pub scale_factor: i32,
    /// Possible modes for an output
    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)>,
    pending: 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,
            // trying to merge a non-existing output ?
            // well, might be some very bad luck of an
            // output being concurrently destroyed at the bad time ?
            None => {
                // clean stale state
                self.pending.retain(|&(ref o, _)| o.is_alive());
                return;
            }
        };
        // slow, but could be improved with Vec::drain_filter
        // see https://github.com/rust-lang/rust/issues/43244
        // this vec should be pretty small at all times anyway
        while let Some(idx) = self.pending.iter().position(|&(ref o, _)| o.equals(output)) {
            let (_, event) = self.pending.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 => {
                    // should not happen
                    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)
                    {
                        // this mode already exists, update it
                        mode.is_preferred = flags.contains(wl_output::Mode::Preferred);
                        mode.is_current = flags.contains(wl_output::Mode::Current);
                        found = true;
                    }
                    if !found {
                        // otherwise, add it
                        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)]
/// An utility tracking the available outputs and their capabilities
pub struct OutputMgr {
    inner: Arc<Mutex<Inner>>,
}

impl OutputMgr {
    pub(crate) fn new() -> OutputMgr {
        OutputMgr {
            inner: Arc::new(Mutex::new(Inner {
                outputs: Vec::new(),
                pending: Vec::new(),
            })),
        }
    }

    pub(crate) fn new_output(
        &self,
        id: u32,
        version: u32,
        registry: &Proxy<wl_registry::WlRegistry>,
    ) {
        let inner = self.inner.clone();
        let output = registry
            .bind(version, id, |output| {
                output.implement(
                    move |event, output| {
                        let mut inner = inner.lock().unwrap();
                        if let Event::Done = event {
                            inner.merge(&output);
                        } else {
                            inner.pending.push((output.clone(), event));
                            if output.version() < 2 {
                                // in case of very old outputs, we can't treat the changes
                                // atomically as the Done event does not exist
                                inner.merge(&output);
                            }
                        }
                    },
                    (),
                )
            })
            .unwrap();

        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);
            // cleanup all remaining pending if any
            inner.pending.retain(|&(ref o, _)| !o.equals(&output));
            if output.version() >= 3 {
                output.release();
            }
        }
    }

    /// Access the information of a specific output from its global id
    ///
    /// If the requested output is not found (likely because it has been destroyed)
    /// the closure is not called and `None` is returned.
    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
        }
    }

    /// Access the information of a specific output
    ///
    /// If the requested output is not found (likely because it has been destroyed)
    /// the closure is not called and `None` is returned.
    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
        }
    }

    /// Access all output information
    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)
    }
}