vt_buf.c revision 262861
1/*-
2 * Copyright (c) 2009, 2013 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Ed Schouten under sponsorship from the
6 * FreeBSD Foundation.
7 *
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/10/sys/dev/vt/vt_buf.c 262861 2014-03-06 18:30:56Z jhb $");
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/mutex.h>
41#include <sys/systm.h>
42
43#include <dev/vt/vt.h>
44
45static 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
57/*
58 * line4
59 * line5 <--- curroffset (terminal output to that line)
60 * line0
61 * line1                  <--- roffset (history display from that point)
62 * line2
63 * line3
64 */
65int
66vthistory_seek(struct vt_buf *vb, int offset, int whence)
67{
68	int diff, top, bottom, roffset;
69
70	/* No scrolling if not enabled. */
71	if ((vb->vb_flags & VBF_SCROLL) == 0) {
72		if (vb->vb_roffset != vb->vb_curroffset) {
73			vb->vb_roffset = vb->vb_curroffset;
74			return (0xffff);
75		}
76		return (0); /* No changes */
77	}
78	top = (vb->vb_flags & VBF_HISTORY_FULL)?
79	    (vb->vb_curroffset + vb->vb_scr_size.tp_row):vb->vb_history_size;
80	bottom = vb->vb_curroffset + vb->vb_history_size;
81
82	/*
83	 * Operate on copy of offset value, since it temporary can be bigger
84	 * than amount of rows in buffer.
85	 */
86	roffset = vb->vb_roffset + vb->vb_history_size;
87	switch (whence) {
88	case VHS_SET:
89		roffset = offset + vb->vb_history_size;
90		break;
91	case VHS_CUR:
92		roffset += offset;
93		break;
94	case VHS_END:
95		/* Go to current offset. */
96		roffset = vb->vb_curroffset + vb->vb_history_size;
97		break;
98	}
99
100	roffset = (roffset < top)?top:roffset;
101	roffset = (roffset > bottom)?bottom:roffset;
102
103	roffset %= vb->vb_history_size;
104
105	if (vb->vb_roffset != roffset) {
106		diff = vb->vb_roffset - roffset;
107		vb->vb_roffset = roffset;
108		/*
109		 * Offset changed, please update Nth lines on sceen.
110		 * +N - Nth lines at top;
111		 * -N - Nth lines at bottom.
112		 */
113		return (diff);
114	}
115	return (0); /* No changes */
116}
117
118void
119vthistory_addlines(struct vt_buf *vb, int offset)
120{
121
122	vb->vb_curroffset += offset;
123	if (vb->vb_curroffset < 0)
124		vb->vb_curroffset = 0;
125	vb->vb_curroffset %= vb->vb_history_size;
126	if ((vb->vb_flags & VBF_SCROLL) == 0) {
127		vb->vb_roffset = vb->vb_curroffset;
128	}
129}
130
131void
132vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
133{
134
135	*offset = vb->vb_roffset;
136}
137
138#ifndef SC_NO_CUTPASTE	/* Only mouse support use it now. */
139/* Translate current view row number to history row. */
140static int
141vtbuf_wth(struct vt_buf *vb, int row)
142{
143
144	return ((vb->vb_roffset + row) % vb->vb_history_size);
145}
146#endif
147
148/* Translate history row to current view row number. */
149static int
150vtbuf_htw(struct vt_buf *vb, int row)
151{
152
153	/*
154	 * total 1000 rows.
155	 * History offset	roffset	winrow
156	 *	205		200	((205 - 200 + 1000) % 1000) = 5
157	 *	90		990	((90 - 990 + 1000) % 1000) = 100
158	 */
159	return ((row - vb->vb_roffset + vb->vb_history_size) %
160	    vb->vb_history_size);
161}
162
163int
164vtbuf_iscursor(struct vt_buf *vb, int row, int col)
165{
166	int sc, sr, ec, er, tmp;
167
168	if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
169	    (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
170		return (1);
171
172	/* Mark cut/paste region. */
173
174	/*
175	 * Luckily screen view is not like circular buffer, so we will
176	 * calculate in screen coordinates.  Translate first.
177	 */
178	sc = vb->vb_mark_start.tp_col;
179	sr = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
180	ec = vb->vb_mark_end.tp_col;
181	er = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
182
183
184	/* Swap start and end if start > end. */
185	if (POS_INDEX(sc, sr) > POS_INDEX(ec, er)) {
186		tmp = sc; sc = ec; ec = tmp;
187		tmp = sr; sr = er; er = tmp;
188	}
189
190	if ((POS_INDEX(sc, sr) <= POS_INDEX(col, row)) &&
191	    (POS_INDEX(col, row) < POS_INDEX(ec, er)))
192		return (1);
193
194	return (0);
195}
196
197static inline uint64_t
198vtbuf_dirty_axis(unsigned int begin, unsigned int end)
199{
200	uint64_t left, right, mask;
201
202	/*
203	 * Mark all bits between begin % 64 and end % 64 dirty.
204	 * This code is functionally equivalent to:
205	 *
206	 * 	for (i = begin; i < end; i++)
207	 * 		mask |= (uint64_t)1 << (i % 64);
208	 */
209
210	/* Obvious case. Mark everything dirty. */
211	if (end - begin >= 64)
212		return (VBM_DIRTY);
213
214	/* 1....0; used bits on the left. */
215	left = VBM_DIRTY << begin % 64;
216	/* 0....1; used bits on the right. */
217	right = VBM_DIRTY >> -end % 64;
218
219	/*
220	 * Only take the intersection.  If the result of that is 0, it
221	 * means that the selection crossed a 64 bit boundary along the
222	 * way, which means we have to take the complement.
223	 */
224	mask = left & right;
225	if (mask == 0)
226		mask = left | right;
227	return (mask);
228}
229
230static inline void
231vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
232{
233
234	VTBUF_LOCK(vb);
235	if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
236		vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
237	if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
238		vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
239	if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
240		vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
241	if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
242		vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
243	vb->vb_dirtymask.vbm_row |=
244	    vtbuf_dirty_axis(area->tr_begin.tp_row, area->tr_end.tp_row);
245	vb->vb_dirtymask.vbm_col |=
246	    vtbuf_dirty_axis(area->tr_begin.tp_col, area->tr_end.tp_col);
247	VTBUF_UNLOCK(vb);
248}
249
250static inline void
251vtbuf_dirty_cell(struct vt_buf *vb, const term_pos_t *p)
252{
253	term_rect_t area;
254
255	area.tr_begin = *p;
256	area.tr_end.tp_row = p->tp_row + 1;
257	area.tr_end.tp_col = p->tp_col + 1;
258	vtbuf_dirty(vb, &area);
259}
260
261static void
262vtbuf_make_undirty(struct vt_buf *vb)
263{
264
265	vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
266	vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
267	vb->vb_dirtymask.vbm_row = vb->vb_dirtymask.vbm_col = 0;
268}
269
270void
271vtbuf_undirty(struct vt_buf *vb, term_rect_t *r, struct vt_bufmask *m)
272{
273
274	VTBUF_LOCK(vb);
275	*r = vb->vb_dirtyrect;
276	*m = vb->vb_dirtymask;
277	vtbuf_make_undirty(vb);
278	VTBUF_UNLOCK(vb);
279}
280
281void
282vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
283{
284	const term_pos_t *p1 = &r->tr_begin;
285	term_rect_t area;
286	unsigned int rows, cols;
287	int pr, rdiff;
288
289	KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
290	    ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
291		r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
292	KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
293	    ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
294		r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
295
296	KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
297	    ("vtbuf_copy end.tp_row %d must be less than screen width %d",
298		r->tr_end.tp_row, vb->vb_scr_size.tp_row));
299	KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
300	    ("vtbuf_copy end.tp_col %d must be less than screen height %d",
301		r->tr_end.tp_col, vb->vb_scr_size.tp_col));
302
303	KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
304	    ("vtbuf_copy tp_row %d must be less than screen width %d",
305		p2->tp_row, vb->vb_scr_size.tp_row));
306	KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
307	    ("vtbuf_copy tp_col %d must be less than screen height %d",
308		p2->tp_col, vb->vb_scr_size.tp_col));
309
310	rows = r->tr_end.tp_row - r->tr_begin.tp_row;
311	rdiff = r->tr_begin.tp_row - p2->tp_row;
312	cols = r->tr_end.tp_col - r->tr_begin.tp_col;
313	if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
314	    r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
315	    (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
316	    rdiff > 0) { /* Only forward dirrection. Do not eat history. */
317		vthistory_addlines(vb, rdiff);
318	} else if (p2->tp_row < p1->tp_row) {
319		/* Handle overlapping copies of line segments. */
320		/* Move data up. */
321		for (pr = 0; pr < rows; pr++)
322			memmove(
323			    &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
324			    &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
325			    cols * sizeof(term_char_t));
326	} else {
327		/* Move data down. */
328		for (pr = rows - 1; pr >= 0; pr--)
329			memmove(
330			    &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
331			    &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
332			    cols * sizeof(term_char_t));
333	}
334
335	area.tr_begin = *p2;
336	area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
337	area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
338	vtbuf_dirty(vb, &area);
339}
340
341static void
342vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
343{
344	unsigned int pr, pc;
345	term_char_t *row;
346
347	for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
348		row = vb->vb_rows[(vb->vb_curroffset + pr) %
349		    VTBUF_MAX_HEIGHT(vb)];
350		for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
351			row[pc] = c;
352		}
353	}
354}
355
356void
357vtbuf_fill_locked(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
358{
359	KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
360	    ("vtbuf_fill_locked begin.tp_row %d must be < screen width %d",
361		r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
362	KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
363	    ("vtbuf_fill_locked begin.tp_col %d must be < screen height %d",
364		r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
365
366	KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
367	    ("vtbuf_fill_locked end.tp_row %d must be <= screen width %d",
368		r->tr_end.tp_row, vb->vb_scr_size.tp_row));
369	KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
370	    ("vtbuf_fill_locked end.tp_col %d must be <= screen height %d",
371		r->tr_end.tp_col, vb->vb_scr_size.tp_col));
372
373	VTBUF_LOCK(vb);
374	vtbuf_fill(vb, r, c);
375	VTBUF_UNLOCK(vb);
376
377	vtbuf_dirty(vb, r);
378}
379
380static void
381vtbuf_init_rows(struct vt_buf *vb)
382{
383	int r;
384
385	vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
386
387	for (r = 0; r < vb->vb_history_size; r++)
388		vb->vb_rows[r] = &vb->vb_buffer[r *
389		    vb->vb_scr_size.tp_col];
390}
391
392void
393vtbuf_init_early(struct vt_buf *vb)
394{
395
396	vb->vb_flags |= VBF_CURSOR;
397	vb->vb_roffset = 0;
398	vb->vb_curroffset = 0;
399	vb->vb_mark_start.tp_row = 0;
400	vb->vb_mark_start.tp_col = 0;
401	vb->vb_mark_end.tp_row = 0;
402	vb->vb_mark_end.tp_col = 0;
403
404	vtbuf_init_rows(vb);
405	vtbuf_make_undirty(vb);
406	if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
407		mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
408		vb->vb_flags |= VBF_MTX_INIT;
409	}
410}
411
412void
413vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
414{
415	int sz;
416
417	vb->vb_scr_size = *p;
418	vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
419
420	if ((vb->vb_flags & VBF_STATIC) == 0) {
421		sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
422		vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
423
424		sz = vb->vb_history_size * sizeof(term_char_t *);
425		vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
426	}
427
428	vtbuf_init_early(vb);
429}
430
431void
432vtbuf_sethistory_size(struct vt_buf *vb, int size)
433{
434	term_pos_t p;
435
436	/* With same size */
437	p.tp_row = vb->vb_scr_size.tp_row;
438	p.tp_col = vb->vb_scr_size.tp_col;
439	vtbuf_grow(vb, &p, size);
440}
441
442void
443vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, int history_size)
444{
445	term_char_t *old, *new, **rows, **oldrows, **copyrows, *row;
446	int bufsize, rowssize, w, h, c, r;
447	term_rect_t rect;
448
449	history_size = MAX(history_size, p->tp_row);
450
451	if (history_size > vb->vb_history_size || p->tp_col >
452	    vb->vb_scr_size.tp_col) {
453		/* Allocate new buffer. */
454		bufsize = history_size * p->tp_col * sizeof(term_char_t);
455		new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
456		rowssize = history_size * sizeof(term_pos_t *);
457		rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
458
459		/* Toggle it. */
460		VTBUF_LOCK(vb);
461		old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
462		oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
463		copyrows = vb->vb_rows;
464		w = vb->vb_scr_size.tp_col;
465		h = vb->vb_history_size;
466
467		vb->vb_history_size = history_size;
468		vb->vb_buffer = new;
469		vb->vb_rows = rows;
470		vb->vb_flags &= ~VBF_STATIC;
471		vb->vb_scr_size = *p;
472		vtbuf_init_rows(vb);
473
474		/* Copy history and fill extra space. */
475		for (r = 0; r < history_size; r ++) {
476			row = rows[r];
477			if (r < h) { /* Copy. */
478				memmove(rows[r], copyrows[r],
479				    MIN(p->tp_col, w) * sizeof(term_char_t));
480				for (c = MIN(p->tp_col, w); c < p->tp_col;
481				    c++) {
482					row[c] = VTBUF_SPACE_CHAR;
483				}
484			} else { /* Just fill. */
485				rect.tr_begin.tp_col = 0;
486				rect.tr_begin.tp_row = r;
487				rect.tr_end.tp_col = p->tp_col;
488				rect.tr_end.tp_row = p->tp_row;
489				vtbuf_fill(vb, &rect, VTBUF_SPACE_CHAR);
490				break;
491			}
492		}
493		vtbuf_make_undirty(vb);
494		VTBUF_UNLOCK(vb);
495		/* Deallocate old buffer. */
496		free(old, M_VTBUF);
497		free(oldrows, M_VTBUF);
498	}
499}
500
501void
502vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
503{
504	term_char_t *row;
505
506	KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
507	    ("vtbuf_putchar tp_row %d must be less than screen width %d",
508		p->tp_row, vb->vb_scr_size.tp_row));
509	KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
510	    ("vtbuf_putchar tp_col %d must be less than screen height %d",
511		p->tp_col, vb->vb_scr_size.tp_col));
512
513	row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
514	    VTBUF_MAX_HEIGHT(vb)];
515	if (row[p->tp_col] != c) {
516		VTBUF_LOCK(vb);
517		row[p->tp_col] = c;
518		VTBUF_UNLOCK(vb);
519		vtbuf_dirty_cell(vb, p);
520	}
521}
522
523void
524vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
525{
526
527	if (vb->vb_flags & VBF_CURSOR) {
528		vtbuf_dirty_cell(vb, &vb->vb_cursor);
529		vb->vb_cursor = *p;
530		vtbuf_dirty_cell(vb, &vb->vb_cursor);
531	} else {
532		vb->vb_cursor = *p;
533	}
534}
535
536#ifndef SC_NO_CUTPASTE
537void
538vtbuf_mouse_cursor_position(struct vt_buf *vb, int col, int row)
539{
540	term_rect_t area;
541
542	area.tr_begin.tp_row = MAX(row - 1, 0);
543	area.tr_begin.tp_col = MAX(col - 1, 0);
544	area.tr_end.tp_row = MIN(row + 2, vb->vb_scr_size.tp_row);
545	area.tr_end.tp_col = MIN(col + 2, vb->vb_scr_size.tp_col);
546	vtbuf_dirty(vb, &area);
547}
548
549static void
550vtbuf_flush_mark(struct vt_buf *vb)
551{
552	term_rect_t area;
553	int s, e;
554
555	/* Notify renderer to update marked region. */
556	if (vb->vb_mark_start.tp_col || vb->vb_mark_end.tp_col ||
557	    vb->vb_mark_start.tp_row || vb->vb_mark_end.tp_row) {
558
559		s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
560		e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
561
562		area.tr_begin.tp_col = 0;
563		area.tr_begin.tp_row = MIN(s, e);
564
565		area.tr_end.tp_col = vb->vb_scr_size.tp_col;
566		area.tr_end.tp_row = MAX(s, e) + 1;
567
568		vtbuf_dirty(vb, &area);
569	}
570}
571
572int
573vtbuf_get_marked_len(struct vt_buf *vb)
574{
575	int ei, si, sz;
576	term_pos_t s, e;
577
578	/* Swap according to window coordinates. */
579	if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
580	    vb->vb_mark_start.tp_col) >
581	    POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
582	    vb->vb_mark_end.tp_col)) {
583		POS_COPY(e, vb->vb_mark_start);
584		POS_COPY(s, vb->vb_mark_end);
585	} else {
586		POS_COPY(s, vb->vb_mark_start);
587		POS_COPY(e, vb->vb_mark_end);
588	}
589
590	si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
591	ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
592
593	/* Number symbols and number of rows to inject \n */
594	sz = ei - si + ((e.tp_row - s.tp_row) * 2) + 1;
595
596	return (sz * sizeof(term_char_t));
597}
598
599void
600vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
601{
602	int i, r, c, cs, ce;
603	term_pos_t s, e;
604
605	/* Swap according to window coordinates. */
606	if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
607	    vb->vb_mark_start.tp_col) >
608	    POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
609	    vb->vb_mark_end.tp_col)) {
610		POS_COPY(e, vb->vb_mark_start);
611		POS_COPY(s, vb->vb_mark_end);
612	} else {
613		POS_COPY(s, vb->vb_mark_start);
614		POS_COPY(e, vb->vb_mark_end);
615	}
616
617	i = 0;
618	for (r = s.tp_row; r <= e.tp_row; r ++) {
619		cs = (r == s.tp_row)?s.tp_col:0;
620		ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
621		for (c = cs; c < ce; c ++) {
622			buf[i++] = vb->vb_rows[r][c];
623		}
624		/* Add new line for all rows, but not for last one. */
625		if (r != e.tp_row) {
626			buf[i++] = '\r';
627			buf[i++] = '\n';
628		}
629	}
630}
631
632int
633vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
634{
635	term_char_t *r;
636	int i;
637
638	switch (type) {
639	case VTB_MARK_END:	/* B1 UP */
640		if (vb->vb_mark_last != VTB_MARK_MOVE)
641			return (0);
642		/* FALLTHROUGH */
643	case VTB_MARK_MOVE:
644	case VTB_MARK_EXTEND:
645		vtbuf_flush_mark(vb); /* Clean old mark. */
646		vb->vb_mark_end.tp_col = col;
647		vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
648		break;
649	case VTB_MARK_START:
650		vtbuf_flush_mark(vb); /* Clean old mark. */
651		vb->vb_mark_start.tp_col = col;
652		vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
653		/* Start again, so clear end point. */
654		vb->vb_mark_end.tp_col = col;
655		vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
656		break;
657	case VTB_MARK_WORD:
658		vtbuf_flush_mark(vb); /* Clean old mark. */
659		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
660		    vtbuf_wth(vb, row);
661		r = vb->vb_rows[vb->vb_mark_start.tp_row];
662		for (i = col; i >= 0; i --) {
663			if (TCHAR_CHARACTER(r[i]) == ' ') {
664				vb->vb_mark_start.tp_col = i + 1;
665				break;
666			}
667		}
668		for (i = col; i < vb->vb_scr_size.tp_col; i ++) {
669			if (TCHAR_CHARACTER(r[i]) == ' ') {
670				vb->vb_mark_end.tp_col = i;
671				break;
672			}
673		}
674		if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
675			vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
676		break;
677	case VTB_MARK_ROW:
678		vtbuf_flush_mark(vb); /* Clean old mark. */
679		vb->vb_mark_start.tp_col = 0;
680		vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
681		vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
682		    vtbuf_wth(vb, row);
683		break;
684	case VTB_MARK_NONE:
685		vb->vb_mark_last = type;
686		/* FALLTHROUGH */
687	default:
688		/* panic? */
689		return (0);
690	}
691
692	vb->vb_mark_last = type;
693	/* Draw new marked region. */
694	vtbuf_flush_mark(vb);
695	return (1);
696}
697#endif
698
699void
700vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
701{
702	int oflags, nflags;
703
704	VTBUF_LOCK(vb);
705	oflags = vb->vb_flags;
706	if (yes)
707		vb->vb_flags |= VBF_CURSOR;
708	else
709		vb->vb_flags &= ~VBF_CURSOR;
710	nflags = vb->vb_flags;
711	VTBUF_UNLOCK(vb);
712
713	if (oflags != nflags)
714		vtbuf_dirty_cell(vb, &vb->vb_cursor);
715}
716
717void
718vtbuf_scroll_mode(struct vt_buf *vb, int yes)
719{
720	int oflags, nflags;
721
722	VTBUF_LOCK(vb);
723	oflags = vb->vb_flags;
724	if (yes)
725		vb->vb_flags |= VBF_SCROLL;
726	else
727		vb->vb_flags &= ~VBF_SCROLL;
728	nflags = vb->vb_flags;
729	VTBUF_UNLOCK(vb);
730
731	if (oflags != nflags)
732		vtbuf_dirty_cell(vb, &vb->vb_cursor);
733}
734
735