aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/mud/terminal/vt320.java
blob: 7c1bbb3dfd9fa5e55733480e7199fa824c556ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
/*
 * This file is part of "JTA - Telnet/SSH for the JAVA(tm) platform".
 *
 * (c) Matthias L. Jugel, Marcus Meiner 1996-2005. All Rights Reserved.
 *
 * Please visit http://javatelnet.org/ for updates and contact.
 *
 * --LICENSE NOTICE--
 * 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 2
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * --LICENSE NOTICE--
 *
 */

package de.mud.terminal;

import java.util.Properties;

/**
 * Implementation of a VT terminal emulation plus ANSI compatible.
 * <P>
 * <B>Maintainer:</B> Marcus Mei�ner
 *
 * @version $Id: vt320.java 507 2005-10-25 10:14:52Z marcus $
 * @author  Matthias L. Jugel, Marcus Mei�ner
 */
public abstract class vt320 extends VDUBuffer implements VDUInput {

  /** The current version id tag.<P>
   * $Id: vt320.java 507 2005-10-25 10:14:52Z marcus $
   *
   */
  public final static String ID = "$Id: vt320.java 507 2005-10-25 10:14:52Z marcus $";

  /** the debug level */
  private final static int debug = 0;
  private StringBuilder debugStr;
  public abstract void debug(String notice);

  /**
   * Write an answer back to the remote host. This is needed to be able to
   * send terminal answers requests like status and type information.
   * @param b the array of bytes to be sent
   */
  public abstract void write(byte[] b);

  /**
   * Write an answer back to the remote host. This is needed to be able to
   * send terminal answers requests like status and type information.
   * @param b the array of bytes to be sent
   */
  public abstract void write(int b);

  /**
   * Play the beep sound ...
   */
  public void beep() { /* do nothing by default */
  }

  /**
   * Convenience function for putString(char[], int, int)
   */
  public void putString(String s) {
	int len = s.length();
	char[] tmp = new char[len];
	s.getChars(0, len, tmp, 0);
	putString(tmp, 0, len);
  }

  /**
   * Put string at current cursor position. Moves cursor
   * according to the String. Does NOT wrap.
   * @param s character array
   * @param start place to start in array
   * @param len number of characters to process
   */
  public void putString(char[] s, int start, int len) {
    if (len > 0) {
      //markLine(R, 1);
      int lastChar = -1;
      char c;

      for (int i = 0; i < len; i++) {
    	c = s[start + i];
    	// Shortcut for my favorite ASCII
    	if (c <= 0x7F) {
    	  if (lastChar != -1)
    	    putChar((char) lastChar, false);
    	  lastChar = c;
    	} else if (!Character.isLowSurrogate(c) && !Character.isHighSurrogate(c)) {
          if (Character.getType(c) == Character.NON_SPACING_MARK) {
            if (lastChar != -1) {
              char nc = Precomposer.precompose((char) lastChar, c);
              putChar(nc, false);
              lastChar = -1;
            }
          } else {
        	if (lastChar != -1)
              putChar((char) lastChar, false);
            lastChar = c;
          }
	    }
      }

      if (lastChar != -1)
        putChar((char) lastChar, false);

      setCursorPosition(C, R);
      redraw();
    }
  }

  protected void sendTelnetCommand(byte cmd) {

  }

  /**
   * Sent the changed window size from the terminal to all listeners.
   */
  protected void setWindowSize(int c, int r) {
    /* To be overridden by Terminal.java */
  }

  @Override
public void setScreenSize(int c, int r, boolean broadcast) {
    int oldrows = getRows();
    //int oldcols = getColumns();

    if (debug>2) {
      debugStr.append("setscreensize (")
        .append(c)
        .append(',')
        .append(r)
        .append(',')
        .append(broadcast)
        .append(')');
      debug(debugStr.toString());
      debugStr.setLength(0);
    }

    super.setScreenSize(c,r,false);

    /* Tricky, since the VDUBuffer works strangely. */
    if (r > oldrows) {
      setCursorPosition(C, R + (r-oldrows));
      redraw();
    }
    if (broadcast) {
      setWindowSize(c, r); /* broadcast up */
    }
  }


  /**
   * Create a new vt320 terminal and intialize it with useful settings.
   */
  public vt320(int width, int height) {
    super(width, height);
    setVMS(false);
    setIBMCharset(false);
    setTerminalID("vt320");
    setBufferSize(100);
    //setBorder(2, false);

    debugStr = new StringBuilder();

    gx = new char[4];
    reset();

    /* top row of numpad */
    PF1 = "\u001bOP";
    PF2 = "\u001bOQ";
    PF3 = "\u001bOR";
    PF4 = "\u001bOS";

    /* the 3x2 keyblock on PC keyboards */
    Insert = new String[4];
    Remove = new String[4];
    KeyHome = new String[4];
    KeyEnd = new String[4];
    NextScn = new String[4];
    PrevScn = new String[4];
    Escape = new String[4];
    BackSpace = new String[4];
    TabKey = new String[4];
    Insert[0] = Insert[1] = Insert[2] = Insert[3] = "\u001b[2~";
    Remove[0] = Remove[1] = Remove[2] = Remove[3] = "\u001b[3~";
    PrevScn[0] = PrevScn[1] = PrevScn[2] = PrevScn[3] = "\u001b[5~";
    NextScn[0] = NextScn[1] = NextScn[2] = NextScn[3] = "\u001b[6~";
    KeyHome[0] = KeyHome[1] = KeyHome[2] = KeyHome[3] = "\u001b[H";
    KeyEnd[0] = KeyEnd[1] = KeyEnd[2] = KeyEnd[3] = "\u001b[F";
    Escape[0] = Escape[1] = Escape[2] = Escape[3] = "\u001b";
    if (vms) {
      BackSpace[1] = "" + (char) 10;	//  VMS shift deletes word back
      BackSpace[2] = "\u0018";	//  VMS control deletes line back
      BackSpace[0] = BackSpace[3] = "\u007f";	//  VMS other is delete
    } else {
      //BackSpace[0] = BackSpace[1] = BackSpace[2] = BackSpace[3] = "\b";
      // ConnectBot modifications.
      BackSpace[0] = "\b";
      BackSpace[1] = "\u007f";
      BackSpace[2] = "\u001b[3~";
      BackSpace[3] = "\u001b[2~";
    }

    /* some more VT100 keys */
    Find = "\u001b[1~";
    Select = "\u001b[4~";
    Help = "\u001b[28~";
    Do = "\u001b[29~";

    FunctionKey = new String[21];
    FunctionKey[0] = "";
    FunctionKey[1] = PF1;
    FunctionKey[2] = PF2;
    FunctionKey[3] = PF3;
    FunctionKey[4] = PF4;
    /* following are defined differently for vt220 / vt132 ... */
    FunctionKey[5] = "\u001b[15~";
    FunctionKey[6] = "\u001b[17~";
    FunctionKey[7] = "\u001b[18~";
    FunctionKey[8] = "\u001b[19~";
    FunctionKey[9] = "\u001b[20~";
    FunctionKey[10] = "\u001b[21~";
    FunctionKey[11] = "\u001b[23~";
    FunctionKey[12] = "\u001b[24~";
    FunctionKey[13] = "\u001b[25~";
    FunctionKey[14] = "\u001b[26~";
    FunctionKey[15] = Help;
    FunctionKey[16] = Do;
    FunctionKey[17] = "\u001b[31~";
    FunctionKey[18] = "\u001b[32~";
    FunctionKey[19] = "\u001b[33~";
    FunctionKey[20] = "\u001b[34~";

    FunctionKeyShift = new String[21];
    FunctionKeyAlt = new String[21];
    FunctionKeyCtrl = new String[21];

    for (int i = 0; i < 20; i++) {
      FunctionKeyShift[i] = "";
      FunctionKeyAlt[i] = "";
      FunctionKeyCtrl[i] = "";
    }
    FunctionKeyShift[15] = Find;
    FunctionKeyShift[16] = Select;


    TabKey[0] = "\u0009";
    TabKey[1] = "\u001bOP\u0009";
    TabKey[2] = TabKey[3] = "";

    KeyUp = new String[4];
    KeyUp[0] = "\u001b[A";
    KeyDown = new String[4];
    KeyDown[0] = "\u001b[B";
    KeyRight = new String[4];
    KeyRight[0] = "\u001b[C";
    KeyLeft = new String[4];
    KeyLeft[0] = "\u001b[D";
    Numpad = new String[10];
    Numpad[0] = "\u001bOp";
    Numpad[1] = "\u001bOq";
    Numpad[2] = "\u001bOr";
    Numpad[3] = "\u001bOs";
    Numpad[4] = "\u001bOt";
    Numpad[5] = "\u001bOu";
    Numpad[6] = "\u001bOv";
    Numpad[7] = "\u001bOw";
    Numpad[8] = "\u001bOx";
    Numpad[9] = "\u001bOy";
    KPMinus = PF4;
    KPComma = "\u001bOl";
    KPPeriod = "\u001bOn";
    KPEnter = "\u001bOM";

    NUMPlus = new String[4];
    NUMPlus[0] = "+";
    NUMDot = new String[4];
    NUMDot[0] = ".";
  }

  public void setBackspace(int type) {
    switch (type) {
    case DELETE_IS_DEL:
      BackSpace[0] = "\u007f";
      BackSpace[1] = "\b";
      break;
    case DELETE_IS_BACKSPACE:
      BackSpace[0] = "\b";
      BackSpace[1] = "\u007f";
      break;
    }
  }

  /**
   * Create a default vt320 terminal with 80 columns and 24 lines.
   */
  public vt320() {
    this(80, 24);
  }

  /**
   * Terminal is mouse-aware and requires (x,y) coordinates of
   * on the terminal (character coordinates) and the button clicked.
   * @param x
   * @param y
   * @param modifiers
   */
  public void mousePressed(int x, int y, int modifiers) {
    if (mouserpt == 0)
      return;

    int mods = modifiers;
    mousebut = 3;
    if ((mods & 16) == 16) mousebut = 0;
    if ((mods & 8) == 8) mousebut = 1;
    if ((mods & 4) == 4) mousebut = 2;

    int mousecode;
    if (mouserpt == 9)	/* X10 Mouse */
      mousecode = 0x20 | mousebut;
    else			/* normal xterm mouse reporting */
      mousecode = mousebut | 0x20 | ((mods & 7) << 2);

    byte b[] = new byte[6];

    b[0] = 27;
    b[1] = (byte) '[';
    b[2] = (byte) 'M';
    b[3] = (byte) mousecode;
    b[4] = (byte) (0x20 + x + 1);
    b[5] = (byte) (0x20 + y + 1);

    write(b); // FIXME: writeSpecial here
  }

  /**
   * Terminal is mouse-aware and requires the coordinates and button
   * of the release.
   * @param x
   * @param y
   * @param modifiers
   */
  public void mouseReleased(int x, int y, int modifiers) {
    if (mouserpt == 0)
      return;

    /* problem is tht modifiers still have the released button set in them.
    int mods = modifiers;
    mousebut = 3;
    if ((mods & 16)==16) mousebut=0;
    if ((mods &  8)==8 ) mousebut=1;
    if ((mods &  4)==4 ) mousebut=2;
    */

    int mousecode;
    if (mouserpt == 9)
      mousecode = 0x20 + mousebut;	/* same as press? appears so. */
    else
      mousecode = '#';

    byte b[] = new byte[6];
    b[0] = 27;
    b[1] = (byte) '[';
    b[2] = (byte) 'M';
    b[3] = (byte) mousecode;
    b[4] = (byte) (0x20 + x + 1);
    b[5] = (byte) (0x20 + y + 1);
    write(b); // FIXME: writeSpecial here
    mousebut = 0;
  }


  /** we should do localecho (passed from other modules). false is default */
  private boolean localecho = false;

  /**
   * Enable or disable the local echo property of the terminal.
   * @param echo true if the terminal should echo locally
   */
  public void setLocalEcho(boolean echo) {
    localecho = echo;
  }

