aboutsummaryrefslogtreecommitdiff
path: root/kernel/drivers/tty/vterminal.c
blob: 9ac3421189616ab8e83e37b7074244060b398d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
#include <drivers/keyboard.h>
#include <drivers/tty/ldisc.h>
#include <drivers/tty/tty.h>
#include <drivers/tty/vterminal.h>
#include <errno.h>
#include <mm/kmalloc.h>
#include <util/debug.h>
#include <util/string.h>

/*

vterminal.c is used to manage the display of the terminal screen, this includes
printing the keys pressed, output of the command passed, managing the cursor 
position, etc. 

vterminal_write is called by functions in tty.c and ldisc.c, namely tty_write
and ldisc_key_pressed. vterminal_write then calls vtconsole_write which takes 
care of the processing of the characters with the help of vtconsole_process
vtconsole_process and vtconsole_append are responsible for printing the
characters corresponding to the keys pressed onto the console. 

vtconsole_append also manages the position of the cursor while the uncooked 
part of the buffer is being printed. There are mutltiple other functions defined
in this file which help in displaying the cursor on the console. The console
also supports scrolling which is handled by vtconsole_scroll. vterminal_clear
is used to clear the content of the console.

The functions, vterminal_make_active, vterminal_init, vtconsole, paint_callback
and cursor_move_callback are responsible for carrying out the necessary
initialization and initial display of the console.

*/

#define vterminal_to_tty(vterminal) \
    CONTAINER_OF((vterminal), tty_t, tty_vterminal)

#ifdef __VGABUF___

/*
Without turning on VGABUF, the terminal is treated as a simple device: one sent characters 
to it to be displayed. It did the right thing with new lines and with backspaces, 
but didn't handle any other control characters. The VGA handles all sorts of other things, 
but we also have to explicitly tell it to scroll. VGABUF allows Weenix to toggle between 
VGA text mode (that understands text) and VGA buffer mode (that is pixel based).
*/

#define VT_LINE_POSITION(vt, line)                                     \
    ((vt)->vt_line_positions[((vt)->vt_line_offset + (vt)->vt_height + \
                              (line)) %                                \
                             (vt)->vt_height])

#define vterminal_to_tty(vterminal) \
    CONTAINER_OF((vterminal), tty_t, tty_vterminal)

#define VT_OFFSCREEN ((size_t)-1)

static long vterminal_add_chunk(vterminal_t *vt);

static vterminal_t *active_vt = NULL;

void vterminal_init(vterminal_t *vt)
{
    vt->vt_width = screen_get_width() / screen_get_character_width();
    vt->vt_height = screen_get_height() / screen_get_character_height();
    list_init(&vt->vt_history_chunks);
    vt->vt_line_positions = kmalloc(sizeof(size_t) * vt->vt_height * 2);
    KASSERT(vt->vt_line_positions);
    vt->vt_line_widths = vt->vt_line_positions + vt->vt_height;

    list_init(&vt->vt_history_chunks);
    long success = vterminal_add_chunk(vt);
    KASSERT(success && !list_empty(&vt->vt_history_chunks));

    vterminal_clear(vt);
}

static void vterminal_seek_to_pos(vterminal_t *vt, size_t pos,
                                  vterminal_history_chunk_t **chunk,
                                  size_t *offset)
{
    if (pos > vt->vt_len)
    {
        *chunk = NULL;
        *offset = 0;
        return;
    }
    *offset = pos % VT_CHARS_PER_HISTORY_CHUNK;
    size_t n_chunks = vt->vt_len / VT_CHARS_PER_HISTORY_CHUNK;
    size_t iterations = pos / VT_CHARS_PER_HISTORY_CHUNK;
    if (iterations > n_chunks >> 1)
    {
        iterations = n_chunks - iterations;
        list_iterate_reverse(&vt->vt_history_chunks, chunk_iter,
                             vterminal_history_chunk_t, link)
        {
            if (!iterations--)
            {
                *chunk = chunk_iter;
                return;
            }
        }
    }
    else
    {
        list_iterate(&vt->vt_history_chunks, chunk_iter,
                     vterminal_history_chunk_t, link)
        {
            if (!iterations--)
            {
                *chunk = chunk_iter;
                return;
            }
        }
    }
}

static inline long vterminal_seek_to_offset(vterminal_t *vt,
                                            vterminal_history_chunk_t **chunk,
                                            size_t *offset)
{
    while (*offset >= VT_CHARS_PER_HISTORY_CHUNK)
    {
        if (*chunk ==
            list_tail(&vt->vt_history_chunks, vterminal_history_chunk_t, link))
            return 0;
        *chunk = list_next(*chunk, vterminal_history_chunk_t, link);
        *offset -= VT_CHARS_PER_HISTORY_CHUNK;
    }
    return 1;
}

