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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::{fmt, mem, ptr};
#[cfg(feature = "unstable_xtarget_notification")]
use std::{marker, pin, thread};
use std::borrow::Borrow;
use winapi::um::xinput::XINPUT_GAMEPAD;
use winapi::shared::winerror;
use crate::*;
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct XButtons {
pub raw: u16,
}
#[allow(non_snake_case)]
#[inline]
pub const fn XButtons(raw: u16) -> XButtons {
XButtons { raw }
}
#[macro_export]
macro_rules! XButtons {
(UP) => { $crate::XButtons { raw: $crate::XButtons::UP } };
(DOWN) => { $crate::XButtons { raw: $crate::XButtons::DOWN } };
(LEFT) => { $crate::XButtons { raw: $crate::XButtons::LEFT } };
(RIGHT) => { $crate::XButtons { raw: $crate::XButtons::RIGHT } };
(START) => { $crate::XButtons { raw: $crate::XButtons::START } };
(BACK) => { $crate::XButtons { raw: $crate::XButtons::BACK } };
(LTHUMB) => { $crate::XButtons { raw: $crate::XButtons::LTHUMB } };
(RTHUMB) => { $crate::XButtons { raw: $crate::XButtons::RTHUMB } };
(LB) => { $crate::XButtons { raw: $crate::XButtons::LB } };
(RB) => { $crate::XButtons { raw: $crate::XButtons::RB } };
(GUIDE) => { $crate::XButtons { raw: $crate::XButtons::GUIDE } };
(A) => { $crate::XButtons { raw: $crate::XButtons::A } };
(B) => { $crate::XButtons { raw: $crate::XButtons::B } };
(X) => { $crate::XButtons { raw: $crate::XButtons::X } };
(Y) => { $crate::XButtons { raw: $crate::XButtons::Y } };
($($face:ident)|*) => {
$crate::XButtons { raw: 0 $(| $crate::XButtons!($face).raw)* }
};
}
impl XButtons {
pub const UP: u16 = 0x0001;
pub const DOWN: u16 = 0x0002;
pub const LEFT: u16 = 0x0004;
pub const RIGHT: u16 = 0x0008;
pub const START: u16 = 0x0010;
pub const BACK: u16 = 0x0020;
pub const LTHUMB: u16 = 0x0040;
pub const RTHUMB: u16 = 0x0080;
pub const LB: u16 = 0x0100;
pub const RB: u16 = 0x0200;
pub const GUIDE: u16 = 0x0400;
pub const A: u16 = 0x1000;
pub const B: u16 = 0x2000;
pub const X: u16 = 0x4000;
pub const Y: u16 = 0x8000;
}
impl From<u16> for XButtons {
#[inline]
fn from(raw: u16) -> Self {
XButtons { raw }
}
}
impl From<XButtons> for u16 {
#[inline]
fn from(buttons: XButtons) -> Self {
buttons.raw
}
}
impl AsRef<u16> for XButtons {
#[inline]
fn as_ref(&self) -> &u16 {
&self.raw
}
}
impl AsMut<u16> for XButtons {
#[inline]
fn as_mut(&mut self) -> &mut u16 {
&mut self.raw
}
}
impl fmt::Debug for XButtons {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
const NAMES: [&'static str; 16] = [
"UP", "DOWN", "LEFT", "RIGHT",
"START", "BACK", "LTHUMB", "RTHUMB",
"LB", "RB", "GUIDE", "?",
"A", "B", "X", "Y",
];
let mut comma = false;
for index in 0..16 {
if self.raw & (1 << index) != 0 {
if comma {
f.write_str("|")?;
comma = true;
}
f.write_str(NAMES[index])?;
}
}
Ok(())
}
else {
write!(f, "XButtons({:#x})", self.raw)
}
}
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct XGamepad {
pub buttons: XButtons,
pub left_trigger: u8,
pub right_trigger: u8,
pub thumb_lx: i16,
pub thumb_ly: i16,
pub thumb_rx: i16,
pub thumb_ry: i16,
}
impl From<XINPUT_GAMEPAD> for XGamepad {
#[inline]
fn from(gamepad: XINPUT_GAMEPAD) -> Self {
unsafe { mem::transmute(gamepad) }
}
}
impl From<XGamepad> for XINPUT_GAMEPAD {
#[inline]
fn from(report: XGamepad) -> XINPUT_GAMEPAD {
unsafe { mem::transmute(report) }
}
}
impl AsRef<XINPUT_GAMEPAD> for XGamepad {
#[inline]
fn as_ref(&self) -> &XINPUT_GAMEPAD {
unsafe { mem::transmute(self) }
}
}
impl AsMut<XINPUT_GAMEPAD> for XGamepad {
#[inline]
fn as_mut(&mut self) -> &mut XINPUT_GAMEPAD {
unsafe { mem::transmute(self) }
}
}
#[cfg(feature = "unstable_xtarget_notification")]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct XNotification {
pub large_motor: u8,
pub small_motor: u8,
pub led_number: u8,
}
#[cfg(feature = "unstable_xtarget_notification")]
pub struct XRequestNotification {
client: Client,
xurn: bus::RequestNotification<bus::XUsbRequestNotification>,
_unpin: marker::PhantomPinned,
}
#[cfg(feature = "unstable_xtarget_notification")]
impl XRequestNotification {
#[inline]
pub fn is_attached(&self) -> bool {
self.xurn.buffer.SerialNo != 0
}
#[inline]
pub fn spawn_thread<F: FnMut(&XRequestNotification, XNotification) + Send + 'static>(self, mut f: F) -> thread::JoinHandle<()> {
thread::spawn(move || {
let mut reqn = self;
let mut reqn = unsafe { pin::Pin::new_unchecked(&mut reqn) };
loop {
reqn.as_mut().request();
let result = reqn.as_mut().poll(true);
match result {
Ok(None) => {},
Ok(Some(data)) => f(&reqn, data),
Err(_) => break,
}
}
})
}
#[inline(never)]
pub fn request(self: pin::Pin<&mut Self>) {
unsafe {
let device = self.client.device;
let xurn = &mut self.get_unchecked_mut().xurn;
if xurn.buffer.SerialNo != 0 {
xurn.ioctl(device);
}
}
}
#[inline(never)]
pub fn poll(self: pin::Pin<&mut Self>, wait: bool) -> Result<Option<XNotification>, Error> {
unsafe {
let device = self.client.device;
let xurn = &mut self.get_unchecked_mut().xurn;
match xurn.poll(device, wait) {
Ok(()) => Ok(Some(XNotification {
large_motor: xurn.buffer.LargeMotor,
small_motor: xurn.buffer.SmallMotor,
led_number: xurn.buffer.LedNumber,
})),
Err(winerror::ERROR_IO_INCOMPLETE) => Ok(None),
Err(winerror::ERROR_OPERATION_ABORTED) => {
xurn.buffer.SerialNo = 0;
Err(Error::OperationAborted)
},
Err(err) => Err(Error::WinError(err)),
}
}
}
}
#[cfg(feature = "unstable_xtarget_notification")]
unsafe impl Sync for XRequestNotification {}
#[cfg(feature = "unstable_xtarget_notification")]
unsafe impl Send for XRequestNotification {}
#[cfg(feature = "unstable_xtarget_notification")]
impl fmt::Debug for XRequestNotification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("XRequestNotification")
.field("client", &format_args!("{:?}", self.client))
.field("serial_no", &self.xurn.buffer.SerialNo)
.finish()
}
}
#[cfg(feature = "unstable_xtarget_notification")]
impl Drop for XRequestNotification {
fn drop(&mut self) {
unsafe {
let this = pin::Pin::new_unchecked(self);
if this.xurn.buffer.SerialNo != 0 {
let device = this.client.device;
let xurn = &mut this.get_unchecked_mut().xurn;
let _ = xurn.cancel(device);
}
}
}
}
pub type XTarget = Xbox360Wired<Client>;
pub struct Xbox360Wired<CL: Borrow<Client>> {
client: CL,
event: Event,
serial_no: u32,
id: TargetId,
}
impl<CL: Borrow<Client>> Xbox360Wired<CL> {
#[inline]
pub fn new(client: CL, id: TargetId) -> Xbox360Wired<CL> {
let event = Event::new(false, false);
Xbox360Wired { client, event, serial_no: 0, id }
}
#[inline]
pub fn is_attached(&self) -> bool {
self.serial_no != 0
}
#[inline]
pub fn id(&self) -> TargetId {
self.id
}
#[inline]
pub fn client(&self) -> &CL {
&self.client
}
#[inline]
pub fn drop(mut self) -> CL {
let _ = self.unplug();
unsafe {
let client = (&self.client as *const CL).read();
ptr::drop_in_place(&mut self.event);
mem::forget(self);
client
}
}
#[inline(never)]
pub fn plugin(&mut self) -> Result<(), Error> {
if self.is_attached() {
return Err(Error::AlreadyConnected);
}
let mut plugin = bus::PluginTarget::x360_wired(1, self.id.vendor, self.id.product);
let device = self.client.borrow().device;
while unsafe { plugin.ioctl(device, self.event.handle) }.is_err() {
plugin.SerialNo += 1;
if plugin.SerialNo >= u16::MAX as u32 {
return Err(Error::NoFreeSlot);
}
}
self.serial_no = plugin.SerialNo;
Ok(())
}
#[inline(never)]
pub fn unplug(&mut self) -> Result<(), Error> {
if !self.is_attached() {
return Err(Error::NotPluggedIn);
}
unsafe {
let mut unplug = bus::UnplugTarget::new(self.serial_no);
let device = self.client.borrow().device;
unplug.ioctl(device, self.event.handle)?;
}
self.serial_no = 0;
Ok(())
}
#[inline(never)]
pub fn wait_ready(&mut self) -> Result<(), Error> {
if !self.is_attached() {
return Err(Error::NotPluggedIn);
}
unsafe {
let mut wait = bus::WaitDeviceReady::new(self.serial_no);
let device = self.client.borrow().device;
wait.ioctl(device, self.event.handle)?;
}
Ok(())
}
#[inline(never)]
pub fn get_user_index(&mut self) -> Result<u32, Error> {
if !self.is_attached() {
return Err(Error::NotPluggedIn);
}
let user_index = unsafe {
let mut gui = bus::XUsbGetUserIndex::new(self.serial_no);
let device = self.client.borrow().device;
match gui.ioctl(device, self.event.handle) {
Ok(()) => (),
Err(winerror::ERROR_INVALID_DEVICE_OBJECT_PARAMETER) => return Err(Error::UserIndexOutOfRange),
Err(err) => return Err(Error::WinError(err)),
}
gui.UserIndex
};
Ok(user_index)
}
#[inline(never)]
pub fn update(&mut self, gamepad: &XGamepad) -> Result<(), Error> {
if !self.is_attached() {
return Err(Error::NotPluggedIn);
}
unsafe {
let mut xsr = bus::XUsbSubmitReport::new(self.serial_no, *gamepad);
let device = self.client.borrow().device;
match xsr.ioctl(device, self.event.handle) {
Ok(()) => Ok(()),
Err(winerror::ERROR_DEV_NOT_EXIST) => Err(Error::TargetNotReady),
Err(err) => Err(Error::WinError(err)),
}
}
}
#[cfg(feature = "unstable_xtarget_notification")]
#[inline(never)]
pub fn request_notification(&mut self) -> Result<XRequestNotification, Error> {
if !self.is_attached() {
return Err(Error::NotPluggedIn);
}
let client = self.client.borrow().try_clone()?;
let xurn = bus::RequestNotification::new(
bus::XUsbRequestNotification::new(self.serial_no));
Ok(XRequestNotification { client, xurn, _unpin: marker::PhantomPinned })
}
}
impl<CL: Borrow<Client>> fmt::Debug for Xbox360Wired<CL> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Xbox360Wired")
.field("client", &format_args!("{:?}", self.client.borrow()))
.field("event", &format_args!("{:?}", self.event))
.field("serial_no", &self.serial_no)
.field("vendor_id", &self.id.vendor)
.field("product_id", &self.id.product)
.finish()
}
}
impl<CL: Borrow<Client>> Drop for Xbox360Wired<CL> {
#[inline]
fn drop(&mut self) {
let _ = self.unplug();
}
}