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