2017-05-21 20:40:15 +00:00
|
|
|
//! Implementation of backend traits for types provided by `winit`
|
|
|
|
|
2020-04-15 11:01:01 +00:00
|
|
|
use crate::backend::egl::display::EGLDisplay;
|
|
|
|
use crate::backend::egl::get_proc_address;
|
2018-12-15 20:32:28 +00:00
|
|
|
use crate::backend::{
|
2020-04-29 22:24:35 +00:00
|
|
|
egl::{context::GlAttributes, native, EGLContext, EGLSurface, Error as EGLError, SurfaceCreationError},
|
2018-11-23 14:11:27 +00:00
|
|
|
graphics::{gl::GLGraphicsBackend, CursorBackend, PixelFormat, SwapBuffersError},
|
2018-10-04 22:37:43 +00:00
|
|
|
input::{
|
|
|
|
Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState, KeyboardKeyEvent,
|
|
|
|
MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent, PointerMotionAbsoluteEvent,
|
|
|
|
Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent, TouchMotionEvent, TouchSlot, TouchUpEvent,
|
|
|
|
UnusedEvent,
|
|
|
|
},
|
2018-09-24 22:32:09 +00:00
|
|
|
};
|
2017-10-08 14:18:56 +00:00
|
|
|
use nix::libc::c_void;
|
2018-11-23 14:11:27 +00:00
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefCell},
|
2020-04-12 20:35:24 +00:00
|
|
|
cmp,
|
2020-04-29 22:24:35 +00:00
|
|
|
convert::TryInto,
|
2018-11-23 14:11:27 +00:00
|
|
|
rc::Rc,
|
|
|
|
time::Instant,
|
|
|
|
};
|
2020-04-12 11:11:10 +00:00
|
|
|
use wayland_egl as wegl;
|
2018-04-18 07:58:32 +00:00
|
|
|
use wayland_server::Display;
|
2018-09-24 22:32:09 +00:00
|
|
|
use winit::{
|
2020-03-21 11:04:03 +00:00
|
|
|
dpi::{LogicalPosition, LogicalSize, PhysicalSize},
|
|
|
|
event::{
|
2020-04-10 15:01:49 +00:00
|
|
|
ElementState, Event, KeyboardInput, MouseButton as WinitMouseButton, MouseScrollDelta, Touch,
|
|
|
|
TouchPhase, WindowEvent,
|
2020-03-21 11:04:03 +00:00
|
|
|
},
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
platform::desktop::EventLoopExtDesktop,
|
|
|
|
window::{CursorIcon, Window as WinitWindow, WindowBuilder},
|
2018-09-24 22:32:09 +00:00
|
|
|
};
|
2017-09-13 20:51:35 +00:00
|
|
|
|
2020-04-15 11:01:01 +00:00
|
|
|
#[cfg(feature = "use_system_lib")]
|
2020-04-15 20:19:20 +00:00
|
|
|
use crate::backend::egl::{display::EGLBufferReader, EGLGraphicsBackend};
|
2020-04-15 11:01:01 +00:00
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Errors thrown by the `winit` backends
|
2020-04-12 20:35:24 +00:00
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// Failed to initialize a window
|
|
|
|
#[error("Failed to initialize a window")]
|
|
|
|
InitFailed(#[from] winit::error::OsError),
|
|
|
|
/// Context creation is not supported on the current window system
|
|
|
|
#[error("Context creation is not supported on the current window system")]
|
|
|
|
NotSupported,
|
|
|
|
/// EGL error
|
|
|
|
#[error("EGL error: {0}")]
|
|
|
|
EGL(#[from] EGLError),
|
2020-04-29 22:24:35 +00:00
|
|
|
/// Surface Creation failed
|
|
|
|
#[error("Surface creation failed: {0}")]
|
|
|
|
SurfaceCreationError(#[from] SurfaceCreationError<EGLError>)
|
2017-09-18 14:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 15:01:16 +00:00
|
|
|
enum Window {
|
|
|
|
Wayland {
|
2020-04-15 11:01:01 +00:00
|
|
|
display: EGLDisplay<native::Wayland, WinitWindow>,
|
|
|
|
context: EGLContext,
|
2017-12-21 15:01:16 +00:00
|
|
|
surface: EGLSurface<wegl::WlEglSurface>,
|
|
|
|
},
|
|
|
|
X11 {
|
2020-04-15 11:01:01 +00:00
|
|
|
display: EGLDisplay<native::X11, WinitWindow>,
|
|
|
|
context: EGLContext,
|
2017-12-21 15:01:16 +00:00
|
|
|
surface: EGLSurface<native::XlibWindow>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
2018-12-15 20:58:43 +00:00
|
|
|
fn window(&self) -> Ref<'_, WinitWindow> {
|
2018-06-28 09:33:49 +00:00
|
|
|
match *self {
|
2020-04-15 11:01:01 +00:00
|
|
|
Window::Wayland { ref display, .. } => display.borrow(),
|
|
|
|
Window::X11 { ref display, .. } => display.borrow(),
|
2017-12-21 15:01:16 +00:00
|
|
|
}
|
2017-09-18 14:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 15:01:16 +00:00
|
|
|
|
2018-10-13 10:36:00 +00:00
|
|
|
struct WindowSize {
|
2020-03-21 11:04:03 +00:00
|
|
|
physical_size: PhysicalSize<u32>,
|
|
|
|
scale_factor: f64,
|
2018-10-13 10:36:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 20:28:02 +00:00
|
|
|
/// Window with an active EGL Context created by `winit`. Implements the
|
2018-12-08 12:40:07 +00:00
|
|
|
/// [`EGLGraphicsBackend`] and [`GLGraphicsBackend`] graphics backend trait
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitGraphicsBackend {
|
|
|
|
window: Rc<Window>,
|
2018-10-13 10:36:00 +00:00
|
|
|
size: Rc<RefCell<WindowSize>>,
|
2017-06-04 21:11:26 +00:00
|
|
|
logger: ::slog::Logger,
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Abstracted event loop of a [`WinitWindow`] implementing the [`InputBackend`] trait
|
2017-05-18 20:28:02 +00:00
|
|
|
///
|
2018-12-08 12:40:07 +00:00
|
|
|
/// You need to call [`dispatch_new_events`](InputBackend::dispatch_new_events)
|
|
|
|
/// periodically to receive any events.
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitInputBackend {
|
2020-03-21 11:04:03 +00:00
|
|
|
events_loop: EventLoop<()>,
|
2018-12-15 20:58:43 +00:00
|
|
|
events_handler: Option<Box<dyn WinitEventsHandler>>,
|
2017-05-18 20:28:02 +00:00
|
|
|
window: Rc<Window>,
|
2018-03-17 15:15:23 +00:00
|
|
|
time: Instant,
|
2017-05-18 20:28:02 +00:00
|
|
|
key_counter: u32,
|
|
|
|
seat: Seat,
|
|
|
|
input_config: (),
|
2018-12-15 20:58:43 +00:00
|
|
|
handler: Option<Box<dyn InputHandler<WinitInputBackend> + 'static>>,
|
2017-06-04 21:11:26 +00:00
|
|
|
logger: ::slog::Logger,
|
2018-10-13 10:36:00 +00:00
|
|
|
size: Rc<RefCell<WindowSize>>,
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Create a new [`WinitGraphicsBackend`], which implements the [`EGLGraphicsBackend`]
|
|
|
|
/// and [`GLGraphicsBackend`] graphics backend trait and a corresponding [`WinitInputBackend`],
|
|
|
|
/// which implements the [`InputBackend`] trait
|
2020-04-12 20:35:24 +00:00
|
|
|
pub fn init<L>(logger: L) -> Result<(WinitGraphicsBackend, WinitInputBackend), Error>
|
2017-06-20 09:31:18 +00:00
|
|
|
where
|
|
|
|
L: Into<Option<::slog::Logger>>,
|
2017-06-04 21:11:26 +00:00
|
|
|
{
|
2017-06-20 09:31:18 +00:00
|
|
|
init_from_builder(
|
|
|
|
WindowBuilder::new()
|
2020-03-21 11:04:03 +00:00
|
|
|
.with_inner_size(LogicalSize::new(1280.0, 800.0))
|
2017-06-20 09:31:18 +00:00
|
|
|
.with_title("Smithay")
|
2020-03-21 11:04:03 +00:00
|
|
|
.with_visible(true),
|
2017-06-20 09:31:18 +00:00
|
|
|
logger,
|
|
|
|
)
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Create a new [`WinitGraphicsBackend`], which implements the [`EGLGraphicsBackend`]
|
|
|
|
/// and [`GLGraphicsBackend`] graphics backend trait, from a given [`WindowBuilder`]
|
|
|
|
/// struct and a corresponding [`WinitInputBackend`], which implements the [`InputBackend`] trait
|
2017-12-15 17:38:10 +00:00
|
|
|
pub fn init_from_builder<L>(
|
2018-04-18 07:58:32 +00:00
|
|
|
builder: WindowBuilder,
|
|
|
|
logger: L,
|
2020-04-12 20:35:24 +00:00
|
|
|
) -> Result<(WinitGraphicsBackend, WinitInputBackend), Error>
|
2017-06-20 09:31:18 +00:00
|
|
|
where
|
|
|
|
L: Into<Option<::slog::Logger>>,
|
2017-06-04 21:11:26 +00:00
|
|
|
{
|
2017-06-20 09:31:18 +00:00
|
|
|
init_from_builder_with_gl_attr(
|
|
|
|
builder,
|
|
|
|
GlAttributes {
|
|
|
|
version: None,
|
|
|
|
profile: None,
|
|
|
|
debug: cfg!(debug_assertions),
|
|
|
|
vsync: true,
|
|
|
|
},
|
|
|
|
logger,
|
|
|
|
)
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Create a new [`WinitGraphicsBackend`], which implements the [`EGLGraphicsBackend`]
|
|
|
|
/// and [`GLGraphicsBackend`] graphics backend trait, from a given [`WindowBuilder`]
|
|
|
|
/// struct, as well as given [`GlAttributes`] for further customization of the rendering pipeline and a
|
|
|
|
/// corresponding [`WinitInputBackend`], which implements the [`InputBackend`] trait.
|
2017-12-15 17:38:10 +00:00
|
|
|
pub fn init_from_builder_with_gl_attr<L>(
|
2018-04-18 07:58:32 +00:00
|
|
|
builder: WindowBuilder,
|
|
|
|
attributes: GlAttributes,
|
|
|
|
logger: L,
|
2020-04-12 20:35:24 +00:00
|
|
|
) -> Result<(WinitGraphicsBackend, WinitInputBackend), Error>
|
2017-06-20 09:31:18 +00:00
|
|
|
where
|
|
|
|
L: Into<Option<::slog::Logger>>,
|
2017-06-04 21:11:26 +00:00
|
|
|
{
|
2018-12-15 20:32:28 +00:00
|
|
|
let log = crate::slog_or_stdlog(logger).new(o!("smithay_module" => "backend_winit"));
|
2017-06-04 21:11:26 +00:00
|
|
|
info!(log, "Initializing a winit backend");
|
|
|
|
|
2020-03-21 11:04:03 +00:00
|
|
|
let events_loop = EventLoop::new();
|
2020-04-12 20:35:24 +00:00
|
|
|
let winit_window = builder.build(&events_loop).map_err(Error::InitFailed)?;
|
2020-03-21 11:04:03 +00:00
|
|
|
|
2017-06-04 21:11:26 +00:00
|
|
|
debug!(log, "Window created");
|
2017-05-18 20:28:02 +00:00
|
|
|
|
2017-12-28 14:28:15 +00:00
|
|
|
let reqs = Default::default();
|
2017-12-21 15:01:16 +00:00
|
|
|
let window = Rc::new(
|
|
|
|
if native::NativeDisplay::<native::Wayland>::is_backend(&winit_window) {
|
2020-04-15 11:01:01 +00:00
|
|
|
let display = EGLDisplay::<native::Wayland, WinitWindow>::new(winit_window, log.clone())?;
|
|
|
|
let context = display.create_context(attributes, reqs)?;
|
|
|
|
let surface = display.create_surface(
|
|
|
|
context.get_pixel_format(),
|
|
|
|
reqs.double_buffer,
|
|
|
|
context.get_config_id(),
|
|
|
|
(),
|
|
|
|
)?;
|
|
|
|
Window::Wayland {
|
|
|
|
display,
|
|
|
|
context,
|
|
|
|
surface,
|
|
|
|
}
|
2017-12-21 15:01:16 +00:00
|
|
|
} else if native::NativeDisplay::<native::X11>::is_backend(&winit_window) {
|
2020-04-15 11:01:01 +00:00
|
|
|
let display = EGLDisplay::<native::X11, WinitWindow>::new(winit_window, log.clone())?;
|
|
|
|
let context = display.create_context(attributes, reqs)?;
|
|
|
|
let surface = display.create_surface(
|
|
|
|
context.get_pixel_format(),
|
|
|
|
reqs.double_buffer,
|
|
|
|
context.get_config_id(),
|
|
|
|
(),
|
|
|
|
)?;
|
|
|
|
Window::X11 {
|
|
|
|
display,
|
|
|
|
context,
|
|
|
|
surface,
|
|
|
|
}
|
2017-12-21 15:01:16 +00:00
|
|
|
} else {
|
2020-04-12 20:35:24 +00:00
|
|
|
return Err(Error::NotSupported);
|
2018-01-07 21:30:38 +00:00
|
|
|
},
|
2017-12-21 15:01:16 +00:00
|
|
|
);
|
2017-05-18 20:28:02 +00:00
|
|
|
|
2018-10-13 10:36:00 +00:00
|
|
|
let size = Rc::new(RefCell::new(WindowSize {
|
2020-03-21 11:04:03 +00:00
|
|
|
physical_size: window.window().inner_size(), // TODO: original code check if window is alive or not using inner_size().expect()
|
2020-03-21 12:27:54 +00:00
|
|
|
scale_factor: window.window().scale_factor(),
|
2018-10-13 10:36:00 +00:00
|
|
|
}));
|
|
|
|
|
2017-06-20 09:31:18 +00:00
|
|
|
Ok((
|
|
|
|
WinitGraphicsBackend {
|
2017-05-21 20:40:15 +00:00
|
|
|
window: window.clone(),
|
2018-10-13 10:36:00 +00:00
|
|
|
size: size.clone(),
|
2017-06-04 21:11:26 +00:00
|
|
|
logger: log.new(o!("smithay_winit_component" => "graphics")),
|
2017-05-21 20:40:15 +00:00
|
|
|
},
|
|
|
|
WinitInputBackend {
|
2017-09-13 20:51:35 +00:00
|
|
|
events_loop,
|
2018-01-11 01:01:22 +00:00
|
|
|
events_handler: None,
|
2017-09-13 20:51:35 +00:00
|
|
|
window,
|
2018-03-17 15:15:23 +00:00
|
|
|
time: Instant::now(),
|
2017-05-21 20:40:15 +00:00
|
|
|
key_counter: 0,
|
2017-06-20 09:31:18 +00:00
|
|
|
seat: Seat::new(
|
|
|
|
0,
|
2018-01-16 15:27:56 +00:00
|
|
|
"winit",
|
2017-06-20 09:31:18 +00:00
|
|
|
SeatCapabilities {
|
|
|
|
pointer: true,
|
|
|
|
keyboard: true,
|
|
|
|
touch: true,
|
|
|
|
},
|
|
|
|
),
|
2017-05-21 20:40:15 +00:00
|
|
|
input_config: (),
|
|
|
|
handler: None,
|
2017-06-04 21:11:26 +00:00
|
|
|
logger: log.new(o!("smithay_winit_component" => "input")),
|
2018-10-13 10:36:00 +00:00
|
|
|
size,
|
2017-06-20 09:31:18 +00:00
|
|
|
},
|
|
|
|
))
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 12:56:30 +00:00
|
|
|
/// Handler trait to receive window-related events to provide a better *nested* experience.
|
2018-01-11 01:01:22 +00:00
|
|
|
pub trait WinitEventsHandler {
|
2018-12-08 12:40:07 +00:00
|
|
|
/// The window was resized, can be used to adjust the associated [`Output`](::wayland::output::Output)s mode.
|
2018-10-13 10:36:00 +00:00
|
|
|
///
|
2018-10-30 12:56:30 +00:00
|
|
|
/// Here are provided the new size (in physical pixels) and the new scale factor provided by `winit`.
|
2018-10-13 10:36:00 +00:00
|
|
|
fn resized(&mut self, size: (f64, f64), scale: f64);
|
2018-01-11 01:01:22 +00:00
|
|
|
/// The window gained or lost focus
|
2018-04-18 07:58:32 +00:00
|
|
|
fn focus_changed(&mut self, focused: bool);
|
2018-01-11 01:01:22 +00:00
|
|
|
/// The window needs to be redrawn
|
2018-04-18 07:58:32 +00:00
|
|
|
fn refresh(&mut self);
|
2018-01-11 01:01:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 00:45:38 +00:00
|
|
|
impl WinitGraphicsBackend {
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Get a reference to the internally used [`WinitWindow`]
|
2018-12-15 20:58:43 +00:00
|
|
|
pub fn winit_window(&self) -> Ref<'_, WinitWindow> {
|
2018-01-11 00:45:38 +00:00
|
|
|
self.window.window()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:26:45 +00:00
|
|
|
impl CursorBackend for WinitGraphicsBackend {
|
|
|
|
type CursorFormat = CursorIcon;
|
2017-09-13 20:51:35 +00:00
|
|
|
type Error = ();
|
2017-05-18 20:28:02 +00:00
|
|
|
|
2017-09-18 14:58:20 +00:00
|
|
|
fn set_cursor_position(&self, x: u32, y: u32) -> ::std::result::Result<(), ()> {
|
2017-06-04 21:11:26 +00:00
|
|
|
debug!(self.logger, "Setting cursor position to {:?}", (x, y));
|
2018-09-26 05:36:18 +00:00
|
|
|
self.window
|
|
|
|
.window()
|
|
|
|
.set_cursor_position(LogicalPosition::new(x as f64, y as f64))
|
|
|
|
.map_err(|err| {
|
|
|
|
debug!(self.logger, "{}", err);
|
|
|
|
})
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:26:45 +00:00
|
|
|
fn set_cursor_representation(
|
|
|
|
&self,
|
|
|
|
cursor: &Self::CursorFormat,
|
2018-04-18 07:58:32 +00:00
|
|
|
_hotspot: (u32, u32),
|
2020-04-18 23:26:45 +00:00
|
|
|
) -> ::std::result::Result<(), ()> {
|
2017-06-04 21:13:19 +00:00
|
|
|
// Cannot log this one, as `CursorFormat` is not `Debug` and should not be
|
2017-06-04 21:11:26 +00:00
|
|
|
debug!(self.logger, "Changing cursor representation");
|
2020-03-21 11:04:03 +00:00
|
|
|
self.window.window().set_cursor_icon(*cursor);
|
2017-09-13 20:51:35 +00:00
|
|
|
Ok(())
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 08:37:57 +00:00
|
|
|
impl GLGraphicsBackend for WinitGraphicsBackend {
|
2017-09-18 14:58:20 +00:00
|
|
|
fn swap_buffers(&self) -> ::std::result::Result<(), SwapBuffersError> {
|
2017-06-04 21:11:26 +00:00
|
|
|
trace!(self.logger, "Swapping buffers");
|
2017-12-21 15:01:16 +00:00
|
|
|
match *self.window {
|
2020-04-29 22:24:35 +00:00
|
|
|
Window::Wayland { ref surface, .. } => surface.swap_buffers().map_err(|err| err.try_into().unwrap()),
|
|
|
|
Window::X11 { ref surface, .. } => surface.swap_buffers().map_err(|err| err.try_into().unwrap()),
|
2017-09-28 17:34:31 +00:00
|
|
|
}
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:16:02 +00:00
|
|
|
fn get_proc_address(&self, symbol: &str) -> *const c_void {
|
2017-06-04 21:11:26 +00:00
|
|
|
trace!(self.logger, "Getting symbol for {:?}", symbol);
|
2020-04-15 11:01:01 +00:00
|
|
|
get_proc_address(symbol)
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
|
2018-10-13 10:36:00 +00:00
|
|
|
let size = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
size.physical_size.into()
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_current(&self) -> bool {
|
2017-12-21 15:01:16 +00:00
|
|
|
match *self.window {
|
2018-01-07 21:30:38 +00:00
|
|
|
Window::Wayland {
|
|
|
|
ref context,
|
|
|
|
ref surface,
|
2020-04-15 11:01:01 +00:00
|
|
|
..
|
2018-01-07 21:30:38 +00:00
|
|
|
} => context.is_current() && surface.is_current(),
|
|
|
|
Window::X11 {
|
|
|
|
ref context,
|
|
|
|
ref surface,
|
2020-04-15 11:01:01 +00:00
|
|
|
..
|
2018-01-07 21:30:38 +00:00
|
|
|
} => context.is_current() && surface.is_current(),
|
2017-12-21 15:01:16 +00:00
|
|
|
}
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 14:58:20 +00:00
|
|
|
unsafe fn make_current(&self) -> ::std::result::Result<(), SwapBuffersError> {
|
2017-12-21 15:01:16 +00:00
|
|
|
trace!(self.logger, "Setting EGL context to be the current context");
|
|
|
|
match *self.window {
|
2020-04-15 11:01:01 +00:00
|
|
|
Window::Wayland {
|
|
|
|
ref surface,
|
|
|
|
ref context,
|
|
|
|
..
|
2020-04-29 22:24:35 +00:00
|
|
|
} => context.make_current_with_surface(surface).map_err(Into::into),
|
2020-04-15 11:01:01 +00:00
|
|
|
Window::X11 {
|
|
|
|
ref surface,
|
|
|
|
ref context,
|
|
|
|
..
|
2020-04-29 22:24:35 +00:00
|
|
|
} => context.make_current_with_surface(surface).map_err(Into::into),
|
2017-12-21 15:01:16 +00:00
|
|
|
}
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_pixel_format(&self) -> PixelFormat {
|
2017-12-21 15:01:16 +00:00
|
|
|
match *self.window {
|
2020-04-15 11:01:01 +00:00
|
|
|
Window::Wayland { ref surface, .. } => surface.get_pixel_format(),
|
|
|
|
Window::X11 { ref surface, .. } => surface.get_pixel_format(),
|
2017-12-21 15:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 19:04:46 +00:00
|
|
|
}
|
2017-12-21 15:01:16 +00:00
|
|
|
|
2020-04-15 11:01:01 +00:00
|
|
|
#[cfg(feature = "use_system_lib")]
|
2018-11-21 08:37:57 +00:00
|
|
|
impl EGLGraphicsBackend for WinitGraphicsBackend {
|
2020-04-15 20:19:20 +00:00
|
|
|
fn bind_wl_display(&self, wl_display: &Display) -> Result<EGLBufferReader, EGLError> {
|
2017-12-21 15:01:16 +00:00
|
|
|
match *self.window {
|
2020-04-15 11:01:01 +00:00
|
|
|
Window::Wayland { ref display, .. } => display.bind_wl_display(wl_display),
|
|
|
|
Window::X11 { ref display, .. } => display.bind_wl_display(wl_display),
|
2018-01-05 19:04:46 +00:00
|
|
|
}
|
2017-12-21 15:01:16 +00:00
|
|
|
}
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Errors that may happen when driving the event loop of [`WinitInputBackend`]
|
2020-04-12 20:35:24 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub enum WinitInputError {
|
2018-12-08 12:40:07 +00:00
|
|
|
/// The underlying [`WinitWindow`] was closed. No further events can be processed.
|
2017-05-18 20:28:02 +00:00
|
|
|
///
|
2018-12-08 12:40:07 +00:00
|
|
|
/// See `dispatch_new_events`.
|
2020-04-12 20:35:24 +00:00
|
|
|
#[error("Winit window was closed")]
|
2017-05-18 20:28:02 +00:00
|
|
|
WindowClosed,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`KeyboardKeyEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitKeyboardInputEvent {
|
|
|
|
time: u32,
|
2017-06-02 11:15:31 +00:00
|
|
|
key: u32,
|
2017-05-18 20:28:02 +00:00
|
|
|
count: u32,
|
|
|
|
state: ElementState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitKeyboardInputEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyboardKeyEvent for WinitKeyboardInputEvent {
|
|
|
|
fn key_code(&self) -> u32 {
|
2017-06-02 11:15:31 +00:00
|
|
|
self.key
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn state(&self) -> KeyState {
|
|
|
|
self.state.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn count(&self) -> u32 {
|
|
|
|
self.count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`PointerMotionAbsoluteEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitMouseMovedEvent {
|
2018-10-13 10:36:00 +00:00
|
|
|
size: Rc<RefCell<WindowSize>>,
|
2017-05-18 20:28:02 +00:00
|
|
|
time: u32,
|
2020-03-23 10:02:29 +00:00
|
|
|
logical_position: LogicalPosition<f64>,
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitMouseMovedEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:01:49 +00:00
|
|
|
impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent {
|
|
|
|
// TODO: maybe use {Logical, Physical}Position from winit?
|
2017-05-18 20:28:02 +00:00
|
|
|
fn x(&self) -> f64 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-23 10:02:29 +00:00
|
|
|
self.logical_position.x * wsize.scale_factor
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y(&self) -> f64 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-23 10:02:29 +00:00
|
|
|
self.logical_position.y * wsize.scale_factor
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn x_transformed(&self, width: u32) -> u32 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
let w_width = wsize.physical_size.to_logical::<f64>(wsize.scale_factor).width;
|
2020-03-23 10:02:29 +00:00
|
|
|
cmp::max((self.logical_position.x * width as f64 / w_width) as i32, 0) as u32
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y_transformed(&self, height: u32) -> u32 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
let w_height = wsize.physical_size.to_logical::<f64>(wsize.scale_factor).height;
|
2020-03-23 10:02:29 +00:00
|
|
|
cmp::max((self.logical_position.y * height as f64 / w_height) as i32, 0) as u32
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`PointerAxisEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitMouseWheelEvent {
|
|
|
|
time: u32,
|
|
|
|
delta: MouseScrollDelta,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitMouseWheelEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PointerAxisEvent for WinitMouseWheelEvent {
|
|
|
|
fn source(&self) -> AxisSource {
|
|
|
|
match self.delta {
|
|
|
|
MouseScrollDelta::LineDelta(_, _) => AxisSource::Wheel,
|
2018-09-26 05:36:18 +00:00
|
|
|
MouseScrollDelta::PixelDelta(_) => AxisSource::Continuous,
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 10:43:16 +00:00
|
|
|
fn amount(&self, axis: Axis) -> Option<f64> {
|
2018-03-22 15:09:58 +00:00
|
|
|
match (axis, self.delta) {
|
2020-01-01 10:43:16 +00:00
|
|
|
(Axis::Horizontal, MouseScrollDelta::PixelDelta(delta)) => Some(delta.x),
|
|
|
|
(Axis::Vertical, MouseScrollDelta::PixelDelta(delta)) => Some(delta.y),
|
2018-03-22 15:09:58 +00:00
|
|
|
(_, MouseScrollDelta::LineDelta(_, _)) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 10:43:16 +00:00
|
|
|
fn amount_discrete(&self, axis: Axis) -> Option<f64> {
|
2018-03-22 15:09:58 +00:00
|
|
|
match (axis, self.delta) {
|
2020-01-01 10:43:16 +00:00
|
|
|
(Axis::Horizontal, MouseScrollDelta::LineDelta(x, _)) => Some(x as f64),
|
|
|
|
(Axis::Vertical, MouseScrollDelta::LineDelta(_, y)) => Some(y as f64),
|
2018-09-26 05:36:18 +00:00
|
|
|
(_, MouseScrollDelta::PixelDelta(_)) => None,
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`PointerButtonEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitMouseInputEvent {
|
|
|
|
time: u32,
|
|
|
|
button: WinitMouseButton,
|
|
|
|
state: ElementState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitMouseInputEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PointerButtonEvent for WinitMouseInputEvent {
|
|
|
|
fn button(&self) -> MouseButton {
|
|
|
|
self.button.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn state(&self) -> MouseButtonState {
|
|
|
|
self.state.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`TouchDownEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitTouchStartedEvent {
|
2018-10-13 10:36:00 +00:00
|
|
|
size: Rc<RefCell<WindowSize>>,
|
2017-05-18 20:28:02 +00:00
|
|
|
time: u32,
|
|
|
|
location: (f64, f64),
|
|
|
|
id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitTouchStartedEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TouchDownEvent for WinitTouchStartedEvent {
|
|
|
|
fn slot(&self) -> Option<TouchSlot> {
|
|
|
|
Some(TouchSlot::new(self.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn x(&self) -> f64 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
self.location.0 * wsize.scale_factor
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y(&self) -> f64 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
self.location.1 * wsize.scale_factor
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn x_transformed(&self, width: u32) -> u32 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 14:22:06 +00:00
|
|
|
let w_width = wsize.physical_size.to_logical::<i32>(wsize.scale_factor).width;
|
2020-04-10 15:01:49 +00:00
|
|
|
cmp::min(self.location.0 as i32 * width as i32 / w_width, 0) as u32
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y_transformed(&self, height: u32) -> u32 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 14:22:06 +00:00
|
|
|
let w_height = wsize.physical_size.to_logical::<i32>(wsize.scale_factor).height;
|
2020-04-10 15:01:49 +00:00
|
|
|
cmp::min(self.location.1 as i32 * height as i32 / w_height, 0) as u32
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`TouchMotionEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitTouchMovedEvent {
|
2018-10-13 10:36:00 +00:00
|
|
|
size: Rc<RefCell<WindowSize>>,
|
2017-05-18 20:28:02 +00:00
|
|
|
time: u32,
|
|
|
|
location: (f64, f64),
|
|
|
|
id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitTouchMovedEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TouchMotionEvent for WinitTouchMovedEvent {
|
|
|
|
fn slot(&self) -> Option<TouchSlot> {
|
|
|
|
Some(TouchSlot::new(self.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn x(&self) -> f64 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
self.location.0 * wsize.scale_factor
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y(&self) -> f64 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 11:04:03 +00:00
|
|
|
self.location.1 * wsize.scale_factor
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn x_transformed(&self, width: u32) -> u32 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 14:22:06 +00:00
|
|
|
let w_width = wsize.physical_size.to_logical::<u32>(wsize.scale_factor).width;
|
|
|
|
self.location.0 as u32 * width / w_width
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y_transformed(&self, height: u32) -> u32 {
|
2018-10-13 10:36:00 +00:00
|
|
|
let wsize = self.size.borrow();
|
2020-03-21 14:22:06 +00:00
|
|
|
let w_height = wsize.physical_size.to_logical::<u32>(wsize.scale_factor).height;
|
|
|
|
self.location.1 as u32 * height / w_height
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2018-10-30 12:56:30 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a `TouchUpEvent`
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitTouchEndedEvent {
|
|
|
|
time: u32,
|
|
|
|
id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitTouchEndedEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TouchUpEvent for WinitTouchEndedEvent {
|
|
|
|
fn slot(&self) -> Option<TouchSlot> {
|
|
|
|
Some(TouchSlot::new(self.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Winit-Backend internal event wrapping `winit`'s types into a [`TouchCancelEvent`]
|
2017-05-18 20:28:02 +00:00
|
|
|
pub struct WinitTouchCancelledEvent {
|
|
|
|
time: u32,
|
|
|
|
id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendEvent for WinitTouchCancelledEvent {
|
|
|
|
fn time(&self) -> u32 {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TouchCancelEvent for WinitTouchCancelledEvent {
|
|
|
|
fn slot(&self) -> Option<TouchSlot> {
|
|
|
|
Some(TouchSlot::new(self.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:01:22 +00:00
|
|
|
impl WinitInputBackend {
|
|
|
|
/// Set the events handler
|
|
|
|
pub fn set_events_handler<H: WinitEventsHandler + 'static>(&mut self, handler: H) {
|
|
|
|
self.events_handler = Some(Box::new(handler));
|
|
|
|
info!(self.logger, "New events handler set.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the set events handler, if any
|
2018-12-15 20:58:43 +00:00
|
|
|
pub fn get_events_handler(&mut self) -> Option<&mut dyn WinitEventsHandler> {
|
2018-01-11 01:01:22 +00:00
|
|
|
self.events_handler
|
|
|
|
.as_mut()
|
2018-12-15 20:58:43 +00:00
|
|
|
.map(|handler| &mut **handler as &mut dyn WinitEventsHandler)
|
2018-01-11 01:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear out the currently set events handler
|
|
|
|
pub fn clear_events_handler(&mut self) {
|
|
|
|
self.events_handler = None;
|
|
|
|
info!(self.logger, "Events handler unset.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:28:02 +00:00
|
|
|
impl InputBackend for WinitInputBackend {
|
|
|
|
type InputConfig = ();
|
|
|
|
type EventError = WinitInputError;
|
|
|
|
|
|
|
|
type KeyboardKeyEvent = WinitKeyboardInputEvent;
|
|
|
|
type PointerAxisEvent = WinitMouseWheelEvent;
|
|
|
|
type PointerButtonEvent = WinitMouseInputEvent;
|
|
|
|
type PointerMotionEvent = UnusedEvent;
|
|
|
|
type PointerMotionAbsoluteEvent = WinitMouseMovedEvent;
|
|
|
|
type TouchDownEvent = WinitTouchStartedEvent;
|
|
|
|
type TouchUpEvent = WinitTouchEndedEvent;
|
|
|
|
type TouchMotionEvent = WinitTouchMovedEvent;
|
|
|
|
type TouchCancelEvent = WinitTouchCancelledEvent;
|
|
|
|
type TouchFrameEvent = UnusedEvent;
|
|
|
|
|
2018-04-18 07:58:32 +00:00
|
|
|
fn set_handler<H: InputHandler<Self> + 'static>(&mut self, mut handler: H) {
|
2017-05-18 20:28:02 +00:00
|
|
|
if self.handler.is_some() {
|
2018-04-18 07:58:32 +00:00
|
|
|
self.clear_handler();
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
2017-06-04 21:11:26 +00:00
|
|
|
info!(self.logger, "New input handler set.");
|
|
|
|
trace!(self.logger, "Calling on_seat_created with {:?}", self.seat);
|
2018-04-18 07:58:32 +00:00
|
|
|
handler.on_seat_created(&self.seat);
|
2017-05-18 20:28:02 +00:00
|
|
|
self.handler = Some(Box::new(handler));
|
|
|
|
}
|
|
|
|
|
2018-12-15 20:58:43 +00:00
|
|
|
fn get_handler(&mut self) -> Option<&mut dyn InputHandler<Self>> {
|
2017-09-05 17:51:05 +00:00
|
|
|
self.handler
|
|
|
|
.as_mut()
|
2018-12-15 20:58:43 +00:00
|
|
|
.map(|handler| handler as &mut dyn InputHandler<Self>)
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 07:58:32 +00:00
|
|
|
fn clear_handler(&mut self) {
|
2017-05-18 20:28:02 +00:00
|
|
|
if let Some(mut handler) = self.handler.take() {
|
2018-09-24 22:32:09 +00:00
|
|
|
trace!(self.logger, "Calling on_seat_destroyed with {:?}", self.seat);
|
2018-04-18 07:58:32 +00:00
|
|
|
handler.on_seat_destroyed(&self.seat);
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
2017-06-04 21:11:26 +00:00
|
|
|
info!(self.logger, "Removing input handler");
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn input_config(&mut self) -> &mut Self::InputConfig {
|
|
|
|
&mut self.input_config
|
|
|
|
}
|
|
|
|
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Processes new events of the underlying event loop to drive the set [`InputHandler`].
|
2017-05-18 20:28:02 +00:00
|
|
|
///
|
|
|
|
/// You need to periodically call this function to keep the underlying event loop and
|
2018-12-08 12:40:07 +00:00
|
|
|
/// [`WinitWindow`] active. Otherwise the window may no respond to user interaction and no
|
|
|
|
/// input events will be received by a set [`InputHandler`].
|
2017-05-18 20:28:02 +00:00
|
|
|
///
|
2018-12-08 12:40:07 +00:00
|
|
|
/// Returns an error if the [`WinitWindow`] the window has been closed. Calling
|
|
|
|
/// `dispatch_new_events` again after the [`WinitWindow`] has been closed is considered an
|
2018-10-30 12:56:30 +00:00
|
|
|
/// application error and unspecified behaviour may occur.
|
2017-05-18 20:28:02 +00:00
|
|
|
///
|
2018-12-08 12:40:07 +00:00
|
|
|
/// The linked [`WinitGraphicsBackend`] will error with a lost context and should
|
2017-05-18 20:28:02 +00:00
|
|
|
/// not be used anymore as well.
|
2018-04-18 07:58:32 +00:00
|
|
|
fn dispatch_new_events(&mut self) -> ::std::result::Result<(), WinitInputError> {
|
2017-05-18 20:28:02 +00:00
|
|
|
let mut closed = false;
|
|
|
|
|
|
|
|
{
|
2018-01-12 14:49:55 +00:00
|
|
|
// NOTE: This ugly pile of references is here, because rustc could not
|
2017-06-04 21:11:26 +00:00
|
|
|
// figure out how to reference all these objects correctly into the
|
|
|
|
// upcoming closure, which is why all are borrowed manually and the
|
|
|
|
// assignments are then moved into the closure to avoid rustc's
|
|
|
|
// wrong interference.
|
2018-12-15 20:58:43 +00:00
|
|
|
let closed_ptr = &mut closed;
|
|
|
|
let key_counter = &mut self.key_counter;
|
2018-03-17 15:15:23 +00:00
|
|
|
let time = &self.time;
|
2017-05-18 20:28:02 +00:00
|
|
|
let seat = &self.seat;
|
|
|
|
let window = &self.window;
|
|
|
|
let mut handler = self.handler.as_mut();
|
2018-01-11 01:01:22 +00:00
|
|
|
let mut events_handler = self.events_handler.as_mut();
|
2017-06-04 21:11:26 +00:00
|
|
|
let logger = &self.logger;
|
2018-10-13 10:36:00 +00:00
|
|
|
let window_size = &self.size;
|
2020-04-10 15:01:49 +00:00
|
|
|
|
|
|
|
self.events_loop
|
|
|
|
.run_return(move |event, _target, control_flow| match event {
|
2020-03-21 11:04:03 +00:00
|
|
|
Event::RedrawEventsCleared => {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
2020-04-10 15:01:49 +00:00
|
|
|
}
|
2020-03-21 11:04:03 +00:00
|
|
|
Event::RedrawRequested(_id) => {
|
|
|
|
if let Some(events_handler) = events_handler.as_mut() {
|
|
|
|
events_handler.refresh();
|
2018-01-11 01:01:22 +00:00
|
|
|
}
|
2020-04-10 15:01:49 +00:00
|
|
|
}
|
2020-03-21 11:04:03 +00:00
|
|
|
Event::WindowEvent { event, .. } => {
|
|
|
|
let duration = Instant::now().duration_since(*time);
|
|
|
|
let nanos = duration.subsec_nanos() as u64;
|
|
|
|
let time = ((1000 * duration.as_secs()) + (nanos / 1_000_000)) as u32;
|
|
|
|
match (event, handler.as_mut(), events_handler.as_mut()) {
|
|
|
|
(WindowEvent::Resized(psize), _, events_handler) => {
|
|
|
|
trace!(logger, "Resizing window to {:?}", psize);
|
|
|
|
let scale_factor = window.window().scale_factor();
|
|
|
|
let mut wsize = window_size.borrow_mut();
|
|
|
|
wsize.physical_size = psize;
|
|
|
|
wsize.scale_factor = scale_factor;
|
|
|
|
if let Window::Wayland { ref surface, .. } = **window {
|
|
|
|
surface.resize(psize.width as i32, psize.height as i32, 0, 0);
|
|
|
|
}
|
|
|
|
if let Some(events_handler) = events_handler {
|
|
|
|
events_handler.resized(psize.into(), scale_factor);
|
|
|
|
}
|
2018-10-13 10:36:00 +00:00
|
|
|
}
|
2020-03-21 11:04:03 +00:00
|
|
|
(WindowEvent::Focused(focus), _, Some(events_handler)) => {
|
|
|
|
events_handler.focus_changed(focus)
|
2018-10-13 10:36:00 +00:00
|
|
|
}
|
2020-04-10 15:01:49 +00:00
|
|
|
(
|
|
|
|
WindowEvent::ScaleFactorChanged {
|
|
|
|
scale_factor,
|
|
|
|
new_inner_size: new_psize,
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
events_handler,
|
|
|
|
) => {
|
2020-03-21 11:04:03 +00:00
|
|
|
let mut wsize = window_size.borrow_mut();
|
|
|
|
wsize.scale_factor = scale_factor;
|
|
|
|
if let Window::Wayland { ref surface, .. } = **window {
|
|
|
|
surface.resize(new_psize.width as i32, new_psize.height as i32, 0, 0);
|
2017-06-20 09:31:18 +00:00
|
|
|
}
|
2020-03-21 11:04:03 +00:00
|
|
|
if let Some(events_handler) = events_handler {
|
2020-04-10 15:01:49 +00:00
|
|
|
let psize_f64: (f64, f64) =
|
|
|
|
(new_psize.width.into(), new_psize.height.into());
|
2020-03-21 11:04:03 +00:00
|
|
|
events_handler.resized(psize_f64, wsize.scale_factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
input: KeyboardInput { scancode, state, .. },
|
|
|
|
..
|
2017-06-20 09:31:18 +00:00
|
|
|
},
|
2020-03-21 11:04:03 +00:00
|
|
|
Some(handler),
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
match state {
|
|
|
|
ElementState::Pressed => *key_counter += 1,
|
|
|
|
ElementState::Released => {
|
|
|
|
*key_counter = key_counter.checked_sub(1).unwrap_or(0)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
trace!(logger, "Calling on_keyboard_key with {:?}", (scancode, state));
|
|
|
|
handler.on_keyboard_key(
|
|
|
|
seat,
|
|
|
|
WinitKeyboardInputEvent {
|
|
|
|
time,
|
|
|
|
key: scancode,
|
|
|
|
count: *key_counter,
|
|
|
|
state,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(WindowEvent::CursorMoved { position, .. }, Some(handler), _) => {
|
|
|
|
trace!(logger, "Calling on_pointer_move_absolute with {:?}", position);
|
2020-03-23 10:02:29 +00:00
|
|
|
let lpos = position.to_logical(window_size.borrow().scale_factor);
|
2020-03-21 11:04:03 +00:00
|
|
|
handler.on_pointer_move_absolute(
|
|
|
|
seat,
|
|
|
|
WinitMouseMovedEvent {
|
|
|
|
size: window_size.clone(),
|
|
|
|
time,
|
2020-03-23 10:02:29 +00:00
|
|
|
logical_position: lpos,
|
2020-03-21 11:04:03 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(WindowEvent::MouseWheel { delta, .. }, Some(handler), _) => {
|
|
|
|
let event = WinitMouseWheelEvent { time, delta };
|
|
|
|
trace!(logger, "Calling on_pointer_axis with {:?}", delta);
|
|
|
|
handler.on_pointer_axis(seat, event);
|
|
|
|
}
|
|
|
|
(WindowEvent::MouseInput { state, button, .. }, Some(handler), _) => {
|
|
|
|
trace!(logger, "Calling on_pointer_button with {:?}", (button, state));
|
|
|
|
handler.on_pointer_button(seat, WinitMouseInputEvent { time, button, state })
|
|
|
|
}
|
|
|
|
(
|
|
|
|
WindowEvent::Touch(Touch {
|
|
|
|
phase: TouchPhase::Started,
|
|
|
|
location,
|
2018-06-28 09:33:49 +00:00
|
|
|
id,
|
2020-03-21 11:04:03 +00:00
|
|
|
..
|
|
|
|
}),
|
|
|
|
Some(handler),
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
trace!(logger, "Calling on_touch_down at {:?}", location);
|
|
|
|
handler.on_touch_down(
|
|
|
|
seat,
|
|
|
|
WinitTouchStartedEvent {
|
|
|
|
size: window_size.clone(),
|
|
|
|
time,
|
|
|
|
location: location.into(),
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(
|
|
|
|
WindowEvent::Touch(Touch {
|
|
|
|
phase: TouchPhase::Moved,
|
|
|
|
location,
|
2018-06-28 09:33:49 +00:00
|
|
|
id,
|
2020-03-21 11:04:03 +00:00
|
|
|
..
|
|
|
|
}),
|
|
|
|
Some(handler),
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
trace!(logger, "Calling on_touch_motion at {:?}", location);
|
|
|
|
handler.on_touch_motion(
|
|
|
|
seat,
|
|
|
|
WinitTouchMovedEvent {
|
|
|
|
size: window_size.clone(),
|
|
|
|
time,
|
|
|
|
location: location.into(),
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(
|
|
|
|
WindowEvent::Touch(Touch {
|
|
|
|
phase: TouchPhase::Ended,
|
|
|
|
location,
|
2018-06-28 09:33:49 +00:00
|
|
|
id,
|
2020-03-21 11:04:03 +00:00
|
|
|
..
|
|
|
|
}),
|
|
|
|
Some(handler),
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
trace!(logger, "Calling on_touch_motion at {:?}", location);
|
|
|
|
handler.on_touch_motion(
|
|
|
|
seat,
|
|
|
|
WinitTouchMovedEvent {
|
|
|
|
size: window_size.clone(),
|
|
|
|
time,
|
|
|
|
location: location.into(),
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
trace!(logger, "Calling on_touch_up");
|
|
|
|
handler.on_touch_up(seat, WinitTouchEndedEvent { time, id });
|
|
|
|
}
|
|
|
|
(
|
|
|
|
WindowEvent::Touch(Touch {
|
|
|
|
phase: TouchPhase::Cancelled,
|
|
|
|
id,
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
Some(handler),
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
trace!(logger, "Calling on_touch_cancel");
|
|
|
|
handler.on_touch_cancel(seat, WinitTouchCancelledEvent { time, id })
|
|
|
|
}
|
|
|
|
(WindowEvent::CloseRequested, _, _) | (WindowEvent::Destroyed, _, _) => {
|
|
|
|
warn!(logger, "Window closed");
|
|
|
|
*closed_ptr = true;
|
2020-04-10 15:01:49 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
2017-06-04 21:13:19 +00:00
|
|
|
}
|
2020-04-10 15:01:49 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
});
|
2017-05-18 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if closed {
|
|
|
|
Err(WinitInputError::WindowClosed)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<WinitMouseButton> for MouseButton {
|
|
|
|
fn from(button: WinitMouseButton) -> MouseButton {
|
|
|
|
match button {
|
|
|
|
WinitMouseButton::Left => MouseButton::Left,
|
|
|
|
WinitMouseButton::Right => MouseButton::Right,
|
|
|
|
WinitMouseButton::Middle => MouseButton::Middle,
|
|
|
|
WinitMouseButton::Other(num) => MouseButton::Other(num),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ElementState> for KeyState {
|
|
|
|
fn from(state: ElementState) -> Self {
|
|
|
|
match state {
|
|
|
|
ElementState::Pressed => KeyState::Pressed,
|
|
|
|
ElementState::Released => KeyState::Released,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ElementState> for MouseButtonState {
|
|
|
|
fn from(state: ElementState) -> Self {
|
|
|
|
match state {
|
|
|
|
ElementState::Pressed => MouseButtonState::Pressed,
|
|
|
|
ElementState::Released => MouseButtonState::Released,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|