size_t vterminal_calculate_line_width_forward(vterminal_t *vt, size_t pos)
{
    vterminal_history_chunk_t *chunk;
    size_t offset;
    vterminal_seek_to_pos(vt, pos, &chunk, &offset);
    if (!chunk)
        return 0;
    size_t width = 0;
    while (pos + width < vt->vt_len && chunk->chars[offset++] != LF)
    {
        width++;
        if (!vterminal_seek_to_offset(vt, &chunk, &offset))
            break;
    }
    return width;
}
static void vterminal_redraw_lines(vterminal_t *vt, size_t start, size_t end)
{
    KASSERT(start < vt->vt_height && start < end && end <= vt->vt_height);

    size_t pos = VT_LINE_POSITION(vt, start);
    vterminal_history_chunk_t *chunk;
    size_t offset;
    vterminal_seek_to_pos(vt, pos, &chunk, &offset);

    color_t cursor = {.value = 0x00D3D3D3};
    color_t background = {.value = 0x00000000};
    color_t foreground = {.value = 0x00FFFFFF};

    size_t screen_y = screen_get_character_height() * start;

    size_t line = start;
    while (line < end && pos <= vt->vt_len &&
           vterminal_seek_to_offset(vt, &chunk, &offset))
    {
        KASSERT(pos == VT_LINE_POSITION(vt, line));

        size_t cur_width = vt->vt_line_widths[line];
        size_t new_width, next_pos;
        if (line + 1 < vt->vt_height &&
            (next_pos = VT_LINE_POSITION(vt, line + 1)) != VT_OFFSCREEN)
        {
            new_width = next_pos - pos - 1;
        }
        else
        {
            new_width = vterminal_calculate_line_width_forward(vt, pos);
        }
        vt->vt_line_widths[line] = new_width;

        screen_fill_rect(
            0, screen_y,
            MAX(cur_width, new_width) * screen_get_character_width(),
            screen_get_character_height(), background);
        if (pos <= vt->vt_cursor_pos && vt->vt_cursor_pos <= pos + new_width)
        {
            screen_fill_rect(
                (vt->vt_cursor_pos - pos) * screen_get_character_width(),
                screen_y, screen_get_character_width(),
                screen_get_character_height(), cursor);
            vt->vt_line_widths[line]++;
        }
        size_t drawn = 0;
        while (drawn != new_width)
        {
            size_t to_draw =
                MIN(VT_CHARS_PER_HISTORY_CHUNK - offset, new_width - drawn);
            screen_draw_string(drawn * screen_get_character_width(), screen_y,
                               chunk->chars + offset, to_draw, foreground);
            drawn += to_draw;
            offset += to_draw;
            if (!vterminal_seek_to_offset(vt, &chunk, &offset))
            {
                vterminal_seek_to_offset(vt, &chunk, &offset);
                KASSERT(drawn == new_width);
                break;
            }
        }

        pos += new_width + 1;
        KASSERT(chunk->chars[offset] == LF || pos >= vt->vt_len);

        offset++;
        line++;
        screen_y += screen_get_character_height();
    }
    while (line < end)
    {
        //        dbg(DBG_TEMP, "clearing line %lu\n", line);
        screen_fill_rect(
            0, screen_y,
            vt->vt_line_widths[line] * screen_get_character_width(),
            screen_get_character_height(), background);
        vt->vt_line_widths[line] = 0;
        line++;
        screen_y += screen_get_character_height();
    }
}

void vterminal_make_active(vterminal_t *vt)
{
    KASSERT(vt);
    if (active_vt == vt)
        return;
    active_vt = vt;
    for (size_t line = 0; line < vt->vt_height; line++)
    {
        vt->vt_line_widths[line] = vt->vt_width;
    }
    color_t background = {.value = 0x00000000};
    screen_fill_rect(
        vt->vt_width * screen_get_character_width(), 0,
        screen_get_width() - vt->vt_width * screen_get_character_width(),
        screen_get_height(), background);
    screen_fill_rect(
        0, vt->vt_height * screen_get_character_height(), screen_get_width(),
        screen_get_height() - vt->vt_height * screen_get_character_height(),
        background);
    vterminal_redraw_lines(vt, 0, vt->vt_height);
}

size_t vterminal_calculate_line_width_backward(vterminal_t *vt, size_t pos)
{
    if (!pos)
        return 0;
    vterminal_history_chunk_t *chunk;
    size_t offset;
    vterminal_seek_to_pos(vt, pos - 1, &chunk, &offset);
    size_t width = 0;
    while (chunk->chars[offset] != LF)
    {
        width++;
        if (offset == 0)
        {
            if (chunk == list_head(&vt->vt_history_chunks,
                                   vterminal_history_chunk_t, link))
                break;
            chunk = list_prev(chunk, vterminal_history_chunk_t, link);
            offset = VT_CHARS_PER_HISTORY_CHUNK;
        }
        offset--;
    }
    return width;
}

static inline void vterminal_get_last_visible_line_information(vterminal_t *vt,
                                                               size_t *position,
                                                               size_t *width)
{
    for (long line = vt->vt_height - 1; line >= 0; line--)
    {
        if (VT_LINE_POSITION(vt, line) != VT_OFFSCREEN)
        {
            *position = VT_LINE_POSITION(vt, line);
            *width = vterminal_calculate_line_width_forward(vt, *position);
            return;
        }
    }
    panic("should always find last visible line information");
}

static inline long vterminal_scrolled_to_bottom(vterminal_t *vt)
{
    size_t position;
    size_t width;
    vterminal_get_last_visible_line_information(vt, &position, &width);
    return position + width == vt->vt_len;
}

