File: slvideo.c

package info (click to toggle)
mc 1:4.6.0-4.6.1-pre3-3sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,224 kB
  • ctags: 11,992
  • sloc: ansic: 93,654; sh: 5,102; perl: 1,961; makefile: 1,107; yacc: 316; cs: 43; sed: 16
file content (2337 lines) | stat: -rw-r--r-- 52,166 bytes parent folder | download | duplicates (2)
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
/* -*- mode: C; mode: fold -*- */
/* Copyright (c) 1992, 1997, 2001, 2002, 2003 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

/* This file is best edited with a folding editor */

#include "slinclud.h"

#if !defined(__WIN32__) && !defined(__IBMC__)
# include <dos.h>
#endif

#include "slang.h"
#include "_slang.h"

int SLtt_Term_Cannot_Insert;
int SLtt_Term_Cannot_Scroll;
int SLtt_Ignore_Beep = 3;
int SLtt_Use_Ansi_Colors;
int SLtt_Has_Status_Line = 0;
int SLtt_Screen_Rows = 25;
int SLtt_Screen_Cols = 80;
int SLtt_Msdos_Cheap_Video = 0;

void (*_SLtt_color_changed_hook)(void);

/* This definition will need changing when SLsmg_Char_Type changes. */
#define SLSMG_CHAR_TO_USHORT(x) ((unsigned short)(x))

/*{{{ ------------- static local variables ---------- */

static int Attribute_Byte;
static int Scroll_r1 = 0, Scroll_r2 = 25;
static int Cursor_Row = 1, Cursor_Col = 1;
static int Current_Color;
static int IsColor = 1;
static int Blink_Killed = 1;	/* high intensity background enabled */

#define JMAX_COLORS	256
#define JNORMAL_COLOR	0
#define JNO_COLOR	-1

static unsigned char Color_Map [JMAX_COLORS] =
{
   0x7, 0x70, 0x70, 0x70, 0x70, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
   0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7
};

#define JMAX_COLOR_NAMES 16
static const char * const Color_Names [JMAX_COLOR_NAMES] =
{
   "black", "blue", "green", "cyan",
     "red", "magenta", "brown", "lightgray",
     "gray", "brightblue", "brightgreen", "brightcyan",
     "brightred", "brightmagenta", "yellow", "white"
};

static void fixup_colors (void);

/*}}}*/

static void goto_rc_abs (int r, int c)
{
   SLtt_goto_rc (r - Scroll_r1, c);
}

#if defined(__BORLANDC__) && defined(__MSDOS__)
# define IBMPC_ASM_VIDEO	1
#endif

#if defined(__WATCOMC__) && !defined(__NT__) && !defined(__os2__)
# define WATCOM_VIDEO		1
#endif

#if defined (__GO32__)
# define GO32_VIDEO		1
#endif

#if defined (__EMX__)		/* EMX video does both DOS & OS/2 */
# define EMX_VIDEO		1
#else
# if defined(__os2__)
#  define OS2_VIDEO		1
# endif
#endif

#if defined (__WIN32__)
# define WIN32_VIDEO		1
#endif

/* The functions in these folds contain somewhat video system specific code
 * that if merged together into single functions will become a confusing
 * mess.
 */

#ifdef IBMPC_ASM_VIDEO /*{{{*/

# include <conio.h>
# include <bios.h>
# include <mem.h>

/* buffer to hold a line of character/attribute pairs */
#define MAXCOLS 256
static unsigned char Line_Buffer [MAXCOLS*2];

#define MK_SPACE_CHAR()	(((Attribute_Byte) << 8) | 0x20)

static unsigned char *Video_Base;
# define MK_SCREEN_POINTER(row,col)	((unsigned short *)\
					 (Video_Base +\
					  2 * (SLtt_Screen_Cols * (row)\
					       + (col))))
static int Video_Status_Port;

# define MONO_STATUS	0x3BA
# define CGA_STATUS	0x3DA
# define CGA_SETMODE	0x3D8

# define SNOW_CHECK \
  if (SLtt_Msdos_Cheap_Video)\
    { while ((inp (CGA_STATUS) & 0x08)); while (!(inp (CGA_STATUS) & 0x08)); }

void SLtt_write_string (char *str)
{
   /* FIXME: Priority=medium
    * This should not go to stdout. */
   fputs (str, stdout);
}

/* row is with respect to the scrolling region. */
void SLtt_goto_rc (int r, int c)
{
   union REGS regs;

   r += Scroll_r1;

   if (r > SLtt_Screen_Rows - 1) r = SLtt_Screen_Rows - 1;
   if (c > SLtt_Screen_Cols - 1) c = SLtt_Screen_Cols - 1;

   Cursor_Row = r;
   Cursor_Col = c;

   regs.h.dh = r;
   regs.h.dl = c;
   regs.h.bh = 0;
   regs.h.ah = 2;
   int86 (0x10, &regs, &regs);
}

static void asm_video_getxy (void)
{
   asm  mov ah, 3
     asm  mov bh, 0
     asm  int 10h
     asm  xor ax, ax
     asm  mov al, dh
     asm  mov Cursor_Row, ax
     asm  xor ax, ax
     asm  mov al, dl
     asm  mov Cursor_Col, ax
}

void SLtt_begin_insert (void)
{
   unsigned short *p;
   int n;

   asm_video_getxy ();
   n = SLtt_Screen_Cols - Cursor_Col;
   p = MK_SCREEN_POINTER (Cursor_Row, SLtt_Screen_Cols - 1);

   SNOW_CHECK;
   asm  mov ax, ds
     asm  mov bx, di
     asm  mov dx, si

     asm  mov cx, n
     asm  les di, p
     asm  lds si, p
     asm  sub si, 2
     asm  std
     asm  rep movsw

     asm  mov ds, ax
     asm  mov di, bx
     asm  mov si, dx
}

void SLtt_end_insert (void)
{
}

void SLtt_delete_char (void)
{
   unsigned short *p;
   int n;

   asm_video_getxy ();
   n = SLtt_Screen_Cols - Cursor_Col - 1;
   p = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);

   SNOW_CHECK;
   asm  mov ax, ds
     asm  mov bx, si
     asm  mov dx, di

     asm  mov cx, n
     asm  les di, p
     asm  lds si, p
     asm  add si, 2
     asm  cld
     asm  rep movsw

     asm  mov ds, ax
     asm  mov si, bx
     asm  mov di, dx
}

void SLtt_erase_line (void)
{
   unsigned short w, *p;

   p = MK_SCREEN_POINTER (Cursor_Row, 0);
   Attribute_Byte = 0x07;

   w = MK_SPACE_CHAR ();

   SNOW_CHECK;
   asm  mov dx, di
     asm  mov ax, w
     asm  mov cx, SLtt_Screen_Cols
     asm  les di, p
     asm  cld
     asm  rep stosw
     asm  mov di, dx

     Current_Color = JNO_COLOR;		/* since we messed with attribute byte */
}

void SLtt_delete_nlines (int nlines)
{
   SLtt_normal_video ();

   /* This has the effect of pulling all lines below it up */
   asm  mov ax, nlines
     asm  mov ah, 6		/* int 6h */
     asm  xor cx, cx
     asm  mov ch, byte ptr Scroll_r1
     asm  mov dx, SLtt_Screen_Cols
     asm  dec dx
     asm  mov dh, byte ptr Scroll_r2
     asm  mov bh, byte ptr Attribute_Byte
     asm  int 10h
}

void SLtt_reverse_index (int nlines)
{
   SLtt_normal_video ();
   asm  xor cx, cx
     asm  mov ch, byte ptr Scroll_r1
     asm  mov dx, SLtt_Screen_Cols
     asm  dec dx
     asm  mov dh, byte ptr Scroll_r2
     asm  mov bh, byte ptr Attribute_Byte
     asm  mov ah, 7
     asm  mov al, byte ptr nlines
     asm  int 10h
}

static void asm_video_invert_region (int top_row, int bot_row)
{
   register unsigned short ch, sh;
   register unsigned short *pmin = MK_SCREEN_POINTER (top_row, 0);
   register unsigned short *pmax = MK_SCREEN_POINTER (bot_row, 0);

   while (pmin < pmax)
     {
	sh = *pmin;
	ch = sh;
	ch = ch ^ 0xFF00;
	*pmin = (ch & 0xFF00) | (sh & 0x00FF);
	pmin++;
     }
}

