Skip to content

Commit

Permalink
chore: remove platform DeviceId
Browse files Browse the repository at this point in the history
The same as for `WindowId`.
  • Loading branch information
kchibisov committed Oct 10, 2024
1 parent da2268a commit 63f4e84
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 123 deletions.
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ changelog entry.
- Add `WindowId::into_raw()` and `from_raw()`.
- Add `PointerKind`, `PointerSource`, `ButtonSource`, `FingerId` and `position` to all pointer
events as part of the pointer event overhaul.
- Add `DeviceId::into_raw()` and `from_raw()`.

### Changed

Expand Down
20 changes: 19 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,25 @@ impl From<MouseButton> for ButtonSource {
/// 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);
pub struct DeviceId(i64);

impl DeviceId {
/// Convert the [`DeviceId`] into the underlying integer.
///
/// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic.
#[allow(dead_code)]
pub(crate) const fn into_raw(self) -> i64 {
self.0
}

/// Construct a [`DeviceId`] from the underlying integer.
///
/// This should only be called with integers returned from [`DeviceId::into_raw`].
#[allow(dead_code)]
pub(crate) const fn from_raw(id: i64) -> Self {
Self(id)
}
}

/// Identifier of a finger in a touch event.
///
Expand Down
11 changes: 9 additions & 2 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::path::Path;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(windows_platform)]
use windows_sys::Win32::Foundation::HANDLE;

use crate::dpi::PhysicalSize;
use crate::event::{DeviceId, FingerId};
Expand Down Expand Up @@ -656,10 +658,15 @@ pub trait DeviceIdExtWindows {
fn persistent_identifier(&self) -> Option<String>;
}