void vterminal_scroll_to_bottom(vterminal_t *vt)
{
    if (vterminal_scrolled_to_bottom(vt))
        return;
    vt->vt_line_offset = 0;
    VT_LINE_POSITION(vt, 0) = vt->vt_len + 1;
    vterminal_scroll(vt, -vt->vt_height);
    for (size_t line = vt->vt_height - vt->vt_line_offset; line < vt->vt_height;
         line++)
    {
        VT_LINE_POSITION(vt, line) = VT_OFFSCREEN;
    }
}

void vterminal_scroll_draw(vterminal_t *vt, long count)
{
    if (count > 0)
    {
        if ((size_t)count > vt->vt_height)
            count = vt->vt_height;
        size_t copy_distance = count * screen_get_character_height();
        size_t screen_y = 0;
        for (size_t line = 0; line < vt->vt_height - count; line++)
        {
            screen_copy_rect(0, screen_y + copy_distance,
                             MAX(vt->vt_line_widths[line],
                                 vt->vt_line_widths[line + count]) *
                                 screen_get_character_width(),
                             screen_get_character_height(), 0, screen_y);
            vt->vt_line_widths[line] = vt->vt_line_widths[line + count];
            screen_y += screen_get_character_height();
        }
        vterminal_redraw_lines(vt, vt->vt_height - count, vt->vt_height);
    }
    else if (count < 0)
    {
        count *= -1;
        if ((size_t)count > vt->vt_height)
            count = vt->vt_height;
        size_t copy_distance = count * screen_get_character_height();
        size_t screen_y =
            (vt->vt_height - count) * screen_get_character_height();
        for (size_t line = vt->vt_height - count; line >= (size_t)count;
             line--)
        {
            screen_copy_rect(0, screen_y - copy_distance,
                             MAX(vt->vt_line_widths[line],
                                 vt->vt_line_widths[line - count]) *
                                 screen_get_character_width(),
                             screen_get_character_height(), 0, screen_y);
            vt->vt_line_widths[line] = vt->vt_line_widths[line - count];
            screen_y -= screen_get_character_height();
        }
        vterminal_redraw_lines(vt, 0, (size_t)count);
    }
}

void vterminal_scroll(vterminal_t *vt, long count)
{
    long n_scrolls = 0;
    if (count < 0)
    {
        size_t first_line_position = VT_LINE_POSITION(vt, 0);
        while (count++ && first_line_position)
        {
            size_t width = vterminal_calculate_line_width_backward(
                vt, first_line_position - 1);
            size_t top_line_position = first_line_position - width - 1;
            VT_LINE_POSITION(vt, -1) = top_line_position;
            if (!vt->vt_line_offset)
                vt->vt_line_offset = vt->vt_height;
            vt->vt_line_offset--;
            n_scrolls++;
            first_line_position = top_line_position;
        }
        if (n_scrolls)
        {
            vterminal_scroll_draw(vt, -n_scrolls);
        }
    }
    else if (count > 0)
    {
        size_t last_line_position;
        size_t last_line_width;
        vterminal_get_last_visible_line_information(vt, &last_line_position,
                                                    &last_line_width);
        while (count-- && last_line_position + last_line_width < vt->vt_len)
        {
            size_t bottom_line_position =
                last_line_position + last_line_width + 1;
            VT_LINE_POSITION(vt, 0) = bottom_line_position;
            vt->vt_line_offset++;
            if ((unsigned)vt->vt_line_offset == vt->vt_height)
                vt->vt_line_offset = 0;
            n_scrolls++;
            last_line_position = bottom_line_position;
            last_line_width =
                vterminal_calculate_line_width_forward(vt, last_line_position);
        }
        if (n_scrolls)
        {
            vterminal_scroll_draw(vt, n_scrolls);
        }
    }
}

void vterminal_clear(vterminal_t *vt)
{
    list_iterate(&vt->vt_history_chunks, chunk, vterminal_history_chunk_t,
                 link)
    {
        if (chunk != list_tail(&vt->vt_history_chunks,
                               vterminal_history_chunk_t, link))
        {
            list_remove(&chunk->link);
            page_free_n(chunk, VT_PAGES_PER_HISTORY_CHUNK);
        }
        else
        {
            memset(chunk, 0, VT_CHARS_PER_HISTORY_CHUNK);
        }
    }
    vt->vt_len = 0;
    for (size_t i = 0; i < vt->vt_height; i++)
    {
        vt->vt_line_widths[i] = 0;
        vt->vt_line_positions[i] = VT_OFFSCREEN;
    }
    vt->vt_line_offset = 0;
    vt->vt_cursor_pos = 0;
    vt->vt_input_pos = 0;
    VT_LINE_POSITION(vt, 0) = 0;
}