void SLtt_del_eol (void)
{
   unsigned short *p;
   unsigned short w;
   int n;

   n = SLtt_Screen_Cols - Cursor_Col;
   p = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);
   if (Current_Color != JNO_COLOR) SLtt_normal_video ();
   w = MK_SPACE_CHAR ();

   SNOW_CHECK;
   asm  mov dx, di
     asm  les di, p
     asm  mov ax, w
     asm  mov cx, n
     asm  cld
     asm  rep stosw

     asm  mov di, dx
}

static unsigned short *asm_video_write (register unsigned char *pp,
					register unsigned char *p,
					register unsigned short *pos)
{
   int n = (int) (p - pp);	/* num of characters of PP to write */

   asm  push si
     asm  push ds
     asm  push di

   /* set up register for BOTH fast and slow */
     asm  mov bx, SLtt_Msdos_Cheap_Video

   /* These are the registers needed for both fast AND slow */
     asm  mov ah, byte ptr Attribute_Byte
     asm  mov cx, n
     asm  lds si, dword ptr pp
     asm  les di, dword ptr pos
     asm  cld

     asm  cmp bx, 0		       /* cheap video test */
     asm  je L_fast
     asm  mov bx, ax
     asm  mov dx, CGA_STATUS
     asm  jg L_slow_blank

   /* slow video */
     asm  cli

   /* wait for retrace */
     L_slow:
   asm  in al, dx
     asm  test al, 1
     asm  jnz L_slow

     L_slow1:
   asm  in al, dx
     asm  test al, 1
     asm  jz L_slow1

   /* move a character out */
     asm  mov ah, bh
     asm  lodsb
     asm  stosw
     asm  loop L_slow

     asm  sti
     asm  jmp done

/* -------------- slow video, vertical retace and pump --------------*/
     L_slow_blank:
   L_slow_blank_loop:
   asm  in al, dx
     asm  test al, 8
     asm  jnz L_slow_blank_loop

     L_slow_blank1:
   asm  in al, dx
     asm  test al, 8
     asm  jz L_slow_blank1
   /* write line */
     asm  mov ah, bh
     L_slow_blank2:
   asm  lodsb
     asm  stosw
     asm  loop L_slow_blank2

     asm jmp done
/*-------------- Fast video --------------*/

     L_fast:
   asm  lodsb
     asm  stosw
     asm  loop L_fast
     done:
   asm  pop di
     asm  pop ds
     asm  pop si
     return (pos + n);
}

static void write_attributes (SLsmg_Char_Type *src, unsigned int count)
{
   register unsigned char *p;
   register unsigned short pair;
   unsigned char ch, color;
   register unsigned short *pos;

   p = Line_Buffer;
   pos = MK_SCREEN_POINTER (Cursor_Row, 0);

   while (count--)
     {
	pair = SLSMG_CHAR_TO_USHORT(*src);	/* character/color pair */
	src++;
	ch = pair & 0xff;	/* character value */
	color = pair >> 8;	/* color value */
	if (color != Current_Color)	/* need a new color */
	  {
	     if (p != Line_Buffer)
	       {
		  pos = asm_video_write (Line_Buffer, p, pos);
		  p = Line_Buffer;
	       }
	     SLtt_reverse_video (color);	/* change color */
	  }
	*(p++) = ch;
     }
   pos = asm_video_write (Line_Buffer, p, pos);
}

void SLtt_cls (void)
{
   SLtt_normal_video ();

   asm  mov dx, SLtt_Screen_Cols
     asm  dec dx
     asm  mov ax, SLtt_Screen_Rows
     asm  dec ax
     asm  mov dh, al
     asm  xor cx, cx
     asm  xor ax, ax
     asm  mov ah, 7
     asm  mov bh, byte ptr Attribute_Byte
     asm  int 10h
}

void SLtt_putchar (char ch)
{
   unsigned short p, *pp;

   if (Current_Color) SLtt_normal_video ();
   asm_video_getxy ();		/* get current position */
   switch (ch)
     {
      case 7:			/* ^G - break */
	SLtt_beep (); break;
      case 8:			/* ^H - backspace */
	goto_rc_abs (Cursor_Row, Cursor_Col - 1); break;
      case 13:			/* ^M - carriage return */
	goto_rc_abs (Cursor_Row, 0); break;
      default:
	/* write character to screen */
	pp = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);
	p = (Attribute_Byte << 8) | (unsigned char) ch;
	SNOW_CHECK;
	*pp = p;
	goto_rc_abs (Cursor_Row, Cursor_Col + 1);
     }
}

void SLtt_get_screen_size (void)
{
   int w, h;

   h = 0;

   /* Get BIOS's screenwidth, this works on ALL displays. */
   w = *((int *)MK_FP(0x40, 0x4a));

   /* Use Ralf Brown test for EGA or greater */
   asm mov ah, 12h
   asm mov bl, 10h
   asm mov bh, 0xFF  /* EGA or greater will change this */
   asm int 10h
   asm cmp bh, 0xFF
   asm je L1
     /* if EGA or compatible: Get BIOS's number of rows. */
     h = *(char *)MK_FP(0x40, 0x84) + 1;
   /* scan_lines = *(int *) 0x485; */

   L1:
      if (h <= 0) h = 25;

   SLtt_Screen_Rows = h;
   SLtt_Screen_Cols = w;
}

void SLtt_get_terminfo (void)
{
   SLtt_get_screen_size ();
}

/*----------------------------------------------------------------------*\
 * Function:	int SLtt_init_video (void);
\*----------------------------------------------------------------------*/
int SLtt_init_video (void)
{
   unsigned char *p;

#ifdef HAS_SAVE_SCREEN
   save_screen ();
#endif

   Cursor_Row = Cursor_Col = 0;
   p = (unsigned char far *) 0x00400049L;
   if (*p == 7)
     {
	Video_Status_Port = MONO_STATUS;
	Video_Base = (unsigned char *) MK_FP (0xb000,0000);
	IsColor = 0;
     }
   else
     {
	Video_Status_Port = CGA_STATUS;
	Video_Base = (unsigned char *) MK_FP (0xb800,0000);
	IsColor = 1;
     }

   /* test for video adapter type.  Of primary interest is whether there is
    * snow or not.  Assume snow if the card is color and not EGA or greater.
    */

   /* Use Ralf Brown test for EGA or greater */
   asm  mov ah, 0x12
     asm  mov bl, 0x10
     asm  mov bh, 0xFF
     asm  int 10h
     asm  cmp bh, 0xFF
     asm  je L1

   /* (V)EGA */
     asm  xor bx, bx
     asm  mov SLtt_Msdos_Cheap_Video, bx
     asm  mov ax, Attribute_Byte
     asm  cmp ax, bx
     asm  jne L2
     asm  mov ax, 0x17
     asm  mov Attribute_Byte, ax
     asm  jmp L2

     L1:
   /* Not (V)EGA */
   asm  mov ah, 0x0F
     asm  int 10h
     asm  cmp al, 7
     asm  je L3
     asm  mov ax, 1
     asm  mov SLtt_Msdos_Cheap_Video, ax
     L3:
   asm  mov ax, Attribute_Byte
     asm  cmp ax, 0
     asm  jne L2
     asm  mov ax, 0x07
     asm  mov Attribute_Byte, ax
     L2:
   /* toggle the blink bit so we can use hi intensity background */
   if (IsColor && !SLtt_Msdos_Cheap_Video)
     {
	asm  mov ax, 0x1003
	  asm  mov bx, 0
	  asm  int 0x10
	  Blink_Killed = 1;
     }

   SLtt_Use_Ansi_Colors = IsColor;
   SLtt_get_screen_size ();
   SLtt_reset_scroll_region ();
   fixup_colors ();
   return 0;
}

