vt_buf.c revision 265681
1219888Sed/*-
2257547Sray * Copyright (c) 2009, 2013 The FreeBSD Foundation
3219888Sed * All rights reserved.
4219888Sed *
5219888Sed * This software was developed by Ed Schouten under sponsorship from the
6219888Sed * FreeBSD Foundation.
7219888Sed *
8257547Sray * Portions of this software were developed by Oleksandr Rybalko
9257547Sray * under sponsorship from the FreeBSD Foundation.
10257547Sray *
11219888Sed * Redistribution and use in source and binary forms, with or without
12219888Sed * modification, are permitted provided that the following conditions
13219888Sed * are met:
14219888Sed * 1. Redistributions of source code must retain the above copyright
15219888Sed *    notice, this list of conditions and the following disclaimer.
16219888Sed * 2. Redistributions in binary form must reproduce the above copyright
17219888Sed *    notice, this list of conditions and the following disclaimer in the
18219888Sed *    documentation and/or other materials provided with the distribution.
19219888Sed *
20219888Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21219888Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22219888Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23219888Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24219888Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25219888Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26219888Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27219888Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28219888Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29219888Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30219888Sed * SUCH DAMAGE.
31219888Sed */
32219888Sed
33219888Sed#include <sys/cdefs.h>
34219888Sed__FBSDID("$FreeBSD: head/sys/dev/vt/vt_buf.c 265681 2014-05-08 13:46:36Z ray $");
35219888Sed
36219888Sed#include <sys/param.h>
37219888Sed#include <sys/kernel.h>
38219888Sed#include <sys/lock.h>
39219888Sed#include <sys/malloc.h>
40219888Sed#include <sys/mutex.h>
41219888Sed#include <sys/systm.h>
42219888Sed
43219888Sed#include <dev/vt/vt.h>
44219888Sed
45219888Sedstatic MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
46219888Sed
47219888Sed#define	VTBUF_LOCK(vb)		mtx_lock_spin(&(vb)->vb_lock)
48219888Sed#define	VTBUF_UNLOCK(vb)	mtx_unlock_spin(&(vb)->vb_lock)
49257971Sray
50258090Sray#define POS_INDEX(c, r) (((r) << 12) + (c))
51258090Sray#define	POS_COPY(d, s)	do {	\
52258090Sray	(d).tp_col = (s).tp_col;	\
53258090Sray	(d).tp_row = (s).tp_row;	\
54258090Sray} while (0)
55257971Sray
56258090Sray
57256145Sray/*
58256145Sray * line4
59256145Sray * line5 <--- curroffset (terminal output to that line)
60256145Sray * line0
61256145Sray * line1                  <--- roffset (history display from that point)
62256145Sray * line2
63256145Sray * line3
64256145Sray */
65256145Srayint
66256145Srayvthistory_seek(struct vt_buf *vb, int offset, int whence)
67256145Sray{
68257074Sray	int diff, top, bottom, roffset;
69219888Sed
70256145Sray	/* No scrolling if not enabled. */
71256145Sray	if ((vb->vb_flags & VBF_SCROLL) == 0) {
72256145Sray		if (vb->vb_roffset != vb->vb_curroffset) {
73256145Sray			vb->vb_roffset = vb->vb_curroffset;
74257074Sray			return (0xffff);
75256145Sray		}
76256145Sray		return (0); /* No changes */
77256145Sray	}
78256970Sray	top = (vb->vb_flags & VBF_HISTORY_FULL)?
79256970Sray	    (vb->vb_curroffset + vb->vb_scr_size.tp_row):vb->vb_history_size;
80256970Sray	bottom = vb->vb_curroffset + vb->vb_history_size;
81256970Sray
82256145Sray	/*
83256145Sray	 * Operate on copy of offset value, since it temporary can be bigger
84256145Sray	 * than amount of rows in buffer.
85256145Sray	 */
86256970Sray	roffset = vb->vb_roffset + vb->vb_history_size;
87256145Sray	switch (whence) {
88256145Sray	case VHS_SET:
89257074Sray		roffset = offset + vb->vb_history_size;
90256145Sray		break;
91256145Sray	case VHS_CUR:
92256145Sray		roffset += offset;
93256145Sray		break;
94256145Sray	case VHS_END:
95256145Sray		/* Go to current offset. */
96257074Sray		roffset = vb->vb_curroffset + vb->vb_history_size;
97256145Sray		break;
98256145Sray	}
99256145Sray
100256970Sray	roffset = (roffset < top)?top:roffset;
101256970Sray	roffset = (roffset > bottom)?bottom:roffset;
102256145Sray
103256970Sray	roffset %= vb->vb_history_size;
104256970Sray
105256145Sray	if (vb->vb_roffset != roffset) {
106257074Sray		diff = vb->vb_roffset - roffset;
107256145Sray		vb->vb_roffset = roffset;
108257074Sray		/*
109257074Sray		 * Offset changed, please update Nth lines on sceen.
110257074Sray		 * +N - Nth lines at top;
111257074Sray		 * -N - Nth lines at bottom.
112257074Sray		 */
113257074Sray		return (diff);
114256145Sray	}
115256145Sray	return (0); /* No changes */
116256145Sray}
117256145Sray
118256145Srayvoid
119256145Srayvthistory_addlines(struct vt_buf *vb, int offset)
120256145Sray{
121256145Sray
122256145Sray	vb->vb_curroffset += offset;
123256145Sray	if (vb->vb_curroffset < 0)
124256145Sray		vb->vb_curroffset = 0;
125256145Sray	vb->vb_curroffset %= vb->vb_history_size;
126256145Sray	if ((vb->vb_flags & VBF_SCROLL) == 0) {
127256145Sray		vb->vb_roffset = vb->vb_curroffset;
128256145Sray	}
129256145Sray}
130256145Sray
131256145Srayvoid
132256145Srayvthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
133256145Sray{
134256145Sray
135256145Sray	*offset = vb->vb_roffset;
136256145Sray}
137256145Sray
138259129Sray#ifndef SC_NO_CUTPASTE	/* Only mouse support use it now. */
139258090Sray/* Translate current view row number to history row. */
140258090Sraystatic int
141258090Srayvtbuf_wth(struct vt_buf *vb, int row)
142258090Sray{
143258090Sray
144258090Sray	return ((vb->vb_roffset + row) % vb->vb_history_size);
145258090Sray}
146259129Sray#endif
147258090Sray
148258090Sray/* Translate history row to current view row number. */
149258090Sraystatic int
150258090Srayvtbuf_htw(struct vt_buf *vb, int row)
151258090Sray{
152258090Sray
153258090Sray	/*
154258090Sray	 * total 1000 rows.
155258090Sray	 * History offset	roffset	winrow
156258090Sray	 *	205		200	((205 - 200 + 1000) % 1000) = 5
157258090Sray	 *	90		990	((90 - 990 + 1000) % 1000) = 100
158258090Sray	 */
159258090Sray	return ((row - vb->vb_roffset + vb->vb_history_size) %
160258090Sray	    vb->vb_history_size);
161258090Sray}
162258090Sray
163257971Srayint
164257971Srayvtbuf_iscursor(struct vt_buf *vb, int row, int col)
165257971Sray{
166258090Sray	int sc, sr, ec, er, tmp;
167258090Sray
168258090Sray	if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
169258090Sray	    (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
170257971Sray		return (1);
171257971Sray
172258090Sray	/* Mark cut/paste region. */
173258090Sray
174258090Sray	/*
175258090Sray	 * Luckily screen view is not like circular buffer, so we will
176258090Sray	 * calculate in screen coordinates.  Translate first.
177258090Sray	 */
178258090Sray	sc = vb->vb_mark_start.tp_col;
179258090Sray	sr = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
180258090Sray	ec = vb->vb_mark_end.tp_col;
181258090Sray	er = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
182258090Sray
183258090Sray
184258090Sray	/* Swap start and end if start > end. */
185258090Sray	if (POS_INDEX(sc, sr) > POS_INDEX(ec, er)) {
186258090Sray		tmp = sc; sc = ec; ec = tmp;
187258090Sray		tmp = sr; sr = er; er = tmp;
188258090Sray	}
189258090Sray
190258090Sray	if ((POS_INDEX(sc, sr) <= POS_INDEX(col, row)) &&
191258090Sray	    (POS_INDEX(col, row) < POS_INDEX(ec, er)))
192257971Sray		return (1);
193257971Sray
194257971Sray	return (0);
195257971Sray}
196257971Sray
197219888Sedstatic inline uint64_t
198219888Sedvtbuf_dirty_axis(unsigned int begin, unsigned int end)
199219888Sed{
200219888Sed	uint64_t left, right, mask;
201219888Sed
202219888Sed	/*
203219888Sed	 * Mark all bits between begin % 64 and end % 64 dirty.
204219888Sed	 * This code is functionally equivalent to:
205219888Sed	 *
206219888Sed	 * 	for (i = begin; i < end; i++)
207219888Sed	 * 		mask |= (uint64_t)1 << (i % 64);
208219888Sed	 */
209219888Sed
210219888Sed	/* Obvious case. Mark everything dirty. */
211219888Sed	if (end - begin >= 64)
212219888Sed		return (VBM_DIRTY);
213219888Sed
214219888Sed	/* 1....0; used bits on the left. */
215219888Sed	left = VBM_DIRTY << begin % 64;
216219888Sed	/* 0....1; used bits on the right. */
217219888Sed	right = VBM_DIRTY >> -end % 64;
218219888Sed
219219888Sed	/*
220219888Sed	 * Only take the intersection.  If the result of that is 0, it
221219888Sed	 * means that the selection crossed a 64 bit boundary along the
222219888Sed	 * way, which means we have to take the complement.
223219888Sed	 */
224219888Sed	mask = left & right;
225219888Sed	if (mask == 0)
226219888Sed		mask = left | right;
227219888Sed	return (mask);
228219888Sed}
229219888Sed
230219888Sedstatic inline void
231219888Sedvtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
232219888Sed{
233219888Sed
234219888Sed	VTBUF_LOCK(vb);
235219888Sed	if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
236219888Sed		vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
237219888Sed	if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
238219888Sed		vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
239219888Sed	if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
240219888Sed		vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
241219888Sed	if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
242219888Sed		vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
243219888Sed	vb->vb_dirtymask.vbm_row |=
244219888Sed	    vtbuf_dirty_axis(area->tr_begin.tp_row, area->tr_end.tp_row);
245219888Sed	vb->vb_dirtymask.vbm_col |=
246219888Sed	    vtbuf_dirty_axis(area->tr_begin.tp_col, area->tr_end.tp_col);
247219888Sed	VTBUF_UNLOCK(vb);
248219888Sed}
249219888Sed
250219888Sedstatic inline void
251219888Sedvtbuf_dirty_cell(struct vt_buf *vb, const term_pos_t *p)
252219888Sed{
253219888Sed	term_rect_t area;
254219888Sed
255219888Sed	area.tr_begin = *p;
256219888Sed	area.tr_end.tp_row = p->tp_row + 1;
257219888Sed	area.tr_end.tp_col = p->tp_col + 1;
258219888Sed	vtbuf_dirty(vb, &area);
259219888Sed}
260219888Sed
261219888Sedstatic void
262219888Sedvtbuf_make_undirty(struct vt_buf *vb)
263219888Sed{
264219888Sed
265256145Sray	vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
266219888Sed	vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
267219888Sed	vb->vb_dirtymask.vbm_row = vb->vb_dirtymask.vbm_col = 0;
268219888Sed}
269219888Sed
270219888Sedvoid
271219888Sedvtbuf_undirty(struct vt_buf *vb, term_rect_t *r, struct vt_bufmask *m)
272219888Sed{
273219888Sed
274219888Sed	VTBUF_LOCK(vb);
275219888Sed	*r = vb->vb_dirtyrect;
276219888Sed	*m = vb->vb_dirtymask;
277219888Sed	vtbuf_make_undirty(vb);
278219888Sed	VTBUF_UNLOCK(vb);
279219888Sed}
280219888Sed
281219888Sedvoid
282219888Sedvtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
283219888Sed{
284219888Sed	const term_pos_t *p1 = &r->tr_begin;
285219888Sed	term_rect_t area;
286219888Sed	unsigned int rows, cols;
287256145Sray	int pr, rdiff;
288219888Sed
289256145Sray	KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
290256145Sray	    ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
291256145Sray		r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
292256145Sray	KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
293256145Sray	    ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
294256145Sray		r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
295256145Sray
296256145Sray	KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
297256145Sray	    ("vtbuf_copy end.tp_row %d must be less than screen width %d",
298256145Sray		r->tr_end.tp_row, vb->vb_scr_size.tp_row));
299256145Sray	KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
300256145Sray	    ("vtbuf_copy end.tp_col %d must be less than screen height %d",
301256145Sray		r->tr_end.tp_col, vb->vb_scr_size.tp_col));
302256145Sray
303256145Sray	KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
304256145Sray	    ("vtbuf_copy tp_row %d must be less than screen width %d",
305256145Sray		p2->tp_row, vb->vb_scr_size.tp_row));
306256145Sray	KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
307256145Sray	    ("vtbuf_copy tp_col %d must be less than screen height %d",
308256145Sray		p2->tp_col, vb->vb_scr_size.tp_col));
309256145Sray
310219888Sed	rows = r->tr_end.tp_row - r->tr_begin.tp_row;
311256145Sray	rdiff = r->tr_begin.tp_row - p2->tp_row;
312219888Sed	cols = r->tr_end.tp_col - r->tr_begin.tp_col;
313256145Sray	if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
314256145Sray	    r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
315256145Sray	    (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
316256145Sray	    rdiff > 0) { /* Only forward dirrection. Do not eat history. */
317256145Sray		vthistory_addlines(vb, rdiff);
318256145Sray	} else if (p2->tp_row < p1->tp_row) {
319256145Sray		/* Handle overlapping copies of line segments. */
320219888Sed		/* Move data up. */
321219888Sed		for (pr = 0; pr < rows; pr++)
322219888Sed			memmove(
323219888Sed			    &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
324219888Sed			    &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
325219888Sed			    cols * sizeof(term_char_t));
326219888Sed	} else {
327219888Sed		/* Move data down. */
328219888Sed		for (pr = rows - 1; pr >= 0; pr--)
329219888Sed			memmove(
330219888Sed			    &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
331219888Sed			    &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
332219888Sed			    cols * sizeof(term_char_t));
333219888Sed	}
334219888Sed
335219888Sed	area.tr_begin = *p2;
336256145Sray	area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
337256145Sray	area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
338219888Sed	vtbuf_dirty(vb, &area);
339219888Sed}
340219888Sed
341256145Sraystatic void
342219888Sedvtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
343219888Sed{
344219888Sed	unsigned int pr, pc;
345256145Sray	term_char_t *row;
346219888Sed
347256145Sray	for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
348256145Sray		row = vb->vb_rows[(vb->vb_curroffset + pr) %
349256145Sray		    VTBUF_MAX_HEIGHT(vb)];
350256145Sray		for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
351256145Sray			row[pc] = c;
352256145Sray		}
353256145Sray	}
354256145Sray}
355219888Sed
356256145Srayvoid
357256145Srayvtbuf_fill_locked(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
358256145Sray{
359256145Sray	KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
360256145Sray	    ("vtbuf_fill_locked begin.tp_row %d must be < screen width %d",
361256145Sray		r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
362256145Sray	KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
363256145Sray	    ("vtbuf_fill_locked begin.tp_col %d must be < screen height %d",
364256145Sray		r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
365256145Sray
366256145Sray	KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
367256145Sray	    ("vtbuf_fill_locked end.tp_row %d must be <= screen width %d",
368256145Sray		r->tr_end.tp_row, vb->vb_scr_size.tp_row));
369256145Sray	KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
370256145Sray	    ("vtbuf_fill_locked end.tp_col %d must be <= screen height %d",
371256145Sray		r->tr_end.tp_col, vb->vb_scr_size.tp_col));
372256145Sray
373256145Sray	VTBUF_LOCK(vb);
374256145Sray	vtbuf_fill(vb, r, c);
375256145Sray	VTBUF_UNLOCK(vb);
376256145Sray
377219888Sed	vtbuf_dirty(vb, r);
378219888Sed}
379219888Sed
380256145Sraystatic void
381256145Srayvtbuf_init_rows(struct vt_buf *vb)
382256145Sray{
383256145Sray	int r;
384256145Sray
385256145Sray	vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
386256145Sray
387256145Sray	for (r = 0; r < vb->vb_history_size; r++)
388256145Sray		vb->vb_rows[r] = &vb->vb_buffer[r *
389256145Sray		    vb->vb_scr_size.tp_col];
390256145Sray}
391256145Sray
392219888Sedvoid
393219888Sedvtbuf_init_early(struct vt_buf *vb)
394219888Sed{
395219888Sed
396219888Sed	vb->vb_flags |= VBF_CURSOR;
397256145Sray	vb->vb_roffset = 0;
398256145Sray	vb->vb_curroffset = 0;
399258090Sray	vb->vb_mark_start.tp_row = 0;
400258090Sray	vb->vb_mark_start.tp_col = 0;
401258090Sray	vb->vb_mark_end.tp_row = 0;
402258090Sray	vb->vb_mark_end.tp_col = 0;
403256145Sray
404256145Sray	vtbuf_init_rows(vb);
405219888Sed	vtbuf_make_undirty(vb);
406256145Sray	if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
407256145Sray		mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
408256145Sray		vb->vb_flags |= VBF_MTX_INIT;
409256145Sray	}
410219888Sed}
411219888Sed
412219888Sedvoid
413219888Sedvtbuf_init(struct vt_buf *vb, const term_pos_t *p)
414219888Sed{
415256145Sray	int sz;
416219888Sed
417256145Sray	vb->vb_scr_size = *p;
418256145Sray	vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
419256145Sray
420256145Sray	if ((vb->vb_flags & VBF_STATIC) == 0) {
421256145Sray		sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
422256894Sray		vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
423256145Sray
424256145Sray		sz = vb->vb_history_size * sizeof(term_char_t *);
425256894Sray		vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
426256145Sray	}
427256145Sray
428219888Sed	vtbuf_init_early(vb);
429219888Sed}
430219888Sed
431219888Sedvoid
432256145Srayvtbuf_sethistory_size(struct vt_buf *vb, int size)
433219888Sed{
434256145Sray	term_pos_t p;
435219888Sed
436256145Sray	/* With same size */
437256145Sray	p.tp_row = vb->vb_scr_size.tp_row;
438256145Sray	p.tp_col = vb->vb_scr_size.tp_col;
439256145Sray	vtbuf_grow(vb, &p, size);
440256145Sray}
441256145Sray
442256145Srayvoid
443256145Srayvtbuf_grow(struct vt_buf *vb, const term_pos_t *p, int history_size)
444256145Sray{
445256145Sray	term_char_t *old, *new, **rows, **oldrows, **copyrows, *row;
446256145Sray	int bufsize, rowssize, w, h, c, r;
447256145Sray	term_rect_t rect;
448256145Sray
449256145Sray	history_size = MAX(history_size, p->tp_row);
450256145Sray
451265681Sray	/* If new screen/history size bigger or buffer is VBF_STATIC. */
452265681Sray	if ((history_size > vb->vb_history_size) || (p->tp_col >
453265681Sray	    vb->vb_scr_size.tp_col) || (vb->vb_flags & VBF_STATIC)) {
454219888Sed		/* Allocate new buffer. */
455256145Sray		bufsize = history_size * p->tp_col * sizeof(term_char_t);
456256894Sray		new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
457256145Sray		rowssize = history_size * sizeof(term_pos_t *);
458256894Sray		rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
459219888Sed
460219888Sed		/* Toggle it. */
461219888Sed		VTBUF_LOCK(vb);
462219888Sed		old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
463256145Sray		oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
464256145Sray		copyrows = vb->vb_rows;
465256145Sray		w = vb->vb_scr_size.tp_col;
466256145Sray		h = vb->vb_history_size;
467256145Sray
468256145Sray		vb->vb_history_size = history_size;
469219888Sed		vb->vb_buffer = new;
470256145Sray		vb->vb_rows = rows;
471219888Sed		vb->vb_flags &= ~VBF_STATIC;
472256145Sray		vb->vb_scr_size = *p;
473256145Sray		vtbuf_init_rows(vb);
474256145Sray
475256145Sray		/* Copy history and fill extra space. */
476256145Sray		for (r = 0; r < history_size; r ++) {
477256145Sray			row = rows[r];
478256145Sray			if (r < h) { /* Copy. */
479256145Sray				memmove(rows[r], copyrows[r],
480256145Sray				    MIN(p->tp_col, w) * sizeof(term_char_t));
481256145Sray				for (c = MIN(p->tp_col, w); c < p->tp_col;
482256145Sray				    c++) {
483256145Sray					row[c] = VTBUF_SPACE_CHAR;
484256145Sray				}
485256145Sray			} else { /* Just fill. */
486256145Sray				rect.tr_begin.tp_col = 0;
487256145Sray				rect.tr_begin.tp_row = r;
488256145Sray				rect.tr_end.tp_col = p->tp_col;
489256145Sray				rect.tr_end.tp_row = p->tp_row;
490256145Sray				vtbuf_fill(vb, &rect, VTBUF_SPACE_CHAR);
491256145Sray				break;
492256145Sray			}
493256145Sray		}
494219888Sed		vtbuf_make_undirty(vb);
495219888Sed		VTBUF_UNLOCK(vb);
496219888Sed		/* Deallocate old buffer. */
497256145Sray		free(old, M_VTBUF);
498256145Sray		free(oldrows, M_VTBUF);
499219888Sed	}
500219888Sed}
501219888Sed
502219888Sedvoid
503219888Sedvtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
504219888Sed{
505256145Sray	term_char_t *row;
506219888Sed
507256145Sray	KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
508256145Sray	    ("vtbuf_putchar tp_row %d must be less than screen width %d",
509256145Sray		p->tp_row, vb->vb_scr_size.tp_row));
510256145Sray	KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
511256145Sray	    ("vtbuf_putchar tp_col %d must be less than screen height %d",
512256145Sray		p->tp_col, vb->vb_scr_size.tp_col));
513256145Sray
514256145Sray	row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
515256145Sray	    VTBUF_MAX_HEIGHT(vb)];
516256145Sray	if (row[p->tp_col] != c) {
517256145Sray		VTBUF_LOCK(vb);
518256145Sray		row[p->tp_col] = c;
519256145Sray		VTBUF_UNLOCK(vb);
520219888Sed		vtbuf_dirty_cell(vb, p);
521219888Sed	}
522219888Sed}
523219888Sed
524219888Sedvoid
525219888Sedvtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
526219888Sed{
527219888Sed
528219888Sed	if (vb->vb_flags & VBF_CURSOR) {
529219888Sed		vtbuf_dirty_cell(vb, &vb->vb_cursor);
530219888Sed		vb->vb_cursor = *p;
531219888Sed		vtbuf_dirty_cell(vb, &vb->vb_cursor);
532219888Sed	} else {
533219888Sed		vb->vb_cursor = *p;
534219888Sed	}
535219888Sed}
536219888Sed
537259129Sray#ifndef SC_NO_CUTPASTE
538219888Sedvoid
539257971Srayvtbuf_mouse_cursor_position(struct vt_buf *vb, int col, int row)
540257971Sray{
541257971Sray	term_rect_t area;
542257971Sray
543257971Sray	area.tr_begin.tp_row = MAX(row - 1, 0);
544257971Sray	area.tr_begin.tp_col = MAX(col - 1, 0);
545257971Sray	area.tr_end.tp_row = MIN(row + 2, vb->vb_scr_size.tp_row);
546257971Sray	area.tr_end.tp_col = MIN(col + 2, vb->vb_scr_size.tp_col);
547257971Sray	vtbuf_dirty(vb, &area);
548257971Sray}
549257971Sray
550258090Sraystatic void
551258090Srayvtbuf_flush_mark(struct vt_buf *vb)
552258090Sray{
553258090Sray	term_rect_t area;
554258090Sray	int s, e;
555258090Sray
556258090Sray	/* Notify renderer to update marked region. */
557258090Sray	if (vb->vb_mark_start.tp_col || vb->vb_mark_end.tp_col ||
558258090Sray	    vb->vb_mark_start.tp_row || vb->vb_mark_end.tp_row) {
559258090Sray
560258090Sray		s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
561258090Sray		e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
562258090Sray
563258090Sray		area.tr_begin.tp_col = 0;
564258090Sray		area.tr_begin.tp_row = MIN(s, e);
565258090Sray
566258090Sray		area.tr_end.tp_col = vb->vb_scr_size.tp_col;
567258090Sray		area.tr_end.tp_row = MAX(s, e) + 1;
568258090Sray
569258090Sray		vtbuf_dirty(vb, &area);
570258090Sray	}
571258090Sray}
572258090Sray
573258090Srayint
574258090Srayvtbuf_get_marked_len(struct vt_buf *vb)
575258090Sray{
576258090Sray	int ei, si, sz;
577258090Sray	term_pos_t s, e;
578258090Sray
579258090Sray	/* Swap according to window coordinates. */
580258134Sray	if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
581258134Sray	    vb->vb_mark_start.tp_col) >
582258134Sray	    POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
583258134Sray	    vb->vb_mark_end.tp_col)) {
584258090Sray		POS_COPY(e, vb->vb_mark_start);
585258090Sray		POS_COPY(s, vb->vb_mark_end);
586258090Sray	} else {
587258090Sray		POS_COPY(s, vb->vb_mark_start);
588258090Sray		POS_COPY(e, vb->vb_mark_end);
589258090Sray	}
590258090Sray
591258090Sray	si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
592258090Sray	ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
593258090Sray
594258090Sray	/* Number symbols and number of rows to inject \n */
595258090Sray	sz = ei - si + ((e.tp_row - s.tp_row) * 2) + 1;
596258090Sray
597258090Sray	return (sz * sizeof(term_char_t));
598258090Sray}
599258090Sray
600257971Srayvoid
601258090Srayvtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
602258090Sray{
603258090Sray	int i, r, c, cs, ce;
604258090Sray	term_pos_t s, e;
605258090Sray
606258090Sray	/* Swap according to window coordinates. */
607258134Sray	if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
608258134Sray	    vb->vb_mark_start.tp_col) >
609258134Sray	    POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
610258134Sray	    vb->vb_mark_end.tp_col)) {
611258090Sray		POS_COPY(e, vb->vb_mark_start);
612258090Sray		POS_COPY(s, vb->vb_mark_end);
613258090Sray	} else {
614258090Sray		POS_COPY(s, vb->vb_mark_start);
615258090Sray		POS_COPY(e, vb->vb_mark_end);
616258090Sray	}
617258090Sray
618258090Sray	i = 0;
619258090Sray	for (r = s.tp_row; r <= e.tp_row; r ++) {
620258090Sray		cs = (r == s.tp_row)?s.tp_col:0;
621258090Sray		ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
622258090Sray		for (c = cs; c < ce; c ++) {
623258090Sray			buf[i++] = vb->vb_rows[r][c];
624258090Sray		}
625258093Sray		/* Add new line for all rows, but not for last one. */
626258093Sray		if (r != e.tp_row) {
627258093Sray			buf[i++] = '\r';
628258093Sray			buf[i++] = '\n';
629258093Sray		}
630258090Sray	}
631258090Sray}
632258090Sray
633258090Srayint
634257971Srayvtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
635257971Sray{
636258136Sray	term_char_t *r;
637258136Sray	int i;
638257971Sray
639257971Sray	switch (type) {
640258130Sray	case VTB_MARK_END:	/* B1 UP */
641258130Sray		if (vb->vb_mark_last != VTB_MARK_MOVE)
642258130Sray			return (0);
643258130Sray		/* FALLTHROUGH */
644258130Sray	case VTB_MARK_MOVE:
645257971Sray	case VTB_MARK_EXTEND:
646258090Sray		vtbuf_flush_mark(vb); /* Clean old mark. */
647257971Sray		vb->vb_mark_end.tp_col = col;
648258090Sray		vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
649257971Sray		break;
650257971Sray	case VTB_MARK_START:
651258090Sray		vtbuf_flush_mark(vb); /* Clean old mark. */
652257971Sray		vb->vb_mark_start.tp_col = col;
653258090Sray		vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
654257971Sray		/* Start again, so clear end point. */
655258090Sray		vb->vb_mark_end.tp_col = col;
656258090Sray		vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
657257971Sray		break;
658257971Sray	case VTB_MARK_WORD:
659258090Sray		vtbuf_flush_mark(vb); /* Clean old mark. */
660258090Sray		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
661258090Sray		    vtbuf_wth(vb, row);
662258136Sray		r = vb->vb_rows[vb->vb_mark_start.tp_row];
663258136Sray		for (i = col; i >= 0; i --) {
664258136Sray			if (TCHAR_CHARACTER(r[i]) == ' ') {
665258136Sray				vb->vb_mark_start.tp_col = i + 1;
666258136Sray				break;
667258136Sray			}
668258136Sray		}
669258136Sray		for (i = col; i < vb->vb_scr_size.tp_col; i ++) {
670258136Sray			if (TCHAR_CHARACTER(r[i]) == ' ') {
671258136Sray				vb->vb_mark_end.tp_col = i;
672258136Sray				break;
673258136Sray			}
674258136Sray		}
675258136Sray		if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
676258136Sray			vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
677257971Sray		break;
678257971Sray	case VTB_MARK_ROW:
679258090Sray		vtbuf_flush_mark(vb); /* Clean old mark. */
680257971Sray		vb->vb_mark_start.tp_col = 0;
681257971Sray		vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
682258090Sray		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
683258090Sray		    vtbuf_wth(vb, row);
684257971Sray		break;
685258090Sray	case VTB_MARK_NONE:
686258130Sray		vb->vb_mark_last = type;
687258130Sray		/* FALLTHROUGH */
688258090Sray	default:
689258090Sray		/* panic? */
690258090Sray		return (0);
691257971Sray	}
692258111Sray
693258130Sray	vb->vb_mark_last = type;
694258111Sray	/* Draw new marked region. */
695258111Sray	vtbuf_flush_mark(vb);
696258111Sray	return (1);
697257971Sray}
698259129Sray#endif
699257971Sray
700257971Srayvoid
701219888Sedvtbuf_cursor_visibility(struct vt_buf *vb, int yes)
702219888Sed{
703219888Sed	int oflags, nflags;
704219888Sed
705219888Sed	VTBUF_LOCK(vb);
706219888Sed	oflags = vb->vb_flags;
707219888Sed	if (yes)
708219888Sed		vb->vb_flags |= VBF_CURSOR;
709219888Sed	else
710219888Sed		vb->vb_flags &= ~VBF_CURSOR;
711219888Sed	nflags = vb->vb_flags;
712219888Sed	VTBUF_UNLOCK(vb);
713219888Sed
714219888Sed	if (oflags != nflags)
715219888Sed		vtbuf_dirty_cell(vb, &vb->vb_cursor);
716219888Sed}
717258090Sray
718258090Srayvoid
719258090Srayvtbuf_scroll_mode(struct vt_buf *vb, int yes)
720258090Sray{
721258090Sray	int oflags, nflags;
722258090Sray
723258090Sray	VTBUF_LOCK(vb);
724258090Sray	oflags = vb->vb_flags;
725258090Sray	if (yes)
726258090Sray		vb->vb_flags |= VBF_SCROLL;
727258090Sray	else
728258090Sray		vb->vb_flags &= ~VBF_SCROLL;
729258090Sray	nflags = vb->vb_flags;
730258090Sray	VTBUF_UNLOCK(vb);
731258090Sray
732258090Sray	if (oflags != nflags)
733258090Sray		vtbuf_dirty_cell(vb, &vb->vb_cursor);
734258090Sray}
735258090Sray
736