xref: /freebsd/sys/dev/vt/vt_buf.c (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009, 2013 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Ed Schouten under sponsorship from the
8  * FreeBSD Foundation.
9  *
10  * Portions of this software were developed by Oleksandr Rybalko
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/reboot.h>
44 #include <sys/systm.h>
45 
46 #include <dev/vt/vt.h>
47 
48 static MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
49 
50 #define	VTBUF_LOCK(vb)		mtx_lock_spin(&(vb)->vb_lock)
51 #define	VTBUF_UNLOCK(vb)	mtx_unlock_spin(&(vb)->vb_lock)
52 
53 #define POS_INDEX(c, r) (((r) << 12) + (c))
54 #define	POS_COPY(d, s)	do {	\
55 	(d).tp_col = (s).tp_col;	\
56 	(d).tp_row = (s).tp_row;	\
57 } while (0)
58 
59 #ifndef SC_NO_CUTPASTE
60 static int vtbuf_htw(const struct vt_buf *vb, int row);
61 static int vtbuf_wth(const struct vt_buf *vb, int row);
62 static int vtbuf_in_this_range(int begin, int test, int end, int sz);
63 #endif
64 
65 /*
66  * line4
67  * line5 <--- curroffset (terminal output to that line)
68  * line0
69  * line1                  <--- roffset (history display from that point)
70  * line2
71  * line3
72  */
73 int
74 vthistory_seek(struct vt_buf *vb, int offset, int whence)
75 {
76 	int diff, top, bottom, roffset;
77 
78 	/* No scrolling if not enabled. */
79 	if ((vb->vb_flags & VBF_SCROLL) == 0) {
80 		if (vb->vb_roffset != vb->vb_curroffset) {
81 			vb->vb_roffset = vb->vb_curroffset;
82 			return (0xffff);
83 		}
84 		return (0); /* No changes */
85 	}
86 
87 	/* "top" may be a negative integer. */
88 	bottom = vb->vb_curroffset;
89 	top = (vb->vb_flags & VBF_HISTORY_FULL) ?
90 	    bottom + vb->vb_scr_size.tp_row - vb->vb_history_size :
91 	    0;
92 
93 	roffset = 0; /* Make gcc happy. */
94 	switch (whence) {
95 	case VHS_SET:
96 		if (offset < 0)
97 			offset = 0;
98 		roffset = top + offset;
99 		break;
100 	case VHS_CUR:
101 		/*
102 		 * Operate on copy of offset value, since it temporary
103 		 * can be bigger than amount of rows in buffer.
104 		 */
105 		roffset = vb->vb_roffset;
106 		if (roffset >= bottom + vb->vb_scr_size.tp_row)
107 			roffset -= vb->vb_history_size;
108 
109 		roffset += offset;
110 		roffset = MAX(roffset, top);
111 		roffset = MIN(roffset, bottom);
112 
113 		if (roffset < 0)
114 			roffset = vb->vb_history_size + roffset;
115 
116 		break;
117 	case VHS_END:
118 		/* Go to current offset. */
119 		roffset = vb->vb_curroffset;
120 		break;
121 	}
122 
123 	diff = vb->vb_roffset != roffset;
124 	vb->vb_roffset = roffset;
125 
126 	return (diff);
127 }
128 
129 void
130 vthistory_addlines(struct vt_buf *vb, int offset)
131 {
132 #ifndef SC_NO_CUTPASTE
133 	int cur, sz;
134 #endif
135 
136 	vb->vb_curroffset += offset;
137 	if (vb->vb_curroffset + vb->vb_scr_size.tp_row >= vb->vb_history_size) {
138 		vb->vb_flags |= VBF_HISTORY_FULL;
139 		vb->vb_curroffset %= vb->vb_history_size;
140 	}
141 	if ((vb->vb_flags & VBF_SCROLL) == 0) {
142 		vb->vb_roffset = vb->vb_curroffset;
143 	}
144 
145 #ifndef SC_NO_CUTPASTE
146 	sz = vb->vb_history_size;
147 	cur = vb->vb_roffset + vb->vb_scr_size.tp_row + sz - 1;
148 	if (vtbuf_in_this_range(cur, vb->vb_mark_start.tp_row, cur + offset, sz) ||
149 	    vtbuf_in_this_range(cur, vb->vb_mark_end.tp_row, cur + offset, sz)) {
150 		/* clear screen selection */
151 		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row;
152 		vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
153 	}
154 #endif
155 }
156 
157 void
158 vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
159 {
160 
161 	*offset = vb->vb_roffset;
162 }
163 
164 #ifndef SC_NO_CUTPASTE	/* Only mouse support use it now. */
165 /* Translate history row to current view row number. */
166 static int
167 vtbuf_htw(const struct vt_buf *vb, int row)
168 {
169 
170 	/*
171 	 * total 1000 rows.
172 	 * History offset	roffset	winrow
173 	 *	205		200	((205 - 200 + 1000) % 1000) = 5
174 	 *	90		990	((90 - 990 + 1000) % 1000) = 100
175 	 */
176 	return ((row - vb->vb_roffset + vb->vb_history_size) %
177 	    vb->vb_history_size);
178 }
179 
180 /* Translate current view row number to history row. */
181 static int
182 vtbuf_wth(const struct vt_buf *vb, int row)
183 {
184 
185 	return ((vb->vb_roffset + row) % vb->vb_history_size);
186 }
187 
188 /*
189  * Test if an index in a circular buffer is within a range.
190  *
191  * begin - start index
192  * end - end index
193  * test - test index
194  * sz - size of circular buffer when it turns over
195  */
196 static int
197 vtbuf_in_this_range(int begin, int test, int end, int sz)
198 {
199 
200 	begin %= sz;
201 	end %= sz;
202 
203 	/* check for inversion */
204 	if (begin > end)
205 		return (test >= begin || test < end);
206 	else
207 		return (test >= begin && test < end);
208 }
209 #endif
210 
211 int
212 vtbuf_iscursor(const struct vt_buf *vb, int row, int col)
213 {
214 #ifndef SC_NO_CUTPASTE
215 	int sc, sr, sz, ec, er, tmp;
216 #endif
217 
218 	if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
219 	    (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
220 		return (1);
221 
222 #ifndef SC_NO_CUTPASTE
223 	/* Mark cut/paste region. */
224 	if (vb->vb_mark_start.tp_col == vb->vb_mark_end.tp_col &&
225 	    vb->vb_mark_start.tp_row == vb->vb_mark_end.tp_row)
226 		return (0);
227 
228 	sc = vb->vb_mark_start.tp_col;
229 	sr = vb->vb_mark_start.tp_row;
230 	ec = vb->vb_mark_end.tp_col;
231 	er = vb->vb_mark_end.tp_row;
232 
233 	/*
234 	 * Information about if the selection was made bottom-top or
235 	 * top-bottom is lost due to modulo arithmetics and needs to
236 	 * be recovered:
237 	 */
238 	sz = vb->vb_history_size;
239 	tmp = (sz + er - sr) % sz;
240 	row = vtbuf_wth(vb, row);
241 
242 	/* Swap start and end if start > end */
243 	if ((2 * tmp) > sz || (tmp == 0 && sc > ec)) {
244 		tmp = sc; sc = ec; ec = tmp;
245 		tmp = sr; sr = er; er = tmp;
246 	}
247 
248 	if (vtbuf_in_this_range(POS_INDEX(sc, sr), POS_INDEX(col, row),
249 	    POS_INDEX(ec, er), POS_INDEX(0, sz)))
250 		return (1);
251 #endif
252 
253 	return (0);
254 }
255 
256 void
257 vtbuf_lock(struct vt_buf *vb)
258 {
259 
260 	VTBUF_LOCK(vb);
261 }
262 
263 void
264 vtbuf_unlock(struct vt_buf *vb)
265 {
266 
267 	VTBUF_UNLOCK(vb);
268 }
269 
270 void
271 vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
272 {
273 
274 	if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
275 		vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
276 	if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
277 		vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
278 	if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
279 		vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
280 	if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
281 		vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
282 }
283 
284 static inline void
285 vtbuf_dirty_cell(struct vt_buf *vb, const term_pos_t *p)
286 {
287 	term_rect_t area;
288 
289 	area.tr_begin = *p;
290 	area.tr_end.tp_row = p->tp_row + 1;
291 	area.tr_end.tp_col = p->tp_col + 1;
292 	vtbuf_dirty(vb, &area);
293 }
294 
295 static void
296 vtbuf_make_undirty(struct vt_buf *vb)
297 {
298 
299 	vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
300 	vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
301 }
302 
303 void
304 vtbuf_undirty(struct vt_buf *vb, term_rect_t *r)
305 {
306 
307 	*r = vb->vb_dirtyrect;
308 	vtbuf_make_undirty(vb);
309 }
310 
311 void
312 vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
313 {
314 	const term_pos_t *p1 = &r->tr_begin;
315 	term_rect_t area;
316 	unsigned int rows, cols;
317 	int pr, rdiff;
318 
319 	KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
320 	    ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
321 		r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
322 	KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
323 	    ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
324 		r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
325 
326 	KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
327 	    ("vtbuf_copy end.tp_row %d must be less than screen width %d",
328 		r->tr_end.tp_row, vb->vb_scr_size.tp_row));
329 	KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
330 	    ("vtbuf_copy end.tp_col %d must be less than screen height %d",
331 		r->tr_end.tp_col, vb->vb_scr_size.tp_col));
332 
333 	KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
334 	    ("vtbuf_copy tp_row %d must be less than screen width %d",
335 		p2->tp_row, vb->vb_scr_size.tp_row));
336 	KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
337 	    ("vtbuf_copy tp_col %d must be less than screen height %d",
338 		p2->tp_col, vb->vb_scr_size.tp_col));
339 
340 	rows = r->tr_end.tp_row - r->tr_begin.tp_row;
341 	rdiff = r->tr_begin.tp_row - p2->tp_row;
342 	cols = r->tr_end.tp_col - r->tr_begin.tp_col;
343 	if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
344 	    r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
345 	    (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
346 	    rdiff > 0) { /* Only forward direction. Do not eat history. */
347 		vthistory_addlines(vb, rdiff);
348 	} else if (p2->tp_row < p1->tp_row) {
349 		/* Handle overlapping copies of line segments. */
350 		/* Move data up. */
351 		for (pr = 0; pr < rows; pr++)
352 			memmove(
353 			    &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
354 			    &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
355 			    cols * sizeof(term_char_t));
356 	} else {
357 		/* Move data down. */
358 		for (pr = rows - 1; pr >= 0; pr--)
359 			memmove(
360 			    &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
361 			    &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
362 			    cols * sizeof(term_char_t));
363 	}
364 
365 	area.tr_begin = *p2;
366 	area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
367 	area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
368 	vtbuf_dirty(vb, &area);
369 }
370 
371 static void
372 vtbuf_do_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
373 {
374 	unsigned int pr, pc;
375 	term_char_t *row;
376 
377 	for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
378 		row = vb->vb_rows[(vb->vb_curroffset + pr) %
379 		    VTBUF_MAX_HEIGHT(vb)];
380 		for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
381 			row[pc] = c;
382 		}
383 	}
384 }
385 
386 void
387 vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
388 {
389 
390 	KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
391 	    ("vtbuf_fill begin.tp_row %d must be < screen height %d",
392 		r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
393 	KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
394 	    ("vtbuf_fill begin.tp_col %d must be < screen width %d",
395 		r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
396 
397 	KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
398 	    ("vtbuf_fill end.tp_row %d must be <= screen height %d",
399 		r->tr_end.tp_row, vb->vb_scr_size.tp_row));
400 	KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
401 	    ("vtbuf_fill end.tp_col %d must be <= screen width %d",
402 		r->tr_end.tp_col, vb->vb_scr_size.tp_col));
403 
404 	vtbuf_do_fill(vb, r, c);
405 	vtbuf_dirty(vb, r);
406 }
407 
408 static void
409 vtbuf_init_rows(struct vt_buf *vb)
410 {
411 	int r;
412 
413 	vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
414 
415 	for (r = 0; r < vb->vb_history_size; r++)
416 		vb->vb_rows[r] = &vb->vb_buffer[r * vb->vb_scr_size.tp_col];
417 }
418 
419 static void
420 vtbuf_do_clearhistory(struct vt_buf *vb)
421 {
422 	term_rect_t rect;
423 	const teken_attr_t *a;
424 	term_char_t ch;
425 
426 	a = teken_get_curattr(&vb->vb_terminal->tm_emulator);
427 	ch = TCOLOR_FG(a->ta_fgcolor) | TCOLOR_BG(a->ta_bgcolor);
428 
429 	rect.tr_begin.tp_row = rect.tr_begin.tp_col = 0;
430 	rect.tr_end.tp_col = vb->vb_scr_size.tp_col;
431 	rect.tr_end.tp_row = vb->vb_history_size;
432 
433 	vtbuf_do_fill(vb, &rect, VTBUF_SPACE_CHAR(ch));
434 }
435 
436 static void
437 vtbuf_reset_scrollback(struct vt_buf *vb)
438 {
439 	vb->vb_roffset = 0;
440 	vb->vb_curroffset = 0;
441 	vb->vb_mark_start.tp_row = 0;
442 	vb->vb_mark_start.tp_col = 0;
443 	vb->vb_mark_end.tp_row = 0;
444 	vb->vb_mark_end.tp_col = 0;
445 }
446 
447 void
448 vtbuf_init_early(struct vt_buf *vb)
449 {
450 	vb->vb_flags |= VBF_CURSOR;
451 	vtbuf_reset_scrollback(vb);
452 	vtbuf_init_rows(vb);
453 	vtbuf_do_clearhistory(vb);
454 	vtbuf_make_undirty(vb);
455 	if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
456 		mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
457 		vb->vb_flags |= VBF_MTX_INIT;
458 	}
459 }
460 
461 void
462 vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
463 {
464 	int sz;
465 
466 	vb->vb_scr_size = *p;
467 	vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
468 
469 	if ((vb->vb_flags & VBF_STATIC) == 0) {
470 		sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
471 		vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
472 
473 		sz = vb->vb_history_size * sizeof(term_char_t *);
474 		vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
475 	}
476 
477 	vtbuf_init_early(vb);
478 }
479 
480 void
481 vtbuf_clearhistory(struct vt_buf *vb)
482 {
483 	VTBUF_LOCK(vb);
484 	vtbuf_do_clearhistory(vb);
485 	vtbuf_reset_scrollback(vb);
486 	vb->vb_flags &= ~VBF_HISTORY_FULL;
487 	VTBUF_UNLOCK(vb);
488 }
489 
490 void
491 vtbuf_sethistory_size(struct vt_buf *vb, unsigned int size)
492 {
493 	term_pos_t p;
494 
495 	/* With same size */
496 	p.tp_row = vb->vb_scr_size.tp_row;
497 	p.tp_col = vb->vb_scr_size.tp_col;
498 	vtbuf_grow(vb, &p, size);
499 }
500 
501 void
502 vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, unsigned int history_size)
503 {
504 	term_char_t *old, *new, **rows, **oldrows, **copyrows, *row, *oldrow;
505 	unsigned int w, h, c, r, old_history_size;
506 	size_t bufsize, rowssize;
507 	int history_full;
508 	const teken_attr_t *a;
509 	term_char_t ch;
510 
511 	a = teken_get_curattr(&vb->vb_terminal->tm_emulator);
512 	ch = TCOLOR_FG(a->ta_fgcolor) | TCOLOR_BG(a->ta_bgcolor);
513 
514 	history_size = MAX(history_size, p->tp_row);
515 
516 	/* Allocate new buffer. */
517 	bufsize = history_size * p->tp_col * sizeof(term_char_t);
518 	new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
519 	rowssize = history_size * sizeof(term_pos_t *);
520 	rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
521 
522 	/* Toggle it. */
523 	VTBUF_LOCK(vb);
524 	old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
525 	oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
526 	copyrows = vb->vb_rows;
527 
528 	w = vb->vb_scr_size.tp_col;
529 	h = vb->vb_scr_size.tp_row;
530 	old_history_size = vb->vb_history_size;
531 	history_full = vb->vb_flags & VBF_HISTORY_FULL ||
532 	    vb->vb_curroffset + h >= history_size;
533 
534 	vb->vb_history_size = history_size;
535 	vb->vb_buffer = new;
536 	vb->vb_rows = rows;
537 	vb->vb_flags &= ~VBF_STATIC;
538 	vb->vb_scr_size = *p;
539 	vtbuf_init_rows(vb);
540 
541 	/*
542 	 * Copy rows to the new buffer. The first row in the history
543 	 * is back to index 0, ie. the new buffer doesn't cycle.
544 	 */
545 	if (history_size > old_history_size) {
546 		for (r = 0; r < old_history_size; r ++) {
547 			row = rows[r];
548 
549 			/* Compute the corresponding row in the old buffer. */
550 			if (history_full)
551 				/*
552 				 * The buffer is full, the "top" row is
553 				 * the one just after the viewable area
554 				 * (curroffset + viewable height) in the
555 				 * cycling buffer. The corresponding row
556 				 * is computed from this top row.
557 				 */
558 				oldrow = copyrows[
559 				    (vb->vb_curroffset + h + r) %
560 				    old_history_size];
561 			else
562 				/*
563 				 * The buffer is not full, therefore,
564 				 * we didn't cycle already. The
565 				 * corresponding rows are the same in
566 				 * both buffers.
567 				 */
568 				oldrow = copyrows[r];
569 
570 			memmove(row, oldrow,
571 			    MIN(p->tp_col, w) * sizeof(term_char_t));
572 
573 			/*
574 			 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
575 			 * extended lines of kernel text using the wrong
576 			 * background color.
577 			 */
578 			for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
579 				row[c] = VTBUF_SPACE_CHAR(ch);
580 			}
581 		}
582 
583 		/* Fill remaining rows. */
584 		for (r = old_history_size; r < history_size; r++) {
585 			row = rows[r];
586 			for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
587 				row[c] = VTBUF_SPACE_CHAR(ch);
588 			}
589 		}
590 
591 		vb->vb_flags &= ~VBF_HISTORY_FULL;
592 
593 		/*
594 		 * If the screen is already filled (there are non-visible lines
595 		 * above the current viewable area), adjust curroffset to the
596 		 * new viewable area.
597 		 *
598 		 * If the old buffer was full, set curroffset to the
599 		 * <h>th most recent line of history in the new, non-cycled
600 		 * buffer. Otherwise, it didn't cycle, so the old curroffset
601 		 * is the same in the new buffer.
602 		 */
603 		if (history_full)
604 			vb->vb_curroffset = old_history_size - h;
605 	} else {
606 		/*
607 		 * (old_history_size - history_size) lines of history are
608 		 * dropped.
609 		 */
610 		for (r = 0; r < history_size; r ++) {
611 			row = rows[r];
612 
613 			/*
614 			 * Compute the corresponding row in the old buffer.
615 			 *
616 			 * See the equivalent if{} block above for an
617 			 * explanation.
618 			 */
619 			if (history_full)
620 				oldrow = copyrows[
621 				    (vb->vb_curroffset + h + r +
622 				     (old_history_size - history_size)) %
623 				    old_history_size];
624 			else
625 				oldrow = copyrows[r];
626 
627 			memmove(row, oldrow,
628 			    MIN(p->tp_col, w) * sizeof(term_char_t));
629 
630 			/*
631 			 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
632 			 * extended lines of kernel text using the wrong
633 			 * background color.
634 			 */
635 			for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
636 				row[c] = VTBUF_SPACE_CHAR(ch);
637 			}
638 		}
639 
640 		if (history_full) {
641 			vb->vb_curroffset = history_size - h;
642 			vb->vb_flags |= VBF_HISTORY_FULL;
643 		}
644 	}
645 
646 	vb->vb_roffset = vb->vb_curroffset;
647 
648 	/* Adjust cursor position. */
649 	if (vb->vb_cursor.tp_col > p->tp_col - 1)
650 		/*
651 		 * Move cursor to the last column, in case its previous
652 		 * position is outside of the new screen area.
653 		 */
654 		vb->vb_cursor.tp_col = p->tp_col - 1;
655 
656 	if (vb->vb_curroffset > 0 || vb->vb_cursor.tp_row > p->tp_row - 1)
657 		/* Move cursor to the last line on the screen. */
658 		vb->vb_cursor.tp_row = p->tp_row - 1;
659 
660 	VTBUF_UNLOCK(vb);
661 
662 	/* Deallocate old buffer. */
663 	free(old, M_VTBUF);
664 	free(oldrows, M_VTBUF);
665 }
666 
667 void
668 vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
669 {
670 	term_char_t *row;
671 
672 	KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
673 	    ("vtbuf_putchar tp_row %d must be less than screen width %d",
674 		p->tp_row, vb->vb_scr_size.tp_row));
675 	KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
676 	    ("vtbuf_putchar tp_col %d must be less than screen height %d",
677 		p->tp_col, vb->vb_scr_size.tp_col));
678 
679 	row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
680 	    VTBUF_MAX_HEIGHT(vb)];
681 	if (row[p->tp_col] != c) {
682 		row[p->tp_col] = c;
683 		vtbuf_dirty_cell(vb, p);
684 	}
685 }
686 
687 void
688 vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
689 {
690 	if (vb->vb_flags & VBF_CURSOR) {
691 		vtbuf_dirty_cell(vb, &vb->vb_cursor);
692 		vb->vb_cursor = *p;
693 		vtbuf_dirty_cell(vb, &vb->vb_cursor);
694 	} else {
695 		vb->vb_cursor = *p;
696 	}
697 }
698 
699 #ifndef SC_NO_CUTPASTE
700 static void
701 vtbuf_flush_mark(struct vt_buf *vb)
702 {
703 	term_rect_t area;
704 	int s, e;
705 
706 	/* Notify renderer to update marked region. */
707 	if ((vb->vb_mark_start.tp_col != vb->vb_mark_end.tp_col) ||
708 	    (vb->vb_mark_start.tp_row != vb->vb_mark_end.tp_row)) {
709 
710 		s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
711 		e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
712 
713 		area.tr_begin.tp_col = 0;
714 		area.tr_begin.tp_row = MIN(s, e);
715 
716 		area.tr_end.tp_col = vb->vb_scr_size.tp_col;
717 		area.tr_end.tp_row = MAX(s, e) + 1;
718 
719 		VTBUF_LOCK(vb);
720 		vtbuf_dirty(vb, &area);
721 		VTBUF_UNLOCK(vb);
722 	}
723 }
724 
725 int
726 vtbuf_get_marked_len(struct vt_buf *vb)
727 {
728 	int ei, si, sz;
729 	term_pos_t s, e;
730 
731 	/* Swap according to window coordinates. */
732 	if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
733 	    vb->vb_mark_start.tp_col) >
734 	    POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
735 	    vb->vb_mark_end.tp_col)) {
736 		POS_COPY(e, vb->vb_mark_start);
737 		POS_COPY(s, vb->vb_mark_end);
738 	} else {
739 		POS_COPY(s, vb->vb_mark_start);
740 		POS_COPY(e, vb->vb_mark_end);
741 	}
742 
743 	si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
744 	ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
745 
746 	/* Number symbols and number of rows to inject \n */
747 	sz = ei - si + ((e.tp_row - s.tp_row) * 2);
748 
749 	return (sz * sizeof(term_char_t));
750 }
751 
752 void
753 vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
754 {
755 	int i, r, c, cs, ce;
756 	term_pos_t s, e;
757 
758 	/* Swap according to window coordinates. */
759 	if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
760 	    vb->vb_mark_start.tp_col) >
761 	    POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
762 	    vb->vb_mark_end.tp_col)) {
763 		POS_COPY(e, vb->vb_mark_start);
764 		POS_COPY(s, vb->vb_mark_end);
765 	} else {
766 		POS_COPY(s, vb->vb_mark_start);
767 		POS_COPY(e, vb->vb_mark_end);
768 	}
769 
770 	i = 0;
771 	for (r = s.tp_row; r <= e.tp_row; r ++) {
772 		cs = (r == s.tp_row)?s.tp_col:0;
773 		ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
774 		for (c = cs; c < ce; c ++) {
775 			buf[i++] = vb->vb_rows[r][c];
776 		}
777 		/* Add new line for all rows, but not for last one. */
778 		if (r != e.tp_row) {
779 			buf[i++] = '\r';
780 			buf[i++] = '\n';
781 		}
782 	}
783 }
784 
785 int
786 vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
787 {
788 	term_char_t *r;
789 	int i;
790 
791 	switch (type) {
792 	case VTB_MARK_END:	/* B1 UP */
793 		if (vb->vb_mark_last != VTB_MARK_MOVE)
794 			return (0);
795 		/* FALLTHROUGH */
796 	case VTB_MARK_MOVE:
797 	case VTB_MARK_EXTEND:
798 		vtbuf_flush_mark(vb); /* Clean old mark. */
799 		vb->vb_mark_end.tp_col = col;
800 		vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
801 		break;
802 	case VTB_MARK_START:
803 		vtbuf_flush_mark(vb); /* Clean old mark. */
804 		vb->vb_mark_start.tp_col = col;
805 		vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
806 		/* Start again, so clear end point. */
807 		vb->vb_mark_end.tp_col = col;
808 		vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
809 		break;
810 	case VTB_MARK_WORD:
811 		vtbuf_flush_mark(vb); /* Clean old mark. */
812 		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
813 		    vtbuf_wth(vb, row);
814 		r = vb->vb_rows[vb->vb_mark_start.tp_row];
815 		for (i = col; i >= 0; i --) {
816 			if (TCHAR_CHARACTER(r[i]) == ' ') {
817 				vb->vb_mark_start.tp_col = i + 1;
818 				break;
819 			}
820 		}
821 		for (i = col; i < vb->vb_scr_size.tp_col; i ++) {
822 			if (TCHAR_CHARACTER(r[i]) == ' ') {
823 				vb->vb_mark_end.tp_col = i;
824 				break;
825 			}
826 		}
827 		if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
828 			vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
829 		break;
830 	case VTB_MARK_ROW:
831 		vtbuf_flush_mark(vb); /* Clean old mark. */
832 		vb->vb_mark_start.tp_col = 0;
833 		vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
834 		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
835 		    vtbuf_wth(vb, row);
836 		break;
837 	case VTB_MARK_NONE:
838 		vb->vb_mark_last = type;
839 		/* FALLTHROUGH */
840 	default:
841 		/* panic? */
842 		return (0);
843 	}
844 
845 	vb->vb_mark_last = type;
846 	/* Draw new marked region. */
847 	vtbuf_flush_mark(vb);
848 	return (1);
849 }
850 #endif
851 
852 void
853 vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
854 {
855 	int oflags, nflags;
856 
857 	oflags = vb->vb_flags;
858 	if (yes)
859 		vb->vb_flags |= VBF_CURSOR;
860 	else
861 		vb->vb_flags &= ~VBF_CURSOR;
862 	nflags = vb->vb_flags;
863 
864 	if (oflags != nflags)
865 		vtbuf_dirty_cell(vb, &vb->vb_cursor);
866 }
867 
868 void
869 vtbuf_scroll_mode(struct vt_buf *vb, int yes)
870 {
871 	int oflags, nflags;
872 
873 	VTBUF_LOCK(vb);
874 	oflags = vb->vb_flags;
875 	if (yes)
876 		vb->vb_flags |= VBF_SCROLL;
877 	else
878 		vb->vb_flags &= ~VBF_SCROLL;
879 	nflags = vb->vb_flags;
880 
881 	if (oflags != nflags)
882 		vtbuf_dirty_cell(vb, &vb->vb_cursor);
883 	VTBUF_UNLOCK(vb);
884 }
885