Skip to content

Commit

Permalink
Use space_elements to remove boilerplate
Browse files Browse the repository at this point in the history
Unsure if this helped or not because I had to add `_ => unreachable!()` everywhere I matched on `WindowElement`
  • Loading branch information
Ottatop committed Dec 24, 2023
1 parent 31172c5 commit f9e81d4
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 98 deletions.
2 changes: 2 additions & 0 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl State {
surface.close().expect("failed to close x11 win");
}
WindowElement::X11OverrideRedirect(_) => (),
_ => unreachable!(),
}
}
}
Expand Down Expand Up @@ -508,6 +509,7 @@ impl State {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
(Some(surface.class()), Some(surface.title()))
}
_ => unreachable!(),
});

let focused = window.as_ref().and_then(|win| {
Expand Down
1 change: 1 addition & 0 deletions src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl WaylandFocus for FocusTarget {
) => surface.same_client_as(object_id),
FocusTarget::Popup(popup) => popup.wl_surface().id().same_client_as(object_id),
FocusTarget::LayerSurface(surf) => surf.wl_surface().id().same_client_as(object_id),
_ => unreachable!(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/grab/resize_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl PointerGrab<State> for ResizeSurfaceGrab {
.expect("failed to configure x11 win");
}
WindowElement::X11OverrideRedirect(_) => (),
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -235,6 +236,7 @@ impl PointerGrab<State> for ResizeSurfaceGrab {
});
}
WindowElement::X11OverrideRedirect(_) => (),
_ => unreachable!(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl State {
// INFO: do i need to configure this?
}
WindowElement::X11OverrideRedirect(_) => (),
_ => unreachable!(),
});
keyboard.set_focus(self, None, serial);
}
Expand Down
1 change: 1 addition & 0 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl State {
// filtered out up there somewhere
unreachable!();
}
_ => unreachable!(),
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ where
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
surface.render_elements(renderer, location, scale, alpha)
}
_ => unreachable!(),
}
.into_iter()
.map(C::from)
Expand Down
136 changes: 40 additions & 96 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{cell::RefCell, time::Duration};
use smithay::{
backend::input::KeyState,
desktop::{
space::SpaceElement,
utils::{
send_dmabuf_feedback_surface_tree, send_frames_surface_tree,
take_presentation_feedback_surface_tree, with_surfaces_surface_tree,
Expand All @@ -26,7 +25,8 @@ use smithay::{
wayland_protocols::wp::presentation_time::server::wp_presentation_feedback,
wayland_server::protocol::wl_surface::WlSurface,
},
utils::{user_data::UserDataMap, IsAlive, Logical, Point, Rectangle, Serial},
space_elements,
utils::{user_data::UserDataMap, Logical, Rectangle, Serial},
wayland::{
compositor::{self, SurfaceData},
dmabuf::DmabufFeedback,
Expand All @@ -42,17 +42,29 @@ use self::window_state::{LocationRequestState, WindowElementState};

pub mod window_state;

/// The different types of windows.
#[derive(Debug, Clone, PartialEq)]
pub enum WindowElement {
space_elements! {
/// The different types of windows.
#[derive(Debug, Clone, PartialEq)]
pub WindowElement;
/// This is a native Wayland window.
Wayland(Window),
Wayland = Window,
/// This is an Xwayland window.
X11(X11Surface),
X11 = X11Surface,
/// This is an Xwayland override redirect window, which should not be messed with.
X11OverrideRedirect(X11Surface),
X11OverrideRedirect = X11Surface,
}

// /// The different types of windows.
// #[derive(Debug, Clone, PartialEq)]
// pub enum WindowElement {
// /// This is a native Wayland window.
// Wayland(Window),
// /// This is an Xwayland window.
// X11(X11Surface),
// /// This is an Xwayland override redirect window, which should not be messed with.
// X11OverrideRedirect(X11Surface),
// }

impl WindowElement {
pub fn with_surfaces<F>(&self, processor: F)
where
Expand All @@ -65,6 +77,7 @@ impl WindowElement {
with_surfaces_surface_tree(&surface, processor);
}
}
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -93,6 +106,7 @@ impl WindowElement {
);
}
}
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -123,6 +137,7 @@ impl WindowElement {
);
}
}
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -153,6 +168,7 @@ impl WindowElement {
);
}
}
_ => unreachable!(),
}
}

Expand All @@ -162,6 +178,7 @@ impl WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
surface.wl_surface()
}
_ => unreachable!(),
}
}

Expand All @@ -171,6 +188,7 @@ impl WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
surface.user_data()
}
_ => unreachable!(),
}
}

