Merge pull request #102 from trimental/master

Upgrade winit backend to 0.17
This commit is contained in:
Victor Berger 2018-09-30 17:06:36 +02:00 committed by GitHub
commit 4654608f07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 42 deletions

View File

@ -2,6 +2,8 @@
## Unreleased ## Unreleased
- **[Breaking]** WinitBackend: Upgrade to winit 0.17
## version 0.1.0 (2017-10-01) ## version 0.1.0 (2017-10-01)
### Protocol handling ### Protocol handling

View File

@ -19,7 +19,7 @@ slog = "2.1.1"
slog-stdlog = "3.0.2" slog-stdlog = "3.0.2"
libloading = "0.4.0" libloading = "0.4.0"
wayland-client = { version = "0.20.5", optional = true } wayland-client = { version = "0.20.5", optional = true }
winit = { version = "0.14.0", optional = true } winit = { version = "0.17.0", optional = true }
drm = { version = "^0.3.1", optional = true } drm = { version = "^0.3.1", optional = true }
gbm = { version = "^0.4.0", optional = true, default-features = false, features = ["drm-support"] } gbm = { version = "^0.4.0", optional = true, default-features = false, features = ["drm-support"] }
glium = { version = "0.19.0", optional = true, default-features = false } glium = { version = "0.19.0", optional = true, default-features = false }

View File

