-
Notifications
You must be signed in to change notification settings - Fork 5
/
csi.c
2179 lines (1858 loc) · 71.4 KB
/
csi.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 "csi.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(_DEBUG)
#include <stdio.h>
#endif
#include <sys/timerfd.h>
#define LOG_MODULE "csi"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "char32.h"
#include "config.h"
#include "debug.h"
#include "grid.h"
#include "selection.h"
#include "sixel.h"
#include "util.h"
#include "version.h"
#include "vt.h"
#include "xmalloc.h"
#include "xsnprintf.h"
#define UNHANDLED() LOG_DBG("unhandled: %s", csi_as_string(term, final, -1))
#define UNHANDLED_SGR(idx) LOG_DBG("unhandled: %s", csi_as_string(term, 'm', idx))
static void
sgr_reset(struct terminal *term)
{
term->vt.attrs = (struct attributes){0};
term->vt.underline = (struct underline_range_data){0};
term->bits_affecting_ascii_printer.underline_style = false;
term->bits_affecting_ascii_printer.underline_color = false;
term_update_ascii_printer(term);
}
static const char *
csi_as_string(struct terminal *term, uint8_t final, int idx)
{
static char msg[1024];
int c = snprintf(msg, sizeof(msg), "CSI: ");
for (size_t i = idx >= 0 ? idx : 0;
i < (idx >= 0 ? idx + 1 : term->vt.params.idx);
i++)
{
c += snprintf(&msg[c], sizeof(msg) - c, "%u",
term->vt.params.v[i].value);
for (size_t j = 0; j < term->vt.params.v[i].sub.idx; j++) {
c += snprintf(&msg[c], sizeof(msg) - c, ":%u",
term->vt.params.v[i].sub.value[j]);
}
c += snprintf(&msg[c], sizeof(msg) - c, "%s",
i == term->vt.params.idx - 1 ? "" : ";");
}
for (size_t i = 0; i < sizeof(term->vt.private); i++) {
char value = (term->vt.private >> (i * 8)) & 0xff;
if (value == 0)
break;
c += snprintf(&msg[c], sizeof(msg) - c, "%c", value);
}
snprintf(&msg[c], sizeof(msg) - c, "%c (%u parameters)",
final, idx >= 0 ? 1 : term->vt.params.idx);
return msg;
}
static void
csi_sgr(struct terminal *term)
{
if (term->vt.params.idx == 0) {
sgr_reset(term);
return;
}
for (size_t i = 0; i < term->vt.params.idx; i++) {
const int param = term->vt.params.v[i].value;
switch (param) {
case 0:
sgr_reset(term);
break;
case 1: term->vt.attrs.bold = true; break;
case 2: term->vt.attrs.dim = true; break;
case 3: term->vt.attrs.italic = true; break;
case 4: {
term->vt.attrs.underline = true;
term->vt.underline.style = UNDERLINE_SINGLE;
if (unlikely(term->vt.params.v[i].sub.idx == 1)) {
enum underline_style style = term->vt.params.v[i].sub.value[0];
switch (style) {
default:
case UNDERLINE_NONE:
term->vt.attrs.underline = false;
term->vt.underline.style = UNDERLINE_NONE;
term->bits_affecting_ascii_printer.underline_style = false;
break;
case UNDERLINE_SINGLE:
case UNDERLINE_DOUBLE:
case UNDERLINE_CURLY:
case UNDERLINE_DOTTED:
case UNDERLINE_DASHED:
term->vt.underline.style = style;
term->bits_affecting_ascii_printer.underline_style =
style > UNDERLINE_SINGLE;
break;
}
term_update_ascii_printer(term);
}
break;
}
case 5: term->vt.attrs.blink = true; break;
case 6: LOG_WARN("ignored: rapid blink"); break;
case 7: term->vt.attrs.reverse = true; break;
case 8: term->vt.attrs.conceal = true; break;
case 9: term->vt.attrs.strikethrough = true; break;
case 21:
term->vt.attrs.underline = true;
term->vt.underline.style = UNDERLINE_DOUBLE;
term->bits_affecting_ascii_printer.underline_style = true;
term_update_ascii_printer(term);
break;
case 22: term->vt.attrs.bold = term->vt.attrs.dim = false; break;
case 23: term->vt.attrs.italic = false; break;
case 24: {
term->vt.attrs.underline = false;
term->vt.underline.style = UNDERLINE_NONE;
term->bits_affecting_ascii_printer.underline_style = false;
term_update_ascii_printer(term);
break;
}
case 25: term->vt.attrs.blink = false; break;
case 26: break; /* rapid blink, ignored */
case 27: term->vt.attrs.reverse = false; break;
case 28: term->vt.attrs.conceal = false; break;
case 29: term->vt.attrs.strikethrough = false; break;
/* Regular foreground colors */
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
term->vt.attrs.fg_src = COLOR_BASE16;
term->vt.attrs.fg = param - 30;
break;
case 38:
case 48:
case 58: {
uint32_t color;
enum color_source src;
/* Indexed: 38;5;<idx> */
if (term->vt.params.idx - i - 1 >= 2 &&
term->vt.params.v[i + 1].value == 5)
{
src = COLOR_BASE256;
color = min(term->vt.params.v[i + 2].value,
ALEN(term->colors.table) - 1);
i += 2;
}
/* RGB: 38;2;<r>;<g>;<b> */
else if (term->vt.params.idx - i - 1 >= 4 &&
term->vt.params.v[i + 1].value == 2)
{
uint8_t r = term->vt.params.v[i + 2].value;
uint8_t g = term->vt.params.v[i + 3].value;
uint8_t b = term->vt.params.v[i + 4].value;
src = COLOR_RGB;
color = r << 16 | g << 8 | b;
i += 4;
}
/* Indexed: 38:5:<idx> */
else if (term->vt.params.v[i].sub.idx >= 2 &&
term->vt.params.v[i].sub.value[0] == 5)
{
src = COLOR_BASE256;
color = min(term->vt.params.v[i].sub.value[1],
ALEN(term->colors.table) - 1);
}
/*
* RGB: 38:2:<color-space>:r:g:b[:ignored:tolerance:tolerance-color-space]
* RGB: 38:2:r:g:b
*
* The second version is a "bastard" version - many
* programs "forget" the color space ID
* parameter... *sigh*
*/
else if (term->vt.params.v[i].sub.idx >= 4 &&
term->vt.params.v[i].sub.value[0] == 2)
{
const struct vt_param *param = &term->vt.params.v[i];
bool have_color_space_id = param->sub.idx >= 5;
/* 0 - color space (ignored) */
int r_idx = 2 - !have_color_space_id;
int g_idx = 3 - !have_color_space_id;
int b_idx = 4 - !have_color_space_id;
/* 5 - unused */
/* 6 - CS tolerance */
/* 7 - color space associated with tolerance */
uint8_t r = param->sub.value[r_idx];
uint8_t g = param->sub.value[g_idx];
uint8_t b = param->sub.value[b_idx];
src = COLOR_RGB;
color = r << 16 | g << 8 | b;
}
/* Transparent: 38:1 */
/* CMY: 38:3:<color-space>:c:m:y[:tolerance:tolerance-color-space] */
/* CMYK: 38:4:<color-space>:c:m:y:k[:tolerance:tolerance-color-space] */
/* Unrecognized */
else {
UNHANDLED_SGR(i);
break;
}
if (unlikely(param == 58)) {
term->vt.underline.color_src = src;
term->vt.underline.color = color;
term->bits_affecting_ascii_printer.underline_color = true;
term_update_ascii_printer(term);
} else if (param == 38) {
term->vt.attrs.fg_src = src;
term->vt.attrs.fg = color;
} else {
xassert(param == 48);
term->vt.attrs.bg_src = src;
term->vt.attrs.bg = color;
}
break;
}
case 39:
term->vt.attrs.fg_src = COLOR_DEFAULT;
break;
/* Regular background colors */
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
term->vt.attrs.bg_src = COLOR_BASE16;
term->vt.attrs.bg = param - 40;
break;
case 49:
term->vt.attrs.bg_src = COLOR_DEFAULT;
break;
case 59:
term->vt.underline.color_src = COLOR_DEFAULT;
term->vt.underline.color = 0;
term->bits_affecting_ascii_printer.underline_color = false;
term_update_ascii_printer(term);
break;
/* Bright foreground colors */
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
term->vt.attrs.fg_src = COLOR_BASE16;
term->vt.attrs.fg = param - 90 + 8;
break;
/* Bright background colors */
case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107:
term->vt.attrs.bg_src = COLOR_BASE16;
term->vt.attrs.bg = param - 100 + 8;
break;
default:
UNHANDLED_SGR(i);
break;
}
}
}
static void
decset_decrst(struct terminal *term, unsigned param, bool enable)
{
#if defined(_DEBUG)
/* For UNHANDLED() */
int UNUSED final = enable ? 'h' : 'l';
#endif
/* Note: update XTSAVE/XTRESTORE if adding/removing things here */
switch (param) {
case 1:
/* DECCKM */
term->cursor_keys_mode =
enable ? CURSOR_KEYS_APPLICATION : CURSOR_KEYS_NORMAL;
break;
case 5:
/* DECSCNM */
term->reverse = enable;
term_damage_all(term);
term_damage_margins(term);
break;
case 6: {
/* DECOM */
term->origin = enable ? ORIGIN_RELATIVE : ORIGIN_ABSOLUTE;
term_cursor_home(term);
break;
}
case 7:
/* DECAWM */
term->auto_margin = enable;
term->grid->cursor.lcf = false;
break;
case 9:
if (enable)
LOG_WARN("unimplemented: X10 mouse tracking mode");
#if 0
else if (term->mouse_tracking == MOUSE_X10)
term->mouse_tracking = MOUSE_NONE;
#endif
break;
case 12:
term->cursor_blink.decset = enable;
term_cursor_blink_update(term);
break;
case 25:
/* DECTCEM */
term->hide_cursor = !enable;
break;
case 45:
term->reverse_wrap = enable;
break;
case 66:
/* DECNKM */
term->keypad_keys_mode = enable ? KEYPAD_APPLICATION : KEYPAD_NUMERICAL;
break;
case 67:
if (enable)
LOG_WARN("unimplemented: DECBKM");
break;
case 80:
term->sixel.scrolling = !enable;
break;
case 1000:
if (enable)
term->mouse_tracking = MOUSE_CLICK;
else if (term->mouse_tracking == MOUSE_CLICK)
term->mouse_tracking = MOUSE_NONE;
term_xcursor_update(term);
break;
case 1001:
if (enable)
LOG_WARN("unimplemented: highlight mouse tracking");
break;
case 1002:
if (enable)
term->mouse_tracking = MOUSE_DRAG;
else if (term->mouse_tracking == MOUSE_DRAG)
term->mouse_tracking = MOUSE_NONE;
term_xcursor_update(term);
break;
case 1003:
if (enable)
term->mouse_tracking = MOUSE_MOTION;
else if (term->mouse_tracking == MOUSE_MOTION)
term->mouse_tracking = MOUSE_NONE;
term_xcursor_update(term);
break;
case 1004:
term->focus_events = enable;
break;
case 1005:
if (enable)
LOG_WARN("unimplemented: mouse reporting mode: UTF-8");
#if 0
else if (term->mouse_reporting == MOUSE_UTF8)
term->mouse_reporting = MOUSE_NONE;
#endif
break;
case 1006:
if (enable)
term->mouse_reporting = MOUSE_SGR;
else if (term->mouse_reporting == MOUSE_SGR)
term->mouse_reporting = MOUSE_NORMAL;
break;
case 1007:
term->alt_scrolling = enable;
break;
case 1015:
if (enable)
term->mouse_reporting = MOUSE_URXVT;
else if (term->mouse_reporting == MOUSE_URXVT)
term->mouse_reporting = MOUSE_NORMAL;
break;
case 1016:
if (enable)
term->mouse_reporting = MOUSE_SGR_PIXELS;
else if (term->mouse_reporting == MOUSE_SGR_PIXELS)
term->mouse_reporting = MOUSE_NORMAL;
break;
case 1034:
/* smm */
LOG_DBG("%s 8-bit meta mode", enable ? "enabling" : "disabling");
term->meta.eight_bit = enable;
break;
case 1035:
/* numLock */
LOG_DBG("%s Num Lock modifier", enable ? "enabling" : "disabling");
term->num_lock_modifier = enable;
break;
case 1036:
/* metaSendsEscape */
LOG_DBG("%s meta-sends-escape", enable ? "enabling" : "disabling");
term->meta.esc_prefix = enable;
break;
case 1042:
term->bell_action_enabled = enable;
break;
#if 0
case 1043:
LOG_WARN("unimplemented: raise window on ctrl-g");
break;
#endif
case 1048:
if (enable)
term_save_cursor(term);
else
term_restore_cursor(term, &term->grid->saved_cursor);
break;
case 47:
case 1047:
case 1049:
if (enable && term->grid != &term->alt) {
selection_cancel(term);
if (param == 1049)
term_save_cursor(term);
term->grid = &term->alt;
/* Cursor retains its position from the normal grid */
term_cursor_to(
term,
min(term->normal.cursor.point.row, term->rows - 1),
min(term->normal.cursor.point.col, term->cols - 1));
tll_free(term->normal.scroll_damage);
term_erase(term, 0, 0, term->rows - 1, term->cols - 1);
}
else if (!enable && term->grid == &term->alt) {
selection_cancel(term);
term->grid = &term->normal;
/* Cursor retains its position from the alt grid */
term_cursor_to(
term, min(term->alt.cursor.point.row, term->rows - 1),
min(term->alt.cursor.point.col, term->cols - 1));
if (param == 1049)
term_restore_cursor(term, &term->grid->saved_cursor);
/* Delete all sixel images on the alt screen */
tll_foreach(term->alt.sixel_images, it) {
sixel_destroy(&it->item);
tll_remove(term->alt.sixel_images, it);
}
tll_free(term->alt.scroll_damage);
term_damage_view(term);
}
term->bits_affecting_ascii_printer.sixels =
tll_length(term->grid->sixel_images) > 0;
term_update_ascii_printer(term);
break;
case 1070:
term->sixel.use_private_palette = enable;
break;
case 2004:
term->bracketed_paste = enable;
break;
case 2026:
if (enable)
term_enable_app_sync_updates(term);
else
term_disable_app_sync_updates(term);
break;
case 2027:
term->grapheme_shaping = enable;
break;
case 2048:
if (enable)
term_enable_size_notifications(term);
else
term_disable_size_notifications(term);
break;
case 8452:
term->sixel.cursor_right_of_graphics = enable;
break;
case 737769:
if (enable)
term_ime_enable(term);
else {
term_ime_disable(term);
term->ime_reenable_after_url_mode = false;
}
break;
default:
UNHANDLED();
break;
}
}
static void
decset(struct terminal *term, unsigned param)
{
decset_decrst(term, param, true);
}
static void
decrst(struct terminal *term, unsigned param)
{
decset_decrst(term, param, false);
}
/*
* These values represent the current state of a DEC private mode,
* as returned in the DECRPM reply to a DECRQM query.
*/
enum decrpm_status {
DECRPM_NOT_RECOGNIZED = 0,
DECRPM_SET = 1,
DECRPM_RESET = 2,
DECRPM_PERMANENTLY_SET = 3,
DECRPM_PERMANENTLY_RESET = 4,
};
static enum decrpm_status
decrpm(bool enabled)
{
return enabled ? DECRPM_SET : DECRPM_RESET;
}
static enum decrpm_status
decrqm(const struct terminal *term, unsigned param)
{
switch (param) {
case 1: return decrpm(term->cursor_keys_mode == CURSOR_KEYS_APPLICATION);
case 5: return decrpm(term->reverse);
case 6: return decrpm(term->origin);
case 7: return decrpm(term->auto_margin);
case 9: return DECRPM_PERMANENTLY_RESET; /* term->mouse_tracking == MOUSE_X10; */
case 12: return decrpm(term->cursor_blink.decset);
case 25: return decrpm(!term->hide_cursor);
case 45: return decrpm(term->reverse_wrap);
case 66: return decrpm(term->keypad_keys_mode == KEYPAD_APPLICATION);
case 67: return DECRPM_PERMANENTLY_RESET; /* https://vt100.net/docs/vt510-rm/DECBKM */
case 80: return decrpm(!term->sixel.scrolling);
case 1000: return decrpm(term->mouse_tracking == MOUSE_CLICK);
case 1001: return DECRPM_PERMANENTLY_RESET;
case 1002: return decrpm(term->mouse_tracking == MOUSE_DRAG);
case 1003: return decrpm(term->mouse_tracking == MOUSE_MOTION);
case 1004: return decrpm(term->focus_events);
case 1005: return DECRPM_PERMANENTLY_RESET; /* term->mouse_reporting == MOUSE_UTF8; */
case 1006: return decrpm(term->mouse_reporting == MOUSE_SGR);
case 1007: return decrpm(term->alt_scrolling);
case 1015: return decrpm(term->mouse_reporting == MOUSE_URXVT);
case 1016: return decrpm(term->mouse_reporting == MOUSE_SGR_PIXELS);
case 1034: return decrpm(term->meta.eight_bit);
case 1035: return decrpm(term->num_lock_modifier);
case 1036: return decrpm(term->meta.esc_prefix);
case 1042: return decrpm(term->bell_action_enabled);
case 47: /* FALLTHROUGH */
case 1047: /* FALLTHROUGH */
case 1049: return decrpm(term->grid == &term->alt);
case 1070: return decrpm(term->sixel.use_private_palette);
case 2004: return decrpm(term->bracketed_paste);
case 2026: return decrpm(term->render.app_sync_updates.enabled);
case 2027: return term->conf->tweak.grapheme_width_method != GRAPHEME_WIDTH_DOUBLE
? DECRPM_PERMANENTLY_RESET
: decrpm(term->grapheme_shaping);
case 2048: return decrpm(term->size_notifications);
case 8452: return decrpm(term->sixel.cursor_right_of_graphics);
case 737769: return decrpm(term_ime_is_enabled(term));
}
return DECRPM_NOT_RECOGNIZED;
}
static void
xtsave(struct terminal *term, unsigned param)
{
switch (param) {
case 1: term->xtsave.application_cursor_keys = term->cursor_keys_mode == CURSOR_KEYS_APPLICATION; break;
case 5: term->xtsave.reverse = term->reverse; break;
case 6: term->xtsave.origin = term->origin; break;
case 7: term->xtsave.auto_margin = term->auto_margin; break;
case 9: /* term->xtsave.mouse_x10 = term->mouse_tracking == MOUSE_X10; */ break;
case 12: term->xtsave.cursor_blink = term->cursor_blink.decset; break;
case 25: term->xtsave.show_cursor = !term->hide_cursor; break;
case 45: term->xtsave.reverse_wrap = term->reverse_wrap; break;
case 47: term->xtsave.alt_screen = term->grid == &term->alt; break;
case 66: term->xtsave.application_keypad_keys = term->keypad_keys_mode == KEYPAD_APPLICATION; break;
case 67: break;
case 80: term->xtsave.sixel_display_mode = !term->sixel.scrolling; break;
case 1000: term->xtsave.mouse_click = term->mouse_tracking == MOUSE_CLICK; break;
case 1001: break;
case 1002: term->xtsave.mouse_drag = term->mouse_tracking == MOUSE_DRAG; break;
case 1003: term->xtsave.mouse_motion = term->mouse_tracking == MOUSE_MOTION; break;
case 1004: term->xtsave.focus_events = term->focus_events; break;
case 1005: /* term->xtsave.mouse_utf8 = term->mouse_reporting == MOUSE_UTF8; */ break;
case 1006: term->xtsave.mouse_sgr = term->mouse_reporting == MOUSE_SGR; break;
case 1007: term->xtsave.alt_scrolling = term->alt_scrolling; break;
case 1015: term->xtsave.mouse_urxvt = term->mouse_reporting == MOUSE_URXVT; break;
case 1016: term->xtsave.mouse_sgr_pixels = term->mouse_reporting == MOUSE_SGR_PIXELS; break;
case 1034: term->xtsave.meta_eight_bit = term->meta.eight_bit; break;
case 1035: term->xtsave.num_lock_modifier = term->num_lock_modifier; break;
case 1036: term->xtsave.meta_esc_prefix = term->meta.esc_prefix; break;
case 1042: term->xtsave.bell_action_enabled = term->bell_action_enabled; break;
case 1047: term->xtsave.alt_screen = term->grid == &term->alt; break;
case 1048: term_save_cursor(term); break;
case 1049: term->xtsave.alt_screen = term->grid == &term->alt; break;
case 1070: term->xtsave.sixel_private_palette = term->sixel.use_private_palette; break;
case 2004: term->xtsave.bracketed_paste = term->bracketed_paste; break;
case 2026: term->xtsave.app_sync_updates = term->render.app_sync_updates.enabled; break;
case 2027: term->xtsave.grapheme_shaping = term->grapheme_shaping; break;
case 2048: term->xtsave.size_notifications = term->size_notifications; break;
case 8452: term->xtsave.sixel_cursor_right_of_graphics = term->sixel.cursor_right_of_graphics; break;
case 737769: term->xtsave.ime = term_ime_is_enabled(term); break;
}
}
static void
xtrestore(struct terminal *term, unsigned param)
{
bool enable;
switch (param) {
case 1: enable = term->xtsave.application_cursor_keys; break;
case 5: enable = term->xtsave.reverse; break;
case 6: enable = term->xtsave.origin; break;
case 7: enable = term->xtsave.auto_margin; break;
case 9: /* enable = term->xtsave.mouse_x10; break; */ return;
case 12: enable = term->xtsave.cursor_blink; break;
case 25: enable = term->xtsave.show_cursor; break;
case 45: enable = term->xtsave.reverse_wrap; break;
case 47: enable = term->xtsave.alt_screen; break;
case 66: enable = term->xtsave.application_keypad_keys; break;
case 67: return;
case 80: enable = term->xtsave.sixel_display_mode; break;
case 1000: enable = term->xtsave.mouse_click; break;
case 1001: return;
case 1002: enable = term->xtsave.mouse_drag; break;
case 1003: enable = term->xtsave.mouse_motion; break;
case 1004: enable = term->xtsave.focus_events; break;
case 1005: /* enable = term->xtsave.mouse_utf8; break; */ return;
case 1006: enable = term->xtsave.mouse_sgr; break;
case 1007: enable = term->xtsave.alt_scrolling; break;
case 1015: enable = term->xtsave.mouse_urxvt; break;
case 1016: enable = term->xtsave.mouse_sgr_pixels; break;
case 1034: enable = term->xtsave.meta_eight_bit; break;
case 1035: enable = term->xtsave.num_lock_modifier; break;
case 1036: enable = term->xtsave.meta_esc_prefix; break;
case 1042: enable = term->xtsave.bell_action_enabled; break;
case 1047: enable = term->xtsave.alt_screen; break;
case 1048: enable = true; break;
case 1049: enable = term->xtsave.alt_screen; break;
case 1070: enable = term->xtsave.sixel_private_palette; break;
case 2004: enable = term->xtsave.bracketed_paste; break;
case 2026: enable = term->xtsave.app_sync_updates; break;
case 2027: enable = term->xtsave.grapheme_shaping; break;
case 2048: enable = term->xtsave.size_notifications; break;
case 8452: enable = term->xtsave.sixel_cursor_right_of_graphics; break;
case 737769: enable = term->xtsave.ime; break;
default: return;
}
decset_decrst(term, param, enable);
}
static bool
params_to_rectangular_area(const struct terminal *term, int first_idx,
int *top, int *left, int *bottom, int *right)
{
int rel_top = vt_param_get(term, first_idx + 0, 1) - 1;
*left = min(vt_param_get(term, first_idx + 1, 1) - 1, term->cols - 1);
int rel_bottom = vt_param_get(term, first_idx + 2, term->rows) - 1;
*right = min(vt_param_get(term, first_idx + 3, term->cols) - 1, term->cols - 1);
if (rel_top > rel_bottom || *left > *right)
return false;
*top = term_row_rel_to_abs(term, rel_top);
*bottom = term_row_rel_to_abs(term, rel_bottom);
return true;
}
void
csi_dispatch(struct terminal *term, uint8_t final)
{
LOG_DBG("%s (%08x)", csi_as_string(term, final, -1), term->vt.private);
switch (term->vt.private) {
case 0: {
switch (final) {
case 'b':
if (term->vt.last_printed != 0) {
/*
* Note: we never reset 'last-printed'. According to
* ECMA-48, the behaviour is undefined if REP was
* _not_ preceded by a graphical character.
*/
int count = vt_param_get(term, 0, 1);
LOG_DBG("REP: '%lc' %d times", (wint_t)term->vt.last_printed, count);
const int width = c32width(term->vt.last_printed);
if (width > 0) {
for (int i = 0; i < count; i++)
term_print(term, term->vt.last_printed, width);
}
}
break;
case 'c': {
if (vt_param_get(term, 0, 0) != 0) {
UNHANDLED();
break;
}
/* Send Device Attributes (Primary DA) */
/*
* Responses:
* - CSI?1;2c vt100 with advanced video option
* - CSI?1;0c vt101 with no options
* - CSI?6c vt102
* - CSI?62;<Ps>c vt220
* - CSI?63;<Ps>c vt320
* - CSI?64;<Ps>c vt420
*
* Ps (response may contain multiple):
* - 1 132 columns
* - 2 Printer.
* - 3 ReGIS graphics.
* - 4 Sixel graphics.
* - 6 Selective erase.
* - 8 User-defined keys.
* - 9 National Replacement Character sets.
* - 15 Technical characters.
* - 16 Locator port.
* - 17 Terminal state interrogation.
* - 18 User windows.
* - 21 Horizontal scrolling.
* - 22 ANSI color, e.g., VT525.
* - 28 Rectangular editing.
* - 29 ANSI text locator (i.e., DEC Locator mode).
*
* Note: we report ourselves as a VT220, mainly to be able
* to pass parameters, to indicate we support sixel, and
* ANSI colors.
*
* The VT level must be synchronized with the secondary DA
* response.
*
* Note: tertiary DA responds with "FOOT".
*/
if (term->conf->tweak.sixel) {
static const char reply[] = "\033[?62;4;22;28c";
term_to_slave(term, reply, sizeof(reply) - 1);
} else {
static const char reply[] = "\033[?62;22;28c";
term_to_slave(term, reply, sizeof(reply) - 1);
}
break;
}
case 'd': {
/* VPA - vertical line position absolute */
int rel_row = vt_param_get(term, 0, 1) - 1;
int row = term_row_rel_to_abs(term, rel_row);
term_cursor_to(term, row, term->grid->cursor.point.col);
break;
}
case 'm':
csi_sgr(term);
break;
case 'A':
term_cursor_up(term, vt_param_get(term, 0, 1));
break;
case 'e':
case 'B':
term_cursor_down(term, vt_param_get(term, 0, 1));
break;
case 'a':
case 'C':
term_cursor_right(term, vt_param_get(term, 0, 1));
break;
case 'D':
term_cursor_left(term, vt_param_get(term, 0, 1));
break;
case 'E':
/* CNL - Cursor Next Line */
term_cursor_down(term, vt_param_get(term, 0, 1));
term_cursor_left(term, term->grid->cursor.point.col);
break;
case 'F':
/* CPL - Cursor Previous Line */
term_cursor_up(term, vt_param_get(term, 0, 1));
term_cursor_left(term, term->grid->cursor.point.col);
break;
case 'g': {
int param = vt_param_get(term, 0, 0);
switch (param) {
case 0:
/* Clear tab stop at *current* column */
tll_foreach(term->tab_stops, it) {
if (it->item == term->grid->cursor.point.col)
tll_remove(term->tab_stops, it);
else if (it->item > term->grid->cursor.point.col)
break;
}
break;
case 3:
/* Clear *all* tabs */
tll_free(term->tab_stops);
break;
default:
UNHANDLED();
break;
}
break;
}
case '`':
case 'G': {
/* Cursor horizontal absolute */
int col = min(vt_param_get(term, 0, 1), term->cols) - 1;
term_cursor_col(term, col);
break;
}
case 'f':
case 'H': {
/* Move cursor */
int rel_row = vt_param_get(term, 0, 1) - 1;
int row = term_row_rel_to_abs(term, rel_row);
int col = min(vt_param_get(term, 1, 1), term->cols) - 1;
term_cursor_to(term, row, col);
break;
}
case 'J': {
/* Erase screen */
int param = vt_param_get(term, 0, 0);
switch (param) {
case 0: {
/* From cursor to end of screen */
const struct coord *cursor = &term->grid->cursor.point;
term_erase(
term,
cursor->row, cursor->col,
term->rows - 1, term->cols - 1);
term->grid->cursor.lcf = false;
break;
}
case 1: {
/* From start of screen to cursor */
const struct coord *cursor = &term->grid->cursor.point;
term_erase(term, 0, 0, cursor->row, cursor->col);
term->grid->cursor.lcf = false;
break;
}
case 2:
/* Erase entire screen */
term_erase(term, 0, 0, term->rows - 1, term->cols - 1);
term->grid->cursor.lcf = false;
break;
case 3: {
/* Erase scrollback */
term_erase_scrollback(term);
break;
}
default:
UNHANDLED();
break;
}
break;
}
case 'K': {
/* Erase line */
int param = vt_param_get(term, 0, 0);
switch (param) {
case 0: {
/* From cursor to end of line */
const struct coord *cursor = &term->grid->cursor.point;
term_erase(
term,
cursor->row, cursor->col,
cursor->row, term->cols - 1);
term->grid->cursor.lcf = false;
break;
}
case 1: {
/* From start of line to cursor */