  /**
   * Enable the VMS mode of the terminal to handle some things differently
   * for VMS hosts.
   * @param vms true for vms mode, false for normal mode
   */
  public void setVMS(boolean vms) {
    this.vms = vms;
  }

  /**
   * Enable the usage of the IBM character set used by some BBS's. Special
   * graphical character are available in this mode.
   * @param ibm true to use the ibm character set
   */
  public void setIBMCharset(boolean ibm) {
    useibmcharset = ibm;
  }

  /**
   * Override the standard key codes used by the terminal emulation.
   * @param codes a properties object containing key code definitions
   */
  public void setKeyCodes(Properties codes) {
    String res, prefixes[] = {"", "S", "C", "A"};
    int i;

    for (i = 0; i < 10; i++) {
      res = codes.getProperty("NUMPAD" + i);
      if (res != null) Numpad[i] = unEscape(res);
    }
    for (i = 1; i < 20; i++) {
      res = codes.getProperty("F" + i);
      if (res != null) FunctionKey[i] = unEscape(res);
      res = codes.getProperty("SF" + i);
      if (res != null) FunctionKeyShift[i] = unEscape(res);
      res = codes.getProperty("CF" + i);
      if (res != null) FunctionKeyCtrl[i] = unEscape(res);
      res = codes.getProperty("AF" + i);
      if (res != null) FunctionKeyAlt[i] = unEscape(res);
    }
    for (i = 0; i < 4; i++) {
      res = codes.getProperty(prefixes[i] + "PGUP");
      if (res != null) PrevScn[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "PGDOWN");
      if (res != null) NextScn[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "END");
      if (res != null) KeyEnd[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "HOME");
      if (res != null) KeyHome[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "INSERT");
      if (res != null) Insert[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "REMOVE");
      if (res != null) Remove[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "UP");
      if (res != null) KeyUp[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "DOWN");
      if (res != null) KeyDown[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "LEFT");
      if (res != null) KeyLeft[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "RIGHT");
      if (res != null) KeyRight[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "ESCAPE");
      if (res != null) Escape[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "BACKSPACE");
      if (res != null) BackSpace[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "TAB");
      if (res != null) TabKey[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "NUMPLUS");
      if (res != null) NUMPlus[i] = unEscape(res);
      res = codes.getProperty(prefixes[i] + "NUMDECIMAL");
      if (res != null) NUMDot[i] = unEscape(res);
    }
  }

  /**
   * Set the terminal id used to identify this terminal.
   * @param terminalID the id string
   */
  public void setTerminalID(String terminalID) {
    this.terminalID = terminalID;

    if (terminalID.equals("scoansi")) {
      FunctionKey[1] = "\u001b[M";  FunctionKey[2] = "\u001b[N";
      FunctionKey[3] = "\u001b[O";  FunctionKey[4] = "\u001b[P";
      FunctionKey[5] = "\u001b[Q";  FunctionKey[6] = "\u001b[R";
      FunctionKey[7] = "\u001b[S";  FunctionKey[8] = "\u001b[T";
      FunctionKey[9] = "\u001b[U";  FunctionKey[10] = "\u001b[V";
      FunctionKey[11] = "\u001b[W"; FunctionKey[12] = "\u001b[X";
      FunctionKey[13] = "\u001b[Y"; FunctionKey[14] = "?";
      FunctionKey[15] = "\u001b[a"; FunctionKey[16] = "\u001b[b";
      FunctionKey[17] = "\u001b[c"; FunctionKey[18] = "\u001b[d";
      FunctionKey[19] = "\u001b[e"; FunctionKey[20] = "\u001b[f";
      PrevScn[0] = PrevScn[1] = PrevScn[2] = PrevScn[3] = "\u001b[I";
      NextScn[0] = NextScn[1] = NextScn[2] = NextScn[3] = "\u001b[G";
      // more theoretically.
    }
  }

  public void setAnswerBack(String ab) {
    this.answerBack = unEscape(ab);
  }

  /**
   * Get the terminal id used to identify this terminal.
   */
  public String getTerminalID() {
    return terminalID;
  }

  /**
   * A small conveniance method thar converts the string to a byte array
   * for sending.
   * @param s the string to be sent
   */
  private boolean write(String s, boolean doecho) {
    if (debug > 2) {
      debugStr.append("write(|")
        .append(s)
        .append("|,")
        .append(doecho);
      debug(debugStr.toString());
      debugStr.setLength(0);
    }
    if (s == null) // aka the empty string.
      return true;
    /* NOTE: getBytes() honours some locale, it *CONVERTS* the string.
     * However, we output only 7bit stuff towards the target, and *some*
     * 8 bit control codes. We must not mess up the latter, so we do hand
     * by hand copy.
     */

    byte arr[] = new byte[s.length()];
    for (int i = 0; i < s.length(); i++) {
      arr[i] = (byte) s.charAt(i);
    }
    write(arr);

    if (doecho)
      putString(s);
    return true;
  }

  private boolean write(int s, boolean doecho) {
    if (debug > 2) {
      debugStr.append("write(|")
        .append(s)
        .append("|,")
        .append(doecho);
      debug(debugStr.toString());
      debugStr.setLength(0);
    }

    write(s);

    if (doecho)
      putChar((char)s, false);
    return true;
  }

  private boolean write(String s) {
    return write(s, localecho);
  }

  // ===================================================================
  // the actual terminal emulation code comes here:
  // ===================================================================

  private String terminalID = "vt320";
  private String answerBack = "Use Terminal.answerback to set ...\n";

  // X - COLUMNS, Y - ROWS
  int R,C;
  int attributes = 0;

  int Sc,Sr,Sa,Stm,Sbm;
  char Sgr,Sgl;
  char Sgx[];

  int insertmode = 0;
  int statusmode = 0;
  boolean vt52mode = false;
  boolean keypadmode = false; /* false - numeric, true - application */
  boolean output8bit = false;
  int normalcursor = 0;
  boolean moveoutsidemargins = true;
  boolean wraparound = true;
  boolean sendcrlf = true;
  boolean capslock = false;
  boolean numlock = false;
  int mouserpt = 0;
  byte mousebut = 0;

  boolean useibmcharset = false;

  int lastwaslf = 0;
  boolean usedcharsets = false;

  private final static char ESC = 27;
  private final static char IND = 132;
  private final static char NEL = 133;
  private final static char RI = 141;
  private final static char SS2 = 142;
  private final static char SS3 = 143;
  private final static char DCS = 144;
  private final static char HTS = 136;
  private final static char CSI = 155;
  private final static char OSC = 157;
  private final static int TSTATE_DATA = 0;
  private final static int TSTATE_ESC = 1; /* ESC */
  private final static int TSTATE_CSI = 2; /* ESC [ */
  private final static int TSTATE_DCS = 3; /* ESC P */
  private final static int TSTATE_DCEQ = 4; /* ESC [? */
  private final static int TSTATE_ESCSQUARE = 5; /* ESC # */
  private final static int TSTATE_OSC = 6;       /* ESC ] */
  private final static int TSTATE_SETG0 = 7;     /* ESC (? */
  private final static int TSTATE_SETG1 = 8;     /* ESC )? */
  private final static int TSTATE_SETG2 = 9;     /* ESC *? */
  private final static int TSTATE_SETG3 = 10;    /* ESC +? */
  private final static int TSTATE_CSI_DOLLAR = 11; /* ESC [ Pn $ */
  private final static int TSTATE_CSI_EX = 12; /* ESC [ ! */
  private final static int TSTATE_ESCSPACE = 13; /* ESC <space> */
  private final static int TSTATE_VT52X = 14;
  private final static int TSTATE_VT52Y = 15;
  private final static int TSTATE_CSI_TICKS = 16;
  private final static int TSTATE_CSI_EQUAL = 17; /* ESC [ = */
  private final static int TSTATE_TITLE = 18; /* xterm title */

  /* Keys we support */
  public final static int KEY_PAUSE = 1;
  public final static int KEY_F1 = 2;
  public final static int KEY_F2 = 3;
  public final static int KEY_F3 = 4;
  public final static int KEY_F4 = 5;
  public final static int KEY_F5 = 6;
  public final static int KEY_F6 = 7;
  public final static int KEY_F7 = 8;
  public final static int KEY_F8 = 9;
  public final static int KEY_F9 = 10;
  public final static int KEY_F10 = 11;
  public final static int KEY_F11 = 12;
  public final static int KEY_F12 = 13;
  public final static int KEY_UP = 14;
  public final static int KEY_DOWN =15 ;
  public final static int KEY_LEFT = 16;
  public final static int KEY_RIGHT = 17;
  public final static int KEY_PAGE_DOWN = 18;
  public final static int KEY_PAGE_UP = 19;
  public final static int KEY_INSERT = 20;
  public final static int KEY_DELETE = 21;
  public final static int KEY_BACK_SPACE = 22;
  public final static int KEY_HOME = 23;
  public final static int KEY_END = 24;
  public final static int KEY_NUM_LOCK = 25;
  public final static int KEY_CAPS_LOCK = 26;
  public final static int KEY_SHIFT = 27;
  public final static int KEY_CONTROL = 28;
  public final static int KEY_ALT = 29;
  public final static int KEY_ENTER = 30;
  public final static int KEY_NUMPAD0 = 31;
  public final static int KEY_NUMPAD1 = 32;
  public final static int KEY_NUMPAD2 = 33;
  public final static int KEY_NUMPAD3 = 34;
  public final static int KEY_NUMPAD4 = 35;
  public final static int KEY_NUMPAD5 = 36;
  public final static int KEY_NUMPAD6 = 37;
  public final static int KEY_NUMPAD7 = 38;
  public final static int KEY_NUMPAD8 = 39;
  public final static int KEY_NUMPAD9 = 40;
  public final static int KEY_DECIMAL = 41;
  public final static int KEY_ADD = 42;
  public final static int KEY_ESCAPE = 43;

  public final static int DELETE_IS_DEL = 0;
  public final static int DELETE_IS_BACKSPACE = 1;

  /* The graphics charsets
   * B - default ASCII
   * A - ISO Latin 1
   * 0 - DEC SPECIAL
   * < - User defined
   * ....
   */
  char gx[];
  char gl;		// GL (left charset)
  char gr;		// GR (right charset)
  int onegl;	// single shift override for GL.

  // Map from scoansi linedrawing to DEC _and_ unicode (for the stuff which
  // is not in linedrawing). Got from experimenting with scoadmin.
  private final static String scoansi_acs = "Tm7k3x4u?kZl@mYjEnB\u2566DqCtAvM\u2550:\u2551N\u2557I\u2554;\u2557H\u255a0a<\u255d";
  // array to store DEC Special -> Unicode mapping
  //  Unicode   DEC  Unicode name    (DEC name)
  private static char DECSPECIAL[] = {
    '\u0040', //5f blank
    '\u2666', //60 black diamond
    '\u2592', //61 grey square
    '\u2409', //62 Horizontal tab  (ht) pict. for control
    '\u240c', //63 Form Feed       (ff) pict. for control
    '\u240d', //64 Carriage Return (cr) pict. for control
    '\u240a', //65 Line Feed       (lf) pict. for control
    '\u00ba', //66 Masculine ordinal indicator
    '\u00b1', //67 Plus or minus sign
    '\u2424', //68 New Line        (nl) pict. for control
    '\u240b', //69 Vertical Tab    (vt) pict. for control
    '\u2518', //6a Forms light up   and left
    '\u2510', //6b Forms light down and left
    '\u250c', //6c Forms light down and right
    '\u2514', //6d Forms light up   and right
    '\u253c', //6e Forms light vertical and horizontal
    '\u2594', //6f Upper 1/8 block                        (Scan 1)
    '\u2580', //70 Upper 1/2 block                        (Scan 3)
    '\u2500', //71 Forms light horizontal or ?em dash?    (Scan 5)
    '\u25ac', //72 \u25ac black rect. or \u2582 lower 1/4 (Scan 7)
    '\u005f', //73 \u005f underscore  or \u2581 lower 1/8 (Scan 9)
    '\u251c', //74 Forms light vertical and right
    '\u2524', //75 Forms light vertical and left
    '\u2534', //76 Forms light up   and horizontal
    '\u252c', //77 Forms light down and horizontal
    '\u2502', //78 vertical bar
    '\u2264', //79 less than or equal
    '\u2265', //7a greater than or equal
    '\u00b6', //7b paragraph
    '\u2260', //7c not equal
    '\u00a3', //7d Pound Sign (british)
    '\u00b7'  //7e Middle Dot
  };

