-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtermptyesc.c
5216 lines (4833 loc) · 139 KB
/
termptyesc.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
#include "private.h"
#include <Elementary.h>
#include <stdint.h>
#include <assert.h>
#include "colors.h"
#include "termio.h"
#include "termpty.h"
#include "termptydbl.h"
#include "termptyesc.h"
#include "termptyops.h"
#include "termptyext.h"
#include "theme.h"
#if defined(BINARY_TYTEST)
#include "tytest.h"
#endif
#include "utils.h"
#undef CRITICAL
#undef ERR
#undef WRN
#undef INF
#undef DBG
#define CRITICAL(...) EINA_LOG_DOM_CRIT(_termpty_log_dom, __VA_ARGS__)
#define ERR(...) EINA_LOG_DOM_ERR(_termpty_log_dom, __VA_ARGS__)
#define WRN(...) EINA_LOG_DOM_WARN(_termpty_log_dom, __VA_ARGS__)
#define INF(...) EINA_LOG_DOM_INFO(_termpty_log_dom, __VA_ARGS__)
#define DBG(...) EINA_LOG_DOM_DBG(_termpty_log_dom, __VA_ARGS__)
#define ST 0x9c // String Terminator
#define BEL 0x07 // Bell
#define ESC 033 // Escape
#define CSI 0x9b
#define OSC 0x9d
#define DEL 0x7f
/* XXX: all handle_ functions return the number of bytes successfully read, 0
* if not enough bytes could be read
*/
static const char *const ASCII_CHARS_TABLE[] =
{
"NUL", // '\0'
"SOH", // '\001'
"STX", // '\002'
"ETX", // '\003'
"EOT", // '\004'
"ENQ", // '\005'
"ACK", // '\006'
"BEL", // '\007'
"BS", // '\010'
"HT", // '\011'
"LF", // '\012'
"VT", // '\013'
"FF", // '\014'
"CR" , // '\015'
"SO", // '\016'
"SI", // '\017'
"DLE", // '\020'
"DC1", // '\021'
"DC2", // '\022'
"DC3", // '\023'
"DC4", // '\024'
"NAK", // '\025'
"SYN", // '\026'
"ETB", // '\027'
"CAN", // '\030'
"EM", // '\031'
"SUB", // '\032'
"ESC", // '\033'
"FS", // '\034'
"GS", // '\035'
"RS", // '\036'
"US" // '\037'
};
const char * EINA_PURE
termptyesc_safechar(const unsigned int c)
{
static char _str[9];
// This should avoid 'BEL' and 'ESC' in particular, which would
// have side effects in the parent terminal (esp. ESC).
if (c < (sizeof(ASCII_CHARS_TABLE) / sizeof(ASCII_CHARS_TABLE[0])))
return ASCII_CHARS_TABLE[c];
if (c == DEL)
return "DEL";
// The rest should be safe (?)
snprintf(_str, 9, "%c", c);
_str[8] = '\0';
return _str;
}
static Eina_Bool
_cursor_is_within_margins(const Termpty *ty)
{
return !(
((ty->termstate.top_margin > 0)
&& (ty->cursor_state.cy < ty->termstate.top_margin))
|| ((ty->termstate.bottom_margin > 0)
&& (ty->cursor_state.cy >= ty->termstate.bottom_margin))
|| ((ty->termstate.left_margin > 0)
&& (ty->cursor_state.cx < ty->termstate.left_margin))
|| ((ty->termstate.right_margin > 0)
&& (ty->cursor_state.cx >= ty->termstate.right_margin))
);
}
enum esc_arg_error {
ESC_ARG_NO_VALUE = 1,
ESC_ARG_ERROR = 2
};
static int
_csi_arg_get(Termpty *ty, Eina_Unicode **ptr)
{
Eina_Unicode *b = *ptr;
int sum = 0;
if ((b == NULL) || (*b == '\0'))
{
*ptr = NULL;
return -ESC_ARG_NO_VALUE;
}
/* Skip potential '?', '>'.... */
while ((*b) && ( (*b) != ';' && ((*b) < '0' || (*b) > '9')))
b++;
if (*b == ';')
{
b++;
*ptr = b;
return -ESC_ARG_NO_VALUE;
}
if (*b == '\0')
{
*ptr = NULL;
return -ESC_ARG_NO_VALUE;
}
while ((*b >= '0') && (*b <= '9'))
{
if (sum > INT32_MAX/10 )
{
ERR("Invalid sequence: argument is too large");
ty->decoding_error = EINA_TRUE;
goto error;
}
sum *= 10;
sum += *b - '0';
b++;
}
if ((*b == ';') || (*b == ':'))
{
if (b[1])
b++;
*ptr = b;
}
else if (*b == '\0')
{
*ptr = NULL;
}
else
{
*ptr = b;
}
return sum;
error:
ERR("Invalid CSI argument");
ty->decoding_error = EINA_TRUE;
*ptr = NULL;
return -ESC_ARG_ERROR;
}
static void
_tab_forward(Termpty *ty, int n)
{
int cx = ty->cursor_state.cx;
for (; n > 0; n--)
{
do
{
cx++;
}
while ((cx < ty->w) && (!TAB_TEST(ty, cx)));
}
ty->cursor_state.cx = cx;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
}
static void
_cursor_to_start_of_line(Termpty *ty)
{
ty->cursor_state.cx = ty->termstate.left_margin;
}
static void
_handle_cursor_control(Termpty *ty, const Eina_Unicode *cc)
{
Termcell *cell;
switch (*cc)
{
case 0x07: // BEL '\a' (bell)
DBG("->BEL");
if (ty->cb.bell.func) ty->cb.bell.func(ty->cb.bell.data);
return;
case 0x08: // BS '\b' (backspace)
DBG("->BS");
ty->cursor_state.wrapnext = 0;
ty->cursor_state.cx--;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
return;
case 0x09: // HT '\t' (horizontal tab)
DBG("->HT");
cell = &(TERMPTY_SCREEN(ty, ty->cursor_state.cx, ty->cursor_state.cy));
cell->att.tab_inserted = 1;
_tab_forward(ty, 1);
cell = &(TERMPTY_SCREEN(ty, ty->cursor_state.cx -1, ty->cursor_state.cy));
cell->att.tab_last = 1;
return;
case 0x0a: // LF '\n' (new line)
case 0x0b: // VT '\v' (vertical tab)
case 0x0c: // FF '\f' (form feed)
DBG("->LF");
ty->cursor_state.wrapnext = 0;
if (ty->termstate.crlf)
_cursor_to_start_of_line(ty);
ty->cursor_state.cy++;
termpty_text_scroll_test(ty, EINA_TRUE);
return;
case 0x0d: // CR '\r' (carriage ret)
DBG("->CR");
if (ty->cursor_state.cx != 0)
{
ty->termstate.had_cr_x = ty->cursor_state.cx;
ty->termstate.had_cr_y = ty->cursor_state.cy;
ty->cursor_state.wrapnext = 0;
}
_cursor_to_start_of_line(ty);
return;
default:
return;
}
}
static void
_switch_to_alternative_screen(Termpty *ty, int mode)
{
// swap screen content now
if (mode != ty->altbuf)
termpty_screen_swap(ty);
}
static void
_move_cursor_to_origin(Termpty *ty)
{
if (ty->termstate.restrict_cursor)
{
ty->cursor_state.cx = ty->termstate.left_margin;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
ty->cursor_state.cy = ty->termstate.top_margin;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
}
else
{
ty->cursor_state.cx = 0;
ty->cursor_state.cy = 0;
}
}
static void
_handle_esc_csi_reset_mode(Termpty *ty, Eina_Unicode cc, Eina_Unicode *b,
const Eina_Unicode * const end)
{
Eina_Bool mode = EINA_FALSE;
Eina_Bool priv = EINA_FALSE;
int arg;
if (cc == 'h')
mode = EINA_TRUE;
if (*b == '?')
{
priv = EINA_TRUE;
b++;
}
if (priv) /* DEC Private Mode Reset (DECRST) */
{
while (b && b <= end)
{
arg = _csi_arg_get(ty, &b);
// complete-ish list here:
// http://ttssh2.sourceforge.jp/manual/en/about/ctrlseq.html
switch (arg)
{
case -ESC_ARG_ERROR:
return;
/* TODO: -ESC_ARG_NO_VALUE */
case 1:
ty->termstate.appcursor = mode;
break;
case 2:
DBG("DECANM - ANSI MODE - VT52");
ty->termstate.kbd_lock = mode;
break;
case 3: // 132 column mode… should we handle this?
#if defined(SUPPORT_80_132_COLUMNS)
if (ty->termstate.att.is_80_132_mode_allowed)
{
/* ONLY FOR TESTING PURPOSE FTM */
Evas_Object *wn;
int w, h;
wn = termio_win_get(ty->obj);
elm_win_size_step_get(wn, &w, &h);
evas_object_resize(wn,
4 +
(mode ? 132 : 80) * w,
4 + ty->h * h);
termpty_resize(ty, mode ? 132 : 80,
ty->h);
termpty_reset_state(ty);
termpty_clear_screen(ty,
TERMPTY_CLR_ALL);
}
#endif
break;
case 4:
DBG("scrolling mode (DECSCLM): %i (always fast mode)", mode);
break;
case 5:
ty->termstate.reverse = mode;
break;
case 6:
/* DECOM */
if (mode)
{
/* set, within margins */
ty->termstate.restrict_cursor = 1;
}
else
{
ty->termstate.restrict_cursor = 0;
}
_move_cursor_to_origin(ty);
DBG("DECOM: mode (%d): cursor is at 0,0"
" cursor limited to screen/start point"
" for line #'s depends on top margin",
mode);
break;
case 7:
DBG("DECAWM: set/reset wrap mode to %i", mode);
ty->termstate.wrap = mode;
break;
case 8:
ty->termstate.no_autorepeat = !mode;
DBG("DECARM - auto repeat %i", mode);
break;
case 9:
DBG("set mouse (X10) %i", mode);
if (mode) ty->mouse_mode = MOUSE_X10;
else ty->mouse_mode = MOUSE_OFF;
break;
case 12: // ignore
WRN("TODO: set blinking cursor to (stop?) %i or local echo (ignored)", mode);
break;
case 19: // never seen this - what to do?
WRN("TODO: set print extent to full screen");
ty->decoding_error = EINA_TRUE;
break;
case 20: // crfl==1 -> cur moves to col 0 on LF, FF or VT, ==0 -> mode is cr+lf
ty->termstate.crlf = mode;
break;
case 25:
ty->termstate.hide_cursor = !mode;
DBG("hide cursor: %d", !mode);
break;
case 30: // ignore
WRN("TODO: set scrollbar mapping %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 33: // ignore
WRN("TODO: Stop cursor blink %i", mode);
break;
case 34: // ignore
WRN("TODO: Underline cursor mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 35: // ignore
WRN("TODO: set shift keys %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 38: // ignore
WRN("TODO: switch to tek window %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 40:
DBG("Allow 80 -> 132 Mode %i", mode);
#if defined(SUPPORT_80_132_COLUMNS)
ty->termstate.att.is_80_132_mode_allowed = mode;
#endif
break;
case 45: // ignore
WRN("TODO: Reverse-wraparound Mode");
ty->decoding_error = EINA_TRUE;
break;
case 47:
_switch_to_alternative_screen(ty, mode);
break;
case 59: // ignore
WRN("TODO: kanji terminal mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 66:
WRN("TODO: app keypad mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 67:
ty->termstate.send_bs = mode;
DBG("backspace send bs not del = %i", mode);
break;
case 69:
ty->termstate.lr_margins = mode;
if (!mode)
{
ty->termstate.left_margin = 0;
ty->termstate.right_margin = 0;
}
break;
case 98:
DBG("DECARSM Set/Reset Auto Resize Mode: %d", mode);
break;
case 100:
DBG("DECAAM Set/Reset Auto Answerback Mode: %d", mode);
break;
case 101:
DBG("DECCANSM Set/Reset Conceal Answerback Message Mode: %d", mode);
break;
case 109:
DBG("DECCAPSLK Set/Reset Caps Lock Mode: %d", mode);
break;
case 1000:
if (mode) ty->mouse_mode = MOUSE_NORMAL;
else ty->mouse_mode = MOUSE_OFF;
DBG("set mouse (press+release only) to %i", mode);
break;
case 1001:
WRN("TODO: x11 mouse highlighting %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1002:
if (mode) ty->mouse_mode = MOUSE_NORMAL_BTN_MOVE;
else ty->mouse_mode = MOUSE_OFF;
DBG("set mouse (press+release+motion while pressed) %i", mode);
break;
case 1003:
if (mode) ty->mouse_mode = MOUSE_NORMAL_ALL_MOVE;
else ty->mouse_mode = MOUSE_OFF;
DBG("set mouse (press+release+all motion) %i", mode);
break;
case 1004:
DBG("%s focus reporting", mode ? "enable" : "disable");
if (mode)
{
ty->focus_reporting = EINA_TRUE;
termpty_focus_report(ty, termio_is_focused(ty->obj));
}
else
{
ty->focus_reporting = EINA_FALSE;
}
break;
case 1005:
if (mode) ty->mouse_ext = MOUSE_EXT_UTF8;
else ty->mouse_ext = MOUSE_EXT_NONE;
DBG("set mouse (xterm utf8 style) %i", mode);
break;
case 1006:
if (mode) ty->mouse_ext = MOUSE_EXT_SGR;
else ty->mouse_ext = MOUSE_EXT_NONE;
DBG("set mouse (xterm sgr style) %i", mode);
break;
case 1010: // ignore
WRN("TODO: set home on tty output %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1012: // ignore
WRN("TODO: set home on tty input %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1015:
if (mode) ty->mouse_ext = MOUSE_EXT_URXVT;
else ty->mouse_ext = MOUSE_EXT_NONE;
DBG("set mouse (rxvt-unicode style) %i", mode);
break;
case 1034: // ignore
/* libreadline6 emits it but it shouldn't.
See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=577012
*/
WRN("Ignored screen mode %i", arg);
break;
case 1047:
if (!mode && ty->altbuf)
/* clear screen before switching back to normal */
termpty_clear_screen(ty, TERMPTY_CLR_ALL);
_switch_to_alternative_screen(ty, mode);
break;
case 1048:
termpty_cursor_copy(ty, mode);
break;
case 1049:
if (mode)
{
// switch to altbuf
termpty_cursor_copy(ty, mode);
_switch_to_alternative_screen(ty, mode);
if (ty->altbuf)
/* clear screen before switching back to normal */
termpty_clear_screen(ty, TERMPTY_CLR_ALL);
}
else
{
if (ty->altbuf)
/* clear screen before switching back to normal */
termpty_clear_screen(ty, TERMPTY_CLR_ALL);
_switch_to_alternative_screen(ty, mode);
termpty_cursor_copy(ty, mode);
}
break;
case 2004:
ty->bracketed_paste = mode;
break;
case 7700: // ignore
WRN("TODO: ambigous width reporting %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 7727:
DBG("%s application escape mode", mode ? "enable" : "disable");
ty->termstate.esc_keycode = !!mode;
break;
case 7728:
DBG("%s alternate escape", mode ? "enable" : "disable");
ty->termstate.alternate_esc = !!mode;
break;
case 7766: // ignore
WRN("TODO: %s scrollbar", mode ? "hide" : "show");
ty->decoding_error = EINA_TRUE;
break;
case 7783: // ignore
WRN("TODO: shortcut override mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 7786: // ignore
EINA_FALLTHROUGH;
case 7787: // ignore
WRN("TODO: enable mouse wheel -> cursor key xlation %i", mode);
ty->decoding_error = EINA_TRUE;
break;
default:
WRN("Unhandled DEC Private Reset Mode arg %i", arg);
ty->decoding_error = EINA_TRUE;
break;
}
}
}
else /* Reset Mode (RM) */
{
while (b && b <= end)
{
arg = _csi_arg_get(ty, &b);
switch (arg)
{
case -ESC_ARG_ERROR:
return;
/* TODO: -ESC_ARG_NO_VALUE */
case 1:
ty->termstate.appcursor = mode;
break;
case 3:
WRN("CRM - Show Control Character Mode: %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 4:
DBG("set insert mode to %i", mode);
ty->termstate.insert = mode;
break;
case 34:
WRN("TODO: hebrew keyboard mapping: %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 36:
WRN("TODO: hebrew encoding mode: %i", mode);
ty->decoding_error = EINA_TRUE;
break;
default:
WRN("Unhandled ANSI Reset Mode arg %i", arg);
ty->decoding_error = EINA_TRUE;
}
}
}
}
static int
_csi_truecolor_arg_get(Termpty *ty, Eina_Unicode **ptr)
{
Eina_Unicode *b = *ptr;
int sum = 0;
char separator;
if ((b == NULL) || (*b == '\0'))
{
*ptr = NULL;
return -ESC_ARG_NO_VALUE;
}
/* by construction, shall be the same separator as the following ones */
separator = *(b-1);
if ((separator != ';') && (separator != ':'))
{
*ptr = NULL;
return -ESC_ARG_NO_VALUE;
}
if (*b == separator)
{
b++;
*ptr = b;
return -ESC_ARG_NO_VALUE;
}
if (*b == '\0')
{
*ptr = NULL;
return -ESC_ARG_NO_VALUE;
}
/* invalid values */
if ((*b < '0') || (*b > '9'))
{
*ptr = NULL;
return -ESC_ARG_ERROR;
}
while ((*b >= '0') && (*b <= '9'))
{
if (sum > INT32_MAX/10 )
{
*ptr = NULL;
ERR("Invalid sequence: argument is too large");
ty->decoding_error = EINA_TRUE;
return -ESC_ARG_ERROR;
}
sum *= 10;
sum += *b - '0';
b++;
}
if (*b == separator)
{
b++;
*ptr = b;
}
else if (*b == '\0')
{
*ptr = NULL;
}
else
{
*ptr = b;
}
return sum;
}
#if !defined(BINARY_TYFUZZ)
/*********************************
* cache true color approximations
*********************************
* Approximating true colors is costly since it needs to compare 256 colors.
* The following considers that a few colors are used a lot. For example, one
* can consider that a text editor with syntax highlighting would only use a
* small palette.
* Given that, using the cache needs to be efficient: it needs to speed up the
* approximation when the color is already in the cache but not knowing the
* color requested is not in the cache needs to be efficient too.
*
* The cache is an array of @TCC_LEN uint32_t.
* Each entry has on the MSB the color as 3 uint8_t (R,G,B) and the LSB is
* the approximated color.
* Searching a color is simply going through the array and testing the mask on
* the 3 most significant bytes.
* When a color is found, it is bubbled up towards the start of the array by
* swapping this entry with the one above. This way the array is ordered to
* get most used colors as fast as possible.
* When a color is not found, the slow process of comparing it with the 256
* colors is used. Then the result is inserted in the lower half of the
* array. This ensures that new entries can live a bit and not be removed by
* the next new color. Using a modulo with a prime number makes for a
* nice-enough random generator to figure out where to insert.
*/
#define TCC_LEN 32
#define TCC_PRIME 17 /* smallest prime number larger than @TCC_LEN/2 */
static struct {
uint32_t colors[TCC_LEN];
} _truecolor_cache;
static int _tcc_random_pos = 0;
static Eina_Bool
_tcc_find(const uint32_t color_msb, uint8_t *chosen_color)
{
int i;
for (i = 0; i < TCC_LEN; i++)
{
if ((_truecolor_cache.colors[i] & 0xffffff00) == color_msb)
{
*chosen_color = _truecolor_cache.colors[i] & 0xff;
/* bubble up this result */
if (i > 0)
{
uint32_t prev = _truecolor_cache.colors[i - 1];
_truecolor_cache.colors[i - 1] = _truecolor_cache.colors[i];
_truecolor_cache.colors[i] = prev;
}
return EINA_TRUE;
}
}
return EINA_FALSE;
}
static void
_tcc_insert(const uint32_t color_msb, const uint8_t approximated)
{
uint32_t c = color_msb | approximated;
int i;
_tcc_random_pos = ((_tcc_random_pos + TCC_PRIME) % (TCC_LEN / 2));
i = (TCC_LEN / 2) + _tcc_random_pos;
_truecolor_cache.colors[i] = c;
}
#endif
static uint8_t
_approximate_truecolor_rgb(Termpty *ty, uint8_t r0, uint8_t g0, uint8_t b0)
{
uint8_t chosen_color = COL_DEF;
#if defined(BINARY_TYFUZZ)
(void) ty;
(void) r0;
(void) g0;
(void) b0;
#else
int c;
int distance_min = INT_MAX;
Evas_Object *textgrid;
const uint32_t color_msb = 0
| (((uint32_t)r0) << 24)
| (((uint32_t)g0) << 16)
| (((uint32_t)b0) << 8);
if (_tcc_find(color_msb, &chosen_color))
return chosen_color;
textgrid = termio_textgrid_get(ty->obj);
for (c = 0; c < 256; c++)
{
int r1 = 0, g1 = 0, b1 = 0, a1 = 0;
int delta_red_sq, delta_green_sq, delta_blue_sq, red_mean;
int distance;
evas_object_textgrid_palette_get(textgrid,
EVAS_TEXTGRID_PALETTE_EXTENDED,
c, &r1, &g1, &b1, &a1);
/* Compute the color distance
* XXX: this is inacurate but should give good enough results.
* See https://en.wikipedia.org/wiki/Color_difference
*/
red_mean = (r0 + r1) / 2;
delta_red_sq = (r0 - r1) * (r0 - r1);
delta_green_sq = (g0 - g1) * (g0 - g1);
delta_blue_sq = (b0 - b1) * (b0 - b1);
#if 1
distance = 2 * delta_red_sq
+ 4 * delta_green_sq
+ 3 * delta_blue_sq
+ ((red_mean) * (delta_red_sq - delta_blue_sq) / 256);
#else
/* from https://www.compuphase.com/cmetric.htm */
distance = (((512 + red_mean) * delta_red_sq) >> 8)
+ 4 * delta_green_sq
+ (((767 - red_mean) * delta_blue_sq) >> 8);
/* euclidian distance */
distance = delta_red_sq + delta_green_sq + delta_blue_sq;
(void)red_mean;
#endif
if (distance < distance_min)
{
distance_min = distance;
chosen_color = c;
}
}
_tcc_insert(color_msb, chosen_color);
#endif
return chosen_color;
}
static uint8_t
_handle_esc_csi_truecolor_rgb(Termpty *ty, Eina_Unicode **ptr)
{
int r, g, b;
Eina_Unicode *u = *ptr;
char separator;
if ((u == NULL) || (*u == '\0'))
{
return COL_DEF;
}
separator = *(u-1);
r = _csi_truecolor_arg_get(ty, ptr);
g = _csi_truecolor_arg_get(ty, ptr);
b = _csi_truecolor_arg_get(ty, ptr);
if ((r == -ESC_ARG_ERROR) ||
(g == -ESC_ARG_ERROR) ||
(b == -ESC_ARG_ERROR))
return COL_DEF;
if (separator == ':' && *ptr)
{
if (**ptr != ';')
{
/* then the first parameter was a color-space-id (ignored) */
r = g;
g = b;
b = _csi_truecolor_arg_get(ty, ptr);
/* Skip other parameters */
while ((*ptr) && (**ptr != ';'))
{
int arg = _csi_truecolor_arg_get(ty, ptr);
if (arg == -ESC_ARG_ERROR)
break;
}
}
if ((*ptr) && (**ptr == ';'))
{
*ptr = (*ptr) + 1;
}
}
if (r == -ESC_ARG_NO_VALUE)
r = 0;
if (g == -ESC_ARG_NO_VALUE)
g = 0;
if (b == -ESC_ARG_NO_VALUE)
b = 0;
return _approximate_truecolor_rgb(ty, (uint8_t)r, (uint8_t)g, (uint8_t)b);
}
static uint8_t
_handle_esc_csi_truecolor_cmy(Termpty *ty, Eina_Unicode **ptr)
{
int r, g, b, c, m, y;
Eina_Unicode *u = *ptr;
char separator;
if ((u == NULL) || (*u == '\0'))
{
return COL_DEF;
}
separator = *(u-1);
/* Considering CMY stored as percents */
c = _csi_truecolor_arg_get(ty, ptr);
m = _csi_truecolor_arg_get(ty, ptr);
y = _csi_truecolor_arg_get(ty, ptr);
if ((c == -ESC_ARG_ERROR) ||
(m == -ESC_ARG_ERROR) ||
(y == -ESC_ARG_ERROR))
return COL_DEF;
if (separator == ':' && *ptr)
{
if (**ptr != ';')
{
/* then the first parameter was a color-space-id (ignored) */
c = m;
m = y;
y = _csi_truecolor_arg_get(ty, ptr);
/* Skip other parameters */
while ((*ptr) && (**ptr != ';'))
{
int arg = _csi_truecolor_arg_get(ty, ptr);
if (arg == -ESC_ARG_ERROR)
break;
}
}
if ((*ptr) && (**ptr == ';'))
{
*ptr = (*ptr) + 1;
}
}
if (c == -ESC_ARG_NO_VALUE)
c = 0;
if (m == -ESC_ARG_NO_VALUE)
m = 0;
if (y == -ESC_ARG_NO_VALUE)
y = 0;
r = 255 - ((c * 255) / 100);
g = 255 - ((m * 255) / 100);
b = 255 - ((y * 255) / 100);
return _approximate_truecolor_rgb(ty, (uint8_t)r, (uint8_t)g, (uint8_t)b);
}
static uint8_t
_handle_esc_csi_truecolor_cmyk(Termpty *ty, Eina_Unicode **ptr)
{
int r, g, b, c, m, y, k;
Eina_Unicode *u = *ptr;
char separator;
if ((u == NULL) || (*u == '\0'))
{
return COL_DEF;
}
separator = *(u-1);
/* Considering CMYK stored as percents */
c = _csi_truecolor_arg_get(ty, ptr);
m = _csi_truecolor_arg_get(ty, ptr);
y = _csi_truecolor_arg_get(ty, ptr);
k = _csi_truecolor_arg_get(ty, ptr);
if ((c == -ESC_ARG_ERROR) ||
(m == -ESC_ARG_ERROR) ||
(y == -ESC_ARG_ERROR) ||
(k == -ESC_ARG_ERROR))
return COL_DEF;
if (separator == ':' && *ptr)
{
if (**ptr != ';')
{
/* then the first parameter was a color-space-id (ignored) */
c = m;
m = y;
y = k;
k = _csi_truecolor_arg_get(ty, ptr);
/* Skip other parameters */
while ((*ptr) && (**ptr != ';'))
{
int arg = _csi_truecolor_arg_get(ty, ptr);
if (arg == -ESC_ARG_ERROR)
break;
}
}
if ((*ptr) && (**ptr == ';'))
{
*ptr = (*ptr) + 1;
}
}
if (c == -ESC_ARG_NO_VALUE)
c = 0;
if (m == -ESC_ARG_NO_VALUE)
m = 0;
if (y == -ESC_ARG_NO_VALUE)
y = 0;
if (k == -ESC_ARG_NO_VALUE)
k = 0;
r = (255 * (100 - c) * (100 - k)) / 100 / 100;
g = (255 * (100 - m) * (100 - k)) / 100 / 100;
b = (255 * (100 - y) * (100 - k)) / 100 / 100;
return _approximate_truecolor_rgb(ty, (uint8_t)r, (uint8_t)g, (uint8_t)b);
}
static void
_handle_esc_csi_color_set(Termpty *ty, Eina_Unicode **ptr,
const Eina_Unicode * const end)
{
Eina_Unicode *b = *ptr;
DBG("color set");
while (b && b <= end)
{