void SLtt_beep (void)
{
   int audible;			/* audible bell */
   int special = 0;		/* first row to invert */
   int visual = 0;		/* final row to invert */

   if (!SLtt_Ignore_Beep) return;

   audible = (SLtt_Ignore_Beep & 1);
   if ( (SLtt_Ignore_Beep & 4) )
     {
	special = SLtt_Screen_Rows - 1;
	visual = special--;	/* only invert bottom status line */
     }
   else if ( (SLtt_Ignore_Beep & 2) )
     {
	visual = SLtt_Screen_Rows;
     }

   if (visual) asm_video_invert_region (special, visual);
   if (audible) sound (1500); delay (100); if (audible) nosound ();
   if (visual) asm_video_invert_region (special, visual);
}

#endif				       /* IBMPC_ASM_VIDEO */

/*}}}*/

#ifdef GO32_VIDEO /*{{{*/

# include <pc.h>
# define HAS_SAVE_SCREEN	1

# ifdef HAS_SAVE_SCREEN
static void *Saved_Screen_Buffer;
static int Saved_Cursor_Row;

static void save_screen (void)
{
   int row, col;

   SLfree ((char *) Saved_Screen_Buffer);
   Saved_Screen_Buffer = NULL;

   Saved_Screen_Buffer = (short *) SLmalloc (sizeof (short) *
				   ScreenCols () * ScreenRows ());

   if (Saved_Screen_Buffer == NULL)
     return;

   ScreenRetrieve (Saved_Screen_Buffer);
   ScreenGetCursor (&row, &col);
   Saved_Cursor_Row = row;
}

static void restore_screen (void)
{
   if (Saved_Screen_Buffer == NULL) return;
   ScreenUpdate (Saved_Screen_Buffer);
   goto_rc_abs (Saved_Cursor_Row, 0);
}
#endif				       /* HAS_SAVE_SCREEN */

void SLtt_write_string (char *str)
{
   while (Cursor_Col < SLtt_Screen_Cols)
     {
	char ch = *str++;

	if (ch == 0)
	  break;

	ScreenPutChar (ch, Attribute_Byte, Cursor_Col, Cursor_Row);
	Cursor_Col++;
     }
   goto_rc_abs (Cursor_Row, Cursor_Col);
}

void SLtt_goto_rc (int row, int col)
{
   row += Scroll_r1;
   if (row > SLtt_Screen_Rows) row = SLtt_Screen_Rows;
   if (col > SLtt_Screen_Cols) col = SLtt_Screen_Cols;

   ScreenSetCursor (row, col);

   Cursor_Row = row;
   Cursor_Col = col;
}

static void go32_video_getxy (void)
{
   ScreenGetCursor (&Cursor_Row, &Cursor_Col);
}

static void go32_video_deleol (int x)
{
   while (x < SLtt_Screen_Cols)
     ScreenPutChar (32, Attribute_Byte, x++, Cursor_Row);
}

void SLtt_begin_insert (void)
{
}

void SLtt_end_insert (void)
{
}

void SLtt_delete_char (void)
{
}

void SLtt_erase_line (void)
{
   Attribute_Byte = 0x07;
   go32_video_deleol (0);
   Current_Color = JNO_COLOR;		/* since we messed with attribute byte */
}

void SLtt_delete_nlines (int nlines)
{
   union REGS r;

   SLtt_normal_video ();

   r.x.ax = nlines;
   r.x.cx = 0;
   r.h.ah = 6;
   r.h.ch = Scroll_r1;
   r.h.dl = SLtt_Screen_Cols - 1;
   r.h.dh = Scroll_r2;
   r.h.bh = Attribute_Byte;
   int86 (0x10, &r, &r);
}

void SLtt_reverse_index (int nlines)
{
   union REGS r;

   SLtt_normal_video ();

   r.h.al = nlines;
   r.x.cx = 0;
   r.h.ah = 7;
   r.h.ch = Scroll_r1;
   r.h.dl = SLtt_Screen_Cols - 1;
   r.h.dh = Scroll_r2;
   r.h.bh = Attribute_Byte;
   int86 (0x10, &r, &r);
}

static void go32_video_invert_region (int top_row, int bot_row)
{
   unsigned char buf [2 * 180 * 80];         /* 180 cols x 80 rows */
   unsigned char *b, *bmax;

   b    = buf + 1 + 2 * SLtt_Screen_Cols * top_row;
   bmax = buf + 1 + 2 * SLtt_Screen_Cols * bot_row;

   ScreenRetrieve (buf);
   while (b < bmax)
     {
	*b ^= 0xFF;
	b += 2;
     }
   ScreenUpdate (buf);
}

void SLtt_beep (void)
{
   int audible;			/* audible bell */
   int special = 0;		/* first row to invert */
   int visual = 0;		/* final row to invert */

   if (!SLtt_Ignore_Beep) return;

   audible = (SLtt_Ignore_Beep & 1);
   if ( (SLtt_Ignore_Beep & 4) )
     {
	special = SLtt_Screen_Rows - 1;
	visual = special--;	/* only invert bottom status line */
     }
   else if ( (SLtt_Ignore_Beep & 2) )
     {
	visual = SLtt_Screen_Rows;
     }

   if (visual) go32_video_invert_region (special, visual);
   if (audible) sound (1500); delay (100); if (audible) nosound ();
   if (visual) go32_video_invert_region (special, visual);
}

void SLtt_del_eol (void)
{
   if (Current_Color != JNO_COLOR) SLtt_normal_video ();
   go32_video_deleol (Cursor_Col);
}

static void
write_attributes (SLsmg_Char_Type *src, unsigned int count)
{
   register unsigned short pair;
   unsigned int n;

   /* write into a character/attribute pair */
   n = Cursor_Col;
   while (count)
     {
	pair = SLSMG_CHAR_TO_USHORT(*src);/* character/color pair */
	src++;
	SLtt_reverse_video (pair >> 8);	/* color change */
	ScreenPutChar ((int)pair & 0xFF, Attribute_Byte, n, Cursor_Row);
	n++;
	count--;
     }
}

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_cls (void);
\*----------------------------------------------------------------------*/
void SLtt_cls (void)
{
   SLtt_normal_video ();
   SLtt_reset_scroll_region ();
   SLtt_goto_rc (0, 0);
   SLtt_delete_nlines (SLtt_Screen_Rows);
}

void SLtt_putchar (char ch)
{
   if (Current_Color) SLtt_normal_video ();

   go32_video_getxy ();		/* get current position */

   switch (ch)
     {
      case 7:			/* ^G - break */
	SLtt_beep (); break;
      case 8:			/* ^H - backspace */
	goto_rc_abs (Cursor_Row, Cursor_Col - 1); break;
      case 13:			/* ^M - carriage return */
	goto_rc_abs (Cursor_Row, 0); break;
      default:			/* write character to screen */
	ScreenPutChar ((int) ch, Attribute_Byte, Cursor_Col, Cursor_Row);
	goto_rc_abs (Cursor_Row, Cursor_Col + 1);
     }
}

void SLtt_get_screen_size (void)
{
   SLtt_Screen_Rows = ScreenRows ();
   SLtt_Screen_Cols = ScreenCols ();
}

void SLtt_get_terminfo (void)
{
   SLtt_get_screen_size ();
}

int SLtt_init_video (void)
{
#ifdef HAS_SAVE_SCREEN
   save_screen ();
#endif

   if (!Attribute_Byte) Attribute_Byte = 0x17;

   IsColor = 1;			/* is it really? */

   if (IsColor)
     {
	union REGS r;
	r.x.ax = 0x1003; r.x.bx = 0;
	int86 (0x10, &r, &r);
	Blink_Killed = 1;
     }

   Cursor_Row = Cursor_Col = 0;

   SLtt_Term_Cannot_Insert = 1;
   SLtt_reset_scroll_region ();
   SLtt_Use_Ansi_Colors = IsColor;
   fixup_colors ();
   return 0;
}

#endif				       /* GO32_VIDEO */

/*}}}*/

#ifdef EMX_VIDEO /*{{{*/

# define INCL_VIO
# define INCL_DOSPROCESS
# include <os2.h>
# include <os2emx.h>
# include <sys/video.h>

static VIOMODEINFO vioModeInfo;
/* buffer to hold a line of character/attribute pairs */
#define MAXCOLS 256
static unsigned char Line_Buffer [MAXCOLS*2];

/* this is how to make a space character */
#define MK_SPACE_CHAR()	(((Attribute_Byte) << 8) | 0x20)

void SLtt_write_string (char *str)
{
   /* FIXME: Priority=medium
    * This should not go to stdout. */
   fputs (str, stdout);
}