@ -187,8 +187,10 @@ unsafe impl NativeDisplay<Wayland> for WinitWindow {
fn create_surface(&self, _args: ()) -> Result<wegl::WlEglSurface> { fn create_surface(&self, _args: ()) -> Result<wegl::WlEglSurface> {
if let Some(surface) = self.get_wayland_surface() { if let Some(surface) = self.get_wayland_surface() {
let (w, h) = self.get_inner_size().unwrap(); let size = self.get_inner_size().unwrap();
Ok(unsafe { wegl::WlEglSurface::new_from_raw(surface as *mut _, w as i32, h as i32) }) Ok(unsafe {
wegl::WlEglSurface::new_from_raw(surface as *mut _, size.width as i32, size.height as i32)
})
} else { } else {
bail!(ErrorKind::NonMatchingBackend("Wayland")) bail!(ErrorKind::NonMatchingBackend("Wayland"))
} }

View File

@ -21,6 +21,7 @@ use std::rc::Rc;
use std::time::Instant; use std::time::Instant;
use wayland_client::egl as wegl; use wayland_client::egl as wegl;
use wayland_server::Display; use wayland_server::Display;
use winit::dpi::{LogicalPosition, LogicalSize};
use winit::{ use winit::{
ElementState, Event, EventsLoop, KeyboardInput, MouseButton as WinitMouseButton, MouseCursor, ElementState, Event, EventsLoop, KeyboardInput, MouseButton as WinitMouseButton, MouseCursor,
MouseScrollDelta, Touch, TouchPhase, Window as WinitWindow, WindowBuilder, WindowEvent, MouseScrollDelta, Touch, TouchPhase, Window as WinitWindow, WindowBuilder, WindowEvent,
@ -84,6 +85,7 @@ pub struct WinitInputBackend {
input_config: (), input_config: (),
handler: Option<Box<InputHandler<WinitInputBackend> + 'static>>, handler: Option<Box<InputHandler<WinitInputBackend> + 'static>>,
logger: ::slog::Logger, logger: ::slog::Logger,
dpi: f64,
} }
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend` /// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
@ -95,7 +97,7 @@ where
{ {
init_from_builder( init_from_builder(
WindowBuilder::new() WindowBuilder::new()
.with_dimensions(1280, 800) .with_dimensions(LogicalSize::new(1280.0, 800.0))
.with_title("Smithay") .with_title("Smithay")
.with_visibility(true), .with_visibility(true),
logger, logger,
@ -183,6 +185,7 @@ where
input_config: (), input_config: (),
handler: None, handler: None,
logger: log.new(o!("smithay_winit_component" => "input")), logger: log.new(o!("smithay_winit_component" => "input")),
dpi: 1.0,
}, },
)) ))
} }
@ -190,9 +193,9 @@ where
/// Handler trait to recieve window-related events to provide a better *nested* experience. /// Handler trait to recieve window-related events to provide a better *nested* experience.
pub trait WinitEventsHandler { pub trait WinitEventsHandler {
/// The window was resized, can be used to adjust the associated `wayland::output::Output`s mode. /// The window was resized, can be used to adjust the associated `wayland::output::Output`s mode.
fn resized(&mut self, width: u32, height: u32); fn resized(&mut self, size: LogicalSize);
/// The window was moved /// The window was moved
fn moved(&mut self, x: i32, h: i32); fn moved(&mut self, position: LogicalPosition);
/// The window gained or lost focus /// The window gained or lost focus
fn focus_changed(&mut self, focused: bool); fn focus_changed(&mut self, focused: bool);
/// The window needs to be redrawn /// The window needs to be redrawn
@ -214,7 +217,12 @@ impl GraphicsBackend for WinitGraphicsBackend {
fn set_cursor_position(&self, x: u32, y: u32) -> ::std::result::Result<(), ()> { fn set_cursor_position(&self, x: u32, y: u32) -> ::std::result::Result<(), ()> {
debug!(self.logger, "Setting cursor position to {:?}", (x, y)); debug!(self.logger, "Setting cursor position to {:?}", (x, y));
self.window.window().set_cursor_position(x as i32, y as i32) self.window
.window()
.set_cursor_position(LogicalPosition::new(x as f64, y as f64))
.map_err(|err| {
debug!(self.logger, "{}", err);
})
} }
fn set_cursor_representation( fn set_cursor_representation(
@ -251,6 +259,7 @@ impl EGLGraphicsBackend for WinitGraphicsBackend {
.window() .window()
.get_inner_size() .get_inner_size()
.expect("Window does not exist anymore") .expect("Window does not exist anymore")
.into()
} }
fn is_current(&self) -> bool { fn is_current(&self) -> bool {
@ -369,16 +378,24 @@ impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent {
fn x_transformed(&self, width: u32) -> u32 { fn x_transformed(&self, width: u32) -> u32 {
cmp::max( cmp::max(
(self.x * width as f64 / self.window.window().get_inner_size().unwrap_or((width, 0)).0 as f64) (self.x * width as f64 / self
as i32, .window
.window()
.get_inner_size()
.unwrap_or(LogicalSize::new(width.into(), 0.0))
.width) as i32,
0, 0,
) as u32 ) as u32
} }
fn y_transformed(&self, height: u32) -> u32 { fn y_transformed(&self, height: u32) -> u32 {
cmp::max( cmp::max(
(self.y * height as f64 / self.window.window().get_inner_size().unwrap_or((0, height)).1 as f64) (self.y * height as f64 / self
as i32, .window
.window()
.get_inner_size()
.unwrap_or(LogicalSize::new(0.0, height.into()))
.height) as i32,
0, 0,
) as u32 ) as u32
} }
@ -401,14 +418,14 @@ impl PointerAxisEvent for WinitMouseWheelEvent {
fn source(&self) -> AxisSource { fn source(&self) -> AxisSource {
match self.delta { match self.delta {
MouseScrollDelta::LineDelta(_, _) => AxisSource::Wheel, MouseScrollDelta::LineDelta(_, _) => AxisSource::Wheel,
MouseScrollDelta::PixelDelta(_, _) => AxisSource::Continuous, MouseScrollDelta::PixelDelta(_) => AxisSource::Continuous,
} }
} }
fn amount(&self, axis: &Axis) -> Option<f64> { fn amount(&self, axis: &Axis) -> Option<f64> {
match (axis, self.delta) { match (axis, self.delta) {
(&Axis::Horizontal, MouseScrollDelta::PixelDelta(x, _)) => Some(x as f64), (&Axis::Horizontal, MouseScrollDelta::PixelDelta(delta)) => Some(delta.x),
(&Axis::Vertical, MouseScrollDelta::PixelDelta(_, y)) => Some(y as f64), (&Axis::Vertical, MouseScrollDelta::PixelDelta(delta)) => Some(delta.y),
(_, MouseScrollDelta::LineDelta(_, _)) => None, (_, MouseScrollDelta::LineDelta(_, _)) => None,
} }
} }
@ -417,7 +434,7 @@ impl PointerAxisEvent for WinitMouseWheelEvent {
match (axis, self.delta) { match (axis, self.delta) {
(&Axis::Horizontal, MouseScrollDelta::LineDelta(x, _)) => Some(x as f64), (&Axis::Horizontal, MouseScrollDelta::LineDelta(x, _)) => Some(x as f64),
(&Axis::Vertical, MouseScrollDelta::LineDelta(_, y)) => Some(y as f64), (&Axis::Vertical, MouseScrollDelta::LineDelta(_, y)) => Some(y as f64),
(_, MouseScrollDelta::PixelDelta(_, _)) => None, (_, MouseScrollDelta::PixelDelta(_)) => None,
} }
} }
} }
@ -476,16 +493,24 @@ impl TouchDownEvent for WinitTouchStartedEvent {
fn x_transformed(&self, width: u32) -> u32 { fn x_transformed(&self, width: u32) -> u32 {
cmp::min( cmp::min(
self.location.0 as i32 * width as i32 self.location.0 as i32 * width as i32 / self
/ self.window.window().get_inner_size().unwrap_or((width, 0)).0 as i32, .window
.window()
.get_inner_size()
.unwrap_or(LogicalSize::new(width.into(), 0.0))
.width as i32,
0, 0,
) as u32 ) as u32
} }
fn y_transformed(&self, height: u32) -> u32 { fn y_transformed(&self, height: u32) -> u32 {
cmp::min( cmp::min(
self.location.1 as i32 * height as i32 self.location.1 as i32 * height as i32 / self
/ self.window.window().get_inner_size().unwrap_or((0, height)).1 as i32, .window
.window()
.get_inner_size()
.unwrap_or(LogicalSize::new(0.0, height.into()))
.height as i32,
0, 0,
) as u32 ) as u32
} }
@ -520,11 +545,21 @@ impl TouchMotionEvent for WinitTouchMovedEvent {
} }
fn x_transformed(&self, width: u32) -> u32 { fn x_transformed(&self, width: u32) -> u32 {
self.location.0 as u32 * width / self.window.window().get_inner_size().unwrap_or((width, 0)).0 self.location.0 as u32 * width / self
.window
.window()
.get_inner_size()
.unwrap_or(LogicalSize::new(width.into(), 0.0))
.width as u32
} }
fn y_transformed(&self, height: u32) -> u32 { fn y_transformed(&self, height: u32) -> u32 {
self.location.1 as u32 * height / self.window.window().get_inner_size().unwrap_or((0, height)).1 self.location.1 as u32 * height / self
.window
.window()
.get_inner_size()
.unwrap_or(LogicalSize::new(0.0, height.into()))
.height as u32
} }
} }
@ -659,6 +694,7 @@ impl InputBackend for WinitInputBackend {
let mut handler = self.handler.as_mut(); let mut handler = self.handler.as_mut();
let mut events_handler = self.events_handler.as_mut(); let mut events_handler = self.events_handler.as_mut();
let logger = &self.logger; let logger = &self.logger;
let dpi = &mut self.dpi;
self.events_loop.poll_events(move |event| { self.events_loop.poll_events(move |event| {
if let Event::WindowEvent { event, .. } = event { if let Event::WindowEvent { event, .. } = event {
@ -666,23 +702,27 @@ impl InputBackend for WinitInputBackend {
let nanos = duration.subsec_nanos() as u64; let nanos = duration.subsec_nanos() as u64;
let time = ((1000 * duration.as_secs()) + (nanos / 1_000_000)) as u32; let time = ((1000 * duration.as_secs()) + (nanos / 1_000_000)) as u32;
match (event, handler.as_mut(), events_handler.as_mut()) { match (event, handler.as_mut(), events_handler.as_mut()) {
(WindowEvent::Resized(w, h), _, events_handler) => { (WindowEvent::Resized(size), _, events_handler) => {
trace!(logger, "Resizing window to {:?}", (w, h)); trace!(logger, "Resizing window to {:?}", size);
window.window().set_inner_size(w, h); window.window().set_inner_size(size);
if let Window::Wayland { ref surface, .. } = **window { if let Window::Wayland { ref surface, .. } = **window {
surface.resize(w as i32, h as i32, 0, 0); let physical_size = size.to_physical(*dpi);
surface.resize(physical_size.width as i32, physical_size.height as i32, 0, 0);
} }
if let Some(events_handler) = events_handler { if let Some(events_handler) = events_handler {
events_handler.resized(w, h); events_handler.resized(size);
} }
} }
(WindowEvent::Moved(x, y), _, Some(events_handler)) => events_handler.moved(x, y), (WindowEvent::Moved(position), _, Some(events_handler)) => {
events_handler.moved(position)
}
(WindowEvent::Focused(focus), _, Some(events_handler)) => { (WindowEvent::Focused(focus), _, Some(events_handler)) => {
events_handler.focus_changed(focus) events_handler.focus_changed(focus)
} }
(WindowEvent::Refresh, _, Some(events_handler)) => events_handler.refresh(), (WindowEvent::Refresh, _, Some(events_handler)) => events_handler.refresh(),
(WindowEvent::HiDPIFactorChanged(factor), _, Some(events_handler)) => { (WindowEvent::HiDpiFactorChanged(factor), _, Some(events_handler)) => {
events_handler.hidpi_changed(factor) *dpi = factor;
events_handler.hidpi_changed(factor as f32)
} }
( (
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
@ -709,15 +749,15 @@ impl InputBackend for WinitInputBackend {
}, },
) )
} }
(WindowEvent::CursorMoved { position: (x, y), .. }, Some(handler), _) => { (WindowEvent::CursorMoved { position, .. }, Some(handler), _) => {
trace!(logger, "Calling on_pointer_move_absolute with {:?}", (x, y)); trace!(logger, "Calling on_pointer_move_absolute with {:?}", position);
handler.on_pointer_move_absolute( handler.on_pointer_move_absolute(
seat, seat,
WinitMouseMovedEvent { WinitMouseMovedEvent {
window: window.clone(), window: window.clone(),
time, time,
x, x: position.x,
y, y: position.y,
}, },
) )
} }
@ -733,20 +773,20 @@ impl InputBackend for WinitInputBackend {
( (
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
phase: TouchPhase::Started, phase: TouchPhase::Started,
location: (x, y), location,
id, id,
.. ..
}), }),
Some(handler), Some(handler),
_, _,
) => { ) => {
trace!(logger, "Calling on_touch_down at {:?}", (x, y)); trace!(logger, "Calling on_touch_down at {:?}", location);
handler.on_touch_down( handler.on_touch_down(
seat, seat,
WinitTouchStartedEvent { WinitTouchStartedEvent {
window: window.clone(), window: window.clone(),
time, time,
location: (x, y), location: location.into(),
id, id,
}, },
) )
@ -754,20 +794,20 @@ impl InputBackend for WinitInputBackend {
( (
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
phase: TouchPhase::Moved, phase: TouchPhase::Moved,
location: (x, y), location,
id, id,
.. ..
}), }),
Some(handler), Some(handler),
_, _,
) => { ) => {
trace!(logger, "Calling on_touch_motion at {:?}", (x, y)); trace!(logger, "Calling on_touch_motion at {:?}", location);
handler.on_touch_motion( handler.on_touch_motion(
seat, seat,
WinitTouchMovedEvent { WinitTouchMovedEvent {
window: window.clone(), window: window.clone(),
time, time,
location: (x, y), location: location.into(),
id, id,
}, },
) )
@ -775,20 +815,20 @@ impl InputBackend for WinitInputBackend {
( (
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
phase: TouchPhase::Ended, phase: TouchPhase::Ended,
location: (x, y), location,
id, id,
.. ..
}), }),
Some(handler), Some(handler),
_, _,
) => { ) => {
trace!(logger, "Calling on_touch_motion at {:?}", (x, y)); trace!(logger, "Calling on_touch_motion at {:?}", location);
handler.on_touch_motion( handler.on_touch_motion(
seat, seat,
WinitTouchMovedEvent { WinitTouchMovedEvent {
window: window.clone(), window: window.clone(),
time, time,
location: (x, y), location: location.into(),
id, id,
}, },
); );