  /** Strings to send on function key pressing */
  private String Numpad[];
  private String FunctionKey[];
  private String FunctionKeyShift[];
  private String FunctionKeyCtrl[];
  private String FunctionKeyAlt[];
  private String TabKey[];
  private String KeyUp[],KeyDown[],KeyLeft[],KeyRight[];
  private String KPMinus, KPComma, KPPeriod, KPEnter;
  private String PF1, PF2, PF3, PF4;
  private String Help, Do, Find, Select;

  private String KeyHome[], KeyEnd[], Insert[], Remove[], PrevScn[], NextScn[];
  private String Escape[], BackSpace[], NUMDot[], NUMPlus[];

  private String osc,dcs;  /* to memorize OSC & DCS control sequence */

  /** vt320 state variable (internal) */
  private int term_state = TSTATE_DATA;
  /** in vms mode, set by Terminal.VMS property */
  private boolean vms = false;
  /** Tabulators */
  private byte[] Tabs;
  /** The list of integers as used by CSI */
  private int[] DCEvars = new int[30];
  private int DCEvar;

  /**
   * Replace escape code characters (backslash + identifier) with their
   * respective codes.
   * @param tmp the string to be parsed
   * @return a unescaped string
   */
  static String unEscape(String tmp) {
    int idx = 0, oldidx = 0;
    String cmd;
    // f.println("unescape("+tmp+")");
    cmd = "";
    while ((idx = tmp.indexOf('\\', oldidx)) >= 0 &&
            ++idx <= tmp.length()) {
      cmd += tmp.substring(oldidx, idx - 1);
      if (idx == tmp.length()) return cmd;
      switch (tmp.charAt(idx)) {
        case 'b':
          cmd += "\b";
          break;
        case 'e':
          cmd += "\u001b";
          break;
        case 'n':
          cmd += "\n";
          break;
        case 'r':
          cmd += "\r";
          break;
        case 't':
          cmd += "\t";
          break;
        case 'v':
          cmd += "\u000b";
          break;
        case 'a':
          cmd += "\u0012";
          break;
        default :
          if ((tmp.charAt(idx) >= '0') && (tmp.charAt(idx) <= '9')) {
            int i;
            for (i = idx; i < tmp.length(); i++)
              if ((tmp.charAt(i) < '0') || (tmp.charAt(i) > '9'))
                break;
            cmd += (char) Integer.parseInt(tmp.substring(idx, i));
            idx = i - 1;
          } else
            cmd += tmp.substring(idx, ++idx);
          break;
      }
      oldidx = ++idx;
    }
    if (oldidx <= tmp.length()) cmd += tmp.substring(oldidx);
    return cmd;
  }

  /**
   * A small conveniance method thar converts a 7bit string to the 8bit
   * version depending on VT52/Output8Bit mode.
   *
   * @param s the string to be sent
   */
  private boolean writeSpecial(String s) {
    if (s == null)
      return true;
    if (((s.length() >= 3) && (s.charAt(0) == 27) && (s.charAt(1) == 'O'))) {
      if (vt52mode) {
        if ((s.charAt(2) >= 'P') && (s.charAt(2) <= 'S')) {
          s = "\u001b" + s.substring(2); /* ESC x */
        } else {
          s = "\u001b?" + s.substring(2); /* ESC ? x */
        }
      } else {
        if (output8bit) {
          s = "\u008f" + s.substring(2);  /* SS3 x */
        } /* else keep string as it is */
      }
    }
    if (((s.length() >= 3) && (s.charAt(0) == 27) && (s.charAt(1) == '['))) {
      if (output8bit) {
        s = "\u009b" + s.substring(2); /* CSI ... */
      } /* else keep */
    }
    return write(s, false);
  }

  /**
   * main keytyping event handler...
   */
  public void keyPressed(int keyCode, char keyChar, int modifiers) {
    boolean control = (modifiers & VDUInput.KEY_CONTROL) != 0;
    boolean shift = (modifiers & VDUInput.KEY_SHIFT) != 0;
    boolean alt = (modifiers & VDUInput.KEY_ALT) != 0;

    if (debug > 1) {
      debugStr.append("keyPressed(")
        .append(keyCode)
        .append(", ")
        .append((int)keyChar)
        .append(", ")
        .append(modifiers)
        .append(')');
      debug(debugStr.toString());
      debugStr.setLength(0);
    }

    int xind;
    String fmap[];
    xind = 0;
    fmap = FunctionKey;
    if (shift) {
      fmap = FunctionKeyShift;
      xind = 1;
    }
    if (control) {
      fmap = FunctionKeyCtrl;
      xind = 2;
    }
    if (alt) {
      fmap = FunctionKeyAlt;
      xind = 3;
    }

    switch (keyCode) {
      case KEY_PAUSE:
        if (shift || control)
          sendTelnetCommand((byte) 243); // BREAK
        break;
      case KEY_F1:
        writeSpecial(fmap[1]);
        break;
      case KEY_F2:
        writeSpecial(fmap[2]);
        break;
      case KEY_F3:
        writeSpecial(fmap[3]);
        break;
      case KEY_F4:
        writeSpecial(fmap[4]);
        break;
      case KEY_F5:
        writeSpecial(fmap[5]);
        break;
      case KEY_F6:
        writeSpecial(fmap[6]);
        break;
      case KEY_F7:
        writeSpecial(fmap[7]);
        break;
      case KEY_F8:
        writeSpecial(fmap[8]);
        break;
      case KEY_F9:
        writeSpecial(fmap[9]);
        break;
      case KEY_F10:
        writeSpecial(fmap[10]);
        break;
      case KEY_F11:
        writeSpecial(fmap[11]);
        break;
      case KEY_F12:
        writeSpecial(fmap[12]);
        break;
      case KEY_UP:
        writeSpecial(KeyUp[xind]);
        break;
      case KEY_DOWN:
        writeSpecial(KeyDown[xind]);
        break;
      case KEY_LEFT:
        writeSpecial(KeyLeft[xind]);
        break;
      case KEY_RIGHT:
        writeSpecial(KeyRight[xind]);
        break;
      case KEY_PAGE_DOWN:
        writeSpecial(NextScn[xind]);
        break;
      case KEY_PAGE_UP:
        writeSpecial(PrevScn[xind]);
        break;
      case KEY_INSERT:
        writeSpecial(Insert[xind]);
        break;
      case KEY_DELETE:
        writeSpecial(Remove[xind]);
        break;
      case KEY_BACK_SPACE:
        writeSpecial(BackSpace[xind]);
	if (localecho) {
	  if (BackSpace[xind] == "\b") {
	    putString("\b \b"); // make the last char 'deleted'
	  } else {
	    putString(BackSpace[xind]); // echo it
	  }
	}
        break;
      case KEY_HOME:
        writeSpecial(KeyHome[xind]);
        break;
      case KEY_END:
        writeSpecial(KeyEnd[xind]);
        break;
      case KEY_NUM_LOCK:
        if (vms && control) {
          writeSpecial(PF1);
        }
        if (!control)
          numlock = !numlock;
        break;
      case KEY_CAPS_LOCK:
        capslock = !capslock;
        return;
      case KEY_SHIFT:
      case KEY_CONTROL:
      case KEY_ALT:
        return;
      default:
        break;
    }
  }
/*
  public void keyReleased(KeyEvent evt) {
    if (debug > 1) debug("keyReleased("+evt+")");
    // ignore
  }
*/
  /**
   * Handle key Typed events for the terminal, this will get
   * all normal key types, but no shift/alt/control/numlock.
   */
  public void keyTyped(int keyCode, char keyChar, int modifiers) {
    boolean control = (modifiers & VDUInput.KEY_CONTROL) != 0;
    boolean shift = (modifiers & VDUInput.KEY_SHIFT) != 0;
    boolean alt = (modifiers & VDUInput.KEY_ALT) != 0;

    if (debug > 1) debug("keyTyped("+keyCode+", "+(int)keyChar+", "+modifiers+")");

    if (keyChar == '\t') {
      if (shift) {
        write(TabKey[1], false);
      } else {
        if (control) {
          write(TabKey[2], false);
        } else {
          if (alt) {
            write(TabKey[3], false);
          } else {
            write(TabKey[0], false);
          }
        }
      }
      return;
    }
    if (alt) {
      write(((char) (keyChar | 0x80)));
      return;
    }

    if (((keyCode == KEY_ENTER) || (keyChar == 10))
            && !control) {
      write('\r');
      if (localecho) putString("\r\n"); // bad hack
      return;
    }

    if ((keyCode == 10) && !control) {
      debug("Sending \\r");
      write('\r');
      return;
    }

    // FIXME: on german PC keyboards you have to use Alt-Ctrl-q to get an @,
    // so we can't just use it here... will probably break some other VMS
    // codes.  -Marcus
    // if(((!vms && keyChar == '2') || keyChar == '@' || keyChar == ' ')
    //    && control)
    if (((!vms && keyChar == '2') || keyChar == ' ') && control)
      write(0);

    if (vms) {
      if (keyChar == 127 && !control) {
        if (shift)
          writeSpecial(Insert[0]);        //  VMS shift delete = insert
        else
          writeSpecial(Remove[0]);        //  VMS delete = remove
        return;
      } else if (control)
        switch (keyChar) {
          case '0':
            writeSpecial(Numpad[0]);
            return;
          case '1':
            writeSpecial(Numpad[1]);
            return;
          case '2':
            writeSpecial(Numpad[2]);
            return;
          case '3':
            writeSpecial(Numpad[3]);
            return;
          case '4':
            writeSpecial(Numpad[4]);
            return;
          case '5':
            writeSpecial(Numpad[5]);
            return;
          case '6':
            writeSpecial(Numpad[6]);
            return;
          case '7':
            writeSpecial(Numpad[7]);
            return;
          case '8':
            writeSpecial(Numpad[8]);
            return;
          case '9':
            writeSpecial(Numpad[9]);
            return;
          case '.':
            writeSpecial(KPPeriod);
            return;
          case '-':
          case 31:
            writeSpecial(KPMinus);
            return;
          case '+':
            writeSpecial(KPComma);
            return;
          case 10:
            writeSpecial(KPEnter);
            return;
          case '/':
            writeSpecial(PF2);
            return;
          case '*':
            writeSpecial(PF3);
            return;
            /* NUMLOCK handled in keyPressed */
          default:
            break;
        }
      /* Now what does this do and how did it get here. -Marcus
      if (shift && keyChar < 32) {
        write(PF1+(char)(keyChar + 64));
        return;
      }
      */
    }

    // FIXME: not used?
    //String fmap[];
    int xind;
    xind = 0;
    //fmap = FunctionKey;
    if (shift) {
      //fmap = FunctionKeyShift;
      xind = 1;
    }
    if (control) {
      //fmap = FunctionKeyCtrl;
      xind = 2;
    }
    if (alt) {
      //fmap = FunctionKeyAlt;
      xind = 3;
    }

    if (keyCode == KEY_ESCAPE) {
      writeSpecial(Escape[xind]);
      return;
    }

    if ((modifiers & VDUInput.KEY_ACTION) != 0)
      switch (keyCode) {
        case KEY_NUMPAD0:
          writeSpecial(Numpad[0]);
          return;
        case KEY_NUMPAD1:
          writeSpecial(Numpad[1]);
          return;
        case KEY_NUMPAD2:
          writeSpecial(Numpad[2]);
          return;
        case KEY_NUMPAD3:
          writeSpecial(Numpad[3]);
          return;
        case KEY_NUMPAD4:
          writeSpecial(Numpad[4]);
          return;
        case KEY_NUMPAD5:
          writeSpecial(Numpad[5]);
          return;
        case KEY_NUMPAD6:
          writeSpecial(Numpad[6]);
          return;
        case KEY_NUMPAD7:
          writeSpecial(Numpad[7]);
          return;
        case KEY_NUMPAD8:
          writeSpecial(Numpad[8]);
          return;
        case KEY_NUMPAD9:
          writeSpecial(Numpad[9]);
          return;
        case KEY_DECIMAL:
          writeSpecial(NUMDot[xind]);
          return;
        case KEY_ADD:
          writeSpecial(NUMPlus[xind]);
          return;
      }

    if (!((keyChar == 8) || (keyChar == 127) || (keyChar == '\r') || (keyChar == '\n'))) {
      write(keyChar);
      return;
    }
  }

