1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Types for "input" devices.
//!
//! This represents the more basic subset of HIDs, keyboard, mouse, game controllers, it does not include media devices.
use bitflags::bitflags;
use serde::{Deserialize, Serialize};
use zng_txt::Txt;
use crate::{
AxisId,
keyboard::{KeyCode, KeyState},
mouse::{ButtonId, ButtonState, MouseScrollDelta},
};
crate::declare_id! {
/// Input device ID in channel.
///
/// In the View Process this is mapped to a system id.
///
/// In the App Process this is mapped to an unique id, but does not survived View crashes.
///
/// The View Process defines the ID.
pub struct InputDeviceId(_);
}
/// Info about an human input device.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InputDeviceInfo {
/// Display name.
pub name: Txt,
/// Device capabilities.
pub capabilities: InputDeviceCapability,
}
impl InputDeviceInfo {
/// New info.
pub fn new(name: impl Into<Txt>, capabilities: InputDeviceCapability) -> Self {
Self {
name: name.into(),
capabilities,
}
}
}
bitflags! {
/// Capabilities of an input device.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InputDeviceCapability: u8 {
/// Device can produce keyboard key presses.
const KEY = 0b0000_0001;
/// Device can produce button presses.
const BUTTON = 0b0000_0010;
/// Device provides scrolling wheel deltas, vertical or horizontal.
const SCROLL_MOTION = 0b0001_0000;
/// Device provides axis aligned 1D motion.
const AXIS_MOTION = 0b0010_0000;
/// Device provides 2D pointer motion.
const POINTER_MOTION = 0b0100_0000;
}
}
/// Raw input device event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum InputDeviceEvent {
/// 2D pointer motion.
///
/// The values if the delta of movement (x, y), not position.
PointerMotion {
/// Delta of change in the cursor position.
delta: euclid::Vector2D<f64, ()>,
},
/// Scroll wheel motion.
ScrollMotion {
/// Delta of change in the mouse scroll wheel state.
delta: MouseScrollDelta,
},
/// Motion on some analog axis.
///
/// This includes the mouse device and any other that fits.
AxisMotion {
/// Device dependent axis of the motion.
axis: AxisId,
/// Device dependent value.
value: f64,
},
/// Device button press or release.
Button {
/// Device dependent button that was used.
button: ButtonId,
/// If the button was pressed or released.
state: ButtonState,
},
/// Device key press or release.
Key {
/// Physical key.
key_code: KeyCode,
/// If the key was pressed or released.
state: KeyState,
},
}
impl InputDeviceEvent {
/// Change `self` to incorporate `other` or returns `other` if both events cannot be coalesced.
pub fn coalesce(&mut self, other: Self) -> Result<(), Self> {
use InputDeviceEvent::*;
match (self, other) {
(PointerMotion { delta }, PointerMotion { delta: n_delta }) => {
*delta += n_delta;
}
(
ScrollMotion {
delta: MouseScrollDelta::LineDelta(delta_x, delta_y),
},
ScrollMotion {
delta: MouseScrollDelta::LineDelta(n_delta_x, n_delta_y),
},
) => {
*delta_x += n_delta_x;
*delta_y += n_delta_y;
}
(
ScrollMotion {
delta: MouseScrollDelta::PixelDelta(delta_x, delta_y),
},
ScrollMotion {
delta: MouseScrollDelta::PixelDelta(n_delta_x, n_delta_y),
},
) => {
*delta_x += n_delta_x;
*delta_y += n_delta_y;
}
(_, e) => return Err(e),
}
Ok(())
}
}