use std::ffi::c_void;
use std::ptr::NonNull;
use crate::dpi::PhysicalSize;
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
use crate::monitor::MonitorHandle;
use crate::window::{Window, WindowAttributes};
pub use crate::window::Theme;
pub trait ActiveEventLoopExtWayland {
fn is_wayland(&self) -> bool;
}
impl ActiveEventLoopExtWayland for ActiveEventLoop {
#[inline]
fn is_wayland(&self) -> bool {
self.p.is_wayland()
}
}
pub trait EventLoopExtWayland {
fn is_wayland(&self) -> bool;
}
impl<T: 'static> EventLoopExtWayland for EventLoop<T> {
#[inline]
fn is_wayland(&self) -> bool {
self.event_loop.is_wayland()
}
}
pub trait EventLoopBuilderExtWayland {
fn with_wayland(&mut self) -> &mut Self;
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
}
impl<T> EventLoopBuilderExtWayland for EventLoopBuilder<T> {
#[inline]
fn with_wayland(&mut self) -> &mut Self {
self.platform_specific.forced_backend = Some(crate::platform_impl::Backend::Wayland);
self
}
#[inline]
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
self.platform_specific.any_thread = any_thread;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WaylandPresentation {
Presented {
clock_id: u32,
timestamp: std::time::Duration,
refresh: std::time::Duration,
sequence: u64,
flags: u32,
},
Discarded,
}
impl WaylandPresentation {
#[doc(hidden)]
pub fn from_protocol(
clock_id: Option<u32>,
time: (u32, u32, u32),
refresh: u32,
sequence: (u32, u32),
flags: Option<u32>,
) -> Self {
match (clock_id, flags) {
(Some(clock_id), Some(flags)) if time.2 < 1_000_000_000 => Self::Presented {
clock_id,
timestamp: std::time::Duration::new(
(u64::from(time.0) << 32) | u64::from(time.1),
time.2,
),
refresh: std::time::Duration::from_nanos(u64::from(refresh)),
sequence: (u64::from(sequence.0) << 32) | u64::from(sequence.1),
flags,
},
_ => Self::Discarded,
}
}
}
pub type WaylandPresentationObserver = Box<dyn FnOnce(WaylandPresentation) + Send + 'static>;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ImeContentType {
pub hint: u32,
pub purpose: u32,
}
pub trait WindowExtWayland {
fn set_ime_content_type(&self, content: Option<ImeContentType>);
fn private_text_input(&self, snapshot: Option<[u8; 32]>) -> bool;
fn acknowledge_private_text_input(&self, serial: u32, outcome: u32);
fn present_frame(
&self,
size: PhysicalSize<u32>,
observer: Option<WaylandPresentationObserver>,
present: impl FnOnce(),
) -> bool;
fn crops_frames(&self) -> bool;
fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;
fn start_drag(&self, offers: Vec<(String, DragData)>) -> bool;
fn server_side_decorations(&self) -> Option<bool>;
fn client_buffer_formats(&self) -> Vec<(u32, u64)>;
#[cfg(wayland_platform)]
fn create_client_buffer(
&self,
source: ClientBufferSource<'_>,
on_release: Box<dyn Fn() + Send + Sync>,
) -> Option<ClientBuffer>;
#[cfg(wayland_platform)]
fn attach_client_buffer(
&self,
buffer: &ClientBuffer,
damage: &[(i32, i32, i32, i32)],
observer: Option<WaylandPresentationObserver>,
) -> bool;
fn is_session_lock_up(&self) -> bool;
fn unlock_session(&self);
}
#[cfg(wayland_platform)]
pub use crate::platform_impl::wayland::{
Anchor, ClientBuffer, ClientBufferSource, DragData, KeyboardInteractivity, Layer,
LayerSurfaceAttributes,
};
impl WindowExtWayland for Window {
fn set_ime_content_type(&self, content: Option<ImeContentType>) {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => (),
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.set_ime_content_type(content),
}
}
fn private_text_input(&self, snapshot: Option<[u8; 32]>) -> bool {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => false,
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.private_text_input(snapshot),
}
}
fn acknowledge_private_text_input(&self, serial: u32, outcome: u32) {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => (),
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => {
window.acknowledge_private_text_input(serial, outcome)
}
}
}
fn present_frame(
&self,
size: PhysicalSize<u32>,
observer: Option<WaylandPresentationObserver>,
present: impl FnOnce(),
) -> bool {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => {
let _ = (size, observer);
present();
false
}
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => {
window.present_frame(size, observer, present)
}
}
}
fn crops_frames(&self) -> bool {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => false,
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.crops_frames(),
}
}
#[inline]
fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
#[allow(clippy::single_match)]
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => None,
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.xdg_toplevel(),
}
}
fn is_session_lock_up(&self) -> bool {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => false,
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.is_session_lock_up(),
}
}
fn unlock_session(&self) {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => (),
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.unlock_session(),
}
}
#[inline]
fn start_drag(&self, offers: Vec<(String, DragData)>) -> bool {
#[allow(clippy::single_match)]
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => false,
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.start_drag(offers),
}
}
#[inline]
fn server_side_decorations(&self) -> Option<bool> {
#[allow(clippy::single_match)]
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => Some(true),
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.server_side_decorations(),
}
}
fn client_buffer_formats(&self) -> Vec<(u32, u64)> {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => Vec::new(),
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.client_buffer_formats(),
}
}
#[cfg(wayland_platform)]
fn create_client_buffer(
&self,
source: ClientBufferSource<'_>,
on_release: Box<dyn Fn() + Send + Sync>,
) -> Option<ClientBuffer> {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => None,
crate::platform_impl::Window::Wayland(window) => {
window.create_client_buffer(source, on_release)
}
}
}
#[cfg(wayland_platform)]
fn attach_client_buffer(
&self,
buffer: &ClientBuffer,
damage: &[(i32, i32, i32, i32)],
observer: Option<WaylandPresentationObserver>,
) -> bool {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => false,
crate::platform_impl::Window::Wayland(window) => {
window.attach_client_buffer(buffer, damage, observer)
}
}
}
}
pub fn delivered_input_serial() -> Option<u32> {
#[cfg(wayland_platform)]
{
crate::platform_impl::wayland::delivered_input_serial()
}
#[cfg(not(wayland_platform))]
{
None
}
}
pub trait KeyEventExtWayland {
fn wayland_serial_time(&self) -> Option<(u32, u32)>;
}
impl KeyEventExtWayland for crate::event::KeyEvent {
fn wayland_serial_time(&self) -> Option<(u32, u32)> {
self.platform_specific.wayland_stamp
}
}
pub trait WindowAttributesExtWayland {
fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
fn with_layer_surface(self, attributes: LayerSurfaceAttributes) -> Self;
fn with_session_lock(self) -> Self;
}
impl WindowAttributesExtWayland for WindowAttributes {
#[inline]
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
self.platform_specific.name = Some(crate::platform_impl::ApplicationName::new(
general.into(),
instance.into(),
));
self
}
#[inline]
fn with_layer_surface(mut self, attributes: LayerSurfaceAttributes) -> Self {
self.platform_specific.layer = Some(attributes);
self
}
#[inline]
fn with_session_lock(mut self) -> Self {
self.platform_specific.session_lock = true;
self
}
}
pub trait MonitorHandleExtWayland {
fn native_id(&self) -> u32;
fn physical_size_mm(&self) -> Option<(u32, u32)>;
}
impl MonitorHandleExtWayland for MonitorHandle {
#[inline]
fn native_id(&self) -> u32 {
self.inner.native_identifier()
}
#[inline]
fn physical_size_mm(&self) -> Option<(u32, u32)> {
self.inner.physical_size_mm()
}
}
#[doc(hidden)]
#[cfg(wayland_platform)]
pub use crate::platform_impl::wayland::private_text_input_protocol;