This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathClient.h
658 lines (606 loc) · 20 KB
/
Client.h
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
MIT License
Copyright (c) 2017-2023 Nefarius Software Solutions e.U. and Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef ViGEmClient_h__
#define ViGEmClient_h__
#ifdef __cplusplus
extern "C" {
#endif
#include "ViGEm/Common.h"
#ifdef VIGEM_DYNAMIC
#ifdef VIGEM_EXPORTS
#define VIGEM_API __declspec(dllexport)
#else
#define VIGEM_API __declspec(dllimport)
#endif
#else
#define VIGEM_API
#endif
#if defined(_MSC_VER)
#define VIGEM_DEPRECATED __declspec(deprecated)
#elif defined(__GNUC__)
#define VIGEM_DEPRECATED __attribute__ ((deprecated))
#endif
/** Values that represent ViGEm errors */
typedef enum _VIGEM_ERRORS
{
//
// API succeeded.
//
VIGEM_ERROR_NONE = 0x20000000,
//
// A compatible bus driver wasn't found on the system.
//
VIGEM_ERROR_BUS_NOT_FOUND = 0xE0000001,
//
// All device slots are occupied, no new device can be spawned.
//
VIGEM_ERROR_NO_FREE_SLOT = 0xE0000002,
VIGEM_ERROR_INVALID_TARGET = 0xE0000003,
VIGEM_ERROR_REMOVAL_FAILED = 0xE0000004,
//
// An attempt has been made to plug in an already connected device.
//
VIGEM_ERROR_ALREADY_CONNECTED = 0xE0000005,
//
// The target device is not initialized.
//
VIGEM_ERROR_TARGET_UNINITIALIZED = 0xE0000006,
//
// The target device is not plugged in.
//
VIGEM_ERROR_TARGET_NOT_PLUGGED_IN = 0xE0000007,
//
// It's been attempted to communicate with an incompatible driver version.
//
VIGEM_ERROR_BUS_VERSION_MISMATCH = 0xE0000008,
//
// Bus driver found but failed to open a handle.
//
VIGEM_ERROR_BUS_ACCESS_FAILED = 0xE0000009,
VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED = 0xE0000010,
VIGEM_ERROR_CALLBACK_NOT_FOUND = 0xE0000011,
VIGEM_ERROR_BUS_ALREADY_CONNECTED = 0xE0000012,
VIGEM_ERROR_BUS_INVALID_HANDLE = 0xE0000013,
VIGEM_ERROR_XUSB_USERINDEX_OUT_OF_RANGE = 0xE0000014,
VIGEM_ERROR_INVALID_PARAMETER = 0xE0000015,
//
// The API is not supported by the driver.
//
VIGEM_ERROR_NOT_SUPPORTED = 0xE0000016,
//
// An unexpected Win32 API error occurred. Call GetLastError() for details.
//
VIGEM_ERROR_WINAPI = 0xE0000017,
//
// The specified timeout has been reached.
//
VIGEM_ERROR_TIMED_OUT = 0xE0000018,
VIGEM_ERROR_IS_DISPOSING = 0xE0000019,
} VIGEM_ERROR;
/**
* A macro that defines if the API succeeded
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 01.09.2020
*
* @param _val_ The error value.
*/
#define VIGEM_SUCCESS(_val_) (_val_ == VIGEM_ERROR_NONE)
/** Defines an alias representing a driver connection object */
typedef struct _VIGEM_CLIENT_T *PVIGEM_CLIENT;
/** Defines an alias representing a target device object */
typedef struct _VIGEM_TARGET_T *PVIGEM_TARGET;
typedef _Function_class_(EVT_VIGEM_TARGET_ADD_RESULT)
VOID CALLBACK
EVT_VIGEM_TARGET_ADD_RESULT(
PVIGEM_CLIENT Client,
PVIGEM_TARGET Target,
VIGEM_ERROR Result
);
typedef EVT_VIGEM_TARGET_ADD_RESULT *PFN_VIGEM_TARGET_ADD_RESULT;
typedef _Function_class_(EVT_VIGEM_X360_NOTIFICATION)
VOID CALLBACK
EVT_VIGEM_X360_NOTIFICATION(
PVIGEM_CLIENT Client,
PVIGEM_TARGET Target,
UCHAR LargeMotor,
UCHAR SmallMotor,
UCHAR LedNumber,
LPVOID UserData
);
typedef EVT_VIGEM_X360_NOTIFICATION *PFN_VIGEM_X360_NOTIFICATION;
typedef _Function_class_(EVT_VIGEM_DS4_NOTIFICATION)
VOID CALLBACK
EVT_VIGEM_DS4_NOTIFICATION(
PVIGEM_CLIENT Client,
PVIGEM_TARGET Target,
UCHAR LargeMotor,
UCHAR SmallMotor,
DS4_LIGHTBAR_COLOR LightbarColor,
LPVOID UserData
);
typedef EVT_VIGEM_DS4_NOTIFICATION *PFN_VIGEM_DS4_NOTIFICATION;
/**
* Allocates an object representing a driver connection
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @returns A PVIGEM_CLIENT object.
*/
VIGEM_API PVIGEM_CLIENT vigem_alloc(void);
/**
* Frees up memory used by the driver connection object
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @param vigem The PVIGEM_CLIENT object.
*/
VIGEM_API void vigem_free(
PVIGEM_CLIENT vigem
);
/**
* Initializes the driver object and establishes a connection to the emulation bus
* driver. Returns an error if no compatible bus device has been found.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @param vigem The PVIGEM_CLIENT object.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_connect(
PVIGEM_CLIENT vigem
);
/**
* Disconnects from the bus device and resets the driver object state. The driver object
* may be reused again after calling this function. When called, all targets which may
* still be connected will be destroyed automatically. Be aware, that allocated target
* objects won't be automatically freed, this has to be taken care of by the caller.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @param vigem The PVIGEM_CLIENT object.
*/
VIGEM_API void vigem_disconnect(
PVIGEM_CLIENT vigem
);
/**
* A useful utility function to check if pre 1.17 driver, meant to be replaced in the future by
* more robust version checks, only able to be checked after at least one device has been
* successfully plugged in.
*
* @author Jason "megadrago88" Hart
* @date 17.08.2021
*
* @param target The PVIGEM_TARGET to check against.
*
* @returns A BOOLEAN, true if the device wait ready ioctl is supported (1.17+) or false if not ( =< 1.16)
*/
VIGEM_API BOOLEAN vigem_target_is_waitable_add_supported(
PVIGEM_TARGET target
);
/**
* Allocates an object representing an Xbox 360 Controller device.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @returns A PVIGEM_TARGET representing an Xbox 360 Controller device.
*/
VIGEM_API PVIGEM_TARGET vigem_target_x360_alloc(void);
/**
* Allocates an object representing a DualShock 4 Controller device.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @returns A PVIGEM_TARGET representing a DualShock 4 Controller device.
*/
VIGEM_API PVIGEM_TARGET vigem_target_ds4_alloc(void);
/**
* Frees up memory used by the target device object. This does not automatically remove
* the associated device from the bus, if present. If the target device doesn't get
* removed before this call, the device becomes orphaned until the owning process is
* terminated.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @param target The target device object.
*/
VIGEM_API void vigem_target_free(
PVIGEM_TARGET target
);
/**
* Adds a provided target device to the bus driver, which is equal to a device plug-in
* event of a physical hardware device. This function blocks until the target device is
* in full operational mode.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_add(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target
);
/**
* Adds a provided target device to the bus driver, which is equal to a device plug-in
* event of a physical hardware device. This function immediately returns. An optional
* callback may be registered which gets called on error or if the target device has
* become fully operational.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param result An optional function getting called when the target device becomes available.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_add_async(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
PFN_VIGEM_TARGET_ADD_RESULT result
);
/**
* Removes a provided target device from the bus driver, which is equal to a device
* unplug event of a physical hardware device. The target device object may be reused
* after this function is called. If this function is never called on target device
* objects, they will be removed from the bus when the owning process terminates.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_remove(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target
);
/**
* Registers a function which gets called, when LED index or vibration state changes
* occur on the provided target device. This function fails if the provided
* target device isn't fully operational or in an erroneous state.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param notification The notification callback.
* @param userData The user data passed to the notification callback.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_x360_register_notification(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
PFN_VIGEM_X360_NOTIFICATION notification,
LPVOID userData
);
/**
* This function is deprecated, use vigem_target_ds4_await_output_report or
* vigem_target_ds4_await_output_report_timeout instead. Registers a function which gets called,
* when LightBar or vibration state changes occur on the provided target device. This function
* fails if the provided target device isn't fully operational or in an erroneous state.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param notification The notification callback.
* @param userData The user data passed to the notification callback.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_DEPRECATED VIGEM_ERROR vigem_target_ds4_register_notification(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
PFN_VIGEM_DS4_NOTIFICATION notification,
LPVOID userData
);
/**
* Removes a previously registered callback function from the provided target object.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
*/
VIGEM_API void vigem_target_x360_unregister_notification(
PVIGEM_TARGET target
);
/**
* Removes a previously registered callback function from the provided target object.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
*/
VIGEM_API VIGEM_DEPRECATED void vigem_target_ds4_unregister_notification(
PVIGEM_TARGET target
);
/**
* Overrides the default Vendor ID value with the provided one.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
* @param vid The Vendor ID to set.
*/
VIGEM_API void vigem_target_set_vid(
PVIGEM_TARGET target,
USHORT vid
);
/**
* Overrides the default Product ID value with the provided one.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
* @param pid The Product ID to set.
*/
VIGEM_API void vigem_target_set_pid(
PVIGEM_TARGET target,
USHORT pid
);
/**
* Returns the Vendor ID of the provided target device object.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
*
* @returns The Vendor ID.
*/
VIGEM_API USHORT vigem_target_get_vid(
PVIGEM_TARGET target
);
/**
* Returns the Product ID of the provided target device object.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
*
* @returns The Product ID.
*/
VIGEM_API USHORT vigem_target_get_pid(
PVIGEM_TARGET target
);
/**
* Sends a state report to the provided target device.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param report The report to send to the target device.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_x360_update(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
XUSB_REPORT report
);
/**
* DEPRECATED. Sends a state report to the provided target device. It's recommended to use
* vigem_target_ds4_update_ex instead to utilize all DS4 features like touch, gyro etc.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param report The report to send to the target device.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_ds4_update(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
DS4_REPORT report
);
/**
* Sends a full size state report to the provided target device. It's recommended to use this
* function over vigem_target_ds4_update.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 07.09.2020
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param report The report buffer.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_ds4_update_ex(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
DS4_REPORT_EX report
);
/**
* Returns the internal index (serial number) the bus driver assigned to the provided
* target device object. Note that this value is specific to the inner workings of
* the bus driver, it does not reflect related values like player index or device
* arrival order experienced by other APIs. It may be used to identify the target
* device object for its lifetime. This value becomes invalid once the target
* device is removed from the bus and may change on the next addition of the
* device.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
*
* @returns The internally used index of the target device.
*/
VIGEM_API ULONG vigem_target_get_index(
PVIGEM_TARGET target
);
/**
* Returns the type of the provided target device object.
*
* @author Benjamin "Nefarius" Höglinger
* @date 28.08.2017
*
* @param target The target device object.
*
* @returns A VIGEM_TARGET_TYPE.
*/
VIGEM_API VIGEM_TARGET_TYPE vigem_target_get_type(
PVIGEM_TARGET target
);
/**
* Returns TRUE if the provided target device object is currently attached to the bus,
* FALSE otherwise.
*
* @author Benjamin "Nefarius" Höglinger
* @date 30.08.2017
*
* @param target The target device object.
*
* @returns TRUE if device is attached to the bus, FALSE otherwise.
*/
VIGEM_API BOOL vigem_target_is_attached(
PVIGEM_TARGET target
);
/**
* Returns the user index of the emulated Xenon device. This value correspondents to the
* (zero-based) index number representing the player number via LED present on a
* physical controller and is compatible to the dwUserIndex property of the
* XInput* APIs.
*
* @author Benjamin "Nefarius" Höglinger
* @date 10.05.2018
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param index The (zero-based) user index of the Xenon device.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_x360_get_user_index(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
PULONG index
);
/**
* Waits until there's one or more pending raw output reports available to consume. This
* function blocks until data becomes available or the device gets disconnected. The waiting is
* event-based, meaning that as soon as a data packet is pending, this call returns a copy of
* the entire buffer. Each call returns a packet in the exact order it arrived in the driver. It
* is recommended to repeatedly call this function in a thread. The call aborts with an error
* code if the target gets unplugged in parallel.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 06.08.2022
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param buffer The fixed-size 64-bytes output report buffer that gets written to.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_ds4_await_output_report(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
PDS4_OUTPUT_BUFFER buffer
);
/**
* Waits until there's one or more pending raw output reports available to consume. This
* function blocks until data becomes available, the provided timeout has been reached or the
* device gets disconnected. The waiting is event-based, meaning that as soon as a data packet
* is pending, this call returns a copy of the entire buffer. Each call returns a packet in the
* exact order it arrived in the driver. It is recommended to repeatedly call this function in a
* thread. A timeout of a few hundred milliseconds can be used to break out of the loop without
* excessive CPU consumption. The call aborts with an error code if the target gets unplugged in
* parallel. If a timeout of INFINITE is specified, the function basically behaves identical to
* vigem_target_ds4_await_output_report.
*
* @author Benjamin "Nefarius" Höglinger-Stelzer
* @date 12.08.2022
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param milliseconds The timeout in milliseconds.
* @param buffer The fixed-size 64-bytes output report buffer that gets written to.
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_ds4_await_output_report_timeout(
PVIGEM_CLIENT vigem,
PVIGEM_TARGET target,
DWORD milliseconds,
PDS4_OUTPUT_BUFFER buffer
);
/**
* Returns the output data of the Xbox gamepad. Output refers to the USB output report, which
* is used to set LEDs and motor values.
*
* @author Matt Wszolek
* @date 09.28.2021
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param output The values that are set by the output reports
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_x360_get_output(PVIGEM_CLIENT vigem, PVIGEM_TARGET target, PXUSB_OUTPUT_DATA output);
/**
* Returns the output data of the DualShock 4 gamepad. Output refers to the USB output report,
* which is used to set LED and motor values.
*
* @author Matt Wszolek
* @date 09.28.2021
*
* @param vigem The driver connection object.
* @param target The target device object.
* @param output The values that are set by the output reports
*
* @returns A VIGEM_ERROR.
*/
VIGEM_API VIGEM_ERROR vigem_target_ds4_get_output(PVIGEM_CLIENT vigem, PVIGEM_TARGET target, PDS4_OUTPUT_DATA output);
#ifdef __cplusplus
}
#endif
#endif // ViGEmClient_h__