summaryrefslogblamecommitdiffstats
path: root/private/os2/os2ses/kbdrqust.c
blob: 3c17e9332cc616c465a8fdb37d8835dfbb43f3af (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046













































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                   
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    kbdrqust.c

Abstract:

    This module contains the Kbd requests thread and
    the handler for Kbd requests.

Author:

    Michael Jarus (mjarus) 23-Oct-1991

Environment:

    User Mode Only

Revision History:

--*/

#define WIN32_ONLY
#include "os2ses.h"
#include "trans.h"
#include "event.h"
#include "kbd.h"
#include "os2win.h"
#include <stdio.h>

#define OS2_SCAN_INSERT_0         0x52
#define OS2_SCAN_END_1            0x4F
#define OS2_SCAN_CTRL_END_1       0x75
#define OS2_SCAN_DOWN_2           0x50
#define OS2_SCAN_LEFT_4           0x4B
#define OS2_SCAN_CTRL_LEFT_4      0x73
#define OS2_SCAN_RIGHT_6          0x4D
#define OS2_SCAN_CTRL_RIGHT_6     0x74
#define OS2_SCAN_HOME_7           0x47
#define OS2_SCAN_CTRL_HOME_7      0x77
#define OS2_SCAN_UP_8             0x48
#define OS2_SCAN_DEL              0x53

DWORD
Ow2GetOs2KbdEventIntoQueue();

DWORD
Ow2VioUpdateCurPos(
    IN  COORD  CurPos
    );

DWORD
Ow2VioReadCurPos(
    );

DWORD
Ow2VioReadCurType();

VOID
KbdSetTable(
    IN ULONG KbdCP
    );

#if DBG
BYTE KbdEchoCharToConsoleStr[] = "KbdEchoCharToConsole";
BYTE KbdEchoCharStr[] = "KbdEchoChar";
BYTE KbdEchoStringToConsoleStr[] = "KbdEchoStringToConsole";
BYTE KbdEchoAStringStr[] = "KbdEchoAString";
BYTE KbdEchoBSAndFillSpacesStr[] = "KbdEchoBSAndFillSpaces";
BYTE KbdEchoESCStr[] = "KbdEchoESC";
BYTE KbdCueMoveToRightStr[] = "KbdCueMoveToRight";
#endif

#define KEYBOARD_NEW_MASK (USHORT)(KEYBOARD_2B_TURNAROUND | KEYBOARD_SHIFT_REPORT)
#define KEYBOARD_ECHO     (USHORT)(KEYBOARD_ECHO_ON | KEYBOARD_ECHO_OFF)
#define KEYBOARD_INPUT    (USHORT)(KEYBOARD_ASCII_MODE | KEYBOARD_BINARY_MODE)

#define KEYBOARD_BINARY_SHIFT (KEYBOARD_BINARY_MODE | KEYBOARD_SHIFT_REPORT)
#define MODE_BINARY_SHIFT     (SHIFT_REPORT_MODE | BINARY_MODE)

#define KEYBOARD_SHIFT_ASCII_INPUT    (USHORT)(KEYBOARD_ASCII_MODE | KEYBOARD_SHIFT_REPORT)
#define KEYBOARD_INPUT_RESERVED                                                      \
    ~( KEYBOARD_ECHO_ON | KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE |                 \
       KEYBOARD_ASCII_MODE | KEYBOARD_MODIFY_STATE | KEYBOARD_MODIFY_INTERIM |       \
       KEYBOARD_MODIFY_TURNAROUND | KEYBOARD_2B_TURNAROUND | KEYBOARD_SHIFT_REPORT )

#define KbdBuffSize 256
#define KBD_BUFFER_ADDRESS ((PUCHAR)KbdAddress)     // used for ECHO to console
#define KBD_BUFFER_SIZE    OS2_KBD_PORT_MSG_SIZE

#define ALPHANUM_CHAR(BuffIndex)                                \
        ((((Char = LineInputBuff[BuffIndex].Char) >= '0') &&    \
                           (Char <= '9')) ||                    \
         ((Char >= 'a') && (Char <= 'z')) ||                    \
         ((Char >= 'A') && (Char <= 'Z')))                      \

#define GET_CURRENT_COORD (SesGrp->WinCoord)

typedef struct  _LINE_EDIT_KBD
{
    UCHAR       Char;
    SHORT       X_Pos;      // Used as X Coordinate for edit
                            // Used as offset for CUE
} LINE_EDIT_KBD;

typedef enum
{
    CharMode,
    AsciiMode,
    BinaryMode
} KBDMODE;

//CONSOLE_SCREEN_BUFFER_INFO KbdConsoleInfo;
KBDMODE     KbdState;               // state for HandleKeyboardInput
ULONG       KbdWaitFlag;            // flags for KbdCheckPackage and
                                    //    arg to GetKeyboardInput
/* (from event.h)
                                   CharIn    Peek    String    Read
                                   ------    ----    ------    ----
   WAIT_MASK             0x01     User    NOWAIT    User     WAIT  (0 IO_WAIT, 1 IO_NOWAIT)
   ENABLE_SHIFT_KEY      0x02   <if SHIFT report>
   ENABLE_NON_ASCII_KEY  0x04      +        +        <if BINARY>
   ENABLE_LN_EDITOR_KEY  0x08                        <if ASCII (for KEYS OFF)>
   ENABLE_KEYS_ON_KEY    0x10                        <if ASCII and KEYS ON>
 */

#define  ENABLE_SHIFT_KEY      0x02   /* enable shift report keys (and break for them) */

/*
 *  for Command line editing
 */

PUCHAR      KbdCueBuffer = NULL;    // 64K CUE buffer for KEYS ON
                                    // This is a cyclic bufffer and all
                                    // pointers are index of type USHORT.
BOOL        KbdLineWasEdited;       // TRUE if user edited the current line
USHORT      KbdNextLinePointer;     // where to put next input line
USHORT      KbdCurrentLine;         // index of current line in KbdCueBuffer
USHORT      KbdIndexInLine;         // index of current char in input line
BOOL        KbdCueUpRowIsCurrent;   // flag set after CR to indicate UP arrow
                                    // returns the current line

/*
 * for GetOs2KbdRead
 */

USHORT      KbdBuffNextPtr;         // when string typed was longer the size (ASCII read only)
                                    // it points to the remain string
USHORT      KbdBuffNextLen;         // remaining bytes in the string

/*
 * for for HandleKeyboardInput.CharMode:
 */

BOOL        KbdPeekFlag;            // called by CharIn (0) or Peek(1)

/*
 * for for HandleKeyboardInput.Ascii/BinaryMode:
 */

USHORT      KbdInputLength;         // the current string length (num of chars
                                    // already in input buffer for the user)
ULONG       KbdMaxLength;           // maximun input length (String.cb
                                    // for StringIn, KbdBuffSize for Read)
USHORT      KbdLength;              // On enter: the requested length,
                                    // On exit: the returned string length
USHORT      KbdEndLength;           // length of Turn Around string

/*
 * for for HandleKeyboardInput.AsciiMode:
 */

LINE_EDIT_KBD   LineInputBuff[KbdBuffSize];  // the current input buffer
UCHAR       KbdLastBuff[KbdBuffSize];   // holds the previous "Buff"
USHORT      KbdLastBuffPtr;         // points to the char in KbdLastBuff to take (if F1)
USHORT      LastStringLength;       // length of the previous "Buff"
USHORT      KbdEditFlag;            // support EDIT line (F1 .. F4, ->, <-, Ins, Del)
USHORT      KbdReadMode;            // called by StringIn (0) or Read(1)
USHORT      KbdFxWaitForChar;       // waiting for char after F2(1) or F4(2)
BOOL        KbdEchoFlag;            // set if ECHO ON
BOOL        KbdInsertOn;            // INSERT key current state (1 - ON)
BOOL        KbdEchoString;          // copy string from last buffer (F2 and F3)
USHORT      KbdTurnAroundChar;      // turn around char to look for
BOOLEAN     KbdTurnAroundCharTwo;   // 2-byte turn around char (KEYBOARD_2B_TURNAROUND)
USHORT      KbdDelIndex;
SHORT       KbdFirstColumn;         // first column for this input request
SHORT       KbdStartOfLine;         // first column on this line

BOOLEAN     KbdSetupTurnAroundCharTwo;  // KbsSetStatus of KbdTurnAroundCharTwo:
                                        // before new read KbdTurnAroundCharTwo
                                        // is updated from this.

#ifdef JAPAN
// MSKK May.07.1993 V-AkihiS
/*
 * for KBDGetKbdType
 */
extern USHORT KbdType, KbdSubType;
extern BYTE OemId, OemSubType;
#endif

/*
 * internal functions
 */

DWORD
GetOs2KbdKey(
    IN  BOOL        PeekFlag,
    IN  USHORT      WaitFlag,
    OUT PKBDKEYINFO KeyInfo,
    IN  PVOID       pMsg,
    OUT PULONG      pReply
    );

DWORD
GetOs2KbdString(
    IN     USHORT       WaitFlag,
    IN OUT PKBDREQUEST  PReq,
    IN     PVOID        pMsg,
    OUT    PULONG       pReply
    );

DWORD
GetOs2KbdRead(
    IN PULONG   Length,
    IN  PVOID   pMsg,
    OUT PULONG  pReply
    );

VOID
KbdNewSetup(
    IN PKBDINFO LastSetup
    );

DWORD
GetOs2KbdStringRead(
    IN      USHORT  WaitFlag,
    IN      USHORT  EditFlag,
    IN      USHORT  ReadMode,
    IN      ULONG   MaxLength,
    IN OUT  PUSHORT Length,
    IN      PVOID   pMsg,
    OUT     PULONG  pReply
    );

DWORD
HandleKeyboardInput(
    IN  PKEYEVENTINFO   pKbd
    );

DWORD
Ow2KbdSetStatus(
    IN  PKBDREQUEST     PReq
    );

BOOL
KbdKeyIsTurnAround(
    IN  UCHAR   Char,
    IN  UCHAR   Scan
    );

/*
 *   Echo functions for KbdStringIn/Read (ASCII)
 */

DWORD
KbdEchoNL(
    IN ULONG    Count
    );

DWORD
KbdEchoESC(
    IN ULONG    Count,
    IN ULONG    HorzMove
    );

DWORD
KbdEchoBSAndFillSpaces(
    IN ULONG    BSCount,
    IN ULONG    SpaceCount
    );

DWORD
KbdEchoBeep(
    IN ULONG    Count
    );

DWORD
KbdEchoChar(
    IN UCHAR    Char,
    IN ULONG    Count
    );

DWORD
KbdEchoAString(
    IN PUCHAR   String,
    IN ULONG    Length
    );

/*
 *  CUE functions
 */

VOID
KbdCueEraseAndDisplayLine(
    IN ULONG    NewLineIndex
    );

VOID
KbdCueMoveToRight(
    IN  ULONG   StartIndex,
    IN  ULONG   StringLength,
    IN  ULONG   UpdateLVB
    );

VOID
KbdCueMoveToLeft(
    IN  ULONG   StartIndex,
    IN  ULONG   MoveLength
    );

VOID
KbdCueUpdateBufferOffset(
    IN  ULONG   StartIndex,
    IN  ULONG   StringLength,
    IN  ULONG   StartOffset
    );

VOID
KbdCueDeleteCharAndShift(
    IN  ULONG   StartIndex,
    IN  ULONG   NumChar
    );

VOID
KbdCueHandleChar(
    IN  UCHAR       Char,
    IN  ULONG       Count
    );

VOID
KbdCueSetCurTypeToHalf();

VOID
KbdCueSetCurTypeToQuater();


DWORD
KbdInit(IN VOID)
{
    KBDINFO     LastSetup;

    if (InitQueue(&KbdQueue))
    {
        return(TRUE);
    }

    KbdMonQueue = PhyKbdQueue = KbdQueue;

    KbdQueue->Setup.cb = sizeof(KBDINFO);
    KbdQueue->Setup.fsMask = KEYBOARD_ECHO_ON | KEYBOARD_ASCII_MODE;
    KbdQueue->Setup.chTurnAround = 0x0D;  /* default: <ENTER>/<CR> */
    KbdQueue->Setup.fsInterim = 0;
    KbdQueue->Setup.fsState = 0;
    KbdQueue->bNlsShift = 0;
    KbdQueue->LastKeyFlag = FALSE;

    LastSetup.fsMask = KEYBOARD_BINARY_MODE;
    KbdNewSetup(&LastSetup);

    KbdAddress = ((PCHAR)Os2SessionCtrlDataBaseAddress) + KBD_OFFSET;

    Ow2KbdXlateVars.OtherFlags = InterruptTime;

    return(FALSE);
}


DWORD
KbdInitAfterSesGrp(IN VOID)
{
    if (SesGrp->KeysOnFlag)
    {
        KbdCueBuffer = HeapAlloc(HandleHeap, 0, 64 * 1024);
        if ( KbdCueBuffer == NULL )
        {
#if DBG
            KdPrint(("OS2SES(KbdRequset): unable to allocate Cue buffer\n"));
#endif
            return ERROR_KBD_NO_MORE_HANDLE;
        }
    }
    return(FALSE);
}


BOOL
ServeKbdRequest(IN  PKBDREQUEST     PReq,
                OUT PVOID           PStatus,
                IN  PVOID           pMsg,
                OUT PULONG          pReply)
{
    DWORD               Rc;
    KBDINFO             LastSetup;
    PKEY_EVENT_QUEUE    TempKbdQueue;
    KEYEVENTINFO        In;

    Rc = 0;
    TempKbdQueue = (PKEY_EVENT_QUEUE) PReq->hKbd;

#if DBG
    if (( PReq->Request != KBDNewFocus ) &&
        ( PReq->Request != KBDNewCountry ) &&
        ( PReq->Request != KBDOpen ) &&
        (KbdQueue != TempKbdQueue))
    {
        KdPrint(("OS2SES(KbdRequest-%u): illegal handle\n",
                PReq->Request));
    }
#endif

    switch (PReq->Request)
    {
        case KBDOpen:
            if((Rc = InitQueue(&TempKbdQueue)) == NO_ERROR)
            {
                PReq->hKbd = TempKbdQueue;
            }
            break;

        case KBDClose:
            if (( TempKbdQueue != PhyKbdQueue) && TempKbdQueue->Count )
            {
                if ( !--TempKbdQueue->Count )
                {
                    HeapFree(HandleHeap, 0,
                             (LPSTR)  TempKbdQueue->MonHdr.MemoryStartAddress);
                }
            }
            break;

        case KBDDupLogHandle:
            if (TempKbdQueue != PhyKbdQueue)
            {
                TempKbdQueue->Count++ ;
            }
            break;

        case KBDReadStdIn:
            if (!hStdInConsoleType)
            {
#if DBG
                KdPrint(("OS2SES(KbdRequest-KbdReadStdIn): illegal request\n"));
#endif
                Rc = ERROR_INVALID_HANDLE;
                break;
            }
            // falls into KBDRead

        case KBDRead:
            Rc = GetOs2KbdRead(&PReq->Length,
                               pMsg,
                               pReply);
            break;


        case KBDCharIn:
            Rc = GetOs2KbdKey(0,                /* CharIn/Peek flag */
                              (USHORT)PReq->fWait,
                              &PReq->d.KeyInfo,
                              pMsg,
                              pReply);
            break;

        case KBDStringIn:
            Rc = GetOs2KbdString((USHORT)PReq->fWait,
                                 PReq,
                                 pMsg,
                                 pReply);
            break;

        case KBDPeek:
            Rc = GetOs2KbdKey(1,                /* CharIn/Peek flag */
                              IO_NOWAIT,
                              &PReq->d.KeyInfo,
                              pMsg,
                              pReply);
            break;

        case KBDFlushBuffer:
            In.wRepeatCount = 0x7FFF;
            while (TRUE)
            {
                KbdQueue->LastKeyFlag = FALSE;
                KbdQueue->Out == KbdQueue->In;

                Rc = GetKeyboardInput( IO_NOWAIT,
                                       &In,
                                       pMsg,
                                       pReply);

                if ( !Rc )                          /* no char & NO_WAIT */
                   break;
            }
            break;

        case KBDGetStatus:
            if (PReq->d.KbdInfo.cb < 10 )
            {   Rc = ERROR_KBD_INVALID_LENGTH;
                break;
            }
            Ow2GetOs2KbdEventIntoQueue();
            PReq->d.KbdInfo = KbdQueue->Setup;
#ifdef DBCS
// MSKK May.18.1992 KazuM
            GetNlsMode(&KbdQueue->Setup);
#endif
            PReq->d.KbdInfo.fsState &= KBDINFO_STATE_MASK;
            break;

        case KBDSetStatus:
            Rc = Ow2KbdSetStatus(PReq);
            break;

        case KBDFreeFocus:
            LastSetup = KbdQueue->Setup;
            KbdQueue = PhyKbdQueue;
            NewKbdQueue(KbdQueue);
            KbdNewSetup(&LastSetup);
            break;

        case KBDNewFocus:
            LastSetup = KbdQueue->Setup;
            KbdQueue = TempKbdQueue;
            NewKbdQueue(KbdQueue);
            KbdNewSetup(&LastSetup);
            break;

// BUGBUG?? - do the following requests working on the current/last-focused
//           logical keyboard, on the physical keyboard or on a logical
//            keyboard passed as a parameter

        case KBDGetInputMode:
            switch (KbdQueue->Setup.fsMask & KEYBOARD_BINARY_SHIFT)
            {
                case KEYBOARD_BINARY_SHIFT:
                    PReq->d.InputMode = MODE_BINARY_SHIFT;
                    break;

                case KEYBOARD_BINARY_MODE:
                    PReq->d.InputMode = BINARY_MODE;
                    break;

                default:
                    PReq->d.InputMode = ASCII_MODE;
                    break;
            }
            break;

        case KBDGetInterimFlag:
#ifdef DBCS
// MSKK May.18.1992 KazuM
            GetNlsMode(&KbdQueue->Setup);
#endif
            PReq->d.Interim = (UCHAR)(KbdQueue->Setup.fsInterim &
                                      (CONVERSION_REQUEST | INTERIM_CHAR));
            break;

        case KBDGetKbdType:
#ifdef JAPAN
// MSKK May.07.1993 V-AkihiS
            if (KbdType == KBD_TYPE_JAPAN && OemId == SUB_KBD_TYPE_MICROSOFT)
            {
                switch(OemSubType)
                {
                    case MICROSOFT_KBD_101_TYPE:
                        PReq->d.KbdType[0] = 0x0001;
                        PReq->d.KbdType[1] = 0x0000;
                        PReq->d.KbdType[2] = 0x0000;
                        break;
                    case MICROSOFT_KBD_AX_TYPE:
                        PReq->d.KbdType[0] = 0x0010;
                        PReq->d.KbdType[1] = 0x0000;
                        PReq->d.KbdType[2] = 0x0001;
                        break;
                    case MICROSOFT_KBD_106_TYPE:
                        PReq->d.KbdType[0] = 0x0001;
                        PReq->d.KbdType[1] = 0x82B3;
                        PReq->d.KbdType[2] = 0x0032;
                        break;
                    case MICROSOFT_KBD_002_TYPE:
                        PReq->d.KbdType[0] = 0x0006;
                        PReq->d.KbdType[1] = 0x82B3;
                        PReq->d.KbdType[2] = 0x0032;
                        break;
                    default:
                        PReq->d.KbdType[0] = 0x0001;
                        PReq->d.KbdType[1] = 0x0000;
                        PReq->d.KbdType[2] = 0x0000;
#if DBG
                        KdPrint(("OS2SES(Kbd) This keyboard isn't supported yet.\n"));
#endif
                }
            } else
            {
                PReq->d.KbdType[0] = 0x0001;
                PReq->d.KbdType[1] = 0x0000;
                PReq->d.KbdType[2] = 0x0000;
#if DBG
                KdPrint(("OS2SES(Kbd) This keyboard isn't supported yet.\n"));
#endif
            }
#else
            PReq->d.KbdType = 0x0001;  // BUGBUG
#endif
            break;

        case KBDGetHotKey:
            // BUGBUG   PReq->d.HotKey =
    /*

    if ((*pParm != HOTKEY_MAX_COUNT) &&
        (*pParm != HOTKEY_CURRENT_COUNT))
    {
    }
#define HOTKEY_MAX_COUNT    0x0000
#define HOTKEY_CURRENT_COUNT    0x0001
     */

            break;

        case KBDGetShiftState:
#ifdef DBCS
// MSKK Oct.20.1992 V-AkihiS
            GetNlsMode(&KbdQueue->Setup);
            PReq->d.Shift.fNLS = HIBYTE(KbdQueue->Setup.fsInterim);
#else
            PReq->d.Shift.fNLS = KbdQueue->bNlsShift;
#endif
            PReq->d.Shift.fsState = KbdQueue->Setup.fsState;
            break;

        case KBDSetInputMode:
            LastSetup = KbdQueue->Setup;
            KbdQueue->Setup.fsMask = (USHORT)
                ((KbdQueue->Setup.fsMask & ~(KEYBOARD_INPUT | KEYBOARD_SHIFT_REPORT)) |
                ((PReq->d.InputMode  & SHIFT_REPORT_MODE) ? KEYBOARD_SHIFT_REPORT : 0) |
                ((PReq->d.InputMode & BINARY_MODE ) ?
                    KEYBOARD_BINARY_MODE : KEYBOARD_ASCII_MODE));
            KbdNewSetup(&LastSetup);
            break;

        case KBDSetShiftState:
            LastSetup = KbdQueue->Setup;
            KbdQueue->Setup.fsState = PReq->d.Shift.fsState;
            // BUGBUG : SHIFTSTATE.fbNLS       PReq->d.Shift.fNLS
#ifdef DBCS
// MSKK Oct.20.1992 V-AkihiS
            KbdQueue->Setup.fsInterim = MAKEWORD(
                                            KbdQueue->Setup.fsInterim,
                                            PReq->d.Shift.fNLS
                                           );
            SetNlsMode(KbdQueue->Setup);
#endif
            KbdNewSetup(&LastSetup);
            break;

        case KBDSetTypamaticRate:
            Rc = !SystemParametersInfo(
                        SPI_SETKEYBOARDDELAY,
                        PReq->d.RateDelay.usDelay,
                        NULL,
                        0);
            if (!Rc)
            {
                Rc = !SystemParametersInfo(
                            SPI_SETKEYBOARDSPEED,
                            PReq->d.RateDelay.usRate,
                            NULL,
                            0
                           );
            }
            break;

        case KBDSetInTerimFlag:
            LastSetup = KbdQueue->Setup;
#ifdef DBCS
// MSKK Mar.03.1993 V-AkihiS
            KbdQueue->Setup.fsInterim = MAKEWORD(
                                            PReq->d.Interim,
                                            HIBYTE(KbdQueue->Setup.fsInterim)
                                           );
            SetNlsMode(KbdQueue->Setup);
#else
            KbdQueue->Setup.fsInterim = (USHORT)PReq->d.Interim;
#endif
            KbdNewSetup(&LastSetup);
            break;

        case KBDGetCp:
            PReq->d.CodePage = SesGrp->KbdCP;
            break;

        case KBDSetCp:
            Rc = KbdNewCp(PReq->d.CodePage);
            break;

        case KBDNewCountry:
            SesGrp->KeyboardCountry = PReq->d.CodePage; //Hack - Not Really CodePage
            KbdSetTable(SesGrp->KbdCP);
            *pReply = 0;

            break;

        case KBDXlate:
            /*
            {
                KBD_XLATE_VARS  XlateVars;
                KBD_MON_PACKAGE KeyInfo[3];
                PKBDTRANS       pKbdXlate = &PReq->d.KbdTrans;

                RtlZeroMemory(&XlateVars, sizeof(KBD_XLATE_VARS));
                if (pKbdXlate->fsShift != 0)
                {
                    // continue translation

                    //? = pKbdXlate->fsShift;
                }

                RtlZeroMemory(&Os2KeyInfo[0], 3 * sizeof(KBD_MON_PACKAGE));

                //KeyInfo[0].MonitorFlag = pKbdXlate->...;
                //KeyInfo[0].DeviceFlag = pKbdXlate->...;
                KeyInfo[0].KeyInfo.chChar = pKbdXlate->chChar;
                KeyInfo[0].KeyInfo.chScan = pKbdXlate->chScan;
                KeyInfo[0].KeyInfo.fbStatus = pKbdXlate->fbStatus;
                KeyInfo[0].KeyInfo.bNlsShift = pKbdXlate->bNlsShift;
                KeyInfo[0].KeyInfo.fsState = pKbdXlate->fsState;
                KeyInfo[0].KeyBoardFlag = pKbdXlate->fsDD;

                KeyInfo[0].KeyInfo.fbStatus = 0x40;

                //? XlateVars.XlateFlags |= SecPrefix;        // Have seen E0 prefix

                Rc = Ow2KbdXlate(
                                 pKbdXlate->chScan,             // ScanCode,
                                 &XlateVars,                    // pFlagArea,
                                 &KeyInfo[0],                   // pMonitorPack,
                                 Ow2KbdScanTable                // pTransTable
                                );

                //? which packet - KeyInfo[?]
                pKbdXlate->chChar = KeyInfo[0].KeyInfo.chChar;
                pKbdXlate->chScan = KeyInfo[0].KeyInfo.chScan;
                pKbdXlate->fbStatus = KeyInfo[0].KeyInfo.fbStatus;
                pKbdXlate->bNlsShift = KeyInfo[0].KeyInfo.bNlsShift;
                pKbdXlate->fsState = KeyInfo[0].KeyInfo.fsState;
                pKbdXlate->fsDD = KeyInfo[0].KeyBoardFlag;
                //? pKbdXlate->fsXlate = ;
                //? pKbdXlate->fsShift = ;
            }
                */
        case KBDSetCustXt:
                /*  from ???               */
        default:
            Rc = (DWORD)-1L; //STATUS_INVALID_PARAMETER;
#if DBG
            IF_OD2_DEBUG2( KBD, OS2_EXE )
                KdPrint(("OS2SES(KbdRequest): Unknown Kbd request = %X\n", PReq->Request));
#endif
    }

    if ( Rc == 1 )
    {
       Rc = GetLastError();
    }

    *(PDWORD) PStatus = Rc;
    return(TRUE);  // Continue
}


DWORD
Ow2KbdSetStatus(IN  PKBDREQUEST     PReq)
{
    KBDINFO         LastSetup, *pKbdSetup;
    USHORT          KbdMask, Mask;

    /*
     *  check legalty of parameters:
     *
     *    1. sizeof structure
     *    2. reserved bits
     *    3. mutuex bits (ECHO_ON & ECHO_OFF, ASCII & BINARY, ASCII & SHIFT)
     */

    LastSetup = KbdQueue->Setup;
    if (PReq->d.KbdInfo.cb != 10 )
    {
        return ERROR_KBD_INVALID_LENGTH;
    }

    KbdMask = PReq->d.KbdInfo.fsMask;
    pKbdSetup = &KbdQueue->Setup;

    if (KbdMask & KEYBOARD_INPUT_RESERVED)
    {
        return ERROR_KBD_INVALID_INPUT_MASK;
    }

    if ((KbdMask & KEYBOARD_ECHO ) == KEYBOARD_ECHO)
    {
        return ERROR_KBD_INVALID_ECHO_MASK;
    }

    if ((KbdMask & KEYBOARD_INPUT) == KEYBOARD_INPUT)
    {
        return ERROR_KBD_INVALID_INPUT_MASK;
    }

    if ((KbdMask & KEYBOARD_SHIFT_ASCII_INPUT) == KEYBOARD_SHIFT_ASCII_INPUT)
    {
        return ERROR_KBD_INVALID_INPUT_MASK;
    }

    /*
     *  check modified bits and set the flags according:
     *
     *    1. STATE      - set fsState
     *    2. INTERIM    - set fsInterim
     *    3. TURNAROUND - set chTurnAround
     */

#ifdef DBCS
// MSKK Jul.1993 V-AKihiS
    if (KbdMask & KEYBOARD_MODIFY_STATE)
    {
        pKbdSetup->fsState = PReq->d.KbdInfo.fsState;
        pKbdSetup->fsInterim = MAKEWORD(
                                   LOBYTE(pKbdSetup->fsInterim),
                                   HIBYTE(PReq->d.KbdInfo.fsInterim)
                                  );
        SetNlsMode(*pKbdSetup);
    }

    if (KbdMask & KEYBOARD_MODIFY_INTERIM)
        pKbdSetup->fsInterim = MAKEWORD(
                                   LOBYTE(PReq->d.KbdInfo.fsInterim),
                                   HIBYTE(pKbdSetup->fsInterim)
                                  );
#else
    if (KbdMask & KEYBOARD_MODIFY_STATE)
        pKbdSetup->fsState = PReq->d.KbdInfo.fsState;

    if (KbdMask & KEYBOARD_MODIFY_INTERIM)
        pKbdSetup->fsInterim = PReq->d.KbdInfo.fsInterim;
#endif

    if (KbdMask & KEYBOARD_MODIFY_TURNAROUND)
        pKbdSetup->chTurnAround = PReq->d.KbdInfo.chTurnAround;

    /*
     *  update mask:
     *
     *    1. update mask of new bits SHIFT_REPORT & 2B_TURNAROUND
     *    2. if new mask includes new ECHO then update mask
     *    3. if new mask includes new INPUT then update mask
     */

    Mask = KEYBOARD_NEW_MASK;

    if (KbdMask & KEYBOARD_ECHO)
    {
        Mask |= KEYBOARD_ECHO;
    }

    if (KbdMask & KEYBOARD_INPUT)
    {
        Mask |= KEYBOARD_INPUT;
    }

    pKbdSetup->fsMask =
                (pKbdSetup->fsMask & ~Mask) | (KbdMask & Mask);

    KbdNewSetup(&LastSetup);

    return (0L);
}


DWORD
GetOs2KbdKey( IN  BOOL        PeekFlag,
              IN  USHORT      WaitFlag,
              OUT PKBDKEYINFO KeyInfo,
              IN  PVOID       pMsg,
              OUT PULONG      pReply)
/*++

Routine Description:

    This routine get kbd char for KbdCharIn and KbdPeek

Arguments:

    PeekFlag - TRUE if peek (0 - CharIn, 1 - Peek)

    WaitFlag - Wait if no input (IO_WAIT or IO_NOWAIT)

    KeyInfo - Where to return the key input record

    pMsg - Pointer to the LPC message

    pReply - Pointer to flag if return reply on LPC

Return Value:


Note:

    If no event and IO_WAIT: save pMsg, put 0 into *pReply and
    the reply is postponed.

--*/
{

    DWORD           Rc;
    KEYEVENTINFO    In;


    RtlZeroMemory(KeyInfo, sizeof(KBDKEYINFO));
    KeyInfo->fsState = 1;

    if ( KbdQueue->Setup.fsMask & KEYBOARD_ASCII_MODE )
    {
        KbdAsciiMode = 1;
    } else
    {
        KbdAsciiMode = 0;
        if ((KbdQueue->Setup.fsMask & KEYBOARD_BINARY_SHIFT) == KEYBOARD_BINARY_SHIFT)
        {
            WaitFlag |= ENABLE_SHIFT_KEY;
        }
    }

    WaitFlag |= ENABLE_NON_ASCII_KEY;

    KbdState = CharMode;
    KbdWaitFlag = (ULONG)WaitFlag;
    KbdPeekFlag = PeekFlag;

    for(;;)
    {
        if (KbdQueue->LastKeyFlag)
        {
            In = KbdQueue->LastKey;
            KbdQueue->LastKeyFlag = FALSE;
        } else
        {
            In.wRepeatCount = 0x7FFF;
            Rc = GetKeyboardInput( KbdWaitFlag,
                                   &In,
                                   pMsg,
                                   pReply);

            if ( !Rc )                          /* no char & (NO_WAIT or postponed reply) */
               return(Rc);
        }

        if (( HandleKeyboardInput(
                                 &In
                                 )) == NO_ERROR )
        {
            *KeyInfo = In.KeyInfo[0].KeyInfo;
            return(0L);
        }
    }
}


DWORD
GetOs2KbdString( IN     USHORT       WaitFlag,
                 //IN OUT KBDRW        *Length,
                 IN OUT PKBDREQUEST  PReq,
                 IN     PVOID        pMsg,
                 OUT    PULONG       pReply)
{

    DWORD           Rc;
    USHORT          EditFlag = 0;
    STRINGINBUF     String = PReq->d.String;

    KbdAsciiMode = (BOOL)(( KbdQueue->Setup.fsMask & KEYBOARD_ASCII_MODE ) ? 1 : 0);

    if ( String.cchIn && ( String.cchIn <= String.cb ) &&
         ( KbdLastBuff[String.cchIn] == '\r' ))
    {
        EditFlag = 1;
    }

    String.cchIn = String.cb;

    Rc = GetOs2KbdStringRead( WaitFlag,
                              EditFlag,
                              0,
                              (ULONG)String.cb,
                              &String.cchIn,
                              pMsg,
                              pReply);
    if ( pReply )
    {
        PReq->Length = (ULONG)KbdLength;
        String.cchIn = KbdLength;
        if ( String.cchIn != String.cb )
            PReq->Length++ ;             // ASCII mode - copy the CR
    }

    return(Rc);

}


DWORD
GetOs2KbdRead(IN    PULONG  Length,
              IN    PVOID   pMsg,
              OUT   PULONG  pReply)
{
    DWORD           Rc, i;

    KbdAsciiMode = (BOOL)(( KbdQueue->Setup.fsMask & KEYBOARD_ASCII_MODE ) ? 1 : 0);

    /*
     *  ignore previously-typed-but-not-returned character in binary mode
     */

    if ( !KbdAsciiMode || !KbdBuffNextLen)
    {

        Rc = GetOs2KbdStringRead( IO_WAIT,
                                  TRUE,
                                  1,
                                  KbdBuffSize - 2,
                                  (PUSHORT)Length,
                                  pMsg,
                                  pReply);

        return(Rc);

    }

    /*
     *
     * Copy from kbd buffer
     *
     */

    if (*Length > (ULONG)KbdBuffNextLen)
        *Length = KbdBuffNextLen;

    for (i = 0 ; i < *Length ; i++ )
    {
        if ((KBD_BUFFER_ADDRESS[i] = KbdLastBuff[i + KbdBuffNextPtr]) == '\n')
        {
            *Length = i + 1;
            break;
        }
    }

    KbdBuffNextLen -= *((PUSHORT)Length);
    KbdBuffNextPtr += (USHORT)*Length;

    return(0L);
}


DWORD
GetOs2KbdStringRead(IN      USHORT  WaitFlag,
                    IN      USHORT  EditFlag,
                    IN      USHORT  ReadMode,
                    IN      ULONG   MaxLength,
                    IN OUT  PUSHORT Length,
                    IN      PVOID   pMsg,
                    OUT     PULONG  pReply)
/*++

Routine Description:

    This routine get kbd string for KbdStringIn and Read("KBD$" or non-redirected
    STD-IN).

Arguments:

    WaitFlag - Wait if no input (IO_WAIT or IO_NOWAIT)

    EditFlag - TRUE if edit last line(0/1 according to cchIn - StringIn, 1 - Read)

    ReadMode - Called by StringIn (0) or Read(1)

    MaxLength - Maximun input length (String.cb for StringIn, KbdBuffSize
        for Read)

    Length - On enter: the requested length, on exit: the returned string
        length (String.cchIn for StringIn, Length field in LPC for Read)

    pMsg - Pointer to the LPC message

    pReply - Pointer to flag if return reply on LPC

Return Value:


Note:

    If no event and IO_WAIT: save pMsg, put 0 into *pReply and
    the reply is postponed.

--*/
{
    KEYEVENTINFO    In;
    DWORD           Rc;

    KbdLastBuffPtr = KbdInputLength = KbdFxWaitForChar = KbdDelIndex = 0;
    KbdIndexInLine = 0;
    KbdInsertOn = KbdEchoString = KbdLineWasEdited = FALSE;
    //KbdSecondTurnAround = KbdEndFlag = FALSE;
    KbdLength = *Length;

    if ( KbdAsciiMode )
    {
        /*
         *   ASCII  mode
         */

        KbdWaitFlag = (ULONG)(WaitFlag | ENABLE_LN_EDITOR_KEY);

        /*
         *
         * Get Start Cursor-Position
         *
         */

        // force SesGrp->WinCoord and CurType params to get updated

        Ow2VioReadCurPos();
        Ow2VioReadCurType();

        if (SesGrp->KeysOnFlag)
        {
            KbdWaitFlag |= ENABLE_KEYS_ON_KEY;
            KbdCueSetCurTypeToQuater();
        }
        KbdMaxLength = MaxLength - 1;    /* leave space for the TurnAround char at the end */
        KbdEndLength = 1;
        KbdReadMode = ReadMode;

        KbdEchoFlag = (BOOL)(( KbdQueue->Setup.fsMask & KEYBOARD_ECHO_ON ) ? 1 : 0);
        KbdTurnAroundChar = KbdQueue->Setup.chTurnAround;
        KbdTurnAroundCharTwo = KbdSetupTurnAroundCharTwo;

        //if (KbdQueue->Setup.chTurnAround & 0x80)
        if(KbdTurnAroundCharTwo)
        {   KbdMaxLength--;          /* need another space for 2-byte-TurnAround */
            KbdEndLength++;
        }

        KbdEditFlag = EditFlag;

        LineInputBuff[KbdInputLength].X_Pos = KbdStartOfLine = KbdFirstColumn =
                GET_CURRENT_COORD.X;

        KbdState = AsciiMode;

    } else
    {
        KbdWaitFlag = (ULONG)(WaitFlag | ENABLE_NON_ASCII_KEY);
        KbdState = BinaryMode;
    }

    for(;;)
    {
        /*
         *
         * Restore saved Input Key if any
         *
         */

        if (KbdQueue->LastKeyFlag)
        {
            In = KbdQueue->LastKey;
            KbdQueue->LastKeyFlag = FALSE;
        } else
        {
            /*
             *
             * Get Input Key if any
             *
             */

            In.wRepeatCount = 0x7FFF;
            Rc = GetKeyboardInput( KbdWaitFlag,
                                   &In,
                                   pMsg,
                                   pReply);

            if ( !Rc )                          /* no char & NO_WAIT */
               return(Rc);
        }

        if (( HandleKeyboardInput(
                                 &In
                                 )) == NO_ERROR )
        {
            *Length = KbdLength;
            return(0L);
        }
    }
}


DWORD
HandleKeyboardInput(IN  PKEYEVENTINFO   pKbd)
{
    DWORD           NumChar = 0;
    UCHAR           Char, Scan, *puchLastString;
    USHORT          i, usIndex, StringLengthToEcho, NumBeep = 0;
    SHORT           X_Pos, Offset;

    Scan = pKbd->KeyInfo[0].KeyInfo.chScan;
    Char = pKbd->KeyInfo[0].KeyInfo.chChar;

#if DBG
    IF_OD2_DEBUG(KBD)
    {
        KdPrint(("HandleKeyboardInput: %s - Char %x, Scan %x, Status %x, State %x, Flag %x\n",
            (KbdState == CharMode) ? "CharMode" :
            (KbdState == AsciiMode) ? "AsciiMode" : "BinaryMode",
            Char, Scan, pKbd->KeyInfo[0].KeyInfo.fbStatus,
            pKbd->KeyInfo[0].KeyInfo.fsState, pKbd->KeyInfo[0].KeyboardFlag));
    }
#endif
    switch (KbdState)
    {
        case CharMode:
            if ( KbdPeekFlag || (pKbd->wRepeatCount > 1))
            {
                KbdQueue->LastKey = *pKbd;
                KbdQueue->LastKeyFlag = TRUE;
                if (!KbdPeekFlag)
                {
                    KbdQueue->LastKey.wRepeatCount--;
                }
            }

            return (NO_ERROR);
            break;

        case AsciiMode:

            KbdQueue->LastKey = *pKbd;           // prepare it in case we read only one copy of the char
            KbdQueue->LastKey.wRepeatCount--;

           /*
            *
            * Handle CR (TurnAround Char)
            *
            *   1. put in buffer (if there is a room), beep otherwise
            *   2. echo if echo_on
            *   3. (READ only) add LF to CR (if there is a place)
            *
            */

            if ( !KbdFxWaitForChar && KbdKeyIsTurnAround(Char, Scan) )
            {
                if ( pKbd->wRepeatCount > 1 )
                {
                    KbdQueue->LastKeyFlag = TRUE;
                }

                if (( KbdReadMode == 1 ) && KbdInputLength &&
                    ( LineInputBuff[0].Char == 0x1A ))        // ^Z
                {
                    NumChar = KbdInputLength = 0;
                } else
                {
                    NumChar = KbdInputLength;

                    /*
                     *  Add the TurnAround char at the end of the buffer
                     */

                    LineInputBuff[KbdInputLength++].Char = Char;

                    if (KbdTurnAroundCharTwo)
                    {
                        if ( KbdReadMode == 1 )
                        {
                            LineInputBuff[KbdInputLength - 1].Char = '\r';
                            Scan = '\n';
                        }
                        LineInputBuff[KbdInputLength++].Char = Scan;
                    } else if (( KbdReadMode == 1 ) && ( Char == '\r' ) &&
                               ( KbdInputLength <= (USHORT)KbdMaxLength ))
                    {
                        LineInputBuff[KbdInputLength++].Char = '\n';
                    }

                }

                /*
                 *  Echo the TurnAround char to console
                 */

                if (KbdEchoFlag && !KbdTurnAroundCharTwo)
                {
                    KbdEchoNL(1);
                }

                /*
                 *
                 * Save string length and data in buffer for editing keys
                 *
                 */

                for ( i = 0 ; i < KbdInputLength ; i++ )
                {
                    KbdLastBuff[i] = LineInputBuff[i].Char;
                }

                if (KbdReadMode == 0)
                {
                    KbdInputLength = (USHORT)NumChar;
                }
                LastStringLength = (USHORT)NumChar;     // not include the TurnAround char

                /*
                 *
                 * Copy to application buffer
                 *
                 */

                if ( KbdLength > KbdInputLength )
                    KbdLength = KbdInputLength;

                RtlMoveMemory(KbdAddress, KbdLastBuff, KbdLength);

                if ( KbdReadMode == 0 )
                    KBD_BUFFER_ADDRESS[KbdLength] = Char;          // add the CR at the end

                /*
                 *
                 * Keep info for Read in case we don't return all the string
                 *
                 */

                KbdBuffNextPtr = KbdLength;
                KbdBuffNextLen = KbdInputLength - KbdLength;

                /*
                 *
                 * Copy string to CUE buffer if active
                 *
                 */

                if ( SesGrp->KeysOnFlag )
                {
                    if( KbdLineWasEdited && NumChar )
                    {
                        KbdCurrentLine = KbdNextLinePointer;

                        for ( i = 0, usIndex = KbdNextLinePointer ;
                              i < NumChar ; i++, usIndex++ )
                        {
                            KbdCueBuffer[usIndex] = KbdLastBuff[i];
                        }

                        KbdNextLinePointer = usIndex + 1;

                        while (KbdCueBuffer[usIndex])
                        {
                            // zero the end of the last string we copy the new
                            // one on (fill with NULL till start of next line)

                            KbdCueBuffer[usIndex++] = 0;
                        }
                    }

                    KbdCueUpRowIsCurrent = TRUE;
                }

                return(0L);
            }

           /*
            *
            * Check for Speciel Keys (PFx, enhanced ...)
            *
            */

            if (( Char == 0 ) || ( Char == 0xE0 ))
            {
                if (SesGrp->KeysOnFlag)
                {
                    if ( Scan == OS2_SCAN_RIGHT_6 )                 // Right
                    {
                        if (( NumChar = KbdInputLength - KbdIndexInLine ) >
                            pKbd->wRepeatCount )
                        {
                            NumChar = pKbd->wRepeatCount;
                        }

                        KbdCueMoveToRight(
                                    KbdIndexInLine,
                                    (USHORT)NumChar,
                                    FALSE
                                   );

                    } else if ( Scan == OS2_SCAN_LEFT_4 )           // Left
                    {
                        NumChar = ( KbdIndexInLine < pKbd->wRepeatCount ) ?
                                    KbdIndexInLine : pKbd->wRepeatCount;

                        KbdCueMoveToLeft(
                                    KbdIndexInLine,
                                    (USHORT)NumChar
                                   );

                    } else if ( Scan == OS2_SCAN_UP_8 )             // Up
                    {
                        /*
                         *  Find previous command in the command queue
                         */

                        if (KbdCueBuffer[KbdCurrentLine])  // if anything at the CUE buffer
                        {
                            if (KbdCueUpRowIsCurrent)
                            {
                                pKbd->wRepeatCount--;
                                KbdCueUpRowIsCurrent = FALSE;
                            }

                            usIndex = KbdCurrentLine;
                            for ( i = 0 ; i < pKbd->wRepeatCount ; i++ )
                            {
                                usIndex--;
                                while (!KbdCueBuffer[--usIndex]);  // skip all null
                                while (KbdCueBuffer[--usIndex]);   // go to start of command

                                usIndex++;
                            }
                            KbdCurrentLine = usIndex;
                        } else
                        {
                            usIndex = KbdCurrentLine;
                        }

                        KbdCueEraseAndDisplayLine(usIndex);
                    } else if ( Scan == OS2_SCAN_DOWN_2 )           // Down
                    {
                        /*
                         *  Find next command in the command queue
                         */

                        if (KbdCueBuffer[KbdCurrentLine])  // if anything at the CUE buffer
                        {
                            usIndex = KbdCurrentLine;
                            KbdCueUpRowIsCurrent = FALSE;

                            for ( i = 0 ; i < pKbd->wRepeatCount ; i++ )
                            {
                                while (KbdCueBuffer[++usIndex]);
                                while (!KbdCueBuffer[++usIndex]);
                            }
                            KbdCurrentLine = usIndex;
                        } else
                        {
                            usIndex = KbdCurrentLine;
                        }

                        KbdCueEraseAndDisplayLine(usIndex);
                    } else if ( Scan == OS2_SCAN_HOME_7 )           // Home
                    {
                        KbdCueMoveToLeft(
                                    KbdIndexInLine,
                                    KbdIndexInLine
                                   );
                    } else if ( Scan == OS2_SCAN_END_1 )            // End
                    {
                        KbdCueMoveToRight(
                                    KbdIndexInLine,
                                    (USHORT)(KbdInputLength - KbdIndexInLine),
                                    FALSE
                                   );

                    } else if ( Scan == OS2_SCAN_CTRL_LEFT_4 )      // ^Left
                    {
                        USHORT      PrevIndex = KbdIndexInLine;

                        for ( i = 0;
                              (i < pKbd->wRepeatCount) && PrevIndex ;
                              i++ )
                        {
                            /* for each char:
                             *   go one char left
                             *   skip all non-alphanumeric chars
                             *   akip all alphanumeric chars
                             * while points to alpha, which is not the original
                             */

                            usIndex = PrevIndex - 1;

                            while (usIndex && !ALPHANUM_CHAR(usIndex))
                            {
                                usIndex--;
                            }

                            while (usIndex && ALPHANUM_CHAR(usIndex))
                            {
                                usIndex--;
                            }

                            if (usIndex || !ALPHANUM_CHAR(usIndex))
                            {
                                usIndex++;
                            }

                            if (!ALPHANUM_CHAR(usIndex) ||
                                (PrevIndex == usIndex))
                            {
                                break;
                            }

                            PrevIndex = usIndex;
                        }
                        if (NumChar = KbdIndexInLine - PrevIndex)
                        {
                            KbdCueMoveToLeft(
                                        KbdIndexInLine,
                                        (USHORT)NumChar
                                       );
                        }
                    } else if ( Scan == OS2_SCAN_CTRL_RIGHT_6 )     // ^Right
                    {
                        USHORT      PrevIndex = KbdIndexInLine;

                        for ( i = 0;
                              (i < pKbd->wRepeatCount) && (PrevIndex < KbdInputLength);
                              i++ )
                        {
                            /* for each char:
                             *   go one char right
                             *   akip all alphanumeric chars
                             *   skip all non-alphanumeric chars
                             * while points to alpha, which is not the original
                             */

                            usIndex = PrevIndex;

                            while ((usIndex < KbdInputLength) &&
                                   ALPHANUM_CHAR(usIndex))
                            {
                                usIndex++;
                            }

                            while ((usIndex < KbdInputLength) &&
                                   !ALPHANUM_CHAR(usIndex))
                            {
                                usIndex++;
                            }

                            if ((usIndex >= KbdInputLength) ||
                                !ALPHANUM_CHAR(usIndex))
                            {
                                break;
                            }

                            PrevIndex = usIndex;
                        }
                        if (NumChar = PrevIndex - KbdIndexInLine)
                        {
                            KbdCueMoveToRight(
                                        KbdIndexInLine,
                                        (USHORT)NumChar,
                                        FALSE
                                       );
                        }
                    } else if ( Scan == OS2_SCAN_CTRL_END_1 )       // ^End
                    {
                        KbdLineWasEdited = TRUE;
                        KbdEchoBSAndFillSpaces(0, LineInputBuff[KbdInputLength].X_Pos -
                                        LineInputBuff[KbdIndexInLine].X_Pos);

                        KbdInputLength = KbdIndexInLine;
                    } else if ( Scan == OS2_SCAN_DEL )              // Del
                    {
                        KbdLineWasEdited = TRUE;
                        if (( NumChar = KbdInputLength - KbdIndexInLine ) >
                            pKbd->wRepeatCount )
                        {
                            NumChar = pKbd->wRepeatCount;
                        }

                        KbdCueDeleteCharAndShift(
                                    KbdIndexInLine,
                                    (USHORT)NumChar
                                   );
                    } else if ( Scan == OS2_SCAN_CTRL_HOME_7 )      // ^Home
                    {
                        if (NumChar = KbdIndexInLine)
                        {
                            KbdLineWasEdited = TRUE;
                            KbdCueMoveToLeft(
                                        KbdIndexInLine,
                                        (USHORT)NumChar
                                       );

                            KbdCueDeleteCharAndShift(
                                        KbdIndexInLine,
                                        (USHORT)NumChar
                                       );
                        }
                    } else if ( Scan == OS2_SCAN_INSERT_0 )         // Ins
                    {
                        if ( pKbd->wRepeatCount % 2 )
                        {
                            if( KbdInsertOn = ~KbdInsertOn )
                            {
                                KbdCueSetCurTypeToHalf();
                            } else
                            {
                                KbdCueSetCurTypeToQuater();
                            }
                        }
                    }

                    break;
                } else
                {
                    if ( KbdFxWaitForChar )
                    {
                        KbdFxWaitForChar = 0;
                        if (--pKbd->wRepeatCount)
                        {
                            KbdQueue->LastKey.wRepeatCount--;
                            KbdQueue->LastKeyFlag = TRUE;
                        } else
                        {
                            break;
                        }
                    }

                    if ( Scan == 0x40 )
                    {
                        Char = 0x1A;       // F6  =>  ^Z
                    } else if ( Scan == 0x41 )
                    {
                        Char = 0x00;       // F7  =>  ^@
                    } else if (KbdEditFlag)
                    {
                        if (( Scan == 0x3B ) ||                     // F1
                            ( Scan == OS2_SCAN_RIGHT_6 ))           // Right
                        {
                            KbdInsertOn = FALSE;
                            if ((USHORT)(KbdDelIndex + KbdLastBuffPtr) < LastStringLength)
                            {
                                Char = KbdLastBuff[KbdDelIndex + KbdLastBuffPtr];    // => previous char
                            } else
                                break;

                        } else if ( Scan == 0x3C )                  // F2
                        {
                            KbdInsertOn = FALSE;
                            if (pKbd->wRepeatCount % 2)
                            {
                                KbdFxWaitForChar = 1;
                            }
                            break;
                        } else if ( Scan == 0x3D )                  // F3
                        {
                            KbdInsertOn = FALSE;
                            if ((USHORT)(KbdDelIndex + KbdLastBuffPtr) < LastStringLength)
                            {
                                StringLengthToEcho = LastStringLength - KbdDelIndex - KbdLastBuffPtr;
                                KbdEchoString = TRUE;
                            } else
                                break;

                        } else if ( Scan == 0x3E )                  // F4
                        {
                            KbdInsertOn = FALSE;
                            if (pKbd->wRepeatCount % 2)
                            {
                                KbdFxWaitForChar = 2;
                            }
                            break;
                        } else if  ( Scan == OS2_SCAN_DEL )         // Del
                        {
                            KbdDelIndex += pKbd->wRepeatCount;
                            if(KbdDelIndex > LastStringLength)
                            {
                                KbdDelIndex = LastStringLength;
                            }
                            break;
                        } else if  ( Scan == OS2_SCAN_INSERT_0 )    // Ins
                        {
                            if (pKbd->wRepeatCount % 2)
                            {
                                KbdInsertOn = ~KbdInsertOn;
                            }

                            break;

                        } else if  ( Scan == OS2_SCAN_LEFT_4 )      // Left
                        {
                            Char = '\b';                    // => BS
                            KbdInsertOn = FALSE;
                        } else
                            break;
                    } else
                        break;
                }
            }

           /*
            *
            * Search for char in KbdLastBuff (for F2 & F4)
            *
            */

            if (KbdFxWaitForChar)
            {
                if (pKbd->wRepeatCount > 1)
                {
                    KbdQueue->LastKeyFlag = TRUE;
                }
                for ( i = KbdDelIndex + KbdLastBuffPtr ;
                      (i < LastStringLength) && (KbdLastBuff[i] != Char) ; i++ );
                if ((i == (USHORT)(KbdDelIndex + KbdLastBuffPtr)) ||
                    (KbdLastBuff[i] != Char))
                {
                    KbdFxWaitForChar = 0;
                    break;
                }

                StringLengthToEcho = i - (KbdDelIndex + KbdLastBuffPtr);

                if (KbdFxWaitForChar == 1)            // F2
                {
                    KbdFxWaitForChar = 0;
                    KbdEchoString = TRUE;
                } else                                // F4
                {
                    KbdDelIndex += i;
                    KbdFxWaitForChar = 0;
                    break;
                }
            }

           /*
            *
            * Echo string from KbdLastBuff (for F2 & F3)
            *
            * StringLengthToEcho - is the string length
            */

            if (KbdEchoString)
            {
                X_Pos = GET_CURRENT_COORD.X;
                NumChar = 0;

                for ( i = KbdDelIndex + KbdLastBuffPtr, puchLastString = &KbdLastBuff[i] ;
                      StringLengthToEcho && (KbdInputLength < (USHORT)KbdMaxLength) ; StringLengthToEcho-- )
                {
                    Char = KbdLastBuff[i++];
                    NumChar++ ;

                    LineInputBuff[KbdInputLength++].Char = Char;
                    if (Char == '\t')  // TAB
                    {
                        X_Pos += 8 - (X_Pos % 8);
                    } else if (Char < ' ')
                    {
                        X_Pos += 2;
                    } else
                    {
                        X_Pos++;
                    }

                    if (X_Pos >= SesGrp->ScreenColNum)
                    {
                        X_Pos -= SesGrp->ScreenColNum;
                        KbdStartOfLine = 0;
                    }

                    LineInputBuff[KbdInputLength].X_Pos = X_Pos;
                }

                if (KbdEchoFlag && NumChar)
                {
                    KbdEchoAString(puchLastString, NumChar);
                }

                KbdLastBuffPtr += (USHORT)NumChar;

                if (StringLengthToEcho)
                {
                    KbdEchoBeep(i);
                }

                KbdEchoString = FALSE;
                break;
            }

           /*
            *
            * Handle BS
            *
            */

            if (( Char == '\b' ) || ( Char == 0x7F ))
            {
                if (SesGrp->KeysOnFlag)
                {
                    if ( NumChar = KbdIndexInLine )
                    {
                        if ( NumChar > pKbd->wRepeatCount )
                        {
                            NumChar = pKbd->wRepeatCount;
                        }

                        KbdCueMoveToLeft(
                                    KbdIndexInLine,
                                    (USHORT)NumChar
                                   );

                        KbdCueDeleteCharAndShift(
                                    KbdIndexInLine,
                                    (USHORT)NumChar
                                   );

                        KbdLineWasEdited = TRUE;
                    }

                    break;
                } else
                {
                    KbdInsertOn = FALSE;

                    for ( i = 0 ; (i < pKbd->wRepeatCount) && KbdInputLength &&
                                   (LineInputBuff[KbdInputLength].X_Pos != KbdStartOfLine) ; i++ )
                    {
                        if (KbdLastBuffPtr)
                        {
                            KbdLastBuffPtr--;
                        } else if (KbdDelIndex)
                        {
                            KbdDelIndex--;
                        }

                        KbdInputLength--;
                    }

                    NumChar = GET_CURRENT_COORD.X - LineInputBuff[KbdInputLength].X_Pos;

                    if(KbdEchoFlag && NumChar)
                    {
                        KbdEchoBSAndFillSpaces(NumChar, NumChar);
                    }

                    break;
                }
            }

            if (!SesGrp->KeysOnFlag)
            {
               /*
                *
                * Handle LF
                *
                */

                if (Char == '\n')
                {
                    if (KbdEchoFlag)
                    {
                        KbdEchoNL(pKbd->wRepeatCount);
                    }

                    LineInputBuff[KbdInputLength].X_Pos = 0;
                    KbdStartOfLine = 0;

                    break;
                }

               /*
                *
                * Handle ^W
                *
                */

                if (Char == 0x17)
                {
                    USHORT      PrevIndex = KbdIndexInLine;

                    KbdInsertOn = FALSE;

                    for ( i = 0;
                          (i < pKbd->wRepeatCount) && ( PrevIndex > KbdStartOfLine ) ;
                          i++ )
                    {
                        /* for each char:
                         *   go one char left
                         *   skip all non-alphanumeric chars
                         *   akip all alphanumeric chars
                         * while points to alpha, which is not the original
                         */

                        usIndex = PrevIndex - 1;

                        while (( usIndex > KbdStartOfLine ) &&
                               !ALPHANUM_CHAR(usIndex))
                        {
                            usIndex--;
                        }

                        while (( usIndex > KbdStartOfLine ) &&
                               ALPHANUM_CHAR(usIndex))
                        {
                            usIndex--;
                        }

                        if (( usIndex > KbdStartOfLine ) ||
                            !ALPHANUM_CHAR(usIndex))
                        {
                            usIndex++;
                        }

                        if (!ALPHANUM_CHAR(usIndex) ||
                            (PrevIndex == usIndex))
                        {
                            break;
                        }

                        PrevIndex = usIndex;
                    }

                    NumChar = LineInputBuff[KbdIndexInLine].X_Pos -
                                        LineInputBuff[PrevIndex].X_Pos ;

                    if(KbdEchoFlag && NumChar)
                    {
                        KbdEchoBSAndFillSpaces(NumChar, NumChar);
                    }

                    KbdIndexInLine = KbdInputLength = PrevIndex;
                    //KbdLastBuffPtr = 0;
                    break;
                }
            }

           /*
            *
            * Handle ESC
            *
            */

            if (Char == 0x1B)
            {
                // ^[  =>  Write '\'<NL> & Clear Buff

                KbdInsertOn = FALSE;

                KbdLastBuffPtr = KbdDelIndex = 0;
                if (SesGrp->KeysOnFlag)
                {
                    if (KbdCueBuffer[KbdCurrentLine])
                    {
                        usIndex = KbdCurrentLine - 1;
                    } else
                    {
                        usIndex = KbdCurrentLine;
                    }

                    KbdCueEraseAndDisplayLine(usIndex);     // don't display anything
                } else if (KbdEchoFlag)
                {
                    KbdEchoESC(pKbd->wRepeatCount, KbdFirstColumn);
                }

                KbdStartOfLine = LineInputBuff[0].X_Pos = KbdFirstColumn;
                KbdInputLength = 0;
                break;
            }

           /*
            *
            * Handle ^F
            *
            */

            if (Char == 0x6)
            {
                break;
            }

           /*
            *
            * Handle char :
            *
            *   1. put in buffer (if there is a room), beep otherwise
            *   2. save in buffer cursor position of next char
            *   3. echo if echo_on
            *
            */

            NumChar = 0;

            if (SesGrp->KeysOnFlag)
            {
                KbdCueHandleChar(Char, pKbd->wRepeatCount);
            } else
            {
                X_Pos = GET_CURRENT_COORD.X;

                if (Char < ' ')
                {
                    if (Char == '\t')  // TAB
                    {
                        Offset = 8;
                        X_Pos &= ~7;
                    } else
                    {
                        Offset = 2;
                    }
                } else
                {
                    Offset = 1;
                }

                for ( i = 0 ; (i < pKbd->wRepeatCount) &&
                              (KbdInputLength < (USHORT)KbdMaxLength) ; i++ )
                {
                    if (!KbdInsertOn)
                    {
                        KbdLastBuffPtr++;
                    }

                    LineInputBuff[KbdInputLength++].Char = Char;
                    X_Pos += Offset;

                    if (X_Pos >= SesGrp->ScreenColNum)
                    {
                        X_Pos -= SesGrp->ScreenColNum;
                        KbdStartOfLine = 0;
                    }
                    LineInputBuff[KbdInputLength].X_Pos = X_Pos;
                }

                if (KbdEchoFlag && i)
                {
                    KbdEchoChar(Char, i);
                }

                if (i < pKbd->wRepeatCount)
                {
                    KbdEchoBeep(pKbd->wRepeatCount - i);
                }
            }
            break;

        case BinaryMode:
            /*
             *
             * Binary mode
             *
             */

            /*
             *
             * Handle char :
             *
             *   put in buffer (if a place exist)
             *   if enhanced - put also scan code
             *
             */

            for ( i = 0 ; (i < pKbd->wRepeatCount) && (KbdInputLength < KbdLength); i++ )
            {
                KBD_BUFFER_ADDRESS[KbdInputLength++] = Char;

                if ((Char == 0x0) && (KbdInputLength < KbdLength))
                {
                    KBD_BUFFER_ADDRESS[KbdInputLength++] = Scan;
                }
            }

            if ( KbdInputLength < KbdLength )
            {
                break;
            } else if ( i < pKbd->wRepeatCount )
            {
                KbdQueue->LastKey = *pKbd;           // prepare it in case we read only one copy of the char
                KbdQueue->LastKey.wRepeatCount -= i;
                KbdQueue->LastKeyFlag = TRUE;
            }

            /*
             *
             * Copy to application buffer
             *
             */

            if ( KbdLength > KbdInputLength )
                KbdLength = KbdInputLength;

           /*
            *
            * Keep info for Read in case we don't return all the string
            *
            */

            KbdBuffNextPtr = KbdLength;
            KbdBuffNextLen = KbdInputLength - KbdLength;

            /*
             *
             * No editing keys in binary mode
             *
             */

            LastStringLength = 0;

            return(0L);

        default:
            break;
    }

#if DBG
    IF_OD2_DEBUG( KBD )
    {
        if ( KbdState == AsciiMode )
        {
            USHORT  CurrentIdx;
            CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;

            CurrentIdx = (SesGrp->KeysOnFlag) ? KbdIndexInLine : KbdInputLength;

            if (!GetConsoleScreenBufferInfo(
                                         hConOut,
                                         &ConsoleScreenBufferInfo
                                        ))
            {
                ConsoleScreenBufferInfo.dwCursorPosition.X =
                    ConsoleScreenBufferInfo.dwCursorPosition.Y = 255;
                KdPrint(("OS2SES(Ow2VioReadCurPos): Rc %lu\n", GetLastError()));
                ASSERT( FALSE );        // should not happend
            }
            KdPrint(("HandleKeyboardInput: Len %u, CUE-Idx %u, Cur-Offset %u, Prev-Offset %u, Col %u, Pos %u:%u\n",
                    KbdInputLength, KbdIndexInLine,
                    LineInputBuff[CurrentIdx].X_Pos,
                    (CurrentIdx) ? LineInputBuff[CurrentIdx - 1].X_Pos : 255,
                    SesGrp->WinCoord.X,
                    ConsoleScreenBufferInfo.dwCursorPosition.Y,
                    ConsoleScreenBufferInfo.dwCursorPosition.X
                   ));
            if (KbdEchoFlag)
            {
                ASSERT( ConsoleScreenBufferInfo.dwCursorPosition.X == SesGrp->WinCoord.X );
                ASSERT( ConsoleScreenBufferInfo.dwCursorPosition.Y == SesGrp->WinCoord.Y );
                ASSERT( (LineInputBuff[CurrentIdx].X_Pos % SesGrp->ScreenColNum) ==
                        SesGrp->WinCoord.X );
            }
        }
    }
#endif

    return(1L);
}


DWORD
KbdHandlePackage(IN  PKEY_EVENT_QUEUE    NextKbdMon,
                 IN  PKBD_MON_PACKAGE    KbdPackage)
{
    KEYEVENTINFO    In;

    if ( KbdCheckPackage( KbdPackage ))
    {
        return (0L);                // ignore key
    }

#ifdef DBCS
// MSKK Jul.23.1992 KazuM
    In.wRepeatCount = NextKbdMon->In->wRepeatCount;
#else
    In.wRepeatCount = 1;
#endif
    In.KeyInfo[0] = *KbdPackage;

    if ( HandleKeyboardInput( &In ))
    {
        return (0L);                // not time to send reply yet
    }

    NextKbdMon->MonHdr.WaitForEvent = FALSE;

#if DBG
    IF_OD2_DEBUG( KBD )
    {
        KdPrint(("KbdHandlePackage: %s respond\n",
                (KbdRequestSaveArea.Request == KBDCharIn) ? "KbdCharIn" :
                (KbdRequestSaveArea.Request == KBDStringIn) ? "KbdStringIn" :
                "KbdRead"));
    }
#endif
    if ( KbdRequestSaveArea.Request == KBDCharIn )
    {
        KbdRequestSaveArea.d.KeyInfo = In.KeyInfo[0].KeyInfo;

        SendKbdReply((PVOID)NextKbdMon->MonHdr.MemoryStartAddress,
                     (PVOID)&KbdRequestSaveArea,
                     KbdAddress,
                     0);
    } else
    {
        KbdRequestSaveArea.Length = (ULONG)KbdLength;

        if ( KbdRequestSaveArea.Request == KBDStringIn )
        {
            KbdRequestSaveArea.d.String.cchIn = KbdLength;
            if ( KbdRequestSaveArea.d.String.cchIn != KbdRequestSaveArea.d.String.cb )
                KbdRequestSaveArea.Length++ ;             // ASCII mode - copy the CR
        }

        SendKbdReply ((PVOID)NextKbdMon->MonHdr.MemoryStartAddress,
                     (PVOID)&KbdRequestSaveArea,
                     KbdAddress,
                     0);
    }

    return (1L);
}


DWORD
KbdCheckPackage(IN  PKBD_MON_PACKAGE    KbdPackage)
{
    BOOL    IgnoreKey = FALSE;
    UCHAR   Scan, Char;

    Char = KbdPackage->KeyInfo.chChar;
    Scan = KbdPackage->KeyInfo.chScan;

    if (( Char == 0 ) && ( Scan == 0 ))
    {
        if(!( ENABLE_SHIFT_KEY & KbdWaitFlag ))
        {
            /*
             *  Ignore shift keys if { in ASCII mode or shift report off }
             */
#if DBG
            IF_OD2_DEBUG(KBD)
            {
                KdPrint(("                              - ignore since shift report disable\n"));
            }
#endif
            IgnoreKey = TRUE;             // ignore shift report
        } else if(!( KbdPackage->KeyInfo.fbStatus & 1))
        {
            /*
             *  Ignore non-shift keys
             */
#if DBG
            IF_OD2_DEBUG(KBD)
            {
                KdPrint(("                              - ignore since non-shift key\n"));
            }
#endif
            IgnoreKey = TRUE;
        }
    } else if ( KbdPackage->KeyboardFlag & KBD_KEY_BREAK )
    {
        /*
         *  Ignore Break if non-shift
         */

#if DBG
        IF_OD2_DEBUG( KBD )
        {
            KdPrint(("KbdCheckPackage: ignore key release\n"));
        }
#endif
        IgnoreKey = TRUE;         // ignore KEY_UP
    } else if (( KbdWaitFlag & ENABLE_LN_EDITOR_KEY ) &&
                KbdKeyIsTurnAround(Char, Scan) )
    {
        // don't ignore the turn around char in ASCII mode

    } else if ( !( KbdWaitFlag & ENABLE_NON_ASCII_KEY ) &&
                (( Char == 0 ) || ( Char == 0xE0 )))
    {
        IgnoreKey = TRUE;         // ignore NON_ASCII

        if ( KbdWaitFlag & ENABLE_KEYS_ON_KEY )
        {
            if (( Scan == OS2_SCAN_HOME_7 )       ||      // Home
                ( Scan == OS2_SCAN_CTRL_HOME_7 )  ||      // ^Home
                ( Scan == OS2_SCAN_END_1 )        ||      // End
                ( Scan == OS2_SCAN_CTRL_END_1 )   ||      // ^End
                ( Scan == OS2_SCAN_LEFT_4 )       ||      // Left
                ( Scan == OS2_SCAN_CTRL_LEFT_4 )  ||      // ^Left
                ( Scan == OS2_SCAN_RIGHT_6 )      ||      // Right
                ( Scan == OS2_SCAN_CTRL_RIGHT_6 ) ||      // ^Right
                ( Scan == OS2_SCAN_UP_8 )         ||      // Up
                ( Scan == OS2_SCAN_DOWN_2 )       ||      // Down
                ( Scan == OS2_SCAN_DEL )          ||      // Del
                ( Scan == OS2_SCAN_INSERT_0 ))            // Ins
            {
                IgnoreKey = FALSE;         // don't ignore CUE keys
            }
        } else if ( KbdWaitFlag & ENABLE_LN_EDITOR_KEY )
        {
            if (( Scan == 0x3B )                  ||      // F1
                ( Scan == 0x3C )                  ||      // F2
                ( Scan == 0x3D )                  ||      // F3
                ( Scan == 0x3E )                  ||      // F4
                ( Scan == 0x40 )                  ||      // F6
                ( Scan == 0x41 )                  ||      // F7
                ( Scan == OS2_SCAN_LEFT_4 )       ||      // Left
                ( Scan == OS2_SCAN_RIGHT_6 )      ||      // Right
                ( Scan == OS2_SCAN_DEL )          ||      // Del
                ( Scan == OS2_SCAN_INSERT_0 ))            // Ins
            {
                IgnoreKey = FALSE;         // don't ignore editing keys
            }
        }

        if (IgnoreKey)
        {
#if DBG
            IF_OD2_DEBUG( KBD )
            {
                KdPrint(("KbdCheckPackage: ignore non-ASCII key\n"));
            }
#endif
        }
    } else if ( KbdAsciiMode )
    {
        /*
         *
         * Ignore speciel CTRL-Keys in ASCII mode
         *
         */

        if (( Char == 0x03 ) ||     // ^C
            ( Char == 0x10 ) ||     // ^P
                                    // ^Q: in non-US kbd(AZARTY) ^Q is passed
                                    //  and ^A not (mjarus 7/5/93)
            (( Scan == 0x10 ) &&
             ((KbdPackage->KeyInfo.fsState & (OS2_CONTROL | OS2_ALT)) == OS2_CONTROL)) ||
            ( Char == 0x13 ))       // ^S
        {
#if DBG
            IF_OD2_DEBUG(KBD)
            {
                KdPrint(("                              - ignore Char %x in ASCII\n",
                        Char));
            }
#endif
            IgnoreKey = TRUE;         // ignore NON_ASCII
        }
    }

    return ( (DWORD)IgnoreKey );
}


VOID
KbdNewSetup(
    IN PKBDINFO  LastSetup
    )
{
    PKBDINFO    NewSetup = &KbdQueue->Setup;
    USHORT      Mask;

//    if (*LastSetup == *NewSetup)
//        return;

    /* BUGBUG=> handle new setup */

    SesGrp->ModeFlag = (USHORT)((NewSetup->fsMask & KEYBOARD_BINARY_MODE ) ? 1 : 0);

    Mask = NewSetup->fsMask & (KEYBOARD_BINARY_MODE | KEYBOARD_SHIFT_REPORT);

    Ow2KbdXlateVars.XInputMode = (Mask) ?
        ((Mask & KEYBOARD_SHIFT_REPORT) ? (BINARY_MODE | SHIFT_REPORT_MODE) :
            BINARY_MODE) : 0;

    if ((NewSetup->fsMask & KEYBOARD_ASCII_MODE ) &&
        (LastSetup->fsMask & KEYBOARD_BINARY_MODE ))
    {
        LastStringLength = 0;
        KbdBuffNextPtr = 0;
        KbdBuffNextLen = 0;
    }

    KbdAsciiMode = (BOOL)(( NewSetup->fsMask & KEYBOARD_ASCII_MODE ) ? 1 : 0);

    if (NewSetup->fsMask & KEYBOARD_2B_TURNAROUND)
    {
        KbdSetupTurnAroundCharTwo = (BOOLEAN)TRUE;
    } else
    {
        KbdSetupTurnAroundCharTwo = (BOOLEAN)FALSE;
    }

    return;
}


VOID
KbdCueEraseAndDisplayLine(
    IN ULONG    NewLineIndex
    )
/*++

Routine Description:

    This routine erase the current input line and display new one.

Arguments:

    NewLineIndex - pointer in CUE buffer for new line (or to NULL
    if nothing to diaply).

Return Value:


Note:

    ASCII CUE string mode.

    Used by UP-ARROW, DOWN-ARROW and ESC.

    1. Erase the active command line being displayed and
    2. Display the new command in the command queue.

    Uses:

    -   KbdInputLength - active command line length.
    -   KbdIndexInLine - current index in command line.
    -   LineInputBuff[0, KbdIndexInLine, KbdInputLength].X_Pos
    -   KbdCueBuffer[NewLineIndex..]

    Updates:

    -   KbdInputLength - new line length.
    -   KbdIndexInLine - 0.
    -   LineInputBuff[0..KbdInputLength].Char and .X_Pos
    -   console display
    -   LVB
    -   SesGrp->WinCoord - by other routines.

    Calls:

    -   KbdEchoBSAndFillSpaces
    -   KbdCueUpdateBufferOffset
    -   KbdCueMoveToRight
    -   KbdCueMoveToLeft

--*/
{
    /*
     *  Erase the active command line being displayed.
     */

    if ( KbdInputLength )
    {
        KbdEchoBSAndFillSpaces(
                    LineInputBuff[KbdIndexInLine].X_Pos - LineInputBuff[0].X_Pos,
                    LineInputBuff[KbdInputLength].X_Pos - LineInputBuff[0].X_Pos
                    );

        KbdIndexInLine = 0;
        KbdInputLength = 0;
    }

    /*
     *  Display the new command in the command queue
     */

    for ( KbdInputLength = 0 ;
          LineInputBuff[KbdInputLength].Char = KbdCueBuffer[NewLineIndex + KbdInputLength] ;
          KbdInputLength++ );

    if (KbdInputLength)
    {
        KbdCueUpdateBufferOffset(
                    0,
                    KbdInputLength,
                    LineInputBuff[0].X_Pos
                   );

        KbdCueMoveToRight(
                    0,
                    KbdInputLength,
                    TRUE
                   );

        KbdCueMoveToLeft(
                    KbdInputLength,
                    KbdInputLength
                   );
    }
}


VOID
KbdCueMoveToRight(
    IN  ULONG   StartIndex,
    IN  ULONG   StringLength,
    IN  ULONG   UpdateLVB
    )
/*++

Routine Description:

    This routine moves the console cursor position to the right
    while sending the active command line to the console.

Arguments:

    StartIndex - index in LineInputBuff[] (the active command line) to start
    moving right from.

    StringLength - number of character to move right.

    UpdateLVB - flag if to set LVB also (for new info)

Return Value:


Note:

    ASCII CUE string mode.

    Used by RIGHT-ARROW, END, ^RIGHT-ARROW, KbdCueEraseAndDisplayLine,
    KbdCueDeleteCharAndShift and KbdCueHandleChar.

    1. Send to console
    2. Update Coord
    3. Update LVB
    4. Update position

    Uses:

    -   LineInputBuff[0..KbdInputLength].Char and .X_Pos

    Updates:

    -   KbdInputLength - NO.
    -   KbdIndexInLine - add StringLength.
    -   LineInputBuff[0..KbdInputLength].Char and .X_Pos - NO
    -   console display
    -   LVB (if UpdateLVB)
    -   SesGrp->WinCoord - add StringLength.

    Calls:

    -   Or2WinWriteConsoleA
    -   Ow2VioUpdateCurPos
    -   VioLVBScrollBuff
    -   VioLVBCopyStr

--*/
{
    UCHAR       Char;
    ULONG       NumChar, NumBytes, NumWritten, NumLines = 0;
    COORD       OldCoord, Coord;

    OldCoord = Coord = GET_CURRENT_COORD;

    /*
     *  Copy to temp buffer
     */

    for ( NumBytes = 0, NumChar = 0 ; NumChar < StringLength ; NumChar++ )
    {
        if ((Char = LineInputBuff[StartIndex + NumChar].Char) < ' ')
        {
            KBD_BUFFER_ADDRESS[NumBytes++] = '^';
            KBD_BUFFER_ADDRESS[NumBytes++] = (UCHAR)(Char + '@');
        } else
        {
            KBD_BUFFER_ADDRESS[NumBytes++] = Char;
        }
    }

    if (!NumBytes)
    {
        return ;
    }

    if ( KbdEchoFlag )
    {
        /*
         *   Send to console
         */

        if(!Or2WinWriteConsoleA(
                    #if DBG
                    KbdCueMoveToRightStr,
                    #endif
                    hConOut,
                    (LPSTR)KBD_BUFFER_ADDRESS,
                    NumBytes,
                    &NumWritten,
                    NULL
                   ))
        {
#if DBG
            ASSERT1("OS2SES(KbdCueMoveToRight): failed on WriteConsoleA", FALSE);
#endif
        }

#if DBG
        if ( NumBytes != NumWritten )
        {
            KdPrint(("OS2SES(KbdCueMoveToRight): partial data WriteConsoleA (%u from %u)\n",
                    NumWritten, NumBytes));
            ASSERT( FALSE );
        }
#endif

        /*
         *  Update Coord
         */

        Coord.X += (SHORT)NumWritten;
        while ( Coord.X >= SesGrp->ScreenColNum )
        {
            Coord.Y++;
            Coord.X -= SesGrp->ScreenColNum;
        }

        if ( Coord.Y >= SesGrp->ScreenRowNum )
        {
            NumLines = Coord.Y - SesGrp->ScreenRowNum + 1;
            Coord.Y = SesGrp->ScreenRowNum - 1;
        }

        Ow2VioUpdateCurPos(Coord);

        /*
         *  Update LVB
         */

        if ( UpdateLVB )
        {
            if ( NumLines && ( SesGrp->ScreenSize < 512 ))
            {
                ULONG   NumChar1 = SesGrp->ScreenRowNum - OldCoord.X;
                UCHAR   *Ptr = &KBD_BUFFER_ADDRESS[0];

                while ( NumWritten )
                {
                    VioLVBCopyStr(
                               Ptr,
                               OldCoord,
                               NumChar1
                              );

                    VioLVBFillAtt(
                               SesGrp->AnsiCellAttr,
                               OldCoord,
                               NumChar1
                              );

                    OldCoord.X = 0;

                    if ( ++OldCoord.Y >= SesGrp->ScreenRowNum )
                    {
                        OldCoord.Y--;
                        VioLVBScrollBuff(1);
                        NumLines--;
                    }

                    NumWritten -= NumChar1;

                    if (( NumChar1 = NumWritten ) > (ULONG)SesGrp->ScreenColNum )
                    {
                        NumChar1 = SesGrp->ScreenColNum;
                    }
                }

                ASSERT( NumLines == 0 );
            } else
            {
                if ( NumLines )
                {
                    OldCoord.Y -= (SHORT)NumLines;
                    VioLVBScrollBuff(NumLines);
                }

                VioLVBCopyStr(
                           KBD_BUFFER_ADDRESS,
                           OldCoord,
                           NumWritten
                          );

                VioLVBFillAtt(
                           SesGrp->AnsiCellAttr,
                           OldCoord,
                           NumWritten
                          );
            }
        }
    }

    /*
     *  Update position index
     */

    KbdIndexInLine += (USHORT)StringLength;
}


VOID
KbdCueMoveToLeft(
    IN  ULONG   StartIndex,
    IN  ULONG   MoveLength
    )
/*++

Routine Description:

    This routine moves the console cursor position to the left
    using '\b'.

Arguments:

    StartIndex - index in LineInputBuff[] (the active command line) to start
    moving left from.

    MoveLength - number of character to move left.

Return Value:


Note:

    ASCII CUE string mode.

    Used by LEFT-ARROW, HOME, ^LEFT-ARROW, ^HOME, BS, KbdCueEraseAndDisplayLine,
    KbdCueDeleteCharAndShift and KbdCueHandleChar.

    1. Send to console
    2. Update position

    Uses:

    -   LineInputBuff[StartIndex - MoveLength,StartIndex].X_Pos
    -   LineInputBuff[StartIndex - MoveLength..StartIndex].Char

    Updates:

    -   KbdInputLength - NO.
    -   KbdIndexInLine - sub MoveLength.
    -   LineInputBuff[0..KbdInputLength].Char and .X_Pos - NO
    -   console display - NO
    -   LVB - NO
    -   SesGrp->WinCoord - by other routines.

    Calls:

    -   KbdEchoBSAndFillSpaces

--*/
{
    ULONG       NumBytes;

    /*
     *  Calculate byte count
     */

    NumBytes = LineInputBuff[StartIndex].X_Pos - LineInputBuff[StartIndex - MoveLength].X_Pos;

    if (!NumBytes)
    {
        return ;
    }

    if ( KbdEchoFlag )
    {
        if ( SesGrp->ScreenSize < 512 )
        {
            ULONG   Row1, Row2;

            Row1 = LineInputBuff[StartIndex - MoveLength].X_Pos / SesGrp->ScreenColNum;
            Row2 = LineInputBuff[KbdInputLength].X_Pos / SesGrp->ScreenColNum;

            if (( Row2 - Row1 + 1 ) > (ULONG)SesGrp->ScreenRowNum )
            {
                NumBytes += LineInputBuff[StartIndex - MoveLength].X_Pos % SesGrp->ScreenColNum;
                NumBytes -= (Row2 - Row1 + 1 - SesGrp->ScreenRowNum) * SesGrp->ScreenColNum;
            }
        }

        /*
         *   Send to console and update coord
         */

        KbdEchoBSAndFillSpaces(NumBytes, 0);
    }

    /*
     *  Update position index
     */

    KbdIndexInLine -= (USHORT)MoveLength;
}


VOID
KbdCueUpdateBufferOffset(
    IN  ULONG   StartIndex,
    IN  ULONG   StringLength,
    IN  ULONG   StartOffset
    )
/*++

Routine Description:

    This routine updates the offset in buffer for the active command line.
    This is done from StartIndex for StringLength + 1 (to update the offset
    of the next char according to the last char in string). The StartIndex
    gets X_Pos of StartOffset and the following are updated according the
    char type (char under 0x20 holds two columns for ^X).

Arguments:

    StartIndex - index in LineInputBuff[] (the active command line) to start
    updaing the X_Pos field from.

    StringLength - number of character to update X_Pos for.

    StartOffset - X_Pos for the first character.

Return Value:


Note:

    ASCII CUE string mode.

    Used by KbdCueEraseAndDisplayLine, KbdCueDeleteCharAndShift and
    KbdCueHandleChar.

    1. Updates the X_Pos field in LineInputBuff[].

    Uses:

    -   KbdInputLength - active command line length.
    -   KbdIndexInLine - current index in command line.
    -   LineInputBuff[0, KbdIndexInLine, KbdInputLength].X_Pos
    -   KbdCueBuffer[NewLineIndex..]

--*/
{
    ULONG       NumChar, Index, Offset;

    /*
     *  Update the offset in buffer. This is done for StringLength + 1
     *  to update the offset of the next char according to the last char
     *  in string.
     */

    for ( Index = StartIndex, Offset = StartOffset, NumChar = 0 ;
          NumChar <= StringLength ; NumChar++ )
    {
        LineInputBuff[Index].X_Pos = (USHORT)Offset++;

        if ( LineInputBuff[Index++].Char < ' ' )
        {
            Offset++;
        }
    }
}


VOID
KbdCueDeleteCharAndShift(
    IN  ULONG   StartIndex,
    IN  ULONG   NumChar
    )
/*++

Routine Description:

    This routine delete character and shift the remaining command line
    input to the left.

Arguments:

    StartIndex - index in LineInputBuff[] (the active command line) to start
    deleting from.

    NumChar - number of character to delete.

Return Value:


Note:

    ASCII CUE string mode.

    Used by DEL, ^HOME and BS.

    1  Shift buffer info (Char field)
    2  Update X_Pos (Offset) field in the shifted area
    3  Send the shifted string to console and LVB
    4  Clear remaining line
    5  Return to the cursor position

    Uses:

    -   LineInputBuff[].X_Pos and Char

    Updates:

    -   KbdInputLength - sub NumChar.
    -   KbdIndexInLine - NO
    -   LineInputBuff[0..KbdInputLength].Char and .X_Pos
    -   console display
    -   LVB
    -   SesGrp->WinCoord - by other routines.

    Calls:

    -   KbdCueUpdateBufferOffset
    -   KbdCueMoveToRight
    -   KbdEchoBSAndFillSpaces
    -   KbdCueMoveToLeft

--*/
{
    if (NumChar)
    {
        ULONG       Offset = LineInputBuff[StartIndex].X_Pos;
        ULONG       Delta = LineInputBuff[StartIndex + NumChar].X_Pos - Offset;
        ULONG       NumShift = KbdInputLength - StartIndex - NumChar;

        if ( NumShift )
        {
            /*
             *  Shift buffer info (Char field)
             */

            RtlMoveMemory(
                        &LineInputBuff[StartIndex].Char,
                        &LineInputBuff[StartIndex + NumChar].Char,
                        NumShift * sizeof(LINE_EDIT_KBD)
                       );

            /*
             *  Update X_Pos (Offset) field in the shifted area
             */

            KbdCueUpdateBufferOffset(
                        StartIndex,
                        NumShift,
                        Offset
                       );

            /*
             *  Send the shifted string to console and LVB
             */

            KbdCueMoveToRight(
                        StartIndex,
                        NumShift,
                        TRUE
                       );
        }

        /*
         *  Clear remaining line
         */

        KbdEchoBSAndFillSpaces(0, Delta);

        /*
         *  Return to the cursor position
         */

        KbdCueMoveToLeft(
                    StartIndex + NumShift,
                    NumShift
                   );

        KbdInputLength -= (USHORT)NumChar;
    }
}


VOID
KbdCueHandleChar(
    IN  UCHAR       Char,
    IN  ULONG       Count
    )
/*++

Routine Description:

    This routine handle new char at CUE mode.

Arguments:

    Char - char to handle.

    NumChar - number of character.

Return Value:


Note:

    ASCII CUE string mode.

    Used by CHAR.

    For INSERT:
    1  Shift buffer info (Char field)
    2  Fill new char
    3  Update X_Pos (Offset) field
    4  Send the new+old string to console and LVB
    5  Return to the cursor position
    6  Beep if no place
    Else:
    1  Shift buffer info (Char field)
    2  Fill new char
    3  Update X_Pos (Offset) field
    4  Send the shifted string to console and LVB
    5  Clear remaining line
    6  Return to the cursor position
    7  Beep if no place

    Uses:

    -   LineInputBuff[].X_Pos and Char
    -   KbdInsertOn
    -   KbdIndexInLine
    -   KbdMaxLength
    -   KbdInputLength

    Updates:

    -   KbdInputLength
    -   KbdIndexInLine
    -   LineInputBuff[0..KbdInputLength].Char and .X_Pos
    -   console display
    -   LVB
    -   SesGrp->WinCoord - by other routines.

    Calls:

    -   KbdCueUpdateBufferOffset
    -   KbdCueMoveToRight
    -   KbdCueMoveToLeft
    -   KbdEchoBSAndFillSpaces
    -   KbdEchoBeep
    -   RtlMoveMemory

--*/
{
    ULONG   NumChar, NumBeep = 0, NumShift, UpdateRight, i;
    ULONG   LastOffset = LineInputBuff[KbdInputLength].X_Pos;

    if ( KbdInsertOn )
    {
        if (( NumChar = KbdMaxLength - KbdInputLength ) > Count )
        {
            NumChar = Count;
        } else
        {
            NumBeep = Count - NumChar;
        }

        /*
         *  Shift buffer info (Char field) to free space for new char
         */

        NumShift = KbdInputLength - KbdIndexInLine;

        RtlMoveMemory(
                    &LineInputBuff[KbdIndexInLine + NumChar].Char,
                    &LineInputBuff[KbdIndexInLine].Char,
                    NumShift * sizeof(LINE_EDIT_KBD)
                   );
        /*
         *  Fill new char
         */

        for ( i = 0 ; i < NumChar ; i++ )
        {
            LineInputBuff[KbdIndexInLine + i].Char = Char;
        }

        /*
         *  Update X_Pos (Offset) field in the shifted and new areas
         */

        KbdCueUpdateBufferOffset(
                    KbdIndexInLine,
                    NumChar + NumShift,
                    LineInputBuff[KbdIndexInLine].X_Pos
                   );

        /*
         *  Send the shifted string to console and LVB
         */

        KbdCueMoveToRight(
                    KbdIndexInLine,
                    NumChar + NumShift,
                    TRUE
                   );

        /*
         *  Return to the cursor position
         */

        KbdCueMoveToLeft(
                    KbdIndexInLine,
                    NumShift
                   );

        KbdInputLength += (USHORT)NumChar;
    } else
    {
        if (( NumChar = KbdMaxLength - KbdIndexInLine ) > Count )
        {
            NumChar = Count;
        } else
        {
            NumBeep = Count - NumChar;
        }

        UpdateRight = max(NumChar, (ULONG)(KbdInputLength - KbdIndexInLine));

        /*
         *  Fill new char
         */

        for ( i = 0 ; i < NumChar ; i++ )
        {
            LineInputBuff[KbdIndexInLine + i].Char = Char;
        }

        /*
         *  Update X_Pos (Offset) field in the old and new areas
         */

        KbdCueUpdateBufferOffset(
                    KbdIndexInLine,
                    UpdateRight,
                    LineInputBuff[KbdIndexInLine].X_Pos
                   );

        /*
         *  Send the shifted string to console and LVB
         */

        KbdCueMoveToRight(
                    KbdIndexInLine,
                    UpdateRight,
                    TRUE
                   );

        if ( LastOffset > (ULONG)LineInputBuff[KbdIndexInLine].X_Pos )
        {
            /*
             *  Fill spaces at the EOL if needed
             */

            KbdEchoBSAndFillSpaces(
                        0,
                        LastOffset - LineInputBuff[KbdIndexInLine].X_Pos
                       );
        }

        if ( UpdateRight != NumChar )
        {
            /*
             *  Return to the cursor position
             */

            KbdCueMoveToLeft(
                        KbdIndexInLine,
                        UpdateRight - NumChar
                       );
        }

        if ( KbdIndexInLine > KbdInputLength )
        {
            KbdInputLength = KbdIndexInLine;
        }
    }

    if ( NumBeep != Count )
    {
        KbdLineWasEdited = TRUE;
    }

    if ( NumBeep )
    {
        KbdEchoBeep(NumBeep);
    }
}


VOID
KbdCueSetCurTypeToHalf()
{
    VIOCURSORINFO  CurType;

    //Ow2VioGetCurType((PVOID)&CurType);
    //CurType = SesGrp->CursorInfo;

    //CurType.cEnd = SesGrp->CellVSize;
    CurType.cEnd = SesGrp->CursorInfo.cEnd;
    CurType.yStart = (CurType.cEnd + 1 ) / 2;
    CurType.cx = 1;
    CurType.attr = 0;

    Ow2VioSetCurType((PVOID)&CurType);
}


VOID
KbdCueSetCurTypeToQuater()
{
    VIOCURSORINFO  CurType;

    //Ow2VioGetCurType((PVOID)&CurType);
    //CurType = SesGrp->CursorInfo;

    //CurType.cEnd = SesGrp->CellVSize;
    CurType.cEnd = SesGrp->CursorInfo.cEnd;
    CurType.yStart = (CurType.cEnd + 1 ) * 3 / 4;
    CurType.cx = 1;
    CurType.attr = 0;

    Ow2VioSetCurType((PVOID)&CurType);
}


DWORD
KbdEchoCharToConsole(
    IN UCHAR    Char,
    IN ULONG    Count
    )
/*++

Routine Description:

    This routine write <Char> to console <Count> times.

Arguments:

    Char - character to write.

    Count - number of times to write char

Return Value:


Note:

    ASCII CUE/EDIT string mode.

    Used by KbdEchoNL, KbdEchoBSAndFillSpaces, KbdEchoBeep and KbdEchoChar.

    Calls:

    -   Or2WinWriteConsoleA

--*/
{
    ULONG   NumChar, NumWritten, MaxCount = Count;

    NumChar = (MaxCount > KBD_BUFFER_SIZE) ? KBD_BUFFER_SIZE : MaxCount;
    memset(KBD_BUFFER_ADDRESS, Char, NumChar);

    while (MaxCount)
    {
        if(!Or2WinWriteConsoleA(
                           #if DBG
                           KbdEchoCharToConsoleStr,
                           #endif
                           hConOut,
                           (LPSTR)KBD_BUFFER_ADDRESS,
                           NumChar,
                           &NumWritten,
                           NULL))
        {
#if DBG
            ASSERT1("OS2SES(KbdEchoCharToConsole): failed on WriteConsoleA", FALSE);
#endif
        }

#if DBG
        if ( NumChar != NumWritten )
        {
            KdPrint(("OS2SES(KbdEchoCharToConsole): partial data WriteConsoleA %u from %u\n",
                    NumWritten, NumChar));
            ASSERT( FALSE );
        }
#endif

        MaxCount -= NumChar;
        NumChar = (MaxCount > KBD_BUFFER_SIZE) ? KBD_BUFFER_SIZE : MaxCount;
    }

    return(NO_ERROR);
}


DWORD
KbdEchoNL(
    IN ULONG    Count
    )
/*++

Routine Description:

    This routine write NL to console <Count> times and update LVB.

Arguments:

    Count - number of times to write char

Return Value:


Note:

    ASCII CUE/EDIT string mode.

    Used by LF (EDIT only) and CR.

    Updates:

    -   SesGrp->WinCoord
    -   LVB

    Calls:

    -   KbdEchoCharToConsole
    -   VioLVBScrollBuff

--*/
{
    ULONG       NumLines = 0;
    COORD       Coord = GET_CURRENT_COORD;

    Coord.X = 0;
    Coord.Y += (SHORT)Count;
    if ( Coord.Y >= SesGrp->ScreenRowNum )
    {
        NumLines = Coord.Y - SesGrp->ScreenRowNum + 1;
        Coord.Y = SesGrp->ScreenRowNum - 1;
    }

    KbdEchoCharToConsole('\n', Count);
    Ow2VioUpdateCurPos(Coord);

    if (NumLines)
    {
        VioLVBScrollBuff(NumLines);
    }

    return(NO_ERROR);
}


DWORD
KbdEchoESC(
    IN ULONG    Count,
    IN ULONG    HorzMove
    )
/*++

Routine Description:

    This routine write <ESC> to console <Count> times with <HorzMove>
    number of spaces on the next line. It also update LVB.

Arguments:

    Count - number of times to write char

    HorzMove - number of spaces on the start of the new line.

Return Value:


Note:

    ASCII EDIT string mode.

    Used by ESC.

    Updates:

    -   SesGrp->WinCoord
    -   LVB

    Calls:

    -   Or2WinWriteConsoleA
    -   VioLVBScrollBuff

--*/
{
    ULONG       NumChar, NumWritten, MaxCount = Count, NumLines = 0, Length = HorzMove + 1;
    COORD       OldCoord, Coord;

    OldCoord = Coord = GET_CURRENT_COORD;

    KBD_BUFFER_ADDRESS[0] = '\\';
    KBD_BUFFER_ADDRESS[1] = '\n';
    memset(&KBD_BUFFER_ADDRESS[2], ' ', HorzMove);
    KBD_BUFFER_ADDRESS[HorzMove + 2] = '\\';
    NumChar = HorzMove + 2;

    Coord.Y += (SHORT)Count;
    Coord.X = (SHORT)HorzMove;

    if ( Coord.Y >= SesGrp->ScreenRowNum )
    {
        NumLines = Coord.Y - SesGrp->ScreenRowNum + 1;
        Coord.Y = SesGrp->ScreenRowNum - 1;
    }

    while ( MaxCount )
    {
        if(!Or2WinWriteConsoleA(
                           #if DBG
                           KbdEchoESCStr,
                           #endif
                           hConOut,
                           (LPSTR)KBD_BUFFER_ADDRESS,
                           NumChar,
                           &NumWritten,
                           NULL))
        {
#if DBG
            ASSERT1("OS2SES(KbdEchoESC): failed on WriteConsoleA", FALSE);
#endif
            //return (1);
        }

#if DBG
        if ( NumChar != NumWritten )
        {
            KdPrint(("OS2SES(KbdEchoESC): partial data WriteConsoleA %u from %u\n",
                    NumWritten, NumChar));
            ASSERT( FALSE );
        }
#endif

        MaxCount--;
    }

    Ow2VioUpdateCurPos(Coord);

    VioLVBFillChar(
#ifdef DBCS
// MSKK Oct.14.1993 V-AkihiS
                "\\",
#else
                '\\',
#endif
                OldCoord,
                1
               );

    VioLVBFillAtt(
               SesGrp->AnsiCellAttr,
               OldCoord,
               1
              );

    OldCoord.X = 0;

    for ( NumChar = 0 ; NumChar < Count ; NumChar++ )
    {
        if ( ++OldCoord.Y >= SesGrp->ScreenRowNum )
        {
            OldCoord.Y--;
            VioLVBScrollBuff(1);
            NumLines--;
        }

        if ( NumChar == ( Count - 1 ))
        {
            Length--;           // don't put the slash at the last line
        }

        VioLVBCopyStr(
                    &KBD_BUFFER_ADDRESS[2],
                    OldCoord,
                    Length
                   );

        VioLVBFillAtt(
                   SesGrp->AnsiCellAttr,
                   OldCoord,
                   Length
                  );
    }

    ASSERT( NumLines == 0 );
    return(NO_ERROR);
}


DWORD
KbdEchoBSAndFillSpaces(
    IN ULONG    BSCount,
    IN ULONG    SpaceCount
    )
/*++

Routine Description:

    This routine write BS ('\b') to console <BSCount> times and fill
    <SpaceCount> times spaces in console and LVB.

Arguments:

    BSCount - number of times to write \b char to console

    SpaceCount - number of times to fill space char to console and LVB

Return Value:


Note:

    ASCII EDIT/CUE string mode.

    Used by ^END, BS (EDIT only), LF (EDIT only), KbdCueEraseAndDisplayLine,
    KbdCueMoveToLeft, KbdCueDeleteCharAndShift and KbdCueHandleChar.

    Updates:

    -   SesGrp->WinCoord (for BSCount only)
    -   LVB (for SpaceCount only)

    Calls:

    -   KbdEchoCharToConsole
    -   Or2WinFillConsoleOutputCharacterA
    -   VioLVBFillChar

--*/
{
    ULONG       NumFilled;
    COORD       OldCoord, Coord;

    if ( !KbdEchoFlag )
    {
        return (0L);
    }

    if (((long) BSCount < 0) || ((long) SpaceCount < 0)) {
#if DBG
        DbgPrint("KbdEchoBSAndFillSpaces: BSCount %d or SpaceCount %d are negative\n",
                (long)BSCount, (long)SpaceCount);
#endif
        return ERROR_INVALID_PARAMETER;
    }

    OldCoord = Coord = GET_CURRENT_COORD;

    Coord.X -= (SHORT)BSCount;
    while ( Coord.X < 0 )
    {
        if ( Coord.Y )
        {
            Coord.Y--;
            Coord.X += SesGrp->ScreenColNum;
        } else
        {
            if ( SpaceCount )
            {
                if ( SpaceCount > (ULONG)abs(Coord.X) )
                {
                    SpaceCount += (ULONG)Coord.X;
                } else
                {
                    SpaceCount = 0;
                }
            }

            BSCount += (ULONG)Coord.X;      // sub the negative value
            Coord.X = 0;
        }
    }

    KbdEchoCharToConsole('\b', BSCount);
    Ow2VioUpdateCurPos(Coord);

    if ( SpaceCount )
    {
        if (!Or2WinFillConsoleOutputCharacterA(
                                #if DBG
                                KbdEchoBSAndFillSpacesStr,
                                #endif
                                hConOut,
                                ' ',
                                SpaceCount,
                                Coord,
                                &NumFilled))
        {
#if DBG
            ASSERT1("OS2SES(KbdEchoBSAndFillSpaces): failed on FillConsoleOutputCharacterA\n", FALSE);
#endif
        }

#if DBG
        if ( NumFilled != SpaceCount )
        {
            KdPrint(("OS2SES(KbdEchoBSAndFillSpaces): partial data %u from %u\n",
                    NumFilled, SpaceCount));
            ASSERT( FALSE );
        }
#endif
        VioLVBFillChar(
#ifdef DBCS
// MSKK Oct.14.1993 V-AkihiS
                    " ",
#else
                    ' ',
#endif
                    Coord,
                    SpaceCount
                   );

        VioLVBFillAtt(
                   SesGrp->AnsiCellAttr,
                   Coord,
                   SpaceCount
                  );
    }
    return(NO_ERROR);
}


DWORD
KbdEchoBeep(
    IN ULONG    Count
    )
/*++

Routine Description:

    This routine send BEPP ('\07') to console <Count> times.

Arguments:

    Count - number of times to write \g char to console

Return Value:


Note:

    ASCII EDIT/CUE string mode.

    Used when no place in the active command line by KbdEchoString,
    Char and KbdCueHandleChar.

    Calls:

    -   KbdEchoCharToConsole

--*/
{
    KbdEchoCharToConsole('\07', Count);
    return(NO_ERROR);
}


DWORD
KbdEchoChar(
    IN UCHAR    Char,
    IN ULONG    Count
    )
/*++

Routine Description:

    This routine echo <Char> to console <Count> times and update LVB.

Arguments:

    Char - char to send to console

    Count - number of times to send char to console

Return Value:


Note:

    ASCII EDIT string mode.

    Updates:

    -   SesGrp->WinCoord
    -   LVB

    Calls:

    -   KbdEchoCharToConsole
    -   VioLVBScrollBuff

--*/
{
    COORD       OldCoord, Coord;
    BOOL        SendByLine = FALSE;
    UCHAR       FillChar;
    ULONG       Offset;

    OldCoord = Coord = GET_CURRENT_COORD;
    if (( Char >= ' ' ) || ( Char == '\t' ))
    {
        KbdEchoCharToConsole(Char, Count);

        if ( Char == '\t' )
        {
            Offset = 8 * Count - (Coord.X & 7);
            FillChar = ' ';
        } else
        {
            FillChar = Char;
            Offset = Count;
        }

        if ((ULONG)( Offset + 2 * SesGrp->ScreenColNum ) > SesGrp->ScreenSize )
        {
            SendByLine = TRUE;
        } else
        {
            Coord.X += (SHORT)Offset;
            while ( Coord.X >= SesGrp->ScreenColNum )
            {
                Coord.Y++;
                Coord.X -= SesGrp->ScreenColNum;
            }

            if ( Coord.Y >= SesGrp->ScreenRowNum )
            {
                VioLVBScrollBuff(Coord.Y - SesGrp->ScreenRowNum + 1);
                OldCoord.Y -= Coord.Y - SesGrp->ScreenRowNum + 1;
                Coord.Y = SesGrp->ScreenRowNum - 1;
            }

            Ow2VioUpdateCurPos(Coord);

            VioLVBFillChar(
#ifdef DBCS
// MSKK Oct.14.1993 V-AkihiS
                        &FillChar,
#else
                        FillChar,
#endif
                        OldCoord,
                        Offset
                       );

            VioLVBFillAtt(
                       SesGrp->AnsiCellAttr,
                       OldCoord,
                       Offset
                      );
        }
    }

    if ( SendByLine || (( Char < ' ' ) && ( Char != '\t' )))
    {
        ULONG   NumChar, NumWritten, MaxCount, Pattern;

        if ( SendByLine )
        {
            Pattern = (ULONG)((FillChar << 24) |
                              (FillChar << 16) |
                              (FillChar << 8) |
                              (FillChar));

            MaxCount = 0;
        } else
        {
            MaxCount = Offset = 2 * Count;
            FillChar = (UCHAR)(Char + '@');

            Pattern = (ULONG)((FillChar << 24) |
                              ('^' << 16) |
                              (FillChar << 8) |
                              ('^'));
        }

        NumChar = ( Offset > KBD_BUFFER_SIZE ) ? KBD_BUFFER_SIZE : Offset;

        RtlFillMemoryUlong(
                KBD_BUFFER_ADDRESS,
                (NumChar + 3) & ~3,
                Pattern
               );

        while ( MaxCount )
        {
            if(!Or2WinWriteConsoleA(
                               #if DBG
                               KbdEchoCharStr,
                               #endif
                               hConOut,
                               (LPSTR)KBD_BUFFER_ADDRESS,
                               NumChar,
                               &NumWritten,
                               NULL))
            {
#if DBG
                ASSERT1("OS2SES(KbdEchoChar): failed on WriteConsoleA", FALSE);
#endif
                //return (1);
            }

#if DBG
            if ( NumChar != NumWritten )
            {
                KdPrint(("OS2SES(KbdEchoChar): partial data WriteConsoleA %u from %u\n",
                        NumWritten, NumChar));
                ASSERT( FALSE );
            }
#endif

            MaxCount -= NumChar;
            NumChar = ( MaxCount > KBD_BUFFER_SIZE ) ? KBD_BUFFER_SIZE : MaxCount;
        }

        if (( NumChar = SesGrp->ScreenColNum - Coord.X ) > Offset )
        {
            NumChar = Offset;
        }

        while ( Offset )
        {
            VioLVBCopyStr(
                        &KBD_BUFFER_ADDRESS[0],
                        Coord,
                        NumChar
                       );


            VioLVBFillAtt(
                       SesGrp->AnsiCellAttr,
                       Coord,
                       NumChar
                      );
            Coord.X += (SHORT)NumChar;
            if ( Coord.X >= SesGrp->ScreenColNum )
            {
                Coord.X -= SesGrp->ScreenColNum;
                if ( ++Coord.Y >= SesGrp->ScreenRowNum )
                {
                    VioLVBScrollBuff(1);
                    Coord.Y--;
                }
            }

            Offset -= NumChar;
            if (( NumChar = SesGrp->ScreenColNum - Coord.X ) > Offset )
            {
                NumChar = Offset;
            }
        }

        Ow2VioUpdateCurPos(Coord);
    }

    return(NO_ERROR);
}


DWORD
KbdEchoAString(
    IN PUCHAR   String,
    IN ULONG    Length
    )
/*++

Routine Description:

    This routine echo <String>, which size is <Lenght> to console and update LVB.

Arguments:

    String - char string to send to console

    Length - string length

Return Value:


Note:

    ASCII EDIT string mode.

    Updates:

    -   SesGrp->WinCoord
    -   LVB

    Calls:

    -   Or2WinWriteConsoleA

--*/
{
    ULONG       NumWritten, NumChar, NumBytes, Num;
    COORD       OldCoord, Coord;
    UCHAR       Char;

    OldCoord = Coord = GET_CURRENT_COORD;

    for ( NumChar = 0, NumBytes = 0 ; NumChar < Length ; NumChar++ )
    {
        Char = String[NumChar];
        if ( Char >= ' ' )
        {
            KBD_BUFFER_ADDRESS[NumBytes++] = Char;
        } else if ( Char = '\t' )
        {
            Num = 8 - ((Coord.X + NumBytes) & 7);
            memset(&KBD_BUFFER_ADDRESS[NumBytes], ' ', Num);
            NumBytes += Num;
        } else
        {
            KBD_BUFFER_ADDRESS[NumBytes++] = '^';
            KBD_BUFFER_ADDRESS[NumBytes++] = (UCHAR)(Char + '@');
        }

        if (((ULONG)( Coord.X + NumBytes ) >= (ULONG)SesGrp->ScreenColNum ) ||
            ( NumChar == ( Length - 1 )))
        {
            if(!Or2WinWriteConsoleA(
                               #if DBG
                               KbdEchoAStringStr,
                               #endif
                               hConOut,
                               (LPSTR)KBD_BUFFER_ADDRESS,
                               NumBytes,
                               &NumWritten,
                               NULL))
            {
#if DBG
                ASSERT1("OS2SES(KbdEchoAString): failed on WriteConsoleA", FALSE);
#endif
            }

#if DBG
            if ( NumBytes != NumWritten )
            {
                KdPrint(("OS2SES(KbdEchoAString): partial data WriteConsoleA %u from %u\n",
                        NumWritten, NumBytes));
                ASSERT( FALSE );
            }
#endif
            OldCoord = Coord;
            Coord.X += (SHORT)NumBytes;
            if ( Coord.X >= SesGrp->ScreenColNum )
            {
                Coord.Y++;
                Coord.X -= SesGrp->ScreenColNum;

                if ( Coord.Y >= SesGrp->ScreenRowNum )
                {
                    Coord.Y = SesGrp->ScreenRowNum - 1;
                    OldCoord.Y--;
                }
            }

            Ow2VioUpdateCurPos(Coord);

            VioLVBCopyStr(
                        &KBD_BUFFER_ADDRESS[0],
                        OldCoord,
                        NumBytes
                       );

            VioLVBFillAtt(
                       SesGrp->AnsiCellAttr,
                       OldCoord,
                       NumBytes
                      );
            NumBytes = 0;
        }
    }
    return(NO_ERROR);
}


BOOL
KbdKeyIsTurnAround(
    IN  UCHAR   Char,
    IN  UCHAR   Scan
    )
{
    if ( KbdTurnAroundCharTwo )
    {
        if ( Scan == (UCHAR)KbdTurnAroundChar )
        {
            if ((( KbdReadMode == 0 ) &&
                 (( Char == 0 ) || ( Char == 0xE0 ))) ||
                (( KbdReadMode == 1 ) && ( Char == HIBYTE(KbdTurnAroundChar) )))
            {
                return(TRUE);
            }
        }
    } else
    {
         if ( Char == (UCHAR)KbdTurnAroundChar )
         {
             return(TRUE);
         }
    }

    return(FALSE);
}