Added support for clang thread-safety analysis The annotations have been added to SDL_mutex.h and have been made public so applications can enable this for their own code. Clang assumes that locking and unlocking can't fail, but SDL has the concept of a NULL mutex, so the mutex functions have been changed not to report errors if a mutex hasn't been initialized. We do have mutexes that might be accessed when they are NULL, notably in the event system, so this is an important change. This commit cleans up a bunch of rare race conditions in the joystick and game controller code so now everything should be completely protected by the joystick lock. To test this, change the compiler to "clang -Wthread-safety -Werror=thread-safety -DSDL_THREAD_SAFETY_ANALYSIS"
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 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
diff --git a/include/SDL_joystick.h b/include/SDL_joystick.h
index 72c6604..9dcadc7 100644
--- a/include/SDL_joystick.h
+++ b/include/SDL_joystick.h
@@ -44,6 +44,7 @@
#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_guid.h"
+#include "SDL_mutex.h"
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
@@ -66,6 +67,9 @@ extern "C" {
/**
* The joystick structure used to identify an SDL joystick
*/
+#ifdef SDL_THREAD_SAFETY_ANALYSIS
+extern SDL_mutex *SDL_joystick_lock;
+#endif
struct _SDL_Joystick;
typedef struct _SDL_Joystick SDL_Joystick;
@@ -131,7 +135,7 @@ typedef enum
*
* \since This function is available since SDL 2.0.7.
*/
-extern DECLSPEC void SDLCALL SDL_LockJoysticks(void);
+extern DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
/**
@@ -146,7 +150,7 @@ extern DECLSPEC void SDLCALL SDL_LockJoysticks(void);
*
* \since This function is available since SDL 2.0.7.
*/
-extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void);
+extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_joystick_lock);
/**
* Count the number of joysticks attached to the system.
diff --git a/include/SDL_mutex.h b/include/SDL_mutex.h
index d4d55a4..1e2f2f4 100644
--- a/include/SDL_mutex.h
+++ b/include/SDL_mutex.h
@@ -31,6 +31,80 @@
#include "SDL_stdinc.h"
#include "SDL_error.h"
+/******************************************************************************/
+/* Enable thread safety attributes only with clang.
+ * The attributes can be safely erased when compiling with other compilers.
+ */
+#if defined(SDL_THREAD_SAFETY_ANALYSIS) && \
+ defined(__clang__) && (!defined(SWIG))
+#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
+#else
+#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
+#endif
+
+#define SDL_CAPABILITY(x) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
+
+#define SDL_SCOPED_CAPABILITY \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
+
+#define SDL_GUARDED_BY(x) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
+
+#define SDL_PT_GUARDED_BY(x) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
+
+#define SDL_ACQUIRED_BEFORE(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
+
+#define SDL_ACQUIRED_AFTER(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
+
+#define SDL_REQUIRES(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
+
+#define SDL_REQUIRES_SHARED(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
+
+#define SDL_ACQUIRE(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
+
+#define SDL_ACQUIRE_SHARED(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
+
+#define SDL_RELEASE(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
+
+#define SDL_RELEASE_SHARED(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
+
+#define SDL_RELEASE_GENERIC(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__))
+
+#define SDL_TRY_ACQUIRE(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
+
+#define SDL_TRY_ACQUIRE_SHARED(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
+
+#define SDL_EXCLUDES(...) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
+
+#define SDL_ASSERT_CAPABILITY(x) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
+
+#define SDL_ASSERT_SHARED_CAPABILITY(x) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
+
+#define SDL_RETURN_CAPABILITY(x) \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
+
+#define SDL_NO_THREAD_SAFETY_ANALYSIS \
+ SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
+
+/******************************************************************************/
+
+
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
@@ -96,7 +170,7 @@ extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
*
* \since This function is available since SDL 2.0.0.
*/
-extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
+extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex) SDL_ACQUIRE(mutex);
#define SDL_mutexP(m) SDL_LockMutex(m)
/**
@@ -119,7 +193,7 @@ extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
* \sa SDL_LockMutex
* \sa SDL_UnlockMutex
*/
-extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
+extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex) SDL_TRY_ACQUIRE(0, mutex);
/**
* Unlock the mutex.
@@ -138,7 +212,7 @@ extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
*
* \since This function is available since SDL 2.0.0.
*/
-extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
+extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex) SDL_RELEASE(mutex);
#define SDL_mutexV(m) SDL_UnlockMutex(m)
/**
diff --git a/src/SDL_assert.c b/src/SDL_assert.c
index a689a55..7ff864a 100644
--- a/src/SDL_assert.c
+++ b/src/SDL_assert.c
@@ -345,10 +345,8 @@ SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
}
SDL_AtomicUnlock(&spinlock);
- if (SDL_LockMutex(assertion_mutex) < 0) {
- return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
- }
-#endif
+ SDL_LockMutex(assertion_mutex);
+#endif /* !SDL_THREADS_DISABLED */
/* doing this because Visual C is upset over assigning in the macro. */
if (data->trigger_count == 0) {
diff --git a/src/SDL_log.c b/src/SDL_log.c
index e9199cc..80d36b5 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -336,15 +336,9 @@ void SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va
}
}
- if (log_function_mutex) {
- SDL_LockMutex(log_function_mutex);
- }
-
+ SDL_LockMutex(log_function_mutex);
SDL_log_function(SDL_log_userdata, category, priority, message);
-
- if (log_function_mutex) {
- SDL_UnlockMutex(log_function_mutex);
- }
+ SDL_UnlockMutex(log_function_mutex);
/* Free only if dynamically allocated */
if (message != stack_buf) {
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 959ce0d..251391a 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -301,14 +301,14 @@ static SDL_INLINE SDL_bool is_in_audio_device_thread(SDL_AudioDevice *device)
return SDL_FALSE;
}
-static void SDL_AudioLockDevice_Default(SDL_AudioDevice *device)
+static void SDL_AudioLockDevice_Default(SDL_AudioDevice *device) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang assumes recursive locks */
{
if (!is_in_audio_device_thread(device)) {
SDL_LockMutex(device->mixer_lock);
}
}
-static void SDL_AudioUnlockDevice_Default(SDL_AudioDevice *device)
+static void SDL_AudioUnlockDevice_Default(SDL_AudioDevice *device) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang assumes recursive locks */
{
if (!is_in_audio_device_thread(device)) {
SDL_UnlockMutex(device->mixer_lock);
diff --git a/src/dynapi/SDL_dynapi.h b/src/dynapi/SDL_dynapi.h
index 7543f2a..adb8bec 100644
--- a/src/dynapi/SDL_dynapi.h
+++ b/src/dynapi/SDL_dynapi.h
@@ -59,7 +59,7 @@
#define SDL_DYNAMIC_API 0
#elif defined(__riscos__) && __riscos__ /* probably not useful on RISC OS, since dlopen() can't be used when using static linking. */
#define SDL_DYNAMIC_API 0
-#elif defined(__clang_analyzer__)
+#elif defined(__clang_analyzer__) || defined(SDL_THREAD_SAFETY_ANALYSIS)
#define SDL_DYNAMIC_API 0 /* Turn off for static analysis, so reports are more clear. */
#elif defined(__VITA__)
#define SDL_DYNAMIC_API 0 /* vitasdk doesn't support dynamic linking */
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 441038e..39a7fe0 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -546,9 +546,7 @@ void SDL_StopEventLoop(void)
SDL_EventEntry *entry;
SDL_SysWMEntry *wmmsg;
- if (SDL_EventQ.lock) {
- SDL_LockMutex(SDL_EventQ.lock);
- }
+ SDL_LockMutex(SDL_EventQ.lock);
SDL_EventQ.active = SDL_FALSE;
@@ -605,8 +603,9 @@ void SDL_StopEventLoop(void)
}
SDL_zero(SDL_EventOK);
+ SDL_UnlockMutex(SDL_EventQ.lock);
+
if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
SDL_DestroyMutex(SDL_EventQ.lock);
SDL_EventQ.lock = NULL;
}
@@ -650,9 +649,7 @@ int SDL_StartEventLoop(void)
#endif
SDL_EventQ.active = SDL_TRUE;
- if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
- }
+ SDL_UnlockMutex(SDL_EventQ.lock);
return 0;
}
@@ -746,17 +743,18 @@ static int SDL_SendWakeupEvent()
if (_this == NULL || !_this->SendWakeupEvent) {
return 0;
}
- if (!_this->wakeup_lock || SDL_LockMutex(_this->wakeup_lock) == 0) {
+
+ SDL_LockMutex(_this->wakeup_lock);
+ {
if (_this->wakeup_window) {
_this->SendWakeupEvent(_this, _this->wakeup_window);
/* No more wakeup events needed until we enter a new wait */
_this->wakeup_window = NULL;
}
- if (_this->wakeup_lock) {
- SDL_UnlockMutex(_this->wakeup_lock);
- }
}
+ SDL_UnlockMutex(_this->wakeup_lock);
+
return 0;
}
@@ -768,16 +766,16 @@ static int SDL_PeepEventsInternal(SDL_Event *events, int numevents, SDL_eventact
/* Lock the event queue */
used = 0;
- if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
+
+ SDL_LockMutex(SDL_EventQ.lock);
+ {
/* Don't look after we've quit */
if (!SDL_EventQ.active) {
- if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
- }
/* We get a few spurious events at shutdown, so don't warn then */
if (action == SDL_GETEVENT) {
SDL_SetError("The event system has been shut down");
}
+ SDL_UnlockMutex(SDL_EventQ.lock);
return -1;
}
if (action == SDL_ADDEVENT) {
@@ -846,12 +844,8 @@ static int SDL_PeepEventsInternal(SDL_Event *events, int numevents, SDL_eventact
}
}
}
- if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
- }
- } else {
- return SDL_SetError("Couldn't lock event queue");
}
+ SDL_UnlockMutex(SDL_EventQ.lock);
if (used > 0 && action == SDL_ADDEVENT) {
SDL_SendWakeupEvent();
@@ -865,14 +859,12 @@ int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
return SDL_PeepEventsInternal(events, numevents, action, minType, maxType, SDL_FALSE);
}
-SDL_bool
-SDL_HasEvent(Uint32 type)
+SDL_bool SDL_HasEvent(Uint32 type)
{
return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0;
}
-SDL_bool
-SDL_HasEvents(Uint32 minType, Uint32 maxType)
+SDL_bool SDL_HasEvents(Uint32 minType, Uint32 maxType)
{
return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0;
}
@@ -899,12 +891,11 @@ void SDL_FlushEvents(Uint32 minType, Uint32 maxType)
#endif
/* Lock the event queue */
- if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
+ SDL_LockMutex(SDL_EventQ.lock);
+ {
/* Don't look after we've quit */
if (!SDL_EventQ.active) {
- if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
- }
+ SDL_UnlockMutex(SDL_EventQ.lock);
return;
}
for (entry = SDL_EventQ.head; entry; entry = next) {
@@ -914,10 +905,8 @@ void SDL_FlushEvents(Uint32 minType, Uint32 maxType)
SDL_CutEvent(entry);
}
}
- if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
- }
}
+ SDL_UnlockMutex(SDL_EventQ.lock);
}
/* Run the system dependent event loops */
@@ -1002,58 +991,59 @@ static int SDL_WaitEventTimeout_Device(_THIS, SDL_Window *wakeup_window, SDL_Eve
/* We only want a single sentinel in the queue. We could get more than one if event is NULL,
* since the SDL_PeepEvents() call below won't remove it in that case.
*/
+ int status;
SDL_bool add_sentinel = (SDL_AtomicGet(&SDL_sentinel_pending) == 0) ? SDL_TRUE : SDL_FALSE;
SDL_PumpEventsInternal(add_sentinel);
- if (!_this->wakeup_lock || SDL_LockMutex(_this->wakeup_lock) == 0) {
- int status = SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
+ SDL_LockMutex(_this->wakeup_lock);
+ {
+ status = SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
/* If status == 0 we are going to block so wakeup will be needed. */
if (status == 0) {
_this->wakeup_window = wakeup_window;
} else {
_this->wakeup_window = NULL;
}
- if (_this->wakeup_lock) {
- SDL_UnlockMutex(_this->wakeup_lock);
- }
- if (status < 0) {
- /* Got an error: return */
- break;
- }
- if (status > 0) {
- /* There is an event, we can return. */
- return 1;
- }
- /* No events found in the queue, call WaitEventTimeout to wait for an event. */
- if (timeout > 0) {
- Uint32 elapsed = SDL_GetTicks() - start;
- if (elapsed >= (Uint32)timeout) {
- /* Set wakeup_window to NULL without holding the lock. */
- _this->wakeup_window = NULL;
- return 0;
- }
- loop_timeout = (int)((Uint32)timeout - elapsed);
- }
- if (need_periodic_poll) {
- if (loop_timeout >= 0) {
- loop_timeout = SDL_min(loop_timeout, PERIODIC_POLL_INTERVAL_MS);
- } else {
- loop_timeout = PERIODIC_POLL_INTERVAL_MS;
- }
+ }
+ SDL_UnlockMutex(_this->wakeup_lock);
+
+ if (status < 0) {
+ /* Got an error: return */
+ break;
+ }
+ if (status > 0) {
+ /* There is an event, we can return. */
+ return 1;
+ }
+ /* No events found in the queue, call WaitEventTimeout to wait for an event. */
+ if (timeout > 0) {
+ Uint32 elapsed = SDL_GetTicks() - start;
+ if (elapsed >= (Uint32)timeout) {
+ /* Set wakeup_window to NULL without holding the lock. */
+ _this->wakeup_window = NULL;
+ return 0;
}
- status = _this->WaitEventTimeout(_this, loop_timeout);
- /* Set wakeup_window to NULL without holding the lock. */
- _this->wakeup_window = NULL;
- if (status == 0 && need_periodic_poll && loop_timeout == PERIODIC_POLL_INTERVAL_MS) {
- /* We may have woken up to poll. Try again */
- continue;
- } else if (status <= 0) {
- /* There is either an error or the timeout is elapsed: return */
- return status;
+ loop_timeout = (int)((Uint32)timeout - elapsed);
+ }
+ if (need_periodic_poll) {
+ if (loop_timeout >= 0) {
+ loop_timeout = SDL_min(loop_timeout, PERIODIC_POLL_INTERVAL_MS);
+ } else {
+ loop_timeout = PERIODIC_POLL_INTERVAL_MS;
}
- /* An event was found and pumped into the SDL events queue. Continue the loop
- to let SDL_PeepEvents pick it up .*/
}
+ status = _this->WaitEventTimeout(_this, loop_timeout);
+ /* Set wakeup_window to NULL without holding the lock. */
+ _this->wakeup_window = NULL;
+ if (status == 0 && need_periodic_poll && loop_timeout == PERIODIC_POLL_INTERVAL_MS) {
+ /* We may have woken up to poll. Try again */
+ continue;
+ } else if (status <= 0) {
+ /* There is either an error or the timeout is elapsed: return */
+ return status;
+ }
+ /* An event was found and pumped into the SDL events queue. Continue the loop
+ to let SDL_PeepEvents pick it up .*/
}
return 0;
}
@@ -1187,11 +1177,10 @@ int SDL_PushEvent(SDL_Event *event)
event->common.timestamp = SDL_GetTicks();
if (SDL_EventOK.callback || SDL_event_watchers_count > 0) {
- if (SDL_event_watchers_lock == NULL || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ SDL_LockMutex(SDL_event_watchers_lock);
+ {
if (SDL_EventOK.callback && !SDL_EventOK.callback(SDL_EventOK.userdata, event)) {
- if (SDL_event_watchers_lock) {
- SDL_UnlockMutex(SDL_event_watchers_lock);
- }
+ SDL_UnlockMutex(SDL_event_watchers_lock);
return 0;
}
@@ -1219,11 +1208,8 @@ int SDL_PushEvent(SDL_Event *event)
SDL_event_watchers_removed = SDL_FALSE;
}
}
-
- if (SDL_event_watchers_lock) {
- SDL_UnlockMutex(SDL_event_watchers_lock);
- }
}
+ SDL_UnlockMutex(SDL_event_watchers_lock);
}
if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
@@ -1237,32 +1223,25 @@ int SDL_PushEvent(SDL_Event *event)
void SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
{
- if (SDL_event_watchers_lock == NULL || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ SDL_LockMutex(SDL_event_watchers_lock);
+ {
/* Set filter and discard pending events */
SDL_EventOK.callback = filter;
SDL_EventOK.userdata = userdata;
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
-
- if (SDL_event_watchers_lock) {
- SDL_UnlockMutex(SDL_event_watchers_lock);
- }
}
+ SDL_UnlockMutex(SDL_event_watchers_lock);
}
-SDL_bool
-SDL_GetEventFilter(SDL_EventFilter *filter, void **userdata)
+SDL_bool SDL_GetEventFilter(SDL_EventFilter *filter, void **userdata)
{
SDL_EventWatcher event_ok;
- if (SDL_event_watchers_lock == NULL || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ SDL_LockMutex(SDL_event_watchers_lock);
+ {
event_ok = SDL_EventOK;
-
- if (SDL_event_watchers_lock) {
- SDL_UnlockMutex(SDL_event_watchers_lock);
- }
- } else {
- SDL_zero(event_ok);
}
+ SDL_UnlockMutex(SDL_event_watchers_lock);
if (filter) {
*filter = event_ok.callback;
@@ -1275,7 +1254,8 @@ SDL_GetEventFilter(SDL_EventFilter *filter, void **userdata)
void SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
{
- if (SDL_event_watchers_lock == NULL || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ SDL_LockMutex(SDL_event_watchers_lock);
+ {
SDL_EventWatcher *event_watchers;
event_watchers = SDL_realloc(SDL_event_watchers, (SDL_event_watchers_count + 1) * sizeof(*event_watchers));
@@ -1289,16 +1269,14 @@ void SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
watcher->removed = SDL_FALSE;
++SDL_event_watchers_count;
}
-
- if (SDL_event_watchers_lock) {
- SDL_UnlockMutex(SDL_event_watchers_lock);
- }
}
+ SDL_UnlockMutex(SDL_event_watchers_lock);
}
void SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
{
- if (SDL_event_watchers_lock == NULL || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ SDL_LockMutex(SDL_event_watchers_lock);
+ {
int i;
for (i = 0; i < SDL_event_watchers_count; ++i) {
@@ -1315,16 +1293,14 @@ void SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
break;
}
}
-
- if (SDL_event_watchers_lock) {
- SDL_UnlockMutex(SDL_event_watchers_lock);
- }
}
+ SDL_UnlockMutex(SDL_event_watchers_lock);
}
void SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
{
- if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
+ SDL_LockMutex(SDL_EventQ.lock);
+ {
SDL_EventEntry *entry, *next;
for (entry = SDL_EventQ.head; entry; entry = next) {
next = entry->next;
@@ -1332,10 +1308,8 @@ void SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
SDL_CutEvent(entry);
}
}
- if (SDL_EventQ.lock) {
- SDL_UnlockMutex(SDL_EventQ.lock);
- }
}
+ SDL_UnlockMutex(SDL_EventQ.lock);
}
Uint8 SDL_EventState(Uint32 type, int state)
@@ -1384,8 +1358,7 @@ Uint8 SDL_EventState(Uint32 type, int state)
return current_state;
}
-Uint32
-SDL_RegisterEvents(int numevents)
+Uint32 SDL_RegisterEvents(int numevents)
{
Uint32 event_base;
diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c
index c20962c..f103501 100644
--- a/src/haptic/SDL_haptic.c
+++ b/src/haptic/SDL_haptic.c
@@ -233,12 +233,17 @@ int SDL_JoystickIsHaptic(SDL_Joystick *joystick)
{
int ret;
- /* Must be a valid joystick */
- if (!SDL_PrivateJoystickValid(joystick)) {
- return -1;
- }
+ SDL_LockJoysticks();
+ {
+ /* Must be a valid joystick */
+ if (!SDL_PrivateJoystickValid(joystick)) {
+ SDL_UnlockJoysticks();
+ return -1;
+ }
- ret = SDL_SYS_JoystickIsHaptic(joystick);
+ ret = SDL_SYS_JoystickIsHaptic(joystick);
+ }
+ SDL_UnlockJoysticks();
if (ret > 0) {
return SDL_TRUE;
@@ -265,44 +270,53 @@ SDL_HapticOpenFromJoystick(SDL_Joystick *joystick)
return NULL;
}
- /* Must be a valid joystick */
- if (!SDL_PrivateJoystickValid(joystick)) {
- SDL_SetError("Haptic: Joystick isn't valid.");
- return NULL;
- }
+ SDL_LockJoysticks();
+ {
+ /* Must be a valid joystick */
+ if (!SDL_PrivateJoystickValid(joystick)) {
+ SDL_SetError("Haptic: Joystick isn't valid.");
+ SDL_UnlockJoysticks();
+ return NULL;
+ }
- /* Joystick must be haptic */
- if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
- SDL_SetError("Haptic: Joystick isn't a haptic device.");
- return NULL;
- }
+ /* Joystick must be haptic */
+ if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
+ SDL_SetError("Haptic: Joystick isn't a haptic device.");
+ SDL_UnlockJoysticks();
+ return NULL;
+ }
- hapticlist = SDL_haptics;
- /* Check to see if joystick's haptic is already open */
- while (hapticlist) {
- if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
- haptic = hapticlist;
- ++haptic->ref_count;
- return haptic;
+ hapticlist = SDL_haptics;
+ /* Check to see if joystick's haptic is already open */
+ while (hapticlist) {
+ if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
+ haptic = hapticlist;
+ ++haptic->ref_count;
+ SDL_UnlockJoysticks();
+ return haptic;
+ }
+ hapticlist = hapticlist->next;
}
- hapticlist = hapticlist->next;
- }
- /* Create the haptic device */
- haptic = (SDL_Haptic *)SDL_malloc((sizeof *haptic));
- if (haptic == NULL) {
- SDL_OutOfMemory();
- return NULL;
- }
+ /* Create the haptic device */
+ haptic = (SDL_Haptic *)SDL_malloc((sizeof *haptic));
+ if (haptic == NULL) {
+ SDL_OutOfMemory();
+ SDL_UnlockJoysticks();
+ return NULL;
+ }
- /* Initialize the haptic device */
- SDL_memset(haptic, 0, sizeof(SDL_Haptic));
- haptic->rumble_id = -1;
- if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
- SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
- SDL_free(haptic);
- return NULL;
+ /* Initialize the haptic device */
+ SDL_memset(haptic, 0, sizeof(SDL_Haptic));
+ haptic->rumble_id = -1;
+ if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
+ SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
+ SDL_free(haptic);
+ SDL_UnlockJoysticks();
+ return NULL;
+ }
}
+ SDL_UnlockJoysticks();
/* Add haptic to list */
++haptic->ref_count;
diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c
index 1a333ec..721cff6 100644
--- a/src/haptic/linux/SDL_syshaptic.c
+++ b/src/haptic/linux/SDL_syshaptic.c
@@ -489,6 +489,8 @@ int SDL_SYS_HapticMouse(void)
int SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
{
#ifdef SDL_JOYSTICK_LINUX
+ SDL_AssertJoysticksLocked();
+
if (joystick->driver != &SDL_LINUX_JoystickDriver) {
return SDL_FALSE;
}
@@ -505,6 +507,8 @@ int SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
int SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
{
#ifdef SDL_JOYSTICK_LINUX
+ SDL_AssertJoysticksLocked();
+
if (joystick->driver != &SDL_LINUX_JoystickDriver) {
return 0;
}
@@ -528,6 +532,8 @@ int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
int ret;
SDL_hapticlist_item *item;
+ SDL_AssertJoysticksLocked();
+
if (joystick->driver != &SDL_LINUX_JoystickDriver) {
return -1;
}
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 4e0aa17..f63368e 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -55,7 +55,7 @@
#define SDL_CONTROLLER_SDKLE_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_SDKLE_FIELD)
/* a list of currently opened game controllers */
-static SDL_GameController *SDL_gamecontrollers = NULL;
+static SDL_GameController *SDL_gamecontrollers SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
typedef struct
{
@@ -103,44 +103,53 @@ typedef enum
SDL_CONTROLLER_MAPPING_PRIORITY_USER,
} SDL_ControllerMappingPriority;
+#define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
+
typedef struct _ControllerMapping_t
{
- SDL_JoystickGUID guid;
- char *name;
- char *mapping;
- SDL_ControllerMappingPriority priority;
- struct _ControllerMapping_t *next;
+ SDL_JoystickGUID guid _guarded;
+ char *name _guarded;
+ char *mapping _guarded;
+ SDL_ControllerMappingPriority priority _guarded;
+ struct _ControllerMapping_t *next _guarded;
} ControllerMapping_t;
+#undef _guarded
+
static SDL_JoystickGUID s_zeroGUID;
-static ControllerMapping_t *s_pSupportedControllers = NULL;
-static ControllerMapping_t *s_pDefaultMapping = NULL;
-static ControllerMapping_t *s_pXInputMapping = NULL;
+static ControllerMapping_t *s_pSupportedControllers SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static ControllerMapping_t *s_pDefaultMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static ControllerMapping_t *s_pXInputMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static char gamecontroller_magic;
+#define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
+
/* The SDL game controller structure */
struct _SDL_GameController
{
- const void *magic;
+ const void *magic _guarded;
- SDL_Joystick *joystick; /* underlying joystick device */
- int ref_count;
+ SDL_Joystick *joystick _guarded; /* underlying joystick device */
+ int ref_count _guarded;
- const char *name;
- ControllerMapping_t *mapping;
- int num_bindings;
- SDL_ExtendedGameControllerBind *bindings;
- SDL_ExtendedGameControllerBind **last_match_axis;
- Uint8 *last_hat_mask;
- Uint32 guide_button_down;
+ const char *name _guarded;
+ ControllerMapping_t *mapping _guarded;
+ int num_bindings _guarded;
+ SDL_ExtendedGameControllerBind *bindings _guarded;
+ SDL_ExtendedGameControllerBind **last_match_axis _guarded;
+ Uint8 *last_hat_mask _guarded;
+ Uint32 guide_button_down _guarded;
- struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
+ struct _SDL_GameController *next _guarded; /* pointer to next game controller we have allocated */
};
+#undef _guarded
+
#define CHECK_GAMECONTROLLER_MAGIC(gamecontroller, retval) \
if (!gamecontroller || gamecontroller->magic != &gamecontroller_magic || \
!SDL_PrivateJoystickValid(gamecontroller->joystick)) { \
SDL_InvalidParamError("gamecontroller"); \
+ SDL_UnlockJoysticks(); \
return retval; \
}
@@ -241,7 +250,7 @@ static void HandleJoystickAxis(SDL_GameController *gamecontroller, int axis, int
SDL_ExtendedGameControllerBind *last_match;
SDL_ExtendedGameControllerBind *match = NULL;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
+ SDL_AssertJoysticksLocked();
last_match = gamecontroller->last_match_axis[axis];
for (i = 0; i < gamecontroller->num_bindings; ++i) {
@@ -294,7 +303,7 @@ static void HandleJoystickButton(SDL_GameController *gamecontroller, int button,
{
int i;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
+ SDL_AssertJoysticksLocked();
for (i = 0; i < gamecontroller->num_bindings; ++i) {
SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
@@ -316,7 +325,7 @@ static void HandleJoystickHat(SDL_GameController *gamecontroller, int hat, Uint8
int i;
Uint8 last_mask, changed_mask;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
+ SDL_AssertJoysticksLocked();
last_mask = gamecontroller->last_hat_mask[hat];
changed_mask = (last_mask ^ value);
@@ -350,8 +359,6 @@ static void RecenterGameController(SDL_GameController *gamecontroller)
SDL_GameControllerButton button;
SDL_GameControllerAxis axis;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
-
for (button = (SDL_GameControllerButton)0; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
if (SDL_GameControllerGetButton(gamecontroller, button)) {
SDL_PrivateGameControllerButton(gamecontroller, button, SDL_RELEASED);
@@ -370,39 +377,41 @@ static void RecenterGameController(SDL_GameController *gamecontroller)
*/
static int SDLCALL SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
{
+ SDL_GameController *controller;
+
switch (event->type) {
case SDL_JOYAXISMOTION:
{
- SDL_GameController *controllerlist = SDL_gamecontrollers;
- while (controllerlist) {
- if (controllerlist->joystick->instance_id == event->jaxis.which) {
- HandleJoystickAxis(controllerlist, event->jaxis.axis, event->jaxis.value);
+ SDL_AssertJoysticksLocked();
+
+ for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
+ if (controller->joystick->instance_id == event->jaxis.which) {
+ HandleJoystickAxis(controller, event->jaxis.axis, event->jaxis.value);
break;
}
- controllerlist = controllerlist->next;
}
} break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
{
- SDL_GameController *controllerlist = SDL_gamecontrollers;
- while (controllerlist) {
- if (controllerlist->joystick->instance_id == event->jbutton.which) {
- HandleJoystickButton(controllerlist, event->jbutton.button, event->jbutton.state);
+ SDL_AssertJoysticksLocked();
+
+ for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
+ if (controller->joystick->instance_id == event->jbutton.which) {
+ HandleJoystickButton(controller, event->jbutton.button, event->jbutton.state);
break;
}
- controllerlist = controllerlist->next;
}
} break;
case SDL_JOYHATMOTION:
{
- SDL_GameController *controllerlist = SDL_gamecontrollers;
- while (controllerlist) {
- if (controllerlist->joystick->instance_id == event->jhat.which) {
- HandleJoystickHat(controllerlist, event->jhat.hat, event->jhat.value);
+ SDL_AssertJoysticksLocked();
+
+ for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
+ if (controller->joystick->instance_id == event->jhat.which) {
+ HandleJoystickHat(controller, event->jhat.hat, event->jhat.value);
break;
}
- controllerlist = controllerlist->next;
}
} break;
case SDL_JOYDEVICEADDED:
@@ -416,13 +425,13 @@ static int SDLCALL SDL_GameControllerEventWatcher(void *userdata, SDL_Event *eve
} break;
case SDL_JOYDEVICEREMOVED:
{
- SDL_GameController *controllerlist = SDL_gamecontrollers;
- while (controllerlist) {
- if (controllerlist->joystick->instance_id == event->jdevice.which) {
- RecenterGameController(controllerlist);
+ SDL_AssertJoysticksLocked();
+
+ for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
+ if (controller->joystick->instance_id == event->jdevice.which) {
+ RecenterGameController(controller);
break;
}
- controllerlist = controllerlist->next;
}
/* We don't know if this was a game controller, so go ahead and send an event */
@@ -717,6 +726,8 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic
ControllerMapping_t *mapping;
Uint16 crc = 0;
+ SDL_AssertJoysticksLocked();
+
if (match_crc) {
SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
}
@@ -829,8 +840,7 @@ static const char *map_StringForControllerAxis[] = {
/*
* convert a string to its enum equivalent
*/
-SDL_GameControllerAxis
-SDL_GameControllerGetAxisFromString(const char *str)
+SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *str)
{
int entry;
@@ -889,8 +899,7 @@ static const char *map_StringForControllerButton[] = {
/*
* convert a string to its enum equivalent
*/
-SDL_GameControllerButton
-SDL_GameControllerGetButtonFromString(const char *str)
+SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *str)
{
int entry;
if (str == NULL || str[0] == '\0') {
@@ -928,7 +937,7 @@ static void SDL_PrivateGameControllerParseElement(SDL_GameController *gamecontro
char half_axis_input = 0;
char half_axis_output = 0;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
+ SDL_AssertJoysticksLocked();
if (*szGameButton == '+' || *szGameButton == '-') {
half_axis_output = *szGameButton++;
@@ -1023,8 +1032,6 @@ static void SDL_PrivateGameControllerParseControllerConfigString(SDL_GameControl
int i = 0;
const char *pchPos = pchString;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
-
SDL_zeroa(szGameButton);
SDL_zeroa(szJoystickButton);
@@ -1072,7 +1079,7 @@ static void SDL_PrivateLoadButtonMapping(SDL_GameController *gamecontroller, Con
{
int i;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, );
+ SDL_AssertJoysticksLocked();
gamecontroller->name = pControllerMapping->name;
gamecontroller->num_bindings = 0;
@@ -1189,21 +1196,23 @@ static char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMappi
*/
static void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
{
- SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
- while (gamecontrollerlist) {
- if (gamecontrollerlist->mapping == pControllerMapping) {
- /* Not really threadsafe. Should this lock access within SDL_GameControllerEventWatcher? */
- SDL_PrivateLoadButtonMapping(gamecontrollerlist, pControllerMapping);
+ SDL_GameController *controller;
+
+ SDL_AssertJoysticksLocked();
+
+ for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
+ if (controller->mapping == pControllerMapping) {
+ SDL_PrivateLoadButtonMapping(controller, pControllerMapping);
{
SDL_Event event;
event.type = SDL_CONTROLLERDEVICEREMAPPED;
- event.cdevice.which = gamecontrollerlist->joystick->instance_id;
+ event.cdevice.which = controller->joystick->instance_id;
SDL_PushEvent(&event);
}
}
- gamecontrollerlist = gamecontrollerlist->next;
+ controller = controller->next;
}
}
@@ -1217,6 +1226,8 @@ static ControllerMapping_t *SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID,
ControllerMapping_t *pControllerMapping;
Uint16 crc;
+ SDL_AssertJoysticksLocked();
+
pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
if (pchName == NULL) {
SDL_SetError("Couldn't parse name from %s", mappingString);
@@ -1322,6 +1333,8 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const
{
ControllerMapping_t *mapping;
+ SDL_AssertJoysticksLocked();
+
mapping = SDL_PrivateGetControllerMappingForGUID(guid);
#ifdef __LINUX__
if (mapping == NULL && name) {
@@ -1428,11 +1441,10 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
SDL_JoystickGUID guid;
ControllerMapping_t *mapping;
- SDL_LockJoysticks();
+ SDL_AssertJoysticksLocked();
if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
- SDL_UnlockJoysticks();
return NULL;
}
@@ -1448,7 +1460,6 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
}
}
- SDL_UnlockJoysticks();
return mapping;
}
@@ -1534,6 +1545,8 @@ static int SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_Co
SDL_bool existing = SDL_FALSE;
ControllerMapping_t *pControllerMapping;
+ SDL_AssertJoysticksLocked();
+
if (mappingString == NULL) {
return SDL_InvalidParamError("mappingString");
}
@@ -1634,7 +1647,15 @@ static int SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_Co
*/
int SDL_GameControllerAddMapping(const char *mappingString)
{
- return SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ retval = SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -1643,14 +1664,20 @@ int SDL_GameControllerAddMapping(const char *mappingString)
int SDL_GameControllerNumMappings(void)
{
int num_mappings = 0;
- ControllerMapping_t *mapping;
- for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
- if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
- continue;
+ SDL_LockJoysticks();
+ {
+ ControllerMapping_t *mapping;
+
+ for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
+ if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
+ continue;
+ }
+ ++num_mappings;
}
- ++num_mappings;
}
+ SDL_UnlockJoysticks();
+
return num_mappings;
}
@@ -1664,6 +1691,8 @@ static char *CreateMappingString(ControllerMapping_t *mapping, SDL_JoystickGUID
size_t needed;
const char *platform = SDL_GetPlatform();
+ SDL_AssertJoysticksLocked();
+
SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
/* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
@@ -1707,48 +1736,71 @@ static char *CreateMappingString(ControllerMapping_t *mapping, SDL_JoystickGUID
/*
* Get the mapping at a particular index.
*/
-char *
-SDL_GameControllerMappingForIndex(int mapping_index)
+char *SDL_GameControllerMappingForIndex(int mapping_index)
{
- ControllerMapping_t *mapping;
+ char *retval = NULL;
- for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
- if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
- continue;
- }
- if (mapping_index == 0) {
- return CreateMappingString(mapping, mapping->guid);
+ SDL_LockJoysticks();
+ {
+ ControllerMapping_t *mapping;
+
+ for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
+ if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
+ continue;
+ }
+ if (mapping_index == 0) {
+ retval = CreateMappingString(mapping, mapping->guid);
+ break;
+ }
+ --mapping_index;
}
- --mapping_index;
}
- SDL_SetError("Mapping not available");
- return NULL;
+ SDL_UnlockJoysticks();
+
+ if (retval == NULL) {
+ SDL_SetError("Mapping not available");
+ }
+ return retval;
}
/*
* Get the mapping string for this GUID
*/
-char *
-SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
+char *SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
{
- ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid);
- if (mapping) {
- return CreateMappingString(mapping, guid);
- } else {
- SDL_SetError("Mapping not available");
+ char *retval;
+
+ SDL_LockJoysticks();
+ {
+ ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid);
+ if (mapping) {
+ retval = CreateMappingString(mapping, guid);
+ } else {
+ SDL_SetError("Mapping not available");
+ retval = NULL;
+ }
}
- return NULL;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
* Get the mapping string for this device
*/
-char *
-SDL_GameControllerMapping(SDL_GameController *gamecontroller)
+char *SDL_GameControllerMapping(SDL_GameController *gamecontroller)
{
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+ char *retval;
- return CreateMappingString(gamecontroller->mapping, gamecontroller->joystick->guid);
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+
+ retval = CreateMappingString(gamecontroller->mapping, gamecontroller->joystick->guid);
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
static void SDL_GameControllerLoadHints()
@@ -1807,6 +1859,9 @@ int SDL_GameControllerInitMappings(void)
char szControllerMapPath[1024];
int i = 0;
const char *pMappingString = NULL;
+
+ SDL_AssertJoysticksLocked();
+
pMappingString = s_ControllerMappings[i];
while (pMappingString) {
SDL_PrivateGameControllerAddMapping(pMappingString, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
@@ -1853,38 +1908,49 @@ int SDL_GameControllerInit(void)
/*
* Get the implementation dependent name of a controller
*/
-const char *
-SDL_GameControllerNameForIndex(int joystick_index)
+const char *SDL_GameControllerNameForIndex(int joystick_index)
{
- ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(joystick_index);
- if (pSupportedController != NULL) {
- if (SDL_strcmp(pSupportedController->name, "*") == 0) {
- return SDL_JoystickNameForIndex(joystick_index);
- } else {
- return pSupportedController->name;
+ const char *retval = NULL;
+
+ SDL_LockJoysticks();
+ {
+ ControllerMapping_t *mapping = SDL_PrivateGetControllerMapping(joystick_index);
+ if (mapping != NULL) {
+ if (SDL_strcmp(mapping->name, "*") == 0) {
+ retval = SDL_JoystickNameForIndex(joystick_index);
+ } else {
+ retval = mapping->name;
+ }
}
}
- return NULL;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
* Get the implementation dependent path of a controller
*/
-const char *
-SDL_GameControllerPathForIndex(int joystick_index)
+const char *SDL_GameControllerPathForIndex(int joystick_index)
{
- ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(joystick_index);
- if (pSupportedController != NULL) {
- return SDL_JoystickPathForIndex(joystick_index);
+ const char *retval = NULL;
+
+ SDL_LockJoysticks();
+ {
+ ControllerMapping_t *mapping = SDL_PrivateGetControllerMapping(joystick_index);
+ if (mapping != NULL) {
+ retval = SDL_JoystickPathForIndex(joystick_index);
+ }
}
- return NULL;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
* Get the type of a game controller.
*/
-SDL_GameControllerType
-SDL_GameControllerTypeForIndex(int joystick_index)
+SDL_GameControllerType SDL_GameControllerTypeForIndex(int joystick_index)
{
return SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGetDeviceGUID(joystick_index), SDL_JoystickNameForIndex(joystick_index));
}
@@ -1894,58 +1960,71 @@ SDL_GameControllerTypeForIndex(int joystick_index)
* This can be called before any controllers are opened.
* If no mapping can be found, this function returns NULL.
*/
-char *
-SDL_GameControllerMappingForDeviceIndex(int joystick_index)
+char *SDL_GameControllerMappingForDeviceIndex(int joystick_index)
{
- char *pMappingString = NULL;
- ControllerMapping_t *mapping;
+ char *retval = NULL;
SDL_LockJoysticks();
- mapping = SDL_PrivateGetControllerMapping(joystick_index);
- if (mapping) {
- SDL_JoystickGUID guid;
- char pchGUID[33];
- size_t needed;
- guid = SDL_JoystickGetDeviceGUID(joystick_index);
- SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
- /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
- needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
- pMappingString = SDL_malloc(needed);
- if (pMappingString == NULL) {
- SDL_OutOfMemory();
- SDL_UnlockJoysticks();
- return NULL;
+ {
+ ControllerMapping_t *mapping = SDL_PrivateGetControllerMapping(joystick_index);
+ if (mapping != NULL) {
+ SDL_JoystickGUID guid;
+ char pchGUID[33];
+ size_t needed;
+ guid = SDL_JoystickGetDeviceGUID(joystick_index);
+ SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
+ /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
+ needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
+ retval = (char *)SDL_malloc(needed);
+ if (retval != NULL) {
+ (void)SDL_snprintf(retval, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
+ } else {
+ SDL_OutOfMemory();
+ }
}
- (void)SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
}
SDL_UnlockJoysticks();
- return pMappingString;
+ return retval;
}
/*
* Return 1 if the joystick with this name and GUID is a supported controller
*/
-SDL_bool
-SDL_IsGameControllerNameAndGUID(const char *name, SDL_JoystickGUID guid)
+SDL_bool SDL_IsGameControllerNameAndGUID(const char *name, SDL_JoystickGUID guid)
{
- ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
- if (pSupportedController) {
- return SDL_TRUE;
+ SDL_bool retval;
+
+ SDL_LockJoysticks();
+ {
+ if (SDL_PrivateGetControllerMappingForNameAndGUID(name, guid) != NULL) {
+ retval = SDL_TRUE;
+ } else {
+ retval = SDL_FALSE;
+ }
}
- return SDL_FALSE;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
* Return 1 if the joystick at this device index is a supported controller
*/
-SDL_bool
-SDL_IsGameController(int joystick_index)
+SDL_bool SDL_IsGameController(int joystick_index)
{
- ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(joystick_index);
- if (pSupportedController != NULL) {
- return SDL_TRUE;
+ SDL_bool retval;
+
+ SDL_LockJoysticks();
+ {
+ if (SDL_PrivateGetControllerMapping(joystick_index) != NULL) {
+ retval = SDL_TRUE;
+ } else {
+ retval = SDL_FALSE;
+ }
}
- return SDL_FALSE;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
#if defined(__LINUX__)
@@ -2047,8 +2126,7 @@ SDL_bool SDL_ShouldIgnoreGameController(const char *name, SDL_JoystickGUID guid)
*
* This function returns a controller identifier, or NULL if an error occurred.
*/
-SDL_GameController *
-SDL_GameControllerOpen(int joystick_index)
+SDL_GameController *SDL_GameControllerOpen(int joystick_index)
{
SDL_JoystickID instance_id;
SDL_GameController *gamecontroller;
@@ -2141,86 +2219,101 @@ void SDL_GameControllerUpdate(void)
/**
* Return whether a game controller has a given axis
*/
-SDL_bool
-SDL_GameControllerHasAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
+SDL_bool SDL_GameControllerHasAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
{
SDL_GameControllerButtonBind bind;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
+
+ bind = SDL_GameControllerGetBindForAxis(gamecontroller, axis);
+ }
+ SDL_UnlockJoysticks();
- bind = SDL_GameControllerGetBindForAxis(gamecontroller, axis);
return (bind.bindType != SDL_CONTROLLER_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
}
/*
* Get the current state of an axis control on a controller
*/
-Sint16
-SDL_GameControllerGetAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
+Sint16 SDL_GameControllerGetAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
{
- int i;
+ Sint16 retval = 0;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
+ SDL_LockJoysticks();
+ {
+ int i;
- for (i = 0; i < gamecontroller->num_bindings; ++i) {
- SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
- if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
- int value = 0;
- SDL_bool valid_input_range;
- SDL_bool valid_output_range;
-
- if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
- value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
- if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
- valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
- } else {
- valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
- }
- if (valid_input_range) {
- if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
- float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
- value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
+
+ for (i = 0; i < gamecontroller->num_bindings; ++i) {
+ SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
+ if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
+ int value = 0;
+ SDL_bool valid_input_range;
+ SDL_bool valid_output_range;
+
+ if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
+ value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
+ if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
+ valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
+ } else {
+ valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
+ }
+ if (valid_input_range) {
+ if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
+ float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
+ value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
+ }
+ } else {
+ value = 0;
+ }
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
+ value = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
+ if (value == SDL_PRESSED) {
+ value = binding->output.axis.axis_max;
+ }
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
+ int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
+ if (hat_mask & binding->input.hat.hat_mask) {
+ value = binding->output.axis.axis_max;
}
- } else {
- value = 0;
}
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
- value = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
- if (value == SDL_PRESSED) {
- value = binding->output.axis.axis_max;
+
+ if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
+ valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
+ } else {
+ valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
}
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
- int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
- if (hat_mask & binding->input.hat.hat_mask) {
- value = binding->output.axis.axis_max;
+ /* If the value is zero, there might be another binding that makes it non-zero */
+ if (value != 0 && valid_output_range) {
+ retval = (Sint16)value;
+ break;
}
}
-
- if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
- valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
- } else {
- valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
- }
- /* If the value is zero, there might be another binding that makes it non-zero */
- if (value != 0 && valid_output_range) {
- return (Sint16)value;
- }
}
}
- return 0;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
* Return whether a game controller has a given button
*/
-SDL_bool
-SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
+SDL_bool SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
SDL_GameControllerButtonBind bind;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
+
+ bind = SDL_GameControllerGetBindForButton(gamecontroller, button);
+ }
+ SDL_UnlockJoysticks();
- bind = SDL_GameControllerGetBindForButton(gamecontroller, button);
return (bind.bindType != SDL_CONTROLLER_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
}
@@ -2229,38 +2322,49 @@ SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControll
*/
Uint8 SDL_GameControllerGetButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
- int i;
-
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
+ Uint8 retval = SDL_RELEASED;
- for (i = 0; i < gamecontroller->num_bindings; ++i) {
- SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
- if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
- if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
- SDL_bool valid_input_range;
-
- int value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
- int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
- if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
- valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
- if (valid_input_range) {
- return (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
- }
- } else {
- valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
- if (valid_input_range) {
- return (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
+ SDL_LockJoysticks();
+ {
+ int i;
+
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
+
+ for (i = 0; i < gamecontroller->num_bindings; ++i) {
+ SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
+ if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
+ if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
+ SDL_bool valid_input_range;
+
+ int value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
+ int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
+ if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
+ valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
+ if (valid_input_range) {
+ retval = (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
+ break;
+ }
+ } else {
+ valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
+ if (valid_input_range) {
+ retval = (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
+ break;
+ }
}
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
+ retval = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
+ break;
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
+ int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
+ retval = (hat_mask & binding->input.hat.hat_mask) ? SDL_PRESSED : SDL_RELEASED;
+ break;
}
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
- return SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
- int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
- return (hat_mask & binding->input.hat.hat_mask) ? SDL_PRESSED : SDL_RELEASED;
}
}
}
- return SDL_RELEASED;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
@@ -2268,12 +2372,18 @@ Uint8 SDL_GameControllerGetButton(SDL_GameController *gamecontroller, SDL_GameCo
*/
int SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ int retval = 0;
- if (joystick) {
- return joystick->ntouchpads;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ retval = joystick->ntouchpads;
+ }
}
- return 0;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
@@ -2281,12 +2391,22 @@ int SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller)
*/
int SDL_GameControllerGetNumTouchpadFingers(SDL_GameController *gamecontroller, int touchpad)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ int retval = 0;
- if (joystick && touchpad >= 0 && touchpad < joystick->ntouchpads) {
- return joystick->touchpads[touchpad].nfingers;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
+ retval = joystick->touchpads[touchpad].nfingers;
+ } else {
+ retval = SDL_InvalidParamError("touchpad");
+ }
+ }
}
- return 0;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
@@ -2294,55 +2414,66 @@ int SDL_GameControllerGetNumTouchpadFingers(SDL_GameController *gamecontroller,
*/
int SDL_GameControllerGetTouchpadFinger(SDL_GameController *gamecontroller, int touchpad, int finger, Uint8 *state, float *x, float *y, float *pressure)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ int retval = -1;
- if (joystick) {
- if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
- SDL_JoystickTouchpadInfo *touchpad_info = &joystick->touchpads[touchpad];
- if (finger >= 0 && finger < touchpad_info->nfingers) {
- SDL_JoystickTouchpadFingerInfo *info = &touchpad_info->fingers[finger];
-
- if (state) {
- *state = info->state;
- }
- if (x) {
- *x = info->x;
- }
- if (y) {
- *y = info->y;
- }
- if (pressure) {
- *pressure = info->pressure;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
+ SDL_JoystickTouchpadInfo *touchpad_info = &joystick->touchpads[touchpad];
+ if (finger >= 0 && finger < touchpad_info->nfingers) {
+ SDL_JoystickTouchpadFingerInfo *info = &touchpad_info->fingers[finger];
+
+ if (state) {
+ *state = info->state;
+ }
+ if (x) {
+ *x = info->x;
+ }
+ if (y) {
+ *y = info->y;
+ }
+ if (pressure) {
+ *pressure = info->pressure;
+ }
+ retval = 0;
+ } else {
+ retval = SDL_InvalidParamError("finger");
}
- return 0;
} else {
- return SDL_InvalidParamError("finger");
+ retval = SDL_InvalidParamError("touchpad");
}
- } else {
- return SDL_InvalidParamError("touchpad");
}
- } else {
- return SDL_InvalidParamError("gamecontroller");
}
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
* Return whether a game controller has a particular sensor.
*/
-SDL_bool
-SDL_GameControllerHasSensor(SDL_GameController *gamecontroller, SDL_SensorType type)
+SDL_bool SDL_GameControllerHasSensor(SDL_GameController *gamecontroller, SDL_SensorType type)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
- int i;
+ SDL_bool retval = SDL_FALSE;
- if (joystick) {
- for (i = 0; i < joystick->nsensors; ++i) {
- if (joystick->sensors[i].type == type) {
- return SDL_TRUE;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ int i;
+ for (i = 0; i < joystick->nsensors; ++i) {
+ if (joystick->sensors[i].type == type) {
+ retval = SDL_TRUE;
+ break;
+ }
}
}
}
- return SDL_FALSE;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -2350,41 +2481,47 @@ SDL_GameControllerHasSensor(SDL_GameController *gamecontroller, SDL_SensorType t
*/
int SDL_GameControllerSetSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type, SDL_bool enabled)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
- int i;
-
- if (joystick == NULL) {
- return -1;
- }
-
- for (i = 0; i < joystick->nsensors; ++i) {
- SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
-
- if (sensor->type == type) {
- if (sensor->enabled == enabled) {
- return 0;
- }
-
- if (enabled) {
- if (joystick->nsensors_enabled == 0) {
- if (joystick->driver->SetSensorsEnabled(joystick, SDL_TRUE) < 0) {
- return -1;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ int i;
+ for (i = 0; i < joystick->nsensors; ++i) {
+ SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
+
+ if (sensor->type == type) {
+ if (sensor->enabled == enabled) {
+ SDL_UnlockJoysticks();
+ return 0;
}
- }
- ++joystick->nsensors_enabled;
- } else {
- if (joystick->nsensors_enabled == 1) {
- if (joystick->driver->SetSensorsEnabled(joystick, SDL_FALSE) < 0) {
- return -1;
+
+ if (enabled) {
+ if (joystick->nsensors_enabled == 0) {
+ if (joystick->driver->SetSensorsEnabled(joystick, SDL_TRUE) < 0) {
+ SDL_UnlockJoysticks();
+ return -1;
+ }
+ }
+ ++joystick->nsensors_enabled;
+ } else {
+ if (joystick->nsensors_enabled == 1) {
+ if (joystick->driver->SetSensorsEnabled(joystick, SDL_FALSE) < 0) {
+ SDL_UnlockJoysticks();
+ return -1;
+ }
+ }
+ --joystick->nsensors_enabled;
}
+
+ sensor->enabled = enabled;
+ SDL_UnlockJoysticks();
+ return 0;
}
- --joystick->nsensors_enabled;
}
-
- sensor->enabled = enabled;
- return 0;
}
}
+ SDL_UnlockJoysticks();
+
return SDL_Unsupported();
}
@@ -2393,17 +2530,24 @@ int SDL_GameControllerSetSensorEnabled(SDL_GameController *gamecontroller, SDL_S
*/
SDL_bool SDL_GameControllerIsSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
- int i;
+ SDL_bool retval = SDL_FALSE;
- if (joystick) {
- for (i = 0; i < joystick->nsensors; ++i) {
- if (joystick->sensors[i].type == type) {
- return joystick->sensors[i].enabled;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ int i;
+ for (i = 0; i < joystick->nsensors; ++i) {
+ if (joystick->sensors[i].type == type) {
+ retval = joystick->sensors[i].enabled;
+ break;
+ }
}
}
}
- return SDL_FALSE;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -2411,21 +2555,26 @@ SDL_bool SDL_GameControllerIsSensorEnabled(SDL_GameController *gamecontroller, S
*/
float SDL_GameControllerGetSensorDataRate(SDL_GameController *gamecontroller, SDL_SensorType type)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
- int i;
+ float retval = 0.0f;
- if (joystick == NULL) {
- return 0.0f;
- }
-
- for (i = 0; i < joystick->nsensors; ++i) {
- SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
-
- if (sensor->type == type) {
- return sensor->rate;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ int i;
+ for (i = 0; i < joystick->nsensors; ++i) {
+ SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
+
+ if (sensor->type == type) {
+ retval = sensor->rate;
+ break;
+ }
+ }
}
}
- return 0.0f;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -2441,42 +2590,51 @@ int SDL_GameControllerGetSensorData(SDL_GameController *gamecontroller, SDL_Sens
*/
int SDL_GameControllerGetSensorDataWithTimestamp(SDL_GameController *gamecontroller, SDL_SensorType type, Uint64 *timestamp, float *data, int num_values)
{
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
- int i;
-
- if (joystick == NULL) {
- return -1;
- }
-
- for (i = 0; i < joystick->nsensors; ++i) {
- SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
-
- if (sensor->type == type) {
- num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
- SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
- if (timestamp) {
- *timestamp = sensor->timestamp_us;
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
+ if (joystick) {
+ int i;
+ for (i = 0; i < joystick->nsensors; ++i) {
+ SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
+
+ if (sensor->type == type) {
+ num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
+ SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
+ if (timestamp) {
+ *timestamp = sensor->timestamp_us;
+ }
+ SDL_UnlockJoysticks();
+ return 0;
+ }
}
- return 0;
}
}
+ SDL_UnlockJoysticks();
+
return SDL_Unsupported();
}
-const char *
-SDL_GameControllerName(SDL_GameController *gamecontroller)
+const char *SDL_GameControllerName(SDL_GameController *gamecontroller)
{
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+ const char *retval = NULL;
- if (SDL_strcmp(gamecontroller->name, "*") == 0) {
- return SDL_JoystickName(SDL_GameControllerGetJoystick(gamecontroller));
- } else {
- return gamecontroller->name;
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+
+ if (SDL_strcmp(gamecontroller->name, "*") == 0) {
+ retval = SDL_JoystickName(gamecontroller->joystick);
+ } else {
+ retval = gamecontroller->name;
+ }
}
+ SDL_UnlockJoysticks();
+
+ return retval;
}
-const char *
-SDL_GameControllerPath(SDL_GameController *gamecontroller)
+const char *SDL_GameControllerPath(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2486,8 +2644,7 @@ SDL_GameControllerPath(SDL_GameController *gamecontroller)
return SDL_JoystickPath(joystick);
}
-SDL_GameControllerType
-SDL_GameControllerGetType(SDL_GameController *gamecontroller)
+SDL_GameControllerType SDL_GameControllerGetType(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2520,8 +2677,7 @@ void SDL_GameControllerSetPlayerIndex(SDL_GameController *gamecontroller, int pl
SDL_JoystickSetPlayerIndex(joystick, player_index);
}
-Uint16
-SDL_GameControllerGetVendor(SDL_GameController *gamecontroller)
+Uint16 SDL_GameControllerGetVendor(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2531,8 +2687,7 @@ SDL_GameControllerGetVendor(SDL_GameController *gamecontroller)
return SDL_JoystickGetVendor(joystick);
}
-Uint16
-SDL_GameControllerGetProduct(SDL_GameController *gamecontroller)
+Uint16 SDL_GameControllerGetProduct(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2542,8 +2697,7 @@ SDL_GameControllerGetProduct(SDL_GameController *gamecontroller)
return SDL_JoystickGetProduct(joystick);
}
-Uint16
-SDL_GameControllerGetProductVersion(SDL_GameController *gamecontroller)
+Uint16 SDL_GameControllerGetProductVersion(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2553,8 +2707,7 @@ SDL_GameControllerGetProductVersion(SDL_GameController *gamecontroller)
return SDL_JoystickGetProductVersion(joystick);
}
-Uint16
-SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontroller)
+Uint16 SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2564,8 +2717,7 @@ SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontroller)
return SDL_JoystickGetFirmwareVersion(joystick);
}
-const char *
-SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
+const char * SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2579,30 +2731,38 @@ SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
* Return if the controller in question is currently attached to the system,
* \return 0 if not plugged in, 1 if still present.
*/
-SDL_bool
-SDL_GameControllerGetAttached(SDL_GameController *gamecontroller)
+SDL_bool SDL_GameControllerGetAttached(SDL_GameController *gamecontroller)
{
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
+ SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
- return SDL_JoystickGetAttached(gamecontroller->joystick);
+ if (joystick == NULL) {
+ return SDL_FALSE;
+ }
+ return SDL_JoystickGetAttached(joystick);
}
/*
* Get the joystick for this controller
*/
-SDL_Joystick *
-SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller)
+SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller)
{
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+ SDL_Joystick *joystick;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
- return gamecontroller->joystick;
+ joystick = gamecontroller->joystick;
+ }
+ SDL_UnlockJoysticks();
+
+ return joystick;
}
/*
* Return the SDL_GameController associated with an instance id.
*/
-SDL_GameController *
-SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
+SDL_GameController *SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
{
SDL_GameController *gamecontroller;
@@ -2624,11 +2784,18 @@ SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
*/
SDL_GameController *SDL_GameControllerFromPlayerIndex(int player_index)
{
- SDL_Joystick *joystick = SDL_JoystickFromPlayerIndex(player_index);
- if (joystick) {
- return SDL_GameControllerFromInstanceID(joystick->instance_id);
+ SDL_GameController *retval = NULL;
+
+ SDL_LockJoysticks();
+ {
+ SDL_Joystick *joystick = SDL_JoystickFromPlayerIndex(player_index);
+ if (joystick) {
+ retval = SDL_GameControllerFromInstanceID(joystick->instance_id);
+ }
}
- return NULL;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -2636,32 +2803,36 @@ SDL_GameController *SDL_GameControllerFromPlayerIndex(int player_index)
*/
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
{
- int i;
SDL_GameControllerButtonBind bind;
- SDL_zero(bind);
-
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, bind);
- if (axis == SDL_CONTROLLER_AXIS_INVALID) {
- return bind;
- }
+ SDL_zero(bind);
- for (i = 0; i < gamecontroller->num_bindings; ++i) {
- SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
- if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
- bind.bindType = binding->inputType;
- if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
- /* FIXME: There might be multiple axes bound now that we have axis ranges... */
- bind.value.axis = binding->input.axis.axis;
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
- bind.value.button = binding->input.button;
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
- bind.value.hat.hat = binding->input.hat.hat;
- bind.value.hat.hat_mask = binding->input.hat.hat_mask;
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, bind);
+
+ if (axis != SDL_CONTROLLER_AXIS_INVALID) {
+ int i;
+ for (i = 0; i < gamecontroller->num_bindings; ++i) {
+ SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
+ if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
+ bind.bindType = binding->inputType;
+ if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
+ /* FIXME: There might be multiple axes bound now that we have axis ranges... */
+ bind.value.axis = binding->input.axis.axis;
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
+ bind.value.button = binding->input.button;
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
+ bind.value.hat.hat = binding->input.hat.hat;
+ bind.value.hat.hat_mask = binding->input.hat.hat_mask;
+ }
+ break;
+ }
}
- break;
}
}
+ SDL_UnlockJoysticks();
+
return bind;
}
@@ -2670,31 +2841,35 @@ SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController
*/
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
- int i;
SDL_GameControllerButtonBind bind;
- SDL_zero(bind);
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, bind);
-
- if (button == SDL_CONTROLLER_BUTTON_INVALID) {
- return bind;
- }
+ SDL_zero(bind);
- for (i = 0; i < gamecontroller->num_bindings; ++i) {
- SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
- if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
- bind.bindType = binding->inputType;
- if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
- bind.value.axis = binding->input.axis.axis;
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
- bind.value.button = binding->input.button;
- } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
- bind.value.hat.hat = binding->input.hat.hat;
- bind.value.hat.hat_mask = binding->input.hat.hat_mask;
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, bind);
+
+ if (button != SDL_CONTROLLER_BUTTON_INVALID) {
+ int i;
+ for (i = 0; i < gamecontroller->num_bindings; ++i) {
+ SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
+ if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
+ bind.bindType = binding->inputType;
+ if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
+ bind.value.axis = binding->input.axis.axis;
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
+ bind.value.button = binding->input.button;
+ } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
+ bind.value.hat.hat = binding->input.hat.hat;
+ bind.value.hat.hat_mask = binding->input.hat.hat_mask;
+ }
+ break;
+ }
}
- break;
}
}
+ SDL_UnlockJoysticks();
+
return bind;
}
@@ -2718,8 +2893,7 @@ int SDL_GameControllerRumbleTriggers(SDL_GameController *gamecontroller, Uint16
return SDL_JoystickRumbleTriggers(joystick, left_rumble, right_rumble, duration_ms);
}
-SDL_bool
-SDL_GameControllerHasLED(SDL_GameController *gamecontroller)
+SDL_bool SDL_GameControllerHasLED(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2729,8 +2903,7 @@ SDL_GameControllerHasLED(SDL_GameController *gamecontroller)
return SDL_JoystickHasLED(joystick);
}
-SDL_bool
-SDL_GameControllerHasRumble(SDL_GameController *gamecontroller)
+SDL_bool SDL_GameControllerHasRumble(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2740,8 +2913,7 @@ SDL_GameControllerHasRumble(SDL_GameController *gamecontroller)
return SDL_JoystickHasRumble(joystick);
}
-SDL_bool
-SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller)
+SDL_bool SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller)
{
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
@@ -2775,12 +2947,13 @@ void SDL_GameControllerClose(SDL_GameController *gamecontroller)
{
SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
+ SDL_LockJoysticks();
+
if (gamecontroller == NULL || gamecontroller->magic != &gamecontroller_magic) {
+ SDL_UnlockJoysticks();
return;
}
- SDL_LockJoysticks();
-
/* First decrement ref count */
if (--gamecontroller->ref_count > 0) {
SDL_UnlockJoysticks();
@@ -2831,6 +3004,8 @@ void SDL_GameControllerQuitMappings(void)
{
ControllerMapping_t *pControllerMap;
+ SDL_AssertJoysticksLocked();
+
while (s_pSupportedControllers) {
pControllerMap = s_pSupportedControllers;
s_pSupportedControllers = s_pSupportedControllers->next;
@@ -2863,7 +3038,7 @@ static int SDL_PrivateGameControllerAxis(SDL_GameController *gamecontroller, SDL
{
int posted;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
+ SDL_AssertJoysticksLocked();
/* translate the event, if desired */
posted = 0;
@@ -2889,7 +3064,7 @@ static int SDL_PrivateGameControllerButton(SDL_GameController *gamecontroller, S
#if !SDL_EVENTS_DISABLED
SDL_Event event;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
+ SDL_AssertJoysticksLocked();
if (button == SDL_CONTROLLER_BUTTON_INVALID) {
return 0;
@@ -2983,39 +3158,53 @@ int SDL_GameControllerEventState(int state)
void SDL_GameControllerHandleDelayedGuideButton(SDL_Joystick *joystick)
{
- SDL_GameController *controllerlist = SDL_gamecontrollers;
- while (controllerlist) {
- if (controllerlist->joystick == joystick) {
- SDL_PrivateGameControllerButton(controllerlist, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
+ SDL_GameController *controller;
+
+ SDL_AssertJoysticksLocked();
+
+ for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
+ if (controller->joystick == joystick) {
+ SDL_PrivateGameControllerButton(controller, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
break;
}
- controllerlist = controllerlist->next;
}
}
-const char *
-SDL_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
+const char *SDL_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
#if defined(SDL_JOYSTICK_MFI)
const char *IOS_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button);
+ const char *retval;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+
+ retval = IOS_GameControllerGetAppleSFSymbolsNameForButton(gamecontroller, button);
+ }
+ SDL_UnlockJoysticks();
- return IOS_GameControllerGetAppleSFSymbolsNameForButton(gamecontroller, button);
+ return retval;
#else
return NULL;
#endif
}
-const char *
-SDL_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
+const char *SDL_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
{
#if defined(SDL_JOYSTICK_MFI)
const char *IOS_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis);
+ const char *retval;
- CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+ SDL_LockJoysticks();
+ {
+ CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
+
+ retval = IOS_GameControllerGetAppleSFSymbolsNameForAxis(gamecontroller, axis);
+ }
+ SDL_UnlockJoysticks();
- return IOS_GameControllerGetAppleSFSymbolsNameForAxis(gamecontroller, axis);
+ return retval;
#else
return NULL;
#endif
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 54791dc..4e874a6 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -109,40 +109,37 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
&SDL_DUMMY_JoystickDriver
#endif
};
+SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
+static int SDL_joysticks_locked;
static SDL_bool SDL_joysticks_initialized;
static SDL_bool SDL_joysticks_quitting = SDL_FALSE;
-static SDL_Joystick *SDL_joysticks = NULL;
-static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
-static int SDL_joysticks_locked;
-static SDL_atomic_t SDL_next_joystick_instance_id;
-static int SDL_joystick_player_count = 0;
-static SDL_JoystickID *SDL_joystick_players = NULL;
+static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static SDL_atomic_t SDL_next_joystick_instance_id SDL_GUARDED_BY(SDL_joystick_lock);
+static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;
+static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
static char joystick_magic;
#define CHECK_JOYSTICK_MAGIC(joystick, retval) \
if (!joystick || joystick->magic != &joystick_magic) { \
SDL_InvalidParamError("joystick"); \
+ SDL_UnlockJoysticks(); \
return retval; \
}
-SDL_bool
-SDL_JoysticksInitialized(void)
+SDL_bool SDL_JoysticksInitialized(void)
{
return SDL_joysticks_initialized;
}
-SDL_bool
-SDL_JoysticksQuitting(void)
+SDL_bool SDL_JoysticksQuitting(void)
{
return SDL_joysticks_quitting;
}
void SDL_LockJoysticks(void)
{
- if (SDL_joystick_lock) {
- SDL_LockMutex(SDL_joystick_lock);
- }
+ SDL_LockMutex(SDL_joystick_lock);
++SDL_joysticks_locked;
}
@@ -151,22 +148,18 @@ void SDL_UnlockJoysticks(void)
{
--SDL_joysticks_locked;
- if (SDL_joystick_lock) {
- SDL_UnlockMutex(SDL_joystick_lock);
+ SDL_UnlockMutex(SDL_joystick_lock);
- /* The last unlock after joysticks are uninitialized will cleanup the mutex,
- * allowing applications to lock joysticks while reinitializing the system.
- */
- if (!SDL_joysticks_locked &&
- !SDL_joysticks_initialized) {
- SDL_DestroyMutex(SDL_joystick_lock);
- SDL_joystick_lock = NULL;
- }
+ /* The last unlock after joysticks are uninitialized will cleanup the mutex,
+ * allowing applications to lock joysticks while reinitializing the system.
+ */
+ if (SDL_joystick_lock && !SDL_joysticks_locked && !SDL_joysticks_initialized) {
+ SDL_DestroyMutex(SDL_joystick_lock);
+ SDL_joystick_lock = NULL;
}
}
-SDL_bool
-SDL_JoysticksLocked(void)
+SDL_bool SDL_JoysticksLocked(void)
{
return (SDL_joysticks_locked > 0) ? SDL_TRUE : SDL_FALSE;
}
@@ -366,8 +359,7 @@ SDL_JoystickID SDL_GetNextJoystickInstanceID()
/*
* Get the implementation dependent name of a joystick
*/
-const char *
-SDL_JoystickNameForIndex(int device_index)
+const char *SDL_JoystickNameForIndex(int device_index)
{
SDL_JoystickDriver *driver;
const char *name = NULL;
@@ -385,8 +377,7 @@ SDL_JoystickNameForIndex(int device_index)
/*
* Get the implementation dependent path of a joystick
*/
-const char *
-SDL_JoystickPathForIndex(int device_index)
+const char *SDL_JoystickPathForIndex(int device_index)
{
SDL_JoystickDriver *driver;
const char *path = NULL;
@@ -433,23 +424,30 @@ static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */
};
+ SDL_bool retval = SDL_FALSE;
int i;
Uint32 id = MAKE_VIDPID(SDL_JoystickGetVendor(joystick),
SDL_JoystickGetProduct(joystick));
/*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/
- if (joystick->naxes == 2) {
- /* Assume D-pad or thumbstick style axes are centered at 0 */
- return SDL_TRUE;
- }
+ SDL_LockJoysticks();
+ {
+ if (joystick->naxes == 2) {
+ /* Assume D-pad or thumbstick style axes are centered at 0 */
+ retval = SDL_TRUE;
+ }
- for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
- if (id == zero_centered_joysticks[i]) {
- return SDL_TRUE;
+ for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
+ if (id == zero_centered_joysticks[i]) {
+ retval = SDL_TRUE;
+ break;
+ }
}
}
- return SDL_FALSE;
+ SDL_UnlockJoysticks();
+
+ return retval;
#endif /* __WINRT__ */
}
@@ -460,8 +458,7 @@ static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
*
* This function returns a joystick identifier, or NULL if an error occurred.
*/
-SDL_Joystick *
-SDL_JoystickOpen(int device_index)
+SDL_Joystick *SDL_JoystickOpen(int device_index)
{
SDL_JoystickDriver *driver;
SDL_JoystickID instance_id;
@@ -577,8 +574,7 @@ SDL_JoystickOpen(int device_index)
return joystick;
}
-int SDL_JoystickAttachVirtual(SDL_JoystickType type,
- int naxes, int nbuttons, int nhats)
+int SDL_JoystickAttachVirtual(SDL_JoystickType type, int naxes, int nbuttons, int nhats)
{
SDL_VirtualJoystickDesc desc;
@@ -594,12 +590,12 @@ int SDL_JoystickAttachVirtual(SDL_JoystickType type,
int SDL_JoystickAttachVirtualEx(const SDL_VirtualJoystickDesc *desc)
{
#if SDL_JOYSTICK_VIRTUAL
- int result;
+ int retval;
SDL_LockJoysticks();
- result = SDL_JoystickAttachVirtualInner(desc);
+ retval = SDL_JoystickAttachVirtualInner(desc);
SDL_UnlockJoysticks();
- return result;
+ return retval;
#else
return SDL_SetError("SDL not built with virtual-joystick support");
#endif
@@ -613,9 +609,9 @@ int SDL_JoystickDetachVirtual(int device_index)
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (driver == &SDL_VIRTUAL_JoystickDriver) {
- const int result = SDL_JoystickDetachVirtualInner(device_index);
+ const int retval = SDL_JoystickDetachVirtualInner(device_index);
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
}
SDL_UnlockJoysticks();
@@ -626,8 +622,7 @@ int SDL_JoystickDetachVirtual(int device_index)
#endif
}
-SDL_bool
-SDL_JoystickIsVirtual(int device_index)
+SDL_bool SDL_JoystickIsVirtual(int device_index)
{
#if SDL_JOYSTICK_VIRTUAL
SDL_JoystickDriver *driver;
@@ -650,49 +645,71 @@ SDL_JoystickIsVirtual(int device_index)
int SDL_JoystickSetVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
#if SDL_JOYSTICK_VIRTUAL
- return SDL_JoystickSetVirtualAxisInner(joystick, axis, value);
+ retval = SDL_JoystickSetVirtualAxisInner(joystick, axis, value);
#else
- return SDL_SetError("SDL not built with virtual-joystick support");
+ retval = SDL_SetError("SDL not built with virtual-joystick support");
#endif
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
int SDL_JoystickSetVirtualButton(SDL_Joystick *joystick, int button, Uint8 value)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
#if SDL_JOYSTICK_VIRTUAL
- return SDL_JoystickSetVirtualButtonInner(joystick, button, value);
+ retval = SDL_JoystickSetVirtualButtonInner(joystick, button, value);
#else
- return SDL_SetError("SDL not built with virtual-joystick support");
+ retval = SDL_SetError("SDL not built with virtual-joystick support");
#endif
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
int SDL_JoystickSetVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
#if SDL_JOYSTICK_VIRTUAL
- return SDL_JoystickSetVirtualHatInner(joystick, hat, value);
+ retval = SDL_JoystickSetVirtualHatInner(joystick, hat, value);
#else
- return SDL_SetError("SDL not built with virtual-joystick support");
+ retval = SDL_SetError("SDL not built with virtual-joystick support");
#endif
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
* Checks to make sure the joystick is valid.
*/
-SDL_bool
-SDL_PrivateJoystickValid(SDL_Joystick *joystick)
+SDL_bool SDL_PrivateJoystickValid(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
- return SDL_TRUE;
+ SDL_AssertJoysticksLocked();
+ return (joystick && joystick->magic == &joystick_magic);
}
-SDL_bool
-SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping *out)
+SDL_bool SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping *out)
{
SDL_JoystickDriver *driver;
SDL_bool is_ok = SDL_FALSE;
@@ -711,9 +728,17 @@ SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping *o
*/
int SDL_JoystickNumAxes(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
+
+ retval = joystick->naxes;
+ }
+ SDL_UnlockJoysticks();
- return joystick->naxes;
+ return retval;
}
/*
@@ -721,9 +746,17 @@ int SDL_JoystickNumAxes(SDL_Joystick *joystick)
*/
int SDL_JoystickNumHats(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
+
+ retval = joystick->nhats;
+ }
+ SDL_UnlockJoysticks();
- return joystick->nhats;
+ return retval;
}
/*
@@ -731,9 +764,17 @@ int SDL_JoystickNumHats(SDL_Joystick *joystick)
*/
int SDL_JoystickNumBalls(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
- return joystick->nballs;
+ retval = joystick->nballs;
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -741,46 +782,66 @@ int SDL_JoystickNumBalls(SDL_Joystick *joystick)
*/
int SDL_JoystickNumButtons(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
+
+ retval = joystick->nbuttons;
+ }
+ SDL_UnlockJoysticks();
- return joystick->nbuttons;
+ return retval;
}
/*
* Get the current state of an axis control on a joystick
*/
-Sint16
-SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
+Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
{
Sint16 state;
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, 0);
- if (axis < joystick->naxes) {
- state = joystick->axes[axis].value;
- } else {
- SDL_SetError("Joystick only has %d axes", joystick->naxes);
- state = 0;
+ if (axis < joystick->naxes) {
+ state = joystick->axes[axis].value;
+ } else {
+ SDL_SetError("Joystick only has %d axes", joystick->naxes);
+ state = 0;
+ }
}
+ SDL_UnlockJoysticks();
+
return state;
}
/*
* Get the initial state of an axis control on a joystick
*/
-SDL_bool
-SDL_JoystickGetAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)
+SDL_bool SDL_JoystickGetAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)
{
- CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+ SDL_bool retval;
- if (axis >= joystick->naxes) {
- SDL_SetError("Joystick only has %d axes", joystick->naxes);
- return SDL_FALSE;
- }
- if (state) {
- *state = joystick->axes[axis].initial_value;
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+
+ if (axis >= joystick->naxes) {
+ SDL_SetError("Joystick only has %d axes", joystick->naxes);
+ retval = SDL_FALSE;
+ } else {
+ if (state) {
+ *state = joystick->axes[axis].initial_value;
+ }
+ retval = joystick->axes[axis].has_initial_value;
+ }
}
- return joystick->axes[axis].has_initial_value;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
@@ -790,14 +851,19 @@ Uint8 SDL_JoystickGetHat(SDL_Joystick *joystick, int hat)
{
Uint8 state;
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, 0);
- if (hat < joystick->nhats) {
- state = joystick->hats[hat];
- } else {
- SDL_SetError("Joystick only has %d hats", joystick->nhats);
- state = 0;
+ if (hat < joystick->nhats) {
+ state = joystick->hats[hat];
+ } else {
+ SDL_SetError("Joystick only has %d hats", joystick->nhats);
+ state = 0;
+ }
}
+ SDL_UnlockJoysticks();
+
return state;
}
@@ -808,21 +874,26 @@ int SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy)
{
int retval;
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
- retval = 0;
- if (ball < joystick->nballs) {
- if (dx) {
- *dx = joystick->balls[ball].dx;
- }
- if (dy) {
- *dy = joystick->balls[ball].dy;
+ if (ball < joystick->nballs) {
+ if (dx) {
+ *dx = joystick->balls[ball].dx;
+ }
+ if (dy) {
+ *dy = joystick->balls[ball].dy;
+ }
+ joystick->balls[ball].dx = 0;
+ joystick->balls[ball].dy = 0;
+ retval = 0;
+ } else {
+ retval = SDL_SetError("Joystick only has %d balls", joystick->nballs);
}
- joystick->balls[ball].dx = 0;
- joystick->balls[ball].dy = 0;
- } else {
- return SDL_SetError("Joystick only has %d balls", joystick->nballs);
}
+ SDL_UnlockJoysticks();
+
return retval;
}
@@ -833,14 +904,19 @@ Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
{
Uint8 state;
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, 0);
- if (button < joystick->nbuttons) {
- state = joystick->buttons[button];
- } else {
- SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
- state = 0;
+ if (button < joystick->nbuttons) {
+ state = joystick->buttons[button];
+ } else {
+ SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
+ state = 0;
+ }
}
+ SDL_UnlockJoysticks();
+
return state;
}
@@ -848,30 +924,43 @@ Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
* Return if the joystick in question is currently attached to the system,
* \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
*/
-SDL_bool
-SDL_JoystickGetAttached(SDL_Joystick *joystick)
+SDL_bool SDL_JoystickGetAttached(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+ SDL_bool retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+
+ retval = joystick->attached;
+ }
+ SDL_UnlockJoysticks();
- return joystick->attached;
+ return retval;
}
/*
* Get the instance id for this opened joystick
*/
-SDL_JoystickID
-SDL_JoystickInstanceID(SDL_Joystick *joystick)
+SDL_JoystickID SDL_JoystickInstanceID(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ SDL_JoystickID retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
- return joystick->instance_id;
+ retval = joystick->instance_id;
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
* Return the SDL_Joystick associated with an instance id.
*/
-SDL_Joystick *
-SDL_JoystickFromInstanceID(SDL_JoystickID instance_id)
+SDL_Joystick *SDL_JoystickFromInstanceID(SDL_JoystickID instance_id)
{
SDL_Joystick *joystick;
@@ -888,8 +977,7 @@ SDL_JoystickFromInstanceID(SDL_JoystickID instance_id)
/**
* Return the SDL_Joystick associated with a player index.
*/
-SDL_Joystick *
-SDL_JoystickFromPlayerIndex(int player_index)
+SDL_Joystick *SDL_JoystickFromPlayerIndex(int player_index)
{
SDL_JoystickID instance_id;
SDL_Joystick *joystick;
@@ -908,27 +996,42 @@ SDL_JoystickFromPlayerIndex(int player_index)
/*
* Get the friendly name of this joystick
*/
-const char *
-SDL_JoystickName(SDL_Joystick *joystick)
+const char *SDL_JoystickName(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, NULL);
+ const char *retval;
- return joystick->name;
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, NULL);
+
+ retval = joystick->name;
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/*
* Get the implementation dependent path of this joystick
*/
-const char *
-SDL_JoystickPath(SDL_Joystick *joystick)
+const char *SDL_JoystickPath(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, NULL);
+ const char *retval;
- if (!joystick->path) {
- SDL_Unsupported();
- return NULL;
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, NULL);
+
+ if (joystick->path) {
+ retval = joystick->path;
+ } else {
+ SDL_Unsupported();
+ retval = NULL;
+ }
}
- return joystick->path;
+ SDL_UnlockJoysticks();
+
+ return retval;
}
/**
@@ -936,15 +1039,17 @@ SDL_JoystickPath(SDL_Joystick *joystick)
*/
int SDL_JoystickGetPlayerIndex(SDL_Joystick *joystick)
{
- int player_index;
-
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
SDL_LockJoysticks();
- player_index = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
+
+ retval = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);
+ }
SDL_UnlockJoysticks();
- return player_index;
+ return retval;
}
/**
@@ -952,175 +1057,178 @@ int SDL_JoystickGetPlayerIndex(SDL_Joystick *joystick)
*/
void SDL_JoystickSetPlayerIndex(SDL_Joystick *joystick, int player_index)
{
- CHECK_JOYSTICK_MAGIC(joystick, );
-
SDL_LockJoysticks();
- SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, );
+
+ SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);
+ }
SDL_UnlockJoysticks();
}
int SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
- int result;
-
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
SDL_LockJoysticks();
- if (low_frequency_rumble == joystick->low_frequency_rumble &&
- high_frequency_rumble == joystick->high_frequency_rumble) {
- /* Just update the expiration */
- result = 0;
- } else {
- result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble);
- joystick->rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS;
- if (!joystick->rumble_resend) {
- joystick->rumble_resend = 1;
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
+
+ if (low_frequency_rumble == joystick->low_frequency_rumble &&
+ high_frequency_rumble == joystick->high_frequency_rumble) {
+ /* Just update the expiration */
+ retval = 0;
+ } else {
+ retval = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble);
+ joystick->rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS;
+ if (!joystick->rumble_resend) {
+ joystick->rumble_resend = 1;
+ }
}
- }
- if (result == 0) {
- joystick->low_frequency_rumble = low_frequency_rumble;
- joystick->high_frequency_rumble = high_frequency_rumble;
+ if (retval == 0) {
+ joystick->low_frequency_rumble = low_frequency_rumble;
+ joystick->high_frequency_rumble = high_frequency_rumble;
- if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
- joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
- if (!joystick->rumble_expiration) {
- joystick->rumble_expiration = 1;
+ if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
+ joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
+ if (!joystick->rumble_expiration) {
+ joystick->rumble_expiration = 1;
+ }
+ } else {
+ joystick->rumble_expiration = 0;
+ joystick->rumble_resend = 0;
}
- } else {
- joystick->rumble_expiration = 0;
- joystick->rumble_resend = 0;
}
}
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
int SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
{
- int result;
-
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
SDL_LockJoysticks();
- if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) {
- /* Just update the expiration */
- result = 0;
- } else {
- result = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble);
- }
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
+
+ if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) {
+ /* Just update the expiration */
+ retval = 0;
+ } else {
+ retval = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble);
+ }
- if (result == 0) {
- joystick->left_trigger_rumble = left_rumble;
- joystick->right_trigger_rumble = right_rumble;
+ if (retval == 0) {
+ joystick->left_trigger_rumble = left_rumble;
+ joystick->right_trigger_rumble = right_rumble;
- if ((left_rumble || right_rumble) && duration_ms) {
- joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
- if (!joystick->trigger_rumble_expiration) {
- joystick->trigger_rumble_expiration = 1;
+ if ((left_rumble || right_rumble) && duration_ms) {
+ joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
+ if (!joystick->trigger_rumble_expiration) {
+ joystick->trigger_rumble_expiration = 1;
+ }
+ } else {
+ joystick->trigger_rumble_expiration = 0;
}
- } else {
- joystick->trigger_rumble_expiration = 0;
}
}
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
-SDL_bool
-SDL_JoystickHasLED(SDL_Joystick *joystick)
+SDL_bool SDL_JoystickHasLED(SDL_Joystick *joystick)
{
- SDL_bool result;
-
- CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+ SDL_bool retval;
SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
- result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_LED) != 0;
-
+ retval = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_LED) != 0;
+ }
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
-SDL_bool
-SDL_JoystickHasRumble(SDL_Joystick *joystick)
+SDL_bool SDL_JoystickHasRumble(SDL_Joystick *joystick)
{
- SDL_bool result;
-
- CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+ SDL_bool retval;
SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
- result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE) != 0;
-
+ retval = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE) != 0;
+ }
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
-SDL_bool
-SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick)
+SDL_bool SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick)
{
- SDL_bool result;
-
- CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
+ SDL_bool retval;
SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_FALSE);
- result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE_TRIGGERS) != 0;
-
+ retval = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE_TRIGGERS) != 0;
+ }
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
int SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
- int result;
+ int retval;
SDL_bool isfreshvalue;
- CHECK_JOYSTICK_MAGIC(joystick, -1);
-
SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
- isfreshvalue = red != joystick->led_red ||
- green != joystick->led_green ||
- blue != joystick->led_blue;
-
- if (isfreshvalue || SDL_TICKS_PASSED(SDL_GetTicks(), joystick->led_expiration)) {
- result = joystick->driver->SetLED(joystick, red, green, blue);
- joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS;
- } else {
- /* Avoid spamming the driver */
- result = 0;
- }
+ isfreshvalue = red != joystick->led_red ||
+ green != joystick->led_green ||
+ blue != joystick->led_blue;
- /* Save the LED value regardless of success, so we don't spam the driver */
- joystick->led_red = red;
- joystick->led_green = green;
- joystick->led_blue = blue;
+ if (isfreshvalue || SDL_TICKS_PASSED(SDL_GetTicks(), joystick->led_expiration)) {
+ retval = joystick->driver->SetLED(joystick, red, green, blue);
+ joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS;
+ } else {
+ /* Avoid spamming the driver */
+ retval = 0;
+ }
+ /* Save the LED value regardless of success, so we don't spam the driver */
+ joystick->led_red = red;
+ joystick->led_green = green;
+ joystick->led_blue = blue;
+ }
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
int SDL_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
- int result;
-
- CHECK_JOYSTICK_MAGIC(joystick, -1);
+ int retval;
SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, -1);
- result = joystick->driver->SendEffect(joystick, data, size);
-
+ retval = joystick->driver->SendEffect(joystick, data, size);
+ }
SDL_UnlockJoysticks();
- return result;
+ return retval;
}
/*
@@ -1132,60 +1240,60 @@ void SDL_JoystickClose(SDL_Joystick *joystick)
SDL_Joystick *joysticklistprev;
int i;
- CHECK_JOYSTICK_MAGIC(joystick, );
-
SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, );
- /* First decrement ref count */
- if (--joystick->ref_count > 0) {
- SDL_UnlockJoysticks();
- return;
- }
+ /* First decrement ref count */
+ if (--joystick->ref_count > 0) {
+ SDL_UnlockJoysticks();
+ return;
+ }
- if (joystick->rumble_expiration) {
- SDL_JoystickRumble(joystick, 0, 0, 0);
- }
- if (joystick->trigger_rumble_expiration) {
- SDL_JoystickRumbleTriggers(joystick, 0, 0, 0);
- }
+ if (joystick->rumble_expiration) {
+ SDL_JoystickRumble(joystick, 0, 0, 0);
+ }
+ if (joystick->trigger_rumble_expiration) {
+ SDL_JoystickRumbleTriggers(joystick, 0, 0, 0);
+ }
- joystick->driver->Close(joystick);
- joystick->hwdata = NULL;
- joystick->magic = NULL;
+ joystick->driver->Close(joystick);
+ joystick->hwdata = NULL;
+ joystick->magic = NULL;
- joysticklist = SDL_joysticks;
- joysticklistprev = NULL;
- while (joysticklist) {
- if (joystick == joysticklist) {
- if (joysticklistprev) {
- /* unlink this entry */
- joysticklistprev->next = joysticklist->next;
- } else {
- SDL_joysticks = joystick->next;
+ joysticklist = SDL_joysticks;
+ joysticklistprev = NULL;
+ while (joysticklist) {
+ if (joystick == joysticklist) {
+ if (joysticklistprev) {
+ /* unlink this entry */
+ joysticklistprev->next = joysticklist->next;
+ } else {
+ SDL_joysticks = joystick->next;
+ }
+ break;
}
- break;
+ joysticklistprev = joysticklist;
+ joysticklist = joysticklist->next;
}
- joysticklistprev = joysticklist;
- joysticklist = joysticklist->next;
- }
- SDL_free(joystick->name);
- SDL_free(joystick->path);
- SDL_free(joystick->serial);
+ SDL_free(joystick->name);
+ SDL_free(joystick->path);
+ SDL_free(joystick->serial);
- /* Free the data associated with this joystick */
- SDL_free(joystick->axes);
- SDL_free(joystick->hats);
- SDL_free(joystick->balls);
- SDL_free(joystick->buttons);
- for (i = 0; i < joystick->ntouchpads; i++) {
- SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
- SDL_free(touchpad->fingers);
+ /* Free the data associated with this joystick */
+ SDL_free(joystick->axes);
+ SDL_free(joystick->hats);
+ SDL_free(joystick->balls);
+ SDL_free(joystick->buttons);
+ for (i = 0; i < joystick->ntouchpads; i++) {
+ SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
+ SDL_free(touchpad->fingers);
+ }
+ SDL_free(joystick->touchpads);
+ SDL_free(joystick->sensors);
+ SDL_free(joystick);
}
- SDL_free(joystick->touchpads);
- SDL_free(joystick->sensors);
- SDL_free(joystick);
-
SDL_UnlockJoysticks();
}
@@ -1249,7 +1357,7 @@ void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers)
int ntouchpads;
SDL_JoystickTouchpadInfo *touchpads;
- CHECK_JOYSTICK_MAGIC(joystick, );
+ SDL_AssertJoysticksLocked();
ntouchpads = joystick->ntouchpads + 1;
touchpads = (SDL_JoystickTouchpadInfo *)SDL_realloc(joystick->touchpads, (ntouchpads * sizeof(SDL_JoystickTouchpadInfo)));
@@ -1276,7 +1384,7 @@ void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, f
int nsensors;
SDL_JoystickSensorInfo *sensors;
- CHECK_JOYSTICK_MAGIC(joystick, );
+ SDL_AssertJoysticksLocked();
nsensors = joystick->nsensors + 1;
sensors = (SDL_JoystickSensorInfo *)SDL_realloc(joystick->sensors, (nsensors * sizeof(SDL_JoystickSensorInfo)));
@@ -1396,7 +1504,7 @@ void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
{
int i, j;
- CHECK_JOYSTICK_MAGIC(joystick, );
+ SDL_AssertJoysticksLocked();
/* Tell the app that everything is centered/unpressed... */
for (i = 0; i < joystick->naxes; i++) {
@@ -1471,8 +1579,6 @@ int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
SDL_AssertJoysticksLocked();
- CHECK_JOYSTICK_MAGIC(joystick, 0);
-
/* Make sure we're not getting garbage or duplicate events */
if (axis >= joystick->naxes) {
return 0;
@@ -1538,8 +1644,6 @@ int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
SDL_AssertJoysticksLocked();
- CHECK_JOYSTICK_MAGIC(joystick, 0);
-
/* Make sure we're not getting garbage or duplicate events */
if (hat >= joystick->nhats) {
return 0;
@@ -1575,15 +1679,12 @@ int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
return posted;
}
-int SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball,
- Sint16 xrel, Sint16 yrel)
+int SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
{
int posted;
SDL_AssertJoysticksLocked();
- CHECK_JOYSTICK_MAGIC(joystick, 0);
-
/* Make sure we're not getting garbage events */
if (ball >= joystick->nballs) {
return 0;
@@ -1620,7 +1721,7 @@ int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
#if !SDL_EVENTS_DISABLED
SDL_Event event;
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ SDL_AssertJoysticksLocked();
switch (state) {
case SDL_PRESSED:
@@ -1835,8 +1936,7 @@ static int PrefixMatch(const char *a, const char *b)
return matchlen;
}
-char *
-SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name)
+char *SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name)
{
static struct
{
@@ -1975,8 +2075,7 @@ SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, c
return name;
}
-SDL_JoystickGUID
-SDL_CreateJoystickGUID(Uint16 bus, Uint16 vendor, Uint16 product, Uint16 version, const char *name, Uint8 driver_signature, Uint8 driver_data)
+SDL_JoystickGUID SDL_CreateJoystickGUID(Uint16 bus, Uint16 vendor, Uint16 product, Uint16 version, const char *name, Uint8 driver_signature, Uint8 driver_data)
{
SDL_JoystickGUID guid;
Uint16 *guid16 = (Uint16 *)guid.data;
@@ -2013,8 +2112,7 @@ SDL_CreateJoystickGUID(Uint16 bus, Uint16 vendor, Uint16 product, Uint16 version
return guid;
}
-SDL_JoystickGUID
-SDL_CreateJoystickGUIDForName(const char *name)
+SDL_JoystickGUID SDL_CreateJoystickGUIDForName(const char *name)
{
return SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_UNKNOWN, 0, 0, 0, name, 0, 0);
}
@@ -2047,8 +2145,7 @@ void SDL_SetJoystickGUIDCRC(SDL_JoystickGUID *guid, Uint16 crc)
guid16[1] = SDL_SwapLE16(crc);
}
-SDL_GameControllerType
-SDL_GetJoystickGameControllerTypeFromVIDPID(Uint16 vendor, Uint16 product, const char *name, SDL_bool forUI)
+SDL_GameControllerType SDL_GetJoystickGameControllerTypeFromVIDPID(Uint16 vendor, Uint16 product, const char *name, SDL_bool forUI)
{
SDL_GameControllerType type = SDL_CONTROLLER_TYPE_UNKNOWN;
@@ -2143,8 +2240,7 @@ SDL_GetJoystickGameControllerTypeFromVIDPID(Uint16 vendor, Uint16 product, const
return type;
}
-SDL_GameControllerType
-SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name)
+SDL_GameControllerType SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name)
{
SDL_GameControllerType type;
Uint16 vendor, product;
@@ -2168,15 +2264,13 @@ SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *nam
return type;
}
-SDL_bool
-SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_XBoxOneController;
}
-SDL_bool
-SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id)
{
if (vendor_id == USB_VENDOR_MICROSOFT) {
if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||
@@ -2189,8 +2283,7 @@ SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id)
return SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)
{
if (vendor_id == USB_VENDOR_MICROSOFT) {
if (product_id == USB_PRODUCT_XBOX_SERIES_X ||
@@ -2226,8 +2319,7 @@ SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)
return SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id)
{
if (vendor_id == USB_VENDOR_MICROSOFT) {
if (product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLUETOOTH ||
@@ -2244,22 +2336,19 @@ SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id)
return SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_PS4Controller;
}
-SDL_bool
-SDL_IsJoystickPS5(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickPS5(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_PS5Controller;
}
-SDL_bool
-SDL_IsJoystickDualSenseEdge(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickDualSenseEdge(Uint16 vendor_id, Uint16 product_id)
{
if (vendor_id == USB_VENDOR_SONY) {
if (product_id == USB_PRODUCT_SONY_DS5_EDGE) {
@@ -2269,86 +2358,73 @@ SDL_IsJoystickDualSenseEdge(Uint16 vendor_id, Uint16 product_id)
return SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchPro(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchPro(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_SwitchProController || eType == k_eControllerType_SwitchInputOnlyController;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_SwitchInputOnlyController;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchJoyCon(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchJoyCon(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_SwitchJoyConLeft || eType == k_eControllerType_SwitchJoyConRight;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchJoyConLeft(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchJoyConLeft(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_SwitchJoyConLeft;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchJoyConRight(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchJoyConRight(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_SwitchJoyConRight;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchJoyConGrip(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchJoyConGrip(Uint16 vendor_id, Uint16 product_id)
{
return vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP;
}
-SDL_bool
-SDL_IsJoystickNintendoSwitchJoyConPair(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickNintendoSwitchJoyConPair(Uint16 vendor_id, Uint16 product_id)
{
return vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;
}
-SDL_bool
-SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id)
+SDL_bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
return eType == k_eControllerType_SteamController || eType == k_eControllerType_SteamControllerV2;
}
-SDL_bool
-SDL_IsJoystickXInput(SDL_JoystickGUID guid)
+SDL_bool SDL_IsJoystickXInput(SDL_JoystickGUID guid)
{
return (guid.data[14] == 'x') ? SDL_TRUE : SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickWGI(SDL_JoystickGUID guid)
+SDL_bool SDL_IsJoystickWGI(SDL_JoystickGUID guid)
{
return (guid.data[14] == 'w') ? SDL_TRUE : SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid)
+SDL_bool SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid)
{
return (guid.data[14] == 'h') ? SDL_TRUE : SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickRAWINPUT(SDL_JoystickGUID guid)
+SDL_bool SDL_IsJoystickRAWINPUT(SDL_JoystickGUID guid)
{
return (guid.data[14] == 'r') ? SDL_TRUE : SDL_FALSE;
}
-SDL_bool
-SDL_IsJoystickVirtual(SDL_JoystickGUID guid)
+SDL_bool SDL_IsJoystickVirtual(SDL_JoystickGUID guid)
{
return (guid.data[14] == 'v') ? SDL_TRUE : SDL_FALSE;
}
@@ -2791,11 +2867,19 @@ int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id)
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
{
- static SDL_JoystickGUID emptyGUID;
+ SDL_JoystickGUID retval;
- CHECK_JOYSTICK_MAGIC(joystick, emptyGUID);
+ SDL_LockJoysticks();
+ {
+ static SDL_JoystickGUID emptyGUID;
- return joystick->guid;
+ CHECK_JOYSTICK_MAGIC(joystick, emptyGUID);
+
+ retval = joystick->guid;
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
Uint16 SDL_JoystickGetVendor(SDL_Joystick *joystick)
@@ -2827,16 +2911,32 @@ Uint16 SDL_JoystickGetProductVersion(SDL_Joystick *joystick)
Uint16 SDL_JoystickGetFirmwareVersion(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ Uint16 retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, 0);
+
+ retval = joystick->firmware_version;
+ }
+ SDL_UnlockJoysticks();
- return joystick->firmware_version;
+ return retval;
}
const char *SDL_JoystickGetSerial(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, NULL);
+ const char *retval;
+
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, NULL);
- return joystick->serial;
+ retval = joystick->serial;
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
SDL_JoystickType SDL_JoystickGetType(SDL_Joystick *joystick)
@@ -2846,9 +2946,15 @@ SDL_JoystickType SDL_JoystickGetType(SDL_Joystick *joystick)
type = SDL_GetJoystickGUIDType(guid);
if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
- if (joystick && joystick->is_game_controller) {
- type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_TYPE_UNKNOWN);
+
+ if (joystick->is_game_controller) {
+ type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
+ }
}
+ SDL_UnlockJoysticks();
}
return type;
}
@@ -2868,7 +2974,7 @@ SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
/* update the power level for this joystick */
void SDL_PrivateJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
{
- CHECK_JOYSTICK_MAGIC(joystick, );
+ SDL_AssertJoysticksLocked();
SDL_assert(joystick->ref_count); /* make sure we are calling this only for update, not for initialization */
if (ePowerLevel != joystick->epowerlevel) {
@@ -2888,9 +2994,17 @@ void SDL_PrivateJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLe
/* return its power level */
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick *joystick)
{
- CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_POWER_UNKNOWN);
+ SDL_JoystickPowerLevel retval;
- return joystick->epowerlevel;
+ SDL_LockJoysticks();
+ {
+ CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_POWER_UNKNOWN);
+
+ retval = joystick->epowerlevel;
+ }
+ SDL_UnlockJoysticks();
+
+ return retval;
}
int SDL_PrivateJoystickTouchpad(SDL_Joystick *joystick, int touchpad, int finger, Uint8 state, float x, float y, float pressure)
@@ -2900,7 +3014,7 @@ int SDL_PrivateJoystickTouchpad(SDL_Joystick *joystick, int touchpad, int finger
int posted;
Uint32 event_type;
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ SDL_AssertJoysticksLocked();
if (touchpad < 0 || touchpad >= joystick->ntouchpads) {
return 0;
@@ -2988,7 +3102,7 @@ int SDL_PrivateJoystickSensor(SDL_Joystick *joystick, SDL_SensorType type, Uint6
int i;
int posted = 0;
- CHECK_JOYSTICK_MAGIC(joystick, 0);
+ SDL_AssertJoysticksLocked();
/* We ignore events if we don't have keyboard focus */
if (SDL_PrivateJoystickShouldIgnoreEvent()) {
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index 71d3542..7b6dca0 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -49,7 +49,7 @@ extern SDL_bool SDL_JoysticksQuitting(void);
extern SDL_bool SDL_JoysticksLocked(void);
/* Make sure we currently have the joysticks locked */
-extern void SDL_AssertJoysticksLocked(void);
+extern void SDL_AssertJoysticksLocked(void) SDL_ASSERT_CAPABILITY(SDL_joystick_lock);
/* Function to get the next available joystick instance ID */
extern SDL_JoystickID SDL_GetNextJoystickInstanceID(void);
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 8a83124..4bad952 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -67,68 +67,72 @@ typedef struct _SDL_JoystickSensorInfo
Uint64 timestamp_us;
} SDL_JoystickSensorInfo;
+#define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
+
struct _SDL_Joystick
{
- const void *magic;
+ const void *magic _guarded;
- SDL_JoystickID instance_id; /* Device instance, monotonically increasing from 0 */
- char *name; /* Joystick name - system dependent */
- char *path; /* Joystick path - system dependent */
- char *serial; /* Joystick serial */
- SDL_JoystickGUID guid; /* Joystick guid */
- Uint16 firmware_version; /* Firmware version, if available */
+ SDL_JoystickID instance_id _guarded; /* Device instance, monotonically increasing from 0 */
+ char *name _guarded; /* Joystick name - system dependent */
+ char *path _guarded; /* Joystick path - system dependent */
+ char *serial _guarded; /* Joystick serial */
+ SDL_JoystickGUID guid _guarded; /* Joystick guid */
+ Uint16 firmware_version _guarded; /* Firmware version, if available */
- int naxes; /* Number of axis controls on the joystick */
- SDL_JoystickAxisInfo *axes;
+ int naxes _guarded; /* Number of axis controls on the joystick */
+ SDL_JoystickAxisInfo *axes _guarded;
- int nhats; /* Number of hats on the joystick */
- Uint8 *hats; /* Current hat states */
+ int nhats _guarded; /* Number of hats on the joystick */
+ Uint8 *hats _guarded; /* Current hat states */
- int nballs; /* Number of trackballs on the joystick */
+ int nballs _guarded; /* Number of trackballs on the joystick */
struct balldelta
{
int dx;
int dy;
- } *balls; /* Current ball motion deltas */
+ } *balls _guarded; /* Current ball motion deltas */
- int nbuttons; /* Number of buttons on the joystick */
- Uint8 *buttons; /* Current button states */
+ int nbuttons _guarded; /* Number of buttons on the joystick */
+ Uint8 *buttons _guarded; /* Current button states */
- int ntouchpads; /* Number of touchpads on the joystick */
- SDL_JoystickTouchpadInfo *touchpads; /* Current touchpad states */
+ int ntouchpads _guarded; /* Number of touchpads on the joystick */
+ SDL_JoystickTouchpadInfo *touchpads _guarded; /* Current touchpad states */
- int nsensors; /* Number of sensors on the joystick */
- int nsensors_enabled;
- SDL_JoystickSensorInfo *sensors;
+ int nsensors _guarded; /* Number of sensors on the joystick */
+ int nsensors_enabled _guarded;
+ SDL_JoystickSensorInfo *sensors _guarded;
- Uint16 low_frequency_rumble;
- Uint16 high_frequency_rumble;
- Uint32 rumble_expiration;
- Uint32 rumble_resend;
+ Uint16 low_frequency_rumble _guarded;
+ Uint16 high_frequency_rumble _guarded;
+ Uint32 rumble_expiration _guarded;
+ Uint32 rumble_resend _guarded;
- Uint16 left_trigger_rumble;
- Uint16 right_trigger_rumble;
- Uint32 trigger_rumble_expiration;
+ Uint16 left_trigger_rumble _guarded;
+ Uint16 right_trigger_rumble _guarded;
+ Uint32 trigger_rumble_expiration _guarded;
- Uint8 led_red;
- Uint8 led_green;
- Uint8 led_blue;
- Uint32 led_expiration;
+ Uint8 led_red _guarded;
+ Uint8 led_green _guarded;
+ Uint8 led_blue _guarded;
+ Uint32 led_expiration _guarded;
- SDL_bool attached;
- SDL_bool is_game_controller;
- SDL_bool delayed_guide_button; /* SDL_TRUE if this device has the guide button event delayed */
- SDL_JoystickPowerLevel epowerlevel; /* power level of this joystick, SDL_JOYSTICK_POWER_UNKNOWN if not supported */
+ SDL_bool attached _guarded;
+ SDL_bool is_game_controller _guarded;
+ SDL_bool delayed_guide_button _guarded; /* SDL_TRUE if this device has the guide button event delayed */
+ SDL_JoystickPowerLevel epowerlevel _guarded; /* power level of this joystick, SDL_JOYSTICK_POWER_UNKNOWN if not supported */
- struct _SDL_JoystickDriver *driver;
+ struct _SDL_JoystickDriver *driver _guarded;
- struct joystick_hwdata *hwdata; /* Driver dependent information */
+ struct joystick_hwdata *hwdata _guarded; /* Driver dependent information */
- int ref_count; /* Reference count for multiple opens */
+ int ref_count _guarded; /* Reference count for multiple opens */
- struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
+ struct _SDL_Joystick *next _guarded; /* pointer to next joystick we have allocated */
};
+#undef _guarded
+
/* Device bus definitions */
#define SDL_HARDWARE_BUS_UNKNOWN 0x00
#define SDL_HARDWARE_BUS_USB 0x03
diff --git a/src/joystick/hidapi/SDL_hidapi_combined.c b/src/joystick/hidapi/SDL_hidapi_combined.c
index a8dbe93..599a385 100644
--- a/src/joystick/hidapi/SDL_hidapi_combined.c
+++ b/src/joystick/hidapi/SDL_hidapi_combined.c
@@ -67,6 +67,8 @@ static SDL_bool HIDAPI_DriverCombined_OpenJoystick(SDL_HIDAPI_Device *device, SD
char *serial = NULL, *new_serial;
size_t serial_length = 0, new_length;
+ SDL_AssertJoysticksLocked();
+
for (i = 0; i < device->num_children; ++i) {
SDL_HIDAPI_Device *child = device->children[i];
if (!child->driver->OpenJoystick(child, joystick)) {
diff --git a/src/joystick/hidapi/SDL_hidapi_gamecube.c b/src/joystick/hidapi/SDL_hidapi_gamecube.c
index 7e6abba..7b39c73 100644
--- a/src/joystick/hidapi/SDL_hidapi_gamecube.c
+++ b/src/joystick/hidapi/SDL_hidapi_gamecube.c
@@ -408,6 +408,9 @@ static SDL_bool HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_Device *device, SD
{
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
Uint8 i;
+
+ SDL_AssertJoysticksLocked();
+
for (i = 0; i < MAX_CONTROLLERS; i += 1) {
if (joystick->instance_id == ctx->joysticks[i]) {
joystick->nbuttons = 12;
@@ -424,6 +427,8 @@ static int HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_J
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
Uint8 i, val;
+ SDL_AssertJoysticksLocked();
+
if (ctx->pc_mode) {
return SDL_Unsupported();
}
@@ -469,6 +474,8 @@ static Uint32 HIDAPI_DriverGameCube_GetJoystickCapabilities(SDL_HIDAPI_Device *d
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
Uint32 result = 0;
+ SDL_AssertJoysticksLocked();
+
if (!ctx->pc_mode) {
Uint8 i;
diff --git a/src/joystick/hidapi/SDL_hidapi_luna.c b/src/joystick/hidapi/SDL_hidapi_luna.c
index 5c2a7f4..f4e0761 100644
--- a/src/joystick/hidapi/SDL_hidapi_luna.c
+++ b/src/joystick/hidapi/SDL_hidapi_luna.c
@@ -100,6 +100,8 @@ static SDL_bool HIDAPI_DriverLuna_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Jo
{
SDL_DriverLuna_Context *ctx = (SDL_DriverLuna_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
SDL_zeroa(ctx->last_state);
/* Initialize the joystick capabilities */
diff --git a/src/joystick/hidapi/SDL_hidapi_ps3.c b/src/joystick/hidapi/SDL_hidapi_ps3.c
index 6c4fcd8..e5eadbc 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps3.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps3.c
@@ -234,6 +234,8 @@ static SDL_bool HIDAPI_DriverPS3_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
{
SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
ctx->effects_updated = SDL_FALSE;
ctx->rumble_left = 0;
@@ -630,6 +632,8 @@ static SDL_bool HIDAPI_DriverPS3ThirdParty_OpenJoystick(SDL_HIDAPI_Device *devic
{
SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
SDL_zeroa(ctx->last_state);
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index 56c5807..8834b54 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -668,6 +668,8 @@ static SDL_bool HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
{
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
ctx->last_packet = SDL_GetTicks();
ctx->report_sensors = SDL_FALSE;
diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
index b5086a5..6612273 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps5.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -822,6 +822,8 @@ static SDL_bool HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
{
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
ctx->last_packet = SDL_GetTicks();
ctx->report_sensors = SDL_FALSE;
@@ -961,7 +963,7 @@ static int HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Jo
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}
- if (SDL_HIDAPI_LockRumble() < 0) {
+ if (SDL_HIDAPI_LockRumble() != 0) {
return -1;
}
diff --git a/src/joystick/hidapi/SDL_hidapi_rumble.c b/src/joystick/hidapi/SDL_hidapi_rumble.c
index d109b0e..6cd8dd5 100644
--- a/src/joystick/hidapi/SDL_hidapi_rumble.c
+++ b/src/joystick/hidapi/SDL_hidapi_rumble.c
@@ -46,13 +46,13 @@ typedef struct SDL_HIDAPI_RumbleContext
SDL_atomic_t initialized;
SDL_atomic_t running;
SDL_Thread *thread;
- SDL_mutex *lock;
SDL_sem *request_sem;
SDL_HIDAPI_RumbleRequest *requests_head;
SDL_HIDAPI_RumbleRequest *requests_tail;
} SDL_HIDAPI_RumbleContext;
-static SDL_HIDAPI_RumbleContext rumble_context;
+SDL_mutex *SDL_HIDAPI_rumble_lock;
+static SDL_HIDAPI_RumbleContext rumble_context SDL_GUARDED_BY(SDL_HIDAPI_rumble_lock);
static int SDLCALL SDL_HIDAPI_RumbleThread(void *data)
{
@@ -65,7 +65,7 @@ static int SDLCALL SDL_HIDAPI_RumbleThread(void *data)
SDL_SemWait(ctx->request_sem);
- SDL_LockMutex(ctx->lock);
+ SDL_LockMutex(SDL_HIDAPI_rumble_lock);
request = ctx->requests_tail;
if (request) {
if (request == ctx->requests_head) {
@@ -73,7 +73,7 @@ static int SDLCALL SDL_HIDAPI_RumbleThread(void *data)
}
ctx->requests_tail = request->prev;
}
- SDL_UnlockMutex(ctx->lock);
+ SDL_UnlockMutex(SDL_HIDAPI_rumble_lock);
if (request) {
SDL_LockMutex(request->device->dev_lock);
@@ -111,7 +111,7 @@ static void SDL_HIDAPI_StopRumbleThread(SDL_HIDAPI_RumbleContext *ctx)
ctx->thread = NULL;
}
- SDL_LockMutex(ctx->lock);
+ SDL_LockMutex(SDL_HIDAPI_rumble_lock);
while (ctx->requests_tail) {
request = ctx->requests_tail;
if (request == ctx->requests_head) {
@@ -125,16 +125,16 @@ static void SDL_HIDAPI_StopRumbleThread(SDL_HIDAPI_RumbleContext *ctx)
(void)SDL_AtomicDecRef(&request->device->rumble_pending);
SDL_free(request);
}
- SDL_UnlockMutex(ctx->lock);
+ SDL_UnlockMutex(SDL_HIDAPI_rumble_lock);
if (ctx->request_sem) {
SDL_DestroySemaphore(ctx->request_sem);
ctx->request_sem = NULL;
}
- if (ctx->lock) {
- SDL_DestroyMutex(ctx->lock);
- ctx->lock = NULL;
+ if (SDL_HIDAPI_rumble_lock) {
+ SDL_DestroyMutex(SDL_HIDAPI_rumble_lock);
+ SDL_HIDAPI_rumble_lock = NULL;
}
SDL_AtomicSet(&ctx->initialized, SDL_FALSE);
@@ -142,8 +142,8 @@ static void SDL_HIDAPI_StopRumbleThread(SDL_HIDAPI_RumbleContext *ctx)
static int SDL_HIDAPI_StartRumbleThread(SDL_HIDAPI_RumbleContext *ctx)
{
- ctx->lock = SDL_CreateMutex();
- if (!ctx->lock) {
+ SDL_HIDAPI_rumble_lock = SDL_CreateMutex();
+ if (!SDL_HIDAPI_rumble_lock) {
SDL_HIDAPI_StopRumbleThread(ctx);
return -1;
}
@@ -173,7 +173,8 @@ int SDL_HIDAPI_LockRumble(void)
}
}
- return SDL_LockMutex(ctx->lock);
+ SDL_LockMutex(SDL_HIDAPI_rumble_lock);
+ return 0;
}
SDL_bool SDL_HIDAPI_GetPendingRumbleLocked(SDL_HIDAPI_Device *device, Uint8 **data, int **size, int *maximum_size)
@@ -241,9 +242,7 @@ int SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(SDL_HIDAPI_Device *device, const
void SDL_HIDAPI_UnlockRumble(void)
{
- SDL_HIDAPI_RumbleContext *ctx = &rumble_context;
-
- SDL_UnlockMutex(ctx->lock);
+ SDL_UnlockMutex(SDL_HIDAPI_rumble_lock);
}
int SDL_HIDAPI_SendRumble(SDL_HIDAPI_Device *device, const Uint8 *data, int size)
@@ -256,7 +255,7 @@ int SDL_HIDAPI_SendRumble(SDL_HIDAPI_Device *device, const Uint8 *data, int size
return SDL_SetError("Tried to send rumble with invalid size");
}
- if (SDL_HIDAPI_LockRumble() < 0) {
+ if (SDL_HIDAPI_LockRumble() != 0) {
return -1;
}
diff --git a/src/joystick/hidapi/SDL_hidapi_rumble.h b/src/joystick/hidapi/SDL_hidapi_rumble.h
index bf9f1ad..fea37de 100644
--- a/src/joystick/hidapi/SDL_hidapi_rumble.h
+++ b/src/joystick/hidapi/SDL_hidapi_rumble.h
@@ -25,12 +25,15 @@
/* Handle rumble on a separate thread so it doesn't block the application */
/* Advanced API */
-int SDL_HIDAPI_LockRumble(void);
+#ifdef SDL_THREAD_SAFETY_ANALYSIS
+extern SDL_mutex *SDL_HIDAPI_rumble_lock;
+#endif
+int SDL_HIDAPI_LockRumble(void) SDL_TRY_ACQUIRE(0, SDL_HIDAPI_rumble_lock);
SDL_bool SDL_HIDAPI_GetPendingRumbleLocked(SDL_HIDAPI_Device *device, Uint8 **data, int **size, int *maximum_size);
-int SDL_HIDAPI_SendRumbleAndUnlock(SDL_HIDAPI_Device *device, const Uint8 *data, int size);
+int SDL_HIDAPI_SendRumbleAndUnlock(SDL_HIDAPI_Device *device, const Uint8 *data, int size) SDL_RELEASE(SDL_HIDAPI_rumble_lock);
typedef void (*SDL_HIDAPI_RumbleSentCallback)(void *userdata);
-int SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(SDL_HIDAPI_Device *device, const Uint8 *data, int size, SDL_HIDAPI_RumbleSentCallback callback, void *userdata);
-void SDL_HIDAPI_UnlockRumble(void);
+int SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(SDL_HIDAPI_Device *device, const Uint8 *data, int size, SDL_HIDAPI_RumbleSentCallback callback, void *userdata) SDL_RELEASE(SDL_HIDAPI_rumble_lock);
+void SDL_HIDAPI_UnlockRumble(void) SDL_RELEASE(SDL_HIDAPI_rumble_lock);
/* Simple API, will replace any pending rumble with the new data */
int SDL_HIDAPI_SendRumble(SDL_HIDAPI_Device *device, const Uint8 *data, int size);
diff --git a/src/joystick/hidapi/SDL_hidapi_shield.c b/src/joystick/hidapi/SDL_hidapi_shield.c
index 4fea8a8..79c44c5 100644
--- a/src/joystick/hidapi/SDL_hidapi_shield.c
+++ b/src/joystick/hidapi/SDL_hidapi_shield.c
@@ -148,7 +148,7 @@ static int HIDAPI_DriverShield_SendCommand(SDL_HIDAPI_Device *device, Uint8 cmd,
return SDL_SetError("Command data exceeds HID report size");
}
- if (SDL_HIDAPI_LockRumble() < 0) {
+ if (SDL_HIDAPI_LockRumble() != 0) {
return -1;
}
@@ -175,6 +175,8 @@ static SDL_bool HIDAPI_DriverShield_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
{
SDL_DriverShield_Context *ctx = (SDL_DriverShield_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->rumble_report_pending = SDL_FALSE;
ctx->rumble_update_pending = SDL_FALSE;
ctx->left_motor_amplitude = 0;
diff --git a/src/joystick/hidapi/SDL_hidapi_stadia.c b/src/joystick/hidapi/SDL_hidapi_stadia.c
index 15542a0..93af500 100644
--- a/src/joystick/hidapi/SDL_hidapi_stadia.c
+++ b/src/joystick/hidapi/SDL_hidapi_stadia.c
@@ -96,6 +96,8 @@ static SDL_bool HIDAPI_DriverStadia_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
{
SDL_DriverStadia_Context *ctx = (SDL_DriverStadia_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
SDL_zeroa(ctx->last_state);
/* Initialize the joystick capabilities */
diff --git a/src/joystick/hidapi/SDL_hidapi_steam.c b/src/joystick/hidapi/SDL_hidapi_steam.c
index a7a4acb..17cc0bd 100644
--- a/src/joystick/hidapi/SDL_hidapi_steam.c
+++ b/src/joystick/hidapi/SDL_hidapi_steam.c
@@ -1012,6 +1012,8 @@ static SDL_bool HIDAPI_DriverSteam_OpenJoystick(SDL_HIDAPI_Device *device, SDL_J
SDL_DriverSteam_Context *ctx = (SDL_DriverSteam_Context *)device->context;
float update_rate_in_hz = 0.0f;
+ SDL_AssertJoysticksLocked();
+
ctx->report_sensors = SDL_FALSE;
SDL_zero(ctx->m_assembler);
SDL_zero(ctx->m_state);
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index a287ca0..1bff6f5 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -323,7 +323,7 @@ static int WriteOutput(SDL_DriverSwitch_Context *ctx, const Uint8 *data, int siz
return SDL_hid_write(ctx->device->dev, data, size);
#else
/* Use the rumble thread for general asynchronous writes */
- if (SDL_HIDAPI_LockRumble() < 0) {
+ if (SDL_HIDAPI_LockRumble() != 0) {
return -1;
}
return SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, size);
@@ -1253,6 +1253,8 @@ static SDL_bool HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
Uint8 input_mode;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
ctx->m_bSyncWrite = SDL_TRUE;
@@ -1891,7 +1893,7 @@ static void HandleMiniControllerStateR(SDL_Joystick *joystick, SDL_DriverSwitch_
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
}
-static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet)
+static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchStatePacket_t *packet) SDL_NO_THREAD_SAFETY_ANALYSIS /* We unlock and lock the device lock to be able to change IMU state */
{
if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
if (ctx->device->parent || ctx->m_bVerticalMode) {
diff --git a/src/joystick/hidapi/SDL_hidapi_wii.c b/src/joystick/hidapi/SDL_hidapi_wii.c
index 3ec2ec6..6ca52fb 100644
--- a/src/joystick/hidapi/SDL_hidapi_wii.c
+++ b/src/joystick/hidapi/SDL_hidapi_wii.c
@@ -221,7 +221,7 @@ static SDL_bool WriteOutput(SDL_DriverWii_Context *ctx, const Uint8 *data, int s
return SDL_hid_write(ctx->device->dev, data, size) >= 0;
} else {
/* Use the rumble thread for general asynchronous writes */
- if (SDL_HIDAPI_LockRumble() < 0) {
+ if (SDL_HIDAPI_LockRumble() != 0) {
return SDL_FALSE;
}
return SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, size) >= 0;
@@ -770,6 +770,8 @@ static SDL_bool HIDAPI_DriverWii_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
{
SDL_DriverWii_Context *ctx = (SDL_DriverWii_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
InitializeExtension(ctx);
diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360.c b/src/joystick/hidapi/SDL_hidapi_xbox360.c
index df618df..98a3d42 100644
--- a/src/joystick/hidapi/SDL_hidapi_xbox360.c
+++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c
@@ -176,6 +176,8 @@ static SDL_bool HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL
{
SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->joystick = joystick;
SDL_zeroa(ctx->last_state);
diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360w.c b/src/joystick/hidapi/SDL_hidapi_xbox360w.c
index 10309f0..d735b12 100644
--- a/src/joystick/hidapi/SDL_hidapi_xbox360w.c
+++ b/src/joystick/hidapi/SDL_hidapi_xbox360w.c
@@ -175,6 +175,8 @@ static SDL_bool HIDAPI_DriverXbox360W_OpenJoystick(SDL_HIDAPI_Device *device, SD
{
SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
SDL_zeroa(ctx->last_state);
/* Initialize player index (needed for setting LEDs) */
diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c
index 1f6f907..c66d39a 100644
--- a/src/joystick/hidapi/SDL_hidapi_xboxone.c
+++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c
@@ -234,7 +234,7 @@ static void SendAckIfNeeded(SDL_HIDAPI_Device *device, const Uint8 *data, int si
#ifdef DEBUG_XBOX_PROTOCOL
HIDAPI_DumpPacket("Xbox One sending ACK packet: size = %d", ack_packet, sizeof(ack_packet));
#endif
- if (SDL_HIDAPI_LockRumble() < 0 ||
+ if (SDL_HIDAPI_LockRumble() != 0 ||
SDL_HIDAPI_SendRumbleAndUnlock(device, ack_packet, sizeof(ack_packet)) != sizeof(ack_packet)) {
SDL_SetError("Couldn't send ack packet");
}
@@ -254,7 +254,7 @@ static SDL_bool SendSerialRequest(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_C
* It will cancel the announce packet if sent before that, and will be
* ignored if sent during the negotiation.
*/
- if (SDL_HIDAPI_LockRumble() < 0 ||
+ if (SDL_HIDAPI_LockRumble() != 0 ||
SDL_HIDAPI_SendRumbleAndUnlock(device, serial_packet, sizeof(serial_packet)) != sizeof(serial_packet)) {
SDL_SetError("Couldn't send serial packet");
return SDL_FALSE;
@@ -312,7 +312,7 @@ static SDL_bool SendControllerInit(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_
#endif
ctx->send_time = SDL_GetTicks();
- if (SDL_HIDAPI_LockRumble() < 0 ||
+ if (SDL_HIDAPI_LockRumble() != 0 ||
SDL_HIDAPI_SendRumbleAndUnlock(device, init_packet, packet->size) != packet->size) {
SDL_SetError("Couldn't write Xbox One initialization packet");
return SDL_FALSE;
@@ -415,6 +415,8 @@ static SDL_bool HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL
{
SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
+ SDL_AssertJoysticksLocked();
+
ctx->low_frequency_rumble = 0;
ctx->high_frequency_rumble = 0;
ctx->left_trigger_rumble = 0;
@@ -478,7 +480,7 @@ static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
/* We're no longer pending, even if we fail to send the rumble below */
ctx->rumble_pending = SDL_FALSE;
- if (SDL_HIDAPI_LockRumble() < 0) {
+ if (SDL_HIDAPI_LockRumble() != 0) {
return -1;
}
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 3e35997..7061158 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -91,7 +91,7 @@ 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;
+static SDL_HIDAPI_Device *SDL_HIDAPI_devices SDL_GUARDED_BY(SDL_joystick_lock);
static int SDL_HIDAPI_numjoysticks = 0;
static SDL_bool SDL_HIDAPI_combine_joycons = SDL_TRUE;
static SDL_bool initialized = SDL_FALSE;
@@ -264,6 +264,8 @@ static SDL_HIDAPI_Device *HIDAPI_GetDeviceByIndex(int device_index, SDL_Joystick
{
SDL_HIDAPI_Device *device;
+ SDL_AssertJoysticksLocked();
+
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->parent) {
continue;
@@ -285,6 +287,8 @@ static SDL_HIDAPI_Device *HIDAPI_GetJoystickByInfo(const char *path, Uint16 vend
{
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) {
@@ -323,7 +327,7 @@ static void HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
SDL_UnlockMutex(device->dev_lock);
}
-static void HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device, SDL_bool *removed)
+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;
@@ -426,6 +430,8 @@ static void SDL_HIDAPI_UpdateDrivers(void)
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];
@@ -571,6 +577,8 @@ HIDAPI_HasConnectedUSBDevice(const char *serial)
{
SDL_HIDAPI_Device *device;
+ SDL_AssertJoysticksLocked();
+
if (serial == NULL) {
return SDL_FALSE;
}
@@ -595,6 +603,8 @@ void HIDAPI_DisconnectBluetoothDevice(const char *serial)
{
SDL_HIDAPI_Device *device;
+ SDL_AssertJoysticksLocked();
+
if (serial == NULL) {
return;
}
@@ -622,6 +632,8 @@ 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--;) {
@@ -717,6 +729,8 @@ static SDL_HIDAPI_Device *HIDAPI_AddDevice(const struct SDL_hid_device_info *inf
SDL_HIDAPI_Device *curr, *last = NULL;
SDL_bool removed;
+ SDL_AssertJoysticksLocked();
+
for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
}
@@ -810,6 +824,8 @@ 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
@@ -849,6 +865,8 @@ 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;
}
@@ -1160,6 +1178,8 @@ 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 */
@@ -1262,6 +1282,8 @@ static int HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
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);
@@ -1299,6 +1321,8 @@ static int HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_ru
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
@@ -1314,6 +1338,8 @@ static int HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rum
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
@@ -1329,6 +1355,8 @@ static Uint32 HIDAPI_JoystickGetCapabilities(SDL_Joystick *joystick)
{
Uint32 result = 0;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
@@ -1342,6 +1370,8 @@ static int HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green,
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
@@ -1357,6 +1387,8 @@ static int HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, i
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
@@ -1372,6 +1404,8 @@ static int HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool ena
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
@@ -1388,8 +1422,10 @@ static void HIDAPI_JoystickUpdate(SDL_Joystick *joystick)
/* This is handled in SDL_HIDAPI_UpdateDevices() */
}
-static void HIDAPI_JoystickClose(SDL_Joystick *joystick)
+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;
@@ -1420,6 +1456,8 @@ static void HIDAPI_JoystickQuit(void)
{
int i;
+ SDL_AssertJoysticksLocked();
+
shutting_down = SDL_TRUE;
SDL_HIDAPI_QuitRumble();
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 50dd3b5..4cb84a8 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -852,6 +852,8 @@ static int allocate_hatdata(SDL_Joystick *joystick)
{
int i;
+ SDL_AssertJoysticksLocked();
+
joystick->hwdata->hats =
(struct hwdata_hat *)SDL_malloc(joystick->nhats *
sizeof(struct hwdata_hat));
@@ -869,6 +871,8 @@ static int allocate_balldata(SDL_Joystick *joystick)
{
int i;
+ SDL_AssertJoysticksLocked();
+
joystick->hwdata->balls =
(struct hwdata_ball *)SDL_malloc(joystick->nballs *
sizeof(struct hwdata_ball));
@@ -925,6 +929,8 @@ static void ConfigJoystick(SDL_Joystick *joystick, int fd)
SDL_bool use_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_DEADZONES, SDL_FALSE);
SDL_bool use_hat_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_HAT_DEADZONES, SDL_TRUE);
+ SDL_AssertJoysticksLocked();
+
/* See if this device uses the new unified event API */
if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
(ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
@@ -1132,6 +1138,8 @@ static void ConfigJoystick(SDL_Joystick *joystick, int fd)
on error. Returns -1 on error, 0 on success. */
static int PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item)
{
+ SDL_AssertJoysticksLocked();
+
joystick->hwdata->item = item;
joystick->hwdata->guid = item->guid;
joystick->hwdata->effect.id = -1;
@@ -1180,6 +1188,8 @@ static int LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
SDL_joylist_item *item = JoystickByDevIndex(device_index);
+ SDL_AssertJoysticksLocked();
+
if (item == NULL) {
return SDL_SetError("No such device");
}
@@ -1210,6 +1220,8 @@ static int LINUX_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rum
{
struct input_event event;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata->ff_rumble) {
struct ff_effect *effect = &joystick->hwdata->effect;
@@ -1256,6 +1268,8 @@ static Uint32 LINUX_JoystickGetCapabilities(SDL_Joystick *joystick)
{
Uint32 result = 0;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata->ff_rumble || joystick->hwdata->ff_sine) {
result |= SDL_JOYCAP_RUMBLE;
}
@@ -1280,7 +1294,7 @@ static int LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enab
static void HandleHat(SDL_Joystick *stick, int hatidx, int axis, int value)
{
- const int hatnum = stick->hwdata->hats_indices[hatidx];
+ int hatnum;
struct hwdata_hat *the_hat;
struct hat_axis_correct *correct;
const Uint8 position_map[3][3] = {
@@ -1289,6 +1303,9 @@ static void HandleHat(SDL_Joystick *stick, int hatidx, int axis, int value)
{ SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN }
};
+ SDL_AssertJoysticksLocked();
+
+ hatnum = stick->hwdata->hats_indices[hatidx];
the_hat = &stick->hwdata->hats[hatnum];
correct = &stick->hwdata->hat_correct[hatidx];
/* Hopefully we detected any analog axes and left them as is rather than trying
@@ -1326,6 +1343,8 @@ static void HandleHat(SDL_Joystick *stick, int hatidx, int axis, int value)
static void HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value)
{
+ SDL_AssertJoysticksLocked();
+
stick->hwdata->balls[ball].axis[axis] += value;
}
@@ -1333,6 +1352,8 @@ static int AxisCorrect(SDL_Joystick *joystick, int which, int value)
{
struct axis_correct *correct;
+ SDL_AssertJoysticksLocked();
+
correct = &joystick->hwdata->abs_correct[which];
if (correct->minimum != correct->maximum) {
if (correct->use_deadzones) {
@@ -1368,6 +1389,8 @@ static void PollAllValues(SDL_Joystick *joystick)
unsigned long keyinfo[NBITS(KEY_MAX)];
int i;
+ SDL_AssertJoysticksLocked();
+
/* Poll all axis */
for (i = ABS_X; i < ABS_MAX; i++) {
/* We don't need to test for digital hats here, they won't have has_abs[] set */
@@ -1424,6 +1447,8 @@ static void HandleInputEvents(SDL_Joystick *joystick)
struct input_event events[32];
int i, len, code, hat_index;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata->fresh) {
PollAllValues(joystick);
joystick->hwdata->fresh = SDL_FALSE;
@@ -1515,6 +1540,8 @@ static void HandleClassicEvents(SDL_Joystick *joystick)
struct js_event events[32];
int i, len, code, hat_index;
+ SDL_AssertJoysticksLocked();
+
joystick->hwdata->fresh = SDL_FALSE;
while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
len /= sizeof(events[0]);
@@ -1557,6 +1584,8 @@ static void LINUX_JoystickUpdate(SDL_Joystick *joystick)
{
int i;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata->m_bSteamController) {
SDL_UpdateSteamController(joystick);
return;
@@ -1585,6 +1614,8 @@ static void LINUX_JoystickUpdate(SDL_Joystick *joystick)
/* Function to close a joystick after use */
static void LINUX_JoystickClose(SDL_Joystick *joystick)
{
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
if (joystick->hwdata->effect.id >= 0) {
ioctl(joystick->hwdata->fd, EVIOCRMFF, joystick->hwdata->effect.id);
@@ -1645,6 +1676,8 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
SDL_joylist_item *item = JoystickByDevIndex(device_index);
unsigned int mapped;
+ SDL_AssertJoysticksLocked();
+
if (item->checked_mapping) {
if (item->mapping) {
SDL_memcpy(out, item->mapping, sizeof(*out));
diff --git a/src/joystick/virtual/SDL_virtualjoystick.c b/src/joystick/virtual/SDL_virtualjoystick.c
index a6c9140..cb165b0 100644
--- a/src/joystick/virtual/SDL_virtualjoystick.c
+++ b/src/joystick/virtual/SDL_virtualjoystick.c
@@ -29,32 +29,36 @@
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
-static joystick_hwdata *g_VJoys = NULL;
+static joystick_hwdata *g_VJoys SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static joystick_hwdata *VIRTUAL_HWDataForIndex(int device_index)
{
- joystick_hwdata *vjoy = g_VJoys;
- while (vjoy) {
+ joystick_hwdata *vjoy;
+
+ SDL_AssertJoysticksLocked();
+
+ for (vjoy = g_VJoys; vjoy; vjoy = vjoy->next) {
if (device_index == 0) {
break;
}
--device_index;
- vjoy = vjoy->next;
}
return vjoy;
}
static void VIRTUAL_FreeHWData(joystick_hwdata *hwdata)
{
- joystick_hwdata *cur = g_VJoys;
+ joystick_hwdata *cur;
joystick_hwdata *prev = NULL;
+ SDL_AssertJoysticksLocked();
+
if (hwdata == NULL) {
return;
}
/* Remove hwdata from SDL-global list */
- while (cur) {
+ for (cur = g_VJoys; cur; prev = cur, cur = cur->next) {
if (hwdata == cur) {
if (prev) {
prev->next = cur->next;
@@ -63,8 +67,6 @@ static void VIRTUAL_FreeHWData(joystick_hwdata *hwdata)
}
break;
}
- prev = cur;
- cur = cur->next;
}
if (hwdata->joystick) {
@@ -98,6 +100,8 @@ int SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *desc)
int axis_triggerleft = -1;
int axis_triggerright = -1;
+ SDL_AssertJoysticksLocked();
+
if (desc == NULL) {
return SDL_InvalidParamError("desc");
}
@@ -329,11 +333,13 @@ static int VIRTUAL_JoystickInit(void)
static int VIRTUAL_JoystickGetCount(void)
{
+ joystick_hwdata *cur;
int count = 0;
- joystick_hwdata *cur = g_VJoys;
- while (cur) {
+
+ SDL_AssertJoysticksLocked();
+
+ for (cur = g_VJoys; cur; cur = cur->next) {
++count;
- cur = cur->next;
}
return count;
}
@@ -392,7 +398,11 @@ static SDL_JoystickID VIRTUAL_JoystickGetDeviceInstanceID(int device_index)
static int VIRTUAL_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
- joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
+ joystick_hwdata *hwdata;
+
+ SDL_AssertJoysticksLocked();
+
+ hwdata = VIRTUAL_HWDataForIndex(device_index);
if (hwdata == NULL) {
return SDL_SetError("No such device");
}
@@ -409,6 +419,8 @@ static int VIRTUAL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_r
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
joystick_hwdata *hwdata = joystick->hwdata;
if (hwdata->desc.Rumble) {
@@ -427,6 +439,8 @@ static int VIRTUAL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_ru
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
joystick_hwdata *hwdata = joystick->hwdata;
if (hwdata->desc.RumbleTriggers) {
@@ -443,9 +457,12 @@ static int VIRTUAL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_ru
static Uint32 VIRTUAL_JoystickGetCapabilities(SDL_Joystick *joystick)
{
- joystick_hwdata *hwdata = joystick->hwdata;
+ joystick_hwdata *hwdata;
Uint32 caps = 0;
+ SDL_AssertJoysticksLocked();
+
+ hwdata = joystick->hwdata;
if (hwdata) {
if (hwdata->desc.Rumble) {
caps |= SDL_JOYCAP_RUMBLE;
@@ -464,6 +481,8 @@ static int VIRTUAL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
joystick_hwdata *hwdata = joystick->hwdata;
if (hwdata->desc.SetLED) {
@@ -482,6 +501,8 @@ static int VIRTUAL_JoystickSendEffect(SDL_Joystick *joystick, const void *data,
{
int result;
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
joystick_hwdata *hwdata = joystick->hwdata;
if (hwdata->desc.SendEffect) {
@@ -506,6 +527,8 @@ static void VIRTUAL_JoystickUpdate(SDL_Joystick *joystick)
joystick_hwdata *hwdata;
int i;
+ SDL_AssertJoysticksLocked();
+
if (joystick == NULL) {
return;
}
@@ -532,6 +555,8 @@ static void VIRTUAL_JoystickUpdate(SDL_Joystick *joystick)
static void VIRTUAL_JoystickClose(SDL_Joystick *joystick)
{
+ SDL_AssertJoysticksLocked();
+
if (joystick->hwdata) {
joystick_hwdata *hwdata = joystick->hwdata;
hwdata->joystick = NULL;
@@ -541,6 +566,8 @@ static void VIRTUAL_JoystickClose(SDL_Joystick *joystick)
static void VIRTUAL_JoystickQuit(void)
{
+ SDL_AssertJoysticksLocked();
+
while (g_VJoys) {
VIRTUAL_FreeHWData(g_VJoys);
}
diff --git a/src/sensor/SDL_sensor.c b/src/sensor/SDL_sensor.c
index 2b73f1c..e5cfeed 100644
--- a/src/sensor/SDL_sensor.c
+++ b/src/sensor/SDL_sensor.c
@@ -51,23 +51,19 @@ static SDL_SensorDriver *SDL_sensor_drivers[] = {
&SDL_DUMMY_SensorDriver
#endif
};
-static SDL_Sensor *SDL_sensors = NULL;
-static SDL_bool SDL_updating_sensor = SDL_FALSE;
static SDL_mutex *SDL_sensor_lock = NULL; /* This needs to support recursive locks */
-static SDL_atomic_t SDL_next_sensor_instance_id;
+static SDL_Sensor *SDL_sensors SDL_GUARDED_BY(SDL_sensor_lock) = NULL;
+static SDL_atomic_t SDL_next_sensor_instance_id SDL_GUARDED_BY(SDL_sensor_lock);
+static SDL_bool SDL_updating_sensor SDL_GUARDED_BY(SDL_sensor_lock) = SDL_FALSE;
-void SDL_LockSensors(void)
+void SDL_LockSensors(void) SDL_ACQUIRE(SDL_sensor_lock)
{
- if (SDL_sensor_lock) {
- SDL_LockMutex(SDL_sensor_lock);
- }
+ SDL_LockMutex(SDL_sensor_lock);
}
-void SDL_UnlockSensors(void)
+void SDL_UnlockSensors(void) SDL_RELEASE(SDL_sensor_lock)
{
- if (SDL_sensor_lock) {
- SDL_UnlockMutex(SDL_sensor_lock);
- }
+ SDL_UnlockMutex(SDL_sensor_lock);
}
int SDL_SensorInit(void)
@@ -145,8 +141,7 @@ static SDL_bool SDL_GetDriverAndSensorIndex(int device_index, SDL_SensorDriver *
/*
* Get the implementation dependent name of a sensor
*/
-const char *
-SDL_SensorGetDeviceName(int device_index)
+const char *SDL_SensorGetDeviceName(int device_index)
{
SDL_SensorDriver *driver;
const char *name = NULL;
@@ -161,8 +156,7 @@ SDL_SensorGetDeviceName(int device_index)
return name;
}
-SDL_SensorType
-SDL_SensorGetDeviceType(int device_index)
+SDL_SensorType SDL_SensorGetDeviceType(int device_index)
{
SDL_SensorDriver *driver;
SDL_SensorType type = SDL_SENSOR_INVALID;
@@ -190,8 +184,7 @@ int SDL_SensorGetDeviceNonPortableType(int device_index)
return type;
}
-SDL_SensorID
-SDL_SensorGetDeviceInstanceID(int device_index)
+SDL_SensorID SDL_SensorGetDeviceInstanceID(int device_index)
{
SDL_SensorDriver *driver;
SDL_SensorID instance_id = -1;
@@ -212,8 +205,7 @@ SDL_SensorGetDeviceInstanceID(int device_index)
*
* This function returns a sensor identifier, or NULL if an error occurred.
*/
-SDL_Sensor *
-SDL_SensorOpen(int device_index)
+SDL_Sensor *SDL_SensorOpen(int device_index)
{
SDL_SensorDriver *driver;
SDL_SensorID instance_id;
@@ -284,8 +276,7 @@ SDL_SensorOpen(int device_index)
/*
* Find the SDL_Sensor that owns this instance id
*/
-SDL_Sensor *
-SDL_SensorFromInstanceID(SDL_SensorID instance_id)
+SDL_Sensor *SDL_SensorFromInstanceID(SDL_SensorID instance_id)
{
SDL_Sensor *sensor;
@@ -319,8 +310,7 @@ static int SDL_PrivateSensorValid(SDL_Sensor *sensor)
/*
* Get the friendly name of this sensor
*/
-const char *
-SDL_SensorGetName(SDL_Sensor *sensor)
+const char *SDL_SensorGetName(SDL_Sensor *sensor)
{
if (!SDL_PrivateSensorValid(sensor)) {
return NULL;
@@ -332,8 +322,7 @@ SDL_SensorGetName(SDL_Sensor *sensor)
/*
* Get the type of this sensor
*/
-SDL_SensorType
-SDL_SensorGetType(SDL_Sensor *sensor)
+SDL_SensorType SDL_SensorGetType(SDL_Sensor *sensor)
{
if (!SDL_PrivateSensorValid(sensor)) {
return SDL_SENSOR_INVALID;
@@ -357,8 +346,7 @@ int SDL_SensorGetNonPortableType(SDL_Sensor *sensor)
/*
* Get the instance id for this opened sensor
*/
-SDL_SensorID
-SDL_SensorGetInstanceID(SDL_Sensor *sensor)
+SDL_SensorID SDL_SensorGetInstanceID(SDL_Sensor *sensor)
{
if (!SDL_PrivateSensorValid(sensor)) {
return -1;
@@ -448,11 +436,11 @@ void SDL_SensorQuit(void)
{
int i;
+ SDL_LockSensors();
+
/* Make sure we're not getting called in the middle of updating sensors */
SDL_assert(!SDL_updating_sensor);
- SDL_LockSensors();
-
/* Stop the event polling */
while (SDL_sensors) {
SDL_sensors->ref_count = 1;
@@ -525,15 +513,10 @@ void SDL_SensorUpdate(void)
SDL_updating_sensor = SDL_TRUE;
- /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
- SDL_UnlockSensors();
-
for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
sensor->driver->Update(sensor);
}
- SDL_LockSensors();
-
SDL_updating_sensor = SDL_FALSE;
/* If any sensors were closed while updating, free them here */
diff --git a/src/thread/generic/SDL_sysmutex.c b/src/thread/generic/SDL_sysmutex.c
index dce3219..cda7183 100644
--- a/src/thread/generic/SDL_sysmutex.c
+++ b/src/thread/generic/SDL_sysmutex.c
@@ -71,7 +71,7 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
}
/* Lock the mutex */
-int SDL_LockMutex(SDL_mutex *mutex)
+int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if SDL_THREADS_DISABLED
return 0;
@@ -79,7 +79,7 @@ int SDL_LockMutex(SDL_mutex *mutex)
SDL_threadID this_thread;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
this_thread = SDL_ThreadID();
@@ -109,7 +109,7 @@ int SDL_TryLockMutex(SDL_mutex *mutex)
SDL_threadID this_thread;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
this_thread = SDL_ThreadID();
@@ -132,13 +132,13 @@ int SDL_TryLockMutex(SDL_mutex *mutex)
}
/* Unlock the mutex */
-int SDL_mutexV(SDL_mutex *mutex)
+int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if SDL_THREADS_DISABLED
return 0;
#else
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
/* If we don't own the mutex, we can't unlock it */
diff --git a/src/thread/n3ds/SDL_sysmutex.c b/src/thread/n3ds/SDL_sysmutex.c
index 1a4063d..63b4d35 100644
--- a/src/thread/n3ds/SDL_sysmutex.c
+++ b/src/thread/n3ds/SDL_sysmutex.c
@@ -51,10 +51,10 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
}
/* Lock the mutex */
-int SDL_LockMutex(SDL_mutex *mutex)
+int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
if (mutex == NULL) {
- return SDL_SetError("Passed a NULL mutex");
+ return 0;
}
RecursiveLock_Lock(&mutex->lock);
@@ -66,17 +66,17 @@ int SDL_LockMutex(SDL_mutex *mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
if (mutex == NULL) {
- return SDL_SetError("Passed a NULL mutex");
+ return 0;
}
return RecursiveLock_TryLock(&mutex->lock);
}
/* Unlock the mutex */
-int SDL_mutexV(SDL_mutex *mutex)
+int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
if (mutex == NULL) {
- return SDL_SetError("Passed a NULL mutex");
+ return 0;
}
RecursiveLock_Unlock(&mutex->lock);
diff --git a/src/thread/ngage/SDL_sysmutex.cpp b/src/thread/ngage/SDL_sysmutex.cpp
index 0983f71..6887938 100644
--- a/src/thread/ngage/SDL_sysmutex.cpp
+++ b/src/thread/ngage/SDL_sysmutex.cpp
@@ -68,15 +68,28 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
}
}
+/* Lock the mutex */
+int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
+{
+ if (mutex == NULL) {
+ return 0;
+ }
+
+ RMutex rmutex;
+ rmutex.SetHandle(mutex->handle);
+ rmutex.Wait();
+
+ return 0;
+}
+
/* Try to lock the mutex */
#if 0
int
-SDL_TryLockMutex(SDL_mutex * mutex)
+SDL_TryLockMutex(SDL_mutex *mutex)
{
if (mutex == NULL)
{
- SDL_SetError("Passed a NULL mutex.");
- return -1;
+ return 0;
}
// Not yet implemented.
@@ -84,25 +97,11 @@ SDL_TryLockMutex(SDL_mutex * mutex)
}
#endif
-/* Lock the mutex */
-int SDL_LockMutex(SDL_mutex *mutex)
-{
- if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
- }
-
- RMutex rmutex;
- rmutex.SetHandle(mutex->handle);
- rmutex.Wait();
-
- return 0;
-}
-
/* Unlock the mutex */
-int SDL_UnlockMutex(SDL_mutex *mutex)
+int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
RMutex rmutex;
diff --git a/src/thread/os2/SDL_sysmutex.c b/src/thread/os2/SDL_sysmutex.c
index d3fc7a3..f1e7049 100644
--- a/src/thread/os2/SDL_sysmutex.c
+++ b/src/thread/os2/SDL_sysmutex.c
@@ -67,13 +67,13 @@ SDL_DestroyMutex(SDL_mutex * mutex)
/* Lock the mutex */
int
-SDL_LockMutex(SDL_mutex * mutex)
+SDL_LockMutex(SDL_mutex * mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
ULONG ulRC;
HMTX hMtx = (HMTX)mutex;
if (hMtx == NULLHANDLE)
- return SDL_InvalidParamError("mutex");
+ return 0;
ulRC = DosRequestMutexSem(hMtx, SEM_INDEFINITE_WAIT);
if (ulRC != NO_ERROR) {
@@ -92,7 +92,7 @@ SDL_TryLockMutex(SDL_mutex * mutex)
HMTX hMtx = (HMTX)mutex;
if (hMtx == NULLHANDLE)
- return SDL_InvalidParamError("mutex");
+ return 0;
ulRC = DosRequestMutexSem(hMtx, SEM_IMMEDIATE_RETURN);
@@ -109,13 +109,13 @@ SDL_TryLockMutex(SDL_mutex * mutex)
/* Unlock the mutex */
int
-SDL_UnlockMutex(SDL_mutex * mutex)
+SDL_UnlockMutex(SDL_mutex * mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
ULONG ulRC;
HMTX hMtx = (HMTX)mutex;
if (hMtx == NULLHANDLE)
- return SDL_InvalidParamError("mutex");
+ return 0;
ulRC = DosReleaseMutexSem(hMtx);
if (ulRC != NO_ERROR)
diff --git a/src/thread/psp/SDL_sysmutex.c b/src/thread/psp/SDL_sysmutex.c
index 5f3a7f2..cf52deb 100644
--- a/src/thread/psp/SDL_sysmutex.c
+++ b/src/thread/psp/SDL_sysmutex.c
@@ -73,56 +73,58 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
}
}
-/* Try to lock the mutex */
-int SDL_TryLockMutex(SDL_mutex *mutex)
+/* Lock the mutex */
+int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if SDL_THREADS_DISABLED
return 0;
#else
SceInt32 res = 0;
+
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
- res = sceKernelTryLockLwMutex(&mutex->lock, 1);
- switch (res) {
- case SCE_KERNEL_ERROR_OK:
- return 0;
- break;
- case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
- return SDL_MUTEX_TIMEDOUT;
- break;
- default:
+ res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
+ if (res != SCE_KERNEL_ERROR_OK) {
return SDL_SetError("Error trying to lock mutex: %lx", res);
- break;
}
- return -1;
+ return 0;
#endif /* SDL_THREADS_DISABLED */
}
-/* Lock the mutex */
-int SDL_mutexP(SDL_mutex *mutex)
+/* Try to lock the mutex */
+int SDL_TryLockMutex(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
#else
SceInt32 res = 0;
+
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
- res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
- if (res != SCE_KERNEL_ERROR_OK) {
+ res = sceKernelTryLockLwMutex(&mutex->lock, 1);
+ switch (res) {
+ case SCE_KERNEL_ERROR_OK:
+ return 0;
+ break;
+ case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
+ return SDL_MUTEX_TIMEDOUT;
+ break;
+ default:
return SDL_SetError("Error trying to lock mutex: %lx", res);
+ break;
}
- return 0;
+ return -1;
#endif /* SDL_THREADS_DISABLED */
}
/* Unlock the mutex */
-int SDL_mutexV(SDL_mutex *mutex)
+int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if SDL_THREADS_DISABLED
return 0;
@@ -130,7 +132,7 @@ int SDL_mutexV(SDL_mutex *mutex)
SceInt32 res = 0;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
res = sceKernelUnlockLwMutex(&mutex->lock, 1);
@@ -141,6 +143,7 @@ int SDL_mutexV(SDL_mutex *mutex)
return 0;
#endif /* SDL_THREADS_DISABLED */
}
+
#endif /* SDL_THREAD_PSP */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/thread/pthread/SDL_sysmutex.c b/src/thread/pthread/SDL_sysmutex.c
index c89c095..75ee732 100644
--- a/src/thread/pthread/SDL_sysmutex.c
+++ b/src/thread/pthread/SDL_sysmutex.c
@@ -76,14 +76,14 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
}
/* Lock the mutex */
-int SDL_LockMutex(SDL_mutex *mutex)
+int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if FAKE_RECURSIVE_MUTEX
pthread_t this_thread;
#endif
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
#if FAKE_RECURSIVE_MUTEX
@@ -119,7 +119,7 @@ int SDL_TryLockMutex(SDL_mutex *mutex)
#endif
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
retval = 0;
@@ -155,10 +155,10 @@ int SDL_TryLockMutex(SDL_mutex *mutex)
return retval;
}
-int SDL_UnlockMutex(SDL_mutex *mutex)
+int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
#if FAKE_RECURSIVE_MUTEX
diff --git a/src/thread/stdcpp/SDL_sysmutex.cpp b/src/thread/stdcpp/SDL_sysmutex.cpp
index 0113e67..0c17019 100644
--- a/src/thread/stdcpp/SDL_sysmutex.cpp
+++ b/src/thread/stdcpp/SDL_sysmutex.cpp
@@ -56,12 +56,12 @@ SDL_DestroyMutex(SDL_mutex *mutex)
}
}
-/* Lock the semaphore */
+/* Lock the mutex */
extern "C" int
-SDL_mutexP(SDL_mutex *mutex)
+SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
try {
@@ -76,8 +76,9 @@ SDL_mutexP(SDL_mutex *mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
int retval = 0;
+
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
if (mutex->cpp_mutex.try_lock() == false) {
@@ -88,10 +89,10 @@ int SDL_TryLockMutex(SDL_mutex *mutex)
/* Unlock the mutex */
extern "C" int
-SDL_mutexV(SDL_mutex *mutex)
+SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
mutex->cpp_mutex.unlock();
diff --git a/src/thread/vita/SDL_sysmutex.c b/src/thread/vita/SDL_sysmutex.c
index 4fbd4b4..460f43b 100644
--- a/src/thread/vita/SDL_sysmutex.c
+++ b/src/thread/vita/SDL_sysmutex.c
@@ -69,56 +69,58 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
}
}
-/* Try to lock the mutex */
-int SDL_TryLockMutex(SDL_mutex *mutex)
+/* Lock the mutex */
+int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if SDL_THREADS_DISABLED
return 0;
#else
SceInt32 res = 0;
+
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
- res = sceKernelTryLockLwMutex(&mutex->lock, 1);
- switch (res) {
- case SCE_KERNEL_OK:
- return 0;
- break;
- case SCE_KERNEL_ERROR_MUTEX_FAILED_TO_OWN:
- return SDL_MUTEX_TIMEDOUT;
- break;
- default:
+ res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
+ if (res != SCE_KERNEL_OK) {
return SDL_SetError("Error trying to lock mutex: %x", res);
- break;
}
- return -1;
+ return 0;
#endif /* SDL_THREADS_DISABLED */
}
-/* Lock the mutex */
-int SDL_mutexP(SDL_mutex *mutex)
+/* Try to lock the mutex */
+int SDL_TryLockMutex(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
#else
SceInt32 res = 0;
+
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
- res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
- if (res != SCE_KERNEL_OK) {
+ res = sceKernelTryLockLwMutex(&mutex->lock, 1);
+ switch (res) {
+ case SCE_KERNEL_OK:
+ return 0;
+ break;
+ case SCE_KERNEL_ERROR_MUTEX_FAILED_TO_OWN:
+ return SDL_MUTEX_TIMEDOUT;
+ break;
+ default:
return SDL_SetError("Error trying to lock mutex: %x", res);
+ break;
}
- return 0;
+ return -1;
#endif /* SDL_THREADS_DISABLED */
}
/* Unlock the mutex */
-int SDL_mutexV(SDL_mutex *mutex)
+int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
#if SDL_THREADS_DISABLED
return 0;
@@ -126,7 +128,7 @@ int SDL_mutexV(SDL_mutex *mutex)
SceInt32 res = 0;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
res = sceKernelUnlockLwMutex(&mutex->lock, 1);
diff --git a/src/thread/windows/SDL_sysmutex.c b/src/thread/windows/SDL_sysmutex.c
index c995244..b3c933c 100644
--- a/src/thread/windows/SDL_sysmutex.c
+++ b/src/thread/windows/SDL_sysmutex.c
@@ -77,13 +77,13 @@ static void SDL_DestroyMutex_srw(SDL_mutex *mutex)
}
}
-static int SDL_LockMutex_srw(SDL_mutex *_mutex)
+static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
DWORD this_thread;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
this_thread = GetCurrentThreadId();
@@ -109,7 +109,7 @@ static int SDL_TryLockMutex_srw(SDL_mutex *_mutex)
int retval = 0;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
this_thread = GetCurrentThreadId();
@@ -127,12 +127,12 @@ static int SDL_TryLockMutex_srw(SDL_mutex *_mutex)
return retval;
}
-static int SDL_UnlockMutex_srw(SDL_mutex *_mutex)
+static int SDL_UnlockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
if (mutex->owner == GetCurrentThreadId()) {
@@ -192,11 +192,11 @@ static void SDL_DestroyMutex_cs(SDL_mutex *mutex_)
}
/* Lock the mutex */
-static int SDL_LockMutex_cs(SDL_mutex *mutex_)
+static int SDL_LockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
EnterCriticalSection(&mutex->cs);
@@ -209,7 +209,7 @@ static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
int retval = 0;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
if (TryEnterCriticalSection(&mutex->cs) == 0) {
@@ -219,11 +219,11 @@ static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
}
/* Unlock the mutex */
-static int SDL_UnlockMutex_cs(SDL_mutex *mutex_)
+static int SDL_UnlockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex == NULL) {
- return SDL_InvalidParamError("mutex");
+ return 0;
}
LeaveCriticalSection(&mutex->cs);