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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#[cfg(target_os = "linux")]
extern crate libc;
#[cfg(target_os = "linux")]
extern crate libudev_sys;
#[cfg(target_os = "linux")]
#[macro_use]
extern crate nix;
#[cfg(target_os = "linux")]
extern crate vec_map;
#[cfg(target_os = "macos")]
#[macro_use(impl_TCFType)]
extern crate core_foundation;
#[cfg(target_os = "macos")]
extern crate io_kit_sys;
#[cfg(target_os = "macos")]
extern crate vec_map;
#[cfg(target_os = "windows")]
extern crate rusty_xinput;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_arch = "wasm32")]
extern crate stdweb;
extern crate uuid;
#[macro_use]
extern crate log;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::mem;
use std::error;
use std::time::Duration;
use std::time::SystemTime;
mod platform;
mod utils;
pub const IS_Y_AXIS_REVERSED: bool = platform::IS_Y_AXIS_REVERSED;
#[derive(Debug)]
pub struct FfDevice {
inner: platform::FfDevice,
}
impl FfDevice {
pub fn set_ff_state(&mut self, strong: u16, weak: u16, min_duration: Duration) {
self.inner.set_ff_state(strong, weak, min_duration)
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Event {
pub id: usize,
pub event: EventType,
pub time: SystemTime,
}
impl Event {
pub fn new(id: usize, event: EventType) -> Self {
#[cfg(not(target_arch = "wasm32"))]
let time = SystemTime::now();
#[cfg(target_arch = "wasm32")]
let time = {
let epoch = SystemTime::UNIX_EPOCH;
let time = stdweb::web::Date::new().get_time();
let offset = Duration::from_millis(time as u64);
epoch + offset
};
Event { id, event, time }
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EventType {
ButtonPressed(EvCode),
ButtonReleased(EvCode),
AxisValueChanged(i32, EvCode),
Connected,
Disconnected,
}
#[derive(Copy, Clone, Debug)]
pub struct AxisInfo {
pub min: i32,
pub max: i32,
pub deadzone: Option<u32>,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PowerInfo {
Unknown,
Wired,
Discharging(u8),
Charging(u8),
Charged,
}
#[derive(Debug)]
pub struct Gilrs {
inner: platform::Gilrs,
}
impl Gilrs {
pub fn new() -> Result<Self, Error> {
let inner = platform::Gilrs::new().map_err(|e| match e {
PlatformError::NotImplemented(inner) => Error::NotImplemented(Gilrs { inner }),
PlatformError::Other(e) => Error::Other(e),
})?;
Ok(Gilrs { inner })
}
pub fn next_event(&mut self) -> Option<Event> {
self.inner.next_event()
}
pub fn gamepad(&self, id: usize) -> Option<&Gamepad> {
unsafe {
let gp: Option<&platform::Gamepad> = self.inner.gamepad(id);
gp.map(|gp| mem::transmute(gp))
}
}
pub fn last_gamepad_hint(&self) -> usize {
self.inner.last_gamepad_hint()
}
}
#[derive(Debug)]
pub struct Gamepad {
inner: platform::Gamepad,
}
impl Gamepad {
pub fn name(&self) -> &str {
self.inner.name()
}
pub fn is_connected(&self) -> bool {
self.inner.is_connected()
}
pub fn uuid(&self) -> [u8; 16] {
self.inner.uuid().as_bytes().clone()
}
pub fn power_info(&self) -> PowerInfo {
self.inner.power_info()
}
pub fn is_ff_supported(&self) -> bool {
self.inner.is_ff_supported()
}
pub fn ff_device(&self) -> Option<FfDevice> {
self.inner.ff_device().map(|inner| FfDevice { inner })
}
pub fn buttons(&self) -> &[EvCode] {
unsafe {
let bt: &[platform::EvCode] = self.inner.buttons();
mem::transmute(bt)
}
}
pub fn axes(&self) -> &[EvCode] {
unsafe {
let ax: &[platform::EvCode] = self.inner.axes();
mem::transmute(ax)
}
}
pub fn axis_info(&self, nec: EvCode) -> Option<&AxisInfo> {
self.inner.axis_info(nec.0)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EvCode(platform::EvCode);
impl EvCode {
pub fn into_u32(self) -> u32 {
self.0.into_u32()
}
}
impl Display for EvCode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug)]
enum PlatformError {
#[allow(dead_code)]
NotImplemented(platform::Gilrs),
#[allow(dead_code)]
Other(Box<error::Error + Send + Sync>),
}
impl Display for PlatformError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&PlatformError::NotImplemented(_) => {
f.write_str("Gilrs does not support current platform.")
}
&PlatformError::Other(ref e) => e.fmt(f),
}
}
}
impl error::Error for PlatformError {
fn description(&self) -> &str {
match self {
&PlatformError::NotImplemented(_) => "platform not supported",
&PlatformError::Other(_) => "platform specific error",
}
}
fn cause(&self) -> Option<&error::Error> {
match self {
&PlatformError::NotImplemented(_) => None,
&PlatformError::Other(ref e) => Some(&**e),
}
}
}
#[derive(Debug)]
pub enum Error {
#[allow(dead_code)]
NotImplemented(Gilrs),
#[allow(dead_code)]
Other(Box<error::Error + Send + Sync>),
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Error::NotImplemented(_) => f.write_str("Gilrs does not support current platform."),
&Error::Other(ref e) => e.fmt(f),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match self {
&Error::NotImplemented(_) => "platform not supported",
&Error::Other(_) => "platform specific error",
}
}
fn cause(&self) -> Option<&error::Error> {
match self {
&Error::NotImplemented(_) => None,
&Error::Other(ref e) => Some(&**e),
}
}
}
pub mod native_ev_codes {
use super::EvCode;
use platform::native_ev_codes as nec;
pub const AXIS_LSTICKX: EvCode = EvCode(nec::AXIS_LSTICKX);
pub const AXIS_LSTICKY: EvCode = EvCode(nec::AXIS_LSTICKY);
pub const AXIS_LEFTZ: EvCode = EvCode(nec::AXIS_LEFTZ);
pub const AXIS_RSTICKX: EvCode = EvCode(nec::AXIS_RSTICKX);
pub const AXIS_RSTICKY: EvCode = EvCode(nec::AXIS_RSTICKY);
pub const AXIS_RIGHTZ: EvCode = EvCode(nec::AXIS_RIGHTZ);
pub const AXIS_DPADX: EvCode = EvCode(nec::AXIS_DPADX);
pub const AXIS_DPADY: EvCode = EvCode(nec::AXIS_DPADY);
pub const AXIS_RT: EvCode = EvCode(nec::AXIS_RT);
pub const AXIS_LT: EvCode = EvCode(nec::AXIS_LT);
pub const AXIS_RT2: EvCode = EvCode(nec::AXIS_RT2);
pub const AXIS_LT2: EvCode = EvCode(nec::AXIS_LT2);
pub const BTN_SOUTH: EvCode = EvCode(nec::BTN_SOUTH);
pub const BTN_EAST: EvCode = EvCode(nec::BTN_EAST);
pub const BTN_C: EvCode = EvCode(nec::BTN_C);
pub const BTN_NORTH: EvCode = EvCode(nec::BTN_NORTH);
pub const BTN_WEST: EvCode = EvCode(nec::BTN_WEST);
pub const BTN_Z: EvCode = EvCode(nec::BTN_Z);
pub const BTN_LT: EvCode = EvCode(nec::BTN_LT);
pub const BTN_RT: EvCode = EvCode(nec::BTN_RT);
pub const BTN_LT2: EvCode = EvCode(nec::BTN_LT2);
pub const BTN_RT2: EvCode = EvCode(nec::BTN_RT2);
pub const BTN_SELECT: EvCode = EvCode(nec::BTN_SELECT);
pub const BTN_START: EvCode = EvCode(nec::BTN_START);
pub const BTN_MODE: EvCode = EvCode(nec::BTN_MODE);
pub const BTN_LTHUMB: EvCode = EvCode(nec::BTN_LTHUMB);
pub const BTN_RTHUMB: EvCode = EvCode(nec::BTN_RTHUMB);
pub const BTN_DPAD_UP: EvCode = EvCode(nec::BTN_DPAD_UP);
pub const BTN_DPAD_DOWN: EvCode = EvCode(nec::BTN_DPAD_DOWN);
pub const BTN_DPAD_LEFT: EvCode = EvCode(nec::BTN_DPAD_LEFT);
pub const BTN_DPAD_RIGHT: EvCode = EvCode(nec::BTN_DPAD_RIGHT);
}