Skip to content

Commit

Permalink
refactor: Remove redundant app state structs
Browse files Browse the repository at this point in the history
Signed-off-by: Shinyzenith <[email protected]>
  • Loading branch information
Shinyzenith committed Apr 7, 2024
1 parent 7aa61ac commit 6de35fa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 93 deletions.
72 changes: 33 additions & 39 deletions libwaysip/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -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<zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, ()> for SecondState {
impl Dispatch<zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, ()> for WaysipState {
fn event(
state: &mut Self,
surface: &zwlr_layer_surface_v1::ZwlrLayerSurfaceV1,
Expand All @@ -58,7 +52,7 @@ impl Dispatch<zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, ()> for SecondState {
}
}

impl Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for SecondState {
impl Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for WaysipState {
fn event(
state: &mut Self,
proxy: &zxdg_output_v1::ZxdgOutputV1,
Expand Down Expand Up @@ -91,7 +85,7 @@ impl Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for SecondState {
}

// so interesting, it is just need to invoke once, it just used to get the globals
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for state::BaseState {
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for state::WaysipState {
fn event(
_state: &mut Self,
_proxy: &wl_registry::WlRegistry,
Expand All @@ -103,7 +97,7 @@ impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for state::BaseState
}
}

impl Dispatch<wl_registry::WlRegistry, ()> for state::SecondState {
impl Dispatch<wl_registry::WlRegistry, ()> for state::WaysipState {
fn event(
state: &mut Self,
proxy: &wl_registry::WlRegistry,
Expand All @@ -128,7 +122,7 @@ impl Dispatch<wl_registry::WlRegistry, ()> for state::SecondState {
}
}

impl Dispatch<wl_output::WlOutput, ()> for state::SecondState {
impl Dispatch<wl_output::WlOutput, ()> for state::WaysipState {
fn event(
state: &mut Self,
wl_output: &wl_output::WlOutput,
Expand Down Expand Up @@ -159,7 +153,7 @@ impl Dispatch<wl_output::WlOutput, ()> for state::SecondState {
}
}

impl Dispatch<xdg_wm_base::XdgWmBase, ()> for state::SecondState {
impl Dispatch<xdg_wm_base::XdgWmBase, ()> for state::WaysipState {
fn event(
_state: &mut Self,
wm_base: &xdg_wm_base::XdgWmBase,
Expand All @@ -174,7 +168,7 @@ impl Dispatch<xdg_wm_base::XdgWmBase, ()> for state::SecondState {
}
}

impl Dispatch<wl_seat::WlSeat, ()> for state::SecondState {
impl Dispatch<wl_seat::WlSeat, ()> for state::WaysipState {
fn event(
_state: &mut Self,
seat: &wl_seat::WlSeat,
Expand All @@ -197,7 +191,7 @@ impl Dispatch<wl_seat::WlSeat, ()> for state::SecondState {
}
}

impl Dispatch<wl_keyboard::WlKeyboard, ()> for state::SecondState {
impl Dispatch<wl_keyboard::WlKeyboard, ()> for state::WaysipState {
fn event(
state: &mut Self,
_proxy: &wl_keyboard::WlKeyboard,
Expand All @@ -214,7 +208,7 @@ impl Dispatch<wl_keyboard::WlKeyboard, ()> for state::SecondState {
}
}

impl Dispatch<wl_pointer::WlPointer, ()> for state::SecondState {
impl Dispatch<wl_pointer::WlPointer, ()> for state::WaysipState {
fn event(
dispatch_state: &mut Self,
pointer: &wl_pointer::WlPointer,
Expand Down Expand Up @@ -310,17 +304,17 @@ impl Dispatch<wl_pointer::WlPointer, ()> 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);
34 changes: 12 additions & 22 deletions libwaysip/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<CursorImageBuffer> {
let mut cursor_theme = CursorTheme::load(connection, shm.clone(), 23).ok()?;
let mut cursor = cursor_theme.get_cursor("crosshair");
Expand All @@ -48,7 +38,7 @@ fn get_cursor_buffer(connection: &Connection, shm: &WlShm) -> Option<CursorImage
pub fn get_area(kind: state::WaySipKind) -> Result<Option<state::AreaInfo>, WaySipError> {
let connection =
Connection::connect_to_env().map_err(|e| WaySipError::InitFailed(e.to_string()))?;
let (globals, _) = registry_queue_init::<state::BaseState>(&connection)
let (globals, _) = registry_queue_init::<state::WaysipState>(&connection)
.map_err(|e| WaySipError::InitFailed(e.to_string()))?; // We just need the
// global, the
// event_queue is
Expand All @@ -57,9 +47,9 @@ pub fn get_area(kind: state::WaySipKind) -> Result<Option<state::AreaInfo>, 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::<state::SecondState>();
let mut event_queue = connection.new_event_queue::<state::WaysipState>();
let qh = event_queue.handle();

let wmcompositer = globals
Expand Down
41 changes: 9 additions & 32 deletions libwaysip/src/state.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -36,9 +16,6 @@ pub enum WaySipKind {
Screen,
}

#[derive(Debug)]
pub struct BaseState;

#[derive(Debug)]
pub struct ZXdgOutputInfo {
pub zxdgoutput: zxdg_output_v1::ZxdgOutputV1,
Expand Down Expand Up @@ -151,7 +128,7 @@ impl ScreenInfo {
}

#[derive(Debug)]
pub struct SecondState {
pub struct WaysipState {
pub outputs: Vec<WlOutputInfo>,
pub zxdgoutputs: Vec<ZXdgOutputInfo>,
pub running: bool,
Expand All @@ -164,9 +141,9 @@ pub struct SecondState {
pub cursor_manager: Option<WpCursorShapeManagerV1>,
}

impl SecondState {
impl WaysipState {
pub fn new(waysipkind: WaySipKind) -> Self {
SecondState {
WaysipState {
outputs: Vec::new(),
zxdgoutputs: Vec::new(),
running: true,
Expand Down

0 comments on commit 6de35fa

Please sign in to comment.