vigem-client 0.1.4

ViGEm client API in pure Rust.
Documentation
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
#![allow(non_snake_case)]

use std::{mem, ptr};
use winapi::um::handleapi::*;
use winapi::um::ioapiset::*;
use winapi::um::minwinbase::*;
use winapi::um::synchapi::*;
use winapi::um::errhandlingapi::*;
use winapi::shared::winerror;
use winapi::shared::ntdef::HANDLE;
use winapi::shared::guiddef::GUID;

pub static GUID_DEVINTERFACE: GUID = GUID {
	Data1: 0x96E42B22, Data2: 0xF5E9, Data3: 0x42F8,
	Data4: [0xB0, 0x43, 0xED, 0x0F, 0x93, 0x2F, 0x01, 0x4F],
};

// IO control codes
// const IOCTL_BASE: u32 = 0x801;
pub const IOCTL_PLUGIN_TARGET: u32 = 0x2AA004; //IOCTL_BASE + 0x000;
pub const IOCTL_UNPLUG_TARGET: u32 = 0x2AA008; //IOCTL_BASE + 0x001;
pub const IOCTL_CHECK_VERSION: u32 = 0x2AA00C; //IOCTL_BASE + 0x002;
pub const IOCTL_WAIT_DEVICE_READY: u32 = 0x2AA010; //IOCTL_BASE + 0x003;
#[cfg(feature = "unstable_xtarget_notification")]
pub const IOCTL_XUSB_REQUEST_NOTIFICATION : u32 = 0x2AE804; //IOCTL_BASE + 0x200 (RW);
pub const IOCTL_XUSB_SUBMIT_REPORT: u32 = 0x2AA808; //IOCTL_BASE + 0x201;
#[cfg(feature = "unstable_ds4")]
pub const IOCTL_DS4_SUBMIT_REPORT: u32 = 0x2AA80C; //IOCTL_BASE + 0x202;
pub const IOCTL_XUSB_GET_USER_INDEX: u32 = 0x2AE81C; //IOCTL_BASE + 0x206;

#[repr(C)]
pub struct CheckVersion {
	pub Size: u32,
	pub Version: u32,
}
impl CheckVersion {
	pub const COMMON: u32 = 0x0001;
	#[inline]
	pub const fn common() -> CheckVersion {
		CheckVersion {
			Size: mem::size_of::<CheckVersion>() as u32,
			Version: Self::COMMON,
		}
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE) -> bool {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = CreateEventW(ptr::null_mut(), 0, 0, ptr::null());

		DeviceIoControl(
			device,
			IOCTL_CHECK_VERSION,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			ptr::null_mut(),
			0,
			&mut transferred,
			&mut overlapped);

		let success = GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1);
		CloseHandle(overlapped.hEvent);
		return success != 0;
	}
}

pub const TARGET_TYPE_XBOX360_WIRED: i32 = 0;
pub const TARGET_TYPE_DUALSHOCK4_WIRED: i32 = 2;

#[repr(C)]
pub struct PluginTarget {
	pub Size: u32,
	pub SerialNo: u32,
	pub TargetType: i32,
	pub VendorId: u16,
	pub ProductId: u16,
}
impl PluginTarget {
	#[inline]
	pub const fn new(serial_no: u32, target_type: i32, vendor_id: u16, product_id: u16) -> PluginTarget {
		PluginTarget {
			Size: mem::size_of::<PluginTarget>() as u32,
			SerialNo: serial_no,
			TargetType: target_type,
			VendorId: vendor_id,
			ProductId: product_id,
		}
	}
	#[inline]
	pub const fn x360_wired(serial_no: u32, vendor_id: u16, product_id: u16) -> PluginTarget {
		PluginTarget::new(serial_no, TARGET_TYPE_XBOX360_WIRED, vendor_id, product_id)
	}
	#[inline]
	pub const fn ds4_wired(serial_no: u32, vendor_id: u16, product_id: u16) -> PluginTarget {
		PluginTarget::new(serial_no, TARGET_TYPE_DUALSHOCK4_WIRED, vendor_id, product_id)
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE, event: HANDLE) -> Result<(), u32> {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = event;

		DeviceIoControl(
			device,
			IOCTL_PLUGIN_TARGET,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			ptr::null_mut(),
			0,
			&mut transferred,
			&mut overlapped);

		if GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1) == 0 {
			return Err(GetLastError());
		}

