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" slog-stdlog = "2.0.0-0.2"
libloading = "0.4.0" libloading = "0.4.0"
wayland-client = { version = "~0.8.6", optional = true } 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 } glium = { version = "~0.16.0", optional = true }
input = { version = "~0.1.1", optional = true } input = { version = "~0.1.1", optional = true }
clippy = { version = "*", optional = true } clippy = { version = "*", optional = true }

View File

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