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
use std::{fmt, mem};
use std::borrow::Borrow;
use winapi::um::xinput::XINPUT_GAMEPAD;
use winapi::shared::winerror;
use crate::*;

/// XInput compatible button flags.
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct XButtons {
	pub raw: u16,
}

/// XInput compatible button flags.
#[allow(non_snake_case)]
#[inline]
pub const fn XButtons(raw: u16) -> XButtons {
	XButtons { raw }
}

/// XInput compatible button flags.
///
/// ```
/// let buttons = vigem_client::XButtons!(UP|RIGHT|LB|A|X);
/// assert_eq!(buttons, vigem_client::XButtons(0x5109));
/// ```
#[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 {
	/// Dpad up button.
	pub const UP: u16     = 0x0001;
	/// Dpad down button.
	pub const DOWN: u16   = 0x0002;
	/// Dpad left button.
	pub const LEFT: u16   = 0x0004;
	/// Dpad right button.
	pub const RIGHT: u16  = 0x0008;
	/// Start button.
	pub const START: u16  = 0x0010;
	/// Back button.
	pub const BACK: u16   = 0x0020;
	/// Left thumb button.
	pub const LTHUMB: u16 = 0x0040;
	/// Right thumb button.
	pub const RTHUMB: u16 = 0x0080;
	/// Left shoulder button.
	pub const LB: u16     = 0x0100;
	/// Right shoulder button.
	pub const RB: u16     = 0x0200;
	/// Xbox guide button.
	pub const GUIDE: u16  = 0x0400;
	/// A button.
	pub const A: u16      = 0x1000;
	/// B button.
	pub const B: u16      = 0x2000;
	/// X button.
	pub const X: u16      = 0x4000;
	/// Y button.
	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)
		}
	}
}

/// Represents an [`XINPUT_GAMEPAD`]-compatible report structure.
///
/// ![image](https://user-images.githubusercontent.com/2324759/124391245-f889b180-dcef-11eb-927c-4b76d2ca332d.png)
#[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) }
	}
}

/// A virtual Microsoft Xbox 360 Controller (wired).
pub struct Xbox360Wired<CL: Borrow<Client>> {
	client: CL,
	event: Event,
	serial_no: u32,
	id: TargetId,
}

impl<CL: Borrow<Client>> Xbox360Wired<CL> {
	/// Creates a new instance.
	#[inline]
	pub fn new(client: CL, id: TargetId) -> Xbox360Wired<CL> {
		let event = Event::new(false, false);
		Xbox360Wired { client, event, serial_no: 0, id }
	}

	/// Returns if the controller is plugged in.
	#[inline]
	pub fn is_attached(&self) -> bool {
		self.serial_no != 0
	}

	/// Returns the vendor and product ids.
	#[inline]
	pub fn id(&self) -> TargetId {
		self.id
	}

	/// Returns the client.
	#[inline]
	pub fn client(&self) -> &CL {
		&self.client
	}

	/// Unplugs and destroys the controller, returning the client.
	#[inline]
	pub fn drop(mut self) -> CL {
		let _ = self.unplug();

		unsafe {
			let client = (&self.client as *const CL).read();
			mem::forget(self);
			client
		}
	}

	/// Plugs the controller in.
	#[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;

		// Yes this is how the driver is implemented
		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(())
	}

	/// Unplugs the controller.
	#[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(())
	}

	/// Waits until the virtual controller is ready.
	///
	/// Any updates submitted before the virtual controller is ready may return an error.
	#[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(())
	}

	/// Gets the user index of the device in XInput.
	#[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_ACCESS_DENIED) => return Err(Error::InvalidTarget),
				Err(winerror::ERROR_INVALID_DEVICE_OBJECT_PARAMETER) => return Err(Error::UserIndexOutOfRange),
				Err(err) => return Err(Error::WinError(err)),
			}

			gui.UserIndex
		};

		Ok(user_index)
	}

	/// Updates the virtual controller state.
	#[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)),
			}
		}
	}
}

impl<CL: Borrow<Client>> fmt::Debug for Xbox360Wired<CL> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("Xbox360Wired")
			.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();
	}
}