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
|
/* NetHack 3.6 nttty.c $NHDT-Date: 1554215932 2019/04/02 14:38:52 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.99 $ */
/* Copyright (c) NetHack PC Development Team 1993 */
/* NetHack may be freely redistributed. See license for details. */
/* tty.c - (Windows NT) version */
/*
* Initial Creation M. Allison 1993/01/31
* Switch to low level console output routines M. Allison 2003/10/01
* Restrict cursor movement until input pending M. Lehotay 2003/10/02
* Call Unicode version of output API on NT R. Chason 2005/10/28
* Use of back buffer to improve performance B. House 2018/05/06
*
*/
#ifdef WIN32
#define NEED_VARARGS /* Uses ... */
#include "win32api.h"
#include "winos.h"
#include "hack.h"
#include "wintty.h"
#include <sys\types.h>
#include <sys\stat.h>
extern boolean getreturn_enabled; /* from sys/share/pcsys.c */
extern int redirect_stdout;
#ifdef TTY_GRAPHICS
/*
* Console Buffer Flipping Support
*
* To minimize the number of calls into the WriteConsoleOutputXXX methods,
* we implement a notion of a console back buffer which keeps the next frame
* of console output as it is being composed. When ready to show the new
* frame, we compare this next frame to what is currently being output and
* only call WriteConsoleOutputXXX for those console values that need to
* change.
*
*/
#define CONSOLE_CLEAR_ATTRIBUTE (FOREGROUND_RED | FOREGROUND_GREEN \
| FOREGROUND_BLUE)
#define CONSOLE_CLEAR_CHARACTER (' ')
#define CONSOLE_UNDEFINED_ATTRIBUTE (0)
#define CONSOLE_UNDEFINED_CHARACTER ('\0')
typedef struct {
WCHAR character;
WORD attribute;
} cell_t;
cell_t clear_cell = { CONSOLE_CLEAR_CHARACTER, CONSOLE_CLEAR_ATTRIBUTE };
cell_t undefined_cell = { CONSOLE_UNDEFINED_CHARACTER,
CONSOLE_UNDEFINED_ATTRIBUTE };
/*
* The following WIN32 Console API routines are used in this file.
*
* CreateFile
* GetConsoleScreenBufferInfo
* GetStdHandle
* SetConsoleCursorPosition
* SetConsoleTextAttribute
* SetConsoleCtrlHandler
* PeekConsoleInput
* ReadConsoleInput
* WriteConsoleOutputCharacter
* FillConsoleOutputAttribute
* GetConsoleOutputCP
*/
static BOOL FDECL(CtrlHandler, (DWORD));
static void FDECL(xputc_core, (char));
void FDECL(cmov, (int, int));
void FDECL(nocmov, (int, int));
int FDECL(process_keystroke,
(INPUT_RECORD *, boolean *, BOOLEAN_P numberpad, int portdebug));
static void NDECL(init_ttycolor);
static void NDECL(really_move_cursor);
static void NDECL(check_and_set_font);
static boolean NDECL(check_font_widths);
static void NDECL(set_known_good_console_font);
static void NDECL(restore_original_console_font);
extern void NDECL(safe_routines);
/* Win32 Screen buffer,coordinate,console I/O information */
COORD ntcoord;
INPUT_RECORD ir;
static boolean orig_QuickEdit;
/* Support for changing console font if existing glyph widths are too wide */
/* Flag for whether NetHack was launched via the GUI, not the command line.
* The reason we care at all, is so that we can get
* a final RETURN at the end of the game when launched from the GUI
* to prevent the scoreboard (or panic message :-|) from vanishing
* immediately after it is displayed, yet not bother when started
* from the command line.
*/
int GUILaunched = FALSE;
/* Flag for whether unicode is supported */
static boolean init_ttycolor_completed;
#ifdef PORT_DEBUG
static boolean display_cursor_info = FALSE;
#endif
#ifdef CHANGE_COLOR
static void NDECL(adjust_palette);
static int FDECL(match_color_name, (const char *));
typedef HWND(WINAPI *GETCONSOLEWINDOW)();
static HWND GetConsoleHandle(void);
static HWND GetConsoleHwnd(void);
static boolean altered_palette;
static COLORREF UserDefinedColors[CLR_MAX];
static COLORREF NetHackColors[CLR_MAX] = {
0x00000000, 0x00c80000, 0x0000c850, 0x00b4b432, 0x000000d2, 0x00800080,
0x000064b4, 0x00c0c0c0, 0x00646464, 0x00f06464, 0x0000ff00, 0x00ffff00,
0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff
};
static COLORREF DefaultColors[CLR_MAX] = {
0x00000000, 0x00800000, 0x00008000, 0x00808000, 0x00000080, 0x00800080,
0x00008080, 0x00c0c0c0, 0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00,
0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff
};
#endif
struct console_t {
WORD background;
WORD foreground;
WORD attr;
int current_nhcolor;
int current_nhattr[ATR_INVERSE+1];
COORD cursor;
HANDLE hConOut;
HANDLE hConIn;
CONSOLE_SCREEN_BUFFER_INFO origcsbi;
int width;
int height;
boolean has_unicode;
int buffer_size;
cell_t * front_buffer;
cell_t * back_buffer;
WCHAR cpMap[256];
boolean font_changed;
CONSOLE_FONT_INFOEX original_font_info;
UINT original_code_page;
} console = {
0,
(FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED),
(FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED),
NO_COLOR,
{FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE},
{0, 0},
NULL,
NULL,
{ 0 },
0,
0,
FALSE,
0,
NULL,
NULL,
{ 0 },
FALSE,
{ 0 },
0
};
static DWORD ccount, acount;
#ifndef CLR_MAX
#define CLR_MAX 16
#endif
int ttycolors[CLR_MAX];
int ttycolors_inv[CLR_MAX];
#define MAX_OVERRIDES 256
unsigned char key_overrides[MAX_OVERRIDES];
static char nullstr[] = "";
char erase_char, kill_char;
#define DEFTEXTCOLOR ttycolors[7]
int default_processkeystroke(HANDLE, INPUT_RECORD *, boolean *, boolean, int);
int default_kbhit(HANDLE, INPUT_RECORD *);
int default_checkinput(HANDLE, INPUT_RECORD *, DWORD *, boolean,
int, int *, coord *);
int ray_processkeystroke(HANDLE, INPUT_RECORD *, boolean *, boolean, int);
int ray_kbhit(HANDLE, INPUT_RECORD *);
int ray_checkinput(HANDLE, INPUT_RECORD *, DWORD *, boolean,
int, int *, coord *);
int nh340_processkeystroke(HANDLE, INPUT_RECORD *, boolean *, boolean, int);
int nh340_kbhit(HANDLE, INPUT_RECORD *);
int nh340_checkinput(HANDLE, INPUT_RECORD *, DWORD *, boolean,
int, int *, coord *);
struct keyboard_handling_t {
char *pKeyHandlingName;
int (*pProcessKeystroke)(HANDLE, INPUT_RECORD *, boolean *,
boolean, int);
int (*pNHkbhit)(HANDLE, INPUT_RECORD *);
int (*pCheckInput)(HANDLE, INPUT_RECORD *, DWORD *, boolean,
int, int *, coord *);
} keyboard_handling = {
(char *) no_keyhandling,
default_processkeystroke,
default_kbhit,
default_checkinput
};
static INPUT_RECORD bogus_key;
/* Console buffer flipping support */
static void back_buffer_flip()
{
cell_t * back = console.back_buffer;
cell_t * front = console.front_buffer;
COORD pos;
DWORD unused;
for (pos.Y = 0; pos.Y < console.height; pos.Y++) {
for (pos.X = 0; pos.X < console.width; pos.X++) {
if (back->attribute != front->attribute) {
WriteConsoleOutputAttribute(console.hConOut, &back->attribute,
1, pos, &unused);
front->attribute = back->attribute;
}
if (back->character != front->character) {
if (console.has_unicode) {
WriteConsoleOutputCharacterW(console.hConOut,
&back->character, 1, pos, &unused);
} else {
char ch = (char)back->character;
WriteConsoleOutputCharacterA(console.hConOut, &ch, 1, pos,
&unused);
}
*front = *back;
}
back++;
front++;
}
}
}
void buffer_fill_to_end(cell_t * buffer, cell_t * fill, int x, int y)
{
nhassert(x >= 0 && x < console.width);
nhassert(y >= 0 && ((y < console.height) || (y == console.height &&
x == 0)));
cell_t * dst = buffer + console.width * y + x;
cell_t * sentinel = buffer + console.buffer_size;
while (dst != sentinel)
*dst++ = *fill;
if (iflags.debug.immediateflips && buffer == console.back_buffer)
back_buffer_flip();
}
static void buffer_clear_to_end_of_line(cell_t * buffer, int x, int y)
{
nhassert(x >= 0 && x < console.width);
nhassert(y >= 0 && ((y < console.height) || (y == console.height &&
x == 0)));
cell_t * dst = buffer + console.width * y + x;
cell_t *sentinel = buffer + console.width * (y + 1);
while (dst != sentinel)
*dst++ = clear_cell;
if (iflags.debug.immediateflips)
back_buffer_flip();
}
void buffer_write(cell_t * buffer, cell_t * cell, COORD pos)
{
nhassert(pos.X >= 0 && pos.X < console.width);
nhassert(pos.Y >= 0 && pos.Y < console.height);
cell_t * dst = buffer + (console.width * pos.Y) + pos.X;
*dst = *cell;
if (iflags.debug.immediateflips && buffer == console.back_buffer)
back_buffer_flip();
}
/*
* Called after returning from ! or ^Z
*/
void
gettty()
{
#ifndef TEXTCOLOR
int k;
#endif
erase_char = '\b';
kill_char = 21; /* cntl-U */
iflags.cbreak = TRUE;
#ifdef TEXTCOLOR
init_ttycolor();
#else
for (k = 0; k < CLR_MAX; ++k)
ttycolors[k] = NO_COLOR;
#endif
}
/* reset terminal to original state */
void
settty(s)
const char *s;
{
cmov(ttyDisplay->curx, ttyDisplay->cury);
end_screen();
if (s)
raw_print(s);
restore_original_console_font();
if (orig_QuickEdit) {
DWORD cmode;
GetConsoleMode(console.hConIn, &cmode);
cmode |= (ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS);
SetConsoleMode(console.hConIn, cmode);
}
}
/* called by init_nhwindows() and resume_nhwindows() */
void
setftty()
{
#ifdef CHANGE_COLOR
if (altered_palette)
adjust_palette();
#endif
start_screen();
}
void
tty_startup(wid, hgt)
int *wid, *hgt;
{
*wid = console.width;
*hgt = console.height;
set_option_mod_status("mouse_support", SET_IN_GAME);
}
void
tty_number_pad(state)
int state;
{
// do nothing
}
void
tty_start_screen()
{
if (iflags.num_pad)
tty_number_pad(1); /* make keypad send digits */
}
void
tty_end_screen()
{
clear_screen();
really_move_cursor();
buffer_fill_to_end(console.back_buffer, &clear_cell, 0, 0);
back_buffer_flip();
FlushConsoleInputBuffer(console.hConIn);
}
static BOOL
CtrlHandler(ctrltype)
DWORD ctrltype;
{
switch (ctrltype) {
/* case CTRL_C_EVENT: */
case CTRL_BREAK_EVENT:
clear_screen();
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
getreturn_enabled = FALSE;
#ifndef NOSAVEONHANGUP
hangup(0);
#endif
#if defined(SAFERHANGUP)
CloseHandle(console.hConIn); /* trigger WAIT_FAILED */
return TRUE;
#endif
default:
return FALSE;
}
}
/* called by pcmain() and process_options() */
void
nttty_open(mode)
int mode; // unused
{
DWORD cmode;
/* Initialize the function pointer that points to
* the kbhit() equivalent, in this TTY case nttty_kbhit()
*/
nt_kbhit = nttty_kbhit;
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE)) {
/* Unable to set control handler */
cmode = 0; /* just to have a statement to break on for debugger */
}
LI = console.height;
CO = console.width;
really_move_cursor();
}
void
nttty_exit()
{
/* go back to using the safe routines */
safe_routines();
}
int
process_keystroke(ir, valid, numberpad, portdebug)
INPUT_RECORD *ir;
boolean *valid;
boolean numberpad;
int portdebug;
{
int ch;
#ifdef QWERTZ_SUPPORT
if (Cmd.swap_yz)
numberpad |= 0x10;
#endif
ch = keyboard_handling.pProcessKeystroke(
console.hConIn, ir, valid, numberpad, portdebug);
#ifdef QWERTZ_SUPPORT
numberpad &= ~0x10;
#endif
/* check for override */
if (ch && ch < MAX_OVERRIDES && key_overrides[ch])
ch = key_overrides[ch];
return ch;
}
int
nttty_kbhit()
{
return keyboard_handling.pNHkbhit(console.hConIn, &ir);
}
int
tgetch()
{
int mod;
coord cc;
DWORD count;
boolean numpad = iflags.num_pad;
really_move_cursor();
if (iflags.debug_fuzzer)
return randomkey();
#ifdef QWERTZ_SUPPORT
if (Cmd.swap_yz)
numpad |= 0x10;
#endif
return (program_state.done_hup)
? '\033'
: keyboard_handling.pCheckInput(
console.hConIn, &ir, &count, numpad, 0, &mod, &cc);
}
int
ntposkey(x, y, mod)
int *x, *y, *mod;
{
int ch;
coord cc;
DWORD count;
boolean numpad = iflags.num_pad;
really_move_cursor();
if (iflags.debug_fuzzer)
return randomkey();
#ifdef QWERTZ_SUPPORT
if (Cmd.swap_yz)
numpad |= 0x10;
#endif
ch = (program_state.done_hup)
? '\033'
: keyboard_handling.pCheckInput(
console.hConIn, &ir, &count, numpad, 1, mod, &cc);
#ifdef QWERTZ_SUPPORT
numpad &= ~0x10;
#endif
if (!ch) {
*x = cc.x;
*y = cc.y;
}
return ch;
}
static void set_console_cursor(int x, int y)
{
nhassert(x >= 0 && x < console.width);
nhassert(y >= 0 && y < console.height);
console.cursor.X = max(0, min(console.width - 1, x));
console.cursor.Y = max(0, min(console.height - 1, y));
}
static void
really_move_cursor()
{
#ifdef PORT_DEBUG
char oldtitle[BUFSZ], newtitle[BUFSZ];
if (display_cursor_info && wizard) {
oldtitle[0] = '\0';
if (GetConsoleTitle(oldtitle, BUFSZ)) {
oldtitle[39] = '\0';
}
Sprintf(newtitle, "%-55s tty=(%02d,%02d) nttty=(%02d,%02d)", oldtitle,
ttyDisplay->curx, ttyDisplay->cury,
console.cursor.X, console.cursor.Y);
(void) SetConsoleTitle(newtitle);
}
#endif
if (ttyDisplay)
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
back_buffer_flip();
SetConsoleCursorPosition(console.hConOut, console.cursor);
}
void
cmov(x, y)
register int x, y;
{
ttyDisplay->cury = y;
ttyDisplay->curx = x;
set_console_cursor(x, y);
}
void
nocmov(x, y)
int x, y;
{
ttyDisplay->curx = x;
ttyDisplay->cury = y;
set_console_cursor(x, y);
}
/* same signature as 'putchar()' with potential failure result ignored */
int
xputc(ch)
int ch;
{
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
xputc_core((char) ch);
return 0;
}
void
xputs(s)
const char *s;
{
int k;
int slen = (int) strlen(s);
if (ttyDisplay)
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
if (s) {
for (k = 0; k < slen && s[k]; ++k)
xputc_core(s[k]);
}
}
/* xputc_core() and g_putch() are the only
* two routines that actually place output
* on the display.
*/
void
xputc_core(ch)
char ch;
{
nhassert(console.cursor.X >= 0 && console.cursor.X < console.width);
nhassert(console.cursor.Y >= 0 && console.cursor.Y < console.height);
boolean inverse = FALSE;
cell_t cell;
switch (ch) {
case '\n':
if (console.cursor.Y < console.height - 1)
console.cursor.Y++;
/* fall through */
case '\r':
console.cursor.X = 1;
break;
case '\b':
if (console.cursor.X > 1) {
console.cursor.X--;
} else if(console.cursor.Y > 0) {
console.cursor.X = console.width - 1;
console.cursor.Y--;
}
break;
default:
inverse = (console.current_nhattr[ATR_INVERSE] && iflags.wc_inverse);
console.attr = (inverse) ?
ttycolors_inv[console.current_nhcolor] :
ttycolors[console.current_nhcolor];
if (console.current_nhattr[ATR_BOLD])
console.attr |= (inverse) ?
BACKGROUND_INTENSITY : FOREGROUND_INTENSITY;
cell.attribute = console.attr;
cell.character = (console.has_unicode ? console.cpMap[ch] : ch);
buffer_write(console.back_buffer, &cell, console.cursor);
if (console.cursor.X == console.width - 1) {
if (console.cursor.Y < console.height - 1) {
console.cursor.X = 1;
console.cursor.Y++;
}
} else {
console.cursor.X++;
}
}
nhassert(console.cursor.X >= 0 && console.cursor.X < console.width);
nhassert(console.cursor.Y >= 0 && console.cursor.Y < console.height);
}
/*
* Overrides wintty.c function of the same name
* for win32. It is used for glyphs only, not text.
*/
void
g_putch(in_ch)
int in_ch;
{
boolean inverse = FALSE;
unsigned char ch = (unsigned char) in_ch;
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
inverse = (console.current_nhattr[ATR_INVERSE] && iflags.wc_inverse);
console.attr = (console.current_nhattr[ATR_INVERSE] && iflags.wc_inverse) ?
ttycolors_inv[console.current_nhcolor] :
ttycolors[console.current_nhcolor];
if (console.current_nhattr[ATR_BOLD])
console.attr |= (inverse) ? BACKGROUND_INTENSITY : FOREGROUND_INTENSITY;
cell_t cell;
cell.attribute = console.attr;
cell.character = (console.has_unicode ? cp437[ch] : ch);
buffer_write(console.back_buffer, &cell, console.cursor);
}
void
cl_end()
{
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
buffer_clear_to_end_of_line(console.back_buffer, console.cursor.X,
console.cursor.Y);
tty_curs(BASE_WINDOW, (int) ttyDisplay->curx + 1, (int) ttyDisplay->cury);
}
void
raw_clear_screen()
{
if (WINDOWPORT("tty")) {
cell_t * back = console.back_buffer;
cell_t * front = console.front_buffer;
COORD pos;
DWORD unused;
for (pos.Y = 0; pos.Y < console.height; pos.Y++) {
for (pos.X = 0; pos.X < console.width; pos.X++) {
WriteConsoleOutputAttribute(console.hConOut, &back->attribute,
1, pos, &unused);
front->attribute = back->attribute;
if (console.has_unicode) {
WriteConsoleOutputCharacterW(console.hConOut,
&back->character, 1, pos, &unused);
} else {
char ch = (char)back->character;
WriteConsoleOutputCharacterA(console.hConOut, &ch, 1, pos,
&unused);
}
*front = *back;
back++;
front++;
}
}
}
}
void
clear_screen()
{
buffer_fill_to_end(console.back_buffer, &clear_cell, 0, 0);
home();
}
void
home()
{
ttyDisplay->curx = ttyDisplay->cury = 0;
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
}
void
backsp()
{
set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
xputc_core('\b');
}
void
cl_eos()
{
buffer_fill_to_end(console.back_buffer, &clear_cell, ttyDisplay->curx,
ttyDisplay->cury);
tty_curs(BASE_WINDOW, (int) ttyDisplay->curx + 1, (int) ttyDisplay->cury);
}
void
tty_nhbell()
{
if (flags.silent || iflags.debug_fuzzer)
return;
Beep(8000, 500);
}
volatile int junk; /* prevent optimizer from eliminating loop below */
void
tty_delay_output()
{
/* delay 50 ms - uses ANSI C clock() function now */
clock_t goal;
int k;
goal = 50 + clock();
back_buffer_flip();
if (iflags.debug_fuzzer)
return;
while (goal > clock()) {
k = junk; /* Do nothing */
}
}
/*
* CLR_BLACK 0
* CLR_RED 1
* CLR_GREEN 2
* CLR_BROWN 3 low-intensity yellow
* CLR_BLUE 4
* CLR_MAGENTA 5
* CLR_CYAN 6
* CLR_GRAY 7 low-intensity white
* NO_COLOR 8
* CLR_ORANGE 9
* CLR_BRIGHT_GREEN 10
* CLR_YELLOW 11
* CLR_BRIGHT_BLUE 12
* CLR_BRIGHT_MAGENTA 13
* CLR_BRIGHT_CYAN 14
* CLR_WHITE 15
* CLR_MAX 16
* BRIGHT 8
*/
static void
init_ttycolor()
{
#ifdef TEXTCOLOR
ttycolors[CLR_BLACK] = FOREGROUND_INTENSITY; /* fix by Quietust */
ttycolors[CLR_RED] = FOREGROUND_RED;
ttycolors[CLR_GREEN] = FOREGROUND_GREEN;
ttycolors[CLR_BROWN] = FOREGROUND_GREEN | FOREGROUND_RED;
ttycolors[CLR_BLUE] = FOREGROUND_BLUE;
ttycolors[CLR_MAGENTA] = FOREGROUND_BLUE | FOREGROUND_RED;
ttycolors[CLR_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE;
ttycolors[CLR_GRAY] = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
ttycolors[NO_COLOR] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
ttycolors[CLR_ORANGE] = FOREGROUND_RED | FOREGROUND_INTENSITY;
ttycolors[CLR_BRIGHT_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
ttycolors[CLR_YELLOW] = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
ttycolors[CLR_BRIGHT_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
ttycolors[CLR_BRIGHT_MAGENTA]=FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
ttycolors[CLR_BRIGHT_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
ttycolors[CLR_WHITE] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED
| FOREGROUND_INTENSITY;
ttycolors_inv[CLR_BLACK] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
| BACKGROUND_INTENSITY;
ttycolors_inv[CLR_RED] = BACKGROUND_RED | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_GREEN] = BACKGROUND_GREEN;
ttycolors_inv[CLR_BROWN] = BACKGROUND_GREEN | BACKGROUND_RED;
ttycolors_inv[CLR_BLUE] = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_MAGENTA] = BACKGROUND_BLUE | BACKGROUND_RED;
ttycolors_inv[CLR_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE;
ttycolors_inv[CLR_GRAY] = BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE;
ttycolors_inv[NO_COLOR] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED;
ttycolors_inv[CLR_ORANGE] = BACKGROUND_RED | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_BRIGHT_GREEN]= BACKGROUND_GREEN | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_YELLOW] = BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_BRIGHT_BLUE] = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_BRIGHT_MAGENTA] =BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_BRIGHT_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
ttycolors_inv[CLR_WHITE] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
| BACKGROUND_INTENSITY;
#else
int k;
ttycolors[0] = FOREGROUND_INTENSITY;
ttycolors_inv[0] = BACKGROUND_INTENSITY;
for (k = 1; k < SIZE(ttycolors); ++k) {
ttycolors[k] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
ttycolors_inv[k] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED;
}
#endif
init_ttycolor_completed = TRUE;
}
#if 0
int
has_color(int color) /* this function is commented out */
{
#ifdef TEXTCOLOR
if ((color >= 0) && (color < CLR_MAX))
return 1;
#else
if ((color == CLR_BLACK) || (color == CLR_WHITE) || (color == NO_COLOR))
return 1;
#endif
else
return 0;
}
#endif
int
term_attr_fixup(int attrmask)
{
return attrmask;
}
void
term_start_attr(int attrib)
{
console.current_nhattr[attrib] = TRUE;
if (attrib) console.current_nhattr[ATR_NONE] = FALSE;
}
void
term_end_attr(int attrib)
{
int k;
switch (attrib) {
case ATR_INVERSE:
case ATR_ULINE:
case ATR_BLINK:
case ATR_BOLD:
break;
}
console.current_nhattr[attrib] = FALSE;
console.current_nhattr[ATR_NONE] = TRUE;
/* re-evaluate all attr now for performance at output time */
for (k=ATR_NONE; k <= ATR_INVERSE; ++k) {
if (console.current_nhattr[k])
console.current_nhattr[ATR_NONE] = FALSE;
}
}
void
term_end_raw_bold(void)
{
term_end_attr(ATR_BOLD);
}
void
term_start_raw_bold(void)
{
term_start_attr(ATR_BOLD);
}
void
term_start_color(int color)
{
#ifdef TEXTCOLOR
if (color >= 0 && color < CLR_MAX) {
console.current_nhcolor = color;
} else
#endif
console.current_nhcolor = NO_COLOR;
}
void
term_end_color(void)
{
#ifdef TEXTCOLOR
console.foreground = DEFTEXTCOLOR;
#endif
console.attr = (console.foreground | console.background);
console.current_nhcolor = NO_COLOR;
}
void
standoutbeg()
{
term_start_attr(ATR_BOLD);
}
void
standoutend()
{
term_end_attr(ATR_BOLD);
}
#ifndef NO_MOUSE_ALLOWED
void
toggle_mouse_support()
{
static int qeinit = 0;
DWORD cmode;
GetConsoleMode(console.hConIn, &cmode);
if (!qeinit) {
qeinit = 1;
orig_QuickEdit = ((cmode & ENABLE_QUICK_EDIT_MODE) != 0);
}
switch(iflags.wc_mouse_support) {
case 2:
cmode |= ENABLE_MOUSE_INPUT;
break;
case 1:
cmode |= ENABLE_MOUSE_INPUT;
cmode &= ~ENABLE_QUICK_EDIT_MODE;
cmode |= ENABLE_EXTENDED_FLAGS;
break;
case 0:
/*FALLTHRU*/
default:
cmode &= ~ENABLE_MOUSE_INPUT;
if (orig_QuickEdit)
cmode |= (ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS);
}
SetConsoleMode(console.hConIn, cmode);
}
#endif
/* handle tty options updates here */
void
nttty_preference_update(pref)
const char *pref;
{
if (stricmp(pref, "mouse_support") == 0) {
#ifndef NO_MOUSE_ALLOWED
toggle_mouse_support();
#endif
}
if (stricmp(pref, "symset") == 0)
check_and_set_font();
return;
}
#ifdef PORT_DEBUG
void
win32con_debug_keystrokes()
{
DWORD count;
boolean valid = 0;
int ch;
xputs("\n");
while (!valid || ch != 27) {
nocmov(ttyDisplay->curx, ttyDisplay->cury);
ReadConsoleInput(console.hConIn, &ir, 1, &count);
if ((ir.EventType == KEY_EVENT) && ir.Event.KeyEvent.bKeyDown)
ch = process_keystroke(&ir, &valid, iflags.num_pad, 1);
}
(void) doredraw();
}
void
win32con_toggle_cursor_info()
{
display_cursor_info = !display_cursor_info;
}
#endif
void
map_subkeyvalue(op)
register char *op;
{
char digits[] = "0123456789";
int length, i, idx, val;
char *kp;
idx = -1;
val = -1;
kp = index(op, '/');
if (kp) {
*kp = '\0';
kp++;
length = strlen(kp);
if (length < 1 || length > 3)
return;
for (i = 0; i < length; i++)
if (!index(digits, kp[i]))
return;
val = atoi(kp);
length = strlen(op);
if (length < 1 || length > 3)
return;
for (i = 0; i < length; i++)
if (!index(digits, op[i]))
return;
idx = atoi(op);
}
if (idx >= MAX_OVERRIDES || idx < 0 || val >= MAX_OVERRIDES || val < 1)
return;
key_overrides[idx] = val;
}
/* fatal error */
/*VARARGS1*/
void nttty_error
VA_DECL(const char *, s)
{
char buf[BUFSZ];
VA_START(s);
VA_INIT(s, const char *);
/* error() may get called before tty is initialized */
if (iflags.window_inited)
end_screen();
buf[0] = '\n';
(void) vsnprintf(&buf[1], sizeof buf - 1, s, VA_ARGS);
msmsg(buf);
really_move_cursor();
VA_END();
exit(EXIT_FAILURE);
}
void
synch_cursor()
{
really_move_cursor();
}
#ifdef CHANGE_COLOR
void
tty_change_color(color_number, rgb, reverse)
int color_number, reverse;
long rgb;
{
/* Map NetHack color index to NT Console palette index */
int idx, win32_color_number[] = {
0, /* CLR_BLACK 0 */
4, /* CLR_RED 1 */
2, /* CLR_GREEN 2 */
6, /* CLR_BROWN 3 */
1, /* CLR_BLUE 4 */
5, /* CLR_MAGENTA 5 */
3, /* CLR_CYAN 6 */
7, /* CLR_GRAY 7 */
8, /* NO_COLOR 8 */
12, /* CLR_ORANGE 9 */
10, /* CLR_BRIGHT_GREEN 10 */
14, /* CLR_YELLOW 11 */
9, /* CLR_BRIGHT_BLUE 12 */
13, /* CLR_BRIGHT_MAGENTA 13 */
11, /* CLR_BRIGHT_CYAN 14 */
15 /* CLR_WHITE 15 */
};
int k;
if (color_number < 0) { /* indicates OPTIONS=palette with no value */
/* copy the NetHack palette into UserDefinedColors */
for (k = 0; k < CLR_MAX; k++)
UserDefinedColors[k] = NetHackColors[k];
} else if (color_number >= 0 && color_number < CLR_MAX) {
if (!altered_palette) {
/* make sure a full suite is available */
for (k = 0; k < CLR_MAX; k++)
UserDefinedColors[k] = DefaultColors[k];
}
idx = win32_color_number[color_number];
UserDefinedColors[idx] = rgb;
}
altered_palette = TRUE;
}
char *
tty_get_color_string()
{
return "";
}
int
match_color_name(c)
const char *c;
{
const struct others {
int idx;
const char *colorname;
} othernames[] = {
{ CLR_MAGENTA, "purple" },
{ CLR_BRIGHT_MAGENTA, "bright purple" },
{ NO_COLOR, "dark gray" },
{ NO_COLOR, "dark grey" },
{ CLR_GRAY, "grey" },
};
int cnt;
for (cnt = 0; cnt < CLR_MAX; ++cnt) {
if (!strcmpi(c, c_obj_colors[cnt]))
return cnt;
}
for (cnt = 0; cnt < SIZE(othernames); ++cnt) {
if (!strcmpi(c, othernames[cnt].colorname))
return othernames[cnt].idx;
}
return -1;
}
/*
* Returns 0 if badoption syntax
*/
int
alternative_palette(op)
char *op;
{
/*
* palette:color-R-G-B
* OPTIONS=palette:green-4-3-1, palette:0-0-0-0
*/
int fieldcnt, color_number, rgb, red, green, blue;
char *fields[4], *cp;
if (!op) {
change_color(-1, 0, 0); /* indicates palette option with
no value meaning "load an entire
hard-coded NetHack palette." */
return 1;
}
cp = fields[0] = op;
for (fieldcnt = 1; fieldcnt < 4; ++fieldcnt) {
cp = index(cp, '-');
if (!cp)
return 0;
fields[fieldcnt] = cp;
cp++;
}
for (fieldcnt = 1; fieldcnt < 4; ++fieldcnt) {
*(fields[fieldcnt]) = '\0';
++fields[fieldcnt];
}
rgb = 0;
for (fieldcnt = 0; fieldcnt < 4; ++fieldcnt) {
if (fieldcnt == 0 && isalpha(*(fields[0]))) {
color_number = match_color_name(fields[0]);
if (color_number == -1)
return 0;
} else {
int dcount = 0, cval = 0;
cp = fields[fieldcnt];
if (*cp == '\\' && index("0123456789xXoO", cp[1])) {
const char *dp, *hex = "00112233445566778899aAbBcCdDeEfF";
cp++;
if (*cp == 'x' || *cp == 'X')
for (++cp; (dp = index(hex, *cp)) && (dcount++ < 2); cp++)
cval = (int) ((cval * 16) + (dp - hex) / 2);
else if (*cp == 'o' || *cp == 'O')
for (++cp; (index("01234567", *cp)) && (dcount++ < 3);
cp++)
cval = (cval * 8) + (*cp - '0');
else
return 0;
} else {
for (; *cp && (index("0123456789", *cp)) && (dcount++ < 3);
cp++)
cval = (cval * 10) + (*cp - '0');
}
switch (fieldcnt) {
case 0:
color_number = cval;
break;
case 1:
red = cval;
break;
case 2:
green = cval;
break;
case 3:
blue = cval;
break;
}
}
}
rgb = RGB(red, green, blue);
if (color_number >= 0 && color_number < CLR_MAX)
change_color(color_number, rgb, 0);
return 1;
}
/*
* This uses an undocumented method to set console attributes
* at runtime including console palette
*
* VOID WINAPI SetConsolePalette(COLORREF palette[16])
*
* Author: James Brown at www.catch22.net
*
* Set palette of current console.
* Palette should be of the form:
*
* COLORREF DefaultColors[CLR_MAX] =
* {
* 0x00000000, 0x00800000, 0x00008000, 0x00808000,
* 0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0,
* 0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00,
* 0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff
* };
*/
#pragma pack(push, 1)
/*
* Structure to send console via WM_SETCONSOLEINFO
*/
typedef struct _CONSOLE_INFO {
ULONG Length;
COORD ScreenBufferSize;
COORD WindowSize;
ULONG WindowPosX;
ULONG WindowPosY;
COORD FontSize;
ULONG FontFamily;
ULONG FontWeight;
WCHAR FaceName[32];
ULONG CursorSize;
ULONG FullScreen;
ULONG QuickEdit;
ULONG AutoPosition;
ULONG InsertMode;
USHORT ScreenColors;
USHORT PopupColors;
ULONG HistoryNoDup;
ULONG HistoryBufferSize;
ULONG NumberOfHistoryBuffers;
COLORREF ColorTable[16];
ULONG CodePage;
HWND Hwnd;
WCHAR ConsoleTitle[0x100];
} CONSOLE_INFO;
#pragma pack(pop)
BOOL SetConsoleInfo(HWND hwndConsole, CONSOLE_INFO *pci);
static void GetConsoleSizeInfo(CONSOLE_INFO *pci);
VOID WINAPI SetConsolePalette(COLORREF crPalette[16]);
void
adjust_palette(VOID_ARGS)
{
SetConsolePalette(UserDefinedColors);
altered_palette = 0;
}
/*
/* only in Win2k+ (use FindWindow for NT4) */
/* HWND WINAPI GetConsoleWindow(); */
/* Undocumented console message */
#define WM_SETCONSOLEINFO (WM_USER + 201)
VOID WINAPI
SetConsolePalette(COLORREF palette[16])
{
CONSOLE_INFO ci = { sizeof(ci) };
int i;
HWND hwndConsole = GetConsoleHandle();
/* get current size/position settings rather than using defaults.. */
GetConsoleSizeInfo(&ci);
/* set these to zero to keep current settings */
ci.FontSize.X = 0; /* def = 8 */
ci.FontSize.Y = 0; /* def = 12 */
ci.FontFamily = 0; /* def = 0x30 = FF_MODERN|FIXED_PITCH */
ci.FontWeight = 0; /* 0x400; */
/* lstrcpyW(ci.FaceName, L"Terminal"); */
ci.FaceName[0] = L'\0';
ci.CursorSize = 25;
ci.FullScreen = FALSE;
ci.QuickEdit = TRUE;
ci.AutoPosition = 0x10000;
ci.InsertMode = TRUE;
ci.ScreenColors = MAKEWORD(0x7, 0x0);
ci.PopupColors = MAKEWORD(0x5, 0xf);
ci.HistoryNoDup = FALSE;
ci.HistoryBufferSize = 50;
ci.NumberOfHistoryBuffers = 4;
// colour table
for (i = 0; i < 16; i++)
ci.ColorTable[i] = palette[i];
ci.CodePage = GetConsoleOutputCP();
ci.Hwnd = hwndConsole;
lstrcpyW(ci.ConsoleTitle, L"");
SetConsoleInfo(hwndConsole, &ci);
}
/*
* Wrapper around WM_SETCONSOLEINFO. We need to create the
* necessary section (file-mapping) object in the context of the
* process which owns the console, before posting the message
*/
BOOL
SetConsoleInfo(HWND hwndConsole, CONSOLE_INFO *pci)
{
DWORD dwConsoleOwnerPid;
HANDLE hProcess;
HANDLE hSection, hDupSection;
PVOID ptrView = 0;
HANDLE hThread;
/*
* Open the process which "owns" the console
*/
GetWindowThreadProcessId(hwndConsole, &dwConsoleOwnerPid);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwConsoleOwnerPid);
/*
* Create a SECTION object backed by page-file, then map a view of
* this section into the owner process so we can write the contents
* of the CONSOLE_INFO buffer into it
*/
hSection = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0,
pci->Length, 0);
/*
* Copy our console structure into the section-object
*/
ptrView = MapViewOfFile(hSection, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0,
pci->Length);
memcpy(ptrView, pci, pci->Length);
UnmapViewOfFile(ptrView);
/*
* Map the memory into owner process
*/
DuplicateHandle(GetCurrentProcess(), hSection, hProcess, &hDupSection, 0,
FALSE, DUPLICATE_SAME_ACCESS);
/* Send console window the "update" message */
SendMessage(hwndConsole, WM_SETCONSOLEINFO, (WPARAM) hDupSection, 0);
/*
* clean up
*/
hThread = CreateRemoteThread(hProcess, 0, 0,
(LPTHREAD_START_ROUTINE) CloseHandle,
hDupSection, 0, 0);
CloseHandle(hThread);
CloseHandle(hSection);
CloseHandle(hProcess);
return TRUE;
}
/*
* Fill the CONSOLE_INFO structure with information
* about the current console window
*/
static void
GetConsoleSizeInfo(CONSOLE_INFO *pci)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut, &csbi);
pci->ScreenBufferSize = csbi.dwSize;
pci->WindowSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
pci->WindowSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
pci->WindowPosX = csbi.srWindow.Left;
pci->WindowPosY = csbi.srWindow.Top;
}
static HWND
GetConsoleHandle(void)
{
HMODULE hMod = GetModuleHandle("kernel32.dll");
GETCONSOLEWINDOW pfnGetConsoleWindow =
(GETCONSOLEWINDOW) GetProcAddress(hMod, "GetConsoleWindow");
if (pfnGetConsoleWindow)
return pfnGetConsoleWindow();
else
return GetConsoleHwnd();
}
static HWND
GetConsoleHwnd(void)
{
int iterations = 0;
HWND hwndFound = 0;
char OldTitle[1024], NewTitle[1024], TestTitle[1024];
/* Get current window title */
GetConsoleTitle(OldTitle, sizeof OldTitle);
(void) sprintf(NewTitle, "NETHACK%d/%d", GetTickCount(),
GetCurrentProcessId());
SetConsoleTitle(NewTitle);
GetConsoleTitle(TestTitle, sizeof TestTitle);
while (strcmp(TestTitle, NewTitle) != 0) {
iterations++;
/* sleep(0); */
GetConsoleTitle(TestTitle, sizeof TestTitle);
}
hwndFound = FindWindow(NULL, NewTitle);
SetConsoleTitle(OldTitle);
/* printf("%d iterations\n", iterations); */
return hwndFound;
}
#endif /*CHANGE_COLOR*/
static int CALLBACK EnumFontCallback(
const LOGFONTW * lf, const TEXTMETRICW * tm, DWORD fontType, LPARAM lParam)
{
LOGFONTW * lf_ptr = (LOGFONTW *) lParam;
*lf_ptr = *lf;
return 0;
}
/* check_and_set_font ensures that the current font will render the symbols
* that are currently being used correctly. If they will not be rendered
* correctly, then it will change the font to a known good font.
*/
void
check_and_set_font()
{
if (!check_font_widths()) {
raw_print("WARNING: glyphs too wide in console font."
" Changing code page to 437 and font to Consolas\n");
set_known_good_console_font();
}
}
/* check_font_widths returns TRUE if all glyphs in current console font
* fit within the width of a single console cell.
*/
boolean
check_font_widths()
{
CONSOLE_FONT_INFOEX console_font_info;
console_font_info.cbSize = sizeof(console_font_info);
BOOL success = GetCurrentConsoleFontEx(console.hConOut, FALSE,
&console_font_info);
/* get console window and DC
* NOTE: the DC from the console window does not have the correct
* font selected at this point.
*/
HWND hWnd = GetConsoleWindow();
HDC hDC = GetDC(hWnd);
LOGFONTW logical_font;
logical_font.lfCharSet = DEFAULT_CHARSET;
wcscpy(logical_font.lfFaceName, console_font_info.FaceName);
logical_font.lfPitchAndFamily = 0;
/* getting matching console font */
LOGFONTW matching_log_font = { 0 };
EnumFontFamiliesExW(hDC, &logical_font, EnumFontCallback,
(LPARAM) &matching_log_font, 0);
if (matching_log_font.lfHeight == 0) {
raw_print("Unable to enumerate system fonts\n");
return FALSE;
}
/* create font matching console font */
LOGFONTW console_font_log_font = matching_log_font;
console_font_log_font.lfWeight = console_font_info.FontWeight;
console_font_log_font.lfHeight = console_font_info.dwFontSize.Y;
console_font_log_font.lfWidth = console_font_info.dwFontSize.X;
HFONT console_font = CreateFontIndirectW(&console_font_log_font);
if (console_font == NULL) {
raw_print("Unable to create console font\n");
return FALSE;
}
/* select font */
HGDIOBJ saved_font = SelectObject(hDC, console_font);
/* determine whether it is a true type font */
TEXTMETRICA tm;
success = GetTextMetricsA(hDC, &tm);
if (!success) {
raw_print("Unable to get console font text metrics\n");
goto clean_up;
}
boolean isTrueType = (tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0;
/* determine which glyphs are used */
boolean used[256];
memset(used, 0, sizeof(used));
for (int i = 0; i < SYM_MAX; i++) {
used[primary_syms[i]] = TRUE;
used[rogue_syms[i]] = TRUE;
}
int wcUsedCount = 0;
wchar_t wcUsed[256];
for (int i = 0; i < sizeof(used); i++)
if (used[i])
wcUsed[wcUsedCount++] = cp437[i];
/* measure the set of used glyphs to ensure they fit */
boolean all_glyphs_fit = TRUE;
for (int i = 0; i < wcUsedCount; i++) {
int width;
if (isTrueType) {
ABC abc;
success = GetCharABCWidthsW(hDC, wcUsed[i], wcUsed[i], &abc);
width = abc.abcA + abc.abcB + abc.abcC;
} else {
success = GetCharWidthW(hDC, wcUsed[i], wcUsed[i], &width);
}
if (success && width > console_font_info.dwFontSize.X) {
all_glyphs_fit = FALSE;
break;
}
}
clean_up:
SelectObject(hDC, saved_font);
DeleteObject(console_font);
return all_glyphs_fit;
}
/* set_known_good_console_font sets the code page and font used by the console
* to settings know to work well with NetHack. It also saves the original
* settings so that they can be restored prior to NetHack exit.
*/
void
set_known_good_console_font()
{
CONSOLE_FONT_INFOEX console_font_info;
console_font_info.cbSize = sizeof(console_font_info);
BOOL success = GetCurrentConsoleFontEx(console.hConOut, FALSE,
&console_font_info);
console.font_changed = TRUE;
console.original_font_info = console_font_info;
console.original_code_page = GetConsoleOutputCP();
wcscpy_s(console_font_info.FaceName,
sizeof(console_font_info.FaceName)
/ sizeof(console_font_info.FaceName[0]),
L"Consolas");
success = SetConsoleOutputCP(437);
nhassert(success);
success = SetCurrentConsoleFontEx(console.hConOut, FALSE, &console_font_info);
nhassert(success);
}
/* restore_original_console_font will restore the console font and code page
* settings to what they were when NetHack was launched.
*/
void
restore_original_console_font()
{
if (console.font_changed) {
BOOL success;
raw_print("Restoring original font and code page\n");
success = SetConsoleOutputCP(console.original_code_page);
if (!success)
raw_print("Unable to restore original code page\n");
success = SetCurrentConsoleFontEx(console.hConOut, FALSE,
&console.original_font_info);
if (!success)
raw_print("Unable to restore original font\n");
console.font_changed = FALSE;
}
}
/* set_cp_map() creates a mapping of every possible character of a code
* page to its corresponding WCHAR. This is necessary due to the high
* cost of making calls to MultiByteToWideChar() for every character we
* wish to print to the console.
*/
void set_cp_map()
{
if (console.has_unicode) {
UINT codePage = GetConsoleOutputCP();
if (codePage == 437) {
memcpy(console.cpMap, cp437, sizeof(console.cpMap));
} else {
for (int i = 0; i < 256; i++) {
char c = (char)i;
int count = MultiByteToWideChar(codePage, 0, &c, 1,
&console.cpMap[i], 1);
nhassert(count == 1);
// If a character was mapped to unicode control codes,
// remap to the appropriate unicode character per our
// code page 437 mappings.
if (console.cpMap[i] < 32)
console.cpMap[i] = cp437[console.cpMap[i]];
}
}
}
}
#if 0
/* early_raw_print() is used during early game intialization prior to the
* setting up of the windowing system. This allows early errors and panics
* to have there messages displayed.
*
* early_raw_print() eventually gets replaced by tty_raw_print().
*
*/
void early_raw_print(const char *s)
{
if (console.hConOut == NULL)
return;
nhassert(console.cursor.X >= 0 && console.cursor.X < console.width);
nhassert(console.cursor.Y >= 0 && console.cursor.Y < console.height);
WORD attribute = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
DWORD unused;
while (*s != '\0') {
switch (*s) {
case '\n':
if (console.cursor.Y < console.height - 1)
console.cursor.Y++;
/* fall through */
case '\r':
console.cursor.X = 1;
break;
case '\b':
if (console.cursor.X > 1) {
console.cursor.X--;
} else if(console.cursor.Y > 0) {
console.cursor.X = console.width - 1;
console.cursor.Y--;
}
break;
default:
WriteConsoleOutputAttribute(console.hConOut, &attribute,
1, console.cursor, &unused);
WriteConsoleOutputCharacterA(console.hConOut, s,
1, console.cursor, &unused);
if (console.cursor.X == console.width - 1) {
if (console.cursor.Y < console.height - 1) {
console.cursor.X = 1;
console.cursor.Y++;
}
} else {
console.cursor.X++;
}
}
s++;
}
nhassert(console.cursor.X >= 0 && console.cursor.X < console.width);
nhassert(console.cursor.Y >= 0 && console.cursor.Y < console.height);
SetConsoleCursorPosition(console.hConOut, console.cursor);
}
#endif
/* nethack_enter_nttty() is the first thing that is called from main
* once the tty port is confirmed.
*
* We initialize all console state to support rendering to the console
* through out flipping support at this time. This allows us to support
* raw_print prior to our returning.
*
* During this early initialization, we also determine the width and
* height of the console that will be used. This width and height will
* not later change.
*
* We also check and set the console font to a font that we know will work
* well with nethack.
*
* The intent of this early initialization is to get all state that is
* not dependent upon game options initialized allowing us to simplify
* any additional initialization that might be needed when we are actually
* asked to open.
*
* Other then the call below which clears the entire console buffer, no
* other code outputs directly to the console other then the code that
* handles flipping the back buffer.
*
*/
void nethack_enter_nttty()
{
#if 0
/* set up state needed by early_raw_print() */
windowprocs.win_raw_print = early_raw_print;
#endif
console.hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
nhassert(console.hConOut != NULL); // NOTE: this assert will not print
GetConsoleScreenBufferInfo(console.hConOut, &console.origcsbi);
/* Testing of widths != COLNO has not turned up any problems. Need
* to do a bit more testing and then we are likely to enable having
* console width match window width.
*/
#if 0
console.width = console.origcsbi.srWindow.Right -
console.origcsbi.srWindow.Left + 1;
console.Width = max(console.Width, COLNO);
#else
console.width = COLNO;
#endif
console.height = console.origcsbi.srWindow.Bottom -
console.origcsbi.srWindow.Top + 1;
console.height = max(console.height, ROWNO + 3);
console.buffer_size = console.width * console.height;
/* clear the entire console buffer */
int size = console.origcsbi.dwSize.X * console.origcsbi.dwSize.Y;
DWORD unused;
set_console_cursor(0, 0);
FillConsoleOutputAttribute(
console.hConOut, CONSOLE_CLEAR_ATTRIBUTE,
size, console.cursor, &unused);
FillConsoleOutputCharacter(
console.hConOut, CONSOLE_CLEAR_CHARACTER,
size, console.cursor, &unused);
set_console_cursor(1, 0);
SetConsoleCursorPosition(console.hConOut, console.cursor);
/* At this point early_raw_print will work */
console.hConIn = GetStdHandle(STD_INPUT_HANDLE);
nhassert(console.hConIn != NULL);
/* grow the size of the console buffer if it is not wide enough */
if (console.origcsbi.dwSize.X < console.width) {
COORD size = {
size.Y = console.origcsbi.dwSize.Y,
size.X = console.width
};
SetConsoleScreenBufferSize(console.hConOut, size);
}
/* setup front and back buffers */
int buffer_size_bytes = sizeof(cell_t) * console.buffer_size;
console.front_buffer = (cell_t *)malloc(buffer_size_bytes);
buffer_fill_to_end(console.front_buffer, &undefined_cell, 0, 0);
console.back_buffer = (cell_t *)malloc(buffer_size_bytes);
buffer_fill_to_end(console.back_buffer, &clear_cell, 0, 0);
/* determine whether OS version has unicode support */
console.has_unicode = ((GetVersion() & 0x80000000) == 0);
/* check the font before we capture the code page map */
check_and_set_font();
set_cp_map();
/* Set console mode */
DWORD cmode, mask;
GetConsoleMode(console.hConIn, &cmode);
#ifdef NO_MOUSE_ALLOWED
mask = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_MOUSE_INPUT
| ENABLE_ECHO_INPUT | ENABLE_WINDOW_INPUT;
#else
mask = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT
| ENABLE_WINDOW_INPUT;
#endif
/* Turn OFF the settings specified in the mask */
cmode &= ~mask;
#ifndef NO_MOUSE_ALLOWED
cmode |= ENABLE_MOUSE_INPUT;
#endif
SetConsoleMode(console.hConIn, cmode);
/* load default keyboard handler */
HKL keyboard_layout = GetKeyboardLayout(0);
DWORD primary_language = (UINT_PTR) keyboard_layout & 0x3f;
/* This was overriding the handler that had already
been loaded during options parsing. Needs to
check first */
if (iflags.key_handling == no_keyhandling) {
if (primary_language == LANG_ENGLISH) {
set_altkeyhandling("default");
} else {
set_altkeyhandling("ray");
}
}
}
#endif /* TTY_GRAPHICS */
/* this is used as a printf() replacement when the window
* system isn't initialized yet
*/
void msmsg
VA_DECL(const char *, fmt)
{
char buf[ROWNO * COLNO]; /* worst case scenario */
VA_START(fmt);
VA_INIT(fmt, const char *);
(void) vsnprintf(buf, sizeof buf, fmt, VA_ARGS);
if (redirect_stdout)
fprintf(stdout, "%s", buf);
else {
#ifdef TTY_GRAPHICS
if(!init_ttycolor_completed)
init_ttycolor();
/* if we have generated too many messages ... ask the user to
* confirm and then clear.
*/
if (console.cursor.Y > console.height - 4) {
xputs("Hit <Enter> to continue.");
while (pgetchar() != '\n')
;
raw_clear_screen();
set_console_cursor(1, 0);
}
xputs(buf);
if (ttyDisplay)
curs(BASE_WINDOW, console.cursor.X + 1, console.cursor.Y);
#else
fprintf(stdout, "%s", buf);
#endif
}
VA_END();
return;
}
/*
* Keyboard translation tables.
* (Adopted from the MSDOS port)
*/
#define KEYPADLO 0x47
#define KEYPADHI 0x53
#define PADKEYS (KEYPADHI - KEYPADLO + 1)
#define iskeypad(x) (KEYPADLO <= (x) && (x) <= KEYPADHI)
#define isnumkeypad(x) \
(KEYPADLO <= (x) && (x) <= 0x51 && (x) != 0x4A && (x) != 0x4E)
#ifdef QWERTZ_SUPPORT
/* when 'numberpad' is 0 and Cmd.swap_yz is True
(signaled by setting 0x10 on boolean numpad argument)
treat keypress of numpad 7 as 'z' rather than 'y' */
static boolean qwertz = FALSE;
#endif
#define inmap(x, vk) (((x) > 'A' && (x) < 'Z') || (vk) == 0xBF || (x) == '2')
const struct pad {
uchar normal, shift, cntrl;
};
/*
* default key handling
*
* This is the default NetHack keystroke processing.
* Use the .nethackrc "altkeyhandling" option to set a
* different handling type.
*
*/
/*
* Keypad keys are translated to the normal values below.
* Shifted keypad keys are translated to the
* shift values below.
*/
static const struct pad default_keypad[PADKEYS] = {
{ 'y', 'Y', C('y') }, /* 7 */
{ 'k', 'K', C('k') }, /* 8 */
{ 'u', 'U', C('u') }, /* 9 */
{ 'm', C('p'), C('p') }, /* - */
{ 'h', 'H', C('h') }, /* 4 */
{ 'g', 'G', 'g' }, /* 5 */
{ 'l', 'L', C('l') }, /* 6 */
{ '+', 'P', C('p') }, /* + */
{ 'b', 'B', C('b') }, /* 1 */
{ 'j', 'J', C('j') }, /* 2 */
{ 'n', 'N', C('n') }, /* 3 */
{ 'i', 'I', C('i') }, /* Ins */
{ '.', ':', ':' } /* Del */
}, default_numpad[PADKEYS] = {
{ '7', M('7'), '7' }, /* 7 */
{ '8', M('8'), '8' }, /* 8 */
{ '9', M('9'), '9' }, /* 9 */
{ 'm', C('p'), C('p') }, /* - */
{ '4', M('4'), '4' }, /* 4 */
{ '5', M('5'), '5' }, /* 5 */
{ '6', M('6'), '6' }, /* 6 */
{ '+', 'P', C('p') }, /* + */
{ '1', M('1'), '1' }, /* 1 */
{ '2', M('2'), '2' }, /* 2 */
{ '3', M('3'), '3' }, /* 3 */
{ '0', M('0'), '0' }, /* Ins */
{ '.', ':', ':' } /* Del */
};
/*
* Keypad keys are translated to the normal values below.
* Shifted keypad keys are translated to the
* shift values below.
*/
static const struct pad
ray_keypad[PADKEYS] = {
{ 'y', 'Y', C('y') }, /* 7 */
{ 'k', 'K', C('k') }, /* 8 */
{ 'u', 'U', C('u') }, /* 9 */
{ 'm', C('p'), C('p') }, /* - */
{ 'h', 'H', C('h') }, /* 4 */
{ 'g', 'G', 'g' }, /* 5 */
{ 'l', 'L', C('l') }, /* 6 */
{ '+', 'P', C('p') }, /* + */
{ 'b', 'B', C('b') }, /* 1 */
{ 'j', 'J', C('j') }, /* 2 */
{ 'n', 'N', C('n') }, /* 3 */
{ 'i', 'I', C('i') }, /* Ins */
{ '.', ':', ':' } /* Del */
},
ray_numpad[PADKEYS] = {
{ '7', M('7'), '7' }, /* 7 */
{ '8', M('8'), '8' }, /* 8 */
{ '9', M('9'), '9' }, /* 9 */
{ 'm', C('p'), C('p') }, /* - */
{ '4', M('4'), '4' }, /* 4 */
{ 'g', 'G', 'g' }, /* 5 */
{ '6', M('6'), '6' }, /* 6 */
{ '+', 'P', C('p') }, /* + */
{ '1', M('1'), '1' }, /* 1 */
{ '2', M('2'), '2' }, /* 2 */
{ '3', M('3'), '3' }, /* 3 */
{ 'i', 'I', C('i') }, /* Ins */
{ '.', ':', ':' } /* Del */
};
static const struct pad
nh340_keypad[PADKEYS] = {
{ 'y', 'Y', C('y') }, /* 7 */
{ 'k', 'K', C('k') }, /* 8 */
{ 'u', 'U', C('u') }, /* 9 */
{ 'm', C('p'), C('p') }, /* - */
{ 'h', 'H', C('h') }, /* 4 */
{ 'g', 'G', 'g' }, /* 5 */
{ 'l', 'L', C('l') }, /* 6 */
{ '+', 'P', C('p') }, /* + */
{ 'b', 'B', C('b') }, /* 1 */
{ 'j', 'J', C('j') }, /* 2 */
{ 'n', 'N', C('n') }, /* 3 */
{ 'i', 'I', C('i') }, /* Ins */
{ '.', ':', ':' } /* Del */
},
nh340_numpad[PADKEYS] = {
{ '7', M('7'), '7' }, /* 7 */
{ '8', M('8'), '8' }, /* 8 */
{ '9', M('9'), '9' }, /* 9 */
{ 'm', C('p'), C('p') }, /* - */
{ '4', M('4'), '4' }, /* 4 */
{ 'g', 'G', 'g' }, /* 5 */
{ '6', M('6'), '6' }, /* 6 */
{ '+', 'P', C('p') }, /* + */
{ '1', M('1'), '1' }, /* 1 */
{ '2', M('2'), '2' }, /* 2 */
{ '3', M('3'), '3' }, /* 3 */
{ 'i', 'I', C('i') }, /* Ins */
{ '.', ':', ':' } /* Del */
};
static struct pad keypad[PADKEYS], numpad[PADKEYS];
static BYTE KeyState[256];
static const char default_name[] = "default";
const char *const legal_key_handling[] = {
"none",
"default",
"ray",
"340",
};
enum windows_key_handling keyh[] = { no_keyhandling, default_keyhandling, ray_keyhandling,
nh340_keyhandling };
void set_altkeyhandling(const char *inName)
{
int i, k;
/*backward compatibility - so people's existing config files
may work as is */
if (!strcmpi(inName, "nhraykey.dll"))
inName = legal_key_handling[ray_keyhandling];
else if (!strcmpi(inName, "nh340key.dll"))
inName = legal_key_handling[nh340_keyhandling];
else if (!strcmpi(inName, "nhdefkey.dll"))
inName = legal_key_handling[default_keyhandling];
for (i = default_keyhandling; i < SIZE(legal_key_handling); i++) {
if (!strcmpi(inName, legal_key_handling[i])) {
iflags.key_handling = keyh[i];
if (keyboard_handling.pKeyHandlingName) {
free((genericptr_t) keyboard_handling.pKeyHandlingName);
keyboard_handling.pKeyHandlingName = (char *) 0;
}
switch(iflags.key_handling) {
case ray_keyhandling:
keyboard_handling.pKeyHandlingName = strdup("ray");
keyboard_handling.pProcessKeystroke = ray_processkeystroke;
keyboard_handling.pNHkbhit = ray_kbhit;
keyboard_handling.pCheckInput = ray_checkinput;
/* A bogus key that will be filtered when received, to keep ReadConsole
* from blocking */
bogus_key.EventType = KEY_EVENT;
bogus_key.Event.KeyEvent.bKeyDown = 1;
bogus_key.Event.KeyEvent.wRepeatCount = 1;
bogus_key.Event.KeyEvent.wVirtualKeyCode = 0;
bogus_key.Event.KeyEvent.wVirtualScanCode = 0;
bogus_key.Event.KeyEvent.uChar.AsciiChar = (uchar) 0x80;
bogus_key.Event.KeyEvent.dwControlKeyState = 0;
for (k = 0; k < SIZE(keypad); ++k) {
keypad[k] = ray_keypad[k];
numpad[k] = ray_numpad[k];
}
break;
case nh340_keyhandling:
keyboard_handling.pKeyHandlingName = strdup("340");
keyboard_handling.pProcessKeystroke = nh340_processkeystroke;
keyboard_handling.pNHkbhit = nh340_kbhit;
keyboard_handling.pCheckInput = nh340_checkinput;
for (k = 0; k < SIZE(keypad); ++k) {
keypad[k] = nh340_keypad[k];
numpad[k] = nh340_numpad[k];
}
break;
case default_keyhandling:
default:
keyboard_handling.pKeyHandlingName = strdup("default");
keyboard_handling.pProcessKeystroke
= default_processkeystroke;
keyboard_handling.pNHkbhit = default_kbhit;
keyboard_handling.pCheckInput = default_checkinput;
for (k = 0; k < SIZE(keypad); ++k) {
keypad[k] = default_keypad[k];
numpad[k] = default_numpad[k];
}
break;
}
return;
}
}
config_error_add("invalid altkeyhandling '%s'", inName);
return;
}
int
set_keyhandling_via_option(void)
{
winid tmpwin;
anything any;
int i;
menu_item *console_key_handling_pick = (menu_item *) 0;
tmpwin = create_nhwindow(NHW_MENU);
start_menu(tmpwin);
any = zeroany;
for (i = default_keyhandling; i < SIZE(legal_key_handling); i++) {
any.a_int = i + 1;
add_menu(tmpwin, 0, &any, 'a' + i,
0, ATR_NONE,
legal_key_handling[i], 0);
}
end_menu(tmpwin, "Select windows console key handling:");
if (select_menu(tmpwin, PICK_ONE, &console_key_handling_pick) > 0) {
iflags.key_handling = keyh[console_key_handling_pick->item.a_int - 1];
free((genericptr_t) console_key_handling_pick);
}
destroy_nhwindow(tmpwin);
set_altkeyhandling(legal_key_handling[iflags.key_handling]);
return 1; /* optn_ok */
}
int
default_processkeystroke(
HANDLE hConIn,
INPUT_RECORD* ir,
boolean* valid,
boolean numberpad,
int portdebug)
{
int k = 0;
int keycode, vk;
unsigned char ch, pre_ch;
unsigned short int scan;
unsigned long shiftstate;
int altseq = 0;
const struct pad *kpad;
#ifdef QWERTZ_SUPPORT
if (numberpad & 0x10) {
numberpad &= ~0x10;
qwertz = TRUE;
} else {
qwertz = FALSE;
}
#endif
shiftstate = 0L;
ch = pre_ch = ir->Event.KeyEvent.uChar.AsciiChar;
scan = ir->Event.KeyEvent.wVirtualScanCode;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
keycode = MapVirtualKey(vk, 2);
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
KeyState[VK_SHIFT] = (shiftstate & SHIFT_PRESSED) ? 0x81 : 0;
KeyState[VK_CONTROL] =
(shiftstate & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) ? 0x81 : 0;
KeyState[VK_CAPITAL] = (shiftstate & CAPSLOCK_ON) ? 0x81 : 0;
if (shiftstate & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
if (ch || inmap(keycode, vk))
altseq = 1;
else
altseq = -1; /* invalid altseq */
}
if (ch || (iskeypad(scan)) || (altseq > 0))
*valid = TRUE;
/* if (!valid) return 0; */
/*
* shiftstate can be checked to see if various special
* keys were pressed at the same time as the key.
* Currently we are using the ALT & SHIFT & CONTROLS.
*
* RIGHT_ALT_PRESSED, LEFT_ALT_PRESSED,
* RIGHT_CTRL_PRESSED, LEFT_CTRL_PRESSED,
* SHIFT_PRESSED,NUMLOCK_ON, SCROLLLOCK_ON,
* CAPSLOCK_ON, ENHANCED_KEY
*
* are all valid bit masks to use on shiftstate.
* eg. (shiftstate & LEFT_CTRL_PRESSED) is true if the
* left control key was pressed with the keystroke.
*/
if (iskeypad(scan)) {
kpad = numberpad ? numpad : keypad;
if (shiftstate & SHIFT_PRESSED) {
ch = kpad[scan - KEYPADLO].shift;
} else if (shiftstate & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
ch = kpad[scan - KEYPADLO].cntrl;
} else {
ch = kpad[scan - KEYPADLO].normal;
}
#ifdef QWERTZ_SUPPORT
/* OPTIONS=number_pad:-1 is for qwertz keyboard; for that setting,
'numberpad' will be 0; core swaps y to zap, z to move northwest;
we want numpad 7 to move northwest, so when qwertz is set,
tell core that user who types numpad 7 typed z rather than y */
if (qwertz && kpad[scan - KEYPADLO].normal == 'y')
ch += 1; /* changes y to z, Y to Z, ^Y to ^Z */
#endif /*QWERTZ_SUPPORT*/
} else if (altseq > 0) { /* ALT sequence */
if (vk == 0xBF)
ch = M('?');
else
ch = M(tolower((uchar) keycode));
}
/* Attempt to work better with international keyboards. */
else {
WORD chr[2];
k = ToAscii(vk, scan, KeyState, chr, 0);
if (k <= 2)
switch (k) {
case 2: /* two characters */
ch = (unsigned char) chr[1];
*valid = TRUE;
break;
case 1: /* one character */
ch = (unsigned char) chr[0];
*valid = TRUE;
break;
case 0: /* no translation */
default: /* negative */
*valid = FALSE;
}
}
if (ch == '\r')
ch = '\n';
#ifdef PORT_DEBUG
if (portdebug) {
char buf[BUFSZ];
Sprintf(buf, "PORTDEBUG (%s): ch=%u, sc=%u, vk=%d, pre=%d, sh=0x%lX, "
"ta=%d (ESC to end)",
"default", ch, scan, vk, pre_ch, shiftstate, k);
fprintf(stdout, "\n%s", buf);
}
#endif
return ch;
}
int
default_kbhit(HANDLE hConIn, INPUT_RECORD *ir)
{
int done = 0; /* true = "stop searching" */
int retval; /* true = "we had a match" */
DWORD count;
unsigned short int scan;
unsigned char ch;
unsigned long shiftstate;
int altseq = 0, keycode, vk;
done = 0;
retval = 0;
while (!done) {
count = 0;
PeekConsoleInput(hConIn, ir, 1, &count);
if (count > 0) {
if (ir->EventType == KEY_EVENT && ir->Event.KeyEvent.bKeyDown) {
ch = ir->Event.KeyEvent.uChar.AsciiChar;
scan = ir->Event.KeyEvent.wVirtualScanCode;
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
keycode = MapVirtualKey(vk, 2);
if (shiftstate & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
if (ch || inmap(keycode, vk))
altseq = 1;
else
altseq = -1; /* invalid altseq */
}
if (ch || iskeypad(scan) || altseq) {
done = 1; /* Stop looking */
retval = 1; /* Found what we sought */
} else {
/* Strange Key event; let's purge it to avoid trouble */
ReadConsoleInput(hConIn, ir, 1, &count);
}
} else if ((ir->EventType == MOUSE_EVENT
&& (ir->Event.MouseEvent.dwButtonState
& MOUSEMASK))) {
done = 1;
retval = 1;
}
else /* Discard it, it's an insignificant event */
ReadConsoleInput(hConIn, ir, 1, &count);
} else /* There are no events in console event queue */ {
done = 1; /* Stop looking */
retval = 0;
}
}
return retval;
}
int
default_checkinput(
HANDLE hConIn,
INPUT_RECORD *ir,
DWORD* count,
boolean numpad,
int mode,
int *mod,
coord *cc)
{
#if defined(SAFERHANGUP)
DWORD dwWait;
#endif
int ch = 0;
boolean valid = 0, done = 0;
#ifdef QWERTZ_SUPPORT
if (numpad & 0x10) {
numpad &= ~0x10;
qwertz = TRUE;
} else {
qwertz = FALSE;
}
#endif
while (!done) {
#if defined(SAFERHANGUP)
dwWait = WaitForSingleObjectEx(hConIn, // event object to wait for
INFINITE, // waits indefinitely
TRUE); // alertable wait enabled
if (dwWait == WAIT_FAILED)
return '\033';
#endif
ReadConsoleInput(hConIn, ir, 1, count);
if (mode == 0) {
if ((ir->EventType == KEY_EVENT) && ir->Event.KeyEvent.bKeyDown) {
ch = default_processkeystroke(hConIn, ir, &valid, numpad, 0);
done = valid;
}
} else {
if (count > 0) {
if (ir->EventType == KEY_EVENT
&& ir->Event.KeyEvent.bKeyDown) {
#ifdef QWERTZ_SUPPORT
if (qwertz)
numpad |= 0x10;
#endif
ch = default_processkeystroke(hConIn, ir, &valid, numpad, 0);
#ifdef QWERTZ_SUPPORT
numpad &= ~0x10;
#endif
if (valid)
return ch;
} else if (ir->EventType == MOUSE_EVENT) {
if ((ir->Event.MouseEvent.dwEventFlags == 0)
&& (ir->Event.MouseEvent.dwButtonState & MOUSEMASK)) {
cc->x = ir->Event.MouseEvent.dwMousePosition.X + 1;
cc->y = ir->Event.MouseEvent.dwMousePosition.Y - 1;
if (ir->Event.MouseEvent.dwButtonState & LEFTBUTTON)
*mod = CLICK_1;
else if (ir->Event.MouseEvent.dwButtonState
& RIGHTBUTTON)
*mod = CLICK_2;
#if 0 /* middle button */
else if (ir->Event.MouseEvent.dwButtonState & MIDBUTTON)
*mod = CLICK_3;
#endif
return 0;
}
}
} else
done = 1;
}
}
return mode ? 0 : ch;
}
/*
* Keystroke handling contributed by Ray Chason.
* The following text was written by Ray Chason.
*
* The problem
* ===========
*
* The console-mode Nethack wants both keyboard and mouse input. The
* problem is that the Windows API provides no easy way to get mouse input
* and also keyboard input properly translated according to the user's
* chosen keyboard layout.
*
* The ReadConsoleInput function returns a stream of keyboard and mouse
* events. Nethack is interested in those events that represent a key
* pressed, or a click on a mouse button. The keyboard events from
* ReadConsoleInput are not translated according to the keyboard layout,
* and do not take into account the shift, control, or alt keys.
*
* The PeekConsoleInput function works similarly to ReadConsoleInput,
* except that it does not remove an event from the queue and it returns
* instead of blocking when the queue is empty.
*
* A program can also use ReadConsole to get a properly translated stream
* of characters. Unfortunately, ReadConsole does not return mouse events,
* does not distinguish the keypad from the main keyboard, does not return
* keys shifted with Alt, and does not even return the ESC key when
* pressed.
*
* We want both the functionality of ReadConsole and the functionality of
* ReadConsoleInput. But Microsoft didn't seem to think of that.
*
*
* The solution, in the original code
* ==================================
*
* The original 3.4.1 distribution tries to get proper keyboard translation
* by passing keyboard events to the ToAscii function. This works, to some
* extent -- it takes the shift key into account, and it processes dead
* keys properly. But it doesn't take non-US keyboards into account. It
* appears that ToAscii is meant for windowed applications, and does not
* have enough information to do its job properly in a console application.
*
*
* The Finnish keyboard patch
* ==========================
*
* This patch adds the "subkeyvalue" option to the defaults.nh file. The
* user can then add OPTIONS=sukeyvalue:171/92, for instance, to replace
* the 171 character with 92, which is \. This works, once properly
* configured, but places too much burden on the user. It also bars the
* use of the substituted characters in naming objects or monsters.
*
*
* The solution presented here
* ===========================
*
* The best way I could find to combine the functionality of ReadConsole
* with that of ReadConsoleInput is simple in concept. First, call
* PeekConsoleInput to get the first event. If it represents a key press,
* call ReadConsole to retrieve the key. Otherwise, pop it off the queue
* with ReadConsoleInput and, if it's a mouse click, return it as such.
*
* But the Devil, as they say, is in the details. The problem is in
* recognizing an event that ReadConsole will return as a key. We don't
* want to call ReadConsole unless we know that it will immediately return:
* if it blocks, the mouse and the Alt sequences will cease to function
* until it returns.
*
* Separating process_keystroke into two functions, one for commands and a
* new one, process_keystroke2, for answering prompts, makes the job a lot
* easier. process_keystroke2 doesn't have to worry about mouse events or
* Alt sequences, and so the consequences are minor if ReadConsole blocks.
* process_keystroke, OTOH, never needs to return a non-ASCII character
* that was read from ReadConsole; it returns bytes with the high bit set
* only in response to an Alt sequence.
*
* So in process_keystroke, before calling ReadConsole, a bogus key event
* is pushed on the queue. This event causes ReadConsole to return, even
* if there was no other character available. Because the bogus key has
* the eighth bit set, it is filtered out. This is not done in
* process_keystroke2, because that would render dead keys unusable.
*
* A separate process_keystroke2 can also process the numeric keypad in a
* way that makes sense for prompts: just return the corresponding symbol,
* and pay no mind to number_pad or the num lock key.
*
* The recognition of Alt sequences is modified, to support the use of
* characters generated with the AltGr key. A keystroke is an Alt sequence
* if an Alt key is seen that can't be an AltGr (since an AltGr sequence
* could be a character, and in some layouts it could even be an ASCII
* character). This recognition is different on NT-based and 95-based
* Windows:
*
* * On NT-based Windows, AltGr signals as right Alt and left Ctrl
* together. So an Alt sequence is recognized if either Alt key is
* pressed and if right Alt and left Ctrl are not both present. This
* is true even if the keyboard in use does not have an AltGr key, and
* uses right Alt for AltGr.
*
* * On 95-based Windows, with a keyboard that lacks the AltGr key, the
* right Alt key is used instead. But it still signals as right Alt,
* without left Ctrl. There is no way for the application to know
* whether right Alt is Alt or AltGr, and so it is always assumed
* to be AltGr. This means that Alt sequences must be formed with
* left Alt.
*
* So the patch processes keystrokes as follows:
*
* * If the scan and virtual key codes are both 0, it's the bogus key,
* and we ignore it.
*
* * Keys on the numeric keypad are processed for commands as in the
* unpatched Nethack, and for prompts by returning the ASCII
* character, even if the num lock is off.
*
* * Alt sequences are processed for commands as in the unpatched
* Nethack, and ignored for prompts.
*
* * Control codes are returned as received, because ReadConsole will
* not return the ESC key.
*
* * Other key-down events are passed to ReadConsole. The use of
* ReadConsole is different for commands than for prompts:
*
* o For commands, the bogus key is pushed onto the queue before
* ReadConsole is called. On return, non-ASCII characters are
* filtered, so they are not mistaken for Alt sequences; this also
* filters the bogus key.
*
* o For prompts, the bogus key is not used, because that would
* interfere with dead keys. Eight bit characters may be returned,
* and are coded in the configured code page.
*
*
* Possible improvements
* =====================
*
* Some possible improvements remain:
*
* * Integrate the existing Finnish keyboard patch, for use with non-
* QWERTY layouts such as the German QWERTZ keyboard or Dvorak.
*
* * Fix the keyboard glitches in the graphical version. Namely, dead
* keys don't work, and input comes in as ISO-8859-1 but is displayed
* as code page 437 if IBMgraphics is set on startup.
*
* * Transform incoming text to ISO-8859-1, for full compatibility with
* the graphical version.
*
* * After pushing the bogus key and calling ReadConsole, check to see
* if we got the bogus key; if so, and an Alt is pressed, process the
* event as an Alt sequence.
*
*/
#if 0
static char where_to_get_source[] = "http://www.nethack.org/";
static char author[] = "Ray Chason";
#endif
int process_keystroke2(HANDLE hConIn, INPUT_RECORD *ir, boolean *valid);
/* Use ray_processkeystroke for key commands, process_keystroke2 for prompts */
/* int ray_processkeystroke(INPUT_RECORD *ir, boolean *valid, int
* portdebug);
*/
int process_keystroke2(HANDLE, INPUT_RECORD *ir, boolean *valid);
static int is_altseq(unsigned long shiftstate);
static int
is_altseq(unsigned long shiftstate)
{
/* We need to distinguish the Alt keys from the AltGr key.
* On NT-based Windows, AltGr signals as right Alt and left Ctrl together;
* on 95-based Windows, AltGr signals as right Alt only.
* So on NT, we signal Alt if either Alt is pressed and left Ctrl is not,
* and on 95, we signal Alt for left Alt only. */
switch (shiftstate
& (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED | LEFT_CTRL_PRESSED)) {
case LEFT_ALT_PRESSED:
case LEFT_ALT_PRESSED | LEFT_CTRL_PRESSED:
return 1;
case RIGHT_ALT_PRESSED:
case RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED:
return (GetVersion() & 0x80000000) == 0;
default:
return 0;
}
}
int ray_processkeystroke(
HANDLE hConIn,
INPUT_RECORD *ir,
boolean *valid,
boolean numberpad,
int portdebug)
{
int keycode, vk;
unsigned char ch, pre_ch;
unsigned short int scan;
unsigned long shiftstate;
int altseq = 0;
const struct pad *kpad;
DWORD count;
#ifdef QWERTZ_SUPPORT
if (numberpad & 0x10) {
numberpad &= ~0x10;
qwertz = TRUE;
} else {
qwertz = FALSE;
}
#endif
shiftstate = 0L;
ch = pre_ch = ir->Event.KeyEvent.uChar.AsciiChar;
scan = ir->Event.KeyEvent.wVirtualScanCode;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
keycode = MapVirtualKey(vk, 2);
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
if (scan == 0 && vk == 0) {
/* It's the bogus_key */
ReadConsoleInput(hConIn, ir, 1, &count);
*valid = FALSE;
return 0;
}
if (is_altseq(shiftstate)) {
if (ch || inmap(keycode, vk))
altseq = 1;
else
altseq = -1; /* invalid altseq */
}
if (ch || (iskeypad(scan)) || (altseq > 0))
*valid = TRUE;
/* if (!valid) return 0; */
/*
* shiftstate can be checked to see if various special
* keys were pressed at the same time as the key.
* Currently we are using the ALT & SHIFT & CONTROLS.
*
* RIGHT_ALT_PRESSED, LEFT_ALT_PRESSED,
* RIGHT_CTRL_PRESSED, LEFT_CTRL_PRESSED,
* SHIFT_PRESSED,NUMLOCK_ON, SCROLLLOCK_ON,
* CAPSLOCK_ON, ENHANCED_KEY
*
* are all valid bit masks to use on shiftstate.
* eg. (shiftstate & LEFT_CTRL_PRESSED) is true if the
* left control key was pressed with the keystroke.
*/
if (iskeypad(scan)) {
ReadConsoleInput(hConIn, ir, 1, &count);
kpad = numberpad ? numpad : keypad;
if (shiftstate & SHIFT_PRESSED) {
ch = kpad[scan - KEYPADLO].shift;
} else if (shiftstate & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
ch = kpad[scan - KEYPADLO].cntrl;
} else {
ch = kpad[scan - KEYPADLO].normal;
}
#ifdef QWERTZ_SUPPORT
/* OPTIONS=number_pad:-1 is for qwertz keyboard; for that setting,
'numberpad' will be 0; core swaps y to zap, z to move northwest;
we want numpad 7 to move northwest, so when qwertz is set,
tell core that user who types numpad 7 typed z rather than y */
if (qwertz && kpad[scan - KEYPADLO].normal == 'y')
ch += 1; /* changes y to z, Y to Z, ^Y to ^Z */
#endif /*QWERTZ_SUPPORT*/
} else if (altseq > 0) { /* ALT sequence */
ReadConsoleInput(hConIn, ir, 1, &count);
if (vk == 0xBF)
ch = M('?');
else
ch = M(tolower((uchar) keycode));
} else if (ch < 32 && !isnumkeypad(scan)) {
/* Control code; ReadConsole seems to filter some of these,
* including ESC */
ReadConsoleInput(hConIn, ir, 1, &count);
}
/* Attempt to work better with international keyboards. */
else {
CHAR ch2;
DWORD written;
/* The bogus_key guarantees that ReadConsole will return,
* and does not itself do anything */
WriteConsoleInput(hConIn, &bogus_key, 1, &written);
ReadConsole(hConIn, &ch2, 1, &count, NULL);
/* Prevent high characters from being interpreted as alt
* sequences; also filter the bogus_key */
if (ch2 & 0x80)
*valid = FALSE;
else
ch = ch2;
if (ch == 0)
*valid = FALSE;
}
if (ch == '\r')
ch = '\n';
#ifdef PORT_DEBUG
if (portdebug) {
char buf[BUFSZ];
Sprintf(buf, "PORTDEBUG: ch=%u, scan=%u, vk=%d, pre=%d, "
"shiftstate=0x%lX (ESC to end)\n",
ch, scan, vk, pre_ch, shiftstate);
fprintf(stdout, "\n%s", buf);
}
#endif
return ch;
}
int
process_keystroke2(
HANDLE hConIn,
INPUT_RECORD *ir,
boolean *valid)
{
/* Use these values for the numeric keypad */
static const char keypad_nums[] = "789-456+1230.";
unsigned char ch;
int vk;
unsigned short int scan;
unsigned long shiftstate;
int altseq;
DWORD count;
ch = ir->Event.KeyEvent.uChar.AsciiChar;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
scan = ir->Event.KeyEvent.wVirtualScanCode;
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
if (scan == 0 && vk == 0) {
/* It's the bogus_key */
ReadConsoleInput(hConIn, ir, 1, &count);
*valid = FALSE;
return 0;
}
altseq = is_altseq(shiftstate);
if (ch || (iskeypad(scan)) || altseq)
*valid = TRUE;
/* if (!valid) return 0; */
/*
* shiftstate can be checked to see if various special
* keys were pressed at the same time as the key.
* Currently we are using the ALT & SHIFT & CONTROLS.
*
* RIGHT_ALT_PRESSED, LEFT_ALT_PRESSED,
* RIGHT_CTRL_PRESSED, LEFT_CTRL_PRESSED,
* SHIFT_PRESSED,NUMLOCK_ON, SCROLLLOCK_ON,
* CAPSLOCK_ON, ENHANCED_KEY
*
* are all valid bit masks to use on shiftstate.
* eg. (shiftstate & LEFT_CTRL_PRESSED) is true if the
* left control key was pressed with the keystroke.
*/
if (iskeypad(scan) && !altseq) {
ReadConsoleInput(hConIn, ir, 1, &count);
ch = keypad_nums[scan - KEYPADLO];
} else if (ch < 32 && !isnumkeypad(scan)) {
/* Control code; ReadConsole seems to filter some of these,
* including ESC */
ReadConsoleInput(hConIn, ir, 1, &count);
}
/* Attempt to work better with international keyboards. */
else {
CHAR ch2;
ReadConsole(hConIn, &ch2, 1, &count, NULL);
ch = ch2 & 0xFF;
if (ch == 0)
*valid = FALSE;
}
if (ch == '\r')
ch = '\n';
return ch;
}
int
ray_checkinput(
HANDLE hConIn,
INPUT_RECORD *ir,
DWORD *count,
boolean numpad,
int mode,
int *mod,
coord *cc)
{
#if defined(SAFERHANGUP)
DWORD dwWait;
#endif
int ch = 0;
boolean valid = 0, done = 0;
#ifdef QWERTZ_SUPPORT
if (numpad & 0x10) {
numpad &= ~0x10;
qwertz = TRUE;
} else {
qwertz = FALSE;
}
#endif
while (!done) {
*count = 0;
dwWait = WaitForSingleObject(hConIn, INFINITE);
#if defined(SAFERHANGUP)
if (dwWait == WAIT_FAILED)
return '\033';
#endif
PeekConsoleInput(hConIn, ir, 1, count);
if (mode == 0) {
if ((ir->EventType == KEY_EVENT) && ir->Event.KeyEvent.bKeyDown) {
ch = process_keystroke2(hConIn, ir, &valid);
done = valid;
} else
ReadConsoleInput(hConIn, ir, 1, count);
} else {
ch = 0;
if (count > 0) {
if (ir->EventType == KEY_EVENT
&& ir->Event.KeyEvent.bKeyDown) {
#ifdef QWERTZ_SUPPORT
if (qwertz)
numpad |= 0x10;
#endif
ch = ray_processkeystroke(hConIn, ir, &valid, numpad,
#ifdef PORTDEBUG
1);
#else
0);
#endif
#ifdef QWERTZ_SUPPORT
numpad &= ~0x10;
#endif
if (valid)
return ch;
} else {
ReadConsoleInput(hConIn, ir, 1, count);
if (ir->EventType == MOUSE_EVENT) {
if ((ir->Event.MouseEvent.dwEventFlags == 0)
&& (ir->Event.MouseEvent.dwButtonState
& MOUSEMASK)) {
cc->x =
ir->Event.MouseEvent.dwMousePosition.X + 1;
cc->y =
ir->Event.MouseEvent.dwMousePosition.Y - 1;
if (ir->Event.MouseEvent.dwButtonState
& LEFTBUTTON)
*mod = CLICK_1;
else if (ir->Event.MouseEvent.dwButtonState
& RIGHTBUTTON)
*mod = CLICK_2;
#if 0 /* middle button */
else if (ir->Event.MouseEvent.dwButtonState & MIDBUTTON)
*mod = CLICK_3;
#endif
return 0;
}
}
#if 0
/* We ignore these types of console events */
else if (ir->EventType == FOCUS_EVENT) {
}
else if (ir->EventType == MENU_EVENT) {
}
#endif
}
} else
done = 1;
}
}
*mod = 0;
return ch;
}
int
ray_kbhit(
HANDLE hConIn,
INPUT_RECORD *ir)
{
int done = 0; /* true = "stop searching" */
int retval; /* true = "we had a match" */
DWORD count;
unsigned short int scan;
unsigned char ch;
unsigned long shiftstate;
int altseq = 0, keycode, vk;
done = 0;
retval = 0;
while (!done) {
count = 0;
PeekConsoleInput(hConIn, ir, 1, &count);
if (count > 0) {
if (ir->EventType == KEY_EVENT && ir->Event.KeyEvent.bKeyDown) {
ch = ir->Event.KeyEvent.uChar.AsciiChar;
scan = ir->Event.KeyEvent.wVirtualScanCode;
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
if (scan == 0 && vk == 0) {
/* It's the bogus_key. Discard it */
ReadConsoleInput(hConIn,ir,1,&count);
} else {
keycode = MapVirtualKey(vk, 2);
if (is_altseq(shiftstate)) {
if (ch || inmap(keycode, vk))
altseq = 1;
else
altseq = -1; /* invalid altseq */
}
if (ch || iskeypad(scan) || altseq) {
done = 1; /* Stop looking */
retval = 1; /* Found what we sought */
} else {
/* Strange Key event; let's purge it to avoid trouble */
ReadConsoleInput(hConIn, ir, 1, &count);
}
}
} else if ((ir->EventType == MOUSE_EVENT
&& (ir->Event.MouseEvent.dwButtonState
& MOUSEMASK))) {
done = 1;
retval = 1;
}
else /* Discard it, it's an insignificant event */
ReadConsoleInput(hConIn, ir, 1, &count);
} else /* There are no events in console event queue */ {
done = 1; /* Stop looking */
retval = 0;
}
}
return retval;
}
/*
* nh340 key handling
*
* This is the NetHack keystroke processing from NetHack 3.4.0.
* It can be built as a run-time loadable dll (nh340key.dll),
* placed in the same directory as the nethack.exe executable,
* and loaded by specifying OPTIONS=altkeyhandler:nh340key
* in defaults.nh
*
* Keypad keys are translated to the normal values below.
* Shifted keypad keys are translated to the
* shift values below.
*/
int
nh340_processkeystroke(
HANDLE hConIn,
INPUT_RECORD *ir,
boolean *valid,
boolean numberpad,
int portdebug)
{
int keycode, vk;
unsigned char ch, pre_ch;
unsigned short int scan;
unsigned long shiftstate;
int altseq = 0;
const struct pad *kpad;
#ifdef QWERTZ_SUPPORT
if (numberpad & 0x10) {
numberpad &= ~0x10;
qwertz = TRUE;
} else {
qwertz = FALSE;
}
#endif
shiftstate = 0L;
ch = pre_ch = ir->Event.KeyEvent.uChar.AsciiChar;
scan = ir->Event.KeyEvent.wVirtualScanCode;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
keycode = MapVirtualKey(vk, 2);
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
if (shiftstate & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
if (ch || inmap(keycode, vk))
altseq = 1;
else
altseq = -1; /* invalid altseq */
}
if (ch || (iskeypad(scan)) || (altseq > 0))
*valid = TRUE;
/* if (!valid) return 0; */
/*
* shiftstate can be checked to see if various special
* keys were pressed at the same time as the key.
* Currently we are using the ALT & SHIFT & CONTROLS.
*
* RIGHT_ALT_PRESSED, LEFT_ALT_PRESSED,
* RIGHT_CTRL_PRESSED, LEFT_CTRL_PRESSED,
* SHIFT_PRESSED,NUMLOCK_ON, SCROLLLOCK_ON,
* CAPSLOCK_ON, ENHANCED_KEY
*
* are all valid bit masks to use on shiftstate.
* eg. (shiftstate & LEFT_CTRL_PRESSED) is true if the
* left control key was pressed with the keystroke.
*/
if (iskeypad(scan)) {
kpad = numberpad ? numpad : keypad;
if (shiftstate & SHIFT_PRESSED) {
ch = kpad[scan - KEYPADLO].shift;
} else if (shiftstate & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
ch = kpad[scan - KEYPADLO].cntrl;
} else {
ch = kpad[scan - KEYPADLO].normal;
}
#ifdef QWERTZ_SUPPORT
/* OPTIONS=number_pad:-1 is for qwertz keyboard; for that setting,
'numberpad' will be 0; core swaps y to zap, z to move northwest;
we want numpad 7 to move northwest, so when qwertz is set,
tell core that user who types numpad 7 typed z rather than y */
if (qwertz && kpad[scan - KEYPADLO].normal == 'y')
ch += 1; /* changes y to z, Y to Z, ^Y to ^Z */
#endif /*QWERTZ_SUPPORT*/
} else if (altseq > 0) { /* ALT sequence */
if (vk == 0xBF)
ch = M('?');
else
ch = M(tolower((uchar) keycode));
}
if (ch == '\r')
ch = '\n';
#ifdef PORT_DEBUG
if (portdebug) {
char buf[BUFSZ];
Sprintf(buf,
"PORTDEBUG (%s): ch=%u, sc=%u, vk=%d, sh=0x%lX (ESC to end)",
"nh340", ch, scan, vk, shiftstate);
fprintf(stdout, "\n%s", buf);
}
#endif
return ch;
}
int
nh340_kbhit(
HANDLE hConIn,
INPUT_RECORD *ir)
{
int done = 0; /* true = "stop searching" */
int retval; /* true = "we had a match" */
DWORD count;
unsigned short int scan;
unsigned char ch;
unsigned long shiftstate;
int altseq = 0, keycode, vk;
done = 0;
retval = 0;
while (!done) {
count = 0;
PeekConsoleInput(hConIn, ir, 1, &count);
if (count > 0) {
if (ir->EventType == KEY_EVENT && ir->Event.KeyEvent.bKeyDown) {
ch = ir->Event.KeyEvent.uChar.AsciiChar;
scan = ir->Event.KeyEvent.wVirtualScanCode;
shiftstate = ir->Event.KeyEvent.dwControlKeyState;
vk = ir->Event.KeyEvent.wVirtualKeyCode;
keycode = MapVirtualKey(vk, 2);
if (shiftstate & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
if (ch || inmap(keycode, vk))
altseq = 1;
else
altseq = -1; /* invalid altseq */
}
if (ch || iskeypad(scan) || altseq) {
done = 1; /* Stop looking */
retval = 1; /* Found what we sought */
} else {
/* Strange Key event; let's purge it to avoid trouble */
ReadConsoleInput(hConIn, ir, 1, &count);
}
} else if ((ir->EventType == MOUSE_EVENT
&& (ir->Event.MouseEvent.dwButtonState
& MOUSEMASK))) {
done = 1;
retval = 1;
}
else /* Discard it, it's an insignificant event */
ReadConsoleInput(hConIn, ir, 1, &count);
} else /* There are no events in console event queue */ {
done = 1; /* Stop looking */
retval = 0;
}
}
return retval;
}
int
nh340_checkinput(
HANDLE hConIn,
INPUT_RECORD *ir,
DWORD *count,
boolean numpad,
int mode,
int *mod,
coord *cc)
{
#if defined(SAFERHANGUP)
DWORD dwWait;
#endif
int ch = 0;
boolean valid = 0, done = 0;
#ifdef QWERTZ_SUPPORT
if (numpad & 0x10) {
numpad &= ~0x10;
qwertz = TRUE;
} else {
qwertz = FALSE;
}
#endif
while (!done) {
#if defined(SAFERHANGUP)
dwWait = WaitForSingleObjectEx(hConIn, // event object to wait for
INFINITE, // waits indefinitely
TRUE); // alertable wait enabled
if (dwWait == WAIT_FAILED)
return '\033';
#endif
ReadConsoleInput(hConIn, ir, 1, count);
if (mode == 0) {
if ((ir->EventType == KEY_EVENT) && ir->Event.KeyEvent.bKeyDown) {
#ifdef QWERTZ_SUPPORT
if (qwertz)
numpad |= 0x10;
#endif
ch = nh340_processkeystroke(hConIn, ir, &valid, numpad, 0);
#ifdef QWERTZ_SUPPORT
numpad &= ~0x10;
#endif
done = valid;
}
} else {
if (count > 0) {
if (ir->EventType == KEY_EVENT
&& ir->Event.KeyEvent.bKeyDown) {
ch = nh340_processkeystroke(hConIn, ir, &valid, numpad, 0);
if (valid)
return ch;
} else if (ir->EventType == MOUSE_EVENT) {
if ((ir->Event.MouseEvent.dwEventFlags == 0)
&& (ir->Event.MouseEvent.dwButtonState & MOUSEMASK)) {
cc->x = ir->Event.MouseEvent.dwMousePosition.X + 1;
cc->y = ir->Event.MouseEvent.dwMousePosition.Y - 1;
if (ir->Event.MouseEvent.dwButtonState & LEFTBUTTON)
*mod = CLICK_1;
else if (ir->Event.MouseEvent.dwButtonState
& RIGHTBUTTON)
*mod = CLICK_2;
#if 0 /* middle button */
else if (ir->Event.MouseEvent.dwButtonState & MIDBUTTON)
*mod = CLICK_3;
#endif
return 0;
}
}
} else
done = 1;
}
}
return mode ? 0 : ch;
}
#endif /* WIN32 */
|