aboutsummaryrefslogtreecommitdiff
path: root/csi.c
blob: 2cb5e7a55f205c7f5c307eeff627548f090c23df (plain)
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
#include "csi.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#if defined(_DEBUG)
 #include <stdio.h>
#endif

#define LOG_MODULE "csi"
#define LOG_ENABLE_DBG 1
#include "log.h"
#include "grid.h"

#define min(x, y) ((x) < (y) ? (x) : (y))

static const struct rgba colors_regular[] = {
    {0.000000, 0.000000, 0.000000, 1.000000},  /* 0x000000ff */
    {0.800000, 0.576471, 0.576471, 1.000000},  /* 0xcc9393ff */
    {0.498039, 0.623529, 0.498039, 1.000000},  /* 0x7f9f7fff */
    {0.815686, 0.749020, 0.560784, 1.000000},  /* 0xd0bf8fff */
    {0.423529, 0.627451, 0.639216, 1.000000},  /* 0x6ca0a3ff */
    {0.862745, 0.549020, 0.764706, 1.000000},  /* 0xdc8cc3ff */
    {0.576471, 0.878431, 0.890196, 1.000000},  /* 0x93e0e3ff */
    {0.862745, 0.862745, 0.800000, 1.000000},  /* 0xdcdcccff */
};

static const struct rgba colors_bright[] = {
    {0.000000, 0.000000, 0.000000, 1.000000},  /* 0x000000ff */
    {0.862745, 0.639216, 0.639216, 1.000000},  /* 0xdca3a3ff */
    {0.749020, 0.921569, 0.749020, 1.000000},  /* 0xbfebbfff */
    {0.941176, 0.874510, 0.686275, 1.000000},  /* 0xf0dfafff */
    {0.549020, 0.815686, 0.827451, 1.000000},  /* 0x8cd0d3ff */
    {0.862745, 0.549020, 0.764706, 1.000000},  /* 0xdc8cc3ff */
    {0.576471, 0.878431, 0.890196, 1.000000},  /* 0x93e0e3ff */
    {1.000000, 1.000000, 1.000000, 1.000000},  /* 0xffffffff */
};

static struct rgba colors256[256];

static void __attribute__((constructor))
initialize_colors256(void)
{
    for (size_t i = 0; i < 8; i++)
        colors256[i] = colors_regular[i];
    for (size_t i = 0; i < 8; i++)
        colors256[8 + i] = colors_bright[i];

    for (size_t r = 0; r < 6; r++) {
        for (size_t g = 0; g < 6; g++) {
            for (size_t b = 0; b < 6; b++) {
                colors256[16 + r * 6 * 6 + g * 6 + b] = (struct rgba) {
                    r * 51 / 255.0,
                    g * 51 / 255.0,
                    b * 51 / 255.0,
                    1.0,
                };
#if 0
                colors256[16 + r * 6 * 6 + g * 6 + b] =
                    (51 * r) << 24 | (51 * g) << 16 | (51 * b) << 8 | 0xff;
#endif
            }
        }
    }

    for (size_t i = 0; i < 24; i++){
        colors256[232 + i] = (struct rgba) {
            i * 11 / 255.0,
            i * 11 / 255.0,
            i * 11 / 255.0,
            1.0
        };
            //(11 * i) << 24 | (11 * i) << 16 | (11 * i) << 8 | 0xff;
    }
}

static int
param_get(const struct terminal *term, size_t idx, int default_value)
{
    if (term->vt.params.idx > idx) {
        int value = term->vt.params.v[idx].value;
        return value != 0 ? value : default_value;
    }

    return default_value;
}

static void
sgr_reset(struct terminal *term)
{
    memset(&term->vt.attrs, 0, sizeof(term->vt.attrs));
    term->vt.dim = false;
    term->vt.attrs.foreground = term->foreground;
    term->vt.attrs.background = term->background;
}

