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
|
#include "terminal.h"
#include <string.h>
#include <assert.h>
#define LOG_MODULE "terminal"
#define LOG_ENABLE_DBG 1
#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 (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);
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;
}
void
term_cursor_left(struct terminal *term, int count)
{
int move_amount = min(term->cursor.col, count);
term_cursor_to(term, term->cursor.row, term->cursor.col - move_amount);
}
void
term_cursor_right(struct terminal *term, int count)
{
int move_amount = min(term->cols - term->cursor.col - 1, count);
term_cursor_to(term, term->cursor.row, term->cursor.col + move_amount);
}
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) {
assert(end <= region.start);
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) {
assert(start >= region.end);
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, rows * term->cols);
term_damage_scroll(term, DAMAGE_SCROLL, region, rows);
}
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 (rows >= region.end - region.start) {
assert(false && "todo");
return;
}
#if 0
int cell_dst = (region.start + rows) * term->cols;
int cell_src = (region.start + 0) * term->cols;
int cell_count = (region.end - region.start - rows) * term->cols;
LOG_DBG("moving %d lines from row %d to row %d", cell_count / term->cols,
cell_src / term->cols, cell_dst / term->cols);
const int bytes = cell_count * sizeof(term->grid->cells[0]);
memmove(
&term->grid->cells[cell_dst], &term->grid->cells[cell_src],
bytes);
memset(&term->grid->cells[cell_src], 0,
rows * term->cols * sizeof(term->grid->cells[0]));
term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);
#else
/* TODO */
assert(false);
assert(region.start == 0 && region.end == 0);
assert(rows < term->rows);
term->grid->offset -= rows * term->cols;
term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);
#endif
}
void
term_scroll_reverse(struct terminal *term, int rows)
{
term_scroll_reverse_partial(term, term->scroll_region, rows);
}
|