static long vterminal_add_chunk(vterminal_t *vt)
{
    vterminal_history_chunk_t *chunk = page_alloc_n(VT_PAGES_PER_HISTORY_CHUNK);
    if (!chunk)
    {
        chunk =
            list_head(&vt->vt_history_chunks, vterminal_history_chunk_t, link);
        if (chunk ==
            list_tail(&vt->vt_history_chunks, vterminal_history_chunk_t, link))
            return 0;
        list_remove(&chunk->link);

        // TODO what if the first chunk that we're removing is visible? lol
        for (size_t i = 0; i < vt->vt_height; i++)
        {
            KASSERT(vt->vt_line_positions[i] >= VT_CHARS_PER_HISTORY_CHUNK &&
                    "NYI");
            vt->vt_line_positions[i] -= VT_CHARS_PER_HISTORY_CHUNK;
        }
        KASSERT(vt->vt_input_pos >= VT_CHARS_PER_HISTORY_CHUNK &&
                vt->vt_cursor_pos >= VT_CHARS_PER_HISTORY_CHUNK &&
                vt->vt_len >= VT_CHARS_PER_HISTORY_CHUNK && "NYI");
        vt->vt_input_pos -= VT_CHARS_PER_HISTORY_CHUNK;
        vt->vt_cursor_pos -= VT_CHARS_PER_HISTORY_CHUNK;
        vt->vt_len -= VT_CHARS_PER_HISTORY_CHUNK;
    }

    memset(chunk, 0, sizeof(vterminal_history_chunk_t));

    list_link_init(&chunk->link);
    list_insert_tail(&vt->vt_history_chunks, &chunk->link);

    return 1;
}

static inline long vterminal_allocate_to_offset(
    vterminal_t *vt, vterminal_history_chunk_t **chunk, size_t *offset)
{
    if (!vterminal_seek_to_offset(vt, chunk, offset))
    {
        if (!vterminal_add_chunk(vt))
        {
            return 0;
        }
        return vterminal_seek_to_offset(vt, chunk, offset);
    }
    return 1;
}

size_t vterminal_write(vterminal_t *vt, const char *buf, size_t len)
{
    size_t written = 0;

    size_t last_line_width =
        vterminal_calculate_line_width_backward(vt, vt->vt_len);
    size_t last_line_idx;
    size_t last_line_position = VT_OFFSCREEN;
    for (last_line_idx = vt->vt_height - 1;; last_line_idx--)
    {
        if ((last_line_position = VT_LINE_POSITION(vt, last_line_idx)) !=
            VT_OFFSCREEN)
        {
            break;
        }
    }
    KASSERT(last_line_idx < vt->vt_height);

    vterminal_history_chunk_t *chunk;
    size_t offset;
    vterminal_seek_to_pos(vt, vt->vt_len, &chunk, &offset);

    size_t last_line_idx_initial = (size_t)last_line_idx;

    long need_to_scroll = last_line_position + last_line_width == vt->vt_len;
    size_t n_scroll_downs = 0;
    while (len--)
    {
        char c = *(buf++);
        written++;
        if (c != LF)
        {
            chunk->chars[offset++] = c;
            vt->vt_len++;
            last_line_width++;
            if (!vterminal_allocate_to_offset(vt, &chunk, &offset))
                goto done;
        }
        if (last_line_width == vt->vt_width)
        {
            c = LF;
        }
        if (c == LF)
        {
            chunk->chars[offset++] = LF;
            vt->vt_len++;
            if (!vterminal_allocate_to_offset(vt, &chunk, &offset))
                goto done;

            if (need_to_scroll)
            {
                KASSERT(last_line_position + last_line_width + 1 == vt->vt_len);
                if (last_line_idx == vt->vt_height - 1)
                {
                    vt->vt_line_offset++;
                    n_scroll_downs++;
                    if ((unsigned)vt->vt_line_offset == vt->vt_height)
                        vt->vt_line_offset = 0;
                    if (last_line_idx_initial)
                        last_line_idx_initial--;
                }
                else
                {
                    last_line_idx++;
                }
                last_line_width = 0;
                last_line_position = VT_LINE_POSITION(vt, last_line_idx) =
                    vt->vt_len;
            }
        }
    }

    last_line_idx++;
done:
    vt->vt_input_pos = vt->vt_len;
    vt->vt_cursor_pos = vt->vt_len;

    if (need_to_scroll)
    {
        if (active_vt == vt)
        {
            if (last_line_idx >= vt->vt_height &&
                n_scroll_downs < vt->vt_height)
            {
                vterminal_scroll_draw(vt, n_scroll_downs);
                last_line_idx = vt->vt_height;
            }
            vterminal_redraw_lines(vt, last_line_idx_initial,
                                   MIN(last_line_idx, vt->vt_height));
        }
        else
        {
            vterminal_scroll(vt, n_scroll_downs);
        }
    }
    return written;
}

static void vterminal_free_from_position_to_end(vterminal_t *vt, size_t pos)
{
    vterminal_history_chunk_t *chunk;
    size_t offset;
    vterminal_seek_to_pos(vt, vt->vt_input_pos, &chunk, &offset);
    while (chunk !=
           list_tail(&vt->vt_history_chunks, vterminal_history_chunk_t, link))
    {
        vterminal_history_chunk_t *to_remove =
            list_tail(&vt->vt_history_chunks, vterminal_history_chunk_t, link);
        list_remove(&to_remove->link);
        page_free_n(to_remove, VT_PAGES_PER_HISTORY_CHUNK);
    }
    vt->vt_len = pos;
    for (size_t line = 0; line < vt->vt_height; line++)
    {
        if (VT_LINE_POSITION(vt, line) > vt->vt_len)
        {
            VT_LINE_POSITION(vt, line) = VT_OFFSCREEN;
            vterminal_redraw_lines(vt, line, line + 1);
        }
    }
}

