From 8ca5d1f301cff00db72d71bd75f441c3f585107e Mon Sep 17 00:00:00 2001 From: i509VCB Date: Thu, 28 Oct 2021 15:19:09 -0500 Subject: [PATCH] input: remove `InputBackend::dispatch_new_events` --- CHANGELOG.md | 2 + src/backend/input/mod.rs | 10 +- src/backend/libinput/mod.rs | 195 +++++++++++++++++------------------- src/backend/winit/input.rs | 13 +-- src/backend/x11/input.rs | 14 +-- 5 files changed, 100 insertions(+), 134 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbf6942..7fed074 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ - All winit backend internal event types now use `WinitInput` as the backend type. - `WinitEventLoop::dispatch_new_events` is now used to receive some `WinitEvent`s. - Added `TabletToolType::Unknown` as an option for tablet events +- Remove `InputBackend::dispatch_new_events`, turning `InputBackend` into a definition of backend event types. Future input backends should be a `calloop::EventSource`. +- Remove `InputBackend::EventError` associated type as it is unneeded since `dispatch_new_events` was removed. ### Additions diff --git a/src/backend/input/mod.rs b/src/backend/input/mod.rs index 97129f7..476e6ea 100644 --- a/src/backend/input/mod.rs +++ b/src/backend/input/mod.rs @@ -1,6 +1,6 @@ //! Common traits for input backends to receive input from. -use std::{error::Error, path::PathBuf}; +use std::path::PathBuf; mod tablet; @@ -507,9 +507,6 @@ impl TouchFrameEvent for UnusedEvent {} /// need to implement this and provide the same base guarantees about the precision of /// given events. pub trait InputBackend: Sized { - /// Type representing errors that may be returned when processing events - type EventError: Error; - /// Type representing input devices type Device: Device; /// Type representing keyboard events @@ -543,11 +540,6 @@ pub trait InputBackend: Sized { /// Special events that are custom to this backend type SpecialEvent; - - /// Processes new events and calls the provided callback. - fn dispatch_new_events(&mut self, callback: F) -> Result<(), Self::EventError> - where - F: FnMut(InputEvent); } /// Different events that can be generated by an input backend diff --git a/src/backend/libinput/mod.rs b/src/backend/libinput/mod.rs index 98fac9c..11a567c 100644 --- a/src/backend/libinput/mod.rs +++ b/src/backend/libinput/mod.rs @@ -12,7 +12,6 @@ use input::event; #[cfg(feature = "backend_session")] use std::path::Path; use std::{ - io::Error as IoError, os::unix::io::{AsRawFd, RawFd}, path::PathBuf, }; @@ -355,8 +354,6 @@ impl backend::Event for event::touch::TouchFrameEvent { impl backend::TouchFrameEvent for event::touch::TouchFrameEvent {} impl InputBackend for LibinputInputBackend { - type EventError = IoError; - type Device = libinput::Device; type KeyboardKeyEvent = event::keyboard::KeyboardKeyEvent; type PointerAxisEvent = event::pointer::PointerAxisEvent; @@ -374,103 +371,6 @@ impl InputBackend for LibinputInputBackend { type TabletToolButtonEvent = event::tablet_tool::TabletToolButtonEvent; type SpecialEvent = backend::UnusedEvent; - - fn dispatch_new_events(&mut self, mut callback: F) -> Result<(), IoError> - where - F: FnMut(InputEvent), - { - self.context.dispatch()?; - - for event in &mut self.context { - match event { - libinput::Event::Device(device_event) => match device_event { - event::DeviceEvent::Added(device_added_event) => { - let added = event::EventTrait::device(&device_added_event); - - info!(self.logger, "New device {:?}", added.sysname(),); - - callback(InputEvent::DeviceAdded { device: added }); - } - event::DeviceEvent::Removed(device_removed_event) => { - let removed = event::EventTrait::device(&device_removed_event); - - info!(self.logger, "Removed device {:?}", removed.sysname(),); - - callback(InputEvent::DeviceRemoved { device: removed }); - } - _ => { - trace!(self.logger, "Unknown libinput device event"); - } - }, - libinput::Event::Touch(touch_event) => match touch_event { - event::TouchEvent::Down(down_event) => { - callback(InputEvent::TouchDown { event: down_event }); - } - event::TouchEvent::Motion(motion_event) => { - callback(InputEvent::TouchMotion { event: motion_event }); - } - event::TouchEvent::Up(up_event) => { - callback(InputEvent::TouchUp { event: up_event }); - } - event::TouchEvent::Cancel(cancel_event) => { - callback(InputEvent::TouchCancel { event: cancel_event }); - } - event::TouchEvent::Frame(frame_event) => { - callback(InputEvent::TouchFrame { event: frame_event }); - } - _ => { - trace!(self.logger, "Unknown libinput touch event"); - } - }, - libinput::Event::Keyboard(keyboard_event) => match keyboard_event { - event::KeyboardEvent::Key(key_event) => { - callback(InputEvent::Keyboard { event: key_event }); - } - _ => { - trace!(self.logger, "Unknown libinput keyboard event"); - } - }, - libinput::Event::Pointer(pointer_event) => match pointer_event { - event::PointerEvent::Motion(motion_event) => { - callback(InputEvent::PointerMotion { event: motion_event }); - } - event::PointerEvent::MotionAbsolute(motion_abs_event) => { - callback(InputEvent::PointerMotionAbsolute { - event: motion_abs_event, - }); - } - event::PointerEvent::Axis(axis_event) => { - callback(InputEvent::PointerAxis { event: axis_event }); - } - event::PointerEvent::Button(button_event) => { - callback(InputEvent::PointerButton { event: button_event }); - } - _ => { - trace!(self.logger, "Unknown libinput pointer event"); - } - }, - libinput::Event::Tablet(tablet_event) => match tablet_event { - event::TabletToolEvent::Axis(event) => { - callback(InputEvent::TabletToolAxis { event }); - } - event::TabletToolEvent::Proximity(event) => { - callback(InputEvent::TabletToolProximity { event }); - } - event::TabletToolEvent::Tip(event) => { - callback(InputEvent::TabletToolTip { event }); - } - event::TabletToolEvent::Button(event) => { - callback(InputEvent::TabletToolButton { event }); - } - _ => { - trace!(self.logger, "Unknown libinput tablet event"); - } - }, - _ => {} //FIXME: What to do with the rest. - } - } - Ok(()) - } } impl From for backend::KeyState { @@ -568,8 +468,101 @@ impl EventSource for LibinputInputBackend { F: FnMut(Self::Event, &mut ()) -> Self::Ret, { if token == self.token { - self.dispatch_new_events(|evt| callback(evt, &mut ()))?; + self.context.dispatch()?; + + for event in &mut self.context { + match event { + libinput::Event::Device(device_event) => match device_event { + event::DeviceEvent::Added(device_added_event) => { + let added = event::EventTrait::device(&device_added_event); + + info!(self.logger, "New device {:?}", added.sysname(),); + + callback(InputEvent::DeviceAdded { device: added }, &mut ()); + } + event::DeviceEvent::Removed(device_removed_event) => { + let removed = event::EventTrait::device(&device_removed_event); + + info!(self.logger, "Removed device {:?}", removed.sysname(),); + + callback(InputEvent::DeviceRemoved { device: removed }, &mut ()); + } + _ => { + trace!(self.logger, "Unknown libinput device event"); + } + }, + libinput::Event::Touch(touch_event) => match touch_event { + event::TouchEvent::Down(down_event) => { + callback(InputEvent::TouchDown { event: down_event }, &mut ()); + } + event::TouchEvent::Motion(motion_event) => { + callback(InputEvent::TouchMotion { event: motion_event }, &mut ()); + } + event::TouchEvent::Up(up_event) => { + callback(InputEvent::TouchUp { event: up_event }, &mut ()); + } + event::TouchEvent::Cancel(cancel_event) => { + callback(InputEvent::TouchCancel { event: cancel_event }, &mut ()); + } + event::TouchEvent::Frame(frame_event) => { + callback(InputEvent::TouchFrame { event: frame_event }, &mut ()); + } + _ => { + trace!(self.logger, "Unknown libinput touch event"); + } + }, + libinput::Event::Keyboard(keyboard_event) => match keyboard_event { + event::KeyboardEvent::Key(key_event) => { + callback(InputEvent::Keyboard { event: key_event }, &mut ()); + } + _ => { + trace!(self.logger, "Unknown libinput keyboard event"); + } + }, + libinput::Event::Pointer(pointer_event) => match pointer_event { + event::PointerEvent::Motion(motion_event) => { + callback(InputEvent::PointerMotion { event: motion_event }, &mut ()); + } + event::PointerEvent::MotionAbsolute(motion_abs_event) => { + callback( + InputEvent::PointerMotionAbsolute { + event: motion_abs_event, + }, + &mut (), + ); + } + event::PointerEvent::Axis(axis_event) => { + callback(InputEvent::PointerAxis { event: axis_event }, &mut ()); + } + event::PointerEvent::Button(button_event) => { + callback(InputEvent::PointerButton { event: button_event }, &mut ()); + } + _ => { + trace!(self.logger, "Unknown libinput pointer event"); + } + }, + libinput::Event::Tablet(tablet_event) => match tablet_event { + event::TabletToolEvent::Axis(event) => { + callback(InputEvent::TabletToolAxis { event }, &mut ()); + } + event::TabletToolEvent::Proximity(event) => { + callback(InputEvent::TabletToolProximity { event }, &mut ()); + } + event::TabletToolEvent::Tip(event) => { + callback(InputEvent::TabletToolTip { event }, &mut ()); + } + event::TabletToolEvent::Button(event) => { + callback(InputEvent::TabletToolButton { event }, &mut ()); + } + _ => { + trace!(self.logger, "Unknown libinput tablet event"); + } + }, + _ => {} //FIXME: What to do with the rest. + } + } } + Ok(PostAction::Continue) } diff --git a/src/backend/winit/input.rs b/src/backend/winit/input.rs index 3fd7c6f..24e232a 100644 --- a/src/backend/winit/input.rs +++ b/src/backend/winit/input.rs @@ -6,12 +6,12 @@ use winit::{ }; use crate::backend::input::{ - self, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, InputBackend, InputEvent, KeyState, + self, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, InputBackend, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionAbsoluteEvent, TouchCancelEvent, TouchDownEvent, TouchMotionEvent, TouchSlot, TouchUpEvent, UnusedEvent, }; -use super::{WindowSize, WinitError}; +use super::WindowSize; /// Marker used to define the `InputBackend` types for the winit backend. #[derive(Debug)] @@ -363,8 +363,6 @@ impl From for ButtonState { } impl InputBackend for WinitInput { - type EventError = WinitError; - type Device = WinitVirtualDevice; type KeyboardKeyEvent = WinitKeyboardInputEvent; type PointerAxisEvent = WinitMouseWheelEvent; @@ -382,11 +380,4 @@ impl InputBackend for WinitInput { type TabletToolButtonEvent = UnusedEvent; type SpecialEvent = UnusedEvent; - - fn dispatch_new_events(&mut self, _callback: F) -> Result<(), Self::EventError> - where - F: FnMut(InputEvent), - { - unreachable!() - } } diff --git a/src/backend/x11/input.rs b/src/backend/x11/input.rs index 547ab8b..3b5774f 100644 --- a/src/backend/x11/input.rs +++ b/src/backend/x11/input.rs @@ -1,9 +1,8 @@ //! Input backend implementation for the X11 backend. -use super::X11Error; use crate::{ backend::input::{ - self, Axis, AxisSource, ButtonState, Device, DeviceCapability, InputBackend, InputEvent, KeyState, + self, Axis, AxisSource, ButtonState, Device, DeviceCapability, InputBackend, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionAbsoluteEvent, UnusedEvent, }, utils::{Logical, Size}, @@ -182,8 +181,6 @@ impl PointerMotionAbsoluteEvent for X11MouseMovedEvent { } impl InputBackend for X11Input { - type EventError = X11Error; - type Device = X11VirtualDevice; type KeyboardKeyEvent = X11KeyboardInputEvent; type PointerAxisEvent = X11MouseWheelEvent; @@ -204,13 +201,4 @@ impl InputBackend for X11Input { type TabletToolButtonEvent = UnusedEvent; type SpecialEvent = UnusedEvent; - - fn dispatch_new_events(&mut self, _callback: F) -> Result<(), Self::EventError> - where - F: FnMut(InputEvent), - { - // The implementation of the trait here is exclusively for type definitions. - // See `X11Event::Input` to handle input events. - unreachable!() - } }