-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvterm_buffer.c
More file actions
815 lines (665 loc) · 23.3 KB
/
Copy pathvterm_buffer.c
File metadata and controls
815 lines (665 loc) · 23.3 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "macros.h"
#include "vterm.h"
#include "vterm_private.h"
#include "vterm_cursor.h"
#include "vterm_buffer.h"
static vterm_cell_t** _vterm_buffer_alloc_raw(int rows, int cols);
/*
Contiguous matrix allocators. The cell data for an entire buffer
lives in ONE block; cells[r] points STRIDE cells apart into it.
cells[0] is therefore the block base -- dealloc frees cells[0] (the
whole block) plus the row-pointer array. This holds because nothing
ever swaps/rotates row pointers (scroll/clone memcpy contents, never
pointers), so cells[0] stays the base for the buffer's lifetime.
Wins: one malloc header instead of N, contiguous rows for cache
locality during scroll/erase, less fragmentation.
NOTE: vterm_copy_buffer keeps the per-row _vterm_buffer_alloc_raw
below -- its returned buffer is freed per-row by the caller (vwm),
so that allocation must stay per-row to preserve the public
free contract.
*/
static vterm_cell_t**
_vterm_cells_alloc_contig(int rows, int stride)
{
vterm_cell_t **ptrs;
vterm_cell_t *block;
int r;
if(rows < 1 || stride < 1) return NULL;
ptrs = (vterm_cell_t **)calloc((size_t)rows, sizeof(vterm_cell_t *));
block = (vterm_cell_t *)calloc((size_t)rows * (size_t)stride,
sizeof(vterm_cell_t));
if(ptrs == NULL || block == NULL)
{
free(ptrs);
free(block);
return NULL;
}
for(r = 0; r < rows; r++)
ptrs[r] = block + (size_t)r * (size_t)stride;
return ptrs;
}
static uint8_t**
_vterm_dirty_alloc_contig(int rows, int row_bytes)
{
uint8_t **ptrs;
uint8_t *block;
int r;
if(rows < 1 || row_bytes < 1) return NULL;
ptrs = (uint8_t **)calloc((size_t)rows, sizeof(uint8_t *));
block = (uint8_t *)calloc((size_t)rows * (size_t)row_bytes, 1);
if(ptrs == NULL || block == NULL)
{
free(ptrs);
free(block);
return NULL;
}
for(r = 0; r < rows; r++)
ptrs[r] = block + (size_t)r * (size_t)row_bytes;
return ptrs;
}
void
vterm_buffer_alloc(vterm_t *vterm, int idx, int width, int height)
{
vterm_desc_t *v_desc;
if(vterm == NULL) return;
if(width < 0 || height < 0) return;
v_desc = &vterm->vterm_desc[idx];
v_desc->cells = _vterm_cells_alloc_contig(height, width);
v_desc->dirty_bits = _vterm_dirty_alloc_contig(height,
VCELL_DIRTY_ROW_BYTES(width));
v_desc->rows = height;
v_desc->cols = width;
v_desc->max_cols = width;
v_desc->head = 0;
v_desc->fill = 0;
v_desc->scroll_min = 0;
v_desc->scroll_max = height - 1;
return;
}
void
vterm_buffer_realloc(vterm_t *vterm, int idx, int width, int height)
{
vterm_desc_t *v_desc;
vterm_cell_t **old_cells;
uint8_t **old_dirty;
vterm_cell_t **new_cells;
uint8_t **new_dirty;
int old_rows;
int max_cols_old; // == old physical row stride
int old_dbytes;
int new_stride; // ratcheted physical row width
int new_dbytes;
int copy_rows;
int copy_cell_bytes;
int r;
if(vterm == NULL) return;
if(width == 0 || height == 0) return;
if(width == -1 && height == -1) return;
// set the vterm description buffer selector
v_desc = &vterm->vterm_desc[idx];
// a -1 means the caller wants to keep an existing dimension
if(width == -1) width = v_desc->cols;
if(height == -1) height = v_desc->rows;
old_rows = v_desc->rows;
max_cols_old = v_desc->max_cols;
old_dbytes = VCELL_DIRTY_ROW_BYTES(max_cols_old);
/*
physical row width never shrinks (the historical max_cols ratchet).
a request narrower than the historical max keeps the wider stride;
only the logical width (cols) drops.
*/
new_stride = (width > max_cols_old) ? width : max_cols_old;
new_dbytes = VCELL_DIRTY_ROW_BYTES(new_stride);
old_cells = v_desc->cells;
old_dirty = v_desc->dirty_bits;
/*
allocate fresh contiguous blocks at the new geometry, copy the
surviving content across, then free the old blocks. resize is a
rare, human-driven event -- not a hot path -- so the extra
alloc+copy buys a far simpler, race-free path than reshaping a
contiguous block in place (which has to reason about row-stride
changes and overlapping moves). the descriptor is only ever
observed in a fully-consistent state.
*/
new_cells = _vterm_cells_alloc_contig(height, new_stride);
new_dirty = _vterm_dirty_alloc_contig(height, new_dbytes);
if(new_cells == NULL || new_dirty == NULL)
{
// allocation failed -- leave the buffer untouched
if(new_cells != NULL) { free(new_cells[0]); free(new_cells); }
if(new_dirty != NULL) { free(new_dirty[0]); free(new_dirty); }
return;
}
/*
copy surviving rows -- content and dirty bits -- from old to new
in LOGICAL order (the source may be ring-rotated; the translator
is the identity when head == 0). the new buffer is therefore
always linear. stride only ever grows, so an old row always
fits in a new row.
*/
copy_rows = (old_rows < height) ? old_rows : height;
copy_cell_bytes = max_cols_old * (int)sizeof(vterm_cell_t);
for(r = 0; r < copy_rows; r++)
{
int prow = vterm_desc_row_phys(v_desc, r);
memcpy(new_cells[r], old_cells[prow], (size_t)copy_cell_bytes);
memcpy(new_dirty[r], old_dirty[prow], (size_t)old_dbytes);
}
// release the old blocks (cells[0]/dirty_bits[0] are the block bases)
free(old_cells[0]);
free(old_cells);
free(old_dirty[0]);
free(old_dirty);
/*
commit the new geometry BEFORE any VCELL_* fill so the dirty-bit
macros (which index off v_desc->cells / v_desc->dirty_bits) see a
consistent descriptor.
*/
v_desc->cells = new_cells;
v_desc->dirty_bits = new_dirty;
v_desc->rows = height;
v_desc->cols = width;
v_desc->max_cols = new_stride;
v_desc->head = 0; // content was relinearized above
// blank brand-new rows entirely (space-fill marks each cell dirty)
for(r = copy_rows; r < height; r++)
vterm_fill_span(v_desc, r, 0, new_stride - 1, L' ', A_NORMAL, 0);
// blank newly-added columns on surviving rows when the stride grew
if(new_stride > max_cols_old)
{
for(r = 0; r < copy_rows; r++)
{
vterm_fill_span(v_desc, r, max_cols_old, new_stride - 1,
L' ', A_NORMAL, 0);
}
}
v_desc->scroll_max = height - 1;
/*
scroll_max is reset above, but scroll_min must be brought back into
range too. shrinking the buffer can leave a previously-set DECSTBM
top margin pointing at or past the new last row. a stale scroll_min
then drives out-of-bounds row writes -- interpret_csi_SD's clear
loop, origin-mode cursor home, and vterm_scroll_down all index
cells[] starting at scroll_min and would walk past the row pointer
array, corrupting the heap. when the old region no longer fits,
drop it back to full height.
*/
if(v_desc->scroll_min > v_desc->scroll_max)
{
v_desc->scroll_min = 0;
v_desc->buffer_state &= ~STATE_SCROLL_SHORT;
}
return;
}
void
vterm_buffer_dealloc(vterm_t *vterm, int idx)
{
vterm_desc_t *v_desc;
if(vterm == NULL) return;
v_desc = &vterm->vterm_desc[idx];
/*
cells/dirty_bits are each one contiguous block reached through a
row-pointer array. [0] is the block base (rows are never
swapped), so freeing [0] releases the whole block. guard each
independently so a partial alloc failure can't deref a NULL base.
*/
if(v_desc->cells != NULL)
{
free(v_desc->cells[0]);
free(v_desc->cells);
v_desc->cells = NULL;
}
if(v_desc->dirty_bits != NULL)
{
free(v_desc->dirty_bits[0]);
free(v_desc->dirty_bits);
v_desc->dirty_bits = NULL;
}
v_desc->head = 0;
return;
}
int
vterm_buffer_set_active(vterm_t *vterm, int idx)
{
vterm_desc_t *v_desc = NULL;
int curr_idx;
struct winsize ws = {.ws_xpixel = 0, .ws_ypixel = 0};
int width;
int height;
int std_width, std_height;
int curr_width, curr_height;
int r;
if(vterm == NULL) return -1;
if(idx != VTERM_BUF_STANDARD && idx != VTERM_BUF_ALTERNATE) return -1;
curr_idx = vterm_buffer_get_active(vterm);
/*
check to see if current buffer index is the same as the
requested one. if so, this is a no-op.
*/
if(idx == curr_idx) return 0;
// run the event hook if installed
if(vterm->event_mask & VTERM_MASK_BUFFER_PREFLIP)
{
if(vterm->event_hook != NULL)
{
vterm->event_hook(vterm, VTERM_EVENT_BUFFER_PREFLIP,
(void *)&idx);
}
}
/*
get current terminal size using the best methods available. typically,
that means using TIOCGWINSZ ioctl which is pretty portable. an
alternative method could be getmaxyx() which is found in most curses
implementations. however, that causes problems for uses of libvterm
where the rendering apparatus is not a curses WINDOW.
*/
ioctl(vterm->pty_fd, TIOCGWINSZ, &ws);
height = ws.ws_row;
width = ws.ws_col;
// treat the standard buffer special -- it never goes away
if(idx == VTERM_BUF_STANDARD)
{
/*
if the current buffer not the STD buffer, we need to handle
a few housekeeping items and then tear down the other
buffer before switching.
*/
if(curr_idx == VTERM_BUF_ALTERNATE)
{
// check to see if a resize happened
std_width = vterm->vterm_desc[VTERM_BUF_STANDARD].cols;
std_height = vterm->vterm_desc[VTERM_BUF_STANDARD].rows;
curr_width = vterm->vterm_desc[curr_idx].cols;
curr_height = vterm->vterm_desc[curr_idx].rows;
if(std_height != curr_height || std_width != curr_width)
{
vterm_buffer_realloc(vterm, VTERM_BUF_STANDARD,
curr_width, curr_height);
}
// run the event hook if installed
if(vterm->event_mask & VTERM_MASK_BUFFER_DEACTIVATED)
{
if(vterm->event_hook != NULL)
{
vterm->event_hook(vterm, VTERM_EVENT_BUFFER_DEACTIVATED,
(void *)&curr_idx);
}
}
// capture the departing alt screen into scrollback, top down
for(r = 0; r < curr_height; r++)
{
vterm_buffer_history_append(vterm,
VTERM_BUF_ALTERNATE, r);
}
// destroy the old buffer
vterm_buffer_dealloc(vterm, VTERM_BUF_ALTERNATE);
vterm_cursor_show(vterm, VTERM_BUF_STANDARD);
}
}
/*
Switch to the ALT buffer.
given the above conditional, this could have been an if-else.
however, this is more readable and it should optimize
about the same.
*/
if(idx != VTERM_BUF_STANDARD)
{
vterm_buffer_alloc(vterm, idx, width, height);
v_desc = &vterm->vterm_desc[idx];
// copy some defaults from standard buffer
v_desc->default_colors =
vterm->vterm_desc[VTERM_BUF_STANDARD].default_colors;
// erase the newly created buffer
vterm_erase(vterm, idx, 0);
}
// update the vterm buffer desc index and the cached active pointer.
// these two MUST stay in sync -- v_desc_active is read on hot paths.
vterm->vterm_desc_idx = idx;
vterm->v_desc_active = &vterm->vterm_desc[idx];
v_desc = vterm->v_desc_active;
VCELL_ALL_SET_DIRTY(v_desc);
// run the event hook if installed
if(vterm->event_mask & VTERM_MASK_BUFFER_ACTIVATED)
{
if(vterm->event_hook != NULL)
{
vterm->event_hook(vterm, VTERM_EVENT_BUFFER_ACTIVATED,
(void *)&idx);
}
}
return 0;
}
inline int
vterm_buffer_get_active(vterm_t *vterm)
{
if(vterm == NULL) return -1;
return vterm->vterm_desc_idx;
}
/*
shift_up/shift_down operate on the PHYSICAL row space -- callers
must not use them on a ring-rotated buffer (head != 0). since the
history ring landed, nothing shifts VTERM_BUF_HISTORY anymore.
*/
int
vterm_buffer_shift_up(vterm_t *vterm, int idx,
int top_row, int bottom_row, int stride)
{
vterm_desc_t *v_desc = NULL;
int rows_moved;
// set vterm desc buffer selector
v_desc = &vterm->vterm_desc[idx];
// compute real values when -1 is supplied
if(top_row == -1) top_row = 0;
if(bottom_row == -1) bottom_row = v_desc->rows - 1;
if(bottom_row - top_row < 1) return -1;
/*
rows that survive the shift. a stride >= the region height
shifts everything out -- nothing to copy (callers blank the
vacated rows).
*/
rows_moved = bottom_row - top_row - stride + 1;
if(rows_moved > 0)
{
if(v_desc->cols == v_desc->max_cols)
{
/*
un-ratcheted (the common case): rows are contiguous in
one block (_vterm_cells_alloc_contig), so the whole
shift is a single overlapping move.
*/
memmove(v_desc->cells[top_row],
v_desc->cells[top_row + stride],
(size_t)rows_moved * (size_t)v_desc->max_cols
* sizeof(vterm_cell_t));
}
else
{
/*
ratcheted stride: physical rows are wider than the
visible width. copy only the visible span -- a full-
stride block move multiplies the bytes copied by
max_cols/cols and blows test_resize's ASan watchdog in
its ratchet storm. ascending order: dst row is always
below src row.
*/
size_t col_mem = (size_t)v_desc->cols * sizeof(vterm_cell_t);
int r;
for(r = 0; r < rows_moved; r++)
{
memcpy(v_desc->cells[top_row + r],
v_desc->cells[top_row + stride + r], col_mem);
}
}
}
/*
scrolling the buffer always invalidates all rows. skip the
operation for the history buffer because we don't maintain the
dirty flags on that one.
*/
if(idx != VTERM_BUF_HISTORY)
{
VCELL_ALL_SET_DIRTY(v_desc);
}
return 0;
}
int
vterm_buffer_shift_down(vterm_t *vterm, int idx,
int top_row, int bottom_row, int stride)
{
vterm_desc_t *v_desc = NULL;
int rows_moved;
// set vterm desc buffer selector
v_desc = &vterm->vterm_desc[idx];
// compute real values when -1 is supplied
if(top_row == -1) top_row = 0;
if(bottom_row == -1) bottom_row = v_desc->rows - 1;
if(bottom_row - top_row < 1) return -1;
/*
rows that survive the shift; rows [top_row .. bottom_row - stride]
land on [top_row + stride .. bottom_row]. a stride >= the region
height shifts everything out -- nothing to copy.
*/
rows_moved = bottom_row - top_row - stride + 1;
// see shift_up for the fast-path / ratchet rationale
if(rows_moved > 0)
{
if(v_desc->cols == v_desc->max_cols)
{
memmove(v_desc->cells[top_row + stride],
v_desc->cells[top_row],
(size_t)rows_moved * (size_t)v_desc->max_cols
* sizeof(vterm_cell_t));
}
else
{
/*
descending order: dst row is always above src row, so
the highest destination must be written first.
*/
size_t col_mem = (size_t)v_desc->cols * sizeof(vterm_cell_t);
int r;
for(r = rows_moved - 1; r >= 0; r--)
{
memcpy(v_desc->cells[top_row + stride + r],
v_desc->cells[top_row + r], col_mem);
}
}
}
/*
scrolling the buffer always invalidates all rows. skip the
operation for the history buffer because we don't maintain the
dirty flags on that one.
*/
if(idx != VTERM_BUF_HISTORY)
{
VCELL_ALL_SET_DIRTY(v_desc);
}
return 0;
}
/*
capture one row of src_idx into the scrollback ring. the copy lands
on the OLDEST history row (the current head) and the head advances;
O(one row) where the legacy path shifted the whole history buffer.
copy width follows the old clone behavior: min() of the physical
strides, which always covers the visible width.
*/
int
vterm_buffer_history_append(vterm_t *vterm, int src_idx, int src_row)
{
vterm_desc_t *v_src;
vterm_desc_t *v_hist;
int stride;
int phys;
if(vterm == NULL) return -1;
v_src = &vterm->vterm_desc[src_idx];
v_hist = &vterm->vterm_desc[VTERM_BUF_HISTORY];
if(v_hist->cells == NULL || v_hist->rows < 1) return -1;
phys = v_hist->head;
stride = USE_MIN(v_src->max_cols, v_hist->max_cols);
memcpy(v_hist->cells[phys], v_src->cells[src_row],
(size_t)stride * sizeof(vterm_cell_t));
// appended cells arrive dirty, matching the legacy clone behavior
memset(v_hist->dirty_bits[phys], 0xFF,
(size_t)VCELL_DIRTY_ROW_BYTES(stride));
v_hist->head++;
if(v_hist->head == v_hist->rows) v_hist->head = 0;
// track how many rows are actually populated (saturates at capacity)
if(v_hist->fill < v_hist->rows) v_hist->fill++;
return 0;
}
int
vterm_get_active_buffer(vterm_t *vterm)
{
if(vterm == NULL) return -1;
return vterm->vterm_desc_idx;
}
void
vterm_dirty_set_span(vterm_desc_t *v_desc, int row, int col_start,
int col_end)
{
uint8_t *bytes = v_desc->dirty_bits[row];
int first = col_start >> 3;
int last = col_end >> 3;
uint8_t first_mask = (uint8_t)(0xFFu << (col_start & 7));
uint8_t last_mask = (uint8_t)(0xFFu >> (7 - (col_end & 7)));
if(first == last)
{
bytes[first] |= (uint8_t)(first_mask & last_mask);
return;
}
bytes[first] |= first_mask;
bytes[last] |= last_mask;
if(last - first > 1)
memset(&bytes[first + 1], 0xFF, (size_t)(last - first - 1));
return;
}
void
vterm_fill_span(vterm_desc_t *v_desc, int row, int col_start,
int col_end, wchar_t ch, attr_t attr, int colors)
{
vterm_cell_t template;
vterm_cell_t *vcell;
int c;
if(col_end < col_start) return;
vcell = &v_desc->cells[row][col_start];
if(colors == -1)
{
// preserve flavor: blank the glyph and attr, keep cell colors
for(c = col_start; c <= col_end; c++)
{
vcell->wch[0] = ch;
vcell->wch[1] = L'\0';
vcell->attr = attr;
vcell++;
}
}
else
{
template.wch[0] = ch;
template.wch[1] = L'\0';
template.attr = attr;
template.colors = colors;
for(c = col_start; c <= col_end; c++)
*vcell++ = template;
}
vterm_dirty_set_span(v_desc, row, col_start, col_end);
return;
}
/*
no internal callers since the history ring replaced the
shift+clone capture path; exported, kept for ABI.
*/
int
vterm_buffer_clone(vterm_t *vterm, int src_idx, int dst_idx,
int src_offset, int dst_offset, int rows)
{
vterm_desc_t *v_desc_src = NULL;
vterm_desc_t *v_desc_dst = NULL;
vterm_cell_t **vcell_src;
vterm_cell_t **vcell_dst;
int col_mem;
int stride;
int r;
if(vterm == NULL) return -1;
v_desc_src = &vterm->vterm_desc[src_idx];
v_desc_dst = &vterm->vterm_desc[dst_idx];
vcell_src = &v_desc_src->cells[src_offset];
vcell_dst = &v_desc_dst->cells[dst_offset];
stride = USE_MIN(v_desc_src->max_cols, v_desc_dst->max_cols);
col_mem = sizeof(vterm_cell_t) * stride;
for(r = 0; r < rows; r++)
{
memcpy(*vcell_dst, *vcell_src, col_mem);
// mark the cloned cells dirty in the destination's bitmap
vterm_dirty_set_span(v_desc_dst, dst_offset + r, 0, stride - 1);
vcell_src++;
vcell_dst++;
}
return 0;
}
vterm_cell_t**
vterm_copy_buffer(vterm_t *vterm, int *rows, int *cols)
{
vterm_desc_t *v_desc = NULL;
vterm_cell_t **buffer;
int r;
if(vterm == NULL) return NULL;
if(rows == NULL || cols == NULL) return NULL;
// set vterm desc buffer selector
v_desc = vterm->v_desc_active;
*rows = v_desc->rows;
*cols = v_desc->max_cols;
buffer = _vterm_buffer_alloc_raw(*rows, *cols);
for(r = 0; r < *rows; r++)
{
// mem copy one row at a time
memcpy(&buffer[r][0], &v_desc->cells[r][0],
v_desc->cols * sizeof(vterm_cell_t));
}
return buffer;
}
/*
Compose the scrollback view -- the newest `nlines` evicted history rows
above the head of the live standard screen -- into a freshly allocated cell
matrix: the copy-buffer counterpart to vterm_wnd_scrollback's on-screen
render. At nlines == 0 this is exactly the live buffer (like
vterm_copy_buffer). *rows / *cols report the matrix size; the caller frees
it per row, same contract as vterm_copy_buffer. `nlines` is clamped to
>= 0 (the caller bounds it to vterm_get_history_used()).
*/
vterm_cell_t**
vterm_copy_scrollback(vterm_t *vterm, int nlines, int *rows, int *cols)
{
vterm_desc_t *hist;
vterm_desc_t *live;
vterm_desc_t *v_desc;
vterm_cell_t **buffer;
int capacity, height, r, lrow, prow;
if(vterm == NULL) return NULL;
if(rows == NULL || cols == NULL) return NULL;
hist = &vterm->vterm_desc[VTERM_BUF_HISTORY];
live = &vterm->vterm_desc[VTERM_BUF_STANDARD];
capacity = hist->rows;
if(nlines < 0) nlines = 0;
height = live->rows;
*rows = height;
*cols = live->max_cols;
buffer = _vterm_buffer_alloc_raw(*rows, *cols);
if(buffer == NULL) return NULL;
/* mirror vterm_wnd_scrollback: the top `nlines` rows come from the tail of
the history ring (newest evicted first, right-aligned at capacity-1);
the rest is the live screen shifted down by `nlines`. */
for(r = 0; r < height; r++)
{
if(r < nlines)
{
v_desc = hist;
lrow = capacity - nlines + r;
}
else
{
v_desc = live;
lrow = r - nlines;
}
if(lrow < 0 || lrow >= v_desc->rows) continue; /* leave row blank */
prow = vterm_desc_row_phys(v_desc, lrow);
memcpy(&buffer[r][0], &v_desc->cells[prow][0],
(size_t)v_desc->cols * sizeof(vterm_cell_t));
}
return buffer;
}
vterm_cell_t **
_vterm_buffer_alloc_raw(int rows, int cols)
{
vterm_cell_t **buffer;
int r;
buffer = (vterm_cell_t **)calloc(rows, sizeof(vterm_cell_t*));
for(r = 0; r < rows; r++)
{
buffer[r] = (vterm_cell_t *)calloc(cols, sizeof(vterm_cell_t));
}
return buffer;
}