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
|
#include "terminal.h"
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/timerfd.h>
#include <linux/input-event-codes.h>
#define LOG_MODULE "terminal"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "grid.h"
#include "render.h"
#include "vt.h"
#include "selection.h"
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
void
term_reset(struct terminal *term, bool hard)
{
term->cursor_keys_mode = CURSOR_KEYS_NORMAL;
term->keypad_keys_mode = KEYPAD_NUMERICAL;
term->reverse = false;
term->hide_cursor = false;
term->auto_margin = true;
term->insert_mode = false;
term->bracketed_paste = false;
term->focus_events = false;
term->mouse_tracking = MOUSE_NONE;
term->mouse_reporting = MOUSE_NORMAL;
term->selected_charset = 0;
term->charset[0] = CHARSET_ASCII;
term->charset[1] = CHARSET_ASCII;
term->charset[2] = CHARSET_ASCII;
term->charset[3] = CHARSET_ASCII;
tll_free_and_free(term->window_title_stack, free);
free(term->window_title);
term->window_title = strdup("foot");
term->scroll_region.start = 0;
term->scroll_region.end = term->rows;
free(term->vt.osc.data);
memset(&term->vt, 0, sizeof(term->vt));
term->vt.state = 1; /* GROUND */
if (term->grid == &term->alt) {
term->grid = &term->normal;
term_restore_cursor(term);
selection_cancel(term);
}
if (!hard)
return;
term->flash.active = false;
term->blink.active = false;
term->blink.state = BLINK_ON;
term->colors.fg = term->colors.default_fg;
term->colors.bg = term->colors.default_bg;
for (size_t i = 0; i < 256; i++)
term->colors.table[i] = term->colors.default_table[i];
term->print_needs_wrap = false;
term->cursor = (struct coord){0, 0};
term->saved_cursor = (struct coord){0, 0};
term->alt_saved_cursor = (struct coord){0, 0};
term->cursor_style = term->default_cursor_style;
term->cursor_blinking = false;
term->cursor_color.text = term->default_cursor_color.text;
term->cursor_color.cursor = term->default_cursor_color.cursor;
selection_cancel(term);
term->normal.offset = term->normal.view = 0;
term->alt.offset = term->alt.view = 0;
for (size_t i = 0; i < term->rows; i++) {
memset(term->normal.rows[i]->cells, 0, term->cols * sizeof(struct cell));
memset(term->alt.rows[i]->cells, 0, term->cols * sizeof(struct cell));
}
for (size_t i = term->rows; i < term->normal.num_rows; i++) {
grid_row_free(term->normal.rows[i]);
term->normal.rows[i] = NULL;
}
for (size_t i = term->rows; i < term->alt.num_rows; i++) {
grid_row_free(term->alt.rows[i]);
term->alt.rows[i] = NULL;
}
term->normal.cur_row = term->normal.rows[0];
term->alt.cur_row = term->alt.rows[0];
tll_free(term->normal.damage);
tll_free(term->normal.scroll_damage);
tll_free(term->alt.damage);
tll_free(term->alt.scroll_damage);
term->render.last_cursor.cell = NULL;
term->render.was_flashing = false;
term_damage_all(term);
}
void
term_damage_rows(struct terminal *term, int start, int end)
{
assert(start <= end);
for (int r = start; r <= end; r++) {
struct row *row = grid_row(term->grid, r);
row->dirty = true;
for (int c = 0; c < term->grid->num_cols; c++)
row->cells[c].attrs.clean = 0;
}
}
void
term_damage_rows_in_view(struct terminal *term, int start, int end)
{
assert(start <= end);
for (int r = start; r <= end; r++) {
struct row *row = grid_row_in_view(term->grid, r);
row->dirty = true;
for (int c = 0; c < term->grid->num_cols; c++)
row->cells[c].attrs.clean = 0;
}
}
void
term_damage_all(struct terminal *term)
{
term_damage_rows(term, 0, term->rows - 1);
}
void
term_damage_view(struct terminal *term)
{
term_damage_rows_in_view(term, 0, term->rows - 1);
}
void
term_damage_scroll(struct terminal *term, enum damage_type damage_type,
struct scroll_region region, int lines)
{
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);
}
static inline void
erase_cell_range(struct terminal *term, struct row *row, int start, int end)
{
assert(start < term->cols);
assert(end < term->cols);
if (unlikely(term->vt.attrs.have_bg)) {
for (int col = start; col <= end; col++) {
struct cell *c = &row->cells[col];
c->wc = 0;
c->attrs = (struct attributes){.have_bg = 1, .bg = term->vt.attrs.bg};
}
} else
memset(&row->cells[start], 0, (end - start + 1) * sizeof(row->cells[0]));
row->dirty = true;
}
static inline void
erase_line(struct terminal *term, struct row *row)
{
erase_cell_range(term, row, 0, term->cols - 1);
}
void
term_erase(struct terminal *term, const struct coord *start, const struct coord *end)
{
assert(start->row <= end->row);
assert(start->col <= end->col || start->row < end->row);
if (start->row == end->row) {
struct row *row = grid_row(term->grid, start->row);
erase_cell_range(term, row, start->col, end->col);
return;
}
assert(end->row > start->row);
erase_cell_range(
term, grid_row(term->grid, start->row), start->col, term->cols - 1);
for (int r = start->row + 1; r < end->row; r++)
erase_line(term, grid_row(term->grid, r));
erase_cell_range(term, grid_row(term->grid, end->row), 0, end->col);
}
void
term_cursor_to(struct terminal *term, int row, int col)
{
assert(row < term->rows);
assert(col < term->cols);
term->print_needs_wrap = false;
term->cursor.col = col;
term->cursor.row = row;
term->grid->cur_row = grid_row(term->grid, row);
}
void
term_cursor_left(struct terminal *term, int count)
{
int move_amount = min(term->cursor.col, count);
term->cursor.col -= move_amount;
assert(term->cursor.col >= 0);
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.col += move_amount;
assert(term->cursor.col < term->cols);
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: rows=%d, region.start=%d, region.end=%d",
rows, region.start, region.end);
#if 0
if (rows > region.end - region.start) {
/* For now, clamp */
rows = region.end - region.start;
}
#endif
bool view_follows = term->grid->view == term->grid->offset;
term->grid->offset += rows;
term->grid->offset &= term->grid->num_rows - 1;
if (view_follows)
term->grid->view = term->grid->offset;
/* Top non-scrolling region. */
for (int i = region.start - 1; i >= 0; i--)
grid_swap_row(term->grid, i - rows, i, false);
/* Bottom non-scrolling region */
for (int i = term->rows - 1; i >= region.end; i--)
grid_swap_row(term->grid, i - rows, i, false);
/* Erase scrolled in lines */
for (int r = max(region.end - rows, region.start); r < region.end; r++) {
erase_line(term, grid_row_and_alloc(term->grid, r));
if (selection_on_row_in_view(term, r))
selection_cancel(term);
}
term_damage_scroll(term, DAMAGE_SCROLL, region, rows);
term->grid->cur_row = grid_row(term->grid, term->cursor.row);
}
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)
{
LOG_DBG("scroll reverse: rows=%d, region.start=%d, region.end=%d",
rows, region.start, region.end);
#if 0
if (rows > region.end - region.start) {
/* For now, clamp */
rows = region.end - region.start;
}
#endif
bool view_follows = term->grid->view == term->grid->offset;
term->grid->offset -= rows;
while (term->grid->offset < 0)
term->grid->offset += term->grid->num_rows;
term->grid->offset &= term->grid->num_rows - 1;
assert(term->grid->offset >= 0);
assert(term->grid->offset < term->grid->num_rows);
if (view_follows)
term->grid->view = term->grid->offset;
/* Bottom non-scrolling region */
for (int i = region.end + rows; i < term->rows + rows; i++)
grid_swap_row(term->grid, i, i - rows, false);
/* Top non-scrolling region */
for (int i = 0 + rows; i < region.start + rows; i++)
grid_swap_row(term->grid, i, i - rows, false);
/* Erase scrolled in lines */
for (int r = region.start; r < min(region.start + rows, region.end); r++) {
erase_line(term, grid_row_and_alloc(term->grid, r));
if (selection_on_row_in_view(term, r))
selection_cancel(term);
}
term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);
term->grid->cur_row = grid_row(term->grid, term->cursor.row);
}
void
term_scroll_reverse(struct terminal *term, int rows)
{
term_scroll_reverse_partial(term, term->scroll_region, rows);
}
void
term_linefeed(struct terminal *term)
{
if (term->cursor.row == term->scroll_region.end - 1)
term_scroll(term, 1);
else
term_cursor_down(term, 1);
}
void
term_reverse_index(struct terminal *term)
{
if (term->cursor.row == term->scroll_region.start)
term_scroll_reverse(term, 1);
else
term_cursor_up(term, 1);
}
void
term_reset_view(struct terminal *term)
{
if (term->grid->view == term->grid->offset)
return;
term->grid->view = term->grid->offset;
term_damage_view(term);
}
void
term_restore_cursor(struct terminal *term)
{
int row = min(term->saved_cursor.row, term->rows - 1);
int col = min(term->saved_cursor.col, term->cols - 1);
term_cursor_to(term, row, col);
}
void
term_focus_in(struct terminal *term)
{
if (!term->focus_events)
return;
vt_to_slave(term, "\033[I", 3);
}
void
term_focus_out(struct terminal *term)
{
if (!term->focus_events)
return;
vt_to_slave(term, "\033[O", 3);
}
static int
linux_mouse_button_to_x(int button)
{
switch (button) {
case BTN_LEFT: return 1;
case BTN_MIDDLE: return 2;
case BTN_RIGHT: return 3;
case BTN_BACK: return 4;
case BTN_FORWARD: return 5;
case BTN_SIDE: return 8;
case BTN_EXTRA: return 9;
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 encoded_button, int row, int col,
bool release)
{
char response[128];
switch (term->mouse_reporting) {
case MOUSE_NORMAL:
snprintf(response, sizeof(response), "\033[M%c%c%c",
32 + (release ? 3 : encoded_button), 32 + col + 1, 32 + row + 1);
break;
case MOUSE_SGR:
snprintf(response, sizeof(response), "\033[<%d;%d;%d%c",
encoded_button, col + 1, row + 1, release ? 'm' : 'M');
break;
case MOUSE_URXVT:
snprintf(response, sizeof(response), "\033[%d;%d;%dM",
32 + (release ? 3 : encoded_button), col + 1, row + 1);
break;
case MOUSE_UTF8:
/* Unimplemented */
return;
}
vt_to_slave(term, response, strlen(response));
}
static void
report_mouse_motion(struct terminal *term, int encoded_button, int row, int col)
{
report_mouse_click(term, encoded_button, row, col, false);
}
void
term_mouse_down(struct terminal *term, int button, int row, int col,
bool shift, bool alt, bool ctrl)
{
if (term->kbd.shift) {
/* "raw" mouse mode */
return;
}
/* Map libevent button event code to X button number */
int xbutton = linux_mouse_button_to_x(button);
if (xbutton == -1)
return;
int encoded = encode_xbutton(xbutton);
if (encoded == -1)
return;
encoded += (shift ? 4 : 0) + (alt ? 8 : 0) + (ctrl ? 16 : 0);
switch (term->mouse_tracking) {
case MOUSE_NONE:
break;
case MOUSE_X10:
case MOUSE_CLICK:
case MOUSE_DRAG:
case MOUSE_MOTION:
report_mouse_click(term, encoded, row, col, false);
break;
}
}
void
term_mouse_up(struct terminal *term, int button, int row, int col,
bool shift, bool alt, bool ctrl)
{
if (term->kbd.shift) {
/* "raw" mouse mode */
return;
}
/* Map libevent button event code to X button number */
int xbutton = linux_mouse_button_to_x(button);
if (xbutton == -1)
return;
if (xbutton == 4 || xbutton == 5) {
/* No release events for scroll buttons */
return;
}
int encoded = encode_xbutton(xbutton);
if (encoded == -1)
return;
encoded += (shift ? 4 : 0) + (alt ? 8 : 0) + (ctrl ? 16 : 0);
switch (term->mouse_tracking) {
case MOUSE_NONE:
break;
case MOUSE_X10:
case MOUSE_CLICK:
case MOUSE_DRAG:
case MOUSE_MOTION:
report_mouse_click(term, encoded, row, col, true);
break;
}
}
void
term_mouse_motion(struct terminal *term, int button, int row, int col,
bool shift, bool alt, bool ctrl)
{
if (term->kbd.shift) {
/* "raw" mouse mode */
return;
}
int encoded = 0;
if (button != 0) {
/* Map libevent button event code to X button number */
int xbutton = linux_mouse_button_to_x(button);
if (xbutton == -1)
return;
encoded = encode_xbutton(xbutton);
if (encoded == -1)
return;
} else
encoded = 3; /* "released" */
encoded += 32; /* Motion event */
encoded += (shift ? 4 : 0) + (alt ? 8 : 0) + (ctrl ? 16 : 0);
switch (term->mouse_tracking) {
case MOUSE_NONE:
case MOUSE_X10:
case MOUSE_CLICK:
return;
case MOUSE_DRAG:
if (button == 0)
return;
/* FALLTHROUGH */
case MOUSE_MOTION:
report_mouse_motion(term, encoded, row, col);
break;
}
}
void
term_set_window_title(struct terminal *term, const char *title)
{
free(term->window_title);
term->window_title = strdup(title);
render_set_title(term, term->window_title);
}
void
term_flash(struct terminal *term, unsigned duration_ms)
{
LOG_DBG("FLASH for %ums", duration_ms);
struct itimerspec alarm = {
.it_value = {.tv_sec = 0, .tv_nsec = duration_ms * 1000000},
};
if (timerfd_settime(term->flash.fd, 0, &alarm, NULL) < 0)
LOG_ERRNO("failed to arm flash timer");
else {
term->flash.active = true;
}
}
|