Update winit

This commit is contained in:
Drakulix 2017-06-02 13:15:31 +02:00
parent dbaf4f7746
commit 115eb4d200
2 changed files with 152 additions and 129 deletions

View File

@ -13,7 +13,7 @@ slog = { version = "2.0.0" }
slog-stdlog = "2.0.0-0.2"
libloading = "0.4.0"
wayland-client = { version = "~0.8.6", optional = true }
winit = { version = "~0.6.4", optional = true }
winit = { git = "https://github.com/tomaka/winit.git", optional = true }
glium = { version = "~0.16.0", optional = true }
input = { version = "~0.1.1", optional = true }
clippy = { version = "*", optional = true }

View File

@ -15,7 +15,7 @@ use std::error::Error;
use std::fmt;
use std::rc::Rc;
use wayland_client::egl as wegl;
use winit::{CreationError as WinitCreationError, ElementState, Event, EventsLoop,
use winit::{CreationError as WinitCreationError, ElementState, Event, EventsLoop, KeyboardInput,
MouseButton as WinitMouseButton, MouseCursor, MouseScrollDelta, Touch, TouchPhase, Window,
WindowBuilder, WindowEvent};
use winit::os::unix::{WindowExt, get_x11_xconnection};
@ -45,7 +45,10 @@ pub struct WinitInputBackend {
/// graphics backend trait and a corresponding `WinitInputBackend`, which implements
/// the `InputBackend` trait
pub fn init() -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> {
init_from_builder(WindowBuilder::new())
init_from_builder(WindowBuilder::new()
.with_dimensions(1280, 800)
.with_title("Smithay")
.with_visibility(true))
}
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
@ -85,14 +88,20 @@ pub fn init_from_builder_with_gl_attr(builder: WindowBuilder, attributes: GlAttr
};
let context = unsafe {
EGLContext::new(native,
match EGLContext::new(native,
attributes,
PixelFormatRequirements {
hardware_accelerated: Some(true),
color_bits: Some(24),
alpha_bits: Some(8),
..Default::default()
})?
}) {
Ok(context) => context,
Err(err) => {
println!("EGLContext creation failed:\n{}", err);
return Err(err);
}
}
};
Ok((WinitGraphicsBackend {
@ -182,7 +191,7 @@ impl fmt::Display for WinitInputError {
/// Winit-Backend internal event wrapping winit's types into a `KeyboardKeyEvent`
pub struct WinitKeyboardInputEvent {
time: u32,
key: u8,
key: u32,
count: u32,
state: ElementState,
}
@ -195,7 +204,7 @@ impl BackendEvent for WinitKeyboardInputEvent {
impl KeyboardKeyEvent for WinitKeyboardInputEvent {
fn key_code(&self) -> u32 {
self.key as u32
self.key
}
fn state(&self) -> KeyState {
@ -212,8 +221,8 @@ impl KeyboardKeyEvent for WinitKeyboardInputEvent {
pub struct WinitMouseMovedEvent {
window: Rc<Window>,
time: u32,
x: i32,
y: i32,
x: f64,
y: f64,
}
impl BackendEvent for WinitMouseMovedEvent {
@ -224,23 +233,24 @@ impl BackendEvent for WinitMouseMovedEvent {
impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent {
fn x(&self) -> f64 {
self.x as f64
self.x
}
fn y(&self) -> f64 {
self.y as f64
self.y
}
fn x_transformed(&self, width: u32) -> u32 {
cmp::min(self.x * width as i32 /
self.window.get_inner_size_points().unwrap_or((width, 0)).0 as i32,
0) as u32
cmp::min((self.x * width as f64 /
self.window.get_inner_size_points().unwrap_or((width, 0)).0 as f64) as u32,
0)
}
fn y_transformed(&self, height: u32) -> u32 {
cmp::min(self.y * height as i32 /
self.window.get_inner_size_points().unwrap_or((0, height)).1 as i32,
0) as u32
cmp::min((self.y * height as f64 /
self.window.get_inner_size_points().unwrap_or((0, height)).1 as f64) as
u32,
0)
}
}
@ -484,16 +494,19 @@ impl InputBackend for WinitInputBackend {
let mut handler = self.handler.as_mut();
self.events_loop
.poll_events(move |event| if let Some(ref mut handler) = handler {
let Event::WindowEvent { event, .. } = event;
match event {
WindowEvent::Resized(x, y) => {
.poll_events(move |event| match event {
Event::WindowEvent { event, .. } => {
match (event, handler.as_mut()) {
(WindowEvent::Resized(x, y), _) => {
window.set_inner_size(x, y);
if let Some(wl_egl_surface) = surface.as_ref() {
wl_egl_surface.resize(x as i32, y as i32, 0, 0);
}
}
WindowEvent::KeyboardInput(state, key_code, _, _) => {
(WindowEvent::KeyboardInput {
input: KeyboardInput { scancode, state, .. }, ..
},
Some(handler)) => {
match state {
ElementState::Pressed => *key_counter += 1,
ElementState::Released => {
@ -503,12 +516,13 @@ impl InputBackend for WinitInputBackend {
handler.on_keyboard_key(seat,
WinitKeyboardInputEvent {
time: *time_counter,
key: key_code,
key: scancode,
count: *key_counter,
state: state,
})
}
WindowEvent::MouseMoved(x, y) => {
(WindowEvent::MouseMoved { position: (x, y), .. },
Some(handler)) => {
handler.on_pointer_move_absolute(seat,
WinitMouseMovedEvent {
window: window.clone(),
@ -517,7 +531,7 @@ impl InputBackend for WinitInputBackend {
y: y,
})
}
WindowEvent::MouseWheel(delta, _) => {
(WindowEvent::MouseWheel { delta, .. }, Some(handler)) => {
let event = WinitMouseWheelEvent {
axis: Axis::Horizontal,
time: *time_counter,
@ -535,7 +549,7 @@ impl InputBackend for WinitInputBackend {
}
}
}
WindowEvent::MouseInput(state, button) => {
(WindowEvent::MouseInput { state, button, .. }, Some(handler)) => {
handler.on_pointer_button(seat,
WinitMouseInputEvent {
time: *time_counter,
@ -543,11 +557,13 @@ impl InputBackend for WinitInputBackend {
state: state,
})
}
WindowEvent::Touch(Touch {
(WindowEvent::Touch(Touch {
phase: TouchPhase::Started,
location: (x, y),
id,
}) => {
..
}),
Some(handler)) => {
handler.on_touch_down(seat,
WinitTouchStartedEvent {
window: window.clone(),
@ -556,11 +572,13 @@ impl InputBackend for WinitInputBackend {
id: id,
})
}
WindowEvent::Touch(Touch {
(WindowEvent::Touch(Touch {
phase: TouchPhase::Moved,
location: (x, y),
id,
}) => {
..
}),
Some(handler)) => {
handler.on_touch_motion(seat,
WinitTouchMovedEvent {
window: window.clone(),
@ -569,11 +587,13 @@ impl InputBackend for WinitInputBackend {
id: id,
})
}
WindowEvent::Touch(Touch {
(WindowEvent::Touch(Touch {
phase: TouchPhase::Ended,
location: (x, y),
id,
}) => {
..
}),
Some(handler)) => {
handler.on_touch_motion(seat,
WinitTouchMovedEvent {
window: window.clone(),
@ -587,21 +607,24 @@ impl InputBackend for WinitInputBackend {
id: id,
});
}
WindowEvent::Touch(Touch {
(WindowEvent::Touch(Touch {
phase: TouchPhase::Cancelled,
id,
..
}) => {
}),
Some(handler)) => {
handler.on_touch_cancel(seat,
WinitTouchCancelledEvent {
time: *time_counter,
id: id,
})
}
WindowEvent::Closed => *closed_ptr = true,
(WindowEvent::Closed, _) => *closed_ptr = true,
_ => {}
}
*time_counter += 1;
}
Event::DeviceEvent { .. } => {}
});
}