		Ok(())
	}
}

#[repr(C)]
pub struct WaitDeviceReady {
	pub Size: u32,
	pub SerialNo: u32,
}
impl WaitDeviceReady {
	#[inline]
	pub const fn new(serial_no: u32) -> WaitDeviceReady {
		WaitDeviceReady {
			Size: mem::size_of::<WaitDeviceReady>() as u32,
			SerialNo: serial_no,
		}
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE, event: HANDLE) -> Result<(), u32> {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = event;

		DeviceIoControl(
			device,
			IOCTL_WAIT_DEVICE_READY,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			ptr::null_mut(),
			0,
			&mut transferred,
			&mut overlapped);

		if GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1) == 0 {
			let err = GetLastError();
			// Version pre-1.17 where this IOCTL doesn't exist
			if err != winerror::ERROR_INVALID_PARAMETER {
				return Err(err);
			}
		}

		Ok(())
	}
}

#[repr(C)]
pub struct UnplugTarget {
	pub Size: u32,
	pub SerialNo: u32,
}
impl UnplugTarget {
	#[inline]
	pub const fn new(serial_no: u32) -> UnplugTarget {
		UnplugTarget {
			Size: mem::size_of::<UnplugTarget>() as u32,
			SerialNo: serial_no,
		}
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE, event: HANDLE) -> Result<(), u32> {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = event;

		DeviceIoControl(
			device,
			IOCTL_UNPLUG_TARGET,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			ptr::null_mut(),
			0,
			&mut transferred,
			&mut overlapped);

		if GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1) == 0 {
			return Err(GetLastError());
		}

		Ok(())
	}
}

#[repr(C)]
pub struct XUsbSubmitReport {
	pub Size: u32,
	pub SerialNo: u32,
	pub Report: crate::XGamepad,
}
impl XUsbSubmitReport {
	#[inline]
	pub const fn new(serial_no: u32, report: crate::XGamepad) -> XUsbSubmitReport {
		XUsbSubmitReport {
			Size: mem::size_of::<XUsbSubmitReport>() as u32,
			SerialNo: serial_no,
			Report: report,
		}
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE, event: HANDLE) -> Result<(), u32> {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = event;

		DeviceIoControl(
			device,
			IOCTL_XUSB_SUBMIT_REPORT,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			ptr::null_mut(),
			0,
			&mut transferred,
			&mut overlapped);

		if GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1) == 0 {
			return Err(GetLastError());
		}

		Ok(())
	}
}

#[cfg(feature = "unstable_xtarget_notification")]
#[repr(C)]
pub struct XUsbRequestNotification {
	pub Size: u32,
	pub SerialNo: u32,
	pub LargeMotor: u8,
	pub SmallMotor: u8,
	pub LedNumber: u8,
}

#[cfg(feature = "unstable_xtarget_notification")]
impl XUsbRequestNotification {
	#[inline]
	pub const fn new(serial_no: u32) -> XUsbRequestNotification {
		XUsbRequestNotification {
			Size: mem::size_of::<XUsbRequestNotification>() as u32,
			SerialNo: serial_no,
			LargeMotor: 0,
			SmallMotor: 0,
			LedNumber: 0,
		}
	}
}

