From c6838b08e2f65285f1391cec93f918cadb804117 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 7 Aug 2023 19:45:55 +0400 Subject: [PATCH] Add rustfmt.toml and reformat --- rustfmt.toml | 4 +++ src/grabs/move_grab.rs | 17 +++++----- src/grabs/resize_grab.rs | 50 ++++++++++++++++++------------ src/handlers/compositor.rs | 33 ++++++++++---------- src/handlers/mod.rs | 10 +++--- src/handlers/xdg_shell.rs | 41 +++++++++---------------- src/input.rs | 24 +++++++-------- src/main.rs | 3 +- src/state.rs | 63 +++++++++++++++++++------------------- src/winit.rs | 55 +++++++++++++++++---------------- 10 files changed, 153 insertions(+), 147 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000..ad544eeb --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,4 @@ +imports_granularity = "Module" +group_imports = "StdExternalCrate" +wrap_comments = true +comment_width = 100 diff --git a/src/grabs/move_grab.rs b/src/grabs/move_grab.rs index c9aacbec..63cdb3bd 100644 --- a/src/grabs/move_grab.rs +++ b/src/grabs/move_grab.rs @@ -1,13 +1,12 @@ -use crate::Smallvil; -use smithay::{ - desktop::Window, - input::pointer::{ - AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, - PointerInnerHandle, RelativeMotionEvent, - }, - reexports::wayland_server::protocol::wl_surface::WlSurface, - utils::{Logical, Point}, +use smithay::desktop::Window; +use smithay::input::pointer::{ + AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, + PointerInnerHandle, RelativeMotionEvent, }; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::utils::{Logical, Point}; + +use crate::Smallvil; pub struct MoveSurfaceGrab { pub start_data: PointerGrabStartData, diff --git a/src/grabs/resize_grab.rs b/src/grabs/resize_grab.rs index 6a39cfcd..ee8aae5d 100644 --- a/src/grabs/resize_grab.rs +++ b/src/grabs/resize_grab.rs @@ -1,18 +1,18 @@ -use crate::Smallvil; -use smithay::{ - desktop::{Space, Window}, - input::pointer::{ - AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, - PointerInnerHandle, RelativeMotionEvent, - }, - reexports::{ - wayland_protocols::xdg::shell::server::xdg_toplevel, wayland_server::protocol::wl_surface::WlSurface, - }, - utils::{Logical, Point, Rectangle, Size}, - wayland::{compositor, shell::xdg::SurfaceCachedState}, -}; use std::cell::RefCell; +use smithay::desktop::{Space, Window}; +use smithay::input::pointer::{ + AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, + PointerInnerHandle, RelativeMotionEvent, +}; +use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::utils::{Logical, Point, Rectangle, Size}; +use smithay::wayland::compositor; +use smithay::wayland::shell::xdg::SurfaceCachedState; + +use crate::Smallvil; + bitflags::bitflags! { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct ResizeEdge: u32 { @@ -56,7 +56,10 @@ impl ResizeSurfaceGrab { let initial_rect = initial_window_rect; ResizeSurfaceState::with(window.toplevel().wl_surface(), |state| { - *state = ResizeSurfaceState::Resizing { edges, initial_rect }; + *state = ResizeSurfaceState::Resizing { + edges, + initial_rect, + }; }); Self { @@ -101,10 +104,11 @@ impl PointerGrab for ResizeSurfaceGrab { new_window_height = (self.initial_rect.size.h as f64 + delta.y) as i32; } - let (min_size, max_size) = compositor::with_states(self.window.toplevel().wl_surface(), |states| { - let data = states.cached_state.current::(); - (data.min_size, data.max_size) - }); + let (min_size, max_size) = + compositor::with_states(self.window.toplevel().wl_surface(), |states| { + let data = states.cached_state.current::(); + (data.min_size, data.max_size) + }); let min_width = min_size.w.max(1); let min_height = min_size.h.max(1); @@ -219,8 +223,14 @@ impl ResizeSurfaceState { fn commit(&mut self) -> Option<(ResizeEdge, Rectangle)> { match *self { - Self::Resizing { edges, initial_rect } => Some((edges, initial_rect)), - Self::WaitingForLastCommit { edges, initial_rect } => { + Self::Resizing { + edges, + initial_rect, + } => Some((edges, initial_rect)), + Self::WaitingForLastCommit { + edges, + initial_rect, + } => { // The resize is done, let's go back to idle *self = Self::Idle; diff --git a/src/handlers/compositor.rs b/src/handlers/compositor.rs index fcb0dc02..99041f34 100644 --- a/src/handlers/compositor.rs +++ b/src/handlers/compositor.rs @@ -1,21 +1,18 @@ -use crate::{grabs::resize_grab, state::ClientState, Smallvil}; -use smithay::{ - backend::renderer::utils::on_commit_buffer_handler, - delegate_compositor, delegate_shm, - reexports::wayland_server::{ - protocol::{wl_buffer, wl_surface::WlSurface}, - Client, - }, - wayland::{ - buffer::BufferHandler, - compositor::{ - get_parent, is_sync_subsurface, CompositorClientState, CompositorHandler, CompositorState, - }, - shm::{ShmHandler, ShmState}, - }, +use smithay::backend::renderer::utils::on_commit_buffer_handler; +use smithay::reexports::wayland_server::protocol::wl_buffer; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::reexports::wayland_server::Client; +use smithay::wayland::buffer::BufferHandler; +use smithay::wayland::compositor::{ + get_parent, is_sync_subsurface, CompositorClientState, CompositorHandler, CompositorState, }; +use smithay::wayland::shm::{ShmHandler, ShmState}; +use smithay::{delegate_compositor, delegate_shm}; use super::xdg_shell; +use crate::grabs::resize_grab; +use crate::state::ClientState; +use crate::Smallvil; impl CompositorHandler for Smallvil { fn compositor_state(&mut self) -> &mut CompositorState { @@ -33,7 +30,11 @@ impl CompositorHandler for Smallvil { while let Some(parent) = get_parent(&root) { root = parent; } - if let Some(window) = self.space.elements().find(|w| w.toplevel().wl_surface() == &root) { + if let Some(window) = self + .space + .elements() + .find(|w| w.toplevel().wl_surface() == &root) + { window.on_commit(); } }; diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index bc2cd23f..58157b27 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,17 +1,17 @@ mod compositor; mod xdg_shell; -use crate::Smallvil; - // // Wl Seat -// - use smithay::input::{SeatHandler, SeatState}; use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; -use smithay::wayland::data_device::{ClientDndGrabHandler, DataDeviceHandler, ServerDndGrabHandler}; +use smithay::wayland::data_device::{ + ClientDndGrabHandler, DataDeviceHandler, ServerDndGrabHandler, +}; use smithay::{delegate_data_device, delegate_output, delegate_seat}; +use crate::Smallvil; + impl SeatHandler for Smallvil { type KeyboardFocus = WlSurface; type PointerFocus = WlSurface; diff --git a/src/handlers/xdg_shell.rs b/src/handlers/xdg_shell.rs index 7351d3bf..3b7f53ff 100644 --- a/src/handlers/xdg_shell.rs +++ b/src/handlers/xdg_shell.rs @@ -1,31 +1,20 @@ -use smithay::{ - delegate_xdg_shell, - desktop::{Space, Window}, - input::{ - pointer::{Focus, GrabStartData as PointerGrabStartData}, - Seat, - }, - reexports::{ - wayland_protocols::xdg::shell::server::xdg_toplevel, - wayland_server::{ - protocol::{wl_seat, wl_surface::WlSurface}, - Resource, - }, - }, - utils::{Rectangle, Serial}, - wayland::{ - compositor::with_states, - shell::xdg::{ - PopupSurface, PositionerState, ToplevelSurface, XdgShellHandler, XdgShellState, - XdgToplevelSurfaceData, - }, - }, +use smithay::delegate_xdg_shell; +use smithay::desktop::{Space, Window}; +use smithay::input::pointer::{Focus, GrabStartData as PointerGrabStartData}; +use smithay::input::Seat; +use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; +use smithay::reexports::wayland_server::protocol::wl_seat; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::reexports::wayland_server::Resource; +use smithay::utils::{Rectangle, Serial}; +use smithay::wayland::compositor::with_states; +use smithay::wayland::shell::xdg::{ + PopupSurface, PositionerState, ToplevelSurface, XdgShellHandler, XdgShellState, + XdgToplevelSurfaceData, }; -use crate::{ - grabs::{MoveSurfaceGrab, ResizeSurfaceGrab}, - Smallvil, -}; +use crate::grabs::{MoveSurfaceGrab, ResizeSurfaceGrab}; +use crate::Smallvil; impl XdgShellHandler for Smallvil { fn xdg_shell_state(&mut self) -> &mut XdgShellState { diff --git a/src/input.rs b/src/input.rs index 0531c29b..9fd894b5 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,15 +1,11 @@ -use smithay::{ - backend::input::{ - AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, InputBackend, InputEvent, - KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, - }, - input::{ - keyboard::FilterResult, - pointer::{AxisFrame, ButtonEvent, MotionEvent}, - }, - reexports::wayland_server::protocol::wl_surface::WlSurface, - utils::SERIAL_COUNTER, +use smithay::backend::input::{ + AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, InputBackend, InputEvent, + KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, }; +use smithay::input::keyboard::FilterResult; +use smithay::input::pointer::{AxisFrame, ButtonEvent, MotionEvent}; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::utils::SERIAL_COUNTER; use crate::state::Smallvil; @@ -70,7 +66,11 @@ impl Smallvil { .map(|(w, l)| (w.clone(), l)) { self.space.raise_element(&window, true); - keyboard.set_focus(self, Some(window.toplevel().wl_surface().clone()), serial); + keyboard.set_focus( + self, + Some(window.toplevel().wl_surface().clone()), + serial, + ); self.space.elements().for_each(|window| { window.toplevel().send_pending_configure(); }); diff --git a/src/main.rs b/src/main.rs index 9d472a7d..03f9aa23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,8 @@ mod input; mod state; mod winit; -use smithay::reexports::{calloop::EventLoop, wayland_server::Display}; +use smithay::reexports::calloop::EventLoop; +use smithay::reexports::wayland_server::Display; pub use state::Smallvil; pub struct CalloopData { diff --git a/src/state.rs b/src/state.rs index a0405b77..7573d27b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,26 +1,22 @@ -use std::{ffi::OsString, os::unix::io::AsRawFd, sync::Arc}; +use std::ffi::OsString; +use std::os::unix::io::AsRawFd; +use std::sync::Arc; -use smithay::{ - desktop::{Space, Window, WindowSurfaceType}, - input::{pointer::PointerHandle, Seat, SeatState}, - reexports::{ - calloop::{generic::Generic, EventLoop, Interest, LoopSignal, Mode, PostAction}, - wayland_server::{ - backend::{ClientData, ClientId, DisconnectReason}, - protocol::wl_surface::WlSurface, - Display, - }, - }, - utils::{Logical, Point}, - wayland::{ - compositor::{CompositorClientState, CompositorState}, - data_device::DataDeviceState, - output::OutputManagerState, - shell::xdg::XdgShellState, - shm::ShmState, - socket::ListeningSocketSource, - }, -}; +use smithay::desktop::{Space, Window, WindowSurfaceType}; +use smithay::input::pointer::PointerHandle; +use smithay::input::{Seat, SeatState}; +use smithay::reexports::calloop::generic::Generic; +use smithay::reexports::calloop::{EventLoop, Interest, LoopSignal, Mode, PostAction}; +use smithay::reexports::wayland_server::backend::{ClientData, ClientId, DisconnectReason}; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::reexports::wayland_server::Display; +use smithay::utils::{Logical, Point}; +use smithay::wayland::compositor::{CompositorClientState, CompositorState}; +use smithay::wayland::data_device::DataDeviceState; +use smithay::wayland::output::OutputManagerState; +use smithay::wayland::shell::xdg::XdgShellState; +use smithay::wayland::shm::ShmState; +use smithay::wayland::socket::ListeningSocketSource; use crate::CalloopData; @@ -59,8 +55,9 @@ impl Smallvil { // A seat typically has a pointer and maintains a keyboard focus and a pointer focus. let mut seat: Seat = seat_state.new_wl_seat(&dh, "winit"); - // Notify clients that we have a keyboard, for the sake of the example we assume that keyboard is always present. - // You may want to track keyboard hot-plug in real compositor. + // Notify clients that we have a keyboard, for the sake of the example we assume that + // keyboard is always present. You may want to track keyboard hot-plug in real + // compositor. seat.add_keyboard(Default::default(), 200, 200).unwrap(); // Notify clients that we have a pointer (mouse) @@ -99,7 +96,8 @@ impl Smallvil { display: &mut Display, event_loop: &mut EventLoop, ) -> OsString { - // Creates a new listening socket, automatically choosing the next available `wayland` socket name. + // Creates a new listening socket, automatically choosing the next available `wayland` + // socket name. let listening_socket = ListeningSocketSource::new_auto().unwrap(); // Get the name of the listening socket. @@ -122,7 +120,8 @@ impl Smallvil { }) .expect("Failed to init the wayland event source."); - // You also need to add the display itself to the event loop, so that client events will be processed by wayland-server. + // You also need to add the display itself to the event loop, so that client events will be + // processed by wayland-server. handle .insert_source( Generic::new( @@ -145,11 +144,13 @@ impl Smallvil { pointer: &PointerHandle, ) -> Option<(WlSurface, Point)> { let pos = pointer.current_location(); - self.space.element_under(pos).and_then(|(window, location)| { - window - .surface_under(pos - location.to_f64(), WindowSurfaceType::ALL) - .map(|(s, p)| (s, p + location)) - }) + self.space + .element_under(pos) + .and_then(|(window, location)| { + window + .surface_under(pos - location.to_f64(), WindowSurfaceType::ALL) + .map(|(s, p)| (s, p + location)) + }) } } diff --git a/src/winit.rs b/src/winit.rs index 6b1b04ae..15754b5d 100644 --- a/src/winit.rs +++ b/src/winit.rs @@ -1,19 +1,13 @@ use std::time::Duration; -use smithay::{ - backend::{ - renderer::{ - damage::OutputDamageTracker, element::surface::WaylandSurfaceRenderElement, gles::GlesRenderer, - }, - winit::{self, WinitError, WinitEvent, WinitEventLoop, WinitGraphicsBackend}, - }, - output::{Mode, Output, PhysicalProperties, Subpixel}, - reexports::calloop::{ - timer::{TimeoutAction, Timer}, - EventLoop, - }, - utils::{Rectangle, Transform}, -}; +use smithay::backend::renderer::damage::OutputDamageTracker; +use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; +use smithay::backend::renderer::gles::GlesRenderer; +use smithay::backend::winit::{self, WinitError, WinitEvent, WinitEventLoop, WinitGraphicsBackend}; +use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; +use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; +use smithay::reexports::calloop::EventLoop; +use smithay::utils::{Rectangle, Transform}; use crate::{CalloopData, Smallvil}; @@ -41,7 +35,12 @@ pub fn init_winit( }, ); let _global = output.create_global::(&display.handle()); - output.change_current_state(Some(mode), Some(Transform::Flipped180), None, Some((0, 0).into())); + output.change_current_state( + Some(mode), + Some(Transform::Flipped180), + None, + Some((0, 0).into()), + ); output.set_preferred(mode); state.space.map_output(&output, (0, 0)); @@ -53,18 +52,20 @@ pub fn init_winit( let mut full_redraw = 0u8; let timer = Timer::immediate(); - event_loop.handle().insert_source(timer, move |_, _, data| { - winit_dispatch( - &mut backend, - &mut winit, - data, - &output, - &mut damage_tracker, - &mut full_redraw, - ) - .unwrap(); - TimeoutAction::ToDuration(Duration::from_millis(16)) - })?; + event_loop + .handle() + .insert_source(timer, move |_, _, data| { + winit_dispatch( + &mut backend, + &mut winit, + data, + &output, + &mut damage_tracker, + &mut full_redraw, + ) + .unwrap(); + TimeoutAction::ToDuration(Duration::from_millis(16)) + })?; Ok(()) }