From 469f6c337c9f19b4bbeb25d74fc0d0493a414f05 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Wed, 14 Aug 2024 01:12:06 +0200 Subject: [PATCH] Change `DeviceId` to `Option` --- examples/window.rs | 2 +- src/application.rs | 6 +- src/changelog/unreleased.md | 2 + src/event.rs | 62 ++++++++++--------- src/platform_impl/android/mod.rs | 4 +- src/platform_impl/apple/appkit/app.rs | 7 +-- src/platform_impl/apple/appkit/mod.rs | 4 -- src/platform_impl/apple/appkit/view.rs | 37 +++++------ src/platform_impl/apple/uikit/mod.rs | 3 - src/platform_impl/apple/uikit/view.rs | 12 ++-- src/platform_impl/linux/mod.rs | 1 + .../linux/wayland/event_loop/mod.rs | 2 +- .../linux/wayland/event_loop/sink.rs | 12 ++-- .../linux/wayland/seat/keyboard/mod.rs | 5 +- .../linux/wayland/seat/pointer/mod.rs | 20 +++--- .../wayland/seat/pointer/relative_pointer.rs | 7 +-- .../linux/wayland/seat/touch/mod.rs | 18 ++---- .../linux/x11/event_processor.rs | 38 ++++++------ src/platform_impl/linux/x11/util/input.rs | 1 - src/platform_impl/orbital/event_loop.rs | 31 ++++------ src/platform_impl/web/event.rs | 19 +++--- src/platform_impl/web/event_loop/mod.rs | 2 +- src/platform_impl/web/event_loop/runner.rs | 12 ++-- .../web/event_loop/window_target.rs | 31 ++++------ src/platform_impl/web/web_sys/canvas.rs | 28 ++++++--- src/platform_impl/web/web_sys/pointer.rs | 28 ++++++--- src/platform_impl/windows/event_loop.rs | 38 ++++++------ src/platform_impl/windows/mod.rs | 3 - 28 files changed, 208 insertions(+), 227 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index 61bd51bd72..c4c41258c5 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -522,7 +522,7 @@ impl ApplicationHandler for Application { fn device_event( &mut self, _event_loop: &dyn ActiveEventLoop, - device_id: DeviceId, + device_id: Option, event: DeviceEvent, ) { info!("Device {device_id:?} event: {event:?}"); diff --git a/src/application.rs b/src/application.rs index 8b268b05d5..2dc224c941 100644 --- a/src/application.rs +++ b/src/application.rs @@ -196,7 +196,7 @@ pub trait ApplicationHandler { fn device_event( &mut self, event_loop: &dyn ActiveEventLoop, - device_id: DeviceId, + device_id: Option, event: DeviceEvent, ) { let _ = (event_loop, device_id, event); @@ -363,7 +363,7 @@ impl ApplicationHandler for &mut A { fn device_event( &mut self, event_loop: &dyn ActiveEventLoop, - device_id: DeviceId, + device_id: Option, event: DeviceEvent, ) { (**self).device_event(event_loop, device_id, event); @@ -431,7 +431,7 @@ impl ApplicationHandler for Box { fn device_event( &mut self, event_loop: &dyn ActiveEventLoop, - device_id: DeviceId, + device_id: Option, event: DeviceEvent, ) { (**self).device_event(event_loop, device_id, event); diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index b75a80dc80..d2e159c061 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -103,6 +103,8 @@ changelog entry. application delegate yourself. - On iOS, no longer act as-if the application successfully open all URLs. Override `application:didFinishLaunchingWithOptions:` and provide the desired behaviour yourself. +- Every event carrying a `DeviceId` now uses `Option` instead. This signifies that the + device can't be uniquely differentiated. ### Removed diff --git a/src/event.rs b/src/event.rs index 468778a81a..cf8fcd17a4 100644 --- a/src/event.rs +++ b/src/event.rs @@ -78,7 +78,7 @@ pub(crate) enum Event { /// /// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event #[allow(clippy::enum_variant_names)] - DeviceEvent { device_id: DeviceId, event: DeviceEvent }, + DeviceEvent { device_id: Option, event: DeviceEvent }, /// See [`ApplicationHandler::suspended`] for details. /// @@ -194,7 +194,7 @@ pub enum WindowEvent { /// numpad keys act as if NumLock wasn't active. When this is used, the OS sends fake key /// events which are not marked as `is_synthetic`. KeyboardInput { - device_id: DeviceId, + device_id: Option, event: KeyEvent, /// If `true`, the event was generated synthetically by winit @@ -231,7 +231,7 @@ pub enum WindowEvent { /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform CursorMoved { - device_id: DeviceId, + device_id: Option, /// (x,y) coords in pixels relative to the top-left corner of the window. Because the range /// of this data is limited by the display area and it may have been transformed by @@ -249,7 +249,7 @@ pub enum WindowEvent { /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform - CursorEntered { device_id: DeviceId }, + CursorEntered { device_id: Option }, /// The cursor has left the window. /// @@ -260,13 +260,13 @@ pub enum WindowEvent { /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform - CursorLeft { device_id: DeviceId }, + CursorLeft { device_id: Option }, /// A mouse wheel movement or touchpad scroll occurred. - MouseWheel { device_id: DeviceId, delta: MouseScrollDelta, phase: TouchPhase }, + MouseWheel { device_id: Option, delta: MouseScrollDelta, phase: TouchPhase }, /// An mouse button press has been received. - MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton }, + MouseInput { device_id: Option, state: ElementState, button: MouseButton }, /// Two-finger pinch gesture, often used for magnification. /// @@ -275,7 +275,7 @@ pub enum WindowEvent { /// - Only available on **macOS** and **iOS**. /// - On iOS, not recognized by default. It must be enabled when needed. PinchGesture { - device_id: DeviceId, + device_id: Option, /// Positive values indicate magnification (zooming in) and negative /// values indicate shrinking (zooming out). /// @@ -291,7 +291,7 @@ pub enum WindowEvent { /// - Only available on **iOS**. /// - On iOS, not recognized by default. It must be enabled when needed. PanGesture { - device_id: DeviceId, + device_id: Option, /// Change in pixels of pan gesture from last update. delta: PhysicalPosition, phase: TouchPhase, @@ -315,7 +315,7 @@ pub enum WindowEvent { /// /// - Only available on **macOS 10.8** and later, and **iOS**. /// - On iOS, not recognized by default. It must be enabled when needed. - DoubleTapGesture { device_id: DeviceId }, + DoubleTapGesture { device_id: Option }, /// Two-finger rotation gesture. /// @@ -327,7 +327,7 @@ pub enum WindowEvent { /// - Only available on **macOS** and **iOS**. /// - On iOS, not recognized by default. It must be enabled when needed. RotationGesture { - device_id: DeviceId, + device_id: Option, /// change in rotation in degrees delta: f32, phase: TouchPhase, @@ -338,7 +338,7 @@ pub enum WindowEvent { /// At the moment, only supported on Apple forcetouch-capable macbooks. /// The parameters are: pressure level (value between 0 and 1 representing how hard the /// touchpad is being pressed) and stage (integer representing the click level). - TouchpadPressure { device_id: DeviceId, pressure: f32, stage: i64 }, + TouchpadPressure { device_id: Option, pressure: f32, stage: i64 }, /// Touch event has been received /// @@ -841,7 +841,7 @@ pub enum TouchPhase { /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform #[derive(Debug, Clone, Copy, PartialEq)] pub struct Touch { - pub device_id: DeviceId, + pub device_id: Option, pub phase: TouchPhase, pub location: PhysicalPosition, /// Describes how hard the screen was pressed. May be `None` if the platform @@ -1023,7 +1023,6 @@ mod tests { ($closure:expr) => {{ #[allow(unused_mut)] let mut x = $closure; - let did = event::DeviceId::dummy(); let fid = event::FingerId::dummy(); #[allow(deprecated)] @@ -1053,39 +1052,39 @@ mod tests { with_window_event(HoveredFile("x.txt".into())); with_window_event(HoveredFileCancelled); with_window_event(Ime(Enabled)); - with_window_event(CursorMoved { device_id: did, position: (0, 0).into() }); + with_window_event(CursorMoved { device_id: None, position: (0, 0).into() }); with_window_event(ModifiersChanged(event::Modifiers::default())); - with_window_event(CursorEntered { device_id: did }); - with_window_event(CursorLeft { device_id: did }); + with_window_event(CursorEntered { device_id: None }); + with_window_event(CursorLeft { device_id: None }); with_window_event(MouseWheel { - device_id: did, + device_id: None, delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), phase: event::TouchPhase::Started, }); with_window_event(MouseInput { - device_id: did, + device_id: None, state: event::ElementState::Pressed, button: event::MouseButton::Other(0), }); with_window_event(PinchGesture { - device_id: did, + device_id: None, delta: 0.0, phase: event::TouchPhase::Started, }); - with_window_event(DoubleTapGesture { device_id: did }); + with_window_event(DoubleTapGesture { device_id: None }); with_window_event(RotationGesture { - device_id: did, + device_id: None, delta: 0.0, phase: event::TouchPhase::Started, }); with_window_event(PanGesture { - device_id: did, + device_id: None, delta: PhysicalPosition::::new(0.0, 0.0), phase: event::TouchPhase::Started, }); - with_window_event(TouchpadPressure { device_id: did, pressure: 0.0, stage: 0 }); + with_window_event(TouchpadPressure { device_id: None, pressure: 0.0, stage: 0 }); with_window_event(Touch(event::Touch { - device_id: did, + device_id: None, phase: event::TouchPhase::Started, location: (0.0, 0.0).into(), finger_id: fid, @@ -1100,7 +1099,7 @@ mod tests { use event::DeviceEvent::*; let with_device_event = - |dev_ev| x(event::Event::DeviceEvent { device_id: did, event: dev_ev }); + |dev_ev| x(event::Event::DeviceEvent { device_id: None, event: dev_ev }); with_device_event(MouseMotion { delta: (0.0, 0.0).into() }); with_device_event(MouseWheel { @@ -1146,7 +1145,6 @@ mod tests { let _ = event::StartCause::Init.clone(); let did = crate::event::DeviceId::dummy().clone(); - let fid = crate::event::FingerId::dummy().clone(); HashSet::new().insert(did); let mut set = [did, did, did]; set.sort_unstable(); @@ -1154,12 +1152,20 @@ mod tests { set2.insert(did); set2.insert(did); + let fid = crate::event::FingerId::dummy().clone(); + HashSet::new().insert(fid); + let mut set = [fid, fid, fid]; + set.sort_unstable(); + let mut set2 = BTreeSet::new(); + set2.insert(fid); + set2.insert(fid); + HashSet::new().insert(event::TouchPhase::Started.clone()); HashSet::new().insert(event::MouseButton::Left.clone()); HashSet::new().insert(event::Ime::Enabled); let _ = event::Touch { - device_id: did, + device_id: None, phase: event::TouchPhase::Started, location: (0.0, 0.0).into(), finger_id: fid, diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 81c5cc6f2f..86a2906b42 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -317,7 +317,7 @@ impl EventLoop { match event { InputEvent::MotionEvent(motion_event) => { let window_id = window::WindowId(WindowId); - let device_id = event::DeviceId(DeviceId(motion_event.device_id())); + let device_id = Some(event::DeviceId(DeviceId(motion_event.device_id()))); let phase = match motion_event.action() { MotionAction::Down | MotionAction::PointerDown => { @@ -389,7 +389,7 @@ impl EventLoop { let window_id = window::WindowId(WindowId); let event = event::WindowEvent::KeyboardInput { - device_id: event::DeviceId(DeviceId(key.device_id())), + device_id: Some(event::DeviceId(DeviceId(key.device_id()))), event: event::KeyEvent { state, physical_key: keycodes::to_physical_key(keycode), diff --git a/src/platform_impl/apple/appkit/app.rs b/src/platform_impl/apple/appkit/app.rs index 5313640131..5292f92f41 100644 --- a/src/platform_impl/apple/appkit/app.rs +++ b/src/platform_impl/apple/appkit/app.rs @@ -7,7 +7,6 @@ use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, N use objc2_foundation::{MainThreadMarker, NSObject}; use super::app_state::AppState; -use super::DEVICE_ID; use crate::event::{DeviceEvent, ElementState}; declare_class!( @@ -61,7 +60,7 @@ fn maybe_dispatch_device_event(app_state: &Rc, event: &NSEvent) { if delta_x != 0.0 || delta_y != 0.0 { app_state.maybe_queue_with_handler(move |app, event_loop| { - app.device_event(event_loop, DEVICE_ID, DeviceEvent::MouseMotion { + app.device_event(event_loop, None, DeviceEvent::MouseMotion { delta: (delta_x, delta_y), }); }); @@ -70,7 +69,7 @@ fn maybe_dispatch_device_event(app_state: &Rc, event: &NSEvent) { NSEventType::LeftMouseDown | NSEventType::RightMouseDown | NSEventType::OtherMouseDown => { let button = unsafe { event.buttonNumber() } as u32; app_state.maybe_queue_with_handler(move |app, event_loop| { - app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button { + app.device_event(event_loop, None, DeviceEvent::Button { button, state: ElementState::Pressed, }); @@ -79,7 +78,7 @@ fn maybe_dispatch_device_event(app_state: &Rc, event: &NSEvent) { NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => { let button = unsafe { event.buttonNumber() } as u32; app_state.maybe_queue_with_handler(move |app, event_loop| { - app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button { + app.device_event(event_loop, None, DeviceEvent::Button { button, state: ElementState::Released, }); diff --git a/src/platform_impl/apple/appkit/mod.rs b/src/platform_impl/apple/appkit/mod.rs index 7aeacf147d..f466f5e8f7 100644 --- a/src/platform_impl/apple/appkit/mod.rs +++ b/src/platform_impl/apple/appkit/mod.rs @@ -27,7 +27,6 @@ pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle}; pub(crate) use self::window::{Window, WindowId}; pub(crate) use self::window_delegate::PlatformSpecificWindowAttributes; pub(crate) use crate::cursor::OnlyCursorImageSource as PlatformCustomCursorSource; -use crate::event::DeviceId as RootDeviceId; pub(crate) use crate::icon::NoIcon as PlatformIcon; pub(crate) use crate::platform_impl::Fullscreen; @@ -41,9 +40,6 @@ impl DeviceId { } } -// Constant device ID; to be removed when if backend is updated to report real device IDs. -pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId); - #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct FingerId; diff --git a/src/platform_impl/apple/appkit/view.rs b/src/platform_impl/apple/appkit/view.rs index 9392a283fc..1bcfd70ef5 100644 --- a/src/platform_impl/apple/appkit/view.rs +++ b/src/platform_impl/apple/appkit/view.rs @@ -24,7 +24,6 @@ use super::event::{ scancode_to_physicalkey, }; use super::window::WinitWindow; -use super::DEVICE_ID; use crate::dpi::{LogicalPosition, LogicalSize}; use crate::event::{ DeviceEvent, ElementState, Ime, Modifiers, MouseButton, MouseScrollDelta, TouchPhase, @@ -486,7 +485,7 @@ declare_class!( if !had_ime_input || self.ivars().forward_key_to_app.get() { let key_event = create_key_event(&event, true, unsafe { event.isARepeat() }, None); self.queue_event(WindowEvent::KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event: key_event, is_synthetic: false, }); @@ -506,7 +505,7 @@ declare_class!( ImeState::Ground | ImeState::Disabled ) { self.queue_event(WindowEvent::KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event: create_key_event(&event, false, false, None), is_synthetic: false, }); @@ -557,7 +556,7 @@ declare_class!( let event = create_key_event(&event, true, unsafe { event.isARepeat() }, None); self.queue_event(WindowEvent::KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event, is_synthetic: false, }); @@ -642,7 +641,7 @@ declare_class!( fn mouse_entered(&self, _event: &NSEvent) { trace_scope!("mouseEntered:"); self.queue_event(WindowEvent::CursorEntered { - device_id: DEVICE_ID, + device_id: None, }); } @@ -651,7 +650,7 @@ declare_class!( trace_scope!("mouseExited:"); self.queue_event(WindowEvent::CursorLeft { - device_id: DEVICE_ID, + device_id: None, }); } @@ -689,10 +688,10 @@ declare_class!( self.update_modifiers(event, false); self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| - app.device_event(event_loop, DEVICE_ID, DeviceEvent::MouseWheel { delta }) + app.device_event(event_loop, None, DeviceEvent::MouseWheel { delta }) ); self.queue_event(WindowEvent::MouseWheel { - device_id: DEVICE_ID, + device_id: None, delta, phase, }); @@ -714,7 +713,7 @@ declare_class!( }; self.queue_event(WindowEvent::PinchGesture { - device_id: DEVICE_ID, + device_id: None, delta: unsafe { event.magnification() }, phase, }); @@ -727,7 +726,7 @@ declare_class!( self.mouse_motion(event); self.queue_event(WindowEvent::DoubleTapGesture { - device_id: DEVICE_ID, + device_id: None, }); } @@ -747,7 +746,7 @@ declare_class!( }; self.queue_event(WindowEvent::RotationGesture { - device_id: DEVICE_ID, + device_id: None, delta: unsafe { event.rotation() }, phase, }); @@ -758,7 +757,7 @@ declare_class!( trace_scope!("pressureChangeWithEvent:"); self.queue_event(WindowEvent::TouchpadPressure { - device_id: DEVICE_ID, + device_id: None, pressure: unsafe { event.pressure() }, stage: unsafe { event.stage() } as i64, }); @@ -972,7 +971,7 @@ impl WinitView { event.location = KeyLocation::Left; event.physical_key = get_left_modifier_code(&event.logical_key).into(); events.push_back(WindowEvent::KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event, is_synthetic: false, }); @@ -981,7 +980,7 @@ impl WinitView { event.location = KeyLocation::Right; event.physical_key = get_right_modifier_code(&event.logical_key).into(); events.push_back(WindowEvent::KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event, is_synthetic: false, }); @@ -1012,7 +1011,7 @@ impl WinitView { } events.push_back(WindowEvent::KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event, is_synthetic: false, }); @@ -1038,11 +1037,7 @@ impl WinitView { self.update_modifiers(event, false); - self.queue_event(WindowEvent::MouseInput { - device_id: DEVICE_ID, - state: button_state, - button, - }); + self.queue_event(WindowEvent::MouseInput { device_id: None, state: button_state, button }); } fn mouse_motion(&self, event: &NSEvent) { @@ -1067,7 +1062,7 @@ impl WinitView { self.update_modifiers(event, false); self.queue_event(WindowEvent::CursorMoved { - device_id: DEVICE_ID, + device_id: None, position: view_point.to_physical(self.scale_factor()), }); } diff --git a/src/platform_impl/apple/uikit/mod.rs b/src/platform_impl/apple/uikit/mod.rs index 57293a2410..98af1bdaaa 100644 --- a/src/platform_impl/apple/uikit/mod.rs +++ b/src/platform_impl/apple/uikit/mod.rs @@ -18,7 +18,6 @@ pub(crate) use self::window::{PlatformSpecificWindowAttributes, Window, WindowId pub(crate) use crate::cursor::{ NoCustomCursor as PlatformCustomCursor, NoCustomCursor as PlatformCustomCursorSource, }; -use crate::event::DeviceId as RootDeviceId; pub(crate) use crate::icon::NoIcon as PlatformIcon; pub(crate) use crate::platform_impl::Fullscreen; @@ -36,8 +35,6 @@ impl DeviceId { } } -pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId); - #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct FingerId(usize); diff --git a/src/platform_impl/apple/uikit/view.rs b/src/platform_impl/apple/uikit/view.rs index d946445eda..2eb4cc3487 100644 --- a/src/platform_impl/apple/uikit/view.rs +++ b/src/platform_impl/apple/uikit/view.rs @@ -14,7 +14,7 @@ use objc2_ui_kit::{ use super::app_state::{self, EventWrapper}; use super::window::WinitUIWindow; -use super::{FingerId, DEVICE_ID}; +use super::FingerId; use crate::dpi::PhysicalPosition; use crate::event::{Event, FingerId as RootFingerId, Force, Touch, TouchPhase, WindowEvent}; use crate::window::{WindowAttributes, WindowId as RootWindowId}; @@ -194,7 +194,7 @@ declare_class!( let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { window_id: RootWindowId(window.id()), event: WindowEvent::PinchGesture { - device_id: DEVICE_ID, + device_id: None, delta: delta as f64, phase, }, @@ -212,7 +212,7 @@ declare_class!( let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { window_id: RootWindowId(window.id()), event: WindowEvent::DoubleTapGesture { - device_id: DEVICE_ID, + device_id: None, }, }); @@ -254,7 +254,7 @@ declare_class!( let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { window_id: RootWindowId(window.id()), event: WindowEvent::RotationGesture { - device_id: DEVICE_ID, + device_id: None, delta: -delta.to_degrees() as _, phase, }, @@ -305,7 +305,7 @@ declare_class!( let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { window_id: RootWindowId(window.id()), event: WindowEvent::PanGesture { - device_id: DEVICE_ID, + device_id: None, delta: PhysicalPosition::new(dx as _, dy as _), phase, }, @@ -501,7 +501,7 @@ impl WinitView { touch_events.push(EventWrapper::StaticEvent(Event::WindowEvent { window_id: RootWindowId(window.id()), event: WindowEvent::Touch(Touch { - device_id: DEVICE_ID, + device_id: None, finger_id: RootFingerId(FingerId(touch_id)), location: physical_location, force, diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index eded839e5d..5647cb1a05 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -158,6 +158,7 @@ pub enum DeviceId { #[cfg(x11_platform)] X(x11::DeviceId), #[cfg(wayland_platform)] + #[allow(unused)] Wayland(wayland::DeviceId), } diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 36b56e8258..b298a9c3c5 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -31,7 +31,7 @@ use sink::EventSink; use super::state::{WindowCompositorUpdate, WinitState}; use super::window::state::FrameCallbackState; -use super::{logical_to_physical_rounded, DeviceId, WaylandError, WindowId}; +use super::{logical_to_physical_rounded, WaylandError, WindowId}; type WaylandDispatcher = calloop::Dispatcher<'static, WaylandSource, WinitState>; diff --git a/src/platform_impl/linux/wayland/event_loop/sink.rs b/src/platform_impl/linux/wayland/event_loop/sink.rs index a313a621ce..ed33f8fd06 100644 --- a/src/platform_impl/linux/wayland/event_loop/sink.rs +++ b/src/platform_impl/linux/wayland/event_loop/sink.rs @@ -2,9 +2,8 @@ use std::vec::Drain; -use super::{DeviceId, WindowId}; -use crate::event::{DeviceEvent, DeviceId as RootDeviceId, Event, WindowEvent}; -use crate::platform_impl::platform::DeviceId as PlatformDeviceId; +use super::WindowId; +use crate::event::{DeviceEvent, Event, WindowEvent}; use crate::window::WindowId as RootWindowId; /// An event loop's sink to deliver events from the Wayland event callbacks @@ -27,11 +26,8 @@ impl EventSink { /// Add new device event to a queue. #[inline] - pub fn push_device_event(&mut self, event: DeviceEvent, device_id: DeviceId) { - self.window_events.push(Event::DeviceEvent { - event, - device_id: RootDeviceId(PlatformDeviceId::Wayland(device_id)), - }); + pub fn push_device_event(&mut self, event: DeviceEvent) { + self.window_events.push(Event::DeviceEvent { event, device_id: None }); } /// Add new window event to a queue. diff --git a/src/platform_impl/linux/wayland/seat/keyboard/mod.rs b/src/platform_impl/linux/wayland/seat/keyboard/mod.rs index 2ce88cd76b..871df785e3 100644 --- a/src/platform_impl/linux/wayland/seat/keyboard/mod.rs +++ b/src/platform_impl/linux/wayland/seat/keyboard/mod.rs @@ -17,7 +17,7 @@ use crate::keyboard::ModifiersState; use crate::platform_impl::common::xkb::Context; use crate::platform_impl::wayland::event_loop::sink::EventSink; use crate::platform_impl::wayland::state::WinitState; -use crate::platform_impl::wayland::{self, DeviceId, WindowId}; +use crate::platform_impl::wayland::{self, WindowId}; impl Dispatch for WinitState { fn event( @@ -369,10 +369,9 @@ fn key_input( None => return, }; - let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId)); if let Some(mut key_context) = keyboard_state.xkb_context.key_context() { let event = key_context.process_key_event(keycode, state, repeat); - let event = WindowEvent::KeyboardInput { device_id, event, is_synthetic: false }; + let event = WindowEvent::KeyboardInput { device_id: None, event, is_synthetic: false }; event_sink.push_window_event(event, window_id); } } diff --git a/src/platform_impl/linux/wayland/seat/pointer/mod.rs b/src/platform_impl/linux/wayland/seat/pointer/mod.rs index fcca59343b..e99988fe28 100644 --- a/src/platform_impl/linux/wayland/seat/pointer/mod.rs +++ b/src/platform_impl/linux/wayland/seat/pointer/mod.rs @@ -30,7 +30,7 @@ use crate::dpi::{LogicalPosition, PhysicalPosition}; use crate::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent}; use crate::platform_impl::wayland::state::WinitState; -use crate::platform_impl::wayland::{self, DeviceId, WindowId}; +use crate::platform_impl::wayland::{self, WindowId}; pub mod relative_pointer; @@ -59,8 +59,6 @@ impl PointerHandler for WinitState { }, }; - let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId)); - for event in events { let surface = &event.surface; @@ -124,8 +122,10 @@ impl PointerHandler for WinitState { }, // Regular events on the main surface. PointerEventKind::Enter { .. } => { - self.events_sink - .push_window_event(WindowEvent::CursorEntered { device_id }, window_id); + self.events_sink.push_window_event( + WindowEvent::CursorEntered { device_id: None }, + window_id, + ); window.pointer_entered(Arc::downgrade(themed_pointer)); @@ -133,7 +133,7 @@ impl PointerHandler for WinitState { pointer.winit_data().inner.lock().unwrap().surface = Some(window_id); self.events_sink.push_window_event( - WindowEvent::CursorMoved { device_id, position }, + WindowEvent::CursorMoved { device_id: None, position }, window_id, ); }, @@ -144,11 +144,11 @@ impl PointerHandler for WinitState { pointer.winit_data().inner.lock().unwrap().surface = None; self.events_sink - .push_window_event(WindowEvent::CursorLeft { device_id }, window_id); + .push_window_event(WindowEvent::CursorLeft { device_id: None }, window_id); }, PointerEventKind::Motion { .. } => { self.events_sink.push_window_event( - WindowEvent::CursorMoved { device_id, position }, + WindowEvent::CursorMoved { device_id: None, position }, window_id, ); }, @@ -164,7 +164,7 @@ impl PointerHandler for WinitState { ElementState::Released }; self.events_sink.push_window_event( - WindowEvent::MouseInput { device_id, state, button }, + WindowEvent::MouseInput { device_id: None, state, button }, window_id, ); }, @@ -209,7 +209,7 @@ impl PointerHandler for WinitState { }; self.events_sink.push_window_event( - WindowEvent::MouseWheel { device_id, delta, phase }, + WindowEvent::MouseWheel { device_id: None, delta, phase }, window_id, ) }, diff --git a/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs b/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs index 33fc9e91e3..b71fc238d2 100644 --- a/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs +++ b/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs @@ -66,10 +66,9 @@ impl Dispatch for RelativePointerS }, _ => return, }; - state.events_sink.push_device_event( - DeviceEvent::MouseMotion { delta: (dx_unaccel, dy_unaccel) }, - super::DeviceId, - ); + state + .events_sink + .push_device_event(DeviceEvent::MouseMotion { delta: (dx_unaccel, dy_unaccel) }); } } diff --git a/src/platform_impl/linux/wayland/seat/touch/mod.rs b/src/platform_impl/linux/wayland/seat/touch/mod.rs index 665279f019..caaf77224d 100644 --- a/src/platform_impl/linux/wayland/seat/touch/mod.rs +++ b/src/platform_impl/linux/wayland/seat/touch/mod.rs @@ -10,7 +10,7 @@ use tracing::warn; use crate::dpi::LogicalPosition; use crate::event::{Touch, TouchPhase, WindowEvent}; use crate::platform_impl::wayland::state::WinitState; -use crate::platform_impl::wayland::{self, DeviceId, FingerId}; +use crate::platform_impl::wayland::{self, FingerId}; impl TouchHandler for WinitState { fn down( @@ -44,9 +44,7 @@ impl TouchHandler for WinitState { self.events_sink.push_window_event( WindowEvent::Touch(Touch { - device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( - DeviceId, - )), + device_id: None, phase: TouchPhase::Started, location: location.to_physical(scale_factor), force: None, @@ -89,9 +87,7 @@ impl TouchHandler for WinitState { self.events_sink.push_window_event( WindowEvent::Touch(Touch { - device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( - DeviceId, - )), + device_id: None, phase: TouchPhase::Ended, location: touch_point.location.to_physical(scale_factor), force: None, @@ -136,9 +132,7 @@ impl TouchHandler for WinitState { self.events_sink.push_window_event( WindowEvent::Touch(Touch { - device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( - DeviceId, - )), + device_id: None, phase: TouchPhase::Moved, location: touch_point.location.to_physical(scale_factor), force: None, @@ -170,9 +164,7 @@ impl TouchHandler for WinitState { self.events_sink.push_window_event( WindowEvent::Touch(Touch { - device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( - DeviceId, - )), + device_id: None, phase: TouchPhase::Cancelled, location, force: None, diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index 09bb02912c..b23a7b571c 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -875,7 +875,6 @@ impl EventProcessor { }; let window_id = mkwid(window); - let device_id = mkdid(util::VIRTUAL_CORE_KEYBOARD); let keycode = xev.keycode as _; @@ -939,7 +938,11 @@ impl EventProcessor { let event = key_processor.process_key_event(keycode, state, repeat); let event = Event::WindowEvent { window_id, - event: WindowEvent::KeyboardInput { device_id, event, is_synthetic: false }, + event: WindowEvent::KeyboardInput { + device_id: None, + event, + is_synthetic: false, + }, }; callback(&self.target, event); } @@ -1009,7 +1012,7 @@ impl EventProcessor { F: FnMut(&ActiveEventLoop, Event), { let window_id = mkwid(event.event as xproto::Window); - let device_id = mkdid(event.deviceid as xinput::DeviceId); + let device_id = Some(mkdid(event.deviceid as xinput::DeviceId)); // Set the timestamp. self.target.xconn.set_timestamp(event.time as xproto::Timestamp); @@ -1063,7 +1066,7 @@ impl EventProcessor { // Set the timestamp. self.target.xconn.set_timestamp(event.time as xproto::Timestamp); - let device_id = mkdid(event.deviceid as xinput::DeviceId); + let device_id = Some(mkdid(event.deviceid as xinput::DeviceId)); let window = event.event as xproto::Window; let window_id = mkwid(window); let new_cursor_pos = (event.event_x, event.event_y); @@ -1158,6 +1161,7 @@ impl EventProcessor { } if self.window_exists(window) { + let device_id = Some(device_id); let position = PhysicalPosition::new(event.event_x, event.event_y); let event = @@ -1187,7 +1191,7 @@ impl EventProcessor { let event = Event::WindowEvent { window_id: mkwid(window), event: WindowEvent::CursorLeft { - device_id: mkdid(event.deviceid as xinput::DeviceId), + device_id: Some(mkdid(event.deviceid as xinput::DeviceId)), }, }; callback(&self.target, event); @@ -1238,16 +1242,15 @@ impl EventProcessor { // The deviceid for this event is for a keyboard instead of a pointer, // so we have to do a little extra work. - let pointer_id = self + let device_id = self .devices .borrow() .get(&DeviceId(xev.deviceid as xinput::DeviceId)) - .map(|device| device.attachment) - .unwrap_or(2); + .map(|device| mkdid(device.attachment as _)); let event = Event::WindowEvent { window_id, - event: WindowEvent::CursorMoved { device_id: mkdid(pointer_id as _), position }, + event: WindowEvent::CursorMoved { device_id, position }, }; callback(&self.target, event); } @@ -1321,10 +1324,7 @@ impl EventProcessor { if is_first_touch(&mut self.first_touch, &mut self.num_touch, id, phase) { let event = Event::WindowEvent { window_id, - event: WindowEvent::CursorMoved { - device_id: mkdid(util::VIRTUAL_CORE_POINTER), - position: location.cast(), - }, + event: WindowEvent::CursorMoved { device_id: None, position: location.cast() }, }; callback(&self.target, event); } @@ -1332,7 +1332,7 @@ impl EventProcessor { let event = Event::WindowEvent { window_id, event: WindowEvent::Touch(Touch { - device_id: mkdid(xev.deviceid as xinput::DeviceId), + device_id: Some(mkdid(xev.deviceid as xinput::DeviceId)), phase, location, force: None, // TODO @@ -1352,7 +1352,7 @@ impl EventProcessor { if xev.flags & xinput2::XIPointerEmulated == 0 { let event = Event::DeviceEvent { - device_id: mkdid(xev.deviceid as xinput::DeviceId), + device_id: Some(mkdid(xev.deviceid as xinput::DeviceId)), event: DeviceEvent::Button { state, button: xev.detail as u32 }, }; callback(&self.target, event); @@ -1366,7 +1366,7 @@ impl EventProcessor { // Set the timestamp. self.target.xconn.set_timestamp(xev.time as xproto::Timestamp); - let did = mkdid(xev.deviceid as xinput::DeviceId); + let did = Some(mkdid(xev.deviceid as xinput::DeviceId)); let mask = unsafe { slice::from_raw_parts(xev.valuators.mask, xev.valuators.mask_len as usize) }; @@ -1418,7 +1418,7 @@ impl EventProcessor { // Set the timestamp. self.target.xconn.set_timestamp(xev.time as xproto::Timestamp); - let device_id = mkdid(xev.sourceid as xinput::DeviceId); + let device_id = Some(mkdid(xev.sourceid as xinput::DeviceId)); let keycode = xev.detail as u32; if keycode < KEYCODE_OFFSET as u32 { return; @@ -1692,8 +1692,6 @@ impl EventProcessor { ) where F: FnMut(&ActiveEventLoop, Event), { - let device_id = mkdid(util::VIRTUAL_CORE_KEYBOARD); - // Update modifiers state and emit key events based on which keys are currently pressed. let xcb = target.xconn.xcb_connection().get_raw_xcb_connection(); @@ -1716,7 +1714,7 @@ impl EventProcessor { let event = key_processor.process_key_event(keycode as u32, state, false); let event = Event::WindowEvent { window_id, - event: WindowEvent::KeyboardInput { device_id, event, is_synthetic: true }, + event: WindowEvent::KeyboardInput { device_id: None, event, is_synthetic: true }, }; callback(target, event); } diff --git a/src/platform_impl/linux/x11/util/input.rs b/src/platform_impl/linux/x11/util/input.rs index 367e890154..12a69bd0c7 100644 --- a/src/platform_impl/linux/x11/util/input.rs +++ b/src/platform_impl/linux/x11/util/input.rs @@ -6,7 +6,6 @@ use x11rb::protocol::xkb; use super::*; pub const VIRTUAL_CORE_POINTER: u16 = 2; -pub const VIRTUAL_CORE_KEYBOARD: u16 = 3; // A base buffer size of 1kB uses a negligible amount of RAM while preventing us from having to // re-allocate (and make another round-trip) in the *vast* majority of cases. diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index d0c77fcccc..25e83c3e41 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -12,8 +12,8 @@ use orbclient::{ use smol_str::SmolStr; use super::{ - DeviceId, KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes, - RedoxSocket, TimeSocket, WindowId, WindowProperties, + KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes, RedoxSocket, + TimeSocket, WindowId, WindowProperties, }; use crate::application::ApplicationHandler; use crate::error::{EventLoopError, ExternalError, NotSupportedError}; @@ -369,7 +369,7 @@ impl EventLoop { let window_id = RootWindowId(window_id); let event = event::WindowEvent::KeyboardInput { - device_id: event::DeviceId(DeviceId), + device_id: None, event: event::KeyEvent { logical_key, physical_key, @@ -412,29 +412,20 @@ impl EventLoop { app.window_event( window_target, RootWindowId(window_id), - event::WindowEvent::CursorMoved { - device_id: event::DeviceId(DeviceId), - position: (x, y).into(), - }, + event::WindowEvent::CursorMoved { device_id: None, position: (x, y).into() }, ); }, EventOption::MouseRelative(MouseRelativeEvent { dx, dy }) => { - app.device_event( - window_target, - event::DeviceId(DeviceId), - event::DeviceEvent::MouseMotion { delta: (dx as f64, dy as f64) }, - ); + app.device_event(window_target, None, event::DeviceEvent::MouseMotion { + delta: (dx as f64, dy as f64), + }); }, EventOption::Button(ButtonEvent { left, middle, right }) => { while let Some((button, state)) = event_state.mouse(left, middle, right) { app.window_event( window_target, RootWindowId(window_id), - event::WindowEvent::MouseInput { - device_id: event::DeviceId(DeviceId), - state, - button, - }, + event::WindowEvent::MouseInput { device_id: None, state, button }, ); } }, @@ -443,7 +434,7 @@ impl EventLoop { window_target, RootWindowId(window_id), event::WindowEvent::MouseWheel { - device_id: event::DeviceId(DeviceId), + device_id: None, delta: event::MouseScrollDelta::LineDelta(x as f32, y as f32), phase: event::TouchPhase::Moved, }, @@ -483,9 +474,9 @@ impl EventLoop { // TODO: Screen, Clipboard, Drop EventOption::Hover(HoverEvent { entered }) => { let event = if entered { - event::WindowEvent::CursorEntered { device_id: event::DeviceId(DeviceId) } + event::WindowEvent::CursorEntered { device_id: None } } else { - event::WindowEvent::CursorLeft { device_id: event::DeviceId(DeviceId) } + event::WindowEvent::CursorLeft { device_id: None } }; app.window_event(window_target, RootWindowId(window_id), event); diff --git a/src/platform_impl/web/event.rs b/src/platform_impl/web/event.rs index a1f37a348f..0b4557708a 100644 --- a/src/platform_impl/web/event.rs +++ b/src/platform_impl/web/event.rs @@ -1,18 +1,21 @@ #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] -pub enum DeviceId { - Wheel, - Keyboard, - Id(i32), -} +pub struct DeviceId(u32); impl DeviceId { - pub fn new(pointer_id: i32) -> Self { - Self::Id(pointer_id) + pub fn new(pointer_id: i32) -> Option { + if let Ok(pointer_id) = u32::try_from(pointer_id) { + Some(Self(pointer_id)) + } else if pointer_id == -1 { + None + } else { + tracing::error!("found unexpected negative `PointerEvent.pointerId`: {pointer_id}"); + None + } } #[cfg(test)] pub const fn dummy() -> Self { - Self::Id(-1) + Self(0) } } diff --git a/src/platform_impl/web/event_loop/mod.rs b/src/platform_impl/web/event_loop/mod.rs index 89721cac8d..eced29cdd9 100644 --- a/src/platform_impl/web/event_loop/mod.rs +++ b/src/platform_impl/web/event_loop/mod.rs @@ -1,4 +1,4 @@ -use super::{backend, event, window, HasMonitorPermissionFuture, MonitorPermissionFuture}; +use super::{backend, window, HasMonitorPermissionFuture, MonitorPermissionFuture}; use crate::application::ApplicationHandler; use crate::error::{EventLoopError, NotSupportedError}; use crate::event::Event; diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index cf430fc9f5..f6fedab0a0 100644 --- a/src/platform_impl/web/event_loop/runner.rs +++ b/src/platform_impl/web/event_loop/runner.rs @@ -286,7 +286,7 @@ impl Shared { } // chorded button event - let device_id = RootDeviceId(DeviceId::new(event.pointer_id())); + let device_id = DeviceId::new(event.pointer_id()).map(RootDeviceId); if let Some(button) = backend::event::mouse_button(&event) { let state = if backend::event::mouse_buttons(&event).contains(button.into()) { @@ -327,7 +327,7 @@ impl Shared { if let Some(delta) = backend::event::mouse_scroll_delta(&window, &event) { runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId::Wheel), + device_id: None, event: DeviceEvent::MouseWheel { delta }, }); } @@ -344,7 +344,7 @@ impl Shared { let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId::new(event.pointer_id())), + device_id: DeviceId::new(event.pointer_id()).map(RootDeviceId), event: DeviceEvent::Button { button: button.to_id(), state: ElementState::Pressed, @@ -363,7 +363,7 @@ impl Shared { let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId::new(event.pointer_id())), + device_id: DeviceId::new(event.pointer_id()).map(RootDeviceId), event: DeviceEvent::Button { button: button.to_id(), state: ElementState::Released, @@ -381,7 +381,7 @@ impl Shared { } runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId::Keyboard), + device_id: None, event: DeviceEvent::Key(RawKeyEvent { physical_key: backend::event::key_code(&event), state: ElementState::Pressed, @@ -399,7 +399,7 @@ impl Shared { } runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId::Keyboard), + device_id: None, event: DeviceEvent::Key(RawKeyEvent { physical_key: backend::event::key_code(&event), state: ElementState::Released, diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index a37a8a5cd7..08a6b8c8f5 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -7,7 +7,6 @@ use web_sys::Element; use super::super::monitor::MonitorPermissionFuture; use super::super::{lock, KeyEventExtra}; -use super::event::DeviceId; use super::runner::{EventWrapper, WeakShared}; use super::window::WindowId; use super::{backend, runner, EventLoopProxy}; @@ -145,13 +144,11 @@ impl ActiveEventLoop { } }); - let device_id = RootDeviceId(DeviceId::Keyboard); - runner.send_events( iter::once(Event::WindowEvent { window_id: RootWindowId(id), event: WindowEvent::KeyboardInput { - device_id, + device_id: None, event: KeyEvent { physical_key, logical_key, @@ -181,13 +178,11 @@ impl ActiveEventLoop { } }); - let device_id = RootDeviceId(DeviceId::Keyboard); - runner.send_events( iter::once(Event::WindowEvent { window_id: RootWindowId(id), event: WindowEvent::KeyboardInput { - device_id, + device_id: None, event: KeyEvent { physical_key, logical_key, @@ -222,7 +217,7 @@ impl ActiveEventLoop { let pointer = pointer_id.map(|device_id| Event::WindowEvent { window_id: RootWindowId(id), - event: WindowEvent::CursorLeft { device_id: RootDeviceId(device_id) }, + event: WindowEvent::CursorLeft { device_id: device_id.map(RootDeviceId) }, }); if focus.is_some() || pointer.is_some() { @@ -247,7 +242,7 @@ impl ActiveEventLoop { let pointer = pointer_id.map(|device_id| Event::WindowEvent { window_id: RootWindowId(id), - event: WindowEvent::CursorEntered { device_id: RootDeviceId(device_id) }, + event: WindowEvent::CursorEntered { device_id: device_id.map(RootDeviceId) }, }); if focus.is_some() || pointer.is_some() { @@ -273,7 +268,7 @@ impl ActiveEventLoop { }); runner.send_events(modifiers.into_iter().chain(events.flat_map(|position| { - let device_id = RootDeviceId(pointer_id); + let device_id = pointer_id.map(RootDeviceId); iter::once(Event::WindowEvent { window_id: RootWindowId(id), @@ -302,7 +297,7 @@ impl ActiveEventLoop { window_id: RootWindowId(id), event: WindowEvent::Touch(Touch { finger_id: RootFingerId(finger_id), - device_id: RootDeviceId(device_id), + device_id: device_id.map(RootDeviceId), phase: TouchPhase::Moved, force: Some(force), location, @@ -330,7 +325,7 @@ impl ActiveEventLoop { } }); - let device_id = RootDeviceId(device_id); + let device_id = device_id.map(RootDeviceId); let state = if buttons.contains(button.into()) { ElementState::Pressed @@ -369,7 +364,7 @@ impl ActiveEventLoop { } }); - let device_id: RootDeviceId = RootDeviceId(pointer_id); + let device_id = pointer_id.map(RootDeviceId); // A mouse down event may come in without any prior CursorMoved events, // therefore we should send a CursorMoved event to make sure that the @@ -408,7 +403,7 @@ impl ActiveEventLoop { window_id: RootWindowId(id), event: WindowEvent::Touch(Touch { finger_id: RootFingerId(finger_id), - device_id: RootDeviceId(device_id), + device_id: device_id.map(RootDeviceId), phase: TouchPhase::Started, force: Some(force), location, @@ -435,7 +430,7 @@ impl ActiveEventLoop { } }); - let device_id: RootDeviceId = RootDeviceId(pointer_id); + let device_id = pointer_id.map(RootDeviceId); // A mouse up event may come in without any prior CursorMoved events, // therefore we should send a CursorMoved event to make sure that the @@ -476,7 +471,7 @@ impl ActiveEventLoop { window_id: RootWindowId(id), event: WindowEvent::Touch(Touch { finger_id: RootFingerId(finger_id), - device_id: RootDeviceId(device_id), + device_id: device_id.map(RootDeviceId), phase: TouchPhase::Ended, force: Some(force), location, @@ -503,7 +498,7 @@ impl ActiveEventLoop { Event::WindowEvent { window_id: RootWindowId(id), event: WindowEvent::MouseWheel { - device_id: RootDeviceId(DeviceId::Keyboard), + device_id: None, delta, phase: TouchPhase::Moved, }, @@ -517,7 +512,7 @@ impl ActiveEventLoop { window_id: RootWindowId(id), event: WindowEvent::Touch(Touch { finger_id: RootFingerId(finger_id), - device_id: RootDeviceId(device_id), + device_id: device_id.map(RootDeviceId), phase: TouchPhase::Cancelled, force: Some(force), location, diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index e7d95d84dc..23037d36d5 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -330,22 +330,23 @@ impl Canvas { pub fn on_cursor_leave(&self, handler: F) where - F: 'static + FnMut(ModifiersState, Option), + F: 'static + FnMut(ModifiersState, Option>), { self.handlers.borrow_mut().pointer_handler.on_cursor_leave(&self.common, handler) } pub fn on_cursor_enter(&self, handler: F) where - F: 'static + FnMut(ModifiersState, Option), + F: 'static + FnMut(ModifiersState, Option>), { self.handlers.borrow_mut().pointer_handler.on_cursor_enter(&self.common, handler) } pub fn on_mouse_release(&self, mouse_handler: M, touch_handler: T) where - M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition, MouseButton), - T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition, Force), + M: 'static + FnMut(ModifiersState, Option, PhysicalPosition, MouseButton), + T: 'static + + FnMut(ModifiersState, Option, FingerId, PhysicalPosition, Force), { self.handlers.borrow_mut().pointer_handler.on_mouse_release( &self.common, @@ -356,8 +357,9 @@ impl Canvas { pub fn on_mouse_press(&self, mouse_handler: M, touch_handler: T) where - M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition, MouseButton), - T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition, Force), + M: 'static + FnMut(ModifiersState, Option, PhysicalPosition, MouseButton), + T: 'static + + FnMut(ModifiersState, Option, FingerId, PhysicalPosition, Force), { self.handlers.borrow_mut().pointer_handler.on_mouse_press( &self.common, @@ -370,16 +372,22 @@ impl Canvas { pub fn on_cursor_move(&self, mouse_handler: M, touch_handler: T, button_handler: B) where M: 'static - + FnMut(ModifiersState, DeviceId, &mut dyn Iterator>), + + FnMut(ModifiersState, Option, &mut dyn Iterator>), T: 'static + FnMut( ModifiersState, - DeviceId, + Option, FingerId, &mut dyn Iterator, Force)>, ), B: 'static - + FnMut(ModifiersState, DeviceId, PhysicalPosition, ButtonsState, MouseButton), + + FnMut( + ModifiersState, + Option, + PhysicalPosition, + ButtonsState, + MouseButton, + ), { self.handlers.borrow_mut().pointer_handler.on_cursor_move( &self.common, @@ -392,7 +400,7 @@ impl Canvas { pub fn on_touch_cancel(&self, handler: F) where - F: 'static + FnMut(DeviceId, FingerId, PhysicalPosition, Force), + F: 'static + FnMut(Option, FingerId, PhysicalPosition, Force), { self.handlers.borrow_mut().pointer_handler.on_touch_cancel(&self.common, handler) } diff --git a/src/platform_impl/web/web_sys/pointer.rs b/src/platform_impl/web/web_sys/pointer.rs index 6679cbd2b0..11a37a3e7a 100644 --- a/src/platform_impl/web/web_sys/pointer.rs +++ b/src/platform_impl/web/web_sys/pointer.rs @@ -36,7 +36,7 @@ impl PointerHandler { pub fn on_cursor_leave(&mut self, canvas_common: &Common, mut handler: F) where - F: 'static + FnMut(ModifiersState, Option), + F: 'static + FnMut(ModifiersState, Option>), { self.on_cursor_leave = Some(canvas_common.add_event("pointerout", move |event: PointerEvent| { @@ -54,7 +54,7 @@ impl PointerHandler { pub fn on_cursor_enter(&mut self, canvas_common: &Common, mut handler: F) where - F: 'static + FnMut(ModifiersState, Option), + F: 'static + FnMut(ModifiersState, Option>), { self.on_cursor_enter = Some(canvas_common.add_event("pointerover", move |event: PointerEvent| { @@ -76,8 +76,9 @@ impl PointerHandler { mut mouse_handler: M, mut touch_handler: T, ) where - M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition, MouseButton), - T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition, Force), + M: 'static + FnMut(ModifiersState, Option, PhysicalPosition, MouseButton), + T: 'static + + FnMut(ModifiersState, Option, FingerId, PhysicalPosition, Force), { let window = canvas_common.window.clone(); self.on_pointer_release = @@ -112,8 +113,9 @@ impl PointerHandler { mut touch_handler: T, prevent_default: Rc>, ) where - M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition, MouseButton), - T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition, Force), + M: 'static + FnMut(ModifiersState, Option, PhysicalPosition, MouseButton), + T: 'static + + FnMut(ModifiersState, Option, FingerId, PhysicalPosition, Force), { let window = canvas_common.window.clone(); let canvas = canvas_common.raw().clone(); @@ -172,16 +174,22 @@ impl PointerHandler { prevent_default: Rc>, ) where M: 'static - + FnMut(ModifiersState, DeviceId, &mut dyn Iterator>), + + FnMut(ModifiersState, Option, &mut dyn Iterator>), T: 'static + FnMut( ModifiersState, - DeviceId, + Option, FingerId, &mut dyn Iterator, Force)>, ), B: 'static - + FnMut(ModifiersState, DeviceId, PhysicalPosition, ButtonsState, MouseButton), + + FnMut( + ModifiersState, + Option, + PhysicalPosition, + ButtonsState, + MouseButton, + ), { let window = canvas_common.window.clone(); let canvas = canvas_common.raw().clone(); @@ -237,7 +245,7 @@ impl PointerHandler { pub fn on_touch_cancel(&mut self, canvas_common: &Common, mut handler: F) where - F: 'static + FnMut(DeviceId, FingerId, PhysicalPosition, Force), + F: 'static + FnMut(Option, FingerId, PhysicalPosition, Force), { let window = canvas_common.window.clone(); self.on_touch_cancel = diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 5a4cadc464..5f1a954b3d 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -81,7 +81,7 @@ use crate::platform_impl::platform::window_state::{ CursorFlags, ImeState, WindowFlags, WindowState, }; use crate::platform_impl::platform::{ - raw_input, util, wrap_device_id, FingerId, Fullscreen, WindowId, DEVICE_ID, + raw_input, util, wrap_device_id, FingerId, Fullscreen, WindowId, }; use crate::platform_impl::Window; use crate::utils::Lazy; @@ -1045,7 +1045,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: KeyboardInput { - device_id: DEVICE_ID, + device_id: None, event: event.event, is_synthetic: event.is_synthetic, }, @@ -1539,7 +1539,7 @@ unsafe fn public_window_callback_inner( drop(w); userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: CursorEntered { device_id: DEVICE_ID }, + event: CursorEntered { device_id: None }, }); // Calling TrackMouseEvent in order to receive mouse leave events. @@ -1560,7 +1560,7 @@ unsafe fn public_window_callback_inner( drop(w); userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: CursorLeft { device_id: DEVICE_ID }, + event: CursorLeft { device_id: None }, }); }, PointerMoveKind::None => drop(w), @@ -1579,7 +1579,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: CursorMoved { device_id: DEVICE_ID, position }, + event: CursorMoved { device_id: None, position }, }); } @@ -1595,7 +1595,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: CursorLeft { device_id: DEVICE_ID }, + event: CursorLeft { device_id: None }, }); result = ProcResult::Value(0); @@ -1612,7 +1612,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: WindowEvent::MouseWheel { - device_id: DEVICE_ID, + device_id: None, delta: LineDelta(0.0, value), phase: TouchPhase::Moved, }, @@ -1632,7 +1632,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: WindowEvent::MouseWheel { - device_id: DEVICE_ID, + device_id: None, delta: LineDelta(value, 0.0), phase: TouchPhase::Moved, }, @@ -1666,7 +1666,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: MouseInput { device_id: DEVICE_ID, state: Pressed, button: Left }, + event: MouseInput { device_id: None, state: Pressed, button: Left }, }); result = ProcResult::Value(0); }, @@ -1682,7 +1682,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: MouseInput { device_id: DEVICE_ID, state: Released, button: Left }, + event: MouseInput { device_id: None, state: Released, button: Left }, }); result = ProcResult::Value(0); }, @@ -1698,7 +1698,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: MouseInput { device_id: DEVICE_ID, state: Pressed, button: Right }, + event: MouseInput { device_id: None, state: Pressed, button: Right }, }); result = ProcResult::Value(0); }, @@ -1714,7 +1714,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: MouseInput { device_id: DEVICE_ID, state: Released, button: Right }, + event: MouseInput { device_id: None, state: Released, button: Right }, }); result = ProcResult::Value(0); }, @@ -1730,7 +1730,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: MouseInput { device_id: DEVICE_ID, state: Pressed, button: Middle }, + event: MouseInput { device_id: None, state: Pressed, button: Middle }, }); result = ProcResult::Value(0); }, @@ -1746,7 +1746,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), - event: MouseInput { device_id: DEVICE_ID, state: Released, button: Middle }, + event: MouseInput { device_id: None, state: Released, button: Middle }, }); result = ProcResult::Value(0); }, @@ -1764,7 +1764,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: MouseInput { - device_id: DEVICE_ID, + device_id: None, state: Pressed, button: match xbutton { 1 => Back, @@ -1789,7 +1789,7 @@ unsafe fn public_window_callback_inner( userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: MouseInput { - device_id: DEVICE_ID, + device_id: None, state: Released, button: match xbutton { 1 => Back, @@ -1853,7 +1853,7 @@ unsafe fn public_window_callback_inner( id: input.dwID, primary: util::has_flag(input.dwFlags, TOUCHEVENTF_PRIMARY), }), - device_id: DEVICE_ID, + device_id: None, }), }); } @@ -2005,7 +2005,7 @@ unsafe fn public_window_callback_inner( POINTER_FLAG_PRIMARY, ), }), - device_id: DEVICE_ID, + device_id: None, }), }); } @@ -2433,7 +2433,7 @@ unsafe fn handle_raw_input(userdata: &ThreadMsgTargetData, data: RAWINPUT) { use crate::event::ElementState::{Pressed, Released}; use crate::event::MouseScrollDelta::LineDelta; - let device_id = wrap_device_id(data.header.hDevice as _); + let device_id = Some(wrap_device_id(data.header.hDevice as _)); if data.header.dwType == RIM_TYPEMOUSE { let mouse = unsafe { data.data.mouse }; diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 8fb95c58c6..8968a6e38e 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -98,9 +98,6 @@ impl FingerId { } } -// Constant device ID, to be removed when this backend is updated to report real device IDs. -const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId(0)); - fn wrap_device_id(id: u32) -> RootDeviceId { RootDeviceId(DeviceId(id)) }