static bool
csi_sgr(struct terminal *term)
{
    if (term->vt.params.idx == 0) {
        sgr_reset(term);
        return true;
    }

    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.dim = true; break;
        case 3: term->vt.attrs.italic = true; break;
        case 4: term->vt.attrs.underline = true; break;
        case 5: term->vt.attrs.blink = true; break;
        case 6: term->vt.attrs.blink = true; 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 22: term->vt.attrs.bold = term->vt.dim = false; break;
        case 23: term->vt.attrs.italic = false; break;
        case 24: term->vt.attrs.underline = false; break;
        case 25: term->vt.attrs.blink = false; break;
        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.have_foreground = true;
            term->vt.attrs.foreground = colors_regular[param - 30];
            break;

        case 38: {
            if (term->vt.params.idx - i - 1 == 2 &&
                term->vt.params.v[i + 1].value == 5)
            {
                size_t idx = term->vt.params.v[i + 2].value;
                term->vt.attrs.foreground = colors256[idx];
                term->vt.attrs.have_foreground = true;
                i += 2;
            } 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;
                term->vt.attrs.foreground = (struct rgba) {
                    r / 255.0,
                    g / 255.0,
                    b / 255.0,
                    1.0,
                };
                term->vt.attrs.have_foreground = true;
                i += 4;
            } else {
                LOG_ERR("invalid CSI SGR sequence");
                return false;
            }
            break;
        }
        case 39:
            term->vt.attrs.foreground = term->foreground;
            term->vt.attrs.have_foreground = false;
            break;

        /* Regular background colors */
        case 40:
        case 41:
        case 42:
        case 43:
        case 44:
        case 45:
        case 46:
        case 47:
            term->vt.attrs.background = colors_regular[param - 40];
            term->vt.attrs.have_background = true;
            break;

        case 48: {
            if (term->vt.params.idx - i - 1 == 2 &&
                term->vt.params.v[i + 1].value == 5)
            {
                size_t idx = term->vt.params.v[i + 2].value;
                term->vt.attrs.background = colors256[idx];
                term->vt.attrs.have_background = true;
                i += 2;
            } 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;
                term->vt.attrs.background = (struct rgba) {
                    r / 255.0,
                    g / 255.0,
                    b / 255.0,
                    1.0
                };
                term->vt.attrs.have_background = true;
                i += 4;
            } else {
                LOG_ERR("invalid CSI SGR sequence");
                return false;
            }
            break;
        }
        case 49:
            term->vt.attrs.background = term->background;
            term->vt.attrs.have_background = false;
            break;

        /* Bright foreground colors */
        case 90:
        case 91:
        case 92:
        case 93:
        case 94:
        case 95:
        case 96:
        case 97:
            term->vt.attrs.foreground = colors_bright[param - 90];
            term->vt.attrs.have_foreground = true;
            break;

        /* Regular background colors */
        case 100:
        case 101:
        case 102:
        case 103:
        case 104:
        case 105:
        case 106:
        case 107:
            term->vt.attrs.background = colors_bright[param - 100];
            term->vt.attrs.have_background = true;
            break;

        default:
            LOG_ERR("unimplemented: CSI: SGR: %u", term->vt.params.v[i].value);
            return false;
        }
    }

    return true;
}

