N3DS port (squashed) A dedicated renderer using Citro3D would likely allow for better much better graphical performances.
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
diff --git a/.github/workflows/n3ds.yml b/.github/workflows/n3ds.yml
new file mode 100644
index 0000000..af985e4
--- /dev/null
+++ b/.github/workflows/n3ds.yml
@@ -0,0 +1,40 @@
+name: Build (Nintendo 3DS)
+
+on: [push, pull_request]
+
+jobs:
+ n3ds:
+ runs-on: ubuntu-latest
+ container:
+ image: devkitpro/devkitarm:latest
+ steps:
+ - uses: actions/checkout@v2
+ - name: Install build requirements
+ run: |
+ apt update
+ apt install ninja-build
+ - name: Configure CMake
+ run: |
+ cmake -S . -B build -G Ninja \
+ -DCMAKE_TOOLCHAIN_FILE=${DEVKITPRO}/cmake/3DS.cmake \
+ -DSDL_TESTS=ON \
+ -DSDL_INSTALL_TESTS=ON \
+ -DCMAKE_BUILD_TYPE=Release \
+ -DCMAKE_INSTALL_PREFIX=prefix
+ - name: Build
+ run: cmake --build build --verbose
+ - name: Install CMake
+ run: |
+ echo "SDL2_DIR=$(pwd)/prefix" >> $GITHUB_ENV
+ cmake --install build/
+ ( cd prefix; find ) | LC_ALL=C sort -u
+ - name: Verify CMake configuration files
+ run: |
+ cmake -S cmake/test -B cmake_config_build -G Ninja \
+ -DCMAKE_TOOLCHAIN_FILE=${DEVKITPRO}/cmake/3DS.cmake \
+ -DTEST_SHARED=FALSE \
+ -DCMAKE_PREFIX_PATH=${{ env.SDL2_DIR }} \
+ -DCMAKE_BUILD_TYPE=Release
+ cmake --build cmake_config_build --verbose
+ # Not running test_pkgconfig.sh and test_sdlconfig.sh
+ # as invoking the compiler manually is not supported
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 15a6cbf..48cbb61 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -196,6 +196,8 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "BeOS.*")
message_error("BeOS support has been removed as of SDL 2.0.2.")
elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku.*")
set(HAIKU TRUE)
+elseif(NINTENDO_3DS)
+ set(N3DS TRUE)
endif()
# Don't mistake osx for unix
@@ -277,7 +279,7 @@ if(APPLE OR ARCH_64 OR MSVC_CLANG)
set(OPT_DEF_SSEMATH ON)
endif()
endif()
-if(UNIX OR MINGW OR MSYS OR (USE_CLANG AND NOT WINDOWS) OR VITA OR PSP OR PS2)
+if(UNIX OR MINGW OR MSYS OR (USE_CLANG AND NOT WINDOWS) OR VITA OR PSP OR PS2 OR N3DS)
set(OPT_DEF_LIBC ON)
endif()
@@ -381,7 +383,7 @@ if(EMSCRIPTEN)
set(SDL_TEST_ENABLED_BY_DEFAULT OFF)
endif()
-if(VITA OR PSP OR PS2)
+if(VITA OR PSP OR PS2 OR N3DS)
set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
set(SDL_LOADSO_ENABLED_BY_DEFAULT OFF)
endif()
@@ -2734,6 +2736,74 @@ elseif(OS2)
if(SDL_HIDAPI)
CheckHIDAPI()
endif()
+
+elseif(N3DS)
+ file(GLOB N3DS_MAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/n3ds/*.c)
+ set(SDLMAIN_SOURCES ${SDLMAIN_SOURCES} ${N3DS_MAIN_SOURCES})
+
+ if(SDL_AUDIO)
+ set(SDL_AUDIO_DRIVER_N3DS 1)
+ file(GLOB N3DS_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_AUDIO_SOURCES})
+ set(HAVE_SDL_AUDIO TRUE)
+ endif()
+
+ if(SDL_FILESYSTEM)
+ set(SDL_FILESYSTEM_N3DS 1)
+ file(GLOB N3DS_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_FILESYSTEM_SOURCES})
+ set(HAVE_SDL_FILESYSTEM TRUE)
+ endif()
+
+ if(SDL_JOYSTICK)
+ set(SDL_JOYSTICK_N3DS 1)
+ file(GLOB N3DS_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_JOYSTICK_SOURCES})
+ set(HAVE_SDL_JOYSTICK TRUE)
+ endif()
+
+ if(SDL_POWER)
+ set(SDL_POWER_N3DS 1)
+ file(GLOB N3DS_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_POWER_SOURCES})
+ set(HAVE_SDL_POWER TRUE)
+ endif()
+
+ if(SDL_THREADS)
+ set(SDL_THREAD_N3DS 1)
+ file(GLOB N3DS_THREAD_SOURCES ${SDL2_SOURCE_DIR}/src/thread/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_THREAD_SOURCES} ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_systls.c)
+ set(HAVE_SDL_THREADS TRUE)
+ endif()
+
+ if(SDL_TIMERS)
+ set(SDL_TIMER_N3DS 1)
+ file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${TIMER_SOURCES})
+ set(HAVE_SDL_TIMERS TRUE)
+ endif()
+
+ if(SDL_VIDEO)
+ set(SDL_VIDEO_DRIVER_N3DS 1)
+ file(GLOB N3DS_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_VIDEO_SOURCES})
+ set(HAVE_SDL_VIDEO TRUE)
+ endif()
+
+ if(SDL_LOCALE)
+ file(GLOB N3DS_LOCALE_SOURCES ${SDL2_SOURCE_DIR}/src/locale/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_LOCALE_SOURCES})
+ set(HAVE_SDL_LOCALE TRUE)
+ endif()
+
+ # Requires the n3ds file implementation
+ if(SDL_FILE)
+ file(GLOB N3DS_FILE_SOURCES ${SDL2_SOURCE_DIR}/src/file/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_FILE_SOURCES})
+ set(HAVE_SDL_FILE TRUE)
+ else()
+ message_error("SDL_FILE must be enabled to build on N3DS")
+ endif()
endif()
if(HAVE_VULKAN AND NOT SDL_LOADSO)
diff --git a/docs/README-n3ds.md b/docs/README-n3ds.md
new file mode 100644
index 0000000..761c76d
--- /dev/null
+++ b/docs/README-n3ds.md
@@ -0,0 +1,27 @@
+# Nintendo 3DS
+
+SDL port for the Nintendo 3DS [Homebrew toolchain](https://devkitpro.org/) contributed by:
+
+- [Pierre Wendling](https://github.com/FtZPetruska)
+
+Credits to:
+
+- The awesome people who ported SDL to other homebrew platforms.
+- The Devkitpro team for making all the tools necessary to achieve this.
+
+## Building
+
+To build for the Nintendo 3DS, make sure you have devkitARM and cmake installed and run:
+
+```bash
+cmake -S. -Bbuild -DCMAKE_TOOLCHAIN_FILE="$DEVKITPRO/cmake/3DS.cmake" -DCMAKE_BUILD_TYPE=Release
+cmake --build build
+cmake --install build
+```
+
+## Notes
+
+- Currently only software rendering is supported.
+- Window are created on the top screen by default, use the `SDL_WINDOW_N3DS_BOTTOM` flag to put them on the bottom screen.
+- SDL2main should be used to ensure all the necessary services are initialised.
+- By default, the extra L2 cache and higher clock speeds of the New 2/3DS lineup are enabled. If you wish to turn it off, [use the PTMSYSM service](https://libctru.devkitpro.org/ptmsysm_8h.html#ae3a437bfd0de05fbc5ba9a460d148430) to turn it off in your program.
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 15c5c9e..6dc89a4 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -325,6 +325,7 @@
#cmakedefine SDL_AUDIO_DRIVER_VITA @SDL_AUDIO_DRIVER_VITA@
#cmakedefine SDL_AUDIO_DRIVER_PSP @SDL_AUDIO_DRIVER_PSP@
#cmakedefine SDL_AUDIO_DRIVER_PS2 @SDL_AUDIO_DRIVER_PS2@
+#cmakedefine SDL_AUDIO_DRIVER_N3DS @SDL_AUDIO_DRIVER_N3DS@
/* Enable various input drivers */
#cmakedefine SDL_INPUT_LINUXEV @SDL_INPUT_LINUXEV@
@@ -349,6 +350,7 @@
#cmakedefine SDL_JOYSTICK_VITA @SDL_JOYSTICK_VITA@
#cmakedefine SDL_JOYSTICK_PSP @SDL_JOYSTICK_PSP@
#cmakedefine SDL_JOYSTICK_PS2 @SDL_JOYSTICK_PS2@
+#cmakedefine SDL_JOYSTICK_N3DS @SDL_JOYSTICK_N3DS@
#cmakedefine SDL_HAPTIC_DUMMY @SDL_HAPTIC_DUMMY@
#cmakedefine SDL_HAPTIC_LINUX @SDL_HAPTIC_LINUX@
#cmakedefine SDL_HAPTIC_IOKIT @SDL_HAPTIC_IOKIT@
@@ -381,6 +383,7 @@
#cmakedefine SDL_THREAD_VITA @SDL_THREAD_VITA@
#cmakedefine SDL_THREAD_PSP @SDL_THREAD_PSP@
#cmakedefine SDL_THREAD_PS2 @SDL_THREAD_PS2@
+#cmakedefine SDL_THREAD_N3DS @SDL_THREAD_N3DS@
/* Enable various timer systems */
#cmakedefine SDL_TIMER_HAIKU @SDL_TIMER_HAIKU@
@@ -391,6 +394,7 @@
#cmakedefine SDL_TIMER_VITA @SDL_TIMER_VITA@
#cmakedefine SDL_TIMER_PSP @SDL_TIMER_PSP@
#cmakedefine SDL_TIMER_PS2 @SDL_TIMER_PS2@
+#cmakedefine SDL_TIMER_N3DS @SDL_TIMER_N3DS@
/* Enable various video drivers */
#cmakedefine SDL_VIDEO_DRIVER_ANDROID @SDL_VIDEO_DRIVER_ANDROID@
@@ -444,6 +448,7 @@
#cmakedefine SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS @SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS@
#cmakedefine SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM @SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM@
#cmakedefine SDL_VIDEO_DRIVER_VITA @SDL_VIDEO_DRIVER_VITA@
+#cmakedefine SDL_VIDEO_DRIVER_N3DS @SDL_VIDEO_DRIVER_N3DS@
#cmakedefine SDL_VIDEO_RENDER_D3D @SDL_VIDEO_RENDER_D3D@
#cmakedefine SDL_VIDEO_RENDER_D3D11 @SDL_VIDEO_RENDER_D3D11@
@@ -487,6 +492,7 @@
#cmakedefine SDL_POWER_HARDWIRED @SDL_POWER_HARDWIRED@
#cmakedefine SDL_POWER_VITA @SDL_POWER_VITA@
#cmakedefine SDL_POWER_PSP @SDL_POWER_PSP@
+#cmakedefine SDL_POWER_N3DS @SDL_POWER_N3DS@
/* Enable system filesystem support */
#cmakedefine SDL_FILESYSTEM_ANDROID @SDL_FILESYSTEM_ANDROID@
@@ -501,6 +507,7 @@
#cmakedefine SDL_FILESYSTEM_VITA @SDL_FILESYSTEM_VITA@
#cmakedefine SDL_FILESYSTEM_PSP @SDL_FILESYSTEM_PSP@
#cmakedefine SDL_FILESYSTEM_PS2 @SDL_FILESYSTEM_PS2@
+#cmakedefine SDL_FILESYSTEM_N3DS @SDL_FILESYSTEM_N3DS@
/* Enable misc subsystem */
#cmakedefine SDL_MISC_DUMMY @SDL_MISC_DUMMY@
diff --git a/include/SDL_main.h b/include/SDL_main.h
index 8b26708..113d11d 100644
--- a/include/SDL_main.h
+++ b/include/SDL_main.h
@@ -108,6 +108,15 @@
void reset_IOP(); \
void reset_IOP() {}
+#elif defined(__3DS__)
+/*
+ On N3DS, SDL provides a main function that sets up the screens
+ and storage.
+
+ If you provide this yourself, you may define SDL_MAIN_HANDLED
+*/
+#define SDL_MAIN_AVAILABLE
+
#endif
#endif /* SDL_MAIN_HANDLED */
diff --git a/include/SDL_platform.h b/include/SDL_platform.h
index f1f6f8b..a8e3ac2 100644
--- a/include/SDL_platform.h
+++ b/include/SDL_platform.h
@@ -221,6 +221,11 @@
#define __VITA__ 1
#endif
+#if defined(__3DS__)
+#undef __3DS__
+#define __3DS__ 1
+#endif
+
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index 5f79c95..c60d6ee 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -410,7 +410,7 @@ SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
/** \cond */
#ifndef DOXYGEN_SHOULD_IGNORE_THIS
-#if !defined(__ANDROID__) && !defined(__VITA__)
+#if !defined(__ANDROID__) && !defined(__VITA__) && !defined(__3DS__)
/* TODO: include/SDL_stdinc.h:174: error: size of array 'SDL_dummy_enum' is negative */
typedef enum
{
diff --git a/include/SDL_video.h b/include/SDL_video.h
index d9dce43..60afda2 100644
--- a/include/SDL_video.h
+++ b/include/SDL_video.h
@@ -126,6 +126,7 @@ typedef enum
SDL_WINDOW_KEYBOARD_GRABBED = 0x00100000, /**< window has grabbed keyboard input */
SDL_WINDOW_VULKAN = 0x10000000, /**< window usable for Vulkan surface */
SDL_WINDOW_METAL = 0x20000000, /**< window usable for Metal view */
+ SDL_WINDOW_N3DS_BOTTOM = 0x40000000, /**< window should be on the bottom screen (N3DS only) */
SDL_WINDOW_INPUT_GRABBED = SDL_WINDOW_MOUSE_GRABBED /**< equivalent to SDL_WINDOW_MOUSE_GRABBED for compatibility */
} SDL_WindowFlags;
diff --git a/src/SDL.c b/src/SDL.c
index 0e1c32a..93f7a7f 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -609,6 +609,8 @@ SDL_GetPlatform(void)
return "PlayStation Vita";
#elif __NGAGE__
return "Nokia N-Gage";
+#elif __3DS__
+ return "Nintendo 3DS";
#else
return "Unknown (see SDL_platform.h)";
#endif
diff --git a/src/SDL_log.c b/src/SDL_log.c
index 34c6fe8..66191d2 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -485,6 +485,13 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
fclose (pFile);
}
+#elif defined(__3DS__)
+ {
+ FILE* pFile;
+ pFile = fopen ("/SDL_Log.txt", "a");
+ fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
+ fclose (pFile);
+ }
#endif
#if HAVE_STDIO_H && \
!(defined(__APPLE__) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT)))
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index e33e41d..11700d4 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -105,6 +105,9 @@ static const AudioBootStrap *const bootstrap[] = {
#if SDL_AUDIO_DRIVER_VITA
&VITAAUD_bootstrap,
#endif
+#if SDL_AUDIO_DRIVER_N3DS
+ &N3DSAUDIO_bootstrap,
+#endif
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
&EMSCRIPTENAUDIO_bootstrap,
#endif
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 6afaae1..a911de0 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -209,6 +209,7 @@ extern AudioBootStrap ANDROIDAUDIO_bootstrap;
extern AudioBootStrap PS2AUDIO_bootstrap;
extern AudioBootStrap PSPAUDIO_bootstrap;
extern AudioBootStrap VITAAUD_bootstrap;
+extern AudioBootStrap N3DSAUDIO_bootstrap;
extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
extern AudioBootStrap OS2AUDIO_bootstrap;
diff --git a/src/audio/n3ds/SDL_n3dsaudio.c b/src/audio/n3ds/SDL_n3dsaudio.c
new file mode 100644
index 0000000..484bec8
--- /dev/null
+++ b/src/audio/n3ds/SDL_n3dsaudio.c
@@ -0,0 +1,363 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_AUDIO_DRIVER_N3DS
+
+#include "SDL_audio.h"
+
+/* N3DS Audio driver */
+
+#include "../SDL_sysaudio.h"
+#include "SDL_n3dsaudio.h"
+#include "SDL_timer.h"
+
+#define N3DSAUDIO_DRIVER_NAME "n3ds"
+
+static dspHookCookie dsp_hook;
+static SDL_AudioDevice *audio_device;
+
+static void FreePrivateData(_THIS);
+static int FindAudioFormat(_THIS);
+
+static SDL_INLINE void
+contextLock(_THIS)
+{
+ LightLock_Lock(&this->hidden->lock);
+}
+
+static SDL_INLINE void
+contextUnlock(_THIS)
+{
+ LightLock_Unlock(&this->hidden->lock);
+}
+
+static void
+N3DSAUD_LockAudio(_THIS)
+{
+ contextLock(this);
+}
+
+static void
+N3DSAUD_UnlockAudio(_THIS)
+{
+ contextUnlock(this);
+}
+
+static void
+N3DSAUD_DspHook(DSP_HookType hook)
+{
+ if (hook == DSPHOOK_ONCANCEL) {
+ contextLock(audio_device);
+ audio_device->hidden->isCancelled = SDL_TRUE;
+ SDL_AtomicSet(&audio_device->enabled, SDL_FALSE);
+ CondVar_Broadcast(&audio_device->hidden->cv);
+ contextUnlock(audio_device);
+ }
+}
+
+static void
+AudioFrameFinished(void *device)
+{
+ bool shouldBroadcast = false;
+ unsigned i;
+ SDL_AudioDevice *this = (SDL_AudioDevice *) device;
+
+ contextLock(this);
+
+ for (i = 0; i < NUM_BUFFERS; i++) {
+ if (this->hidden->waveBuf[i].status == NDSP_WBUF_DONE) {
+ this->hidden->waveBuf[i].status = NDSP_WBUF_FREE;
+ shouldBroadcast = SDL_TRUE;
+ }
+ }
+
+ if (shouldBroadcast) {
+ CondVar_Broadcast(&this->hidden->cv);
+ }
+
+ contextUnlock(this);
+}
+
+static int
+N3DSAUDIO_OpenDevice(_THIS, const char *devname)
+{
+ Result ndsp_init_res;
+ Uint8 *data_vaddr;
+ float mix[12];
+ this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof *this->hidden);
+
+ if (this->hidden == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ /* Initialise the DSP service */
+ ndsp_init_res = ndspInit();
+ if (R_FAILED(ndsp_init_res)) {
+ if ((R_SUMMARY(ndsp_init_res) == RS_NOTFOUND) && (R_MODULE(ndsp_init_res) == RM_DSP)) {
+ SDL_SetError("DSP init failed: dspfirm.cdc missing!");
+ } else {
+ SDL_SetError("DSP init failed. Error code: 0x%lX", ndsp_init_res);
+ }
+ return -1;
+ }
+
+ /* Initialise internal state */
+ LightLock_Init(&this->hidden->lock);
+ CondVar_Init(&this->hidden->cv);
+
+ if (this->spec.channels > 2) {
+ this->spec.channels = 2;
+ }
+
+ /* Should not happen but better be safe. */
+ if (FindAudioFormat(this) < 0) {
+ return SDL_SetError("No supported audio format found.");
+ }
+
+ /* Update the fragment size as size in bytes */
+ SDL_CalculateAudioSpec(&this->spec);
+
+ /* Allocate mixing buffer */
+ if (this->spec.size >= SDL_MAX_UINT32 / 2) {
+ return SDL_SetError("Mixing buffer is too large.");
+ }
+
+ this->hidden->mixlen = this->spec.size;
+ this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
+ if (this->hidden->mixbuf == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
+
+ data_vaddr = (Uint8 *) linearAlloc(this->hidden->mixlen * NUM_BUFFERS);
+ if (data_vaddr == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ SDL_memset(data_vaddr, 0, this->hidden->mixlen * NUM_BUFFERS);
+ DSP_FlushDataCache(data_vaddr, this->hidden->mixlen * NUM_BUFFERS);
+
+ this->hidden->nextbuf = 0;
+ this->hidden->channels = this->spec.channels;
+ this->hidden->samplerate = this->spec.freq;
+
+ ndspChnReset(0);
+
+ ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
+ ndspChnSetRate(0, this->spec.freq);
+ ndspChnSetFormat(0, this->hidden->format);
+
+ SDL_memset(mix, 0, sizeof(mix));
+ mix[0] = 1.0;
+ mix[1] = 1.0;
+ ndspChnSetMix(0, mix);
+
+ SDL_memset(this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
+
+ for (unsigned i = 0; i < NUM_BUFFERS; i++) {
+ this->hidden->waveBuf[i].data_vaddr = data_vaddr;
+ this->hidden->waveBuf[i].nsamples = this->hidden->mixlen / this->hidden->bytePerSample;
+ data_vaddr += this->hidden->mixlen;
+ }
+
+ /* Setup callback */
+ audio_device = this;
+ ndspSetCallback(AudioFrameFinished, this);
+ dspHook(&dsp_hook, N3DSAUD_DspHook);
+
+ return 0;
+}
+
+static int
+N3DSAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
+{
+ /* Delay to make this sort of simulate real audio input. */
+ SDL_Delay((this->spec.samples * 1000) / this->spec.freq);
+
+ /* always return a full buffer of silence. */
+ SDL_memset(buffer, this->spec.silence, buflen);
+ return buflen;
+}
+
+static void
+N3DSAUDIO_PlayDevice(_THIS)
+{
+ size_t nextbuf;
+ size_t sampleLen;
+ contextLock(this);
+
+ nextbuf = this->hidden->nextbuf;
+ sampleLen = this->hidden->mixlen;
+
+ if (this->hidden->isCancelled ||
+ this->hidden->waveBuf[nextbuf].status != NDSP_WBUF_FREE) {
+ contextUnlock(this);
+ return;
+ }
+
+ this->hidden->nextbuf = (nextbuf + 1) % NUM_BUFFERS;
+
+ contextUnlock(this);
+
+ memcpy((void *) this->hidden->waveBuf[nextbuf].data_vaddr,
+ this->hidden->mixbuf, sampleLen);
+ DSP_FlushDataCache(this->hidden->waveBuf[nextbuf].data_vaddr, sampleLen);
+
+ ndspChnWaveBufAdd(0, &this->hidden->waveBuf[nextbuf]);
+}
+
+static void
+N3DSAUDIO_WaitDevice(_THIS)
+{
+ contextLock(this);
+ while (!this->hidden->isCancelled &&
+ this->hidden->waveBuf[this->hidden->nextbuf].status != NDSP_WBUF_FREE) {
+ CondVar_Wait(&this->hidden->cv, &this->hidden->lock);
+ }
+ contextUnlock(this);
+}
+
+static Uint8 *
+N3DSAUDIO_GetDeviceBuf(_THIS)
+{
+ return this->hidden->mixbuf;
+}
+
+static void
+N3DSAUDIO_CloseDevice(_THIS)
+{
+ contextLock(this);
+
+ dspUnhook(&dsp_hook);
+ ndspSetCallback(NULL, NULL);
+
+ if (!this->hidden->isCancelled) {
+ ndspChnReset(0);
+ memset(this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
+ CondVar_Broadcast(&this->hidden->cv);
+ }
+
+ contextUnlock(this);
+
+ ndspExit();
+
+ FreePrivateData(this);
+}
+
+static void
+N3DSAUDIO_ThreadInit(_THIS)
+{
+ s32 current_priority;
+ svcGetThreadPriority(¤t_priority, CUR_THREAD_HANDLE);
+ current_priority--;
+ /* 0x18 is reserved for video, 0x30 is the default for main thread */
+ current_priority = SDL_clamp(current_priority, 0x19, 0x2F);
+ svcSetThreadPriority(CUR_THREAD_HANDLE, current_priority);
+}
+
+static SDL_bool
+N3DSAUDIO_Init(SDL_AudioDriverImpl *impl)
+{
+ /* Set the function pointers */
+ impl->OpenDevice = N3DSAUDIO_OpenDevice;
+ impl->PlayDevice = N3DSAUDIO_PlayDevice;
+ impl->WaitDevice = N3DSAUDIO_WaitDevice;
+ impl->GetDeviceBuf = N3DSAUDIO_GetDeviceBuf;
+ impl->CloseDevice = N3DSAUDIO_CloseDevice;
+ impl->ThreadInit = N3DSAUDIO_ThreadInit;
+ impl->LockDevice = N3DSAUD_LockAudio;
+ impl->UnlockDevice = N3DSAUD_UnlockAudio;
+ impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
+
+ /* Should be possible, but micInit would fail */
+ impl->HasCaptureSupport = SDL_FALSE;
+ impl->CaptureFromDevice = N3DSAUDIO_CaptureFromDevice;
+
+ return SDL_TRUE; /* this audio target is available. */
+}
+
+AudioBootStrap N3DSAUDIO_bootstrap = {
+ N3DSAUDIO_DRIVER_NAME,
+ "SDL N3DS audio driver",
+ N3DSAUDIO_Init,
+ 0
+};
+
+/**
+ * Cleans up all allocated memory, safe to call with null pointers
+ */
+static void
+FreePrivateData(_THIS)
+{
+ if (!this->hidden) {
+ return;
+ }
+
+ if (this->hidden->waveBuf[0].data_vaddr) {
+ linearFree((void *) this->hidden->waveBuf[0].data_vaddr);
+ }
+
+ if (this->hidden->mixbuf) {
+ SDL_free(this->hidden->mixbuf);
+ this->hidden->mixbuf = NULL;
+ }
+
+ SDL_free(this->hidden);
+ this->hidden = NULL;
+}
+
+static int
+FindAudioFormat(_THIS)
+{
+ SDL_bool found_valid_format = SDL_FALSE;
+ Uint16 test_format = SDL_FirstAudioFormat(this->spec.format);
+
+ while (!found_valid_format && test_format) {
+ this->spec.format = test_format;
+ switch (test_format) {
+ case AUDIO_S8:
+ /* Signed 8-bit audio supported */
+ this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM8 : NDSP_FORMAT_MONO_PCM8;
+ this->hidden->isSigned = 1;
+ this->hidden->bytePerSample = this->spec.channels;
+ found_valid_format = SDL_TRUE;
+ break;
+ case AUDIO_S16:
+ /* Signed 16-bit audio supported */
+ this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16;
+ this->hidden->isSigned = 1;
+ this->hidden->bytePerSample = this->spec.channels * 2;
+ found_valid_format = SDL_TRUE;
+ break;
+ default:
+ test_format = SDL_NextAudioFormat();
+ break;
+ }
+ }
+
+ return found_valid_format ? 0 : -1;
+}
+
+#endif /* SDL_AUDIO_DRIVER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/audio/n3ds/SDL_n3dsaudio.h b/src/audio/n3ds/SDL_n3dsaudio.h
new file mode 100644
index 0000000..d01f17f
--- /dev/null
+++ b/src/audio/n3ds/SDL_n3dsaudio.h
@@ -0,0 +1,50 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef _SDL_n3dsaudio_h_
+#define _SDL_n3dsaudio_h_
+
+#include <3ds.h>
+
+/* Hidden "this" pointer for the audio functions */
+#define _THIS SDL_AudioDevice *this
+
+#define NUM_BUFFERS 2 /* -- Don't lower this! */
+
+struct SDL_PrivateAudioData
+{
+ /* Speaker data */
+ Uint8 *mixbuf;
+ Uint32 mixlen;
+ Uint32 format;
+ Uint32 samplerate;
+ Uint32 channels;
+ Uint8 bytePerSample;
+ Uint32 isSigned;
+ Uint32 nextbuf;
+ ndspWaveBuf waveBuf[NUM_BUFFERS];
+ LightLock lock;
+ CondVar cv;
+ SDL_bool isCancelled;
+};
+
+#endif /* _SDL_n3dsaudio_h_ */
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/cpuinfo/SDL_cpuinfo.c b/src/cpuinfo/SDL_cpuinfo.c
index 8221f77..6ba5c85 100644
--- a/src/cpuinfo/SDL_cpuinfo.c
+++ b/src/cpuinfo/SDL_cpuinfo.c
@@ -468,6 +468,8 @@ CPU_haveNEON(void)
return 1; /* ARMv8 always has non-optional NEON support. */
#elif __VITA__
return 1;
+#elif __3DS__
+ return 1;
#elif defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)
/* (note that sysctlbyname("hw.optional.neon") doesn't work!) */
return 1; /* all Apple ARMv7 chips and later have NEON. */
diff --git a/src/dynapi/SDL_dynapi.h b/src/dynapi/SDL_dynapi.h
index dc53e58..e326869 100644
--- a/src/dynapi/SDL_dynapi.h
+++ b/src/dynapi/SDL_dynapi.h
@@ -63,6 +63,8 @@
#define SDL_DYNAMIC_API 0 /* vitasdk doesn't support dynamic linking */
#elif defined(__NGAGE__)
#define SDL_DYNAMIC_API 0 /* The N-Gage doesn't support dynamic linking either */
+#elif defined(__3DS__)
+#define SDL_DYNAMIC_API 0 /* devkitARM doesn't support dynamic linking */
#elif defined(DYNAPI_NEEDS_DLOPEN) && !defined(HAVE_DLOPEN)
#define SDL_DYNAMIC_API 0 /* we need dlopen(), but don't have it.... */
#endif
diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c
index a93c2fb..ead504c 100644
--- a/src/file/SDL_rwops.c
+++ b/src/file/SDL_rwops.c
@@ -53,6 +53,10 @@
#include "cocoa/SDL_rwopsbundlesupport.h"
#endif /* __APPLE__ */
+#ifdef __3DS__
+#include "n3ds/SDL_rwopsromfs.h"
+#endif /* __3DS__ */
+
#ifdef __ANDROID__
#include "../core/android/SDL_android.h"
#include "SDL_system.h"
@@ -601,6 +605,8 @@ SDL_RWFromFile(const char *file, const char *mode)
#elif __WINRT__
FILE *fp = NULL;
fopen_s(&fp, file, mode);
+ #elif __3DS__
+ FILE *fp = N3DS_FileOpen(file, mode);
#else
FILE *fp = fopen(file, mode);
#endif
diff --git a/src/file/n3ds/SDL_rwopsromfs.c b/src/file/n3ds/SDL_rwopsromfs.c
new file mode 100644
index 0000000..fe92c3d
--- /dev/null
+++ b/src/file/n3ds/SDL_rwopsromfs.c
@@ -0,0 +1,56 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "SDL_rwopsromfs.h"
+
+/* Nintendo 3DS applications may embed resources in the executable. The
+ resources are stored in a special read-only partition prefixed with
+ 'romfs:/'. As such, when opening a file, we should first try the romfs
+ unless sdmc is specifically mentionned.
+*/
+FILE *
+N3DS_FileOpen(const char *file, const char *mode)
+{
+ FILE *fp = NULL;
+ char romfs_path[4096];
+
+ /* romfs are read-only */
+ if (SDL_strchr(mode, 'r') == NULL) {
+ return fopen(file, mode);
+ }
+
+ /* If the path has an explicit prefix, we skip the guess work */
+ if (SDL_strncmp("romfs:/", file, 7) == 0 ||
+ SDL_strncmp("sdmc:/", file, 6) == 0) {
+ return fopen(file, mode);
+ }
+
+ SDL_snprintf(romfs_path, 4096, "romfs:/%s", file);
+
+ fp = fopen(romfs_path, mode);
+ if (fp == NULL) {
+ fp = fopen(file, mode);
+ }
+
+ return fp;
+}
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/file/n3ds/SDL_rwopsromfs.h b/src/file/n3ds/SDL_rwopsromfs.h
new file mode 100644
index 0000000..9a6a734
--- /dev/null
+++ b/src/file/n3ds/SDL_rwopsromfs.h
@@ -0,0 +1,30 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifndef SDL_rwopsromfs_h_
+#define SDL_rwopsromfs_h_
+
+FILE *N3DS_FileOpen(const char *file, const char *mode);
+
+#endif /* SDL_rwopsromfs_h_ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/filesystem/n3ds/SDL_sysfilesystem.c b/src/filesystem/n3ds/SDL_sysfilesystem.c
new file mode 100644
index 0000000..e402e03
--- /dev/null
+++ b/src/filesystem/n3ds/SDL_sysfilesystem.c
@@ -0,0 +1,86 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_FILESYSTEM_N3DS
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+/* System dependent filesystem routines */
+
+#include <3ds.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include "SDL_error.h"
+#include "SDL_filesystem.h"
+
+SDL_FORCE_INLINE char *MakePrefPath(const char *app);
+SDL_FORCE_INLINE int CreatePrefPathDir(const char *pref);
+
+char *
+SDL_GetBasePath(void)
+{
+ char *base_path = SDL_strdup("romfs:/");
+ return base_path;
+}
+
+char *
+SDL_GetPrefPath(const char *org, const char *app)
+{
+ char *pref_path = NULL;
+ if (app == NULL) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+
+ pref_path = MakePrefPath(app);
+ if (CreatePrefPathDir(pref_path) < 0) {
+ SDL_free(pref_path);
+ return NULL;
+ }
+
+ return pref_path;
+}
+
+SDL_FORCE_INLINE char *
+MakePrefPath(const char *app)
+{
+ static const char *FMT = "/3ds/%s/";
+ size_t length = SDL_snprintf(NULL, 0, FMT, app) + 1;
+ char *pref_path = (char *) SDL_calloc(length, sizeof(char));
+ SDL_snprintf(pref_path, length, FMT, app);
+ return pref_path;
+}
+
+SDL_FORCE_INLINE int
+CreatePrefPathDir(const char *pref)
+{
+ int result = mkdir(pref, 0666);
+
+ if (result == -1 && errno != EEXIST) {
+ return SDL_SetError("Failed to create '%s' (%s)", pref, strerror(errno));
+ }
+ return 0;
+}
+
+#endif /* SDL_FILESYSTEM_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/joystick/SDL_gamecontrollerdb.h b/src/joystick/SDL_gamecontrollerdb.h
index fd8534f..a39a46d 100644
--- a/src/joystick/SDL_gamecontrollerdb.h
+++ b/src/joystick/SDL_gamecontrollerdb.h
@@ -978,6 +978,9 @@ static const char *s_ControllerMappings [] =
#if SDL_JOYSTICK_VITA
"0000000050535669746120436f6e7400,PSVita Controller,crc:d598,a:b2,b:b1,back:b10,dpdown:b6,dpleft:b7,dpright:b9,dpup:b8,leftshoulder:b4,leftstick:b14,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b15,righttrigger:a5,rightx:a2,righty:a3,start:b11,x:b3,y:b0,",
#endif
+#if SDL_JOYSTICK_N3DS
+ "4e696e74656e646f20334453206d6170,Nintendo 3DS,a:b0,b:b1,back:b2,dpdown:b7,dpleft:b5,dpright:b4,dpup:b6,leftshoulder:b9,leftstick:b14,leftx:a0,lefty:a1,rightshoulder:b8,rightstick:b15,rightx:a2,righty:a3,start:b3,x:b10,y:b11,",
+#endif
"hidapi,*,a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,",
NULL
};
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index a42951c..b22c878 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -102,6 +102,9 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
#ifdef SDL_JOYSTICK_VITA
&SDL_VITA_JoystickDriver,
#endif
+#ifdef SDL_JOYSTICK_N3DS
+ &SDL_N3DS_JoystickDriver
+#endif
#if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)
&SDL_DUMMY_JoystickDriver
#endif
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index a0400c6..707ab4a 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -238,6 +238,7 @@ extern SDL_JoystickDriver SDL_OS2_JoystickDriver;
extern SDL_JoystickDriver SDL_PS2_JoystickDriver;
extern SDL_JoystickDriver SDL_PSP_JoystickDriver;
extern SDL_JoystickDriver SDL_VITA_JoystickDriver;
+extern SDL_JoystickDriver SDL_N3DS_JoystickDriver;
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
new file mode 100644
index 0000000..f2f15c1
--- /dev/null
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -0,0 +1,367 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_JOYSTICK_N3DS
+
+/* This is the N3DS implementation of the SDL joystick API */
+
+#include <3ds.h>
+
+#include "../SDL_sysjoystick.h"
+#include "SDL_events.h"
+
+#define NB_BUTTONS 23
+
+/*
+ N3DS sticks values are roughly within +/-160
+ which is too small to pass the jitter tolerance.
+ This correction factor is applied to axis values
+ so they fit better in SDL's value range.
+*/
+#define CORRECTION_FACTOR_X SDL_JOYSTICK_AXIS_MAX / 160
+
+/*
+ The Y axis needs to be flipped because SDL's "up"
+ is reversed compared to libctru's "up"
+*/
+#define CORRECTION_FACTOR_Y -CORRECTION_FACTOR_X
+
+/*
+ Factors used to convert touchscreen coordinates to
+ SDL's 0-1 values. Note that the N3DS's screen is
+ internally in a portrait disposition so the
+ GSP_SCREEN constants are flipped.
+*/
+#define TOUCHPAD_SCALE_X 1.0f / GSP_SCREEN_HEIGHT_BOTTOM
+#define TOUCHPAD_SCALE_Y 1.0f / GSP_SCREEN_WIDTH
+
+typedef struct N3DSJoystickState
+{
+ u32 kDown;
+ u32 kUp;
+ circlePosition circlePos;
+ circlePosition cStickPos;
+ accelVector acceleration;
+ angularRate rate;
+} N3DSJoystickState;
+
+SDL_FORCE_INLINE void UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
+SDL_FORCE_INLINE void UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
+SDL_FORCE_INLINE void UpdateSensors(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
+SDL_FORCE_INLINE void UpdateTouch(SDL_Joystick *joystick);
+
+static N3DSJoystickState current_state;
+static SDL_bool sensors_enabled = SDL_FALSE;
+
+static int
+N3DS_JoystickInit(void)
+{
+ hidInit();
+ HIDUSER_EnableAccelerometer();
+ HIDUSER_EnableGyroscope();
+ return 0;
+}
+
+static const char *
+N3DS_JoystickGetDeviceName(int device_index)
+{
+ return "Nintendo 3DS";
+}
+
+static int
+N3DS_JoystickGetCount(void)
+{
+ return 1;
+}
+
+static SDL_JoystickGUID
+N3DS_JoystickGetDeviceGUID(int device_index)
+{
+ /* GUID corresponds to the name "Nintendo 3DS map" */
+ SDL_JoystickGUID guid = { { 0x4e, 0x69, 0x6e, 0x74, 0x65, 0x6e, 0x64, 0x6f, 0x20, 0x33, 0x44, 0x53, 0x20, 0x6d, 0x61, 0x70 } };
+ return guid;
+}
+
+static SDL_JoystickID
+N3DS_JoystickGetDeviceInstanceID(int device_index)
+{
+ return device_index;
+}
+
+static int
+N3DS_JoystickOpen(SDL_Joystick *joystick, int device_index)
+{
+ joystick->nbuttons = NB_BUTTONS;
+ joystick->naxes = 4;
+ joystick->nhats = 0;
+ joystick->instance_id = device_index;
+
+ SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 0.0f);
+ SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);
+ SDL_PrivateJoystickAddTouchpad(joystick, 1);
+
+ return 0;
+}
+
+static int
+N3DS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
+{
+ sensors_enabled = enabled;
+ return 0;
+}
+
+static void
+N3DS_JoystickUpdate(SDL_Joystick *joystick)
+{
+ N3DSJoystickState previous_state = current_state;
+ hidScanInput();
+
+ UpdateAxis(joystick, &previous_state);
+ UpdateButtons(joystick, &previous_state);
+ UpdateTouch(joystick);
+
+ if (sensors_enabled) {
+ UpdateSensors(joystick, &previous_state);
+ }
+}
+
+SDL_FORCE_INLINE void
+UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
+{
+ hidCircleRead(¤t_state.circlePos);
+ if (previous_state->circlePos.dx != current_state.circlePos.dx) {
+ SDL_PrivateJoystickAxis(joystick,
+ 0,
+ current_state.circlePos.dx * CORRECTION_FACTOR_X);
+ }
+ if (previous_state->circlePos.dy != current_state.circlePos.dy) {
+ SDL_PrivateJoystickAxis(joystick,
+ 1,
+ current_state.circlePos.dy * CORRECTION_FACTOR_Y);
+ }
+
+ hidCstickRead(¤t_state.cStickPos);
+ if (previous_state->cStickPos.dx != current_state.cStickPos.dx) {
+ SDL_PrivateJoystickAxis(joystick,
+ 2,
+ current_state.cStickPos.dx * CORRECTION_FACTOR_X);
+ }
+ if (previous_state->cStickPos.dy != current_state.cStickPos.dy) {
+ SDL_PrivateJoystickAxis(joystick,
+ 3,
+ current_state.cStickPos.dy * CORRECTION_FACTOR_Y);
+ }
+}
+
+SDL_FORCE_INLINE void
+UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
+{
+ u32 updated_down, updated_up;
+
+ current_state.kDown = hidKeysDown();
+ updated_down = previous_state->kDown ^ current_state.kDown;
+ if (updated_down) {
+ for (Uint8 i = 0; i < joystick->nbuttons; i++) {
+ if (current_state.kDown & BIT(i) & updated_down) {
+ SDL_PrivateJoystickButton(joystick, i, SDL_PRESSED);
+ }
+ }
+ }
+
+ current_state.kUp = hidKeysUp();
+ updated_up = previous_state->kUp ^ current_state.kUp;
+ if (updated_up) {
+ for (Uint8 i = 0; i < joystick->nbuttons; i++) {
+ if (current_state.kUp & BIT(i) & updated_up) {
+ SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED);
+ }
+ }
+ }
+}
+
+SDL_FORCE_INLINE void
+UpdateTouch(SDL_Joystick *joystick)
+{
+ touchPosition touch;
+ Uint8 state;
+ hidTouchRead(&touch);
+ state = (touch.px == 0 && touch.py == 0) ? SDL_RELEASED : SDL_PRESSED;
+
+ SDL_PrivateJoystickTouchpad(joystick,
+ 0,
+ 0,
+ state,
+ touch.px * TOUCHPAD_SCALE_X,
+ touch.py * TOUCHPAD_SCALE_Y,
+ state == SDL_PRESSED ? 1.0f : 0.0f);
+}
+
+SDL_FORCE_INLINE void
+UpdateSensors(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
+{
+ float data[3];
+
+ hidAccelRead(¤t_state.acceleration);
+ if (SDL_memcmp(&previous_state->acceleration, ¤t_state.acceleration, sizeof(accelVector)) != 0) {
+ SDL_memcpy(&previous_state->acceleration, ¤t_state.acceleration, sizeof(accelVector));
+ data[0] = (float) current_state.acceleration.x * SDL_STANDARD_GRAVITY;
+ data[1] = (float) current_state.acceleration.y * SDL_STANDARD_GRAVITY;
+ data[2] = (float) current_state.acceleration.z * SDL_STANDARD_GRAVITY;
+ SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_ACCEL, data, sizeof data);
+ }
+
+ hidGyroRead(¤t_state.rate);
+ if (SDL_memcmp(&previous_state->rate, ¤t_state.rate, sizeof(angularRate)) != 0) {
+ SDL_memcpy(&previous_state->rate, ¤t_state.rate, sizeof(angularRate));
+ data[0] = (float) current_state.rate.y;
+ data[1] = (float) current_state.rate.z;
+ data[2] = (float) current_state.rate.x;
+ SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, data, sizeof data);
+ }
+}
+
+static void
+N3DS_JoystickClose(SDL_Joystick *joystick)
+{
+}
+
+static void
+N3DS_JoystickQuit(void)
+{
+ HIDUSER_DisableGyroscope();
+ HIDUSER_DisableAccelerometer();
+ hidExit();
+}
+
+static SDL_bool
+N3DS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+ /* There is only one possible mapping. */
+ *out = (SDL_GamepadMapping){
+ .a = { EMappingKind_Button, 0 },
+ .b = { EMappingKind_Button, 1 },
+ .x = { EMappingKind_Button, 10 },
+ .y = { EMappingKind_Button, 11 },
+ .back = { EMappingKind_Button, 2 },
+ .guide = { EMappingKind_None, 255 },
+ .start = { EMappingKind_Button, 3 },
+ .leftstick = { EMappingKind_None, 255 },
+ .rightstick = { EMappingKind_None, 255 },
+ .leftshoulder = { EMappingKind_Button, 9 },
+ .rightshoulder = { EMappingKind_Button, 8 },
+ .dpup = { EMappingKind_Button, 6 },
+ .dpdown = { EMappingKind_Button, 7 },
+ .dpleft = { EMappingKind_Button, 5 },
+ .dpright = { EMappingKind_Button, 4 },
+ .misc1 = { EMappingKind_None, 255 },
+ .paddle1 = { EMappingKind_None, 255 },
+ .paddle2 = { EMappingKind_None, 255 },
+ .paddle3 = { EMappingKind_None, 255 },
+ .paddle4 = { EMappingKind_None, 255 },
+ .leftx = { EMappingKind_Axis, 0 },
+ .lefty = { EMappingKind_Axis, 1 },
+ .rightx = { EMappingKind_Axis, 2 },
+ .righty = { EMappingKind_Axis, 3 },
+ .lefttrigger = { EMappingKind_Button, 14 },
+ .righttrigger = { EMappingKind_Button, 15 },
+ };
+ return SDL_TRUE;
+}
+
+static void
+N3DS_JoystickDetect(void)
+{
+}
+
+static const char *
+N3DS_JoystickGetDevicePath(int device_index)
+{
+ return NULL;
+}
+
+static int
+N3DS_JoystickGetDevicePlayerIndex(int device_index)
+{
+ return -1;
+}
+
+static void
+N3DS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
+{
+}
+
+static Uint32
+N3DS_JoystickGetCapabilities(SDL_Joystick *joystick)
+{
+ return 0;
+}
+
+static int
+N3DS_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+{
+ return SDL_Unsupported();
+}
+
+static int
+N3DS_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
+{
+ return SDL_Unsupported();
+}
+
+static int
+N3DS_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
+{
+ return SDL_Unsupported();
+}
+
+static int
+N3DS_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
+{
+ return SDL_Unsupported();
+}
+
+SDL_JoystickDriver SDL_N3DS_JoystickDriver = {
+ N3DS_JoystickInit,
+ N3DS_JoystickGetCount,
+ N3DS_JoystickDetect,
+ N3DS_JoystickGetDeviceName,
+ N3DS_JoystickGetDevicePath,
+ N3DS_JoystickGetDevicePlayerIndex,
+ N3DS_JoystickSetDevicePlayerIndex,
+ N3DS_JoystickGetDeviceGUID,
+ N3DS_JoystickGetDeviceInstanceID,
+ N3DS_JoystickOpen,
+ N3DS_JoystickRumble,
+ N3DS_JoystickRumbleTriggers,
+ N3DS_JoystickGetCapabilities,
+ N3DS_JoystickSetLED,
+ N3DS_JoystickSendEffect,
+ N3DS_JoystickSetSensorsEnabled,
+ N3DS_JoystickUpdate,
+ N3DS_JoystickClose,
+ N3DS_JoystickQuit,
+ N3DS_JoystickGetGamepadMapping
+};
+
+#endif /* SDL_JOYSTICK_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/libm/math_private.h b/src/libm/math_private.h
index f560cca..ee67c44 100644
--- a/src/libm/math_private.h
+++ b/src/libm/math_private.h
@@ -27,7 +27,7 @@
#define libm_hidden_def(x)
#define strong_alias(x, y)
-#if !defined(__HAIKU__) && !defined(__PSP__) && !defined(__PS2__) /* already defined in a system header. */
+#if !defined(__HAIKU__) && !defined(__PSP__) && !defined(__3DS__) && !defined(__PS2__) /* already defined in a system header. */
typedef unsigned int u_int32_t;
#endif
diff --git a/src/locale/n3ds/SDL_syslocale.c b/src/locale/n3ds/SDL_syslocale.c
new file mode 100644
index 0000000..bcfec8f
--- /dev/null
+++ b/src/locale/n3ds/SDL_syslocale.c
@@ -0,0 +1,59 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../SDL_syslocale.h"
+#include "../../SDL_internal.h"
+
+#include <3ds.h>
+
+/* Used when the CFGU fails to work. */
+#define BAD_LOCALE 255
+
+SDL_FORCE_INLINE u8 GetLocaleIndex(void);
+
+void
+SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
+{
+ /* The 3DS only supports these 12 languages, only one can be active at a time */
+ static const char AVAILABLE_LOCALES[][6] = { "ja_JP", "en_US", "fr_FR", "de_DE",
+ "it_IT", "es_ES", "zn_CN", "ko_KR",
+ "nl_NL", "pt_PT", "ru_RU", "zh_TW" };
+ u8 current_locale = GetLocaleIndex();
+ if (current_locale != BAD_LOCALE) {
+ SDL_strlcpy(buf, AVAILABLE_LOCALES[current_locale], buflen);
+ }
+}
+
+SDL_FORCE_INLINE u8
+GetLocaleIndex(void)
+{
+ u8 current_locale;
+ if (R_FAILED(cfguInit())) {
+ return BAD_LOCALE;
+ }
+ if (R_FAILED(CFGU_GetSystemLanguage(¤t_locale))) {
+ return BAD_LOCALE;
+ }
+ cfguExit();
+ return current_locale;
+}
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/main/n3ds/SDL_n3ds_main.c b/src/main/n3ds/SDL_n3ds_main.c
new file mode 100644
index 0000000..982dcb3
--- /dev/null
+++ b/src/main/n3ds/SDL_n3ds_main.c
@@ -0,0 +1,82 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../../SDL_internal.h"
+
+#ifdef __3DS__
+
+#include "SDL_main.h"
+#include <3ds.h>
+
+#ifdef main
+#undef main
+#endif
+
+SDL_FORCE_INLINE void N3DS_Init(void);
+SDL_FORCE_INLINE void N3DS_SetCPUSpeed(void);
+SDL_FORCE_INLINE void N3DS_Quit(void);
+
+#define HIGH_CLOCK 1
+#define L2_CACHE 2
+
+int
+main(int argc, char *argv[])
+{
+ int result;
+ N3DS_Init();
+ SDL_SetMainReady();
+ result = SDL_main(argc, argv);
+ N3DS_Quit();
+ return result;
+}
+
+SDL_FORCE_INLINE void
+N3DS_Init(void)
+{
+ N3DS_SetCPUSpeed();
+ romfsInit();
+ gfxInit(GSP_RGBA8_OES, GSP_RGBA8_OES, false);
+ hidInit();
+}
+
+/* If available, enable L2 cache and high CPU clock */
+SDL_FORCE_INLINE void
+N3DS_SetCPUSpeed(void)
+{
+ if (R_SUCCEEDED(ptmSysmInit())) {
+ if (R_SUCCEEDED(PTMSYSM_CheckNew3DS())) {
+ PTMSYSM_ConfigureNew3DSCPU(HIGH_CLOCK | L2_CACHE);
+ }
+ ptmSysmExit();
+ }
+}
+
+SDL_FORCE_INLINE void
+N3DS_Quit(void)
+{
+ hidExit();
+ gfxExit();
+ romfsExit();
+}
+
+#endif /* __3DS__ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/power/SDL_power.c b/src/power/SDL_power.c
index 625a8fa..fbf8d5a 100644
--- a/src/power/SDL_power.c
+++ b/src/power/SDL_power.c
@@ -71,6 +71,9 @@ static SDL_GetPowerInfo_Impl implementations[] = {
#ifdef SDL_POWER_VITA /* handles PSVita. */
SDL_GetPowerInfo_VITA,
#endif
+#ifdef SDL_POWER_N3DS /* handles N3DS. */
+ SDL_GetPowerInfo_N3DS,
+#endif
#ifdef SDL_POWER_WINRT /* handles WinRT */
SDL_GetPowerInfo_WinRT,
#endif
diff --git a/src/power/SDL_syspower.h b/src/power/SDL_syspower.h
index d66e50f..2f81756 100644
--- a/src/power/SDL_syspower.h
+++ b/src/power/SDL_syspower.h
@@ -39,6 +39,7 @@ SDL_bool SDL_GetPowerInfo_Haiku(SDL_PowerState *, int *, int *);
SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *);
SDL_bool SDL_GetPowerInfo_PSP(SDL_PowerState *, int *, int *);
SDL_bool SDL_GetPowerInfo_VITA(SDL_PowerState *, int *, int *);
+SDL_bool SDL_GetPowerInfo_N3DS(SDL_PowerState *, int *, int *);
SDL_bool SDL_GetPowerInfo_WinRT(SDL_PowerState *, int *, int *);
SDL_bool SDL_GetPowerInfo_Emscripten(SDL_PowerState *, int *, int *);
diff --git a/src/power/n3ds/SDL_syspower.c b/src/power/n3ds/SDL_syspower.c
new file mode 100644
index 0000000..fe167af
--- /dev/null
+++ b/src/power/n3ds/SDL_syspower.c
@@ -0,0 +1,111 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../../SDL_internal.h"
+
+#if !defined(SDL_POWER_DISABLED) && defined(SDL_POWER_N3DS)
+
+#include <3ds.h>
+
+#include "SDL_error.h"
+#include "SDL_power.h"
+
+SDL_FORCE_INLINE SDL_PowerState GetPowerState(void);
+SDL_FORCE_INLINE int ReadStateFromPTMU(bool *is_plugged, u8 *is_charging);
+SDL_FORCE_INLINE int GetBatteryPercentage(void);
+
+#define BATTERY_PERCENT_REG 0xB
+#define BATTERY_PERCENT_REG_SIZE 2
+
+SDL_bool
+SDL_GetPowerInfo_N3DS(SDL_PowerState *state, int *seconds, int *percent)
+{
+ *state = GetPowerState();
+ *percent = GetBatteryPercentage();
+ *seconds = -1; /* libctru doesn't provide a way to estimate battery life */
+
+ return SDL_TRUE;
+}
+
+SDL_FORCE_INLINE SDL_PowerState
+GetPowerState(void)
+{
+ bool is_plugged;
+ u8 is_charging;
+
+ if (ReadStateFromPTMU(&is_plugged, &is_charging) < 0) {
+ return SDL_POWERSTATE_UNKNOWN;
+ }
+
+ if (is_charging) {
+ return SDL_POWERSTATE_CHARGING;
+ }
+
+ if (is_plugged) {
+ return SDL_POWERSTATE_CHARGED;
+ }
+
+ return SDL_POWERSTATE_ON_BATTERY;
+}
+
+SDL_FORCE_INLINE int
+ReadStateFromPTMU(bool *is_plugged, u8 *is_charging)
+{
+ if (R_FAILED(ptmuInit())) {
+ return SDL_SetError("Failed to initialise PTMU service");
+ }
+
+ if (R_FAILED(PTMU_GetAdapterState(is_plugged))) {
+ ptmuExit();
+ return SDL_SetError("Failed to read adapter state");
+ }
+
+ if (R_FAILED(PTMU_GetBatteryChargeState(is_charging))) {
+ ptmuExit();
+ return SDL_SetError("Failed to read battery charge state");
+ }
+
+ ptmuExit();
+ return 0;
+}
+
+SDL_FORCE_INLINE int
+GetBatteryPercentage(void)
+{
+ u8 data[BATTERY_PERCENT_REG_SIZE];
+
+ if (R_FAILED(mcuHwcInit())) {
+ return SDL_SetError("Failed to initialise mcuHwc service");
+ }
+
+ if (R_FAILED(MCUHWC_ReadRegister(BATTERY_PERCENT_REG, data, BATTERY_PERCENT_REG_SIZE))) {
+ mcuHwcExit();
+ return SDL_SetError("Failed to read battery register");
+ }
+
+ mcuHwcExit();
+
+ return (int) SDL_round(data[0] + data[1] / 256.0);
+}
+
+#endif /* !SDL_POWER_DISABLED && SDL_POWER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/thread/SDL_thread_c.h b/src/thread/SDL_thread_c.h
index 55a4a88..f3348ea 100644
--- a/src/thread/SDL_thread_c.h
+++ b/src/thread/SDL_thread_c.h
@@ -38,6 +38,8 @@
#include "psp/SDL_systhread_c.h"
#elif SDL_THREAD_VITA
#include "vita/SDL_systhread_c.h"
+#elif SDL_THREAD_N3DS
+#include "n3ds/SDL_systhread_c.h"
#elif SDL_THREAD_STDCPP
#include "stdcpp/SDL_systhread_c.h"
#elif SDL_THREAD_OS2
diff --git a/src/thread/n3ds/SDL_syscond.c b/src/thread/n3ds/SDL_syscond.c
new file mode 100644
index 0000000..9c671f5
--- /dev/null
+++ b/src/thread/n3ds/SDL_syscond.c
@@ -0,0 +1,133 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_THREAD_N3DS
+
+/* An implementation of condition variables using libctru's CondVar */
+
+#include "SDL_sysmutex_c.h"
+
+struct SDL_cond
+{
+ CondVar cond_variable;
+};
+
+/* Create a condition variable */
+SDL_cond *
+SDL_CreateCond(void)
+{
+ SDL_cond *cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
+ if (cond) {
+ CondVar_Init(&cond->cond_variable);
+ } else {
+ SDL_OutOfMemory();
+ }
+ return cond;
+}
+
+/* Destroy a condition variable */
+void
+SDL_DestroyCond(SDL_cond *cond)
+{
+ if (cond) {
+ SDL_free(cond);
+ }
+}
+
+/* Restart one of the threads that are waiting on the condition variable */
+int
+SDL_CondSignal(SDL_cond *cond)
+{
+ if (!cond) {
+ return SDL_SetError("Passed a NULL condition variable");
+ }
+
+ CondVar_Signal(&cond->cond_variable);
+ return 0;
+}
+
+/* Restart all threads that are waiting on the condition variable */
+int
+SDL_CondBroadcast(SDL_cond *cond)
+{
+ if (!cond) {
+ return SDL_SetError("Passed a NULL condition variable");
+ }
+
+ CondVar_Broadcast(&cond->cond_variable);
+ return 0;
+}
+
+/* Wait on the condition variable for at most 'ms' milliseconds.
+ The mutex must be locked before entering this function!
+ The mutex is unlocked during the wait, and locked again after the wait.
+
+Typical use:
+
+Thread A:
+ SDL_LockMutex(lock);
+ while ( ! condition ) {
+ SDL_CondWait(cond, lock);
+ }
+ SDL_UnlockMutex(lock);
+
+Thread B:
+ SDL_LockMutex(lock);
+ ...
+ condition = true;
+ ...
+ SDL_CondSignal(cond);
+ SDL_UnlockMutex(lock);
+ */
+int
+SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
+{
+ Result res;
+
+ if (!cond) {
+ return SDL_SetError("Passed a NULL condition variable");
+ }
+ if (!mutex) {
+ return SDL_SetError("Passed a NULL mutex");
+ }
+
+ res = 0;
+ if (ms == SDL_MUTEX_MAXWAIT) {
+ CondVar_Wait(&cond->cond_variable, &mutex->lock.lock);
+ } else {
+ res = CondVar_WaitTimeout(&cond->cond_variable, &mutex->lock.lock,
+ (s64) ms * 1000000LL);
+ }
+
+ return R_SUCCEEDED(res) ? 0 : SDL_MUTEX_TIMEDOUT;
+}
+
+/* Wait on the condition variable forever */
+int
+SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
+{
+ return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
+}
+
+#endif /* SDL_THREAD_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/thread/n3ds/SDL_sysmutex.c b/src/thread/n3ds/SDL_sysmutex.c
new file mode 100644
index 0000000..59fa8a8
--- /dev/null
+++ b/src/thread/n3ds/SDL_sysmutex.c
@@ -0,0 +1,93 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_THREAD_N3DS
+
+/* An implementation of mutexes using libctru's RecursiveLock */
+
+#include "SDL_sysmutex_c.h"
+
+/* Create a mutex */
+SDL_mutex *
+SDL_CreateMutex(void)
+{
+ SDL_mutex *mutex;
+
+ /* Allocate mutex memory */
+ mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
+ if (mutex) {
+ RecursiveLock_Init(&mutex->lock);
+ } else {
+ SDL_OutOfMemory();
+ }
+ return mutex;
+}
+
+/* Free the mutex */
+void
+SDL_DestroyMutex(SDL_mutex *mutex)
+{
+ if (mutex) {
+ SDL_free(mutex);
+ }
+}
+
+/* Lock the mutex */
+int
+SDL_LockMutex(SDL_mutex *mutex)
+{
+ if (mutex == NULL) {
+ return SDL_SetError("Passed a NULL mutex");
+ }
+
+ RecursiveLock_Lock(&mutex->lock);
+
+ return 0;
+}
+
+/* try Lock the mutex */
+int
+SDL_TryLockMutex(SDL_mutex *mutex)
+{
+ if (mutex == NULL) {
+ return SDL_SetError("Passed a NULL mutex");
+ }
+
+ return RecursiveLock_TryLock(&mutex->lock);
+}
+
+/* Unlock the mutex */
+int
+SDL_mutexV(SDL_mutex *mutex)
+{
+ if (mutex == NULL) {
+ return SDL_SetError("Passed a NULL mutex");
+ }
+
+ RecursiveLock_Unlock(&mutex->lock);
+
+ return 0;
+}
+
+#endif /* SDL_THREAD_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/thread/n3ds/SDL_sysmutex_c.h b/src/thread/n3ds/SDL_sysmutex_c.h
new file mode 100644
index 0000000..d7658e1
--- /dev/null
+++ b/src/thread/n3ds/SDL_sysmutex_c.h
@@ -0,0 +1,37 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifndef SDL_sysmutex_c_h_
+#define SDL_sysmutex_c_h_
+
+#include <3ds.h>
+
+#include "SDL_mutex.h"
+
+struct SDL_mutex
+{
+ RecursiveLock lock;
+};
+
+#endif /* SDL_sysmutex_c_h */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/thread/n3ds/SDL_syssem.c b/src/thread/n3ds/SDL_syssem.c
new file mode 100644
index 0000000..08fbb6f
--- /dev/null
+++ b/src/thread/n3ds/SDL_syssem.c
@@ -0,0 +1,134 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_THREAD_N3DS
+
+/* An implementation of semaphores using libctru's LightSemaphore */
+
+#include <3ds.h>
+
+#include "SDL_thread.h"
+
+struct SDL_semaphore
+{
+ LightSemaphore semaphore;
+};
+
+SDL_sem *
+SDL_CreateSemaphore(Uint32 initial_value)
+{
+ SDL_sem *sem;
+
+ if (initial_value > SDL_MAX_SINT16) {
+ SDL_SetError("Initial semaphore value too high for this platform");
+ return NULL;
+ }
+
+ sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
+ if (!sem) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
+
+ LightSemaphore_Init(&sem->semaphore, initial_value, SDL_MAX_SINT16);
+
+ return sem;
+}
+
+/* WARNING:
+ You cannot call this function when another thread is using the semaphore.
+*/
+void
+SDL_DestroySemaphore(SDL_sem *sem)
+{
+ if (sem) {
+ SDL_free(sem);
+ }
+}
+
+int
+SDL_SemTryWait(SDL_sem *sem)
+{
+ if (!sem) {
+ return SDL_SetError("Passed a NULL semaphore");
+ }
+
+ return SDL_SemWaitTimeout(sem, 0);
+}
+
+int
+SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
+{
+ int retval;
+
+ if (!sem) {
+ return SDL_SetError("Passed a NULL semaphore");
+ }
+
+ if (timeout == SDL_MUTEX_MAXWAIT) {
+ LightSemaphore_Acquire(&sem->semaphore, 1);
+ retval = 0;
+ } else {
+ int return_code = LightSemaphore_TryAcquire(&sem->semaphore, 1);
+ if (return_code != 0) {
+ for (u32 i = 0; i < timeout; i++) {
+ svcSleepThread(1000000LL);
+ return_code = LightSemaphore_TryAcquire(&sem->semaphore, 1);
+ if (return_code == 0) {
+ break;
+ }
+ }
+ }
+ retval = return_code != 0 ? SDL_MUTEX_TIMEDOUT : 0;
+ }
+
+ return retval;
+}
+
+int
+SDL_SemWait(SDL_sem *sem)
+{
+ return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
+}
+
+Uint32
+SDL_SemValue(SDL_sem *sem)
+{
+ if (!sem) {
+ return SDL_SetError("Passed a NULL semaphore");
+ }
+ return sem->semaphore.current_count;
+}
+
+int
+SDL_SemPost(SDL_sem *sem)
+{
+ if (!sem) {
+ return SDL_SetError("Passed a NULL semaphore");
+ }
+ LightSemaphore_Release(&sem->semaphore, 1);
+ return 0;
+}
+
+#endif /* SDL_THREAD_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/thread/n3ds/SDL_systhread.c b/src/thread/n3ds/SDL_systhread.c
new file mode 100644
index 0000000..a588400
--- /dev/null
+++ b/src/thread/n3ds/SDL_systhread.c
@@ -0,0 +1,148 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_THREAD_N3DS
+
+/* Thread management routines for SDL */
+
+#include "../SDL_systhread.h"
+
+/* N3DS has very limited RAM (128MB), so we put a limit on thread stack size. */
+#define N3DS_THREAD_STACK_SIZE_MAX (16 * 1024)
+#define N3DS_THREAD_STACK_SIZE_DEFAULT (4 * 1024)
+
+#define N3DS_THREAD_PRIORITY_LOW 0x3F /**< Minimum priority */
+#define N3DS_THREAD_PRIORITY_MEDIUM 0x2F /**< Slightly higher than main thread (0x30) */
+#define N3DS_THREAD_PRIORITY_HIGH 0x19 /**< High priority for non-video work */
+#define N3DS_THREAD_PRIORITY_TIME_CRITICAL 0x18 /**< Highest priority */
+
+static size_t GetStackSize(size_t requested_size);
+
+static void
+ThreadEntry(void *arg)
+{
+ SDL_RunThread((SDL_Thread *) arg);
+ threadExit(0);
+}
+
+#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
+#error "SDL_PASSED_BEGINTHREAD_ENDTHREAD is not supported on N3DS"
+#endif
+
+int
+SDL_SYS_CreateThread(SDL_Thread *thread)
+{
+ s32 priority = N3DS_THREAD_PRIORITY_MEDIUM;
+ size_t stack_size = GetStackSize(thread->stacksize);
+
+ thread->handle = threadCreate(ThreadEntry,
+ thread,
+ stack_size,
+ priority,
+ -1,
+ false);
+
+ if (thread->handle == NULL) {
+ return SDL_SetError("Couldn't create thread");
+ }
+
+ return 0;
+}
+
+static size_t
+GetStackSize(size_t requested_size)
+{
+ if (requested_size == 0) {
+ return N3DS_THREAD_STACK_SIZE_DEFAULT;
+ }
+
+ if (requested_size > N3DS_THREAD_STACK_SIZE_MAX) {
+ SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM,
+ "Requested a thread size of %zu,"
+ " falling back to the maximum supported of %zu\n",
+ requested_size,
+ N3DS_THREAD_STACK_SIZE_MAX);
+ requested_size = N3DS_THREAD_STACK_SIZE_MAX;
+ }
+ return requested_size;
+}
+
+void
+SDL_SYS_SetupThread(const char *name)
+{
+ return;
+}
+
+SDL_threadID
+SDL_ThreadID(void)
+{
+ u32 thread_ID = 0;
+ svcGetThreadId(&thread_ID, CUR_THREAD_HANDLE);
+ return (SDL_threadID) thread_ID;
+}
+
+int
+SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority)
+{
+ s32 svc_priority;
+ switch (sdl_priority) {
+ case SDL_THREAD_PRIORITY_LOW:
+ svc_priority = N3DS_THREAD_PRIORITY_LOW;
+ break;
+ case SDL_THREAD_PRIORITY_NORMAL:
+ svc_priority = N3DS_THREAD_PRIORITY_MEDIUM;
+ break;
+ case SDL_THREAD_PRIORITY_HIGH:
+ svc_priority = N3DS_THREAD_PRIORITY_HIGH;
+ break;
+ case SDL_THREAD_PRIORITY_TIME_CRITICAL:
+ svc_priority = N3DS_THREAD_PRIORITY_TIME_CRITICAL;
+ break;
+ default:
+ svc_priority = N3DS_THREAD_PRIORITY_MEDIUM;
+ }
+ return (int) svcSetThreadPriority(CUR_THREAD_HANDLE, svc_priority);
+}
+
+void
+SDL_SYS_WaitThread(SDL_Thread *thread)
+{
+ Result res = threadJoin(thread->handle, U64_MAX);
+
+ /*
+ Detached threads can be waited on, but should NOT be cleaned manually
+ as it would result in a fatal error.
+ */
+ if (R_SUCCEEDED(res) && SDL_AtomicGet(&thread->state) != SDL_THREAD_STATE_DETACHED) {
+ threadFree(thread->handle);
+ }
+}
+
+void
+SDL_SYS_DetachThread(SDL_Thread *thread)
+{
+ threadDetach(thread->handle);
+}
+
+#endif /* SDL_THREAD_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/thread/n3ds/SDL_systhread_c.h b/src/thread/n3ds/SDL_systhread_c.h
new file mode 100644
index 0000000..71b9a27
--- /dev/null
+++ b/src/thread/n3ds/SDL_systhread_c.h
@@ -0,0 +1,32 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifndef SDL_systhread_c_h_
+#define SDL_systhread_c_h_
+
+#include <3ds.h>
+
+typedef Thread SYS_ThreadHandle;
+
+#endif /* SDL_systhread_c_h_ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/timer/n3ds/SDL_systimer.c b/src/timer/n3ds/SDL_systimer.c
new file mode 100644
index 0000000..6a4d29b
--- /dev/null
+++ b/src/timer/n3ds/SDL_systimer.c
@@ -0,0 +1,81 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_TIMER_N3DS
+
+#include <3ds.h>
+
+static SDL_bool ticks_started = SDL_FALSE;
+static u64 start_tick;
+
+#define NSEC_PER_MSEC 1000000ULL
+
+void
+SDL_TicksInit(void)
+{
+ if (ticks_started) {
+ return;
+ }
+ ticks_started = SDL_TRUE;
+
+ start_tick = svcGetSystemTick();
+}
+
+void
+SDL_TicksQuit(void)
+{
+ ticks_started = SDL_FALSE;
+}
+
+Uint64
+SDL_GetTicks64(void)
+{
+ u64 elapsed;
+ if (!ticks_started) {
+ SDL_TicksInit();
+ }
+
+ elapsed = svcGetSystemTick() - start_tick;
+ return elapsed / CPU_TICKS_PER_MSEC;
+}
+
+Uint64
+SDL_GetPerformanceCounter(void)
+{
+ return svcGetSystemTick();
+}
+
+Uint64
+SDL_GetPerformanceFrequency(void)
+{
+ return SYSCLOCK_ARM11;
+}
+
+void
+SDL_Delay(Uint32 ms)
+{
+ svcSleepThread(ms * NSEC_PER_MSEC);
+}
+
+#endif /* SDL_TIMER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 7202f1d..4bc1f11 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -461,6 +461,7 @@ extern VideoBootStrap PS2_bootstrap;
extern VideoBootStrap PSP_bootstrap;
extern VideoBootStrap VITA_bootstrap;
extern VideoBootStrap RISCOS_bootstrap;
+extern VideoBootStrap N3DS_bootstrap;
extern VideoBootStrap RPI_bootstrap;
extern VideoBootStrap KMSDRM_bootstrap;
extern VideoBootStrap KMSDRM_LEGACY_bootstrap;
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 139c550..fbeaaaa 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -100,6 +100,9 @@ static VideoBootStrap *bootstrap[] = {
#if SDL_VIDEO_DRIVER_VITA
&VITA_bootstrap,
#endif
+#if SDL_VIDEO_DRIVER_N3DS
+ &N3DS_bootstrap,
+#endif
#if SDL_VIDEO_DRIVER_KMSDRM
&KMSDRM_bootstrap,
#endif
@@ -1498,7 +1501,7 @@ SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool fullscreen)
}
#define CREATE_FLAGS \
- (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_SKIP_TASKBAR | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL)
+ (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_SKIP_TASKBAR | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL | SDL_WINDOW_N3DS_BOTTOM)
static SDL_INLINE SDL_bool
IsAcceptingDragAndDrop(void)
diff --git a/src/video/n3ds/SDL_n3dsevents.c b/src/video/n3ds/SDL_n3dsevents.c
new file mode 100644
index 0000000..f54d68e
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsevents.c
@@ -0,0 +1,45 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_VIDEO_DRIVER_N3DS
+
+/* Pumping the events for the Home and Power buttons. */
+
+#include <3ds.h>
+
+#include "../../events/SDL_events_c.h"
+#include "SDL_n3dsevents_c.h"
+
+void
+N3DS_PumpEvents(_THIS)
+{
+ if (!aptMainLoop()) {
+ SDL_Event ev;
+ ev.type = SDL_QUIT;
+ SDL_PushEvent(&ev);
+ return;
+ }
+}
+
+#endif /* SDL_VIDEO_DRIVER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsevents_c.h b/src/video/n3ds/SDL_n3dsevents_c.h
new file mode 100644
index 0000000..418368c
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsevents_c.h
@@ -0,0 +1,31 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_n3dsevents_c_h_
+#define SDL_n3dsevents_c_h_
+
+#include "../../SDL_internal.h"
+
+extern void N3DS_PumpEvents(_THIS);
+
+#endif /* SDL_n3dsevents_c_h_ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsframebuffer.c b/src/video/n3ds/SDL_n3dsframebuffer.c
new file mode 100644
index 0000000..ba4ec51
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsframebuffer.c
@@ -0,0 +1,148 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_VIDEO_DRIVER_N3DS
+
+#include "../SDL_sysvideo.h"
+#include "SDL_n3dsframebuffer_c.h"
+#include "SDL_n3dsvideo.h"
+
+#define N3DS_SURFACE "_SDL_N3DSSurface"
+
+typedef struct
+{
+ int width, height;
+} Dimensions;
+
+SDL_FORCE_INLINE void FreePreviousWindowFramebuffer(SDL_Window *window);
+SDL_FORCE_INLINE SDL_Surface *CreateNewWindowFramebuffer(SDL_Window *window);
+SDL_FORCE_INLINE void CopyFramebuffertoN3DS(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim);
+SDL_FORCE_INLINE int GetDestOffset(int x, int y, int dest_width);
+SDL_FORCE_INLINE int GetSourceOffset(int x, int y, int source_width);
+SDL_FORCE_INLINE void FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen);
+
+int
+SDL_N3DS_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
+{
+ SDL_Surface *framebuffer;
+
+ FreePreviousWindowFramebuffer(window);
+ framebuffer = CreateNewWindowFramebuffer(window);
+
+ if (!framebuffer) {
+ return SDL_OutOfMemory();
+ }
+
+ SDL_SetWindowData(window, N3DS_SURFACE, framebuffer);
+ *format = FRAMEBUFFER_FORMAT;
+ *pixels = framebuffer->pixels;
+ *pitch = framebuffer->pitch;
+ return 0;
+}
+
+SDL_FORCE_INLINE void
+FreePreviousWindowFramebuffer(SDL_Window *window)
+{
+ SDL_Surface *surface = (SDL_Surface *) SDL_GetWindowData(window, N3DS_SURFACE);
+ SDL_FreeSurface(surface);
+}
+
+SDL_FORCE_INLINE SDL_Surface *
+CreateNewWindowFramebuffer(SDL_Window *window)
+{
+ int w, h, bpp;
+ Uint32 Rmask, Gmask, Bmask, Amask;
+ SDL_PixelFormatEnumToMasks(FRAMEBUFFER_FORMAT, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
+ SDL_GetWindowSize(window, &w, &h);
+ return SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
+}
+
+int
+SDL_N3DS_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
+{
+ SDL_WindowData *drv_data = (SDL_WindowData *) window->driverdata;
+ SDL_Surface *surface;
+ u16 width, height;
+ u32 *framebuffer;
+ u32 bufsize;
+
+ surface = (SDL_Surface *) SDL_GetWindowData(window, N3DS_SURFACE);
+ if (!surface) {
+ return SDL_SetError("%s: Unable to get the window surface.", __func__);
+ }
+
+ /* Get the N3DS internal framebuffer and its size */
+ framebuffer = (u32 *) gfxGetFramebuffer(drv_data->screen, GFX_LEFT, &width, &height);
+ bufsize = width * height * 4;
+
+ CopyFramebuffertoN3DS(framebuffer, (Dimensions){ width, height },
+ surface->pixels, (Dimensions){ surface->w, surface->h });
+ FlushN3DSBuffer(framebuffer, bufsize, drv_data->screen);
+
+ return 0;
+}
+
+SDL_FORCE_INLINE void
+CopyFramebuffertoN3DS(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim)
+{
+ int rows = SDL_min(dest_dim.width, source_dim.height);
+ int cols = SDL_min(dest_dim.height, source_dim.width);
+ for (int y = 0; y < rows; ++y) {
+ for (int x = 0; x < cols; ++x) {
+ SDL_memcpy(
+ dest + GetDestOffset(x, y, dest_dim.width),
+ source + GetSourceOffset(x, y, source_dim.width),
+ 4);
+ }
+ }
+}
+
+SDL_FORCE_INLINE int
+GetDestOffset(int x, int y, int dest_width)
+{
+ return dest_width - y - 1 + dest_width * x;
+}
+
+SDL_FORCE_INLINE int
+GetSourceOffset(int x, int y, int source_width)
+{
+ return x + y * source_width;
+}
+
+SDL_FORCE_INLINE void
+FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen)
+{
+ GSPGPU_FlushDataCache(buffer, bufsize);
+ gfxScreenSwapBuffers(screen, false);
+}
+
+void
+SDL_N3DS_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
+{
+ SDL_Surface *surface;
+ surface = (SDL_Surface *) SDL_SetWindowData(window, N3DS_SURFACE, NULL);
+ SDL_FreeSurface(surface);
+}
+
+#endif /* SDL_VIDEO_DRIVER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsframebuffer_c.h b/src/video/n3ds/SDL_n3dsframebuffer_c.h
new file mode 100644
index 0000000..3a4750c
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsframebuffer_c.h
@@ -0,0 +1,33 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_n3dsframebuffer_c_h_
+#define SDL_n3dsframebuffer_c_h_
+
+#include "../../SDL_internal.h"
+
+int SDL_N3DS_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch);
+int SDL_N3DS_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects);
+void SDL_N3DS_DestroyWindowFramebuffer(_THIS, SDL_Window *window);
+
+#endif /* SDL_n3dsframebuffer_c_h_ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsswkb.c b/src/video/n3ds/SDL_n3dsswkb.c
new file mode 100644
index 0000000..b577a36
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsswkb.c
@@ -0,0 +1,76 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../../SDL_internal.h"
+
+#ifdef SDL_VIDEO_DRIVER_N3DS
+
+#include <3ds.h>
+
+#include "SDL_n3dsswkb.h"
+
+static SwkbdState sw_keyboard;
+const static size_t BUFFER_SIZE = 256;
+
+void
+N3DS_SwkbInit()
+{
+ swkbdInit(&sw_keyboard, SWKBD_TYPE_NORMAL, 2, -1);
+}
+
+void
+N3DS_SwkbPoll()
+{
+ return;
+}
+
+void
+N3DS_SwkbQuit()
+{
+ return;
+}
+
+SDL_bool
+N3DS_HasScreenKeyboardSupport(_THIS)
+{
+ return SDL_TRUE;
+}
+
+void
+N3DS_StartTextInput(_THIS)
+{
+ char buffer[BUFFER_SIZE];
+ SwkbdButton button_pressed;
+ button_pressed = swkbdInputText(&sw_keyboard, buffer, BUFFER_SIZE);
+ if (button_pressed == SWKBD_BUTTON_CONFIRM) {
+ SDL_SendKeyboardText(buffer);
+ }
+}
+
+void
+N3DS_StopTextInput(_THIS)
+{
+ return;
+}
+
+#endif /* SDL_VIDEO_DRIVER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsswkb.h b/src/video/n3ds/SDL_n3dsswkb.h
new file mode 100644
index 0000000..6a48ae8
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsswkb.h
@@ -0,0 +1,38 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_n3dskeyboard_h_
+#define SDL_n3dskeyboard_h_
+
+#include "../../events/SDL_events_c.h"
+
+void N3DS_SwkbInit();
+void N3DS_SwkbPoll();
+void N3DS_SwkbQuit();
+
+SDL_bool N3DS_HasScreenKeyboardSupport(_THIS);
+
+void N3DS_StartTextInput(_THIS);
+void N3DS_StopTextInput(_THIS);
+
+#endif /* SDL_n3dskeyboard_h_ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsvideo.c b/src/video/n3ds/SDL_n3dsvideo.c
new file mode 100644
index 0000000..6d3babd
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsvideo.c
@@ -0,0 +1,163 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifdef SDL_VIDEO_DRIVER_N3DS
+
+#include "../SDL_sysvideo.h"
+#include "SDL_n3dsevents_c.h"
+#include "SDL_n3dsframebuffer_c.h"
+#include "SDL_n3dsswkb.h"
+#include "SDL_n3dsvideo.h"
+
+#define N3DSVID_DRIVER_NAME "n3ds"
+
+SDL_FORCE_INLINE void AddN3DSDisplay(gfxScreen_t screen);
+
+static int N3DS_VideoInit(_THIS);
+static void N3DS_VideoQuit(_THIS);
+static void N3DS_GetDisplayModes(_THIS, SDL_VideoDisplay *display);
+static int N3DS_CreateWindow(_THIS, SDL_Window *window);
+static void N3DS_DestroyWindow(_THIS, SDL_Window *window);
+
+/* N3DS driver bootstrap functions */
+
+static void
+N3DS_DeleteDevice(SDL_VideoDevice *device)
+{
+ SDL_free(device->displays);
+ SDL_free(device->driverdata);
+ SDL_free(device);
+}
+
+static SDL_VideoDevice *
+N3DS_CreateDevice(void)
+{
+ SDL_VideoDevice *device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
+ if (!device) {
+ SDL_OutOfMemory();
+ return (0);
+ }
+
+ device->VideoInit = N3DS_VideoInit;
+ device->VideoQuit = N3DS_VideoQuit;
+
+ device->GetDisplayModes = N3DS_GetDisplayModes;
+
+ device->CreateSDLWindow = N3DS_CreateWindow;
+ device->DestroyWindow = N3DS_DestroyWindow;
+
+ device->HasScreenKeyboardSupport = N3DS_HasScreenKeyboardSupport;
+ device->StartTextInput = N3DS_StartTextInput;
+ device->StopTextInput = N3DS_StopTextInput;
+
+ device->PumpEvents = N3DS_PumpEvents;
+
+ device->CreateWindowFramebuffer = SDL_N3DS_CreateWindowFramebuffer;
+ device->UpdateWindowFramebuffer = SDL_N3DS_UpdateWindowFramebuffer;
+ device->DestroyWindowFramebuffer = SDL_N3DS_DestroyWindowFramebuffer;
+
+ device->num_displays = 2;
+
+ device->free = N3DS_DeleteDevice;
+
+ return device;
+}
+
+VideoBootStrap N3DS_bootstrap = { N3DSVID_DRIVER_NAME, "N3DS Video Driver", N3DS_CreateDevice };
+
+static int
+N3DS_VideoInit(_THIS)
+{
+ AddN3DSDisplay(GFX_TOP);
+ AddN3DSDisplay(GFX_BOTTOM);
+
+ N3DS_SwkbInit();
+
+ return 0;
+}
+
+SDL_FORCE_INLINE void
+AddN3DSDisplay(gfxScreen_t screen)
+{
+ SDL_DisplayMode mode;
+ SDL_VideoDisplay display;
+
+ SDL_zero(mode);
+ SDL_zero(display);
+
+ mode.w = (screen == GFX_TOP) ? GSP_SCREEN_HEIGHT_TOP : GSP_SCREEN_HEIGHT_BOTTOM;
+ mode.h = GSP_SCREEN_WIDTH;
+ mode.refresh_rate = 60;
+ mode.format = FRAMEBUFFER_FORMAT;
+ mode.driverdata = NULL;
+
+ display.desktop_mode = mode;
+ display.current_mode = mode;
+ display.driverdata = NULL;
+
+ SDL_AddVideoDisplay(&display, SDL_FALSE);
+}
+
+static void
+N3DS_VideoQuit(_THIS)
+{
+ N3DS_SwkbQuit();
+ return;
+}
+
+static void
+N3DS_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
+{
+ /* Each display only has a single mode */
+ SDL_AddDisplayMode(display, &display->current_mode);
+}
+
+static int
+N3DS_CreateWindow(_THIS, SDL_Window *window)
+{
+ SDL_WindowData *drv_data = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
+ if (drv_data == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ if (window->flags & SDL_WINDOW_N3DS_BOTTOM) {
+ drv_data->screen = GFX_BOTTOM;
+ } else {
+ drv_data->screen = GFX_TOP;
+ }
+
+ window->driverdata = drv_data;
+ return 0;
+}
+
+static void
+N3DS_DestroyWindow(_THIS, SDL_Window *window)
+{
+ if (window == NULL) {
+ return;
+ }
+ SDL_free(window->driverdata);
+}
+
+#endif /* SDL_VIDEO_DRIVER_N3DS */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/src/video/n3ds/SDL_n3dsvideo.h b/src/video/n3ds/SDL_n3dsvideo.h
new file mode 100644
index 0000000..c1b15f7
--- /dev/null
+++ b/src/video/n3ds/SDL_n3dsvideo.h
@@ -0,0 +1,38 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifndef SDL_n3dsvideo_h_
+#define SDL_n3dsvideo_h_
+
+#include <3ds.h>
+
+#include "../SDL_sysvideo.h"
+typedef struct SDL_WindowData
+{
+ gfxScreen_t screen; /**< Keeps track of which N3DS screen is targetted */
+} SDL_WindowData;
+
+#define FRAMEBUFFER_FORMAT SDL_PIXELFORMAT_RGBA8888
+
+#endif /* SDL_n3dsvideo_h_ */
+
+/* vi: set sts=4 ts=4 sw=4 expandtab: */
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index ebea6de..578a422 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -14,6 +14,10 @@ if(SDL_INSTALL_TESTS)
include(GNUInstallDirs)
endif()
+if(N3DS)
+ link_libraries(SDL2::SDL2main)
+endif()
+
if(PSP)
link_libraries(
SDL2::SDL2main
@@ -430,6 +434,27 @@ if(PSP)
endforeach()
endif()
+if(N3DS)
+ set(ROMFS_DIR "${CMAKE_CURRENT_BINARY_DIR}/romfs")
+ file(COPY ${RESOURCE_FILES} DESTINATION "${ROMFS_DIR}")
+
+ foreach(APP IN LISTS ALL_TESTS)
+ get_target_property(TARGET_BINARY_DIR ${APP} BINARY_DIR)
+ set(SMDH_FILE "${TARGET_BINARY_DIR}/${APP}.smdh")
+ ctr_generate_smdh("${SMDH_FILE}"
+ NAME "SDL-${APP}"
+ DESCRIPTION "SDL2 Test suite"
+ AUTHOR "SDL2 Contributors"
+ ICON "${CMAKE_CURRENT_SOURCE_DIR}/n3ds/logo48x48.png"
+ )
+ ctr_create_3dsx(
+ ${APP}
+ ROMFS "${ROMFS_DIR}"
+ SMDH "${SMDH_FILE}"
+ )
+ endforeach()
+endif()
+
if(RISCOS)
set(ALL_TESTS_AIF "")
foreach(APP IN LISTS ALL_TESTS)
diff --git a/test/n3ds/logo48x48.png b/test/n3ds/logo48x48.png
new file mode 100644
index 0000000..fb3338d
Binary files /dev/null and b/test/n3ds/logo48x48.png differ