Hash :
192cdf3d
Author :
Date :
2022-11-24T16:07:03
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 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 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"
/* This is the iOS implementation of the SDL joystick API */
#include "SDL_events.h"
#include "SDL_joystick.h"
#include "SDL_hints.h"
#include "SDL_stdinc.h"
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#include "../hidapi/SDL_hidapijoystick_c.h"
#include "../usb_ids.h"
#include "SDL_mfijoystick_c.h"
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
#if TARGET_OS_IOS
#define SDL_JOYSTICK_iOS_ACCELEROMETER
#import <CoreMotion/CoreMotion.h>
#endif
#if defined(__MACOSX__)
#include <IOKit/hid/IOHIDManager.h>
#include <AppKit/NSApplication.h>
#ifndef NSAppKitVersionNumber10_15
#define NSAppKitVersionNumber10_15 1894
#endif
#endif /* __MACOSX__ */
#ifdef SDL_JOYSTICK_MFI
#import <GameController/GameController.h>
static id connectObserver = nil;
static id disconnectObserver = nil;
static NSString *GCInputXboxShareButton = @"Button Share";
#include <Availability.h>
#include <objc/message.h>
/* remove compilation warnings for strict builds by defining these selectors, even though
* they are only ever used indirectly through objc_msgSend
*/
@interface GCController (SDL)
#if defined(__MACOSX__) && (__MAC_OS_X_VERSION_MAX_ALLOWED <= 101600)
+ (BOOL)supportsHIDDevice:(IOHIDDeviceRef)device;
#endif
#if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 130000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1500000))
@property(nonatomic, readonly) NSString *productCategory;
#endif
#if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 140500) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 140500) || (__MAC_OS_X_VERSION_MAX_ALLOWED >= 110300))
@property(class, nonatomic, readwrite) BOOL shouldMonitorBackgroundEvents;
#endif
@end
@interface GCExtendedGamepad (SDL)
#if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 121000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 121000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1401000))
@property (nonatomic, readonly, nullable) GCControllerButtonInput *leftThumbstickButton;
@property (nonatomic, readonly, nullable) GCControllerButtonInput *rightThumbstickButton;
#endif
#if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 130000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1500000))
@property (nonatomic, readonly) GCControllerButtonInput *buttonMenu;
@property (nonatomic, readonly, nullable) GCControllerButtonInput *buttonOptions;
#endif
#if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 140000) || (__MAC_OS_VERSION_MAX_ALLOWED > 1500000))
@property (nonatomic, readonly, nullable) GCControllerButtonInput *buttonHome;
#endif
@end
@interface GCMicroGamepad (SDL)
#if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 130000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1500000))
@property (nonatomic, readonly) GCControllerButtonInput *buttonMenu;
#endif
@end
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 140000) || (__MAC_OS_VERSION_MAX_ALLOWED > 1500000) || (__MAC_OS_X_VERSION_MAX_ALLOWED > 101600)
#define ENABLE_MFI_BATTERY
#define ENABLE_MFI_RUMBLE
#define ENABLE_MFI_LIGHT
#define ENABLE_MFI_SENSORS
#define ENABLE_MFI_SYSTEM_GESTURE_STATE
#define ENABLE_PHYSICAL_INPUT_PROFILE
#endif
#ifdef ENABLE_MFI_RUMBLE
#import <CoreHaptics/CoreHaptics.h>
#endif
#endif /* SDL_JOYSTICK_MFI */
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
static const char *accelerometerName = "iOS Accelerometer";
static CMMotionManager *motionManager = nil;
#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER */
static SDL_JoystickDeviceItem *deviceList = NULL;
static int numjoysticks = 0;
int SDL_AppleTVRemoteOpenedAsJoystick = 0;
static SDL_JoystickDeviceItem *
GetDeviceForIndex(int device_index)
{
SDL_JoystickDeviceItem *device = deviceList;
int i = 0;
while (i < device_index) {
if (device == NULL) {
return NULL;
}
device = device->next;
i++;
}
return device;
}
#ifdef SDL_JOYSTICK_MFI
static BOOL
IsControllerPS4(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"DualShock 4"]) {
return TRUE;
}
} else {
if ([controller.vendorName containsString: @"DUALSHOCK"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IsControllerPS5(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"DualSense"]) {
return TRUE;
}
} else {
if ([controller.vendorName containsString: @"DualSense"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IsControllerXbox(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"Xbox One"]) {
return TRUE;
}
} else {
if ([controller.vendorName containsString: @"Xbox"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IsControllerSwitchPro(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"Switch Pro Controller"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IsControllerSwitchJoyConL(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L)"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IsControllerSwitchJoyConR(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (R)"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IsControllerSwitchJoyConPair(GCController *controller)
{
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
if ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L/R)"]) {
return TRUE;
}
}
return FALSE;
}
static BOOL
IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
{
Uint16 vendor = 0;
Uint16 product = 0;
Uint8 subtype = 0;
const char *name = NULL;
if (@available(macOS 11.3, iOS 14.5, tvOS 14.5, *)) {
if (!GCController.shouldMonitorBackgroundEvents) {
GCController.shouldMonitorBackgroundEvents = YES;
}
}
/* Explicitly retain the controller because SDL_JoystickDeviceItem is a
* struct, and ARC doesn't work with structs. */
device->controller = (__bridge GCController *) CFBridgingRetain(controller);
if (controller.vendorName) {
name = controller.vendorName.UTF8String;
}
if (!name) {
name = "MFi Gamepad";
}
device->name = SDL_CreateJoystickName(0, 0, NULL, name);
#ifdef DEBUG_CONTROLLER_PROFILE
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
if (controller.physicalInputProfile) {
for (id key in controller.physicalInputProfile.buttons) {
NSLog(@"Button %@ available\n", key);
}
for (id key in controller.physicalInputProfile.axes) {
NSLog(@"Axis %@ available\n", key);
}
}
}
#endif
if (controller.extendedGamepad) {
GCExtendedGamepad *gamepad = controller.extendedGamepad;
BOOL is_xbox = IsControllerXbox(controller);
BOOL is_ps4 = IsControllerPS4(controller);
BOOL is_ps5 = IsControllerPS5(controller);
BOOL is_switch_pro = IsControllerSwitchPro(controller);
BOOL is_switch_joycon_pair = IsControllerSwitchJoyConPair(controller);
int nbuttons = 0;
BOOL has_direct_menu;
#ifdef SDL_JOYSTICK_HIDAPI
if ((is_xbox && HIDAPI_IsDeviceTypePresent(SDL_CONTROLLER_TYPE_XBOXONE)) ||
(is_ps4 && HIDAPI_IsDeviceTypePresent(SDL_CONTROLLER_TYPE_PS4)) ||
(is_ps5 && HIDAPI_IsDeviceTypePresent(SDL_CONTROLLER_TYPE_PS5)) ||
(is_switch_pro && HIDAPI_IsDeviceTypePresent(SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO))) {
/* The HIDAPI driver is taking care of this device */
return FALSE;
}
#endif
/* These buttons are part of the original MFi spec */
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_X);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_Y);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
nbuttons += 6;
/* These buttons are available on some newer controllers */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
if ([gamepad respondsToSelector:@selector(leftThumbstickButton)] && gamepad.leftThumbstickButton) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK);
++nbuttons;
}
if ([gamepad respondsToSelector:@selector(rightThumbstickButton)] && gamepad.rightThumbstickButton) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK);
++nbuttons;
}
if ([gamepad respondsToSelector:@selector(buttonOptions)] && gamepad.buttonOptions) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_BACK);
++nbuttons;
}
/* The Nintendo Switch JoyCon home button doesn't ever show as being held down */
if ([gamepad respondsToSelector:@selector(buttonHome)] && gamepad.buttonHome && !is_switch_joycon_pair) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_GUIDE);
++nbuttons;
}
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
++nbuttons;
has_direct_menu = [gamepad respondsToSelector:@selector(buttonMenu)] && gamepad.buttonMenu;
if (!has_direct_menu) {
device->uses_pause_handler = SDL_TRUE;
}
#if TARGET_OS_TV
/* The single menu button isn't very reliable, at least as of tvOS 16.1 */
if ((device->button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) == 0) {
device->uses_pause_handler = SDL_TRUE;
}
#endif
#ifdef ENABLE_PHYSICAL_INPUT_PROFILE
if ([controller respondsToSelector:@selector(physicalInputProfile)]) {
if (controller.physicalInputProfile.buttons[GCInputDualShockTouchpadButton] != nil) {
device->has_dualshock_touchpad = SDL_TRUE;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_MISC1);
++nbuttons;
}
if (controller.physicalInputProfile.buttons[GCInputXboxPaddleOne] != nil) {
device->has_xbox_paddles = SDL_TRUE;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE1);
++nbuttons;
}
if (controller.physicalInputProfile.buttons[GCInputXboxPaddleTwo] != nil) {
device->has_xbox_paddles = SDL_TRUE;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE2);
++nbuttons;
}
if (controller.physicalInputProfile.buttons[GCInputXboxPaddleThree] != nil) {
device->has_xbox_paddles = SDL_TRUE;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE3);
++nbuttons;
}
if (controller.physicalInputProfile.buttons[GCInputXboxPaddleFour] != nil) {
device->has_xbox_paddles = SDL_TRUE;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE4);
++nbuttons;
}
if (controller.physicalInputProfile.buttons[GCInputXboxShareButton] != nil) {
device->has_xbox_share_button = SDL_TRUE;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_MISC1);
++nbuttons;
}
}
#endif
#pragma clang diagnostic pop
if (is_xbox) {
vendor = USB_VENDOR_MICROSOFT;
if (device->has_xbox_paddles) {
/* Assume Xbox One Elite Series 2 Controller unless/until GCController flows VID/PID */
product = USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH;
subtype = 1;
} else if (device->has_xbox_share_button) {
/* Assume Xbox Series X Controller unless/until GCController flows VID/PID */
product = USB_PRODUCT_XBOX_SERIES_X_BLE;
subtype = 1;
} else {
/* Assume Xbox One S Bluetooth Controller unless/until GCController flows VID/PID */
product = USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH;
subtype = 0;
}
} else if (is_ps4) {
/* Assume DS4 Slim unless/until GCController flows VID/PID */
vendor = USB_VENDOR_SONY;
product = USB_PRODUCT_SONY_DS4_SLIM;
if (device->has_dualshock_touchpad) {
subtype = 1;
} else {
subtype = 0;
}
} else if (is_ps5) {
vendor = USB_VENDOR_SONY;
product = USB_PRODUCT_SONY_DS5;
subtype = 0;
} else if (is_switch_pro) {
vendor = USB_VENDOR_NINTENDO;
product = USB_PRODUCT_NINTENDO_SWITCH_PRO;
subtype = 0;
} else if (is_switch_joycon_pair) {
vendor = USB_VENDOR_NINTENDO;
product = USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;
subtype = 0;
} else {
vendor = USB_VENDOR_APPLE;
product = 1;
subtype = 1;
}
if (SDL_strcmp(name, "Backbone One") == 0) {
/* The Backbone app uses share button */
if ((device->button_mask & (1 << SDL_CONTROLLER_BUTTON_MISC1)) != 0) {
device->button_mask &= ~(1 << SDL_CONTROLLER_BUTTON_MISC1);
--nbuttons;
device->has_xbox_share_button = SDL_FALSE;
}
}
device->naxes = 6; /* 2 thumbsticks and 2 triggers */
device->nhats = 1; /* d-pad */
device->nbuttons = nbuttons;
} else if (controller.gamepad) {
BOOL is_switch_joyconL = IsControllerSwitchJoyConL(controller);
BOOL is_switch_joyconR = IsControllerSwitchJoyConR(controller);
int nbuttons = 0;
if (is_switch_joyconL) {
vendor = USB_VENDOR_NINTENDO;
product = USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT;
subtype = 0;
} else if (is_switch_joyconR) {
vendor = USB_VENDOR_NINTENDO;
product = USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT;
subtype = 0;
} else {
vendor = USB_VENDOR_APPLE;
product = 2;
subtype = 2;
}
/* These buttons are part of the original MFi spec */
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_X);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_Y);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
#if TARGET_OS_TV
/* The menu button is used by the OS and not available to applications */
nbuttons += 6;
#else
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
nbuttons += 7;
device->uses_pause_handler = SDL_TRUE;
#endif
device->naxes = 0; /* no traditional analog inputs */
device->nhats = 1; /* d-pad */
device->nbuttons = nbuttons;
}
#if TARGET_OS_TV
else if (controller.microGamepad) {
int nbuttons = 0;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B); /* Button X on microGamepad */
nbuttons += 2;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
++nbuttons;
device->uses_pause_handler = SDL_TRUE;
vendor = USB_VENDOR_APPLE;
product = 3;
subtype = 3;
device->naxes = 2; /* treat the touch surface as two axes */
device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
device->nbuttons = nbuttons;
controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
}
#endif /* TARGET_OS_TV */
if (vendor == USB_VENDOR_APPLE) {
/* Note that this is an MFI controller and what subtype it is */
device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_BLUETOOTH, vendor, product, 0, name, 'm', subtype);
} else {
device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_BLUETOOTH, vendor, product, 0, name, 0, subtype);
}
/* Update the GUID with capability bits */
{
Uint16 *guid16 = (Uint16 *)device->guid.data;
guid16[6] = SDL_SwapLE16(device->button_mask);
}
/* This will be set when the first button press of the controller is
* detected. */
controller.playerIndex = -1;
return TRUE;
}
#endif /* SDL_JOYSTICK_MFI */
#if defined(SDL_JOYSTICK_iOS_ACCELEROMETER) || defined(SDL_JOYSTICK_MFI)
static void
IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
{
SDL_JoystickDeviceItem *device = deviceList;
#if TARGET_OS_TV
if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
/* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
if (controller && !controller.extendedGamepad && !controller.gamepad && controller.microGamepad) {
return;
}
}
#endif
while (device != NULL) {
if (device->controller == controller) {
return;
}
device = device->next;
}
device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
if (device == NULL) {
return;
}
device->accelerometer = accelerometer;
device->instance_id = SDL_GetNextJoystickInstanceID();
if (accelerometer) {
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
device->name = SDL_strdup(accelerometerName);
device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
device->nhats = 0;
device->nbuttons = 0;
/* Use the accelerometer name as a GUID. */
SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
#else
SDL_free(device);
return;
#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER */
} else if (controller) {
#ifdef SDL_JOYSTICK_MFI
if (!IOS_AddMFIJoystickDevice(device, controller)) {
SDL_free(device);
return;
}
#else
SDL_free(device);
return;
#endif /* SDL_JOYSTICK_MFI */
}
if (deviceList == NULL) {
deviceList = device;
} else {
SDL_JoystickDeviceItem *lastdevice = deviceList;
while (lastdevice->next != NULL) {
lastdevice = lastdevice->next;
}
lastdevice->next = device;
}
++numjoysticks;
SDL_PrivateJoystickAdded(device->instance_id);
}
#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER || SDL_JOYSTICK_MFI */
static SDL_JoystickDeviceItem *
IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
{
SDL_JoystickDeviceItem *prev = NULL;
SDL_JoystickDeviceItem *next = NULL;
SDL_JoystickDeviceItem *item = deviceList;
if (device == NULL) {
return NULL;
}
next = device->next;
while (item != NULL) {
if (item == device) {
break;
}
prev = item;
item = item->next;
}
/* Unlink the device item from the device list. */
if (prev) {
prev->next = device->next;
} else if (device == deviceList) {
deviceList = device->next;
}
if (device->joystick) {
device->joystick->hwdata = NULL;
}
#ifdef SDL_JOYSTICK_MFI
@autoreleasepool {
if (device->controller) {
/* The controller was explicitly retained in the struct, so it
* should be explicitly released before freeing the struct. */
GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
controller.controllerPausedHandler = nil;
device->controller = nil;
}
}
#endif /* SDL_JOYSTICK_MFI */
--numjoysticks;
SDL_PrivateJoystickRemoved(device->instance_id);
SDL_free(device->name);
SDL_free(device);
return next;
}
#if TARGET_OS_TV
static void SDLCALL
SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
{
BOOL allowRotation = newValue != NULL && *newValue != '0';
@autoreleasepool {
for (GCController *controller in [GCController controllers]) {
if (controller.microGamepad) {
controller.microGamepad.allowsRotation = allowRotation;
}
}
}
}
#endif /* TARGET_OS_TV */
static int
IOS_JoystickInit(void)
{
#if defined(__MACOSX__)
#if _SDL_HAS_BUILTIN(__builtin_available)
if (@available(macOS 10.16, *)) {
/* Continue with initialization on macOS 11+ */
} else {
return 0;
}
#else
/* No @available, must be an older macOS version */
return 0;
#endif
#endif
@autoreleasepool {
#ifdef SDL_JOYSTICK_MFI
NSNotificationCenter *center;
#endif
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
/* Default behavior, accelerometer as joystick */
IOS_AddJoystickDevice(nil, SDL_TRUE);
}
#endif
#ifdef SDL_JOYSTICK_MFI
/* GameController.framework was added in iOS 7. */
if (![GCController class]) {
return 0;
}
/* For whatever reason, this always returns an empty array on
macOS 11.0.1 */
for (GCController *controller in [GCController controllers]) {
IOS_AddJoystickDevice(controller, SDL_FALSE);
}
#if TARGET_OS_TV
SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
center = [NSNotificationCenter defaultCenter];
connectObserver = [center addObserverForName:GCControllerDidConnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
SDL_LockJoysticks();
IOS_AddJoystickDevice(controller, SDL_FALSE);
SDL_UnlockJoysticks();
}];
disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
SDL_JoystickDeviceItem *device;
SDL_LockJoysticks();
for (device = deviceList; device != NULL; device = device->next) {
if (device->controller == controller) {
IOS_RemoveJoystickDevice(device);
break;
}
}
SDL_UnlockJoysticks();
}];
#endif /* SDL_JOYSTICK_MFI */
}
return 0;
}
static int
IOS_JoystickGetCount(void)
{
return numjoysticks;
}
static void
IOS_JoystickDetect(void)
{
}
static const char *
IOS_JoystickGetDeviceName(int device_index)
{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->name : "Unknown";
}
static const char *
IOS_JoystickGetDevicePath(int device_index)
{
return NULL;
}
static int
IOS_JoystickGetDevicePlayerIndex(int device_index)
{
#ifdef SDL_JOYSTICK_MFI
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
if (device && device->controller) {
return (int)device->controller.playerIndex;
}
#endif
return -1;
}
static void
IOS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
#ifdef SDL_JOYSTICK_MFI
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
if (device && device->controller) {
device->controller.playerIndex = player_index;
}
#endif
}
static SDL_JoystickGUID
IOS_JoystickGetDeviceGUID(int device_index)
{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
SDL_JoystickGUID guid;
if (device) {
guid = device->guid;
} else {
SDL_zero(guid);
}
return guid;
}
static SDL_JoystickID
IOS_JoystickGetDeviceInstanceID(int device_index)
{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->instance_id : -1;
}
static int
IOS_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
if (device == NULL) {
return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
}
joystick->hwdata = device;
joystick->instance_id = device->instance_id;
joystick->naxes = device->naxes;
joystick->nhats = device->nhats;
joystick->nbuttons = device->nbuttons;
joystick->nballs = 0;
if (device->has_dualshock_touchpad) {
SDL_PrivateJoystickAddTouchpad(joystick, 2);
}
device->joystick = joystick;
@autoreleasepool {
if (device->accelerometer) {
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
/* Shorter times between updates can significantly increase CPU usage. */
motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates];
#endif
} else {
#ifdef SDL_JOYSTICK_MFI
if (device->uses_pause_handler) {
GCController *controller = device->controller;
controller.controllerPausedHandler = ^(GCController *c) {
if (joystick->hwdata) {
++joystick->hwdata->num_pause_presses;
}
};
}
#ifdef ENABLE_MFI_SENSORS
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = joystick->hwdata->controller;
GCMotion *motion = controller.motion;
if (motion && motion.hasRotationRate) {
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);
}
if (motion && motion.hasGravityAndUserAcceleration) {
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 0.0f);
}
}
#endif /* ENABLE_MFI_SENSORS */
#ifdef ENABLE_MFI_SYSTEM_GESTURE_STATE
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = joystick->hwdata->controller;
for (id key in controller.physicalInputProfile.buttons) {
GCControllerButtonInput *button = controller.physicalInputProfile.buttons[key];
if ([button isBoundToSystemGesture]) {
button.preferredSystemGestureState = GCSystemGestureStateDisabled;
}
}
}
#endif /* ENABLE_MFI_SYSTEM_GESTURE_STATE */
#endif /* SDL_JOYSTICK_MFI */
}
}
if (device->remote) {
++SDL_AppleTVRemoteOpenedAsJoystick;
}
return 0;
}
static void
IOS_AccelerometerUpdate(SDL_Joystick *joystick)
{
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
const float maxgforce = SDL_IPHONE_MAX_GFORCE;
const SInt16 maxsint16 = 0x7FFF;
CMAcceleration accel;
@autoreleasepool {
if (!motionManager.isAccelerometerActive) {
return;
}
accel = motionManager.accelerometerData.acceleration;
}
/*
Convert accelerometer data from floating point to Sint16, which is what
the joystick system expects.
To do the conversion, the data is first clamped onto the interval
[-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
by MAX_SINT16 so that it is mapped to the full range of an Sint16.
You can customize the clamped range of this function by modifying the
SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
Once converted to Sint16, the accelerometer data no longer has coherent
units. You can convert the data back to units of g-force by multiplying
it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
*/
/* clamp the data */
accel.x = SDL_clamp(accel.x, -maxgforce, maxgforce);
accel.y = SDL_clamp(accel.y, -maxgforce, maxgforce);
accel.z = SDL_clamp(accel.z, -maxgforce, maxgforce);
/* pass in data mapped to range of SInt16 */
SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER */
}
#ifdef SDL_JOYSTICK_MFI
static Uint8
IOS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
{
Uint8 hat = 0;
if (dpad.up.isPressed) {
hat |= SDL_HAT_UP;
} else if (dpad.down.isPressed) {
hat |= SDL_HAT_DOWN;
}
if (dpad.left.isPressed) {
hat |= SDL_HAT_LEFT;
} else if (dpad.right.isPressed) {
hat |= SDL_HAT_RIGHT;
}
if (hat == 0) {
return SDL_HAT_CENTERED;
}
return hat;
}
#endif
static void
IOS_MFIJoystickUpdate(SDL_Joystick *joystick)
{
#if SDL_JOYSTICK_MFI
@autoreleasepool {
GCController *controller = joystick->hwdata->controller;
Uint8 hatstate = SDL_HAT_CENTERED;
int i;
int pause_button_index = 0;
#ifdef DEBUG_CONTROLLER_STATE
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
if (controller.physicalInputProfile) {
for (id key in controller.physicalInputProfile.buttons) {
GCControllerButtonInput *button = controller.physicalInputProfile.buttons[key];
if (button.isPressed)
NSLog(@"Button %@ = %s\n", key, button.isPressed ? "pressed" : "released");
}
for (id key in controller.physicalInputProfile.axes) {
GCControllerAxisInput *axis = controller.physicalInputProfile.axes[key];
if (axis.value != 0.0f)
NSLog(@"Axis %@ = %.2f\n", key, axis.value);
}
}
}
#endif
if (controller.extendedGamepad) {
SDL_bool isstack;
GCExtendedGamepad *gamepad = controller.extendedGamepad;
/* Axis order matches the XInput Windows mappings. */
Sint16 axes[] = {
(Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
(Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
(Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
(Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
(Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
(Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
};
/* Button order matches the XInput Windows mappings. */
Uint8 *buttons = SDL_small_alloc(Uint8, joystick->nbuttons, &isstack);
int button_count = 0;
if (buttons == NULL) {
SDL_OutOfMemory();
return;
}
/* These buttons are part of the original MFi spec */
buttons[button_count++] = gamepad.buttonA.isPressed;
buttons[button_count++] = gamepad.buttonB.isPressed;
buttons[button_count++] = gamepad.buttonX.isPressed;
buttons[button_count++] = gamepad.buttonY.isPressed;
buttons[button_count++] = gamepad.leftShoulder.isPressed;
buttons[button_count++] = gamepad.rightShoulder.isPressed;
/* These buttons are available on some newer controllers */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK)) {
buttons[button_count++] = gamepad.leftThumbstickButton.isPressed;
}
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK)) {
buttons[button_count++] = gamepad.rightThumbstickButton.isPressed;
}
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
buttons[button_count++] = gamepad.buttonOptions.isPressed;
}
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_GUIDE)) {
buttons[button_count++] = gamepad.buttonHome.isPressed;
}
/* This must be the last button, so we can optionally handle it with pause_button_index below */
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
if (joystick->hwdata->uses_pause_handler) {
pause_button_index = button_count;
buttons[button_count++] = joystick->delayed_guide_button;
} else {
buttons[button_count++] = gamepad.buttonMenu.isPressed;
}
}
#ifdef ENABLE_PHYSICAL_INPUT_PROFILE
if (joystick->hwdata->has_dualshock_touchpad) {
GCControllerDirectionPad *dpad;
buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputDualShockTouchpadButton].isPressed;
dpad = controller.physicalInputProfile.dpads[GCInputDualShockTouchpadOne];
if (dpad.xAxis.value || dpad.yAxis.value) {
SDL_PrivateJoystickTouchpad(joystick, 0, 0, SDL_PRESSED, (1.0f + dpad.xAxis.value) * 0.5f, 1.0f - (1.0f + dpad.yAxis.value) * 0.5f, 1.0f);
} else {
SDL_PrivateJoystickTouchpad(joystick, 0, 0, SDL_RELEASED, 0.0f, 0.0f, 1.0f);
}
dpad = controller.physicalInputProfile.dpads[GCInputDualShockTouchpadTwo];
if (dpad.xAxis.value || dpad.yAxis.value) {
SDL_PrivateJoystickTouchpad(joystick, 0, 1, SDL_PRESSED, (1.0f + dpad.xAxis.value) * 0.5f, 1.0f - (1.0f + dpad.yAxis.value) * 0.5f, 1.0f);
} else {
SDL_PrivateJoystickTouchpad(joystick, 0, 1, SDL_RELEASED, 0.0f, 0.0f, 1.0f);
}
}
if (joystick->hwdata->has_xbox_paddles) {
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE1)) {
buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleOne].isPressed;
}
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE2)) {
buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleTwo].isPressed;
}
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE3)) {
buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleThree].isPressed;
}
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE4)) {
buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleFour].isPressed;
}
/*
SDL_Log("Paddles: [%d,%d,%d,%d]",
controller.physicalInputProfile.buttons[GCInputXboxPaddleOne].isPressed,
controller.physicalInputProfile.buttons[GCInputXboxPaddleTwo].isPressed,
controller.physicalInputProfile.buttons[GCInputXboxPaddleThree].isPressed,
controller.physicalInputProfile.buttons[GCInputXboxPaddleFour].isPressed);
*/
}
if (joystick->hwdata->has_xbox_share_button) {
buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxShareButton].isPressed;
}
#endif
#pragma clang diagnostic pop
hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
for (i = 0; i < SDL_arraysize(axes); i++) {
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
for (i = 0; i < button_count; i++) {
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
#ifdef ENABLE_MFI_SENSORS
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCMotion *motion = controller.motion;
if (motion && motion.sensorsActive) {
float data[3];
if (motion.hasRotationRate) {
GCRotationRate rate = motion.rotationRate;
data[0] = rate.x;
data[1] = rate.z;
data[2] = -rate.y;
SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, 0, data, 3);
}
if (motion.hasGravityAndUserAcceleration) {
GCAcceleration accel = motion.acceleration;
data[0] = -accel.x * SDL_STANDARD_GRAVITY;
data[1] = -accel.y * SDL_STANDARD_GRAVITY;
data[2] = -accel.z * SDL_STANDARD_GRAVITY;
SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_ACCEL, 0, data, 3);
}
}
}
#endif /* ENABLE_MFI_SENSORS */
SDL_small_free(buttons, isstack);
} else if (controller.gamepad) {
SDL_bool isstack;
GCGamepad *gamepad = controller.gamepad;
/* Button order matches the XInput Windows mappings. */
Uint8 *buttons = SDL_small_alloc(Uint8, joystick->nbuttons, &isstack);
int button_count = 0;
if (buttons == NULL) {
SDL_OutOfMemory();
return;
}
buttons[button_count++] = gamepad.buttonA.isPressed;
buttons[button_count++] = gamepad.buttonB.isPressed;
buttons[button_count++] = gamepad.buttonX.isPressed;
buttons[button_count++] = gamepad.buttonY.isPressed;
buttons[button_count++] = gamepad.leftShoulder.isPressed;
buttons[button_count++] = gamepad.rightShoulder.isPressed;
pause_button_index = button_count;
buttons[button_count++] = joystick->delayed_guide_button;
hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
for (i = 0; i < button_count; i++) {
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
SDL_small_free(buttons, isstack);
}
#if TARGET_OS_TV
else if (controller.microGamepad) {
GCMicroGamepad *gamepad = controller.microGamepad;
Sint16 axes[] = {
(Sint16) (gamepad.dpad.xAxis.value * 32767),
(Sint16) (gamepad.dpad.yAxis.value * -32767),
};
for (i = 0; i < SDL_arraysize(axes); i++) {
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
Uint8 buttons[joystick->nbuttons];
int button_count = 0;
buttons[button_count++] = gamepad.buttonA.isPressed;
buttons[button_count++] = gamepad.buttonX.isPressed;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
/* This must be the last button, so we can optionally handle it with pause_button_index below */
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
if (joystick->hwdata->uses_pause_handler) {
pause_button_index = button_count;
buttons[button_count++] = joystick->delayed_guide_button;
} else {
buttons[button_count++] = gamepad.buttonMenu.isPressed;
}
}
#pragma clang diagnostic pop
for (i = 0; i < button_count; i++) {
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
}
#endif /* TARGET_OS_TV */
if (joystick->nhats > 0) {
SDL_PrivateJoystickHat(joystick, 0, hatstate);
}
if (joystick->hwdata->uses_pause_handler) {
for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_PRESSED);
SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_RELEASED);
}
joystick->hwdata->num_pause_presses = 0;
}
#ifdef ENABLE_MFI_BATTERY
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCDeviceBattery *battery = controller.battery;
if (battery) {
SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
switch (battery.batteryState) {
case GCDeviceBatteryStateDischarging:
{
float power_level = battery.batteryLevel;
if (power_level <= 0.05f) {
ePowerLevel = SDL_JOYSTICK_POWER_EMPTY;
} else if (power_level <= 0.20f) {
ePowerLevel = SDL_JOYSTICK_POWER_LOW;
} else if (power_level <= 0.70f) {
ePowerLevel = SDL_JOYSTICK_POWER_MEDIUM;
} else {
ePowerLevel = SDL_JOYSTICK_POWER_FULL;
}
}
break;
case GCDeviceBatteryStateCharging:
ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
break;
case GCDeviceBatteryStateFull:
ePowerLevel = SDL_JOYSTICK_POWER_FULL;
break;
default:
break;
}
SDL_PrivateJoystickBatteryLevel(joystick, ePowerLevel);
}
}
#endif /* ENABLE_MFI_BATTERY */
}
#endif /* SDL_JOYSTICK_MFI */
}
#ifdef ENABLE_MFI_RUMBLE
@interface SDL_RumbleMotor : NSObject
@property(nonatomic,strong) CHHapticEngine *engine API_AVAILABLE(macos(10.16), ios(13.0), tvos(14.0));
@property(nonatomic,strong) id<CHHapticPatternPlayer> player API_AVAILABLE(macos(10.16), ios(13.0), tvos(14.0));
@property bool active;
@end
@implementation SDL_RumbleMotor {
}
-(void)cleanup
{
@autoreleasepool {
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
if (self.player != nil) {
[self.player cancelAndReturnError:nil];
self.player = nil;
}
if (self.engine != nil) {
[self.engine stopWithCompletionHandler:nil];
self.engine = nil;
}
}
}
}
-(int)setIntensity:(float)intensity
{
@autoreleasepool {
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
NSError *error = nil;
CHHapticDynamicParameter *param;
if (self.engine == nil) {
return SDL_SetError("Haptics engine was stopped");
}
if (intensity == 0.0f) {
if (self.player && self.active) {
[self.player stopAtTime:0 error:&error];
}
self.active = false;
return 0;
}
if (self.player == nil) {
CHHapticEventParameter *event_param = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:1.0f];
CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:[NSArray arrayWithObjects:event_param, nil] relativeTime:0 duration:GCHapticDurationInfinite];
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:[NSArray arrayWithObject:event] parameters:[[NSArray alloc] init] error:&error];
if (error != nil) {
return SDL_SetError("Couldn't create haptic pattern: %s", [error.localizedDescription UTF8String]);
}
self.player = [self.engine createPlayerWithPattern:pattern error:&error];
if (error != nil) {
return SDL_SetError("Couldn't create haptic player: %s", [error.localizedDescription UTF8String]);
}
self.active = false;
}
param = [[CHHapticDynamicParameter alloc] initWithParameterID:CHHapticDynamicParameterIDHapticIntensityControl value:intensity relativeTime:0];
[self.player sendParameters:[NSArray arrayWithObject:param] atTime:0 error:&error];
if (error != nil) {
return SDL_SetError("Couldn't update haptic player: %s", [error.localizedDescription UTF8String]);
}
if (!self.active) {
[self.player startAtTime:0 error:&error];
self.active = true;
}
}
return 0;
}
}
-(id) initWithController:(GCController*)controller locality:(GCHapticsLocality)locality API_AVAILABLE(macos(10.16), ios(14.0), tvos(14.0))
{
@autoreleasepool {
NSError *error;
__weak __typeof(self) weakSelf;
self = [super init];
weakSelf = self;
self.engine = [controller.haptics createEngineWithLocality:locality];
if (self.engine == nil) {
SDL_SetError("Couldn't create haptics engine");
return nil;
}
[self.engine startAndReturnError:&error];
if (error != nil) {
SDL_SetError("Couldn't start haptics engine");
return nil;
}
self.engine.stoppedHandler = ^(CHHapticEngineStoppedReason stoppedReason) {
SDL_RumbleMotor *_this = weakSelf;
if (_this == nil) {
return;
}
_this.player = nil;
_this.engine = nil;
};
self.engine.resetHandler = ^{
SDL_RumbleMotor *_this = weakSelf;
if (_this == nil) {
return;
}
_this.player = nil;
[_this.engine startAndReturnError:nil];
};
return self;
}
}
@end
@interface SDL_RumbleContext : NSObject
@property(nonatomic,strong) SDL_RumbleMotor *m_low_frequency_motor;
@property(nonatomic,strong) SDL_RumbleMotor *m_high_frequency_motor;
@property(nonatomic,strong) SDL_RumbleMotor *m_left_trigger_motor;
@property(nonatomic,strong) SDL_RumbleMotor *m_right_trigger_motor;
@end
@implementation SDL_RumbleContext {
}
-(id) initWithLowFrequencyMotor:(SDL_RumbleMotor*)low_frequency_motor
HighFrequencyMotor:(SDL_RumbleMotor*)high_frequency_motor
LeftTriggerMotor:(SDL_RumbleMotor*)left_trigger_motor
RightTriggerMotor:(SDL_RumbleMotor*)right_trigger_motor
{
self = [super init];
self.m_low_frequency_motor = low_frequency_motor;
self.m_high_frequency_motor = high_frequency_motor;
self.m_left_trigger_motor = left_trigger_motor;
self.m_right_trigger_motor = right_trigger_motor;
return self;
}
-(int) rumbleWithLowFrequency:(Uint16)low_frequency_rumble andHighFrequency:(Uint16)high_frequency_rumble
{
int result = 0;
result += [self.m_low_frequency_motor setIntensity:((float)low_frequency_rumble / 65535.0f)];
result += [self.m_high_frequency_motor setIntensity:((float)high_frequency_rumble / 65535.0f)];
return ((result < 0) ? -1 : 0);
}
-(int) rumbleLeftTrigger:(Uint16)left_rumble andRightTrigger:(Uint16)right_rumble
{
int result = 0;
if (self.m_left_trigger_motor && self.m_right_trigger_motor) {
result += [self.m_left_trigger_motor setIntensity:((float)left_rumble / 65535.0f)];
result += [self.m_right_trigger_motor setIntensity:((float)right_rumble / 65535.0f)];
} else {
result = SDL_Unsupported();
}
return ((result < 0) ? -1 : 0);
}
-(void)cleanup
{
[self.m_low_frequency_motor cleanup];
[self.m_high_frequency_motor cleanup];
}
@end
static SDL_RumbleContext *IOS_JoystickInitRumble(GCController *controller)
{
@autoreleasepool {
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
SDL_RumbleMotor *low_frequency_motor = [[SDL_RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityLeftHandle];
SDL_RumbleMotor *high_frequency_motor = [[SDL_RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityRightHandle];
SDL_RumbleMotor *left_trigger_motor = [[SDL_RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityLeftTrigger];
SDL_RumbleMotor *right_trigger_motor = [[SDL_RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityRightTrigger];
if (low_frequency_motor && high_frequency_motor) {
return [[SDL_RumbleContext alloc] initWithLowFrequencyMotor:low_frequency_motor
HighFrequencyMotor:high_frequency_motor
LeftTriggerMotor:left_trigger_motor
RightTriggerMotor:right_trigger_motor];
}
}
}
return nil;
}
#endif /* ENABLE_MFI_RUMBLE */
static int
IOS_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
#ifdef ENABLE_MFI_RUMBLE
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return SDL_SetError("Controller is no longer connected");
}
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
if (!device->rumble && device->controller && device->controller.haptics) {
SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller);
if (rumble) {
device->rumble = (void *)CFBridgingRetain(rumble);
}
}
}
if (device->rumble) {
SDL_RumbleContext *rumble = (__bridge SDL_RumbleContext *)device->rumble;
return [rumble rumbleWithLowFrequency:low_frequency_rumble andHighFrequency:high_frequency_rumble];
} else {
return SDL_Unsupported();
}
#else
return SDL_Unsupported();
#endif
}
static int
IOS_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
#ifdef ENABLE_MFI_RUMBLE
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return SDL_SetError("Controller is no longer connected");
}
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
if (!device->rumble && device->controller && device->controller.haptics) {
SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller);
if (rumble) {
device->rumble = (void *)CFBridgingRetain(rumble);
}
}
}
if (device->rumble) {
SDL_RumbleContext *rumble = (__bridge SDL_RumbleContext *)device->rumble;
return [rumble rumbleLeftTrigger:left_rumble andRightTrigger:right_rumble];
} else {
return SDL_Unsupported();
}
#else
return SDL_Unsupported();
#endif
}
static Uint32
IOS_JoystickGetCapabilities(SDL_Joystick *joystick)
{
Uint32 result = 0;
#if defined(ENABLE_MFI_LIGHT) || defined(ENABLE_MFI_RUMBLE)
@autoreleasepool {
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return 0;
}
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = device->controller;
#ifdef ENABLE_MFI_LIGHT
if (controller.light) {
result |= SDL_JOYCAP_LED;
}
#endif
#ifdef ENABLE_MFI_RUMBLE
if (controller.haptics) {
for (GCHapticsLocality locality in controller.haptics.supportedLocalities) {
if ([locality isEqualToString:GCHapticsLocalityHandles]) {
result |= SDL_JOYCAP_RUMBLE;
} else if ([locality isEqualToString:GCHapticsLocalityTriggers]) {
result |= SDL_JOYCAP_RUMBLE_TRIGGERS;
}
}
}
#endif
}
}
#endif /* ENABLE_MFI_LIGHT || ENABLE_MFI_RUMBLE */
return result;
}
static int
IOS_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
#ifdef ENABLE_MFI_LIGHT
@autoreleasepool {
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return SDL_SetError("Controller is no longer connected");
}
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = device->controller;
GCDeviceLight *light = controller.light;
if (light) {
light.color = [[GCColor alloc] initWithRed:(float)red / 255.0f
green:(float)green / 255.0f
blue:(float)blue / 255.0f];
return 0;
}
}
}
#endif /* ENABLE_MFI_LIGHT */
return SDL_Unsupported();
}
static int
IOS_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int
IOS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{
#ifdef ENABLE_MFI_SENSORS
@autoreleasepool {
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return SDL_SetError("Controller is no longer connected");
}
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = device->controller;
GCMotion *motion = controller.motion;
if (motion) {
motion.sensorsActive = enabled ? YES : NO;
return 0;
}
}
}
#endif /* ENABLE_MFI_SENSORS */
return SDL_Unsupported();
}
static void
IOS_JoystickUpdate(SDL_Joystick *joystick)
{
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
if (device->accelerometer) {
IOS_AccelerometerUpdate(joystick);
} else if (device->controller) {
IOS_MFIJoystickUpdate(joystick);
}
}
static void
IOS_JoystickClose(SDL_Joystick *joystick)
{
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
device->joystick = NULL;
@autoreleasepool {
#ifdef ENABLE_MFI_RUMBLE
if (device->rumble) {
SDL_RumbleContext *rumble = (__bridge SDL_RumbleContext *)device->rumble;
[rumble cleanup];
CFRelease(device->rumble);
device->rumble = NULL;
}
#endif /* ENABLE_MFI_RUMBLE */
if (device->accelerometer) {
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
[motionManager stopAccelerometerUpdates];
#endif
} else if (device->controller) {
#ifdef SDL_JOYSTICK_MFI
GCController *controller = device->controller;
controller.controllerPausedHandler = nil;
controller.playerIndex = -1;
#ifdef ENABLE_MFI_SYSTEM_GESTURE_STATE
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
for (id key in controller.physicalInputProfile.buttons) {
GCControllerButtonInput *button = controller.physicalInputProfile.buttons[key];
if ([button isBoundToSystemGesture]) {
button.preferredSystemGestureState = GCSystemGestureStateEnabled;
}
}
}
#endif /* ENABLE_MFI_SYSTEM_GESTURE_STATE */
#endif /* SDL_JOYSTICK_MFI */
}
}
if (device->remote) {
--SDL_AppleTVRemoteOpenedAsJoystick;
}
}
static void
IOS_JoystickQuit(void)
{
@autoreleasepool {
#ifdef SDL_JOYSTICK_MFI
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
if (connectObserver) {
[center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
connectObserver = nil;
}
if (disconnectObserver) {
[center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
disconnectObserver = nil;
}
#if TARGET_OS_TV
SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
#endif /* SDL_JOYSTICK_MFI */
while (deviceList != NULL) {
IOS_RemoveJoystickDevice(deviceList);
}
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
motionManager = nil;
#endif
}
numjoysticks = 0;
}
static SDL_bool
IOS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
{
return SDL_FALSE;
}
#if defined(SDL_JOYSTICK_MFI) && defined(__MACOSX__)
SDL_bool IOS_SupportedHIDDevice(IOHIDDeviceRef device)
{
if (@available(macOS 10.16, *)) {
if ([GCController supportsHIDDevice:device]) {
return SDL_TRUE;
}
/* GCController supportsHIDDevice may return false if the device hasn't been
* seen by the framework yet, so check a few controllers we know are supported.
*/
{
Sint32 vendor = 0;
Sint32 product = 0;
CFTypeRef refCF = NULL;
refCF = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor);
}
refCF = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &product);
}
if (vendor == USB_VENDOR_MICROSOFT && SDL_IsJoystickXboxSeriesX(vendor, product)) {
return SDL_TRUE;
}
}
}
return SDL_FALSE;
}
#endif
#if defined(SDL_JOYSTICK_MFI) && defined(ENABLE_PHYSICAL_INPUT_PROFILE)
static void
GetAppleSFSymbolsNameForElement(GCControllerElement *element, char *name)
{
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
if (element) {
[element.sfSymbolsName getCString: name maxLength: 255 encoding: NSASCIIStringEncoding];
}
}
}
static GCControllerDirectionPad *
GetDirectionalPadForController(GCController *controller)
{
if (controller.extendedGamepad) {
return controller.extendedGamepad.dpad;
}
if (controller.gamepad) {
return controller.gamepad.dpad;
}
if (controller.microGamepad) {
return controller.microGamepad.dpad;
}
return nil;
}
#endif /* SDL_JOYSTICK_MFI && ENABLE_PHYSICAL_INPUT_PROFILE */
static char elementName[256];
const char *
IOS_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
elementName[0] = '\0';
#if defined(SDL_JOYSTICK_MFI) && defined(ENABLE_PHYSICAL_INPUT_PROFILE)
if (gamecontroller && SDL_GameControllerGetJoystick(gamecontroller)->driver == &SDL_IOS_JoystickDriver) {
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = SDL_GameControllerGetJoystick(gamecontroller)->hwdata->controller;
if ([controller respondsToSelector:@selector(physicalInputProfile)]) {
NSDictionary<NSString *,GCControllerElement *> *elements = controller.physicalInputProfile.elements;
switch (button)
{
case SDL_CONTROLLER_BUTTON_A:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonA], elementName);
break;
case SDL_CONTROLLER_BUTTON_B:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonB], elementName);
break;
case SDL_CONTROLLER_BUTTON_X:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonX], elementName);
break;
case SDL_CONTROLLER_BUTTON_Y:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonY], elementName);
break;
case SDL_CONTROLLER_BUTTON_BACK:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonOptions], elementName);
break;
case SDL_CONTROLLER_BUTTON_GUIDE:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonHome], elementName);
break;
case SDL_CONTROLLER_BUTTON_START:
GetAppleSFSymbolsNameForElement(elements[GCInputButtonMenu], elementName);
break;
case SDL_CONTROLLER_BUTTON_LEFTSTICK:
GetAppleSFSymbolsNameForElement(elements[GCInputLeftThumbstickButton], elementName);
break;
case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
GetAppleSFSymbolsNameForElement(elements[GCInputRightThumbstickButton], elementName);
break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
GetAppleSFSymbolsNameForElement(elements[GCInputLeftShoulder], elementName);
break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
GetAppleSFSymbolsNameForElement(elements[GCInputRightShoulder], elementName);
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP: {
GCControllerDirectionPad * dpad = GetDirectionalPadForController(controller);
if (dpad) {
GetAppleSFSymbolsNameForElement(dpad.up, elementName);
if (SDL_strlen(elementName) == 0) {
SDL_strlcpy(elementName, "dpad.up.fill", sizeof(elementName));
}
}
break;
}
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: {
GCControllerDirectionPad * dpad = GetDirectionalPadForController(controller);
if (dpad) {
GetAppleSFSymbolsNameForElement(dpad.down, elementName);
if (SDL_strlen(elementName) == 0) {
SDL_strlcpy(elementName, "dpad.down.fill", sizeof(elementName));
}
}
break;
}
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: {
GCControllerDirectionPad * dpad = GetDirectionalPadForController(controller);
if (dpad) {
GetAppleSFSymbolsNameForElement(dpad.left, elementName);
if (SDL_strlen(elementName) == 0) {
SDL_strlcpy(elementName, "dpad.left.fill", sizeof(elementName));
}
}
break;
}
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: {
GCControllerDirectionPad * dpad = GetDirectionalPadForController(controller);
if (dpad) {
GetAppleSFSymbolsNameForElement(dpad.right, elementName);
if (SDL_strlen(elementName) == 0) {
SDL_strlcpy(elementName, "dpad.right.fill", sizeof(elementName));
}
}
break;
}
case SDL_CONTROLLER_BUTTON_MISC1:
GetAppleSFSymbolsNameForElement(elements[GCInputDualShockTouchpadButton], elementName);
break;
case SDL_CONTROLLER_BUTTON_PADDLE1:
GetAppleSFSymbolsNameForElement(elements[GCInputXboxPaddleOne], elementName);
break;
case SDL_CONTROLLER_BUTTON_PADDLE2:
GetAppleSFSymbolsNameForElement(elements[GCInputXboxPaddleTwo], elementName);
break;
case SDL_CONTROLLER_BUTTON_PADDLE3:
GetAppleSFSymbolsNameForElement(elements[GCInputXboxPaddleThree], elementName);
break;
case SDL_CONTROLLER_BUTTON_PADDLE4:
GetAppleSFSymbolsNameForElement(elements[GCInputXboxPaddleFour], elementName);
break;
case SDL_CONTROLLER_BUTTON_TOUCHPAD:
GetAppleSFSymbolsNameForElement(elements[GCInputDualShockTouchpadButton], elementName);
break;
default:
break;
}
}
}
}
#endif
return elementName;
}
const char *
IOS_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
{
elementName[0] = '\0';
#if defined(SDL_JOYSTICK_MFI) && defined(ENABLE_PHYSICAL_INPUT_PROFILE)
if (gamecontroller && SDL_GameControllerGetJoystick(gamecontroller)->driver == &SDL_IOS_JoystickDriver) {
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCController *controller = SDL_GameControllerGetJoystick(gamecontroller)->hwdata->controller;
if ([controller respondsToSelector:@selector(physicalInputProfile)]) {
NSDictionary<NSString *,GCControllerElement *> *elements = controller.physicalInputProfile.elements;
switch (axis)
{
case SDL_CONTROLLER_AXIS_LEFTX:
GetAppleSFSymbolsNameForElement(elements[GCInputLeftThumbstick], elementName);
break;
case SDL_CONTROLLER_AXIS_LEFTY:
GetAppleSFSymbolsNameForElement(elements[GCInputLeftThumbstick], elementName);
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
GetAppleSFSymbolsNameForElement(elements[GCInputRightThumbstick], elementName);
break;
case SDL_CONTROLLER_AXIS_RIGHTY:
GetAppleSFSymbolsNameForElement(elements[GCInputRightThumbstick], elementName);
break;
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
GetAppleSFSymbolsNameForElement(elements[GCInputLeftTrigger], elementName);
break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
GetAppleSFSymbolsNameForElement(elements[GCInputRightTrigger], elementName);
break;
default:
break;
}
}
}
}
#endif
return *elementName ? elementName : NULL;
}
SDL_JoystickDriver SDL_IOS_JoystickDriver =
{
IOS_JoystickInit,
IOS_JoystickGetCount,
IOS_JoystickDetect,
IOS_JoystickGetDeviceName,
IOS_JoystickGetDevicePath,
IOS_JoystickGetDevicePlayerIndex,
IOS_JoystickSetDevicePlayerIndex,
IOS_JoystickGetDeviceGUID,
IOS_JoystickGetDeviceInstanceID,
IOS_JoystickOpen,
IOS_JoystickRumble,
IOS_JoystickRumbleTriggers,
IOS_JoystickGetCapabilities,
IOS_JoystickSetLED,
IOS_JoystickSendEffect,
IOS_JoystickSetSensorsEnabled,
IOS_JoystickUpdate,
IOS_JoystickClose,
IOS_JoystickQuit,
IOS_JoystickGetGamepadMapping
};
/* vi: set ts=4 sw=4 expandtab: */