/
vterm.cpp
1903 lines (1618 loc) · 48.4 KB
/
vterm.cpp
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
/*
* Copyright (c) 2018 - 2024 Pedro Falcato
* This file is part of Onyx, and is released under the terms of the MIT License
* check LICENSE at the root directory for more information
*
* SPDX-License-Identifier: MIT
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <onyx/console.h>
#include <onyx/dpc.h>
#include <onyx/font.h>
#include <onyx/framebuffer.h>
#include <onyx/init.h>
#include <onyx/input/device.h>
#include <onyx/input/event.h>
#include <onyx/input/keys.h>
#include <onyx/input/state.h>
#include <onyx/intrinsics.h>
#include <onyx/page.h>
#include <onyx/scheduler.h>
#include <onyx/semaphore.h>
#include <onyx/serial.h>
#include <onyx/thread.h>
#include <onyx/tty.h>
#include <onyx/types.h>
#include <onyx/utf8.h>
#include <onyx/vm.h>
#include <uapi/ioctls.h>
#include <onyx/utility.hpp>
struct tty;
struct color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
static struct color default_fg = {.r = 0xaa, .g = 0xaa, .b = 0xaa};
static struct color default_bg = {};
const struct color color_table[] = {
{.r = 0, .g = 0, .b = 0}, /* Black */
{.r = 0xff}, /* Red */
{.g = 0xff}, /* Green */
{.r = 0xff, .g = 0xff}, /* Yellow */
{.b = 0xff}, /* Blue */
{.r = 0xff, .b = 0xff}, /* Magenta */
{.g = 0xff, .b = 0xff}, /* Cyan */
{.r = 0xaa, .g = 0xaa, .b = 0xaa} /* White */
};
#define VTERM_CONSOLE_CELL_DIRTY (1 << 0)
#define VTERM_CONSOLE_CELL_CONTINUATION (1 << 1)
struct console_cell
{
uint32_t codepoint;
struct color bg;
struct color fg;
uint32_t flags;
};
static inline void vterm_set_dirty(struct console_cell *c)
{
c->flags |= VTERM_CONSOLE_CELL_DIRTY;
}
static inline void vterm_clear_dirty(struct console_cell *c)
{
c->flags &= ~VTERM_CONSOLE_CELL_DIRTY;
}
static inline bool vterm_is_dirty(struct console_cell *c)
{
return c->flags & VTERM_CONSOLE_CELL_DIRTY;
}
#define VTERM_MESSAGE_FLUSH 1
#define VTERM_MESSAGE_FLUSH_ALL 2
#define VTERM_MESSAGE_DIE 3
struct vterm_message
{
unsigned long message;
void *ctx;
struct vterm_message *next;
};
#define MAX_ARGS 4
struct vterm
{
struct mutex vt_lock;
unsigned int columns;
unsigned int rows;
unsigned int cursor_x, cursor_y;
unsigned int top, bottom; /* For the scrolling region */
struct framebuffer *fb;
struct console_cell *cells;
struct color fg;
struct color bg;
struct thread *blink_thread;
bool blink_status; /* true = visible, false = not */
unsigned int saved_x, saved_y; /* Used by ANSI_SAVE/RESTORE_CURSOR */
bool blink_die;
struct tty *tty;
bool multithread_enabled;
struct thread *render_thread;
struct cond condvar;
struct mutex condvar_mutex;
struct vterm_message *msgs;
// Buffer used for any multibyte buffering for utf8
char multibyte_buffer[10];
bool in_escape;
bool seq_finished;
bool in_csi;
bool in_dec;
struct
{
bool dec_private;
bool in_arg;
unsigned long args[MAX_ARGS];
size_t nr_args;
char escape_character;
} csi_data;
size_t do_escape(const char *buffer, size_t length);
utf32_t last_char; // for REP (CSI Ps b)
private:
void process_escape_char(char c);
void reset_escape_status();
void process_csi_char(char c);
void process_dec_char(char c);
void insert_blank(unsigned long nr);
void do_device_attributes(unsigned long nr);
void do_device_status_report(unsigned long nr);
void do_dec_command(char c);
void do_csi_command(char escape);
void do_generic_escape(char escape);
void insert_lines(unsigned long nr);
void repeat_last(unsigned long nr);
void do_ri();
void do_nl();
void do_cr();
};
void vterm_append_msg(struct vterm *vterm, struct vterm_message *msg) ACQUIRE(vterm->condvar_mutex)
{
mutex_lock(&vterm->condvar_mutex);
struct vterm_message **pp = &vterm->msgs;
while (*pp)
{
pp = &(*pp)->next;
}
*pp = msg;
}
void vterm_send_message(struct vterm *vterm, unsigned long message, void *ctx)
{
struct vterm_message *msg = (vterm_message *) zalloc(sizeof(*msg));
/* TODO: Maybe don't crash here? */
assert(msg != NULL);
msg->message = message;
msg->ctx = ctx;
msg->next = NULL;
vterm_append_msg(vterm, msg);
condvar_signal(&vterm->condvar);
mutex_unlock(&vterm->condvar_mutex);
}
static inline uint32_t unpack_rgba(struct color color, struct framebuffer *fb)
{
uint32_t c = ((color.r << fb->color.red_shift) & fb->color.red_mask) |
((color.g << fb->color.green_shift) & fb->color.green_mask) |
((color.b << fb->color.blue_shift) & fb->color.blue_mask);
return c;
}
static void draw_char(uint32_t c, unsigned int x, unsigned int y, struct framebuffer *fb,
struct color fg, struct color bg)
{
struct font *font = get_font_data();
if (c >= font->chars)
c = '?';
volatile char *buffer = (volatile char *) fb->framebuffer;
buffer += y * fb->pitch + x * (fb->bpp / 8);
unsigned int font_start = c * font->height;
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 8; j++)
{
struct color color;
unsigned char f = font->font_bitmap[font_start + i];
if (f & font->mask[j])
color = fg;
else
color = bg;
uint32_t color_u32 = unpack_rgba(color, fb);
volatile uint32_t *b = (volatile uint32_t *) ((uint32_t *) buffer + j);
/* If the bpp is 32 bits, we can just blit it out */
if (fb->bpp == 32)
mov_non_temporal(b, color_u32);
else
{
volatile unsigned char *buf = (volatile unsigned char *) (buffer + j);
int bytes = fb->bpp / 8;
for (int k = 0; k < bytes; k++)
{
buf[k] = color_u32;
color_u32 >>= 8;
}
}
}
buffer = (volatile char *) (((char *) buffer) + fb->pitch);
}
}
void do_vterm_flush_all(struct vterm *vterm)
{
struct font *f = get_font_data();
for (unsigned int i = 0; i < vterm->columns; i++)
{
for (unsigned int j = 0; j < vterm->rows; j++)
{
struct console_cell *cell = &vterm->cells[j * vterm->columns + i];
draw_char(cell->codepoint, i * f->width, j * f->height, vterm->fb, cell->fg, cell->bg);
vterm_clear_dirty(cell);
}
}
}
void vterm_flush_all(struct vterm *vterm)
{
if (vterm->multithread_enabled)
vterm_send_message(vterm, VTERM_MESSAGE_FLUSH_ALL, NULL);
else
do_vterm_flush_all(vterm);
}
void vterm_scroll(struct framebuffer *fb, struct vterm *vt)
{
unsigned int start = vt->columns * vt->top;
unsigned int dest = vt->columns * (vt->top + 1);
unsigned int end = vt->columns * (vt->bottom - 1);
memcpy(vt->cells + start, vt->cells + dest, sizeof(struct console_cell) * (end - start));
for (unsigned int i = 0; i < vt->columns; i++)
{
struct console_cell *c = &vt->cells[(vt->bottom - 1) * vt->columns + i];
c->codepoint = ' ';
c->bg = vt->bg;
c->fg = vt->fg;
}
}
void vterm_scroll_down(struct framebuffer *fb, struct vterm *vt)
{
unsigned int start = vt->columns * (vt->top + 1);
unsigned int dest = vt->columns * vt->top;
unsigned int end = vt->columns * (vt->bottom - 1);
memmove(vt->cells + start, vt->cells + dest, sizeof(struct console_cell) * (end - start));
for (unsigned int i = 0; i < vt->columns; i++)
{
struct console_cell *c = &vt->cells[dest + i];
c->codepoint = ' ';
c->bg = vt->bg;
c->fg = vt->fg;
}
}
void vterm_set_char(utf32_t c, unsigned int x, unsigned int y, struct color fg, struct color bg,
struct vterm *vterm)
{
struct console_cell *cell = &vterm->cells[y * vterm->columns + x];
cell->codepoint = c;
cell->fg = fg;
cell->bg = bg;
vterm_set_dirty(cell);
}
void vterm_dirty_cell(unsigned int x, unsigned int y, struct vterm *vt)
{
struct console_cell *cell = &vt->cells[y * vt->columns + x];
vterm_set_dirty(cell);
}
bool vterm_putc(utf32_t c, struct vterm *vt)
{
vt->last_char = c;
if (c == '\0')
return false;
// TODO: Special behavior?
if (c == '\a')
return false;
if (c == '\016' || c == '\017')
{
// Shift in and shift out do nothing right now
return false;
}
if (c == '\t')
{
// TODO: Support variable tab sizes
unsigned int next_stop = ALIGN_TO(vt->cursor_x + 1, 8);
if (next_stop >= vt->columns)
next_stop = vt->columns - 1;
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
vt->cursor_x = next_stop;
return false;
}
struct framebuffer *fb = vt->fb;
if (c == '\n')
{
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
vt->cursor_y++;
}
else if (c == '\r')
{
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
vt->cursor_x = 0;
}
else if (c == '\b')
{
if (vt->cursor_x == 0)
return false;
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
vt->cursor_x--;
}
else if (c == '\x7f')
{
/* Delete char */
if (vt->cursor_x == 0)
return false;
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
vt->cursor_x--;
vterm_set_char(' ', vt->cursor_x, vt->cursor_y, vt->fg, vt->bg, vt);
}
else
{
vterm_set_char(c, vt->cursor_x, vt->cursor_y, vt->fg, vt->bg, vt);
vt->cursor_x++;
}
if (vt->cursor_x == vt->columns)
{
/* Forced newline */
vt->cursor_x = 0;
vt->cursor_y++;
}
if (vt->cursor_y == vt->bottom)
{
if (vt->cursor_y <= vt->bottom && vt->cursor_y >= vt->top)
vterm_scroll(fb, vt);
vt->cursor_y--;
return true;
}
return false;
}
void draw_cursor(int x, int y, struct framebuffer *fb, struct color fg)
{
struct font *font = get_font_data();
volatile char *buffer = (volatile char *) fb->framebuffer;
buffer += y * fb->pitch + x * (fb->bpp / 8);
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 8; j++)
{
struct color color;
unsigned char f = font->cursor_bitmap[i];
if (f & font->mask[j])
continue;
else
color = fg;
uint32_t c = unpack_rgba(color, fb);
volatile uint32_t *b = (volatile uint32_t *) ((uint32_t *) buffer + j);
/* If the bpp is 32 bits, we can just blit it out */
if (fb->bpp == 32)
*b = c;
else
{
volatile unsigned char *buf = (volatile unsigned char *) (buffer + j);
int bytes = fb->bpp / 8;
for (int k = 0; k < bytes; k++)
{
buf[k] = c;
c >>= 8;
}
}
}
buffer = (volatile char *) (((char *) buffer) + fb->pitch);
}
}
void update_cursor(struct vterm *vt)
{
struct framebuffer *fb = vt->fb;
struct font *f = get_font_data();
draw_cursor(vt->cursor_x * f->width, vt->cursor_y * f->height, fb, vt->fg);
}
void vterm_flush(struct vterm *vterm);
/*ssize_t vterm_write(const void *buffer, size_t len, struct tty *c)
{
ssize_t written = 0;
const char *str = buffer;
for(size_t i = 0; i != len; str++, written++, len--)
{
vterm_putc(*str, c->priv);
}
vterm_flush(c->priv);
update_cursor(c->priv);
return written;
}*/
void do_vterm_flush(struct vterm *vterm)
{
struct font *f = get_font_data();
for (unsigned int i = 0; i < vterm->columns; i++)
{
for (unsigned int j = 0; j < vterm->rows; j++)
{
struct console_cell *cell = &vterm->cells[j * vterm->columns + i];
if (vterm_is_dirty(cell))
{
draw_char(cell->codepoint, i * f->width, j * f->height, vterm->fb, cell->fg,
cell->bg);
vterm_clear_dirty(cell);
}
}
}
}
void platform_serial_write(const char *s, size_t size);
void vterm_flush(struct vterm *vterm)
{
if (vterm->multithread_enabled)
vterm_send_message(vterm, VTERM_MESSAGE_FLUSH, NULL);
else
do_vterm_flush(vterm);
}
void vterm_fill_screen(struct vterm *vterm, uint32_t character, struct color fg, struct color bg)
{
for (unsigned int i = 0; i < vterm->columns; i++)
{
for (unsigned int j = 0; j < vterm->rows; j++)
{
struct console_cell *cell = &vterm->cells[i * vterm->rows + j];
cell->codepoint = character;
cell->bg = bg;
cell->fg = fg;
vterm_set_dirty(cell);
}
}
}
struct vterm primary_vterm = {};
static void fb_fill_color(uint32_t color, struct framebuffer *frameb)
{
volatile uint32_t *fb = (volatile uint32_t *) frameb->framebuffer;
for (size_t i = 0; i < frameb->height; i++)
{
for (size_t j = 0; j < frameb->width; j++)
fb[j] = color;
fb = (volatile uint32_t *) ((char *) fb + frameb->pitch);
}
}
void vterm_set_fgcolor(struct color c, struct vterm *vt)
{
vt->fg = c;
}
int isdigit(int c)
{
return c - '0' < 10;
}
void vterm_ansi_adjust_cursor(char code, unsigned long relative, struct vterm *vt)
{
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
if (relative == 0 || relative == 1)
relative = 1;
switch (code)
{
case ANSI_CURSOR_UP: {
unsigned int cy = vt->cursor_y;
unsigned int result = cy - relative;
/* Clamp the result */
if (cy < result)
{
vt->cursor_y = 0;
}
else
vt->cursor_y = result;
break;
}
case ANSI_CURSOR_DOWN:
case CSI_VPR: {
unsigned int cy = vt->cursor_y;
unsigned int result = cy + relative;
if (result > vt->rows - 1)
result = vt->rows - 1;
vt->cursor_y = result;
break;
}
case ANSI_CURSOR_FORWARD: {
unsigned int cx = vt->cursor_x;
unsigned int result = cx + relative;
if (result > vt->columns - 1)
result = vt->columns - 1;
vt->cursor_x = result;
break;
}
case ANSI_CURSOR_BACK: {
unsigned int cx = vt->cursor_x;
unsigned int result = cx - relative;
/* Clamp the result */
if (cx < result)
{
vt->cursor_x = 0;
}
else
vt->cursor_x = result;
break;
}
}
}
void vterm_cursor_set_line(unsigned long line, struct vterm *vt)
{
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
line--;
if (line >= vt->rows)
line = vt->rows - 1;
vt->cursor_y = line;
}
void vterm_ansi_do_cup(unsigned long x, unsigned long y, struct vterm *vt)
{
vterm_dirty_cell(vt->cursor_x, vt->cursor_y, vt);
/* Transform x and y into 0-based values */
x--;
y--;
if (x > vt->columns - 1)
x = vt->columns - 1;
if (y > vt->rows - 1)
y = vt->rows - 1;
vt->cursor_x = x;
vt->cursor_y = y;
}
void vterm_blink_thread(void *ctx)
{
struct vterm *vt = (vterm *) ctx;
while (true)
{
struct font *f = get_font_data();
mutex_lock(&vt->vt_lock);
if (vt->blink_die)
{
mutex_unlock(&vt->vt_lock);
sched_sleep_until_wake();
mutex_lock(&vt->vt_lock);
}
struct color c = vt->fg;
if (vt->blink_status == true)
{
vt->blink_status = false;
c = vt->bg;
}
else
vt->blink_status = true;
draw_cursor(vt->cursor_x * f->width, vt->cursor_y * f->height, vt->fb, c);
mutex_unlock(&vt->vt_lock);
sched_sleep_ms(500);
}
}
void vterm_ansi_do_sgr(unsigned long n, struct vterm *vt)
{
switch (n)
{
case ANSI_SGR_RESET: {
vt->bg = default_bg;
vt->fg = default_fg;
break;
}
case ANSI_SGR_REVERSE: {
struct color temp = vt->bg;
vt->bg = vt->fg;
vt->fg = temp;
break;
}
case ANSI_SGR_DEFAULTBG: {
vt->bg = default_bg;
break;
}
case ANSI_SGR_DEFAULTFG: {
vt->fg = default_fg;
break;
}
case ANSI_SGR_SLOWBLINK:
case ANSI_SGR_RAPIDBLINK: {
/* NOTE: We only support one blink speed, which is
* 2 times/s, 120 times per minute
*/
/*if(!vt->blink_thread)
{
vt->blink_status = false;
vt->blink_thread =
sched_create_thread(vterm_blink_thread,
THREAD_KERNEL, vt);
if(vt->blink_thread) sched_start_thread(vt->blink_thread);
}*/
/* TODO: We need a blink for text, fix */
break;
}
case ANSI_SGR_BLINKOFF: {
if (vt->blink_thread)
thread_destroy(vt->blink_thread);
break;
}
default: {
if (n >= ANSI_SGR_SETBGMIN && n <= ANSI_SGR_SETBGMAX)
{
int index = n - ANSI_SGR_SETBGMIN;
vt->bg = color_table[index];
}
else if (n >= ANSI_SGR_SETFGMIN && n <= ANSI_SGR_SETFGMAX)
{
int index = n - ANSI_SGR_SETFGMIN;
vt->fg = color_table[index];
}
}
}
}
void vterm_ansi_erase_in_line(unsigned long n, struct vterm *vt)
{
switch (n)
{
/* Clear from cursor to end */
case 0: {
for (unsigned int i = vt->cursor_x; i < vt->columns; i++)
{
struct console_cell *c = &vt->cells[vt->cursor_y * vt->columns + i];
c->codepoint = ' ';
c->fg = vt->fg;
c->bg = vt->bg;
vterm_set_dirty(c);
}
break;
}
/* Clear from cursor to beginning */
case 1: {
unsigned int x = vt->cursor_x;
for (unsigned int i = 0; i <= x; i++)
{
struct console_cell *c = &vt->cells[vt->cursor_y * vt->columns + i];
c->codepoint = ' ';
c->fg = vt->fg;
c->bg = vt->bg;
vterm_set_dirty(c);
}
break;
}
/* Clear entire line */
case 2: {
for (unsigned int i = 0; i < vt->columns; i++)
{
struct console_cell *c = &vt->cells[vt->cursor_y * vt->columns + i];
c->codepoint = ' ';
c->fg = vt->fg;
c->bg = vt->bg;
vterm_set_dirty(c);
}
break;
}
}
}
void vterm_ansi_erase_in_display(unsigned long n, struct vterm *vt)
{
switch (n)
{
/* Cursor to end of display */
case 0: {
/* Calculate the cidx, then loop through until the end of the array */
unsigned int cidx = vt->cursor_y * vt->columns + vt->cursor_x;
unsigned int max = vt->rows * vt->columns;
if (cidx + 1 < cidx)
break;
unsigned int iters = max - cidx;
for (unsigned int i = 0; i < iters; i++)
{
struct console_cell *c = &vt->cells[cidx + i];
c->codepoint = ' ';
c->fg = vt->fg;
c->bg = vt->bg;
vterm_set_dirty(c);
}
break;
}
/* Cursor to start of display */
case 1: {
unsigned int cidx = vt->cursor_y * vt->columns + vt->cursor_x;
for (unsigned int i = 0; i <= cidx; i++)
{
struct console_cell *c = &vt->cells[i];
c->codepoint = ' ';
c->fg = vt->fg;
c->bg = vt->bg;
vterm_set_dirty(c);
}
break;
}
/* Whole screen */
case 2:
case 3: {
vterm_fill_screen(vt, ' ', vt->fg, vt->bg);
break;
}
}
vterm_flush(vt);
}
void vterm_csi_delete_chars(unsigned long chars, struct vterm *vt)
{
unsigned int x = vt->cursor_x;
if (chars > vt->columns - x)
chars = vt->columns - x;
memcpy(&vt->cells[vt->cursor_y * vt->columns + x],
&vt->cells[vt->cursor_y * vt->columns + x + chars],
sizeof(console_cell) * (vt->columns - x - chars));
for (unsigned int i = 0; i < vt->columns; i++)
{
struct console_cell *c = &vt->cells[vt->cursor_y * vt->columns + i];
if (i >= vt->columns - chars)
{
c->codepoint = ' ';
c->fg = vt->fg;
c->bg = vt->bg;
}
vterm_set_dirty(c);
}
}
void vterm::insert_blank(unsigned long nr)
{
auto x = cursor_x;
auto to_blank = cul::min((unsigned int) nr, columns - x);
memmove(&cells[cursor_y * columns + x + to_blank], &cells[cursor_y * columns + x],
sizeof(console_cell) * (columns - nr - x));
for (unsigned int i = x; i < columns; i++)
{
auto &cell = cells[cursor_y * columns + i];
if (i < x + to_blank)
{
cell.codepoint = ' ';
cell.fg = fg;
cell.bg = bg;
}
vterm_set_dirty(&cell);
}
}
void vterm::do_dec_command(char c)
{
switch (c)
{
case DEC_DECALN:
vterm_fill_screen(this, 'E', fg, bg);
}
}
void vterm::do_ri()
{
vterm_dirty_cell(cursor_x, cursor_y, this);
if (cursor_y == top)
{
vterm_scroll_down(fb, this);
vterm_flush_all(this);
}
else if (cursor_y)
cursor_y--;
}
void vterm::do_nl()
{
vterm_dirty_cell(cursor_x, cursor_y, this);
cursor_y++;
if (cursor_y == bottom)
{
if (cursor_y <= bottom && cursor_y >= top)
{
vterm_scroll(fb, this);
vterm_flush_all(this);
}
cursor_y--;
}
}
void vterm::do_cr()
{
vterm_dirty_cell(cursor_x, cursor_y, this);
cursor_x = 0;
}
void vterm::do_generic_escape(char escape)
{
switch (escape)
{
case 'D': {
/* 'D' = NL */
do_nl();
break;
}
case 'M': {
/* 'M' == C1 RI (0x8d), move cursor up and scroll if needed */
do_ri();
break;
}
case 'E': {
/* 'E' = CRNL */
do_cr();
do_nl();
break;
}
case ESC_SAVECUR: {
saved_x = cursor_x;
saved_y = cursor_y;
break;
}
case ESC_RESTORECUR: {
cursor_x = saved_x;
cursor_y = saved_y;
break;
}
}
}
void vterm::do_device_attributes(unsigned long nr)
{
if (nr != 0)
return; // Unrecognized command
// c with nr = 5 is a "who are you" command
// Return Base VT100, no options in stdin
tty_send_response(tty, (char *) "\x1b[?1;0c");
}
void vterm::do_device_status_report(unsigned long nr)
{
switch (nr)
{
// Command from host – Please report status (using a DSR control sequence)
case 5: {
// Only two responses possible
// Ps = 0 -> terminal is fine
// Ps = 3 -> malfunction
// As we are a virtual terminal I don't see a usecase for malfunction here.
tty_send_response(tty, (char *) "\x1b[0n");
break;
}
// Command from host - Please report active position (using a CPR control sequence)
case 6: {
char buffer[20];
snprintf(buffer, 20, "\x1b[%u;%uR", cursor_y + 1, cursor_x + 1);
tty_send_response(tty, buffer);
break;
}
}
}
void vterm::reset_escape_status()
{
in_escape = false;
if (in_csi || in_dec)
{
for (auto &n : csi_data.args)
n = 0;
csi_data.nr_args = 0;
csi_data.escape_character = '\0';
csi_data.in_arg = false;
csi_data.dec_private = false;
}
in_dec = false;
in_csi = false;
seq_finished = false;
}
void vterm::process_dec_char(char c)
{
// DEC commands have the format