#[cfg(windows_platform)]
impl DeviceIdExtWindows for DeviceId {
#[inline]
fn persistent_identifier(&self) -> Option<String> {
self.0.persistent_identifier()
let raw_id = self.into_raw();
if raw_id != 0 {
crate::platform_impl::raw_input::get_raw_input_device_name(raw_id as HANDLE)
} else {
None
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::application::ApplicationHandler;
use crate::cursor::Cursor;
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{EventLoopError, NotSupportedError, RequestError};
use crate::event::{self, Force, StartCause, SurfaceSizeWriter};
use crate::event::{self, DeviceId, Force, StartCause, SurfaceSizeWriter};
use crate::event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
EventLoopProxy as RootEventLoopProxy, OwnedDisplayHandle as RootOwnedDisplayHandle,
Expand Down Expand Up @@ -314,7 +314,7 @@ impl EventLoop {
let mut input_status = InputStatus::Handled;
match event {
InputEvent::MotionEvent(motion_event) => {
let device_id = Some(event::DeviceId(DeviceId(motion_event.device_id())));
let device_id = Some(DeviceId::from_raw(motion_event.device_id() as i64));
let action = motion_event.action();

let pointers: Option<
Expand Down Expand Up @@ -452,7 +452,7 @@ impl EventLoop {
);

let event = event::WindowEvent::KeyboardInput {
device_id: Some(event::DeviceId(DeviceId(key.device_id()))),
device_id: Some(DeviceId::from_raw(key.device_id() as i64)),
event: event::KeyEvent {
state,
physical_key: keycodes::to_physical_key(keycode),
Expand Down Expand Up @@ -728,9 +728,6 @@ impl OwnedDisplayHandle {
}
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DeviceId(i32);

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct FingerId(i32);

Expand Down
3 changes: 0 additions & 3 deletions src/platform_impl/apple/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ pub(crate) use crate::cursor::OnlyCursorImageSource as PlatformCustomCursorSourc
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;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId;

Expand Down
7 changes: 0 additions & 7 deletions src/platform_impl/apple/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ pub(crate) use crate::cursor::{
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 whether 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;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId(usize);

Expand Down
9 changes: 0 additions & 9 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ impl Default for PlatformSpecificWindowAttributes {
pub(crate) static X11_BACKEND: Lazy<Mutex<Result<Arc<XConnection>, XNotSupported>>> =
Lazy::new(|| Mutex::new(XConnection::new(Some(x_error_callback)).map(Arc::new)));

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DeviceId {
#[cfg(x11_platform)]
X(x11::DeviceId),
#[cfg(wayland_platform)]
#[allow(unused)]
Wayland(wayland::DeviceId),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FingerId {
#[cfg(x11_platform)]
Expand Down
4 changes: 0 additions & 4 deletions src/platform_impl/linux/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ mod state;
mod types;
mod window;

/// Dummy device id, since Wayland doesn't have device events.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId(i32);

Expand Down
18 changes: 9 additions & 9 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use xkbcommon_dl::xkb_mod_mask_t;

use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::event::{
ButtonSource, DeviceEvent, ElementState, Event, Ime, MouseButton, MouseScrollDelta,
ButtonSource, DeviceEvent, DeviceId, ElementState, Event, Ime, MouseButton, MouseScrollDelta,
PointerKind, PointerSource, RawKeyEvent, SurfaceSizeWriter, TouchPhase, WindowEvent,
};
use crate::keyboard::ModifiersState;
Expand All @@ -33,8 +33,8 @@ use crate::platform_impl::platform::x11::ActiveEventLoop;
use crate::platform_impl::x11::atoms::*;
use crate::platform_impl::x11::util::cookie::GenericEventCookie;
use crate::platform_impl::x11::{
mkdid, mkfid, mkwid, util, CookieResultExt, Device, DeviceId, DeviceInfo, Dnd, DndState,
ImeReceiver, ScrollOrientation, UnownedWindow, WindowId,
mkdid, mkfid, mkwid, util, CookieResultExt, Device, DeviceInfo, Dnd, DndState, ImeReceiver,
ScrollOrientation, UnownedWindow, WindowId,
};

/// The maximum amount of X modifiers to replay.
Expand Down Expand Up @@ -319,7 +319,7 @@ impl EventProcessor {
let mut devices = self.devices.borrow_mut();
if let Some(info) = DeviceInfo::get(&self.target.xconn, device as _) {
for info in info.iter() {
devices.insert(DeviceId(info.deviceid as _), Device::new(info));
devices.insert(mkdid(info.deviceid as xinput::DeviceId), Device::new(info));
}
}
}
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl EventProcessor {
slice::from_raw_parts(event.valuators.mask, event.valuators.mask_len as usize)
};
let mut devices = self.devices.borrow_mut();
let physical_device = match devices.get_mut(&DeviceId(event.sourceid as xinput::DeviceId)) {
let physical_device = match devices.get_mut(&mkdid(event.sourceid as xinput::DeviceId)) {
Some(device) => device,
None => return,
};
Expand Down Expand Up @@ -1178,7 +1178,7 @@ impl EventProcessor {
if device_info.deviceid == event.sourceid
|| device_info.attachment == event.sourceid
{
let device_id = DeviceId(device_info.deviceid as _);
let device_id = mkdid(device_info.deviceid as xinput::DeviceId);
if let Some(device) = devices.get_mut(&device_id) {
device.reset_scroll_position(device_info);
}
Expand Down Expand Up @@ -1273,8 +1273,8 @@ impl EventProcessor {
let device_id = self
.devices
.borrow()
.get(&DeviceId(xev.deviceid as xinput::DeviceId))
.map(|device| mkdid(device.attachment as _));
.get(&mkdid(xev.deviceid as xinput::DeviceId))
.map(|device| mkdid(device.attachment as xinput::DeviceId));

let event = Event::WindowEvent {
window_id,
Expand Down Expand Up @@ -1520,7 +1520,7 @@ impl EventProcessor {
self.init_device(info.deviceid as xinput::DeviceId);
} else if 0 != info.flags & (xinput2::XISlaveRemoved | xinput2::XIMasterRemoved) {
let mut devices = self.devices.borrow_mut();
devices.remove(&DeviceId(info.deviceid as xinput::DeviceId));
devices.remove(&mkdid(info.deviceid as xinput::DeviceId));
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use x11rb::xcb_ffi::ReplyOrIdError;

use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, RequestError};
use crate::event::{Event, StartCause, WindowEvent};
use crate::event::{DeviceId, Event, StartCause, WindowEvent};
use crate::event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
OwnedDisplayHandle as RootOwnedDisplayHandle,
Expand Down Expand Up @@ -805,9 +805,6 @@ impl<'a> Deref for DeviceInfo<'a> {
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(xinput::DeviceId);

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId(u32);

Expand Down Expand Up @@ -993,8 +990,8 @@ impl<'a, E: fmt::Debug> CookieResultExt for Result<VoidCookie<'a>, E> {
fn mkwid(w: xproto::Window) -> crate::window::WindowId {
crate::window::WindowId::from_raw(w as _)
}
fn mkdid(w: xinput::DeviceId) -> crate::event::DeviceId {
crate::event::DeviceId(crate::platform_impl::DeviceId::X(DeviceId(w)))
fn mkdid(w: xinput::DeviceId) -> DeviceId {
DeviceId::from_raw(w as i64)
}

fn mkfid(w: u32) -> crate::event::FingerId {
Expand Down
3 changes: 0 additions & 3 deletions src/platform_impl/orbital/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,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;

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct FingerId;

Expand Down
23 changes: 9 additions & 14 deletions src/platform_impl/web/event.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::event::FingerId as RootFingerId;
use crate::event::{DeviceId, FingerId as RootFingerId};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(pub(crate) u32);

impl DeviceId {
pub fn new(pointer_id: i32) -> Option<Self> {
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
}
pub(crate) fn mkdid(pointer_id: i32) -> Option<DeviceId> {
if let Ok(pointer_id) = u32::try_from(pointer_id) {
Some(DeviceId::from_raw(pointer_id as i64))
} else if pointer_id == -1 {
None
} else {
tracing::error!("found unexpected negative `PointerEvent.pointerId`: {pointer_id}");
None
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ use wasm_bindgen::JsCast;
use web_sys::{Document, KeyboardEvent, Navigator, PageTransitionEvent, PointerEvent, WheelEvent};
use web_time::{Duration, Instant};

use super::super::event;
use super::super::main_thread::MainThreadMarker;
use super::super::monitor::MonitorHandler;
use super::super::DeviceId;
use super::backend;
use super::state::State;
use crate::dpi::PhysicalSize;
use crate::event::{
DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, RawKeyEvent, StartCause,
WindowEvent,
};
use crate::event::{DeviceEvent, ElementState, Event, RawKeyEvent, StartCause, WindowEvent};
use crate::event_loop::{ControlFlow, DeviceEvents};
use crate::platform::web::{PollStrategy, WaitUntilStrategy};
use crate::platform_impl::platform::backend::EventListenerHandle;
Expand Down Expand Up @@ -286,7 +283,7 @@ impl Shared {
}

// chorded button event
let device_id = DeviceId::new(event.pointer_id()).map(RootDeviceId);
let device_id = event::mkdid(event.pointer_id());

if let Some(button) = backend::event::mouse_button(&event) {
let state = if backend::event::mouse_buttons(&event).contains(button.into()) {
Expand Down Expand Up @@ -344,7 +341,7 @@ impl Shared {

let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent {
device_id: DeviceId::new(event.pointer_id()).map(RootDeviceId),
device_id: event::mkdid(event.pointer_id()),
event: DeviceEvent::Button {
button: button.to_id().into(),
state: ElementState::Pressed,
Expand All @@ -363,7 +360,7 @@ impl Shared {

let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent {
device_id: DeviceId::new(event.pointer_id()).map(RootDeviceId),
device_id: event::mkdid(event.pointer_id()),
event: DeviceEvent::Button {
button: button.to_id().into(),
state: ElementState::Released,
Expand Down
Loading

0 comments on commit 63f4e84

Please sign in to comment.