From a85d8d0df5b8c1781540d02ca463cac7ceabadeb Mon Sep 17 00:00:00 2001 From: Janis Date: Thu, 25 Nov 2021 11:27:41 +0100 Subject: [PATCH] changed to using `Point` instead of tuples or slices --- src/backends/window_event.rs | 56 ++++++++++++++++++---- src/backends/xlib/mod.rs | 9 ++-- src/state.rs | 91 +++++++++++++++++++----------------- 3 files changed, 102 insertions(+), 54 deletions(-) diff --git a/src/backends/window_event.rs b/src/backends/window_event.rs index cfae8ea..f3fb489 100644 --- a/src/backends/window_event.rs +++ b/src/backends/window_event.rs @@ -128,11 +128,49 @@ impl KeyEvent { } } +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] +pub struct Point +where + I: Copy + Clone + PartialEq + PartialOrd, +{ + pub x: I, + pub y: I, +} +impl From<(I, I)> for Point +where + I: Copy + Clone + PartialEq + PartialOrd, +{ + fn from(value: (I, I)) -> Self { + Self::from_tuple(value) + } +} + +impl Point +where + I: Copy + Clone + PartialEq + PartialOrd, +{ + pub fn new(x: I, y: I) -> Self { + Self { x, y } + } + + pub fn from_tuple(tuple: (I, I)) -> Self { + Self { + x: tuple.0, + y: tuple.1, + } + } + + pub fn as_tuple(&self) -> (I, I) { + (self.x, self.y) + } +} + #[derive(Debug)] pub struct ButtonEvent { pub window: Window, pub state: KeyState, pub keycode: MouseButton, + pub cursor_position: Point, pub modifierstate: ModifierState, } @@ -141,12 +179,14 @@ impl ButtonEvent { window: Window, state: KeyState, keycode: MouseButton, + cursor_position: Point, modifierstate: ModifierState, ) -> Self { Self { window, state, keycode, + cursor_position, modifierstate, } } @@ -154,12 +194,12 @@ impl ButtonEvent { #[derive(Debug)] pub struct MotionEvent { - pub position: [i32; 2], + pub position: Point, pub window: Window, } impl MotionEvent { - pub fn new(position: [i32; 2], window: Window) -> Self { + pub fn new(position: Point, window: Window) -> Self { Self { position, window } } } @@ -193,12 +233,12 @@ impl DestroyEvent { #[derive(Debug)] pub struct CreateEvent { pub window: Window, - pub position: [i32; 2], - pub size: [i32; 2], + pub position: Point, + pub size: Point, } impl CreateEvent { - pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self { + pub fn new(window: Window, position: Point, size: Point) -> Self { Self { window, position, @@ -210,12 +250,12 @@ impl CreateEvent { #[derive(Debug)] pub struct ConfigureEvent { pub window: Window, - pub position: [i32; 2], - pub size: [i32; 2], + pub position: Point, + pub size: Point, } impl ConfigureEvent { - pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self { + pub fn new(window: Window, position: Point, size: Point) -> Self { Self { window, position, diff --git a/src/backends/xlib/mod.rs b/src/backends/xlib/mod.rs index 4d0b1ba..1229d3a 100644 --- a/src/backends/xlib/mod.rs +++ b/src/backends/xlib/mod.rs @@ -303,8 +303,8 @@ impl TryFrom for XLibWindowEvent { let ev = unsafe { &event.configure_request }; Ok(Self::ConfigureEvent(ConfigureEvent { window: ev.window, - position: [ev.x, ev.y], - size: [ev.width, ev.height], + position: (ev.x, ev.y).into(), + size: (ev.width, ev.height).into(), })) } xlib::EnterNotify => { @@ -315,6 +315,8 @@ impl TryFrom for XLibWindowEvent { let ev = unsafe { &event.destroy_window }; Ok(Self::DestroyEvent(DestroyEvent { window: ev.window })) } + // both ButtonPress and ButtonRelease use the XButtonEvent structure, aliased as either + // XButtonReleasedEvent or XButtonPressedEvent xlib::ButtonPress | xlib::ButtonRelease => { let ev = unsafe { &event.button }; let keycode = xev_to_mouse_button(ev).unwrap(); @@ -327,9 +329,10 @@ impl TryFrom for XLibWindowEvent { let modifierstate = ModifierState::empty(); Ok(Self::ButtonEvent(ButtonEvent::new( - ev.window, + ev.subwindow, state, keycode, + (ev.x, ev.y).into(), modifierstate, ))) } diff --git a/src/state.rs b/src/state.rs index e278b64..0fdd44f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,10 +2,7 @@ use std::rc::Rc; use log::{error, info}; -use x11::xlib::{ - self, Window, XButtonPressedEvent, XButtonReleasedEvent, XEvent, XKeyEvent, - XMotionEvent, -}; +use x11::xlib::{self, Window, XEvent, XKeyEvent, XMotionEvent}; use xlib::{ XConfigureRequestEvent, XCrossingEvent, XDestroyWindowEvent, XMapRequestEvent, XUnmapEvent, @@ -15,7 +12,8 @@ use crate::{ backends::{ keycodes::{MouseButton, VirtualKeyCode}, window_event::{ - ButtonEvent, KeyBind, KeyEvent, ModifierKey, ModifierState, + ButtonEvent, KeyBind, KeyEvent, KeyState, ModifierKey, + ModifierState, }, xlib::XLib, WindowServerBackend, @@ -661,18 +659,18 @@ where } /// ensure event.subwindow refers to a valid client. - fn start_move_resize_window(&mut self, event: &XButtonPressedEvent) { - let window = event.subwindow; + fn start_move_resize_window(&mut self, event: &ButtonEvent) { + let window = event.window; // xev.subwindow - match event.button { - 1 => { + match event.keycode { + MouseButton::Left => { if self.clients.set_floating(&window) { self.arrange_clients(); } self.move_resize_window = MoveResizeInfo::Move(MoveInfoInner { window, - starting_cursor_pos: (event.x, event.y), + starting_cursor_pos: event.cursor_position.as_tuple(), starting_window_pos: self .clients .get(&window) @@ -680,7 +678,7 @@ where .position, }); } - 3 => { + MouseButton::Right => { if self.clients.set_floating(&window) { self.arrange_clients(); } @@ -709,13 +707,17 @@ where } } - fn end_move_resize_window(&mut self, event: &XButtonReleasedEvent) { - if event.button == 1 || event.button == 3 { - self.move_resize_window = MoveResizeInfo::None; - } - if event.button == 3 { - // TODO fix backend cursor api - //self.xlib.release_cursor(); + fn end_move_resize_window(&mut self, event: &ButtonEvent) { + match event.keycode { + MouseButton::Left => { + self.move_resize_window = MoveResizeInfo::None; + } + MouseButton::Right => { + self.move_resize_window = MoveResizeInfo::None; + // TODO fix backend cursor api + //self.xlib.release_cursor(); + } + _ => {} } } @@ -759,36 +761,39 @@ where } } - fn button_press(&mut self, event: &ButtonEvent) { - self.focus_client(&event.window, true); + fn button_event(&mut self, event: &ButtonEvent) { + match event.state { + KeyState::Pressed => { + self.focus_client(&event.window, true); - match event.keycode { - MouseButton::Left | MouseButton::Right => { - match self.move_resize_window { - MoveResizeInfo::None - if ModifierState::from([self.config.mod_key]) - .eq_ignore_lock(&event.modifierstate) - && self.clients.contains(&event.window) => - { - //self.start_move_resize_window(event) + match event.keycode { + MouseButton::Left | MouseButton::Right => { + match self.move_resize_window { + MoveResizeInfo::None + if ModifierState::from([self + .config + .mod_key]) + .eq_ignore_lock(&event.modifierstate) + && self.clients.contains(&event.window) => + { + self.start_move_resize_window(event) + } + _ => {} + } + } + MouseButton::Middle => { + self.clients.toggle_floating(&event.window); + self.arrange_clients(); } _ => {} } } - MouseButton::Middle => { - self.clients.toggle_floating(&event.window); - self.arrange_clients(); - } - _ => {} - } - } - - fn button_release(&mut self, event: &XButtonReleasedEvent) { - match self.move_resize_window { - MoveResizeInfo::None => {} - _ => { - self.end_move_resize_window(event); - } + KeyState::Released => match self.move_resize_window { + MoveResizeInfo::None => {} + _ => { + self.end_move_resize_window(event); + } + }, } }