aboutsummaryrefslogtreecommitdiff
path: root/terminal.c
blob: 37cb9bbdbe578cc8b4f6bce6c0b4b5ae96f1a26d (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
#include "terminal.h"

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

#define LOG_MODULE "terminal"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "grid.h"

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

static bool
damage_merge_range(struct terminal *term, const struct damage *dmg)
{
    if (tll_length(term->grid->damage) == 0)
        return false;

    struct damage *old = &tll_back(term->grid->damage);
    if (old->type != dmg->type)
        return false;

    const int start = dmg->range.start;
    const int end = start + dmg->range.length;

    const int prev_start = old->range.start;
    const int prev_end = prev_start + old->range.length;

    if ((start >= prev_start && start <= prev_end) ||
        (end >= prev_start && end <= prev_end) ||
        (start <= prev_start && end >= prev_end))
    {
        /* The two damage ranges intersect */
        int new_start = min(start, prev_start);
        int new_end = max(end, prev_end);

        old->range.start = new_start;
        old->range.length = new_end - new_start;

        return true;
    }

    return false;
}

static void
term_damage_update_or_erase(struct terminal *term, enum damage_type damage_type,
                            int start, int length)
{
#if 1
    if (tll_length(term->grid->damage) > 1024) {
        term_damage_all(term);
        return;
    }
#endif

    struct damage dmg = {
        .type = damage_type,
        .range = {.start = term->grid->offset + start, .length = length},
    };

    if (damage_merge_range(term, &dmg))
        return;

    tll_push_back(term->grid->damage, dmg);
}

void
term_damage_update(struct terminal *term, int start, int length)
{
    assert(start + length <= term->rows * term->cols);
    term_damage_update_or_erase(term, DAMAGE_UPDATE, start, length);
}

void
term_damage_erase(struct terminal *term, int start, int length)
{
    assert(start + length <= term->rows * term->cols);
    term_damage_update_or_erase(term, DAMAGE_ERASE, start, length);
}

void
term_damage_all(struct terminal *term)
{
    tll_free(term->grid->damage);
    tll_free(term->grid->scroll_damage);
    term_damage_update(term, 0, term->rows * term->cols);
}

#if 0
static void
damage_adjust_after_scroll(struct terminal *term, enum damage_type damage_type,
                           struct scroll_region region, int lines)
{
    tll_foreach(term->grid->damage, it) {
#if 0
        if (it->item.range.start < term->grid->offset) {
            int end = it->item.range.start + it->item.range.length;
            if (end >= term->grid->offset) {
                it->item.range.start = term->grid->offset;
                it->item.range.length = end - it->item.range.start;
            } else {
                tll_remove(term->grid->damage, it);
            }
        }
#endif

        int start = it->item.range.start;
        int end = start + it->item.range.length;

        if (start - 
    }
}
#endif

void
term_damage_scroll(struct terminal *term, enum damage_type damage_type,
                   struct scroll_region region, int lines)
{
    //damage_adjust_after_scroll(term, damage_type, region, lines);
    if (damage_type == DAMAGE_SCROLL) {
        tll_foreach(term->grid->damage, it) {
            int start = it->item.range.start;
            int length = it->item.range.length;

            if (start < term->grid->offset) {
                int end = start + length;
                if (end >= term->grid->offset) {
                    it->item.range.start = term->grid->offset;
                    it->item.range.length = end - it->item.range.start;
                } else
                    tll_remove(term->grid->damage, it);
            } else
                break;
        }
    }

    if (tll_length(term->grid->scroll_damage) > 0) {
        struct damage *dmg = &tll_back(term->grid->scroll_damage);

        if (dmg->type == damage_type &&
            dmg->scroll.region.start == region.start &&
            dmg->scroll.region.end == region.end)
        {
            dmg->scroll.lines += lines;
            return;
        }
    }
    struct damage dmg = {
        .type = damage_type,
        .scroll = {.region = region, .lines = lines},
    };
    tll_push_back(term->grid->scroll_damage, dmg);
}

void
term_erase(struct terminal *term, int start, int end)
{
    LOG_DBG("erase: %d-%d", start, end);
    assert(end >= start);
    assert(end <= term->rows * term->cols);

    if (term->vt.attrs.have_background) {
        int erase_start = start;
        int left = end - erase_start;

        while (left > 0) {
            int len = left;
            struct cell *cells = grid_get_range(term->grid, erase_start, &len);

            memset(cells, 0, len * sizeof(cells[0]));

            for (int i = 0; i < len; i++) {
                cells[i].attrs.background = term->vt.attrs.background;
                cells[i].attrs.have_background = true;
            }

            erase_start += len;
            left -= len;
        }

        term_damage_update(term, start, end - start);
    } else {
        grid_memset(term->grid, start, 0, end - start);
        term_damage_erase(term, start, end - start);
    }
}

int
term_cursor_linear(const struct terminal *term, int row, int col)
{
    return row * term->cols + col;
}

void
term_cursor_to(struct terminal *term, int row, int col)
{
    assert(row < term->rows);
    assert(col < term->cols);

    int new_linear = row * term->cols + col;
    assert(new_linear < term->rows * term->cols);

    term->print_needs_wrap = false;

    term->cursor.linear = new_linear;
    term->cursor.col = col;
    term->cursor.row = row;

    int len = term->cols;
    term->grid->cur_line = grid_get_range(
        term->grid, term->cursor.linear - col, &len);

    assert(len == term->cols);
}

void
term_cursor_left(struct terminal *term, int count)
{
    int move_amount = min(term->cursor.col, count);
    term->cursor.linear -= move_amount;
    term->cursor.col -= move_amount;
    term->print_needs_wrap = false;
}

void
term_cursor_right(struct terminal *term, int count)
{
    int move_amount = min(term->cols - term->cursor.col - 1, count);
    term->cursor.linear += move_amount;
    term->cursor.col += move_amount;
    term->print_needs_wrap = false;
}

void
term_cursor_up(struct terminal *term, int count)
{
    int move_amount = min(term->cursor.row, count);
    term_cursor_to(term, term->cursor.row - move_amount, term->cursor.col);
}

void
term_cursor_down(struct terminal *term, int count)
{
    int move_amount = min(term->rows - term->cursor.row - 1, count);
    term_cursor_to(term, term->cursor.row + move_amount, term->cursor.col);
}

void
term_scroll_partial(struct terminal *term, struct scroll_region region, int rows)
{
    LOG_DBG("scroll: %d rows", rows);

    if (region.start > 0) {
        /* TODO: check if it's worth memoving the scroll area instead,
         * under certain circumstances */

        grid_memmove(term->grid, rows * term->cols, 0, region.start * term->cols);

        tll_foreach(term->grid->damage, it) {
            int start = it->item.range.start - term->grid->offset;
            int end __attribute__((unused))  = start + it->item.range.length;

            if (start < region.start * term->cols) {
                assert(end <= region.start * term->cols);
                it->item.range.start += rows * term->cols;
            }
        }
    }

    if (region.end < term->rows) {
        /* Copy scrolled-up bottom region to new bottom region */
        grid_memmove(
            term->grid,
            (region.end + rows) * term->cols,
            region.end * term->cols,
            (term->rows - region.end) * term->cols);

        tll_foreach(term->grid->damage, it) {
            int start = it->item.range.start - term->grid->offset;
            int end = start + it->item.range.length;

            if (end > region.end * term->cols) {
                assert(start >= region.end * term->cols);
                it->item.range.start += rows * term->cols;
            }
        }
    }

    /* Offset grid origin */
    term->grid->offset += rows * term->cols;

    /* Clear scrolled-in lines */
    grid_memset(
        term->grid,
        max(0, region.end - rows) * term->cols,
        0,
        min(rows, term->rows) * term->cols);

    term_damage_scroll(term, DAMAGE_SCROLL, region, rows);

    int len = term->cols;
    term->grid->cur_line = grid_get_range(
        term->grid, term->cursor.linear - term->cursor.col, &len);

    assert(len == term->cols);
}

void
term_scroll(struct terminal *term, int rows)
{
    term_scroll_partial(term, term->scroll_region, rows);
}

void
term_scroll_reverse_partial(struct terminal *term,
                            struct scroll_region region, int rows)
{
    if (region.end < term->rows) {
        grid_memmove(
            term->grid,
            (region.end - rows) * term->cols,
            region.end * term->cols,
            (term->rows - region.end) * term->cols);

        tll_foreach(term->grid->damage, it) {
            int start = it->item.range.start - term->grid->offset;
            int end = start + it->item.range.length;

            if (end > region.end * term->cols) {
                assert(start >= region.end * term->cols);
                it->item.range.start -= rows * term->cols;
            }
        }

    }

    if (region.start > 0) {
        grid_memmove(
            term->grid, -rows * term->cols, 0, region.start * term->cols);

        tll_foreach(term->grid->damage, it) {
            int start = it->item.range.start - term->grid->offset;
            int end __attribute__((unused))  = start + it->item.range.length;

            if (start < region.start * term->cols) {
                if (end > region.start * term->cols) {
                    LOG_ERR("region.start = %d, rows = %d, damage.start = %d, damage.end = %d (%s)",
                            region.start, rows, start, end, it->item.type == DAMAGE_UPDATE ? "UPDATE" : "ERASE");
                    abort();
                }
                assert(end <= region.start * term->cols);
                it->item.range.start -= rows * term->cols;
            }
        }
    }

    term->grid->offset -= rows * term->cols;

    grid_memset(term->grid, region.start * term->cols, 0, rows * term->cols);

    term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);

    int len = term->cols;
    term->grid->cur_line = grid_get_range(
        term->grid, term->cursor.linear - term->cursor.col, &len);

    assert(len == term->cols);
}

void
term_scroll_reverse(struct terminal *term, int rows)
{
    term_scroll_reverse_partial(term, term->scroll_region, rows);
}

#include <linux/input-event-codes.h>

static int
linux_mouse_button_to_x(int button)
{
    switch (button) {
    case BTN_LEFT:    return 1;
    case BTN_RIGHT:   return 3;
    case BTN_MIDDLE:  return 2;
    case BTN_SIDE:    return 8;
    case BTN_EXTRA:   return 9;
    case BTN_FORWARD: return 4;
    case BTN_BACK:    return 5;
    case BTN_TASK:    return -1;  /* TODO: ??? */

    default:
        LOG_WARN("unrecognized mouse button: %d (0x%x)", button, button);
        return -1;
    }
}

static int
encode_xbutton(int xbutton)
{
    switch (xbutton) {
    case 1: case 2: case 3:
        return xbutton - 1;

    case 4: case 5:
        /* Like button 1 and 2, but with 64 added */
        return xbutton - 4 + 64;

    case 6: case 7:
        /* Same as 4 and 5. Note: the offset should be something else? */
        return xbutton - 6 + 64;

    case 8: case 9: case 10: case 11:
        /* Similar to 4 and 5, but adding 128 instead of 64 */
        return xbutton - 8 + 128;

    default:
        LOG_ERR("cannot encode X mouse button: %d", xbutton);
        return -1;
    }
}

static void
report_mouse_click(struct terminal *term, int button, int row, int col,
                   bool release, bool shift, bool alt, bool ctrl)
{
    /* Map libevent button event code to X button number */
    int xbutton = linux_mouse_button_to_x(button);
    if (xbutton == -1)
        return;

    if (release && (xbutton == 4 || xbutton == 5)) {
        /* No button release events for scroll buttons */
        return;
    }

    int encoded = release ? 3 : encode_xbutton(xbutton);
    if (encoded == -1)
        return;

    encoded += (shift ? 4 : 0) + (alt ? 8 : 0) + (ctrl ? 16 : 0);

    char response[16];
    snprintf(response, sizeof(response), "\033[M%c%c%c",
             32 + encoded, 32 + col + 1, 32 + row + 1);
    write(term->ptmx, response, strlen(response));
}

void
term_mouse_down(struct terminal *term, int button, int row, int col,
                bool shift, bool alt, bool ctrl)
{
    switch (term->mouse_tracking) {
    case MOUSE_NONE:
        break;

    case MOUSE_X10:
    case MOUSE_CLICK:
    case MOUSE_DRAG:
    case MOUSE_MOTION:
        report_mouse_click(term, button, row, col, false, shift, alt, ctrl);
        break;
    }
}

void
term_mouse_up(struct terminal *term, int button, int row, int col,
              bool shift, bool alt, bool ctrl)
{
    switch (term->mouse_tracking) {
    case MOUSE_NONE:
        break;

    case MOUSE_X10:
    case MOUSE_CLICK:
    case MOUSE_DRAG:
    case MOUSE_MOTION:
        report_mouse_click(term, button, row, col, true, shift, alt, ctrl);
        break;
    }
}