  private void handle_dcs(String dcs) {
    debugStr.append("DCS: ")
      .append(dcs);
    debug(debugStr.toString());
    debugStr.setLength(0);
  }

  private void handle_osc(String osc) {
	  if (osc.length() > 2 && osc.substring(0, 2).equals("4;")) {
			// Define color palette
			String[] colorData = osc.split(";");

			try {
				int colorIndex = Integer.parseInt(colorData[1]);

				if ("rgb:".equals(colorData[2].substring(0, 4))) {
					String[] rgb = colorData[2].substring(4).split("/");

					int red = Integer.parseInt(rgb[0].substring(0, 2), 16) & 0xFF;
					int green = Integer.parseInt(rgb[1].substring(0, 2), 16) & 0xFF;
					int blue = Integer.parseInt(rgb[2].substring(0, 2), 16) & 0xFF;
					display.setColor(colorIndex, red, green, blue);
				}
			} catch (Exception e) {
				debugStr.append("OSC: invalid color sequence encountered: ")
				  .append(osc);
				debug(debugStr.toString());
				debugStr.setLength(0);
			}
		} else
			debug("OSC: " + osc);
  }

  private final static char unimap[] = {
    //#
    //#    Name:     cp437_DOSLatinUS to Unicode table
    //#    Unicode version: 1.1
    //#    Table version: 1.1
    //#    Table format:  Format A
    //#    Date:          03/31/95
    //#    Authors:       Michel Suignard <michelsu@microsoft.com>
    //#                   Lori Hoerth <lorih@microsoft.com>
    //#    General notes: none
    //#
    //#    Format: Three tab-separated columns
    //#        Column #1 is the cp1255_WinHebrew code (in hex)
    //#        Column #2 is the Unicode (in hex as 0xXXXX)
    //#        Column #3 is the Unicode name (follows a comment sign, '#')
    //#
    //#    The entries are in cp437_DOSLatinUS order
    //#

    0x0000, // #NULL
    0x0001, // #START OF HEADING
    0x0002, // #START OF TEXT
    0x0003, // #END OF TEXT
    0x0004, // #END OF TRANSMISSION
    0x0005, // #ENQUIRY
    0x0006, // #ACKNOWLEDGE
    0x0007, // #BELL
    0x0008, // #BACKSPACE
    0x0009, // #HORIZONTAL TABULATION
    0x000a, // #LINE FEED
    0x000b, // #VERTICAL TABULATION
    0x000c, // #FORM FEED
    0x000d, // #CARRIAGE RETURN
    0x000e, // #SHIFT OUT
    0x000f, // #SHIFT IN
    0x0010, // #DATA LINK ESCAPE
    0x0011, // #DEVICE CONTROL ONE
    0x0012, // #DEVICE CONTROL TWO
    0x0013, // #DEVICE CONTROL THREE
    0x0014, // #DEVICE CONTROL FOUR
    0x0015, // #NEGATIVE ACKNOWLEDGE
    0x0016, // #SYNCHRONOUS IDLE
    0x0017, // #END OF TRANSMISSION BLOCK
    0x0018, // #CANCEL
    0x0019, // #END OF MEDIUM
    0x001a, // #SUBSTITUTE
    0x001b, // #ESCAPE
    0x001c, // #FILE SEPARATOR
    0x001d, // #GROUP SEPARATOR
    0x001e, // #RECORD SEPARATOR
    0x001f, // #UNIT SEPARATOR
    0x0020, // #SPACE
    0x0021, // #EXCLAMATION MARK
    0x0022, // #QUOTATION MARK
    0x0023, // #NUMBER SIGN
    0x0024, // #DOLLAR SIGN
    0x0025, // #PERCENT SIGN
    0x0026, // #AMPERSAND
    0x0027, // #APOSTROPHE
    0x0028, // #LEFT PARENTHESIS
    0x0029, // #RIGHT PARENTHESIS
    0x002a, // #ASTERISK
    0x002b, // #PLUS SIGN
    0x002c, // #COMMA
    0x002d, // #HYPHEN-MINUS
    0x002e, // #FULL STOP
    0x002f, // #SOLIDUS
    0x0030, // #DIGIT ZERO
    0x0031, // #DIGIT ONE
    0x0032, // #DIGIT TWO
    0x0033, // #DIGIT THREE
    0x0034, // #DIGIT FOUR
    0x0035, // #DIGIT FIVE
    0x0036, // #DIGIT SIX
    0x0037, // #DIGIT SEVEN
    0x0038, // #DIGIT EIGHT
    0x0039, // #DIGIT NINE
    0x003a, // #COLON
    0x003b, // #SEMICOLON
    0x003c, // #LESS-THAN SIGN
    0x003d, // #EQUALS SIGN
    0x003e, // #GREATER-THAN SIGN
    0x003f, // #QUESTION MARK
    0x0040, // #COMMERCIAL AT
    0x0041, // #LATIN CAPITAL LETTER A
    0x0042, // #LATIN CAPITAL LETTER B
    0x0043, // #LATIN CAPITAL LETTER C
    0x0044, // #LATIN CAPITAL LETTER D
    0x0045, // #LATIN CAPITAL LETTER E
    0x0046, // #LATIN CAPITAL LETTER F
    0x0047, // #LATIN CAPITAL LETTER G
    0x0048, // #LATIN CAPITAL LETTER H
    0x0049, // #LATIN CAPITAL LETTER I
    0x004a, // #LATIN CAPITAL LETTER J
    0x004b, // #LATIN CAPITAL LETTER K
    0x004c, // #LATIN CAPITAL LETTER L
    0x004d, // #LATIN CAPITAL LETTER M
    0x004e, // #LATIN CAPITAL LETTER N
    0x004f, // #LATIN CAPITAL LETTER O
    0x0050, // #LATIN CAPITAL LETTER P
    0x0051, // #LATIN CAPITAL LETTER Q
    0x0052, // #LATIN CAPITAL LETTER R
    0x0053, // #LATIN CAPITAL LETTER S
    0x0054, // #LATIN CAPITAL LETTER T
    0x0055, // #LATIN CAPITAL LETTER U
    0x0056, // #LATIN CAPITAL LETTER V
    0x0057, // #LATIN CAPITAL LETTER W
    0x0058, // #LATIN CAPITAL LETTER X
    0x0059, // #LATIN CAPITAL LETTER Y
    0x005a, // #LATIN CAPITAL LETTER Z
    0x005b, // #LEFT SQUARE BRACKET
    0x005c, // #REVERSE SOLIDUS
    0x005d, // #RIGHT SQUARE BRACKET
    0x005e, // #CIRCUMFLEX ACCENT
    0x005f, // #LOW LINE
    0x0060, // #GRAVE ACCENT
    0x0061, // #LATIN SMALL LETTER A
    0x0062, // #LATIN SMALL LETTER B
    0x0063, // #LATIN SMALL LETTER C
    0x0064, // #LATIN SMALL LETTER D
    0x0065, // #LATIN SMALL LETTER E
    0x0066, // #LATIN SMALL LETTER F
    0x0067, // #LATIN SMALL LETTER G
    0x0068, // #LATIN SMALL LETTER H
    0x0069, // #LATIN SMALL LETTER I
    0x006a, // #LATIN SMALL LETTER J
    0x006b, // #LATIN SMALL LETTER K
    0x006c, // #LATIN SMALL LETTER L
    0x006d, // #LATIN SMALL LETTER M
    0x006e, // #LATIN SMALL LETTER N
    0x006f, // #LATIN SMALL LETTER O
    0x0070, // #LATIN SMALL LETTER P
    0x0071, // #LATIN SMALL LETTER Q
    0x0072, // #LATIN SMALL LETTER R
    0x0073, // #LATIN SMALL LETTER S
    0x0074, // #LATIN SMALL LETTER T
    0x0075, // #LATIN SMALL LETTER U
    0x0076, // #LATIN SMALL LETTER V
    0x0077, // #LATIN SMALL LETTER W
    0x0078, // #LATIN SMALL LETTER X
    0x0079, // #LATIN SMALL LETTER Y
    0x007a, // #LATIN SMALL LETTER Z
    0x007b, // #LEFT CURLY BRACKET
    0x007c, // #VERTICAL LINE
    0x007d, // #RIGHT CURLY BRACKET
    0x007e, // #TILDE
    0x007f, // #DELETE
    0x00c7, // #LATIN CAPITAL LETTER C WITH CEDILLA
    0x00fc, // #LATIN SMALL LETTER U WITH DIAERESIS
    0x00e9, // #LATIN SMALL LETTER E WITH ACUTE
    0x00e2, // #LATIN SMALL LETTER A WITH CIRCUMFLEX
    0x00e4, // #LATIN SMALL LETTER A WITH DIAERESIS
    0x00e0, // #LATIN SMALL LETTER A WITH GRAVE
    0x00e5, // #LATIN SMALL LETTER A WITH RING ABOVE
    0x00e7, // #LATIN SMALL LETTER C WITH CEDILLA
    0x00ea, // #LATIN SMALL LETTER E WITH CIRCUMFLEX
    0x00eb, // #LATIN SMALL LETTER E WITH DIAERESIS
    0x00e8, // #LATIN SMALL LETTER E WITH GRAVE
    0x00ef, // #LATIN SMALL LETTER I WITH DIAERESIS
    0x00ee, // #LATIN SMALL LETTER I WITH CIRCUMFLEX
    0x00ec, // #LATIN SMALL LETTER I WITH GRAVE
    0x00c4, // #LATIN CAPITAL LETTER A WITH DIAERESIS
    0x00c5, // #LATIN CAPITAL LETTER A WITH RING ABOVE
    0x00c9, // #LATIN CAPITAL LETTER E WITH ACUTE
    0x00e6, // #LATIN SMALL LIGATURE AE
    0x00c6, // #LATIN CAPITAL LIGATURE AE
    0x00f4, // #LATIN SMALL LETTER O WITH CIRCUMFLEX
    0x00f6, // #LATIN SMALL LETTER O WITH DIAERESIS
    0x00f2, // #LATIN SMALL LETTER O WITH GRAVE
    0x00fb, // #LATIN SMALL LETTER U WITH CIRCUMFLEX
    0x00f9, // #LATIN SMALL LETTER U WITH GRAVE
    0x00ff, // #LATIN SMALL LETTER Y WITH DIAERESIS
    0x00d6, // #LATIN CAPITAL LETTER O WITH DIAERESIS
    0x00dc, // #LATIN CAPITAL LETTER U WITH DIAERESIS
    0x00a2, // #CENT SIGN
    0x00a3, // #POUND SIGN
    0x00a5, // #YEN SIGN
    0x20a7, // #PESETA SIGN
    0x0192, // #LATIN SMALL LETTER F WITH HOOK
    0x00e1, // #LATIN SMALL LETTER A WITH ACUTE
    0x00ed, // #LATIN SMALL LETTER I WITH ACUTE
    0x00f3, // #LATIN SMALL LETTER O WITH ACUTE
    0x00fa, // #LATIN SMALL LETTER U WITH ACUTE
    0x00f1, // #LATIN SMALL LETTER N WITH TILDE
    0x00d1, // #LATIN CAPITAL LETTER N WITH TILDE
    0x00aa, // #FEMININE ORDINAL INDICATOR
    0x00ba, // #MASCULINE ORDINAL INDICATOR
    0x00bf, // #INVERTED QUESTION MARK
    0x2310, // #REVERSED NOT SIGN
    0x00ac, // #NOT SIGN
    0x00bd, // #VULGAR FRACTION ONE HALF
    0x00bc, // #VULGAR FRACTION ONE QUARTER
    0x00a1, // #INVERTED EXCLAMATION MARK
    0x00ab, // #LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
    0x00bb, // #RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
    0x2591, // #LIGHT SHADE
    0x2592, // #MEDIUM SHADE
    0x2593, // #DARK SHADE
    0x2502, // #BOX DRAWINGS LIGHT VERTICAL
    0x2524, // #BOX DRAWINGS LIGHT VERTICAL AND LEFT
    0x2561, // #BOX DRAWINGS VERTICAL SINGLE AND LEFT DOUBLE
    0x2562, // #BOX DRAWINGS VERTICAL DOUBLE AND LEFT SINGLE
    0x2556, // #BOX DRAWINGS DOWN DOUBLE AND LEFT SINGLE
    0x2555, // #BOX DRAWINGS DOWN SINGLE AND LEFT DOUBLE
    0x2563, // #BOX DRAWINGS DOUBLE VERTICAL AND LEFT
    0x2551, // #BOX DRAWINGS DOUBLE VERTICAL
    0x2557, // #BOX DRAWINGS DOUBLE DOWN AND LEFT
    0x255d, // #BOX DRAWINGS DOUBLE UP AND LEFT
    0x255c, // #BOX DRAWINGS UP DOUBLE AND LEFT SINGLE
    0x255b, // #BOX DRAWINGS UP SINGLE AND LEFT DOUBLE
    0x2510, // #BOX DRAWINGS LIGHT DOWN AND LEFT
    0x2514, // #BOX DRAWINGS LIGHT UP AND RIGHT
    0x2534, // #BOX DRAWINGS LIGHT UP AND HORIZONTAL
    0x252c, // #BOX DRAWINGS LIGHT DOWN AND HORIZONTAL
    0x251c, // #BOX DRAWINGS LIGHT VERTICAL AND RIGHT
    0x2500, // #BOX DRAWINGS LIGHT HORIZONTAL
    0x253c, // #BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL
    0x255e, // #BOX DRAWINGS VERTICAL SINGLE AND RIGHT DOUBLE
    0x255f, // #BOX DRAWINGS VERTICAL DOUBLE AND RIGHT SINGLE
    0x255a, // #BOX DRAWINGS DOUBLE UP AND RIGHT
    0x2554, // #BOX DRAWINGS DOUBLE DOWN AND RIGHT
    0x2569, // #BOX DRAWINGS DOUBLE UP AND HORIZONTAL
    0x2566, // #BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL
    0x2560, // #BOX DRAWINGS DOUBLE VERTICAL AND RIGHT
    0x2550, // #BOX DRAWINGS DOUBLE HORIZONTAL
    0x256c, // #BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL
    0x2567, // #BOX DRAWINGS UP SINGLE AND HORIZONTAL DOUBLE
    0x2568, // #BOX DRAWINGS UP DOUBLE AND HORIZONTAL SINGLE
    0x2564, // #BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE
    0x2565, // #BOX DRAWINGS DOWN DOUBLE AND HORIZONTAL SINGLE
    0x2559, // #BOX DRAWINGS UP DOUBLE AND RIGHT SINGLE
    0x2558, // #BOX DRAWINGS UP SINGLE AND RIGHT DOUBLE
    0x2552, // #BOX DRAWINGS DOWN SINGLE AND RIGHT DOUBLE
    0x2553, // #BOX DRAWINGS DOWN DOUBLE AND RIGHT SINGLE
    0x256b, // #BOX DRAWINGS VERTICAL DOUBLE AND HORIZONTAL SINGLE
    0x256a, // #BOX DRAWINGS VERTICAL SINGLE AND HORIZONTAL DOUBLE
    0x2518, // #BOX DRAWINGS LIGHT UP AND LEFT
    0x250c, // #BOX DRAWINGS LIGHT DOWN AND RIGHT
    0x2588, // #FULL BLOCK
    0x2584, // #LOWER HALF BLOCK
    0x258c, // #LEFT HALF BLOCK
    0x2590, // #RIGHT HALF BLOCK
    0x2580, // #UPPER HALF BLOCK
    0x03b1, // #GREEK SMALL LETTER ALPHA
    0x00df, // #LATIN SMALL LETTER SHARP S
    0x0393, // #GREEK CAPITAL LETTER GAMMA
    0x03c0, // #GREEK SMALL LETTER PI
    0x03a3, // #GREEK CAPITAL LETTER SIGMA
    0x03c3, // #GREEK SMALL LETTER SIGMA
    0x00b5, // #MICRO SIGN
    0x03c4, // #GREEK SMALL LETTER TAU
    0x03a6, // #GREEK CAPITAL LETTER PHI
    0x0398, // #GREEK CAPITAL LETTER THETA
    0x03a9, // #GREEK CAPITAL LETTER OMEGA
    0x03b4, // #GREEK SMALL LETTER DELTA
    0x221e, // #INFINITY
    0x03c6, // #GREEK SMALL LETTER PHI
    0x03b5, // #GREEK SMALL LETTER EPSILON
    0x2229, // #INTERSECTION
    0x2261, // #IDENTICAL TO
    0x00b1, // #PLUS-MINUS SIGN
    0x2265, // #GREATER-THAN OR EQUAL TO
    0x2264, // #LESS-THAN OR EQUAL TO
    0x2320, // #TOP HALF INTEGRAL
    0x2321, // #BOTTOM HALF INTEGRAL
    0x00f7, // #DIVISION SIGN
    0x2248, // #ALMOST EQUAL TO
    0x00b0, // #DEGREE SIGN
    0x2219, // #BULLET OPERATOR
    0x00b7, // #MIDDLE DOT
    0x221a, // #SQUARE ROOT
    0x207f, // #SUPERSCRIPT LATIN SMALL LETTER N
    0x00b2, // #SUPERSCRIPT TWO
    0x25a0, // #BLACK SQUARE
    0x00a0, // #NO-BREAK SPACE
  };