void SLtt_goto_rc (int row, int col)
{
   row += Scroll_r1;
   if (row > SLtt_Screen_Rows) row = SLtt_Screen_Rows;
   if (col > SLtt_Screen_Cols) col = SLtt_Screen_Cols;
   v_gotoxy (col, row);
   Cursor_Row = row;
   Cursor_Col = col;
}

static void emx_video_getxy (void)
{
   v_getxy (&Cursor_Col, &Cursor_Row);
}

static void emx_video_deleol (int x)
{
   unsigned char *p, *pmax;
   int count = SLtt_Screen_Cols - x;
   int w = MK_SPACE_CHAR ();

   p = Line_Buffer;
   pmax = p + 2 * count;

   while (p < pmax)
     {
	*p++ = (unsigned char) w;
	*p++ = (unsigned char) (w >> 8);
     }

   v_putline (Line_Buffer, x, Cursor_Row, count);
}

void SLtt_begin_insert (void)
{
   int n;

   emx_video_getxy ();

   n = SLtt_Screen_Cols - Cursor_Col;
   v_getline (Line_Buffer, Cursor_Col, Cursor_Row, n);
   v_putline (Line_Buffer, Cursor_Col+1, Cursor_Row, n - 1);
}

void SLtt_end_insert (void)
{
}

void SLtt_delete_char (void)
{
   int n;

   emx_video_getxy ();

   n = SLtt_Screen_Cols - Cursor_Col - 1;
   v_getline (Line_Buffer, Cursor_Col+1, Cursor_Row, n);
   v_putline (Line_Buffer, Cursor_Col, Cursor_Row, n);
}

void SLtt_erase_line (void)
{
   Attribute_Byte = 0x07;
   emx_video_deleol (0);
   Current_Color = JNO_COLOR;		/* since we messed with attribute byte */
}

void SLtt_delete_nlines (int nlines)
{
   SLtt_normal_video ();
   v_attrib (Attribute_Byte);
   v_scroll (0, Scroll_r1, SLtt_Screen_Cols-1, Scroll_r2, nlines, V_SCROLL_UP);
}

void SLtt_reverse_index (int nlines)
{
   SLtt_normal_video ();

   v_attrib (Attribute_Byte);
   v_scroll (0, Scroll_r1, SLtt_Screen_Cols-1, Scroll_r2, nlines,
	     V_SCROLL_DOWN);
}

static void emx_video_invert_region (int top_row, int bot_row)
{
   int row, col;

   for (row = top_row; row < bot_row; row++)
     {
	v_getline (Line_Buffer, 0, row, SLtt_Screen_Cols);
	for (col = 1; col < SLtt_Screen_Cols * 2; col += 2)
	  Line_Buffer [col] ^= 0xff;
	v_putline (Line_Buffer, 0, row, SLtt_Screen_Cols);
     }
}

void SLtt_beep (void)
{
   int audible;			/* audible bell */
   int special = 0;		/* first row to invert */
   int visual = 0;		/* final row to invert */

   if (!SLtt_Ignore_Beep) return;

   audible = (SLtt_Ignore_Beep & 1);
   if ( (SLtt_Ignore_Beep & 4) )
     {
	special = SLtt_Screen_Rows - 1;
	visual = special--;	/* only invert bottom status line */
     }
   else if ( (SLtt_Ignore_Beep & 2) )
     {
	visual = SLtt_Screen_Rows;
     }

   if (visual) emx_video_invert_region (special, visual);
   if (audible) /*sound (1500)*/; _sleep2 (100); if (audible) /* nosound () */;
   if (visual) emx_video_invert_region (special, visual);
}

void SLtt_del_eol (void)
{
   if (Current_Color != JNO_COLOR) SLtt_normal_video ();
   emx_video_deleol (Cursor_Col);
}

static void
write_attributes (SLsmg_Char_Type *src, unsigned int count)
{
   register unsigned char *p = Line_Buffer;
   register unsigned short pair;
   int n = count;

   /* write into a character/attribute pair */
   while (n-- > 0)
     {
	pair = SLSMG_CHAR_TO_USHORT(*src);/* character/color pair */
	src++;
	SLtt_reverse_video (pair >> 8);	/* color change */
	*(p++) = pair & 0xff;		/* character byte */
	*(p++) = Attribute_Byte;	/* attribute byte */
     }
   v_putline (Line_Buffer, Cursor_Col, Cursor_Row, count);
}

void SLtt_cls (void)
{
   SLtt_normal_video ();
   SLtt_reset_scroll_region ();
   SLtt_goto_rc (0, 0);
   SLtt_delete_nlines (SLtt_Screen_Rows);
}

void SLtt_putchar (char ch)
{
   if (Current_Color) SLtt_normal_video ();

   emx_video_getxy ();		/* get current position */
   switch (ch)
     {
      case 7:			/* ^G - break */
	SLtt_beep (); break;
      case 8:			/* ^H - backspace */
	goto_rc_abs (Cursor_Row, Cursor_Col - 1); break;
      case 13:			/* ^M - carriage return */
	goto_rc_abs (Cursor_Row, 0); break;
      default:			/* write character to screen */
	v_putn (ch, 1);
	goto_rc_abs (Cursor_Row, Cursor_Col + 1);
     }
}

void SLtt_get_terminfo (void)
{
   SLtt_get_screen_size ();
}

void SLtt_get_screen_size (void)
{
   vioModeInfo.cb = sizeof(vioModeInfo);
   VioGetMode (&vioModeInfo, 0);
   SLtt_Screen_Cols = vioModeInfo.col;
   SLtt_Screen_Rows = vioModeInfo.row;
}

int SLtt_init_video (void)
{
   int OldCol, OldRow;
   PTIB   ptib;
   PPIB   ppib;
   USHORT args[3] = { 6, 2, 1 };

#ifdef HAS_SAVE_SCREEN
   save_screen ();
#endif

   Cursor_Row = Cursor_Col = 0;

   v_init ();
   if ( v_hardware () != V_MONOCHROME ) IsColor = 1; else IsColor = 0;

   v_getxy(&OldCol,&OldRow);
   v_gotoxy (0, 0);
   if (IsColor)
     {
	if (_osmode == OS2_MODE)
	  {
# if 0
	     /* Enable high-intensity background colors */
	     VIOINTENSITY RequestBlock;
	     RequestBlock.cb = sizeof (RequestBlock);
	     RequestBlock.type = 2; RequestBlock.fs = 1;
	     VioSetState (&RequestBlock, 0);	/* nop if !fullscreen */
# endif
	  }
     }

   DosGetInfoBlocks (&ptib, &ppib);
   if ((ppib->pib_ultype) == 2)        /* VIO */
     Blink_Killed = 1;
   else
     {                              /* Fullscreen */
        if (VioSetState (args, 0) == 0)
	  Blink_Killed = 1;
        else
	  Blink_Killed = 0;
     }

   if (!Attribute_Byte)
     {
	/* find the attribute currently under the cursor */
	v_getline (Line_Buffer, OldCol, OldRow, 1);
	Attribute_Byte = Line_Buffer[1];
	SLtt_set_color (JNORMAL_COLOR, NULL,
			Color_Names[(Attribute_Byte) & 0xf],
			Color_Names[(Attribute_Byte) >> 4]);
     }

   v_attrib (Attribute_Byte);

   fixup_colors ();

   SLtt_get_screen_size ();
   SLtt_Use_Ansi_Colors = IsColor;
   SLtt_reset_scroll_region ();
   return 0;
}

#endif				       /* EMX_VIDEO */

/*}}}*/

#ifdef WIN32_VIDEO /*{{{*/

#include <windows.h>

static HANDLE hStdout = INVALID_HANDLE_VALUE;

#define MAXCOLS 256
static CHAR_INFO Line_Buffer [MAXCOLS];

void SLtt_write_string (char *str)
{
   DWORD bytes;
   int n, c;
   if (str == NULL) return;

   n = (int) strlen (str);
   c = n + Cursor_Col;
   if (c >= SLtt_Screen_Cols)
     n = SLtt_Screen_Cols - Cursor_Col;
   if (n < 0) n = 0;

   (void) WriteConsole (hStdout, str, (unsigned int) n, &bytes, NULL);

   goto_rc_abs (Cursor_Row, Cursor_Col + n);
}

