-
Notifications
You must be signed in to change notification settings - Fork 0
/
dualshock4.cpp
651 lines (561 loc) · 26.1 KB
/
dualshock4.cpp
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
#include "pch.h"
enum Direction : uint8_t {
North = 0,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
NorthWest,
Neutral = 8
};
enum PowerState : uint8_t {
Discharging = 0x00, // Use PowerPercent
Charging = 0x01, // Use PowerPercent
Complete = 0x02, // PowerPercent not valid? assume 100%?
AbnormalVoltage = 0x0A, // PowerPercent not valid?
AbnormalTemperature = 0x0B, // PowerPercent not valid?
ChargingError = 0x0F // PowerPercent not valid?
};
enum LightBrightness : uint8_t {
Bright = 0,
Mid,
Dim,
NoLightAction3,
NoLightAction4,
NoLightAction5,
NoLightAction6,
NoLightAction7 = 7
};
enum LightFadeAnimation : uint8_t {
Nothing = 0,
FadeIn, // from black to blue
FadeOut // from blue to black
};
struct TouchFingerData { // 4
/*0.0*/ uint32_t Index : 7;
/*0.7*/ uint32_t NotTouching : 1;
/*1.0*/ uint32_t FingerX : 12;
/*2.4*/ uint32_t FingerY : 12;
};
struct TouchData { // 9
/*0*/ TouchFingerData Finger [2];
/*8*/ uint8_t Timestamp;
};
#pragma pack(push,1)
template <int N> struct BTCRC {
uint8_t Buff [N-4];
uint32_t CRC;
};
#pragma pack(pop)
#pragma pack(push,1)
template <int N> struct BTAudio {
uint16_t FrameNumber;
uint8_t AudioTarget; // 0x02 speaker?, 0x24 headset?, 0x03 mic?
uint8_t SBCData [N-3];
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_SetStateData // 47
{
uint8_t EnableRumbleUpdate : 1;
uint8_t EnableLedUpdate : 1;
uint8_t EnableLedBlink : 1;
uint8_t EnableExtWrite : 1;
uint8_t EnableVolumeLeftUpdate : 1;
uint8_t EnableVolumeRightUpdate : 1;
uint8_t EnableVolumeMicUpdate : 1;
uint8_t EnableVolumeSpeakerUpdate : 1;
uint8_t UNK_RESET1 : 1; // unknown reset, both set high by Remote Play
uint8_t UNK_RESET2 : 1; // unknown reset, both set high by Remote Play
uint8_t UNK1 : 1;
uint8_t UNK2 : 1;
uint8_t UNK3 : 1;
uint8_t UNKPad : 3;
uint8_t Empty1;
uint8_t RumbleRight; // weak
uint8_t RumbleLeft; // strong
uint8_t LedRed;
uint8_t LedGreen;
uint8_t LedBlue;
uint8_t LedFlashOnPeriod;
uint8_t LedFlashOffPeriod;
uint8_t ExtDataSend [8]; // sent to I2C EXT port, stored in 8x8 byte block
uint8_t VolumeLeft; // 0x00 - 0x4F inclusive
uint8_t VolumeRight; // 0x00 - 0x4F inclusive
uint8_t VolumeMic; // 0x00, 0x01 - 0x40 inclusive (0x00 is special behavior)
uint8_t VolumeSpeaker; // 0x00 - 0x4F
uint8_t UNK_AUDIO1 : 7; // clamped to 1-64 inclusive, appears to be set to 5 for audio
uint8_t UNK_AUDIO2 : 1; // unknown, appears to be set to 1 for audio
uint8_t Pad [8];
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_SetStateDataBt // 74
{
uint8_t EnableRumbleUpdate : 1;
uint8_t EnableLedUpdate : 1;
uint8_t EnableLedBlink : 1;
uint8_t EnableExtWrite : 1;
uint8_t EnableVolumeLeftUpdate : 1;
uint8_t EnableVolumeRightUpdate : 1;
uint8_t EnableVolumeMicUpdate : 1;
uint8_t EnableVolumeSpeakerUpdate : 1;
uint8_t UNK_RESET1 : 1; // unknown reset, both set high by Remote Play
uint8_t UNK_RESET2 : 1; // unknown reset, both set high by Remote Play
uint8_t UNK1 : 1;
uint8_t UNK2 : 1;
uint8_t UNK3 : 1;
uint8_t UNKPad : 3;
uint8_t Empty1;
uint8_t RumbleRight; // Weak
uint8_t RumbleLeft; // Strong
uint8_t LedRed;
uint8_t LedGreen;
uint8_t LedBlue;
uint8_t LedFlashOnPeriod;
uint8_t LedFlashOffPeriod;
uint8_t ExtDataSend [8]; // sent to I2C EXT port, stored in 8x8 byte block
uint8_t VolumeLeft; // 0x00 - 0x4F inclusive
uint8_t VolumeRight; // 0x00 - 0x4F inclusive
uint8_t VolumeMic; // 0x00, 0x01 - 0x40 inclusive (0x00 is special behavior)
uint8_t VolumeSpeaker; // 0x00 - 0x4F
uint8_t UNK_AUDIO1 : 7; // clamped to 1-64 inclusive, appears to be set to 5 for audio
uint8_t UNK_AUDIO2 : 1; // unknown, appears to be set to 1 for audio
uint8_t Pad [52];
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_ReportOut11 // 78
{
union {
BTCRC <78> CRC;
struct {
uint8_t ReportID; // 0x11
uint8_t PollingRate : 6; // note 0 appears to be clamped to 1
uint8_t EnableCRC : 1;
uint8_t EnableHID : 1;
uint8_t EnableMic : 3; // somehow enables mic, appears to be 3 bit flags
uint8_t UnkA4 : 1;
uint8_t UnkB1 : 1;
uint8_t UnkB2 : 1; // seems to always be 1
uint8_t UnkB3 : 1;
uint8_t EnableAudio : 1;
union {
SK_HID_DualShock4_SetStateDataBt State;
BTAudio <75> Audio;
};
} Data;
};
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_BasicGetStateData {
/*0 */ uint8_t LeftStickX;
/*1 */ uint8_t LeftStickY;
/*2 */ uint8_t RightStickX;
/*3 */ uint8_t RightStickY;
/*4.0*/ Direction DPad : 4;
/*4.4*/ uint8_t ButtonSquare : 1;
/*4.5*/ uint8_t ButtonCross : 1;
/*4.6*/ uint8_t ButtonCircle : 1;
/*4.7*/ uint8_t ButtonTriangle : 1;
/*5.0*/ uint8_t ButtonL1 : 1;
/*5.1*/ uint8_t ButtonR1 : 1;
/*5.2*/ uint8_t ButtonL2 : 1;
/*5.3*/ uint8_t ButtonR2 : 1;
/*5.4*/ uint8_t ButtonShare : 1;
/*5.5*/ uint8_t ButtonOptions : 1;
/*5.6*/ uint8_t ButtonL3 : 1;
/*5.7*/ uint8_t ButtonR3 : 1;
/*6.0*/ uint8_t ButtonHome : 1;
/*6.1*/ uint8_t ButtonPad : 1;
/*6.2*/ uint8_t Counter : 6; // always 0 on USB, counts up with some skips on BT
/*7 */ uint8_t TriggerLeft;
/*8 */ uint8_t TriggerRight;
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_GetStateData : SK_HID_DualShock4_BasicGetStateData {
/* 9 */ uint16_t Timestamp; // in 5.33us units?
/*11 */ uint8_t Temperature;
/*12 */ int16_t AngularVelocityX;
/*14 */ int16_t AngularVelocityZ;
/*16 */ int16_t AngularVelocityY;
/*18 */ int16_t AccelerometerX;
/*20 */ int16_t AccelerometerY;
/*22 */ int16_t AccelerometerZ;
/*24 */ uint8_t ExtData [5]; // range can be set by EXT device
/*29 */ uint8_t PowerPercent : 4; // 0x00-0x0A or 0x01-0x0B if plugged int
/*29.4*/ uint8_t PluggedPowerCable : 1;
/*29.5*/ uint8_t PluggedHeadphones : 1;
/*29.6*/ uint8_t PluggedMic : 1;
/*29,7*/ uint8_t PluggedExt : 1;
/*30.0*/ uint8_t UnkExt1 : 1; // ExtCapableOfExtraData?
/*30.1*/ uint8_t UnkExt2 : 1; // ExtHasExtraData?
/*30.2*/ uint8_t NotConnected : 1; // Used by dongle to indicate no controller
/*30.3*/ uint8_t Unk1 : 5;
/*31 */ uint8_t Unk2; // unused?
/*32 */ uint8_t TouchCount;
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_GetStateDataUSB : SK_HID_DualShock4_GetStateData {
TouchData TouchData [3];
uint8_t Pad [3];
};
#pragma pack(pop)
#pragma pack(push,1)
struct SK_HID_DualShock4_GetStateDataBt : SK_HID_DualShock4_GetStateData {
TouchData TouchData [4];
uint8_t Pad [6];
};
#pragma pack(pop)
bool
SK_DualShock4_GetInputReportUSB (void *pGenericDev)
{
hid_device_file_s* pDevice =
(hid_device_file_s *)pGenericDev;
ZeroMemory ( pDevice->input_report.data (),
pDevice->input_report.size () );
BYTE* report = pDevice->input_report.data ();
// HID Input Report 0x1 (USB)
report [0] = 0x1;
DWORD dwBytesRead = 0;
bool bNewData = false;
if (ReadFile (pDevice->hDeviceFile, report, (DWORD)pDevice->input_report.size (), &dwBytesRead, nullptr))
{
SK_HID_DualShock4_GetStateDataUSB *pData =
(SK_HID_DualShock4_GetStateDataUSB *)&report [1];
pDevice->state.current.Gamepad = { };
pDevice->state.current.Gamepad.bLeftTrigger =
static_cast <BYTE> (pData->TriggerLeft);
pDevice->state.current.Gamepad.bRightTrigger =
static_cast <BYTE> (pData->TriggerRight);
pDevice->state.current.Gamepad.sThumbLX =
static_cast <SHORT> (32767.0 * static_cast <double> ( static_cast <LONG> (pData->LeftStickX) - 128) / 128.0);
pDevice->state.current.Gamepad.sThumbLY =
static_cast <SHORT> (32767.0 * static_cast <double> (128 - static_cast <LONG> (pData->LeftStickY) ) / 128.0);
pDevice->state.current.Gamepad.sThumbRX =
static_cast <SHORT> (32767.0 * static_cast <double> ( static_cast <LONG> (pData->RightStickX) - 128) / 128.0);
pDevice->state.current.Gamepad.sThumbRY =
static_cast <SHORT> (32767.0 * static_cast <double> (128 - static_cast <LONG> (pData->RightStickY) ) / 128.0);
switch (pData->DPad)
{
case North: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
case NorthEast: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
case East: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
case SouthEast: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
case South: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
case SouthWest: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
case West: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
case NorthWest: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
case Neutral:
default:
// Centered value, do nothing
break;
}
if (pData->ButtonSquare != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_X;
if (pData->ButtonCross != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_A;
if (pData->ButtonCircle != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_B;
if (pData->ButtonTriangle != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_Y;
if (pData->ButtonL1 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER;
if (pData->ButtonR1 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER;
if (pData->ButtonL2 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_TRIGGER;
if (pData->ButtonR2 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_TRIGGER;
if (pData->ButtonL3 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB;
if (pData->ButtonR3 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB;
if (pData->ButtonShare != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK;
if (pData->ButtonOptions != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_START;
if (pData->ButtonHome != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE;
float LX = pDevice->state.current.Gamepad.sThumbLX;
float LY = pDevice->state.current.Gamepad.sThumbLY;
float normL = sqrtf ( LX*LX + LY*LY );
float RX = pDevice->state.current.Gamepad.sThumbRX;
float RY = pDevice->state.current.Gamepad.sThumbRY;
float normR = sqrtf ( RX*RX + RY*RY );
if (! SK_XInput_UpdatePolledDataAndTimestamp (
pDevice, (
pDevice->state.current.Gamepad.wButtons != 0 ||
pDevice->state.current.Gamepad.wButtons != pDevice->state.prev.Gamepad.wButtons ||
normL > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ||
normR > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ||
pDevice->state.current.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD ||
pDevice->state.current.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD
), bNewData )
)
{
return false;
}
#if 0
// Common to DualSense and DualShock4, but no representation in XInput
pData->ButtonPad != 0;
#endif
return bNewData;
}
else
{
DWORD dwLastErr =
GetLastError ();
if (dwLastErr == ERROR_DEVICE_NOT_CONNECTED ||
dwLastErr == ERROR_INVALID_HANDLE)
{
pDevice->disconnect ();
return false;
}
if (dwLastErr == ERROR_INVALID_USER_BUFFER)
{
pDevice->disconnect ();
return false;
}
static auto x =
MessageBox (
nullptr, std::to_wstring (dwLastErr).c_str (),
L"HID Input Error (ReadFile - DualShock4 [USB])",
MB_OK
);
}
return false;
}
bool
SK_DualShock4_GetInputReportBt (void *pGenericDev)
{
hid_device_file_s* pDevice =
(hid_device_file_s *)pGenericDev;
ZeroMemory ( pDevice->input_report.data (),
pDevice->input_report.size () );
BYTE* report = pDevice->input_report.data ();
// HID Input Report 0x11 (Bluetooth)
report [0] = 0x11;
DWORD dwBytesRead = 0;
if (ReadFile (pDevice->hDeviceFile, report, (DWORD)pDevice->input_report.size (), &dwBytesRead, nullptr))
{
pDevice->state.current.Gamepad = { };
BYTE* pInputRaw =
(BYTE *)&report [0];
SK_HID_DualShock4_GetStateDataBt *pData =
(SK_HID_DualShock4_GetStateDataBt *)&pInputRaw [3];
bool bSimple = false;
// We're in simplified mode...
if (pInputRaw [0] == 0x1)
{
bSimple = true;
}
bool bNewData = false;
if (! bSimple)
{
pDevice->state.current.Gamepad.bLeftTrigger =
static_cast <BYTE> (pData->TriggerLeft);
pDevice->state.current.Gamepad.bRightTrigger =
static_cast <BYTE> (pData->TriggerRight);
pDevice->state.current.Gamepad.sThumbLX =
static_cast <SHORT> (32767.0 * static_cast <double> ( static_cast <LONG> (pData->LeftStickX) - 128) / 128.0);
pDevice->state.current.Gamepad.sThumbLY =
static_cast <SHORT> (32767.0 * static_cast <double> (128 - static_cast <LONG> (pData->LeftStickY) ) / 128.0);
pDevice->state.current.Gamepad.sThumbRX =
static_cast <SHORT> (32767.0 * static_cast <double> ( static_cast <LONG> (pData->RightStickX) - 128) / 128.0);
pDevice->state.current.Gamepad.sThumbRY =
static_cast <SHORT> (32767.0 * static_cast <double> (128 - static_cast <LONG> (pData->RightStickY) ) / 128.0);
switch (pData->DPad)
{
case North: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
case NorthEast: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
case East: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
case SouthEast: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
case South: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
case SouthWest: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
case West: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
case NorthWest: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
case Neutral:
default:
// Centered value, do nothing
break;
}
if (pData->ButtonSquare != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_X;
if (pData->ButtonCross != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_A;
if (pData->ButtonCircle != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_B;
if (pData->ButtonTriangle != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_Y;
if (pData->ButtonL1 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER;
if (pData->ButtonR1 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER;
if (pData->ButtonL2 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_TRIGGER;
if (pData->ButtonR2 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_TRIGGER;
if (pData->ButtonL3 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB;
if (pData->ButtonR3 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB;
if (pData->ButtonShare != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK;
if (pData->ButtonOptions != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_START;
if (pData->ButtonHome != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE;
float LX = pDevice->state.current.Gamepad.sThumbLX;
float LY = pDevice->state.current.Gamepad.sThumbLY;
float normL = sqrtf ( LX*LX + LY*LY );
float RX = pDevice->state.current.Gamepad.sThumbRX;
float RY = pDevice->state.current.Gamepad.sThumbRY;
float normR = sqrtf ( RX*RX + RY*RY );
if (! SK_XInput_UpdatePolledDataAndTimestamp (
pDevice, (
pDevice->state.current.Gamepad.wButtons != 0 ||
pDevice->state.current.Gamepad.wButtons != pDevice->state.prev.Gamepad.wButtons ||
normL > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ||
normR > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ||
pDevice->state.current.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD ||
pDevice->state.current.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD
), bNewData )
)
{
return false;
}
}
else
{
SK_HID_DualShock4_BasicGetStateData *pSimpleData =
(SK_HID_DualShock4_BasicGetStateData *)&pInputRaw [1];
#if 0
// The analog axes
memset (
&pSimpleData->LeftStickX, 128, 4);
memset (
&pSimpleData->TriggerLeft, 0, 2);
pSimpleData->Counter++;
pSimpleData->DPad = Neutral;
pSimpleData->ButtonSquare = 0;
pSimpleData->ButtonCross = 0;
pSimpleData->ButtonCircle = 0;
pSimpleData->ButtonTriangle = 0;
pSimpleData->ButtonL1 = 0;
pSimpleData->ButtonR1 = 0;
pSimpleData->ButtonL2 = 0;
pSimpleData->ButtonR2 = 0;
pSimpleData->ButtonShare = 0;
pSimpleData->ButtonOptions = 0;
pSimpleData->ButtonL3 = 0;
pSimpleData->ButtonR3 = 0;
pSimpleData->ButtonHome = 0;
pSimpleData->ButtonPad = 0;
#endif
pDevice->state.current.Gamepad.bLeftTrigger =
static_cast <BYTE> (pSimpleData->TriggerLeft);
pDevice->state.current.Gamepad.bRightTrigger =
static_cast <BYTE> (pSimpleData->TriggerRight);
pDevice->state.current.Gamepad.sThumbLX =
static_cast <SHORT> (32767.0 * static_cast <double> ( static_cast <LONG> (pSimpleData->LeftStickX) - 128) / 128.0);
pDevice->state.current.Gamepad.sThumbLY =
static_cast <SHORT> (32767.0 * static_cast <double> (128 - static_cast <LONG> (pSimpleData->LeftStickY) ) / 128.0);
pDevice->state.current.Gamepad.sThumbRX =
static_cast <SHORT> (32767.0 * static_cast <double> ( static_cast <LONG> (pSimpleData->RightStickX) - 128) / 128.0);
pDevice->state.current.Gamepad.sThumbRY =
static_cast <SHORT> (32767.0 * static_cast <double> (128 - static_cast <LONG> (pSimpleData->RightStickY) ) / 128.0);
switch (pSimpleData->DPad)
{
case North: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
case NorthEast: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
case East: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
case SouthEast: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
case South: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
case SouthWest: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
case West: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
case NorthWest: pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT;
pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
case Neutral:
default:
// Centered value, do nothing
break;
}
if (pSimpleData->ButtonSquare != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_X;
if (pSimpleData->ButtonCross != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_A;
if (pSimpleData->ButtonCircle != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_B;
if (pSimpleData->ButtonTriangle != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_Y;
if (pSimpleData->ButtonL1 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER;
if (pSimpleData->ButtonR1 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER;
if (pSimpleData->ButtonL2 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_TRIGGER;
if (pSimpleData->ButtonR2 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_TRIGGER;
if (pSimpleData->ButtonL3 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB;
if (pSimpleData->ButtonR3 != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB;
if (pSimpleData->ButtonShare != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK;
if (pSimpleData->ButtonOptions != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_START;
if (pSimpleData->ButtonHome != 0) pDevice->state.current.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE;
float LX = pDevice->state.current.Gamepad.sThumbLX;
float LY = pDevice->state.current.Gamepad.sThumbLY;
float normL = sqrtf ( LX*LX + LY*LY );
float RX = pDevice->state.current.Gamepad.sThumbRX;
float RY = pDevice->state.current.Gamepad.sThumbRY;
float normR = sqrtf ( RX*RX + RY*RY );
if (! SK_XInput_UpdatePolledDataAndTimestamp (
pDevice, (
pDevice->state.current.Gamepad.wButtons != 0 ||
pDevice->state.current.Gamepad.wButtons != pDevice->state.prev.Gamepad.wButtons ||
normL > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ||
normR > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ||
pDevice->state.current.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD ||
pDevice->state.current.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD
), bNewData )
)
{
return false;
}
}
if (config.bSpecialTriangleShutsOff)
{
if ( bNewData &&
(pDevice->state.current.Gamepad.wButtons & XINPUT_GAMEPAD_Y ) != 0 &&
(pDevice->state.current.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) != 0 )
{
if (SK_Bluetooth_PowerOffGamepad (pDevice))
{
pDevice->disconnect ();
return false;
}
}
if ( bNewData &&
(pDevice->state.current.Gamepad.wButtons & XINPUT_GAMEPAD_A ) != 0 &&
(pDevice->state.current.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) != 0 )
{
SendMessage (GetDesktopWindow (), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
}
#if 0
// Common to DualSense and DualShock4, but no representation in XInput
pData->ButtonPad != 0;
#endif
if ( bNewData &&
(pDevice->state.current.Gamepad.wButtons & XINPUT_GAMEPAD_A ) != 0 &&
(pDevice->state.current.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) != 0 )
{
SendMessage (GetDesktopWindow (), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
return bNewData;
}
else
{
DWORD dwLastErr =
GetLastError ();
if (dwLastErr == ERROR_DEVICE_NOT_CONNECTED ||
dwLastErr == ERROR_INVALID_HANDLE)
{
pDevice->disconnect ();
return false;
}
if (dwLastErr == ERROR_INVALID_USER_BUFFER)
{
pDevice->disconnect ();
return false;
}
static auto x =
MessageBox (
nullptr, std::to_wstring (dwLastErr).c_str (),
L"HID Input Error (ReadFile - DualShock4 [Bt])",
MB_OK
);
}
return false;
}