From ae375624ac96461f898aded7e7dd77253821f0d7 Mon Sep 17 00:00:00 2001 From: Drakulix Date: Mon, 20 Mar 2017 14:33:27 +0100 Subject: [PATCH] Fix formatting --- examples/simple.rs | 14 +- src/backend/glium.rs | 25 ++-- src/backend/glutin.rs | 238 +++++++++++++++++-------------- src/backend/graphics/opengl.rs | 3 +- src/backend/graphics/software.rs | 2 +- src/backend/input.rs | 52 +++---- 6 files changed, 179 insertions(+), 155 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index b53ff49..e0d828e 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,9 +1,9 @@ extern crate wayland_server; extern crate smithay; -use smithay::shm::ShmGlobal; use smithay::backend::glutin; use smithay::backend::input::InputBackend; +use smithay::shm::ShmGlobal; use wayland_server::protocol::wl_shm; fn main() { @@ -11,13 +11,11 @@ fn main() { // Insert the ShmGlobal as a handler to your event loop // Here, we specify tha the standard Argb8888 and Xrgb8888 is the only supported. - let handler_id = event_loop.add_handler_with_init(ShmGlobal::new( - vec![], - None // we don't provide a logger here - )); + let handler_id = + event_loop.add_handler_with_init(ShmGlobal::new(vec![], None /* we don't provide a logger here */)); // Register this handler to advertise a wl_shm global of version 1 - let shm_global = event_loop.register_global::(handler_id, 1); + let shm_global = event_loop.register_global::(handler_id, 1); // Retrieve the shm token for later use to access the buffers let shm_token = { @@ -28,9 +26,9 @@ fn main() { // Initialize a simple backend for testing let (mut renderer, mut input) = glutin::init_windowed().unwrap(); - //TODO render stuff + // TODO render stuff - //TODO put input handling on the event loop + // TODO put input handling on the event loop input.dispatch_new_events().unwrap(); event_loop.run().unwrap(); diff --git a/src/backend/glium.rs b/src/backend/glium.rs index 2faaf57..7814443 100644 --- a/src/backend/glium.rs +++ b/src/backend/glium.rs @@ -1,12 +1,12 @@ -use glium::backend::Backend; + + +use backend::graphics::opengl::{OpenglGraphicsBackend, SwapBuffersError}; use glium::SwapBuffersError as GliumSwapBuffersError; +use glium::backend::Backend; use std::os::raw::c_void; -use ::backend::graphics::opengl::{OpenglGraphicsBackend, SwapBuffersError}; - -impl From for GliumSwapBuffersError -{ +impl From for GliumSwapBuffersError { fn from(error: SwapBuffersError) -> Self { match error { SwapBuffersError::ContextLost => GliumSwapBuffersError::ContextLost, @@ -17,27 +17,22 @@ impl From for GliumSwapBuffersError pub struct GliumGraphicBackend(T); -pub trait IntoGlium: OpenglGraphicsBackend + Sized -{ +pub trait IntoGlium: OpenglGraphicsBackend + Sized { fn into_glium(self) -> GliumGraphicBackend; } -impl IntoGlium for T -{ - fn into_glium(self) -> GliumGraphicBackend - { +impl IntoGlium for T { + fn into_glium(self) -> GliumGraphicBackend { GliumGraphicBackend(self) } } -unsafe impl Backend for GliumGraphicBackend -{ +unsafe impl Backend for GliumGraphicBackend { fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> { self.0.swap_buffers().map_err(Into::into) } - unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void - { + unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void { self.0.get_proc_address(symbol) as *const c_void } diff --git a/src/backend/glutin.rs b/src/backend/glutin.rs index 6e53401..ff97418 100644 --- a/src/backend/glutin.rs +++ b/src/backend/glutin.rs @@ -1,28 +1,29 @@ //! Implementation of backend traits for types provided by `glutin` -use glutin::{ContextError, CreationError, Event, ElementState, MouseScrollDelta, Touch, TouchPhase, GlContext, HeadlessRendererBuilder, HeadlessContext, WindowBuilder, Window}; -use glutin::{Api as GlutinApi, PixelFormat as GlutinPixelFormat, MouseButton as GlutinMouseButton}; -use nix::c_void; -use std::rc::Rc; -use std::error::Error; -use std::fmt; use backend::{SeatInternal, TouchSlotInternal}; use backend::graphics::opengl::{Api, OpenglGraphicsBackend, PixelFormat, SwapBuffersError}; -use backend::input::{InputBackend, InputHandler, Seat, SeatCapabilities, KeyState, MouseButton, MouseButtonState, Axis, AxisSource, TouchEvent, TouchSlot}; +use backend::input::{Axis, AxisSource, InputBackend, InputHandler, KeyState, MouseButton, MouseButtonState, + Seat, SeatCapabilities, TouchEvent, TouchSlot}; +use glutin::{Api as GlutinApi, MouseButton as GlutinMouseButton, PixelFormat as GlutinPixelFormat}; +use glutin::{ContextError, CreationError, ElementState, Event, GlContext, HeadlessContext, + HeadlessRendererBuilder, MouseScrollDelta, Touch, TouchPhase, Window, WindowBuilder}; +use nix::c_void; +use std::error::Error; +use std::fmt; +use std::rc::Rc; /// Create a new `GlutinHeadlessRenderer` which implements the `OpenglRenderer` graphics /// backend trait -pub fn init_headless_renderer() -> Result -{ +pub fn init_headless_renderer() -> Result { init_headless_renderer_from_builder(HeadlessRendererBuilder::new(1024, 600)) } /// Create a new `GlutinHeadlessRenderer`, which implements the `OpenglRenderer` graphics /// backend trait, with a given already configured `HeadlessRendererBuilder` for /// customization -pub fn init_headless_renderer_from_builder(builder: HeadlessRendererBuilder) -> Result -{ +pub fn init_headless_renderer_from_builder(builder: HeadlessRendererBuilder) + -> Result { let (w, h) = builder.dimensions; let context = builder.build_strict()?; @@ -31,15 +32,14 @@ pub fn init_headless_renderer_from_builder(builder: HeadlessRendererBuilder) -> /// Create a new `GlutinWindowedRenderer`, which implements the `OpenglRenderer` graphics /// backend trait -pub fn init_windowed_renderer() -> Result -{ +pub fn init_windowed_renderer() -> Result { init_windowed_renderer_from_builder(WindowBuilder::new()) } /// Create a new `GlutinWindowedRenderer`, which implements the `OpenglRenderer` graphics /// backend trait, with a given already configured `WindowBuilder` for customization. -pub fn init_windowed_renderer_from_builder(builder: WindowBuilder) -> Result -{ +pub fn init_windowed_renderer_from_builder(builder: WindowBuilder) + -> Result { let window = Rc::new(builder.build_strict()?); Ok(GlutinWindowedRenderer::new(window)) } @@ -47,8 +47,7 @@ pub fn init_windowed_renderer_from_builder(builder: WindowBuilder) -> Result Result<(GlutinWindowedRenderer, GlutinInputBackend), CreationError> -{ +pub fn init_windowed() -> Result<(GlutinWindowedRenderer, GlutinInputBackend), CreationError> { init_windowed_from_builder(WindowBuilder::new()) } @@ -56,26 +55,21 @@ pub fn init_windowed() -> Result<(GlutinWindowedRenderer, GlutinInputBackend), C /// customization. Returns a `GlutinWindowedRenderer` implementing /// the `OpenglRenderer` graphics backend trait and a `GlutinInputBackend` implementing /// the `InputBackend` trait. -pub fn init_windowed_from_builder(builder: WindowBuilder) -> Result<(GlutinWindowedRenderer, GlutinInputBackend), CreationError> -{ +pub fn init_windowed_from_builder(builder: WindowBuilder) + -> Result<(GlutinWindowedRenderer, GlutinInputBackend), CreationError> { let window = Rc::new(builder.build_strict()?); - Ok(( - GlutinWindowedRenderer::new(window.clone()), - GlutinInputBackend::new(window) - )) + Ok((GlutinWindowedRenderer::new(window.clone()), GlutinInputBackend::new(window))) } /// Headless Opengl Context created by `glutin`. Implements the `OpenglGraphicsBackend` graphics /// backend trait. -pub struct GlutinHeadlessRenderer -{ +pub struct GlutinHeadlessRenderer { context: HeadlessContext, w: u32, h: u32, } -impl GlutinHeadlessRenderer -{ +impl GlutinHeadlessRenderer { fn new(context: HeadlessContext, w: u32, h: u32) -> GlutinHeadlessRenderer { GlutinHeadlessRenderer { context: context, @@ -85,8 +79,7 @@ impl GlutinHeadlessRenderer } } -impl OpenglGraphicsBackend for GlutinHeadlessRenderer -{ +impl OpenglGraphicsBackend for GlutinHeadlessRenderer { #[inline] fn swap_buffers(&self) -> Result<(), SwapBuffersError> { match self.context.swap_buffers() { @@ -127,22 +120,17 @@ impl OpenglGraphicsBackend for GlutinHeadlessRenderer /// Window with an active Opengl Context created by `glutin`. Implements the /// `OpenglGraphicsBackend` graphics backend trait. -pub struct GlutinWindowedRenderer -{ - window: Rc +pub struct GlutinWindowedRenderer { + window: Rc, } -impl GlutinWindowedRenderer -{ +impl GlutinWindowedRenderer { fn new(window: Rc) -> GlutinWindowedRenderer { - GlutinWindowedRenderer { - window: window, - } + GlutinWindowedRenderer { window: window } } } -impl OpenglGraphicsBackend for GlutinWindowedRenderer -{ +impl OpenglGraphicsBackend for GlutinWindowedRenderer { #[inline] fn swap_buffers(&self) -> Result<(), SwapBuffersError> { match self.window.swap_buffers() { @@ -159,7 +147,7 @@ impl OpenglGraphicsBackend for GlutinWindowedRenderer #[inline] fn get_framebuffer_dimensions(&self) -> (u32, u32) { - let (width, height) = self.window.get_inner_size().unwrap_or((800, 600)); // TODO: 800x600 ? + let (width, height) = self.window.get_inner_size().unwrap_or((800, 600)); // TODO: 800x600 ? let scale = self.window.hidpi_factor(); ((width as f32 * scale) as u32, (height as f32 * scale) as u32) } @@ -185,28 +173,23 @@ impl OpenglGraphicsBackend for GlutinWindowedRenderer /// Errors that may happen when driving the event loop of `GlutinInputBackend` #[derive(Debug)] -pub enum GlutinInputError -{ +pub enum GlutinInputError { /// The underlying `glutin` `Window` was closed. No further events can be processed. /// /// See `GlutinInputBackend::process_new_events`. - WindowClosed + WindowClosed, } -impl Error for GlutinInputError -{ - fn description(&self) -> &str - { +impl Error for GlutinInputError { + fn description(&self) -> &str { match *self { GlutinInputError::WindowClosed => "Glutin Window was closed", } } } -impl fmt::Display for GlutinInputError -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result - { +impl fmt::Display for GlutinInputError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) } } @@ -214,8 +197,7 @@ impl fmt::Display for GlutinInputError /// Abstracted event loop of a `glutin` `Window` implementing the `InputBackend` trait /// /// You need to call `process_new_events` periodically to receive any events. -pub struct GlutinInputBackend -{ +pub struct GlutinInputBackend { window: Rc, time_counter: u32, seat: Seat, @@ -223,8 +205,7 @@ pub struct GlutinInputBackend handler: Option + 'static>>, } -impl InputBackend for GlutinInputBackend -{ +impl InputBackend for GlutinInputBackend { type InputConfig = (); type EventError = GlutinInputError; @@ -236,8 +217,7 @@ impl InputBackend for GlutinInputBackend self.handler = Some(Box::new(handler)); } - fn get_handler(&mut self) -> Option<&mut InputHandler> - { + fn get_handler(&mut self) -> Option<&mut InputHandler> { self.handler.as_mut().map(|handler| handler as &mut InputHandler) } @@ -271,42 +251,96 @@ impl InputBackend for GlutinInputBackend /// /// The linked `GlutinWindowedRenderer` will error with a lost Context and should /// not be used anymore as well. - fn dispatch_new_events(&mut self) -> Result<(), GlutinInputError> - { - for event in self.window.poll_events() - { + fn dispatch_new_events(&mut self) -> Result<(), GlutinInputError> { + for event in self.window.poll_events() { if let Some(ref mut handler) = self.handler { match event { - Event::KeyboardInput(state, key_code, _) => handler.on_keyboard_key(&self.seat, self.time_counter, key_code as u32, state.into(), 1), - Event::MouseMoved(x, y) => handler.on_pointer_move(&self.seat, self.time_counter, (x as u32, y as u32)), - Event::MouseWheel(delta, _) => match delta { - MouseScrollDelta::LineDelta(x, y) => { - if x != 0.0 { - handler.on_pointer_scroll(&self.seat, self.time_counter, Axis::Horizontal, AxisSource::Wheel, x as f64); - } - if y != 0.0 { - handler.on_pointer_scroll(&self.seat, self.time_counter, Axis::Vertical, AxisSource::Wheel, y as f64); - } - }, - MouseScrollDelta::PixelDelta(x, y) => { - if x != 0.0 { - handler.on_pointer_scroll(&self.seat, self.time_counter, Axis::Vertical, AxisSource::Continous, x as f64); - } - if y != 0.0 { - handler.on_pointer_scroll(&self.seat, self.time_counter, Axis::Horizontal, AxisSource::Continous, y as f64); - } - }, - }, - Event::MouseInput(state, button) => handler.on_pointer_button(&self.seat, self.time_counter, button.into(), state.into()), - Event::Touch(Touch { phase: TouchPhase::Started, location: (x, y), id}) => handler.on_touch(&self.seat, self.time_counter, TouchEvent::Down { slot: Some(TouchSlot::new(id as u32)), x: x, y: y }), - Event::Touch(Touch { phase: TouchPhase::Moved, location: (x, y), id}) => handler.on_touch(&self.seat, self.time_counter, TouchEvent::Motion { slot: Some(TouchSlot::new(id as u32)), x: x, y: y }), - Event::Touch(Touch { phase: TouchPhase::Ended, location: (x, y), id }) => { - handler.on_touch(&self.seat, self.time_counter, TouchEvent::Motion { slot: Some(TouchSlot::new(id as u32)), x: x, y: y }); - handler.on_touch(&self.seat, self.time_counter, TouchEvent::Up { slot: Some(TouchSlot::new(id as u32)) }); + Event::KeyboardInput(state, key_code, _) => { + handler.on_keyboard_key(&self.seat, + self.time_counter, + key_code as u32, + state.into(), + 1) + } + Event::MouseMoved(x, y) => { + handler.on_pointer_move(&self.seat, self.time_counter, (x as u32, y as u32)) + } + Event::MouseWheel(delta, _) => { + match delta { + MouseScrollDelta::LineDelta(x, y) => { + if x != 0.0 { + handler.on_pointer_scroll(&self.seat, + self.time_counter, + Axis::Horizontal, + AxisSource::Wheel, + x as f64); + } + if y != 0.0 { + handler.on_pointer_scroll(&self.seat, + self.time_counter, + Axis::Vertical, + AxisSource::Wheel, + y as f64); + } + } + MouseScrollDelta::PixelDelta(x, y) => { + if x != 0.0 { + handler.on_pointer_scroll(&self.seat, + self.time_counter, + Axis::Vertical, + AxisSource::Continous, + x as f64); + } + if y != 0.0 { + handler.on_pointer_scroll(&self.seat, + self.time_counter, + Axis::Horizontal, + AxisSource::Continous, + y as f64); + } + } + } + } + Event::MouseInput(state, button) => { + handler.on_pointer_button(&self.seat, self.time_counter, button.into(), state.into()) + } + Event::Touch(Touch { phase: TouchPhase::Started, location: (x, y), id }) => { + handler.on_touch(&self.seat, + self.time_counter, + TouchEvent::Down { + slot: Some(TouchSlot::new(id as u32)), + x: x, + y: y, + }) + } + Event::Touch(Touch { phase: TouchPhase::Moved, location: (x, y), id }) => { + handler.on_touch(&self.seat, + self.time_counter, + TouchEvent::Motion { + slot: Some(TouchSlot::new(id as u32)), + x: x, + y: y, + }) + } + Event::Touch(Touch { phase: TouchPhase::Ended, location: (x, y), id }) => { + handler.on_touch(&self.seat, + self.time_counter, + TouchEvent::Motion { + slot: Some(TouchSlot::new(id as u32)), + x: x, + y: y, + }); + handler.on_touch(&self.seat, + self.time_counter, + TouchEvent::Up { slot: Some(TouchSlot::new(id as u32)) }); + } + Event::Touch(Touch { phase: TouchPhase::Cancelled, id, .. }) => { + handler.on_touch(&self.seat, + self.time_counter, + TouchEvent::Cancel { slot: Some(TouchSlot::new(id as u32)) }) } - Event::Touch(Touch { phase: TouchPhase::Cancelled, id, ..}) => handler.on_touch(&self.seat, self.time_counter, TouchEvent::Cancel { slot: Some(TouchSlot::new(id as u32)) }), Event::Closed => return Err(GlutinInputError::WindowClosed), - _ => {}, + _ => {} } self.time_counter += 1; } @@ -315,18 +349,17 @@ impl InputBackend for GlutinInputBackend } } -impl GlutinInputBackend -{ - fn new(window: Rc) -> GlutinInputBackend - { +impl GlutinInputBackend { + fn new(window: Rc) -> GlutinInputBackend { GlutinInputBackend { window: window, time_counter: 0, - seat: Seat::new(0, SeatCapabilities { - pointer: true, - keyboard: true, - touch: true, - }), + seat: Seat::new(0, + SeatCapabilities { + pointer: true, + keyboard: true, + touch: true, + }), input_config: (), handler: None, } @@ -359,8 +392,7 @@ impl From for PixelFormat { } } -impl From for MouseButton -{ +impl From for MouseButton { fn from(button: GlutinMouseButton) -> MouseButton { match button { GlutinMouseButton::Left => MouseButton::Left, @@ -371,8 +403,7 @@ impl From for MouseButton } } -impl From for KeyState -{ +impl From for KeyState { fn from(state: ElementState) -> Self { match state { ElementState::Pressed => KeyState::Pressed, @@ -381,8 +412,7 @@ impl From for KeyState } } -impl From for MouseButtonState -{ +impl From for MouseButtonState { fn from(state: ElementState) -> Self { match state { ElementState::Pressed => MouseButtonState::Pressed, diff --git a/src/backend/graphics/opengl.rs b/src/backend/graphics/opengl.rs index cf6f331..a03f93b 100644 --- a/src/backend/graphics/opengl.rs +++ b/src/backend/graphics/opengl.rs @@ -61,8 +61,7 @@ pub struct PixelFormat { /// Trait that describes objects that have an OpenGl context /// and can be used to render upon -pub trait OpenglGraphicsBackend -{ +pub trait OpenglGraphicsBackend { /// Swaps buffers at the end of a frame. fn swap_buffers(&self) -> Result<(), SwapBuffersError>; diff --git a/src/backend/graphics/software.rs b/src/backend/graphics/software.rs index d6c732a..278885a 100644 --- a/src/backend/graphics/software.rs +++ b/src/backend/graphics/software.rs @@ -1,7 +1,7 @@ //! Common traits and types used for software rendering on graphics backends -use wayland_server::protocol::wl_shm::Format; use std::error::Error; +use wayland_server::protocol::wl_shm::Format; /// Trait that describes objects providing a software rendering implementation pub trait CpuGraphicsBackend { diff --git a/src/backend/input.rs b/src/backend/input.rs index b02bd6c..03d0ca5 100644 --- a/src/backend/input.rs +++ b/src/backend/input.rs @@ -1,4 +1,5 @@ //! Common traits for input backends to receive input from. + use backend::{SeatInternal, TouchSlotInternal}; use std::error::Error; @@ -12,20 +13,23 @@ use std::error::Error; /// /// Seats can be checked for equality and hashed for differentiation. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Seat { id: u32, capabilities: SeatCapabilities } +pub struct Seat { + id: u32, + capabilities: SeatCapabilities, +} -impl SeatInternal for Seat -{ - fn new(id: u32, capabilities: SeatCapabilities) -> Seat - { - Seat { id: id, capabilities: capabilities } +impl SeatInternal for Seat { + fn new(id: u32, capabilities: SeatCapabilities) -> Seat { + Seat { + id: id, + capabilities: capabilities, + } } } impl Seat { /// Get the currently capabilities of this `Seat` - pub fn capabilities(&self) -> &SeatCapabilities - { + pub fn capabilities(&self) -> &SeatCapabilities { &self.capabilities } } @@ -38,7 +42,7 @@ pub struct SeatCapabilities { /// `Seat` has a keyboard pub keyboard: bool, /// `Seat` has a touchscreen - pub touch: bool + pub touch: bool, } /// State of key on a keyboard. Either pressed or released @@ -74,8 +78,7 @@ pub enum MouseButtonState { /// Axis when scrolling #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum Axis -{ +pub enum Axis { /// Vertical axis Vertical, /// Horizonal axis @@ -84,8 +87,7 @@ pub enum Axis /// Source of an axis when scrolling #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum AxisSource -{ +pub enum AxisSource { /// Finger. Mostly used for trackpads. /// /// Guarantees that a scroll sequence is terminated with a scroll value of 0. @@ -122,20 +124,19 @@ pub enum AxisSource /// fingers on a multi-touch enabled input device. Events should only /// be interpreted in the context of other events on the same slot. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TouchSlot { id: u32 } +pub struct TouchSlot { + id: u32, +} -impl TouchSlotInternal for TouchSlot -{ - fn new(id: u32) -> Self - { +impl TouchSlotInternal for TouchSlot { + fn new(id: u32) -> Self { TouchSlot { id: id } } } /// Touch event #[derive(Debug, PartialEq, Clone, Copy)] -pub enum TouchEvent -{ +pub enum TouchEvent { /// The start of an event at a given position (x, y). /// /// If the device has multi-touch capabilities a slot is given. @@ -145,7 +146,7 @@ pub enum TouchEvent /// Absolute x-coordinate of the touch position. x: f64, /// Absolute y-coordinate of the touch position. - y: f64 + y: f64, }, /// Movement of a touch on the device surface to a given position (x, y). /// @@ -156,23 +157,24 @@ pub enum TouchEvent /// Absolute x-coordinate of the final touch position after the motion. x: f64, /// Absolute y-coordinate of the final touch position after the motion. - y: f64 }, + y: f64, + }, /// Stop of an event chain. /// /// If the device has multi-touch capabilities a slot is given. Up { /// `TouchSlot`, if the device has multi-touch capabilities - slot: Option + slot: Option, }, /// Cancel of an event chain. All previous events in the chain should be ignored. /// /// If the device has multi-touch capabilities a slot is given. Cancel { /// `TouchSlot`, if the device has multi-touch capabilities - slot: Option + slot: Option, }, /// Signals the end of a set of touchpoints at one device sample time. - Frame + Frame, } /// Trait that describes objects providing a source of input events. All input backends