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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/system/terminals.h"
#include "gate/platforms.h"
#include "gate/atomics.h"
#include "gate/results.h"
#include "gate/debugging.h"
#include "gate/inputs.h"
#if defined(GATE_SYS_WIN) && !defined(GATE_SYS_WIN16)
# define GATE_SYSTEM_TERMINALS_WINAPI 1
#elif defined(GATE_SYS_POSIX)
# define GATE_SYSTEM_TERMINALS_POSIX 1
#elif defined(GATE_SYS_DOS)
# define GATE_SYSTEM_TERMINALS_DOS 1
#elif defined(GATE_SYS_EFI)
# define GATE_SYSTEM_TERMINALS_EFI 1
#else
# define GATE_SYSTEM_TERMINALS_NOIMPL 1
#endif
/* generic definitions and implementations */
static void gate_terminal_console_release(void* thisptr);
static int gate_terminal_console_retain(void* thisptr);
static char const* gate_terminal_console_get_interface_name(void* thisptr);
static gate_result_t gate_terminal_console_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
static gate_result_t gate_terminal_console_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned);
static gate_result_t gate_terminal_console_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
static gate_result_t gate_terminal_console_flush(void* thisptr);
static gate_result_t gate_terminal_console_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* resource);
static gate_result_t gate_terminal_console_await_char(void* thisptr, gate_uint32_t timeout_ms);
static gate_result_t gate_terminal_console_read_char(void* thisptr, gate_char32_t* ptr_char);
static gate_result_t gate_terminal_console_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written);
static gate_result_t gate_terminal_console_flush_err(void* thisptr);
static gate_result_t gate_terminal_console_get_type(void* thisptr);
static gate_result_t gate_terminal_console_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height);
static gate_result_t gate_terminal_console_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y);
static gate_result_t gate_terminal_console_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y);
static gate_result_t gate_terminal_console_set_cursor_mode(void* thisptr, gate_uint16_t mode);
static gate_result_t gate_terminal_console_clear(void* thisptr);
static gate_result_t gate_terminal_console_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code);
static gate_result_t gate_terminal_console_set_text_color(void* thisptr, gate_uint8_t color_code);
static gate_result_t gate_terminal_console_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code);
static gate_result_t gate_terminal_console_set_back_color(void* thisptr, gate_uint8_t color_code);
static GATE_INTERFACE_VTBL(gate_terminal) gate_terminal_console_vtbl;
static void gate_init_terminal_console_vtbl()
{
if (!gate_terminal_console_vtbl.get_interface_name)
{
GATE_INTERFACE_VTBL(gate_terminal) const local_vtbl =
{
&gate_terminal_console_get_interface_name,
&gate_terminal_console_release,
&gate_terminal_console_retain,
&gate_terminal_console_read,
&gate_terminal_console_peek,
&gate_terminal_console_write,
&gate_terminal_console_flush,
&gate_terminal_console_get_resource,
&gate_terminal_console_await_char,
&gate_terminal_console_read_char,
&gate_terminal_console_write_err,
&gate_terminal_console_flush_err,
&gate_terminal_console_get_type,
&gate_terminal_console_get_size,
&gate_terminal_console_get_cursor_pos,
&gate_terminal_console_set_cursor_pos,
&gate_terminal_console_set_cursor_mode,
&gate_terminal_console_clear,
&gate_terminal_console_get_text_color,
&gate_terminal_console_set_text_color,
&gate_terminal_console_get_back_color,
&gate_terminal_console_set_back_color
};
gate_terminal_console_vtbl = local_vtbl;
}
}
static gate_result_t gate_terminal_vt100_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
char buffer[128];
static gate_size_t len = sizeof(buffer) - 1;
gate_size_t used = 0;
++x;
++y;
used += gate_str_print_text(&buffer[used], len - used, "\x1b[", 2);
used += gate_str_print_uint16(&buffer[used], len - used, y);
used += gate_str_print_text(&buffer[used], len - used, ";", 1);
used += gate_str_print_uint16(&buffer[used], len - used, x);
used += gate_str_print_text(&buffer[used], len - used, "H", 1);
return gate_terminal_console_write(thisptr, buffer, used, NULL);
}
static gate_result_t gate_terminal_vt100_clear(void* thisptr)
{
#define cmd_clear "\x1b[0;40;37m\x1b[1;1H\x1b[2J"
gate_size_t const cmd_clear_size = gate_str_length(cmd_clear);
return gate_terminal_console_write(thisptr, cmd_clear, cmd_clear_size, NULL);
}
static gate_result_t gate_terminal_get_text_color_code(int color_type, int* color_code, int* attr_code)
{
switch (color_type)
{
case GATE_TERMINAL_COLOR_BLACK: *color_code = 30; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_BLUE: *color_code = 34; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_GREEN: *color_code = 32; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_CYAN: *color_code = 36; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_RED: *color_code = 31; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_MAGENTA: *color_code = 35; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_ORANGE: *color_code = 33; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_WHITE: *color_code = 37; *attr_code = 0; break;
case GATE_TERMINAL_COLOR_GRAY: *color_code = 30; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_LIGHTBLUE: *color_code = 34; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_LIGHTGREEN: *color_code = 32; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_LIGHTCYAN: *color_code = 36; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_LIGHTRED: *color_code = 31; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_LIGHTMAGENTA: *color_code = 35; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_YELLOW: *color_code = 33; *attr_code = 1; break;
case GATE_TERMINAL_COLOR_LIGHTWHITE: *color_code = 37; *attr_code = 1; break;
default: return false;
}
return true;
}
static gate_result_t gate_terminal_get_back_color_code(int color_type, int* color_code)
{
switch (color_type)
{
case GATE_TERMINAL_COLOR_BLACK: *color_code = 40; break;
case GATE_TERMINAL_COLOR_BLUE: *color_code = 44; break;
case GATE_TERMINAL_COLOR_GREEN: *color_code = 42; break;
case GATE_TERMINAL_COLOR_CYAN: *color_code = 46; break;
case GATE_TERMINAL_COLOR_RED: *color_code = 41; break;
case GATE_TERMINAL_COLOR_MAGENTA: *color_code = 45; break;
case GATE_TERMINAL_COLOR_ORANGE: *color_code = 43; break;
case GATE_TERMINAL_COLOR_WHITE: *color_code = 47; break;
case GATE_TERMINAL_COLOR_GRAY: *color_code = 40; break;
case GATE_TERMINAL_COLOR_LIGHTBLUE: *color_code = 44; break;
case GATE_TERMINAL_COLOR_LIGHTGREEN: *color_code = 42; break;
case GATE_TERMINAL_COLOR_LIGHTCYAN: *color_code = 46; break;
case GATE_TERMINAL_COLOR_LIGHTRED: *color_code = 41; break;
case GATE_TERMINAL_COLOR_LIGHTMAGENTA: *color_code = 45; break;
case GATE_TERMINAL_COLOR_YELLOW: *color_code = 43; break;
case GATE_TERMINAL_COLOR_LIGHTWHITE: *color_code = 47; break;
default: return false;
}
return true;
}
static gate_result_t gate_terminal_console_set_color(void* thisptr, int attr, int textcolor, int backcolor)
{
char buffer[128];
gate_size_t const len = sizeof(buffer);
gate_size_t used = 0;
used += gate_str_print_text(&buffer[used], len - used, "\x1b[", 2);
used += gate_str_print_int32(&buffer[used], len - used, attr);
used += gate_str_print_text(&buffer[used], len - used, ";", 1);
used += gate_str_print_int32(&buffer[used], len - used, textcolor);
used += gate_str_print_text(&buffer[used], len - used, ";", 1);
used += gate_str_print_int32(&buffer[used], len - used, backcolor);
used += gate_str_print_text(&buffer[used], len - used, "m", 1);
return gate_terminal_console_write(thisptr, buffer, used, NULL);
}
static gate_result_t gate_terminal_vt100_set_text_color(void* thisptr, gate_uint8_t text_color_code)
{
int attr;
gate_uint8_t back_color_code = 0;
int textcolor;
int backcolor;
gate_terminal_console_get_back_color(thisptr, &back_color_code);
gate_terminal_get_text_color_code(text_color_code, &textcolor, &attr);
gate_terminal_get_back_color_code(back_color_code, &backcolor);
return gate_terminal_console_set_color(thisptr, attr, textcolor, backcolor);
}
static gate_result_t gate_terminal_vt100_set_back_color(void* thisptr, gate_uint8_t back_color_code)
{
int attr;
int textcolor;
int backcolor;
gate_uint8_t text_color_code;
gate_terminal_console_get_text_color(thisptr, &text_color_code);
gate_terminal_get_text_color_code(text_color_code, &textcolor, &attr);
gate_terminal_get_back_color_code(back_color_code, &backcolor);
return gate_terminal_console_set_color(thisptr, attr, textcolor, backcolor);
}
static gate_result_t gate_terminal_vt100_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_width);
GATE_UNUSED_ARG(ptr_height);
/*TODO*/
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_terminal_vt100_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_x);
GATE_UNUSED_ARG(ptr_y);
/*TODO*/
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_terminal_vt100_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_color_code);
/*TODO*/
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_terminal_vt100_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_color_code);
/*TODO*/
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_terminal_vt100_enable_cursor(void* thisptr, gate_bool_t enabled)
{
static char const* vt100_enable_cursor_code = "\x1b[25h";
static char const* vt100_disable_cursor_code = "\x1b[25l";
return gate_terminal_console_write(
thisptr,
(enabled ? vt100_enable_cursor_code : vt100_disable_cursor_code),
5,
NULL);
}
typedef struct vterm_cell_class
{
gate_char16_t character;
gate_uint8_t color;
gate_uint8_t flags;
} vterm_cell_t;
typedef struct vterm_class
{
GATE_INTERFACE_VTBL(gate_terminal) const* vtbl;
gate_atomic_int_t ref_counter;
gate_uint16_t cols;
gate_uint16_t rows;
gate_enumint_t flags;
gate_uint16_t cursor_x;
gate_uint16_t cursor_y;
gate_uint8_t cursor_color;
gate_terminal_read_char_t reader;
void* reader_param;
gate_terminal_write_char_t writer;
void* writer_param;
gate_char16_t input_buffer[4];
gate_uint32_t default_read_timeout_ms;
vterm_cell_t** lines;
} vterm_t;
static char const* vterm_get_interface_name(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_INTERFACE_NAME_TERMINAL;
}
static void vterm_destroy(vterm_t* self)
{
gate_mem_dealloc(self);
}
static void vterm_release(void* thisptr)
{
vterm_t* self = (vterm_t*)thisptr;
if (gate_atomic_int_dec(&self->ref_counter) == 0)
{
vterm_destroy(self);
}
}
static int vterm_retain(void* thisptr)
{
vterm_t* self = (vterm_t*)thisptr;
return gate_atomic_int_inc(&self->ref_counter);
}
static gate_result_t vterm_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
vterm_t* self = (vterm_t*)thisptr;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t vterm_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
vterm_t* self = (vterm_t*)thisptr;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTIMPLEMENTED;
}
static void vterm_scroll_line(vterm_t* self)
{
gate_size_t line_len = self->cols * sizeof(vterm_cell_t);
gate_size_t move_len = line_len * self->rows - 1;
vterm_cell_t* first_line = self->lines[0];
vterm_cell_t* second_line = self->lines[1];
vterm_cell_t* last_line = self->lines[self->rows - 1];
gate_size_t ndx;
gate_mem_move(first_line, second_line, move_len);
for (ndx = 0; ndx != self->cols; ++ndx)
{
last_line[ndx].character = ' ';
last_line[ndx].color = self->cursor_color;
}
}
static void vterm_newline(vterm_t* self)
{
self->cursor_x = 0;
++self->cursor_y;
if (self->cursor_y >= self->rows)
{
self->cursor_y = self->rows - 1;
vterm_scroll_line(self);
}
}
static void vterm_set_char(vterm_t* self, gate_char16_t chr)
{
vterm_cell_t* ptr_cell = &self->lines[self->cursor_y][self->cursor_x];
ptr_cell->character = chr;
ptr_cell->color = self->cursor_color;
}
static void vterm_write_char(vterm_t* self, gate_char16_t chr)
{
switch (chr)
{
case 8: /* backspace */
{
if (self->cursor_x > 0)
{
vterm_set_char(self, ' ');
--self->cursor_x;
}
break;
}
case 9: /* tab */
{
self->cursor_x += 4;
self->cursor_x -= (self->cursor_x % 4);
if (self->cursor_x >= self->cols)
{
vterm_newline(self);
}
break;
}
case 10: /* newline */
{
vterm_newline(self);
break;
}
case 13: /* carriage return */
{
self->cursor_x = 0;
break;
}
default:
{
vterm_set_char(self, chr);
++self->cursor_x;
if (self->cursor_x >= self->cols)
{
vterm_newline(self);
}
break;
}
}
}
static gate_result_t vterm_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
vterm_t* self = (vterm_t*)thisptr;
gate_result_t ret = GATE_RESULT_OK;
gate_size_t processed = 0;
gate_char16_t chr16;
while (bufferlength > 0)
{
chr16 = *buffer;
vterm_write_char(self, chr16);
if (self->writer)
{
self->writer(chr16, self->writer_param);
}
--bufferlength;
++buffer;
++processed;
}
if (written)
{
*written = processed;
}
return ret;
}
static gate_result_t vterm_flush(void* thisptr)
{
vterm_t* self = (vterm_t*)thisptr;
GATE_UNUSED_ARG(self);
return GATE_RESULT_OK;
}
static gate_result_t vterm_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* resource)
{
vterm_t* self = (vterm_t*)thisptr;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(resource_type);
GATE_UNUSED_ARG(resource);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t vterm_await_char(void* thisptr, gate_uint32_t timeout_ms)
{
vterm_t* self = (vterm_t*)thisptr;
gate_result_t ret = GATE_RESULT_NOTAVAILABLE;
gate_char16_t chr16;
if (self->input_buffer[0])
{
/* we have something in the input buffer */
ret = GATE_RESULT_OK;
}
else
{
if (self->reader)
{
ret = self->reader(&chr16, timeout_ms, self->reader_param);
if (GATE_SUCCEEDED(ret))
{
self->input_buffer[0] = chr16;
}
}
}
return ret;
}
static gate_result_t vterm_read_char(void* thisptr, gate_char32_t* ptr_char)
{
vterm_t* self = (vterm_t*)thisptr;
gate_result_t ret = GATE_RESULT_NOTAVAILABLE;
do
{
ret = vterm_await_char(self, self->default_read_timeout_ms);
GATE_BREAK_IF_FAILED(ret);
if (self->input_buffer[0])
{
if (ptr_char)
{
*ptr_char = self->input_buffer[0];
}
self->input_buffer[0] = 0;
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_NOTAVAILABLE;
}
} while (0);
return ret;
}
static gate_result_t vterm_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
return vterm_write(thisptr, buffer, bufferlength, written);
}
static gate_result_t vterm_flush_err(void* thisptr)
{
return vterm_flush(thisptr);
}
static gate_enumint_t vterm_get_type(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_TERMINAL_TYPE_VT100;
}
static gate_result_t vterm_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
vterm_t* self = (vterm_t*)thisptr;
if (ptr_width)
{
*ptr_width = self->cols;
}
if (ptr_height)
{
*ptr_height = self->rows;
}
return GATE_RESULT_OK;
}
static gate_result_t vterm_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
vterm_t* self = (vterm_t*)thisptr;
if (ptr_x)
{
*ptr_x = self->cursor_x;
}
if (ptr_y)
{
*ptr_y = self->cursor_y;
}
return GATE_RESULT_OK;
}
static gate_result_t vterm_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
vterm_t* self = (vterm_t*)thisptr;
if (x >= self->cols)
{
x = self->cols - 1;
}
if (y >= self->rows)
{
y = self->rows - 1;
}
self->cursor_x = x;
self->cursor_y = y;
return GATE_RESULT_OK;
}
static gate_result_t vterm_set_cursor_mode(void* thisptr, gate_uint16_t mode)
{
vterm_t* self = (vterm_t*)thisptr;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(mode);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t vterm_clear(void* thisptr)
{
vterm_t* self = (vterm_t*)thisptr;
unsigned x, y;
vterm_cell_t* ptr_cell;<--- The scope of the variable 'ptr_cell' can be reduced. [+]The scope of the variable 'ptr_cell' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
for (y = 0; y != self->rows; ++y)
{
ptr_cell = self->lines[y];
for (x = 0; x != self->cols; ++x)
{
ptr_cell->character = ' ';
ptr_cell->color = self->cursor_color;
ptr_cell->flags = 0;
++ptr_cell;
}
}
return GATE_RESULT_OK;
}
static gate_result_t vterm_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
vterm_t* self = (vterm_t*)thisptr;
if (ptr_color_code)
{
*ptr_color_code = self->cursor_color & 0x0f;
}
return GATE_RESULT_OK;
}
static gate_result_t vterm_set_text_color(void* thisptr, gate_uint8_t color_code)
{
vterm_t* self = (vterm_t*)thisptr;
self->cursor_color = (self->cursor_color & 0xf0) | (color_code & 0xf);
return GATE_RESULT_OK;
}
static gate_result_t vterm_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
vterm_t* self = (vterm_t*)thisptr;
if (ptr_color_code)
{
*ptr_color_code = ((self->cursor_color & 0xf0) >> 4);
}
return GATE_RESULT_OK;
}
static gate_result_t vterm_set_back_color(void* thisptr, gate_uint8_t color_code)
{
vterm_t* self = (vterm_t*)thisptr;
self->cursor_color = (self->cursor_color & 0x0f) | ((color_code & 0xf) << 4);
return GATE_RESULT_OK;
}
static GATE_INTERFACE_VTBL(gate_terminal) vterm_vtbl;
static void init_vterm_vtbl()
{
if (vterm_vtbl.get_interface_name == NULL)
{
GATE_INTERFACE_VTBL(gate_terminal) const vtbl = {
&vterm_get_interface_name,
&vterm_release,
&vterm_retain,
&vterm_read,
&vterm_peek,
&vterm_write,
&vterm_flush,
&vterm_get_resource,
&vterm_await_char,
&vterm_read_char,
&vterm_write_err,
&vterm_flush_err,
&vterm_get_type,
&vterm_get_size,
&vterm_get_cursor_pos,
&vterm_set_cursor_pos,
&vterm_set_cursor_mode,
&vterm_clear,
&vterm_get_text_color,
&vterm_set_text_color,
&vterm_get_back_color,
&vterm_set_back_color
};
vterm_vtbl = vtbl;
}
}
gate_terminal_t* gate_terminal_virtual_create(gate_uint16_t columns, gate_uint16_t rows, gate_enumint_t flags,
gate_terminal_read_char_t read_callback, void* read_param,
gate_terminal_write_char_t write_callback, void* write_param)
{
gate_terminal_t* ret = NULL;
vterm_t* ptr = NULL;
gate_size_t line_length;
gate_size_t additional_size;
char* first_line_ptr;
char* first_cell_ptr;<--- The scope of the variable 'first_cell_ptr' can be reduced. [+]The scope of the variable 'first_cell_ptr' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t ndx;
do
{
if (columns == 0) columns = 80;
if (rows == 0) rows = 25;
line_length = sizeof(vterm_cell_t) * columns;
additional_size = rows * line_length + (rows * sizeof(vterm_cell_t*));
ptr = (vterm_t*)gate_mem_alloc(sizeof(vterm_t) + additional_size);
if (!ptr)
{
break;
}
gate_mem_clear(ptr, sizeof(vterm_t) + additional_size);
init_vterm_vtbl();
ptr->vtbl = &vterm_vtbl;
gate_atomic_int_init(&ptr->ref_counter, 1);
ptr->cols = columns;
ptr->rows = rows;
ptr->flags = flags;
ptr->cursor_x = 0;
ptr->cursor_y = 0;
ptr->cursor_color = 0x07;
ptr->reader = read_callback;
ptr->reader_param = read_param;
ptr->writer = write_callback;
ptr->writer_param = write_param;
ptr->default_read_timeout_ms = 4000;
first_line_ptr = (char*)ptr + sizeof(vterm_t);
first_cell_ptr = first_line_ptr + (sizeof(vterm_cell_t*) * rows);
ptr->lines = (vterm_cell_t**)first_line_ptr;
for (ndx = 0; ndx != rows; ++ndx)
{
ptr->lines[ndx] = (vterm_cell_t*)(first_cell_ptr + line_length * ndx);
}
ret = (gate_terminal_t*)ptr;
ptr = NULL;
} while (0);
if (ptr)
{
vterm_destroy(ptr);
}
return ret;
}
#if defined(GATE_SYSTEM_TERMINALS_WINAPI)
#include "gate/times.h"
#if defined(GATE_SYS_WINCE)
# include <stdio.h>
#endif
typedef struct gate_terminal_console_impl_class
{
GATE_INTERFACE_VTBL(gate_terminal) const* vtbl;
gate_atomic_int_t ref_counter;
gate_uint32_t flags;
HANDLE con_input;
HANDLE con_output;
HANDLE con_error;
gate_char32_t char_buffer[16];
gate_size_t char_buffer_used;
} gate_terminal_console_impl_t;
static void close_file_handle(HANDLE handle)
{
if ((handle != NULL) && (handle != INVALID_HANDLE_VALUE))
{
CloseHandle(handle);
}
}
#ifndef GATE_SYS_WINCE
#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
#endif
#ifndef ENABLE_QUICK_EDIT_MODE
#define ENABLE_QUICK_EDIT_MODE 0x0040
#endif
#ifndef ENABLE_INSERT_MODE
#define ENABLE_INSERT_MODE 0x0020
#endif
static BOOL gate_terminal_console_set_input_flags(HANDLE handle, gate_uint32_t flags)
{
BOOL ret = FALSE;
DWORD dwMode = 0;
if (!GetConsoleMode(handle, &dwMode))
{
return FALSE;
}
GATE_FLAG_SET(dwMode, ENABLE_WINDOW_INPUT, true);
GATE_FLAG_SET(dwMode, ENABLE_QUICK_EDIT_MODE, false);
GATE_FLAG_SET(dwMode, ENABLE_PROCESSED_INPUT, !GATE_FLAG_ENABLED(flags, GATE_TERMINAL_FLAG_GETCTRL));
GATE_FLAG_SET(dwMode, ENABLE_MOUSE_INPUT, true);
GATE_FLAG_SET(dwMode, ENABLE_INSERT_MODE, false);
GATE_FLAG_SET(dwMode, ENABLE_ECHO_INPUT, GATE_FLAG_ENABLED(flags, GATE_TERMINAL_FLAG_ECHO));
ret = SetConsoleMode(handle, dwMode);
if (GATE_SUCCEEDED(ret))
{
// VT100 support is optional (not available before Windows 10)
GATE_FLAG_SET(dwMode, ENABLE_VIRTUAL_TERMINAL_INPUT, GATE_FLAG_ENABLED(flags, GATE_TERMINAL_FLAG_VT100));
}
return ret;
}
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
static BOOL gate_terminal_console_set_output_flags(HANDLE handle, gate_uint32_t flags)
{
BOOL ret = FALSE;
DWORD dwMode = 0;
if (!GetConsoleMode(handle, &dwMode))
{
return FALSE;
}
GATE_FLAG_SET(dwMode, ENABLE_PROCESSED_OUTPUT, true);
GATE_FLAG_SET(dwMode, ENABLE_WRAP_AT_EOL_OUTPUT, true);
ret = SetConsoleMode(handle, dwMode);
if (GATE_SUCCEEDED(ret))
{
// VT100 support is optional (not available before Windows 10)
GATE_FLAG_SET(dwMode, ENABLE_VIRTUAL_TERMINAL_PROCESSING, GATE_FLAG_ENABLED(flags, GATE_TERMINAL_FLAG_VT100));
}
return ret;
}
#endif
gate_terminal_t* gate_terminal_console_get_current()
{
gate_terminal_t* ret = NULL;
gate_terminal_console_impl_t* impl = NULL;
HANDLE hProc = GetCurrentProcess();
HANDLE hConOut, hConIn, hConErr;
HANDLE hOutput = INVALID_HANDLE_VALUE;
HANDLE hInput = INVALID_HANDLE_VALUE;
HANDLE hError = INVALID_HANDLE_VALUE;
do
{
/* OUTPUT handle */
#if defined(GATE_SYS_WINCE)
# if defined(GATE_COMPILER_MSVC)
hConOut = (HANDLE)fileno(stdout);
hConIn = (HANDLE)fileno(stdin);
hConErr = (HANDLE)fileno(stderr);
# else
hConOut = (HANDLE)STDOUT_FILENO;
hConIn = (HANDLE)STDIN_FILENO;
hConErr = (HANDLE)STDERR_FILENO;
# endif
#else
hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
hConIn = GetStdHandle(STD_INPUT_HANDLE);
hConErr = GetStdHandle(STD_ERROR_HANDLE);
#endif
if ((hConOut == INVALID_HANDLE_VALUE) || (hConIn == INVALID_HANDLE_VALUE))
{
break;
}
if (!DuplicateHandle(hProc, hConOut, hProc, &hOutput, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
break;
}
if (!DuplicateHandle(hProc, hConIn, hProc, &hInput, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
break;
}
if (hConErr != INVALID_HANDLE_VALUE)
{
if (!DuplicateHandle(hProc, hConErr, hProc, &hError, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
break;
}
}
else
{
hError = INVALID_HANDLE_VALUE;
}
impl = (gate_terminal_console_impl_t*)gate_mem_alloc(sizeof(gate_terminal_console_impl_t));
if (impl == NULL)
{
break;
}
gate_mem_clear(impl, sizeof(gate_terminal_console_impl_t));
gate_init_terminal_console_vtbl();
impl->vtbl = &gate_terminal_console_vtbl;
gate_atomic_int_init(&impl->ref_counter, 1);
impl->con_input = hInput;
impl->con_output = hOutput;
impl->con_error = hError;
hInput = NULL;
hOutput = NULL;
hError = NULL;
ret = (gate_terminal_t*)impl;
impl = NULL;
} while (0);
close_file_handle(hInput);
close_file_handle(hOutput);
close_file_handle(hError);
if (impl != NULL)
{
gate_mem_dealloc(impl);
}
return ret;
}
#ifdef GATE_SYS_WINCE
gate_terminal_t* gate_terminal_console_open(gate_enumint_t flags)
{
return gate_terminal_console_get_current();
}
#else
gate_terminal_t* gate_terminal_console_open(gate_enumint_t flags)
{
gate_terminal_t* ret = NULL;
gate_terminal_console_impl_t* impl = NULL;
HANDLE hProc = GetCurrentProcess();
HANDLE hCon = INVALID_HANDLE_VALUE;
HANDLE hInput = INVALID_HANDLE_VALUE;
HANDLE hOutput = INVALID_HANDLE_VALUE;
HANDLE hError = INVALID_HANDLE_VALUE;
do
{
AllocConsole();
/* open console input buffer */
hInput = gate_win32_createfile(_T("CONIN$"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
OPEN_EXISTING, 0, 0, NULL);
if (INVALID_HANDLE_VALUE == hInput)
{
/* try stdin as alternative */
if (!DuplicateHandle(hProc, GetStdHandle(STD_INPUT_HANDLE), hProc, &hInput, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
break;
}
}
/* open console output buffer */
hCon = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
if (INVALID_HANDLE_VALUE == hCon)
{
break;
}
if (!DuplicateHandle(hProc, hCon, hProc, &hOutput, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
break;
}
if (!DuplicateHandle(hProc, hCon, hProc, &hError, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
break;
}
impl = (gate_terminal_console_impl_t*)gate_mem_alloc(sizeof(gate_terminal_console_impl_t));
if (impl == NULL)
{
break;
}
if (!SetConsoleActiveScreenBuffer(hOutput))
{
break;
}
if (!gate_terminal_console_set_output_flags(hOutput, flags))
{
break;
}
if (!gate_terminal_console_set_input_flags(hInput, flags))
{
break;
}
gate_mem_clear(impl, sizeof(gate_terminal_console_impl_t));
gate_init_terminal_console_vtbl();
impl->vtbl = &gate_terminal_console_vtbl;
gate_atomic_int_init(&impl->ref_counter, 1);
impl->con_input = hInput;
impl->con_output = hOutput;
impl->con_error = hError;
impl->flags = flags;
hInput = NULL;
hOutput = NULL;
hError = NULL;
ret = (gate_terminal_t*)impl;
impl = NULL;
} while (0);
close_file_handle(hCon);
close_file_handle(hInput);
close_file_handle(hOutput);
close_file_handle(hError);
if (impl != NULL)
{
gate_mem_dealloc(impl);
}
return ret;
}
#endif
void gate_terminal_console_release(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
if (0 == gate_atomic_int_dec(&impl->ref_counter))
{
close_file_handle(impl->con_input);
close_file_handle(impl->con_output);
close_file_handle(impl->con_error);
gate_mem_dealloc(impl);
}
}
int gate_terminal_console_retain(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
return gate_atomic_int_inc(&impl->ref_counter);
}
char const* gate_terminal_console_get_interface_name(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_INTERFACE_NAME_TERMINAL;
}
gate_result_t gate_terminal_console_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
DWORD chars_read;
do
{
#ifdef GATE_SYS_WINCE
if (!gate_win32_readfile(impl->con_input, buffer, (DWORD)bufferlength, &chars_read, NULL))
#else
if (!ReadConsole(impl->con_input, buffer, (DWORD)bufferlength, &chars_read, NULL))
#endif
{
DWORD lasterr = GetLastError();
if (lasterr != ERROR_INVALID_FUNCTION)
{
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
break;
}
if (!gate_win32_readfile(impl->con_input, buffer, (DWORD)bufferlength, &chars_read, NULL))
{
lasterr = GetLastError();
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
break;
}
}
if ((gate_size_t)chars_read < bufferlength)
{
buffer[chars_read] = 0;
}
if (returned)
{
*returned = (gate_size_t)chars_read;
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_terminal_console_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_terminal_console_write_to_handle(HANDLE handle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_result_t ret = GATE_RESULT_OK;
DWORD chars_written = 0;
TCHAR text_buffer[7168];
TCHAR* ptr_text = &text_buffer[0];
gate_size_t text_len = sizeof(text_buffer) / sizeof(text_buffer[0]);
gate_size_t text_used;
do
{
if (bufferlength > text_len)
{
text_len = bufferlength * sizeof(TCHAR);
ptr_text = (TCHAR*)gate_mem_alloc(text_len);
if (ptr_text == NULL)
{
ret = GATE_RESULT_FAILED;
break;
}
}
text_used = gate_win32_utf8_2_winstr(buffer, bufferlength, ptr_text, text_len);
#ifdef GATE_SYS_WINCE
if (!gate_win32_writefile(handle, buffer, (DWORD)bufferlength, &chars_written, NULL))
#else
if (!WriteConsole(handle, ptr_text, (DWORD)text_used, &chars_written, NULL))
#endif
{
DWORD lasterr = GetLastError();
if (lasterr != ERROR_INVALID_FUNCTION)
{
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
break;
}
if (!gate_win32_writefile(handle, buffer, (DWORD)bufferlength, &chars_written, NULL))
{
lasterr = GetLastError();
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
break;
}
}
if (NULL != written)
{
if (chars_written == text_used)
{
*written = bufferlength;
}
else
{
*written = (gate_size_t)chars_written;
}
}
ret = GATE_RESULT_OK;
} while (0);
if ((ptr_text != NULL) && (ptr_text != &text_buffer[0]))
{
gate_mem_dealloc(ptr_text);
}
return ret;
}
gate_result_t gate_terminal_console_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
return gate_terminal_console_write_to_handle(impl->con_output, buffer, bufferlength, written);
}
gate_result_t gate_terminal_console_flush(void* thisptr)
{
gate_result_t ret = GATE_RESULT_OK;
GATE_UNUSED_ARG(thisptr);
return ret;
}
gate_result_t gate_terminal_console_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* resource)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
/* TODO */
do
{
if (!resource)
{
ret = GATE_RESULT_NULLPOINTER;
break;
}
switch (resource_type)
{
case GATE_STREAM_RESOURCE_DEFAULT:
case GATE_STREAM_RESOURCE_INPUT:
{
*resource = (gate_uintptr_t)impl->con_input;
ret = GATE_RESULT_OK;
}
case GATE_STREAM_RESOURCE_OUTPUT:
{
*resource = (gate_uintptr_t)impl->con_output;
ret = GATE_RESULT_OK;
}
case GATE_STREAM_RESOURCE_ERROR:
{
*resource = (gate_uintptr_t)impl->con_error;
ret = GATE_RESULT_OK;
}
default:
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
}
} while (0);
return ret;
}
static gate_char32_t map_scan_code_to_ctrl_ascii_code(WORD vkey_code)
{
if ((vkey_code >= 0x41) && (vkey_code <= 0x5A))
{
return GATE_TERMINAL_CHAR_CTRL_A + (vkey_code - 0x41);
}
return 0;
}
static gate_char32_t gate_scan_code_to_alt_ascii_code(WORD vkey_code)
{
if ((vkey_code >= 0x41) && (vkey_code <= 0x5A))
{
switch ((vkey_code - 0x41))
{
case 0: return GATE_TERMINAL_CHAR_ALT_A;
case 1: return GATE_TERMINAL_CHAR_ALT_B;
case 2: return GATE_TERMINAL_CHAR_ALT_C;
case 3: return GATE_TERMINAL_CHAR_ALT_D;
case 4: return GATE_TERMINAL_CHAR_ALT_E;
case 5: return GATE_TERMINAL_CHAR_ALT_F;
case 6: return GATE_TERMINAL_CHAR_ALT_G;
case 7: return GATE_TERMINAL_CHAR_ALT_H;
case 8: return GATE_TERMINAL_CHAR_ALT_I;
case 9: return GATE_TERMINAL_CHAR_ALT_J;
case 10: return GATE_TERMINAL_CHAR_ALT_K;
case 11: return GATE_TERMINAL_CHAR_ALT_L;
case 12: return GATE_TERMINAL_CHAR_ALT_M;
case 13: return GATE_TERMINAL_CHAR_ALT_N;
case 14: return GATE_TERMINAL_CHAR_ALT_O;
case 15: return GATE_TERMINAL_CHAR_ALT_P;
case 16: return GATE_TERMINAL_CHAR_ALT_Q;
case 17: return GATE_TERMINAL_CHAR_ALT_R;
case 18: return GATE_TERMINAL_CHAR_ALT_S;
case 19: return GATE_TERMINAL_CHAR_ALT_T;
case 20: return GATE_TERMINAL_CHAR_ALT_U;
case 21: return GATE_TERMINAL_CHAR_ALT_V;
case 22: return GATE_TERMINAL_CHAR_ALT_W;
case 23: return GATE_TERMINAL_CHAR_ALT_X;
case 24: return GATE_TERMINAL_CHAR_ALT_Y;
case 25: return GATE_TERMINAL_CHAR_ALT_Z;
}
}
else
{
switch (vkey_code)
{
case VK_F1: return GATE_TERMINAL_CHAR_ALT_F1;
case VK_F2: return GATE_TERMINAL_CHAR_ALT_F2;
case VK_F3: return GATE_TERMINAL_CHAR_ALT_F3;
case VK_F4: return GATE_TERMINAL_CHAR_ALT_F4;
case VK_F5: return GATE_TERMINAL_CHAR_ALT_F5;
case VK_F6: return GATE_TERMINAL_CHAR_ALT_F6;
case VK_F7: return GATE_TERMINAL_CHAR_ALT_F7;
case VK_F8: return GATE_TERMINAL_CHAR_ALT_F8;
case VK_F9: return GATE_TERMINAL_CHAR_ALT_F9;
case VK_F10: return GATE_TERMINAL_CHAR_ALT_F10;
case VK_F11: return GATE_TERMINAL_CHAR_ALT_F11;
case VK_F12: return GATE_TERMINAL_CHAR_ALT_F12;
}
}
return 0;
}
static gate_char32_t gate_special_key_to_ascii_code(WORD vkey)
{
switch (vkey)
{
case VK_PRIOR: return GATE_TERMINAL_CHAR_PGUP;
case VK_NEXT: return GATE_TERMINAL_CHAR_PGDOWN;
case VK_HOME: return GATE_TERMINAL_CHAR_HOME;
case VK_END: return GATE_TERMINAL_CHAR_END;
case VK_UP: return GATE_TERMINAL_CHAR_UPARROW;
case VK_DOWN: return GATE_TERMINAL_CHAR_DOWNARROW;
case VK_LEFT: return GATE_TERMINAL_CHAR_LEFTARROW;
case VK_RIGHT: return GATE_TERMINAL_CHAR_RIGHTARROW;
case VK_F1: return GATE_TERMINAL_CHAR_F1;
case VK_F2: return GATE_TERMINAL_CHAR_F2;
case VK_F3: return GATE_TERMINAL_CHAR_F3;
case VK_F4: return GATE_TERMINAL_CHAR_F4;
case VK_F5: return GATE_TERMINAL_CHAR_F5;
case VK_F6: return GATE_TERMINAL_CHAR_F6;
case VK_F7: return GATE_TERMINAL_CHAR_F7;
case VK_F8: return GATE_TERMINAL_CHAR_F8;
case VK_F9: return GATE_TERMINAL_CHAR_F9;
case VK_F10: return GATE_TERMINAL_CHAR_F10;
case VK_F11: return GATE_TERMINAL_CHAR_F11;
case VK_F12: return GATE_TERMINAL_CHAR_F12;
case VK_TAB:
case VK_CAPITAL:
case VK_BACK:
case VK_RETURN:
case VK_ESCAPE:
case VK_INSERT:
case VK_DELETE:
case VK_PRINT:
case VK_PAUSE:
case VK_SCROLL:
case VK_NUMLOCK:
default:
return 0;
}
}
gate_result_t gate_terminal_console_read_char_impl(void* thisptr, gate_char32_t* ptr_char, gate_uint32_t timeout_ms)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
#ifdef GATE_SYS_WINCE
ret = GATE_RESULT_NOTSUPPORTED;
#else
gate_timecounter_t tc_start;
gate_timecounter_t tc_now;
gate_int64_t time_diff;
DWORD wait = timeout_ms;
INPUT_RECORD input_record = GATE_INIT_EMPTY;
DWORD event_count;
DWORD wait_result;
gate_char32_t chr;
/* TODO */
do
{
ret = gate_timecounter_now(&tc_start);
GATE_BREAK_IF_FAILED(ret);
if (impl->char_buffer_used != 0)
{
if (ptr_char)
{
*ptr_char = impl->char_buffer[0];
}
if (--impl->char_buffer_used != 0)
{
gate_mem_copy(&impl->char_buffer[0], &impl->char_buffer[1], impl->char_buffer_used * sizeof(impl->char_buffer[0]));
}
return GATE_RESULT_OK;
}
ret = GATE_RESULT_FAILED;
while (WAIT_OBJECT_0 == (wait_result = WaitForSingleObject(impl->con_input, wait)))
{
event_count = 0;
if (!ReadConsoleInput(impl->con_input, &input_record, 1, &event_count))
{
GATE_DEBUG_TRACE("ReadConsoleInput() failed");
GATE_DEBUG_TRACE_VALUE(gate_win32_getlasterror());
}
else
{
if (input_record.EventType == KEY_EVENT)
{
if (input_record.Event.KeyEvent.bKeyDown)
{
if ((input_record.Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED)
|| ((input_record.Event.KeyEvent.dwControlKeyState & RIGHT_CTRL_PRESSED)))
{
chr = map_scan_code_to_ctrl_ascii_code(input_record.Event.KeyEvent.wVirtualKeyCode);
if (chr != 0)
{
if (ptr_char)
{
*ptr_char = chr;
}
else
{
impl->char_buffer[0] = chr;
impl->char_buffer_used = 1;
}
ret = GATE_RESULT_OK;
break;
}
}
if ((input_record.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED)
|| ((input_record.Event.KeyEvent.dwControlKeyState & RIGHT_ALT_PRESSED)))
{
chr = gate_scan_code_to_alt_ascii_code(input_record.Event.KeyEvent.wVirtualScanCode);
if (chr != 0)
{
if (ptr_char)
{
*ptr_char = 0;
impl->char_buffer[0] = chr;
impl->char_buffer_used = 1;
}
else
{
impl->char_buffer[0] = 0;
impl->char_buffer[1] = chr;
impl->char_buffer_used = 2;
}
ret = GATE_RESULT_OK;
break;
}
}
chr = gate_special_key_to_ascii_code(input_record.Event.KeyEvent.wVirtualScanCode);
if (chr != 0)
{
if (ptr_char)
{
*ptr_char = 0;
impl->char_buffer[0] = chr;
impl->char_buffer_used = 1;
}
else
{
impl->char_buffer[0] = 0;
impl->char_buffer[1] = chr;
impl->char_buffer_used = 2;
}
ret = GATE_RESULT_OK;
break;
}
if (input_record.Event.KeyEvent.uChar.UnicodeChar != 0)
{
/* we have an unicode character */
if (ptr_char)
{
*ptr_char = input_record.Event.KeyEvent.uChar.UnicodeChar;
}
else
{
impl->char_buffer[0] = input_record.Event.KeyEvent.uChar.UnicodeChar;
impl->char_buffer_used = 1;
}
ret = GATE_RESULT_OK;
break;
}
/* we could not deduce a code for an entered key -> ignore input */
}
}
}
ret = gate_timecounter_now(&tc_now);
GATE_BREAK_IF_FAILED(ret);
time_diff = gate_timecounter_diff(tc_now, tc_start) / 1000;
if (time_diff >= timeout_ms)
{
ret = GATE_RESULT_TIMEOUT;
break;
}
wait = (DWORD)(timeout_ms - time_diff);
}
if (wait_result == WAIT_TIMEOUT)
{
ret = GATE_RESULT_TIMEOUT;
}
else if (wait_result != WAIT_OBJECT_0)
{
ret = GATE_RESULT_FAILED;
}
} while (0);
#endif
return ret;
}
static gate_bool_t is_control_key(WORD keyCode)
{
switch (keyCode)
{
case VK_SHIFT:
case VK_CONTROL:
case VK_MENU:
case VK_LSHIFT:
case VK_RSHIFT:
case VK_LCONTROL:
case VK_RCONTROL:
case VK_LMENU:
case VK_RMENU:
return true;
default:
return false;
}
}
gate_result_t gate_terminal_console_await_char(void* thisptr, gate_uint32_t timeout_ms)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* ptr = (gate_terminal_console_impl_t*)thisptr;
#ifdef GATE_SYS_WINCE
ret = GATE_RESULT_NOTSUPPORTED;
#else
gate_timecounter_t tc_start;
gate_timecounter_t tc_now;
gate_int64_t time_diff;
DWORD next_timeout = (timeout_ms == GATE_CONSOLE_TIMEOUT_NEVER) ? INFINITE : (DWORD)timeout_ms;
INPUT_RECORD input_record = GATE_INIT_EMPTY;
DWORD event_count;
DWORD wait_result;
HANDLE input_handle = ptr->con_input;
do
{
ret = gate_timecounter_now(&tc_start);
GATE_BREAK_IF_FAILED(ret);
ret = GATE_RESULT_FAILED;
while (WAIT_OBJECT_0 == (wait_result = WaitForSingleObject(input_handle, next_timeout)))
{
event_count = 0;
if (PeekConsoleInput(input_handle, &input_record, 1, &event_count))
{
if (input_record.EventType != KEY_EVENT)
{
/* not interested in out events -> remove event from buffer */
ReadConsoleInput(input_handle, &input_record, 1, &event_count);
}
else if (!input_record.Event.KeyEvent.bKeyDown)
{
/* not interested in key_up -> remove event */
ReadConsoleInput(input_handle, &input_record, 1, &event_count);
}
else if (is_control_key(input_record.Event.KeyEvent.wVirtualKeyCode))
{
/* ignore control keys -> remove event */
ReadConsoleInput(input_handle, &input_record, 1, &event_count);
}
else
{
/* next event is a readable key */
ret = GATE_RESULT_OK;
break;
}
}
ret = gate_timecounter_now(&tc_now);
GATE_BREAK_IF_FAILED(ret);
time_diff = gate_timecounter_diff(tc_now, tc_start) / 1000;
if (time_diff >= timeout_ms)
{
ret = GATE_RESULT_TIMEOUT;
break;
}
next_timeout = (DWORD)(timeout_ms - time_diff);
}
if (wait_result == WAIT_TIMEOUT)
{
ret = GATE_RESULT_TIMEOUT;
}
else if (wait_result != WAIT_OBJECT_0)
{
ret = GATE_RESULT_FAILED;
}
} while (0);
#endif
return ret;
}
gate_result_t gate_terminal_console_read_char(void* thisptr, gate_char32_t* ptr_char)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* ptr = (gate_terminal_console_impl_t*)thisptr;
#ifdef GATE_SYS_WINCE
ret = GATE_RESULT_NOTSUPPORTED;
#else
INPUT_RECORD input_record = GATE_INIT_EMPTY;
DWORD event_count = 0;
gate_char32_t chr;
HANDLE input_handle = ptr->con_input;
do
{
ret = gate_terminal_console_await_char(thisptr, GATE_CONSOLE_TIMEOUT_NEVER);
GATE_BREAK_IF_FAILED(ret);
event_count = 0;
if (!ReadConsoleInput(input_handle, &input_record, 1, &event_count))
{
ret = GATE_RESULT_FAILED;
break;
}
if (input_record.EventType != KEY_EVENT)
{
ret = GATE_RESULT_FAILED;
break;
}
if (!input_record.Event.KeyEvent.bKeyDown)
{
ret = GATE_RESULT_FAILED;
break;
}
#if defined(GATE_WIN32_UNICODE)
chr = input_record.Event.KeyEvent.uChar.UnicodeChar;
#else
chr = input_record.Event.KeyEvent.uChar.AsciiChar;
#endif
if (chr == 0)
{
gate_keyboard_native_key_to_unicode(input_record.Event.KeyEvent.wVirtualKeyCode, &chr);
}
*ptr_char = chr;
} while (0);
#endif
return ret;
}
gate_result_t gate_terminal_console_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_terminal_console_impl_t* const impl = (gate_terminal_console_impl_t*)thisptr;
return gate_terminal_console_write_to_handle(impl->con_error, buffer, bufferlength, written);
}
gate_result_t gate_terminal_console_flush_err(void* thisptr)
{
gate_result_t ret = GATE_RESULT_OK;
GATE_UNUSED_ARG(thisptr);
return ret;
}
#ifndef GATE_SYS_WINCE
static gate_bool_t is_real_console(gate_terminal_console_impl_t* impl)
{
DWORD con_mode = 0;
BOOL is_console = GetConsoleMode(impl->con_output, &con_mode);
return is_console ? true : false;
}
#endif
gate_enumint_t gate_terminal_console_get_type(void* thisptr)
{
#ifdef GATE_SYS_WINCE
return GATE_TERMINAL_TYPE_CHARSTREAM;
#else
gate_terminal_console_impl_t* const impl = (gate_terminal_console_impl_t*)thisptr;
return is_real_console(impl) ? GATE_TERMINAL_TYPE_UI : GATE_TERMINAL_TYPE_CHARSTREAM;
#endif
}
gate_result_t gate_terminal_console_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* const impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(impl->con_output, &info))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
if (ptr_width != NULL) *ptr_width = 80;
if (ptr_height != NULL) *ptr_height = 25;
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
if (ptr_width != NULL) *ptr_width = (gate_uint16_t)info.dwSize.X;
if (ptr_height != NULL) *ptr_height = (gate_uint16_t)info.dwSize.Y;
ret = GATE_RESULT_OK;
}
break;
}
#endif
ret = gate_terminal_vt100_get_size(thisptr, ptr_width, ptr_height);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* const impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(impl->con_output, &info))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
// return fake values
if (ptr_x != NULL) *ptr_x = 0;
if (ptr_y != NULL) *ptr_y = 0;
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
if (ptr_x != NULL) *ptr_x = (gate_uint16_t)info.dwCursorPosition.X;
if (ptr_y != NULL) *ptr_y = (gate_uint16_t)info.dwCursorPosition.Y;
ret = GATE_RESULT_OK;
break;
}
}
#endif
ret = gate_terminal_vt100_get_cursor_pos(thisptr, ptr_x, ptr_y);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
COORD coord;
coord.X = (SHORT)x;
coord.Y = (SHORT)y;
if (!SetConsoleCursorPosition(impl->con_output, coord))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
/* no action, but simulate success */
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
ret = GATE_RESULT_OK;
break;
}
}
#endif
ret = gate_terminal_vt100_set_cursor_pos(thisptr, x, y);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_set_cursor_mode(void* thisptr, gate_uint16_t mode)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_CURSOR_INFO info = GATE_INIT_EMPTY;
switch (mode)
{
case GATE_TERMINAL_CURSOR_MODE_OFF:
{
info.bVisible = false;
info.dwSize = 1;
break;
}
case GATE_TERMINAL_CURSOR_MODE_SMALL:
{
info.bVisible = true;
info.dwSize = 15;
break;
}
case GATE_TERMINAL_CURSOR_MODE_MEDIUM:
{
info.bVisible = true;
info.dwSize = 50;
break;
}
case GATE_TERMINAL_CURSOR_MODE_LARGE:
default:
{
info.bVisible = true;
info.dwSize = 100;
break;
}
}
if (SetConsoleCursorInfo(impl->con_output, &info))
{
ret = GATE_RESULT_OK;
break;
}
else
{
if (!is_real_console(impl))
{
/* simluate success */
ret = GATE_RESULT_OK;
break;
}
}
}
#endif
ret = gate_terminal_vt100_enable_cursor(impl, (mode == GATE_TERMINAL_CURSOR_MODE_OFF) ? false : true);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_clear(void* thisptr)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
do
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
if (!GetConsoleScreenBufferInfo(impl->con_output, &csbi))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
/* no change, simulate success */
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
break;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacter(impl->con_output, _T(' '), dwConSize, coordScreen, &cCharsWritten))
{
gate_win32_print_lasterror(&ret, NULL, 0);
break;
}
if (!GetConsoleScreenBufferInfo(impl->con_output, &csbi))
{
gate_win32_print_lasterror(&ret, NULL, 0);
break;
}
if (!FillConsoleOutputAttribute(impl->con_output, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
{
gate_win32_print_lasterror(&ret, NULL, 0);
break;
}
if (!SetConsoleCursorPosition(impl->con_output, coordScreen))
{
gate_win32_print_lasterror(&ret, NULL, 0);
break;
}
ret = GATE_RESULT_OK;
} while (0);
if (GATE_SUCCEEDED(ret))
{
break;
}
}
#endif
ret = gate_terminal_vt100_clear(thisptr);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
//if(!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(impl->con_output, &info))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
if (ptr_color_code != NULL) *ptr_color_code = GATE_TERMINAL_COLOR_WHITE;
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
if (ptr_color_code != NULL)
{
*ptr_color_code = info.wAttributes & 0x0f;
}
ret = GATE_RESULT_OK;
break;
}
}
#endif
ret = gate_terminal_vt100_get_text_color(thisptr, ptr_color_code);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_set_text_color(void* thisptr, gate_uint8_t color_code)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_SCREEN_BUFFER_INFO info = GATE_INIT_EMPTY;
GetConsoleScreenBufferInfo(impl->con_output, &info);
info.wAttributes &= 0xfff0;
info.wAttributes |= (gate_uint16_t)color_code;
if (!SetConsoleTextAttribute(impl->con_output, info.wAttributes))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
ret = GATE_RESULT_OK;
break;
}
}
#endif
ret = gate_terminal_vt100_set_text_color(thisptr, color_code);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
//if(!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(impl->con_output, &info))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
if (ptr_color_code) *ptr_color_code = GATE_TERMINAL_COLOR_BLACK;
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
if (NULL != ptr_color_code)
{
*ptr_color_code = (info.wAttributes >> 4) & 0x0f;
}
ret = GATE_RESULT_OK;
break;
}
}
#endif
ret = gate_terminal_vt100_get_back_color(thisptr, ptr_color_code);
} while (0);
return ret;
}
gate_result_t gate_terminal_console_set_back_color(void* thisptr, gate_uint8_t color_code)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
do
{
#ifndef GATE_SYS_WINCE
if (!GATE_FLAG_ENABLED(impl->flags, GATE_TERMINAL_FLAG_VT100))
{
CONSOLE_SCREEN_BUFFER_INFO info = GATE_INIT_EMPTY;
GetConsoleScreenBufferInfo(impl->con_output, &info);
info.wAttributes &= 0xff0f;
info.wAttributes |= (((gate_uint16_t)(color_code) << 4) & 0x00f0);
if (!SetConsoleTextAttribute(impl->con_output, info.wAttributes))
{
DWORD lasterr = GetLastError();
if (!is_real_console(impl))
{
/* no action, simulate success */
ret = GATE_RESULT_OK;
break;
}
gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
}
else
{
ret = GATE_RESULT_OK;
break;
}
}
#endif
ret = gate_terminal_vt100_set_back_color(thisptr, color_code);
} while (0);
return ret;
}
#endif /* GATE_SYSTEM_TERMINALS_WINAPI */
#if defined(GATE_SYSTEM_TERMINALS_POSIX)
# include <sys/ioctl.h>
# include <sys/types.h>
# include <sys/select.h>
# include <sys/wait.h>
# include <unistd.h>
# include <stdio.h>
# include <termios.h>
typedef struct gate_terminal_console_impl_class
{
GATE_INTERFACE_VTBL(gate_terminal) const* vtbl;
gate_atomic_int_t ref_counter;
int con_input;
int con_output;
int con_error;
int text_color;
int back_color;
gate_bool_t restore_termios;
struct termios termios_attrs_backup;
} gate_terminal_console_impl_t;
static gate_terminal_console_impl_t* gate_terminal_console_create(int fd_in, int fd_out, int fd_err)
{
gate_terminal_console_impl_t* impl = gate_mem_alloc(sizeof(gate_terminal_console_impl_t));
if (impl != NULL)
{
gate_mem_clear(impl, sizeof(gate_terminal_console_impl_t));
gate_init_terminal_console_vtbl();
impl->vtbl = &gate_terminal_console_vtbl;
gate_atomic_int_init(&impl->ref_counter, 1);
impl->con_input = fd_in; //STDIN_FILENO;
impl->con_output = fd_out; //STDOUT_FILENO;
impl->con_error = fd_err; //STDERR_FILENO;
impl->text_color = GATE_TERMINAL_COLOR_WHITE;
impl->back_color = GATE_TERMINAL_COLOR_BLACK;
gate_terminal_set_text_color(impl, GATE_TERMINAL_COLOR_WHITE);
gate_terminal_set_back_color(impl, GATE_TERMINAL_COLOR_BLACK);
}
return impl;
}
gate_terminal_t* gate_terminal_console_get_current()
{
return gate_terminal_console_open(0);
}
gate_terminal_t* gate_terminal_console_open(gate_enumint_t flags)
{
gate_terminal_console_impl_t* term = NULL;
struct termios termios_attrs;
int fd_in = dup(STDIN_FILENO);
int fd_out = dup(STDOUT_FILENO);
int fd_err = dup(STDERR_FILENO);
term = gate_terminal_console_create(fd_in, fd_out, fd_err);
if (!term)
{
gate_posix_close(fd_in);
gate_posix_close(fd_out);
gate_posix_close(fd_err);
}
else
{
if (0 == tcgetattr(term->con_input, &term->termios_attrs_backup))
{
gate_mem_copy(&termios_attrs, &term->termios_attrs_backup, sizeof(struct termios));
term->restore_termios = true;
GATE_FLAG_SET(termios_attrs.c_lflag, ICANON, false);
GATE_FLAG_SET(termios_attrs.c_lflag, ECHO, GATE_FLAG_ENABLED(flags, GATE_TERMINAL_FLAG_ECHO));
GATE_FLAG_SET(termios_attrs.c_lflag, ECHONL, GATE_FLAG_ENABLED(flags, GATE_TERMINAL_FLAG_ECHO));
GATE_FLAG_SET(termios_attrs.c_lflag, ISIG, false);
GATE_FLAG_SET(termios_attrs.c_lflag, IEXTEN, false);
tcsetattr(term->con_input, TCSANOW, &termios_attrs);
}
}
return (gate_terminal_t*)term;
}
static void gate_terminal_console_release(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
if (0 == gate_atomic_int_dec(&impl->ref_counter))
{
if (impl->restore_termios)
{
tcsetattr(impl->con_input, TCSANOW, &impl->termios_attrs_backup);
}
gate_posix_close(impl->con_input);
gate_posix_close(impl->con_output);
gate_posix_close(impl->con_error);
gate_mem_dealloc(impl);
}
}
static int gate_terminal_console_retain(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
return gate_atomic_int_dec(&impl->ref_counter);
}
static char const* gate_terminal_console_get_interface_name(void* thisptr)
{
return GATE_INTERFACE_NAME_TERMINAL;
}
static gate_result_t gate_terminal_console_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
ptrdiff_t result = gate_posix_read(impl->con_input, buffer, bufferlength);
if (result < 0)
{
gate_posix_errno(&ret);
}
else
{
if (NULL != returned)
{
*returned = (gate_size_t)result;
}
ret = GATE_RESULT_OK;
}
return ret;
}
static gate_result_t gate_terminal_console_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t gate_terminal_console_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
ptrdiff_t result = gate_posix_write(impl->con_output, buffer, bufferlength);
if (result < 0)
{
gate_posix_errno(&ret);
}
else
{
if (NULL != written)
{
*written = (gate_size_t)result;
}
ret = GATE_RESULT_OK;
}
return ret;
}
static gate_result_t gate_terminal_console_flush(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
if (fsync(impl->con_output) < 0)
{
gate_posix_errno(&ret);
}
else
{
ret = GATE_RESULT_OK;
}
return ret;
}
static gate_result_t gate_terminal_console_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* ptr_resource)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_uintptr_t res = 0;
gate_result_t ret = GATE_RESULT_OK;
switch (resource_type)
{
case GATE_STREAM_RESOURCE_INPUT: res = impl->con_input; break;
case GATE_STREAM_RESOURCE_OUTPUT: res = impl->con_output; break;
case GATE_STREAM_RESOURCE_ERROR: res = impl->con_error; break;
case GATE_STREAM_RESOURCE_DEFAULT: res = impl->con_input; break;
default: ret = GATE_RESULT_INVALIDARG; break;
}
if (ptr_resource)
{
*ptr_resource = res;
}
return ret;
}
static gate_result_t gate_terminal_console_await_char(void* thisptr, gate_uint32_t timeout_ms)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
return gate_posix_await_read(impl->con_input, timeout_ms);
}
static gate_result_t gate_terminal_console_read_char(void* thisptr, gate_char32_t* ptr_char)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
char buffer[8];
ret = gate_terminal_console_read(impl, buffer, 1, NULL);
if (ptr_char)
{
*ptr_char = (gate_char32_t)(gate_uint8_t)buffer[0];
}
return ret;
}
static gate_result_t gate_terminal_console_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
ptrdiff_t result = gate_posix_write(impl->con_error, buffer, bufferlength);
if (result < 0)
{
gate_posix_errno(&ret);
}
else
{
if (NULL != written)
{
*written = (gate_size_t)result;
}
ret = GATE_RESULT_OK;
}
return ret;
}
static gate_result_t gate_terminal_console_flush_err(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
if (fsync(impl->con_error) < 0)
{
gate_posix_errno(&ret);
}
else
{
ret = GATE_RESULT_OK;
}
return ret;
}
gate_enumint_t gate_terminal_console_get_type(void* thisptr)
{
return GATE_TERMINAL_TYPE_VT100;
}
//"\x1b[0;40;37m\x1b[1;1H\x1b[2J"
static gate_result_t gate_terminal_console_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret;
struct winsize ws = { 0, 0, 0, 0 };
if (ioctl(impl->con_output, TIOCGWINSZ, &ws) != -1)
{
if (NULL != ptr_width) *ptr_width = (gate_uint16_t)ws.ws_col;
if (NULL != ptr_height) *ptr_height = (gate_uint16_t)ws.ws_row;
ret = GATE_RESULT_OK;
}
else
{
gate_posix_errno(&ret);
}
return ret;
}
static gate_result_t gate_terminal_console_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
struct termios t;
/*TODO implementation*/
(void)impl;
(void)t;
(void)ptr_x;
(void)ptr_y;
return ret;
}
static gate_result_t gate_terminal_console_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
return gate_terminal_vt100_set_cursor_pos(thisptr, x, y);
}
static gate_result_t gate_terminal_console_set_cursor_mode(void* thisptr, gate_uint16_t mode)
{
return gate_terminal_vt100_enable_cursor(thisptr, (mode == GATE_TERMINAL_CURSOR_MODE_OFF) ? false : true);
}
static gate_result_t gate_terminal_console_clear(void* thisptr)
{
return gate_terminal_vt100_clear(thisptr);
}
static gate_result_t gate_terminal_console_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
*ptr_color_code = impl->text_color;
return GATE_RESULT_OK;
}
static gate_result_t gate_terminal_console_set_text_color(void* thisptr, gate_uint8_t color_code)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret = gate_terminal_vt100_set_text_color(thisptr, color_code);
if (GATE_SUCCEEDED(ret))
{
impl->text_color = color_code;
}
return ret;
}
static gate_result_t gate_terminal_console_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
*ptr_color_code = impl->back_color;
return GATE_RESULT_OK;
}
static gate_result_t gate_terminal_console_set_back_color(void* thisptr, gate_uint8_t color_code)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_result_t ret = gate_terminal_vt100_set_back_color(thisptr, color_code);
if (GATE_SUCCEEDED(ret))
{
impl->back_color = color_code;
}
return ret;
}
#endif /* GATE_SYSTEM_TERMINALS_POSIX */
#if defined(GATE_SYSTEM_TERMINALS_DOS)
#include <graph.h>
#include <conio.h>
static void gate_term_init()
{
_setvideomode(_TEXTC80);
}
static void gate_term_text_color(int textcol)
{
_settextcolor(textcol);
}
static void gate_term_back_color(int bkcol)
{
_setbkcolor(bkcol);
}
static void gate_term_text_cursor_size(int sz_0_to_3)
{
int value = 0;
switch (sz_0_to_3)
{
case 3: value = 0x0007; break; /* full block */
case 2: value = 0x0407; break; /* half size */
case 1: value = 0x0607; break; /* underline cursor */
default: value = 0x2000; break; /* no cursor /invisible */
}
_settextcursor(value);
}
static void gate_term_text_cursor_pos(char y_1_to_25, char x_1_to_80)
{
_settextposition(y_1_to_25, x_1_to_80);
}
static void gate_term_print_char(char chr)
{
char buf[2] = { chr, 0 };
_outtext(buf);
}
static void gate_term_text_cursor_enable(gate_bool_t enabled)
{
_displaycursor(enabled ? _GCURSORON : _GCURSOROFF);
}
static void gate_term_print_text(char const* text)
{
char buffer[GATE_MAX_BLOCK_COPYBUFFER_LENGTH];
gate_str_print_text(buffer, sizeof(buffer), text, gate_str_length(text));
_outtext(buffer);
}
typedef struct gate_terminal_console_impl_class
{
GATE_INTERFACE_VTBL(gate_terminal) const* vtbl;
gate_atomic_int_t ref_counter;
gate_uint32_t flags;
gate_uint8_t text_color;
gate_uint8_t back_color;
char read_buffer[64];
gate_size_t read_buffer_used;
} gate_terminal_console_impl_t;
void gate_terminal_console_release(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
if (0 == gate_atomic_int_dec(&impl->ref_counter))
{
gate_mem_dealloc(impl);
}
}
int gate_terminal_console_retain(void* thisptr)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
return gate_atomic_int_inc(&impl->ref_counter);
}
char const* gate_terminal_console_get_interface_name(void* thisptr)
{
(void)thisptr;
return GATE_INTERFACE_NAME_TERMINAL;
}
static gate_size_t gate_term_read_buffer(char* buffer, gate_size_t buffer_length)
{
gate_size_t count = 0;
unsigned int ch;
while ((buffer_length > 0) && gate_dos_has_kbd_event())
{
if (!gate_dos_get_next_kbd_event(&ch))
{
break;
}
*buffer = (char)ch;
++buffer;
++count;
--buffer_length;
}
return count;
}
gate_result_t gate_terminal_console_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_size_t len = 0;
unsigned int ch;
do
{
if (!buffer || !bufferlength)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
if (impl->read_buffer_used == 0)
{
if (!gate_dos_get_next_kbd_event(&ch))
{
break;
}
impl->read_buffer[0] = (char)ch;
impl->read_buffer_used = 1;
impl->read_buffer_used += gate_term_read_buffer(&impl->read_buffer[1], sizeof(impl->read_buffer) - 1);
}
ret = GATE_RESULT_OK;
len = (bufferlength > impl->read_buffer_used) ? impl->read_buffer_used : bufferlength;
gate_mem_copy(buffer, impl->read_buffer, len);
if (returned)
{
*returned = len;
}
if (len >= impl->read_buffer_used)
{
impl->read_buffer_used = 0;
}
else if (len > 0)
{
gate_mem_copy(&impl->read_buffer[0], &impl->read_buffer[len], impl->read_buffer_used - len);
impl->read_buffer_used -= len;
}
} while (0);
return ret;
}
gate_result_t gate_terminal_console_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
gate_terminal_console_impl_t* impl = (gate_terminal_console_impl_t*)thisptr;
gate_size_t len;
if (!buffer || !bufferlength)
{
return GATE_RESULT_INVALIDARG;
}
if (impl->read_buffer_used < sizeof(impl->read_buffer))
{
len = gate_term_read_buffer(&impl->read_buffer[impl->read_buffer_used], sizeof(impl->read_buffer) - impl->read_buffer_used);
impl->read_buffer_used += len;
}
len = (bufferlength > impl->read_buffer_used) ? impl->read_buffer_used : bufferlength;
gate_mem_copy(buffer, impl->read_buffer, len);
if (returned)
{
*returned = len;
}
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_result_t ret = GATE_RESULT_OK;
gate_size_t processed = 0;
GATE_UNUSED_ARG(thisptr);
while (bufferlength > 0)
{
gate_term_print_char(*buffer);
++buffer;
++processed;
--bufferlength;
}
if (written)
{
*written = processed;
}
return ret;
}
gate_result_t gate_terminal_console_flush(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* resource)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(resource_type);
GATE_UNUSED_ARG(resource);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_await_char(void* thisptr, gate_uint32_t timeout_ms)
{
GATE_UNUSED_ARG(thisptr);
return gate_dos_await_and_read_kbd(NULL, timeout_ms);
}
gate_result_t gate_terminal_console_read_char(void* thisptr, gate_char32_t* ptr_char)
{
GATE_UNUSED_ARG(thisptr);
return gate_dos_await_and_read_kbd(ptr_char, -1);
}
gate_result_t gate_terminal_console_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(written);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_flush_err(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_RESULT_OK;
}
gate_enumint_t gate_terminal_console_get_type(void* thisptr)
{
return GATE_TERMINAL_TYPE_UI;
}
gate_result_t gate_terminal_console_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
GATE_UNUSED_ARG(thisptr);
if (ptr_width) *ptr_width = 80;
if (ptr_height) *ptr_height = 25;
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_x);
GATE_UNUSED_ARG(ptr_y);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
GATE_UNUSED_ARG(thisptr);
if (x > 79) x = 79;
if (y > 24) y = 24;
gate_term_text_cursor_pos(y + 1, x + 1);
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_set_cursor_mode(void* thisptr, gate_uint16_t mode)
{
GATE_UNUSED_ARG(thisptr);
gate_term_text_cursor_enable(mode == GATE_TERMINAL_CURSOR_MODE_OFF ? false : true);
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_clear(void* thisptr)
{
(void)thisptr;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
(void)thisptr;
(void)ptr_color_code;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_text_color(void* thisptr, gate_uint8_t color_code)
{
(void)thisptr;
gate_term_text_color(color_code);
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
(void)thisptr;
(void)ptr_color_code;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_back_color(void* thisptr, gate_uint8_t color_code)
{
(void)thisptr;
gate_term_back_color(color_code);
return GATE_RESULT_OK;
}
gate_terminal_t* gate_terminal_console_get_current()
{
gate_terminal_console_impl_t* impl = gate_mem_alloc(sizeof(gate_terminal_console_impl_t));
if (impl != NULL)
{
gate_mem_clear(impl, sizeof(gate_terminal_console_impl_t));
gate_init_terminal_console_vtbl();
impl->vtbl = &gate_terminal_console_vtbl;
gate_atomic_int_init(&impl->ref_counter, 1);
impl->text_color = GATE_TERMINAL_COLOR_WHITE;
impl->back_color = GATE_TERMINAL_COLOR_BLACK;
impl->read_buffer_used = 0;
gate_term_init();
}
return (gate_terminal_t*)impl;
}
gate_terminal_t* gate_terminal_console_open(gate_enumint_t flags)
{
(void)flags;
return gate_terminal_console_get_current();
}
#endif /* GATE_SYSTEM_TERMINALS_DOS */
#if defined(GATE_SYSTEM_TERMINALS_EFI)
#include "gate/platform/efi/efi_gate.h"
typedef struct
{
GATE_INTERFACE_VTBL(gate_terminal)* vtbl;
} gate_terminal_console_efi_singleton;
static gate_terminal_t efi_terminal_singleton =
{
&gate_terminal_console_vtbl
};
void gate_terminal_console_release(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
}
int gate_terminal_console_retain(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return 1;
}
char const* gate_terminal_console_get_interface_name(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_INTERFACE_NAME_TERMINAL;
}
gate_result_t gate_terminal_console_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
gate_result_t result = GATE_RESULT_INVALIDARG;
gate_uint32_t next_timeout = (gate_uint32_t)-1;
gate_char16_t chr;
gate_uint16_t scancode;
gate_size_t len;
gate_size_t len_returned = 0;
GATE_UNUSED_ARG(thisptr);
while (buffer && (bufferlength > 0))
{
result = gate_platform_efi_console_await_char(next_timeout);
GATE_BREAK_IF_FAILED(result);
next_timeout = 0;
result = gate_platform_efi_console_read_char(&chr, &scancode, true);
GATE_BREAK_IF_FAILED(result);
len = gate_char_write_utf8((gate_char32_t)chr, buffer, bufferlength);
len_returned += len;
buffer += len;
bufferlength -= len;
}
if (returned)
{
*returned = len_returned;
}
if ((result == GATE_RESULT_TIMEOUT) && (len_returned > 0))
{
result = GATE_RESULT_OK;
}
return result;
}
gate_result_t gate_terminal_console_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
gate_result_t result;
gate_char32_t chr32;
gate_size_t length;
gate_size_t bytes_written = 0;
GATE_UNUSED_ARG(thisptr);
while (buffer && (bufferlength > 0))
{
length = gate_char_read_utf8(buffer, bufferlength, &chr32);
buffer += length;
bufferlength -= length;
result = gate_platform_efi_console_write_char((gate_char16_t)chr32);
GATE_BREAK_IF_FAILED(result);
bytes_written += length;
}
if (written)
{
*written = bytes_written;
}
if (GATE_FAILED(result) && (bytes_written > 0))
{
result = GATE_RESULT_OK;
}
return result;
}
gate_result_t gate_terminal_console_flush(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_RESULT_OK;
}
gate_result_t gate_terminal_console_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* resource)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(resource_type);
GATE_UNUSED_ARG(resource);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_await_char(void* thisptr, gate_uint32_t timeout_ms)
{
gate_result_t result;
GATE_UNUSED_ARG(thisptr);
result = gate_platform_efi_console_await_char(timeout_ms == GATE_CONSOLE_TIMEOUT_NEVER ? (gate_uint32_t)-1 : timeout_ms);
return result;
}
gate_result_t gate_terminal_console_read_char(void* thisptr, gate_char32_t* ptr_char)
{
gate_result_t result;
gate_char16_t chr16 = 0;
gate_uint16_t scan_code = 0;
GATE_UNUSED_ARG(thisptr);
result = gate_platform_efi_console_read_char(&chr16, &scan_code, false);
if (ptr_char)
{
*ptr_char = (gate_char32_t)chr16;
}
return result;
}
gate_result_t gate_terminal_console_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(written);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_flush_err(void* thisptr)
{
(void)thisptr;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_enumint_t gate_terminal_console_get_type(void* thisptr)
{
return GATE_TERMINAL_TYPE_UI;
}
gate_result_t gate_terminal_console_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
GATE_UNUSED_ARG(thisptr);
return gate_platform_efi_console_get_size(ptr_width, ptr_height);
}
gate_result_t gate_terminal_console_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
GATE_UNUSED_ARG(thisptr);
return gate_platform_efi_console_get_cursor_pos(ptr_x, ptr_y);
}
gate_result_t gate_terminal_console_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
GATE_UNUSED_ARG(thisptr);
return gate_platform_efi_console_set_cursor_pos(x, y);
}
gate_result_t gate_terminal_console_set_cursor_mode(void* thisptr, gate_uint16_t mode)
{
GATE_UNUSED_ARG(thisptr);
return gate_platform_efi_console_enable_cursor(mode == GATE_TERMINAL_CURSOR_MODE_OFF ? false : true);
}
gate_result_t gate_terminal_console_clear(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return gate_platform_efi_console_clear();
}
gate_result_t gate_terminal_console_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
GATE_UNUSED_ARG(thisptr);
gate_uint8_t dummy = 0;
return gate_platform_efi_console_get_color(ptr_color_code, &dummy);
}
gate_result_t gate_terminal_console_set_text_color(void* thisptr, gate_uint8_t color_code)
{
GATE_UNUSED_ARG(thisptr);
gate_uint8_t dummy = 0;
gate_uint8_t backcolor = 0;
gate_platform_efi_console_get_color(&dummy, &backcolor);
return gate_platform_efi_console_set_color(color_code, backcolor);
}
gate_result_t gate_terminal_console_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
GATE_UNUSED_ARG(thisptr);
gate_uint8_t dummy = 0;
return gate_platform_efi_console_get_color(&dummy, ptr_color_code);
}
gate_result_t gate_terminal_console_set_back_color(void* thisptr, gate_uint8_t color_code)
{
GATE_UNUSED_ARG(thisptr);
gate_uint8_t dummy = 0;
gate_uint8_t textcolor = 0;
gate_platform_efi_console_get_color(&textcolor, &dummy);
return gate_platform_efi_console_set_color(textcolor, color_code);
}
gate_terminal_t* gate_terminal_console_get_current()
{
gate_init_terminal_console_vtbl();
return &efi_terminal_singleton;
}
gate_terminal_t* gate_terminal_console_open(gate_enumint_t flags)
{
GATE_UNUSED_ARG(flags);
gate_init_terminal_console_vtbl();
return &efi_terminal_singleton;
}
#endif /* GATE_SYSTEM_TERMINALS_EFI*/
#if defined(GATE_SYSTEM_TERMINALS_NOIMPL)
void gate_terminal_console_release(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
}
int gate_terminal_console_retain(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return 1;
}
char const* gate_terminal_console_get_interface_name(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return NULL;
}
gate_result_t gate_terminal_console_read(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_peek(void* thisptr, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_write(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(written);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_flush(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_get_resource(void* thisptr, gate_uint32_t resource_type, gate_uintptr_t* resource)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(resource_type);
GATE_UNUSED_ARG(resource);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_await_char(void* thisptr, gate_uint32_t timeout_ms)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(timeout_ms);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_read_char(void* thisptr, gate_char32_t* ptr_char)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_char);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_write_err(void* thisptr, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlength);
GATE_UNUSED_ARG(written);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_flush_err(void* thisptr)
{
(void)thisptr;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_enumint_t gate_terminal_console_get_type(void* thisptr)
{
return GATE_TERMINAL_TYPE_UNKNOWN;
}
gate_result_t gate_terminal_console_get_size(void* thisptr, gate_uint16_t* ptr_width, gate_uint16_t* ptr_height)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_width);
GATE_UNUSED_ARG(ptr_height);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_get_cursor_pos(void* thisptr, gate_uint16_t* ptr_x, gate_uint16_t* ptr_y)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_x);
GATE_UNUSED_ARG(ptr_y);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_cursor_pos(void* thisptr, gate_uint16_t x, gate_uint16_t y)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(x);
GATE_UNUSED_ARG(y);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_cursor_mode(void* thisptr, gate_uint16_t mode)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(mode);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_clear(void* thisptr)
{
GATE_UNUSED_ARG(thisptr);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_get_text_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_color_code);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_text_color(void* thisptr, gate_uint8_t color_code)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(color_code);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_get_back_color(void* thisptr, gate_uint8_t* ptr_color_code)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(ptr_color_code);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_terminal_console_set_back_color(void* thisptr, gate_uint8_t color_code)
{
GATE_UNUSED_ARG(thisptr);
GATE_UNUSED_ARG(color_code);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_terminal_t* gate_terminal_console_get_current()
{
return NULL;
}
gate_terminal_t* gate_terminal_console_open(gate_enumint_t flags)
{
GATE_UNUSED_ARG(flags);
return NULL;
}
#endif /* GATE_SYSTEM_TERMINALS_NOIMPL */
|