void SLtt_goto_rc (int row, int col)
{
   COORD newPosition;

   row += Scroll_r1;
   if (row > SLtt_Screen_Rows) row = SLtt_Screen_Rows;
   if (col > SLtt_Screen_Cols) col = SLtt_Screen_Cols;
   newPosition.X = col;
   newPosition.Y = row;

   (void) SetConsoleCursorPosition(hStdout, newPosition);

   Cursor_Row = row;
   Cursor_Col = col;
}

static void win32_video_getxy (void)
{
   CONSOLE_SCREEN_BUFFER_INFO screenInfo;

   if (TRUE == GetConsoleScreenBufferInfo(hStdout, &screenInfo))
     {
	Cursor_Row = screenInfo.dwCursorPosition.Y;
	Cursor_Col = screenInfo.dwCursorPosition.X;
     }
}

static void win32_video_hscroll (int n)
{
   SMALL_RECT rc;
   COORD c;
   CHAR_INFO ci;
   WORD w = 227;
   DWORD d;

   win32_video_getxy ();

   rc.Left = Cursor_Col;
   rc.Right = SLtt_Screen_Cols;
   rc.Top = rc.Bottom = Cursor_Row;

   c.Y = Cursor_Row;
#if 1
   c.X = SLtt_Screen_Cols - 1;
   ReadConsoleOutputAttribute(hStdout, &w, 1, c, &d);
#else
   /* New region gets the current color */
   w = Attribute_Byte;
#endif
   c.X = Cursor_Col + n;

   ci.Char.AsciiChar = ' ';
   ci.Attributes = w;

   ScrollConsoleScreenBuffer(hStdout, &rc, &rc, c, &ci);
}

static void win32_video_deleol (int x)
{
   DWORD d;
   COORD c;

   c.X = x;
   c.Y = Cursor_Row;

   x = SLtt_Screen_Cols - x;
   FillConsoleOutputCharacter(hStdout, ' ', x, c, &d);
   FillConsoleOutputAttribute(hStdout, (char)Attribute_Byte, x, c, &d);
}

static void win32_video_vscroll (int n)
{
   SMALL_RECT rc, clip_rc;
   COORD c;
   CHAR_INFO ci;

   SLtt_normal_video();

   /* ScrollConsoleScreenBuffer appears to have a bug when
    * Scroll_r1 == Scroll_r2.  Sigh.
    */
   if (Scroll_r2 == Scroll_r1)
     {
	SLtt_goto_rc (0, 0);
	win32_video_deleol (0);
	return;
     }

   rc.Left = clip_rc.Left = 0;
   rc.Right = clip_rc.Right = SLtt_Screen_Cols - 1;
   rc.Top = clip_rc.Top = Scroll_r1;
   rc.Bottom = clip_rc.Bottom = Scroll_r2;

   c.X = 0;
   c.Y = Scroll_r1 + n;

   ci.Char.AsciiChar = ' ';
   ci.Attributes = Attribute_Byte;

   ScrollConsoleScreenBuffer(hStdout, &rc, &clip_rc, c, &ci);
}

void SLtt_begin_insert (void)
{
   win32_video_hscroll (1);
}

void SLtt_end_insert (void)
{
}

void SLtt_delete_char (void)
{
   win32_video_hscroll (-1);
}

void SLtt_erase_line (void)
{
   Attribute_Byte = 0x7;
   win32_video_deleol (0);
   Current_Color = JNO_COLOR;
}

void SLtt_delete_nlines (int nlines)
{
   win32_video_vscroll (-nlines);
}

void SLtt_reverse_index (int nlines)
{
   win32_video_vscroll (nlines);
}

static void win32_invert_region (int top_row, int bot_row)
{
   (void) top_row; (void) bot_row;
}

void SLtt_beep (void)
{
   int audible;			/* audible bell */
   int special = 0;		/* first row to invert */
   int visual = 0;		/* final row to invert */

   if (!SLtt_Ignore_Beep) return;

   audible = (SLtt_Ignore_Beep & 1);

   if ( (SLtt_Ignore_Beep & 4) )
     {
	special = SLtt_Screen_Rows - 1;
	visual = special--;	/* only invert bottom status line */
     }
   else if ( (SLtt_Ignore_Beep & 2) )
     {
	visual = SLtt_Screen_Rows;
     }

   if (visual) win32_invert_region (special, visual);
   if (audible) Beep (1500, 100); else Sleep (100);
   if (visual) win32_invert_region (special, visual);
}

void SLtt_del_eol (void)
{
   if (Current_Color != JNO_COLOR)
     SLtt_normal_video ();
   win32_video_deleol (Cursor_Col);
}

static void
write_attributes (SLsmg_Char_Type *src, unsigned int count)
{
   unsigned short pair;
   COORD coord, c;
   CHAR_INFO *p;
   unsigned int n;
   SMALL_RECT rc;

   /* write into a character/attribute pair */
   n = count;
   p = Line_Buffer;
   while (n)
     {
	n--;
	pair = SLSMG_CHAR_TO_USHORT(*src);/* character/color pair */
	src++;
	SLtt_reverse_video (pair >> 8);	/* color change */
	p->Char.AsciiChar = pair & 0xff;
	p->Attributes = Attribute_Byte;
	p++;
     }

   c.X = count;
   c.Y = 1;
   coord.X = coord.Y = 0;
   rc.Left = Cursor_Col;
   rc.Right = Cursor_Col + count - 1;
   rc.Top = rc.Bottom = Cursor_Row;
   WriteConsoleOutput(hStdout, Line_Buffer, c, coord, &rc);
}

void SLtt_cls (void)
{
   DWORD bytes;
   COORD coord;
   char ch;

   SLtt_normal_video ();
   /* clear the WIN32 screen in one shot */
   coord.X = 0;
   coord.Y = 0;

   ch = ' ';

   (void) FillConsoleOutputCharacter(hStdout,
				     ch,
				     SLtt_Screen_Cols * SLtt_Screen_Rows,
				     coord,
				     &bytes);

     /* now set screen to the current attribute */
   ch = Attribute_Byte;
   (void) FillConsoleOutputAttribute(hStdout,
				     ch,
				     SLtt_Screen_Cols * SLtt_Screen_Rows,
				     coord,
				     &bytes);
}

void SLtt_putchar (char ch)
{
   DWORD bytes;
   WORD attr;
   COORD c;

   if (Current_Color) SLtt_normal_video ();
   win32_video_getxy ();
   switch (ch)
     {
      case 7:			/* ^G - break */
	SLtt_beep (); break;
      case 8:			/* ^H - backspace */
	goto_rc_abs (Cursor_Row, Cursor_Col - 1); break;
      case 13:			/* ^M - carriage return */
	goto_rc_abs (Cursor_Row, 0); break;
      default:			/* write character to screen */
	c.X = Cursor_Col;
	c.Y = Cursor_Row;
	attr = Attribute_Byte;
	WriteConsoleOutputCharacter(hStdout, &ch, 1, c, &bytes);
	WriteConsoleOutputAttribute(hStdout, &attr, 1, c, &bytes);
	goto_rc_abs (Cursor_Row, Cursor_Col + 1);
     }
}

void SLtt_get_screen_size (void)
{
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   HANDLE h;

   h = hStdout;
   if (h == INVALID_HANDLE_VALUE)
     h = GetStdHandle (STD_OUTPUT_HANDLE);

   if ((h == INVALID_HANDLE_VALUE)
       || (FALSE == GetConsoleScreenBufferInfo(h, &csbi)))
     {
	SLang_exit_error ("Unable to determine the screen size");
	return;
     }
#if 0
   SLtt_Screen_Rows = csbi.dwSize.Y;
   SLtt_Screen_Cols = csbi.dwSize.X;
#else
   SLtt_Screen_Rows = (csbi.srWindow.Bottom - csbi.srWindow.Top) + 1;
   SLtt_Screen_Cols = (csbi.srWindow.Right - csbi.srWindow.Left) + 1;
#endif
}

void SLtt_get_terminfo (void)
{
   SLtt_get_screen_size ();
}