void vterminal_key_pressed(vterminal_t *vt)
{
    KASSERT(active_vt == vt);
    vterminal_scroll_to_bottom(vt);
    char buf[LDISC_BUFFER_SIZE];
    size_t len =
        ldisc_get_current_line_raw(&vterminal_to_tty(vt)->tty_ldisc, buf);
    size_t initial_input_pos = vt->vt_input_pos;
    vterminal_free_from_position_to_end(vt, initial_input_pos);
    vterminal_write(vt, buf, len);

    vt->vt_input_pos = initial_input_pos;
}

#endif

#define VGA_SCREEN_WIDTH 80
#define VGA_SCREEN_HEIGHT 25

#define VGACOLOR_BLACK 0X0
#define VGACOLOR_BLUE 0X1
#define VGACOLOR_GREEN 0X2
#define VGACOLOR_CYAN 0X3
#define VGACOLOR_RED 0X4
#define VGACOLOR_MAGENTA 0X5
#define VGACOLOR_BROWN 0X6
#define VGACOLOR_LIGHT_GRAY 0X7
#define VGACOLOR_GRAY 0X8
#define VGACOLOR_LIGHT_BLUE 0X9
#define VGACOLOR_LIGHT_GREEN 0XA
#define VGACOLOR_LIGHT_CYAN 0XB
#define VGACOLOR_LIGHT_RED 0XC
#define VGACOLOR_LIGHT_MAGENTA 0XD
#define VGACOLOR_LIGHT_YELLOW 0XE
#define VGACOLOR_WHITE 0XF

/* --- Constructor/Destructor ----------------------------------------------- */

// vtconsole contructor/init function
vtconsole_t *vtconsole(vtconsole_t *vtc, int width, int height,
                       vtc_paint_handler_t on_paint,
                       vtc_cursor_handler_t on_move)
{
    vtc->width = width;
    vtc->height = height;

    vtansi_parser_t ap;
    ap.state = VTSTATE_ESC;
    ap.index = 0;
    vtansi_arg_t vta[8];
    memset(ap.stack, 0, sizeof(vtansi_arg_t) * VTC_ANSI_PARSER_STACK_SIZE);
    //  ap.stack = vta;
    vtc->ansiparser = ap;
    
    vtc->attr = VTC_DEFAULT_ATTR;

    vtc->buffer = kmalloc(width * height * sizeof(vtcell_t));

    vtc->tabs = kmalloc(LDISC_BUFFER_SIZE * sizeof(int));
    vtc->tab_index = 0;

    vtc->cursor = (vtcursor_t){0, 0};

    vtc->on_paint = on_paint;
    vtc->on_move = on_move;

    vtconsole_clear(vtc, 0, 0, width, height - 1);

    return vtc;
}

// function to free the vtconosle/vterminal buffer
void vtconsole_delete(vtconsole_t *vtc)
{
    kfree(vtc->buffer);
    kfree(vtc->tabs);
    kfree(vtc);
}

/* --- Internal methods ---------------------------------------------------- */

// function to clear everything on the vterminal
void vtconsole_clear(vtconsole_t *vtc, int fromx, int fromy, int tox, int toy)
{
    for (int i = fromx + fromy * vtc->width; i < tox + toy * vtc->width; i++)
    {
        vtcell_t *cell = &vtc->buffer[i];

        cell->attr = VTC_DEFAULT_ATTR;
        cell->c = ' ';

        if (vtc->on_paint)
        {
            vtc->on_paint(vtc, cell, i % vtc->width, i / vtc->width);
        }
    }
}

// helper function for vtconsole_newline to scroll down the screen.
void vtconsole_scroll(vtconsole_t *vtc, int lines)
{
    if (lines == 0)
        return;

    lines = lines > vtc->height ? vtc->height : lines;

    // Scroll the screen by number of $lines.
    for (int i = 0; i < ((vtc->width * vtc->height) - (vtc->width * lines));
         i++)
    {
        vtc->buffer[i] = vtc->buffer[i + (vtc->width * lines)];

        if (vtc->on_paint)
        {
            vtc->on_paint(vtc, &vtc->buffer[i], i % vtc->width, i / vtc->width);
        }
    }

    // Clear the last $lines.
    for (int i = ((vtc->width * vtc->height) - (vtc->width * lines));
         i < vtc->width * vtc->height; i++)
    {
        vtcell_t *cell = &vtc->buffer[i];
        cell->attr = VTC_DEFAULT_ATTR;
        cell->c = ' ';

        if (vtc->on_paint)
        {
            vtc->on_paint(vtc, &vtc->buffer[i], i % vtc->width, i / vtc->width);
        }
    }

    // Move the cursor up $lines
    if (vtc->cursor.y > 0)
    {
        vtc->cursor.y -= lines;

        if (vtc->cursor.y < 0)
            vtc->cursor.y = 0;

        if (vtc->on_move)
        {
            vtc->on_move(vtc, &vtc->cursor);
        }
    }
}