  public char map_cp850_unicode(char x) {
    if (x >= 0x100)
      return x;
    return unimap[x];
  }

  private void _SetCursor(int row, int col) {
    int maxr = getRows();
    int tm = getTopMargin();

    R = (row < 0)?0:row;
    C = (col < 0)?0:col;

    if (!moveoutsidemargins) {
      R += tm;
      maxr = getBottomMargin();
    }
    if (R > maxr) R = maxr;
  }

  private void putChar(char c, boolean doshowcursor) {
    int rows = getRows(); //statusline
    int columns = getColumns();
    // byte msg[];

    if (debug > 4) {
      debugStr.append("putChar(")
        .append(c)
        .append(" [")
        .append((int) c)
        .append("]) at R=")
        .append(R)
        .append(" , C=")
        .append(C)
        .append(", columns=")
        .append(columns)
        .append(", rows=")
        .append(rows);
      debug(debugStr.toString());
      debugStr.setLength(0);
    }
    //markLine(R, 1);
    if (c > 255) {
      if (debug > 0)
        debug("char > 255:" + (int) c);
      //return;
    }


    switch (term_state) {
      case TSTATE_DATA:
        /* FIXME: we shouldn't use chars with bit 8 set if ibmcharset.
         * probably... but some BBS do anyway...
         */
        if (!useibmcharset) {
          boolean doneflag = true;
          switch (c) {
            case OSC:
              osc = "";
              term_state = TSTATE_OSC;
              break;
            case RI:
              if (R > getTopMargin())
                R--;
              else
                insertLine(R, 1, SCROLL_DOWN);
              if (debug > 1)
                debug("RI");
              break;
            case IND:
              if (debug > 2) {
                debugStr.append("IND at ")
                  .append(R)
                  .append(", tm is ")
                  .append(getTopMargin())
                  .append(", bm is ")
                  .append(getBottomMargin());
                debug(debugStr.toString());
                debugStr.setLength(0);
              }
              if (R == getBottomMargin() || R == rows - 1)
                insertLine(R, 1, SCROLL_UP);
              else
                R++;
              if (debug > 1)
                debug("IND (at " + R + " )");
              break;
            case NEL:
              if (R == getBottomMargin() || R == rows - 1)
                insertLine(R, 1, SCROLL_UP);
              else
                R++;
              C = 0;
              if (debug > 1)
                debug("NEL (at " + R + " )");
              break;
            case HTS:
              Tabs[C] = 1;
              if (debug > 1)
                debug("HTS");
              break;
            case DCS:
              dcs = "";
              term_state = TSTATE_DCS;
              break;
            default:
              doneflag = false;
              break;
          }
          if (doneflag) break;
        }
        switch (c) {
          case SS3:
            onegl = 3;
            break;
          case SS2:
            onegl = 2;
            break;
          case CSI: // should be in the 8bit section, but some BBS use this
            DCEvar = 0;
            DCEvars[0] = 0;
            DCEvars[1] = 0;
            DCEvars[2] = 0;
            DCEvars[3] = 0;
            term_state = TSTATE_CSI;
            break;
          case ESC:
            term_state = TSTATE_ESC;
            lastwaslf = 0;
            break;
          case 5: /* ENQ */
            write(answerBack, false);
            break;
          case 12:
            /* FormFeed, Home for the BBS world */
            deleteArea(0, 0, columns, rows, attributes);
            C = R = 0;
            break;
          case '\b': /* 8 */
            C--;
            if (C < 0)
              C = 0;
            lastwaslf = 0;
            break;
          case '\t':
            do {
              // Don't overwrite or insert! TABS are not destructive, but movement!
              C++;
            } while (C < columns && (Tabs[C] == 0));
            lastwaslf = 0;
            break;
          case '\r': // 13 CR
            C = 0;
            break;
          case '\n': // 10 LF
            if (debug > 3)
              debug("R= " + R + ", bm " + getBottomMargin() + ", tm=" + getTopMargin() + ", rows=" + rows);
            if (!vms) {
              if (lastwaslf != 0 && lastwaslf != c)   //  Ray: I do not understand this logic.
                break;
              lastwaslf = c;
              /*C = 0;*/
            }
            if (R == getBottomMargin() || R >= rows - 1)
              insertLine(R, 1, SCROLL_UP);
            else
              R++;
            break;
          case 7:
            beep();
            break;
          case '\016': /* SMACS , as */
            /* ^N, Shift out - Put G1 into GL */
            gl = 1;
            usedcharsets = true;
            break;
          case '\017': /* RMACS , ae */
            /* ^O, Shift in - Put G0 into GL */
            gl = 0;
            usedcharsets = true;
            break;
          default:
            {
              int thisgl = gl;

              if (onegl >= 0) {
                thisgl = onegl;
                onegl = -1;
              }
              lastwaslf = 0;
              if (c < 32) {
                if (c != 0)
                  if (debug > 0)
                    debug("TSTATE_DATA char: " + ((int) c));
                /*break; some BBS really want those characters, like hearst etc. */
                if (c == 0) /* print 0 ... you bet */
                  break;
              }
              if (C >= columns) {
                if (wraparound) {
                  int bot = rows;

                  // If we're in the scroll region, check against the bottom margin
                  if (R <= getBottomMargin() && R >= getTopMargin())
                    bot = getBottomMargin() + 1;

                  if (R < bot - 1)
                    R++;
                  else {
                    if (debug > 3) debug("scrolling due to wrap at " + R);
                    insertLine(R, 1, SCROLL_UP);
                  }
                  C = 0;
                } else {
                  // cursor stays on last character.
                  C = columns - 1;
                }
              }

              boolean mapped = false;

              // Mapping if DEC Special is chosen charset
              if (usedcharsets) {
                if (c >= '\u0020' && c <= '\u007f') {
                  switch (gx[thisgl]) {
                    case '0':
                      // Remap SCOANSI line drawing to VT100 line drawing chars
                      // for our SCO using customers.
                      if (terminalID.equals("scoansi") || terminalID.equals("ansi")) {
                        for (int i = 0; i < scoansi_acs.length(); i += 2) {
                          if (c == scoansi_acs.charAt(i)) {
                            c = scoansi_acs.charAt(i + 1);
                            break;
                          }
                        }
                      }
                      if (c >= '\u005f' && c <= '\u007e') {
                        c = DECSPECIAL[(short) c - 0x5f];
                        mapped = true;
                      }
                      break;
                    case '<': // 'user preferred' is currently 'ISO Latin-1 suppl
                      c = (char) ((c & 0x7f) | 0x80);
                      mapped = true;
                      break;
                    case 'A':
                    case 'B': // Latin-1 , ASCII -> fall through
                      mapped = true;
                      break;
                    default:
                      debug("Unsupported GL mapping: " + gx[thisgl]);
                      break;
                  }
                }
                if (!mapped && (c >= '\u0080' && c <= '\u00ff')) {
                  switch (gx[gr]) {
                    case '0':
                      if (c >= '\u00df' && c <= '\u00fe') {
                        c = DECSPECIAL[c - '\u00df'];
                        mapped = true;
                      }
                      break;
                    case '<':
                    case 'A':
                    case 'B':
                      mapped = true;
                      break;
                    default:
                      debug("Unsupported GR mapping: " + gx[gr]);
                      break;
                  }
                }
              }
              if (!mapped && useibmcharset)
                c = map_cp850_unicode(c);

              /*if(true || (statusmode == 0)) { */
              if (insertmode == 1) {
                insertChar(C, R, c, attributes);
              } else {
                putChar(C, R, c, attributes);
              }
              /*
                } else {
                if (insertmode==1) {
                insertChar(C, rows, c, attributes);
                } else {
                putChar(C, rows, c, attributes);
                }
                }
              */
              C++;
              break;
            }
        } /* switch(c) */
        break;
      case TSTATE_OSC:
        if ((c < 0x20) && (c != ESC)) {// NP - No printing character
          handle_osc(osc);
          term_state = TSTATE_DATA;
          break;
        }
        //but check for vt102 ESC \
        if (c == '\\' && osc.charAt(osc.length() - 1) == ESC) {
          handle_osc(osc);
          term_state = TSTATE_DATA;
          break;
        }
        osc = osc + c;
        break;
      case TSTATE_ESCSPACE:
        term_state = TSTATE_DATA;
        switch (c) {
          case 'F': /* S7C1T, Disable output of 8-bit controls, use 7-bit */
            output8bit = false;
            break;
          case 'G': /* S8C1T, Enable output of 8-bit control codes*/
            output8bit = true;
            break;
          default:
            debug("ESC <space> " + c + " unhandled.");
        }
        break;
      case TSTATE_ESC:
        term_state = TSTATE_DATA;
        switch (c) {
          case ' ':
            term_state = TSTATE_ESCSPACE;
            break;
          case '#':
            term_state = TSTATE_ESCSQUARE;
            break;
          case 'c':
            /* Hard terminal reset */
            reset();
            break;
          case '[':
            DCEvar = 0;
            DCEvars[0] = 0;
            DCEvars[1] = 0;
            DCEvars[2] = 0;
            DCEvars[3] = 0;
            term_state = TSTATE_CSI;
            break;
          case ']':
            osc = "";
            term_state = TSTATE_OSC;
            break;
          case 'P':
            dcs = "";
            term_state = TSTATE_DCS;
            break;
          case 'A': /* CUU */
            R--;
            if (R < 0) R = 0;
            break;
          case 'B': /* CUD */
            R++;
            if (R > rows - 1) R = rows - 1;
            break;
          case 'C':
            C++;
            if (C >= columns) C = columns - 1;
            break;
          case 'I': // RI
            insertLine(R, 1, SCROLL_DOWN);
            break;
          case 'E': /* NEL */
            if (R == getBottomMargin() || R == rows - 1)
              insertLine(R, 1, SCROLL_UP);
            else
              R++;
            C = 0;
            if (debug > 1)
              debug("ESC E (at " + R + ")");
            break;
          case 'D': /* IND */
            if (R == getBottomMargin() || R == rows - 1)
              insertLine(R, 1, SCROLL_UP);
            else
              R++;
            if (debug > 1)
              debug("ESC D (at " + R + " )");
            break;
          case 'J': /* erase to end of screen */
            if (R < rows - 1)
              deleteArea(0, R + 1, columns, rows - R - 1, attributes);
            if (C < columns - 1)
              deleteArea(C, R, columns - C, 1, attributes);
            break;
          case 'K':
            if (C < columns - 1)
              deleteArea(C, R, columns - C, 1, attributes);
            break;
          case 'M': // RI
            debug("ESC M : R is "+R+", tm is "+getTopMargin()+", bm is "+getBottomMargin());
            if (R > getTopMargin()) { // just go up 1 line.
              R--;
            } else { // scroll down
              insertLine(R, 1, SCROLL_DOWN);
            }
            /* else do nothing ; */
            if (debug > 2)
              debug("ESC M ");
            break;
          case 'H':
            if (debug > 1)
              debug("ESC H at " + C);
            /* right border probably ...*/
            if (C >= columns)
              C = columns - 1;
            Tabs[C] = 1;
            break;
          case 'N': // SS2
            onegl = 2;
            break;
          case 'O': // SS3
            onegl = 3;
            break;
          case '=':
            /*application keypad*/
            if (debug > 0)
              debug("ESC =");
            keypadmode = true;
            break;
          case '<': /* vt52 mode off */
            vt52mode = false;
            break;
          case '>': /*normal keypad*/
            if (debug > 0)
              debug("ESC >");
            keypadmode = false;
            break;
          case '7': /*save cursor, attributes, margins */
            Sc = C;
            Sr = R;
            Sgl = gl;
            Sgr = gr;
            Sa = attributes;
            Sgx = new char[4];
            for (int i = 0; i < 4; i++) Sgx[i] = gx[i];
            Stm = getTopMargin();
            Sbm = getBottomMargin();
            if (debug > 1)
              debug("ESC 7");
            break;
          case '8': /*restore cursor, attributes, margins */
            C = Sc;
            R = Sr;
            gl = Sgl;
            gr = Sgr;
            for (int i = 0; i < 4; i++) gx[i] = Sgx[i];
            setTopMargin(Stm);
            setBottomMargin(Sbm);
            attributes = Sa;
            if (debug > 1)
              debug("ESC 8");
            break;
          case '(': /* Designate G0 Character set (ISO 2022) */
            term_state = TSTATE_SETG0;
            usedcharsets = true;
            break;
          case ')': /* Designate G1 character set (ISO 2022) */
            term_state = TSTATE_SETG1;
            usedcharsets = true;
            break;
          case '*': /* Designate G2 Character set (ISO 2022) */
            term_state = TSTATE_SETG2;
            usedcharsets = true;
            break;
          case '+': /* Designate G3 Character set (ISO 2022) */
            term_state = TSTATE_SETG3;
            usedcharsets = true;
            break;
          case '~': /* Locking Shift 1, right */
            gr = 1;
            usedcharsets = true;
            break;
          case 'n': /* Locking Shift 2 */
            gl = 2;
            usedcharsets = true;
            break;
          case '}': /* Locking Shift 2, right */
            gr = 2;
            usedcharsets = true;
            break;
          case 'o': /* Locking Shift 3 */
            gl = 3;
            usedcharsets = true;
            break;
          case '|': /* Locking Shift 3, right */
            gr = 3;
            usedcharsets = true;
            break;
          case 'Y': /* vt52 cursor address mode , next chars are x,y */
            term_state = TSTATE_VT52Y;
            break;
          case '_':
          	term_state = TSTATE_TITLE;
          	break;
          case '\\':
          	// TODO save title
          	term_state = TSTATE_DATA;
          	break;
          default:
            debug("ESC unknown letter: " + c + " (" + ((int) c) + ")");
            break;
        }
        break;
      case TSTATE_VT52X:
        C = c - 37;
        term_state = TSTATE_VT52Y;
        break;
      case TSTATE_VT52Y:
        R = c - 37;
        term_state = TSTATE_DATA;
        break;
      case TSTATE_SETG0:
        if (c != '0' && c != 'A' && c != 'B' && c != '<')
          debug("ESC ( " + c + ": G0 char set?  (" + ((int) c) + ")");
        else {
          if (debug > 2) debug("ESC ( : G0 char set  (" + c + " " + ((int) c) + ")");
          gx[0] = c;
        }
        term_state = TSTATE_DATA;
        break;
      case TSTATE_SETG1:
        if (c != '0' && c != 'A' && c != 'B' && c != '<') {
          debug("ESC ) " + c + " (" + ((int) c) + ") :G1 char set?");
        } else {
          if (debug > 2) debug("ESC ) :G1 char set  (" + c + " " + ((int) c) + ")");
          gx[1] = c;
        }
        term_state = TSTATE_DATA;
        break;
      case TSTATE_SETG2:
        if (c != '0' && c != 'A' && c != 'B' && c != '<')
          debug("ESC*:G2 char set?  (" + ((int) c) + ")");
        else {
          if (debug > 2) debug("ESC*:G2 char set  (" + c + " " + ((int) c) + ")");
          gx[2] = c;
        }
        term_state = TSTATE_DATA;
        break;
      case TSTATE_SETG3:
        if (c != '0' && c != 'A' && c != 'B' && c != '<')
          debug("ESC+:G3 char set?  (" + ((int) c) + ")");
        else {
          if (debug > 2) debug("ESC+:G3 char set  (" + c + " " + ((int) c) + ")");
          gx[3] = c;
        }
        term_state = TSTATE_DATA;
        break;
      case TSTATE_ESCSQUARE:
        switch (c) {
          case '8':
            for (int i = 0; i < columns; i++)
              for (int j = 0; j < rows; j++)
                putChar(i, j, 'E', 0);
            break;
          default:
            debug("ESC # " + c + " not supported.");
            break;
        }
        term_state = TSTATE_DATA;
        break;
      case TSTATE_DCS:
        if (c == '\\' && dcs.charAt(dcs.length() - 1) == ESC) {
          handle_dcs(dcs);
          term_state = TSTATE_DATA;
          break;
        }
        dcs = dcs + c;
        break;

      case TSTATE_DCEQ:
        term_state = TSTATE_DATA;
        switch (c) {
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            DCEvars[DCEvar] = DCEvars[DCEvar] * 10 + (c) - 48;
            term_state = TSTATE_DCEQ;
            break;
          case ';':
            DCEvar++;
            DCEvars[DCEvar] = 0;
            term_state = TSTATE_DCEQ;
            break;
          case 's': // XTERM_SAVE missing!
            if (true || debug > 1)
              debug("ESC [ ? " + DCEvars[0] + " s unimplemented!");
            break;
          case 'r': // XTERM_RESTORE
            if (true || debug > 1)
              debug("ESC [ ? " + DCEvars[0] + " r");
            /* DEC Mode reset */
            for (int i = 0; i <= DCEvar; i++) {
              switch (DCEvars[i]) {
                case 3: /* 80 columns*/
                  setScreenSize(80, getRows(), true);
                  break;
                case 4: /* scrolling mode, smooth */
                  break;
                case 5: /* light background */
                  break;
                case 6: /* DECOM (Origin Mode) move inside margins. */
                  moveoutsidemargins = true;
                  break;
                case 7: /* DECAWM: Autowrap Mode */
                  wraparound = false;
                  break;
                case 12:/* local echo off */
                  break;
                case 9: 	/* X10 mouse */
                case 1000:	/* xterm style mouse report on */
                case 1001:
                case 1002:
                case 1003:
                  mouserpt = DCEvars[i];
                  break;
                default:
                  debug("ESC [ ? " + DCEvars[0] + " r, unimplemented!");
              }
            }
            break;
          case 'h': // DECSET
            if (debug > 0)
              debug("ESC [ ? " + DCEvars[0] + " h");
            /* DEC Mode set */
            for (int i = 0; i <= DCEvar; i++) {
              switch (DCEvars[i]) {
                case 1:  /* Application cursor keys */
                  KeyUp[0] = "\u001bOA";
                  KeyDown[0] = "\u001bOB";
                  KeyRight[0] = "\u001bOC";
                  KeyLeft[0] = "\u001bOD";
                  break;
                case 2: /* DECANM */
                  vt52mode = false;
                  break;
                case 3: /* 132 columns*/
                  setScreenSize(132, getRows(), true);
                  break;
                case 6: /* DECOM: move inside margins. */
                  moveoutsidemargins = false;
                  break;
                case 7: /* DECAWM: Autowrap Mode */
                  wraparound = true;
                  break;
                case 25: /* turn cursor on */
                  showCursor(true);
                  break;
                case 9: 	/* X10 mouse */
                case 1000:	/* xterm style mouse report on */
                case 1001:
                case 1002:
                case 1003:
                  mouserpt = DCEvars[i];
                  break;

                  /* unimplemented stuff, fall through */
                  /* 4  - scrolling mode, smooth */
                  /* 5  - light background */
                  /* 12 - local echo off */
                  /* 18 - DECPFF - Printer Form Feed Mode -> On */
                  /* 19 - DECPEX - Printer Extent Mode -> Screen */
                default:
                  debug("ESC [ ? " + DCEvars[0] + " h, unsupported.");
                  break;
              }
            }
            break;
          case 'i': // DEC Printer Control, autoprint, echo screenchars to printer
            // This is different to CSI i!
            // Also: "Autoprint prints a final display line only when the
            // cursor is moved off the line by an autowrap or LF, FF, or
            // VT (otherwise do not print the line)."
            switch (DCEvars[0]) {
              case 1:
                if (debug > 1)
                  debug("CSI ? 1 i : Print line containing cursor");
                break;
              case 4:
                if (debug > 1)
                  debug("CSI ? 4 i : Start passthrough printing");
                break;
              case 5:
                if (debug > 1)
                  debug("CSI ? 4 i : Stop passthrough printing");
                break;
            }
            break;
          case 'l':	//DECRST
            /* DEC Mode reset */
            if (debug > 0)
              debug("ESC [ ? " + DCEvars[0] + " l");
            for (int i = 0; i <= DCEvar; i++) {
              switch (DCEvars[i]) {
                case 1:  /* Application cursor keys */
                  KeyUp[0] = "\u001b[A";
                  KeyDown[0] = "\u001b[B";
                  KeyRight[0] = "\u001b[C";
                  KeyLeft[0] = "\u001b[D";
                  break;
                case 2: /* DECANM */
                  vt52mode = true;
                  break;
                case 3: /* 80 columns*/
                  setScreenSize(80, getRows(), true);
                  break;
                case 6: /* DECOM: move outside margins. */
                  moveoutsidemargins = true;
                  break;
                case 7: /* DECAWM: Autowrap Mode OFF */
                  wraparound = false;
                  break;
                case 25: /* turn cursor off */
                  showCursor(false);
                  break;
                  /* Unimplemented stuff: */
                  /* 4  - scrolling mode, jump */
                  /* 5  - dark background */
                  /* 7  - DECAWM - no wrap around mode */
                  /* 12 - local echo on */
                  /* 18 - DECPFF - Printer Form Feed Mode -> Off*/
                  /* 19 - DECPEX - Printer Extent Mode -> Scrolling Region */
                case 9: 	/* X10 mouse */
                case 1000:	/* xterm style mouse report OFF */
                case 1001:
                case 1002:
                case 1003:
                  mouserpt = 0;
                  break;
                default:
                  debug("ESC [ ? " + DCEvars[0] + " l, unsupported.");
                  break;
              }
            }
            break;
          case 'n':
            if (debug > 0)
              debug("ESC [ ? " + DCEvars[0] + " n");
            switch (DCEvars[0]) {
              case 15:
                /* printer? no printer. */
                write((ESC) + "[?13n", false);
                debug("ESC[5n");
                break;
              default:
                debug("ESC [ ? " + DCEvars[0] + " n, unsupported.");
                break;
            }
            break;
          default:
            debug("ESC [ ? " + DCEvars[0] + " " + c + ", unsupported.");
            break;
        }
        break;
      case TSTATE_CSI_EX:
        term_state = TSTATE_DATA;
        switch (c) {
          case ESC:
            term_state = TSTATE_ESC;
            break;
          default:
            debug("Unknown character ESC[! character is " + (int) c);
            break;
        }
        break;
      case TSTATE_CSI_TICKS:
        term_state = TSTATE_DATA;
        switch (c) {
          case 'p':
            debug("Conformance level: " + DCEvars[0] + " (unsupported)," + DCEvars[1]);
            if (DCEvars[0] == 61) {
              output8bit = false;
              break;
            }
            if (DCEvars[1] == 1) {
              output8bit = false;
            } else {
              output8bit = true; /* 0 or 2 */
            }
            break;
          default:
            debug("Unknown ESC [...  \"" + c);
            break;
        }
        break;
      case TSTATE_CSI_EQUAL:
        term_state = TSTATE_DATA;
        switch (c) {
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            DCEvars[DCEvar] = DCEvars[DCEvar] * 10 + (c) - 48;
            term_state = TSTATE_CSI_EQUAL;
            break;
          case ';':
            DCEvar++;
            DCEvars[DCEvar] = 0;
            term_state = TSTATE_CSI_EQUAL;
            break;

          case 'F': /* SCO ANSI foreground */
	  {
	    int newcolor;

            debug("ESC [ = "+DCEvars[0]+" F");

            attributes &= ~COLOR_FG;
	    newcolor =	((DCEvars[0] & 1) << 2)	|
	    		 (DCEvars[0] & 2)	|
	    		((DCEvars[0] & 4) >> 2) ;
            attributes |= (newcolor+1) << COLOR_FG_SHIFT;

	    break;
	  }
          case 'G': /* SCO ANSI background */
	  {
	    int newcolor;

            debug("ESC [ = "+DCEvars[0]+" G");

            attributes &= ~COLOR_BG;
	    newcolor =	((DCEvars[0] & 1) << 2)	|
	    		 (DCEvars[0] & 2)	|
	    		((DCEvars[0] & 4) >> 2) ;
            attributes |= (newcolor+1) << COLOR_BG_SHIFT;
	    break;
          }

          default:
            debugStr.append("Unknown ESC [ = ");
            for (int i=0;i<=DCEvar;i++) {
              debugStr.append(DCEvars[i])
                .append(',');
            }
            debugStr.append(c);
            debug(debugStr.toString());
            debugStr.setLength(0);
            break;
        }
        break;
      case TSTATE_CSI_DOLLAR:
        term_state = TSTATE_DATA;
        switch (c) {
          case '}':
            debug("Active Status Display now " + DCEvars[0]);
            statusmode = DCEvars[0];
            break;
            /* bad documentation?
               case '-':
               debug("Set Status Display now "+DCEvars[0]);
               break;
            */
          case '~':
            debug("Status Line mode now " + DCEvars[0]);
            break;
          default:
            debug("UNKNOWN Status Display code " + c + ", with Pn=" + DCEvars[0]);
            break;
        }
        break;
      case TSTATE_CSI:
        term_state = TSTATE_DATA;
        switch (c) {
          case '"':
            term_state = TSTATE_CSI_TICKS;
            break;
          case '$':
            term_state = TSTATE_CSI_DOLLAR;
            break;
          case '=':
            term_state = TSTATE_CSI_EQUAL;
            break;
          case '!':
            term_state = TSTATE_CSI_EX;
            break;
          case '?':
            DCEvar = 0;
            DCEvars[0] = 0;
            term_state = TSTATE_DCEQ;
            break;
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            DCEvars[DCEvar] = DCEvars[DCEvar] * 10 + (c) - 48;
            term_state = TSTATE_CSI;
            break;
          case ';':
            DCEvar++;
            DCEvars[DCEvar] = 0;
            term_state = TSTATE_CSI;
            break;
          case 'c':/* send primary device attributes */
            /* send (ESC[?61c) */

            String subcode = "";
            if (terminalID.equals("vt320")) subcode = "63;";
            if (terminalID.equals("vt220")) subcode = "62;";
            if (terminalID.equals("vt100")) subcode = "61;";
            write((ESC) + "[?" + subcode + "1;2c", false);
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " c");
            break;
          case 'q':
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " q");
            break;
          case 'g':
            /* used for tabsets */
            switch (DCEvars[0]) {
              case 3:/* clear them */
                Tabs = new byte[getColumns()];
                break;
              case 0:
                Tabs[C] = 0;
                break;
            }
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " g");
            break;
          case 'h':
            switch (DCEvars[0]) {
              case 4:
                insertmode = 1;
                break;
              case 20:
                debug("Setting CRLF to TRUE");
                sendcrlf = true;
                break;
              default:
                debug("unsupported: ESC [ " + DCEvars[0] + " h");
                break;
            }
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " h");
            break;
          case 'i': // Printer Controller mode.
            // "Transparent printing sends all output, except the CSI 4 i
            //  termination string, to the printer and not the screen,
            //  uses an 8-bit channel if no parity so NUL and DEL will be
            //  seen by the printer and by the termination recognizer code,
            //  and all translation and character set selections are
            //  bypassed."
            switch (DCEvars[0]) {
              case 0:
                if (debug > 1)
                  debug("CSI 0 i:  Print Screen, not implemented.");
                break;
              case 4:
                if (debug > 1)
                  debug("CSI 4 i:  Enable Transparent Printing, not implemented.");
                break;
              case 5:
                if (debug > 1)
                  debug("CSI 4/5 i:  Disable Transparent Printing, not implemented.");
                break;
              default:
                debug("ESC [ " + DCEvars[0] + " i, unimplemented!");
            }
            break;
          case 'l':
            switch (DCEvars[0]) {
              case 4:
                insertmode = 0;
                break;
              case 20:
                debug("Setting CRLF to FALSE");
                sendcrlf = false;
                break;
              default:
                debug("ESC [ " + DCEvars[0] + " l, unimplemented!");
                break;
            }
            break;
          case 'A': // CUU
            {
              int limit;
              /* FIXME: xterm only cares about 0 and topmargin */
              if (R >= getTopMargin()) {
                limit = getTopMargin();
              } else
                limit = 0;
              if (DCEvars[0] == 0)
                R--;
              else
                R -= DCEvars[0];
              if (R < limit)
                R = limit;
              if (debug > 1)
                debug("ESC [ " + DCEvars[0] + " A");
              break;
            }
          case 'B':	// CUD
            /* cursor down n (1) times */
            {
              int limit;
              if (R <= getBottomMargin()) {
                limit = getBottomMargin();
              } else
                limit = rows - 1;
              if (DCEvars[0] == 0)
                R++;
              else
                R += DCEvars[0];
              if (R > limit)
                R = limit;
              else {
                if (debug > 2) debug("Not limited.");
              }
              if (debug > 2) debug("to: " + R);
              if (debug > 1)
                debug("ESC [ " + DCEvars[0] + " B (at C=" + C + ")");
              break;
            }
          case 'C':
            if (DCEvars[0] == 0)
              C++;
            else
              C += DCEvars[0];
            if (C > columns - 1)
              C = columns - 1;
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " C");
            break;
          case 'd': // CVA
            R = DCEvars[0];
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " d");
            break;
          case 'D':
            if (DCEvars[0] == 0)
              C--;
            else
              C -= DCEvars[0];
            if (C < 0) C = 0;
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " D");
            break;
          case 'r': // DECSTBM
            if (DCEvar > 0)   //  Ray:  Any argument is optional
            {
              R = DCEvars[1] - 1;
              if (R < 0)
                R = rows - 1;
              else if (R >= rows) {
                R = rows - 1;
              }
            } else
              R = rows - 1;
            int bot = R;
            if (R >= DCEvars[0]) {
              R = DCEvars[0] - 1;
              if (R < 0)
                R = 0;
            }
            setMargins(R, bot);
            _SetCursor(0, 0);
            if (debug > 1)
              debug("ESC [" + DCEvars[0] + " ; " + DCEvars[1] + " r");
            break;
          case 'G':  /* CUP  / cursor absolute column */
            C = DCEvars[0];
            if (debug > 1) debug("ESC [ " + DCEvars[0] + " G");
            break;
          case 'H':  /* CUP  / cursor position */
            /* gets 2 arguments */
            _SetCursor(DCEvars[0] - 1, DCEvars[1] - 1);
            if (debug > 2) {
              debug("ESC [ " + DCEvars[0] + ";" + DCEvars[1] + " H, moveoutsidemargins " + moveoutsidemargins);
              debug("	-> R now " + R + ", C now " + C);
            }
            break;
          case 'f':  /* move cursor 2 */
            /* gets 2 arguments */
            R = DCEvars[0] - 1;
            C = DCEvars[1] - 1;
            if (C < 0) C = 0;
            if (R < 0) R = 0;
            if (debug > 2)
              debug("ESC [ " + DCEvars[0] + ";" + DCEvars[1] + " f");
            break;
          case 'S': /* ind aka 'scroll forward' */
            if (DCEvars[0] == 0)
              insertLine(rows - 1, SCROLL_UP);
            else
              insertLine(rows - 1, DCEvars[0], SCROLL_UP);
            break;
          case 'L':
            /* insert n lines */
            if (DCEvars[0] == 0)
              insertLine(R, SCROLL_DOWN);
            else
              insertLine(R, DCEvars[0], SCROLL_DOWN);
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + "" + (c) + " (at R " + R + ")");
            break;
          case 'T': /* 'ri' aka scroll backward */
            if (DCEvars[0] == 0)
              insertLine(0, SCROLL_DOWN);
            else
              insertLine(0, DCEvars[0], SCROLL_DOWN);
            break;
          case 'M':
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + "" + (c) + " at R=" + R);
            if (DCEvars[0] == 0)
              deleteLine(R);
            else
              for (int i = 0; i < DCEvars[0]; i++)
                deleteLine(R);
            break;
          case 'K':
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " K");
            /* clear in line */
            switch (DCEvars[0]) {
              case 6: /* 97801 uses ESC[6K for delete to end of line */
              case 0:/*clear to right*/
                if (C < columns - 1)
                  deleteArea(C, R, columns - C, 1, attributes);
                break;
              case 1:/*clear to the left, including this */
                if (C > 0)
                  deleteArea(0, R, C + 1, 1, attributes);
                break;
              case 2:/*clear whole line */
                deleteArea(0, R, columns, 1, attributes);
                break;
            }
            break;
          case 'J':
            /* clear below current line */
            switch (DCEvars[0]) {
              case 0:
                if (R < rows - 1)
                  deleteArea(0, R + 1, columns, rows - R - 1, attributes);
                if (C < columns - 1)
                  deleteArea(C, R, columns - C, 1, attributes);
                break;
              case 1:
                if (R > 0)
                  deleteArea(0, 0, columns, R, attributes);
                if (C > 0)
                  deleteArea(0, R, C + 1, 1, attributes);// include up to and including current
                break;
              case 2:
                deleteArea(0, 0, columns, rows, attributes);
                break;
            }
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " J");
            break;
          case '@':
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " @");
            for (int i = 0; i < DCEvars[0]; i++)
              insertChar(C, R, ' ', attributes);
            break;
          case 'X':
            {
              int toerase = DCEvars[0];
              if (debug > 1)
                debug("ESC [ " + DCEvars[0] + " X, C=" + C + ",R=" + R);
              if (toerase == 0)
                toerase = 1;
              if (toerase + C > columns)
                toerase = columns - C;
              deleteArea(C, R, toerase, 1, attributes);
              // does not change cursor position
              break;
            }
          case 'P':
            if (debug > 1)
              debug("ESC [ " + DCEvars[0] + " P, C=" + C + ",R=" + R);
            if (DCEvars[0] == 0) DCEvars[0] = 1;
            for (int i = 0; i < DCEvars[0]; i++)
              deleteChar(C, R);
            break;
          case 'n':
            switch (DCEvars[0]) {
              case 5: /* malfunction? No malfunction. */
                writeSpecial((ESC) + "[0n");
                if (debug > 1)
                  debug("ESC[5n");
                break;
              case 6:
                // DO NOT offset R and C by 1! (checked against /usr/X11R6/bin/resize
                // FIXME check again.
                // FIXME: but vttest thinks different???
                writeSpecial((ESC) + "[" + R + ";" + C + "R");
                if (debug > 1)
                  debug("ESC[6n");
                break;
              default:
                if (debug > 0)
                  debug("ESC [ " + DCEvars[0] + " n??");
                break;
            }
            break;
          case 's':  /* DECSC - save cursor */
            Sc = C;
            Sr = R;
            Sa = attributes;
            if (debug > 3)
              debug("ESC[s");
            break;
          case 'u': /* DECRC - restore cursor */
            C = Sc;
            R = Sr;
            attributes = Sa;
            if (debug > 3)
              debug("ESC[u");
            break;
          case 'm':  /* attributes as color, bold , blink,*/
            if (debug > 3)
              debug("ESC [ ");
            if (DCEvar == 0 && DCEvars[0] == 0)
              attributes = 0;
            for (int i = 0; i <= DCEvar; i++) {
              switch (DCEvars[i]) {
                case 0:
                  if (DCEvar > 0) {
                    if (terminalID.equals("scoansi")) {
                      attributes &= COLOR; /* Keeps color. Strange but true. */
                    } else {
                      attributes = 0;
                    }
                  }
                  break;
                case 1:
                  attributes |= BOLD;
                  attributes &= ~LOW;
                  break;
                case 2:
                  /* SCO color hack mode */
                  if (terminalID.equals("scoansi") && ((DCEvar - i) >= 2)) {
                    int ncolor;
                    attributes &= ~(COLOR | BOLD);

                    ncolor = DCEvars[i + 1];
                    if ((ncolor & 8) == 8)
                      attributes |= BOLD;
                    ncolor = ((ncolor & 1) << 2) | (ncolor & 2) | ((ncolor & 4) >> 2);
                    attributes |= ((ncolor) + 1) << COLOR_FG_SHIFT;
                    ncolor = DCEvars[i + 2];
                    ncolor = ((ncolor & 1) << 2) | (ncolor & 2) | ((ncolor & 4) >> 2);
                    attributes |= ((ncolor) + 1) << COLOR_BG_SHIFT;
                    i += 2;
                  } else {
                    attributes |= LOW;
                  }
                  break;
                case 3: /* italics */
            	  attributes |= INVERT;
            	  break;
                case 4:
                  attributes |= UNDERLINE;
                  break;
                case 7:
                  attributes |= INVERT;
                  break;
                case 8:
                  attributes |= INVISIBLE;
                  break;
                case 5: /* blink on */
                  break;
                  /* 10 - ANSI X3.64-1979, select primary font, don't display control
                   *      chars, don't set bit 8 on output */
                case 10:
                  gl = 0;
                  usedcharsets = true;
                  break;
                  /* 11 - ANSI X3.64-1979, select second alt. font, display control
                   *      chars, set bit 8 on output */
                case 11: /* SMACS , as */
                case 12:
                  gl = 1;
                  usedcharsets = true;
                  break;
                case 21: /* normal intensity */
                  attributes &= ~(LOW | BOLD);
                  break;
                case 23: /* italics off */
                  attributes &= ~INVERT;
                  break;
                case 25: /* blinking off */
                  break;
                case 27:
                  attributes &= ~INVERT;
                  break;
                case 28:
                  attributes &= ~INVISIBLE;
                  break;
                case 24:
                  attributes &= ~UNDERLINE;
                  break;
                case 22:
                  attributes &= ~BOLD;
                  break;
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36:
                case 37:
                  attributes &= ~COLOR_FG;
                  attributes |= ((DCEvars[i] - 30) + 1)<< COLOR_FG_SHIFT;
                  break;
                case 38:
                  if (DCEvars[i+1] == 5) {
                    attributes &= ~COLOR_FG;
                    attributes |= ((DCEvars[i + 2]) + 1) << COLOR_FG_SHIFT;
                    i += 2;
                  }
                  break;
                case 39:
                  attributes &= ~COLOR_FG;
                  break;
                case 40:
                case 41:
                case 42:
                case 43:
                case 44:
                case 45:
                case 46:
                case 47:
                  attributes &= ~COLOR_BG;
                  attributes |= ((DCEvars[i] - 40) + 1) << COLOR_BG_SHIFT;
                  break;
                case 48:
                  if (DCEvars[i+1] == 5) {
                    attributes &= ~COLOR_BG;
                    attributes |= (DCEvars[i + 2] + 1) << COLOR_BG_SHIFT;
                    i += 2;
                  }
                  break;
                case 49:
                  attributes &= ~COLOR_BG;
                  break;
                case 90:
                case 91:
                case 92:
                case 93:
                case 94:
                case 95:
                case 96:
                case 97:
                  attributes &= ~COLOR_FG;
                  attributes |= ((DCEvars[i] - 82) + 1) << COLOR_FG_SHIFT;
                  break;
                case 100:
                case 101:
                case 102:
                case 103:
                case 104:
                case 105:
                case 106:
                case 107:
                  attributes &= ~COLOR_BG;
                  attributes |= ((DCEvars[i] - 92) + 1) << COLOR_BG_SHIFT;
                  break;

                default:
                  debugStr.append("ESC [ ")
                    .append(DCEvars[i])
                    .append(" m unknown...");
                  debug(debugStr.toString());
                  debugStr.setLength(0);
                  break;
              }
              if (debug > 3) {
                debugStr.append(DCEvars[i])
                  .append(';');
                debug(debugStr.toString());
                debugStr.setLength(0);
              }
            }
            if (debug > 3) {
              debugStr.append(" (attributes = ")
                .append(attributes)
                .append(")m");
              debug(debugStr.toString());
              debugStr.setLength(0);
            }
            break;
          default:
            debugStr.append("ESC [ unknown letter: ")
              .append(c)
              .append(" (")
              .append((int)c)
              .append(')');
            debug(debugStr.toString());
            debugStr.setLength(0);
            break;
        }
        break;
      case TSTATE_TITLE:
      	switch (c) {
      	  case ESC:
      		term_state = TSTATE_ESC;
      		break;
      	  default:
      		// TODO save title
      		break;
      	}
      	break;
      default:
        term_state = TSTATE_DATA;
        break;
    }

    if (C > columns)
      C = columns;
    else if (C < 0)
      C = 0;

    if (R > rows)
      R = rows;
    else if (R < 0)
      R = 0;

    if (doshowcursor)
      setCursorPosition(C, R);
    //markLine(R, 1);
  }

  /* hard reset the terminal */
  public void reset() {
    gx[0] = 'B';
    gx[1] = 'B';
    gx[2] = 'B';
    gx[3] = 'B';

    gl = 0;  // default GL to G0
    gr = 2;  // default GR to G2

    onegl = -1; // Single shift override

    /* reset tabs */
    int nw = getColumns();
    if (nw < 132) nw = 132;
    Tabs = new byte[nw];
    for (int i = 0; i < nw; i += 8) {
      Tabs[i] = 1;
    }

    deleteArea(0, 0, getColumns(), getRows(), attributes);
    setMargins(0, getRows());
    C = R = 0;
    _SetCursor(0, 0);

    if (display != null)
    	display.resetColors();

    showCursor(true);
    /*FIXME:*/
    term_state = TSTATE_DATA;
  }
}