static int win32_resize (void)
{
   SMALL_RECT windowRect;

   SLtt_get_screen_size ();

   windowRect.Left = 0;
   windowRect.Top = 0;
   windowRect.Right = SLtt_Screen_Cols - 1;
   windowRect.Bottom = SLtt_Screen_Rows - 1;

   if (FALSE == SetConsoleWindowInfo(hStdout, TRUE, &windowRect))
     return -1;

   return 0;
}

static int win32_init (void)
{
   SECURITY_ATTRIBUTES sec;

   memset ((char *) &sec, 0, sizeof(SECURITY_ATTRIBUTES));
   sec.nLength = sizeof (SECURITY_ATTRIBUTES);
   sec.bInheritHandle = FALSE;

   hStdout = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
				       FILE_SHARE_READ|FILE_SHARE_WRITE,
				       &sec,
				       CONSOLE_TEXTMODE_BUFFER,
				       0);

   if (hStdout == INVALID_HANDLE_VALUE)
     return -1;

   if ((FALSE == SetConsoleActiveScreenBuffer(hStdout))
       || (FALSE == SetConsoleMode(hStdout, 0))
       || (-1 == win32_resize ()))
     {
	SLtt_reset_video ();
	return -1;
     }

   return 0;
}

int SLtt_init_video (void)
{
   SLtt_reset_video ();

   if (-1 == win32_init ())
     return -1;

   /* It is possible for SLtt_init_video to be called after suspension.
    * For all I know, the window size may have changed.  So, resize it
    * now.
    */

   Cursor_Row = Cursor_Col = 0;
   SLtt_Use_Ansi_Colors = IsColor = 1;
   Blink_Killed = 1;

   SLtt_reset_scroll_region ();
   goto_rc_abs (0, 0);
   fixup_colors ();

   return 0;
}

int SLtt_reset_video (void)
{
   if (hStdout == INVALID_HANDLE_VALUE)
     return 0;

   SLtt_reset_scroll_region ();
   SLtt_goto_rc (SLtt_Screen_Rows - 1, 0);
   Attribute_Byte = 0x7;
   Current_Color = JNO_COLOR;
   SLtt_del_eol ();
   (void) CloseHandle (hStdout);

   hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
   if (hStdout != INVALID_HANDLE_VALUE)
     (void) SetConsoleActiveScreenBuffer(hStdout);

   hStdout = INVALID_HANDLE_VALUE;
   return 0;
}

#endif

/*}}}*/

#ifdef OS2_VIDEO /*{{{*/

# define INCL_BASE
# define INCL_NOPM
# define INCL_VIO
# define INCL_KBD

# define INCL_DOSPROCESS

# include <os2.h>
# ifndef __IBMC__
#  include <dos.h>
# endif
/* this is how to make a space character */
#define MK_SPACE_CHAR()	(((Attribute_Byte) << 8) | 0x20)

/* buffer to hold a line of character/attribute pairs */
#define MAXCOLS 256
static unsigned char Line_Buffer [MAXCOLS*2];

void SLtt_write_string (char *str)
{
   /* FIXME: Priority=medium
    * This should not go to stdout. */
   fputs (str, stdout);
}

void SLtt_goto_rc (int row, int col)
{
   row += Scroll_r1;
   VioSetCurPos (row, col, 0);
   Cursor_Row = row;
   Cursor_Col = col;
}

static void os2_video_getxy (void)
{
   USHORT r, c;

   VioGetCurPos (&r, &c, 0);
   Cursor_Row = r;
   Cursor_Col = c;
}

void SLtt_begin_insert (void)
{
   USHORT n;

   os2_video_getxy ();
   n = SLtt_Screen_Cols - Cursor_Col;

   n = 2 * (n - 1);
   VioReadCellStr ((PCH)Line_Buffer, &n, Cursor_Row, Cursor_Col, 0);
   VioWrtCellStr ((PCH)Line_Buffer, n, Cursor_Row, Cursor_Col + 1, 0);
}

void SLtt_end_insert (void)
{
}

void SLtt_delete_char (void)
{
   USHORT n;

   os2_video_getxy ();
   n = SLtt_Screen_Cols - Cursor_Col - 1;

   n *= 2;
   VioReadCellStr ((PCH)Line_Buffer, &n, Cursor_Row, Cursor_Col + 1, 0);
   VioWrtCellStr ((PCH)Line_Buffer, n, Cursor_Row, Cursor_Col, 0);
}

void SLtt_erase_line (void)
{
   USHORT w;

   Attribute_Byte = 0x07;
   w = MK_SPACE_CHAR ();

   VioWrtNCell ((BYTE*)&w, SLtt_Screen_Cols, Cursor_Row, 0, 0);

   Current_Color = JNO_COLOR;		/* since we messed with attribute byte */
}

void SLtt_delete_nlines (int nlines)
{
   SLtt_normal_video ();

   Line_Buffer[0] = ' '; Line_Buffer[1] = Attribute_Byte;
   VioScrollUp (Scroll_r1, 0, Scroll_r2, SLtt_Screen_Cols-1,
		nlines, (PCH) Line_Buffer, 0);
}

void SLtt_reverse_index (int nlines)
{
   SLtt_normal_video ();

   Line_Buffer[0] = ' '; Line_Buffer[1] = Attribute_Byte;
   VioScrollDn (Scroll_r1, 0, Scroll_r2, SLtt_Screen_Cols-1,
		nlines, (PCH) Line_Buffer, 0);
}

static void os2_video_invert_region (int top_row, int bot_row)
{
   int row, col;
   USHORT length = SLtt_Screen_Cols * 2;

   for (row = top_row; row < bot_row; row++)
     {
	VioReadCellStr ((PCH)Line_Buffer, &length, row, 0, 0);
	for (col = 1; col < length; col += 2)
	  Line_Buffer [col] ^= 0xff;
	VioWrtCellStr ((PCH)Line_Buffer, length, row, 0, 0);
     }
}

void SLtt_beep (void)
{
   int audible;			/* audible bell */
   int special = 0;		/* first row to invert */
   int visual = 0;		/* final row to invert */

   if (!SLtt_Ignore_Beep) return;

   audible = (SLtt_Ignore_Beep & 1);

   if ( (SLtt_Ignore_Beep & 4) )
     {
	special = SLtt_Screen_Rows - 1;
	visual = special--;	/* only invert bottom status line */
     }
   else if ( (SLtt_Ignore_Beep & 2) )
     {
	visual = SLtt_Screen_Rows;
     }

   if (visual) os2_video_invert_region (special, visual);
   if (audible) DosBeep (1500, 100); else DosSleep (100);
   if (visual) os2_video_invert_region (special, visual);
}

void SLtt_del_eol (void)
{
   USHORT w;
   if (Current_Color != JNO_COLOR) SLtt_normal_video ();

   w = MK_SPACE_CHAR ();

   VioWrtNCell ((BYTE*)&w, (SLtt_Screen_Cols - Cursor_Col),
		Cursor_Row, Cursor_Col, 0);
}

static void
write_attributes (SLsmg_Char_Type *src, unsigned int count)
{
   register unsigned char *p = Line_Buffer;
   register unsigned short pair;
   int n = count;

   /* write into a character/attribute pair */
   while (n-- > 0)
     {
	pair = SLSMG_CHAR_TO_USHORT(*src);/* character/color pair */
	src++;
	SLtt_reverse_video (pair >> 8);	/* color change */
	*(p++) = pair & 0xff;		/* character byte */
	*(p++) = Attribute_Byte;	/* attribute byte */
     }

   VioWrtCellStr ((PCH)Line_Buffer, (USHORT)(2 * count),
		  (USHORT)Cursor_Row, (USHORT)Cursor_Col, 0);
}

void SLtt_cls (void)
{
   SLtt_normal_video ();
   Line_Buffer [0] = ' '; Line_Buffer [1] = Attribute_Byte;
   VioScrollUp (0, 0, -1, -1, -1, (PCH)Line_Buffer, 0);
}

void SLtt_putchar (char ch)
{
   unsigned short p, *pp;

   if (Current_Color) SLtt_normal_video ();
   os2_video_getxy ();		/* get current position */

   switch (ch)
     {
      case 7:			/* ^G - break */
	SLtt_beep (); break;
      case 8:			/* ^H - backspace */
	goto_rc_abs (Cursor_Row, Cursor_Col - 1); break;
      case 13:			/* ^M - carriage return */
	goto_rc_abs (Cursor_Row, 0); break;
      default:			/* write character to screen */
 	VioWrtCharStrAtt (&ch, 1, Cursor_Row, Cursor_Col,
 			  (BYTE*)&Attribute_Byte, 0);
	goto_rc_abs (Cursor_Row, Cursor_Col + 1);
     }
}