bool
csi_dispatch(struct terminal *term, uint8_t final)
{
#if defined(_DEBUG) && defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
    char log[1024];
    int c = snprintf(log, sizeof(log), "CSI: ");

    for (size_t i = 0; i < term->vt.intermediates.idx; i++)
        c += snprintf(&log[c], sizeof(log) - c, "%c", term->vt.intermediates.data[i]);

    for (size_t i = 0; i < term->vt.params.idx; i++){
        c += snprintf(&log[c], sizeof(log) - c, "%d",
                      term->vt.params.v[i].value);

        for (size_t j = 0; i < term->vt.params.v[i].sub.idx; j++) {
            c += snprintf(&log[c], sizeof(log) - c, ":%d",
                          term->vt.params.v[i].sub.value[j]);
        }

        c += snprintf(&log[c], sizeof(log) - c, "%s",
                      i == term->vt.params.idx - 1 ? "" : ";");
    }

    c += snprintf(&log[c], sizeof(log) - c, "%c (%d parameters)",
                  final, term->vt.params.idx);
    LOG_DBG("%s", log);
#endif

    if (term->vt.intermediates.idx == 0) {
        switch (final) {
        case 'c':
            return write(term->ptmx, "\033[?6c", 5) == 5;

        case 'd': {
            /* VPA - vertical line position absolute */
            int row = param_get(term, 0, 1);

            if (row > term->rows)
                row = term->rows;

            term_cursor_to(term, row - 1, term->cursor.col);
            break;
        }

        case 'm':
            return csi_sgr(term);

        case 'A':
            term_cursor_up(term, param_get(term, 0, 1));
            break;

        case 'e':
        case 'B':
            term_cursor_down(term, param_get(term, 0, 1));
            break;

        case 'C':
            term_cursor_right(term, param_get(term, 0, 1));
            break;

        case 'D':
            term_cursor_left(term, param_get(term, 0, 1));
            break;

        case 'G': {
            /* Cursor horizontal absolute */
            int col = param_get(term, 0, 1);

            if (col > term->cols)
                col = term->cols;

            term_cursor_to(term, term->cursor.row, col);
            break;
        }

        case 'H': {
            /* Move cursor */
            int row = param_get(term, 0, 1);
            int col = param_get(term, 1, 1);

            if (row > term->rows)
                row = term->rows;
            if (col > term->cols)
                col = term->cols;

            term_cursor_to(term, row - 1, col - 1);
            break;
        }

        case 'J': {
            /* Erase screen */

            int param = param_get(term, 0, 0);
            int start = -1;
            int end = -1;
            switch (param) {
            case 0:
                /* From cursor to end of screen */
                start = term->cursor.linear;
                end = term->cols * term->rows;
                break;

            case 1:
                /* From start of screen to cursor */
                start = 0;
                end = term->cursor.linear;
                break;

            case 2:
                /* Erase entire screen */
                start = 0;
                end = term->cols * term->rows;
                break;

            default:
                LOG_ERR("CSI: J: invalid argument: %d", param);
                return false;
            }

            term_erase(term, start, end);
            break;
        }

        case 'K': {
            /* Erase line */

            int param = param_get(term, 0, 0);
            int start = -1;
            int end = -1;
            switch (param) {
            case 0:
                /* From cursor to end of line */
                start = term->cursor.linear;
                end = term_cursor_linear(term, term->cursor.row, term->cols);
                break;

            case 1:
                /* From start of line to cursor */
                start = term_cursor_linear(term, term->cursor.row, 0);
                end = term->cursor.linear;
                break;

            case 2:
                /* Entire line */
                start = term_cursor_linear(term, term->cursor.row, 0);
                end = term_cursor_linear(term, term->cursor.row, term->cols);
                break;

            default:
                LOG_ERR("CSI: K: invalid argument: %d", param);
                return false;
            }

            term_erase(term, start, end);
            break;
        }

        case 'L': {
            if (term->cursor.row < term->scroll_region.start ||
                term->cursor.row >= term->scroll_region.end)
                break;

            int count = min(
                param_get(term, 0, 1),
                term->scroll_region.end - term->cursor.row);

            term_scroll_reverse_partial(
                term,
                (struct scroll_region){
                    .start = term->cursor.row,
                    .end = term->scroll_region.end},
                count);
            break;
        }

        case 'M': {
            if (term->cursor.row < term->scroll_region.start ||
                term->cursor.row >= term->scroll_region.end)
                break;

            int count = min(
                param_get(term, 0, 1),
                term->scroll_region.end - term->cursor.row);

            term_scroll_partial(
                term,
                (struct scroll_region){
                    .start = term->cursor.row,
                    .end = term->scroll_region.end},
                count);
            break;
        }

        case 'P': {
            /* DCH: Delete character */
            int count = param_get(term, 0, 1);

            /* Only delete up to the right margin */
            const int max_end = term_cursor_linear(
                term, term->cursor.row, term->cols);

            int start = term->cursor.linear;
            int end = min(start + count, max_end);

            /* Erase the requested number of characters */
            term_erase(term, start, end);

            /* Move remaining (up til the right margin) characters */
            int remaining = max_end - end;
            memmove(&term->grid->cells[start],
                    &term->grid->cells[end],
                    remaining * sizeof(term->grid->cells[0]));
            term_damage_update(term, term->cursor.linear, remaining);
            break;
        }

        case 'r': {
            int start = param_get(term, 0, 1);
            int end = param_get(term, 1, term->rows);

            /* 1-based */
            term->scroll_region.start = start - 1;
            term->scroll_region.end = end;

            term_cursor_to(term, (start - 1) * term->cols, 0);

            LOG_INFO("scroll region: %d-%d",
                     term->scroll_region.start,
                     term->scroll_region.end);
            break;
        }

        case 't':
            /*
             * TODO: xterm's terminfo specifies *both* \e[?1049h *and*
             * \e[22;0;0t in smcup, but only one is necessary. We
             * should provide our own terminfo with *only* \e[?1049h
             * (and \e[?1049l for rmcup)
             */
            LOG_WARN("ignoring CSI with final 't'");
            break;

        case 'n': {
            if (term->vt.params.idx > 0) {
                int param = param_get(term, 0, 0);
                switch (param) {
                case 6: {
                    /* u7 - cursor position query */
                    /* TODO: we use 0-based position, while the xterm
                     * terminfo says the receiver of the reply should
                     * decrement, hence we must add 1 */
                    char reply[64];
                    snprintf(reply, sizeof(reply), "\x1b[%d;%dR",
                             term->cursor.row + 1,
                             term->cursor.col + 1);
                    write(term->ptmx, reply, strlen(reply));
                    break;
                }

                default:
                    LOG_ERR("CSI: unimplemented parameter for 'n': %d", param);
                    return false;
                }

                return true;
            } else {
                LOG_ERR("CSI: missing parameter for 'n'");
                return false;
            }
            break;
        }

        case '=':
            /*
             * TODO: xterm's terminfo specifies *both* \e[?1h *and*
             * \e= in smkx, but only one is necessary. We should
             * provide our own terminfo with *only* \e[?1h (and \e[?1l
             * for rmkx)
             */
            LOG_WARN("ignoring CSI with final '='");
            break;

        default:
            LOG_ERR("CSI: unimplemented final: %c", final);
            abort();
        }

        return true;
    }

    else if (term->vt.intermediates.idx == 1 &&
               term->vt.intermediates.data[0] == '?') {

        switch (final) {
        case 'h': {
            for (size_t i = 0; i < term->vt.params.idx; i++) {
                switch (term->vt.params.v[i].value) {
                case 1:
                    term->decckm = DECCKM_SS3;
                    break;

                case 5:
                    LOG_WARN("unimplemented: flash");
                    break;

                case 12:
                    LOG_WARN("unimplemented: cursor blinking");
                    break;

                case 25:
                    LOG_WARN("unimplemented: civis");
                    break;

                case 1000:
                    LOG_WARN("unimplemented: report mouse clicks");
                    break;

                case 1002:
                    LOG_WARN("unimplemented: report cell mouse motion");
                    break;

                case 1006:
                    LOG_WARN("unimplemented: SGR mouse");
                    break;

                case 1049:
                    if (term->grid != &term->alt) {
                        term->grid = &term->alt;
                        term->saved_cursor = term->cursor;

                        tll_free(term->alt.damage);
                        tll_free(term->alt.scroll_damage);
                        term_erase(term, 0, term->rows * term->cols);
                    }
                    break;

                case 2004:
                    term->bracketed_paste = true;
                    break;

                default:
                    LOG_ERR("CSI: 'h' (set mode): unimplemented param: %d",
                            term->vt.params.v[i].value);
                    abort();
                    break;
                }
            }
            break;
        }

        case 'l': {
            for (size_t i = 0; i < term->vt.params.idx; i++) {
                switch (term->vt.params.v[i].value) {
                case 1:
                    term->decckm = DECCKM_CSI;
                    break;

                case 5:
                    LOG_WARN("unimplemented: flash");
                    break;

                case 12:
                    LOG_WARN("unimplemented: cursor blinking");
                    break;

                case 25:
                    LOG_WARN("unimplemented: civis");
                    break;

                case 1000:
                    LOG_WARN("unimplemented: report mouse clicks");
                    break;

                case 1002:
                    LOG_WARN("unimplemented: report cell mouse motion");
                    break;

                case 1006:
                    LOG_WARN("unimplemented: SGR mouse");
                    break;

                case 1049:
                    if (term->grid == &term->alt) {
                        term->grid = &term->normal;

                        term->cursor = term->saved_cursor;

                        /* Should these be restored from saved values? */
                        term->scroll_region.start = 0;
                        term->scroll_region.end = term->rows;

                        tll_free(term->alt.damage);
                        tll_free(term->alt.scroll_damage);

                        term_damage_all(term);
                    }
                    break;

                case 2004:
                    term->bracketed_paste = false;
                    break;

                default:
                    LOG_ERR("CSI: 'h' (unset mode): unimplemented param: %d",
                            term->vt.params.v[i].value);
                    abort();
                    break;
                }
            }
            break;
        }

        default:
            LOG_ERR("CSI: intermediate '?': unimplemented final: %c", final);
            abort();
        }

        return true;
    }

    else if (term->vt.intermediates.idx == 1 &&
               term->vt.intermediates.data[0] == '>') {
        switch (final) {
            case 'c': {
                int param = param_get(term, 0, 0);
                if (param != 0) {
                    LOG_ERR(
                        "unimplemented: send device attributes with param = %d",
                        param);
                    return false;
                }

                return write(term->ptmx, "\033[?6c", 5) == 5;
            }

        default:
            LOG_ERR("CSI: intermediate '>': unimplemented final: %c", final);
            abort();
        }
    }

    else {
        LOG_ERR("CSI: unimplemented: intermediates: %.*s",
                (int)term->vt.intermediates.idx,
                term->vt.intermediates.data);
        abort();
    }

    return false;
}