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