-
-
Notifications
You must be signed in to change notification settings - Fork 487
/
terminal.c
2758 lines (2435 loc) · 76.1 KB
/
terminal.c
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
/**
* @brief Virtual terminal emulator.
*
* Provides a graphical character cell terminal with support for
* antialiased text, basic Unicode, bitmap fallbacks, nearly
* complete ANSI escape sequence support, 256- and 24-bit color,
* scrollback, selection, alternate screens, and various scroll
* methods.
*
* @copyright
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2022 K. Lange
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <errno.h>
#include <pty.h>
#include <wchar.h>
#include <dlfcn.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/fswait.h>
#define TRACE_APP_NAME "terminal"
#include <toaru/trace.h>
#include <toaru/decodeutf8.h>
#include <toaru/yutani.h>
#include <toaru/decorations.h>
#include <toaru/graphics.h>
#include <toaru/kbd.h>
#include <toaru/termemu.h>
#include <toaru/spinlock.h>
#include <toaru/list.h>
#include <toaru/menu.h>
#include <toaru/text.h>
/* 16- and 256-color palette */
#include "terminal-palette.h"
/* Bitmap font */
#include "terminal-font.h"
/* Show help text */
static void usage(char * argv[]) {
printf(
"Terminal Emulator\n"
"\n"
"usage: %s [-Fbxn] [-s SCALE] [-g WIDTHxHEIGHT] [COMMAND...]\n"
"\n"
" -F --fullscreen \033[3mRun in fullscreen (background) mode.\033[0m\n"
" -b --bitmap \033[3mUse the integrated bitmap font.\033[0m\n"
" -s --scale \033[3mScale the font in antialiased mode by a given amount.\033[0m\n"
" -h --help \033[3mShow this help message.\033[0m\n"
" -x --grid \033[3mMake resizes round to nearest match for character cell size.\033[0m\n"
" -n --no-frame \033[3mDisable decorations.\033[0m\n"
" -g --geometry \033[3mSet requested terminal size WIDTHxHEIGHT\033[0m\n"
" -B --blurred \033[3mBlur background behind terminal.\033[0m\n"
" -S --scrollback \033[3mSet the scrollback buffer size, 0 for unlimited.\033[0m\n"
"\n"
" This terminal emulator provides basic support for VT220 escapes and\n"
" XTerm extensions, including 256 color support and font effects.\n",
argv[0]);
}
/* master and slave pty descriptors */
static int fd_master, fd_slave;
static FILE * terminal;
static pid_t child_pid = 0;
static int scale_fonts = 0; /* Whether fonts should be scaled */
static float font_scaling = 1.0; /* How much they should be scaled by */
static uint16_t term_width = 0; /* Width of the terminal (in cells) */
static uint16_t term_height = 0; /* Height of the terminal (in cells) */
static uint16_t font_size = 16; /* Font size according to tt library */
static uint16_t char_width = 8; /* Width of a cell in pixels */
static uint16_t char_height = 17; /* Height of a cell in pixels */
static uint16_t char_offset = 0; /* Offset of the font within the cell */
static int csr_x = 0; /* Cursor X */
static int csr_y = 0; /* Cursor Y */
static int csr_h = 0; /* Cursor last column hold flag */
static uint32_t current_fg = 7; /* Current foreground color */
static uint32_t current_bg = 0; /* Current background color */
static term_cell_t * term_buffer = NULL; /* The active terminal cell buffer */
static term_cell_t * term_buffer_a = NULL; /* The main buffer */
static term_cell_t * term_buffer_b = NULL; /* The secondary buffer */
static term_cell_t * term_mirror = NULL; /* What we want to draw */
static term_cell_t * term_display = NULL; /* What we think we've drawn already */
static term_state_t * ansi_state = NULL; /* ANSI parser library state */
static int active_buffer = 0;
static int _orig_x = 0;
static int _orig_y = 0;
static uint32_t _orig_fg = 7;
static uint32_t _orig_bg = 0;
static bool cursor_on = 1; /* Whether or not the cursor should be rendered */
static bool _fullscreen = 0; /* Whether or not we are running in fullscreen mode (GUI only) */
static bool _no_frame = 0; /* Whether to disable decorations or not */
static bool _use_aa = 1; /* Whether or not to use best-available anti-aliased renderer */
static bool _free_size = 1; /* Disable rounding when resized */
static struct TT_Font * _tt_font_normal = NULL;
static struct TT_Font * _tt_font_bold = NULL;
static struct TT_Font * _tt_font_oblique = NULL;
static struct TT_Font * _tt_font_bold_oblique = NULL;
static struct TT_Font * _tt_font_fallback = NULL;
static struct TT_Font * _tt_font_japanese = NULL;
static list_t * images_list = NULL;
static int menu_bar_height = 24;
/* Text selection information */
static int selection = 0;
static int selection_start_x = 0;
static int selection_start_y = 0;
static int selection_end_x = 0;
static int selection_end_y = 0;
static char * selection_text = NULL;
static int _selection_count = 0;
static int _selection_i = 0;
/* Mouse state */
static int last_mouse_x = -1;
static int last_mouse_y = -1;
static int button_state = 0;
static unsigned long long mouse_ticks = 0;
static yutani_window_t * window = NULL; /* GUI window */
static yutani_t * yctx = NULL;
/* Window flip bounds */
static int32_t l_x = INT32_MAX;
static int32_t l_y = INT32_MAX;
static int32_t r_x = -1;
static int32_t r_y = -1;
static uint32_t window_width = 640;
static uint32_t window_height = 480;
static bool window_position_set = 0;
static int32_t window_left = 0;
static int32_t window_top = 0;
#define TERMINAL_TITLE_SIZE 512
static char terminal_title[TERMINAL_TITLE_SIZE];
static size_t terminal_title_length = 0;
static gfx_context_t * ctx;
static struct MenuList * menu_right_click = NULL;
static void render_decors(void);
static void term_clear(int i);
static void reinit(void);
static void term_redraw_cursor();
static int decor_left_width = 0;
static int decor_top_height = 0;
static int decor_right_width = 0;
static int decor_bottom_height = 0;
static int decor_width = 0;
static int decor_height = 0;
struct scrollback_row {
unsigned short width;
term_cell_t cells[];
};
static size_t max_scrollback = 10000;
static list_t * scrollback_list = NULL;
static int scrollback_offset = 0;
/* Menu bar entries */
struct menu_bar terminal_menu_bar = {0};
struct menu_bar_entries terminal_menu_entries[] = {
{"File", "file"},
{"Edit", "edit"},
{"View", "view"},
{"Help", "help"},
{NULL, NULL},
};
/* Trigger to exit the terminal when the child process dies or
* we otherwise receive an exit signal */
static volatile int exit_application = 0;
static void cell_redraw(uint16_t x, uint16_t y);
static void cell_redraw_inverted(uint16_t x, uint16_t y);
static void cell_redraw_offset(uint16_t x, uint16_t y);
static void cell_redraw_offset_inverted(uint16_t x, uint16_t y);
static void update_bounds(void);
static uint64_t get_ticks(void) {
struct timeval now;
gettimeofday(&now, NULL);
return (uint64_t)now.tv_sec * 1000000LL + (uint64_t)now.tv_usec;
}
static void display_flip(void) {
if (l_x != INT32_MAX && l_y != INT32_MAX) {
flip(ctx);
yutani_flip_region(yctx, window, l_x, l_y, r_x - l_x, r_y - l_y);
l_x = INT32_MAX;
l_y = INT32_MAX;
r_x = -1;
r_y = -1;
}
}
/* Returns the lower of two shorts */
static int32_t min(int32_t a, int32_t b) {
return (a < b) ? a : b;
}
/* Returns the higher of two shorts */
static int32_t max(int32_t a, int32_t b) {
return (a > b) ? a : b;
}
/*
* Convert codepoint to UTF-8
*
* Returns length of byte sequence written.
*/
static int to_eight(uint32_t codepoint, char * out) {
memset(out, 0x00, 7);
if (codepoint < 0x0080) {
out[0] = (char)codepoint;
} else if (codepoint < 0x0800) {
out[0] = 0xC0 | (codepoint >> 6);
out[1] = 0x80 | (codepoint & 0x3F);
} else if (codepoint < 0x10000) {
out[0] = 0xE0 | (codepoint >> 12);
out[1] = 0x80 | ((codepoint >> 6) & 0x3F);
out[2] = 0x80 | (codepoint & 0x3F);
} else if (codepoint < 0x200000) {
out[0] = 0xF0 | (codepoint >> 18);
out[1] = 0x80 | ((codepoint >> 12) & 0x3F);
out[2] = 0x80 | ((codepoint >> 6) & 0x3F);
out[3] = 0x80 | ((codepoint) & 0x3F);
} else if (codepoint < 0x4000000) {
out[0] = 0xF8 | (codepoint >> 24);
out[1] = 0x80 | (codepoint >> 18);
out[2] = 0x80 | ((codepoint >> 12) & 0x3F);
out[3] = 0x80 | ((codepoint >> 6) & 0x3F);
out[4] = 0x80 | ((codepoint) & 0x3F);
} else {
out[0] = 0xF8 | (codepoint >> 30);
out[1] = 0x80 | ((codepoint >> 24) & 0x3F);
out[2] = 0x80 | ((codepoint >> 18) & 0x3F);
out[3] = 0x80 | ((codepoint >> 12) & 0x3F);
out[4] = 0x80 | ((codepoint >> 6) & 0x3F);
out[5] = 0x80 | ((codepoint) & 0x3F);
}
return strlen(out);
}
/* Set the terminal title string */
static void set_title(char * c) {
int len = min(TERMINAL_TITLE_SIZE, strlen(c)+1);
memcpy(terminal_title, c, len);
terminal_title[len-1] = '\0';
terminal_title_length = len - 1;
render_decors();
}
/* Call a function for each selected cell */
static void iterate_selection(void (*func)(uint16_t x, uint16_t y)) {
if (!selection) return;
if (selection_end_y < selection_start_y) {
for (int x = selection_end_x; x < term_width; ++x) {
func(x, selection_end_y);
}
for (int y = selection_end_y + 1; y < selection_start_y; ++y) {
for (int x = 0; x < term_width; ++x) {
func(x, y);
}
}
for (int x = 0; x <= selection_start_x; ++x) {
func(x, selection_start_y);
}
} else if (selection_start_y == selection_end_y) {
if (selection_start_x > selection_end_x) {
for (int x = selection_end_x; x <= selection_start_x; ++x) {
func(x, selection_start_y);
}
} else {
for (int x = selection_start_x; x <= selection_end_x; ++x) {
func(x, selection_start_y);
}
}
} else {
for (int x = selection_start_x; x < term_width; ++x) {
func(x, selection_start_y);
}
for (int y = selection_start_y + 1; y < selection_end_y; ++y) {
for (int x = 0; x < term_width; ++x) {
func(x, y);
}
}
for (int x = 0; x <= selection_end_x; ++x) {
func(x, selection_end_y);
}
}
}
/* Redraw the selection with the selection hint (inversion) */
static void redraw_selection(void) {
iterate_selection(cell_redraw_offset_inverted);
}
static term_cell_t * cell_at(uint16_t x, uint16_t _y) {
int y = _y;
y -= scrollback_offset;
if (y >= 0) {
return &term_buffer[y * term_width + x];
} else {
node_t * node = scrollback_list->tail;
for (; y < -1; y++) {
if (!node) break;
node = node->prev;
}
if (node) {
struct scrollback_row * row = (struct scrollback_row *)node->value;
if (row && x < row->width) {
return &row->cells[x];
}
}
}
return NULL;
}
static void mark_cell(uint16_t x, uint16_t y) {
term_cell_t * c = cell_at(x,y);
if (c) {
c->flags |= 0x200;
}
}
static void mark_selection(void) {
iterate_selection(mark_cell);
}
static void red_cell(uint16_t x, uint16_t y) {
term_cell_t * c = cell_at(x,y);
if (c) {
if (c->flags & 0x200) {
c->flags &= ~(0x200);
} else {
c->flags |= 0x400;
}
}
}
static void flip_selection(void) {
iterate_selection(red_cell);
for (int y = 0; y < term_height; ++y) {
for (int x = 0; x < term_width; ++x) {
term_cell_t * c = cell_at(x,y);
if (c) {
if (c->flags & 0x200) cell_redraw_offset(x,y);
if (c->flags & 0x400) cell_redraw_offset_inverted(x,y);
c->flags &= ~(0x600);
}
}
}
}
/* Figure out how long the UTF-8 selection string should be. */
static void count_selection(uint16_t x, uint16_t _y) {
int y = _y;
y -= scrollback_offset;
if (y >= 0) {
term_cell_t * cell = &term_buffer[y * term_width + x];
if (!(cell->flags & ANSI_EXT_IMG)) {
if (((uint32_t *)cell)[0] != 0x00000000) {
char tmp[7];
_selection_count += to_eight(cell->c, tmp);
}
}
} else {
node_t * node = scrollback_list->tail;
for (; y < -1; y++) {
if (!node) break;
node = node->prev;
}
if (node) {
struct scrollback_row * row = (struct scrollback_row *)node->value;
if (row && x < row->width) {
term_cell_t * cell = &row->cells[x];
if (cell && ((uint32_t *)cell)[0] != 0x00000000) {
char tmp[7];
_selection_count += to_eight(cell->c, tmp);
}
}
}
}
if (x == term_width - 1) {
_selection_count++;
}
}
/* Fill the selection text buffer with the selected text. */
void write_selection(uint16_t x, uint16_t _y) {
int y = _y;
y -= scrollback_offset;
if (y >= 0) {
term_cell_t * cell = &term_buffer[y * term_width + x];
if (!(cell->flags & ANSI_EXT_IMG)) {
if (((uint32_t *)cell)[0] != 0x00000000 && cell->c != 0xFFFF) {
char tmp[7];
int count = to_eight(cell->c, tmp);
for (int i = 0; i < count; ++i) {
selection_text[_selection_i] = tmp[i];
_selection_i++;
}
}
}
} else {
node_t * node = scrollback_list->tail;
for (; y < -1; y++) {
if (!node) break;
node = node->prev;
}
if (node) {
struct scrollback_row * row = (struct scrollback_row *)node->value;
if (row && x < row->width) {
term_cell_t * cell = &row->cells[x];
if (cell && ((uint32_t *)cell)[0] != 0x00000000 && cell->c != 0xFFFF) {
char tmp[7];
int count = to_eight(cell->c, tmp);
for (int i = 0; i < count; ++i) {
selection_text[_selection_i] = tmp[i];
_selection_i++;
}
}
}
}
}
if (x == term_width - 1) {
selection_text[_selection_i] = '\n';;
_selection_i++;
}
}
/* Copy the selection text to the clipboard. */
static char * copy_selection(void) {
_selection_count = 0;
iterate_selection(count_selection);
if (selection_text) {
free(selection_text);
}
if (_selection_count == 0) {
return NULL;
}
selection_text = malloc(_selection_count + 1);
selection_text[_selection_count] = '\0';
_selection_i = 0;
iterate_selection(write_selection);
if (selection_text[_selection_count-1] == '\n') {
/* Don't end on a line feed */
selection_text[_selection_count-1] = '\0';
}
yutani_set_clipboard(yctx, selection_text);
return selection_text;
}
static volatile int input_buffer_lock = 0;
static int input_buffer_semaphore[2];
static list_t * input_buffer_queue = NULL;
struct input_data {
size_t len;
char data[];
};
void * handle_input_writing(void * unused) {
(void)unused;
while (1) {
/* Read one byte from semaphore; as long as semaphore has data,
* there is another input blob to write to the TTY */
char tmp[1];
int c = read(input_buffer_semaphore[0],tmp,1);
if (c > 0) {
/* Retrieve blob */
spin_lock(&input_buffer_lock);
node_t * blob = list_dequeue(input_buffer_queue);
spin_unlock(&input_buffer_lock);
/* No blobs? This shouldn't happen, but just in case, just continue */
if (!blob) {
continue;
}
/* Write blob data to the tty */
struct input_data * value = blob->value;
write(fd_master, value->data, value->len);
free(blob->value);
free(blob);
} else {
/* The pipe has closed, terminal is exiting */
break;
}
}
return NULL;
}
static void write_input_buffer(char * data, size_t len) {
struct input_data * d = malloc(sizeof(struct input_data) + len);
d->len = len;
memcpy(&d->data, data, len);
spin_lock(&input_buffer_lock);
list_insert(input_buffer_queue, d);
spin_unlock(&input_buffer_lock);
write(input_buffer_semaphore[1], d, 1);
}
/* Stuffs a string into the stdin of the terminal's child process
* Useful for things like the ANSI DSR command. */
static void input_buffer_stuff(char * str) {
size_t len = strlen(str);
write_input_buffer(str, len);
}
/* Redraw the decorations */
static void render_decors(void) {
/* Don't draw decorations or bother advertising the window if in "fullscreen mode" */
if (_fullscreen) return;
if (!_no_frame) {
/* Draw the decorations */
render_decorations(window, ctx, terminal_title_length ? terminal_title : "Terminal");
/* Update menu bar position and size */
terminal_menu_bar.x = decor_left_width;
terminal_menu_bar.y = decor_top_height;
terminal_menu_bar.width = window_width;
terminal_menu_bar.window = window;
/* Redraw the menu bar */
menu_bar_render(&terminal_menu_bar, ctx);
}
/* Advertise the window icon to the panel. */
yutani_window_advertise_icon(yctx, window, terminal_title_length ? terminal_title : "Terminal", "utilities-terminal");
/*
* Flip the whole window
* We do this regardless of whether we drew decorations to catch
* a case where decorations are toggled.
*/
l_x = 0; l_y = 0;
r_x = window->width;
r_y = window->height;
display_flip();
}
/* Set a pixel in the terminal cell area */
static inline void term_set_point(uint16_t x, uint16_t y, uint32_t color ) {
GFX(ctx, (x+decor_left_width),(y+decor_top_height+menu_bar_height)) = color;
}
static void _fill_region(uint32_t _bg, uint16_t x, uint16_t y, uint16_t width, uint16_t height) {
for (uint8_t i = 0; i < height; ++i) {
for (uint8_t j = 0; j < width; ++j) {
term_set_point(x+j,y+i,_bg);
}
}
}
/* Draw a partial block character. */
static void draw_semi_block(int c, int x, int y, uint32_t fg, uint32_t bg) {
bg = premultiply(bg);
fg = alpha_blend_rgba(bg, premultiply(fg));
_fill_region(bg, x, y, char_width, char_height);
if (c == 0x2580) {
for (uint8_t i = 0; i < char_height / 2; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
term_set_point(x+j,y+i,fg);
}
}
} else if (c >= 0x2589) {
c -= 0x2588;
int width = char_width - ((c * char_width) / 8);
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = 0; j < width; ++j) {
term_set_point(x+j, y+i, fg);
}
}
} else {
c -= 0x2580;
int height = char_height - ((c * char_height) / 8);
for (uint8_t i = height; i < char_height; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
term_set_point(x+j, y+i,fg);
}
}
}
}
static void draw_box_drawing(int c, int x, int y, uint32_t fg, uint32_t bg) {
bg = premultiply(bg);
fg = alpha_blend_rgba(bg, premultiply(fg));
_fill_region(bg, x, y, char_width, char_height);
int lineheight = char_height / 16;
int linewidth = char_width / 8;
lineheight = lineheight < 1 ? 1 : lineheight;
linewidth = linewidth < 1 ? 1 : linewidth;
int mid_x = char_width / 2 - linewidth / 2;
int mid_y = char_height / 2 - lineheight / 2;
int extra_x = (mid_x * 2 < char_width) ? char_width - mid_x * 2 : 0;
int extra_y = (mid_y * 2 < char_height) ? char_height - mid_y * 2 : 0;
#define UP _fill_region(fg, x + mid_x, y, linewidth, mid_y + lineheight)
#define DOWN _fill_region(fg, x + mid_x, y + mid_y, linewidth, mid_y + extra_y)
#define LEFT _fill_region(fg, x, y + mid_y, mid_x + linewidth, lineheight)
#define RIGHT _fill_region(fg, x + mid_x, y + mid_y, mid_x + extra_x, lineheight)
#define VERT _fill_region(fg, x + mid_x, y, linewidth, char_height)
#define HORI _fill_region(fg, x, y + mid_y, char_width, lineheight)
switch (c) {
case 0x2500: HORI; break;
case 0x2502: VERT; break;
case 0x250c: RIGHT; DOWN; break;
case 0x2510: LEFT; DOWN; break;
case 0x2514: UP; RIGHT; break;
case 0x2518: UP; LEFT; break;
case 0x251c: VERT; RIGHT; break;
case 0x2524: VERT; LEFT; break;
case 0x252c: HORI; DOWN; break;
case 0x2534: UP; HORI; break;
case 0x253c: HORI; VERT; break;
case 0x2574: LEFT; break;
case 0x2575: UP; break;
case 0x2576: RIGHT; break;
case 0x2577: DOWN; break;
}
}
#include "apps/ununicode.h"
struct GlyphCacheEntry {
struct TT_Font * font;
sprite_t * sprite;
uint32_t size;
uint32_t glyph;
uint32_t color;
};
static struct GlyphCacheEntry glyph_cache[1024];
static unsigned long _hits = 0;
static unsigned long _misses = 0;
static unsigned long _wrongcolor = 0;
static void _menu_action_cache_stats(struct MenuEntry * self) {
char msg[400];
unsigned long count = 0;
unsigned long size = 0;
for (int i = 0; i < 1024; ++i) {
if (glyph_cache[i].sprite) {
count++;
size += glyph_cache[i].sprite->width * glyph_cache[i].sprite->height * 4;
}
}
snprintf(msg, 400,
"Hits: %lu\n"
"Misses: %lu\n"
"Wrong color: %lu\n"
"Populated cache entries: %lu\n"
"Size of sprites: %lu\n",
_hits, _misses, _wrongcolor, count, size);
write(fd_slave, msg, strlen(msg));
}
static void _menu_action_clear_cache(struct MenuEntry * self) {
for (int i = 0; i < 1024; ++i) {
if (glyph_cache[i].sprite) {
sprite_free(glyph_cache[i].sprite);
}
}
memset(glyph_cache,0,sizeof(glyph_cache));
}
static void draw_cached_glyph(gfx_context_t * ctx, struct TT_Font * _font, uint32_t size, int x, int y, uint32_t glyph, uint32_t fg, int flags) {
unsigned int hash = (((uintptr_t)_font >> 8) ^ (glyph * size)) & 1023;
struct GlyphCacheEntry * entry = &glyph_cache[hash];
if (entry->font != _font || entry->size != size || entry->glyph != glyph) {
if (entry->sprite) sprite_free(entry->sprite);
int wide = (flags & ANSI_WIDE) ? 2 : 1;
tt_set_size(_font, size);
entry->font = _font;
entry->size = size;
entry->glyph = glyph;
entry->sprite = create_sprite(char_width * wide, char_height, ALPHA_EMBEDDED);
entry->color = _ALP(fg) == 255 ? fg : 0xFFFFFFFF;
gfx_context_t * _ctx = init_graphics_sprite(entry->sprite);
draw_fill(_ctx, 0);
tt_draw_glyph(_ctx, entry->font, 0, char_offset, glyph, entry->color);
free(_ctx);
_misses++;
} else {
_hits++;
}
if (entry->color != fg) {
_wrongcolor++;
draw_sprite_alpha_paint(ctx, entry->sprite, x, y, 1.0, fg);
} else {
draw_sprite(ctx, entry->sprite, x, y);
}
}
/* Write a character to the window. */
static void term_write_char(uint32_t val, uint16_t x, uint16_t y, uint32_t fg, uint32_t bg, uint8_t flags) {
uint32_t _fg, _bg;
/* Select foreground color from palette. */
if (fg < PALETTE_COLORS) {
_fg = term_colors[fg];
_fg |= 0xFF << 24;
} else {
_fg = fg;
}
/* Select background color from aplette. */
if (bg < PALETTE_COLORS) {
_bg = term_colors[bg];
if (flags & ANSI_SPECBG) {
_bg |= 0xFF << 24;
} else {
_bg |= TERM_DEFAULT_OPAC << 24;
}
} else {
_bg = bg;
}
if (_fullscreen) {
_bg |= 0xFF << 24;
}
switch (val) {
/* Line drawing */
case 0x2500:
case 0x2502:
case 0x250c:
case 0x2510:
case 0x2514:
case 0x2518:
case 0x251c:
case 0x2524:
case 0x252c:
case 0x2534:
case 0x253c:
case 0x2574:
case 0x2575:
case 0x2576:
case 0x2577:
draw_box_drawing(val, x, y, _fg, _bg);
goto _extra_stuff;
/* Semi-filled blocks */
case 0x2580 ... 0x258f: {
draw_semi_block(val, x, y, _fg, _bg);
goto _extra_stuff;
}
/* Instead of checker, does 50% opacity fill */
case 0x2591:
case 0x2592:
case 0x2593:
_fill_region(alpha_blend_rgba(premultiply(_bg), interp_colors(rgb(0,0,0), premultiply(_fg), 255 * (val - 0x2590) / 4)), x, y, char_width, char_height);
goto _extra_stuff;
default:
break;
}
/* Draw glyphs */
if (_use_aa) {
if (val == 0xFFFF) return;
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
term_set_point(x+j,y+i,_bg);
}
}
if (flags & ANSI_WIDE) {
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = char_width; j < 2 * char_width; ++j) {
term_set_point(x+j,y+i,_bg);
}
}
}
if (val < 32 || val == ' ') {
goto _extra_stuff;
}
struct TT_Font * _font = _tt_font_normal;
if (flags & ANSI_BOLD && flags & ANSI_ITALIC) {
_font = _tt_font_bold_oblique;
} else if (flags & ANSI_BOLD) {
_font = _tt_font_bold;
} else if (flags & ANSI_ITALIC) {
_font = _tt_font_oblique;
}
unsigned int glyph = tt_glyph_for_codepoint(_font, val);
/* Try the regular sans serif font as a fallback */
if (!glyph) {
int nglyph = tt_glyph_for_codepoint(_tt_font_fallback, val);
if (nglyph) {
_font = _tt_font_fallback;
glyph = nglyph;
}
}
/* Try the VL Gothic, if it's installed and this is a reasonably high codepoint */
if (!glyph && _tt_font_japanese && val >= 0x2E80) {
int nglyph = tt_glyph_for_codepoint(_tt_font_japanese, val);
if (nglyph) {
_font = _tt_font_japanese;
glyph = nglyph;
}
}
int _x = x + decor_left_width;
int _y = y + decor_top_height + menu_bar_height;
draw_cached_glyph(ctx, _font, font_size, _x,_y, glyph, _fg, flags);
} else {
/* Convert other unicode characters. */
if (val > 128) {
val = ununicode(val);
}
/* Draw using the bitmap font. */
uint8_t * c = large_font[val];
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
if (c[i] & (1 << (LARGE_FONT_MASK-j))) {
term_set_point(x+j,y+i,_fg);
} else {
term_set_point(x+j,y+i,_bg);
}
}
}
}
/* Draw additional text elements, like underlines and cross-outs. */
_extra_stuff:
if (flags & ANSI_UNDERLINE) {
for (uint8_t i = 0; i < char_width; ++i) {
term_set_point(x + i, y + char_height - 1, _fg);
}
}
if (flags & ANSI_CROSS) {
for (uint8_t i = 0; i < char_width; ++i) {
term_set_point(x + i, y + char_height - 7, _fg);
}
}
if (flags & ANSI_BORDER) {
for (uint8_t i = 0; i < char_height; ++i) {
term_set_point(x , y + i, _fg);
term_set_point(x + (char_width - 1), y + i, _fg);
}
for (uint8_t j = 0; j < char_width; ++j) {
term_set_point(x + j, y, _fg);
term_set_point(x + j, y + (char_height - 1), _fg);
}
}
/* Calculate the bounds of the updated region of the window */
l_x = min(l_x, decor_left_width + x);
l_y = min(l_y, decor_top_height+menu_bar_height + y);
if (flags & ANSI_WIDE) {
r_x = max(r_x, decor_left_width + x + char_width * 2);
r_y = max(r_y, decor_top_height+menu_bar_height + y + char_height * 2);
} else {
r_x = max(r_x, decor_left_width + x + char_width);
r_y = max(r_y, decor_top_height+menu_bar_height + y + char_height);
}
}
static void term_mirror_set(uint16_t x, uint16_t y, uint32_t val, uint32_t fg, uint32_t bg, uint8_t flags) {
if (x >= term_width || y >= term_height) return;
term_cell_t * cell = &term_mirror[y * term_width + x];
cell->c = val;
cell->fg = fg;
cell->bg = bg;
cell->flags = flags;
}
static void term_mirror_copy(uint16_t x, uint16_t y, term_cell_t * from) {
if (x >= term_width || y >= term_height) return;
term_cell_t * cell = &term_mirror[y * term_width + x];
if (!from->c && !from->fg && !from->bg) {
cell->c = ' ';
cell->fg = TERM_DEFAULT_FG;
cell->bg = TERM_DEFAULT_BG;
cell->flags = from->flags;
} else {
*cell = *from;
}
}
static void term_mirror_copy_inverted(uint16_t x, uint16_t y, term_cell_t * from) {
if (x >= term_width || y >= term_height) return;
term_cell_t * cell = &term_mirror[y * term_width + x];
if (!from->c && !from->fg && !from->bg) {
cell->c = ' ';
cell->fg = TERM_DEFAULT_BG;
cell->bg = TERM_DEFAULT_FG;
cell->flags = from->flags;
} else if (from->flags & ANSI_EXT_IMG) {
cell->c = ' ';
cell->fg = from->fg;
cell->bg = from->bg;
cell->flags = from->flags | ANSI_SPECBG;
} else {
cell->c = from->c;
cell->fg = from->bg;
cell->bg = from->fg;
cell->flags = from->flags | ANSI_SPECBG;
}
}
/* Set a terminal cell */
static void cell_set(uint16_t x, uint16_t y, uint32_t c, uint32_t fg, uint32_t bg, uint32_t flags) {
/* Avoid setting cells out of range. */
if (x >= term_width || y >= term_height) return;
/* Calculate the cell position in the terminal buffer */
term_cell_t * cell = &term_buffer[y * term_width + x];
/* Set cell attributes */
cell->c = c;
cell->fg = fg;
cell->bg = bg;
cell->flags = flags;
}
/* Redraw an embedded image cell */
static void redraw_cell_image(uint16_t x, uint16_t y, term_cell_t * cell, int inverted) {
/* Avoid setting cells out of range. */
if (x >= term_width || y >= term_height) return;
/* Draw the image data */
uint32_t * data = (uint32_t *)((uintptr_t)cell->bg << 32 | cell->fg);
if (inverted) {
for (uint32_t yy = 0; yy < char_height; ++yy) {
for (uint32_t xx = 0; xx < char_width; ++xx) {
uint32_t alpha = 0xFF000000 & *data;
uint32_t color = 0xFFFFFF - (*data & 0xFFFFFF);
term_set_point(x * char_width + xx, y * char_height + yy, color | alpha);
data++;
}
}
} else {
for (uint32_t yy = 0; yy < char_height; ++yy) {
for (uint32_t xx = 0; xx < char_width; ++xx) {
term_set_point(x * char_width + xx, y * char_height + yy, *data);
data++;
}
}
}
/* Update bounds */
l_x = min(l_x, decor_left_width + x * char_width);
l_y = min(l_y, decor_top_height+menu_bar_height + y * char_height);
r_x = max(r_x, decor_left_width + x * char_width + char_width);
r_y = max(r_y, decor_top_height+menu_bar_height + y * char_height + char_height);