Upgrade winit backend to 0.17

This commit is contained in:
Lucas Timmins 2018-09-26 13:36:18 +08:00
parent ae5b9e55f4
commit ffdf245259
3 changed files with 77 additions and 40 deletions

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,
@ -95,7 +96,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,
@ -214,7 +215,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 +257,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 +376,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 +416,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 +432,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 +491,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 +543,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
} }
} }
@ -666,23 +699,25 @@ 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); surface.resize(size.width as i32, 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.width as u32, size.height as u32);
} }
} }
(WindowEvent::Moved(x, y), _, Some(events_handler)) => events_handler.moved(x, y), (WindowEvent::Moved(position), _, Some(events_handler)) => {
events_handler.moved(position.x as i32, position.y as i32)
}
(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) events_handler.hidpi_changed(factor as f32)
} }
( (
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
@ -709,15 +744,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 +768,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 +789,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 +810,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,
}, },
); );