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
|
/* File: z-term.c */
/*
* Copyright (c) 1997 Ben Harrison
*
* This software may be copied and distributed for educational, research,
* and not for profit purposes provided that this copyright and statement
* are included in all such copies.
*/
/* Purpose: a generic, efficient, terminal window package -BEN- */
#include "angband.h"
#include "z-term.h"
#include "z-virt.h"
/*
* This file provides a generic, efficient, terminal window package,
* which can be used not only on standard terminal environments such
* as dumb terminals connected to a Unix box, but also in more modern
* "graphic" environments, such as the Macintosh or Unix/X11.
*
* Each "window" works like a standard "dumb terminal", that is, it
* can display a two dimensional array of grids containing colored
* textual symbols, plus an optional cursor, and it can be used to
* get keypress events from the user.
*
* In fact, this package can simply be used, if desired, to support
* programs which will look the same on a dumb terminal as they do
* on a graphic platform such as the Macintosh.
*
* This package was designed to help port the game "Angband" to a wide
* variety of different platforms. Angband, like many other games in
* the "rogue-like" heirarchy, requires, at the minimum, the ability
* to display "colored textual symbols" in a standard 80x24 "window",
* such as that provided by most dumb terminals, and many old personal
* computers, and to check for "keypresses" from the user. The major
* concerns were thus portability and efficiency, so Angband could be
* easily ported to many different systems, with minimal effort, and
* yet would run quickly on each of these systems, no matter what kind
* of underlying hardware/software support was being used.
*
* It is important to understand the differences between the older
* "dumb terminals" and the newer "graphic interface" machines, since
* this package was designed to work with both types of systems.
*
* New machines:
* waiting for a keypress is complex
* checking for a keypress is often cheap
* changing "colors" may be expensive
* the "color" of a "blank" is rarely important
* moving the "cursor" is relatively cheap
* use a "software" cursor (only moves when requested)
* drawing characters normally will not erase old ones
* drawing a character on the cursor often erases it
* may have fast routines for "clear a region"
* the bottom right corner is usually not special
*
* Old machines:
* waiting for a keypress is simple
* checking for a keypress is often expensive
* changing "colors" is usually cheap
* the "color" of a "blank" may be important
* moving the "cursor" may be expensive
* use a "hardware" cursor (moves during screen updates)
* drawing new symbols automatically erases old ones
* characters may only be drawn at the cursor location
* drawing a character on the cursor will move the cursor
* may have fast routines for "clear entire window"
* may have fast routines for "clear to end of line"
* the bottom right corner is often dangerous
*
*
* This package provides support for multiple windows, each of an
* arbitrary size (up to 255x255), each with its own set of flags,
* and its own hooks to handle several low-level procedures which
* differ from platform to platform. Then the main program simply
* creates one or more "term" structures, setting the various flags
* and hooks in a manner appropriate for the current platform, and
* then it can use the various "term" structures without worrying
* about the underlying platform.
*
*
* This package allows each "grid" in each window to hold an attr/char
* pair, with each ranging from 0 to 255, and makes very few assumptions
* about the meaning of any attr/char values. Normally, we assume that
* "attr 0" is "black", with the semantics that "black" text should be
* sent to "Term_wipe()" instead of "Term_text()", but this sematics is
* modified if either the "always_pict" or the "always_text" flags are
* set. We assume that "char 0" is "dangerous", since placing such a
* "char" in the middle of a string "terminates" the string, and usually
* we prevent its use.
*
* Finally, we use a special attr/char pair, defaulting to "attr 0" and
* "char 32", also known as "black space", when we "erase" or "clear"
* any window, but this pair can be redefined to any pair, including
* the standard "white space", or the bizarre "emptiness" ("attr 0"
* and "char 0"), as long as various obscure restrictions are met.
*
*
* This package provides several functions which allow a program to
* interact with the "term" structures. Most of the functions allow
* the program to "request" certain changes to the current "term",
* such as moving the cursor, drawing an attr/char pair, erasing a
* region of grids, hiding the cursor, etc. Then there is a special
* function which causes all of the "pending" requests to be performed
* in an efficient manner. There is another set of functions which
* allow the program to query the "requested state" of the current
* "term", such as asking for the cursor location, or what attr/char
* is at a given location, etc. There is another set of functions
* dealing with "keypress" events, which allows the program to ask if
* the user has pressed any keys, or to forget any keys the user pressed.
* There is a pair of functions to allow this package to memorize the
* contents of the current "term", and to restore these contents at
* a later time. There is a special function which allows the program
* to specify which "term" structure should be the "current" one. At
* the lowest level, there is a set of functions which allow a new
* "term" to be initialized or destroyed, and which allow this package,
* or a program, to access the special "hooks" defined for the current
* "term", and a set of functions which those "hooks" can use to inform
* this package of the results of certain occurances, for example, one
* such function allows this package to learn about user keypresses,
* detected by one of the special "hooks".
*
* We provide, among other things, the functions "Term_keypress()"
* to "react" to keypress events, and "Term_redraw()" to redraw the
* entire window, plus "Term_resize()" to note a new size.
*
*
* Note that the current "term" contains two "window images". One of
* these images represents the "requested" contents of the "term", and
* the other represents the "actual" contents of the "term", at the time
* of the last performance of pending requests. This package uses these
* two images to determine the "minimal" amount of work needed to make
* the "actual" contents of the "term" match the "requested" contents of
* the "term". This method is not perfect, but it often reduces the
* amount of work needed to perform the pending requests, which thus
* increases the speed of the program itself. This package promises
* that the requested changes will appear to occur either "all at once"
* or in a "top to bottom" order. In addition, a "cursor" is maintained,
* and this cursor is updated along with the actual window contents.
*
* Currently, the "Term_fresh()" routine attempts to perform the "minimum"
* number of physical updates, in terms of total "work" done by the hooks
* Term_wipe(), Term_text(), and Term_pict(), making use of the fact that
* adjacent characters of the same color can both be drawn together using
* the "Term_text()" hook, and that "black" text can often be sent to the
* "Term_wipe()" hook instead of the "Term_text()" hook, and if something
* is already displayed in a window, then it is not necessary to display
* it again. Unfortunately, this may induce slightly non-optimal results
* in some cases, in particular, those in which, say, a string of ten
* characters needs to be written, but the fifth character has already
* been displayed. Currently, this will cause the "Term_text()" routine
* to be called once for each half of the string, instead of once for the
* whole string, which, on some machines, may be non-optimal behavior.
*
* The new formalism includes a "displayed" screen image (old) which
* is actually seen by the user, a "requested" screen image (scr)
* which is being prepared for display, a "memorized" screen image
* (mem) which is used to save and restore screen images, and a
* "temporary" screen image (tmp) which is currently unused.
*
*
* Several "flags" are available in each "term" to allow the underlying
* visual system (which initializes the "term" structure) to "optimize"
* the performance of this package for the given system, or to request
* certain behavior which is helpful/required for the given system.
*
* The "soft_cursor" flag indicates the use of a "soft" cursor, which
* only moves when explicitly requested,and which is "erased" when
* any characters are drawn on top of it. This flag is used for all
* "graphic" systems which handle the cursor by "drawing" it.
*
* The "icky_corner" flag indicates that the bottom right "corner"
* of the windows are "icky", and "printing" anything there may
* induce "messy" behavior, such as "scrolling". This flag is used
* for most old "dumb terminal" systems.
*
*
* The "term" structure contains the following function "hooks":
*
* Term->init_hook = Init the term
* Term->nuke_hook = Nuke the term
* Term->user_hook = Perform user actions
* Term->xtra_hook = Perform extra actions
* Term->curs_hook = Draw (or Move) the cursor
* Term->wipe_hook = Draw some blank spaces
* Term->text_hook = Draw some text in the window
* Term->pict_hook = Draw some attr/chars in the window
*
* The "Term->user_hook" hook provides a simple hook to an implementation
* defined function, with application defined semantics. It is available
* to the program via the "Term_user()" function.
*
* The "Term->xtra_hook" hook provides a variety of different functions,
* based on the first parameter (which should be taken from the various
* TERM_XTRA_* defines) and the second parameter (which may make sense
* only for some first parameters). It is available to the program via
* the "Term_xtra()" function, though some first parameters are only
* "legal" when called from inside this package.
*
* The "Term->curs_hook" hook provides this package with a simple way
* to "move" or "draw" the cursor to the grid "x,y", depending on the
* setting of the "soft_cursor" flag. Note that the cursor is never
* redrawn if "nothing" has happened to the screen (even temporarily).
* This hook is required.
*
* The "Term->wipe_hook" hook provides this package with a simple way
* to "erase", starting at "x,y", the next "n" grids. This hook assumes
* that the input is valid. This hook is required, unless the setting
* of the "always_pict" or "always_text" flags makes it optional.
*
* The "Term->text_hook" hook provides this package with a simple way
* to "draw", starting at "x,y", the "n" chars contained in "cp", using
* the attr "a". This hook assumes that the input is valid, and that
* "n" is between 1 and 256 inclusive, but it should NOT assume that
* the contents of "cp" are null-terminated. This hook is required,
* unless the setting of the "always_pict" flag makes it optional.
*
* The "Term->pict_hook" hook provides this package with a simple way
* to "draw", starting at "x,y", the "n" attr/char pairs contained in
* the arrays "ap" and "cp". This hook assumes that the input is valid,
* and that "n" is between 1 and 256 inclusive, but it should NOT assume
* that the contents of "cp" are null-terminated. This hook is optional,
* unless the setting of the "always_pict" or "higher_pict" flags make
* it required. Note that recently, this hook was changed from taking
* a byte "a" and a char "c" to taking a length "n", an array of bytes
* "ap" and an array of chars "cp". Old implementations of this hook
* should now iterate over all "n" attr/char pairs.
*
*
* The game "Angband" uses a set of files called "main-xxx.c", for
* various "xxx" suffixes. Most of these contain a function called
* "init_xxx()", that will prepare the underlying visual system for
* use with Angband, and then create one or more "term" structures,
* using flags and hooks appropriate to the given platform, so that
* the "main()" function can call one (or more) of the "init_xxx()"
* functions, as appropriate, to prepare the required "term" structs
* (one for each desired sub-window), and these "init_xxx()" functions
* are called from a centralized "main()" function in "main.c". Other
* "main-xxx.c" systems contain their own "main()" function which, in
* addition to doing everything needed to initialize the actual program,
* also does everything that the normal "init_xxx()" functions would do.
*
* The game "Angband" defines, in addition to "attr 0", all of the
* attr codes from 1 to 15, using definitions in "defines.h", and
* thus the "main-xxx.c" files used by Angband must handle these
* attr values correctly. Also, they must handle all other attr
* values, though they may do so in any way they wish, for example,
* by always taking every attr code mod 16. Many of the "main-xxx.c"
* files use "white space" ("attr 1" / "char 32") to "erase" or "clear"
* any window, for efficiency.
*
* The game "Angband" uses the "Term_user" hook to allow any of the
* "main-xxx.c" files to interact with the user, by calling this hook
* whenever the user presses the "!" key when the game is waiting for
* a new command. This could be used, for example, to provide "unix
* shell commands" to the Unix versions of the game.
*
* See "main-xxx.c" for a simple skeleton file which can be used to
* create a "visual system" for a new platform when porting Angband.
*/
/*
* The current "term"
*/
term *Term = NULL;
/* File handler for saving movies */
FILE *movfile = NULL;
int do_movies = 0; /* Later set this as a global */
/* set to 1 if you want movies made */
time_t lastc;
int last_paused = 0;
int cmovie_get_msecond(void);
/* Record cmovies with millisecond frame rate */
long cmov_last_time_msec;
long cmov_delta_time_msec;
/*** Local routines ***/
/*
* Nuke a term_win (see below)
*/
static errr term_win_nuke(term_win *s, int w, int h)
{
/* Free the window access arrays */
C_KILL(s->a, h, byte*);
C_KILL(s->c, h, char*);
/* Free the window content arrays */
C_KILL(s->va, h * w, byte);
C_KILL(s->vc, h * w, char);
/* Free the terrain access arrays */
C_KILL(s->ta, h, byte*);
C_KILL(s->tc, h, char*);
/* Free the terrain content arrays */
C_KILL(s->vta, h * w, byte);
C_KILL(s->vtc, h * w, char);
/* Free the ego graphics access arrays */
C_KILL(s->ea, h, byte*);
C_KILL(s->ec, h, char*);
/* Free the ego graphics content arrays */
C_KILL(s->vea, h * w, byte);
C_KILL(s->vec, h * w, char);
/* Success */
return (0);
}
/*
* Initialize a "term_win" (using the given window size)
*/
static errr term_win_init(term_win *s, int w, int h)
{
int y;
/* Make the window access arrays */
C_MAKE(s->a, h, byte*);
C_MAKE(s->c, h, char*);
/* Make the window content arrays */
C_MAKE(s->va, h * w, byte);
C_MAKE(s->vc, h * w, char);
/* Make the terrain access arrays */
C_MAKE(s->ta, h, byte*);
C_MAKE(s->tc, h, char*);
/* Make the terrain content arrays */
C_MAKE(s->vta, h * w, byte);
C_MAKE(s->vtc, h * w, char);
/* Make the ego graphics access arrays */
C_MAKE(s->ea, h, byte*);
C_MAKE(s->ec, h, char*);
/* Make the ego graphics content arrays */
C_MAKE(s->vea, h * w, byte);
C_MAKE(s->vec, h * w, char);
/* Prepare the window access arrays */
for (y = 0; y < h; y++)
{
s->a[y] = s->va + w * y;
s->c[y] = s->vc + w * y;
s->ta[y] = s->vta + w * y;
s->tc[y] = s->vtc + w * y;
s->ea[y] = s->vea + w * y;
s->ec[y] = s->vec + w * y;
}
/* Success */
return (0);
}
/*
* Copy a "term_win" from another
*/
static errr term_win_copy(term_win *s, term_win *f, int w, int h)
{
int x, y;
/* Copy contents */
for (y = 0; y < h; y++)
{
byte *f_aa = f->a[y];
char *f_cc = f->c[y];
byte *s_aa = s->a[y];
char *s_cc = s->c[y];
byte *f_taa = f->ta[y];
char *f_tcc = f->tc[y];
byte *s_taa = s->ta[y];
char *s_tcc = s->tc[y];
byte *f_eaa = f->ea[y];
char *f_ecc = f->ec[y];
byte *s_eaa = s->ea[y];
char *s_ecc = s->ec[y];
for (x = 0; x < w; x++)
{
*s_aa++ = *f_aa++;
*s_cc++ = *f_cc++;
*s_taa++ = *f_taa++;
*s_tcc++ = *f_tcc++;
*s_eaa++ = *f_eaa++;
*s_ecc++ = *f_ecc++;
}
}
/* Copy cursor */
s->cx = f->cx;
s->cy = f->cy;
s->cu = f->cu;
s->cv = f->cv;
/* Success */
return (0);
}
/*** External hooks ***/
/*
* Execute the "Term->user_hook" hook, if available (see above).
*/
errr Term_user(int n)
{
/* Verify the hook */
if (!Term->user_hook) return ( -1);
/* Call the hook */
return ((*Term->user_hook)(n));
}
/*
* Execute the "Term->xtra_hook" hook, if available (see above).
* And *hacky* get a return code
*/
long Term_xtra_long;
char scansubdir_dir[1024];
int scansubdir_max = 0;
cptr scansubdir_result[255];
errr Term_xtra(int n, int v)
{
/* Verify the hook */
if (!Term->xtra_hook) return ( -1);
/* Call the hook */
return ((*Term->xtra_hook)(n, v));
}
/*** Fake hooks ***/
/*
* Hack -- fake hook for "Term_curs()" (see above)
*/
static errr Term_curs_hack(int x, int y)
{
/* Compiler silliness */
if (x || y) return ( -2);
/* Oops */
return ( -1);
}
/*
* Hack -- fake hook for "Term_wipe()" (see above)
*/
static errr Term_wipe_hack(int x, int y, int n)
{
/* Compiler silliness */
if (x || y || n) return ( -2);
/* Oops */
return ( -1);
}
/*
* Hack -- fake hook for "Term_text()" (see above)
*/
static errr Term_text_hack(int x, int y, int n, byte a, const char *cp)
{
/* Compiler silliness */
if (x || y || n || a || cp) return ( -2);
/* Oops */
return ( -1);
}
/*
* Hack -- fake hook for "Term_pict()" (see above)
*/
static errr Term_pict_hack(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp, const byte *eap, const char *ecp)
{
/* Compiler silliness */
if (x || y || n || ap || cp || tap || tcp || eap || ecp) return ( -2);
/* Oops */
return ( -1);
}
/*** Efficient routines ***/
/*
* Mentally draw an attr/char at a given location
*
* Assumes given location and values are valid.
*/
void Term_queue_char(int x, int y, byte a, char c, byte ta, char tc, byte ea, char ec)
{
term_win *scrn = Term->scr;
byte *scr_aa = &scrn->a[y][x];
char *scr_cc = &scrn->c[y][x];
byte *scr_taa = &scrn->ta[y][x];
char *scr_tcc = &scrn->tc[y][x];
byte *scr_eaa = &scrn->ea[y][x];
char *scr_ecc = &scrn->ec[y][x];
/* Hack -- Ignore non-changes */
if ((*scr_aa == a) && (*scr_cc == c) &&
(*scr_taa == ta) && (*scr_tcc == tc) &&
(*scr_eaa == ea) && (*scr_ecc == ec)) return;
/* Save the "literal" information */
*scr_aa = a;
*scr_cc = c;
*scr_taa = ta;
*scr_tcc = tc;
*scr_eaa = ea;
*scr_ecc = ec;
/* Check for new min/max row info */
if (y < Term->y1) Term->y1 = y;
if (y > Term->y2) Term->y2 = y;
/* Check for new min/max col info for this row */
if (x < Term->x1[y]) Term->x1[y] = x;
if (x > Term->x2[y]) Term->x2[y] = x;
}
/*
* Mentally draw a string of attr/chars at a given location
*
* Assumes given location and values are valid.
*
* This function is designed to be fast, with no consistancy checking.
* It is used to update the map in the game.
*/
void Term_queue_line(int x, int y, int n, byte *a, char *c, byte *ta, char *tc, byte *ea, char *ec)
{
term_win *scrn = Term->scr;
int x1 = -1;
int x2 = -1;
byte *scr_aa = &scrn->a[y][x];
char *scr_cc = &scrn->c[y][x];
byte *scr_taa = &scrn->ta[y][x];
char *scr_tcc = &scrn->tc[y][x];
byte *scr_eaa = &scrn->ea[y][x];
char *scr_ecc = &scrn->ec[y][x];
while (n--)
{
/* Hack -- Ignore non-changes */
if ((*scr_aa == *a) && (*scr_cc == *c) &&
(*scr_taa == *ta) && (*scr_tcc == *tc) &&
(*scr_eaa == *ea) && (*scr_ecc == *ec))
{
x++;
a++;
c++;
ta++;
tc++;
ea++;
ec++;
scr_aa++;
scr_cc++;
scr_taa++;
scr_tcc++;
scr_eaa++;
scr_ecc++;
continue;
}
/* Save the "literal" information */
*scr_taa++ = *ta++;
*scr_tcc++ = *tc++;
/* Save the "literal" information */
*scr_eaa++ = *ea++;
*scr_ecc++ = *ec++;
/* Save the "literal" information */
*scr_aa++ = *a++;
*scr_cc++ = *c++;
/* Track minimum changed column */
if (x1 < 0) x1 = x;
/* Track maximum changed column */
x2 = x;
x++;
}
/* Expand the "change area" as needed */
if (x1 >= 0)
{
/* Check for new min/max row info */
if (y < Term->y1) Term->y1 = y;
if (y > Term->y2) Term->y2 = y;
/* Check for new min/max col info in this row */
if (x1 < Term->x1[y]) Term->x1[y] = x1;
if (x2 > Term->x2[y]) Term->x2[y] = x2;
}
}
/*
* Mentally draw some attr/chars at a given location
*
* Assumes that (x,y) is a valid location, that the first "n" characters
* of the string "s" are all valid (non-zero), and that (x+n-1,y) is also
* a valid location, so the first "n" characters of "s" can all be added
* starting at (x,y) without causing any illegal operations.
*/
void Term_queue_chars(int x, int y, int n, byte a, cptr s)
{
int x1 = -1, x2 = -1;
byte *scr_aa = Term->scr->a[y];
char *scr_cc = Term->scr->c[y];
byte *scr_taa = Term->scr->ta[y];
char *scr_tcc = Term->scr->tc[y];
byte *scr_eaa = Term->scr->ea[y];
char *scr_ecc = Term->scr->ec[y];
/* Queue the attr/chars */
for ( ; n; x++, s++, n--)
{
int oa = scr_aa[x];
int oc = scr_cc[x];
int ota = scr_taa[x];
int otc = scr_tcc[x];
int oea = scr_eaa[x];
int oec = scr_ecc[x];
/* Hack -- Ignore non-changes */
if ((oa == a) && (oc == *s) &&
(ota == 0) && (otc == 0) &&
(oea == 0) && (oec == 0)) continue;
/* Save the "literal" information */
scr_aa[x] = a;
scr_cc[x] = *s;
scr_taa[x] = 0;
scr_tcc[x] = 0;
scr_taa[x] = 0;
scr_tcc[x] = 0;
/* Note the "range" of window updates */
if (x1 < 0) x1 = x;
x2 = x;
}
/* Expand the "change area" as needed */
if (x1 >= 0)
{
/* Check for new min/max row info */
if (y < Term->y1) Term->y1 = y;
if (y > Term->y2) Term->y2 = y;
/* Check for new min/max col info in this row */
if (x1 < Term->x1[y]) Term->x1[y] = x1;
if (x2 > Term->x2[y]) Term->x2[y] = x2;
}
}
/*** Refresh routines ***/
/*
* Flush a row of the current window (see "Term_fresh")
*
* Display text using "Term_pict()"
*/
static void Term_fresh_row_pict(int y, int x1, int x2)
{
int x;
byte *old_aa = Term->old->a[y];
char *old_cc = Term->old->c[y];
byte *scr_aa = Term->scr->a[y];
char *scr_cc = Term->scr->c[y];
byte *old_taa = Term->old->ta[y];
char *old_tcc = Term->old->tc[y];
byte *scr_taa = Term->scr->ta[y];
char *scr_tcc = Term->scr->tc[y];
byte ota;
char otc;
byte nta;
char ntc;
byte *old_eaa = Term->old->ea[y];
char *old_ecc = Term->old->ec[y];
byte *scr_eaa = Term->scr->ea[y];
char *scr_ecc = Term->scr->ec[y];
byte oea;
char oec;
byte nea;
char nec;
/* Pending length */
int fn = 0;
/* Pending start */
int fx = 0;
byte oa;
char oc;
byte na;
char nc;
/* Scan "modified" columns */
for (x = x1; x <= x2; x++)
{
/* See what is currently here */
oa = old_aa[x];
oc = old_cc[x];
/* See what is desired there */
na = scr_aa[x];
nc = scr_cc[x];
ota = old_taa[x];
otc = old_tcc[x];
nta = scr_taa[x];
ntc = scr_tcc[x];
oea = old_eaa[x];
oec = old_ecc[x];
nea = scr_eaa[x];
nec = scr_ecc[x];
/* Handle unchanged grids */
if ((na == oa) && (nc == oc) &&
(nta == ota) && (ntc == otc) &&
(nea == oea) && (nec == oec))
{
/* Flush */
if (fn)
{
/* Draw pending attr/char pairs */
(void)((*Term->pict_hook)(fx, y, fn,
&scr_aa[fx], &scr_cc[fx],
&scr_taa[fx], &scr_tcc[fx],
&scr_eaa[fx], &scr_ecc[fx]));
/* Forget */
fn = 0;
}
/* Skip */
continue;
}
/* Save new contents */
old_aa[x] = na;
old_cc[x] = nc;
old_taa[x] = nta;
old_tcc[x] = ntc;
old_eaa[x] = nea;
old_ecc[x] = nec;
/* Restart and Advance */
if (fn++ == 0) fx = x;
}
/* Flush */
if (fn)
{
/* Draw pending attr/char pairs */
(void)((*Term->pict_hook)(fx, y, fn,
&scr_aa[fx], &scr_cc[fx],
&scr_taa[fx], &scr_tcc[fx],
&scr_eaa[fx], &scr_ecc[fx]));
}
}
/*
* Flush a row of the current window (see "Term_fresh")
*
* Display text using "Term_text()" and "Term_wipe()",
* but use "Term_pict()" for high-bit attr/char pairs
*/
static void Term_fresh_row_both(int y, int x1, int x2)
{
int x;
byte *old_aa = Term->old->a[y];
char *old_cc = Term->old->c[y];
byte *scr_aa = Term->scr->a[y];
char *scr_cc = Term->scr->c[y];
byte *old_taa = Term->old->ta[y];
char *old_tcc = Term->old->tc[y];
byte *scr_taa = Term->scr->ta[y];
char *scr_tcc = Term->scr->tc[y];
byte ota;
char otc;
byte nta;
char ntc;
byte *old_eaa = Term->old->ea[y];
char *old_ecc = Term->old->ec[y];
byte *scr_eaa = Term->scr->ea[y];
char *scr_ecc = Term->scr->ec[y];
byte oea;
char oec;
byte nea;
char nec;
/* The "always_text" flag */
int always_text = Term->always_text;
/* Pending length */
int fn = 0;
/* Pending start */
int fx = 0;
/* Pending attr */
byte fa = Term->attr_blank;
byte oa;
char oc;
byte na;
char nc;
/* Scan "modified" columns */
for (x = x1; x <= x2; x++)
{
/* See what is currently here */
oa = old_aa[x];
oc = old_cc[x];
/* See what is desired there */
na = scr_aa[x];
nc = scr_cc[x];
ota = old_taa[x];
otc = old_tcc[x];
nta = scr_taa[x];
ntc = scr_tcc[x];
oea = old_eaa[x];
oec = old_ecc[x];
nea = scr_eaa[x];
nec = scr_ecc[x];
/* Handle unchanged grids */
if ((na == oa) && (nc == oc) &&
(nta == ota) && (ntc == otc) &&
(nea == oea) && (nec == oec))
{
/* Flush */
if (fn)
{
/* Draw pending chars (normal) */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Draw pending chars (black) */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
/* Forget */
fn = 0;
}
/* Skip */
continue;
}
/* Save new contents */
old_aa[x] = na;
old_cc[x] = nc;
old_taa[x] = nta;
old_tcc[x] = ntc;
old_eaa[x] = nea;
old_ecc[x] = nec;
/* 2nd byte of bigtile */
if (na == 255) continue;
/* Handle high-bit attr/chars */
if (na & 0x80)
{
/* Flush */
if (fn)
{
/* Draw pending chars (normal) */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Draw pending chars (black) */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
/* Forget */
fn = 0;
}
/* Hack -- Draw the special attr/char pair */
(void)((*Term->pict_hook)(x, y, 1, &na, &nc, &nta, &ntc, &nea, &nec));
/* Skip */
continue;
}
/* Notice new color */
if (fa != na)
{
/* Flush */
if (fn)
{
/* Draw the pending chars */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Hack -- Erase "leading" spaces */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
/* Forget */
fn = 0;
}
/* Save the new color */
fa = na;
}
/* Restart and Advance */
if (fn++ == 0) fx = x;
}
/* Flush */
if (fn)
{
/* Draw pending chars (normal) */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Draw pending chars (black) */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
}
}
/*
* Flush a row of the current window (see "Term_fresh")
*
* Display text using "Term_text()" and "Term_wipe()"
*/
static void Term_fresh_row_text(int y, int x1, int x2)
{
int x;
byte *old_aa = Term->old->a[y];
char *old_cc = Term->old->c[y];
byte *scr_aa = Term->scr->a[y];
char *scr_cc = Term->scr->c[y];
/* The "always_text" flag */
int always_text = Term->always_text;
/* Pending length */
int fn = 0;
/* Pending start */
int fx = 0;
/* Pending attr */
byte fa = Term->attr_blank;
byte oa;
char oc;
byte na;
char nc;
/* Scan "modified" columns */
for (x = x1; x <= x2; x++)
{
/* See what is currently here */
oa = old_aa[x];
oc = old_cc[x];
/* See what is desired there */
na = scr_aa[x];
nc = scr_cc[x];
/* Handle unchanged grids */
if ((na == oa) && (nc == oc))
{
/* Flush */
if (fn)
{
/* Draw pending chars (normal) */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Draw pending chars (black) */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
/* Forget */
fn = 0;
}
/* Skip */
continue;
}
/* Save new contents */
old_aa[x] = na;
old_cc[x] = nc;
/* Notice new color */
if (fa != na)
{
/* Flush */
if (fn)
{
/* Draw the pending chars */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Hack -- Erase "leading" spaces */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
/* Forget */
fn = 0;
}
/* Save the new color */
fa = na;
}
/* Restart and Advance */
if (fn++ == 0) fx = x;
}
/* Flush */
if (fn)
{
/* Draw pending chars (normal) */
if (fa || always_text)
{
(void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
}
/* Draw pending chars (black) */
else
{
(void)((*Term->wipe_hook)(fx, y, fn));
}
}
}
/*
* Actually perform all requested changes to the window
*
* If absolutely nothing has changed, not even temporarily, or if the
* current "Term" is not mapped, then this function will return 1 and
* do absolutely nothing.
*
* Note that when "soft_cursor" is true, we erase the cursor (if needed)
* whenever anything has changed, and redraw it (if needed) after all of
* the screen updates are complete. This will induce a small amount of
* "cursor flicker" but only when the screen has been updated. If the
* screen is updated and then restored, you may still get this flicker.
*
* When "soft_cursor" is not true, we make the cursor invisible before
* doing anything else if it is supposed to be invisible by the time we
* are done, and we make it visible after moving it to its final location
* after all of the screen updates are complete.
*
* Note that "Term_xtra(TERM_XTRA_CLEAR,0)" must erase the entire screen,
* including the cursor, if needed, and may place the cursor anywhere.
*
* Note that "Term_xtra(TERM_XTRA_FROSH,y)" will be always be called
* after any row "y" has been "flushed", unless the "Term->never_frosh"
* flag is set, and "Term_xtra(TERM_XTRA_FRESH,0)" will be called after
* all of the rows have been "flushed".
*
* Note the use of three different functions to handle the actual flush,
* based on the settings of the "Term->always_pict" and "Term->higher_pict"
* flags (see below).
*
* The three helper functions (above) work by collecting similar adjacent
* grids into stripes, and then sending each stripe to "Term->pict_hook",
* "Term->text_hook", or "Term->wipe_hook", based on the settings of the
* "Term->always_pict" and "Term->higher_pict" flags, which select which
* of the helper functions to call to flush each row.
*
* The helper functions currently "skip" any grids which already contain
* the desired contents. This may or may not be the best method, especially
* when the desired content fits nicely into the current stripe. For example,
* it might be better to go ahead and queue them while allowed, but keep a
* count of the "trailing skipables", then, when time to flush, or when a
* "non skippable" is found, force a flush if there are too many skippables.
*
* Perhaps an "initialization" stage, where the "text" (and "attr")
* buffers are "filled" with information, converting "blanks" into
* a convenient representation, and marking "skips" with "zero chars",
* and then some "processing" is done to determine which chars to skip.
*
* Currently, the helper functions are optimal for systems which prefer
* to "print a char + move a char + print a char" to "print three chars",
* and for applications that do a lot of "detailed" color printing.
*
* In the two "queue" functions, total "non-changes" are "pre-skipped".
* The helper functions must also handle situations in which the contents
* of a grid are changed, but then changed back to the original value,
* and situations in which two grids in the same row are changed, but
* the grids between them are unchanged.
*
* If the "Term->always_pict" flag is set, then "Term_fresh_row_pict()"
* will be used instead of "Term_fresh_row_text()". This allows all the
* modified grids to be collected into stripes of attr/char pairs, which
* are then sent to the "Term->pict_hook" hook, which can draw these pairs
* in whatever way it would like.
*
* If the "Term->higher_pict" flag is set, then "Term_fresh_row_both()"
* will be used instead of "Term_fresh_row_text()". This allows all the
* "special" attr/char pairs (in which both the attr and char have the
* high-bit set) to be sent (one pair at a time) to the "Term->pict_hook"
* hook, which can draw these pairs in whatever way it would like.
*
* Normally, the "Term_wipe()" function is used only to display "blanks"
* that were induced by "Term_clear()" or "Term_erase()", and then only
* if the "attr_blank" and "char_blank" fields have not been redefined
* to use "white space" instead of the default "black space". Actually,
* the "Term_wipe()" function is used to display all "black" text, such
* as the default "spaces" created by "Term_clear()" and "Term_erase()".
*
* Note that the "Term->always_text" flag will disable the use of the
* "Term_wipe()" function hook entirely, and force all text, even text
* drawn in the color "black", to be explicitly drawn. This is useful
* for machines which implement "Term_wipe()" by just drawing spaces.
*
* Note that the "Term->always_pict" flag will disable the use of the
* "Term_wipe()" function entirely, and force everything, even text
* drawn in the attr "black", to be explicitly drawn.
*
* Note that if no "black" text is ever drawn, and if "attr_blank" is
* not "zero", then the "Term_wipe" hook will never be used, even if
* the "Term->always_text" flag is not set.
*
* This function does nothing unless the "Term" is "mapped", which allows
* certain systems to optimize the handling of "closed" windows.
*
* On systems with a "soft" cursor, we must explicitly erase the cursor
* before flushing the output, if needed, to prevent a "jumpy" refresh.
* The actual method for this is horrible, but there is very little that
* we can do to simplify it efficiently. XXX XXX XXX
*
* On systems with a "hard" cursor, we will "hide" the cursor before
* flushing the output, if needed, to avoid a "flickery" refresh. It
* would be nice to *always* hide the cursor during the refresh, but
* this might be expensive (and/or ugly) on some machines.
*
* The "Term->icky_corner" flag is used to avoid calling "Term_wipe()"
* or "Term_pict()" or "Term_text()" on the bottom right corner of the
* window, which might induce "scrolling" or other nasty stuff on old
* dumb terminals. This flag is handled very efficiently. We assume
* that the "Term_curs()" call will prevent placing the cursor in the
* corner, if needed, though I doubt such placement is ever a problem.
* Currently, the use of "Term->icky_corner" and "Term->soft_cursor"
* together may result in undefined behavior.
*/
errr Term_fresh(void)
{
int x, y;
int w = Term->wid;
int h = Term->hgt;
int y1 = Term->y1;
int y2 = Term->y2;
term_win *old = Term->old;
term_win *scr = Term->scr;
/* Do nothing unless "mapped" */
if (!Term->mapped_flag) return (1);
/* Trivial Refresh */
if ((y1 > y2) &&
(scr->cu == old->cu) &&
(scr->cv == old->cv) &&
(scr->cx == old->cx) &&
(scr->cy == old->cy) &&
!(Term->total_erase))
{
/* Nothing */
return (1);
}
/* Paranoia -- use "fake" hooks to prevent core dumps */
if (!Term->curs_hook) Term->curs_hook = Term_curs_hack;
if (!Term->wipe_hook) Term->wipe_hook = Term_wipe_hack;
if (!Term->text_hook) Term->text_hook = Term_text_hack;
if (!Term->pict_hook) Term->pict_hook = Term_pict_hack;
/* Handle "total erase" */
if (Term->total_erase)
{
byte na = Term->attr_blank;
char nc = Term->char_blank;
if ((do_movies == 1) && IN_MAINWINDOW)
{
if (!cmovie_get_msecond())
{
fprintf(movfile, "S:%ld:\n", cmov_delta_time_msec);
}
fprintf(movfile, "C:\n");
last_paused = 0;
}
/* Physically erase the entire window */
Term_xtra(TERM_XTRA_CLEAR, 0);
/* Hack -- clear all "cursor" data */
old->cv = old->cu = old->cx = old->cy = 0;
/* Wipe each row */
for (y = 0; y < h; y++)
{
byte *aa = old->a[y];
char *cc = old->c[y];
byte *taa = old->ta[y];
char *tcc = old->tc[y];
byte *eaa = old->ea[y];
char *ecc = old->ec[y];
/* Wipe each column */
for (x = 0; x < w; x++)
{
/* Wipe each grid */
*aa++ = na;
*cc++ = nc;
*taa++ = na;
*tcc++ = nc;
*eaa++ = na;
*ecc++ = nc;
}
}
/* Redraw every row */
Term->y1 = y1 = 0;
Term->y2 = y2 = h - 1;
/* Redraw every column */
for (y = 0; y < h; y++)
{
Term->x1[y] = 0;
Term->x2[y] = w - 1;
}
/* Forget "total erase" */
Term->total_erase = FALSE;
}
/* Cursor update -- Erase old Cursor */
if (Term->soft_cursor)
{
/* Cursor was visible */
if (!old->cu && old->cv)
{
int tx = old->cx;
int ty = old->cy;
byte *old_aa = old->a[ty];
char *old_cc = old->c[ty];
byte oa = old_aa[tx];
char oc = old_cc[tx];
byte *old_taa = old->ta[ty];
char *old_tcc = old->tc[ty];
byte ota = old_taa[tx];
char otc = old_tcc[tx];
byte *old_eaa = old->ea[ty];
char *old_ecc = old->ec[ty];
byte oea = old_eaa[tx];
char oec = old_ecc[tx];
/* Hack -- use "Term_pict()" always */
if (Term->always_pict)
{
(void)((*Term->pict_hook)(tx, ty, 1, &oa, &oc, &ota, &otc, &oea, &oec));
}
/* Hack -- use "Term_pict()" sometimes */
else if (Term->higher_pict && (oa & 0x80))
{
(void)((*Term->pict_hook)(tx, ty, 1, &oa, &oc, &ota, &otc, &oea, &oec));
}
/* Hack -- restore the actual character */
else if (oa || Term->always_text)
{
(void)((*Term->text_hook)(tx, ty, 1, oa, &oc));
}
/* Hack -- erase the grid */
else
{
(void)((*Term->wipe_hook)(tx, ty, 1));
}
}
}
/* Cursor Update -- Erase old Cursor */
else
{
/* Cursor will be invisible */
if (scr->cu || !scr->cv)
{
/* Make the cursor invisible */
Term_xtra(TERM_XTRA_SHAPE, 0);
}
}
/* Something to update */
if (y1 <= y2)
{
/* Handle "icky corner" */
if (Term->icky_corner)
{
/* Avoid the corner */
if (y2 >= h - 1)
{
/* Avoid the corner */
if (Term->x2[h - 1] > w - 2)
{
/* Avoid the corner */
Term->x2[h - 1] = w - 2;
}
}
}
/* Scan the "modified" rows */
for (y = y1; y <= y2; ++y)
{
int x1 = Term->x1[y];
int x2 = Term->x2[y];
/* Flush each "modified" row */
if (x1 <= x2)
{
if ((do_movies == 1) && IN_MAINWINDOW)
{
/* Most magic happens here */
cmovie_record_line(y);
last_paused = 0;
}
/* Always use "Term_pict()" */
if (Term->always_pict)
{
/* Flush the row */
Term_fresh_row_pict(y, x1, x2);
}
/* Sometimes use "Term_pict()" */
else if (Term->higher_pict)
{
/* Flush the row */
Term_fresh_row_both(y, x1, x2);
}
/* Never use "Term_pict()" */
else
{
/* Flush the row */
Term_fresh_row_text(y, x1, x2);
}
/* This row is all done */
Term->x1[y] = w;
Term->x2[y] = 0;
/* Hack -- Flush that row (if allowed) */
if (!Term->never_frosh) Term_xtra(TERM_XTRA_FROSH, y);
}
}
/* No rows are invalid */
Term->y1 = h;
Term->y2 = 0;
}
/* Cursor update -- Show new Cursor */
if (Term->soft_cursor)
{
/* Draw the cursor */
if (!scr->cu && scr->cv)
{
/* Call the cursor display routine */
(void)((*Term->curs_hook)(scr->cx, scr->cy));
}
}
/* Cursor Update -- Show new Cursor */
else
{
/* The cursor is useless, hide it */
if (scr->cu)
{
/* Paranoia -- Put the cursor NEAR where it belongs */
(void)((*Term->curs_hook)(w - 1, scr->cy));
/* Make the cursor invisible */
/* Term_xtra(TERM_XTRA_SHAPE, 0); */
}
/* The cursor is invisible, hide it */
else if (!scr->cv)
{
/* Paranoia -- Put the cursor where it belongs */
(void)((*Term->curs_hook)(scr->cx, scr->cy));
/* Make the cursor invisible */
/* Term_xtra(TERM_XTRA_SHAPE, 0); */
}
/* The cursor is visible, display it correctly */
else
{
/* Put the cursor where it belongs */
(void)((*Term->curs_hook)(scr->cx, scr->cy));
/* Make the cursor visible */
Term_xtra(TERM_XTRA_SHAPE, 1);
}
}
/* Save the "cursor state" */
old->cu = scr->cu;
old->cv = scr->cv;
old->cx = scr->cx;
old->cy = scr->cy;
/* Actually flush the output */
Term_xtra(TERM_XTRA_FRESH, 0);
/* Success */
return (0);
}
/*** Output routines ***/
/*
* Set the cursor visibility
*/
errr Term_set_cursor(int v)
{
/* Already done */
if (Term->scr->cv == v) return (1);
/* Change */
Term->scr->cv = v;
/* Success */
return (0);
}
/*
* Place the cursor at a given location
*
* Note -- "illegal" requests do not move the cursor.
*/
errr Term_gotoxy(int x, int y)
{
int w = Term->wid;
int h = Term->hgt;
/* Verify */
if ((x < 0) || (x >= w)) return ( -1);
if ((y < 0) || (y >= h)) return ( -1);
/* Remember the cursor */
Term->scr->cx = x;
Term->scr->cy = y;
/* The cursor is not useless */
Term->scr->cu = 0;
/* Success */
return (0);
}
/*
* At a given location, place an attr/char
* Do not change the cursor position
* No visual changes until "Term_fresh()".
*/
errr Term_draw(int x, int y, byte a, char c)
{
int w = Term->wid;
int h = Term->hgt;
/* Verify location */
if ((x < 0) || (x >= w)) return ( -1);
if ((y < 0) || (y >= h)) return ( -1);
/* Paranoia -- illegal char */
if (!c) return ( -2);
/* Queue it for later */
Term_queue_char(x, y, a, c, 0, 0, 0, 0);
/* Success */
return (0);
}
/*
* Using the given attr, add the given char at the cursor.
*
* We return "-2" if the character is "illegal". XXX XXX
*
* We return "-1" if the cursor is currently unusable.
*
* We queue the given attr/char for display at the current
* cursor location, and advance the cursor to the right,
* marking it as unuable and returning "1" if it leaves
* the screen, and otherwise returning "0".
*
* So when this function, or the following one, return a
* positive value, future calls to either function will
* return negative ones.
*/
errr Term_addch(byte a, char c)
{
int w = Term->wid;
/* Handle "unusable" cursor */
if (Term->scr->cu) return ( -1);
/* Paranoia -- no illegal chars */
if (!c) return ( -2);
/* Queue the given character for display */
Term_queue_char(Term->scr->cx, Term->scr->cy, a, c, 0, 0, 0, 0);
/* Advance the cursor */
Term->scr->cx++;
/* Success */
if (Term->scr->cx < w) return (0);
/* Note "Useless" cursor */
Term->scr->cu = 1;
/* Note "Useless" cursor */
return (1);
}
/*
* At the current location, using an attr, add a string
*
* We also take a length "n", using negative values to imply
* the largest possible value, and then we use the minimum of
* this length and the "actual" length of the string as the
* actual number of characters to attempt to display, never
* displaying more characters than will actually fit, since
* we do NOT attempt to "wrap" the cursor at the screen edge.
*
* We return "-1" if the cursor is currently unusable.
* We return "N" if we were "only" able to write "N" chars,
* even if all of the given characters fit on the screen,
* and mark the cursor as unusable for future attempts.
*
* So when this function, or the preceding one, return a
* positive value, future calls to either function will
* return negative ones.
*/
errr Term_addstr(int n, byte a, cptr s)
{
int k;
int w = Term->wid;
errr res = 0;
/* Handle "unusable" cursor */
if (Term->scr->cu) return ( -1);
/* Obtain maximal length */
k = (n < 0) ? (w + 1) : n;
/* Obtain the usable string length */
for (n = 0; (n < k) && s[n]; n++) /* loop */;
/* React to reaching the edge of the screen */
if (Term->scr->cx + n >= w) res = n = w - Term->scr->cx;
/* Queue the first "n" characters for display */
Term_queue_chars(Term->scr->cx, Term->scr->cy, n, a, s);
/* Advance the cursor */
Term->scr->cx += n;
/* Hack -- Notice "Useless" cursor */
if (res) Term->scr->cu = 1;
/* Success (usually) */
return (res);
}
/*
* Move to a location and, using an attr, add a char
*/
errr Term_putch(int x, int y, byte a, char c)
{
errr res;
/* Move first */
if ((res = Term_gotoxy(x, y)) != 0) return (res);
/* Then add the char */
if ((res = Term_addch(a, c)) != 0) return (res);
/* Success */
return (0);
}
/*
* Move to a location and, using an attr, add a string
*/
errr Term_putstr(int x, int y, int n, byte a, cptr s)
{
errr res;
/* Move first */
if ((res = Term_gotoxy(x, y)) != 0) return (res);
/* Then add the string */
if ((res = Term_addstr(n, a, s)) != 0) return (res);
/* Success */
return (0);
}
/*
* Place cursor at (x,y), and clear the next "n" chars
*/
errr Term_erase(int x, int y, int n)
{
int i;
int w = Term->wid;
/* int h = Term->hgt; */
int x1 = -1;
int x2 = -1;
int na = Term->attr_blank;
int nc = Term->char_blank;
byte *scr_aa;
char *scr_cc;
byte *scr_taa;
char *scr_tcc;
byte *scr_eaa;
char *scr_ecc;
/* Place cursor */
if (Term_gotoxy(x, y)) return ( -1);
/* Force legal size */
if (x + n > w) n = w - x;
/* Fast access */
scr_aa = Term->scr->a[y];
scr_cc = Term->scr->c[y];
scr_taa = Term->scr->ta[y];
scr_tcc = Term->scr->tc[y];
scr_eaa = Term->scr->ea[y];
scr_ecc = Term->scr->ec[y];
if (n > 0 && (byte)scr_cc[x] == 255 && scr_aa[x] == 255)
{
x--;
n++;
}
/* Scan every column */
for (i = 0; i < n; i++, x++)
{
int oa = scr_aa[x];
int oc = scr_cc[x];
/* Hack -- Ignore "non-changes" */
if ((oa == na) && (oc == nc)) continue;
/* Save the "literal" information */
scr_aa[x] = na;
scr_cc[x] = nc;
scr_taa[x] = 0;
scr_tcc[x] = 0;
scr_eaa[x] = 0;
scr_ecc[x] = 0;
/* Track minimum changed column */
if (x1 < 0) x1 = x;
/* Track maximum changed column */
x2 = x;
}
/* Expand the "change area" as needed */
if (x1 >= 0)
{
/* Check for new min/max row info */
if (y < Term->y1) Term->y1 = y;
if (y > Term->y2) Term->y2 = y;
/* Check for new min/max col info in this row */
if (x1 < Term->x1[y]) Term->x1[y] = x1;
if (x2 > Term->x2[y]) Term->x2[y] = x2;
}
/* Success */
return (0);
}
/*
* Clear the entire window, and move to the top left corner
*
* Note the use of the special "total_erase" code
*/
errr Term_clear(void)
{
int x, y;
int w = Term->wid;
int h = Term->hgt;
byte na = Term->attr_blank;
char nc = Term->char_blank;
/* Cursor usable */
Term->scr->cu = 0;
/* Cursor to the top left */
Term->scr->cx = Term->scr->cy = 0;
/* Wipe each row */
for (y = 0; y < h; y++)
{
byte *scr_aa = Term->scr->a[y];
char *scr_cc = Term->scr->c[y];
byte *scr_taa = Term->scr->ta[y];
char *scr_tcc = Term->scr->tc[y];
byte *scr_eaa = Term->scr->ea[y];
char *scr_ecc = Term->scr->ec[y];
/* Wipe each column */
for (x = 0; x < w; x++)
{
scr_aa[x] = na;
scr_cc[x] = nc;
scr_taa[x] = 0;
scr_tcc[x] = 0;
scr_eaa[x] = 0;
scr_ecc[x] = 0;
}
/* This row has changed */
Term->x1[y] = 0;
Term->x2[y] = w - 1;
}
/* Every row has changed */
Term->y1 = 0;
Term->y2 = h - 1;
/* Force "total erase" */
Term->total_erase = TRUE;
/* Success */
return (0);
}
/*
* Redraw (and refresh) the whole window.
*/
errr Term_redraw(void)
{
/* Pat */
if ((do_movies == 1) && IN_MAINWINDOW)
{
if (!cmovie_get_msecond())
{
fprintf(movfile, "S:%ld:\n", cmov_delta_time_msec);
}
last_paused = 1;
}
/* Endpat */
/* Force "total erase" */
Term->total_erase = TRUE;
/* Hack -- Refresh */
Term_fresh();
/* Success */
return (0);
}
/*
* Redraw part of a window.
*/
errr Term_redraw_section(int x1, int y1, int x2, int y2)
{
int i, j;
char *c_ptr;
/* Bounds checking */
if (y2 >= Term->hgt) y2 = Term->hgt - 1;
if (x2 >= Term->wid) x2 = Term->wid - 1;
if (y1 < 0) y1 = 0;
if (x1 < 0) x1 = 0;
/* Set y limits */
Term->y1 = y1;
Term->y2 = y2;
/* Set the x limits */
for (i = Term->y1; i <= Term->y2; i++)
{
Term->x1[i] = x1;
Term->x2[i] = x2;
c_ptr = Term->old->c[i];
/* Clear the section so it is redrawn */
for (j = x1; j <= x2; j++)
{
/* Hack - set the old character to "none" */
c_ptr[j] = 0;
}
}
/* Hack -- Refresh */
Term_fresh();
/* Success */
return (0);
}
/*** Access routines ***/
/*
* Extract the cursor visibility
*/
errr Term_get_cursor(int *v)
{
/* Extract visibility */
(*v) = Term->scr->cv;
/* Success */
return (0);
}
/*
* Extract the current window size
*/
errr Term_get_size(int *w, int *h)
{
/* Access the cursor */
(*w) = Term->wid;
(*h) = Term->hgt;
/* Success */
return (0);
}
/*
* Extract the current cursor location
*/
errr Term_locate(int *x, int *y)
{
/* Access the cursor */
(*x) = Term->scr->cx;
(*y) = Term->scr->cy;
/* Warn about "useless" cursor */
if (Term->scr->cu) return (1);
/* Success */
return (0);
}
/*
* At a given location, determine the "current" attr and char
* Note that this refers to what will be on the window after the
* next call to "Term_fresh()". It may or may not already be there.
*/
errr Term_what(int x, int y, byte *a, char *c)
{
int w = Term->wid;
int h = Term->hgt;
/* Verify location */
if ((x < 0) || (x >= w)) return ( -1);
if ((y < 0) || (y >= h)) return ( -1);
/* Direct access */
(*a) = Term->scr->a[y][x];
(*c) = Term->scr->c[y][x];
/* Success */
return (0);
}
/*** Input routines ***/
/*
* Flush and forget the input
*/
errr Term_flush(void)
{
/* Hack -- Flush all events */
Term_xtra(TERM_XTRA_FLUSH, 0);
/* Forget all keypresses */
Term->key_head = Term->key_tail = 0;
/* Success */
return (0);
}
/*
* Add a keypress to the "queue"
*/
errr Term_keypress(int k)
{
/* Hack -- Refuse to enqueue non-keys */
if (!k) return ( -1);
/* Store the char, advance the queue */
Term->key_queue[Term->key_head++] = k;
/* Circular queue, handle wrap */
if (Term->key_head == Term->key_size) Term->key_head = 0;
/* Success (unless overflow) */
if (Term->key_head != Term->key_tail) return (0);
/* Problem */
return (1);
}
/*
* Add a keypress to the FRONT of the "queue"
*/
errr Term_key_push(int k)
{
/* Hack -- Refuse to enqueue non-keys */
if (!k) return ( -1);
/* Hack -- Overflow may induce circular queue */
if (Term->key_tail == 0) Term->key_tail = Term->key_size;
/* Back up, Store the char */
Term->key_queue[--Term->key_tail] = k;
/* Success (unless overflow) */
if (Term->key_head != Term->key_tail) return (0);
/* Problem */
return (1);
}
/*
* Check for a pending keypress on the key queue.
*
* Store the keypress, if any, in "ch", and return "0".
* Otherwise store "zero" in "ch", and return "1".
*
* Wait for a keypress if "wait" is true.
*
* Remove the keypress if "take" is true.
*/
errr Term_inkey(char *ch, bool_ wait, bool_ take)
{
/* Assume no key */
(*ch) = '\0';
/* Hack -- get bored */
if (!Term->never_bored)
{
/* Process random events */
Term_xtra(TERM_XTRA_BORED, 0);
}
/* PatN */
if ((do_movies == 1) && (last_paused == 0) && (!cmovie_get_msecond()))
{
fprintf(movfile, "S:%ld:\n", cmov_delta_time_msec);
last_paused = 1;
}
/* PatNEnd */
/* Wait */
if (wait)
{
/* Process pending events while necessary */
while (Term->key_head == Term->key_tail)
{
/* Process events (wait for one) */
Term_xtra(TERM_XTRA_EVENT, TRUE);
}
}
/* Do not Wait */
else
{
/* Process pending events if necessary */
if (Term->key_head == Term->key_tail)
{
/* Process events (do not wait) */
Term_xtra(TERM_XTRA_EVENT, FALSE);
}
}
/* No keys are ready */
if (Term->key_head == Term->key_tail) return (1);
/* Extract the next keypress */
(*ch) = Term->key_queue[Term->key_tail];
/* If requested, advance the queue, wrap around if necessary */
if (take && (++Term->key_tail == Term->key_size)) Term->key_tail = 0;
/* Success */
return (0);
}
/*** Extra routines ***/
/*
* Save the "requested" screen into the "memorized" screen
*
* Every "Term_save()" should match exactly one "Term_load()"
*/
errr Term_save(void)
{
int w = Term->wid;
int h = Term->hgt;
/* Create */
if (!Term->mem)
{
/* Allocate window */
MAKE(Term->mem, term_win);
/* Initialize window */
term_win_init(Term->mem, w, h);
}
/* Grab */
term_win_copy(Term->mem, Term->scr, w, h);
/* Success */
return (0);
}
/*
* Same as before but can save more than once
*/
term_win* Term_save_to(void)
{
int w = Term->wid;
int h = Term->hgt;
term_win *save;
/* Allocate window */
MAKE(save, term_win);
/* Initialize window */
term_win_init(save, w, h);
/* Grab */
term_win_copy(save, Term->scr, w, h);
/* Success */
return (save);
}
/*
* Restore the "requested" contents (see above).
*
* Every "Term_save()" should match exactly one "Term_load()"
*/
errr Term_load(void)
{
int y;
int w = Term->wid;
int h = Term->hgt;
/* Create */
if (!Term->mem)
{
/* Allocate window */
MAKE(Term->mem, term_win);
/* Initialize window */
term_win_init(Term->mem, w, h);
}
/* Load */
term_win_copy(Term->scr, Term->mem, w, h);
/* Assume change */
for (y = 0; y < h; y++)
{
/* Assume change */
Term->x1[y] = 0;
Term->x2[y] = w - 1;
}
/* Assume change */
Term->y1 = 0;
Term->y2 = h - 1;
/* Success */
return (0);
}
/*
* Same as previous but allow to save more than one
*/
errr Term_load_from(term_win *save, bool_ final)
{
int y;
int w = Term->wid;
int h = Term->hgt;
/* Create */
if (!save)
{
return (1);
}
/* Load */
term_win_copy(Term->scr, save, w, h);
/* Assume change */
for (y = 0; y < h; y++)
{
/* Assume change */
Term->x1[y] = 0;
Term->x2[y] = w - 1;
}
/* Assume change */
Term->y1 = 0;
Term->y2 = h - 1;
/* Free is requested */
if (final)
FREE(save, term_win);
/* Success */
return (0);
}
/*
* Exchange the "requested" screen with the "tmp" screen
*/
errr Term_exchange(void)
{
int y;
int w = Term->wid;
int h = Term->hgt;
term_win *exchanger;
/* Create */
if (!Term->tmp)
{
/* Allocate window */
MAKE(Term->tmp, term_win);
/* Initialize window */
term_win_init(Term->tmp, w, h);
}
/* Swap */
exchanger = Term->scr;
Term->scr = Term->tmp;
Term->tmp = exchanger;
/* Assume change */
for (y = 0; y < h; y++)
{
/* Assume change */
Term->x1[y] = 0;
Term->x2[y] = w - 1;
}
/* Assume change */
Term->y1 = 0;
Term->y2 = h - 1;
/* Success */
return (0);
}
/*
* React to a new physical window size.
*/
errr Term_resize(int w, int h)
{
int i;
int wid, hgt;
byte *hold_x1;
byte *hold_x2;
term_win *hold_old;
term_win *hold_scr;
term_win *hold_mem;
term_win *hold_tmp;
/* Resizing is forbidden */
if (Term->fixed_shape) return ( -1);
/* Ignore illegal changes */
if ((w < 1) || (h < 1)) return ( -1);
/* Ignore non-changes */
if ((Term->wid == w) && (Term->hgt == h) && (arg_bigtile == use_bigtile)) return (1);
use_bigtile = arg_bigtile;
/* Minimum dimensions */
wid = MIN(Term->wid, w);
hgt = MIN(Term->hgt, h);
/* Save scanners */
hold_x1 = Term->x1;
hold_x2 = Term->x2;
/* Save old window */
hold_old = Term->old;
/* Save old window */
hold_scr = Term->scr;
/* Save old window */
hold_mem = Term->mem;
/* Save old window */
hold_tmp = Term->tmp;
/* Create new scanners */
C_MAKE(Term->x1, h, byte);
C_MAKE(Term->x2, h, byte);
/* Create new window */
MAKE(Term->old, term_win);
/* Initialize new window */
term_win_init(Term->old, w, h);
/* Save the contents */
term_win_copy(Term->old, hold_old, wid, hgt);
/* Create new window */
MAKE(Term->scr, term_win);
/* Initialize new window */
term_win_init(Term->scr, w, h);
/* Save the contents */
term_win_copy(Term->scr, hold_scr, wid, hgt);
/* If needed */
if (hold_mem)
{
/* Create new window */
MAKE(Term->mem, term_win);
/* Initialize new window */
term_win_init(Term->mem, w, h);
/* Save the contents */
term_win_copy(Term->mem, hold_mem, wid, hgt);
}
/* If needed */
if (hold_tmp)
{
/* Create new window */
MAKE(Term->tmp, term_win);
/* Initialize new window */
term_win_init(Term->tmp, w, h);
/* Save the contents */
term_win_copy(Term->tmp, hold_tmp, wid, hgt);
}
/* Free some arrays */
C_KILL(hold_x1, Term->hgt, byte);
C_KILL(hold_x2, Term->hgt, byte);
/* Nuke */
term_win_nuke(hold_old, Term->wid, Term->hgt);
/* Kill */
KILL(hold_old, term_win);
/* Illegal cursor */
if (Term->old->cx >= w) Term->old->cu = 1;
if (Term->old->cy >= h) Term->old->cu = 1;
/* Nuke */
term_win_nuke(hold_scr, Term->wid, Term->hgt);
/* Kill */
KILL(hold_scr, term_win);
/* Illegal cursor */
if (Term->scr->cx >= w) Term->scr->cu = 1;
if (Term->scr->cy >= h) Term->scr->cu = 1;
/* If needed */
if (hold_mem)
{
/* Nuke */
term_win_nuke(hold_mem, Term->wid, Term->hgt);
/* Kill */
KILL(hold_mem, term_win);
/* Illegal cursor */
if (Term->mem->cx >= w) Term->mem->cu = 1;
if (Term->mem->cy >= h) Term->mem->cu = 1;
}
/* If needed */
if (hold_tmp)
{
/* Nuke */
term_win_nuke(hold_tmp, Term->wid, Term->hgt);
/* Kill */
KILL(hold_tmp, term_win);
/* Illegal cursor */
if (Term->tmp->cx >= w) Term->tmp->cu = 1;
if (Term->tmp->cy >= h) Term->tmp->cu = 1;
}
/* Save new size */
Term->wid = w;
Term->hgt = h;
/* Force "total erase" */
Term->total_erase = TRUE;
/* Assume change */
for (i = 0; i < h; i++)
{
/* Assume change */
Term->x1[i] = 0;
Term->x2[i] = w - 1;
}
/* Assume change */
Term->y1 = 0;
Term->y2 = h - 1;
/* Execute the "resize_hook" hook, if available */
if (Term->resize_hook)
{
Term->resize_hook();
}
/* Success */
return (0);
}
/*
* Activate a new Term (and deactivate the current Term)
*
* This function is extremely important, and also somewhat bizarre.
* It is the only function that should "modify" the value of "Term".
*
* To "create" a valid "term", one should do "term_init(t)", then
* set the various flags and hooks, and then do "Term_activate(t)".
*/
errr Term_activate(term *t)
{
/* Hack -- already done */
if (Term == t) return (1);
/* Deactivate the old Term */
if (Term) Term_xtra(TERM_XTRA_LEVEL, 0);
/* Hack -- Call the special "init" hook */
if (t && !t->active_flag)
{
/* Call the "init" hook */
if (t->init_hook) (*t->init_hook)(t);
/* Remember */
t->active_flag = TRUE;
/* Assume mapped */
t->mapped_flag = TRUE;
}
/* Remember the Term */
Term = t;
/* Activate the new Term */
if (Term) Term_xtra(TERM_XTRA_LEVEL, 1);
/* Success */
return (0);
}
/*
* Nuke a term
*/
errr term_nuke(term *t)
{
int w = t->wid;
int h = t->hgt;
/* Hack -- Call the special "nuke" hook */
if (t->active_flag)
{
/* Call the "nuke" hook */
if (t->nuke_hook) (*t->nuke_hook)(t);
/* Remember */
t->active_flag = FALSE;
/* Assume not mapped */
t->mapped_flag = FALSE;
}
/* Nuke "displayed" */
term_win_nuke(t->old, w, h);
/* Kill "displayed" */
KILL(t->old, term_win);
/* Nuke "requested" */
term_win_nuke(t->scr, w, h);
/* Kill "requested" */
KILL(t->scr, term_win);
/* If needed */
if (t->mem)
{
/* Nuke "memorized" */
term_win_nuke(t->mem, w, h);
/* Kill "memorized" */
KILL(t->mem, term_win);
}
/* If needed */
if (t->tmp)
{
/* Nuke "temporary" */
term_win_nuke(t->tmp, w, h);
/* Kill "temporary" */
KILL(t->tmp, term_win);
}
/* Free some arrays */
C_KILL(t->x1, h, byte);
C_KILL(t->x2, h, byte);
/* Free the input queue */
C_KILL(t->key_queue, t->key_size, char);
/* Success */
return (0);
}
/*
* Initialize a term, using a window of the given size.
* Also prepare the "input queue" for "k" keypresses
* By default, the cursor starts out "invisible"
* By default, we "erase" using "black spaces"
*/
errr term_init(term *t, int w, int h, int k)
{
int y;
/* Wipe it */
(void)WIPE(t, term);
/* Prepare the input queue */
t->key_head = t->key_tail = 0;
/* Determine the input queue size */
t->key_size = k;
/* Allocate the input queue */
C_MAKE(t->key_queue, t->key_size, char);
/* Save the size */
t->wid = w;
t->hgt = h;
/* Allocate change arrays */
C_MAKE(t->x1, h, byte);
C_MAKE(t->x2, h, byte);
/* Allocate "displayed" */
MAKE(t->old, term_win);
/* Initialize "displayed" */
term_win_init(t->old, w, h);
/* Allocate "requested" */
MAKE(t->scr, term_win);
/* Initialize "requested" */
term_win_init(t->scr, w, h);
/* Assume change */
for (y = 0; y < h; y++)
{
/* Assume change */
t->x1[y] = 0;
t->x2[y] = w - 1;
}
/* Assume change */
t->y1 = 0;
t->y2 = h - 1;
/* Force "total erase" */
t->total_erase = TRUE;
/* Default "blank" */
t->attr_blank = 0;
t->char_blank = ' ';
/* Success */
return (0);
}
/*
* Determine if we are called in the same second as the last time?
* This *ASSUMES* that time_t is seconds past something. Is this portable?
*/
int cmovie_get_msecond(void)
{
#ifndef USE_PRECISE_CMOVIE
/* Not very precise, but portable */
time_t thisc;
thisc = time(NULL);
cmov_delta_time_msec = 300;
if (thisc == lastc)
{
return 1;
}
return 0;
#else /* Very precise but needs main-foo.c to define TERM_XTRA_GET_DELAY */
Term_xtra(TERM_XTRA_GET_DELAY, 0);
cmov_delta_time_msec = Term_xtra_long - cmov_last_time_msec;
cmov_last_time_msec = Term_xtra_long;
return 0;
#endif
}
void cmovie_init_second()
{
#ifndef USE_PRECISE_CMOVIE
/* Not very precise, but portable */
cmov_last_time_msec = 0;
#else /* Precise but need main-foo.c to have TERM_XTRA_GET_DELAY */
Term_xtra(TERM_XTRA_GET_DELAY, 0);
cmov_last_time_msec = Term_xtra_long;
#endif
}
|