From 859a6f109c485378da4bcefd922a62fecab56c40 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sun, 4 Feb 2024 12:01:40 -0800 Subject: [PATCH] feat: Move DeviceId to winit-core This requires a similar restructuring as with WindowId. Signed-off-by: John Nunley --- winit-core/src/event.rs | 36 +++++++++++++++++++ winit-core/src/lib.rs | 1 + winit/src/event.rs | 27 ++------------ winit/src/platform/windows.rs | 2 +- winit/src/platform_impl/android/mod.rs | 13 ++----- winit/src/platform_impl/ios/mod.rs | 17 ++------- winit/src/platform_impl/linux/mod.rs | 17 --------- .../linux/wayland/event_loop/mod.rs | 2 +- .../linux/wayland/event_loop/sink.rs | 7 ++-- winit/src/platform_impl/linux/wayland/mod.rs | 10 ------ .../linux/wayland/seat/keyboard/mod.rs | 4 +-- .../linux/wayland/seat/pointer/mod.rs | 5 +-- .../wayland/seat/pointer/relative_pointer.rs | 2 +- .../linux/wayland/seat/touch/mod.rs | 19 ++++------ .../linux/x11/event_processor.rs | 14 ++++---- winit/src/platform_impl/linux/x11/mod.rs | 12 +------ winit/src/platform_impl/macos/mod.rs | 13 ++----- winit/src/platform_impl/orbital/event_loop.rs | 14 ++++---- winit/src/platform_impl/orbital/mod.rs | 9 ----- winit/src/platform_impl/web/device.rs | 8 ----- winit/src/platform_impl/web/event_loop/mod.rs | 2 +- .../platform_impl/web/event_loop/runner.rs | 15 ++++---- .../web/event_loop/window_target.rs | 29 ++++++++------- winit/src/platform_impl/web/mod.rs | 2 -- winit/src/platform_impl/windows/mod.rs | 26 +++++--------- 25 files changed, 107 insertions(+), 199 deletions(-) create mode 100644 winit-core/src/event.rs delete mode 100644 winit/src/platform_impl/web/device.rs diff --git a/winit-core/src/event.rs b/winit-core/src/event.rs new file mode 100644 index 000000000..05f9f4260 --- /dev/null +++ b/winit-core/src/event.rs @@ -0,0 +1,36 @@ +//! Incoming notifications from the GUI system. + +/// Identifier of an input device. +/// +/// Whenever you receive an event arising from a particular input device, this event contains a `DeviceId` which +/// identifies its origin. Note that devices may be virtual (representing an on-screen cursor and keyboard focus) or +/// physical. Virtual devices typically aggregate inputs from multiple physical devices. +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DeviceId(u64); + +impl DeviceId { + /// Returns a dummy id, useful for unit testing. + /// + /// # Safety + /// + /// The only guarantee made about the return value of this function is that + /// it will always be equal to itself and to future values returned by this function. + /// No other guarantees are made. This may be equal to a real `DeviceId`. + /// + /// **Passing this into a winit function will result in undefined behavior.** + pub const unsafe fn dummy() -> Self { + DeviceId(0) + } +} + +impl From for DeviceId { + fn from(value: u64) -> Self { + Self(value) + } +} + +impl From for u64 { + fn from(value: DeviceId) -> Self { + value.0 + } +} diff --git a/winit-core/src/lib.rs b/winit-core/src/lib.rs index b5fb0e3ac..837054dcf 100644 --- a/winit-core/src/lib.rs +++ b/winit-core/src/lib.rs @@ -12,5 +12,6 @@ compile_error! { "no-std and no-alloc usage are not yet supported" } pub mod dpi; pub mod error; +pub mod event; pub mod keyboard; pub mod window; diff --git a/winit/src/event.rs b/winit/src/event.rs index 798721c0f..818202ee8 100644 --- a/winit/src/event.rs +++ b/winit/src/event.rs @@ -54,6 +54,9 @@ use crate::{ window::{ActivationToken, Theme, WindowId}, }; +#[doc(inline)] +pub use winit_core::event::DeviceId; + /// Describes a generic event. /// /// See the module-level docs for more information on the event loop manages each event. @@ -599,30 +602,6 @@ pub enum WindowEvent { RedrawRequested, } -/// Identifier of an input device. -/// -/// Whenever you receive an event arising from a particular input device, this event contains a `DeviceId` which -/// identifies its origin. Note that devices may be virtual (representing an on-screen cursor and keyboard focus) or -/// physical. Virtual devices typically aggregate inputs from multiple physical devices. -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId(pub(crate) platform_impl::DeviceId); - -impl DeviceId { - /// Returns a dummy id, useful for unit testing. - /// - /// # Safety - /// - /// The only guarantee made about the return value of this function is that - /// it will always be equal to itself and to future values returned by this function. - /// No other guarantees are made. This may be equal to a real `DeviceId`. - /// - /// **Passing this into a winit function will result in undefined behavior.** - pub const unsafe fn dummy() -> Self { - #[allow(unused_unsafe)] - DeviceId(unsafe { platform_impl::DeviceId::dummy() }) - } -} - /// Represents raw hardware events that are not associated with any particular window. /// /// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera or first-person diff --git a/winit/src/platform/windows.rs b/winit/src/platform/windows.rs index cbf9699ca..a1969d551 100644 --- a/winit/src/platform/windows.rs +++ b/winit/src/platform/windows.rs @@ -504,7 +504,7 @@ pub trait DeviceIdExtWindows { impl DeviceIdExtWindows for DeviceId { #[inline] fn persistent_identifier(&self) -> Option { - self.0.persistent_identifier() + crate::platform_impl::persistent_identifier(*self) } } diff --git a/winit/src/platform_impl/android/mod.rs b/winit/src/platform_impl/android/mod.rs index 1ff195328..4d270ab30 100644 --- a/winit/src/platform_impl/android/mod.rs +++ b/winit/src/platform_impl/android/mod.rs @@ -385,7 +385,7 @@ impl EventLoop { match event { InputEvent::MotionEvent(motion_event) => { let window_id = WindowId::from(WINDOW_ID); - let device_id = event::DeviceId(DeviceId(motion_event.device_id())); + let device_id = event::DeviceId::from(motion_event.device_id() as u64); let phase = match motion_event.action() { MotionAction::Down | MotionAction::PointerDown => { @@ -457,7 +457,7 @@ impl EventLoop { let event = event::Event::WindowEvent { window_id: WindowId::from(WINDOW_ID), event: event::WindowEvent::KeyboardInput { - device_id: event::DeviceId(DeviceId(key.device_id())), + device_id: event::DeviceId::from(key.device_id() as u64), event: event::KeyEvent { state, physical_key: keycodes::to_physical_key(keycode), @@ -744,15 +744,6 @@ impl OwnedDisplayHandle { } } -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct DeviceId(i32); - -impl DeviceId { - pub const fn dummy() -> Self { - DeviceId(0) - } -} - #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct PlatformSpecificWindowBuilderAttributes; diff --git a/winit/src/platform_impl/ios/mod.rs b/winit/src/platform_impl/ios/mod.rs index 854df7c49..2991ac6f1 100644 --- a/winit/src/platform_impl/ios/mod.rs +++ b/winit/src/platform_impl/ios/mod.rs @@ -68,7 +68,7 @@ mod window; use std::fmt; -use crate::event::DeviceId as RootDeviceId; +use crate::event::DeviceId; pub(crate) use self::{ event_loop::{ @@ -83,20 +83,7 @@ pub(crate) use crate::cursor::NoCustomCursor as PlatformCustomCursorBuilder; pub(crate) use crate::icon::NoIcon as PlatformIcon; pub(crate) use crate::platform_impl::Fullscreen; -/// There is no way to detect which device that performed a certain event in -/// UIKit (i.e. you can't differentiate between different external keyboards, -/// or wether it was the main touchscreen, assistive technologies, or some -/// other pointer device that caused a touch event). -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId; - -impl DeviceId { - pub const unsafe fn dummy() -> Self { - DeviceId - } -} - -pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId); +pub(crate) const DEVICE_ID: DeviceId = unsafe { DeviceId::dummy() }; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct KeyEventExtra {} diff --git a/winit/src/platform_impl/linux/mod.rs b/winit/src/platform_impl/linux/mod.rs index 4a3eff6ac..c54ccae55 100644 --- a/winit/src/platform_impl/linux/mod.rs +++ b/winit/src/platform_impl/linux/mod.rs @@ -142,23 +142,6 @@ pub(crate) enum Window { Wayland(wayland::Window), } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum DeviceId { - #[cfg(x11_platform)] - X(x11::DeviceId), - #[cfg(wayland_platform)] - Wayland(wayland::DeviceId), -} - -impl DeviceId { - pub const unsafe fn dummy() -> Self { - #[cfg(wayland_platform)] - return DeviceId::Wayland(unsafe { wayland::DeviceId::dummy() }); - #[cfg(all(not(wayland_platform), x11_platform))] - return DeviceId::X(unsafe { x11::DeviceId::dummy() }); - } -} - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub enum MonitorHandle { #[cfg(x11_platform)] diff --git a/winit/src/platform_impl/linux/wayland/event_loop/mod.rs b/winit/src/platform_impl/linux/wayland/event_loop/mod.rs index 69833e63f..fff7f7a72 100644 --- a/winit/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/winit/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -34,7 +34,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/winit/src/platform_impl/linux/wayland/event_loop/sink.rs b/winit/src/platform_impl/linux/wayland/event_loop/sink.rs index b6b4db249..14eb475d2 100644 --- a/winit/src/platform_impl/linux/wayland/event_loop/sink.rs +++ b/winit/src/platform_impl/linux/wayland/event_loop/sink.rs @@ -2,12 +2,9 @@ use std::vec::Drain; -use crate::event::{DeviceEvent, DeviceId as RootDeviceId, Event, WindowEvent}; -use crate::platform_impl::platform::DeviceId as PlatformDeviceId; +use crate::event::{DeviceEvent, DeviceId, Event, WindowEvent}; use crate::window::WindowId; -use super::DeviceId; - /// An event loop's sink to deliver events from the Wayland event callbacks /// to the winit's user. #[derive(Default)] @@ -31,7 +28,7 @@ impl EventSink { 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)), + device_id, }); } diff --git a/winit/src/platform_impl/linux/wayland/mod.rs b/winit/src/platform_impl/linux/wayland/mod.rs index ebf3b108e..7dcc33b0b 100644 --- a/winit/src/platform_impl/linux/wayland/mod.rs +++ b/winit/src/platform_impl/linux/wayland/mod.rs @@ -65,16 +65,6 @@ impl From for OsError { } } -/// Dummy device id, since Wayland doesn't have device events. -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId; - -impl DeviceId { - pub const unsafe fn dummy() -> Self { - DeviceId - } -} - /// Get the WindowId out of the surface. #[inline] fn make_wid(surface: &WlSurface) -> WindowId { diff --git a/winit/src/platform_impl/linux/wayland/seat/keyboard/mod.rs b/winit/src/platform_impl/linux/wayland/seat/keyboard/mod.rs index cf1ef2185..6bd8fccbc 100644 --- a/winit/src/platform_impl/linux/wayland/seat/keyboard/mod.rs +++ b/winit/src/platform_impl/linux/wayland/seat/keyboard/mod.rs @@ -21,7 +21,7 @@ use crate::platform_impl::common::xkb_state::KbdState; use crate::platform_impl::wayland::event_loop::sink::EventSink; use crate::platform_impl::wayland::seat::WinitSeatState; 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( @@ -384,7 +384,7 @@ fn key_input( let keyboard_state = seat_state.keyboard_state.as_mut().unwrap(); - let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId)); + let device_id = crate::event::DeviceId::from(0); let event = keyboard_state .xkb_state .process_key_event(keycode, state, repeat); diff --git a/winit/src/platform_impl/linux/wayland/seat/pointer/mod.rs b/winit/src/platform_impl/linux/wayland/seat/pointer/mod.rs index 3b62bae10..69d933d0e 100644 --- a/winit/src/platform_impl/linux/wayland/seat/pointer/mod.rs +++ b/winit/src/platform_impl/linux/wayland/seat/pointer/mod.rs @@ -22,12 +22,13 @@ use sctk::globals::GlobalData; use sctk::seat::pointer::{PointerData, PointerDataExt}; use sctk::seat::pointer::{PointerEvent, PointerEventKind, PointerHandler}; use sctk::seat::SeatState; +use winit_core::event::DeviceId; 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; @@ -42,7 +43,7 @@ impl PointerHandler for WinitState { let seat = pointer.winit_data().seat(); let seat_state = self.seats.get(&seat.id()).unwrap(); - let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId)); + let device_id = DeviceId::from(0); for event in events { let surface = &event.surface; diff --git a/winit/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs b/winit/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs index 23a6e2199..4ca661747 100644 --- a/winit/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs +++ b/winit/src/platform_impl/linux/wayland/seat/pointer/relative_pointer.rs @@ -70,7 +70,7 @@ impl Dispatch for RelativePointerS DeviceEvent::MouseMotion { delta: (dx_unaccel, dy_unaccel), }, - super::DeviceId, + crate::event::DeviceId::from(0), ); } } diff --git a/winit/src/platform_impl/linux/wayland/seat/touch/mod.rs b/winit/src/platform_impl/linux/wayland/seat/touch/mod.rs index 7232b2279..e55753b8f 100644 --- a/winit/src/platform_impl/linux/wayland/seat/touch/mod.rs +++ b/winit/src/platform_impl/linux/wayland/seat/touch/mod.rs @@ -6,12 +6,13 @@ use sctk::reexports::client::protocol::wl_touch::WlTouch; use sctk::reexports::client::{Connection, Proxy, QueueHandle}; use sctk::seat::touch::{TouchData, TouchHandler}; +use winit_core::event::DeviceId; use crate::dpi::LogicalPosition; use crate::event::{Touch, TouchPhase, WindowEvent}; use crate::platform_impl::wayland::state::WinitState; -use crate::platform_impl::wayland::{self, DeviceId}; +use crate::platform_impl::wayland; impl TouchHandler for WinitState { fn down( @@ -42,9 +43,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: DeviceId::from(0), phase: TouchPhase::Started, location: location.to_physical(scale_factor), force: None, @@ -79,9 +78,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: DeviceId::from(0), phase: TouchPhase::Ended, location: touch_point.location.to_physical(scale_factor), force: None, @@ -118,9 +115,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: DeviceId::from(0), phase: TouchPhase::Moved, location: touch_point.location.to_physical(scale_factor), force: None, @@ -144,9 +139,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: DeviceId::from(0), phase: TouchPhase::Cancelled, location, force: None, diff --git a/winit/src/platform_impl/linux/x11/event_processor.rs b/winit/src/platform_impl/linux/x11/event_processor.rs index e6304cbd4..a4288a5f5 100644 --- a/winit/src/platform_impl/linux/x11/event_processor.rs +++ b/winit/src/platform_impl/linux/x11/event_processor.rs @@ -17,13 +17,13 @@ use x11rb::{ }; use super::{ - atoms::*, ffi, get_xtarget, mkdid, mkwid, util, CookieResultExt, Device, DeviceId, DeviceInfo, + atoms::*, ffi, get_xtarget, mkdid, mkwid, util, CookieResultExt, Device, DeviceInfo, Dnd, DndState, GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow, }; use crate::{ dpi::{PhysicalPosition, PhysicalSize}, - event::{DeviceEvent, ElementState, Event, Ime, RawKeyEvent, TouchPhase, WindowEvent}, + event::{DeviceEvent, ElementState, Event, Ime, RawKeyEvent, TouchPhase, WindowEvent, DeviceId}, event_loop::EventLoopWindowTarget as RootELW, keyboard::ModifiersState, platform_impl::platform::common::{keymap, xkb_state::KbdState}, @@ -67,7 +67,7 @@ impl EventProcessor { let mut devices = self.devices.borrow_mut(); if let Some(info) = DeviceInfo::get(&wt.xconn, device as _) { for info in info.iter() { - devices.insert(DeviceId(info.deviceid as _), Device::new(info)); + devices.insert(mkdid(info.deviceid as _), Device::new(info)); } } } @@ -859,7 +859,7 @@ impl EventProcessor { }; let mut devices = self.devices.borrow_mut(); let physical_device = match devices - .get_mut(&DeviceId(xev.sourceid as xinput::DeviceId)) + .get_mut(&mkdid(xev.sourceid as xinput::DeviceId)) { Some(device) => device, None => return, @@ -933,7 +933,7 @@ impl EventProcessor { // the virtual device. || device_info.attachment == xev.sourceid { - let device_id = DeviceId(device_info.deviceid as _); + let device_id = mkdid(device_info.deviceid as _); if let Some(device) = devices.get_mut(&device_id) { device.reset_scroll_position(device_info); } @@ -1015,7 +1015,7 @@ impl EventProcessor { let pointer_id = self .devices .borrow() - .get(&DeviceId(xev.deviceid as xinput::DeviceId)) + .get(&mkdid(xev.deviceid as xinput::DeviceId)) .map(|device| device.attachment) .unwrap_or(2); @@ -1252,7 +1252,7 @@ impl EventProcessor { event: DeviceEvent::Removed, }); let mut devices = self.devices.borrow_mut(); - devices.remove(&DeviceId(info.deviceid as xinput::DeviceId)); + devices.remove(&mkdid(info.deviceid as xinput::DeviceId)); } } } diff --git a/winit/src/platform_impl/linux/x11/mod.rs b/winit/src/platform_impl/linux/x11/mod.rs index 0dabaf217..76a2a6b7a 100644 --- a/winit/src/platform_impl/linux/x11/mod.rs +++ b/winit/src/platform_impl/linux/x11/mod.rs @@ -812,16 +812,6 @@ impl<'a> Deref for DeviceInfo<'a> { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId(xinput::DeviceId); - -impl DeviceId { - #[allow(unused)] - pub const unsafe fn dummy() -> Self { - DeviceId(0) - } -} - pub(crate) struct Window(Arc); impl Deref for Window { @@ -1043,7 +1033,7 @@ fn mkwid(w: xproto::Window) -> crate::window::WindowId { WindowId::from(w as u64) } fn mkdid(w: xinput::DeviceId) -> crate::event::DeviceId { - crate::event::DeviceId(crate::platform_impl::DeviceId::X(DeviceId(w))) + crate::event::DeviceId::from(w as u64) } #[derive(Debug)] diff --git a/winit/src/platform_impl/macos/mod.rs b/winit/src/platform_impl/macos/mod.rs index 2d3e625bb..7b84979cb 100644 --- a/winit/src/platform_impl/macos/mod.rs +++ b/winit/src/platform_impl/macos/mod.rs @@ -25,7 +25,7 @@ pub(crate) use self::{ monitor::{MonitorHandle, VideoModeHandle}, window_delegate::PlatformSpecificWindowBuilderAttributes, }; -use crate::event::DeviceId as RootDeviceId; +use crate::event::DeviceId; pub(crate) use self::cursor::CustomCursor as PlatformCustomCursor; pub(crate) use self::window::Window; @@ -33,17 +33,8 @@ pub(crate) use crate::cursor::OnlyCursorImageBuilder as PlatformCustomCursorBuil pub(crate) use crate::icon::NoIcon as PlatformIcon; pub(crate) use crate::platform_impl::Fullscreen; -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId; - -impl DeviceId { - pub const unsafe fn dummy() -> Self { - 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); +pub(crate) const DEVICE_ID: DeviceId = unsafe { DeviceId::dummy() }; #[derive(Debug)] pub enum OsError { diff --git a/winit/src/platform_impl/orbital/event_loop.rs b/winit/src/platform_impl/orbital/event_loop.rs index 97ecf09a9..094b978b1 100644 --- a/winit/src/platform_impl/orbital/event_loop.rs +++ b/winit/src/platform_impl/orbital/event_loop.rs @@ -25,7 +25,7 @@ use crate::{ }; use super::{ - DeviceId, KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes, + KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes, RedoxSocket, TimeSocket, WindowProperties, }; @@ -344,7 +344,7 @@ impl EventLoop { event_handler(event::Event::WindowEvent { window_id, event: event::WindowEvent::KeyboardInput { - device_id: event::DeviceId(DeviceId), + device_id: event::DeviceId::from(0), event: event::KeyEvent { logical_key: Key::Unidentified(NativeKey::Unidentified), physical_key, @@ -382,7 +382,7 @@ impl EventLoop { event_handler(event::Event::WindowEvent { window_id, event: event::WindowEvent::CursorMoved { - device_id: event::DeviceId(DeviceId), + device_id: event::DeviceId::from(0), position: (x, y).into(), }, }); @@ -396,7 +396,7 @@ impl EventLoop { event_handler(event::Event::WindowEvent { window_id, event: event::WindowEvent::MouseInput { - device_id: event::DeviceId(DeviceId), + device_id: event::DeviceId::from(0), state, button, }, @@ -407,7 +407,7 @@ impl EventLoop { event_handler(event::Event::WindowEvent { window_id, event: event::WindowEvent::MouseWheel { - device_id: event::DeviceId(DeviceId), + device_id: event::DeviceId::from(0), delta: event::MouseScrollDelta::LineDelta(x as f32, y as f32), phase: event::TouchPhase::Moved, }, @@ -446,14 +446,14 @@ impl EventLoop { event_handler(event::Event::WindowEvent { window_id, event: event::WindowEvent::CursorEntered { - device_id: event::DeviceId(DeviceId), + device_id: event::DeviceId::from(0), }, }); } else { event_handler(event::Event::WindowEvent { window_id, event: event::WindowEvent::CursorLeft { - device_id: event::DeviceId(DeviceId), + device_id: event::DeviceId::from(0), }, }); } diff --git a/winit/src/platform_impl/orbital/mod.rs b/winit/src/platform_impl/orbital/mod.rs index 0f7a97198..7cbf4b4f8 100644 --- a/winit/src/platform_impl/orbital/mod.rs +++ b/winit/src/platform_impl/orbital/mod.rs @@ -95,15 +95,6 @@ impl TimeSocket { #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub(crate) struct PlatformSpecificEventLoopAttributes {} -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct DeviceId; - -impl DeviceId { - pub const fn dummy() -> Self { - DeviceId - } -} - #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct PlatformSpecificWindowBuilderAttributes; diff --git a/winit/src/platform_impl/web/device.rs b/winit/src/platform_impl/web/device.rs deleted file mode 100644 index 383bf3a1b..000000000 --- a/winit/src/platform_impl/web/device.rs +++ /dev/null @@ -1,8 +0,0 @@ -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId(pub i32); - -impl DeviceId { - pub const unsafe fn dummy() -> Self { - Self(0) - } -} diff --git a/winit/src/platform_impl/web/event_loop/mod.rs b/winit/src/platform_impl/web/event_loop/mod.rs index 6b97d5196..61313b52b 100644 --- a/winit/src/platform_impl/web/event_loop/mod.rs +++ b/winit/src/platform_impl/web/event_loop/mod.rs @@ -5,7 +5,7 @@ use crate::error::EventLoopError; use crate::event::Event; use crate::event_loop::EventLoopWindowTarget as RootEventLoopWindowTarget; -use super::{backend, device}; +use super::backend; mod proxy; pub(crate) mod runner; diff --git a/winit/src/platform_impl/web/event_loop/runner.rs b/winit/src/platform_impl/web/event_loop/runner.rs index f7932ff76..c22016838 100644 --- a/winit/src/platform_impl/web/event_loop/runner.rs +++ b/winit/src/platform_impl/web/event_loop/runner.rs @@ -1,9 +1,8 @@ use super::super::main_thread::MainThreadMarker; -use super::super::DeviceId; use super::{backend, state::State}; use crate::dpi::PhysicalSize; use crate::event::{ - DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, RawKeyEvent, StartCause, + DeviceEvent, DeviceId, ElementState, Event, RawKeyEvent, StartCause, WindowEvent, }; use crate::event_loop::{ControlFlow, DeviceEvents}; @@ -265,7 +264,7 @@ impl Shared { } // chorded button event - let device_id = RootDeviceId(DeviceId(event.pointer_id())); + let device_id = DeviceId::from(event.pointer_id() as u64); if let Some(button) = backend::event::mouse_button(&event) { debug_assert_eq!( @@ -337,7 +336,7 @@ impl Shared { if let Some(delta) = backend::event::mouse_scroll_delta(&window, &event) { runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId(0)), + device_id: DeviceId::from(0u64), event: DeviceEvent::MouseWheel { delta }, }); } @@ -358,7 +357,7 @@ impl Shared { let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId(event.pointer_id())), + device_id: DeviceId::from(event.pointer_id() as u64), event: DeviceEvent::Button { button: button.to_id(), state: ElementState::Pressed, @@ -381,7 +380,7 @@ impl Shared { let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId(event.pointer_id())), + device_id: DeviceId::from(event.pointer_id() as u64), event: DeviceEvent::Button { button: button.to_id(), state: ElementState::Released, @@ -399,7 +398,7 @@ impl Shared { } runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(unsafe { DeviceId::dummy() }), + device_id: unsafe { DeviceId::dummy() }, event: DeviceEvent::Key(RawKeyEvent { physical_key: backend::event::key_code(&event), state: ElementState::Pressed, @@ -417,7 +416,7 @@ impl Shared { } runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(unsafe { DeviceId::dummy() }), + device_id: unsafe { DeviceId::dummy() }, event: DeviceEvent::Key(RawKeyEvent { physical_key: backend::event::key_code(&event), state: ElementState::Released, diff --git a/winit/src/platform_impl/web/event_loop/window_target.rs b/winit/src/platform_impl/web/event_loop/window_target.rs index 6f063552a..24f3c3e35 100644 --- a/winit/src/platform_impl/web/event_loop/window_target.rs +++ b/winit/src/platform_impl/web/event_loop/window_target.rs @@ -10,11 +10,10 @@ use super::runner::{EventWrapper, Execution}; use super::{ super::{monitor::MonitorHandle, KeyEventExtra}, backend, - device::DeviceId, runner, }; use crate::event::{ - DeviceId as RootDeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent, + DeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent, }; use crate::event_loop::{ControlFlow, DeviceEvents}; use crate::keyboard::ModifiersState; @@ -138,7 +137,7 @@ impl EventLoopWindowTarget { } }); - let device_id = RootDeviceId(unsafe { DeviceId::dummy() }); + let device_id = unsafe { DeviceId::dummy() }; runner.send_events( iter::once(Event::WindowEvent { @@ -174,7 +173,7 @@ impl EventLoopWindowTarget { } }); - let device_id = RootDeviceId(unsafe { DeviceId::dummy() }); + let device_id = unsafe { DeviceId::dummy() }; runner.send_events( iter::once(Event::WindowEvent { @@ -216,7 +215,7 @@ impl EventLoopWindowTarget { let pointer = pointer_id.map(|pointer_id| Event::WindowEvent { window_id: id, event: WindowEvent::CursorLeft { - device_id: RootDeviceId(DeviceId(pointer_id)), + device_id: DeviceId::from(pointer_id as u64), }, }); @@ -243,7 +242,7 @@ impl EventLoopWindowTarget { let pointer = pointer_id.map(|pointer_id| Event::WindowEvent { window_id: id, event: WindowEvent::CursorEntered { - device_id: RootDeviceId(DeviceId(pointer_id)), + device_id: DeviceId::from(pointer_id as u64), }, }); @@ -285,7 +284,7 @@ impl EventLoopWindowTarget { }); runner.send_events(modifiers.into_iter().chain(events.flat_map(|position| { - let device_id = RootDeviceId(DeviceId(pointer_id)); + let device_id = DeviceId::from(pointer_id as u64); iter::once(Event::WindowEvent { window_id: id, @@ -317,7 +316,7 @@ impl EventLoopWindowTarget { window_id: id, event: WindowEvent::Touch(Touch { id: device_id as u64, - device_id: RootDeviceId(DeviceId(device_id)), + device_id: DeviceId::from(device_id as u64), phase: TouchPhase::Moved, force: Some(force), location, @@ -345,7 +344,7 @@ impl EventLoopWindowTarget { } }); - let device_id = RootDeviceId(DeviceId(pointer_id)); + let device_id = DeviceId::from(pointer_id as u64); let state = if buttons.contains(button.into()) { ElementState::Pressed @@ -405,7 +404,7 @@ impl EventLoopWindowTarget { } }); - let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id)); + let device_id: DeviceId = DeviceId::from(pointer_id as u64); // A mouse down event may come in without any prior CursorMoved events, // therefore we should send a CursorMoved event to make sure that the @@ -447,7 +446,7 @@ impl EventLoopWindowTarget { window_id: id, event: WindowEvent::Touch(Touch { id: device_id as u64, - device_id: RootDeviceId(DeviceId(device_id)), + device_id: DeviceId::from(device_id as u64), phase: TouchPhase::Started, force: Some(force), location, @@ -489,7 +488,7 @@ impl EventLoopWindowTarget { } }); - let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id)); + let device_id: DeviceId = DeviceId::from(pointer_id as u64); // A mouse up event may come in without any prior CursorMoved events, // therefore we should send a CursorMoved event to make sure that the @@ -533,7 +532,7 @@ impl EventLoopWindowTarget { window_id: id, event: WindowEvent::Touch(Touch { id: device_id as u64, - device_id: RootDeviceId(DeviceId(device_id)), + device_id: DeviceId::from(device_id as u64), phase: TouchPhase::Ended, force: Some(force), location, @@ -560,7 +559,7 @@ impl EventLoopWindowTarget { Event::WindowEvent { window_id: id, event: WindowEvent::MouseWheel { - device_id: RootDeviceId(DeviceId(pointer_id)), + device_id: DeviceId::from(pointer_id as u64), delta, phase: TouchPhase::Moved, }, @@ -574,7 +573,7 @@ impl EventLoopWindowTarget { window_id: id, event: WindowEvent::Touch(Touch { id: device_id as u64, - device_id: RootDeviceId(DeviceId(device_id)), + device_id: DeviceId::from(device_id as u64), phase: TouchPhase::Cancelled, force: Some(force), location, diff --git a/winit/src/platform_impl/web/mod.rs b/winit/src/platform_impl/web/mod.rs index bbc02d838..905a41687 100644 --- a/winit/src/platform_impl/web/mod.rs +++ b/winit/src/platform_impl/web/mod.rs @@ -19,7 +19,6 @@ mod r#async; mod cursor; -mod device; mod error; mod event_loop; mod keyboard; @@ -30,7 +29,6 @@ mod window; #[path = "web_sys/mod.rs"] mod backend; -pub use self::device::DeviceId; pub use self::error::OsError; pub(crate) use self::event_loop::{ EventLoop, EventLoopProxy, EventLoopWindowTarget, OwnedDisplayHandle, diff --git a/winit/src/platform_impl/windows/mod.rs b/winit/src/platform_impl/windows/mod.rs index 775428523..931a67205 100644 --- a/winit/src/platform_impl/windows/mod.rs +++ b/winit/src/platform_impl/windows/mod.rs @@ -22,7 +22,7 @@ pub use self::icon::WinIcon as PlatformIcon; pub(crate) use crate::cursor::OnlyCursorImageBuilder as PlatformCustomCursorBuilder; use crate::platform_impl::Fullscreen; -use crate::event::DeviceId as RootDeviceId; +use crate::event::DeviceId; use crate::icon::Icon; use crate::keyboard::Key; use crate::platform::windows::{BackdropType, Color, CornerPreference}; @@ -75,30 +75,20 @@ pub struct Cursor(pub *const u16); unsafe impl Send for Cursor {} unsafe impl Sync for Cursor {} -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DeviceId(u32); - -impl DeviceId { - pub const unsafe fn dummy() -> Self { - DeviceId(0) - } -} - -impl DeviceId { - pub fn persistent_identifier(&self) -> Option { - if self.0 != 0 { - raw_input::get_raw_input_device_name(self.0 as HANDLE) + pub fn persistent_identifier(id: DeviceId) -> Option { + let val: u64 = id.into(); + if val != 0 { + raw_input::get_raw_input_device_name(val as HANDLE) } else { None } } -} // Constant device ID, to be removed when this backend is updated to report real device IDs. -const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId(0)); +const DEVICE_ID: DeviceId = unsafe { DeviceId::dummy() }; -fn wrap_device_id(id: u32) -> RootDeviceId { - RootDeviceId(DeviceId(id)) +fn wrap_device_id(id: u32) -> DeviceId { + (id as u64).into() } pub type OsError = std::io::Error;