[go: up one dir, main page]

Menu

[904618]: / src / i_util.c  Maximize  Restore  History

Download this file

2653 lines (2201 with data), 61.6 kB

   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
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2011-2013 GhostlyDeath <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// ----------------------------------------------------------------------------
// DESCRIPTION: Common Interface Utilities (to reduce code bloat and dup)
/***************
*** INCLUDES ***
***************/
/* System */
#if !defined(__REMOOD_USECCSTUB)
// On UNIX include the standard header
#if defined(__unix__) || defined(__APPLE__)
#include <unistd.h> // Standard Stuff
#include <dirent.h>
#include <sys/time.h>
// DJGPP has dirname in unistd.h
#if !defined(__DJGPP__)
#include <libgen.h>
#endif
#endif
// On Windows include windows.h
#if defined(_WIN32)
#include <windows.h>
#endif
// On DOS include dos.h (and conio.h for colors)
#if defined(__MSDOS__)
#include <dos.h>
#include <conio.h>
#include <dpmi.h>
#endif
// Include the standard C stuff here
#include <stdlib.h>
// POSIX Memory Mapping
#if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
#include <sys/mman.h>
#endif
#endif
/* Local */
#include "i_util.h"
#include "i_system.h"
#include "i_video.h"
#include "console.h"
#include "screen.h"
#include "m_argv.h"
#include "g_state.h"
#include "z_zone.h"
#include "v_video.h"
#include "w_wad.h"
#include "dstrings.h"
#include "d_clisrv.h"
#include "d_main.h"
#include "g_game.h"
#include "m_menu.h"
#include "sn.h"
#include "cl.h"
/****************
*** CONSTANTS ***
****************/
#define EVENTQUEUESIZE 128 // Max events allowed in queue
#define MODENAMELENGTH 16 // Length of mode name
#define MAX_QUIT_FUNCS 16 // Max number of quit functions
/* c_KeyNames -- Names for keys */
const char* const c_KeyNames[NUMIKEYBOARDKEYS][2] =
{
// Lower Control keys
{"null", "Null"}, // IKBK_NULL
{"key01", "Key 01"}, // 0x01
{"key02", "Key 02"}, // 0x02
{"key03", "Key 03"}, // 0x03
{"key04", "Key 04"}, // 0x04
{"key05", "Key 05"}, // 0x05
{"key06", "Key 06"}, // 0x06
{"key07", "Key 07"}, // 0x07
{"backspace", "Backspace"}, // IKBK_BACKSPACE = 0x08,
{"tab", "Tab"}, // IKBK_TAB = 0x09,
{"return", "Return/Enter"}, // IKBK_RETURN = 0x0A,
{"key0B", "Key 0B"}, // 0x0B
{"key0C", "Key 0C"}, // 0x0C
{"key0D", "Key 0D"}, // 0x0D
{"shift", "Shift"}, // IKBK_SHIFT = 0x0E,
{"ctrl", "Control"}, // IKBK_CTRL = 0x0F,
{"alt", "Alt"}, // IKBK_ALT = 0x10,
{"up", "Up Arrow"}, // IKBK_UP = 0x11,
{"down", "Down Arrow"}, // IKBK_DOWN,
{"left", "Left Arrow"}, // IKBK_LEFT,
{"right", "Right Arrow"}, // IKBK_RIGHT,
{"key15", "Key 15"}, // 0x15
{"key16", "Key 16"}, // 0x16
{"key17", "Key 17"}, // 0x17
{"key18", "Key 18"}, // 0x18
{"key19", "Key 19"}, // 0x19
{"key1A", "Key 1A"}, // 0x1A
{"escape", "Escape"}, // IKBK_ESCAPE = 0x1B,
{"key1C", "Key 1C"}, // 0x1C
{"key1D", "Key 1D"}, // 0x1D
{"key1E", "Key 1E"}, // 0x1E
{"key1F", "Key 1F"}, // 0x1F
// Standard ASCII
{"space", "Spacebar"}, // IKBK_SPACE = ' ',
{"exclaim", "Exclaimation Point"}, // IKBK_EXCLAIM,
{"quote", "Quote"}, // IKBK_QUOTE,
{"hash", "Hash"}, // IKBK_HASH,
{"dollar", "Dollar Sign"}, // IKBK_DOLLAR,
{"percent", "Percent"}, // IKBK_PERCENT,
{"ampersand", "Ampersand"}, // IKBK_AMPERSAND,
{"apostrophe", "Apostrophe"}, // IKBK_APOSTROPHE,
{"leftparenth", "Left Parenthesis"}, // IKBK_LEFTPARENTHESES,
{"rightparenth", "Right Parenthesis"}, // IKBK_RIGHTPARENTHESES,
{"asterisk", "Asterisk"}, // IKBK_ASTERISK,
{"plus", "Plus"}, // IKBK_PLUS,
{"comma", "Comma"}, // IKBK_COMMA,
{"minus", "Minus"}, // IKBK_MINUS,
{"period", "Period"}, // IKBK_PERIOD,
{"fslash", "Forward Slash"}, // IKBK_FORWARDSLASH,
{"0", "0"}, // IKBK_0,
{"1", "1"}, // IKBK_1,
{"2", "2"}, // IKBK_2,
{"3", "3"}, // IKBK_3,
{"4", "4"}, // IKBK_4,
{"5", "5"}, // IKBK_5,
{"6", "6"}, // IKBK_6,
{"7", "7"}, // IKBK_7,
{"8", "8"}, // IKBK_8,
{"9", "9"}, // IKBK_9,
{"colon", "Colon"}, // IKBK_COLON,
{"semicolon", "Semicolon"}, // IKBK_SEMICOLON,
{"lessthan", "Less Than"}, // IKBK_LESSTHAN,
{"equals", "Equals"}, // IKBK_EQUALS,
{"greaterthan", "Greater Than"}, // IKBK_GREATERTHAN,
{"questionmark", "Question Mark"}, // IKBK_QUESTION,
{"at", "At Sign"}, // IKBK_AT,
{"a", "A"}, // IKBK_A,
{"b", "B"}, // IKBK_B,
{"c", "C"}, // IKBK_C,
{"d", "D"}, // IKBK_D,
{"e", "E"}, // IKBK_E,
{"f", "F"}, // IKBK_F,
{"g", "G"}, // IKBK_G,
{"h", "H"}, // IKBK_H,
{"i", "I"}, // IKBK_I,
{"j", "J"}, // IKBK_J,
{"k", "K"}, // IKBK_K,
{"l", "L"}, // IKBK_L,
{"m", "M"}, // IKBK_M,
{"n", "N"}, // IKBK_N,
{"o", "O"}, // IKBK_O,
{"p", "P"}, // IKBK_P,
{"q", "Q"}, // IKBK_Q,
{"r", "R"}, // IKBK_R,
{"s", "S"}, // IKBK_S,
{"t", "T"}, // IKBK_T,
{"u", "U"}, // IKBK_U,
{"v", "V"}, // IKBK_V,
{"w", "W"}, // IKBK_W,
{"x", "X"}, // IKBK_X,
{"y", "Y"}, // IKBK_Y,
{"z", "Z"}, // IKBK_Z,
{"leftbracket", "Left Bracket"}, // IKBK_LEFTBRACKET,
{"backslash", "Backslash"}, // IKBK_BACKSLASH,
{"rightbracket", "Right Bracket"}, // IKBK_RIGHTBRACKET,
{"caret", "Caret"}, // IKBK_CARET,
{"underscore", "Underscore"}, // IKBK_UNDERSCORE,
{"grave", "Grave Accent"}, // IKBK_GRAVE,
{"key61", "Key 61"}, // 0x61
{"key62", "Key 62"}, // 0x62
{"key63", "Key 63"}, // 0x63
{"key64", "Key 64"}, // 0x64
{"key65", "Key 65"}, // 0x65
{"key66", "Key 66"}, // 0x66
{"key67", "Key 67"}, // 0x67
{"key68", "Key 68"}, // 0x68
{"key69", "Key 69"}, // 0x69
{"key6A", "Key 6A"}, // 0x6A
{"key6B", "Key 6B"}, // 0x6B
{"key6C", "Key 6C"}, // 0x6C
{"key6D", "Key 6D"}, // 0x6D
{"key6E", "Key 6E"}, // 0x6E
{"key6F", "Key 6F"}, // 0x6F
{"key70", "Key 70"}, // 0x70
{"key71", "Key 71"}, // 0x71
{"key72", "Key 72"}, // 0x72
{"key73", "Key 73"}, // 0x73
{"key74", "Key 74"}, // 0x74
{"key75", "Key 75"}, // 0x75
{"key76", "Key 76"}, // 0x76
{"key77", "Key 77"}, // 0x77
{"key78", "Key 78"}, // 0x78
{"key79", "Key 79"}, // 0x79
{"key7A", "Key 7A"}, // 0x7A
{"leftbrace", "Left Brace"}, // IKBK_LEFTBRACE = 0x7B,
{"pipe", "Pipe"}, // IKBK_PIPE,
{"rightbrace", "Right Brace"}, // IKBK_RIGHTBRACE,
{"tilde", "Tilde"}, // IKBK_TILDE,
// Cool Keys
{"delete", "Delete"}, // IKBK_KDELETE = 0x7F,
{"home", "Home"}, // IKBK_HOME,
{"end", "End"}, // IKBK_END,
{"ins", "Insert"}, // IKBK_INSERT,
{"pgup", "Page Up"}, // IKBK_PAGEUP,
{"pgdn", "Page Down"}, // IKBK_PAGEDOWN,
{"print", "Print Screen"}, // IKBK_PRINTSCREEN,
{"numlock", "Num Lock"}, // IKBK_NUMLOCK,
{"capslock", "Caps Lock"}, // IKBK_CAPSLOCK,
{"scrolllock", "Scroll Lock"}, // IKBK_SCROLLLOCK,
{"pause", "Pause"}, // IKBK_PAUSE,
{"f1", "F1"}, // IKBK_F1,
{"f2", "F2"}, // IKBK_F2,
{"f3", "F3"}, // IKBK_F3,
{"f4", "F4"}, // IKBK_F4,
{"f5", "F5"}, // IKBK_F5,
{"f6", "F6"}, // IKBK_F6,
{"f7", "F7"}, // IKBK_F7,
{"f8", "F8"}, // IKBK_F8,
{"f9", "F9"}, // IKBK_F9,
{"f10", "F10"}, // IKBK_F10,
{"f11", "F11"}, // IKBK_F11,
{"f12", "F12"}, // IKBK_F12,
{"num0", "NumPad 0"}, // IKBK_NUM0,
{"num1", "NumPad 1"}, // IKBK_NUM1,
{"num2", "NumPad 2"}, // IKBK_NUM2,
{"num3", "NumPad 3"}, // IKBK_NUM3,
{"num4", "NumPad 4"}, // IKBK_NUM4,
{"num5", "NumPad 5"}, // IKBK_NUM5,
{"num6", "NumPad 6"}, // IKBK_NUM6,
{"num7", "NumPad 7"}, // IKBK_NUM7,
{"num8", "NumPad 8"}, // IKBK_NUM8,
{"num9", "NumPad 9"}, // IKBK_NUM9,
{"numdiv", "NumPad Divide"}, // IKBK_NUMDIVIDE,
{"nummul", "NumPad Multiply"}, // IKBK_NUMMULTIPLY,
{"numsub", "NumPad Subtract"}, // IKBK_NUMSUBTRACT,
{"numadd", "NumPad Addition"}, // IKBK_NUMADD,
{"nument", "NumPad Enter"}, // IKBK_NUMENTER,
{"numdec", "NumPad Decimal Point"}, // IKBK_NUMPERIOD,
{"numdel", "NumPad Delete"}, // IKBK_NUMDELETE,
{"logokey", "Logo Key"}, // IKBK_WINDOWSKEY,
{"menukey", "Menu Key"}, // IKBK_MENUKEY,
{"euro", "Euro"}, // IKBK_EVILEURO,
};
/**************
*** GLOBALS ***
**************/
uint8_t graphics_started = 0;
/* My stuff */
static bool_t l_MouseOK = false; // OK to use the mouse code?
static bool_t l_NoMouseGrab = false; // Don't grab mouse
/*****************
*** STRUCTURES ***
*****************/
/* I_VideoMode_t -- A video mode */
typedef struct I_VideoMode_s
{
uint16_t Width; // Screen Width
uint16_t Height; // Screen height
char Name[MODENAMELENGTH]; // Mode name
} I_VideoMode_t;
/*************
*** LOCALS ***
*************/
static I_VideoMode_t* l_Modes = NULL; // Video Modes
static size_t l_NumModes = 0; // Number of video modes
static I_EventEx_t l_EventQ[EVENTQUEUESIZE]; // Events in queue
static size_t l_EQRead = 0; // Read position in queue
static size_t l_EQWrite = 0; // Write position in queue
typedef void (*quitfuncptr)(void);
static quitfuncptr quit_funcs[MAX_QUIT_FUNCS];
static bool_t l_JoyOK = false; // Joysticks OK
static uint8_t l_NumJoys; // Number of joysticks
static uint8_t l_RealJoyMap[MAXJOYSTICKS]; // Joystick mappings
// i_enablemouse -- Enable mouse input
CONL_StaticVar_t l_IEnableMouse =
{
CLVT_INTEGER, c_CVPVBoolean, CLVF_SAVE,
"i_enablemouse", DSTR_CVHINT_IENABLEMOUSE, CLVVT_STRING, "true",
NULL
};
// i_enablejoystick -- Enable joystick input
CONL_StaticVar_t l_IEnableJoystick =
{
CLVT_INTEGER, c_CVPVBoolean, CLVF_SAVE,
"i_enablejoystick", DSTR_CVHINT_IENABLEJOYSTICK, CLVVT_STRING, "true",
NULL
};
/****************
*** FUNCTIONS ***
****************/
/* I_EventExPush() -- Pushes an event to the queue */
void I_EventExPush(const I_EventEx_t* const a_Event)
{
/* Check */
if (!a_Event)
return;
/* Mouse/Joystick event and they are not OK */
// I know this seems kinda bad, but some interfaces always handle mouse inputs
// and whatnot, so it should just be ignored here.
if ((a_Event->Type == IET_MOUSE && !l_MouseOK) || (a_Event->Type == IET_JOYSTICK && !l_JoyOK))
return;
/* Keyboard key, but no key code */
if (a_Event->Type == IET_KEYBOARD)
if (a_Event->Data.Keyboard.KeyCode == IKBK_NULL)
return;
/* Write at current write pos */
l_EventQ[l_EQWrite++] = *a_Event;
// Overlap?
if (l_EQWrite >= EVENTQUEUESIZE)
l_EQWrite = 0;
// Got too many events in Q?
if (l_EQWrite == l_EQRead)
{
// Indicate overflow
if (devparm)
CONL_PrintF("I_EventExPush: Event overflow (%i)!\n", (int)l_EQWrite);
// Increment reader
l_EQRead++;
// Reader overlap?
if (l_EQRead >= EVENTQUEUESIZE)
l_EQRead = 0;
}
}
/* I_EventExPop() -- Pops event from the queue */
bool_t I_EventExPop(I_EventEx_t* const a_Event)
{
/* Determine whether something is in the queue currently */
if (l_EQRead == l_EQWrite)
return false; // Nothing!
/* If event was passed, copy */
if (a_Event)
*a_Event = l_EventQ[l_EQRead];
/* Remove from queue */
// Wipe
memset(&l_EventQ[l_EQRead], 0, sizeof(l_EventQ[l_EQRead]));
// Increment
l_EQRead++;
// Overlap?
if (l_EQRead >= EVENTQUEUESIZE)
l_EQRead = 0;
/* Something was there */
return true;
}
/* I_OsPolling() -- Handles operating system polling (all of it) */
void I_OsPolling(void)
{
I_EventEx_t Event;
static int DevEvents;
/* Check for event debugging */
if (!DevEvents)
DevEvents = (!!M_CheckParm("-devevent")) + 1;
/* Just read all events */
I_GetEvent();
/* Translate events to old Doom events */
while (I_EventExPop(&Event))
{
// Debug
if (DevEvents == 2)
{
// Which type of event?
switch (Event.Type)
{
// Quit
case IET_QUIT:
CONL_PrintF("Event: QUIT!!\n");
break;
// Keyboard
case IET_KEYBOARD:
CONL_PrintF("Event: KEYBRD St:%2s Rp:%2s Kc:%03X Ch:%c [aka %s]\n",
(Event.Data.Keyboard.Down ? "Dn" : "Up"),
(Event.Data.Keyboard.Repeat ? "Re" : "--"),
Event.Data.Keyboard.KeyCode, (Event.Data.Keyboard.Character & 0x7F ? Event.Data.Keyboard.Character & 0x7F : ' '),
c_KeyNames[Event.Data.Keyboard.KeyCode][0]);
break;
// Mouse
case IET_MOUSE:
CONL_PrintF("Event: MOUSE_ Id:%2i Ps:(%5i, %5i)", Event.Data.Mouse.MouseID, Event.Data.Mouse.Pos[0], Event.Data.Mouse.Pos[1]);
// Button
if (Event.Data.Mouse.Button)
CONL_PrintF(" St:%2s Bt:%2i\n", (Event.Data.Mouse.Down ? "Dn" : "Up"), Event.Data.Mouse.Button);
// Movement
else
CONL_PrintF(" Mv:(%+4i, %+4i)\n", Event.Data.Mouse.Move[0], Event.Data.Mouse.Move[1]);
break;
// Joystick
case IET_JOYSTICK:
CONL_PrintF("Event: JOYSTK Id:%2i", Event.Data.Joystick.JoyID);
// Button
if (Event.Data.Joystick.Button)
CONL_PrintF(" St:%2s Bt:%2i\n", (Event.Data.Joystick.Down ? "Dn" : "Up"), Event.Data.Joystick.Button);
// Axis
else
CONL_PrintF(" Ax:%2i Vl:%+6i\n", Event.Data.Joystick.Axis, Event.Data.Joystick.Value);
break;
// OSK
case IET_SYNTHOSK:
CONL_PrintF("Event: SYNOSK So:%2u Ri:%+1i Dn:%+1i Pr:%u Sh:%u Dr:%08x Cn:%u Kc:%03X Ch:%c [aka %s]\n",
Event.Data.SynthOSK.SNum,
Event.Data.SynthOSK.Right,
Event.Data.SynthOSK.Down,
Event.Data.SynthOSK.Press,
Event.Data.SynthOSK.Shift,
Event.Data.SynthOSK.Direct,
Event.Data.SynthOSK.Cancel,
Event.Data.SynthOSK.KeyCode, (Event.Data.SynthOSK.Character & 0x7F ? Event.Data.SynthOSK.Character & 0x7F : ' '),
c_KeyNames[Event.Data.SynthOSK.KeyCode][0]
);
break;
// Unknown
default:
CONL_PrintF("Event: UNKNWN\n");
break;
}
}
// Quit event?
if (Event.Type == IET_QUIT)
{
if (gamestate == GS_DEMOSCREEN || demoplayback)
I_Quit();
continue;
}
#if !defined(__REMOOD_DEDICATED)
if (UI_HandleEvent(&Event, true))
continue;
#endif
if (CONL_HandleEvent(&Event))
continue;
if (M_SMHandleEvent(&Event))
continue;
if (CL_SockEvent(&Event))
continue;
#if !defined(__REMOOD_DEDICATED)
if (UI_HandleEvent(&Event, false))
continue;
#endif
}
}
/* I_DoMouseGrabbing() -- Does grabbing if the mouse should be grabbed */
void I_DoMouseGrabbing(void)
{
#if !defined(__REMOOD_DEDICATED)
static bool_t Grabbed = false;
bool_t New = false;
/* If the mouse is not OK, don't touch grabbing */
if (!l_MouseOK)
return;
/* Don't grab if... */
// g_DedicatedServer Server, Watching demo, not playing, in a menu, in the console
New = !(g_DedicatedServer || demoplayback || UI_GrabMouse() || gamestate == GS_DEMOSCREEN);
if (New != Grabbed && !l_NoMouseGrab)
{
// Change grab
I_MouseGrab(New);
// Set grabbed to new
Grabbed = New;
}
#else
return;
#endif
}
/* I_StartupMouse() -- Initializes the mouse */
void I_StartupMouse(void)
{
static bool_t VarRegged;
/* Register var? */
if (!VarRegged)
{
CONL_VarRegister(&l_IEnableMouse);
VarRegged = true;
}
/* Check parameter */
// Disable Mouse?
if (M_CheckParm("-nomouse"))
{
CONL_PrintF("I_StartupMouse: -nomouse is preventing mice from being used.\n");
return;
}
// Disable Grabbing?
if (M_CheckParm("-nomousegrab"))
l_NoMouseGrab = true;
/* Enabling the mouse */
if (l_IEnableMouse.Value->Int)
{
// Enabled?
if (l_MouseOK)
return;
// Probe the mouse
if (!I_ProbeMouse(0))
{
CONL_PrintF("I_StartupMouse: There is no mouse\n");
return;
}
// Enable
CONL_PrintF("I_StartupMouse: Mouse enabled.\n");
l_MouseOK = true;
}
/* Disabling the mouse */
else
{
// Not enabled?
if (!l_MouseOK)
return;
// Ungrab the mouse before removal
I_MouseGrab(false);
// Remove the mouse
I_RemoveMouse(0);
// Disable
CONL_PrintF("I_StartupMouse: Mouse disabled.\n");
l_MouseOK = false;
}
}
/* I_StartupMouse2() -- Initializes the second mouse */
void I_StartupMouse2(void)
{
/* Check parameter */
if (M_CheckParm("-nomouse"))
{
CONL_PrintF("I_StartupMouse: -nomouse is preventing mice from being used.\n");
return;
}
/* Enabling the mouse */
if (l_IEnableMouse.Value->Int)
{
// Probe the mouse
if (!I_ProbeMouse(1))
{
CONL_PrintF("I_StartupMouse: There is no secondary mouse\n");
return;
}
}
/* Disabling the mouse */
else
{
// Remove the mouse
I_RemoveMouse(1);
}
}
/* I_InitJoystick() -- Initialize the joystick */
void I_InitJoystick(void)
{
#define BUFSIZE 256
char Buf[BUFSIZE];
char CoolBuf[BUFSIZE];
uint32_t ID, NumAxis, NumButtons;
size_t i;
static bool_t VarRegged;
/* Register var? */
if (!VarRegged)
{
CONL_VarRegister(&l_IEnableJoystick);
VarRegged = true;
}
/* Check parameter */
if (M_CheckParm("-nojoy"))
{
CONL_PrintF("I_InitJoystick: -nojoy is preventing joysticks from being used.\n");
return;
}
/* Enabling the joystick */
if (l_IEnableJoystick.Value->Int)
{
// Enabled?
if (l_JoyOK)
return;
// Probe joysticks
l_NumJoys = I_ProbeJoysticks();
if (!l_NumJoys)
{
CONL_PrintF("I_InitJoystick: There are no joysticks.\n");
return;
}
// Print number of sticks
CONL_PrintF("I_InitJoystick: There are %i joystick(s).\n", (int)l_NumJoys);
// Map base joysticks
for (i = 0; i < MAXJOYSTICKS; i++)
l_RealJoyMap[i] = i;
// Print name of joysticks
for (i = 0; i < l_NumJoys; i++)
{
if (I_GetJoystickID(i, &ID, Buf, BUFSIZE, CoolBuf, BUFSIZE))
CONL_PrintF("I_InitJoystick: Joystick ID %08x is called \"%s\" (Full name \"%s\").\n", ID, CoolBuf, Buf);
if (I_GetJoystickCounts(i, &NumAxis, &NumButtons))
CONL_PrintF("I_InitJoystick: Has %u axis and %u buttons.\n", NumAxis, NumButtons);
}
// Enable
l_JoyOK = true;
}
/* Disabling the joystick */
else
{
// Not enabled?
if (!l_JoyOK)
return;
// Destroy joysticks
I_RemoveJoysticks();
// Disable
l_JoyOK = false;
}
#undef BUFSIZE
}
/* I_NumJoysticks() -- Returns Joystick Count */
uint8_t I_NumJoysticks(void)
{
return l_NumJoys;
}
/* VID_NumModes() -- Returns the number of video modes */
int VID_NumModes(void)
{
return l_NumModes;
}
/* VID_GetModeName() -- Gets the name of the video modes */
char* __REMOOD_DEPRECATED VID_GetModeName(int a_ModeNum)
{
/* Check */
if (a_ModeNum < 0 || a_ModeNum >= l_NumModes)
return NULL;
return l_Modes[a_ModeNum].Name;
}
/* VID_ClosestMode() -- Returns the closest mode against width and height */
// Ignore fullscreen for now
int VID_ClosestMode(int* const a_WidthP, int* const a_HeightP, const bool_t a_Fullscreen)
{
size_t i, BestMode;
/* Check */
if (!a_WidthP || !a_HeightP || !l_NumModes)
return 0;
/* Go through list */
for (BestMode = 0, i = 0; i < l_NumModes; i++)
// Width matches
if (l_Modes[i].Width == *a_WidthP)
{
// Height matches
if (l_Modes[i].Height == *a_HeightP)
{
BestMode = i;
break; // it is here!
}
// Otherwise, set the best mode as long as height diff is lower
else if (abs((int32_t)l_Modes[i].Height - (int32_t)*a_HeightP) < abs((int32_t)l_Modes[BestMode].Height - (int32_t)*a_HeightP))
BestMode = i;
}
/* Return mode */
*a_WidthP = l_Modes[BestMode].Width;
*a_HeightP = l_Modes[BestMode].Height;
return BestMode;
}
/* VID_GetModeForSize() -- Gets the closest mode for a widthxheight */
int __REMOOD_DEPRECATED VID_GetModeForSize(int a_Width, int a_Height)
{
int w, h;
/* Set */
w = a_Width;
h = a_Height;
/* Return whatever */
return VID_ClosestMode(&w, &h, true);
}
/* VID_AddMode() -- Add video mode to the list, either being fullscreen or not */
// Ignore fullscreen for now
bool_t VID_AddMode(const int a_Width, const int a_Height, const bool_t a_Fullscreen)
{
size_t i;
/* Check */
if (!a_Width || !a_Height)
return false;
/* Was this mode already set? */
for (i = 0; i < l_NumModes; i++)
if (l_Modes[i].Width == a_Width && l_Modes[i].Height == a_Height)
return true;
/* Resize mode list and set */
l_Modes = I_SysRealloc(l_Modes, sizeof(*l_Modes) * (l_NumModes + 1));
// Set
snprintf(l_Modes[l_NumModes].Name, MODENAMELENGTH, "%ix%i", a_Width, a_Height);
l_Modes[l_NumModes].Width = a_Width;
l_Modes[l_NumModes++].Height = a_Height;
/* Success! */
return true;
}
extern CONL_StaticVar_t l_SCRWidth;
extern CONL_StaticVar_t l_SCRHeight;
extern CONL_StaticVar_t l_SCRDepth;
extern CONL_StaticVar_t l_SCRFullScreen;
/* VID_SetMode() -- Sets the specified video mode */
// Funny thing is, despite returning an int, Legacy never checked if it worked!
int VID_SetMode(int a_ModeNum)
{
int Try;
int32_t w, h;
/* Check */
if (a_ModeNum < 0 || a_ModeNum >= l_NumModes)
return 0; // Failure despite not being checked!
/* Try to set the mode */
for (Try = 0; Try < 6; Try++)
{
// See which mode to attempt
switch (Try)
{
// Actual Mode
case 0:
w = l_Modes[a_ModeNum].Width,
h = l_Modes[a_ModeNum].Height;
break;
// Saved Default
case 1:
w = l_SCRWidth.Value->Int;
h = l_SCRHeight.Value->Int;
break;
// 640x480
case 2:
w = 640;
h = 480;
break;
// 320x240
case 3:
w = 320;
h = 240;
break;
// 320x200
case 4:
w = 320;
h = 200;
break;
// First Found Mode
case 5:
w = l_Modes[0].Width,
h = l_Modes[0].Height;
break;
// Unknown
default:
break;
}
// Try it
if (I_SetVideoMode(w, h, l_SCRFullScreen.Value[0].Int, l_SCRDepth.Value->Int))
break;
}
/* Too many failed tries? */
if (Try >= 6)
return false;
/* Success! */
return true;
}
/* I_UtilWinArgToUNIXArg() -- Converts Windows-style command line to a UNIX one */
bool_t I_UtilWinArgToUNIXArg(int* const a_argc, char** *const a_argv, const char* const a_Win)
{
/* Check */
if (!a_argc || !a_argv || !a_Win)
return false;
return true;
}
/* I_VideoPreInit() -- Common nitialization before everything */
bool_t I_VideoPreInit(void)
{
/* If graphics are already started, do not start again */
if (graphics_started)
return false;
/* Reset vid structure fields */
vid.bpp = 1;
vid.width = BASEVIDWIDTH;
vid.height = BASEVIDHEIGHT;
vid.rowbytes = vid.width * vid.bpp;
vid.recalc = true;
vid.bpp = 1;
vid.gl = 0;
return true;
}
/* I_VideoBefore320200Init() -- Initialization before initial 320x200 set */
bool_t I_VideoBefore320200Init(void)
{
return true;
}
/* I_VideoPostInit() -- Initialization before end of function */
bool_t I_VideoPostInit(void)
{
/* Set started */
graphics_started = 1;
/* Add exit function */
I_AddExitFunc(I_ShutdownGraphics);
return true;
}
/* I_VideoGenericInit() -- Generic Initialization */
bool_t I_VideoGenericInit(void)
{
/* Initialize before mode set */
if (!I_VideoBefore320200Init())
return;
if (!I_SetVideoMode(320, 200, false, 1)) // 320x200 console scroller, never fullscreen
return;
/* Prepare the video mode list */
if (!I_VideoPostInit())
return;
}
/* I_VideoSetBuffer() -- Sets the video buffer */
// This is here so I do not constantly repeat code in I_SetVideoMode()
void I_VideoSetBuffer(const uint32_t a_Width, const uint32_t a_Height, const uint32_t a_Pitch, uint8_t* const a_Direct, const bool_t a_HWDblBuf, const bool_t a_GL, const uint32_t a_Depth)
{
int w, h;
/* Setup */
w = a_Width;
h = a_Height;
/* Set direct video buffer */
vid.rowbytes = a_Pitch; // Set rowbytes to pitch
vid.direct = a_Direct; // Set direct, if it is passed (if not, direct access not supported)
vid.width = a_Width;
vid.height = a_Height;
vid.modenum = VID_ClosestMode(&w, &h, true);
vid.HWDblBuf = a_HWDblBuf;
#if !defined(__REMOOD_DEDICATED)
UI_SetBitDepth((a_GL ? I_VIDEOGLMODECONST : a_Depth));
#endif
/* Nothing after this is done in GL mode */
if (a_GL)
{
vid.bpp = 0;
vid.gl = 1;
return;
}
// Bitdepth and GL mode
vid.bpp = a_Depth;
vid.gl = 0;
/* Allocate buffer for mode */
// If hardware double buffer is enabled, use direct buffer
if (vid.HWDblBuf && vid.direct)
vid.buffer = vid.direct;
// Otherwise create a buffer
else
{
vid.buffer = I_SysAlloc(a_Width * a_Height * vid.bpp * NUMSCREENS);
// Oops!
if (!vid.buffer)
I_Error("You do not have enough memory to use this video mode!");
// Clear buffer
memset(vid.buffer, 0, a_Width * a_Height * vid.bpp * NUMSCREENS);
}
/* Initialize video stuff (ouch) */
V_Init();
}
/* I_VideoUnsetBuffer() -- Unsets the video buffer */
void I_VideoUnsetBuffer(void)
{
/* Free */
if (!vid.HWDblBuf && vid.buffer != vid.direct)
{
if (vid.buffer)
I_SysFree(vid.buffer);
vid.buffer = NULL;
}
/* Clear direct */
vid.rowbytes = 0;
vid.direct = NULL;
vid.width = 0;
vid.height = 0;
}
/* I_VideoSoftBuffer() -- Returns the soft buffer */
uint8_t* I_VideoSoftBuffer(uint32_t* const a_WidthP, uint32_t* const a_HeightP, uint32_t* const a_DepthP, uint32_t* const a_PitchP)
{
/* Set sizes */
if (a_WidthP)
*a_WidthP = vid.width;
if (a_HeightP)
*a_HeightP = vid.height;
if (a_DepthP)
*a_DepthP = vid.bpp;
if (a_PitchP)
*a_PitchP = vid.rowbytes;
/* Return soft buffer */
return I_GetVideoBuffer(IVS_BACKBUFFER, a_WidthP);
}
/* I_BeginRead() -- Before a file is read */
// Flashes floppy disk (or CD)
void I_BeginRead(void)
{
}
/* I_EndRead() -- When a file is no longer being read */
// Flashes floppy disk (or CD)
void I_EndRead(void)
{
}
/* I_GetTime() -- Returns time since the game started */
uint32_t I_GetTime(void)
{
return (I_GetTimeMS() * TICRATE) / 1000;
}
/* I_DumpTemporary() -- Creates a temporary file with data inside of it */
bool_t I_DumpTemporary(char* const a_PathBuf, const size_t a_PathSize, const uint8_t* const a_Data, const size_t a_Size)
{
#if defined(__unix__)
int fd;
#elif defined(_WIN32)
TCHAR Buf[PATH_MAX];
#endif
/* Check */
if (!a_PathBuf || !a_PathSize || !a_Data || !a_Size)
return false;
/* Under UNIX, use mkstemp() */
#if defined(__unix__)
// Create it
snprintf(a_PathBuf, a_PathSize,
#if defined(__MSDOS__)
"rmXXXXXX" // On DJGPP with DOS, don't place in /tmp/ because that will always fail!
#else
"/tmp/rmXXXXXX"
#endif
);
if ((fd = mkstemp(a_PathBuf)) == -1)
return false;
// Place data in fd
write(fd, a_Data, a_Size);
close(fd);
return true;
/* Under Windows, use GetTempPath()/GetTempFileName() */
#elif defined(_WIN32)
/* For everything else, just guess */
#else
/* */
#endif
/* Failure */
return false;
}
/* I_ReadScreen() -- Reads the screen into pointer */
// This is enough to make the code work as it should
void I_ReadScreen(uint8_t* scr)
{
uint8_t* p;
uint32_t w, h, b;
/* Check */
if (!scr)
return;
/* Blind copy */
p = I_VideoSoftBuffer(&w, &h, &b, NULL);
memcpy(scr, p, w * h * b);
}
/* I_ShowEndTxt() -- Shows the ending text screen */
void I_ShowEndTxt(const uint8_t* const a_TextData)
{
size_t i, c, Cols;
const char* p;
/* Check */
if (!a_TextData)
return;
/* Get environment */
// Columns?
if ((p = I_GetEnvironment("COLUMNS")))
Cols = atoi(p);
else
Cols = 79;
/* Load ENDTXT */
for (i = 0; i < 4000; i += 2)
{
// Get logical column number
c = (i >> 1) % 80;
// Add a newline
if (c == 0 && Cols != 80)
I_TextModeNextLine();
// Print character
if (c < Cols) // but only if it fits!
I_TextModeChar(a_TextData[i], a_TextData[i + 1]);
}
/* Leave text mode */
I_TextModeNextLine();
I_TextMode(false);
}
/* I_TextModeNextLine() -- Prints a new line */
void I_TextModeNextLine(void)
{
/* Print character */
#if defined(__MSDOS__)
cprintf("\r\n");
#else
printf("\n");
#endif
}
/* I_TextModeChar() -- Prints a text mode character */
void I_TextModeChar(const uint8_t a_Char, const uint8_t Attr)
{
uint8_t BG, FG;
bool_t Blink;
#if defined(_WIN32)
HANDLE* StdOut;
static const uint32_t c_BGColorToWin32[8] =
{
0,
BACKGROUND_BLUE,
BACKGROUND_GREEN,
BACKGROUND_BLUE | BACKGROUND_GREEN,
BACKGROUND_RED,
BACKGROUND_RED | BACKGROUND_BLUE,
BACKGROUND_RED | BACKGROUND_GREEN,
BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE,
};
static const uint32_t c_FGColorToWin32[8] =
{
0,
FOREGROUND_BLUE,
FOREGROUND_GREEN,
FOREGROUND_BLUE | FOREGROUND_GREEN,
FOREGROUND_RED,
FOREGROUND_RED | FOREGROUND_BLUE,
FOREGROUND_RED | FOREGROUND_GREEN,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
};
#endif
static const char c_ColorToVT[8] =
{
0, 4, 2, 6, 1, 5, 3, 7
}; // Colors to VT
static const uint8_t c_ASCIIMap[256] =
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
' ', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', 'M', 'F', '?', '?', '#', // 0
'>', '<', '|', '!', 'P', 'S', '=', '|', '^', 'v', '<', '>', 'L', '=', '^', 'v', // 1
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', // 2
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', // 3
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', // 4
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', // 5
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', // 6
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '^', // 7
'C', 'u', 'e', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'i', 'i', 'i', 'A', 'A', // 8
'E', '@', '&', 'o', 'o', 'o', 'u', 'u', 'y', 'O', 'U', 'c', 'E', 'Y', 'P', 'f', // 9
'a', 'i', 'o', 'u', 'n', 'N', '*', 'o', '?', '/', '\\', '/', '/', '!', '<', '>', // A
'-', '=', '#', '|', '|', '|', '|', '+', '+', '|', '|', '+', '+', '+', '+', '+', // B
'+', '+', '+', '+', '-', '+', '|', '|', '+', '+', '-', '-', '|', '-', '+', '-', // C
'-', '-', '-', '+', '+', '+', '+', '+', '+', '+', '+', '#', '#', '#', '#', '#', // D
'a', 'B', 'r', 'n', 'E', 'o', 'u', 't', 'o', 'h', 'h', 'o', '8', 'o', 'q', 'n', // E
'=', '+', '>', '<', 'f', 'j', '/', '=', 'o', '.', '.', '/', 'n', '2', '.', ' ', // F
};
/* Get common attribute colors */
FG = Attr & 0xF;
BG = (Attr >> 4) & 0x7;
Blink = (Attr >> 7) & 1;
/* Create attribute */
// Use DOS color functions
#if defined(__MSDOS__)
// ENDOOM uses DOS attributes
textattr(Attr);
// Use Win32 console color functions
#elif defined(_WIN32)
#if !defined(_WIN32_WCE)
// GetConsoleScreenBufferInfo
StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(StdOut, c_BGColorToWin32[BG] | c_FGColorToWin32[FG & 7] | ((FG & 8) ? FOREGROUND_INTENSITY : 0));
#endif
// Use VT escape characters
#elif defined(__unix__)
if (FG & 8)
printf("%c[1m", 0x1B);
printf("%c[%i;%im", 0x1B, 30 + c_ColorToVT[FG & 7], 40 + c_ColorToVT[BG]);
// Don't bother with formatting
#else
//
#endif
/* Print character */
#if defined(__MSDOS__)
cprintf
#else
printf
#endif
("%c", c_ASCIIMap[a_Char]);
/* Reset attributes */
// Use DOS color functions
#if defined(__MSDOS__)
textattr(1);
// Use Win32 console color functions
#elif defined(_WIN32)
// Use VT escape characters
#elif defined(__unix__)
printf("%c[0m", 0x1B);
// Don't bother with formatting
#else
//
#endif
}
/* I_AddExitFunc() -- Adds an exit function */
void I_AddExitFunc(void (*func)(void))
{
int c;
/* Check */
if (!func)
return;
/* Do not add function twice */
for (c = 0; c < MAX_QUIT_FUNCS; c++)
if (quit_funcs[c] == func)
return;
/* Look in array */
for (c = 0; c < MAX_QUIT_FUNCS; c++)
if (!quit_funcs[c])
{
quit_funcs[c] = func;
break;
}
}
/* I_RemoveExitFunc() -- Removes an exit function */
void I_RemoveExitFunc(void (*func)(void))
{
int c;
for (c = 0; c < MAX_QUIT_FUNCS; c++)
if (quit_funcs[c] == func)
{
while (c < MAX_QUIT_FUNCS - 1)
{
quit_funcs[c] = quit_funcs[c + 1];
c++;
}
quit_funcs[MAX_QUIT_FUNCS - 1] = NULL;
break;
}
}
/* I_ShutdownSystem() -- Shuts the system down */
void I_ShutdownSystem(void)
{
int c;
uint8_t* EDData[2] = { 0, 0 };
WX_WADEntry_t* Entry;
uint8_t* Temp;
size_t Size;
/* Before we start exiting, load ENDOOM, ENREMOOD */
for (c = 0; c < 2; c++)
{
// Get it
Entry = WX_EntryForName(NULL, (c ? "ENREMOOD" : "ENDOOM"), false);
// Check
if (!Entry)
continue;
// Get size and data
Temp = WX_CacheEntry(Entry);
Size = WX_GetEntrySize(Entry);
// Duplicate
EDData[c] = I_SysAlloc(4000);
if (EDData[c])
{
memset(EDData[c], 0, 4000);
memmove(EDData[c], Temp, (Size <= 4000 ? Size : 4000));
}
}
/* Pre exit func */
I_SystemPreExit();
/* Call functions */
for (c = MAX_QUIT_FUNCS - 1; c >= 0; c--)
if (quit_funcs[c])
(*quit_funcs[c])();
/* Post exit func */
I_SystemPostExit();
/* Show the end text */
for (c = 0; c < 2; c++)
if (EDData[c])
{
I_ShowEndTxt(EDData[c]);
I_SysFree(EDData[c]);
}
}
/* I_mkdir() -- Creates a new directory */
int I_mkdir(const char* a_Path, int a_UNIXPowers)
{
#if defined(__REMOOD_SYSTEM_WINDOWS) || defined(__REMOOD_SYSTEM_WINCE) || defined(__REMOOD_USECCSTUB)
return mkdir(a_Path);
#else
// Ignore UNIX Powers
return mkdir(a_Path, S_IRUSR | S_IWUSR | S_IXUSR);
#endif
}
/* I_GetUserName() -- Returns the current username */
const char* I_GetUserName(void)
{
#define MAXUSERNAME 32 // Username limit (youch)
static char RememberName[MAXUSERNAME];
const char* p;
#if defined(_WIN32_WCE)
LONG ErrRet;
HKEY hKey;
DWORD ByteCount;
DWORD Type;
TCHAR* ValBuf;
size_t i;
#elif defined(_WIN32)
TCHAR Buf[MAXUSERNAME];
DWORD Size;
size_t i;
#endif
/* Clear Buffer */
memset(RememberName, 0, sizeof(RememberName));
/* Try system specific username getting */
// Under WinCE, Get the name of the owner (HKEY_CURRENT_USER\Control Panel\Owner)
#if defined(_WIN32_WCE)
ErrRet = RegOpenKeyEx(HKEY_CURRENT_USER, (LPCWSTR)L"\\Control Panel\\Owner", 0, 0, &hKey);
// If it is error success, it worked ok
if (ErrRet == ERROR_SUCCESS)
{
// Get the size of the key first
ErrRet = RegQueryValueEx(hKey, (LPCWSTR)L"Owner", NULL, &Type, NULL, &ByteCount);
if (ByteCount > 0)
{
// Allocate an array big enough for the value
ValBuf = Z_Malloc(sizeof(TCHAR) * (ByteCount + 1), PU_STATIC, NULL);
// Re-obtain the value
ErrRet = RegQueryValueEx(hKey, (LPCWSTR)L"Owner", NULL, &Type, (LPBYTE)ValBuf, &ByteCount);
// Copy value up to 32
for (i = 0; ValBuf[i] && i < 31; i++)
RememberName[i] = ValBuf[i];
// Clear value
Z_Free(ValBuf);
}
// Be sure to close the key
RegCloseKey(hKey);
// Return the name obtained
if (RememberName[0])
return RememberName;
}
// Under Win32, use GetUserName
#elif defined(_WIN32)
Size = MAXUSERNAME - 1;
if (GetUserName(Buf, &Size))
{
// Cheap copy
for (i = 0; i <= Size; i++)
RememberName[i] = Buf[i] & 0x7F;
return RememberName;
}
// Under UNIX, use getlogin
#elif defined(__unix__)
// prefer getlogin_r if it exists
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
if (getlogin_r(RememberName, MAXUSERNAME - 1))
return RememberName;
// Otherwise use getlogin
#else
p = getlogin();
if (p)
{
// Dupe string
strncpy(RememberName, p, MAXUSERNAME - 1);
return RememberName;
}
#endif
// Otherwise whoops!
#else
#endif
/* Try environment variables that usually exist */
// USER, USERNAME, LOGNAME
p = I_GetEnvironment("USER");
// Nope
if (!p)
{
p = I_GetEnvironment("USERNAME");
// Nope
if (!p)
{
p = I_GetEnvironment("LOGNAME");
// Nope
if (!p)
return NULL;
}
}
// Copy p to buffer and return the buffer
strncpy(RememberName, p, MAXUSERNAME - 1);
return RememberName;
#undef MAXUSERNAME
}
/* I_GetStorageDir() -- Get location to store local data */
size_t I_GetStorageDir(char* const a_Out, const size_t a_Size, const I_DataStorageType_t a_Type)
{
char* Env;
size_t i;
#if defined(_WIN32)
HMODULE hMod;
char* BaseN;
TCHAR Path[MAX_PATH];
#elif defined(__unix__)
char CheckPath[PATH_MAX];
char* Clone, *DirName;
char* PathEnv, *End;
size_t j;
bool_t IsDir;
#endif
/* Check */
if (!a_Out || !a_Size || a_Type < 0 || a_Type >= NUMDATASTORAGETYPES)
return 0;
/* Clear */
memset(a_Out, 0, sizeof(*a_Out) * a_Size);
/* Windows Systems */
#if defined(_WIN32)
// Directory of executable?
if (a_Type == DST_EXE)
{
// Obtain current module
hMod = GetModuleHandle(NULL);
#if !defined(_UNICODE)
// Direct ASCII?
GetModuleFileName(hMod, a_Out, a_Size - 1);
#else
// Unicode
memset(Path, 0, sizeof(Path));
GetModuleFileName(hMod, Path, MAX_PATH - 1);
// Slowly Copy
for (i = 0; i < MAX_PATH && Path[i] && i < a_Size; i++)
a_Out[i] = Path[i];
a_Out[a_Size - 1] = 0;
#endif
// Obtain pointer to base name
BaseN = WL_BaseNameEx(a_Out);
// Got it? If so, end it there
if (BaseN)
*BaseN = 0;
// Return with size
return strlen(a_Out);
}
// Temporary Dir
else if (a_Type == DST_TEMP)
{
// Get temporary directory
memset(Path, 0, sizeof(Path));
GetTempPath(MAX_PATH - 1, Path);
// Slowly Copy
for (i = 0; i < MAX_PATH && Path[i] && i < a_Size; i++)
a_Out[i] = Path[i];
a_Out[a_Size - 1] = 0;
// Return size of output
return strlen(a_Out);
}
/* DOS */
#elif defined(__MSDOS__)
// Directory of executable?
if (a_Type == DST_EXE)
{
}
// Use C:\DOOMDATA and make sure it exists
strncat(a_Out, "c:/doomdata", a_Size);
I_mkdir(a_Out, 0);
/* UNIX Systems */
#elif defined(__unix__)
// Directory of executable?
if (a_Type == DST_EXE)
{
// Based on the first argument passed
switch (*myargv[0])
{
// Relative/Absolute Path
case '.':
case '/':
// Clone first argument
Clone = Z_StrDup(myargv[0], PU_STATIC, NULL);
// Obtain directory name
DirName = dirname(Clone);
// Copy that
strncpy(a_Out, DirName, a_Size);
// Free clone
Z_Free(Clone);
break;
// PATH
default:
// Obtain basename of executable
DirName = WL_BaseNameEx(myargv[0]);
// Retrieve executable path
PathEnv = I_GetEnvironment("PATH");
// Parse PATH bits
if (PathEnv)
do
{
// Find target (the next :)
End = strchr(PathEnv, ':');
// Get size, from End to DirArg or just strlen if this is the end
if (End)
j = End - PathEnv;
else
j = strlen(PathEnv);
// See if basename file exists here
memset(CheckPath, 0, sizeof(CheckPath));
strncpy(CheckPath, PathEnv, (j < PATH_MAX ? j : PATH_MAX));
strncat(CheckPath, "/", PATH_MAX);
strncat(CheckPath, DirName, PATH_MAX);
// See if it exists here (and it is not a directory)
IsDir = false;
if (I_CheckFileAccess(CheckPath, false, &IsDir))
if (!IsDir)
{
// Copy base directory path
strncpy(a_Out, PathEnv, (j < a_Size ? j : a_Size));
// Stop looping
break;
}
// Go to end
if (End)
PathEnv = End + 1;
else
PathEnv = NULL;
}
while (PathEnv);
break;
}
}
// Hardcoded paths are easy
else if (a_Type == DST_TEMP)
{
strncat(a_Out, "/tmp", a_Size);
return strlen(a_Out);
}
// Get XDG Config directory
else if ((Env = I_GetEnvironment((a_Type == DST_CONFIG ? "XDG_CONFIG_HOME" : "XDG_DATA_HOME"))))
{
// Use the following then
strncat(a_Out, Env, a_Size);
// Create directory just in case
I_mkdir(a_Out, 0);
// And concat remood
strncat(a_Out, "/", a_Size);
strncat(a_Out, "remood", a_Size);
// Create the directory
I_mkdir(a_Out, 0);
}
// Otherwise, get the home directory
else if ((Env = I_GetEnvironment("HOME")))
{
// Use the following then
strncat(a_Out, Env, a_Size);
// Add slash
strncat(a_Out, "/", a_Size);
// Add .config or .local and create the dir (just in case)
strncat(a_Out, ((a_Type == DST_CONFIG) ? ".config" : ".local"), a_Size);
I_mkdir(a_Out, 0);
// If this is data, add share also (and create the directory)
if (a_Type == DST_DATA)
{
strncat(a_Out, "/share", a_Size);
I_mkdir(a_Out, 0);
}
// Add remood directory and create it
strncat(a_Out, "/remood", a_Size);
I_mkdir(a_Out, 0);
}
/* Other */
#else
/* */
#endif
/* Directory is empty? */
// Then fallback to current directory
if (strlen(a_Out) == 0)
strncat(a_Out, ".", a_Size);
/* Return size of set buffer */
return strlen(a_Out);
}
/* I_GetDiskFreeSpace() -- Returns space being used */
uint64_t I_GetDiskFreeSpace(const char* const a_Path)
{
/* Check */
if (!a_Path)
return 0;
// TODO
return 2 << 30;
}
/* I_GetFreeMemory() -- Returns the amount of free memory available */
uint64_t I_GetFreeMemory(uint64_t* const a_TotalMem)
{
#define DEFAULTMEMCOUNT 32
/* MS-DOS w/ DJGPP */
#if defined(__MSDOS__)
__dpmi_memory_info MInfo;
__dpmi_version_ret DPMIVer;
__dpmi_free_mem_info FreeMemFo;
unsigned long PageSize;
// Get the current DPMI version
__dpmi_get_version(&DPMIVer);
// Only DPMI 1.0 supports __dpmi_get_memory_information()
// and not CWSDPMI and Win32's internal DPMI
if (DPMIVer.major >= 1)
if (__dpmi_get_memory_information(&MInfo) >= 0)
{
// Set total
if (a_TotalMem)
*a_TotalMem = MInfo.total_allocated_bytes_of_virtual_memory_host;
// Return free memory
return MInfo.total_available_bytes_of_virtual_memory_host;
}
// Either DPMI < 1.0 or __dpmi_get_memory_information() failed.
__dpmi_get_free_memory_information(&FreeMemFo);
// Will also need the page size (since that info is all in pages)
if (__dpmi_get_page_size(&PageSize) < 0)
PageSize = 0; // Just use 0 here (-1 == 16-bit host)
// Set total memory
if (a_TotalMem)
*a_TotalMem = FreeMemFo.linear_address_space_size_in_pages * PageSize;
// Return free memory
return FreeMemFo.free_linear_address_space_in_pages * PageSize;
/* Windows 64-bit */
#elif defined(_WIN64)
MEMORYSTATUSEX MemStatEx;
/* Obtain status info */
memset(&MemStatEx, 0, sizeof(MemStatEx));
MemStatEx.dwLength = sizeof(MemStatEx);
// Try getting it
if (GlobalMemoryStatusEx(&MemStatEx) == 0)
return DEFAULTMEMCOUNT;
// Return figures
if (a_TotalMem)
*a_TotalMem = MemStatEx.ullTotalPhys + (MemStatEx.ullTotalPageFile / 4);
// Return free memory
return MemStatEx.ullAvailPhys + (MemStatEx.ullAvailPageFile / 4);
/* Windows 32-bit */
#elif defined(_WIN32)
MEMORYSTATUS MemStat;
/* Obtain status info */
memset(&MemStat, 0, sizeof(MemStat));
MemStat.dwLength = sizeof(MemStat);
// Get Info
GlobalMemoryStatus(&MemStat);
// Return total
if (a_TotalMem)
*a_TotalMem = MemStat.dwTotalPhys + (MemStat.dwTotalPageFile / 4);
// Return free memory
return MemStat.dwAvailPhys + (MemStat.dwAvailPageFile / 4);
/* Linux */
#elif defined(__linux__)
// TODO: This could probably be improved, but it is only called in few places
#define BUFSIZE 256
FILE* ProcMem;
char Buf[BUFSIZE];
char* p;
uint64_t Total, Free, Temp;
// Clear
Total = Free = 0;
// Read /proc/meminfo and obtain memory info
ProcMem = fopen("/proc/meminfo", "rb");
// Check
if (!ProcMem)
{
if (a_TotalMem)
*a_TotalMem = DEFAULTMEMCOUNT << 20;
return DEFAULTMEMCOUNT << 20;
}
// Read in lines
while (!feof(ProcMem))
{
// Read line from file
fgets(Buf, BUFSIZE, ProcMem);
// See if it is something we like
if (strncasecmp(Buf, "MemTotal", 8) == 0)
Total = strtol(Buf + 8 + 2, &p, 10) << 10;
else if (strncasecmp(Buf, "MemFree", 7) == 0)
Free += strtol(Buf + 7 + 2, &p, 10) << 10;
// Buffers and Cache count at 75%
else if (strncasecmp(Buf, "Buffers", 7) == 0)
{
Temp = strtol(Buf + 7 + 2, &p, 10) << 10;
Free += Temp - (Temp >> 2);
}
else if (strncasecmp(Buf, "Cached", 6) == 0)
{
Temp = strtol(Buf + 6 + 2, &p, 10) << 10;
Free += Temp - (Temp >> 2);
}
// Swap counts at 50%
else if (strncasecmp(Buf, "SwapTotal", 9) == 0)
{
Temp = strtol(Buf + 9 + 2, &p, 10) << 10;
Total += Temp - (Temp >> 1);
}
else if (strncasecmp(Buf, "SwapFree", 8) == 0)
{
Temp = strtol(Buf + 8 + 2, &p, 10) << 10;
Free += Temp - (Temp >> 1);
}
}
// Close
fclose(ProcMem);
// Set total
if (a_TotalMem)
*a_TotalMem = Total;
// Return free
return Free;
#undef BUFSIZE
/* UNIX */
#elif defined(__unix__)
uint64_t Total, Free;
// Get total memory (RAM)
Total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
// Return total
if (a_TotalMem)
*a_TotalMem = Total;
// Just use free as total (ouch)
return Total;
/* Unknown */
#else
// Return 32MiB
return DEFAULTMEMCOUNT << 20;
/* */
#endif
#undef DEFAULTMEMCOUNT
}
/* I_CommonCommandLine() -- Common command line stuff */
void I_CommonCommandLine(int* const a_argc, char** *const a_argv, const char* const a_Long)
{
/* argc and argv */
if (a_argc && a_argv)
{
myargc = *a_argc;
myargv = *a_argv;
}
/* Windows command line (long format) */
else if (a_Long)
{
}
}
/* I_Quit() -- Quits the game */
void I_Quit(void)
{
static bool_t quiting;
/* prevent recursive I_Quit() */
if (quiting)
return;
quiting = 1;
//added:16-02-98: when recording a demo, should exit using 'q' key,
// but sometimes we forget and use 'F10'.. so save here too.
if (demorecording)
{
G_CheckDemoStatus();
// Force stop record, even if demo was checked already!
G_StopDemoRecord();
}
// Disconnect from network game
//D_XNetSendQuit();
//D_XNetDisconnect(false);
SN_Disconnect(false, "Quit ReMooD");
D_QuitNetGame();
// use this for 1.28 19990220 by Kin
I_ShutdownSystem();
exit(EXIT_SUCCESS);
}
/* I_GetEnvironment() -- Gets an environment variable */
char* I_GetEnvironment(const char* const a_VarName)
{
/* Windows CE lacks an environment */
#if defined(_WIN32_WCE)
return NULL;
/* Use standard I_GetEnvironment() */
#else
return getenv(a_VarName);
#endif
}
/* I_CheckFileAccess() -- Checks file read/write */
bool_t I_CheckFileAccess(const char* const a_Path, const bool_t a_Write, bool_t* const a_IsDir)
{
/* On WinCE, always return true */
#if defined(_WIN32_WCE)
#define BUFSIZE 512
TCHAR TCDir[BUFSIZE];
int i;
DWORD Attribs;
// Copy name slowly
for (i = 0; a_Path[i] && i < BUFSIZE - 2; i++)
TCDir[i] = a_Path[i];
TCDir[i] = 0;
TCDir[BUFSIZE - 1] = 0;
// Try getting the file attributes
Attribs = GetFileAttributes(TCDir);
// Does not exist?
if (Attribs == 0xFFFFFFFFU)
return false;
// Asked for write but is read only?
if (a_Write && (Attribs & FILE_ATTRIBUTE_READONLY))
return false;
// Set directory status
if (a_IsDir)
*a_IsDir = !!(Attribs & FILE_ATTRIBUTE_DIRECTORY);
// Otherwise success
return true;
/* Use access() */
#else
int Modes, Ret;
struct stat Stat;
/* Stat the file (see if it is a dir) */
if (a_IsDir)
{
// Init to false
*a_IsDir = false;
// Now see if it is a dir
if (stat(a_Path, &Stat) == 0)
if (Stat.st_mode & S_IFDIR)
*a_IsDir = true;
}
/* Use access */
Modes = R_OK | (a_Write ? W_OK : 0);
return !access(a_Path, Modes); // zero means OK
#endif
}
/* I_GetCurrentPID() -- Returns the current PID number */
uint16_t I_GetCurrentPID(void)
{
/* Win32 */
#if defined(_WIN32)
return (uint16_t)GetCurrentProcessId();
/* UNIX */
#elif defined(__unix__)
return getpid();
/* Unknown */
#else
return 0;
#endif
}
#if defined(__unix__)
static DIR* l_UNIXDir = NULL;
#elif defined(_WIN32)
static WIN32_FIND_DATA l_WinDir;
static HANDLE l_WinDirH;
static bool_t l_WinDirOK;
#else
#endif
/* I_OpenDir() -- Opens a directory */
bool_t I_OpenDir(const char* const a_Path)
{
#if defined(__unix__)
/* Check */
if (!a_Path)
return false;
/* Already open? */
if (l_UNIXDir)
return false;
/* Attempt Directory Open */
l_UNIXDir = opendir(a_Path);
/* If it worked, return */
if (l_UNIXDir)
return true;
/* Failure */
return false;
#elif defined(_WIN32)
size_t i;
TCHAR PathBuf[MAX_PATH];
/* Check */
if (!a_Path)
return false;
/* Already open? */
if (l_WinDirH)
return false;
/* Convert to TCHAR */
for (i = 0; a_Path[i] && i < MAX_PATH - 2; i++)
PathBuf[i] = a_Path[i];
PathBuf[i] = 0;
PathBuf[MAX_PATH - 1] = 0;
/* Find first file */
memset(&l_WinDir, 0, sizeof(l_WinDir));
l_WinDirH = FindFirstFile(PathBuf, &l_WinDir);
// Failed?
if (l_WinDirH == INVALID_HANDLE_VALUE)
{
l_WinDirH = NULL;
return false;
}
/* Mark file there */
l_WinDirOK = true;
/* Success! */
return true;
#else
/* Unknown */
return false;
#endif
}
/* I_ReadDir() -- Reads directory contents */
bool_t I_ReadDir(char* const a_Dest, const size_t a_BufSize)
{
#if defined(__unix__)
struct dirent* DEnt;
/* Check */
if (!a_Dest || !a_BufSize)
return false;
/* No Directory Open */
if (!l_UNIXDir)
return false;
/* Read single entry */
DEnt = readdir(l_UNIXDir);
// Failed?
if (!DEnt)
return false;
/* Copy name, return */
strncpy(a_Dest, DEnt->d_name, a_BufSize);
return true;
#elif defined(_WIN32)
size_t i;
/* Check */
if (!a_Dest || !a_BufSize)
return false;
/* No Directory Open */
if (!l_WinDirH)
return false;
/* No file? */
if (!l_WinDirOK)
return false;
/* Copy current file data */
for (i = 0; i < a_BufSize - 1 && i < MAX_PATH - 1; i++)
a_Dest[i] = l_WinDir.cFileName[i];
a_Dest[i] = 0;
/* Setup for next incursion */
l_WinDirOK = false;
// Find the next file
if (FindNextFile(l_WinDirH, &l_WinDir))
l_WinDirOK = true;
/* Successful */
return true;
#else
/* Unknown */
return false;
#endif
}
/* I_CloseDir() -- Closes directory */
void I_CloseDir(void)
{
#if defined(__unix__)
/* No Directory Open */
if (!l_UNIXDir)
return;
/* Close it */
closedir(l_UNIXDir);
/* Wipe it */
l_UNIXDir = NULL;
#elif defined(_WIN32)
/* Not Open? */
if (!l_WinDirH)
return;
/* Close */
FindClose(l_WinDirH);
/* Wipe it */
l_WinDirH = NULL;
#else
/* Unknown */
#endif
}
//
// I_Error
//
void I_Error(char* error, ...)
{
va_list argptr;
char txt[512];
if (devparm)
abort();
// Shutdown. Here might be other errors.
if (demorecording)
G_CheckDemoStatus();
// shutdown everything else which was registered
I_ShutdownSystem();
// Message first.
va_start(argptr, error);
fprintf(stderr, "Error: ");
vfprintf(stderr, error, argptr);
fprintf(stderr, "\n");
va_end(argptr);
fflush(stderr);
#ifdef _WIN32
va_start(argptr, error);
wvsprintf(txt, error, argptr);
va_end(argptr);
MessageBox(NULL, txt, "ReMooD Error", MB_OK | MB_ICONERROR);
#endif
exit(EXIT_FAILURE);
}
/* I_GetVideoBuffer() -- Gets the video buffer to draw to */
void* I_GetVideoBuffer(const I_VideoScreen_t a_Type, uint32_t* const a_Pitch)
{
static bool_t Checked;
static bool_t VBuf;
/* Check */
if (!Checked)
{
if (M_CheckParm("-virtualbuffer"))
VBuf = true;
Checked = true;
}
/* Which? */
switch (a_Type)
{
// Back buffer (to draw to)
case IVS_BACKBUFFER:
if (VBuf)
{
if (a_Pitch)
*a_Pitch = vid.rowbytes;
vid.buffer = screens[1];
return screens[1];
}
// If hardware double buffer is enabled, use the direct screen (if any)
else if (vid.HWDblBuf && vid.direct)
{
if (a_Pitch)
*a_Pitch = vid.rowbytes;
I_VideoLockBuffer(true);
return vid.direct;
}
// Otherwise, return the first screen
else
{
if (a_Pitch)
*a_Pitch = vid.width;
return vid.buffer;
}
break;
// Fore buffer, what is on the screen now
case IVS_FOREBUFFER:
// Direct access to the screen
if (vid.direct)
{
if (a_Pitch)
*a_Pitch = vid.rowbytes;
I_VideoLockBuffer(true);
return vid.direct;
}
// If no direct screen, return the raw buffer
else
{
if (a_Pitch)
*a_Pitch = vid.width;
return vid.buffer;
}
break;
// Done with buffer
case IVS_DONEWITHBUFFER:
I_VideoLockBuffer(false);
break;
default:
return NULL;
}
/* Oops! */
return NULL;
}
/* I_LocateFile() -- Locates file in list */
// Was formerly WL_LocateWAD(), now generalized! yipee
bool_t I_LocateFile(const char* const a_Name, const uint32_t a_Flags, const char* const* const a_List, const size_t a_Count, char* const a_OutPath, const size_t a_OutSize, const I_LFCheckFunc_t a_CheckFunc, void* const a_CheckData)
{
size_t i, j;
char CheckBuffer[PATH_MAX];
char BaseWAD[PATH_MAX];
const char* p, *bn;
bool_t Found, IsDir;
/* Name is required and a size must be given if a_OutPath is set */
// List and count must be available also
if (!a_Name || (a_OutPath && !a_OutSize) || !a_List || !a_Count)
return false;
/* Look in the search buffer for said WADs */
for (j = 0; j < 2; j++)
{
// Search the exact given name
if (!j)
p = a_Name;
// If this point is reached, then try the basename (always try to succeed)
else
{
// If not searching base name, die
if (!(a_Flags & ILFF_TRYBASE))
return false;
// Otherwise...
memset(BaseWAD, 0, sizeof(BaseWAD));
strncpy(BaseWAD, WL_BaseNameEx(a_Name), PATH_MAX);
p = BaseWAD;
bn = BaseWAD;
// Basename is NULL
if (!bn[0])
return false;
}
// Now search (explicit names)
for (i = 0; i < a_Count; i++)
{
// Clear the check buffer
memset(CheckBuffer, 0, sizeof(CheckBuffer));
// Append the current search path along with the name of the WAD
// Do not add trailing slash here since we already do it as needed in WX_Init()
strncpy(CheckBuffer, a_List[i], PATH_MAX);
strncat(CheckBuffer, p, PATH_MAX);
// Check whether we can read it
IsDir = false;
if (I_CheckFileAccess(CheckBuffer, false, &IsDir))
{
// If a directory, ignore
if (IsDir)
continue;
// Send to output buffer (if it was passed)
if (a_OutPath)
strncpy(a_OutPath, CheckBuffer, a_OutSize);
// Success? No function, then return
if (!a_CheckFunc)
return true;
// Otherwise call the checker
else
if (a_CheckFunc(a_OutPath, a_OutSize, a_CheckData))
return true;
}
}
// Search list again, using caseless names
if (j && (a_Flags & ILFF_DIRSEARCH))
for (i = 0; i < a_Count; i++)
if (*a_List[i] && I_OpenDir(a_List[i]))
{
// Clear found
Found = false;
// While reading a file
while (I_ReadDir(CheckBuffer, PATH_MAX))
// Compare name
if (strcasecmp(bn, CheckBuffer) == 0)
{
// Found it
Found = true;
// Copy name
if (a_OutPath)
{
strncpy(a_OutPath, a_List[i], a_OutSize);
strncat(a_OutPath, "/", a_OutSize);
strncat(a_OutPath, CheckBuffer, a_OutSize);
}
// use checker?
if (a_CheckFunc)
Found = a_CheckFunc(a_OutPath, a_OutSize, a_CheckData);
// Close
if (Found)
{
I_CloseDir();
return true;
}
}
// Nothing found here, so close directory search
I_CloseDir();
}
}
/* Not found */
return false;
}
/* I_FileOpen() -- Opens file */
I_File_t* I_FileOpen(const char* const a_Path, const uint32_t a_Modes)
{
#define BUFSIZE 8
char Buf[BUFSIZE];
FILE* File;
/* Check */
if (!a_Path || ((a_Modes & (IFM_READ | IFM_WRITE)) == 0))
return NULL;
/* Create C compatible modes */
memset(Buf, 0, sizeof(Buf));
if ((a_Modes & (IFM_READ | IFM_WRITE | IFM_TRUNCATE)) == (IFM_READ))
strncpy(Buf, "r", BUFSIZE - 1);
else if ((a_Modes & (IFM_READ | IFM_WRITE | IFM_TRUNCATE)) == (IFM_READ | IFM_WRITE))
strncpy(Buf, "r+", BUFSIZE - 1);
else if ((a_Modes & (IFM_READ | IFM_WRITE | IFM_TRUNCATE)) == (IFM_WRITE))
return NULL;
else if ((a_Modes & (IFM_READ | IFM_WRITE | IFM_TRUNCATE)) == (IFM_WRITE | IFM_TRUNCATE))
strncpy(Buf, "w", BUFSIZE - 1);
else if ((a_Modes & (IFM_READ | IFM_WRITE | IFM_TRUNCATE)) == (IFM_READ | IFM_WRITE | IFM_TRUNCATE))
strncpy(Buf, "w+", BUFSIZE - 1);
// Text or binary file?
if (a_Modes & IFM_TEXT)
strncat(Buf, "t", BUFSIZE - 1);
else
// Binary file for other files that make said difference
strncat(Buf, "b", BUFSIZE - 1);
/* Open it */
File = fopen(a_Path, Buf);
// Failed?
if (!File)
return NULL;
// Seek to start of file and flush
fseek(File, 0, SEEK_SET);
fflush(File);
/* Return file pointer */
return (I_File_t*)File;
#undef BUFSIZE
}
/* I_FileClose() -- Closes File */
void I_FileClose(I_File_t* const a_File)
{
/* Check */
if (!a_File)
return;
/* Close */
fclose((FILE*)a_File);
}
/* I_FileFlush() -- Flushes File */
void I_FileFlush(I_File_t* const a_File)
{
/* Check */
if (!a_File)
return;
/* Close */
fflush((FILE*)a_File);
}
/* I_FileSeek() -- Seeks to new position */
uint64_t I_FileSeek(I_File_t* const a_File, const uint64_t a_Offset, const bool_t a_End)
{
/* Check */
if (!a_File)
return 0;
/* Seek */
fseek((FILE*)a_File, a_Offset, (a_End ? SEEK_END : SEEK_SET));
/* Return told position */
return I_FileTell(a_File);
}
/* I_FileTell() -- Return position in file */
uint64_t I_FileTell(I_File_t* const a_File)
{
/* Check */
if (!a_File)
return 0;
/* Return position */
return ftell((FILE*)a_File);
}
/* I_FileRead() -- Reads part of a file */
uint64_t I_FileRead(I_File_t* const a_File, void* const a_Dest, const uint64_t a_Len)
{
/* Check */
if (!a_File || !a_Dest || !a_Len)
return 0;
/* Read */
return fread(a_Dest, 1, a_Len, (FILE*)a_File);
}
/* I_FileWrite() -- Writes part of a file */
uint64_t I_FileWrite(I_File_t* const a_File, const void* const a_Src, const uint64_t a_Len)
{
/* Check */
if (!a_File || !a_Src || !a_Len)
return 0;
/* Wwrite */
return fwrite(a_Src, 1, a_Len, (FILE*)a_File);
}
/* I_FileEOF() -- Returns EOF status */
bool_t I_FileEOF(I_File_t* const a_File)
{
/* Check */
if (!a_File)
return true;
/* Is EOF? */
if (feof((FILE*)a_File))
return true;
/* Is not */
return false;
}
/* I_FileDeletePath() -- Deletes a file */
void I_FileDeletePath(const char* const a_Path)
{
/* Check */
if (!a_Path)
return;
/* Call remove */
remove(a_Path);
}
/* I_MemMap() -- Maps memory */
bool_t I_MemMap(void* const a_CFile, void** const a_Out, const uint32_t a_Len, const uint32_t a_Off, const bool_t a_Write)
{
void* MapPtr;
long PageLen, FixedLen, FixedOff;
/* Check */
if (!a_Out || !a_Len)
return false;
/* POSIX Memory Mapping */
#if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
// Get length of pages and fix the amount of what we want to map
PageLen = sysconf(_SC_PAGE_SIZE);
FixedLen = a_Len + (PageLen - ((a_Len + PageLen) % (PageLen)));
FixedOff = a_Off + (PageLen - ((a_Off + PageLen) % (PageLen)));
// Attempt memory map
if (!(MapPtr = mmap(NULL, a_Len, PROT_READ | (a_Write ? PROT_WRITE : 0), MAP_PRIVATE, fileno(a_CFile), FixedOff)))
return false;
// Worked
*a_Out = MapPtr;
return true;
/* Win32 Memory Mapping */
#elif defined(_WIN32)
return false; // TODO FIXME
/* Memory mapping not available */
#else
return false;
#endif
}
/* I_UnMap() -- Unmaps memory at address */
void I_UnMap(void* const a_Ptr, const uint32_t a_Len)
{
long PageLen, FixedLen;
/* Check */
if (!a_Ptr || !a_Len)
return;
/* POSIX Memory Mapping */
#if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
// Get length of pages and fix the amount of what we want to map
PageLen = sysconf(_SC_PAGE_SIZE);
FixedLen = a_Len + (PageLen - ((a_Len + PageLen) % (PageLen)));
// Try an unmap
munmap(a_Ptr, a_Len);
/* Win32 Memory Mapping */
#elif defined(_WIN32)
// TODO FIXME
/* Memory mapping not available */
#else
#endif
}