void SLtt_get_screen_size (void)
{
#ifdef __os2__
# ifdef __IBMC__
   VIOMODEINFO vioModeInfo;
# endif
   vioModeInfo.cb = sizeof(vioModeInfo);
   VioGetMode (&vioModeInfo, 0);
   SLtt_Screen_Cols = vioModeInfo.col;
   SLtt_Screen_Rows = vioModeInfo.row;
#endif
}

void SLtt_get_terminfo (void)
{
   SLtt_get_screen_size ();
}

int SLtt_init_video (void)
{
   VIOINTENSITY RequestBlock;
   PTIB   ptib;
   PPIB   ppib;
   USHORT args[3] = { 6, 2, 1 };

   Cursor_Row = Cursor_Col = 0;

   IsColor = 1;			/* is it really? */

   /* Enable high-intensity background colors */
   RequestBlock.cb = sizeof (RequestBlock);
   RequestBlock.type = 2; RequestBlock.fs = 1;
   VioSetState (&RequestBlock, 0);	/* nop if !fullscreen */

   DosGetInfoBlocks (&ptib, &ppib);
   if ((ppib->pib_ultype) == 2)        /* VIO */
     Blink_Killed = 1;
   else
     {                              /* Fullscreen */
        if (VioSetState (args, 0) == 0)
	  Blink_Killed = 1;
        else
	  Blink_Killed = 0;
     }

   if (!Attribute_Byte)
     {
	/* find the attribute currently under the cursor */
	USHORT len, r, c;

	len = 2;
	VioGetCurPos (&r, &c, 0);
	VioReadCellStr ((PCH)Line_Buffer, &len, r, c, 0);
	Attribute_Byte = Line_Buffer[1];

	SLtt_set_color (JNORMAL_COLOR, NULL,
			Color_Names[(Attribute_Byte) & 0xf],
			Color_Names[(Attribute_Byte) >> 4]);
     }

   SLtt_Use_Ansi_Colors = IsColor;
   SLtt_get_screen_size ();
   SLtt_reset_scroll_region ();
   fixup_colors ();

   return 0;
}

#endif				       /* OS2_VIDEO */
/*}}}*/

#ifdef WATCOM_VIDEO /*{{{*/

# include <graph.h>
# define int86	int386		/* simplify code writing */

#include <dos.h>

/* this is how to make a space character */
#define MK_SPACE_CHAR()	(((Attribute_Byte) << 8) | 0x20)

/* buffer to hold a line of character/attribute pairs */
#define MAXCOLS 256
static unsigned char Line_Buffer [MAXCOLS*2];

/* define for direct to memory screen writes */
static unsigned char *Video_Base;
#define MK_SCREEN_POINTER(row,col) \
  ((unsigned short *)(Video_Base + 2 * (SLtt_Screen_Cols * (row) + (col))))

#define ScreenPrimary	(0xb800 << 4)
#define ScreenSize	(SLtt_Screen_Cols * SLtt_Screen_Rows)
#define ScreenSetCursor(x,y) _settextposition (x+1,y+1)

void ScreenGetCursor (int *x, int *y)
{
   struct rccoord rc = _gettextposition ();
   *x = rc.row - 1;
   *y = rc.col - 1;
}

void ScreenRetrieve (unsigned char *dest)
{
   memcpy (dest, (unsigned char *) ScreenPrimary, 2 * ScreenSize);
}

void ScreenUpdate (unsigned char *src)
{
   memcpy ((unsigned char *) ScreenPrimary, src, 2 * ScreenSize);
}

void SLtt_write_string (char *str)
{
   /* FIXME: Priority=medium
    * This should not go to stdout. */
   fputs (str, stdout);
}

void SLtt_goto_rc (int row, int col)
{
   row += Scroll_r1;
   if (row > SLtt_Screen_Rows) row = SLtt_Screen_Rows;
   if (col > SLtt_Screen_Cols) col = SLtt_Screen_Cols;
   ScreenSetCursor(row, col);
   Cursor_Row = row;
   Cursor_Col = col;
}

static void watcom_video_getxy (void)
{
   ScreenGetCursor (&Cursor_Row, &Cursor_Col);
}

void SLtt_begin_insert (void)
{
   unsigned short *p;
   unsigned short *pmin;
   int n;

   watcom_video_getxy ();
   n = SLtt_Screen_Cols - Cursor_Col;

   p = MK_SCREEN_POINTER (Cursor_Row, SLtt_Screen_Cols - 1);
   pmin = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);

   while (p-- > pmin) *(p + 1) = *p;
}

void SLtt_end_insert (void)
{
}

void SLtt_delete_char (void)
{
   unsigned short *p;
   register unsigned short *p1;
   int n;

   watcom_video_getxy ();
   n = SLtt_Screen_Cols - Cursor_Col - 1;

   p = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);
   while (n--)
     {
	p1 = p + 1;
	*p = *p1;
	p++;
     }
}

void SLtt_erase_line (void)
{
   unsigned short w;
   unsigned short *p = MK_SCREEN_POINTER (Cursor_Row, 0);
   register unsigned short *pmax = p + SLtt_Screen_Cols;

   Attribute_Byte = 0x07;
   w = MK_SPACE_CHAR ();
   while (p < pmax) *p++ = w;
   Current_Color = JNO_COLOR;		/* since we messed with attribute byte */
}

void SLtt_delete_nlines (int nlines)
{
   union REGS r;

   SLtt_normal_video ();
   r.x.eax = nlines;
   r.x.ecx = 0;
   r.h.ah = 6;
   r.h.ch = Scroll_r1;
   r.h.dl = SLtt_Screen_Cols - 1;
   r.h.dh = Scroll_r2;
   r.h.bh = Attribute_Byte;
   int86 (0x10, &r, &r);
}

void SLtt_reverse_index (int nlines)
{
   union REGS r;

   SLtt_normal_video ();
   r.h.al = nlines;
   r.x.ecx = 0;
   r.h.ah = 7;
   r.h.ch = Scroll_r1;
   r.h.dl = SLtt_Screen_Cols - 1;
   r.h.dh = Scroll_r2;
   r.h.bh = Attribute_Byte;
   int86 (0x10, &r, &r);
}

static void watcom_video_invert_region (int top_row, int bot_row)
{
   unsigned char buf [2 * 180 * 80];         /* 180 cols x 80 rows */
   unsigned char *b, *bmax;

   b    = buf + 1 + 2 * SLtt_Screen_Cols * top_row;
   bmax = buf + 1 + 2 * SLtt_Screen_Cols * bot_row;
   ScreenRetrieve (buf);
   while (b < bmax)
     {
	*b ^= 0xFF;
	b += 2;
     }
   ScreenUpdate (buf);
}

void SLtt_beep (void)
{
   int audible;			/* audible bell */
   int special = 0;		/* first row to invert */
   int visual = 0;		/* final row to invert */

   if (!SLtt_Ignore_Beep) return;

   audible = (SLtt_Ignore_Beep & 1);
   if ( (SLtt_Ignore_Beep & 4) )
     {
	special = SLtt_Screen_Rows - 1;
	visual = special--;	/* only invert bottom status line */
     }
   else if ( (SLtt_Ignore_Beep & 2) )
     {
	visual = SLtt_Screen_Rows;
     }

   if (visual) watcom_video_invert_region (special, visual);
   if (audible) sound (1500); delay (100); if (audible) nosound ();
   if (visual) watcom_video_invert_region (special, visual);
}

void SLtt_del_eol (void)
{
   unsigned short *p, *pmax;
   unsigned short w;
   int n;

   n = SLtt_Screen_Cols - Cursor_Col;
   p = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);
   pmax = p + n;

   if (Current_Color != JNO_COLOR) SLtt_normal_video ();

   w = MK_SPACE_CHAR ();
   while (p < pmax) *p++ = w;
}

