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
|
#include <assert.h>
#include "terminalframebuffer.h"
using namespace Terminal;
void Cell::reset( int background_color )
{
contents.clear();
fallback = false;
width = 1;
renditions = Renditions( background_color );
}
DrawState::DrawState( int s_width, int s_height )
: width( s_width ), height( s_height ),
cursor_col( 0 ), cursor_row( 0 ),
combining_char_col( 0 ), combining_char_row( 0 ), tabs( s_width ),
scrolling_region_top_row( 0 ), scrolling_region_bottom_row( height - 1 ),
renditions( 0 ), save(),
next_print_will_wrap( false ), origin_mode( false ), auto_wrap_mode( true ),
insert_mode( false ), cursor_visible( true ), reverse_video( false ),
application_mode_cursor_keys( false )
{
for ( int i = 0; i < width; i++ ) {
tabs[ i ] = ( (i % 8) == 0 );
}
}
Framebuffer::Framebuffer( int s_width, int s_height )
: rows( s_height, Row( s_width, 0 ) ), window_title(), bell_count( 0 ), ds( s_width, s_height )
{
assert( s_height > 0 );
assert( s_width > 0 );
}
void Framebuffer::scroll( int N )
{
if ( N >= 0 ) {
for ( int i = 0; i < N; i++ ) {
delete_line( ds.get_scrolling_region_top_row() );
ds.move_row( -1, true );
}
} else {
N = -N;
for ( int i = 0; i < N; i++ ) {
rows.insert( rows.begin() + ds.get_scrolling_region_top_row(), newrow() );
rows.erase( rows.begin() + ds.get_scrolling_region_bottom_row() + 1 );
ds.move_row( 1, true );
}
}
}
void DrawState::new_grapheme( void )
{
combining_char_col = cursor_col;
combining_char_row = cursor_row;
}
void DrawState::snap_cursor_to_border( void )
{
if ( cursor_row < limit_top() ) cursor_row = limit_top();
if ( cursor_row > limit_bottom() ) cursor_row = limit_bottom();
if ( cursor_col < 0 ) cursor_col = 0;
if ( cursor_col >= width ) cursor_col = width - 1;
}
void DrawState::move_row( int N, bool relative )
{
if ( relative ) {
cursor_row += N;
} else {
cursor_row = N + limit_top();
}
snap_cursor_to_border();
new_grapheme();
next_print_will_wrap = false;
}
void DrawState::move_col( int N, bool relative, bool implicit )
{
if ( implicit ) {
new_grapheme();
}
if ( relative ) {
cursor_col += N;
} else {
cursor_col = N;
}
if ( implicit && (cursor_col >= width) ) {
next_print_will_wrap = true;
}
snap_cursor_to_border();
if ( !implicit ) {
new_grapheme();
next_print_will_wrap = false;
}
}
void Framebuffer::move_rows_autoscroll( int rows )
{
/* don't scroll if outside the scrolling region */
if ( (ds.get_cursor_row() < ds.get_scrolling_region_top_row())
|| (ds.get_cursor_row() > ds.get_scrolling_region_bottom_row()) ) {
ds.move_row( rows, true );
return;
}
if ( ds.get_cursor_row() + rows > ds.get_scrolling_region_bottom_row() ) {
scroll( ds.get_cursor_row() + rows - ds.get_scrolling_region_bottom_row() );
} else if ( ds.get_cursor_row() + rows < ds.get_scrolling_region_top_row() ) {
scroll( ds.get_cursor_row() + rows - ds.get_scrolling_region_top_row() );
}
ds.move_row( rows, true );
}
Cell *Framebuffer::get_combining_cell( void )
{
if ( (ds.get_combining_char_col() < 0)
|| (ds.get_combining_char_row() < 0)
|| (ds.get_combining_char_col() >= ds.get_width())
|| (ds.get_combining_char_row() >= ds.get_height()) ) {
return NULL;
} /* can happen if a resize came in between */
return &rows[ ds.get_combining_char_row() ].cells[ ds.get_combining_char_col() ];
}
void DrawState::set_tab( void )
{
tabs[ cursor_col ] = true;
}
void DrawState::clear_tab( int col )
{
tabs[ col ] = false;
}
int DrawState::get_next_tab( void )
{
for ( int i = cursor_col + 1; i < width; i++ ) {
if ( tabs[ i ] ) {
return i;
}
}
return -1;
}
void DrawState::set_scrolling_region( int top, int bottom )
{
if ( height < 1 ) {
return;
}
scrolling_region_top_row = top;
scrolling_region_bottom_row = bottom;
if ( scrolling_region_top_row < 0 ) scrolling_region_top_row = 0;
if ( scrolling_region_bottom_row >= height ) scrolling_region_bottom_row = height - 1;
if ( scrolling_region_bottom_row < scrolling_region_top_row )
scrolling_region_bottom_row = scrolling_region_top_row;
/* real rule requires TWO-line scrolling region */
if ( origin_mode ) {
snap_cursor_to_border();
new_grapheme();
}
}
int DrawState::limit_top( void )
{
return origin_mode ? scrolling_region_top_row : 0;
}
int DrawState::limit_bottom( void )
{
return origin_mode ? scrolling_region_bottom_row : height - 1;
}
std::vector<int> DrawState::get_tabs( void )
{
std::vector<int> ret;
for ( int i = 0; i < width; i++ ) {
if ( tabs[ i ] ) {
ret.push_back( i );
}
}
return ret;
}
void Framebuffer::apply_renditions_to_current_cell( void )
{
get_mutable_cell()->renditions = ds.get_renditions();
}
SavedCursor::SavedCursor()
: cursor_col( 0 ), cursor_row( 0 ),
renditions( 0 ),
auto_wrap_mode( true ),
origin_mode( false )
{}
void DrawState::save_cursor( void )
{
save.cursor_col = cursor_col;
save.cursor_row = cursor_row;
save.renditions = renditions;
save.auto_wrap_mode = auto_wrap_mode;
save.origin_mode = origin_mode;
}
void DrawState::restore_cursor( void )
{
cursor_col = save.cursor_col;
cursor_row = save.cursor_row;
renditions = save.renditions;
auto_wrap_mode = save.auto_wrap_mode;
origin_mode = save.origin_mode;
snap_cursor_to_border(); /* we could have resized in between */
new_grapheme();
}
void Framebuffer::insert_line( int before_row )
{
if ( (before_row < ds.get_scrolling_region_top_row())
|| (before_row > ds.get_scrolling_region_bottom_row() + 1) ) {
return;
}
rows.insert( rows.begin() + before_row, newrow() );
rows.erase( rows.begin() + ds.get_scrolling_region_bottom_row() + 1 );
}
void Framebuffer::delete_line( int row )
{
if ( (row < ds.get_scrolling_region_top_row())
|| (row > ds.get_scrolling_region_bottom_row()) ) {
return;
}
int insertbefore = ds.get_scrolling_region_bottom_row() + 1;
if ( insertbefore == ds.get_height() ) {
rows.push_back( newrow() );
} else {
rows.insert( rows.begin() + insertbefore, newrow() );
}
rows.erase( rows.begin() + row );
}
void Row::insert_cell( int col, int background_color )
{
cells.insert( cells.begin() + col, Cell( background_color ) );
cells.pop_back();
}
void Row::delete_cell( int col, int background_color )
{
cells.push_back( Cell( background_color ) );
cells.erase( cells.begin() + col );
}
void Framebuffer::insert_cell( int row, int col )
{
rows[ row ].insert_cell( col, ds.get_background_rendition() );
}
void Framebuffer::delete_cell( int row, int col )
{
rows[ row ].delete_cell( col, ds.get_background_rendition() );
}
void Framebuffer::reset( void )
{
int width = ds.get_width(), height = ds.get_height();
ds = DrawState( width, height );
rows = std::deque<Row>( height, newrow() );
window_title.clear();
/* do not reset bell_count */
}
void Framebuffer::soft_reset( void )
{
ds.insert_mode = false;
ds.origin_mode = false;
ds.cursor_visible = true; /* per xterm and gnome-terminal */
ds.application_mode_cursor_keys = false;
ds.set_scrolling_region( 0, ds.get_height() - 1 );
ds.add_rendition( 0 );
ds.clear_saved_cursor();
}
void Framebuffer::resize( int s_width, int s_height )
{
assert( s_width > 0 );
assert( s_height > 0 );
rows.resize( s_height, newrow() );
for ( std::deque<Row>::iterator i = rows.begin();
i != rows.end();
i++ ) {
(*i).cells.resize( s_width, Cell( ds.get_background_rendition() ) );
}
ds.resize( s_width, s_height );
}
void DrawState::resize( int s_width, int s_height )
{
if ( (width != s_width)
|| (height != s_height) ) {
/* reset entire scrolling region on any resize */
/* xterm and rxvt-unicode do this. gnome-terminal only
resets scrolling region if it has to become smaller in resize */
scrolling_region_top_row = 0;
scrolling_region_bottom_row = s_height - 1;
}
width = s_width;
height = s_height;
snap_cursor_to_border();
tabs.resize( width );
/* saved cursor will be snapped to border on restore */
/* invalidate combining char cell if necessary */
if ( (combining_char_col >= width)
|| (combining_char_row >= height) ) {
combining_char_col = combining_char_row = -1;
}
}
Renditions::Renditions( int s_background )
: bold( false ), underlined( false ), blink( false ),
inverse( false ), invisible( false ), foreground_color( 0 ),
background_color( s_background )
{}
void Renditions::set_rendition( int num )
{
if ( num == 0 ) {
bold = underlined = blink = inverse = invisible = false;
foreground_color = background_color = 0;
return;
}
if ( (30 <= num) && (num <= 39) ) { /* foreground color */
foreground_color = num;
return;
} else if ( (40 <= num) && (num <= 49) ) { /* background color */
background_color = num;
return;
}
switch ( num ) {
case 1: case 22: bold = (num == 1); break;
case 4: case 24: underlined = (num == 4); break;
case 5: case 25: blink = (num == 5); break;
case 7: case 27: inverse = (num == 7); break;
case 8: case 28: invisible = (num == 8); break;
}
}
std::string Renditions::sgr( void ) const
{
std::string ret;
ret.append( "\033[0" );
if ( bold ) ret.append( ";1" );
if ( underlined ) ret.append( ";4" );
if ( blink ) ret.append( ";5" );
if ( inverse ) ret.append( ";7" );
if ( invisible ) ret.append( ";8" );
if ( foreground_color ) {
char col[ 8 ];
snprintf( col, 8, ";%d", foreground_color );
ret.append( col );
}
if ( background_color ) {
char col[ 8 ];
snprintf( col, 8, ";%d", background_color );
ret.append( col );
}
ret.append( "m" );
return ret;
}
void Row::reset( int background_color )
{
for ( std::vector<Cell>::iterator i = cells.begin();
i != cells.end();
i++ ) {
i->reset( background_color );
}
}
void Framebuffer::prefix_window_title( const std::deque<wchar_t> &s )
{
for ( auto i = s.rbegin(); i != s.rend(); i++ ) {
window_title.push_front( *i );
}
}
wchar_t Cell::debug_contents( void ) const
{
if ( contents.empty() ) {
return '_';
} else {
return contents.front();
}
}
|