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
|
#ifndef TERM_INTERNAL_H
#define TERM_INTERNAL_H
#include "term.h"
#include "cell.h"
#include "csi.h"
#include "gstore.h"
#include "hist.h"
#include "hist_view.h"
#include "palette.h"
#include "pen.h"
#include "text_run.h"
#include "compat.h"
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#define HASH64_BASIS UINT64_C(0x9E3779B97F4A7C15)
#define KNUTH_HASH_MULT UINT32_C(2654435761)
static inline uint64_t hash64_step(uint64_t hash, uint32_t value)
{
hash ^= value;
hash *= HASH64_BASIS;
return hash ^ (hash >> 29);
}
static inline uint64_t hash64_finish(uint64_t hash)
{
hash ^= hash >> 32;
hash *= UINT64_C(0xD6E8FEB86659FD93);
hash ^= hash >> 32;
return hash;
}
#define KEYBOARD_STACK_SIZE 32
struct keyboard_mode {
unsigned flags, saved[KEYBOARD_STACK_SIZE], depth;
};
/* What DECSC saves, as in xterm: the cursor, the pen, origin mode, the
* pending wrap, and the character sets. */
struct term_saved_cursor {
int x, y;
uint32_t fg, bg, ul;
char charsets[2];
uint8_t underline;
uint8_t charset_active;
bool bold : 1;
bool faint : 1;
bool italic : 1;
bool strike : 1;
bool overline : 1;
bool inverse : 1;
bool origin : 1;
bool pending_wrap : 1;
};
/*
* A row stores cells, text, or default-pen ASCII. A text row uses the history
* format. An ASCII row stores only its pointer and length. A screen read or
* edit sets its text fields first. Other changes settle text into cells.
*/
enum { ROW_CELLS, ROW_TEXT, ROW_ASCII };
enum { RF_WRAPPED = 1, RF_WIDE = 4, RF_CLUSTER = 8, RF_TAIL_PADDED = 16 };
struct srow {
struct cell *cells; /* stride >= cols, from a per-screen slab */
uint8_t *ascii;
struct text_row text; /* TEXT: the content; len == text.cells, or cols with a wrap pad */
uint64_t uid; /* new id when the row scrolls in or clears */
uint32_t version; /* bumped on every change */
uint16_t len;
uint8_t kind, flags;
};
struct screen {
struct srow *slot;
struct cell *cells_slab;
uint8_t *text_slab;
size_t cells_cap, text_cap;
/* Without history: the line of row 0, counted as rows leave the top. */
uint64_t top_id;
int rows, cols, top;
int stride;
/* Capacity of the buffers above. A resize reuses them. */
int slot_cap;
uint32_t top_seg;
};
struct term_sel_point {
uint64_t line_id;
uint32_t cell_off;
};
/* A buffer that only grows, so work that repeats does not allocate. */
struct grow_buf {
void *data;
size_t cap;
};
/* What a resize needs besides the screens. It keeps its buffers, so a
* resize allocates nothing once the terminal has seen its sizes. */
struct resize_scratch {
struct grow_buf row, rows, cells, line, pull, rest, lens;
};
struct term {
_Atomic uint64_t seq;
_Atomic uint64_t writer_tid;
uint32_t write_depth;
int cols;
int rows;
int cursor_x;
int cursor_y;
int scroll_top, scroll_bottom;
uint32_t cur_fg;
uint32_t cur_bg;
uint32_t cur_ul;
uint32_t last_char;
uint32_t utf8_state;
uint32_t utf8_codep;
struct width_cache text_width;
enum { ST_GROUND, ST_ESC, ST_CSI, ST_OSC, ST_DCS, ST_STR_ESC, ST_CHARSET,
ST_DCS_ESC, ST_IGNORE, ST_IGNORE_ESC, ST_DOCS, ST_DECALN } state;
struct csi_parser csi_parser;
uint16_t cur_link;
char charsets[2];
uint8_t underline;
uint8_t decsace;
char charset_designation;
bool graphics;
bool bold : 1;
bool faint : 1;
bool italic : 1;
bool strike : 1;
bool overline : 1;
bool inverse : 1;
bool pending_wrap : 1;
bool autowrap : 1;
bool insert_mode : 1;
bool origin_mode : 1;
bool reverse_video : 1;
bool synchronized_output : 1;
bool alternate : 1;
bool application_cursor : 1;
bool application_keypad : 1;
bool lr_margin_mode : 1;
bool backarrow_sends_bs : 1;
bool focus_events : 1;
bool cursor_visible : 1;
bool cursor_blink : 1;
bool default_cursor_blink : 1;
bool selection : 1;
bool bracketed_paste : 1;
bool lnm : 1;
bool transport_newline : 1;
bool str_truncated : 1;
bool has_prompt : 1;
struct screen scr;
struct screen alt_scr;
struct screen spare; /* the buffers a resize builds into */
struct resize_scratch resize;
struct grow_buf ascii_ends;
struct grow_buf ascii_tail;
struct term_saved_cursor normal_saved_cursor;
struct term_saved_cursor normal_saved_decsc;
int normal_cols, normal_rows;
bool alt_allocated;
bool normal_cursor_visible;
uint8_t normal_cursor_style;
bool normal_cursor_blink;
struct hist hist;
struct hist_view view;
uint64_t next_row_uid;
uint64_t full_gen;
int prompt_y;
int left_margin, right_margin;
struct term_sel_point sel_start, sel_end;
struct term_sel_point sel_anchor_first, sel_anchor_last;
enum term_select_unit select_unit;
int mouse_mode, mouse_encoding;
enum term_cursor_style cursor_style;
enum term_cursor_style default_cursor_style;
int charset_active;
int osc_len;
int dcs_len;
int sb_capacity;
struct keyboard_mode keyboard[2];
/* The DECSC slot. Cold: it stays out of the hot fields above. */
struct term_saved_cursor saved;
uint8_t *tabs;
struct cell *view_row_scratch;
int tabs_cap;
int view_scratch_cap;
bool custom_tabs;
char osc_buf[256];
char tcap_name[64];
char *osc_heap;
size_t osc_heap_cap;
char *dcs_buf;
size_t dcs_cap;
void *links;
char *cwd;
char *pointer_shape;
struct term_callbacks cb;
struct palette palette;
struct gstore *gstore;
/* Unique for the life of the process. A snapshot compares it, not the
* address, because a new terminal can reuse a freed one's memory. */
uint64_t serial;
};
static inline void reset_sgr(struct term *t)
{
t->cur_fg = TERM_COLOR_FG;
t->cur_bg = TERM_COLOR_BG;
t->bold = false;
t->faint = false;
t->italic = false;
t->underline = 0;
t->strike = false;
t->overline = false;
t->inverse = false;
t->cur_ul = 0;
}
/* The DECSC slot before any save: home, the default pen, and ASCII. */
static inline struct term_saved_cursor term_saved_cursor_initial(void)
{
return (struct term_saved_cursor){
.fg = TERM_COLOR_FG, .bg = TERM_COLOR_BG, .charsets = { 'B', 'B' },
};
}
static inline struct term_saved_cursor term_cursor_save(const struct term *t)
{
return (struct term_saved_cursor){
.x = t->cursor_x, .y = t->cursor_y,
.fg = t->cur_fg, .bg = t->cur_bg, .ul = t->cur_ul,
.charsets = { t->charsets[0], t->charsets[1] },
.underline = t->underline,
.charset_active = (uint8_t)t->charset_active,
.bold = t->bold, .faint = t->faint, .italic = t->italic,
.strike = t->strike, .overline = t->overline, .inverse = t->inverse,
.origin = t->origin_mode, .pending_wrap = t->pending_wrap,
};
}
/* Take the pen, origin mode, and character sets from s. The caller places
* the cursor. */
static inline void term_cursor_apply(struct term *t, const struct term_saved_cursor *s)
{
t->cur_fg = s->fg;
t->cur_bg = s->bg;
t->cur_ul = s->ul;
t->underline = s->underline;
t->bold = s->bold;
t->faint = s->faint;
t->italic = s->italic;
t->strike = s->strike;
t->overline = s->overline;
t->inverse = s->inverse;
t->origin_mode = s->origin;
t->charsets[0] = s->charsets[0];
t->charsets[1] = s->charsets[1];
t->charset_active = s->charset_active;
t->graphics = t->charsets[t->charset_active] == '0';
}
static inline void send_response(struct term *t, const char *str)
{
if (t && t->cb.reply)
t->cb.reply(t->cb.ud, str, strlen(str));
}
static inline int term_hex_nib(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
static inline bool term_lnm(const struct term *t)
{
return t->lnm || t->transport_newline;
}
static inline bool mode_is_mouse(int m)
{
return m == 9 || (m >= 1000 && m <= 1003);
}
static inline bool mode_is_mouse_enc(int m)
{
return m == 1005 || m == 1006 || m == 1015 || m == 1016;
}
static const uint16_t term_ul_flags[] = {
0, CELL_UNDERLINE, CELL_DOUBLE_UNDERLINE,
CELL_CURLY_UNDERLINE, CELL_DOTTED_UNDERLINE,
CELL_DASHED_UNDERLINE,
};
static inline uint16_t term_current_flags(const struct term *t)
{
uint16_t f = 0;
if (t->bold) f |= CELL_BOLD;
if (t->faint) f |= CELL_FAINT;
if (t->italic) f |= CELL_ITALIC;
if (t->underline < sizeof(term_ul_flags) / sizeof(term_ul_flags[0]))
f |= term_ul_flags[t->underline];
if (t->strike) f |= CELL_STRIKE;
if (t->overline) f |= CELL_OVERLINE;
if (t->cur_link) f |= CELL_LINK;
return f;
}
static inline uint32_t term_flags_pad(const struct term *t)
{
return term_current_flags(t) | ((uint32_t)t->cur_link << 16);
}
static inline struct text_pen term_text_pen(const struct term *t)
{
return (struct text_pen){
.fg = TERM_EXPECT(t->inverse, 0) ? t->cur_bg : t->cur_fg,
.bg = TERM_EXPECT(t->inverse, 0) ? t->cur_fg : t->cur_bg,
.sl = term_flags_pad(t),
};
}
static inline void term_tabs_clear(struct term *t)
{
memset(t->tabs, 0, (size_t)t->tabs_cap);
t->custom_tabs = true;
}
static inline void term_tabs_default(struct term *t)
{
for (int c = 8; c < t->tabs_cap; c += 8) t->tabs[c] = 1;
t->custom_tabs = false;
}
static inline bool term_str_was_truncated(struct term *t)
{
bool truncated = t->str_truncated;
t->str_truncated = false;
return truncated;
}
/* Every thread has its own anchor, so its address names the thread. */
extern _Thread_local char term_thread_anchor;
static inline void term_write_begin(struct term *t)
{
if (!t) return;
uint64_t self = (uint64_t)(uintptr_t)&term_thread_anchor;
if (atomic_load_explicit(&t->writer_tid, memory_order_relaxed) == self) {
t->write_depth++;
return;
}
uint64_t s;
for (;;) {
s = atomic_load_explicit(&t->seq, memory_order_relaxed);
if (s & 1) {
term_cpu_relax();
continue;
}
if (atomic_compare_exchange_weak_explicit(&t->seq, &s, s + 1,
memory_order_acquire,
memory_order_relaxed))
break;
}
atomic_store_explicit(&t->writer_tid, self, memory_order_relaxed);
t->write_depth = 1;
}
static inline void term_write_end(struct term *t)
{
if (!t) return;
if (--t->write_depth == 0) {
atomic_store_explicit(&t->writer_tid, 0, memory_order_relaxed);
atomic_fetch_add_explicit(&t->seq, 1, memory_order_release);
}
}
#endif
|