Moved the PS5 controller driver to a separate file, as the advanced feature protocol is very different from that of the PS4
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
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index 7326a70..bda9dab 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -431,6 +431,7 @@
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapijoystick.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gamecube.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_ps4.c" />
+ <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_ps5.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_rumble.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
index ed593a6..e969617 100644
--- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
@@ -3979,6 +3979,15 @@
F3950CD8212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
F3950CD9212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
F3950CDA212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ F3A4909E2554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A4909F2554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A02554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A12554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A22554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A32554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A42554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A52554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
+ F3A490A62554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
FA24348B21D41FFB00B8918A /* SDL_metal.h in Headers */ = {isa = PBXBuildFile; fileRef = FA24348A21D41FFB00B8918A /* SDL_metal.h */; settings = {ATTRIBUTES = (Public, ); }; };
FA73671D19A540EF004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; };
FA73671F19A54144004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; };
@@ -4531,6 +4540,7 @@
F37DC5F225350EBC0002E6F7 /* CoreHaptics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreHaptics.framework; path = System/Library/Frameworks/CoreHaptics.framework; sourceTree = SDKROOT; };
F37DC5F425350ECC0002E6F7 /* CoreHaptics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreHaptics.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS14.0.sdk/System/Library/Frameworks/CoreHaptics.framework; sourceTree = DEVELOPER_DIR; };
F3950CD7212BC88D00F51292 /* SDL_sensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor.h; sourceTree = "<group>"; };
+ F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_ps5.c; sourceTree = "<group>"; };
F59C710300D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = "<group>"; };
F59C710600D5CB5801000001 /* SDL.info */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = SDL.info; sourceTree = "<group>"; };
F5A2EF3900C6A39A01000001 /* BUGS.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; name = BUGS.txt; path = ../../BUGS.txt; sourceTree = SOURCE_ROOT; };
@@ -5448,6 +5458,7 @@
children = (
A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */,
A7D8A7C323E2513E00DCD162 /* SDL_hidapi_ps4.c */,
+ F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */,
A75FDBC423EA380300529352 /* SDL_hidapi_rumble.c */,
A75FDBC323EA380300529352 /* SDL_hidapi_rumble.h */,
A75FDAAC23E2795C00529352 /* SDL_hidapi_steam.c */,
@@ -8471,6 +8482,7 @@
A75FCDE923E25AB700529352 /* SDL_drawline.c in Sources */,
A75FCDEA23E25AB700529352 /* SDL_yuv.c in Sources */,
A75FCDEB23E25AB700529352 /* SDL_sysfilesystem.m in Sources */,
+ F3A490A52554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A75FCDEC23E25AB700529352 /* e_pow.c in Sources */,
A75FCDED23E25AB700529352 /* SDL_systls.c in Sources */,
A75FCDEE23E25AB700529352 /* SDL_vulkan_utils.c in Sources */,
@@ -8671,6 +8683,7 @@
A75FCFA223E25AC700529352 /* SDL_drawline.c in Sources */,
A75FCFA323E25AC700529352 /* SDL_yuv.c in Sources */,
A75FCFA423E25AC700529352 /* SDL_sysfilesystem.m in Sources */,
+ F3A490A62554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A75FCFA523E25AC700529352 /* e_pow.c in Sources */,
A75FCFA623E25AC700529352 /* SDL_systls.c in Sources */,
A75FCFA723E25AC700529352 /* SDL_vulkan_utils.c in Sources */,
@@ -8979,6 +8992,7 @@
A769B1C523E259AE00872273 /* SDL_wave.c in Sources */,
A769B1C623E259AE00872273 /* s_tan.c in Sources */,
A769B1C723E259AE00872273 /* SDL_hints.c in Sources */,
+ F3A490A32554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A769B1C823E259AE00872273 /* SDL_hidapi_ps4.c in Sources */,
A769B1C923E259AE00872273 /* SDL_pixels.c in Sources */,
A769B1CA23E259AE00872273 /* SDL_x11clipboard.c in Sources */,
@@ -9265,6 +9279,7 @@
A7D8BA5C23E2514400DCD162 /* SDL_shaders_gles2.c in Sources */,
A7D8B14123E2514200DCD162 /* SDL_blit_1.c in Sources */,
5605720F2473688000B46B66 /* SDL_locale.c in Sources */,
+ F3A4909F2554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A7D8B17D23E2514200DCD162 /* SDL_x11dyn.c in Sources */,
A7D8BB1623E2514500DCD162 /* SDL_mouse.c in Sources */,
A7D8BADA23E2514500DCD162 /* e_rem_pio2.c in Sources */,
@@ -9464,6 +9479,7 @@
A7D8BA5D23E2514400DCD162 /* SDL_shaders_gles2.c in Sources */,
A7D8B14223E2514200DCD162 /* SDL_blit_1.c in Sources */,
560572102473688000B46B66 /* SDL_locale.c in Sources */,
+ F3A490A02554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A7D8B17E23E2514200DCD162 /* SDL_x11dyn.c in Sources */,
A7D8BB1723E2514500DCD162 /* SDL_mouse.c in Sources */,
A7D8BADB23E2514500DCD162 /* e_rem_pio2.c in Sources */,
@@ -9578,6 +9594,7 @@
A7D8B76E23E2514300DCD162 /* SDL_wave.c in Sources */,
A7D8BAD723E2514500DCD162 /* s_tan.c in Sources */,
A7D8AA6923E2514000DCD162 /* SDL_hints.c in Sources */,
+ F3A490A22554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A7D8B54323E2514300DCD162 /* SDL_hidapi_ps4.c in Sources */,
A7D8AD7223E2514100DCD162 /* SDL_pixels.c in Sources */,
A7D8B1A423E2514200DCD162 /* SDL_x11clipboard.c in Sources */,
@@ -9835,6 +9852,7 @@
A7D8B54B23E2514300DCD162 /* SDL_hidapi_xboxone.c in Sources */,
A7D8AD2323E2514100DCD162 /* SDL_blit_auto.c in Sources */,
A7D8B1A623E2514200DCD162 /* SDL_x11keyboard.c in Sources */,
+ F3A4909E2554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A7D8BB6923E2514500DCD162 /* SDL_keyboard.c in Sources */,
A7D8ACE723E2514100DCD162 /* SDL_rect.c in Sources */,
A7D8AE9A23E2514100DCD162 /* SDL_cocoaopengles.m in Sources */,
@@ -9898,6 +9916,7 @@
A7D8AD2C23E2514100DCD162 /* SDL_vulkan_utils.c in Sources */,
A7D8A95423E2514000DCD162 /* SDL_spinlock.c in Sources */,
A7D8BAB223E2514400DCD162 /* s_atan.c in Sources */,
+ F3A490A12554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A7D8B75523E2514300DCD162 /* SDL_sysloadso.c in Sources */,
A7D8B98923E2514400DCD162 /* SDL_render_metal.m in Sources */,
A7D8AE7923E2514100DCD162 /* SDL_clipboard.c in Sources */,
@@ -10096,6 +10115,7 @@
A7D8A95623E2514000DCD162 /* SDL_spinlock.c in Sources */,
A7D8BAB423E2514400DCD162 /* s_atan.c in Sources */,
A7D8B75723E2514300DCD162 /* SDL_sysloadso.c in Sources */,
+ F3A490A42554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A7D8B98B23E2514400DCD162 /* SDL_render_metal.m in Sources */,
A7D8AE7B23E2514100DCD162 /* SDL_clipboard.c in Sources */,
A7D8AEC923E2514100DCD162 /* SDL_cocoaevents.m in Sources */,
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 70319ac..a9dbbae 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -493,6 +493,7 @@ extern "C" {
* XboxOne
* PS3
* PS4
+ * PS5
* SwitchPro
*
* This hint affects what driver is used, and must be set before calling SDL_Init(SDL_INIT_GAMECONTROLLER)
@@ -604,6 +605,17 @@ extern "C" {
#define SDL_HINT_JOYSTICK_HIDAPI_PS4 "SDL_JOYSTICK_HIDAPI_PS4"
/**
+ * \brief A variable controlling whether the HIDAPI driver for PS5 controllers should be used.
+ *
+ * This variable can be set to the following values:
+ * "0" - HIDAPI driver is not used
+ * "1" - HIDAPI driver is used
+ *
+ * The default is the value of SDL_HINT_JOYSTICK_HIDAPI
+ */
+#define SDL_HINT_JOYSTICK_HIDAPI_PS5 "SDL_JOYSTICK_HIDAPI_PS5"
+
+/**
* \brief A variable controlling whether extended input reports should be used for PS4 controllers when using the HIDAPI driver.
*
* This variable can be set to the following values:
diff --git a/src/joystick/controller_type.h b/src/joystick/controller_type.h
index cf079f7..6fb50d4 100644
--- a/src/joystick/controller_type.h
+++ b/src/joystick/controller_type.h
@@ -629,6 +629,10 @@ static SDL_INLINE EControllerType GuessControllerType( int nVID, int nPID )
{
return k_eControllerType_PS4Controller;
}
+ if ( SDL_strncasecmp( pszOverride, "PS5", 3 ) == 0 )
+ {
+ return k_eControllerType_PS5Controller;
+ }
if ( SDL_strncasecmp( pszOverride, "SwitchPro", 9 ) == 0 )
{
return k_eControllerType_SwitchProController;
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index b044b9b..173e1ed 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -80,19 +80,6 @@ typedef struct
typedef struct
{
- Uint8 ucLeftJoystickX;
- Uint8 ucLeftJoystickY;
- Uint8 ucRightJoystickX;
- Uint8 ucRightJoystickY;
- Uint8 ucTriggerLeft;
- Uint8 ucTriggerRight;
- Uint8 ucCounter;
- Uint8 rgucButtonsAndHat[ 3 ];
- Uint8 rgucUnknown[ 53 ];
-} PS5StatePacket_t;
-
-typedef struct
-{
Uint8 ucRumbleRight;
Uint8 ucRumbleLeft;
Uint8 ucLedRed;
@@ -108,7 +95,6 @@ typedef struct
} DS4EffectsState_t;
typedef struct {
- SDL_bool is_PS5;
SDL_bool is_dongle;
SDL_bool is_bluetooth;
SDL_bool audio_supported;
@@ -122,40 +108,10 @@ typedef struct {
Uint8 led_blue;
Uint8 volume;
Uint32 last_volume_check;
- union
- {
- PS4StatePacket_t PS4;
- PS5StatePacket_t PS5;
- } last_state;
+ PS4StatePacket_t last_state;
} SDL_DriverPS4_Context;
-/* Define this if you want to log all packets from the controller */
-/*#define DEBUG_PS4_PROTOCOL*/
-#define DEBUG_PS4_PROTOCOL
-
-#ifdef DEBUG_PS4_PROTOCOL
-static void
-DumpPacket(const char *prefix, Uint8 *data, int size)
-{
- int i;
- char *buffer;
- size_t length = SDL_strlen(prefix) + 11*(USB_PACKET_LENGTH/8) + (5*USB_PACKET_LENGTH) + 1 + 1;
-
- buffer = (char *)SDL_malloc(length);
- SDL_snprintf(buffer, length, prefix, size);
- for (i = 0; i < size; ++i) {
- if ((i % 8) == 0) {
- SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), "\n%.2d: ", i);
- }
- SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), " 0x%.2x", data[i]);
- }
- SDL_strlcat(buffer, "\n", length);
- SDL_Log("%s", buffer);
- SDL_free(buffer);
-}
-#endif /* DEBUG_PS4_PROTOCOL */
-
/* Public domain CRC implementation adapted from:
http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
*/
@@ -180,18 +136,14 @@ static Uint32 crc32(Uint32 crc, const void *data, int count)
static SDL_bool
HIDAPI_DriverPS4_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
{
- return (type == SDL_CONTROLLER_TYPE_PS4 || type == SDL_CONTROLLER_TYPE_PS5);
+ return (type == SDL_CONTROLLER_TYPE_PS4);
}
static const char *
HIDAPI_DriverPS4_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
{
if (vendor_id == USB_VENDOR_SONY) {
- if (SDL_IsJoystickPS5(vendor_id, product_id)) {
- return "PS5 Controller";
- } else {
- return "PS4 Controller";
- }
+ return "PS4 Controller";
}
return NULL;
}
@@ -339,14 +291,6 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
}
}
- /* Special handling for PS5 controller */
- ctx->is_PS5 = SDL_IsJoystickPS5(device->vendor_id, device->product_id);
- if (ctx->is_PS5) {
- /* Don't know how these work yet... */
- ctx->audio_supported = SDL_FALSE;
- ctx->rumble_supported = SDL_FALSE;
- }
-
/* Initialize player index (needed for setting LEDs) */
ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
@@ -424,9 +368,7 @@ HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
static SDL_bool
HIDAPI_DriverPS4_HasJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
- SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
-
- return ctx->rumble_supported;
+ return SDL_TRUE;
}
static int
@@ -439,15 +381,16 @@ HIDAPI_DriverPS4_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
ctx->led_green = green;
ctx->led_blue = blue;
+ /* FIXME: Is there a better way to send this without sending another rumble packet? */
return HIDAPI_DriverPS4_RumbleJoystick(device, joystick, ctx->rumble_left, ctx->rumble_right);
}
static void
-HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS4StatePacket_t *packet, int size)
+HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS4StatePacket_t *packet)
{
Sint16 axis;
- if (ctx->last_state.PS4.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
+ if (ctx->last_state.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
{
Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
@@ -502,7 +445,7 @@ HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_
}
}
- if (ctx->last_state.PS4.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
+ if (ctx->last_state.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
Uint8 data = packet->rgucButtonsHatAndCounter[1];
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
@@ -522,7 +465,7 @@ HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_
packet->ucTriggerRight = (data & 0x08) && packet->ucTriggerRight == 0 ? 255 : packet->ucTriggerRight;
}
- if (ctx->last_state.PS4.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
+ if (ctx->last_state.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
@@ -542,119 +485,23 @@ HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_
axis = ((int)packet->ucRightJoystickY * 257) - 32768;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
- if (size >= 30) {
- if (packet->ucBatteryLevel & 0x10) {
- joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
+ if (packet->ucBatteryLevel & 0x10) {
+ joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
+ } else {
+ /* Battery level ranges from 0 to 10 */
+ int level = (packet->ucBatteryLevel & 0xF);
+ if (level == 0) {
+ joystick->epowerlevel = SDL_JOYSTICK_POWER_EMPTY;
+ } else if (level <= 2) {
+ joystick->epowerlevel = SDL_JOYSTICK_POWER_LOW;
+ } else if (level <= 7) {
+ joystick->epowerlevel = SDL_JOYSTICK_POWER_MEDIUM;
} else {
- /* Battery level ranges from 0 to 10 */
- int level = (packet->ucBatteryLevel & 0xF);
- if (level == 0) {
- joystick->epowerlevel = SDL_JOYSTICK_POWER_EMPTY;
- } else if (level <= 2) {
- joystick->epowerlevel = SDL_JOYSTICK_POWER_LOW;
- } else if (level <= 7) {
- joystick->epowerlevel = SDL_JOYSTICK_POWER_MEDIUM;
- } else {
- joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
- }
+ joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
}
}
- SDL_memcpy(&ctx->last_state.PS4, packet, SDL_min(size, sizeof(ctx->last_state.PS4)));
-}
-
-static void
-HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS5StatePacket_t *packet)
-{
- Sint16 axis;
-
- if (ctx->last_state.PS5.rgucButtonsAndHat[0] != packet->rgucButtonsAndHat[0]) {
- {
- Uint8 data = (packet->rgucButtonsAndHat[0] >> 4);
-
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
- }
- {
- Uint8 data = (packet->rgucButtonsAndHat[0] & 0x0F);
- SDL_bool dpad_up = SDL_FALSE;
- SDL_bool dpad_down = SDL_FALSE;
- SDL_bool dpad_left = SDL_FALSE;
- SDL_bool dpad_right = SDL_FALSE;
-
- switch (data) {
- case 0:
- dpad_up = SDL_TRUE;
- break;
- case 1:
- dpad_up = SDL_TRUE;
- dpad_right = SDL_TRUE;
- break;
- case 2:
- dpad_right = SDL_TRUE;
- break;
- case 3:
- dpad_right = SDL_TRUE;
- dpad_down = SDL_TRUE;
- break;
- case 4:
- dpad_down = SDL_TRUE;
- break;
- case 5:
- dpad_left = SDL_TRUE;
- dpad_down = SDL_TRUE;
- break;
- case 6:
- dpad_left = SDL_TRUE;
- break;
- case 7:
- dpad_up = SDL_TRUE;
- dpad_left = SDL_TRUE;
- break;
- default:
- break;
- }
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
- }
- }
-
- if (ctx->last_state.PS5.rgucButtonsAndHat[1] != packet->rgucButtonsAndHat[1]) {
- Uint8 data = packet->rgucButtonsAndHat[1];
-
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
- }
-
- if (ctx->last_state.PS5.rgucButtonsAndHat[2] != packet->rgucButtonsAndHat[2]) {
- Uint8 data = (packet->rgucButtonsAndHat[2] & 0x03);
-
- SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
- SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
- }
-
- axis = ((int)packet->ucTriggerLeft * 257) - 32768;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
- axis = ((int)packet->ucTriggerRight * 257) - 32768;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
- axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
- axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
- axis = ((int)packet->ucRightJoystickX * 257) - 32768;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
- axis = ((int)packet->ucRightJoystickY * 257) - 32768;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
-
- SDL_memcpy(&ctx->last_state.PS5, packet, sizeof(ctx->last_state.PS5));
+ SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
}
static SDL_bool
@@ -673,20 +520,13 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
}
while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
-#ifdef DEBUG_PS4_PROTOCOL
- DumpPacket("PS4 packet: size = %d", data, size);
-#endif
switch (data[0]) {
case k_EPS4ReportIdUsbState:
- if (ctx->is_PS5 && size > 10) {
- HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[1]);
- } else {
- HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[1], size - 1);
- }
+ HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[1]);
break;
case k_EPS4ReportIdBluetoothState:
/* Bluetooth state packets have two additional bytes at the beginning */
- HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[3], size - 3);
+ HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[3]);
break;
default:
#ifdef DEBUG_JOYSTICK
diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
new file mode 100644
index 0000000..f3910bf
--- /dev/null
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -0,0 +1,437 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_JOYSTICK_HIDAPI
+
+#include "SDL_hints.h"
+#include "SDL_events.h"
+#include "SDL_joystick.h"
+#include "SDL_gamecontroller.h"
+#include "../SDL_sysjoystick.h"
+#include "SDL_hidapijoystick_c.h"
+
+
+#ifdef SDL_JOYSTICK_HIDAPI_PS5
+
+typedef struct
+{
+ Uint8 ucLeftJoystickX;
+ Uint8 ucLeftJoystickY;
+ Uint8 ucRightJoystickX;
+ Uint8 ucRightJoystickY;
+ Uint8 rgucButtonsHatAndCounter[ 3 ];
+ Uint8 ucTriggerLeft;
+ Uint8 ucTriggerRight;
+} PS5SimpleStatePacket_t;
+
+typedef struct
+{
+ Uint8 ucLeftJoystickX;
+ Uint8 ucLeftJoystickY;
+ Uint8 ucRightJoystickX;
+ Uint8 ucRightJoystickY;
+ Uint8 ucTriggerLeft;
+ Uint8 ucTriggerRight;
+ Uint8 ucCounter;
+ Uint8 rgucButtonsAndHat[ 3 ];
+ Uint8 rgucUnknown[ 53 ];
+} PS5StatePacket_t;
+
+typedef struct {
+ union
+ {
+ PS5SimpleStatePacket_t simple;
+ PS5StatePacket_t state;
+ } last_state;
+} SDL_DriverPS5_Context;
+
+
+/* Define this if you want to log all packets from the controller */
+/*#define DEBUG_PS5_PROTOCOL*/
+
+#ifdef DEBUG_PS5_PROTOCOL
+static void
+DumpPacket(const char *prefix, Uint8 *data, int size)
+{
+ int i;
+ char *buffer;
+ size_t length = SDL_strlen(prefix) + 11*(USB_PACKET_LENGTH/8) + (5*USB_PACKET_LENGTH) + 1 + 1;
+
+ buffer = (char *)SDL_malloc(length);
+ SDL_snprintf(buffer, length, prefix, size);
+ for (i = 0; i < size; ++i) {
+ if ((i % 8) == 0) {
+ SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), "\n%.2d: ", i);
+ }
+ SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), " 0x%.2x", data[i]);
+ }
+ SDL_strlcat(buffer, "\n", length);
+ SDL_Log("%s", buffer);
+ SDL_free(buffer);
+}
+#endif /* DEBUG_PS5_PROTOCOL */
+
+static SDL_bool
+HIDAPI_DriverPS5_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
+{
+ return (type == SDL_CONTROLLER_TYPE_PS5);
+}
+
+static const char *
+HIDAPI_DriverPS5_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
+{
+ if (vendor_id == USB_VENDOR_SONY) {
+ return "PS5 Controller";
+ }
+ return NULL;
+}
+
+static SDL_bool
+HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
+{
+ return HIDAPI_JoystickConnected(device, NULL, SDL_FALSE);
+}
+
+static int
+HIDAPI_DriverPS5_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
+{
+ return -1;
+}
+
+static void
+HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
+{
+}
+
+static SDL_bool
+HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
+{
+ SDL_DriverPS5_Context *ctx;
+
+ ctx = (SDL_DriverPS5_Context *)SDL_calloc(1, sizeof(*ctx));
+ if (!ctx) {
+ SDL_OutOfMemory();
+ return SDL_FALSE;
+ }
+
+ device->dev = hid_open_path(device->path, 0);
+ if (!device->dev) {
+ SDL_free(ctx);
+ SDL_SetError("Couldn't open %s", device->path);
+ return SDL_FALSE;
+ }
+ device->context = ctx;
+
+ /* Initialize the joystick capabilities */
+ joystick->nbuttons = 16;
+ joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
+
+ return SDL_TRUE;
+}
+
+static int
+HIDAPI_DriverPS5_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+{
+ return SDL_Unsupported();
+}
+
+static SDL_bool
+HIDAPI_DriverPS5_HasJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
+{
+ return SDL_FALSE;
+}
+
+static int
+HIDAPI_DriverPS5_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
+{
+ return SDL_Unsupported();
+}
+
+static void
+HIDAPI_DriverPS5_HandleSimpleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS5_Context *ctx, PS5SimpleStatePacket_t *packet)
+{
+ Sint16 axis;
+
+ if (ctx->last_state.simple.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
+ {
+ Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
+
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
+ }
+ {
+ Uint8 data = (packet->rgucButtonsHatAndCounter[0] & 0x0F);
+ SDL_bool dpad_up = SDL_FALSE;
+ SDL_bool dpad_down = SDL_FALSE;
+ SDL_bool dpad_left = SDL_FALSE;
+ SDL_bool dpad_right = SDL_FALSE;
+
+ switch (data) {
+ case 0:
+ dpad_up = SDL_TRUE;
+ break;
+ case 1:
+ dpad_up = SDL_TRUE;
+ dpad_right = SDL_TRUE;
+ break;
+ case 2:
+ dpad_right = SDL_TRUE;
+ break;
+ case 3:
+ dpad_right = SDL_TRUE;
+ dpad_down = SDL_TRUE;
+ break;
+ case 4:
+ dpad_down = SDL_TRUE;
+ break;
+ case 5:
+ dpad_left = SDL_TRUE;
+ dpad_down = SDL_TRUE;
+ break;
+ case 6:
+ dpad_left = SDL_TRUE;
+ break;
+ case 7:
+ dpad_up = SDL_TRUE;
+ dpad_left = SDL_TRUE;
+ break;
+ default:
+ break;
+ }
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
+ }
+ }
+
+ if (ctx->last_state.simple.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
+ Uint8 data = packet->rgucButtonsHatAndCounter[1];
+
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
+ }
+
+ if (ctx->last_state.simple.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
+ Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
+
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ }
+
+ axis = ((int)packet->ucTriggerLeft * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
+ axis = ((int)packet->ucTriggerRight * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
+ axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
+ axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
+ axis = ((int)packet->ucRightJoystickX * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
+ axis = ((int)packet->ucRightJoystickY * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
+
+ SDL_memcpy(&ctx->last_state.simple, packet, sizeof(ctx->last_state.simple));
+}
+
+static void
+HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacket_t *packet)
+{
+ Sint16 axis;
+
+ if (ctx->last_state.state.rgucButtonsAndHat[0] != packet->rgucButtonsAndHat[0]) {
+ {
+ Uint8 data = (packet->rgucButtonsAndHat[0] >> 4);
+
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
+ }
+ {
+ Uint8 data = (packet->rgucButtonsAndHat[0] & 0x0F);
+ SDL_bool dpad_up = SDL_FALSE;
+ SDL_bool dpad_down = SDL_FALSE;
+ SDL_bool dpad_left = SDL_FALSE;
+ SDL_bool dpad_right = SDL_FALSE;
+
+ switch (data) {
+ case 0:
+ dpad_up = SDL_TRUE;
+ break;
+ case 1:
+ dpad_up = SDL_TRUE;
+ dpad_right = SDL_TRUE;
+ break;
+ case 2:
+ dpad_right = SDL_TRUE;
+ break;
+ case 3:
+ dpad_right = SDL_TRUE;
+ dpad_down = SDL_TRUE;
+ break;
+ case 4:
+ dpad_down = SDL_TRUE;
+ break;
+ case 5:
+ dpad_left = SDL_TRUE;
+ dpad_down = SDL_TRUE;
+ break;
+ case 6:
+ dpad_left = SDL_TRUE;
+ break;
+ case 7:
+ dpad_up = SDL_TRUE;
+ dpad_left = SDL_TRUE;
+ break;
+ default:
+ break;
+ }
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
+ }
+ }
+
+ if (ctx->last_state.state.rgucButtonsAndHat[1] != packet->rgucButtonsAndHat[1]) {
+ Uint8 data = packet->rgucButtonsAndHat[1];
+
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
+ }
+
+ if (ctx->last_state.state.rgucButtonsAndHat[2] != packet->rgucButtonsAndHat[2]) {
+ Uint8 data = (packet->rgucButtonsAndHat[2] & 0x03);
+
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ }
+
+ axis = ((int)packet->ucTriggerLeft * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
+ axis = ((int)packet->ucTriggerRight * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
+ axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
+ axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
+ axis = ((int)packet->ucRightJoystickX * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
+ axis = ((int)packet->ucRightJoystickY * 257) - 32768;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
+
+ SDL_memcpy(&ctx->last_state.state, packet, sizeof(ctx->last_state.state));
+}
+
+static SDL_bool
+HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
+{
+ SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
+ SDL_Joystick *joystick = NULL;
+ Uint8 data[USB_PACKET_LENGTH];
+ int size;
+
+ if (device->num_joysticks > 0) {
+ joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
+ }
+ if (!joystick) {
+ return SDL_FALSE;
+ }
+
+ while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
+#ifdef DEBUG_PS5_PROTOCOL
+ DumpPacket("PS5 packet: size = %d", data, size);
+#endif
+ switch (data[0]) {
+ case 0x01:
+ if (size == 10) {
+ HIDAPI_DriverPS5_HandleSimpleStatePacket(joystick, device->dev, ctx, (PS5SimpleStatePacket_t *)&data[1]);
+ } else {
+ HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[1]);
+ }
+ break;
+ default:
+#ifdef DEBUG_JOYSTICK
+ SDL_Log("Unknown PS5 packet: 0x%.2x\n", data[0]);
+#endif
+ break;
+ }
+ }
+
+ if (size < 0) {
+ /* Read error, device is disconnected */
+ HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
+ }
+ return (size >= 0);
+}
+
+static void
+HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
+{
+ hid_close(device->dev);
+ device->dev = NULL;
+
+ SDL_free(device->context);
+ device->context = NULL;
+}
+
+static void
+HIDAPI_DriverPS5_FreeDevice(SDL_HIDAPI_Device *device)
+{
+}
+
+SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5 =
+{
+ SDL_HINT_JOYSTICK_HIDAPI_PS5,
+ SDL_TRUE,
+ HIDAPI_DriverPS5_IsSupportedDevice,
+ HIDAPI_DriverPS5_GetDeviceName,
+ HIDAPI_DriverPS5_InitDevice,
+ HIDAPI_DriverPS5_GetDevicePlayerIndex,
+ HIDAPI_DriverPS5_SetDevicePlayerIndex,
+ HIDAPI_DriverPS5_UpdateDevice,
+ HIDAPI_DriverPS5_OpenJoystick,
+ HIDAPI_DriverPS5_RumbleJoystick,
+ HIDAPI_DriverPS5_HasJoystickLED,
+ HIDAPI_DriverPS5_SetJoystickLED,
+ HIDAPI_DriverPS5_CloseJoystick,
+ HIDAPI_DriverPS5_FreeDevice,
+ NULL
+};
+
+#endif /* SDL_JOYSTICK_HIDAPI_PS5 */
+
+#endif /* SDL_JOYSTICK_HIDAPI */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 2ce2f13..3872b6a 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -63,6 +63,9 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
#ifdef SDL_JOYSTICK_HIDAPI_PS4
&SDL_HIDAPI_DriverPS4,
#endif
+#ifdef SDL_JOYSTICK_HIDAPI_PS5
+ &SDL_HIDAPI_DriverPS5,
+#endif
#ifdef SDL_JOYSTICK_HIDAPI_STEAM
&SDL_HIDAPI_DriverSteam,
#endif
diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h
index f88542b..5b13c20 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick_c.h
+++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h
@@ -32,6 +32,7 @@
/* This is the full set of HIDAPI drivers available */
#define SDL_JOYSTICK_HIDAPI_PS4
+#define SDL_JOYSTICK_HIDAPI_PS5
#define SDL_JOYSTICK_HIDAPI_SWITCH
#define SDL_JOYSTICK_HIDAPI_XBOX360
#define SDL_JOYSTICK_HIDAPI_XBOXONE
@@ -108,6 +109,7 @@ typedef struct _SDL_HIDAPI_DeviceDriver
/* HIDAPI device support */
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4;
+extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;