This repository has been archived by the owner on Oct 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathterminal.c
2412 lines (2174 loc) · 69.4 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
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* 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-2018 K. Lange
*
* Terminal Emulator
*
* Graphical terminal emulator.
*
* Provides a number of features:
* - Windowed and full screen modes
* - Antialiased fonts
* - Built-in fallback bitmap font
* - ANSI escape support
* - 256 colors
*/
#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 <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/sdf.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"
" -f --no-ft \033[3mForce disable the freetype backend.\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 float font_gamma = 1.7; /* Gamma to use for SDF library */
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 SDF library */
static uint16_t char_width = 9; /* 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 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 terminal cell buffer */
static term_cell_t * term_buffer_a = NULL;
static term_cell_t * term_buffer_b = NULL;
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 _have_freetype = 0; /* Whether freetype is available */
static bool _force_no_ft = 0; /* Whether to force disable the freetype backend */
static bool _hold_out = 0; /* state indicator on last cell ignore \n */
static bool _free_size = 1; /* Disable rounding when resized */
/** Freetype extension renderer functions */
static void (*freetype_set_font_face)(int face) = NULL;
static void (*freetype_set_font_size)(int size) = NULL;
static void (*freetype_draw_char)(gfx_context_t * ctx, int x, int y, uint32_t fg, uint32_t codepoint) = 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;
#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();
static void reinit();
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[];
};
#define MAX_SCROLLBACK 10240
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 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_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_inverted);
}
static void redraw_new_selection(int old_x, int old_y) {
if (selection_end_y == selection_start_y && old_y != selection_start_y) {
int a, b;
a = selection_end_x;
b = selection_end_y;
selection_end_x = old_x;
selection_end_y = old_y;
iterate_selection(cell_redraw);
selection_end_x = a;
selection_end_y = b;
iterate_selection(cell_redraw_inverted);
} else {
int a, b;
a = selection_start_x;
b = selection_start_y;
selection_start_x = old_x;
selection_start_y = old_y;
/* Figure out direction */
if (old_y < b) {
/* Backwards */
if (selection_end_y < old_y || (selection_end_y == old_y && selection_end_x < old_x)) {
/* Selection extended */
iterate_selection(cell_redraw_inverted);
} else {
/* Selection got smaller */
iterate_selection(cell_redraw);
}
} else if (old_y == b) {
/* Was a single line */
if (selection_end_y == b) {
/* And still is */
if (old_x < a) {
/* Backwards */
if (selection_end_x < old_x) {
iterate_selection(cell_redraw_inverted);
} else {
iterate_selection(cell_redraw);
}
} else {
if (selection_end_x < old_x) {
iterate_selection(cell_redraw);
} else {
iterate_selection(cell_redraw_inverted);
}
}
} else if (selection_end_y < b) {
/* Moved up */
if (old_x <= a) {
/* Should be fine with just append */
iterate_selection(cell_redraw_inverted);
} else {
/* Need to erase first */
iterate_selection(cell_redraw);
selection_start_x = a;
selection_start_y = b;
iterate_selection(cell_redraw_inverted);
}
} else if (selection_end_y > b) {
if (old_x >= a) {
/* Should be fine with just append */
iterate_selection(cell_redraw_inverted);
} else {
/* Need to erase first */
iterate_selection(cell_redraw);
selection_start_x = a;
selection_start_y = b;
iterate_selection(cell_redraw_inverted);
}
}
} else {
/* Forward */
if (selection_end_y < old_y || (selection_end_y == old_y && selection_end_x < old_x)) {
/* Selection got smaller */
iterate_selection(cell_redraw);
} else {
/* Selection extended */
iterate_selection(cell_redraw_inverted);
}
}
cell_redraw_inverted(a,b);
cell_redraw_inverted(selection_end_x, selection_end_y);
/* Restore */
selection_start_x = a;
selection_start_y = b;
}
}
/* Figure out how long the UTF-8 selection string should be. */
static void count_selection(uint16_t x, uint16_t y) {
term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
if (!(cell->flags & ANSI_EXT_IMG)) {
if (((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) {
term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
if (!(cell->flags & ANSI_EXT_IMG)) {
if (((uint32_t *)cell)[0] != 0x00000000) {
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);
fprintf(stderr, "Selection length is %d\n", _selection_count);
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;
}
/* 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 s = strlen(str) + 1;
write(fd_master, str, s);
}
/* 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 ) {
if (_fullscreen) {
/* In full screen mode, pre-blend the color over black. */
color = alpha_blend_rgba(premultiply(rgba(0,0,0,0xFF)), color);
}
if (!_no_frame) {
GFX(ctx, (x+decor_left_width),(y+decor_top_height+menu_bar_height)) = color;
} else {
GFX(ctx, x,y) = color;
}
}
/* Draw a partial block character. */
static void draw_semi_block(int c, int x, int y, uint32_t fg, uint32_t bg) {
int height;
bg = premultiply(bg);
fg = premultiply(fg);
if (c == 0x2580) {
uint32_t t = bg;
bg = fg;
fg = t;
c = 0x2584;
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);
}
}
}
c -= 0x2580;
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);
}
}
}
/* Convert unicode codepoint to fallback codepage codepoint */
static uint32_t ununicode(uint32_t c) {
switch (c) {
case L'☺': return 1;
case L'☻': return 2;
case L'♥': return 3;
case L'♦': return 4;
case L'♣': return 5;
case L'♠': return 6;
case L'•': return 7;
case L'◘': return 8;
case L'○': return 9;
case L'◙': return 10;
case L'♂': return 11;
case L'♀': return 12;
case L'♪': return 13;
case L'♫': return 14;
case L'☼': return 15;
case L'►': return 16;
case L'◄': return 17;
case L'↕': return 18;
case L'‼': return 19;
case L'¶': return 20;
case L'§': return 21;
case L'▬': return 22;
case L'↨': return 23;
case L'↑': return 24;
case L'↓': return 25;
case L'→': return 26;
case L'←': return 27;
case L'∟': return 28;
case L'↔': return 29;
case L'▲': return 30;
case L'▼': return 31;
/* ASCII text */
case L'⌂': return 127;
case L'Ç': return 128;
case L'ü': return 129;
case L'é': return 130;
case L'â': return 131;
case L'ä': return 132;
case L'à': return 133;
case L'å': return 134;
case L'ç': return 135;
case L'ê': return 136;
case L'ë': return 137;
case L'è': return 138;
case L'ï': return 139;
case L'î': return 140;
case L'ì': return 141;
case L'Ä': return 142;
case L'Å': return 143;
case L'É': return 144;
case L'æ': return 145;
case L'Æ': return 146;
case L'ô': return 147;
case L'ö': return 148;
case L'ò': return 149;
case L'û': return 150;
case L'ù': return 151;
case L'ÿ': return 152;
case L'Ö': return 153;
case L'Ü': return 154;
case L'¢': return 155;
case L'£': return 156;
case L'¥': return 157;
case L'₧': return 158;
case L'ƒ': return 159;
case L'á': return 160;
case L'í': return 161;
case L'ó': return 162;
case L'ú': return 163;
case L'ñ': return 164;
case L'Ñ': return 165;
case L'ª': return 166;
case L'º': return 167;
case L'¿': return 168;
case L'⌐': return 169;
case L'¬': return 170;
case L'½': return 171;
case L'¼': return 172;
case L'¡': return 173;
case L'«': return 174;
case L'»': return 175;
case L'░': return 176;
case L'▒': return 177;
case L'▓': return 178;
case L'│': return 179;
case L'┤': return 180;
case L'╡': return 181;
case L'╢': return 182;
case L'╖': return 183;
case L'╕': return 184;
case L'╣': return 185;
case L'║': return 186;
case L'╗': return 187;
case L'╝': return 188;
case L'╜': return 189;
case L'╛': return 190;
case L'┐': return 191;
case L'└': return 192;
case L'┴': return 193;
case L'┬': return 194;
case L'├': return 195;
case L'─': return 196;
case L'┼': return 197;
case L'╞': return 198;
case L'╟': return 199;
case L'╚': return 200;
case L'╔': return 201;
case L'╩': return 202;
case L'╦': return 203;
case L'╠': return 204;
case L'═': return 205;
case L'╬': return 206;
case L'╧': return 207;
case L'╨': return 208;
case L'╤': return 209;
case L'╥': return 210;
case L'╙': return 211;
case L'╘': return 212;
case L'╒': return 213;
case L'╓': return 214;
case L'╫': return 215;
case L'╪': return 216;
case L'┘': return 217;
case L'┌': return 218;
case L'█': return 219;
case L'▄': return 220;
case L'▌': return 221;
case L'▐': return 222;
case L'▀': return 223;
case L'α': return 224;
case L'ß': return 225;
case L'Γ': return 226;
case L'π': return 227;
case L'Σ': return 228;
case L'σ': return 229;
case L'µ': return 230;
case L'τ': return 231;
case L'Φ': return 232;
case L'Θ': return 233;
case L'Ω': return 234;
case L'δ': return 235;
case L'∞': return 236;
case L'φ': return 237;
case L'ε': return 238;
case L'∩': return 239;
case L'≡': return 240;
case L'±': return 241;
case L'≥': return 242;
case L'≤': return 243;
case L'⌠': return 244;
case L'⌡': return 245;
case L'÷': return 246;
case L'≈': return 247;
case L'°': return 248;
case L'∙': return 249;
case L'·': return 250;
case L'√': return 251;
case L'ⁿ': return 252;
case L'²': return 253;
case L'■': return 254;
}
return 4;
}
/* 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;
}
/* Draw block characters */
if (val >= 0x2580 && val <= 0x2588) {
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,premultiply(_bg));
}
}
draw_semi_block(val, x, y, _fg, _bg);
goto _extra_stuff;
}
/* Draw glyphs */
if (_use_aa && !_have_freetype) {
/* Convert other unicode characters. */
if (val > 128) {
val = ununicode(val);
}
/* Draw using the Toaru SDF rendering library */
char tmp[2] = {val,0};
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 (val != 0 && val != ' ' && _fg != _bg) {
int _font = SDF_FONT_MONO;
if (flags & ANSI_BOLD && flags & ANSI_ITALIC) {
_font = SDF_FONT_MONO_BOLD_OBLIQUE;
} else if (flags & ANSI_BOLD) {
_font = SDF_FONT_MONO_BOLD;
} else if (flags & ANSI_ITALIC) {
_font = SDF_FONT_MONO_OBLIQUE;
}
if (_no_frame) {
draw_sdf_string_gamma(ctx, x-1, y, tmp, font_size, _fg, _font, font_gamma);
} else {
draw_sdf_string_gamma(ctx, x+decor_left_width-1, y+decor_top_height+menu_bar_height, tmp, font_size, _fg, _font, font_gamma);
}
}
} else if (_use_aa && _have_freetype) {
/* Draw using freetype extension */
if (val == 0xFFFF) { return; } /* Unicode, do not redraw here */
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;
}
#define FONT_MONOSPACE 4
#define FONT_MONOSPACE_BOLD 5
#define FONT_MONOSPACE_ITALIC 6
#define FONT_MONOSPACE_BOLD_ITALIC 7
int _font = FONT_MONOSPACE;
if (flags & ANSI_BOLD && flags & ANSI_ITALIC) {
_font = FONT_MONOSPACE_BOLD_ITALIC;
} else if (flags & ANSI_ITALIC) {
_font = FONT_MONOSPACE_ITALIC;
} else if (flags & ANSI_BOLD) {
_font = FONT_MONOSPACE_BOLD;
}
freetype_set_font_face(_font);
freetype_set_font_size(font_size);
if (_no_frame) {
freetype_draw_char(ctx, x, y + char_offset, _fg, val);
} else {
freetype_draw_char(ctx, x + decor_left_width, y + char_offset + decor_top_height + menu_bar_height, _fg, val);
}
} else {
/* Convert other unicode characters. */
if (val > 128) {
val = ununicode(val);
}
/* Draw using the bitmap font. */
uint16_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 << (15-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 */
if (!_no_frame) {
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);
}
} else {
l_x = min(l_x, x);
l_y = min(l_y, y);
if (flags & ANSI_WIDE) {
r_x = max(r_x, x + char_width * 2);
r_y = max(r_y, y + char_height * 2);
} else {
r_x = max(r_x, x + char_width);
r_y = max(r_y, y + char_height);
}
}
}
/* 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_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
/* 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) {
/* Avoid setting cells out of range. */
if (x >= term_width || y >= term_height) return;
/* Draw the image data */
uint32_t * data = (uint32_t *)cell->fg;
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 */
if (!_no_frame) {
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);
} else {
l_x = min(l_x, x * char_width);
l_y = min(l_y, y * char_height);
r_x = max(r_x, x * char_width + char_width);
r_y = max(r_y, y * char_height + char_height);
}
}
/* Redraw a text cell normally. */
static void cell_redraw(uint16_t x, uint16_t y) {
/* Avoid 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_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
/* If it's an image cell, redraw the image data. */
if (cell->flags & ANSI_EXT_IMG) {
redraw_cell_image(x,y,cell);
return;
}
/* Special case empty cells. */
if (((uint32_t *)cell)[0] == 0x00000000) {
term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
} else {
term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
}
}
/* Redraw text cell inverted. */
static void cell_redraw_inverted(uint16_t x, uint16_t y) {
/* Avoid 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_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
/* If it's an image cell, redraw the image data. */
if (cell->flags & ANSI_EXT_IMG) {
redraw_cell_image(x,y,cell);
return;
}
/* Special case empty cells. */
if (((uint32_t *)cell)[0] == 0x00000000) {
term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_BG, TERM_DEFAULT_FG, TERM_DEFAULT_FLAGS | ANSI_SPECBG);
} else {
term_write_char(cell->c, x * char_width, y * char_height, cell->bg, cell->fg, cell->flags | ANSI_SPECBG);
}
}
/* Redraw text cell with a surrounding box (used by cursor) */
static void cell_redraw_box(uint16_t x, uint16_t y) {
/* Avoid 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_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
/* If it's an image cell, redraw the image data. */
if (cell->flags & ANSI_EXT_IMG) {
redraw_cell_image(x,y,cell);
return;
}
/* Special case empty cells. */
if (((uint32_t *)cell)[0] == 0x00000000) {
term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS | ANSI_BORDER);
} else {
term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags | ANSI_BORDER);
}
}
/* Draw the cursor cell */
static void render_cursor() {
if (!window->focused) {
/* An unfocused terminal should draw an unfilled box. */
cell_redraw_box(csr_x, csr_y);
} else {
/* A focused terminal draws a solid box. */
cell_redraw_inverted(csr_x, csr_y);
}
}