Files
ab_glyph_rasterizer
adler
adler32
andrew
bitflags
bytemuck
byteorder
calloop
cfg_if
color_quant
crc32fast
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
deflate
dlib
downcast_rs
draw_state
either
event_loop
float
fnv
gfx
gfx_core
gfx_device_gl
gfx_gl
gfx_graphics
gfx_texture
gif
gl
glutin
glutin_egl_sys
glutin_glx_sys
glutin_window
graphics
graphics_api_version
image
input
instant
interpolation
iovec
jpeg_decoder
lazy_static
lazycell
libc
libloading
lock_api
log
maybe_uninit
memchr
memmap2
memoffset
miniz_oxide
mio
mio_extras
net2
nix
nom
num_cpus
num_integer
num_iter
num_rational
num_traits
once_cell
osmesa_sys
owned_ttf_parser
parking_lot
parking_lot_core
percent_encoding
piston
piston_window
png
proc_macro2
quote
raw_window_handle
rayon
rayon_core
read_color
rusttype
same_file
scoped_threadpool
scoped_tls
scopeguard
serde
serde_derive
shader_version
shaders_graphics2d
colored
textured
textured_color
shared_library
slab
smallvec
smithay_client_toolkit
spin_sleep
syn
texture
tiff
ttf_parser
unicode_xid
vecmath
viewport
walkdir
wayland_client
wayland_commons
wayland_cursor
wayland_egl
wayland_protocols
wayland_sys
weezl
window
winit
x11_dl
xcursor
xdg
xml
  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
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

use sctk::reexports::client::protocol::wl_output::WlOutput;
use sctk::reexports::client::Display;

use sctk::environment::Environment;
use sctk::output::OutputStatusListener;

use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode};
use crate::platform_impl::platform::{
    MonitorHandle as PlatformMonitorHandle, VideoMode as PlatformVideoMode,
};

use super::env::WinitEnv;
use super::event_loop::EventLoopWindowTarget;

/// Output manager.
pub struct OutputManager {
    /// A handle that actually performs all operations on outputs.
    handle: OutputManagerHandle,

    _output_listener: OutputStatusListener,
}

impl OutputManager {
    pub fn new(env: &Environment<WinitEnv>) -> Self {
        let handle = OutputManagerHandle::new();

        // Handle existing outputs.
        for output in env.get_all_outputs() {
            match sctk::output::with_output_info(&output, |info| info.obsolete) {
                Some(false) => (),
                // The output is obsolete or we've failed to access its data, skipping.
                _ => continue,
            }

            // The output is present and unusable, add it to the output manager manager.
            handle.add_output(output);
        }

        let handle_for_listener = handle.clone();

        let output_listener = env.listen_for_outputs(move |output, info, _| {
            if info.obsolete {
                handle_for_listener.remove_output(output)
            } else {
                handle_for_listener.add_output(output)
            }
        });

        Self {
            handle,
            _output_listener: output_listener,
        }
    }

    pub fn handle(&self) -> OutputManagerHandle {
        self.handle.clone()
    }
}

/// A handle to output manager.
#[derive(Debug, Clone)]
pub struct OutputManagerHandle {
    outputs: Arc<Mutex<VecDeque<MonitorHandle>>>,
}

impl OutputManagerHandle {
    fn new() -> Self {
        let outputs = Arc::new(Mutex::new(VecDeque::new()));
        Self { outputs }
    }

    /// Handle addition of the output.
    fn add_output(&self, output: WlOutput) {
        let mut outputs = self.outputs.lock().unwrap();
        let position = outputs.iter().position(|handle| handle.proxy == output);
        if position.is_none() {
            outputs.push_back(MonitorHandle::new(output));
        }
    }

    /// Handle removal of the output.
    fn remove_output(&self, output: WlOutput) {
        let mut outputs = self.outputs.lock().unwrap();
        let position = outputs.iter().position(|handle| handle.proxy == output);
        if let Some(position) = position {
            outputs.remove(position);
        }
    }

