-
Notifications
You must be signed in to change notification settings - Fork 2
/
vt.c
557 lines (487 loc) · 11.7 KB
/
vt.c
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
/*
* Funktionen, die beide Emulatoren benutzen.
*
* This file needs to be rewritten. Many functions can be
* optimized a lot.
*/
#include "vt.h"
#include "isotost.h"
#include "ansicol.h"
/*
* paint(v, c): put character 'c' at the current (x, y) coordinates
* of window v. If insert mode is on, this involves moving all the
* other characters one space to the right, first.
*/
void paint(TEXTWIN *v, unsigned int c)
{
int i;
int line = v->cy;
unsigned long use_attribute = ~CLINEDRAW & v->curr_cattr;
char charset = v->gsets[v->curr_tflags & TCHARSET_MASK];
if ((charset == '0') &&
(c== '`' ||
c == 'a' ||
(c >= 'f' && c <= '~') ||
(c >= '+' && c <= '.') ||
c == '.' ||
c == '0'))
use_attribute |= CLINEDRAW;
switch (v->cfg->char_tab)
{
case TAB_ATARI :
/* do nothing */
break;
case TAB_ISO :
c = (int) iso_to_st[c];
break;
default:
debug("paint: unknown char_tab: %d\n", v->cfg->char_tab);
break;
}
if (v->curr_tflags & TINSERT)
{
memmove (v->cdata[line] + v->cx + 1, v->cdata[line] + v->cx, (NCOLS(v) - v->cx - 1) * sizeof(v->cdata[0][0]));
for (i = NCOLS(v) - 1; i > v->cx; --i)
{
v->cflags[line][i] = v->cflags[line][i-1] | CDIRTY;
}
}
v->cdata[line][v->cx] = c;
v->cflags[line][v->cx] = CDIRTY | use_attribute;
v->dirty[line] |= SOMEDIRTY;
}
/* Unconditionally display character C even if it is a
* graphic character. */
void vt_quote_putch (TEXTWIN* tw, unsigned int c)
{
if (tw->do_wrap && (tw->curr_tflags & TWRAPAROUND) && tw->cx >= tw->maxx - 1)
{
new_line (tw);
tw->cx = 0;
tw->do_wrap = 0;
}
paint (tw, c);
++tw->cx;
if (tw->cx >= tw->maxx)
{
tw->cx = tw->maxx - 1;
/* Character was drawn in last column. */
if (tw->curr_tflags & TWRAPAROUND)
tw->do_wrap = 1;
}
}
/* Cursor down one line. */
void
cud1 (TEXTWIN* tw)
{
gotoxy (tw, tw->cx, tw->cy + 1 - RELOFFSET (tw));
}
/* Cursor down N lines, no scrolling */
void
cud (TEXTWIN* tw, short n)
{
gotoxy (tw, tw->cx, tw->cy + n - RELOFFSET (tw));
}
/* Cursor up one line. */
void
cuu1 (TEXTWIN* tw)
{
gotoxy (tw, tw->cx, tw->cy - 1 - RELOFFSET (tw));
}
/* Cursor up N lines. */
void
cuu (TEXTWIN* tw, short n)
{
gotoxy (tw, tw->cx, tw->cy - n - RELOFFSET (tw));
}
/* Cursor left one column. */
void
cub1 (TEXTWIN* tw)
{
gotoxy (tw, tw->cx - 1, tw->cy - RELOFFSET (tw));
}
/* Cursor left N columns. */
void
cub (TEXTWIN* tw, short n)
{
gotoxy (tw, tw->cx - n, tw->cy - RELOFFSET (tw));
}
/* Cursor right one column. */
void
cuf1 (TEXTWIN* tw)
{
gotoxy (tw, tw->cx + 1, tw->cy - RELOFFSET (tw));
}
/* Cursor right N columns. */
void
cuf (TEXTWIN* tw, short n)
{
gotoxy (tw, tw->cx + n, tw->cy - RELOFFSET (tw));
}
/* Move current cursor address of window TW to (X|Y),
* verifies that (X|Y) is within range
*/
void
gotoxy (TEXTWIN* tw, short x, short y)
{
if (x < 0)
x = 0;
else if (x >= tw->maxx)
x = tw->maxx - 1;
y += RELOFFSET (tw);
if (tw->curr_tflags & TORIGIN) {
/*
* line & column numbers are relativ to the margin,
* and cursor cannot be places outside margins
*/
if (y < tw->scroll_top)
y = tw->scroll_top;
else if (y >= tw->scroll_bottom)
y = tw->scroll_bottom - 1;
} else
{
/*
* line & column numbers are relativ to the upper left
* character position on the screen
*/
if (y < tw->miny)
y = tw->miny;
else if (y >= tw->maxy)
y = tw->maxy - 1;
}
tw->cx = x;
tw->cy = y;
tw->do_wrap = 0;
}
void
reverse_cr (TEXTWIN* tw)
{
if (tw->cy > tw->scroll_top)
cuu1 (tw);
else if (tw->cy == tw->scroll_top)
insert_line (tw);
}
/*
* Moves cursor down amount lines, scrolls if necessary.
* Won't leave scrolling region. No carriage return.
*/
void
new_line (TEXTWIN* tw)
{
if (tw->cy < tw->scroll_bottom - 1)
cud1 (tw);
else if (tw->cy == tw->scroll_bottom - 1) {
unsigned short cy = tw->cy;
tw->cy = tw->scroll_top;
delete_line (tw);
tw->cy = cy;
}
}
void carriage_return(TEXTWIN *tw)
{
gotoxy (tw, 0, tw->cy - RELOFFSET (tw));
}
/* Delete line at cursor position. */
void
delete_line (TEXTWIN* tw)
{
unsigned char* saved_data;
unsigned long* saved_cflag;
unsigned short cy = tw->cy;
unsigned long lines;
if (cy < tw->scroll_top || cy >= tw->scroll_bottom)
return;
tw->do_wrap = 0;
if (cy == tw->miny)
cy = 0;
lines = tw->scroll_bottom - cy - 1;
if (tw->last_cy >= cy && tw->last_cy < tw->scroll_bottom)
--tw->last_cy;
/* FIXME: A ring buffer would be better. */
saved_data = tw->cdata[cy];
saved_cflag = tw->cflags[cy];
memmove (tw->cdata + cy, tw->cdata + cy + 1,
(sizeof saved_data) * lines);
memmove (tw->cflags + cy, tw->cflags + cy + 1,
(sizeof saved_cflag) * lines);
if (cy == 0) {
/* This must be tw->cy, not cy! */
memmove (tw->dirty + cy, tw->dirty + cy + 1, lines);
} else {
memset (tw->dirty + cy, ALLDIRTY, lines);
}
tw->cdata[tw->scroll_bottom - 1] = saved_data;
tw->cflags[tw->scroll_bottom - 1] = saved_cflag;
/* clear the last line */
clrline (tw, tw->scroll_bottom - 1);
if (cy == 0)
++tw->scrolled;
}
/* Insert line at current cursor position. */
void
insert_line (TEXTWIN* tw)
{
unsigned char* saved_data;
unsigned long* saved_cflag;
unsigned short cy = tw->cy;
unsigned long lines;
if (cy < tw->scroll_top || cy >= tw->scroll_bottom)
return;
tw->do_wrap = 0;
lines = tw->scroll_bottom - cy - 1;
if (tw->last_cy > cy && tw->last_cy < tw->scroll_bottom)
++tw->last_cy;
/* FIXME: A ring buffer would be better. */
saved_data = tw->cdata[cy + lines];
saved_cflag = tw->cflags[cy + lines];
memset (tw->dirty + cy + 1, ALLDIRTY, lines);
memmove (tw->cdata + cy + 1, tw->cdata + cy,
(sizeof saved_data) * lines);
memmove (tw->cflags + cy + 1, tw->cflags + cy,
(sizeof saved_cflag) * lines);
tw->cdata[cy] = saved_data;
tw->cflags[cy] = saved_cflag;
clrline (tw, cy);
tw->do_wrap = 0;
refresh_textwin (tw, 0);
}
/*
* clrline(v, r): clear line r of window v
*/
void clrline(TEXTWIN *v, short r)
{
int i;
for (i = v->maxx-1; i >= 0; --i)
{
v->cdata[r][i] = ' ';
v->cflags[r][i] = v->curr_cattr &
(CBGCOL | CFGCOL);
}
v->dirty[r] = ALLDIRTY;
}
/*
* clear(v): clear the whole window v
*/
void clear(TEXTWIN *v)
{
int y;
for (y = v->miny; y < v->maxy; y++)
clrline(v, y);
v->do_wrap = 0;
}
/*
* clrchar(v, x, y): clear the (x,y) position on window v
*/
void clrchar(TEXTWIN *v, short x, short y)
{
v->cdata[y][x] = ' ';
v->cflags[y][x] = CDIRTY | (v->curr_cattr & (CBGCOL|CFGCOL));
v->dirty[y] |= SOMEDIRTY;
}
/*
* Clear window TW from position (X1, Y1) to position (X2, Y2)
* inclusive. It is assumed that Y2 >= Y1.
*/
void
clrfrom (TEXTWIN* tw, short x1, short y1, short x2, short y2)
{
int x, y;
if (y2 < y1)
return;
/* Only one line to clear? */
if (y1 == y2) {
if (x1 == 0 && x2 == tw->maxx - 1)
clrline (tw, y1);
else
for (x = x1; x <= x2; ++x)
clrchar (tw, x, y1);
return;
}
/* Clear first line. */
if (x1 == 0 && x2 == tw->maxx - 1)
clrline (tw, y1);
else
for (x = x1; x < tw->maxx; ++x) {
clrchar (tw, x, y1);
}
y = y1 + 1;
/* Middle part is always entire lines. */
while (y < y2) {
clrline (tw, y++);
}
/* Last line. */
if (x1 == 0 && x2 == tw->maxx - 1)
clrline (tw, y2);
else
for (x = 0; x <= x2; ++x) {
clrchar (tw, x, y2);
}
}
/*
* delete_char(v, x, y): delete the character at position (x, y) on
* the screen; the rest of the line is scrolled left, and a blank is
* inserted at the end of the line.
*/
void delete_char(TEXTWIN *v, short x, short y)
{
int i;
for (i = x; i < v->maxx-1; i++)
{
v->cdata[y][i] = v->cdata[y][i+1];
v->cflags[y][i] = v->cflags[y][i+1] | CDIRTY;
}
v->cdata[y][v->maxx-1] = ' ';
v->cflags[y][v->maxx-1] = CDIRTY | (v->curr_cattr & (CBGCOL|CFGCOL));
v->dirty[y] |= SOMEDIRTY;
v->do_wrap = 0;
}
/*
* assorted callback functions
*/
void set_title(TEXTWIN *v)
{
WINDOW *w = v->win;
title_window(w, v->captbuf);
}
void set_size(TEXTWIN *v)
{
int rows, cols;
char *s;
s = v->captbuf;
while (*s && *s != ',')
s++;
if (*s)
*s++ = 0;
cols = atoi(v->captbuf);
rows = atoi(s);
if (rows == 0)
rows = v->maxy;
else if (rows < MINROWS)
rows = MINROWS;
else if (rows > MAXROWS)
rows = MAXROWS;
if (cols == 0)
cols = v->maxx;
else if (rows < MINCOLS)
rows = MINCOLS;
else if (cols > MAXCOLS)
cols = MAXCOLS;
resize_textwin(v, cols, rows, v->miny);
}
/* Turn inverse video on/off. */
void
inverse_video (TEXTWIN* tw, int flag)
{
if (flag && (tw->curr_tflags & TDECSCNM))
return;
if (!flag && !(tw->curr_tflags & TDECSCNM))
return;
tw->cfg->bg_color ^= tw->cfg->fg_color;
tw->cfg->fg_color ^= tw->cfg->bg_color;
tw->cfg->bg_color ^= tw->cfg->fg_color;
tw->cfg->bg_effects ^= tw->cfg->fg_effects;
tw->cfg->fg_effects ^= tw->cfg->bg_effects;
tw->cfg->bg_effects ^= tw->cfg->fg_effects;
if (tw->curr_tflags & TDECSCNM)
tw->curr_tflags &= ~TDECSCNM;
else
tw->curr_tflags |= TDECSCNM;
memset (tw->dirty + tw->miny, ALLDIRTY, NROWS (tw));
refresh_textwin (tw, 0);
}
/* Reset colors to original colors (i. e. those that were
* configured in the window configuration dialog).
*/
void
original_colors(TEXTWIN *v)
{
if (v->cfg->vdi_colors) {
v->curr_cattr = (v->curr_cattr &
~(CFGCOL | CBGCOL)) |
COLORS (v->cfg->fg_color, v->cfg->bg_color);
} else {
v->curr_cattr = (v->curr_cattr &
~(CFGCOL | CBGCOL | CE_ANSI_EFFECTS)) |
COLORS (ANSI_DEFAULT, ANSI_DEFAULT);
}
}
/* Save cursor position and attributes.
FIXME: Two data sets (standard and alternate display)
have to be stored. */
void
save_cursor (TEXTWIN* tw)
{
tw->saved_x = tw->cx;
tw->saved_y = tw->cy;
tw->saved_tflags = tw->curr_tflags;
tw->saved_cattr = tw->curr_cattr;
memcpy (tw->saved_gsets, tw->gsets, sizeof tw->saved_gsets);
}
/* Restore cursor position and attributes.
FIXME: Two data sets (standard and alternate display)
have to be stored. */
void
restore_cursor (TEXTWIN* tw)
{
/* Xterm restores the character sets if the cursor has
actually never been saved before. */
if (tw->saved_x != -1 && tw->saved_y != -1) {
memcpy (tw->gsets, tw->saved_gsets, sizeof tw->gsets);
} else {
tw->gsets[0] = 'B';
tw->gsets[1] = '0';
tw->gsets[2] = 'B';
tw->gsets[3] = 'B';
}
tw->curr_cattr &= ~ATTRIBUTES;
tw->curr_cattr |= (tw->saved_cattr & ATTRIBUTES);
tw->curr_tflags &= ~DECSC_FLAGS;
tw->curr_tflags |= (tw->saved_tflags & DECSC_FLAGS);
/* If never saved the coordinates will be negative.
but that will place the cursor in the upper left
corner anyway, which is exactly what we want. */
gotoxy (tw, tw->saved_x, tw->saved_y - RELOFFSET (tw));
}
/* Implement hard or soft reset. Modelled after VTReset in
xterm. */
void
vt_reset (struct textwin* tw, bool full, bool saved)
{
tw->scroll_top = tw->miny;
tw->scroll_bottom = tw->maxy;
tw->curr_cattr &= ~CPROTECTED;
tw->curr_tflags &= ~TORIGIN;
original_colors (tw);
memcpy (tw->gsets, "B0BB", sizeof tw->gsets);
tw->curr_tflags = (tw->curr_tflags & ~TCHARSET_MASK);
if (full) {
tw->curr_tflags |= TWRAPAROUND;
tw->curr_tflags &= ~TINSERT;
tw->curr_cattr &= ~(ATTRIBUTES ^ CBGCOL ^ CFGCOL);
reset_tabs (tw);
tw->curs_mode = CURSOR_NORMAL;
tw->vt_mode = tw->cfg->vt_mode;
clear (tw);
/* Leave alternate screen! */
/* Reset to 80 columns. */
if (tw->curr_tflags & TDECCOLM)
resize_textwin (tw, 80, NROWS (tw), SCROLLBACK (tw));
/* Reset to normal video. */
inverse_video (tw, 0);
gotoxy (tw, 0, 0);
save_cursor (tw);
} else {
tw->curr_tflags |= TWRAPAROUND; /* FIXME: Read from config. */
tw->curr_tflags &= ~TINSERT;
tw->curr_cattr &= ~(ATTRIBUTES ^ CBGCOL ^ CFGCOL);
tw->saved_x = tw->saved_y = -1;
}
if (saved) {
short i;
for (i = 0; i < tw->miny; ++i) {
memset (tw->cdata[i], ' ',
(sizeof tw->cdata[i][0]) * tw->maxx);
memulset (tw->cflags[i], tw->curr_cattr, tw->maxx);
}
}
}