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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use gamepad::NativeEvCode;
use vec_map::{self, VecMap};
use std::iter::Iterator;
use std::time::SystemTime;
pub mod filter;
#[derive(Clone, Debug)]
pub struct GamepadState {
buttons: VecMap<ButtonData>,
axes: VecMap<AxisData>,
}
impl GamepadState {
pub(crate) fn new() -> Self {
GamepadState {
buttons: VecMap::new(),
axes: VecMap::new(),
}
}
pub fn is_pressed(&self, btn: NativeEvCode) -> bool {
self.buttons
.get(btn as usize)
.map(|s| s.is_pressed())
.unwrap_or(false)
}
pub fn value(&self, axis: NativeEvCode) -> f32 {
self.axes
.get(axis as usize)
.map(|s| s.value())
.unwrap_or(0.0)
}
pub fn buttons(&self) -> ButtonDataIter {
ButtonDataIter(self.buttons.iter())
}
pub fn axes(&self) -> AxisDataIter {
AxisDataIter(self.axes.iter())
}
pub fn button_data(&self, btn: NativeEvCode) -> Option<&ButtonData> {
self.buttons.get(btn as usize)
}
pub fn axis_data(&self, axis: NativeEvCode) -> Option<&AxisData> {
self.axes.get(axis as usize)
}
pub(crate) fn update_btn(&mut self, btn: NativeEvCode, data: ButtonData) {
self.buttons.insert(btn as usize, data);
}
pub(crate) fn update_axis(&mut self, axis: NativeEvCode, data: AxisData) {
self.axes.insert(axis as usize, data);
}
}
pub struct ButtonDataIter<'a>(vec_map::Iter<'a, ButtonData>);
pub struct AxisDataIter<'a>(vec_map::Iter<'a, AxisData>);
impl<'a> Iterator for ButtonDataIter<'a> {
type Item = (usize, &'a ButtonData);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<'a> Iterator for AxisDataIter<'a> {
type Item = (usize, &'a AxisData);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
#[derive(Clone, Copy, Debug)]
pub struct ButtonData {
last_event_ts: SystemTime,
state_and_counter: u64,
}
impl ButtonData {
pub(crate) fn new(pressed: bool, repeating: bool, counter: u64, time: SystemTime) -> Self {
debug_assert!(counter <= 0x3FFF_FFFF_FFFF_FFFF);
let state = ((pressed as u64) << 63) | ((repeating as u64) << 62);
ButtonData {
last_event_ts: time,
state_and_counter: state | counter,
}
}
pub fn is_pressed(&self) -> bool {
self.state_and_counter >> 63 == 1
}
pub fn is_repeating(&self) -> bool {
self.state_and_counter & 0x4000_0000_0000_0000 != 0
}
pub fn counter(&self) -> u64 {
self.state_and_counter & 0x3FFF_FFFF_FFFF_FFFF
}
pub fn timestamp(&self) -> SystemTime {
self.last_event_ts
}
}
#[derive(Clone, Copy, Debug)]
pub struct AxisData {
last_event_ts: SystemTime,
last_event_c: u64,
value: f32,
}
impl AxisData {
pub(crate) fn new(value: f32, counter: u64, time: SystemTime) -> Self {
AxisData {
last_event_ts: time,
last_event_c: counter,
value,
}
}
pub fn value(&self) -> f32 {
self.value
}
pub fn counter(&self) -> u64 {
self.last_event_c
}
pub fn timestamp(&self) -> SystemTime {
self.last_event_ts
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::SystemTime;
#[test]
fn button_data() {
let mut state = 1234567890123456789u64;
let mut xorshift = || {
let mut x = state;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
state = x;
x.wrapping_mul(0x2545F4914F6CDD1D)
};
for _ in 0..(1024 * 1024 * 16) {
let counter = xorshift() & 0x3FFF_FFFF_FFFF_FFFF;
let pressed = xorshift() % 2 == 1;
let repeating = xorshift() % 2 == 1;
let btn = ButtonData::new(pressed, repeating, counter, SystemTime::now());
assert_eq!(btn.is_pressed(), pressed);
assert_eq!(btn.is_repeating(), repeating);
assert_eq!(btn.counter(), counter);
}
}
}