File size: 63,624 Bytes
18a519f | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 | #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WSA || PACKAGE_DOCS_GENERATION
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.DualShock.LowLevel;
using UnityEngine.InputSystem.Utilities;
////TODO: figure out sensor formats and add support for acceleration, angularVelocity, and orientation (also add to base layout then)
namespace UnityEngine.InputSystem.DualShock.LowLevel
{
/// <summary>
/// This is abstract input report for PS5 DualSense controller, similar to what is on the wire, but not exactly binary matching any state events.
/// See ConvertInputReport for the exact conversion.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 9 /* !!! Beware !!! If you plan to increase this, think about how you gonna fit 10 byte state events because we can only shrink events in IEventPreProcessor */)]
internal struct DualSenseHIDInputReport : IInputStateTypeInfo
{
public static FourCC Format = new FourCC('D', 'S', 'V', 'S'); // DualSense Virtual State
public FourCC format => Format;
[InputControl(name = "leftStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "leftStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "leftStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(0)] public byte leftStickX;
[FieldOffset(1)] public byte leftStickY;
[InputControl(name = "rightStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "rightStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "rightStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(2)] public byte rightStickX;
[FieldOffset(3)] public byte rightStickY;
[InputControl(name = "leftTrigger", format = "BYTE")]
[FieldOffset(4)] public byte leftTrigger;
[InputControl(name = "rightTrigger", format = "BYTE")]
[FieldOffset(5)] public byte rightTrigger;
[InputControl(name = "dpad", format = "BIT", layout = "Dpad", sizeInBits = 4, defaultState = 8)]
[InputControl(name = "dpad/up", format = "BIT", layout = "DiscreteButton", parameters = "minValue=7,maxValue=1,nullValue=8,wrapAtValue=7", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/right", format = "BIT", layout = "DiscreteButton", parameters = "minValue=1,maxValue=3", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/down", format = "BIT", layout = "DiscreteButton", parameters = "minValue=3,maxValue=5", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/left", format = "BIT", layout = "DiscreteButton", parameters = "minValue=5, maxValue=7", bit = 0, sizeInBits = 4)]
[InputControl(name = "buttonWest", displayName = "Square", bit = 4)]
[InputControl(name = "buttonSouth", displayName = "Cross", bit = 5)]
[InputControl(name = "buttonEast", displayName = "Circle", bit = 6)]
[InputControl(name = "buttonNorth", displayName = "Triangle", bit = 7)]
[FieldOffset(6)] public byte buttons0;
[InputControl(name = "leftShoulder", bit = 0)]
[InputControl(name = "rightShoulder", bit = 1)]
[InputControl(name = "leftTriggerButton", layout = "Button", bit = 2)]
[InputControl(name = "rightTriggerButton", layout = "Button", bit = 3)]
[InputControl(name = "select", displayName = "Share", bit = 4)]
[InputControl(name = "start", displayName = "Options", bit = 5)]
[InputControl(name = "leftStickPress", bit = 6)]
[InputControl(name = "rightStickPress", bit = 7)]
[FieldOffset(7)] public byte buttons1;
[InputControl(name = "systemButton", layout = "Button", displayName = "System", bit = 0)]
[InputControl(name = "touchpadButton", layout = "Button", displayName = "Touchpad Press", bit = 1)]
[InputControl(name = "micButton", layout = "Button", displayName = "Mic Mute", bit = 2)]
[FieldOffset(8)] public byte buttons2;
}
[StructLayout(LayoutKind.Explicit, Size = 47)]
internal struct DualSenseHIDOutputReportPayload
{
[FieldOffset(0)] public byte enableFlags1;
[FieldOffset(1)] public byte enableFlags2;
[FieldOffset(2)] public byte highFrequencyMotorSpeed;
[FieldOffset(3)] public byte lowFrequencyMotorSpeed;
[FieldOffset(44)] public byte redColor;
[FieldOffset(45)] public byte greenColor;
[FieldOffset(46)] public byte blueColor;
}
[StructLayout(LayoutKind.Explicit, Size = kSize)]
internal struct DualSenseHIDUSBOutputReport : IInputDeviceCommandInfo
{
public static FourCC Type => new FourCC('H', 'I', 'D', 'O');
public FourCC typeStatic => Type;
internal const int kSize = InputDeviceCommand.BaseCommandSize + 48;
[FieldOffset(0)] public InputDeviceCommand baseCommand;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 0)] public byte reportId;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 1)] public DualSenseHIDOutputReportPayload payload;
public static DualSenseHIDUSBOutputReport Create(DualSenseHIDOutputReportPayload payload)
{
return new DualSenseHIDUSBOutputReport
{
baseCommand = new InputDeviceCommand(Type, kSize),
reportId = 2,
payload = payload
};
}
}
[StructLayout(LayoutKind.Explicit, Size = kSize)]
internal struct DualSenseHIDBluetoothOutputReport : IInputDeviceCommandInfo
{
public static FourCC Type => new FourCC('H', 'I', 'D', 'O');
public FourCC typeStatic => Type;
internal const int kSize = InputDeviceCommand.BaseCommandSize + 78;
[FieldOffset(0)] public InputDeviceCommand baseCommand;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 0)] public byte reportId;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 1)] public byte tag1;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 2)] public byte tag2;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 3)] public DualSenseHIDOutputReportPayload payload;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 74)] public uint crc32;
[FieldOffset(InputDeviceCommand.BaseCommandSize + 0)] public unsafe fixed byte rawData[74];
public static DualSenseHIDBluetoothOutputReport Create(DualSenseHIDOutputReportPayload payload, byte outputSequenceId)
{
var report = new DualSenseHIDBluetoothOutputReport
{
baseCommand = new InputDeviceCommand(Type, kSize),
reportId = 0x31,
tag1 = (byte)((outputSequenceId & 0xf) << 4),
tag2 = 0x10,
payload = payload
};
////FIXME: Calculate crc32 correctly
return report;
}
}
/// <summary>
/// Structure of HID input reports for PS4 DualShock 4 controllers.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 9 /* !!! Beware !!! If you plan to increase this, think about how you gonna fit 10 byte state events because we can only shrink events in IEventPreProcessor */)]
internal struct DualShock4HIDInputReport : IInputStateTypeInfo
{
public static FourCC Format = new FourCC('D', '4', 'V', 'S'); // DualShock4 Virtual State
public FourCC format => Format;
[InputControl(name = "leftStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "leftStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "leftStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(0)] public byte leftStickX;
[FieldOffset(1)] public byte leftStickY;
[InputControl(name = "rightStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "rightStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "rightStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(2)] public byte rightStickX;
[FieldOffset(3)] public byte rightStickY;
[InputControl(name = "dpad", format = "BIT", layout = "Dpad", sizeInBits = 4, defaultState = 8)]
[InputControl(name = "dpad/up", format = "BIT", layout = "DiscreteButton", parameters = "minValue=7,maxValue=1,nullValue=8,wrapAtValue=7", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/right", format = "BIT", layout = "DiscreteButton", parameters = "minValue=1,maxValue=3", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/down", format = "BIT", layout = "DiscreteButton", parameters = "minValue=3,maxValue=5", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/left", format = "BIT", layout = "DiscreteButton", parameters = "minValue=5, maxValue=7", bit = 0, sizeInBits = 4)]
[InputControl(name = "buttonWest", displayName = "Square", bit = 4)]
[InputControl(name = "buttonSouth", displayName = "Cross", bit = 5)]
[InputControl(name = "buttonEast", displayName = "Circle", bit = 6)]
[InputControl(name = "buttonNorth", displayName = "Triangle", bit = 7)]
[FieldOffset(4)] public byte buttons1;
[InputControl(name = "leftShoulder", bit = 0)]
[InputControl(name = "rightShoulder", bit = 1)]
[InputControl(name = "leftTriggerButton", layout = "Button", bit = 2, synthetic = true)]
[InputControl(name = "rightTriggerButton", layout = "Button", bit = 3, synthetic = true)]
[InputControl(name = "select", displayName = "Share", bit = 4)]
[InputControl(name = "start", displayName = "Options", bit = 5)]
[InputControl(name = "leftStickPress", bit = 6)]
[InputControl(name = "rightStickPress", bit = 7)]
[FieldOffset(5)] public byte buttons2;
[InputControl(name = "systemButton", layout = "Button", displayName = "System", bit = 0)]
[InputControl(name = "touchpadButton", layout = "Button", displayName = "Touchpad Press", bit = 1)]
[FieldOffset(6)] public byte buttons3;
[InputControl(name = "leftTrigger", format = "BYTE")]
[FieldOffset(7)] public byte leftTrigger;
[InputControl(name = "rightTrigger", format = "BYTE")]
[FieldOffset(8)] public byte rightTrigger;
}
/// <summary>
/// Structure of HID input reports for PS3 DualShock 3 controllers.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 32)]
internal unsafe struct DualShock3HIDInputReport : IInputStateTypeInfo
{
[FieldOffset(0)] private ushort padding1;
[InputControl(name = "select", displayName = "Share", bit = 0)]
[InputControl(name = "leftStickPress", bit = 1)]
[InputControl(name = "rightStickPress", bit = 2)]
[InputControl(name = "start", displayName = "Options", bit = 3)]
[InputControl(name = "dpad", format = "BIT", layout = "Dpad", bit = 4, sizeInBits = 4)]
[InputControl(name = "dpad/up", bit = 4)]
[InputControl(name = "dpad/right", bit = 5)]
[InputControl(name = "dpad/down", bit = 6)]
[InputControl(name = "dpad/left", bit = 7)]
[FieldOffset(2)] public byte buttons1;
[InputControl(name = "leftTriggerButton", layout = "Button", bit = 0, synthetic = true)]
[InputControl(name = "rightTriggerButton", layout = "Button", bit = 1, synthetic = true)]
[InputControl(name = "leftShoulder", bit = 2)]
[InputControl(name = "rightShoulder", bit = 3)]
[InputControl(name = "buttonNorth", displayName = "Triangle", bit = 4)]
[InputControl(name = "buttonEast", displayName = "Circle", bit = 5)]
[InputControl(name = "buttonSouth", displayName = "Cross", bit = 6)]
[InputControl(name = "buttonWest", displayName = "Square", bit = 7)]
[FieldOffset(3)] public byte buttons2;
[InputControl(name = "systemButton", layout = "Button", displayName = "System", bit = 0)]
[InputControl(name = "touchpadButton", layout = "Button", displayName = "Touchpad Press", bit = 1)] // always 0, does not exist on DualShock 3
[FieldOffset(4)] public byte buttons3;
[FieldOffset(5)] private byte padding2;
[InputControl(name = "leftStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "leftStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "leftStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(6)] public byte leftStickX;
[FieldOffset(7)] public byte leftStickY;
[InputControl(name = "rightStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "rightStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "rightStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(8)] public byte rightStickX;
[FieldOffset(9)] public byte rightStickY;
[FieldOffset(10)] private fixed byte padding3[8];
[InputControl(name = "leftTrigger", format = "BYTE")]
[FieldOffset(18)] public byte leftTrigger;
[InputControl(name = "rightTrigger", format = "BYTE")]
[FieldOffset(19)] public byte rightTrigger;
public FourCC format
{
get { return new FourCC('H', 'I', 'D'); }
}
}
/// <summary>
/// PS4 output report sent as command to HID backend.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = kSize)]
internal unsafe struct DualShockHIDOutputReport : IInputDeviceCommandInfo
{
public static FourCC Type => new FourCC('H', 'I', 'D', 'O');
internal const int kSize = InputDeviceCommand.kBaseCommandSize + 32;
internal const int kReportId = 5;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flags", Justification = "No better term for underlying data.")]
[Flags]
public enum Flags
{
Rumble = 0x1,
Color = 0x2
}
[FieldOffset(0)] public InputDeviceCommand baseCommand;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 0)] public byte reportId;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flags", Justification = "No better term for underlying data.")]
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 1)] public byte flags;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 2)] public fixed byte unknown1[2];
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 4)] public byte highFrequencyMotorSpeed;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 5)] public byte lowFrequencyMotorSpeed;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 6)] public byte redColor;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 7)] public byte greenColor;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 8)] public byte blueColor;
[FieldOffset(InputDeviceCommand.kBaseCommandSize + 9)] public fixed byte unknown2[23];
public FourCC typeStatic => Type;
public void SetMotorSpeeds(float lowFreq, float highFreq)
{
flags |= (byte)Flags.Rumble;
lowFrequencyMotorSpeed = (byte)Mathf.Clamp(lowFreq * 255, 0, 255);
highFrequencyMotorSpeed = (byte)Mathf.Clamp(highFreq * 255, 0, 255);
}
public void SetColor(Color color)
{
flags |= (byte)Flags.Color;
redColor = (byte)Mathf.Clamp(color.r * 255, 0, 255);
greenColor = (byte)Mathf.Clamp(color.g * 255, 0, 255);
blueColor = (byte)Mathf.Clamp(color.b * 255, 0, 255);
}
public static DualShockHIDOutputReport Create()
{
return new DualShockHIDOutputReport
{
baseCommand = new InputDeviceCommand(Type, kSize),
reportId = kReportId,
};
}
}
}
namespace UnityEngine.InputSystem.DualShock
{
/// <summary>
/// PS5 DualSense controller that is interfaced to a HID backend.
/// </summary>
[InputControlLayout(stateType = typeof(DualSenseHIDInputReport), displayName = "DualSense HID")]
public class DualSenseGamepadHID : DualShockGamepad, IEventMerger, IEventPreProcessor, IInputStateCallbackReceiver
{
// Gamepad might send 3 types of input reports:
// - Minimal report, first byte is 0x01, observed size is 78, also can be 10
// - Full USB report, first byte is 0x01, observed size is 64
// - Full Bluetooth report, first byte is 0x31, observed size 78
// While USB and Bluetooth reports only differ in header,
// Minimal report also differs in order of fields (buttons -> triggers vs triggers -> buttons).
public ButtonControl leftTriggerButton { get; protected set; }
public ButtonControl rightTriggerButton { get; protected set; }
public ButtonControl playStationButton { get; protected set; }
private float? m_LowFrequencyMotorSpeed;
private float? m_HighFrequenceyMotorSpeed;
private Color? m_LightBarColor;
private byte outputSequenceId;
protected override void FinishSetup()
{
leftTriggerButton = GetChildControl<ButtonControl>("leftTriggerButton");
rightTriggerButton = GetChildControl<ButtonControl>("rightTriggerButton");
playStationButton = GetChildControl<ButtonControl>("systemButton");
base.FinishSetup();
}
public override void PauseHaptics()
{
if (!m_LowFrequencyMotorSpeed.HasValue && !m_HighFrequenceyMotorSpeed.HasValue)
return;
SetMotorSpeedsAndLightBarColor(0.0f, 0.0f, m_LightBarColor);
}
public override void ResetHaptics()
{
if (!m_LowFrequencyMotorSpeed.HasValue && !m_HighFrequenceyMotorSpeed.HasValue)
return;
m_HighFrequenceyMotorSpeed = null;
m_LowFrequencyMotorSpeed = null;
SetMotorSpeedsAndLightBarColor(m_LowFrequencyMotorSpeed, m_HighFrequenceyMotorSpeed, m_LightBarColor);
}
public override void ResumeHaptics()
{
if (!m_LowFrequencyMotorSpeed.HasValue && !m_HighFrequenceyMotorSpeed.HasValue)
return;
SetMotorSpeedsAndLightBarColor(m_LowFrequencyMotorSpeed, m_HighFrequenceyMotorSpeed, m_LightBarColor);
}
public override void SetLightBarColor(Color color)
{
m_LightBarColor = color;
SetMotorSpeedsAndLightBarColor(m_LowFrequencyMotorSpeed, m_HighFrequenceyMotorSpeed, m_LightBarColor);
}
public override void SetMotorSpeeds(float lowFrequency, float highFrequency)
{
m_LowFrequencyMotorSpeed = lowFrequency;
m_HighFrequenceyMotorSpeed = highFrequency;
SetMotorSpeedsAndLightBarColor(m_LowFrequencyMotorSpeed, m_HighFrequenceyMotorSpeed, m_LightBarColor);
}
/// <summary>
/// Set motor speeds of both motors and the light bar color simultaneously.
/// </summary>
/// <param name="lowFrequency"><see cref="Haptics.IDualMotorRumble.SetMotorSpeeds"/></param>
/// <param name="highFrequency"><see cref="Haptics.IDualMotorRumble.SetMotorSpeeds"/></param>
/// <param name="color"><see cref="IDualShockHaptics.SetLightBarColor"/></param>
/// <returns>True if the command succeeded. Will return false if another command is currently being processed.</returns>
/// <remarks>
/// Use this method to set both the motor speeds and the light bar color in the same call. This method exists
/// because it is currently not possible to process an input/output control (IOCTL) command while another one
/// is in flight. For example, calling <see cref="SetMotorSpeeds"/> immediately after calling
/// <see cref="SetLightBarColor"/> might result in only the light bar color changing. The <see cref="SetMotorSpeeds"/>
/// call could fail. It is however possible to combine multiple IOCTL instructions into a single command, which
/// is what this method does.
///
/// See <see cref="Haptics.IDualMotorRumble.SetMotorSpeeds"/> and <see cref="IDualShockHaptics.SetLightBarColor"/>
/// for the respective documentation regarding setting rumble and light bar color.</remarks>
public bool SetMotorSpeedsAndLightBarColor(float? lowFrequency, float? highFrequency, Color? color)
{
var lf = lowFrequency.HasValue ? lowFrequency.Value : 0;
var hf = highFrequency.HasValue ? highFrequency.Value : 0;
var c = color.HasValue ? color.Value : Color.black;
// DualSense differs a bit from DualShock 4 because all effects need to be set at a same time,
// otherwise setting just a color would disable motor rumble.
var payload = new DualSenseHIDOutputReportPayload
{
enableFlags1 = 0x1 | // Enable motor rumble.
0x2, // Disable haptics.
enableFlags2 = 0x4, // Enable LEDs color.
lowFrequencyMotorSpeed = (byte)NumberHelpers.NormalizedFloatToUInt(lf, byte.MinValue, byte.MaxValue),
highFrequencyMotorSpeed = (byte)NumberHelpers.NormalizedFloatToUInt(hf, byte.MinValue, byte.MaxValue),
redColor = (byte)NumberHelpers.NormalizedFloatToUInt(c.r, byte.MinValue, byte.MaxValue),
greenColor = (byte)NumberHelpers.NormalizedFloatToUInt(c.g, byte.MinValue, byte.MaxValue),
blueColor = (byte)NumberHelpers.NormalizedFloatToUInt(c.b, byte.MinValue, byte.MaxValue)
};
////FIXME: Bluetooth reports are not working
//var command = DualSenseHIDBluetoothOutputReport.Create(payload, ++outputSequenceId);
var command = DualSenseHIDUSBOutputReport.Create(payload);
return ExecuteCommand(ref command) >= 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe bool MergeForward(DualSenseHIDUSBInputReport* currentState, DualSenseHIDUSBInputReport* nextState)
{
return currentState->buttons0 == nextState->buttons0 && currentState->buttons1 == nextState->buttons1 &&
currentState->buttons2 == nextState->buttons2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe bool MergeForward(DualSenseHIDBluetoothInputReport* currentState, DualSenseHIDBluetoothInputReport* nextState)
{
return currentState->buttons0 == nextState->buttons0 && currentState->buttons1 == nextState->buttons1 &&
currentState->buttons2 == nextState->buttons2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe bool MergeForward(DualSenseHIDMinimalInputReport* currentState, DualSenseHIDMinimalInputReport* nextState)
{
return currentState->buttons0 == nextState->buttons0 && currentState->buttons1 == nextState->buttons1 &&
currentState->buttons2 == nextState->buttons2;
}
unsafe bool IEventMerger.MergeForward(InputEventPtr currentEventPtr, InputEventPtr nextEventPtr)
{
if (currentEventPtr.type != StateEvent.Type || nextEventPtr.type != StateEvent.Type)
return false;
var currentEvent = StateEvent.FromUnchecked(currentEventPtr);
var nextEvent = StateEvent.FromUnchecked(nextEventPtr);
if (currentEvent->stateFormat != DualSenseHIDGenericInputReport.Format || nextEvent->stateFormat != DualSenseHIDGenericInputReport.Format)
return false;
if (currentEvent->stateSizeInBytes != nextEvent->stateSizeInBytes)
return false;
var currentGenericReport = (DualSenseHIDGenericInputReport*)currentEvent->state;
var nextGenericReport = (DualSenseHIDGenericInputReport*)nextEvent->state;
if (currentGenericReport->reportId != nextGenericReport->reportId)
return false;
if (currentGenericReport->reportId == DualSenseHIDUSBInputReport.ExpectedReportId)
{
if (currentEvent->stateSizeInBytes == DualSenseHIDMinimalInputReport.ExpectedSize1 ||
currentEvent->stateSizeInBytes == DualSenseHIDMinimalInputReport.ExpectedSize2)
{
var currentState = (DualSenseHIDMinimalInputReport*)currentEvent->state;
var nextState = (DualSenseHIDMinimalInputReport*)nextEvent->state;
return MergeForward(currentState, nextState);
}
else
{
var currentState = (DualSenseHIDUSBInputReport*)currentEvent->state;
var nextState = (DualSenseHIDUSBInputReport*)nextEvent->state;
return MergeForward(currentState, nextState);
}
}
else if (currentGenericReport->reportId == DualSenseHIDBluetoothInputReport.ExpectedReportId)
{
var currentState = (DualSenseHIDBluetoothInputReport*)currentEvent->state;
var nextState = (DualSenseHIDBluetoothInputReport*)nextEvent->state;
return MergeForward(currentState, nextState);
}
else
return false;
}
unsafe bool IEventPreProcessor.PreProcessEvent(InputEventPtr eventPtr)
{
if (eventPtr.type != StateEvent.Type)
return eventPtr.type != DeltaStateEvent.Type; // only skip delta state events
var stateEvent = StateEvent.FromUnchecked(eventPtr);
if (stateEvent->stateFormat == DualSenseHIDInputReport.Format)
return true; // if someone queued DSVS directly, just use as-is
var size = stateEvent->stateSizeInBytes;
if (stateEvent->stateFormat != DualSenseHIDGenericInputReport.Format || size < sizeof(DualSenseHIDInputReport))
return false; // skip unrecognized state events otherwise they will corrupt control states
var genericReport = (DualSenseHIDGenericInputReport*)stateEvent->state;
if (genericReport->reportId == DualSenseHIDUSBInputReport.ExpectedReportId)
{
if (stateEvent->stateSizeInBytes == DualSenseHIDMinimalInputReport.ExpectedSize1 ||
stateEvent->stateSizeInBytes == DualSenseHIDMinimalInputReport.ExpectedSize2)
{
// minimal report
var data = ((DualSenseHIDMinimalInputReport*)stateEvent->state)->ToHIDInputReport();
*((DualSenseHIDInputReport*)stateEvent->state) = data;
}
else
{
var data = ((DualSenseHIDUSBInputReport*)stateEvent->state)->ToHIDInputReport();
*((DualSenseHIDInputReport*)stateEvent->state) = data;
}
stateEvent->stateFormat = DualSenseHIDInputReport.Format;
return true;
}
else if (genericReport->reportId == DualSenseHIDBluetoothInputReport.ExpectedReportId)
{
var data = ((DualSenseHIDBluetoothInputReport*)stateEvent->state)->ToHIDInputReport();
*((DualSenseHIDInputReport*)stateEvent->state) = data;
stateEvent->stateFormat = DualSenseHIDInputReport.Format;
return true;
}
else
return false; // skip unrecognized reportId
}
public void OnNextUpdate()
{
}
// filter out three lower bits as jitter noise
internal const byte JitterMaskLow = 0b01111000;
internal const byte JitterMaskHigh = 0b10000111;
public unsafe void OnStateEvent(InputEventPtr eventPtr)
{
if (eventPtr.type == StateEvent.Type && eventPtr.stateFormat == DualSenseHIDInputReport.Format)
{
var currentState = (DualSenseHIDInputReport*)((byte*)currentStatePtr + m_StateBlock.byteOffset);
var newState = (DualSenseHIDInputReport*)StateEvent.FromUnchecked(eventPtr)->state;
var actuated =
// we need to make device current if axes are outside of deadzone specifying hardware jitter of sticks around zero point
newState->leftStickX<JitterMaskLow
|| newState->leftStickX> JitterMaskHigh
|| newState->leftStickY<JitterMaskLow
|| newState->leftStickY> JitterMaskHigh
|| newState->rightStickX<JitterMaskLow
|| newState->rightStickX> JitterMaskHigh
|| newState->rightStickY<JitterMaskLow
|| newState->rightStickY> JitterMaskHigh
// we need to make device current if triggers or buttons state change
|| newState->leftTrigger != currentState->leftTrigger
|| newState->rightTrigger != currentState->rightTrigger
|| newState->buttons0 != currentState->buttons0
|| newState->buttons1 != currentState->buttons1
|| newState->buttons2 != currentState->buttons2;
if (!actuated)
InputSystem.s_Manager.DontMakeCurrentlyUpdatingDeviceCurrent();
}
InputState.Change(this, eventPtr);
}
public bool GetStateOffsetForEvent(InputControl control, InputEventPtr eventPtr, ref uint offset)
{
return false;
}
[StructLayout(LayoutKind.Explicit)]
internal struct DualSenseHIDGenericInputReport
{
public static FourCC Format => new FourCC('H', 'I', 'D');
[FieldOffset(0)] public byte reportId;
}
[StructLayout(LayoutKind.Explicit)]
internal struct DualSenseHIDUSBInputReport
{
public const int ExpectedReportId = 0x01;
[FieldOffset(0)] public byte reportId;
[FieldOffset(1)] public byte leftStickX;
[FieldOffset(2)] public byte leftStickY;
[FieldOffset(3)] public byte rightStickX;
[FieldOffset(4)] public byte rightStickY;
[FieldOffset(5)] public byte leftTrigger;
[FieldOffset(6)] public byte rightTrigger;
[FieldOffset(8)] public byte buttons0;
[FieldOffset(9)] public byte buttons1;
[FieldOffset(10)] public byte buttons2;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DualSenseHIDInputReport ToHIDInputReport()
{
return new DualSenseHIDInputReport
{
leftStickX = leftStickX,
leftStickY = leftStickY,
rightStickX = rightStickX,
rightStickY = rightStickY,
leftTrigger = leftTrigger,
rightTrigger = rightTrigger,
buttons0 = buttons0,
buttons1 = buttons1,
buttons2 = (byte)(buttons2 & 0x07)
};
}
}
[StructLayout(LayoutKind.Explicit)]
internal struct DualSenseHIDBluetoothInputReport
{
public const int ExpectedReportId = 0x31;
[FieldOffset(0)] public byte reportId;
[FieldOffset(2)] public byte leftStickX;
[FieldOffset(3)] public byte leftStickY;
[FieldOffset(4)] public byte rightStickX;
[FieldOffset(5)] public byte rightStickY;
[FieldOffset(6)] public byte leftTrigger;
[FieldOffset(7)] public byte rightTrigger;
[FieldOffset(9)] public byte buttons0;
[FieldOffset(10)] public byte buttons1;
[FieldOffset(11)] public byte buttons2;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DualSenseHIDInputReport ToHIDInputReport()
{
return new DualSenseHIDInputReport
{
leftStickX = leftStickX,
leftStickY = leftStickY,
rightStickX = rightStickX,
rightStickY = rightStickY,
leftTrigger = leftTrigger,
rightTrigger = rightTrigger,
buttons0 = buttons0,
buttons1 = buttons1,
buttons2 = (byte)(buttons2 & 0x07)
};
}
}
[StructLayout(LayoutKind.Explicit)]
internal struct DualSenseHIDMinimalInputReport
{
public static int ExpectedSize1 = 10;
public static int ExpectedSize2 = 78;
[FieldOffset(0)] public byte reportId;
[FieldOffset(1)] public byte leftStickX;
[FieldOffset(2)] public byte leftStickY;
[FieldOffset(3)] public byte rightStickX;
[FieldOffset(4)] public byte rightStickY;
[FieldOffset(5)] public byte buttons0;
[FieldOffset(6)] public byte buttons1;
[FieldOffset(7)] public byte buttons2;
[FieldOffset(8)] public byte leftTrigger;
[FieldOffset(9)] public byte rightTrigger;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DualSenseHIDInputReport ToHIDInputReport()
{
return new DualSenseHIDInputReport
{
leftStickX = leftStickX,
leftStickY = leftStickY,
rightStickX = rightStickX,
rightStickY = rightStickY,
leftTrigger = leftTrigger,
rightTrigger = rightTrigger,
buttons0 = buttons0,
buttons1 = buttons1,
buttons2 = (byte)(buttons2 & 0x03) // higher bits seem to contain random data, and mic button is not supported
};
}
}
}
/// <summary>
/// PS4 DualShock controller that is interfaced to a HID backend.
/// </summary>
[InputControlLayout(stateType = typeof(DualShock4HIDInputReport), hideInUI = true, isNoisy = true)]
public class DualShock4GamepadHID : DualShockGamepad, IEventPreProcessor, IInputStateCallbackReceiver
{
public ButtonControl leftTriggerButton { get; protected set; }
public ButtonControl rightTriggerButton { get; protected set; }
public ButtonControl playStationButton { get; protected set; }
protected override void FinishSetup()
{
leftTriggerButton = GetChildControl<ButtonControl>("leftTriggerButton");
rightTriggerButton = GetChildControl<ButtonControl>("rightTriggerButton");
playStationButton = GetChildControl<ButtonControl>("systemButton");
base.FinishSetup();
}
public override void PauseHaptics()
{
if (!m_LowFrequencyMotorSpeed.HasValue && !m_HighFrequenceyMotorSpeed.HasValue && !m_LightBarColor.HasValue)
return;
var command = DualShockHIDOutputReport.Create();
command.SetMotorSpeeds(0f, 0f);
////REVIEW: when pausing&resuming haptics, you probably don't want the lightbar color to change
if (m_LightBarColor.HasValue)
command.SetColor(Color.black);
ExecuteCommand(ref command);
}
public override void ResetHaptics()
{
if (!m_LowFrequencyMotorSpeed.HasValue && !m_HighFrequenceyMotorSpeed.HasValue && !m_LightBarColor.HasValue)
return;
var command = DualShockHIDOutputReport.Create();
command.SetMotorSpeeds(0f, 0f);
if (m_LightBarColor.HasValue)
command.SetColor(Color.black);
ExecuteCommand(ref command);
m_HighFrequenceyMotorSpeed = null;
m_LowFrequencyMotorSpeed = null;
m_LightBarColor = null;
}
public override void ResumeHaptics()
{
if (!m_LowFrequencyMotorSpeed.HasValue && !m_HighFrequenceyMotorSpeed.HasValue && !m_LightBarColor.HasValue)
return;
var command = DualShockHIDOutputReport.Create();
if (m_LowFrequencyMotorSpeed.HasValue || m_HighFrequenceyMotorSpeed.HasValue)
command.SetMotorSpeeds(m_LowFrequencyMotorSpeed.Value, m_HighFrequenceyMotorSpeed.Value);
if (m_LightBarColor.HasValue)
command.SetColor(m_LightBarColor.Value);
ExecuteCommand(ref command);
}
////FIXME: SetLightBarColor and SetMotorSpeeds to not mutually respect their settings
public override void SetLightBarColor(Color color)
{
var command = DualShockHIDOutputReport.Create();
command.SetColor(color);
ExecuteCommand(ref command);
m_LightBarColor = color;
}
public override void SetMotorSpeeds(float lowFrequency, float highFrequency)
{
var command = DualShockHIDOutputReport.Create();
command.SetMotorSpeeds(lowFrequency, highFrequency);
ExecuteCommand(ref command);
m_LowFrequencyMotorSpeed = lowFrequency;
m_HighFrequenceyMotorSpeed = highFrequency;
}
/// <summary>
/// Set motor speeds of both motors and the light bar color simultaneously.
/// </summary>
/// <param name="lowFrequency"><see cref="Haptics.IDualMotorRumble.SetMotorSpeeds"/></param>
/// <param name="highFrequency"><see cref="Haptics.IDualMotorRumble.SetMotorSpeeds"/></param>
/// <param name="color"><see cref="IDualShockHaptics.SetLightBarColor"/></param>
/// <returns>True if the command succeeded. Will return false if another command is currently being processed.</returns>
/// <remarks>
/// Use this method to set both the motor speeds and the light bar color in the same call. This method exists
/// because it is currently not possible to process an input/output control (IOCTL) command while another one
/// is in flight. For example, calling <see cref="SetMotorSpeeds"/> immediately after calling
/// <see cref="SetLightBarColor"/> might result in only the light bar color changing. The <see cref="SetMotorSpeeds"/>
/// call could fail. It is however possible to combine multiple IOCTL instructions into a single command, which
/// is what this method does.
///
/// See <see cref="Haptics.IDualMotorRumble.SetMotorSpeeds"/> and <see cref="IDualShockHaptics.SetLightBarColor"/>
/// for the respective documentation regarding setting rumble and light bar color.</remarks>
public bool SetMotorSpeedsAndLightBarColor(float lowFrequency, float highFrequency, Color color)
{
var command = DualShockHIDOutputReport.Create();
command.SetMotorSpeeds(lowFrequency, highFrequency);
command.SetColor(color);
var result = ExecuteCommand(ref command);
m_LowFrequencyMotorSpeed = lowFrequency;
m_HighFrequenceyMotorSpeed = highFrequency;
m_LightBarColor = color;
return result >= 0;
}
private float? m_LowFrequencyMotorSpeed;
private float? m_HighFrequenceyMotorSpeed;
private Color? m_LightBarColor;
unsafe bool IEventPreProcessor.PreProcessEvent(InputEventPtr eventPtr)
{
if (eventPtr.type != StateEvent.Type)
return eventPtr.type != DeltaStateEvent.Type; // only skip delta state events
var stateEvent = StateEvent.FromUnchecked(eventPtr);
if (stateEvent->stateFormat == DualShock4HIDInputReport.Format)
return true; // if someone queued D4VS directly, just use as-is
var size = stateEvent->stateSizeInBytes;
if (stateEvent->stateFormat != DualShock4HIDGenericInputReport.Format || size < sizeof(DualShock4HIDGenericInputReport))
return false; // skip unrecognized state events otherwise they will corrupt control states
var binaryData = (byte*)stateEvent->state;
switch (binaryData[0])
{
// normal USB or non-enhanced report
case 1:
{
if (size < sizeof(DualShock4HIDGenericInputReport) + 1)
return false;
var data = ((DualShock4HIDGenericInputReport*)(binaryData + 1))->ToHIDInputReport();
*((DualShock4HIDInputReport*)stateEvent->state) = data;
stateEvent->stateFormat = DualShock4HIDInputReport.Format;
return true;
}
// bluetooth or enhanced report
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
if ((binaryData[1] & 0x80) != 0)
{
if (size < sizeof(DualShock4HIDGenericInputReport) + 3)
return false;
var data = ((DualShock4HIDGenericInputReport*)(binaryData + 3))->ToHIDInputReport();
*((DualShock4HIDInputReport*)stateEvent->state) = data;
stateEvent->stateFormat = DualShock4HIDInputReport.Format;
return true;
}
else
return false;
default:
return false; // skip unrecognized reportId
}
}
public void OnNextUpdate()
{
}
// filter out three lower bits as jitter noise
internal const byte JitterMaskLow = 0b01111000;
internal const byte JitterMaskHigh = 0b10000111;
public unsafe void OnStateEvent(InputEventPtr eventPtr)
{
if (eventPtr.type == StateEvent.Type && eventPtr.stateFormat == DualShock4HIDInputReport.Format)
{
var currentState = (DualShock4HIDInputReport*)((byte*)currentStatePtr + m_StateBlock.byteOffset);
var newState = (DualShock4HIDInputReport*)StateEvent.FromUnchecked(eventPtr)->state;
var actuatedOrChanged =
// we need to make device current if axes are outside of deadzone specifying hardware jitter of sticks around zero point
newState->leftStickX<JitterMaskLow
|| newState->leftStickX> JitterMaskHigh
|| newState->leftStickY<JitterMaskLow
|| newState->leftStickY> JitterMaskHigh
|| newState->rightStickX<JitterMaskLow
|| newState->rightStickX> JitterMaskHigh
|| newState->rightStickY<JitterMaskLow
|| newState->rightStickY> JitterMaskHigh
// we need to make device current if triggers or buttons state change
|| newState->leftTrigger != currentState->leftTrigger
|| newState->rightTrigger != currentState->rightTrigger
|| newState->buttons1 != currentState->buttons1
|| newState->buttons2 != currentState->buttons2
|| newState->buttons3 != currentState->buttons3;
if (!actuatedOrChanged)
InputSystem.s_Manager.DontMakeCurrentlyUpdatingDeviceCurrent();
}
InputState.Change(this, eventPtr);
}
public bool GetStateOffsetForEvent(InputControl control, InputEventPtr eventPtr, ref uint offset)
{
return false;
}
[StructLayout(LayoutKind.Explicit)]
internal struct DualShock4HIDGenericInputReport
{
public static FourCC Format => new FourCC('H', 'I', 'D');
[FieldOffset(0)] public byte leftStickX;
[FieldOffset(1)] public byte leftStickY;
[FieldOffset(2)] public byte rightStickX;
[FieldOffset(3)] public byte rightStickY;
[FieldOffset(4)] public byte buttons0;
[FieldOffset(5)] public byte buttons1;
[FieldOffset(6)] public byte buttons2;
[FieldOffset(7)] public byte leftTrigger;
[FieldOffset(8)] public byte rightTrigger;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DualShock4HIDInputReport ToHIDInputReport()
{
return new DualShock4HIDInputReport
{
leftStickX = leftStickX,
leftStickY = leftStickY,
rightStickX = rightStickX,
rightStickY = rightStickY,
leftTrigger = leftTrigger,
rightTrigger = rightTrigger,
buttons1 = buttons0,
buttons2 = buttons1,
buttons3 = buttons2
};
}
}
}
[InputControlLayout(stateType = typeof(DualShock3HIDInputReport), hideInUI = true, displayName = "PS3 Controller")]
public class DualShock3GamepadHID : DualShockGamepad
{
public ButtonControl leftTriggerButton { get; private set; }
public ButtonControl rightTriggerButton { get; private set; }
public ButtonControl playStationButton { get; private set; }
protected override void FinishSetup()
{
leftTriggerButton = GetChildControl<ButtonControl>("leftTriggerButton");
rightTriggerButton = GetChildControl<ButtonControl>("rightTriggerButton");
playStationButton = GetChildControl<ButtonControl>("systemButton");
base.FinishSetup();
}
// TODO: see if we can implement rumble support on DualShock 3
}
}
// PS4 HID structures:
//
// struct PS4InputReport1
// {
// byte reportId; // #0
// byte leftStickX; // #1
// byte leftStickY; // #2
// byte rightStickX; // #3
// byte rightStickY; // #4
// byte dpad : 4; // #5 bit #0 (0=up, 2=right, 4=down, 6=left)
// byte squareButton : 1; // #5 bit #4
// byte crossButton : 1; // #5 bit #5
// byte circleButton : 1; // #5 bit #6
// byte triangleButton : 1; // #5 bit #7
// byte leftShoulder : 1; // #6 bit #0
// byte rightShoulder : 1; // #6 bit #1
// byte leftTriggerButton : 2;// #6 bit #2
// byte rightTriggerButton : 2;// #6 bit #3
// byte shareButton : 1; // #6 bit #4
// byte optionsButton : 1; // #6 bit #5
// byte leftStickPress : 1; // #6 bit #6
// byte rightStickPress : 1; // #6 bit #7
// byte psButton : 1; // #7 bit #0
// byte touchpadPress : 1; // #7 bit #1
// byte padding : 6;
// byte leftTrigger; // #8
// byte rightTrigger; // #9
// }
//
// struct PS4OutputReport5
// {
// byte reportId; // #0
// byte flags; // #1
// byte unknown1[2];
// byte highFrequencyMotor; // #4
// byte lowFrequencyMotor; // #5
// byte redColor; // #6
// byte greenColor; // #7
// byte blueColor; // #8
// byte unknown2[23];
// }
//
// Raw HID report descriptor:
//
// Usage Page (Generic Desktop) 05 01
// Usage (Game Pad) 09 05
// Collection (Application) A1 01
// Report ID (1) 85 01
// Usage (X) 09 30
// Usage (Y) 09 31
// Usage (Z) 09 32
// Usage (Rz) 09 35
// Logical Minimum (0) 15 00
// Logical Maximum (255) 26 FF 00
// Report Size (8) 75 08
// Report Count (4) 95 04
// Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02
// Usage (Hat Switch) 09 39
// Logical Minimum (0) 15 00
// Logical Maximum (7) 25 07
// Physical Minimum (0) 35 00
// Physical Maximum (315) 46 3B 01
// Unit (Eng Rot: Degree) 65 14
// Report Size (4) 75 04
// Report Count (1) 95 01
// Input (Data,Var,Abs,NWrp,Lin,Pref,Null,Bit) 81 42
// Unit (None) 65 00
// Usage Page (Button) 05 09
// Usage Minimum (Button 1) 19 01
// Usage Maximum (Button 14) 29 0E
// Logical Minimum (0) 15 00
// Logical Maximum (1) 25 01
// Report Size (1) 75 01
// Report Count (14) 95 0E
// Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02
// Usage Page (Vendor-Defined 1) 06 00 FF
// Usage (Vendor-Defined 32) 09 20
// Report Size (6) 75 06
// Report Count (1) 95 01
// Logical Minimum (0) 15 00
// Logical Maximum (127) 25 7F
// Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02
// Usage Page (Generic Desktop) 05 01
// Usage (Rx) 09 33
// Usage (Ry) 09 34
// Logical Minimum (0) 15 00
// Logical Maximum (255) 26 FF 00
// Report Size (8) 75 08
// Report Count (2) 95 02
// Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02
// Usage Page (Vendor-Defined 1) 06 00 FF
// Usage (Vendor-Defined 33) 09 21
// Report Count (54) 95 36
// Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02
// Report ID (5) 85 05
// Usage (Vendor-Defined 34) 09 22
// Report Count (31) 95 1F
// Output (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) 91 02
// Report ID (4) 85 04
// Usage (Vendor-Defined 35) 09 23
// Report Count (36) 95 24
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (2) 85 02
// Usage (Vendor-Defined 36) 09 24
// Report Count (36) 95 24
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (8) 85 08
// Usage (Vendor-Defined 37) 09 25
// Report Count (3) 95 03
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (16) 85 10
// Usage (Vendor-Defined 38) 09 26
// Report Count (4) 95 04
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (17) 85 11
// Usage (Vendor-Defined 39) 09 27
// Report Count (2) 95 02
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (18) 85 12
// Usage Page (Vendor-Defined 3) 06 02 FF
// Usage (Vendor-Defined 33) 09 21
// Report Count (15) 95 0F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (19) 85 13
// Usage (Vendor-Defined 34) 09 22
// Report Count (22) 95 16
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (20) 85 14
// Usage Page (Vendor-Defined 6) 06 05 FF
// Usage (Vendor-Defined 32) 09 20
// Report Count (16) 95 10
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (21) 85 15
// Usage (Vendor-Defined 33) 09 21
// Report Count (44) 95 2C
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Usage Page (Vendor-Defined 129) 06 80 FF
// Report ID (128) 85 80
// Usage (Vendor-Defined 32) 09 20
// Report Count (6) 95 06
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (129) 85 81
// Usage (Vendor-Defined 33) 09 21
// Report Count (6) 95 06
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (130) 85 82
// Usage (Vendor-Defined 34) 09 22
// Report Count (5) 95 05
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (131) 85 83
// Usage (Vendor-Defined 35) 09 23
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (132) 85 84
// Usage (Vendor-Defined 36) 09 24
// Report Count (4) 95 04
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (133) 85 85
// Usage (Vendor-Defined 37) 09 25
// Report Count (6) 95 06
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (134) 85 86
// Usage (Vendor-Defined 38) 09 26
// Report Count (6) 95 06
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (135) 85 87
// Usage (Vendor-Defined 39) 09 27
// Report Count (35) 95 23
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (136) 85 88
// Usage (Vendor-Defined 40) 09 28
// Report Count (34) 95 22
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (137) 85 89
// Usage (Vendor-Defined 41) 09 29
// Report Count (2) 95 02
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (144) 85 90
// Usage (Vendor-Defined 48) 09 30
// Report Count (5) 95 05
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (145) 85 91
// Usage (Vendor-Defined 49) 09 31
// Report Count (3) 95 03
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (146) 85 92
// Usage (Vendor-Defined 50) 09 32
// Report Count (3) 95 03
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (147) 85 93
// Usage (Vendor-Defined 51) 09 33
// Report Count (12) 95 0C
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (160) 85 A0
// Usage (Vendor-Defined 64) 09 40
// Report Count (6) 95 06
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (161) 85 A1
// Usage (Vendor-Defined 65) 09 41
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (162) 85 A2
// Usage (Vendor-Defined 66) 09 42
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (163) 85 A3
// Usage (Vendor-Defined 67) 09 43
// Report Count (48) 95 30
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (164) 85 A4
// Usage (Vendor-Defined 68) 09 44
// Report Count (13) 95 0D
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (165) 85 A5
// Usage (Vendor-Defined 69) 09 45
// Report Count (21) 95 15
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (166) 85 A6
// Usage (Vendor-Defined 70) 09 46
// Report Count (21) 95 15
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (240) 85 F0
// Usage (Vendor-Defined 71) 09 47
// Report Count (63) 95 3F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (241) 85 F1
// Usage (Vendor-Defined 72) 09 48
// Report Count (63) 95 3F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (242) 85 F2
// Usage (Vendor-Defined 73) 09 49
// Report Count (15) 95 0F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (167) 85 A7
// Usage (Vendor-Defined 74) 09 4A
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (168) 85 A8
// Usage (Vendor-Defined 75) 09 4B
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (169) 85 A9
// Usage (Vendor-Defined 76) 09 4C
// Report Count (8) 95 08
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (170) 85 AA
// Usage (Vendor-Defined 78) 09 4E
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (171) 85 AB
// Usage (Vendor-Defined 79) 09 4F
// Report Count (57) 95 39
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (172) 85 AC
// Usage (Vendor-Defined 80) 09 50
// Report Count (57) 95 39
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (173) 85 AD
// Usage (Vendor-Defined 81) 09 51
// Report Count (11) 95 0B
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (174) 85 AE
// Usage (Vendor-Defined 82) 09 52
// Report Count (1) 95 01
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (175) 85 AF
// Usage (Vendor-Defined 83) 09 53
// Report Count (2) 95 02
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (176) 85 B0
// Usage (Vendor-Defined 84) 09 54
// Report Count (63) 95 3F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (177) 85 B1
// Usage (Vendor-Defined 85) 09 55
// Report Count (2) 95 02
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (178) 85 B2
// Usage (Vendor-Defined 86) 09 56
// Report Count (2) 95 02
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (224) 85 E0
// Usage (Vendor-Defined 87) 09 57
// Report Count (2) 95 02
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (179) 85 B3
// Usage (Vendor-Defined 85) 09 55
// Report Count (63) 95 3F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// Report ID (180) 85 B4
// Usage (Vendor-Defined 85) 09 55
// Report Count (63) 95 3F
// Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02
// End Collection C0
#endif // UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WSA
|