feat: Move DeviceId to winit-core

This requires a similar restructuring as with WindowId.

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley
2024-02-04 12:01:40 -08:00
parent a232f7bc77
commit 859a6f109c
25 changed files with 107 additions and 199 deletions

36
winit-core/src/event.rs Normal file
View File

@@ -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<u64> for DeviceId {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<DeviceId> for u64 {
fn from(value: DeviceId) -> Self {
value.0
}
}

View File

@@ -12,5 +12,6 @@ compile_error! { "no-std and no-alloc usage are not yet supported" }
pub mod dpi; pub mod dpi;
pub mod error; pub mod error;
pub mod event;
pub mod keyboard; pub mod keyboard;
pub mod window; pub mod window;

View File

@@ -54,6 +54,9 @@ use crate::{
window::{ActivationToken, Theme, WindowId}, window::{ActivationToken, Theme, WindowId},
}; };
#[doc(inline)]
pub use winit_core::event::DeviceId;
/// Describes a generic event. /// Describes a generic event.
/// ///
/// See the module-level docs for more information on the event loop manages each event. /// See the module-level docs for more information on the event loop manages each event.
@@ -599,30 +602,6 @@ pub enum WindowEvent {
RedrawRequested, 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. /// 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 /// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera or first-person

View File

@@ -504,7 +504,7 @@ pub trait DeviceIdExtWindows {
impl DeviceIdExtWindows for DeviceId { impl DeviceIdExtWindows for DeviceId {
#[inline] #[inline]
fn persistent_identifier(&self) -> Option<String> { fn persistent_identifier(&self) -> Option<String> {
self.0.persistent_identifier() crate::platform_impl::persistent_identifier(*self)
} }
} }

View File

@@ -385,7 +385,7 @@ impl<T: 'static> EventLoop<T> {
match event { match event {
InputEvent::MotionEvent(motion_event) => { InputEvent::MotionEvent(motion_event) => {
let window_id = WindowId::from(WINDOW_ID); 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() { let phase = match motion_event.action() {
MotionAction::Down | MotionAction::PointerDown => { MotionAction::Down | MotionAction::PointerDown => {
@@ -457,7 +457,7 @@ impl<T: 'static> EventLoop<T> {
let event = event::Event::WindowEvent { let event = event::Event::WindowEvent {
window_id: WindowId::from(WINDOW_ID), window_id: WindowId::from(WINDOW_ID),
event: event::WindowEvent::KeyboardInput { 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 { event: event::KeyEvent {
state, state,
physical_key: keycodes::to_physical_key(keycode), 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)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PlatformSpecificWindowBuilderAttributes; pub struct PlatformSpecificWindowBuilderAttributes;

View File

@@ -68,7 +68,7 @@ mod window;
use std::fmt; use std::fmt;
use crate::event::DeviceId as RootDeviceId; use crate::event::DeviceId;
pub(crate) use self::{ pub(crate) use self::{
event_loop::{ 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::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen; pub(crate) use crate::platform_impl::Fullscreen;
/// There is no way to detect which device that performed a certain event in pub(crate) const DEVICE_ID: DeviceId = unsafe { DeviceId::dummy() };
/// 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);
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeyEventExtra {} pub struct KeyEventExtra {}

View File

@@ -142,23 +142,6 @@ pub(crate) enum Window {
Wayland(wayland::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)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum MonitorHandle { pub enum MonitorHandle {
#[cfg(x11_platform)] #[cfg(x11_platform)]

View File

@@ -34,7 +34,7 @@ use sink::EventSink;
use super::state::{WindowCompositorUpdate, WinitState}; use super::state::{WindowCompositorUpdate, WinitState};
use super::window::state::FrameCallbackState; 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>, WinitState>; type WaylandDispatcher = calloop::Dispatcher<'static, WaylandSource<WinitState>, WinitState>;

View File

@@ -2,12 +2,9 @@
use std::vec::Drain; use std::vec::Drain;
use crate::event::{DeviceEvent, DeviceId as RootDeviceId, Event, WindowEvent}; use crate::event::{DeviceEvent, DeviceId, Event, WindowEvent};
use crate::platform_impl::platform::DeviceId as PlatformDeviceId;
use crate::window::WindowId; use crate::window::WindowId;
use super::DeviceId;
/// An event loop's sink to deliver events from the Wayland event callbacks /// An event loop's sink to deliver events from the Wayland event callbacks
/// to the winit's user. /// to the winit's user.
#[derive(Default)] #[derive(Default)]
@@ -31,7 +28,7 @@ impl EventSink {
pub fn push_device_event(&mut self, event: DeviceEvent, device_id: DeviceId) { pub fn push_device_event(&mut self, event: DeviceEvent, device_id: DeviceId) {
self.window_events.push(Event::DeviceEvent { self.window_events.push(Event::DeviceEvent {
event, event,
device_id: RootDeviceId(PlatformDeviceId::Wayland(device_id)), device_id,
}); });
} }

View File

@@ -65,16 +65,6 @@ impl From<WaylandError> 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. /// Get the WindowId out of the surface.
#[inline] #[inline]
fn make_wid(surface: &WlSurface) -> WindowId { fn make_wid(surface: &WlSurface) -> WindowId {

View File

@@ -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::event_loop::sink::EventSink;
use crate::platform_impl::wayland::seat::WinitSeatState; use crate::platform_impl::wayland::seat::WinitSeatState;
use crate::platform_impl::wayland::state::WinitState; use crate::platform_impl::wayland::state::WinitState;
use crate::platform_impl::wayland::{self, DeviceId, WindowId}; use crate::platform_impl::wayland::{self, WindowId};
impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState { impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
fn event( fn event(
@@ -384,7 +384,7 @@ fn key_input(
let keyboard_state = seat_state.keyboard_state.as_mut().unwrap(); 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 let event = keyboard_state
.xkb_state .xkb_state
.process_key_event(keycode, state, repeat); .process_key_event(keycode, state, repeat);

View File

@@ -22,12 +22,13 @@ use sctk::globals::GlobalData;
use sctk::seat::pointer::{PointerData, PointerDataExt}; use sctk::seat::pointer::{PointerData, PointerDataExt};
use sctk::seat::pointer::{PointerEvent, PointerEventKind, PointerHandler}; use sctk::seat::pointer::{PointerEvent, PointerEventKind, PointerHandler};
use sctk::seat::SeatState; use sctk::seat::SeatState;
use winit_core::event::DeviceId;
use crate::dpi::{LogicalPosition, PhysicalPosition}; use crate::dpi::{LogicalPosition, PhysicalPosition};
use crate::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent}; use crate::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent};
use crate::platform_impl::wayland::state::WinitState; 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; pub mod relative_pointer;
@@ -42,7 +43,7 @@ impl PointerHandler for WinitState {
let seat = pointer.winit_data().seat(); let seat = pointer.winit_data().seat();
let seat_state = self.seats.get(&seat.id()).unwrap(); 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 { for event in events {
let surface = &event.surface; let surface = &event.surface;

View File

@@ -70,7 +70,7 @@ impl Dispatch<ZwpRelativePointerV1, GlobalData, WinitState> for RelativePointerS
DeviceEvent::MouseMotion { DeviceEvent::MouseMotion {
delta: (dx_unaccel, dy_unaccel), delta: (dx_unaccel, dy_unaccel),
}, },
super::DeviceId, crate::event::DeviceId::from(0),
); );
} }
} }

View File

@@ -6,12 +6,13 @@ use sctk::reexports::client::protocol::wl_touch::WlTouch;
use sctk::reexports::client::{Connection, Proxy, QueueHandle}; use sctk::reexports::client::{Connection, Proxy, QueueHandle};
use sctk::seat::touch::{TouchData, TouchHandler}; use sctk::seat::touch::{TouchData, TouchHandler};
use winit_core::event::DeviceId;
use crate::dpi::LogicalPosition; use crate::dpi::LogicalPosition;
use crate::event::{Touch, TouchPhase, WindowEvent}; use crate::event::{Touch, TouchPhase, WindowEvent};
use crate::platform_impl::wayland::state::WinitState; use crate::platform_impl::wayland::state::WinitState;
use crate::platform_impl::wayland::{self, DeviceId}; use crate::platform_impl::wayland;
impl TouchHandler for WinitState { impl TouchHandler for WinitState {
fn down( fn down(
@@ -42,9 +43,7 @@ impl TouchHandler for WinitState {
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( device_id: DeviceId::from(0),
DeviceId,
)),
phase: TouchPhase::Started, phase: TouchPhase::Started,
location: location.to_physical(scale_factor), location: location.to_physical(scale_factor),
force: None, force: None,
@@ -79,9 +78,7 @@ impl TouchHandler for WinitState {
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( device_id: DeviceId::from(0),
DeviceId,
)),
phase: TouchPhase::Ended, phase: TouchPhase::Ended,
location: touch_point.location.to_physical(scale_factor), location: touch_point.location.to_physical(scale_factor),
force: None, force: None,
@@ -118,9 +115,7 @@ impl TouchHandler for WinitState {
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( device_id: DeviceId::from(0),
DeviceId,
)),
phase: TouchPhase::Moved, phase: TouchPhase::Moved,
location: touch_point.location.to_physical(scale_factor), location: touch_point.location.to_physical(scale_factor),
force: None, force: None,
@@ -144,9 +139,7 @@ impl TouchHandler for WinitState {
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::Touch(Touch { WindowEvent::Touch(Touch {
device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( device_id: DeviceId::from(0),
DeviceId,
)),
phase: TouchPhase::Cancelled, phase: TouchPhase::Cancelled,
location, location,
force: None, force: None,

View File

@@ -17,13 +17,13 @@ use x11rb::{
}; };
use super::{ 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, Dnd, DndState, GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow,
}; };
use crate::{ use crate::{
dpi::{PhysicalPosition, PhysicalSize}, 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, event_loop::EventLoopWindowTarget as RootELW,
keyboard::ModifiersState, keyboard::ModifiersState,
platform_impl::platform::common::{keymap, xkb_state::KbdState}, platform_impl::platform::common::{keymap, xkb_state::KbdState},
@@ -67,7 +67,7 @@ impl EventProcessor {
let mut devices = self.devices.borrow_mut(); let mut devices = self.devices.borrow_mut();
if let Some(info) = DeviceInfo::get(&wt.xconn, device as _) { if let Some(info) = DeviceInfo::get(&wt.xconn, device as _) {
for info in info.iter() { 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 mut devices = self.devices.borrow_mut();
let physical_device = match devices let physical_device = match devices
.get_mut(&DeviceId(xev.sourceid as xinput::DeviceId)) .get_mut(&mkdid(xev.sourceid as xinput::DeviceId))
{ {
Some(device) => device, Some(device) => device,
None => return, None => return,
@@ -933,7 +933,7 @@ impl EventProcessor {
// the virtual device. // the virtual device.
|| device_info.attachment == xev.sourceid || 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) { if let Some(device) = devices.get_mut(&device_id) {
device.reset_scroll_position(device_info); device.reset_scroll_position(device_info);
} }
@@ -1015,7 +1015,7 @@ impl EventProcessor {
let pointer_id = self let pointer_id = self
.devices .devices
.borrow() .borrow()
.get(&DeviceId(xev.deviceid as xinput::DeviceId)) .get(&mkdid(xev.deviceid as xinput::DeviceId))
.map(|device| device.attachment) .map(|device| device.attachment)
.unwrap_or(2); .unwrap_or(2);
@@ -1252,7 +1252,7 @@ impl EventProcessor {
event: DeviceEvent::Removed, event: DeviceEvent::Removed,
}); });
let mut devices = self.devices.borrow_mut(); let mut devices = self.devices.borrow_mut();
devices.remove(&DeviceId(info.deviceid as xinput::DeviceId)); devices.remove(&mkdid(info.deviceid as xinput::DeviceId));
} }
} }
} }

View File

@@ -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<UnownedWindow>); pub(crate) struct Window(Arc<UnownedWindow>);
impl Deref for Window { impl Deref for Window {
@@ -1043,7 +1033,7 @@ fn mkwid(w: xproto::Window) -> crate::window::WindowId {
WindowId::from(w as u64) WindowId::from(w as u64)
} }
fn mkdid(w: xinput::DeviceId) -> crate::event::DeviceId { 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)] #[derive(Debug)]

View File

@@ -25,7 +25,7 @@ pub(crate) use self::{
monitor::{MonitorHandle, VideoModeHandle}, monitor::{MonitorHandle, VideoModeHandle},
window_delegate::PlatformSpecificWindowBuilderAttributes, 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::cursor::CustomCursor as PlatformCustomCursor;
pub(crate) use self::window::Window; 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::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen; 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. // 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)] #[derive(Debug)]
pub enum OsError { pub enum OsError {

View File

@@ -25,7 +25,7 @@ use crate::{
}; };
use super::{ use super::{
DeviceId, KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes, KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes,
RedoxSocket, TimeSocket, WindowProperties, RedoxSocket, TimeSocket, WindowProperties,
}; };
@@ -344,7 +344,7 @@ impl<T: 'static> EventLoop<T> {
event_handler(event::Event::WindowEvent { event_handler(event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::KeyboardInput { event: event::WindowEvent::KeyboardInput {
device_id: event::DeviceId(DeviceId), device_id: event::DeviceId::from(0),
event: event::KeyEvent { event: event::KeyEvent {
logical_key: Key::Unidentified(NativeKey::Unidentified), logical_key: Key::Unidentified(NativeKey::Unidentified),
physical_key, physical_key,
@@ -382,7 +382,7 @@ impl<T: 'static> EventLoop<T> {
event_handler(event::Event::WindowEvent { event_handler(event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::CursorMoved { event: event::WindowEvent::CursorMoved {
device_id: event::DeviceId(DeviceId), device_id: event::DeviceId::from(0),
position: (x, y).into(), position: (x, y).into(),
}, },
}); });
@@ -396,7 +396,7 @@ impl<T: 'static> EventLoop<T> {
event_handler(event::Event::WindowEvent { event_handler(event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::MouseInput { event: event::WindowEvent::MouseInput {
device_id: event::DeviceId(DeviceId), device_id: event::DeviceId::from(0),
state, state,
button, button,
}, },
@@ -407,7 +407,7 @@ impl<T: 'static> EventLoop<T> {
event_handler(event::Event::WindowEvent { event_handler(event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::MouseWheel { 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), delta: event::MouseScrollDelta::LineDelta(x as f32, y as f32),
phase: event::TouchPhase::Moved, phase: event::TouchPhase::Moved,
}, },
@@ -446,14 +446,14 @@ impl<T: 'static> EventLoop<T> {
event_handler(event::Event::WindowEvent { event_handler(event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::CursorEntered { event: event::WindowEvent::CursorEntered {
device_id: event::DeviceId(DeviceId), device_id: event::DeviceId::from(0),
}, },
}); });
} else { } else {
event_handler(event::Event::WindowEvent { event_handler(event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::CursorLeft { event: event::WindowEvent::CursorLeft {
device_id: event::DeviceId(DeviceId), device_id: event::DeviceId::from(0),
}, },
}); });
} }

View File

@@ -95,15 +95,6 @@ impl TimeSocket {
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct PlatformSpecificEventLoopAttributes {} 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)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PlatformSpecificWindowBuilderAttributes; pub struct PlatformSpecificWindowBuilderAttributes;

View File

@@ -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)
}
}

View File

@@ -5,7 +5,7 @@ use crate::error::EventLoopError;
use crate::event::Event; use crate::event::Event;
use crate::event_loop::EventLoopWindowTarget as RootEventLoopWindowTarget; use crate::event_loop::EventLoopWindowTarget as RootEventLoopWindowTarget;
use super::{backend, device}; use super::backend;
mod proxy; mod proxy;
pub(crate) mod runner; pub(crate) mod runner;

View File

@@ -1,9 +1,8 @@
use super::super::main_thread::MainThreadMarker; use super::super::main_thread::MainThreadMarker;
use super::super::DeviceId;
use super::{backend, state::State}; use super::{backend, state::State};
use crate::dpi::PhysicalSize; use crate::dpi::PhysicalSize;
use crate::event::{ use crate::event::{
DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, RawKeyEvent, StartCause, DeviceEvent, DeviceId, ElementState, Event, RawKeyEvent, StartCause,
WindowEvent, WindowEvent,
}; };
use crate::event_loop::{ControlFlow, DeviceEvents}; use crate::event_loop::{ControlFlow, DeviceEvents};
@@ -265,7 +264,7 @@ impl Shared {
} }
// chorded button event // 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) { if let Some(button) = backend::event::mouse_button(&event) {
debug_assert_eq!( debug_assert_eq!(
@@ -337,7 +336,7 @@ impl Shared {
if let Some(delta) = backend::event::mouse_scroll_delta(&window, &event) { if let Some(delta) = backend::event::mouse_scroll_delta(&window, &event) {
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(0)), device_id: DeviceId::from(0u64),
event: DeviceEvent::MouseWheel { delta }, event: DeviceEvent::MouseWheel { delta },
}); });
} }
@@ -358,7 +357,7 @@ impl Shared {
let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())), device_id: DeviceId::from(event.pointer_id() as u64),
event: DeviceEvent::Button { event: DeviceEvent::Button {
button: button.to_id(), button: button.to_id(),
state: ElementState::Pressed, state: ElementState::Pressed,
@@ -381,7 +380,7 @@ impl Shared {
let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())), device_id: DeviceId::from(event.pointer_id() as u64),
event: DeviceEvent::Button { event: DeviceEvent::Button {
button: button.to_id(), button: button.to_id(),
state: ElementState::Released, state: ElementState::Released,
@@ -399,7 +398,7 @@ impl Shared {
} }
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(unsafe { DeviceId::dummy() }), device_id: unsafe { DeviceId::dummy() },
event: DeviceEvent::Key(RawKeyEvent { event: DeviceEvent::Key(RawKeyEvent {
physical_key: backend::event::key_code(&event), physical_key: backend::event::key_code(&event),
state: ElementState::Pressed, state: ElementState::Pressed,
@@ -417,7 +416,7 @@ impl Shared {
} }
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(unsafe { DeviceId::dummy() }), device_id: unsafe { DeviceId::dummy() },
event: DeviceEvent::Key(RawKeyEvent { event: DeviceEvent::Key(RawKeyEvent {
physical_key: backend::event::key_code(&event), physical_key: backend::event::key_code(&event),
state: ElementState::Released, state: ElementState::Released,

View File

@@ -10,11 +10,10 @@ use super::runner::{EventWrapper, Execution};
use super::{ use super::{
super::{monitor::MonitorHandle, KeyEventExtra}, super::{monitor::MonitorHandle, KeyEventExtra},
backend, backend,
device::DeviceId,
runner, runner,
}; };
use crate::event::{ 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::event_loop::{ControlFlow, DeviceEvents};
use crate::keyboard::ModifiersState; 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( runner.send_events(
iter::once(Event::WindowEvent { 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( runner.send_events(
iter::once(Event::WindowEvent { iter::once(Event::WindowEvent {
@@ -216,7 +215,7 @@ impl EventLoopWindowTarget {
let pointer = pointer_id.map(|pointer_id| Event::WindowEvent { let pointer = pointer_id.map(|pointer_id| Event::WindowEvent {
window_id: id, window_id: id,
event: WindowEvent::CursorLeft { 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 { let pointer = pointer_id.map(|pointer_id| Event::WindowEvent {
window_id: id, window_id: id,
event: WindowEvent::CursorEntered { 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| { 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 { iter::once(Event::WindowEvent {
window_id: id, window_id: id,
@@ -317,7 +316,7 @@ impl EventLoopWindowTarget {
window_id: id, window_id: id,
event: WindowEvent::Touch(Touch { event: WindowEvent::Touch(Touch {
id: device_id as u64, id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)), device_id: DeviceId::from(device_id as u64),
phase: TouchPhase::Moved, phase: TouchPhase::Moved,
force: Some(force), force: Some(force),
location, 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()) { let state = if buttons.contains(button.into()) {
ElementState::Pressed 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, // A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the // therefore we should send a CursorMoved event to make sure that the
@@ -447,7 +446,7 @@ impl EventLoopWindowTarget {
window_id: id, window_id: id,
event: WindowEvent::Touch(Touch { event: WindowEvent::Touch(Touch {
id: device_id as u64, id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)), device_id: DeviceId::from(device_id as u64),
phase: TouchPhase::Started, phase: TouchPhase::Started,
force: Some(force), force: Some(force),
location, 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, // A mouse up event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the // therefore we should send a CursorMoved event to make sure that the
@@ -533,7 +532,7 @@ impl EventLoopWindowTarget {
window_id: id, window_id: id,
event: WindowEvent::Touch(Touch { event: WindowEvent::Touch(Touch {
id: device_id as u64, id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)), device_id: DeviceId::from(device_id as u64),
phase: TouchPhase::Ended, phase: TouchPhase::Ended,
force: Some(force), force: Some(force),
location, location,
@@ -560,7 +559,7 @@ impl EventLoopWindowTarget {
Event::WindowEvent { Event::WindowEvent {
window_id: id, window_id: id,
event: WindowEvent::MouseWheel { event: WindowEvent::MouseWheel {
device_id: RootDeviceId(DeviceId(pointer_id)), device_id: DeviceId::from(pointer_id as u64),
delta, delta,
phase: TouchPhase::Moved, phase: TouchPhase::Moved,
}, },
@@ -574,7 +573,7 @@ impl EventLoopWindowTarget {
window_id: id, window_id: id,
event: WindowEvent::Touch(Touch { event: WindowEvent::Touch(Touch {
id: device_id as u64, id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)), device_id: DeviceId::from(device_id as u64),
phase: TouchPhase::Cancelled, phase: TouchPhase::Cancelled,
force: Some(force), force: Some(force),
location, location,

View File

@@ -19,7 +19,6 @@
mod r#async; mod r#async;
mod cursor; mod cursor;
mod device;
mod error; mod error;
mod event_loop; mod event_loop;
mod keyboard; mod keyboard;
@@ -30,7 +29,6 @@ mod window;
#[path = "web_sys/mod.rs"] #[path = "web_sys/mod.rs"]
mod backend; mod backend;
pub use self::device::DeviceId;
pub use self::error::OsError; pub use self::error::OsError;
pub(crate) use self::event_loop::{ pub(crate) use self::event_loop::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, OwnedDisplayHandle, EventLoop, EventLoopProxy, EventLoopWindowTarget, OwnedDisplayHandle,

View File

@@ -22,7 +22,7 @@ pub use self::icon::WinIcon as PlatformIcon;
pub(crate) use crate::cursor::OnlyCursorImageBuilder as PlatformCustomCursorBuilder; pub(crate) use crate::cursor::OnlyCursorImageBuilder as PlatformCustomCursorBuilder;
use crate::platform_impl::Fullscreen; use crate::platform_impl::Fullscreen;
use crate::event::DeviceId as RootDeviceId; use crate::event::DeviceId;
use crate::icon::Icon; use crate::icon::Icon;
use crate::keyboard::Key; use crate::keyboard::Key;
use crate::platform::windows::{BackdropType, Color, CornerPreference}; use crate::platform::windows::{BackdropType, Color, CornerPreference};
@@ -75,30 +75,20 @@ pub struct Cursor(pub *const u16);
unsafe impl Send for Cursor {} unsafe impl Send for Cursor {}
unsafe impl Sync for Cursor {} unsafe impl Sync for Cursor {}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub fn persistent_identifier(id: DeviceId) -> Option<String> {
pub struct DeviceId(u32); let val: u64 = id.into();
if val != 0 {
impl DeviceId { raw_input::get_raw_input_device_name(val as HANDLE)
pub const unsafe fn dummy() -> Self {
DeviceId(0)
}
}
impl DeviceId {
pub fn persistent_identifier(&self) -> Option<String> {
if self.0 != 0 {
raw_input::get_raw_input_device_name(self.0 as HANDLE)
} else { } else {
None None
} }
} }
}
// Constant device ID, to be removed when this backend is updated to report real device IDs. // 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 { fn wrap_device_id(id: u32) -> DeviceId {
RootDeviceId(DeviceId(id)) (id as u64).into()
} }
pub type OsError = std::io::Error; pub type OsError = std::io::Error;