From 6de35fa67ba7d25ca3e70758db4cc2069d73cd51 Mon Sep 17 00:00:00 2001 From: Shinyzenith Date: Sun, 7 Apr 2024 13:56:45 +0530 Subject: [PATCH] refactor: Remove redundant app state structs Signed-off-by: Shinyzenith --- libwaysip/src/dispatch.rs | 72 ++++++++++++++++++--------------------- libwaysip/src/lib.rs | 34 +++++++----------- libwaysip/src/state.rs | 41 +++++----------------- 3 files changed, 54 insertions(+), 93 deletions(-) diff --git a/libwaysip/src/dispatch.rs b/libwaysip/src/dispatch.rs index 800959a..905e89d 100644 --- a/libwaysip/src/dispatch.rs +++ b/libwaysip/src/dispatch.rs @@ -1,41 +1,35 @@ - -use crate::state::{self, LayerSurfaceInfo, SecondState}; -use wayland_client::protocol::wl_output; +use crate::state::{self, LayerSurfaceInfo, WaysipState}; use wayland_client::{ delegate_noop, - globals::{GlobalListContents}, + globals::GlobalListContents, + protocol::wl_output, protocol::{ wl_buffer::WlBuffer, wl_compositor::WlCompositor, wl_keyboard, wl_pointer, wl_registry, wl_seat::{self}, - wl_shm::{WlShm}, + wl_shm::WlShm, wl_shm_pool::WlShmPool, wl_surface::WlSurface, }, Connection, Dispatch, Proxy, WEnum, }; - -use wayland_protocols::xdg::shell::client::{xdg_toplevel::XdgToplevel, xdg_wm_base}; - -use wayland_protocols::xdg::xdg_output::zv1::client::{ - zxdg_output_manager_v1::ZxdgOutputManagerV1, zxdg_output_v1, +use wayland_protocols::{ + wp::cursor_shape::v1::client::{ + wp_cursor_shape_device_v1::{self, WpCursorShapeDeviceV1}, + wp_cursor_shape_manager_v1::WpCursorShapeManagerV1, + }, + xdg::{ + shell::client::{xdg_toplevel::XdgToplevel, xdg_wm_base}, + xdg_output::zv1::client::{zxdg_output_manager_v1::ZxdgOutputManagerV1, zxdg_output_v1}, + }, }; - use wayland_protocols_wlr::layer_shell::v1::client::{ - zwlr_layer_shell_v1::{ZwlrLayerShellV1}, + zwlr_layer_shell_v1::ZwlrLayerShellV1, zwlr_layer_surface_v1::{self}, }; -use wayland_protocols::wp::cursor_shape::v1::client::{ - wp_cursor_shape_device_v1::{self, WpCursorShapeDeviceV1}, - wp_cursor_shape_manager_v1::WpCursorShapeManagerV1, -}; - - - - -impl Dispatch for SecondState { +impl Dispatch for WaysipState { fn event( state: &mut Self, surface: &zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, @@ -58,7 +52,7 @@ impl Dispatch for SecondState { } } -impl Dispatch for SecondState { +impl Dispatch for WaysipState { fn event( state: &mut Self, proxy: &zxdg_output_v1::ZxdgOutputV1, @@ -91,7 +85,7 @@ impl Dispatch for SecondState { } // so interesting, it is just need to invoke once, it just used to get the globals -impl Dispatch for state::BaseState { +impl Dispatch for state::WaysipState { fn event( _state: &mut Self, _proxy: &wl_registry::WlRegistry, @@ -103,7 +97,7 @@ impl Dispatch for state::BaseState } } -impl Dispatch for state::SecondState { +impl Dispatch for state::WaysipState { fn event( state: &mut Self, proxy: &wl_registry::WlRegistry, @@ -128,7 +122,7 @@ impl Dispatch for state::SecondState { } } -impl Dispatch for state::SecondState { +impl Dispatch for state::WaysipState { fn event( state: &mut Self, wl_output: &wl_output::WlOutput, @@ -159,7 +153,7 @@ impl Dispatch for state::SecondState { } } -impl Dispatch for state::SecondState { +impl Dispatch for state::WaysipState { fn event( _state: &mut Self, wm_base: &xdg_wm_base::XdgWmBase, @@ -174,7 +168,7 @@ impl Dispatch for state::SecondState { } } -impl Dispatch for state::SecondState { +impl Dispatch for state::WaysipState { fn event( _state: &mut Self, seat: &wl_seat::WlSeat, @@ -197,7 +191,7 @@ impl Dispatch for state::SecondState { } } -impl Dispatch for state::SecondState { +impl Dispatch for state::WaysipState { fn event( state: &mut Self, _proxy: &wl_keyboard::WlKeyboard, @@ -214,7 +208,7 @@ impl Dispatch for state::SecondState { } } -impl Dispatch for state::SecondState { +impl Dispatch for state::WaysipState { fn event( dispatch_state: &mut Self, pointer: &wl_pointer::WlPointer, @@ -310,17 +304,17 @@ impl Dispatch for state::SecondState { } } -delegate_noop!(SecondState: ignore WlCompositor); // WlCompositor is need to create a surface -delegate_noop!(SecondState: ignore WlSurface); // surface is the base needed to show buffer +delegate_noop!(WaysipState: ignore WlCompositor); // WlCompositor is need to create a surface +delegate_noop!(WaysipState: ignore WlSurface); // surface is the base needed to show buffer // -delegate_noop!(SecondState: ignore WlShm); // shm is used to create buffer pool -delegate_noop!(SecondState: ignore XdgToplevel); // so it is the same with layer_shell, private a +delegate_noop!(WaysipState: ignore WlShm); // shm is used to create buffer pool +delegate_noop!(WaysipState: ignore XdgToplevel); // so it is the same with layer_shell, private a // place for surface -delegate_noop!(SecondState: ignore WlShmPool); // so it is pool, created by wl_shm -delegate_noop!(SecondState: ignore WlBuffer); // buffer show the picture -delegate_noop!(SecondState: ignore ZwlrLayerShellV1); // it is simillar with xdg_toplevel, also the +delegate_noop!(WaysipState: ignore WlShmPool); // so it is pool, created by wl_shm +delegate_noop!(WaysipState: ignore WlBuffer); // buffer show the picture +delegate_noop!(WaysipState: ignore ZwlrLayerShellV1); // it is simillar with xdg_toplevel, also the // ext-session-shell -delegate_noop!(SecondState: ignore ZxdgOutputManagerV1); +delegate_noop!(WaysipState: ignore ZxdgOutputManagerV1); -delegate_noop!(SecondState: ignore WpCursorShapeManagerV1); -delegate_noop!(SecondState: ignore WpCursorShapeDeviceV1); +delegate_noop!(WaysipState: ignore WpCursorShapeManagerV1); +delegate_noop!(WaysipState: ignore WpCursorShapeDeviceV1); diff --git a/libwaysip/src/lib.rs b/libwaysip/src/lib.rs index 39fced1..522bd1b 100644 --- a/libwaysip/src/lib.rs +++ b/libwaysip/src/lib.rs @@ -1,40 +1,30 @@ mod dispatch; -mod error; mod render; -pub mod state; -pub use error::WaySipError; +pub mod error; +pub mod state; +use error::WaySipError; use std::os::unix::prelude::AsFd; - - use wayland_client::{ - globals::{registry_queue_init}, + globals::registry_queue_init, protocol::{ wl_compositor::WlCompositor, - wl_seat::{WlSeat}, + wl_seat::WlSeat, wl_shm::{self, WlShm}, }, Connection, }; - - - -use wayland_protocols::xdg::xdg_output::zv1::client::{ - zxdg_output_manager_v1::ZxdgOutputManagerV1, +use wayland_cursor::{CursorImageBuffer, CursorTheme}; +use wayland_protocols::{ + wp::cursor_shape::v1::client::wp_cursor_shape_manager_v1::WpCursorShapeManagerV1, + xdg::xdg_output::zv1::client::zxdg_output_manager_v1::ZxdgOutputManagerV1, }; - use wayland_protocols_wlr::layer_shell::v1::client::{ zwlr_layer_shell_v1::{Layer, ZwlrLayerShellV1}, zwlr_layer_surface_v1::{self, Anchor}, }; -use wayland_protocols::wp::cursor_shape::v1::client::{ - wp_cursor_shape_manager_v1::WpCursorShapeManagerV1, -}; - -use wayland_cursor::CursorImageBuffer; -use wayland_cursor::CursorTheme; fn get_cursor_buffer(connection: &Connection, shm: &WlShm) -> Option { let mut cursor_theme = CursorTheme::load(connection, shm.clone(), 23).ok()?; let mut cursor = cursor_theme.get_cursor("crosshair"); @@ -48,7 +38,7 @@ fn get_cursor_buffer(connection: &Connection, shm: &WlShm) -> Option Result, WaySipError> { let connection = Connection::connect_to_env().map_err(|e| WaySipError::InitFailed(e.to_string()))?; - let (globals, _) = registry_queue_init::(&connection) + let (globals, _) = registry_queue_init::(&connection) .map_err(|e| WaySipError::InitFailed(e.to_string()))?; // We just need the // global, the // event_queue is @@ -57,9 +47,9 @@ pub fn get_area(kind: state::WaySipKind) -> Result, WayS // state::BaseState after // this anymore - let mut state = state::SecondState::new(kind); + let mut state = state::WaysipState::new(kind); - let mut event_queue = connection.new_event_queue::(); + let mut event_queue = connection.new_event_queue::(); let qh = event_queue.handle(); let wmcompositer = globals diff --git a/libwaysip/src/state.rs b/libwaysip/src/state.rs index 187eee3..6111bf3 100644 --- a/libwaysip/src/state.rs +++ b/libwaysip/src/state.rs @@ -1,30 +1,10 @@ - - -use wayland_client::{ - protocol::{ - wl_buffer::WlBuffer, - wl_surface::WlSurface, - }, -}; - - - -use wayland_protocols::xdg::xdg_output::zv1::client::{ - zxdg_output_v1, -}; - -use wayland_protocols_wlr::layer_shell::v1::client::{ - zwlr_layer_surface_v1::{ZwlrLayerSurfaceV1}, -}; - -use wayland_protocols::wp::cursor_shape::v1::client::{ - wp_cursor_shape_manager_v1::WpCursorShapeManagerV1, -}; - +use wayland_client::protocol::{wl_buffer::WlBuffer, wl_output::WlOutput, wl_surface::WlSurface}; use wayland_cursor::CursorImageBuffer; - - -use wayland_client::protocol::wl_output::WlOutput; +use wayland_protocols::{ + wp::cursor_shape::v1::client::wp_cursor_shape_manager_v1::WpCursorShapeManagerV1, + xdg::xdg_output::zv1::client::zxdg_output_v1, +}; +use wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::ZwlrLayerSurfaceV1; /// You are allow to choose three actions of waysip, include area selection, point selection, and /// select sreen @@ -36,9 +16,6 @@ pub enum WaySipKind { Screen, } -#[derive(Debug)] -pub struct BaseState; - #[derive(Debug)] pub struct ZXdgOutputInfo { pub zxdgoutput: zxdg_output_v1::ZxdgOutputV1, @@ -151,7 +128,7 @@ impl ScreenInfo { } #[derive(Debug)] -pub struct SecondState { +pub struct WaysipState { pub outputs: Vec, pub zxdgoutputs: Vec, pub running: bool, @@ -164,9 +141,9 @@ pub struct SecondState { pub cursor_manager: Option, } -impl SecondState { +impl WaysipState { pub fn new(waysipkind: WaySipKind) -> Self { - SecondState { + WaysipState { outputs: Vec::new(), zxdgoutputs: Vec::new(), running: true,