static void
write_attributes (SLsmg_Char_Type *src, unsigned int count)
{
   register unsigned short pair;
   register unsigned short *pos = MK_SCREEN_POINTER (Cursor_Row, 0);

   /* write into a character/attribute pair */
   while (count--)
     {
	pair = SLSMG_CHAR_TO_USHORT(*src);/* character/color pair */
	src++;
	SLtt_reverse_video (pair >> 8);	/* color change */
	*(pos++) = ((unsigned short) Attribute_Byte << 8) | (pair & 0xff);
     }
}

void SLtt_cls (void)
{
   SLtt_normal_video ();
   SLtt_reset_scroll_region ();
   SLtt_goto_rc (0, 0);
   SLtt_delete_nlines (SLtt_Screen_Rows);
}

void SLtt_putchar (char ch)
{
   unsigned short p, *pp;

   if (Current_Color) SLtt_normal_video ();
   watcom_video_getxy ();
   switch (ch)
     {
      case 7:			/* ^G - break */
	SLtt_beep (); break;
      case 8:			/* ^H - backspace */
	goto_rc_abs (Cursor_Row, Cursor_Col - 1); break;
      case 13:			/* ^M - carriage return */
	goto_rc_abs (Cursor_Row, 0); break;
      default:			/* write character to screen */
	p = (Attribute_Byte << 8) | (unsigned char) ch;
	pp = MK_SCREEN_POINTER (Cursor_Row, Cursor_Col);
	*pp = p;
	goto_rc_abs (Cursor_Row, Cursor_Col + 1);
     }
}

void SLtt_get_screen_size (void)
{
   struct videoconfig vc;
   _getvideoconfig(&vc);

   SLtt_Screen_Rows = vc.numtextrows;
   SLtt_Screen_Cols = vc.numtextcols;
}

void SLtt_get_terminfo (void)
{
   SLtt_get_screen_size ();
}

int SLtt_init_video (void)
{
#ifdef HAS_SAVE_SCREEN
   save_screen ();
#endif

   Cursor_Row = Cursor_Col = 0;
   Video_Base = (unsigned char *) ScreenPrimary;
   if (!Attribute_Byte) Attribute_Byte = 0x17;
   IsColor = 1;			/* is it really? */

   if (IsColor)
     {
	union REGS r;
	r.x.eax = 0x1003; r.x.ebx = 0;
	int86 (0x10, &r, &r);
	Blink_Killed = 1;
     }

   SLtt_Use_Ansi_Colors = IsColor;
   SLtt_get_screen_size ();
   SLtt_reset_scroll_region ();
   fixup_colors ();

   return 0;
}

#endif				       /* WATCOM_VIDEO */

/*}}}*/

/* -------------------------------------------------------------------------*\
 * The rest of the functions are, for the most part, independent of a specific
 * video system.
\* ------------------------------------------------------------------------ */

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_set_scroll_region (int r1, int r2);
 *
 * define a scroll region of top_row to bottom_row
\*----------------------------------------------------------------------*/
void SLtt_set_scroll_region (int top_row, int bottom_row)
{
   Scroll_r1 = top_row;
   Scroll_r2 = bottom_row;
}

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_reset_scroll_region (void);
 *
 * reset the scrol region to be the entire screen,
 * ie, SLtt_set_scroll_region (0, SLtt_Screen_Rows);
\*----------------------------------------------------------------------*/
void SLtt_reset_scroll_region (void)
{
   Scroll_r1 = 0;
   Scroll_r2 = SLtt_Screen_Rows - 1;
}

/*----------------------------------------------------------------------*\
 * Function:	int SLtt_flush_output (void);
\*----------------------------------------------------------------------*/
int SLtt_flush_output (void)
{
#if defined(WIN32_VIDEO)
   return 0;
#else
   /* FIXME: Priority=medium
    * This should not go to stdout. */
   fflush (stdout);
   return 0;
#endif
}

int SLtt_set_cursor_visibility (int show)
{
#if defined(WIN32_VIDEO)
   CONSOLE_CURSOR_INFO c;
     
   if (0 == GetConsoleCursorInfo (hStdout, &c))
     return -1;
   c.bVisible = (show ? TRUE: FALSE);
   if (0 == SetConsoleCursorInfo (hStdout, &c))
     return -1;
   return 0;
#else
   (void) show;
   return -1;
#endif
}

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_reverse_video (int color);
 *
 * set Attribute_Byte corresponding to COLOR.
 * Use Current_Color to remember the color which was set.
 * convert from the COLOR number to the attribute value.
\*----------------------------------------------------------------------*/
void SLtt_reverse_video (int color)
{
   if ((color >= JMAX_COLORS) || (color < 0))
     return;

   Attribute_Byte = Color_Map [color];
   Current_Color = color;
}

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_normal_video (void);
 *
 * reset the attributes for normal video
\*----------------------------------------------------------------------*/
void SLtt_normal_video (void)
{
   SLtt_reverse_video (JNORMAL_COLOR);
}

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_smart_puts (SLsmg_Char_Type *new_string,
 *				      SLsmg_Char_Type *old_string,
 *				      int len, int row);
 *
 * puts NEW_STRING, which has length LEN, at row ROW.  NEW_STRING contains
 * characters/colors packed in the form value = ((color << 8) | (ch));
 *
 * the puts tries to avoid overwriting the same characters/colors
 *
 * OLD_STRING is not used, maintained for compatibility with other systems
\*----------------------------------------------------------------------*/
void SLtt_smart_puts (SLsmg_Char_Type *new_string,
		      SLsmg_Char_Type *old_string,
		      int len, int row)
{
   (void) old_string;
   Cursor_Row = row;
   Cursor_Col = 0;
   write_attributes (new_string, len);
}

/*----------------------------------------------------------------------*\
 * Function:	int SLtt_reset_video (void);
\*----------------------------------------------------------------------*/
#ifndef WIN32_VIDEO
int SLtt_reset_video (void)
{
   SLtt_reset_scroll_region ();
   SLtt_goto_rc (SLtt_Screen_Rows - 1, 0);
#ifdef HAS_SAVE_SCREEN
   restore_screen ();
#endif
   Attribute_Byte = 0x07;
   Current_Color = JNO_COLOR;
   SLtt_del_eol ();
   return 0;
}
#endif

/*----------------------------------------------------------------------*\
 * Function:	void SLtt_set_color (int obj, char *what, char *fg, char *bg);
 *
 * set foreground and background colors of OBJ to the attributes which
 * correspond to the names FG and BG, respectively.
 *
 * WHAT is the name corresponding to the object OBJ, but is not used in
 * this routine.
\*----------------------------------------------------------------------*/
void SLtt_set_color (int obj, char *what, char *fg, char *bg)
{
   int i, b = 0, f = 7;

   (void) what;

   if ((obj < 0) || (obj >= JMAX_COLORS))
     return;

   for (i = 0; i < JMAX_COLOR_NAMES; i++ )
     {
	if (!strcmp (fg, Color_Names [i]))
	  {
	     f = i;
	     break;
	  }
     }

   for (i = 0; i < JMAX_COLOR_NAMES; i++)
     {
	if (!strcmp (bg, Color_Names [i]))
	  {
	     if (Blink_Killed) b = i; else b = i & 0x7;
	     break;
	  }
     }
   if (f == b) return;

   Color_Map [obj] = (b << 4) | f;

    /* if we're setting the normal color, and the attribute byte hasn't
     been set yet, set it to the new color */
   if ((obj == 0) && (Attribute_Byte == 0))
     SLtt_reverse_video (0);
   
   if (_SLtt_color_changed_hook != NULL)
     (*_SLtt_color_changed_hook)();
}

static void fixup_colors (void)
{
   unsigned int i;

   if (Blink_Killed)
     return;

   for (i = 0; i < JMAX_COLORS; i++)
     Color_Map[i] &= 0x7F;

   SLtt_normal_video ();
}


/* FIXME!!! Add mono support.
 * The following functions have not been fully implemented.
 */
void SLtt_set_mono (int obj_unused, char *unused, SLtt_Char_Type c_unused)
{
   (void) obj_unused;
   (void) unused;
   (void) c_unused;
}

#if 0
void SLtt_add_color_attribute (int obj, SLtt_Char_Type attr)
{
   (void) obj;
   (void) attr;
}
#endif