// Append a new line
void vtconsole_newline(vtconsole_t *vtc)
{
    vtc->cursor.x = 0;
    vtc->cursor.y++;

    if (vtc->cursor.y == vtc->height)
    {
        vtconsole_scroll(vtc, 1);
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Append character to the console buffer.
void vtconsole_append(vtconsole_t *vtc, char c)
{
    if (c == '\n')
    {
        vtconsole_newline(vtc);
    }
    else if (c == '\r')
    {
        vtc->cursor.x = 0;

        if (vtc->on_move)
        {
            vtc->on_move(vtc, &vtc->cursor);
        }
    }
    else if (c == '\t')
    {
        int n = 8 - (vtc->cursor.x % 8);
        // storing all the tabs and their size encountered.
        vtc->tabs[vtc->tab_index % LDISC_BUFFER_SIZE] = n;
        vtc->tab_index++;

        for (int i = 0; i < n; i++)
        {
            vtconsole_append(vtc, ' ');
        }
    }
    else if (c == '\b')
    {
        if (vtc->cursor.x > 0)
        {
            vtc->cursor.x--;
        }
        else
        {
            vtc->cursor.y--;
            vtc->cursor.x = vtc->width - 1;
        }

        if (vtc->on_move)
        {
            vtc->on_move(vtc, &vtc->cursor);
        }

        int i = (vtc->width * vtc->cursor.y) + vtc->cursor.x;
        vtcell_t *cell = &vtc->buffer[i];
        cell->attr = VTC_DEFAULT_ATTR;
        cell->c = ' ';
        vtc->on_paint(vtc, &vtc->buffer[i], i % vtc->width, i / vtc->width);
    }
    else
    {
        if (vtc->cursor.x >= vtc->width)
            vtconsole_newline(vtc);

        vtcell_t *cell =
            &vtc->buffer[vtc->cursor.x + vtc->cursor.y * vtc->width];
        cell->c = c;
        cell->attr = vtc->attr;

        if (vtc->on_paint)
        {
            vtc->on_paint(vtc, cell, vtc->cursor.x, vtc->cursor.y);
        }

        vtc->cursor.x++;

        if (vtc->on_move)
        {
            vtc->on_move(vtc, &vtc->cursor);
        }
    }
}

// Helper function for vtconsole_process to move the cursor P1 rows up
void vtconsole_csi_cuu(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.y = MAX(MIN(vtc->cursor.y - attr, vtc->height - 1), 1);
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Helper function for vtconsole_process to move the cursor P1 columns left
void vtconsole_csi_cud(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.y = MAX(MIN(vtc->cursor.y + attr, vtc->height - 1), 1);
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Helper function for vtconsole_process to move the cursor P1 columns right
void vtconsole_csi_cuf(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.x = MAX(MIN(vtc->cursor.x + attr, vtc->width - 1), 1);
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Helper function for vtconsole_process to move the cursor P1 rows down
void vtconsole_csi_cub(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.x = MAX(MIN(vtc->cursor.x - attr, vtc->width - 1), 1);
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Helper function for vtconsole_process to place the cursor to the first
// column of line P1 rows down from current
void vtconsole_csi_cnl(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.y = MAX(MIN(vtc->cursor.y + attr, vtc->height - 1), 1);
        vtc->cursor.x = 0;
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Helper function for vtconsole_process to place the cursor to the first
// column of line P1 rows up from current
void vtconsole_csi_cpl(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.y = MAX(MIN(vtc->cursor.y - attr, vtc->height - 1), 1);
        vtc->cursor.x = 0;
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Helper function of vtconsole_process to move the cursor to column P1
void vtconsole_csi_cha(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && !stack[0].empty)
    {
        int attr = stack[0].value;
        vtc->cursor.y = MAX(MIN(attr, vtc->height - 1), 1);
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Moves the cursor to row n, column m. The values are 1-based,
void vtconsole_csi_cup(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count == 1 && stack[0].empty)
    {
        vtc->cursor.x = 0;
        vtc->cursor.y = 0;
    }
    else if (count == 2)
    {
        if (stack[0].empty)
        {
            vtc->cursor.y = 0;
        }
        else
        {
            vtc->cursor.y = MIN(stack[0].value - 1, vtc->height - 1);
        }

        if (stack[1].empty)
        {
            vtc->cursor.y = 0;
        }
        else
        {
            vtc->cursor.x = MIN(stack[1].value - 1, vtc->width - 1);
        }
    }

    if (vtc->on_move)
    {
        vtc->on_move(vtc, &vtc->cursor);
    }
}

// Clears part of the screen.
void vtconsole_csi_ed(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    (void)(count);

    vtcursor_t cursor = vtc->cursor;

    if (stack[0].empty)
    {
        vtconsole_clear(vtc, cursor.x, cursor.y, vtc->width, vtc->height - 1);
    }
    else
    {
        int attr = stack[0].value;

        if (attr == 0)
            vtconsole_clear(vtc, cursor.x, cursor.y, vtc->width,
                            vtc->height - 1);
        else if (attr == 1)
            vtconsole_clear(vtc, 0, 0, cursor.x, cursor.y);
        else if (attr == 2)
            vtconsole_clear(vtc, 0, 0, vtc->width, vtc->height - 1);
    }
}

// Erases part of the line.
void vtconsole_csi_el(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    (void)(count);

    vtcursor_t cursor = vtc->cursor;

    if (stack[0].empty)
    {
        vtconsole_clear(vtc, cursor.x, cursor.y, vtc->width, cursor.y);
    }
    else
    {
        int attr = stack[0].value;

        if (attr == 0)
            vtconsole_clear(vtc, cursor.x, cursor.y, vtc->width, cursor.y);
        else if (attr == 1)
            vtconsole_clear(vtc, 0, cursor.y, cursor.x, cursor.y);
        else if (attr == 2)
            vtconsole_clear(vtc, 0, cursor.y, vtc->width, cursor.y);
    }
}

// Sets the appearance of the following characters
void vtconsole_csi_sgr(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    for (int i = 0; i < count; i++)
    {
        if (stack[i].empty || stack[i].value == 0)
        {
            vtc->attr = VTC_DEFAULT_ATTR;
        }
        else
        {
            int attr = stack[i].value;

            if (attr == 1) // Increased intensity
            {
                vtc->attr.bright = 1;
            }
            else if (attr >= 30 && attr <= 37) // Set foreground color
            {
                vtc->attr.fg = attr - 30;
            }
            else if (attr >= 40 && attr <= 47) // Set background color
            {
                vtc->attr.bg = attr - 40;
            }
        }
    }
}

void vtconsole_csi_l(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count != 1)
    {
        return;
    }
    if (stack[0].empty || stack[0].value != 25)
    {
        return;
    }

    vga_disable_cursor();
}

void vtconsole_csi_h(vtconsole_t *vtc, vtansi_arg_t *stack, int count)
{
    if (count != 1)
    {
        return;
    }

    if (stack[0].empty || stack[0].value != 25)
    {
        return;
    }

    vga_enable_cursor();
}

// vtconsole_append is called by vtconsole_process to process and print the
// keys pressed onto the console.
void vtconsole_process(vtconsole_t *vtc, char c)
{
    vtansi_parser_t *parser = &vtc->ansiparser;

    switch (parser->state)
    {
    case VTSTATE_ESC:
        if (c == '\033')
        {
            parser->state = VTSTATE_BRACKET;

            parser->index = 0;

            parser->stack[parser->index].value = 0;
            parser->stack[parser->index].empty = 1;
        }
        else
        {
            parser->state = VTSTATE_ESC;
            vtconsole_append(vtc, c);
        }
        break;

    case VTSTATE_BRACKET:
        if (c == '[')
        {
            parser->state = VTSTATE_ATTR;
        }
        else
        {
            parser->state = VTSTATE_ESC;
            vtconsole_append(vtc, c);
        }
        break;
    case VTSTATE_ATTR:
        if (c >= '0' && c <= '9')
        {
            parser->stack[parser->index].value *= 10;
            parser->stack[parser->index].value += (c - '0');
            parser->stack[parser->index].empty = 0;
        }
        else if (c == '?')
        {
            /* questionable (aka wrong) */
            break;
        }
        else
        {
            if ((parser->index) < VTC_ANSI_PARSER_STACK_SIZE)
            {
                parser->index++;
            }

            parser->stack[parser->index].value = 0;
            parser->stack[parser->index].empty = 1;

            parser->state = VTSTATE_ENDVAL;
        }
        break;
    default:
        break;
    }

    if (parser->state == VTSTATE_ENDVAL)
    {
        if (c == ';')
        {
            parser->state = VTSTATE_ATTR;
        }
        else
        {
            switch (c)
            {
            case 'A':
                /* Cursor up P1 rows */
                vtconsole_csi_cuu(vtc, parser->stack, parser->index);
                break;
            case 'B':
                /* Cursor down P1 rows */
                vtconsole_csi_cub(vtc, parser->stack, parser->index);
                break;
            case 'C':
                /* Cursor right P1 columns */
                vtconsole_csi_cuf(vtc, parser->stack, parser->index);
                break;
            case 'D':
                /* Cursor left P1 columns */
                vtconsole_csi_cud(vtc, parser->stack, parser->index);
                break;
            case 'E':
                /* Cursor to first column of line P1 rows down from current
                     */
                vtconsole_csi_cnl(vtc, parser->stack, parser->index);
                break;
            case 'F':
                /* Cursor to first column of line P1 rows up from current */
                vtconsole_csi_cpl(vtc, parser->stack, parser->index);
                break;
            case 'G':
                /* Cursor to column P1 */
                vtconsole_csi_cha(vtc, parser->stack, parser->index);
                break;
            case 'd':
                /* Cursor left P1 columns */
                break;
            case 'H':
                /* Moves the cursor to row n, column m. */
                vtconsole_csi_cup(vtc, parser->stack, parser->index);
                break;
            case 'J':
                /* Clears part of the screen. */
                vtconsole_csi_ed(vtc, parser->stack, parser->index);
                break;
            case 'K':
                /* Erases part of the line. */
                vtconsole_csi_el(vtc, parser->stack, parser->index);
                break;
            case 'm':
                /* Sets the appearance of the following characters */
                vtconsole_csi_sgr(vtc, parser->stack, parser->index);
                break;
            case 'l':
                vtconsole_csi_l(vtc, parser->stack, parser->index);
                break;
            case 'h':
                vtconsole_csi_h(vtc, parser->stack, parser->index);
                break;
            }

            parser->state = VTSTATE_ESC;
        }
    }
}

// vtconosle_putchar is called from vterminal_key_pressed
void vtconsole_putchar(vtconsole_t *vtc, char c) { vtconsole_process(vtc, c); }

// vtconsole_write is called from vterminal_write
void vtconsole_write(vtconsole_t *vtc, const char *buffer, uint32_t size)
{
    // looping through the whole size of the buffer
    for (uint32_t i = 0; i < size; i++)
    {
        // acquiting the ldisc associated with the vtconsole/vterminal
        ldisc_t *new_ldisc = &vterminal_to_tty(vtc)->tty_ldisc;

        // checking if the buffer is a backspsace and the last entered character was a tab
        if (buffer[i] == '\b' && new_ldisc->ldisc_buffer[(new_ldisc->ldisc_head)] == '\t')
        {
            // calling vtcomsole_process 'n' number of times.
            // where 'n' is the size of the tab.
            for (int j = 0; j < vtc->tabs[(vtc->tab_index - 1) % LDISC_BUFFER_SIZE]; j++)
            {
                vtconsole_process(vtc, buffer[i]);
            }
            vtc->tab_index--;
        }
        else
        {
            vtconsole_process(vtc, buffer[i]);
        }
    }
}

// called by vterminal_make_active to redraw the console.
void vtconsole_redraw(vtconsole_t *vtc)
{
    for (int i = 0; i < (vtc->width * vtc->height); i++)
    {
        if (vtc->on_paint)
        {
            vtc->on_paint(vtc, &vtc->buffer[i], i % vtc->width, i / vtc->width);
        }
    }
}

#define VGA_COLOR(__fg, __bg) (__bg << 4 | __fg)
#define VGA_ENTRY(__c, __fg, __bg) \
    ((((__bg)&0XF) << 4 | ((__fg)&0XF)) << 8 | ((__c)&0XFF))

// helper function for paint_callback.
void vga_cell(unsigned int x, unsigned int y, unsigned short entry)
{
    if (x < VGA_SCREEN_WIDTH)
    {
        if (y < VGA_SCREEN_WIDTH)
        {
            vga_write_char_at(y, x, entry);
        }
    }
}

static char colors[] = {
    [VTCOLOR_BLACK] = VGACOLOR_BLACK,
    [VTCOLOR_RED] = VGACOLOR_RED,
    [VTCOLOR_GREEN] = VGACOLOR_GREEN,
    [VTCOLOR_YELLOW] = VGACOLOR_BROWN,
    [VTCOLOR_BLUE] = VGACOLOR_BLUE,
    [VTCOLOR_MAGENTA] = VGACOLOR_MAGENTA,
    [VTCOLOR_CYAN] = VGACOLOR_CYAN,
    [VTCOLOR_GREY] = VGACOLOR_LIGHT_GRAY,
};

static char brightcolors[] = {
    [VTCOLOR_BLACK] = VGACOLOR_GRAY,
    [VTCOLOR_RED] = VGACOLOR_LIGHT_RED,
    [VTCOLOR_GREEN] = VGACOLOR_LIGHT_GREEN,
    [VTCOLOR_YELLOW] = VGACOLOR_LIGHT_YELLOW,
    [VTCOLOR_BLUE] = VGACOLOR_LIGHT_BLUE,
    [VTCOLOR_MAGENTA] = VGACOLOR_LIGHT_MAGENTA,
    [VTCOLOR_CYAN] = VGACOLOR_LIGHT_CYAN,
    [VTCOLOR_GREY] = VGACOLOR_WHITE,
};

static vterminal_t *active_vt = NULL;

// used for initializing the vtconsoles.
void paint_callback(vtconsole_t *vtc, vtcell_t *cell, int x, int y)
{
    if (vtc != active_vt)
    {
        return;
    }

    if (cell->attr.bright)
    {
        vga_cell(x, y,
                 VGA_ENTRY(cell->c, brightcolors[cell->attr.fg],
                           colors[cell->attr.bg]));
    }
    else
    {
        vga_cell(
            x, y,
            VGA_ENTRY(cell->c, colors[cell->attr.fg], colors[cell->attr.bg]));
    }
}

// used for initializing the vtconsoles.
void cursor_move_callback(vtconsole_t *vtc, vtcursor_t *cur)
{
    if (vtc != active_vt)
    {
        return;
    }
    vga_set_cursor(cur->y, cur->x);
}

// initialization function for vterminal which calls the vtconsole constructor
void vterminal_init(vtconsole_t *vt)
{
    vtconsole(vt, VGA_SCREEN_WIDTH, VGA_SCREEN_HEIGHT, paint_callback,
              cursor_move_callback);
}

// Used in tty.c to make a vterminal active and working.
void vterminal_make_active(vterminal_t *vt)
{
    active_vt = vt;
    vtconsole_redraw(vt);
    vga_set_cursor(vt->cursor.y, vt->cursor.x);
}

// called by ldisc_key_pressed from ldisc.c
void vterminal_key_pressed(vterminal_t *vt)
{
    char buf[LDISC_BUFFER_SIZE];
    size_t len =
        ldisc_get_current_line_raw(&vterminal_to_tty(vt)->tty_ldisc, buf);
    vtconsole_putchar(vt, buf[len - 1]);
}

void vterminal_scroll_to_bottom(vterminal_t *vt) { KASSERT(0); }

// ldisc_key_pressed calls this vterminal_write if VGA_BUF is not specified.
size_t vterminal_write(vterminal_t *vt, const char *buf, size_t len)
{
    vtconsole_write(vt, buf, len);
    return len;
}

// could be used in ldisc_key_pressed
size_t vterminal_echo_input(vterminal_t *vt, const char *buf, size_t len)
{
    vtconsole_write(vt, buf, len);
    return len;
}