use std::{error::Error, string::ToString};
#[derive(Debug, Clone, Eq)]
pub struct Seat {
id: u64,
name: String,
capabilities: SeatCapabilities,
}
impl Seat {
pub(crate) fn new<S: ToString>(id: u64, name: S, capabilities: SeatCapabilities) -> Seat {
Seat {
id,
name: name.to_string(),
capabilities,
}
}
pub(crate) fn capabilities_mut(&mut self) -> &mut SeatCapabilities {
&mut self.capabilities
}
pub fn capabilities(&self) -> &SeatCapabilities {
&self.capabilities
}
pub fn name(&self) -> &str {
&*self.name
}
}
impl ::std::cmp::PartialEq for Seat {
fn eq(&self, other: &Seat) -> bool {
self.id == other.id
}
}
impl ::std::hash::Hash for Seat {
fn hash<H>(&self, state: &mut H)
where
H: ::std::hash::Hasher,
{
self.id.hash(state);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SeatCapabilities {
pub pointer: bool,
pub keyboard: bool,
pub touch: bool,
}
pub trait Event {
fn time(&self) -> u32;
}
pub enum UnusedEvent {}
impl Event for UnusedEvent {
fn time(&self) -> u32 {
match *self {}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum KeyState {
Released,
Pressed,
}
pub trait KeyboardKeyEvent: Event {
fn key_code(&self) -> u32;
fn state(&self) -> KeyState;
fn count(&self) -> u32;
}
impl KeyboardKeyEvent for UnusedEvent {
fn key_code(&self) -> u32 {
match *self {}
}
fn state(&self) -> KeyState {
match *self {}
}
fn count(&self) -> u32 {
match *self {}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MouseButton {
Left,
Middle,
Right,
Other(u8),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MouseButtonState {
Released,
Pressed,
}
pub trait PointerButtonEvent: Event {
fn button(&self) -> MouseButton;
fn state(&self) -> MouseButtonState;
}
impl PointerButtonEvent for UnusedEvent {
fn button(&self) -> MouseButton {
match *self {}
}
fn state(&self) -> MouseButtonState {
match *self {}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Axis {
Vertical,
Horizontal,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum AxisSource {
Finger,
Continuous,
Wheel,
WheelTilt,
}
pub trait PointerAxisEvent: Event {
fn amount(&self, axis: &Axis) -> Option<f64>;
fn amount_discrete(&self, axis: &Axis) -> Option<f64>;
fn source(&self) -> AxisSource;
}
impl PointerAxisEvent for UnusedEvent {
fn amount(&self, _axis: &Axis) -> Option<f64> {
match *self {}
}
fn amount_discrete(&self, _axis: &Axis) -> Option<f64> {
match *self {}
}
fn source(&self) -> AxisSource {
match *self {}
}
}
pub trait PointerMotionEvent: Event {
fn delta(&self) -> (i32, i32) {
(self.delta_x(), self.delta_y())
}
fn delta_x(&self) -> i32;
fn delta_y(&self) -> i32;
}
impl PointerMotionEvent for UnusedEvent {
fn delta_x(&self) -> i32 {
match *self {}
}
fn delta_y(&self) -> i32 {
match *self {}
}
}
pub trait PointerMotionAbsoluteEvent: Event {
fn position(&self) -> (f64, f64) {
(self.x(), self.y())
}
fn x(&self) -> f64;
fn y(&self) -> f64;
fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) {
(
self.x_transformed(coordinate_space.0),
self.y_transformed(coordinate_space.1),
)
}
fn x_transformed(&self, width: u32) -> u32;
fn y_transformed(&self, height: u32) -> u32;
}
impl PointerMotionAbsoluteEvent for UnusedEvent {
fn x(&self) -> f64 {
match *self {}
}
fn y(&self) -> f64 {
match *self {}
}
fn x_transformed(&self, _width: u32) -> u32 {
match *self {}
}
fn y_transformed(&self, _height: u32) -> u32 {
match *self {}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TouchSlot {
id: u64,
}
impl TouchSlot {
pub(crate) fn new(id: u64) -> Self {
TouchSlot { id }
}
}
pub trait TouchDownEvent: Event {
fn slot(&self) -> Option<TouchSlot>;
fn position(&self) -> (f64, f64) {
(self.x(), self.y())
}
fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) {
(
self.x_transformed(coordinate_space.0),
self.y_transformed(coordinate_space.1),
)
}
fn x(&self) -> f64;
fn y(&self) -> f64;
fn x_transformed(&self, width: u32) -> u32;
fn y_transformed(&self, height: u32) -> u32;
}
impl TouchDownEvent for UnusedEvent {
fn slot(&self) -> Option<TouchSlot> {
match *self {}
}
fn x(&self) -> f64 {
match *self {}
}
fn y(&self) -> f64 {
match *self {}
}
fn x_transformed(&self, _width: u32) -> u32 {
match *self {}
}
fn y_transformed(&self, _height: u32) -> u32 {
match *self {}
}
}
pub trait TouchMotionEvent: Event {
fn slot(&self) -> Option<TouchSlot>;
fn position(&self) -> (f64, f64) {
(self.x(), self.y())
}
fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) {
(
self.x_transformed(coordinate_space.0),
self.y_transformed(coordinate_space.1),
)
}
fn x(&self) -> f64;
fn y(&self) -> f64;
fn x_transformed(&self, width: u32) -> u32;
fn y_transformed(&self, height: u32) -> u32;
}
impl TouchMotionEvent for UnusedEvent {
fn slot(&self) -> Option<TouchSlot> {
match *self {}
}
fn x(&self) -> f64 {
match *self {}
}
fn y(&self) -> f64 {
match *self {}
}
fn x_transformed(&self, _width: u32) -> u32 {
match *self {}
}
fn y_transformed(&self, _height: u32) -> u32 {
match *self {}
}
}
pub trait TouchUpEvent: Event {
fn slot(&self) -> Option<TouchSlot>;
}
impl TouchUpEvent for UnusedEvent {
fn slot(&self) -> Option<TouchSlot> {
match *self {}
}
}
pub trait TouchCancelEvent: Event {
fn slot(&self) -> Option<TouchSlot>;
}
impl TouchCancelEvent for UnusedEvent {
fn slot(&self) -> Option<TouchSlot> {
match *self {}
}
}
pub trait TouchFrameEvent: Event {}
impl TouchFrameEvent for UnusedEvent {}
pub trait InputBackend: Sized {
type InputConfig: ?Sized;
type EventError: Error;
type KeyboardKeyEvent: KeyboardKeyEvent;
type PointerAxisEvent: PointerAxisEvent;
type PointerButtonEvent: PointerButtonEvent;
type PointerMotionEvent: PointerMotionEvent;
type PointerMotionAbsoluteEvent: PointerMotionAbsoluteEvent;
type TouchDownEvent: TouchDownEvent;
type TouchUpEvent: TouchUpEvent;
type TouchMotionEvent: TouchMotionEvent;
type TouchCancelEvent: TouchCancelEvent;
type TouchFrameEvent: TouchFrameEvent;
fn set_handler<H: InputHandler<Self> + 'static>(&mut self, handler: H);
fn get_handler(&mut self) -> Option<&mut dyn InputHandler<Self>>;
fn clear_handler(&mut self);
fn input_config(&mut self) -> &mut Self::InputConfig;
fn dispatch_new_events(&mut self) -> Result<(), Self::EventError>;
}
pub trait InputHandler<B: InputBackend> {
fn on_seat_created(&mut self, seat: &Seat);
fn on_seat_destroyed(&mut self, seat: &Seat);
fn on_seat_changed(&mut self, seat: &Seat);
fn on_keyboard_key(&mut self, seat: &Seat, event: B::KeyboardKeyEvent);
fn on_pointer_move(&mut self, seat: &Seat, event: B::PointerMotionEvent);
fn on_pointer_move_absolute(&mut self, seat: &Seat, event: B::PointerMotionAbsoluteEvent);
fn on_pointer_button(&mut self, seat: &Seat, event: B::PointerButtonEvent);
fn on_pointer_axis(&mut self, seat: &Seat, event: B::PointerAxisEvent);
fn on_touch_down(&mut self, seat: &Seat, event: B::TouchDownEvent);
fn on_touch_motion(&mut self, seat: &Seat, event: B::TouchMotionEvent);
fn on_touch_up(&mut self, seat: &Seat, event: B::TouchUpEvent);
fn on_touch_cancel(&mut self, seat: &Seat, event: B::TouchCancelEvent);
fn on_touch_frame(&mut self, seat: &Seat, event: B::TouchFrameEvent);
fn on_input_config_changed(&mut self, config: &mut B::InputConfig);
}
impl<B: InputBackend> InputHandler<B> for Box<dyn InputHandler<B>> {
fn on_seat_created(&mut self, seat: &Seat) {
(**self).on_seat_created(seat)
}
fn on_seat_destroyed(&mut self, seat: &Seat) {
(**self).on_seat_destroyed(seat)
}
fn on_seat_changed(&mut self, seat: &Seat) {
(**self).on_seat_changed(seat)
}
fn on_keyboard_key(&mut self, seat: &Seat, event: B::KeyboardKeyEvent) {
(**self).on_keyboard_key(seat, event)
}
fn on_pointer_move(&mut self, seat: &Seat, event: B::PointerMotionEvent) {
(**self).on_pointer_move(seat, event)
}
fn on_pointer_move_absolute(&mut self, seat: &Seat, event: B::PointerMotionAbsoluteEvent) {
(**self).on_pointer_move_absolute(seat, event)
}
fn on_pointer_button(&mut self, seat: &Seat, event: B::PointerButtonEvent) {
(**self).on_pointer_button(seat, event)
}
fn on_pointer_axis(&mut self, seat: &Seat, event: B::PointerAxisEvent) {
(**self).on_pointer_axis(seat, event)
}
fn on_touch_down(&mut self, seat: &Seat, event: B::TouchDownEvent) {
(**self).on_touch_down(seat, event)
}
fn on_touch_motion(&mut self, seat: &Seat, event: B::TouchMotionEvent) {
(**self).on_touch_motion(seat, event)
}
fn on_touch_up(&mut self, seat: &Seat, event: B::TouchUpEvent) {
(**self).on_touch_up(seat, event)
}
fn on_touch_cancel(&mut self, seat: &Seat, event: B::TouchCancelEvent) {
(**self).on_touch_cancel(seat, event)
}
fn on_touch_frame(&mut self, seat: &Seat, event: B::TouchFrameEvent) {
(**self).on_touch_frame(seat, event)
}
fn on_input_config_changed(&mut self, config: &mut B::InputConfig) {
(**self).on_input_config_changed(config)
}
}