-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhid_app.cpp
More file actions
1197 lines (1130 loc) · 52.7 KB
/
Copy pathhid_app.cpp
File metadata and controls
1197 lines (1130 loc) · 52.7 KB
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* author : Shuichi TAKANO
* since : Thu Jul 29 2021 03:39:11
*/
#include <tusb.h>
#include <stdio.h>
#include <string.h>
#include "xinput_host.h"
#include "gamepad.h"
#ifndef ABSWAPPED
#define ABSWAPPED 1
#endif
// set to 1 to enable printing of button states in binary when they change
#ifndef PRINTFBUTTONS
#define PRINTFBUTTONS 0
#endif
// Default mode for the MantaPad (081f:e401), which is sold both as a NES-
// and as a SNES-shaped controller with the same VID/PID: 1 = NES behavior
// (SNES mode activated by pressing Y), 2 = SNES from the start. SNES
// emulators should define this as 2 so the pad works fully at connect.
#ifndef MANTAPAD_DEFAULT_MODE
#define MANTAPAD_DEFAULT_MODE 1
#endif
int abSwapped = ABSWAPPED;
int isManta[2] = {0, 0}; // 1 NES, 2 SNES (per player)
int isMantaVariant[2] = {0, 0}; // SNES pad (per player)
#ifdef __cplusplus
extern "C"
{
#endif
#define MAX_REPORT 4
namespace
{
uint8_t _report_count[CFG_TUH_HID];
tuh_hid_report_info_t _report_info_arr[CFG_TUH_HID][MAX_REPORT];
static constexpr int MAX_USB_PLAYERS = 2;
static uint8_t playerDevAddr[MAX_USB_PLAYERS] = {0, 0};
// Boot-protocol mouse tracking. A mouse does not occupy a player
// slot, so a keyboard + mouse + gamepad can all be connected at once.
static bool mouseMounted = false;
static uint8_t mouseDevAddr = 0;
static uint8_t mouseInstance = 0;
static void processMouseReport(hid_mouse_report_t const *r, uint16_t len)
{
auto &m = io::getCurrentMouseState();
m.connected = true;
m.buttons = r->buttons;
m.dx += r->x;
m.dy += r->y;
if (len >= 4) // wheel-less mice send 3-byte reports
{
m.wheel += r->wheel;
}
}
static int assignPlayer(uint8_t dev_addr)
{
for (int i = 0; i < MAX_USB_PLAYERS; i++)
if (playerDevAddr[i] == dev_addr)
return i;
for (int i = 0; i < MAX_USB_PLAYERS; i++)
{
if (playerDevAddr[i] == 0)
{
playerDevAddr[i] = dev_addr;
return i;
}
}
return -1;
}
static void unassignPlayer(uint8_t dev_addr)
{
for (int i = 0; i < MAX_USB_PLAYERS; i++)
if (playerDevAddr[i] == dev_addr)
{
playerDevAddr[i] = 0;
return;
}
}
static int getPlayerIndex(uint8_t dev_addr)
{
for (int i = 0; i < MAX_USB_PLAYERS; i++)
if (playerDevAddr[i] == dev_addr)
return i;
return -1;
}
// Is dual shock 4 controller detected?
static inline bool isDS4(uint16_t vid, uint16_t pid)
{
return vid == 0x054c && (pid == 0x09cc || pid == 0x05c4);
}
// Is dual sense controller detected?
static inline bool isDS5(uint16_t vid, uint16_t pid)
{
return vid == 0x054c && pid == 0x0ce6;
}
// Is PSClassic controller detected?
static inline bool isPSClassic(uint16_t vid, uint16_t pid)
{
return vid == 0x054c && pid == 0x0cda;
}
// Is Nintendo controller detected?
static inline bool isNintendo(uint16_t vid, uint16_t pid)
{
return vid == 0x057e && (pid == 0x2009 || pid == 0x2017);
}
static inline bool isGenesisMini(uint16_t vid, uint16_t pid)
{
return vid == 0x0ca3 && (pid == 0x0025 || pid == 0x0024);
}
// Retro-bit 8 button Mega Drive Arcade Pad with USB
static inline bool isMDArcadePad(uint16_t vid, uint16_t pid)
{
return (vid == 0xf0d) && (pid == 0x00c1);
}
// Is MantaPad detected? Cheap Aliexpress SNES/NES controller
static inline bool isMantaPad(uint16_t vid, uint16_t pid)
{
return vid == 0x081f && pid == 0xe401;
}
// Is MantaPad Variant detected? Cheap Aliexpress SNES controller (0810:e501)
static inline bool isMantaPadVariant(uint16_t vid, uint16_t pid)
{
return vid == 0x0810 && pid == 0xe501;
}
struct DS4Report
{
// https://www.psdevwiki.com/ps4/DS4-USB
struct Button1
{
inline static constexpr int SQUARE = 1 << 4;
inline static constexpr int CROSS = 1 << 5;
inline static constexpr int CIRCLE = 1 << 6;
inline static constexpr int TRIANGLE = 1 << 7;
};
struct Button2
{
inline static constexpr int L1 = 1 << 0;
inline static constexpr int R1 = 1 << 1;
inline static constexpr int L2 = 1 << 2;
inline static constexpr int R2 = 1 << 3;
inline static constexpr int SHARE = 1 << 4;
inline static constexpr int OPTIONS = 1 << 5;
inline static constexpr int L3 = 1 << 6;
inline static constexpr int R3 = 1 << 7;
};
uint8_t reportID;
uint8_t stickL[2];
uint8_t stickR[2];
uint8_t buttons1;
uint8_t buttons2;
uint8_t ps : 1;
uint8_t tpad : 1;
uint8_t counter : 6;
uint8_t triggerL;
uint8_t triggerR;
// ...
int getHat() const { return buttons1 & 15; }
};
struct DS5Report
{
uint8_t reportID;
uint8_t stickL[2];
uint8_t stickR[2];
uint8_t triggerL;
uint8_t triggerR;
uint8_t counter;
uint8_t buttons[3];
// ...
struct Button
{
inline static constexpr int SQUARE = 1 << 4;
inline static constexpr int CROSS = 1 << 5;
inline static constexpr int CIRCLE = 1 << 6;
inline static constexpr int TRIANGLE = 1 << 7;
inline static constexpr int L1 = 1 << 8;
inline static constexpr int R1 = 1 << 9;
inline static constexpr int L2 = 1 << 10;
inline static constexpr int R2 = 1 << 11;
inline static constexpr int SHARE = 1 << 12;
inline static constexpr int OPTIONS = 1 << 13;
inline static constexpr int L3 = 1 << 14;
inline static constexpr int R3 = 1 << 15;
inline static constexpr int PS = 1 << 16;
inline static constexpr int TPAD = 1 << 17;
};
int getHat() const { return buttons[0] & 15; }
};
// Report for Genesis Mini controller and Retro-bit 8 button Mega Drive Arcade Pad with USB
struct GenesisMiniReport
{
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
uint8_t byte5;
uint8_t byte6;
uint8_t byte7;
uint8_t byte8;
struct Button
{
inline static constexpr int A = 0b01000000; // byte 6
inline static constexpr int B = 0b00100000; // byte 6
inline static constexpr int C = 0b00000010; // byte 7
inline static constexpr int X = 0b10001111; // byte 6
inline static constexpr int Y = 0b00011111; // byte 6
inline static constexpr int Z = 0b00000001; // byte 6
inline static constexpr int MODE = 0b00010000; // byte 7
inline static constexpr int START = 0b00100000; // byte 7
inline static constexpr int UP = 0; // byte 5
inline static constexpr int DOWN = 0b11111111; // byte 5
inline static constexpr int LEFT = 0; // byte 4
inline static constexpr int RIGHT = 0b11111111; // byte 4
;
};
struct ButtonRetrobit
{
inline static constexpr int A = 0b00000100; // byte 1
inline static constexpr int B = 0b00000010; // byte 1
inline static constexpr int C = 0b10000000; // byte 1
inline static constexpr int X = 0b00001000; // byte 1
inline static constexpr int Y = 0b00000001; // byte 1
inline static constexpr int Z = 0b01000000; // byte 1
inline static constexpr int MODE = 0b00000001; // byte 2
inline static constexpr int L = 0b00010000; // byte 1
inline static constexpr int R = 0b00100000; // byte 1
inline static constexpr int START = 0b00000010; // byte 2
inline static constexpr int UP = 0; // byte 5
inline static constexpr int DOWN = 0b11111111; // byte 5
inline static constexpr int LEFT = 0; // byte 4
inline static constexpr int RIGHT = 0b11111111; // byte 4
;
};
};
// Report for MantaPad, cheap AliExpress SNES controller
struct MantaPadReport
{
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
uint8_t byte5;
uint8_t byte6;
uint8_t byte7;
uint8_t byte8;
struct Button
{
inline static constexpr int A = 0b00100000;
inline static constexpr int B = 0b01000000;
inline static constexpr int X = 0b00010000;
inline static constexpr int Y = 0b10000000;
inline static constexpr int NESB = X; // B Button on NES controller is X on SNES controller
inline static constexpr int SELECT = 0b00010000;
inline static constexpr int START = 0b00100000;
inline static constexpr int UP = 0b00000000;
inline static constexpr int DOWN = 0b11111111;
inline static constexpr int LEFT = 0b00000000;
inline static constexpr int RIGHT = 0b11111111;
inline static constexpr int SHOULDERLEFT = 0b00000001;
inline static constexpr int SHOULDERRIGHT = 0b00000010;
};
};
// Report for PSClassic controller
struct PSClassicReport
{
uint8_t buttons;
// Idle 00010100
// up 00000100
// upright 00001000
// right 00011000
// rightdown 00101000
// down 00100100
// downleft 00100000
// left 00010000
// leftup 00100000
// start 00010110
// select 00010101
// St + sel 00010111
// selectup 00000101
// selectdown 00100101
uint8_t hat;
struct Button
{
inline static constexpr int ButtonsIdle = 0x00;
inline static constexpr int HatIdle = 0b00010100;
inline static constexpr int Square = 0x08;
inline static constexpr int Circle = 0x02;
inline static constexpr int Cross = 0x04;
inline static constexpr int Triangle = 0x01;
inline static constexpr int SELECT = 0b00010101;
inline static constexpr int START = 0b00010110;
inline static constexpr int UP = 0b00000100;
inline static constexpr int UPRIGHT = 0b00001000;
inline static constexpr int RIGHT = 0b00011000;
inline static constexpr int RIGHTDOWN = 0b00101000;
inline static constexpr int DOWN = 0b00100100;
inline static constexpr int DOWNLEFT = 0b00100000;
inline static constexpr int LEFT = 0b00010000;
inline static constexpr int LEFTUP = 0b00000000;
inline static constexpr int SELECTUP = 0b00000101;
inline static constexpr int SELECTDOWN = 0b00100101;
inline static constexpr int SELECTSTART = 0b00010111;
};
};
// Helper: print a HID report (any length up to MAX) as binary only when it changes.
static void printHIDReportIfChanged(const uint8_t *report, uint16_t len)
{
if (!report || !len)
return;
// Typical HID report sizes are <= 64 bytes for full-speed devices
constexpr uint16_t MAX_PRINT = 64; // adjust if you expect bigger
static uint8_t prev_report[MAX_PRINT];
static uint16_t prev_len = 0;
static bool prev_valid = false;
if (len > MAX_PRINT)
{
// Fallback: if size exceeds buffer, just print length notice once (or always?)
// Print raw truncated binary when it changes relative to first MAX_PRINT bytes.
// Compare only first MAX_PRINT bytes.
bool changed_large = !prev_valid || prev_len != len || memcmp(prev_report, report, MAX_PRINT) != 0;
if (!changed_large)
return;
printf("HID report (len=%u > MAX_PRINT=%u) (changed bytes in []): ", len, MAX_PRINT);
for (uint16_t i = 0; i < MAX_PRINT; ++i)
{
bool byte_changed = !prev_valid || prev_report[i] != report[i];
if (byte_changed)
putchar('[');
uint8_t v = report[i];
for (int b = 7; b >= 0; --b)
putchar((v & (1 << b)) ? '1' : '0');
if (byte_changed)
putchar(']');
if (i + 1 < MAX_PRINT)
putchar(' ');
}
putchar('\n');
memcpy(prev_report, report, MAX_PRINT);
prev_len = len; // store true len for change detection
prev_valid = true;
return;
}
bool changed = !prev_valid || prev_len != len || memcmp(prev_report, report, len) != 0;
if (!changed)
return;
printf("HID report (len=%u) (bin, changed bytes in []): ", len);
for (uint16_t i = 0; i < len; ++i)
{
bool byte_changed = !prev_valid || prev_report[i] != report[i];
if (byte_changed)
putchar('[');
uint8_t v = report[i];
for (int b = 7; b >= 0; --b)
putchar((v & (1 << b)) ? '1' : '0');
if (byte_changed)
putchar(']');
if (i + 1 < len)
putchar(' ');
}
putchar('\n');
memcpy(prev_report, report, len);
prev_len = len;
prev_valid = true;
}
}
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len)
{
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
// Mice are handled separately and do not claim a player slot.
if (tuh_hid_interface_protocol(dev_addr, instance) == HID_ITF_PROTOCOL_MOUSE)
{
printf("Mouse detected - device address = %d, instance = %d is mounted - VID = %04x, PID = %04x\n",
dev_addr, instance, vid, pid);
mouseMounted = true;
mouseDevAddr = dev_addr;
mouseInstance = instance;
io::getCurrentMouseState().connected = true;
if (!tuh_hid_receive_report(dev_addr, instance))
{
printf("Error: cannot request to receive report\r\n");
}
return;
}
int player = assignPlayer(dev_addr);
if (player < 0)
{
printf("No free player slot for HID device address = %d (VID = %04x, PID = %04x)\n", dev_addr, vid, pid);
return;
}
isManta[player] = 0;
isMantaVariant[player] = 0;
auto &gp = io::getCurrentGamePadState(player);
if (isDS4(vid, pid))
{
printf("Dual Shock 4 Controller detected - device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
gp.GamePadName = "Dual Shock 4";
gp.GamePadShortName = "DS4";
}
else if (isDS5(vid, pid))
{
printf("Dual Sense Controller detected - device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
gp.GamePadName = "Dual Sense";
gp.GamePadShortName = "DS5";
}
else if (isMantaPad(vid, pid))
{
printf("MantaPad detected - device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
isManta[player] = MANTAPAD_DEFAULT_MODE;
if (isManta[player] == 2)
{
gp.GamePadName = "Manta SNES";
gp.GamePadShortName = "MSNES";
}
else
{
printf("Press Y to activate SNES mode\n");
gp.GamePadName = "Manta NES";
gp.GamePadShortName = "MNES";
}
}
else if (isMantaPadVariant(vid, pid))
{
printf("MantaPad Variant detected - device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
isManta[player] = 2;
isMantaVariant[player] = 1;
gp.GamePadName = "Manta SNES";
gp.GamePadShortName = "MSNES";
}
else if (isGenesisMini(vid, pid))
{
printf("Sega Mega Drive/Genesis Mini %d controller detected - device address = %d, instance = %d, player %d is mounted - ", (pid == 0x0025) ? 1 : 2, dev_addr, instance, player + 1);
gp.GamePadName = (pid == 0x0025) ? "Genesis Mini 1" : "Genesis Mini 2";
gp.GamePadShortName = (pid == 0x0025) ? "GM1" : "GM2";
}
else if (isMDArcadePad(vid, pid))
{
printf("Retro-bit MD Arcade pad detected - device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
gp.GamePadName = "MDArcade";
gp.GamePadShortName = "MD";
}
else if (isPSClassic(vid, pid))
{
printf("PlayStation Classic controller detected - device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
gp.GamePadName = "PSClassic";
gp.GamePadShortName = "PSC";
}
else
{
printf("Unkown device detected - HID device address = %d, instance = %d, player %d is mounted - ", dev_addr, instance, player + 1);
static char unknownName[20];
snprintf(unknownName, sizeof(unknownName), "%04x:%04x", vid, pid);
gp.GamePadName = unknownName;
gp.GamePadShortName = "??";
}
printf("VID = %04x, PID = %04x\r\n", vid, pid);
const char *protocol_str[] = {"None", "Keyboard", "Mouse"}; // hid_protocol_type_t
uint8_t const interface_protocol = tuh_hid_interface_protocol(dev_addr, instance);
// Parse report descriptor with built-in parser
_report_count[instance] = tuh_hid_parse_report_descriptor(_report_info_arr[instance], MAX_REPORT, desc_report, desc_len);
printf("HID has %u reports and interface protocol = %d:%s\n", _report_count[instance],
interface_protocol, protocol_str[interface_protocol]);
if (!tuh_hid_receive_report(dev_addr, instance))
{
printf("Error: cannot request to receive report\r\n");
}
}
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{
if (mouseMounted && dev_addr == mouseDevAddr && instance == mouseInstance)
{
printf("Mouse device address = %d, instance = %d is unmounted\n", dev_addr, instance);
mouseMounted = false;
auto &m = io::getCurrentMouseState();
m.connected = false;
m.buttons = 0;
return;
}
int player = getPlayerIndex(dev_addr);
if (player >= 0)
{
printf("HID device address = %d, instance = %d, player %d is unmounted\n", dev_addr, instance, player + 1);
auto &gp = io::getCurrentGamePadState(player);
gp.flagConnected(false);
gp.GamePadName = nullptr;
gp.GamePadShortName = nullptr;
unassignPlayer(dev_addr);
}
else
{
printf("HID device address = %d, instance = %d is unmounted (was not assigned)\n", dev_addr, instance);
}
}
void tuh_hid_report_received_cb(uint8_t dev_addr,
uint8_t instance, uint8_t const *report, uint16_t len)
{
if (mouseMounted && dev_addr == mouseDevAddr && instance == mouseInstance)
{
// Boot-protocol mouse report (TinyUSB host defaults interfaces to
// boot protocol): buttons, x, y, wheel.
if (len >= 3)
{
processMouseReport(reinterpret_cast<hid_mouse_report_t const *>(report), len);
}
if (!tuh_hid_receive_report(dev_addr, instance))
{
printf("Error: cannot request to receive report\r\n");
}
return;
}
int player = getPlayerIndex(dev_addr);
if (player < 0)
{
tuh_hid_receive_report(dev_addr, instance);
return;
}
uint8_t const rpt_count = _report_count[instance];
tuh_hid_report_info_t *rpt_info_arr = _report_info_arr[instance];
tuh_hid_report_info_t *rpt_info = NULL;
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
#if PRINTFBUTTONS
// helper for figuring out button states
printHIDReportIfChanged(report, len);
#endif
if (isDS4(vid, pid))
{
if (sizeof(DS4Report) <= len)
{
auto r = reinterpret_cast<const DS4Report *>(report);
if (r->reportID != 1)
{
printf("Invalid reportID %d\n", r->reportID);
return;
}
auto &gp = io::getCurrentGamePadState(player);
gp.axis[0] = r->stickL[0];
gp.axis[1] = r->stickL[1];
if (abSwapped)
{
gp.buttons = (r->buttons1 & DS4Report::Button1::CROSS ? io::GamePadState::Button::B : 0) |
(r->buttons1 & DS4Report::Button1::CIRCLE ? io::GamePadState::Button::A : 0);
}
else
{
gp.buttons = (r->buttons1 & DS4Report::Button1::CROSS ? io::GamePadState::Button::A : 0) |
(r->buttons1 & DS4Report::Button1::CIRCLE ? io::GamePadState::Button::B : 0);
}
gp.buttons = gp.buttons |
(r->buttons1 & DS4Report::Button1::TRIANGLE ? io::GamePadState::Button::X : 0) |
(r->buttons1 & DS4Report::Button1::SQUARE ? io::GamePadState::Button::Y : 0) |
(r->buttons2 & (DS4Report::Button2::L1 | DS4Report::Button2::L2) ? io::GamePadState::Button::L : 0) |
(r->buttons2 & (DS4Report::Button2::R1 | DS4Report::Button2::R2) ? io::GamePadState::Button::R : 0) |
(r->buttons2 & DS4Report::Button2::SHARE ? io::GamePadState::Button::SELECT : 0) |
(r->tpad ? io::GamePadState::Button::SELECT : 0) |
(r->buttons2 & DS4Report::Button2::OPTIONS ? io::GamePadState::Button::START : 0);
gp.hat = static_cast<io::GamePadState::Hat>(r->getHat());
gp.convertButtonsFromAxis(0, 1);
gp.convertButtonsFromHat();
gp.flagConnected(true);
}
else
{
printf("Invalid DS4 report size %zd\n", len);
return;
}
}
else if (isDS5(vid, pid))
{
if (sizeof(DS5Report) <= len)
{
auto r = reinterpret_cast<const DS5Report *>(report);
if (r->reportID != 1)
{
printf("Invalid reportID %d\n", r->reportID);
return;
}
auto buttons = r->buttons[0] | (r->buttons[1] << 8) | (r->buttons[2] << 16);
auto &gp = io::getCurrentGamePadState(player);
gp.axis[0] = r->stickL[0];
gp.axis[1] = r->stickL[1];
if (abSwapped)
{
gp.buttons =
(buttons & DS5Report::Button::CROSS ? io::GamePadState::Button::B : 0) |
(buttons & DS5Report::Button::CIRCLE ? io::GamePadState::Button::A : 0);
}
else
{
gp.buttons = (buttons & DS5Report::Button::CIRCLE ? io::GamePadState::Button::B : 0) |
(buttons & DS5Report::Button::CROSS ? io::GamePadState::Button::A : 0);
}
gp.buttons =
gp.buttons |
(buttons & DS5Report::Button::TRIANGLE ? io::GamePadState::Button::X : 0) |
(buttons & DS5Report::Button::SQUARE ? io::GamePadState::Button::Y : 0) |
(buttons & (DS5Report::Button::L1 | DS5Report::Button::L2) ? io::GamePadState::Button::L : 0) |
(buttons & (DS5Report::Button::R1 | DS5Report::Button::R2) ? io::GamePadState::Button::R : 0) |
(buttons & (DS5Report::Button::SHARE | DS5Report::Button::TPAD) ? io::GamePadState::Button::SELECT : 0) |
(buttons & DS5Report::Button::OPTIONS ? io::GamePadState::Button::START : 0);
gp.hat = static_cast<io::GamePadState::Hat>(r->getHat());
gp.convertButtonsFromAxis(0, 1);
gp.convertButtonsFromHat();
gp.flagConnected(true);
}
else
{
printf("Invalid DS5 report size %zd\n", len);
return;
}
}
else if (isMantaPad(vid, pid) || isMantaPadVariant(vid, pid))
{
if (sizeof(MantaPadReport) == len)
{
auto r = reinterpret_cast<const MantaPadReport *>(report);
auto &gp = io::getCurrentGamePadState(player);
uint8_t udb = r->byte2;
uint8_t lrb = r->byte1;
// When using AlieExpress SNES usb controller, activate SNES mode by pressing Y
if (r->byte6 & MantaPadReport::Button::Y)
{
isManta[player] = 2;
gp.GamePadName = "Manta SNES";
gp.GamePadShortName = "MSNES";
// printf("MantaPad SNES mode activated\n");
}
// printf("MantaPad report: %02x %02x %02x %02x %02x %02x %02x %02x\n",
// r->byte1, r->byte2, r->byte3, r->byte4, r->byte5, r->byte6, r->byte7, r->byte8);
if (isMantaVariant[player])
{
udb = r->byte5; // UP / DOWN
lrb = r->byte4; // LEFT / RIGHT
}
if (abSwapped)
{
gp.buttons =
(r->byte6 & MantaPadReport::Button::A ? io::GamePadState::Button::A : 0) |
((isManta[player] == 1 && r->byte6 & MantaPadReport::Button::NESB) ? io::GamePadState::Button::B : 0) |
((isManta[player] == 2 && r->byte6 & MantaPadReport::Button::B) ? io::GamePadState::Button::B : 0) |
((isManta[player] == 2 && r->byte6 & MantaPadReport::Button::X) ? io::GamePadState::Button::X : 0) |
((isManta[player] == 2 && r->byte6 & MantaPadReport::Button::Y) ? io::GamePadState::Button::Y : 0) |
(r->byte7 & MantaPadReport::Button::SHOULDERLEFT ? io::GamePadState::Button::L : 0) |
(r->byte7 & MantaPadReport::Button::SHOULDERRIGHT ? io::GamePadState::Button::R : 0) |
(r->byte7 & MantaPadReport::Button::START ? io::GamePadState::Button::START : 0) |
(r->byte7 & MantaPadReport::Button::SELECT ? io::GamePadState::Button::SELECT : 0) |
(udb == MantaPadReport::Button::UP ? io::GamePadState::Button::UP : 0) |
(udb == MantaPadReport::Button::DOWN ? io::GamePadState::Button::DOWN : 0) |
(lrb == MantaPadReport::Button::LEFT ? io::GamePadState::Button::LEFT : 0) |
(lrb == MantaPadReport::Button::RIGHT ? io::GamePadState::Button::RIGHT : 0);
}
else
{
gp.buttons =
(r->byte6 & MantaPadReport::Button::A ? io::GamePadState::Button::B : 0) |
((isManta[player] == 1 && r->byte6 & MantaPadReport::Button::NESB) ? io::GamePadState::Button::A : 0) |
((isManta[player] == 2 && r->byte6 & MantaPadReport::Button::B) ? io::GamePadState::Button::A : 0) |
((isManta[player] == 2 && r->byte6 & MantaPadReport::Button::X) ? io::GamePadState::Button::X : 0) |
((isManta[player] == 2 && r->byte6 & MantaPadReport::Button::Y) ? io::GamePadState::Button::Y : 0) |
(r->byte7 & MantaPadReport::Button::SHOULDERLEFT ? io::GamePadState::Button::L : 0) |
(r->byte7 & MantaPadReport::Button::SHOULDERRIGHT ? io::GamePadState::Button::R : 0) |
(r->byte7 & MantaPadReport::Button::START ? io::GamePadState::Button::START : 0) |
(r->byte7 & MantaPadReport::Button::SELECT ? io::GamePadState::Button::SELECT : 0) |
(udb == MantaPadReport::Button::UP ? io::GamePadState::Button::UP : 0) |
(udb == MantaPadReport::Button::DOWN ? io::GamePadState::Button::DOWN : 0) |
(lrb == MantaPadReport::Button::LEFT ? io::GamePadState::Button::LEFT : 0) |
(lrb == MantaPadReport::Button::RIGHT ? io::GamePadState::Button::RIGHT : 0);
}
gp.flagConnected(true);
}
else
{
printf("Invalid MantaPad report size %zd\n", len);
return;
}
}
else if (isGenesisMini(vid, pid))
{
if (sizeof(GenesisMiniReport) == len)
{
auto r = reinterpret_cast<const GenesisMiniReport *>(report);
auto &gp = io::getCurrentGamePadState(player);
if (abSwapped)
{
gp.buttons = (r->byte6 & GenesisMiniReport::Button::B ? io::GamePadState::Button::A : 0) |
(r->byte6 & GenesisMiniReport::Button::A ? io::GamePadState::Button::B : 0);
}
else
{
gp.buttons = (r->byte6 & GenesisMiniReport::Button::A ? io::GamePadState::Button::A : 0) |
(r->byte6 & GenesisMiniReport::Button::B ? io::GamePadState::Button::B : 0);
}
gp.buttons = gp.buttons |
(((r->byte7 & GenesisMiniReport::Button::C) && pid == 0x0025)? io::GamePadState::Button::SELECT : 0) | // C button is SELECT only original 3-button controller
((r->byte7 & GenesisMiniReport::Button::C) ? io::GamePadState::Button::C : 0) |
(r->byte7 & GenesisMiniReport::Button::START ? io::GamePadState::Button::START : 0) |
(r->byte7 & GenesisMiniReport::Button::MODE ? io::GamePadState::Button::SELECT : 0) |
(r->byte5 == GenesisMiniReport::Button::UP ? io::GamePadState::Button::UP : 0) |
(r->byte5 == GenesisMiniReport::Button::DOWN ? io::GamePadState::Button::DOWN : 0) |
(r->byte4 == GenesisMiniReport::Button::LEFT ? io::GamePadState::Button::LEFT : 0) |
(r->byte4 == GenesisMiniReport::Button::RIGHT ? io::GamePadState::Button::RIGHT : 0);
gp.flagConnected(true);
}
else
{
printf("Invalid Genesis Mini report size %zd\n", len);
return;
}
}
else if (isMDArcadePad(vid, pid))
{
if (sizeof(GenesisMiniReport) == len)
{
auto r = reinterpret_cast<const GenesisMiniReport *>(report);
auto &gp = io::getCurrentGamePadState(player);
if (abSwapped)
{
gp.buttons = (r->byte1 & GenesisMiniReport::ButtonRetrobit::B ? io::GamePadState::Button::A : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::A ? io::GamePadState::Button::B : 0);
}
else
{
gp.buttons = (r->byte1 & GenesisMiniReport::ButtonRetrobit::A ? io::GamePadState::Button::A : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::B ? io::GamePadState::Button::B : 0);
}
gp.buttons = gp.buttons |
(r->byte2 & GenesisMiniReport::ButtonRetrobit::MODE ? io::GamePadState::Button::SELECT : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::C ? io::GamePadState::Button::C : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::X ? io::GamePadState::Button::X : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::Y ? io::GamePadState::Button::Y : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::Z ? io::GamePadState::Button::Z : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::L ? io::GamePadState::Button::L : 0) |
(r->byte1 & GenesisMiniReport::ButtonRetrobit::R ? io::GamePadState::Button::R : 0) |
(r->byte2 & GenesisMiniReport::ButtonRetrobit::START ? io::GamePadState::Button::START : 0) |
(r->byte5 == GenesisMiniReport::ButtonRetrobit::UP ? io::GamePadState::Button::UP : 0) |
(r->byte5 == GenesisMiniReport::ButtonRetrobit::DOWN ? io::GamePadState::Button::DOWN : 0) |
(r->byte4 == GenesisMiniReport::ButtonRetrobit::LEFT ? io::GamePadState::Button::LEFT : 0) |
(r->byte4 == GenesisMiniReport::ButtonRetrobit::RIGHT ? io::GamePadState::Button::RIGHT : 0);
gp.flagConnected(true);
}
else
{
printf("Invalid Genesis Mini report size %zd\n", len);
return;
}
}
else if (isPSClassic(vid, pid))
{
if (sizeof(PSClassicReport) == len)
{
auto r = reinterpret_cast<const PSClassicReport *>(report);
auto &gp = io::getCurrentGamePadState(player);
if (abSwapped)
{
gp.buttons = (r->buttons & PSClassicReport::Button::Cross ? io::GamePadState::Button::B : 0) |
(r->buttons & PSClassicReport::Button::Circle ? io::GamePadState::Button::A : 0);
}
else
{
gp.buttons = (r->buttons & PSClassicReport::Button::Circle ? io::GamePadState::Button::B : 0) |
(r->buttons & PSClassicReport::Button::Cross ? io::GamePadState::Button::A : 0);
}
gp.buttons |= (r->buttons & PSClassicReport::Button::Triangle ? io::GamePadState::Button::X : 0) |
(r->buttons & PSClassicReport::Button::Square ? io::GamePadState::Button::Y : 0);
// L1/L2/R1/R2 bit positions are not verified yet; capture a
// report with PRINTFBUTTONS=1 before mapping them to L/R.
switch (r->hat)
{
case PSClassicReport::Button::UP:
gp.buttons = gp.buttons | io::GamePadState::Button::UP;
break;
case PSClassicReport::Button::UPRIGHT:
gp.buttons = gp.buttons | io::GamePadState::Button::UP | io::GamePadState::Button::RIGHT;
break;
case PSClassicReport::Button::RIGHT:
gp.buttons = gp.buttons | io::GamePadState::Button::RIGHT;
break;
case PSClassicReport::Button::RIGHTDOWN:
gp.buttons = gp.buttons | io::GamePadState::Button::RIGHT | io::GamePadState::Button::DOWN;
break;
case PSClassicReport::Button::DOWN:
gp.buttons = gp.buttons | io::GamePadState::Button::DOWN;
break;
case PSClassicReport::Button::DOWNLEFT:
gp.buttons = gp.buttons | io::GamePadState::Button::DOWN | io::GamePadState::Button::LEFT;
break;
case PSClassicReport::Button::LEFT:
gp.buttons = gp.buttons | io::GamePadState::Button::LEFT;
break;
case PSClassicReport::Button::LEFTUP:
gp.buttons = gp.buttons | io::GamePadState::Button::LEFT | io::GamePadState::Button::UP;
break;
case PSClassicReport::Button::SELECT:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT;
break;
case PSClassicReport::Button::START:
gp.buttons = gp.buttons | io::GamePadState::Button::START;
break;
case PSClassicReport::Button::SELECTSTART:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT | io::GamePadState::Button::START;
break;
case PSClassicReport::Button::SELECTUP:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT | io::GamePadState::Button::UP;
break;
case PSClassicReport::Button::SELECTDOWN:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT | io::GamePadState::Button::DOWN;
break;
default:
break;
}
gp.flagConnected(true);
}
else
{
printf("Invalid PSClassic Mini report size %zd\n", len);
return;
}
}
else if (isNintendo(vid, pid))
{
printf("Nintendo: len = %d\n", len); // Nintendo controllers do not report back
}
else
{
if (rpt_count == 1 && rpt_info_arr[0].report_id == 0)
{
// Simple report without report ID as 1st byte
rpt_info = &rpt_info_arr[0];
}
else
{
// Composite report, 1st byte is report ID, data starts from 2nd byte
uint8_t const rpt_id = report[0];
// Find report id in the arrray
for (uint8_t i = 0; i < rpt_count; i++)
{
if (rpt_id == rpt_info_arr[i].report_id)
{
rpt_info = &rpt_info_arr[i];
break;
}
}
report++;
len--;
}
if (!rpt_info)
{
printf("Couldn't find the report info for this report !\n");
return;
}
// printf("usage %d, %d\n", rpt_info->usage_page, rpt_info->usage);
if (rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP)
{
switch (rpt_info->usage)
{
case HID_USAGE_DESKTOP_KEYBOARD:
{
auto r = reinterpret_cast<const hid_keyboard_report_t *>(report);
// Expose the raw HID report for emulators that emulate a
// real keyboard (e.g. O2 / G7400). The gamepad mapping
// below stays in place for joystick-style use.
auto &kb = const_cast<io::KeyboardState &>(io::getCurrentKeyboardState());
kb.modifier = r->modifier;
memcpy(kb.keycode, r->keycode, sizeof(kb.keycode));
auto &gp = io::getCurrentGamePadState(player);
gp.GamePadName = "Keyboard";
gp.GamePadShortName = "KB";
gp.buttons = 0;
for (uint8_t i = 0; i < 6; i++)
{
if (r->keycode[i])
{
switch (r->keycode[i])
{
case HID_KEY_A:
gp.buttons |= io::GamePadState::Button::SELECT;
break;
case HID_KEY_S:
gp.buttons |= io::GamePadState::Button::START;
break;
case HID_KEY_Z:
gp.buttons |= (abSwapped ? io::GamePadState::Button::B : io::GamePadState::Button::A);
break;
case HID_KEY_X:
gp.buttons |= (abSwapped ? io::GamePadState::Button::A : io::GamePadState::Button::B);
break;
case HID_KEY_C:
gp.buttons |= io::GamePadState::Button::X;
break;
case HID_KEY_V:
gp.buttons |= io::GamePadState::Button::Y;
break;
case HID_KEY_Q:
gp.buttons |= io::GamePadState::Button::L;
break;
case HID_KEY_W:
gp.buttons |= io::GamePadState::Button::R;
break;
case HID_KEY_ARROW_UP:
gp.buttons |= io::GamePadState::Button::UP;
break;
case HID_KEY_ARROW_DOWN:
gp.buttons |= io::GamePadState::Button::DOWN;
break;
case HID_KEY_ARROW_LEFT:
gp.buttons |= io::GamePadState::Button::LEFT;
break;
case HID_KEY_ARROW_RIGHT:
gp.buttons |= io::GamePadState::Button::RIGHT;
break;
default:
break;
}
}
}
break;
}
case HID_USAGE_DESKTOP_MOUSE:
TU_LOG1("HID receive mouse report\n");
// Assume mouse follows boot report layout (report ID, if
// any, has already been stripped above).
if (len >= 3)
{
processMouseReport(reinterpret_cast<hid_mouse_report_t const *>(report), len);
}
break;
case HID_USAGE_DESKTOP_JOYSTICK:
{
// TU_LOG1("HID receive joystick report\n");
struct JoyStickReport
{
uint8_t axis[3];
uint8_t buttons;
// 実際のところはしらん
};
auto *rep = reinterpret_cast<const JoyStickReport *>(report);
// printf("x %d y %d button %02x\n", rep->axis[0], rep->axis[1], rep->buttons);
auto &gp = io::getCurrentGamePadState(player);
gp.axis[0] = rep->axis[0];
gp.axis[1] = rep->axis[1];
gp.axis[2] = rep->axis[2];
gp.buttons = rep->buttons;
gp.convertButtonsFromAxis(0, 1);
// BUFFALO BGC-FC801