-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
324 lines (275 loc) · 8.7 KB
/
utils.cpp
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
// Copyright Timothy Miller, 1999
#include "gterm.hpp"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define ASSERT_X(x) (assert((x) >= 0 && (x) < width))
#define ASSERT_Y(y) (assert((y) >= 0 && (y) < height))
int GTerm::calc_color(int fg, int bg, int flags)
{
return (flags & 0x0f) | (fg << 4) | (bg << 8);
}
void GTerm::update_changes()
{
int yp, start_x, mx, end_x;
int blank, c, x, y;
constexpr int no_blank = UNDERLINE | INVERSE;
// prevent recursion for scrolls which cause exposures
if (doing_update) return;
doing_update = true;
// first perform scroll-copy
// TODO: understand pending scroll
mx = scroll_bot-scroll_top+1;
if (!(mode_flags & TEXTONLY) && pending_scroll &&
pending_scroll<mx && -pending_scroll<mx) {
if (pending_scroll<0) {
//scroll down
MoveChars(0, scroll_top, 0, scroll_top-pending_scroll,
width, scroll_bot-scroll_top+pending_scroll+1);
} else {
MoveChars(0, scroll_top+pending_scroll, 0, scroll_top,
width, scroll_bot-scroll_top-pending_scroll+1);
}
}
pending_scroll = 0;
// then update characters
for (y=0; y<height; y++) {
if (dirty_startx[y]>=width) continue;
yp = linenumbers[y]*width;
blank = !(mode_flags & TEXTONLY);
start_x = dirty_startx[y];
end_x = dirty_endx[y];
// printf("Line %d dirty from %d to %d\n", y, start_x, end_x);
c = color[yp+start_x];
//TODO:optimize
//use blank to fast clearing
for (x=start_x; x<=end_x; x++) {
if ((text[yp+x]!=32 && text[yp+x]) || (no_blank & FLAG(c))!=0) blank = 0;
if (c != color[yp+x]) {
if (!blank) {
if (inverse_mode)
DrawText(0,7, FLAG(c), start_x,
y, x-start_x, text+yp+start_x);
else
DrawText(FGCOLOR(c),BGCOLOR(c), FLAG(c), start_x,
y, x-start_x, text+yp+start_x);
} else {
ClearChars(inverse_mode?7:BGCOLOR(c), start_x, y, x-start_x, 1);
}
start_x = x;
c = color[yp+x];
blank = !(mode_flags & TEXTONLY);
if ((text[yp+x]!=32 && text[yp+x]) || (no_blank & FLAG(c))!=0) blank = 0;
}
}
if (!blank) {
if (inverse_mode)
DrawText(0,7, FLAG(c), start_x,
y, x-start_x, text+yp+start_x);
else
DrawText(FGCOLOR(c),BGCOLOR(c), FLAG(c), start_x,
y, x-start_x, text+yp+start_x);
} else {
ClearChars(inverse_mode?7:BGCOLOR(c), start_x, y, x-start_x, 1);
}
dirty_endx[y] = 0;
dirty_startx[y] = width;
}
if (!(mode_flags & CURSORINVISIBLE)) {
x = cursor_x;
ASSERT_X(x);
//if (x>=width) x = width-1;
yp = linenumbers[cursor_y] * width + x;
c = color[yp];
DrawCursor(FGCOLOR(c),BGCOLOR(c), FLAG(c), x, cursor_y, text[yp]);
}
doing_update = false;
}
/**
* scroll region in [start_y,end_y] ciclicaly
* num > 0:scroll up
* num < 0:scroll down
* fill scrolled lined with blanks
* FIXME:vt102 specification(IL,DL) requires the filled blanks have the same
* attributes as that of the lines scrolled, which is replaced
* with default attribute in this implementation.
*/
void GTerm::scroll_region(int start_y, int end_y, int num)
{
ASSERT_Y(start_y);
ASSERT_Y(end_y);
assert(end_y >= start_y);
assert(num != 0);
if (cursor_y >= start_y && cursor_y <= end_y) {
// Cause the cursor to be erased after scroll
changed_line(cursor_y, cursor_x, cursor_x);
}
int y, takey, fast_scroll, mx, clr, x, yp, c;
//TODO:optimize
short *temp = new short[height];
unsigned char *temp_sx = new unsigned char[height];
unsigned char *temp_ex = new unsigned char[height];
//if (!num) return;
mx = end_y-start_y+1;
if (num > mx) num = mx;
if (-num > mx) num = -mx;
fast_scroll = (start_y == scroll_top && end_y == scroll_bot &&
!(mode_flags & TEXTONLY));
if (fast_scroll) pending_scroll += num;
memcpy(temp, linenumbers, height * sizeof(linenumbers[0]));
if (fast_scroll) {
memcpy(temp_sx, dirty_startx,height);
memcpy(temp_ex, dirty_endx,height);
}
c = calc_color(fg_color, bg_color, mode_flags);
// move the lines by renumbering where they point to
if (num<mx && -num<mx)
for (y=start_y; y<=end_y; y++) {
takey = y+num;
clr = (takey<start_y) || (takey>end_y);
if (takey<start_y) takey = end_y+1-(start_y-takey);
if (takey>end_y) takey = start_y-1+(takey-end_y);
linenumbers[y] = temp[takey];
if (!fast_scroll || clr) {
dirty_startx[y] = 0;
dirty_endx[y] = width-1;
} else {
dirty_startx[y] = temp_sx[takey];
dirty_endx[y] = temp_ex[takey];
}
if (clr) {
yp = linenumbers[y]*width;
memset(text+yp, ' ', width);
//TODO:optimize
for (x=0; x<width; x++) {
color[yp++] = c;
}
}
}
delete[] temp;
delete[] temp_sx;
delete[] temp_ex;
}
/**
* shift text in [(start_x,y),(end_x,y)]
* num > 0:shift right
* num < 0:shift left
* fill shifted text with blanks
*/
void GTerm::shift_text(int y, int start_x, int end_x, int num)
{
ASSERT_Y(y);
ASSERT_X(start_x);
ASSERT_X(end_x);
assert(num != 0);
int x, yp, mx, c;
// if (!num) return;
yp = linenumbers[y]*width;
mx = end_x-start_x+1;
if (num>mx) num = mx;
if (-num>mx) num = -mx;
if (num<mx && -num<mx) {
if (num<0) {
memmove(text+yp+start_x, text+yp+start_x-num, mx+num);
memmove(color+yp+start_x, color+yp+start_x-num, (mx+num) * sizeof(*color));
} else {
memmove(text+yp+start_x+num, text+yp+start_x, mx-num);
memmove(color+yp+start_x+num, color+yp+start_x, (mx-num) * sizeof(*color));
}
}
if (num<0) {
x = yp+end_x+num+1;
} else {
x = yp+start_x;
}
num = abs(num);
memset(text+x, ' ', num);
c = calc_color(fg_color, bg_color, mode_flags);
//TODO:optimize
while (num--) color[x++] = c;
changed_line(y, start_x, end_x);
}
void GTerm::clear_area(int start_x, int start_y, int end_x, int end_y)
{
ASSERT_X(start_x);
ASSERT_X(end_x);
ASSERT_Y(start_y);
ASSERT_Y(end_y);
int x, y, c, yp, w;
c = calc_color(fg_color, bg_color, mode_flags);
w = end_x - start_x + 1;
assert(w >= 0);
//if (w<1) return;
for (y=start_y; y<=end_y; y++) {
yp = linenumbers[y]*width;
memset(text+yp+start_x, ' ', w);
for (x=start_x; x<=end_x; x++) {
color[yp+x] = c;
}
changed_line(y, start_x, end_x);
}
}
void GTerm::changed_line(int y, int start_x, int end_x)
{
ASSERT_X(start_x);
ASSERT_X(end_x);
ASSERT_Y(y);
if (dirty_startx[y] > start_x) dirty_startx[y] = start_x;
if (dirty_endx[y] < end_x) dirty_endx[y] = end_x;
}
void GTerm::move_cursor(int x, int y)
{
ASSERT_X(x);
ASSERT_Y(y);
changed_line(cursor_y, cursor_x, cursor_x);
cursor_x = x;
cursor_y = y;
if (cursor_x>=width) cursor_x = width-1;
if (cursor_y>=height) cursor_y = height-1;
force_wrap = false;
}
void GTerm::set_mode_flag(int flag)
{
mode_flags |= flag;
ModeChange(mode_flags);
}
void GTerm::clear_mode_flag(int flag)
{
mode_flags &= ~flag;
ModeChange(mode_flags);
}
/*
* Translate a string to the display form. This assumes the font has the
* DEC graphic characters in cells 0-31, and otherwise is ISO-8859-1.
* modified from xterm source:charsets.c:xtermCharSetOut()
*/
void GTerm::translate_charset(unsigned char *buf,unsigned char *ptr)
{
unsigned char *s;
char cs = charset[cur_charset];
for (s = buf; s < ptr; ++s) {
switch (cs) {
case 'A': /* United Kingdom set (or Latin 1) */
#if OPT_XMC_GLITCH
case '?':
#endif
case '1': /* Alternate Character ROM standard characters */
case '2': /* Alternate Character ROM special graphics */
case 'B': /* ASCII set */
break;
case '0': /* special graphics (line drawing) */
if (*s >= 0x5f && *s <= 0x7e) {
#if OPT_WIDE_CHARS
if (screen->utf8_mode)
*s = dec2ucs[chr - 0x5f];
else
#endif
*s = (*s == 0x5f) ? 0x7f : (*s - 0x5f);
}
break;
default: /* any character sets we don't recognize*/
break;
}
}
}