#[cfg(feature = "unstable_xtarget_notification")]
#[repr(C)]
pub struct RequestNotification<T> {
	pub overlapped: OVERLAPPED,
	pub buffer: T,
}
// Safety: This instance must have a stable address (eg. on the heap)
// Required for non-blocking DeviceIoControl, see msdn.
#[cfg(feature = "unstable_xtarget_notification")]
impl<T> RequestNotification<T> {
	#[inline]
	pub fn new(buffer: T) -> RequestNotification<T> {
		let mut overlapped: OVERLAPPED = unsafe { mem::zeroed() };
		overlapped.hEvent = unsafe { CreateEventW(ptr::null_mut(), 0, 0, ptr::null()) };
		RequestNotification { overlapped, buffer }
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE) {
		let mut transferred = 0;

		let buffer_ptr = &mut self.buffer as *mut _ as _;
		let buffer_size = mem::size_of::<T>() as u32;

		DeviceIoControl(
			device,
			IOCTL_XUSB_REQUEST_NOTIFICATION,
			buffer_ptr,
			buffer_size,
			buffer_ptr,
			buffer_size,
			&mut transferred,
			&mut self.overlapped);
	}
	#[inline]
	pub unsafe fn cancel(&mut self, device: HANDLE) -> Result<(), u32> {
		if CancelIoEx(device, &mut self.overlapped) == 0 {
			let err = GetLastError();
			// If no pending IO then everything is fine
			if err == winerror::ERROR_NOT_FOUND {
				return Ok(());
			}
			return Err(err);
		}
		let mut transferred = 0;
		if GetOverlappedResult(device, &mut self.overlapped, &mut transferred, /*bWait: */1) == 0 {
			let err = GetLastError();
			// Expect the operation to be aborted
			if err != winerror::ERROR_OPERATION_ABORTED {
				return Err(err);
			}
		}
		Ok(())
	}
	#[inline]
	pub unsafe fn poll(&mut self, device: HANDLE, wait: bool) -> Result<(), u32> {
		let mut transferred = 0;
		if GetOverlappedResult(device, &mut self.overlapped, &mut transferred, wait as i32) == 0 {
			return Err(GetLastError());
		}
		Ok(())
	}
}
#[cfg(feature = "unstable_xtarget_notification")]
impl<T> Drop for RequestNotification<T> {
	fn drop(&mut self) {
		unsafe { CloseHandle(self.overlapped.hEvent); }
	}
}

#[cfg(feature = "unstable_ds4")]
#[repr(C)]
pub struct DS4SubmitReport {
	pub Size: u32,
	pub SerialNo: u32,
	pub Report: crate::DS4Report,
}
#[cfg(feature = "unstable_ds4")]
impl DS4SubmitReport {
	#[inline]
	pub const fn new(serial_no: u32, report: crate::DS4Report) -> DS4SubmitReport {
		DS4SubmitReport {
			Size: mem::size_of::<DS4SubmitReport>() as u32,
			SerialNo: serial_no,
			Report: report,
		}
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE, event: HANDLE) -> Result<(), u32> {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = event;

		DeviceIoControl(
			device,
			IOCTL_DS4_SUBMIT_REPORT,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			ptr::null_mut(),
			0,
			&mut transferred,
			&mut overlapped);

		if GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1) == 0 {
			return Err(GetLastError());
		}

		Ok(())
	}
}

#[repr(C)]
pub struct XUsbGetUserIndex {
	pub Size: u32,
	pub SerialNo: u32,
	pub UserIndex: u32,
}
impl XUsbGetUserIndex {
	#[inline]
	pub const fn new(serial_no: u32) -> XUsbGetUserIndex {
		XUsbGetUserIndex {
			Size: mem::size_of::<XUsbGetUserIndex>() as u32,
			SerialNo: serial_no,
			UserIndex: 0,
		}
	}
	#[inline]
	pub unsafe fn ioctl(&mut self, device: HANDLE, event: HANDLE) -> Result<(), u32> {
		let mut transferred = 0;
		let mut overlapped: OVERLAPPED = mem::zeroed();
		overlapped.hEvent = event;

		DeviceIoControl(
			device,
			IOCTL_XUSB_GET_USER_INDEX,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			self as *mut _ as _,
			mem::size_of_val(self) as u32,
			&mut transferred,
			&mut overlapped);

		if GetOverlappedResult(device, &mut overlapped, &mut transferred, /*bWait: */1) == 0 {
			return Err(GetLastError());
		}

		Ok(())
	}
}