From caee59b109902c60d4bcfe597565278443f75782 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 20 Nov 2024 14:01:52 -0800 Subject: [PATCH 1/2] input: Make `TabletToolEvent` depend on `AbsolutePositionEvent` `TabletToolEvent` provides all the methods that are provide by `AbsolutePositionEvent`. Seemingly with the only difference that tablet events guarantee the untransformed coordinates are in mm, while it is otherwise unspecified with `AbsolutePositionEvent`. Re-using the `AbsolutePositionEvent` trait here makes it easier to share a bit of code for handling tablet and touch events. --- src/backend/input/tablet.rs | 47 +++++----------------------------- src/backend/libinput/tablet.rs | 37 ++++++++++++++------------ 2 files changed, 27 insertions(+), 57 deletions(-) diff --git a/src/backend/input/tablet.rs b/src/backend/input/tablet.rs index 7a44d4f162a1..a281a5aa9b24 100644 --- a/src/backend/input/tablet.rs +++ b/src/backend/input/tablet.rs @@ -1,5 +1,5 @@ -use super::{ButtonState, Event, InputBackend, UnusedEvent}; -use crate::utils::{Logical, Point, Raw, Size}; +use super::{AbsolutePositionEvent, ButtonState, Event, InputBackend, UnusedEvent}; +use crate::utils::{Logical, Point}; use bitflags::bitflags; /// Description of physical tablet tool @@ -61,7 +61,10 @@ bitflags! { } /// Tablet tool event -pub trait TabletToolEvent { +/// +/// The [AbsolutePositionEvent] implementation for tablet tool events produces (untransformed) +/// coordinates in mm from the top left corner of the tablet in its current logical orientation. +pub trait TabletToolEvent: AbsolutePositionEvent { /// Get tablet tool that caused this event fn tool(&self) -> TabletToolDescriptor; @@ -70,20 +73,6 @@ pub trait TabletToolEvent { (self.delta_x(), self.delta_y()).into() } - /// Tool position in the device's native coordinate space - fn position(&self) -> Point { - (self.x(), self.y()).into() - } - - /// Tool position converted into the target coordinate space. - fn position_transformed(&self, coordinate_space: Size) -> Point { - ( - self.x_transformed(coordinate_space.w), - self.y_transformed(coordinate_space.h), - ) - .into() - } - /// Returns the current tilt along the (X,Y) axis of the tablet's current logical /// orientation, in degrees off the tablet's z axis. /// @@ -107,18 +96,6 @@ pub trait TabletToolEvent { /// Delta on the y axis between the last and new pointer device position interpreted as pixel movement fn delta_y(&self) -> f64; - /// Returns the x coordinate of the tablet tool, in mm from the top left corner of the tablet in its current logical orientation. - fn x(&self) -> f64; - /// Returns the y coordinate of the tablet tool, in mm from the top left corner of the tablet in its current logical orientation. - fn y(&self) -> f64; - - /// Return the current absolute X coordinate of the tablet tool event, transformed to screen coordinates. - fn x_transformed(&self, width: i32) -> f64; - /// Return the current absolute Y coordinate of the tablet tool event, transformed to screen coordinates. - fn y_transformed(&self, height: i32) -> f64; - - /// Returns the current distance from the tablet's sensor, normalized to the range [0, 1] - /// /// If this axis does not exist on the current tool, this function returns 0. fn distance(&self) -> f64; @@ -202,18 +179,6 @@ impl TabletToolEvent for UnusedEvent { fn delta_y(&self) -> f64 { match *self {} } - fn x(&self) -> f64 { - match *self {} - } - fn y(&self) -> f64 { - match *self {} - } - fn x_transformed(&self, _width: i32) -> f64 { - match *self {} - } - fn y_transformed(&self, _height: i32) -> f64 { - match *self {} - } fn distance(&self) -> f64 { match *self {} } diff --git a/src/backend/libinput/tablet.rs b/src/backend/libinput/tablet.rs index 00295c76df9c..9b01ec28e5de 100644 --- a/src/backend/libinput/tablet.rs +++ b/src/backend/libinput/tablet.rs @@ -49,6 +49,27 @@ impl backend::TabletToolTipEvent for tablet_tool::TabletTo } } +impl backend::AbsolutePositionEvent for E +where + E: IsTabletEvent + event::EventTrait, +{ + fn x(&self) -> f64 { + tablet_tool::TabletToolEventTrait::x(self) + } + + fn y(&self) -> f64 { + tablet_tool::TabletToolEventTrait::y(self) + } + + fn x_transformed(&self, width: i32) -> f64 { + tablet_tool::TabletToolEventTrait::x_transformed(self, width as u32) + } + + fn y_transformed(&self, height: i32) -> f64 { + tablet_tool::TabletToolEventTrait::y_transformed(self, height as u32) + } +} + impl backend::TabletToolEvent for E where E: IsTabletEvent + event::EventTrait, @@ -96,22 +117,6 @@ where tablet_tool::TabletToolEventTrait::dy(self) } - fn x(&self) -> f64 { - tablet_tool::TabletToolEventTrait::x(self) - } - - fn y(&self) -> f64 { - tablet_tool::TabletToolEventTrait::y(self) - } - - fn x_transformed(&self, width: i32) -> f64 { - tablet_tool::TabletToolEventTrait::x_transformed(self, width as u32) - } - - fn y_transformed(&self, height: i32) -> f64 { - tablet_tool::TabletToolEventTrait::y_transformed(self, height as u32) - } - fn distance(&self) -> f64 { tablet_tool::TabletToolEventTrait::distance(self) } From 2127ec3e0275c7098132ffbdc0c1b537eee827a4 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 20 Nov 2024 14:04:17 -0800 Subject: [PATCH 2/2] anvil: Apply output transform to tablet events --- anvil/src/input_handler.rs | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/anvil/src/input_handler.rs b/anvil/src/input_handler.rs index 5a001777a967..af765e44ed85 100644 --- a/anvil/src/input_handler.rs +++ b/anvil/src/input_handler.rs @@ -924,15 +924,7 @@ impl AnvilState { fn on_tablet_tool_axis(&mut self, evt: B::TabletToolAxisEvent) { let tablet_seat = self.seat.tablet_seat(); - let output_geometry = self - .space - .outputs() - .next() - .map(|o| self.space.output_geometry(o).unwrap()); - - if let Some(rect) = output_geometry { - let pointer_location = evt.position_transformed(rect.size) + rect.loc.to_f64(); - + if let Some(pointer_location) = self.touch_location_transformed(&evt) { let pointer = self.pointer.clone(); let under = self.surface_under(pointer_location); let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&evt.device())); @@ -988,18 +980,10 @@ impl AnvilState { ) { let tablet_seat = self.seat.tablet_seat(); - let output_geometry = self - .space - .outputs() - .next() - .map(|o| self.space.output_geometry(o).unwrap()); - - if let Some(rect) = output_geometry { + if let Some(pointer_location) = self.touch_location_transformed(&evt) { let tool = evt.tool(); tablet_seat.add_tool::(self, dh, &tool); - let pointer_location = evt.position_transformed(rect.size) + rect.loc.to_f64(); - let pointer = self.pointer.clone(); let under = self.surface_under(pointer_location); let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&evt.device()));