Hash :
3a5abee5
Author :
Date :
2023-06-14T15:16:21
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 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 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_atomic.h"
#include "SDL_endian.h"
#include "SDL_hints.h"
#include "SDL_timer.h"
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
#include "SDL_hidapi_rumble.h"
#include "../../SDL_hints_c.h"
#if defined(__WIN32__) || defined(__WINGDK__)
#include "../windows/SDL_rawinputjoystick_c.h"
#endif
#ifdef SDL_USE_LIBUDEV
#include "../../core/linux/SDL_sandbox.h"
#endif
struct joystick_hwdata
{
SDL_HIDAPI_Device *device;
};
static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
&SDL_HIDAPI_DriverGameCube,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_LUNA
&SDL_HIDAPI_DriverLuna,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_SHIELD
&SDL_HIDAPI_DriverShield,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_PS3
&SDL_HIDAPI_DriverPS3,
&SDL_HIDAPI_DriverPS3ThirdParty,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_PS4
&SDL_HIDAPI_DriverPS4,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_PS5
&SDL_HIDAPI_DriverPS5,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_STADIA
&SDL_HIDAPI_DriverStadia,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_STEAM
&SDL_HIDAPI_DriverSteam,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_SWITCH
&SDL_HIDAPI_DriverNintendoClassic,
&SDL_HIDAPI_DriverJoyCons,
&SDL_HIDAPI_DriverSwitch,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_WII
&SDL_HIDAPI_DriverWii,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_XBOX360
&SDL_HIDAPI_DriverXbox360,
&SDL_HIDAPI_DriverXbox360W,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
&SDL_HIDAPI_DriverXboxOne,
#endif
};
static int SDL_HIDAPI_numdrivers = 0;
static SDL_SpinLock SDL_HIDAPI_spinlock;
static SDL_bool SDL_HIDAPI_hints_changed = SDL_FALSE;
static Uint32 SDL_HIDAPI_change_count = 0;
static SDL_HIDAPI_Device *SDL_HIDAPI_devices SDL_GUARDED_BY(SDL_joystick_lock);
static char SDL_HIDAPI_device_magic;
static int SDL_HIDAPI_numjoysticks = 0;
static SDL_bool SDL_HIDAPI_combine_joycons = SDL_TRUE;
static SDL_bool initialized = SDL_FALSE;
static SDL_bool shutting_down = SDL_FALSE;
static char *HIDAPI_ConvertString(const wchar_t *wide_string)
{
char *string = NULL;
if (wide_string) {
string = SDL_iconv_string("UTF-8", "WCHAR_T", (char *)wide_string, (SDL_wcslen(wide_string) + 1) * sizeof(wchar_t));
if (string == NULL) {
switch (sizeof(wchar_t)) {
case 2:
string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)wide_string, (SDL_wcslen(wide_string) + 1) * sizeof(wchar_t));
break;
case 4:
string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char *)wide_string, (SDL_wcslen(wide_string) + 1) * sizeof(wchar_t));
break;
}
}
}
return string;
}
void HIDAPI_DumpPacket(const char *prefix, const Uint8 *data, int size)
{
int i;
char *buffer;
size_t length = SDL_strlen(prefix) + 11 * (USB_PACKET_LENGTH / 8) + (5 * USB_PACKET_LENGTH * 2) + 1 + 1;
int start = 0, amount = size;
size_t current_len;
buffer = (char *)SDL_malloc(length);
current_len = SDL_snprintf(buffer, length, prefix, size);
for (i = start; i < start + amount; ++i) {
if ((i % 8) == 0) {
current_len += SDL_snprintf(&buffer[current_len], length - current_len, "\n%.2d: ", i);
}
current_len += SDL_snprintf(&buffer[current_len], length - current_len, " 0x%.2x", data[i]);
}
SDL_strlcat(buffer, "\n", length);
SDL_Log("%s", buffer);
SDL_free(buffer);
}
SDL_bool HIDAPI_SupportsPlaystationDetection(Uint16 vendor, Uint16 product)
{
/* If we already know the controller is a different type, don't try to detect it.
* This fixes a hang with the HORIPAD for Nintendo Switch (0x0f0d/0x00c1)
*/
if (SDL_GetJoystickGameControllerTypeFromVIDPID(vendor, product, NULL, SDL_FALSE) != SDL_CONTROLLER_TYPE_UNKNOWN) {
return SDL_FALSE;
}
switch (vendor) {
case USB_VENDOR_DRAGONRISE:
return SDL_TRUE;
case USB_VENDOR_HORI:
return SDL_TRUE;
case USB_VENDOR_LOGITECH:
/* Most Logitech devices are fine with this, but the F310 will lock up */
if (product == USB_PRODUCT_LOGITECH_F310) {
return SDL_FALSE;
}
return SDL_TRUE;
case USB_VENDOR_MADCATZ:
return SDL_TRUE;
case USB_VENDOR_NACON:
return SDL_TRUE;
case USB_VENDOR_PDP:
return SDL_TRUE;
case USB_VENDOR_POWERA:
return SDL_TRUE;
case USB_VENDOR_POWERA_ALT:
return SDL_TRUE;
case USB_VENDOR_QANBA:
return SDL_TRUE;
case USB_VENDOR_RAZER:
/* Most Razer devices are not game controllers, and some of them lock up
* or reset when we send them the Sony third-party query feature report,
* so don't include that vendor here. Instead add devices as appropriate
* to controller_type.c
*
* Reference: https://github.com/libsdl-org/SDL/issues/6733
* https://github.com/libsdl-org/SDL/issues/6799
*/
return SDL_FALSE;
case USB_VENDOR_SHANWAN:
return SDL_TRUE;
case USB_VENDOR_SHANWAN_ALT:
return SDL_TRUE;
case USB_VENDOR_THRUSTMASTER:
return SDL_TRUE;
case USB_VENDOR_ZEROPLUS:
return SDL_TRUE;
case 0x7545 /* SZ-MYPOWER */:
return SDL_TRUE;
default:
return SDL_FALSE;
}
}
float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max)
{
return output_min + (output_max - output_min) * (val - val_min) / (val_max - val_min);
}
static void HIDAPI_UpdateDeviceList(void);
static void HIDAPI_JoystickClose(SDL_Joystick *joystick);
static SDL_GameControllerType SDL_GetJoystickGameControllerProtocol(const char *name, Uint16 vendor, Uint16 product, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
{
static const int LIBUSB_CLASS_VENDOR_SPEC = 0xFF;
static const int XB360_IFACE_SUBCLASS = 93;
static const int XB360_IFACE_PROTOCOL = 1; /* Wired */
static const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
static const int XBONE_IFACE_SUBCLASS = 71;
static const int XBONE_IFACE_PROTOCOL = 208;
SDL_GameControllerType type = SDL_CONTROLLER_TYPE_UNKNOWN;
/* This code should match the checks in libusb/hid.c and HIDDeviceManager.java */
if (interface_class == LIBUSB_CLASS_VENDOR_SPEC &&
interface_subclass == XB360_IFACE_SUBCLASS &&
(interface_protocol == XB360_IFACE_PROTOCOL ||
interface_protocol == XB360W_IFACE_PROTOCOL)) {
static const int SUPPORTED_VENDORS[] = {
0x0079, /* GPD Win 2 */
0x044f, /* Thrustmaster */
0x045e, /* Microsoft */
0x046d, /* Logitech */
0x056e, /* Elecom */
0x06a3, /* Saitek */
0x0738, /* Mad Catz */
0x07ff, /* Mad Catz */
0x0e6f, /* PDP */
0x0f0d, /* Hori */
0x1038, /* SteelSeries */
0x11c9, /* Nacon */
0x12ab, /* Unknown */
0x1430, /* RedOctane */
0x146b, /* BigBen */
0x1532, /* Razer */
0x15e4, /* Numark */
0x162e, /* Joytech */
0x1689, /* Razer Onza */
0x1949, /* Lab126, Inc. */
0x1bad, /* Harmonix */
0x20d6, /* PowerA */
0x24c6, /* PowerA */
0x2c22, /* Qanba */
0x2dc8, /* 8BitDo */
0x9886, /* ASTRO Gaming */
};
int i;
for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) {
if (vendor == SUPPORTED_VENDORS[i]) {
type = SDL_CONTROLLER_TYPE_XBOX360;
break;
}
}
}
if (interface_number == 0 &&
interface_class == LIBUSB_CLASS_VENDOR_SPEC &&
interface_subclass == XBONE_IFACE_SUBCLASS &&
interface_protocol == XBONE_IFACE_PROTOCOL) {
static const int SUPPORTED_VENDORS[] = {
0x044f, /* Thrustmaster */
0x045e, /* Microsoft */
0x0738, /* Mad Catz */
0x0e6f, /* PDP */
0x0f0d, /* Hori */
0x10f5, /* Turtle Beach */
0x1532, /* Razer */
0x20d6, /* PowerA */
0x24c6, /* PowerA */
0x2dc8, /* 8BitDo */
0x2e24, /* Hyperkin */
};
int i;
for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) {
if (vendor == SUPPORTED_VENDORS[i]) {
type = SDL_CONTROLLER_TYPE_XBOXONE;
break;
}
}
}
if (type == SDL_CONTROLLER_TYPE_UNKNOWN) {
type = SDL_GetJoystickGameControllerTypeFromVIDPID(vendor, product, name, SDL_FALSE);
}
return type;
}
static SDL_bool HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
{
int i;
SDL_GameControllerType type = SDL_GetJoystickGameControllerProtocol(name, vendor_id, product_id, -1, 0, 0, 0);
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
if (driver->enabled && driver->IsSupportedDevice(NULL, name, type, vendor_id, product_id, version, -1, 0, 0, 0)) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static SDL_HIDAPI_DeviceDriver *HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
{
const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
const Uint16 USAGE_JOYSTICK = 0x0004;
const Uint16 USAGE_GAMEPAD = 0x0005;
const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
int i;
if (device->num_children > 0) {
return &SDL_HIDAPI_DriverCombined;
}
if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
return NULL;
}
if (device->vendor_id != USB_VENDOR_VALVE) {
if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
return NULL;
}
if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
return NULL;
}
}
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
if (driver->enabled && driver->IsSupportedDevice(device, device->name, device->type, device->vendor_id, device->product_id, device->version, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol)) {
return driver;
}
}
return NULL;
}
static SDL_HIDAPI_Device *HIDAPI_GetDeviceByIndex(int device_index, SDL_JoystickID *pJoystickID)
{
SDL_HIDAPI_Device *device;
SDL_AssertJoysticksLocked();
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->parent) {
continue;
}
if (device->driver) {
if (device_index < device->num_joysticks) {
if (pJoystickID) {
*pJoystickID = device->joysticks[device_index];
}
return device;
}
device_index -= device->num_joysticks;
}
}
return NULL;
}
static SDL_HIDAPI_Device *HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
{
SDL_HIDAPI_Device *device;
SDL_AssertJoysticksLocked();
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->vendor_id == vendor_id && device->product_id == product_id &&
SDL_strcmp(device->path, path) == 0) {
break;
}
}
return device;
}
static void HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
{
if (!device->driver) {
return; /* Already cleaned up */
}
/* Disconnect any joysticks */
while (device->num_joysticks && device->joysticks) {
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
}
device->driver->FreeDevice(device);
device->driver = NULL;
SDL_LockMutex(device->dev_lock);
{
if (device->dev) {
SDL_hid_close(device->dev);
device->dev = NULL;
}
if (device->context) {
SDL_free(device->context);
device->context = NULL;
}
}
SDL_UnlockMutex(device->dev_lock);
}
static void HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device, SDL_bool *removed) SDL_NO_THREAD_SAFETY_ANALYSIS /* We unlock the joystick lock to be able to open the HID device on Android */
{
*removed = SDL_FALSE;
if (device->driver) {
SDL_bool enabled;
if (device->vendor_id == USB_VENDOR_NINTENDO && device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR) {
enabled = SDL_HIDAPI_combine_joycons;
} else {
enabled = device->driver->enabled;
}
if (device->children) {
int i;
for (i = 0; i < device->num_children; ++i) {
SDL_HIDAPI_Device *child = device->children[i];
if (!child->driver || !child->driver->enabled) {
enabled = SDL_FALSE;
break;
}
}
}
if (!enabled) {
HIDAPI_CleanupDeviceDriver(device);
}
return; /* Already setup */
}
if (HIDAPI_GetDeviceDriver(device)) {
/* We might have a device driver for this device, try opening it and see */
if (device->num_children == 0) {
SDL_hid_device *dev;
/* Wait a little bit for the device to initialize */
SDL_Delay(10);
#ifdef __ANDROID__
/* On Android we need to leave joysticks unlocked because it calls
* out to the main thread for permissions and the main thread can
* be in the process of handling controller input.
*
* See https://github.com/libsdl-org/SDL/issues/6347 for details
*/
{
SDL_HIDAPI_Device *curr;
int lock_count = 0;
char *path = SDL_strdup(device->path);
SDL_AssertJoysticksLocked();
while (SDL_JoysticksLocked()) {
++lock_count;
SDL_UnlockJoysticks();
}
dev = SDL_hid_open_path(path, 0);
while (lock_count > 0) {
--lock_count;
SDL_LockJoysticks();
}
SDL_free(path);
/* Make sure the device didn't get removed while opening the HID path */
for (curr = SDL_HIDAPI_devices; curr && curr != device; curr = curr->next) {
continue;
}
if (curr == NULL) {
*removed = SDL_TRUE;
if (dev) {
SDL_hid_close(dev);
}
return;
}
}
#else
/* On other platforms we want to keep the lock so other threads wait for
* us to finish opening the controller before checking to see whether the
* HIDAPI driver is handling the device.
*
* On Windows, for example, the main thread can be enumerating DirectInput
* devices while the Windows.Gaming.Input thread is calling back with a new
* controller available.
*
* See https://github.com/libsdl-org/SDL/issues/7304 for details.
*/
dev = SDL_hid_open_path(device->path, 0);
#endif
if (dev == NULL) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"HIDAPI_SetupDeviceDriver() couldn't open %s: %s\n",
device->path, SDL_GetError());
return;
}
SDL_hid_set_nonblocking(dev, 1);
device->dev = dev;
}
device->driver = HIDAPI_GetDeviceDriver(device);
/* Initialize the device, which may cause a connected event */
if (device->driver && !device->driver->InitDevice(device)) {
HIDAPI_CleanupDeviceDriver(device);
}
if (!device->driver && device->dev) {
/* No driver claimed this device, go ahead and close it */
SDL_hid_close(device->dev);
device->dev = NULL;
}
}
}
static void SDL_HIDAPI_UpdateDrivers(void)
{
int i;
SDL_HIDAPI_Device *device;
SDL_bool removed;
SDL_AssertJoysticksLocked();
SDL_HIDAPI_numdrivers = 0;
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
driver->enabled = driver->IsEnabled();
if (driver->enabled && driver != &SDL_HIDAPI_DriverCombined) {
++SDL_HIDAPI_numdrivers;
}
}
removed = SDL_FALSE;
do {
for (device = SDL_HIDAPI_devices; device; device = device->next) {
HIDAPI_SetupDeviceDriver(device, &removed);
if (removed) {
break;
}
}
} while (removed);
}
static void SDLCALL SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS) == 0) {
SDL_HIDAPI_combine_joycons = SDL_GetStringBoolean(hint, SDL_TRUE);
}
SDL_HIDAPI_hints_changed = SDL_TRUE;
SDL_HIDAPI_change_count = 0;
}
static int HIDAPI_JoystickInit(void)
{
int i;
if (initialized) {
return 0;
}
#if defined(SDL_USE_LIBUDEV)
if (linux_enumeration_method == ENUMERATION_UNSET) {
if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
linux_enumeration_method = ENUMERATION_FALLBACK;
} else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"Container detected, disabling HIDAPI udev integration");
linux_enumeration_method = ENUMERATION_FALLBACK;
} else {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"Using udev for HIDAPI joystick device discovery");
linux_enumeration_method = ENUMERATION_LIBUDEV;
}
}
#endif
if (SDL_hid_init() < 0) {
return SDL_SetError("Couldn't initialize hidapi");
}
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
driver->RegisterHints(SDL_HIDAPIDriverHintChanged, driver);
}
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS,
SDL_HIDAPIDriverHintChanged, NULL);
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
SDL_HIDAPIDriverHintChanged, NULL);
SDL_HIDAPI_change_count = SDL_hid_device_change_count();
HIDAPI_UpdateDeviceList();
HIDAPI_UpdateDevices();
initialized = SDL_TRUE;
return 0;
}
static SDL_bool HIDAPI_AddJoystickInstanceToDevice(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
{
SDL_JoystickID *joysticks = (SDL_JoystickID *)SDL_realloc(device->joysticks, (device->num_joysticks + 1) * sizeof(*device->joysticks));
if (joysticks == NULL) {
return SDL_FALSE;
}
device->joysticks = joysticks;
device->joysticks[device->num_joysticks++] = joystickID;
return SDL_TRUE;
}
static SDL_bool HIDAPI_DelJoystickInstanceFromDevice(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
{
int i, size;
for (i = 0; i < device->num_joysticks; ++i) {
if (device->joysticks[i] == joystickID) {
size = (device->num_joysticks - i - 1) * sizeof(SDL_JoystickID);
SDL_memmove(&device->joysticks[i], &device->joysticks[i + 1], size);
--device->num_joysticks;
if (device->num_joysticks == 0) {
SDL_free(device->joysticks);
device->joysticks = NULL;
}
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static SDL_bool HIDAPI_JoystickInstanceIsUnique(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
{
if (device->parent && device->num_joysticks == 1 && device->parent->num_joysticks == 1 &&
device->joysticks[0] == device->parent->joysticks[0]) {
return SDL_FALSE;
}
return SDL_TRUE;
}
void HIDAPI_SetDeviceName(SDL_HIDAPI_Device *device, const char *name)
{
if (name && *name && SDL_strcmp(name, device->name) != 0) {
SDL_free(device->name);
device->name = SDL_strdup(name);
SDL_SetJoystickGUIDCRC(&device->guid, SDL_crc16(0, name, SDL_strlen(name)));
}
}
void HIDAPI_SetDeviceProduct(SDL_HIDAPI_Device *device, Uint16 vendor_id, Uint16 product_id)
{
/* Don't set the device product ID directly, or we'll constantly re-enumerate this device */
device->guid = SDL_CreateJoystickGUID(device->guid.data[0], vendor_id, product_id, device->version, device->name, 'h', 0);
}
static void HIDAPI_UpdateJoystickSerial(SDL_HIDAPI_Device *device)
{
int i;
for (i = 0; i < device->num_joysticks; ++i) {
SDL_Joystick *joystick = SDL_JoystickFromInstanceID(device->joysticks[i]);
if (joystick && device->serial) {
SDL_free(joystick->serial);
joystick->serial = SDL_strdup(device->serial);
}
}
}
static SDL_bool HIDAPI_SerialIsEmpty(SDL_HIDAPI_Device *device)
{
SDL_bool all_zeroes = SDL_TRUE;
if (device->serial) {
const char *serial = device->serial;
for (serial = device->serial; *serial; ++serial) {
if (*serial != '0') {
all_zeroes = SDL_FALSE;
break;
}
}
}
return all_zeroes;
}
void HIDAPI_SetDeviceSerial(SDL_HIDAPI_Device *device, const char *serial)
{
if (serial && *serial && (!device->serial || SDL_strcmp(serial, device->serial) != 0)) {
SDL_free(device->serial);
device->serial = SDL_strdup(serial);
HIDAPI_UpdateJoystickSerial(device);
}
}
static int wcstrcmp(const wchar_t *str1, const char *str2)
{
int result;
while (1) {
result = (*str1 - *str2);
if (result != 0 || *str1 == 0) {
break;
}
++str1;
++str2;
}
return result;
}
static void HIDAPI_SetDeviceSerialW(SDL_HIDAPI_Device *device, const wchar_t *serial)
{
if (serial && *serial && (!device->serial || wcstrcmp(serial, device->serial) != 0)) {
SDL_free(device->serial);
device->serial = HIDAPI_ConvertString(serial);
HIDAPI_UpdateJoystickSerial(device);
}
}
SDL_bool HIDAPI_HasConnectedUSBDevice(const char *serial)
{
SDL_HIDAPI_Device *device;
SDL_AssertJoysticksLocked();
if (serial == NULL) {
return SDL_FALSE;
}
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (!device->driver) {
continue;
}
if (device->is_bluetooth) {
continue;
}
if (device->serial && SDL_strcmp(serial, device->serial) == 0) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
void HIDAPI_DisconnectBluetoothDevice(const char *serial)
{
SDL_HIDAPI_Device *device;
SDL_AssertJoysticksLocked();
if (serial == NULL) {
return;
}
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (!device->driver) {
continue;
}
if (!device->is_bluetooth) {
continue;
}
if (device->serial && SDL_strcmp(serial, device->serial) == 0) {
while (device->num_joysticks && device->joysticks) {
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
}
}
}
}
SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID)
{
int i, j;
SDL_JoystickID joystickID;
SDL_AssertJoysticksLocked();
for (i = 0; i < device->num_children; ++i) {
SDL_HIDAPI_Device *child = device->children[i];
for (j = child->num_joysticks; j--;) {
HIDAPI_JoystickDisconnected(child, child->joysticks[j]);
}
}
joystickID = SDL_GetNextJoystickInstanceID();
HIDAPI_AddJoystickInstanceToDevice(device, joystickID);
for (i = 0; i < device->num_children; ++i) {
SDL_HIDAPI_Device *child = device->children[i];
HIDAPI_AddJoystickInstanceToDevice(child, joystickID);
}
++SDL_HIDAPI_numjoysticks;
SDL_PrivateJoystickAdded(joystickID);
if (pJoystickID) {
*pJoystickID = joystickID;
}
return SDL_TRUE;
}
void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
{
int i, j;
SDL_LockJoysticks();
if (!HIDAPI_JoystickInstanceIsUnique(device, joystickID)) {
/* Disconnecting a child always disconnects the parent */
device = device->parent;
}
for (i = 0; i < device->num_joysticks; ++i) {
if (device->joysticks[i] == joystickID) {
SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystickID);
if (joystick) {
HIDAPI_JoystickClose(joystick);
}
HIDAPI_DelJoystickInstanceFromDevice(device, joystickID);
for (j = 0; j < device->num_children; ++j) {
SDL_HIDAPI_Device *child = device->children[j];
HIDAPI_DelJoystickInstanceFromDevice(child, joystickID);
}
--SDL_HIDAPI_numjoysticks;
if (!shutting_down) {
SDL_PrivateJoystickRemoved(joystickID);
}
}
}
/* Rescan the device list in case device state has changed */
SDL_HIDAPI_change_count = 0;
SDL_UnlockJoysticks();
}
static int HIDAPI_JoystickGetCount(void)
{
return SDL_HIDAPI_numjoysticks;
}
static SDL_HIDAPI_Device *HIDAPI_AddDevice(const struct SDL_hid_device_info *info, int num_children, SDL_HIDAPI_Device **children)
{
SDL_HIDAPI_Device *device;
SDL_HIDAPI_Device *curr, *last = NULL;
SDL_bool removed;
SDL_AssertJoysticksLocked();
for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
}
device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
if (device == NULL) {
return NULL;
}
device->magic = &SDL_HIDAPI_device_magic;
device->path = SDL_strdup(info->path);
if (!device->path) {
SDL_free(device);
return NULL;
}
device->seen = SDL_TRUE;
device->vendor_id = info->vendor_id;
device->product_id = info->product_id;
device->version = info->release_number;
device->interface_number = info->interface_number;
device->interface_class = info->interface_class;
device->interface_subclass = info->interface_subclass;
device->interface_protocol = info->interface_protocol;
device->usage_page = info->usage_page;
device->usage = info->usage;
device->dev_lock = SDL_CreateMutex();
/* Need the device name before getting the driver to know whether to ignore this device */
{
char *manufacturer_string = HIDAPI_ConvertString(info->manufacturer_string);
char *product_string = HIDAPI_ConvertString(info->product_string);
char *serial_number = HIDAPI_ConvertString(info->serial_number);
device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
if (manufacturer_string) {
SDL_free(manufacturer_string);
}
if (product_string) {
SDL_free(product_string);
}
if (serial_number && *serial_number) {
device->serial = serial_number;
} else {
SDL_free(serial_number);
}
if (!device->name) {
SDL_free(device->serial);
SDL_free(device->path);
SDL_free(device);
return NULL;
}
}
/* FIXME: Is there any way to tell whether this is a Bluetooth device? */
device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_USB, device->vendor_id, device->product_id, device->version, device->name, 'h', 0);
device->joystick_type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
device->type = SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
if (num_children > 0) {
int i;
device->num_children = num_children;
device->children = children;
for (i = 0; i < num_children; ++i) {
children[i]->parent = device;
}
}
/* Add it to the list */
if (last) {
last->next = device;
} else {
SDL_HIDAPI_devices = device;
}
removed = SDL_FALSE;
HIDAPI_SetupDeviceDriver(device, &removed);
if (removed) {
return NULL;
}
#ifdef DEBUG_HIDAPI
SDL_Log("Added HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->name : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
#endif
return device;
}
static void HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
{
SDL_HIDAPI_Device *curr, *last;
int i;
SDL_AssertJoysticksLocked();
#ifdef DEBUG_HIDAPI
SDL_Log("Removing HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->name : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
#endif
for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
if (curr == device) {
if (last) {
last->next = curr->next;
} else {
SDL_HIDAPI_devices = curr->next;
}
HIDAPI_CleanupDeviceDriver(device);
/* Make sure the rumble thread is done with this device */
while (SDL_AtomicGet(&device->rumble_pending) > 0) {
SDL_Delay(10);
}
for (i = 0; i < device->num_children; ++i) {
device->children[i]->parent = NULL;
}
device->magic = NULL;
SDL_DestroyMutex(device->dev_lock);
SDL_free(device->serial);
SDL_free(device->name);
SDL_free(device->path);
SDL_free(device->children);
SDL_free(device);
return;
}
}
}
static SDL_bool HIDAPI_CreateCombinedJoyCons()
{
SDL_HIDAPI_Device *device, *combined;
SDL_HIDAPI_Device *joycons[2] = { NULL, NULL };
SDL_AssertJoysticksLocked();
if (!SDL_HIDAPI_combine_joycons) {
return SDL_FALSE;
}
for (device = SDL_HIDAPI_devices; device; device = device->next) {
Uint16 vendor, product;
if (!device->driver) {
/* Unsupported device */
continue;
}
if (device->parent) {
/* This device is already part of a combined device */
continue;
}
SDL_GetJoystickGUIDInfo(device->guid, &vendor, &product, NULL, NULL);
if (!joycons[0] &&
(SDL_IsJoystickNintendoSwitchJoyConLeft(vendor, product) ||
(SDL_IsJoystickNintendoSwitchJoyConGrip(vendor, product) &&
SDL_strstr(device->name, "(L)") != NULL))) {
joycons[0] = device;
}
if (!joycons[1] &&
(SDL_IsJoystickNintendoSwitchJoyConRight(vendor, product) ||
(SDL_IsJoystickNintendoSwitchJoyConGrip(vendor, product) &&
SDL_strstr(device->name, "(R)") != NULL))) {
joycons[1] = device;
}
if (joycons[0] && joycons[1]) {
SDL_hid_device_info info;
SDL_HIDAPI_Device **children = (SDL_HIDAPI_Device **)SDL_malloc(2 * sizeof(SDL_HIDAPI_Device *));
if (children == NULL) {
return SDL_FALSE;
}
children[0] = joycons[0];
children[1] = joycons[1];
SDL_zero(info);
info.path = "nintendo_joycons_combined";
info.vendor_id = USB_VENDOR_NINTENDO;
info.product_id = USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;
info.interface_number = -1;
info.usage_page = USB_USAGEPAGE_GENERIC_DESKTOP;
info.usage = USB_USAGE_GENERIC_GAMEPAD;
info.manufacturer_string = L"Nintendo";
info.product_string = L"Switch Joy-Con (L/R)";
combined = HIDAPI_AddDevice(&info, 2, children);
if (combined && combined->driver) {
return SDL_TRUE;
} else {
if (combined) {
HIDAPI_DelDevice(combined);
} else {
SDL_free(children);
}
return SDL_FALSE;
}
}
}
return SDL_FALSE;
}
static void HIDAPI_UpdateDeviceList(void)
{
SDL_HIDAPI_Device *device;
struct SDL_hid_device_info *devs, *info;
SDL_LockJoysticks();
if (SDL_HIDAPI_hints_changed) {
SDL_HIDAPI_UpdateDrivers();
SDL_HIDAPI_hints_changed = SDL_FALSE;
}
/* Prepare the existing device list */
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->children) {
continue;
}
device->seen = SDL_FALSE;
}
/* Enumerate the devices */
if (SDL_HIDAPI_numdrivers > 0) {
devs = SDL_hid_enumerate(0, 0);
if (devs) {
for (info = devs; info; info = info->next) {
device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
if (device) {
device->seen = SDL_TRUE;
/* Check to see if the serial number is available now */
if(HIDAPI_SerialIsEmpty(device)) {
HIDAPI_SetDeviceSerialW(device, info->serial_number);
}
} else {
HIDAPI_AddDevice(info, 0, NULL);
}
}
SDL_hid_free_enumeration(devs);
}
}
/* Remove any devices that weren't seen or have been disconnected due to read errors */
check_removed:
device = SDL_HIDAPI_devices;
while (device) {
SDL_HIDAPI_Device *next = device->next;
if (!device->seen ||
((device->driver || device->children) && device->num_joysticks == 0 && !device->dev)) {
if (device->parent) {
/* When a child device goes away, so does the parent */
int i;
device = device->parent;
for (i = 0; i < device->num_children; ++i) {
HIDAPI_DelDevice(device->children[i]);
}
HIDAPI_DelDevice(device);
/* Update the device list again to pick up any children left */
SDL_HIDAPI_change_count = 0;
/* We deleted more than one device here, restart the loop */
goto check_removed;
} else {
HIDAPI_DelDevice(device);
/* Update the device list again in case this device comes back */
SDL_HIDAPI_change_count = 0;
}
}
device = next;
}
/* See if we can create any combined Joy-Con controllers */
while (HIDAPI_CreateCombinedJoyCons()) {
}
SDL_UnlockJoysticks();
}
static SDL_bool HIDAPI_IsEquivalentToDevice(Uint16 vendor_id, Uint16 product_id, SDL_HIDAPI_Device *device)
{
if (vendor_id == device->vendor_id && product_id == device->product_id) {
return SDL_TRUE;
}
if (vendor_id == USB_VENDOR_MICROSOFT) {
/* If we're looking for the wireless XBox 360 controller, also look for the dongle */
if (product_id == USB_PRODUCT_XBOX360_XUSB_CONTROLLER && device->product_id == USB_PRODUCT_XBOX360_WIRELESS_RECEIVER) {
return SDL_TRUE;
}
/* If we're looking for the raw input Xbox One controller, match it against any other Xbox One controller */
if (product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER &&
device->type == SDL_CONTROLLER_TYPE_XBOXONE) {
return SDL_TRUE;
}
/* If we're looking for an XInput controller, match it against any other Xbox controller */
if (product_id == USB_PRODUCT_XBOX_ONE_XINPUT_CONTROLLER) {
if (device->type == SDL_CONTROLLER_TYPE_XBOX360 || device->type == SDL_CONTROLLER_TYPE_XBOXONE) {
return SDL_TRUE;
}
}
}
if (vendor_id == USB_VENDOR_NVIDIA) {
/* If we're looking for the NVIDIA SHIELD controller Xbox interface, match it against any NVIDIA SHIELD controller */
if (product_id == 0xb400 &&
device->type == SDL_CONTROLLER_TYPE_NVIDIA_SHIELD) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
SDL_bool HIDAPI_IsDeviceTypePresent(SDL_GameControllerType type)
{
SDL_HIDAPI_Device *device;
SDL_bool result = SDL_FALSE;
/* Make sure we're initialized, as this could be called from other drivers during startup */
if (HIDAPI_JoystickInit() < 0) {
return SDL_FALSE;
}
if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
HIDAPI_UpdateDeviceList();
SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
}
SDL_LockJoysticks();
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->driver && device->type == type) {
result = SDL_TRUE;
break;
}
}
SDL_UnlockJoysticks();
#ifdef DEBUG_HIDAPI
SDL_Log("HIDAPI_IsDeviceTypePresent() returning %s for %d\n", result ? "true" : "false", type);
#endif
return result;
}
SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
{
SDL_HIDAPI_Device *device;
SDL_bool supported = SDL_FALSE;
SDL_bool result = SDL_FALSE;
/* Make sure we're initialized, as this could be called from other drivers during startup */
if (HIDAPI_JoystickInit() < 0) {
return SDL_FALSE;
}
/* Only update the device list for devices we know might be supported.
If we did this for every device, it would hit the USB driver too hard and potentially
lock up the system. This won't catch devices that we support but can only detect using
USB interface details, like Xbox controllers, but hopefully the device list update is
responsive enough to catch those.
*/
supported = HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name);
#if defined(SDL_JOYSTICK_HIDAPI_XBOX360) || defined(SDL_JOYSTICK_HIDAPI_XBOXONE)
if (!supported &&
(SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box") || SDL_strstr(name, "XBOX"))) {
supported = SDL_TRUE;
}
#endif /* SDL_JOYSTICK_HIDAPI_XBOX360 || SDL_JOYSTICK_HIDAPI_XBOXONE */
if (supported) {
if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
HIDAPI_UpdateDeviceList();
SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
}
}
/* Note that this isn't a perfect check - there may be multiple devices with 0 VID/PID,
or a different name than we have it listed here, etc, but if we support the device
and we have something similar in our device list, mark it as present.
*/
SDL_LockJoysticks();
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->driver &&
HIDAPI_IsEquivalentToDevice(vendor_id, product_id, device)) {
result = SDL_TRUE;
break;
}
}
SDL_UnlockJoysticks();
#ifdef DEBUG_HIDAPI
SDL_Log("HIDAPI_IsDevicePresent() returning %s for 0x%.4x / 0x%.4x\n", result ? "true" : "false", vendor_id, product_id);
#endif
return result;
}
SDL_JoystickType HIDAPI_GetJoystickTypeFromGUID(SDL_JoystickGUID guid)
{
SDL_HIDAPI_Device *device;
SDL_JoystickType type = SDL_JOYSTICK_TYPE_UNKNOWN;
SDL_LockJoysticks();
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (SDL_memcmp(&guid, &device->guid, sizeof(guid)) == 0) {
type = device->joystick_type;
break;
}
}
SDL_UnlockJoysticks();
return type;
}
SDL_GameControllerType HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid)
{
SDL_HIDAPI_Device *device;
SDL_GameControllerType type = SDL_CONTROLLER_TYPE_UNKNOWN;
SDL_LockJoysticks();
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (SDL_memcmp(&guid, &device->guid, sizeof(guid)) == 0) {
type = device->type;
break;
}
}
SDL_UnlockJoysticks();
return type;
}
static void HIDAPI_JoystickDetect(void)
{
if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
Uint32 count = SDL_hid_device_change_count();
if (SDL_HIDAPI_change_count != count) {
SDL_HIDAPI_change_count = count;
HIDAPI_UpdateDeviceList();
}
SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
}
}
void HIDAPI_UpdateDevices(void)
{
SDL_HIDAPI_Device *device;
SDL_AssertJoysticksLocked();
/* Update the devices, which may change connected joysticks and send events */
/* Prepare the existing device list */
if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->parent) {
continue;
}
if (device->driver) {
if (SDL_TryLockMutex(device->dev_lock) == 0) {
device->updating = SDL_TRUE;
device->driver->UpdateDevice(device);
device->updating = SDL_FALSE;
SDL_UnlockMutex(device->dev_lock);
}
}
}
SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
}
}
static const char *HIDAPI_JoystickGetDeviceName(int device_index)
{
SDL_HIDAPI_Device *device;
const char *name = NULL;
device = HIDAPI_GetDeviceByIndex(device_index, NULL);
if (device) {
/* FIXME: The device could be freed after this name is returned... */
name = device->name;
}
return name;
}
static const char *HIDAPI_JoystickGetDevicePath(int device_index)
{
SDL_HIDAPI_Device *device;
const char *path = NULL;
device = HIDAPI_GetDeviceByIndex(device_index, NULL);
if (device) {
/* FIXME: The device could be freed after this path is returned... */
path = device->path;
}
return path;
}
static int HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
{
SDL_HIDAPI_Device *device;
SDL_JoystickID instance_id;
int player_index = -1;
device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
if (device) {
player_index = device->driver->GetDevicePlayerIndex(device, instance_id);
}
return player_index;
}
static void HIDAPI_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
SDL_HIDAPI_Device *device;
SDL_JoystickID instance_id;
device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
if (device) {
device->driver->SetDevicePlayerIndex(device, instance_id, player_index);
}
}
static SDL_JoystickGUID HIDAPI_JoystickGetDeviceGUID(int device_index)
{
SDL_HIDAPI_Device *device;
SDL_JoystickGUID guid;
device = HIDAPI_GetDeviceByIndex(device_index, NULL);
if (device) {
SDL_memcpy(&guid, &device->guid, sizeof(guid));
} else {
SDL_zero(guid);
}
return guid;
}
static SDL_JoystickID HIDAPI_JoystickGetDeviceInstanceID(int device_index)
{
SDL_JoystickID joystickID = -1;
HIDAPI_GetDeviceByIndex(device_index, &joystickID);
return joystickID;
}
static int HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
SDL_JoystickID joystickID = -1;
SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index, &joystickID);
struct joystick_hwdata *hwdata;
SDL_AssertJoysticksLocked();
if (device == NULL || !device->driver) {
/* This should never happen - validated before being called */
return SDL_SetError("Couldn't find HIDAPI device at index %d\n", device_index);
}
hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
if (hwdata == NULL) {
return SDL_OutOfMemory();
}
hwdata->device = device;
/* Process any pending reports before opening the device */
SDL_LockMutex(device->dev_lock);
device->updating = SDL_TRUE;
device->driver->UpdateDevice(device);
device->updating = SDL_FALSE;
SDL_UnlockMutex(device->dev_lock);
if (!device->driver->OpenJoystick(device, joystick)) {
/* The open failed, mark this device as disconnected and update devices */
HIDAPI_JoystickDisconnected(device, joystickID);
SDL_free(hwdata);
return -1;
}
if (device->serial) {
joystick->serial = SDL_strdup(device->serial);
}
joystick->hwdata = hwdata;
return 0;
}
static SDL_bool HIDAPI_GetJoystickDevice(SDL_Joystick *joystick, SDL_HIDAPI_Device **device)
{
SDL_AssertJoysticksLocked();
if (joystick && joystick->hwdata) {
*device = joystick->hwdata->device;
if (*device && (*device)->magic == &SDL_HIDAPI_device_magic && (*device)->driver != NULL) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static int HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
int result;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->RumbleJoystick(device, joystick, low_frequency_rumble, high_frequency_rumble);
} else {
result = SDL_SetError("Rumble failed, device disconnected");
}
return result;
}
static int HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
int result;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->RumbleJoystickTriggers(device, joystick, left_rumble, right_rumble);
} else {
result = SDL_SetError("Rumble failed, device disconnected");
}
return result;
}
static Uint32 HIDAPI_JoystickGetCapabilities(SDL_Joystick *joystick)
{
Uint32 result = 0;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->GetJoystickCapabilities(device, joystick);
}
return result;
}
static int HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int result;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->SetJoystickLED(device, joystick, red, green, blue);
} else {
result = SDL_SetError("SetLED failed, device disconnected");
}
return result;
}
static int HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
int result;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->SendJoystickEffect(device, joystick, data, size);
} else {
result = SDL_SetError("SendEffect failed, device disconnected");
}
return result;
}
static int HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{
int result;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->SetJoystickSensorsEnabled(device, joystick, enabled);
} else {
result = SDL_SetError("SetSensorsEnabled failed, device disconnected");
}
return result;
}
static void HIDAPI_JoystickUpdate(SDL_Joystick *joystick)
{
/* This is handled in SDL_HIDAPI_UpdateDevices() */
}
static void HIDAPI_JoystickClose(SDL_Joystick *joystick) SDL_NO_THREAD_SAFETY_ANALYSIS /* We unlock the device lock so rumble can complete */
{
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
int i;
/* Wait up to 30 ms for pending rumble to complete */
if (device->updating) {
/* Unlock the device so rumble can complete */
SDL_UnlockMutex(device->dev_lock);
}
for (i = 0; i < 3; ++i) {
if (SDL_AtomicGet(&device->rumble_pending) > 0) {
SDL_Delay(10);
}
}
if (device->updating) {
/* Relock the device */
SDL_LockMutex(device->dev_lock);
}
device->driver->CloseJoystick(device, joystick);
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
}
}
static void HIDAPI_JoystickQuit(void)
{
int i;
SDL_AssertJoysticksLocked();
shutting_down = SDL_TRUE;
SDL_HIDAPI_QuitRumble();
while (SDL_HIDAPI_devices) {
SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
if (device->parent) {
/* When a child device goes away, so does the parent */
device = device->parent;
for (i = 0; i < device->num_children; ++i) {
HIDAPI_DelDevice(device->children[i]);
}
HIDAPI_DelDevice(device);
} else {
HIDAPI_DelDevice(device);
}
}
/* Make sure the drivers cleaned up properly */
SDL_assert(SDL_HIDAPI_numjoysticks == 0);
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
driver->UnregisterHints(SDL_HIDAPIDriverHintChanged, driver);
}
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS,
SDL_HIDAPIDriverHintChanged, NULL);
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
SDL_HIDAPIDriverHintChanged, NULL);
SDL_hid_exit();
SDL_HIDAPI_change_count = 0;
shutting_down = SDL_FALSE;
initialized = SDL_FALSE;
}
static SDL_bool HIDAPI_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
{
return SDL_FALSE;
}
SDL_JoystickDriver SDL_HIDAPI_JoystickDriver = {
HIDAPI_JoystickInit,
HIDAPI_JoystickGetCount,
HIDAPI_JoystickDetect,
HIDAPI_JoystickGetDeviceName,
HIDAPI_JoystickGetDevicePath,
HIDAPI_JoystickGetDevicePlayerIndex,
HIDAPI_JoystickSetDevicePlayerIndex,
HIDAPI_JoystickGetDeviceGUID,
HIDAPI_JoystickGetDeviceInstanceID,
HIDAPI_JoystickOpen,
HIDAPI_JoystickRumble,
HIDAPI_JoystickRumbleTriggers,
HIDAPI_JoystickGetCapabilities,
HIDAPI_JoystickSetLED,
HIDAPI_JoystickSendEffect,
HIDAPI_JoystickSetSensorsEnabled,
HIDAPI_JoystickUpdate,
HIDAPI_JoystickClose,
HIDAPI_JoystickQuit,
HIDAPI_JoystickGetGamepadMapping
};
#endif /* SDL_JOYSTICK_HIDAPI */
/* vi: set ts=4 sw=4 expandtab: */