From 9c3c3d939e803d8dd727d683e665a04fb45439b8 Mon Sep 17 00:00:00 2001 From: Victor Brekenfeld Date: Sun, 28 Jun 2020 00:07:48 +0200 Subject: [PATCH] cargo fmt & lint --- anvil/src/buffer_utils.rs | 59 +++++++++++-------- anvil/src/glium_drawer.rs | 86 ++++++++++++---------------- anvil/src/input_handler.rs | 25 ++++---- anvil/src/state.rs | 7 +-- anvil/src/udev.rs | 77 +++++++++++++++---------- anvil/src/winit.rs | 4 +- src/backend/drm/atomic/surface.rs | 2 +- src/backend/drm/eglstream/surface.rs | 3 +- src/backend/drm/gbm/surface.rs | 9 +-- src/backend/drm/legacy/surface.rs | 2 +- src/backend/session/mod.rs | 4 +- src/reexports.rs | 2 +- 12 files changed, 148 insertions(+), 132 deletions(-) diff --git a/anvil/src/buffer_utils.rs b/anvil/src/buffer_utils.rs index e42b650..8509c11 100644 --- a/anvil/src/buffer_utils.rs +++ b/anvil/src/buffer_utils.rs @@ -1,9 +1,3 @@ -use std::collections::HashMap; -#[cfg(feature = "egl")] -use std::{ - cell::RefCell, - rc::Rc, -}; use glium::texture::Texture2d; #[cfg(feature = "egl")] use glium::{ @@ -11,9 +5,14 @@ use glium::{ GlObject, }; use slog::Logger; +use std::collections::HashMap; +#[cfg(feature = "egl")] +use std::{cell::RefCell, rc::Rc}; #[cfg(feature = "egl")] -use smithay::backend::egl::{display::EGLBufferReader, EGLImages, BufferAccessError as EGLBufferAccessError, Format}; +use smithay::backend::egl::{ + display::EGLBufferReader, BufferAccessError as EGLBufferAccessError, EGLImages, Format, +}; use smithay::{ backend::graphics::gl::GLGraphicsBackend, reexports::wayland_server::protocol::wl_buffer::WlBuffer, @@ -65,11 +64,10 @@ impl BufferUtils { /// Returns the dimensions of an image stored in the shm buffer. fn shm_buffer_dimensions(&self, buffer: &WlBuffer) -> Result<(i32, i32), BufferAccessError> { - shm_buffer_contents(buffer, |_, data| (data.width, data.height)) - .map_err(|err| { - warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err)); - err - }) + shm_buffer_contents(buffer, |_, data| (data.width, data.height)).map_err(|err| { + warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err)); + err + }) } #[cfg(feature = "egl")] @@ -111,13 +109,14 @@ impl BufferUtils { } fn load_shm_buffer(&self, buffer: WlBuffer) -> Result { - let (width, height, format) = match shm_buffer_contents(&buffer, |_, data| (data.width, data.height, data.format)) { - Ok(x) => x, - Err(err) => { - warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err)); - return Err(buffer); - } - }; + let (width, height, format) = + match shm_buffer_contents(&buffer, |_, data| (data.width, data.height, data.format)) { + Ok(x) => x, + Err(err) => { + warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err)); + return Err(buffer); + } + }; let shader = match crate::shm_load::load_format(format) { Ok(x) => x.1, Err(format) => { @@ -151,12 +150,16 @@ pub struct BufferTextures { impl BufferTextures { #[cfg(feature = "egl")] - pub fn load_texture<'a, F: GLGraphicsBackend + 'static>(&'a mut self, drawer: &GliumDrawer) -> Result<&'a Texture2d, ()> { + pub fn load_texture<'a, F: GLGraphicsBackend + 'static>( + &'a mut self, + drawer: &GliumDrawer, + ) -> Result<&'a Texture2d, ()> { if self.textures.contains_key(&drawer.id) { return Ok(&self.textures[&drawer.id]); } - if let Some(images) = self.images.as_ref() { //EGL buffer + if let Some(images) = self.images.as_ref() { + //EGL buffer let format = match images.format { Format::RGB => UncompressedFloatFormat::U8U8U8, Format::RGBA => UncompressedFloatFormat::U8U8U8U8, @@ -189,7 +192,10 @@ impl BufferTextures { } #[cfg(not(feature = "egl"))] - pub fn load_texture<'a, F: GLGraphicsBackend + 'static>(&'a mut self, drawer: &GliumDrawer) -> Result<&'a Texture2d, ()> { + pub fn load_texture<'a, F: GLGraphicsBackend + 'static>( + &'a mut self, + drawer: &GliumDrawer, + ) -> Result<&'a Texture2d, ()> { if self.textures.contains_key(&drawer.id) { return Ok(&self.textures[&drawer.id]); } @@ -197,7 +203,10 @@ impl BufferTextures { self.load_shm_texture(drawer) } - fn load_shm_texture<'a, F: GLGraphicsBackend + 'static>(&'a mut self, drawer: &GliumDrawer) -> Result<&'a Texture2d, ()> { + fn load_shm_texture<'a, F: GLGraphicsBackend + 'static>( + &'a mut self, + drawer: &GliumDrawer, + ) -> Result<&'a Texture2d, ()> { match shm_buffer_contents(&self.buffer, |slice, data| { crate::shm_load::load_shm_buffer(data, slice) .map(|(image, _kind)| Texture2d::new(&drawer.display, image).unwrap()) @@ -205,7 +214,7 @@ impl BufferTextures { Ok(Ok(texture)) => { self.textures.insert(drawer.id, texture); Ok(&self.textures[&drawer.id]) - }, + } Ok(Err(format)) => { warn!(self.logger, "Unsupported SHM buffer format"; "format" => format!("{:?}", format)); Err(()) @@ -222,4 +231,4 @@ impl Drop for BufferTextures { fn drop(&mut self) { self.buffer.release() } -} \ No newline at end of file +} diff --git a/anvil/src/glium_drawer.rs b/anvil/src/glium_drawer.rs index d2bc935..bcf2a76 100644 --- a/anvil/src/glium_drawer.rs +++ b/anvil/src/glium_drawer.rs @@ -1,28 +1,19 @@ use std::{ cell::{Ref, RefCell}, rc::Rc, - sync::atomic::{AtomicUsize, AtomicBool, Ordering}, + sync::atomic::{AtomicBool, AtomicUsize, Ordering}, }; -use glium::{ - self, - index::PrimitiveType, - texture::Texture2d, - Surface, -}; +use glium::{self, index::PrimitiveType, texture::Texture2d, Surface}; use slog::Logger; use smithay::{ backend::graphics::{ - CursorBackend, gl::GLGraphicsBackend, glium::{Frame, GliumGraphicsBackend}, - SwapBuffersError, - }, - reexports::{ - calloop::LoopHandle, - wayland_server::protocol::wl_surface, + CursorBackend, SwapBuffersError, }, + reexports::{calloop::LoopHandle, wayland_server::protocol::wl_surface}, utils::Rectangle, wayland::{ compositor::{roles::Role, SubsurfaceRole, TraversalAction}, @@ -68,11 +59,7 @@ impl GliumDrawer { } impl> + GLGraphicsBackend + 'static> GliumDrawer { - pub fn init( - backend: T, - buffer_loader: BufferUtils, - log: Logger, - ) -> GliumDrawer { + pub fn init(backend: T, buffer_loader: BufferUtils, log: Logger) -> GliumDrawer { let display = backend.into(); // building the vertex buffer, which contains all the vertices that we will draw @@ -127,13 +114,14 @@ impl GliumDrawer { ) { let (x, y) = position; let _ = self.display.borrow().set_cursor_position(x as u32, y as u32); - if !self.hardware_cursor.swap(true, Ordering::SeqCst) { - if let Err(_) = self.display.borrow().set_cursor_representation(cursor, hotspot) { - warn!( - self.log, - "Failed to upload hardware cursor", - ); - } + if !self.hardware_cursor.swap(true, Ordering::SeqCst) + && self + .display + .borrow() + .set_cursor_representation(cursor, hotspot) + .is_err() + { + warn!(self.log, "Failed to upload hardware cursor",); } } @@ -160,15 +148,15 @@ impl GliumDrawer { } pub fn clear_cursor(&self) { - if self.hardware_cursor.swap(false, Ordering::SeqCst) { - if let Err(_) = self.display.borrow().clear_cursor_representation() { - warn!(self.log, "Failed to clear cursor"); - } + if self.hardware_cursor.swap(false, Ordering::SeqCst) + && self.display.borrow().clear_cursor_representation().is_err() + { + warn!(self.log, "Failed to clear cursor"); } } } -// I would love to do this, but this is essentially specialization... +// I would love to do this (check on !CursorBackend), but this is essentially specialization... // And since this is just an example compositor, it seems we require now, // that for the use of software cursors we need the hardware cursor trait (to do automatic cleanup..) /* @@ -347,25 +335,27 @@ impl GliumDrawer { // redraw the frame, in a simple but inneficient way { let screen_dimensions = self.borrow().get_framebuffer_dimensions(); - window_map.with_windows_from_bottom_to_top(|toplevel_surface, mut initial_place, bounding_box| { - // skip windows that do not overlap with a given output - if let Some(output) = output_rect { - if !output.overlaps(bounding_box) { - return; + window_map.with_windows_from_bottom_to_top( + |toplevel_surface, mut initial_place, bounding_box| { + // skip windows that do not overlap with a given output + if let Some(output) = output_rect { + if !output.overlaps(bounding_box) { + return; + } + initial_place.0 -= output.x; } - initial_place.0 -= output.x; - } - if let Some(wl_surface) = toplevel_surface.get_surface() { - // this surface is a root of a subsurface tree that needs to be drawn - self.draw_surface_tree( - frame, - &wl_surface, - initial_place, - compositor_token, - screen_dimensions, - ); - } - }); + if let Some(wl_surface) = toplevel_surface.get_surface() { + // this surface is a root of a subsurface tree that needs to be drawn + self.draw_surface_tree( + frame, + &wl_surface, + initial_place, + compositor_token, + screen_dimensions, + ); + } + }, + ); } } diff --git a/anvil/src/input_handler.rs b/anvil/src/input_handler.rs index 17174e1..9d288e6 100644 --- a/anvil/src/input_handler.rs +++ b/anvil/src/input_handler.rs @@ -81,18 +81,20 @@ impl AnvilState { "err" => format!("{:?}", e) ); } - }, + } #[cfg(feature = "udev")] KeyAction::Screen(num) => { - let output_map = self.output_map - .as_ref().unwrap(); + let output_map = self.output_map.as_ref().unwrap(); let outputs = output_map.borrow(); if let Some(output) = outputs.get(num) { - let x = outputs.iter().take(num).fold(0, |acc, output| acc + output.size.0) as f64 + (output.size.0 as f64 / 2.0); + let x = outputs + .iter() + .take(num) + .fold(0, |acc, output| acc + output.size.0) as f64 + + (output.size.0 as f64 / 2.0); let y = output.size.1 as f64 / 2.0; *self.pointer_location.borrow_mut() = (x as f64, y as f64) } - } _ => (), } @@ -104,7 +106,7 @@ impl AnvilState { let mut location = self.pointer_location.borrow_mut(); location.0 += x as f64; location.1 += y as f64; - + #[cfg(feature = "udev")] { // clamp to screen limits @@ -149,19 +151,16 @@ impl AnvilState { #[cfg(feature = "udev")] fn clamp_coords(&self, pos: (f64, f64)) -> (f64, f64) { - let output_map = self.output_map - .as_ref().unwrap(); + let output_map = self.output_map.as_ref().unwrap(); let outputs = output_map.borrow(); - + if outputs.len() == 0 { return pos; } let (mut x, mut y) = pos; // max_x is the sum of the width of all outputs - let max_x = outputs - .iter() - .fold(0u32, |acc, output| acc + output.size.0); + let max_x = outputs.iter().fold(0u32, |acc, output| acc + output.size.0); x = x.max(0.0).min(max_x as f64); // max y depends on the current output @@ -170,7 +169,7 @@ impl AnvilState { (x, y) } - + #[cfg(feature = "udev")] fn current_output_idx(&self, x: f64) -> usize { let output_map = self.output_map.as_ref().unwrap(); diff --git a/anvil/src/state.rs b/anvil/src/state.rs index edc4c62..48bc6ce 100644 --- a/anvil/src/state.rs +++ b/anvil/src/state.rs @@ -24,14 +24,11 @@ use smithay::{ }; #[cfg(feature = "udev")] -use smithay::backend::session::{ - auto::AutoSession, - Session, -}; +use smithay::backend::session::{auto::AutoSession, Session}; -use crate::{buffer_utils::BufferUtils, shell::init_shell}; #[cfg(feature = "udev")] use crate::udev::MyOutput; +use crate::{buffer_utils::BufferUtils, shell::init_shell}; pub struct AnvilState { pub socket_name: String, diff --git a/anvil/src/udev.rs b/anvil/src/udev.rs index f2fd041..0f33b28 100644 --- a/anvil/src/udev.rs +++ b/anvil/src/udev.rs @@ -112,7 +112,7 @@ pub fn run_udev( let buffer_utils = BufferUtils::new(egl_buffer_reader.clone(), log.clone()); #[cfg(not(feature = "egl"))] let buffer_utils = BufferUtils::new(log.clone()); - + let output_map = Rc::new(RefCell::new(Vec::new())); /* @@ -143,7 +143,7 @@ pub fn run_udev( let mut udev_handler = UdevHandlerImpl { compositor_token: state.ctoken, - buffer_utils: buffer_utils, + buffer_utils, #[cfg(feature = "egl")] egl_buffer_reader, session: state.session.clone().unwrap(), @@ -164,7 +164,6 @@ pub fn run_udev( /* * Initialize a fake output (we render one screen to every device in this example) */ - /* * Initialize libinput backend @@ -238,7 +237,13 @@ pub struct MyOutput { } impl MyOutput { - fn new(display: &mut Display, device_id: dev_t, crtc: crtc::Handle, conn: ConnectorInfo, logger: ::slog::Logger) -> MyOutput { + fn new( + display: &mut Display, + device_id: dev_t, + crtc: crtc::Handle, + conn: ConnectorInfo, + logger: ::slog::Logger, + ) -> MyOutput { let (output, global) = Output::new( display, format!("{:?}", conn.interface()), @@ -351,7 +356,13 @@ impl UdevHandlerImpl { buffer_utils.clone(), logger.clone(), ); - output_map.push(MyOutput::new(display, device.device_id(), crtc, connector_info, logger.clone())); + output_map.push(MyOutput::new( + display, + device.device_id(), + crtc, + connector_info, + logger.clone(), + )); entry.insert(Rc::new(renderer)); break 'outer; @@ -408,7 +419,10 @@ impl UdevHandlerImpl { #[cfg(feature = "egl")] { if path.canonicalize().ok() == self.primary_gpu { - info!(self.logger, "Initializing EGL Hardware Acceleration via {:?}", path); + info!( + self.logger, + "Initializing EGL Hardware Acceleration via {:?}", path + ); *self.egl_buffer_reader.borrow_mut() = device.bind_wl_display(&*self.display.borrow()).ok(); } @@ -484,13 +498,13 @@ impl UdevHandlerImpl { self.loop_handle .with_source(&backend_data.event_source, |source| { let mut backends = backend_data.surfaces.borrow_mut(); - *backends = - UdevHandlerImpl::::scan_connectors( - &mut source.file, - buffer_utils, - &mut *display, - &mut *output_map, - logger); + *backends = UdevHandlerImpl::::scan_connectors( + &mut source.file, + buffer_utils, + &mut *display, + &mut *output_map, + logger, + ); for renderer in backends.values() { // render first frame @@ -507,7 +521,9 @@ impl UdevHandlerImpl { backend_data.surfaces.borrow_mut().clear(); debug!(self.logger, "Surfaces dropped"); // clear outputs - self.output_map.borrow_mut().retain(|output| output.device_id != device); + self.output_map + .borrow_mut() + .retain(|output| output.device_id != device); let device = self.loop_handle.remove(backend_data.event_source).unwrap(); @@ -582,16 +598,20 @@ impl DrmRenderer { ) { if let Some(drawer) = self.backends.borrow().get(&crtc) { // get output coordinates - let (x, y) = self.output_map.borrow() + let (x, y) = self + .output_map + .borrow() .iter() .take_while(|output| output.device_id != self.device_id || output.crtc != crtc) .fold((0u32, 0u32), |pos, output| (pos.0 + output.size.0, pos.1)); - let (width, height) = self.output_map.borrow() + let (width, height) = self + .output_map + .borrow() .iter() .find(|output| output.device_id == self.device_id && output.crtc == crtc) .map(|output| output.size) .unwrap_or((0, 0)); // in this case the output will be removed. - + // and draw in sync with our monitor let mut frame = drawer.draw(); frame.clear(None, Some((0.8, 0.8, 0.9, 1.0)), false, Some(1.0), None); @@ -600,7 +620,8 @@ impl DrmRenderer { &mut frame, &*self.window_map.borrow(), Some(Rectangle { - x: x as i32, y: y as i32, + x: x as i32, + y: y as i32, width: width as i32, height: height as i32, }), @@ -614,21 +635,14 @@ impl DrmRenderer { // set cursor if ptr_x >= 0 && ptr_x < width as i32 && ptr_y >= 0 && ptr_y < height as i32 { - let _ = drawer - .borrow() - .set_cursor_position(ptr_x as u32, ptr_y as u32); - + let _ = drawer.borrow().set_cursor_position(ptr_x as u32, ptr_y as u32); + // draw the dnd icon if applicable { let guard = self.dnd_icon.lock().unwrap(); if let Some(ref surface) = *guard { if surface.as_ref().is_alive() { - drawer.draw_dnd_icon( - &mut frame, - surface, - (ptr_x, ptr_y), - self.compositor_token, - ); + drawer.draw_dnd_icon(&mut frame, surface, (ptr_x, ptr_y), self.compositor_token); } } } @@ -644,7 +658,12 @@ impl DrmRenderer { *guard = CursorImageStatus::Default; } if let CursorImageStatus::Image(ref surface) = *guard { - drawer.draw_software_cursor(&mut frame, surface, (ptr_x, ptr_y), self.compositor_token); + drawer.draw_software_cursor( + &mut frame, + surface, + (ptr_x, ptr_y), + self.compositor_token, + ); } else { drawer.draw_hardware_cursor(&self.pointer_image, (2, 2), (ptr_x, ptr_y)); } diff --git a/anvil/src/winit.rs b/anvil/src/winit.rs index 104286d..7c876b0 100644 --- a/anvil/src/winit.rs +++ b/anvil/src/winit.rs @@ -1,5 +1,7 @@ use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration}; +#[cfg(feature = "egl")] +use smithay::backend::egl::EGLGraphicsBackend; use smithay::{ backend::{graphics::gl::GLGraphicsBackend, input::InputBackend, winit}, reexports::{ @@ -13,8 +15,6 @@ use smithay::{ SERIAL_COUNTER as SCOUNTER, }, }; -#[cfg(feature = "egl")] -use smithay::backend::egl::EGLGraphicsBackend; use slog::Logger; diff --git a/src/backend/drm/atomic/surface.rs b/src/backend/drm/atomic/surface.rs index ce0bb08..96c08ef 100644 --- a/src/backend/drm/atomic/surface.rs +++ b/src/backend/drm/atomic/surface.rs @@ -1031,7 +1031,7 @@ impl CursorBackend for AtomicDrmSurface { ) -> Result<(), Error> { self.0.set_cursor_representation(buffer, hotspot) } - + fn clear_cursor_representation(&self) -> Result<(), Self::Error> { self.0.clear_cursor_representation() } diff --git a/src/backend/drm/eglstream/surface.rs b/src/backend/drm/eglstream/surface.rs index b2ca40c..0d26d88 100644 --- a/src/backend/drm/eglstream/surface.rs +++ b/src/backend/drm/eglstream/surface.rs @@ -192,7 +192,8 @@ impl CursorBackend for EglStreamSurfaceInternal { } fn clear_cursor_representation(&self) -> Result<(), Self::Error> { - self.crtc.set_cursor(self.crtc.crtc(), Option::<&DumbBuffer>::None) + self.crtc + .set_cursor(self.crtc.crtc(), Option::<&DumbBuffer>::None) .compat() .map_err(|source| DrmError::Access { errmsg: "Failed to clear cursor", diff --git a/src/backend/drm/gbm/surface.rs b/src/backend/drm/gbm/surface.rs index 2bdf3bb..56e500d 100644 --- a/src/backend/drm/gbm/surface.rs +++ b/src/backend/drm/gbm/surface.rs @@ -307,7 +307,9 @@ where } fn clear_cursor_representation(&self) -> Result<(), Self::Error> { - *self.cursor.lock().unwrap() = (self.dev.lock() + *self.cursor.lock().unwrap() = ( + self.dev + .lock() .unwrap() .create_buffer_object( 1, @@ -316,10 +318,9 @@ where BufferObjectFlags::CURSOR | BufferObjectFlags::WRITE, ) .map_err(Error::BufferCreationFailed)?, - (0, 0) + (0, 0), ); - self.crtc.clear_cursor_representation() - .map_err(Error::Underlying) + self.crtc.clear_cursor_representation().map_err(Error::Underlying) } } diff --git a/src/backend/drm/legacy/surface.rs b/src/backend/drm/legacy/surface.rs index d54b6a1..04cbe49 100644 --- a/src/backend/drm/legacy/surface.rs +++ b/src/backend/drm/legacy/surface.rs @@ -90,7 +90,7 @@ impl CursorBackend for LegacyDrmSurfaceInternal { fn clear_cursor_representation(&self) -> Result<(), Error> { self.set_cursor(self.crtc, Option::<&DumbBuffer>::None) - .compat() + .compat() .map_err(|source| Error::Access { errmsg: "Failed to set cursor", dev: self.dev_path(), diff --git a/src/backend/session/mod.rs b/src/backend/session/mod.rs index 8047cf2..5c9206d 100644 --- a/src/backend/session/mod.rs +++ b/src/backend/session/mod.rs @@ -162,7 +162,7 @@ impl AsErrno for () { pub mod auto; pub mod direct; -#[cfg(feature="backend_session_logind")] +#[cfg(feature = "backend_session_logind")] mod dbus; -#[cfg(feature="backend_session_logind")] +#[cfg(feature = "backend_session_logind")] pub use self::dbus::*; diff --git a/src/reexports.rs b/src/reexports.rs index e7ec2c8..6c9aaee 100644 --- a/src/reexports.rs +++ b/src/reexports.rs @@ -22,4 +22,4 @@ pub use wayland_protocols; #[cfg(feature = "wayland_frontend")] pub use wayland_server; #[cfg(feature = "backend_winit")] -pub use winit; \ No newline at end of file +pub use winit;