    /// Get all observed outputs.
    pub fn available_outputs(&self) -> VecDeque<MonitorHandle> {
        self.outputs.lock().unwrap().clone()
    }
}

#[derive(Clone, Debug)]
pub struct MonitorHandle {
    pub(crate) proxy: WlOutput,
}

impl PartialEq for MonitorHandle {
    fn eq(&self, other: &Self) -> bool {
        self.native_identifier() == other.native_identifier()
    }
}

impl Eq for MonitorHandle {}

impl PartialOrd for MonitorHandle {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(&other))
    }
}

impl Ord for MonitorHandle {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.native_identifier().cmp(&other.native_identifier())
    }
}

impl std::hash::Hash for MonitorHandle {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.native_identifier().hash(state);
    }
}

impl MonitorHandle {
    #[inline]
    pub(crate) fn new(proxy: WlOutput) -> Self {
        Self { proxy }
    }

    #[inline]
    pub fn name(&self) -> Option<String> {
        sctk::output::with_output_info(&self.proxy, |info| {
            format!("{} ({})", info.model, info.make)
        })
    }

    #[inline]
    pub fn native_identifier(&self) -> u32 {
        sctk::output::with_output_info(&self.proxy, |info| info.id).unwrap_or(0)
    }

    #[inline]
    pub fn size(&self) -> PhysicalSize<u32> {
        match sctk::output::with_output_info(&self.proxy, |info| {
            info.modes
                .iter()
                .find(|mode| mode.is_current)
                .map(|mode| mode.dimensions)
        }) {
            Some(Some((w, h))) => (w as u32, h as u32),
            _ => (0, 0),
        }
        .into()
    }

    #[inline]
    pub fn position(&self) -> PhysicalPosition<i32> {
        sctk::output::with_output_info(&self.proxy, |info| info.location)
            .unwrap_or((0, 0))
            .into()
    }

    #[inline]
    pub fn scale_factor(&self) -> i32 {
        sctk::output::with_output_info(&self.proxy, |info| info.scale_factor).unwrap_or(1)
    }

    #[inline]
    pub fn video_modes(&self) -> impl Iterator<Item = RootVideoMode> {
        let modes = sctk::output::with_output_info(&self.proxy, |info| info.modes.clone())
            .unwrap_or_else(Vec::new);

        let monitor = self.clone();

        modes.into_iter().map(move |mode| RootVideoMode {
            video_mode: PlatformVideoMode::Wayland(VideoMode {
                size: (mode.dimensions.0 as u32, mode.dimensions.1 as u32).into(),
                refresh_rate: (mode.refresh_rate as f32 / 1000.0).round() as u16,
                bit_depth: 32,
                monitor: monitor.clone(),
            }),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VideoMode {
    pub(crate) size: PhysicalSize<u32>,
    pub(crate) bit_depth: u16,
    pub(crate) refresh_rate: u16,
    pub(crate) monitor: MonitorHandle,
}

impl VideoMode {
    #[inline]
    pub fn size(&self) -> PhysicalSize<u32> {
        self.size
    }

    #[inline]
    pub fn bit_depth(&self) -> u16 {
        self.bit_depth
    }

    #[inline]
    pub fn refresh_rate(&self) -> u16 {
        self.refresh_rate
    }

    pub fn monitor(&self) -> RootMonitorHandle {
        RootMonitorHandle {
            inner: PlatformMonitorHandle::Wayland(self.monitor.clone()),
        }
    }
}

impl<T> EventLoopWindowTarget<T> {
    #[inline]
    pub fn display(&self) -> &Display {
        &self.display
    }

    #[inline]
    pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
        self.output_manager.handle.available_outputs()
    }

    #[inline]
    pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
        // There's no primary monitor on Wayland.
        None
    }
}