pub mod clock;
mod damage;
mod device;
pub mod keys;
pub mod text_input;
pub use device::device_numbers;
pub use damage::DamageRect;
#[cfg(all(feature = "gpu-damage", not(target_arch = "wasm32")))]
pub use damage::set_next_present_damage;
use std::time::Duration;
use winit::{event::WindowEvent, window::Window};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Feedback {
Presented {
clock_id: u32,
timestamp: Duration,
refresh: Duration,
sequence: u64,
flags: u32,
},
Discarded,
}
pub const fn supported() -> bool {
cfg!(target_os = "linux")
}
pub fn key_stamp(event: &WindowEvent) -> Option<(u32, u32)> {
#[cfg(target_os = "linux")]
if let WindowEvent::KeyboardInput { event, .. } = event {
use winit::platform::wayland::KeyEventExtWayland;
return event.wayland_serial_time();
}
let _ = event;
None
}
pub fn desktop_draws_frame(window: &Window) -> Option<bool> {
#[cfg(target_os = "linux")]
{
use winit::platform::wayland::WindowExtWayland as _;
window.server_side_decorations()
}
#[cfg(not(target_os = "linux"))]
{
let _ = window;
None
}
}
pub fn crops_frames(window: &Window) -> bool {
#[cfg(target_os = "linux")]
{
use winit::platform::wayland::WindowExtWayland as _;
window.crops_frames()
}
#[cfg(not(target_os = "linux"))]
{
let _ = window;
false
}
}
pub fn present(
window: &Window,
size: (u32, u32),
observer: Option<Box<dyn FnOnce(Feedback) + Send>>,
present: impl FnOnce(),
) -> bool {
window.pre_present_notify();
#[cfg(target_os = "linux")]
{
use winit::platform::wayland::{WaylandPresentation, WindowExtWayland};
window.present_frame(
winit::dpi::PhysicalSize::new(size.0, size.1),
observer.map(
|observer| -> winit::platform::wayland::WaylandPresentationObserver {
Box::new(move |result| {
observer(match result {
WaylandPresentation::Presented {
clock_id,
timestamp,
refresh,
sequence,
flags,
} => Feedback::Presented {
clock_id,
timestamp,
refresh,
sequence,
flags,
},
WaylandPresentation::Discarded => Feedback::Discarded,
});
})
},
),
present,
)
}
#[cfg(not(target_os = "linux"))]
{
let _ = (window, size, observer);
present();
false
}
}
#[cfg(test)]
#[cfg(target_os = "linux")]
mod wayland_tests {
use std::time::Duration;
use winit::platform::wayland::WaylandPresentation;
#[test]
fn standard_wayland_dispatch_decoder_preserves_kernel_clock_and_sequence() {
assert_eq!(
WaylandPresentation::from_protocol(
Some(1),
(2, 7, 123_456),
16_666_667,
(3, 9),
Some(7)
),
WaylandPresentation::Presented {
clock_id: 1,
timestamp: Duration::new((2u64 << 32) | 7, 123_456),
refresh: Duration::from_nanos(16_666_667),
sequence: (3u64 << 32) | 9,
flags: 7,
}
);
for (clock, nanos, flags) in [
(None, 0, Some(7)),
(Some(1), 1_000_000_000, Some(7)),
(Some(1), 0, None),
] {
assert_eq!(
WaylandPresentation::from_protocol(clock, (0, 1, nanos), 16_666_667, (0, 1), flags),
WaylandPresentation::Discarded
);
}
}
}