Expand All @@ -196,6 +214,7 @@ impl WindowElement {
.expect("failed to configure x11 win");
}
}
_ => unreachable!(),
}
self.with_state(|state| {
state.loc_request_state = LocationRequestState::Sent(new_geo.loc);
Expand All @@ -219,6 +238,7 @@ impl WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
Some(surface.class())
}
_ => unreachable!(),
}
}

Expand All @@ -239,6 +259,7 @@ impl WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
Some(surface.title())
}
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -317,24 +338,14 @@ impl WindowElement {
}
}

impl IsAlive for WindowElement {
fn alive(&self) -> bool {
match self {
WindowElement::Wayland(window) => window.alive(),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
surface.alive()
}
}
}
}

impl PointerTarget<State> for WindowElement {
fn frame(&self, seat: &Seat<State>, data: &mut State) {
match self {
WindowElement::Wayland(window) => window.frame(seat, data),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
surface.frame(seat, data)
}
_ => unreachable!(),
}
}

Expand All @@ -345,6 +356,7 @@ impl PointerTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
PointerTarget::enter(surface, seat, data, event)
}
_ => unreachable!(),
}
}

Expand All @@ -355,6 +367,7 @@ impl PointerTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
PointerTarget::motion(surface, seat, data, event)
}
_ => unreachable!(),
}
}

Expand All @@ -372,6 +385,7 @@ impl PointerTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
PointerTarget::relative_motion(surface, seat, data, event);
}
_ => unreachable!(),
}
}

Expand All @@ -387,6 +401,7 @@ impl PointerTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
PointerTarget::button(surface, seat, data, event)
}
_ => unreachable!(),
}
}

Expand All @@ -397,6 +412,7 @@ impl PointerTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
PointerTarget::axis(surface, seat, data, frame)
}
_ => unreachable!(),
}
}

Expand All @@ -409,6 +425,7 @@ impl PointerTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
PointerTarget::leave(surface, seat, data, serial, time)
}
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -500,6 +517,7 @@ impl KeyboardTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
KeyboardTarget::enter(surface, seat, data, keys, serial)
}
_ => unreachable!(),
}
}

Expand All @@ -509,6 +527,7 @@ impl KeyboardTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
KeyboardTarget::leave(surface, seat, data, serial)
}
_ => unreachable!(),
}
}

Expand All @@ -528,6 +547,7 @@ impl KeyboardTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
KeyboardTarget::key(surface, seat, data, key, state, serial, time);
}
_ => unreachable!(),
}
}

Expand All @@ -545,83 +565,7 @@ impl KeyboardTarget<State> for WindowElement {
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
KeyboardTarget::modifiers(surface, seat, data, modifiers, serial);
}
}
}
}

impl SpaceElement for WindowElement {
fn geometry(&self) -> Rectangle<i32, Logical> {
// TODO: ssd
match self {
WindowElement::Wayland(window) => SpaceElement::geometry(window),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::geometry(surface)
}
}
}

fn bbox(&self) -> Rectangle<i32, Logical> {
// TODO: ssd
match self {
WindowElement::Wayland(window) => SpaceElement::bbox(window),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::bbox(surface)
}
}
}

fn is_in_input_region(&self, point: &Point<f64, Logical>) -> bool {
// TODO: ssd
match self {
WindowElement::Wayland(window) => SpaceElement::is_in_input_region(window, point),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::is_in_input_region(surface, point)
}
}
}

fn z_index(&self) -> u8 {
match self {
WindowElement::Wayland(window) => SpaceElement::z_index(window),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::z_index(surface)
}
}
}

fn set_activate(&self, activated: bool) {
match self {
WindowElement::Wayland(window) => SpaceElement::set_activate(window, activated),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::set_activate(surface, activated)
}
}
}

fn output_enter(&self, output: &Output, overlap: Rectangle<i32, Logical>) {
match self {
WindowElement::Wayland(window) => SpaceElement::output_enter(window, output, overlap),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::output_enter(surface, output, overlap)
}
}
}

fn output_leave(&self, output: &Output) {
match self {
WindowElement::Wayland(window) => SpaceElement::output_leave(window, output),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::output_leave(surface, output)
}
}
}

fn refresh(&self) {
match self {
WindowElement::Wayland(window) => SpaceElement::refresh(window),
WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => {
SpaceElement::refresh(surface)
}
_ => unreachable!(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/window/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl TiledWindowBlocker {
})
}
WindowElement::X11(_) | WindowElement::X11OverrideRedirect(_) => true,
_ => unreachable!(),
});

tracing::debug!(
Expand Down
Loading

0 comments on commit f9e81d4

Please sign in to comment.