127cf7d04SAleksandr Rybalko /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
427cf7d04SAleksandr Rybalko * Copyright (c) 2009, 2013 The FreeBSD Foundation
527cf7d04SAleksandr Rybalko *
627cf7d04SAleksandr Rybalko * This software was developed by Ed Schouten under sponsorship from the
727cf7d04SAleksandr Rybalko * FreeBSD Foundation.
827cf7d04SAleksandr Rybalko *
927cf7d04SAleksandr Rybalko * Portions of this software were developed by Oleksandr Rybalko
1027cf7d04SAleksandr Rybalko * under sponsorship from the FreeBSD Foundation.
1127cf7d04SAleksandr Rybalko *
1227cf7d04SAleksandr Rybalko * Redistribution and use in source and binary forms, with or without
1327cf7d04SAleksandr Rybalko * modification, are permitted provided that the following conditions
1427cf7d04SAleksandr Rybalko * are met:
1527cf7d04SAleksandr Rybalko * 1. Redistributions of source code must retain the above copyright
1627cf7d04SAleksandr Rybalko * notice, this list of conditions and the following disclaimer.
1727cf7d04SAleksandr Rybalko * 2. Redistributions in binary form must reproduce the above copyright
1827cf7d04SAleksandr Rybalko * notice, this list of conditions and the following disclaimer in the
1927cf7d04SAleksandr Rybalko * documentation and/or other materials provided with the distribution.
2027cf7d04SAleksandr Rybalko *
2127cf7d04SAleksandr Rybalko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2227cf7d04SAleksandr Rybalko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2327cf7d04SAleksandr Rybalko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2427cf7d04SAleksandr Rybalko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2527cf7d04SAleksandr Rybalko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2627cf7d04SAleksandr Rybalko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2727cf7d04SAleksandr Rybalko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2827cf7d04SAleksandr Rybalko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2927cf7d04SAleksandr Rybalko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3027cf7d04SAleksandr Rybalko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3127cf7d04SAleksandr Rybalko * SUCH DAMAGE.
3227cf7d04SAleksandr Rybalko */
3327cf7d04SAleksandr Rybalko
3427cf7d04SAleksandr Rybalko #include <sys/param.h>
35327da507SEd Maste #include <sys/systm.h>
3627cf7d04SAleksandr Rybalko #include <sys/kernel.h>
3727cf7d04SAleksandr Rybalko #include <sys/lock.h>
3827cf7d04SAleksandr Rybalko #include <sys/malloc.h>
3927cf7d04SAleksandr Rybalko #include <sys/mutex.h>
407344ee18SMarius Strobl #include <sys/reboot.h>
4127cf7d04SAleksandr Rybalko
4227cf7d04SAleksandr Rybalko #include <dev/vt/vt.h>
4327cf7d04SAleksandr Rybalko
4427cf7d04SAleksandr Rybalko static MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
4527cf7d04SAleksandr Rybalko
4627cf7d04SAleksandr Rybalko #define VTBUF_LOCK(vb) mtx_lock_spin(&(vb)->vb_lock)
4727cf7d04SAleksandr Rybalko #define VTBUF_UNLOCK(vb) mtx_unlock_spin(&(vb)->vb_lock)
4827cf7d04SAleksandr Rybalko
4927cf7d04SAleksandr Rybalko #define POS_INDEX(c, r) (((r) << 12) + (c))
5027cf7d04SAleksandr Rybalko #define POS_COPY(d, s) do { \
5127cf7d04SAleksandr Rybalko (d).tp_col = (s).tp_col; \
5227cf7d04SAleksandr Rybalko (d).tp_row = (s).tp_row; \
5327cf7d04SAleksandr Rybalko } while (0)
5427cf7d04SAleksandr Rybalko
55c0e295deSAleksandr Rybalko #ifndef SC_NO_CUTPASTE
5607a45594SRavi Pokala static int vtbuf_htw(const struct vt_buf *vb, int row);
57c0e295deSAleksandr Rybalko static int vtbuf_wth(const struct vt_buf *vb, int row);
58c0e295deSAleksandr Rybalko static int vtbuf_in_this_range(int begin, int test, int end, int sz);
59c0e295deSAleksandr Rybalko #endif
6027cf7d04SAleksandr Rybalko
6127cf7d04SAleksandr Rybalko /*
6227cf7d04SAleksandr Rybalko * line4
6327cf7d04SAleksandr Rybalko * line5 <--- curroffset (terminal output to that line)
6427cf7d04SAleksandr Rybalko * line0
6527cf7d04SAleksandr Rybalko * line1 <--- roffset (history display from that point)
6627cf7d04SAleksandr Rybalko * line2
6727cf7d04SAleksandr Rybalko * line3
6827cf7d04SAleksandr Rybalko */
6927cf7d04SAleksandr Rybalko int
vthistory_seek(struct vt_buf * vb,int offset,int whence)7027cf7d04SAleksandr Rybalko vthistory_seek(struct vt_buf *vb, int offset, int whence)
7127cf7d04SAleksandr Rybalko {
7227cf7d04SAleksandr Rybalko int diff, top, bottom, roffset;
7327cf7d04SAleksandr Rybalko
7427cf7d04SAleksandr Rybalko /* No scrolling if not enabled. */
7527cf7d04SAleksandr Rybalko if ((vb->vb_flags & VBF_SCROLL) == 0) {
7627cf7d04SAleksandr Rybalko if (vb->vb_roffset != vb->vb_curroffset) {
7727cf7d04SAleksandr Rybalko vb->vb_roffset = vb->vb_curroffset;
7827cf7d04SAleksandr Rybalko return (0xffff);
7927cf7d04SAleksandr Rybalko }
8027cf7d04SAleksandr Rybalko return (0); /* No changes */
8127cf7d04SAleksandr Rybalko }
8227cf7d04SAleksandr Rybalko
831454439cSJean-Sébastien Pédron /* "top" may be a negative integer. */
841454439cSJean-Sébastien Pédron bottom = vb->vb_curroffset;
851454439cSJean-Sébastien Pédron top = (vb->vb_flags & VBF_HISTORY_FULL) ?
861454439cSJean-Sébastien Pédron bottom + vb->vb_scr_size.tp_row - vb->vb_history_size :
871454439cSJean-Sébastien Pédron 0;
881454439cSJean-Sébastien Pédron
89a10068fdSBjoern A. Zeeb roffset = 0; /* Make gcc happy. */
9027cf7d04SAleksandr Rybalko switch (whence) {
9127cf7d04SAleksandr Rybalko case VHS_SET:
921454439cSJean-Sébastien Pédron if (offset < 0)
931454439cSJean-Sébastien Pédron offset = 0;
941454439cSJean-Sébastien Pédron roffset = top + offset;
9527cf7d04SAleksandr Rybalko break;
9627cf7d04SAleksandr Rybalko case VHS_CUR:
971454439cSJean-Sébastien Pédron /*
981454439cSJean-Sébastien Pédron * Operate on copy of offset value, since it temporary
991454439cSJean-Sébastien Pédron * can be bigger than amount of rows in buffer.
1001454439cSJean-Sébastien Pédron */
1011454439cSJean-Sébastien Pédron roffset = vb->vb_roffset;
1021454439cSJean-Sébastien Pédron if (roffset >= bottom + vb->vb_scr_size.tp_row)
1031454439cSJean-Sébastien Pédron roffset -= vb->vb_history_size;
1041454439cSJean-Sébastien Pédron
10527cf7d04SAleksandr Rybalko roffset += offset;
1061454439cSJean-Sébastien Pédron roffset = MAX(roffset, top);
1071454439cSJean-Sébastien Pédron roffset = MIN(roffset, bottom);
1081454439cSJean-Sébastien Pédron
1091454439cSJean-Sébastien Pédron if (roffset < 0)
1101454439cSJean-Sébastien Pédron roffset = vb->vb_history_size + roffset;
1111454439cSJean-Sébastien Pédron
11227cf7d04SAleksandr Rybalko break;
11327cf7d04SAleksandr Rybalko case VHS_END:
11427cf7d04SAleksandr Rybalko /* Go to current offset. */
1151454439cSJean-Sébastien Pédron roffset = vb->vb_curroffset;
11627cf7d04SAleksandr Rybalko break;
11727cf7d04SAleksandr Rybalko }
11827cf7d04SAleksandr Rybalko
1191454439cSJean-Sébastien Pédron diff = vb->vb_roffset != roffset;
12027cf7d04SAleksandr Rybalko vb->vb_roffset = roffset;
1211454439cSJean-Sébastien Pédron
12227cf7d04SAleksandr Rybalko return (diff);
12327cf7d04SAleksandr Rybalko }
12427cf7d04SAleksandr Rybalko
12527cf7d04SAleksandr Rybalko void
vthistory_addlines(struct vt_buf * vb,int offset)12627cf7d04SAleksandr Rybalko vthistory_addlines(struct vt_buf *vb, int offset)
12727cf7d04SAleksandr Rybalko {
128c0e295deSAleksandr Rybalko #ifndef SC_NO_CUTPASTE
129c0e295deSAleksandr Rybalko int cur, sz;
130c0e295deSAleksandr Rybalko #endif
13127cf7d04SAleksandr Rybalko
13227cf7d04SAleksandr Rybalko vb->vb_curroffset += offset;
133e1734edfSEd Maste if (vb->vb_curroffset + vb->vb_scr_size.tp_row >= vb->vb_history_size) {
1341454439cSJean-Sébastien Pédron vb->vb_flags |= VBF_HISTORY_FULL;
13527cf7d04SAleksandr Rybalko vb->vb_curroffset %= vb->vb_history_size;
136e1734edfSEd Maste }
13727cf7d04SAleksandr Rybalko if ((vb->vb_flags & VBF_SCROLL) == 0) {
13827cf7d04SAleksandr Rybalko vb->vb_roffset = vb->vb_curroffset;
13927cf7d04SAleksandr Rybalko }
140c0e295deSAleksandr Rybalko
141c0e295deSAleksandr Rybalko #ifndef SC_NO_CUTPASTE
142c0e295deSAleksandr Rybalko sz = vb->vb_history_size;
143c0e295deSAleksandr Rybalko cur = vb->vb_roffset + vb->vb_scr_size.tp_row + sz - 1;
144c0e295deSAleksandr Rybalko if (vtbuf_in_this_range(cur, vb->vb_mark_start.tp_row, cur + offset, sz) ||
145c0e295deSAleksandr Rybalko vtbuf_in_this_range(cur, vb->vb_mark_end.tp_row, cur + offset, sz)) {
146c0e295deSAleksandr Rybalko /* clear screen selection */
147c0e295deSAleksandr Rybalko vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row;
148c0e295deSAleksandr Rybalko vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
149c0e295deSAleksandr Rybalko }
150c0e295deSAleksandr Rybalko #endif
15127cf7d04SAleksandr Rybalko }
15227cf7d04SAleksandr Rybalko
15327cf7d04SAleksandr Rybalko void
vthistory_getpos(const struct vt_buf * vb,unsigned int * offset)15427cf7d04SAleksandr Rybalko vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
15527cf7d04SAleksandr Rybalko {
15627cf7d04SAleksandr Rybalko
15727cf7d04SAleksandr Rybalko *offset = vb->vb_roffset;
15827cf7d04SAleksandr Rybalko }
15927cf7d04SAleksandr Rybalko
1600f49db6eSAleksandr Rybalko #ifndef SC_NO_CUTPASTE /* Only mouse support use it now. */
16107a45594SRavi Pokala /* Translate history row to current view row number. */
16207a45594SRavi Pokala static int
vtbuf_htw(const struct vt_buf * vb,int row)16307a45594SRavi Pokala vtbuf_htw(const struct vt_buf *vb, int row)
16407a45594SRavi Pokala {
16507a45594SRavi Pokala
16607a45594SRavi Pokala /*
16707a45594SRavi Pokala * total 1000 rows.
16807a45594SRavi Pokala * History offset roffset winrow
16907a45594SRavi Pokala * 205 200 ((205 - 200 + 1000) % 1000) = 5
17007a45594SRavi Pokala * 90 990 ((90 - 990 + 1000) % 1000) = 100
17107a45594SRavi Pokala */
17207a45594SRavi Pokala return ((row - vb->vb_roffset + vb->vb_history_size) %
17307a45594SRavi Pokala vb->vb_history_size);
17407a45594SRavi Pokala }
17507a45594SRavi Pokala
17627cf7d04SAleksandr Rybalko /* Translate current view row number to history row. */
17727cf7d04SAleksandr Rybalko static int
vtbuf_wth(const struct vt_buf * vb,int row)178c0e295deSAleksandr Rybalko vtbuf_wth(const struct vt_buf *vb, int row)
17927cf7d04SAleksandr Rybalko {
18027cf7d04SAleksandr Rybalko
18127cf7d04SAleksandr Rybalko return ((vb->vb_roffset + row) % vb->vb_history_size);
18227cf7d04SAleksandr Rybalko }
183c0e295deSAleksandr Rybalko
184c0e295deSAleksandr Rybalko /*
185c0e295deSAleksandr Rybalko * Test if an index in a circular buffer is within a range.
186c0e295deSAleksandr Rybalko *
187c0e295deSAleksandr Rybalko * begin - start index
188c0e295deSAleksandr Rybalko * end - end index
189c0e295deSAleksandr Rybalko * test - test index
190c0e295deSAleksandr Rybalko * sz - size of circular buffer when it turns over
191c0e295deSAleksandr Rybalko */
192c0e295deSAleksandr Rybalko static int
vtbuf_in_this_range(int begin,int test,int end,int sz)193c0e295deSAleksandr Rybalko vtbuf_in_this_range(int begin, int test, int end, int sz)
194c0e295deSAleksandr Rybalko {
195c0e295deSAleksandr Rybalko
196c0e295deSAleksandr Rybalko begin %= sz;
197c0e295deSAleksandr Rybalko end %= sz;
198c0e295deSAleksandr Rybalko
199c0e295deSAleksandr Rybalko /* check for inversion */
200c0e295deSAleksandr Rybalko if (begin > end)
201c0e295deSAleksandr Rybalko return (test >= begin || test < end);
202c0e295deSAleksandr Rybalko else
203c0e295deSAleksandr Rybalko return (test >= begin && test < end);
204c0e295deSAleksandr Rybalko }
2050f49db6eSAleksandr Rybalko #endif
20627cf7d04SAleksandr Rybalko
20727cf7d04SAleksandr Rybalko int
vtbuf_iscursor(const struct vt_buf * vb,int row,int col)208c6e1a987SJean-Sébastien Pédron vtbuf_iscursor(const struct vt_buf *vb, int row, int col)
20927cf7d04SAleksandr Rybalko {
210c0e295deSAleksandr Rybalko #ifndef SC_NO_CUTPASTE
211c0e295deSAleksandr Rybalko int sc, sr, sz, ec, er, tmp;
212c0e295deSAleksandr Rybalko #endif
21327cf7d04SAleksandr Rybalko
21427cf7d04SAleksandr Rybalko if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
21527cf7d04SAleksandr Rybalko (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
21627cf7d04SAleksandr Rybalko return (1);
21727cf7d04SAleksandr Rybalko
218c0e295deSAleksandr Rybalko #ifndef SC_NO_CUTPASTE
21927cf7d04SAleksandr Rybalko /* Mark cut/paste region. */
220c0e295deSAleksandr Rybalko if (vb->vb_mark_start.tp_col == vb->vb_mark_end.tp_col &&
221c0e295deSAleksandr Rybalko vb->vb_mark_start.tp_row == vb->vb_mark_end.tp_row)
222c0e295deSAleksandr Rybalko return (0);
223c0e295deSAleksandr Rybalko
224c0e295deSAleksandr Rybalko sc = vb->vb_mark_start.tp_col;
225c0e295deSAleksandr Rybalko sr = vb->vb_mark_start.tp_row;
226c0e295deSAleksandr Rybalko ec = vb->vb_mark_end.tp_col;
227c0e295deSAleksandr Rybalko er = vb->vb_mark_end.tp_row;
22827cf7d04SAleksandr Rybalko
22927cf7d04SAleksandr Rybalko /*
230c0e295deSAleksandr Rybalko * Information about if the selection was made bottom-top or
231c0e295deSAleksandr Rybalko * top-bottom is lost due to modulo arithmetics and needs to
232c0e295deSAleksandr Rybalko * be recovered:
23327cf7d04SAleksandr Rybalko */
234c0e295deSAleksandr Rybalko sz = vb->vb_history_size;
235c0e295deSAleksandr Rybalko tmp = (sz + er - sr) % sz;
236c0e295deSAleksandr Rybalko row = vtbuf_wth(vb, row);
23727cf7d04SAleksandr Rybalko
238c0e295deSAleksandr Rybalko /* Swap start and end if start > end */
239c0e295deSAleksandr Rybalko if ((2 * tmp) > sz || (tmp == 0 && sc > ec)) {
24027cf7d04SAleksandr Rybalko tmp = sc; sc = ec; ec = tmp;
24127cf7d04SAleksandr Rybalko tmp = sr; sr = er; er = tmp;
24227cf7d04SAleksandr Rybalko }
24327cf7d04SAleksandr Rybalko
244c0e295deSAleksandr Rybalko if (vtbuf_in_this_range(POS_INDEX(sc, sr), POS_INDEX(col, row),
245c0e295deSAleksandr Rybalko POS_INDEX(ec, er), POS_INDEX(0, sz)))
24627cf7d04SAleksandr Rybalko return (1);
247c0e295deSAleksandr Rybalko #endif
24827cf7d04SAleksandr Rybalko
24927cf7d04SAleksandr Rybalko return (0);
25027cf7d04SAleksandr Rybalko }
25127cf7d04SAleksandr Rybalko
252547e74a8SJean-Sébastien Pédron void
vtbuf_lock(struct vt_buf * vb)253547e74a8SJean-Sébastien Pédron vtbuf_lock(struct vt_buf *vb)
254547e74a8SJean-Sébastien Pédron {
255547e74a8SJean-Sébastien Pédron
256547e74a8SJean-Sébastien Pédron VTBUF_LOCK(vb);
257547e74a8SJean-Sébastien Pédron }
258547e74a8SJean-Sébastien Pédron
259547e74a8SJean-Sébastien Pédron void
vtbuf_unlock(struct vt_buf * vb)260547e74a8SJean-Sébastien Pédron vtbuf_unlock(struct vt_buf *vb)
261547e74a8SJean-Sébastien Pédron {
262547e74a8SJean-Sébastien Pédron
263547e74a8SJean-Sébastien Pédron VTBUF_UNLOCK(vb);
264547e74a8SJean-Sébastien Pédron }
265547e74a8SJean-Sébastien Pédron
266547e74a8SJean-Sébastien Pédron void
vtbuf_dirty(struct vt_buf * vb,const term_rect_t * area)267547e74a8SJean-Sébastien Pédron vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
26827cf7d04SAleksandr Rybalko {
26927cf7d04SAleksandr Rybalko
27027cf7d04SAleksandr Rybalko if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
27127cf7d04SAleksandr Rybalko vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
27227cf7d04SAleksandr Rybalko if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
27327cf7d04SAleksandr Rybalko vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
27427cf7d04SAleksandr Rybalko if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
27527cf7d04SAleksandr Rybalko vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
27627cf7d04SAleksandr Rybalko if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
27727cf7d04SAleksandr Rybalko vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
27800c33067SJean-Sébastien Pédron }
27900c33067SJean-Sébastien Pédron
28027cf7d04SAleksandr Rybalko static inline void
vtbuf_dirty_cell(struct vt_buf * vb,const term_pos_t * p)281547e74a8SJean-Sébastien Pédron vtbuf_dirty_cell(struct vt_buf *vb, const term_pos_t *p)
28227cf7d04SAleksandr Rybalko {
28327cf7d04SAleksandr Rybalko term_rect_t area;
28427cf7d04SAleksandr Rybalko
28527cf7d04SAleksandr Rybalko area.tr_begin = *p;
28627cf7d04SAleksandr Rybalko area.tr_end.tp_row = p->tp_row + 1;
28727cf7d04SAleksandr Rybalko area.tr_end.tp_col = p->tp_col + 1;
288547e74a8SJean-Sébastien Pédron vtbuf_dirty(vb, &area);
28927cf7d04SAleksandr Rybalko }
29027cf7d04SAleksandr Rybalko
29127cf7d04SAleksandr Rybalko static void
vtbuf_make_undirty(struct vt_buf * vb)29227cf7d04SAleksandr Rybalko vtbuf_make_undirty(struct vt_buf *vb)
29327cf7d04SAleksandr Rybalko {
29427cf7d04SAleksandr Rybalko
29527cf7d04SAleksandr Rybalko vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
29627cf7d04SAleksandr Rybalko vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
29727cf7d04SAleksandr Rybalko }
29827cf7d04SAleksandr Rybalko
29927cf7d04SAleksandr Rybalko void
vtbuf_undirty(struct vt_buf * vb,term_rect_t * r)300c3a05e54SJean-Sébastien Pédron vtbuf_undirty(struct vt_buf *vb, term_rect_t *r)
30127cf7d04SAleksandr Rybalko {
30227cf7d04SAleksandr Rybalko
30327cf7d04SAleksandr Rybalko *r = vb->vb_dirtyrect;
30427cf7d04SAleksandr Rybalko vtbuf_make_undirty(vb);
30527cf7d04SAleksandr Rybalko }
30627cf7d04SAleksandr Rybalko
30727cf7d04SAleksandr Rybalko void
vtbuf_copy(struct vt_buf * vb,const term_rect_t * r,const term_pos_t * p2)30827cf7d04SAleksandr Rybalko vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
30927cf7d04SAleksandr Rybalko {
31027cf7d04SAleksandr Rybalko const term_pos_t *p1 = &r->tr_begin;
31127cf7d04SAleksandr Rybalko term_rect_t area;
31227cf7d04SAleksandr Rybalko unsigned int rows, cols;
31327cf7d04SAleksandr Rybalko int pr, rdiff;
31427cf7d04SAleksandr Rybalko
31527cf7d04SAleksandr Rybalko KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
31627cf7d04SAleksandr Rybalko ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
31727cf7d04SAleksandr Rybalko r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
31827cf7d04SAleksandr Rybalko KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
31927cf7d04SAleksandr Rybalko ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
32027cf7d04SAleksandr Rybalko r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
32127cf7d04SAleksandr Rybalko
32227cf7d04SAleksandr Rybalko KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
32327cf7d04SAleksandr Rybalko ("vtbuf_copy end.tp_row %d must be less than screen width %d",
32427cf7d04SAleksandr Rybalko r->tr_end.tp_row, vb->vb_scr_size.tp_row));
32527cf7d04SAleksandr Rybalko KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
32627cf7d04SAleksandr Rybalko ("vtbuf_copy end.tp_col %d must be less than screen height %d",
32727cf7d04SAleksandr Rybalko r->tr_end.tp_col, vb->vb_scr_size.tp_col));
32827cf7d04SAleksandr Rybalko
32927cf7d04SAleksandr Rybalko KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
33027cf7d04SAleksandr Rybalko ("vtbuf_copy tp_row %d must be less than screen width %d",
33127cf7d04SAleksandr Rybalko p2->tp_row, vb->vb_scr_size.tp_row));
33227cf7d04SAleksandr Rybalko KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
33327cf7d04SAleksandr Rybalko ("vtbuf_copy tp_col %d must be less than screen height %d",
33427cf7d04SAleksandr Rybalko p2->tp_col, vb->vb_scr_size.tp_col));
33527cf7d04SAleksandr Rybalko
33627cf7d04SAleksandr Rybalko rows = r->tr_end.tp_row - r->tr_begin.tp_row;
33727cf7d04SAleksandr Rybalko rdiff = r->tr_begin.tp_row - p2->tp_row;
33827cf7d04SAleksandr Rybalko cols = r->tr_end.tp_col - r->tr_begin.tp_col;
33927cf7d04SAleksandr Rybalko if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
34027cf7d04SAleksandr Rybalko r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
34127cf7d04SAleksandr Rybalko (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
34213c6f8a0SAleksandr Rybalko rdiff > 0) { /* Only forward direction. Do not eat history. */
34327cf7d04SAleksandr Rybalko vthistory_addlines(vb, rdiff);
34427cf7d04SAleksandr Rybalko } else if (p2->tp_row < p1->tp_row) {
34527cf7d04SAleksandr Rybalko /* Handle overlapping copies of line segments. */
34627cf7d04SAleksandr Rybalko /* Move data up. */
34727cf7d04SAleksandr Rybalko for (pr = 0; pr < rows; pr++)
34827cf7d04SAleksandr Rybalko memmove(
34927cf7d04SAleksandr Rybalko &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
35027cf7d04SAleksandr Rybalko &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
35127cf7d04SAleksandr Rybalko cols * sizeof(term_char_t));
35227cf7d04SAleksandr Rybalko } else {
35327cf7d04SAleksandr Rybalko /* Move data down. */
35427cf7d04SAleksandr Rybalko for (pr = rows - 1; pr >= 0; pr--)
35527cf7d04SAleksandr Rybalko memmove(
35627cf7d04SAleksandr Rybalko &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
35727cf7d04SAleksandr Rybalko &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
35827cf7d04SAleksandr Rybalko cols * sizeof(term_char_t));
35927cf7d04SAleksandr Rybalko }
36027cf7d04SAleksandr Rybalko
36127cf7d04SAleksandr Rybalko area.tr_begin = *p2;
36227cf7d04SAleksandr Rybalko area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
36327cf7d04SAleksandr Rybalko area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
36427cf7d04SAleksandr Rybalko vtbuf_dirty(vb, &area);
36527cf7d04SAleksandr Rybalko }
36627cf7d04SAleksandr Rybalko
36727cf7d04SAleksandr Rybalko static void
vtbuf_do_fill(struct vt_buf * vb,const term_rect_t * r,term_char_t c)368547e74a8SJean-Sébastien Pédron vtbuf_do_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
36927cf7d04SAleksandr Rybalko {
37027cf7d04SAleksandr Rybalko unsigned int pr, pc;
37127cf7d04SAleksandr Rybalko term_char_t *row;
37227cf7d04SAleksandr Rybalko
37327cf7d04SAleksandr Rybalko for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
37427cf7d04SAleksandr Rybalko row = vb->vb_rows[(vb->vb_curroffset + pr) %
37527cf7d04SAleksandr Rybalko VTBUF_MAX_HEIGHT(vb)];
37627cf7d04SAleksandr Rybalko for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
37727cf7d04SAleksandr Rybalko row[pc] = c;
37827cf7d04SAleksandr Rybalko }
37927cf7d04SAleksandr Rybalko }
38027cf7d04SAleksandr Rybalko }
38127cf7d04SAleksandr Rybalko
38227cf7d04SAleksandr Rybalko void
vtbuf_fill(struct vt_buf * vb,const term_rect_t * r,term_char_t c)383547e74a8SJean-Sébastien Pédron vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
38427cf7d04SAleksandr Rybalko {
385547e74a8SJean-Sébastien Pédron
38627cf7d04SAleksandr Rybalko KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
387547e74a8SJean-Sébastien Pédron ("vtbuf_fill begin.tp_row %d must be < screen height %d",
38827cf7d04SAleksandr Rybalko r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
38927cf7d04SAleksandr Rybalko KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
390547e74a8SJean-Sébastien Pédron ("vtbuf_fill begin.tp_col %d must be < screen width %d",
39127cf7d04SAleksandr Rybalko r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
39227cf7d04SAleksandr Rybalko
39327cf7d04SAleksandr Rybalko KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
394547e74a8SJean-Sébastien Pédron ("vtbuf_fill end.tp_row %d must be <= screen height %d",
39527cf7d04SAleksandr Rybalko r->tr_end.tp_row, vb->vb_scr_size.tp_row));
39627cf7d04SAleksandr Rybalko KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
397547e74a8SJean-Sébastien Pédron ("vtbuf_fill end.tp_col %d must be <= screen width %d",
39827cf7d04SAleksandr Rybalko r->tr_end.tp_col, vb->vb_scr_size.tp_col));
39927cf7d04SAleksandr Rybalko
400547e74a8SJean-Sébastien Pédron vtbuf_do_fill(vb, r, c);
401547e74a8SJean-Sébastien Pédron vtbuf_dirty(vb, r);
40227cf7d04SAleksandr Rybalko }
40327cf7d04SAleksandr Rybalko
40427cf7d04SAleksandr Rybalko static void
vtbuf_init_rows(struct vt_buf * vb)40527cf7d04SAleksandr Rybalko vtbuf_init_rows(struct vt_buf *vb)
40627cf7d04SAleksandr Rybalko {
40727cf7d04SAleksandr Rybalko int r;
40827cf7d04SAleksandr Rybalko
40927cf7d04SAleksandr Rybalko vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
41027cf7d04SAleksandr Rybalko
41127cf7d04SAleksandr Rybalko for (r = 0; r < vb->vb_history_size; r++)
4127344ee18SMarius Strobl vb->vb_rows[r] = &vb->vb_buffer[r * vb->vb_scr_size.tp_col];
41327cf7d04SAleksandr Rybalko }
41427cf7d04SAleksandr Rybalko
41598f7cf02SJason A. Harmening static void
vtbuf_do_clearhistory(struct vt_buf * vb)41698f7cf02SJason A. Harmening vtbuf_do_clearhistory(struct vt_buf *vb)
41727cf7d04SAleksandr Rybalko {
4187344ee18SMarius Strobl term_rect_t rect;
419df1bc27aSToomas Soome const teken_attr_t *a;
42098f7cf02SJason A. Harmening term_char_t ch;
42127cf7d04SAleksandr Rybalko
42298f7cf02SJason A. Harmening a = teken_get_curattr(&vb->vb_terminal->tm_emulator);
42398f7cf02SJason A. Harmening ch = TCOLOR_FG(a->ta_fgcolor) | TCOLOR_BG(a->ta_bgcolor);
42498f7cf02SJason A. Harmening
42598f7cf02SJason A. Harmening rect.tr_begin.tp_row = rect.tr_begin.tp_col = 0;
42698f7cf02SJason A. Harmening rect.tr_end.tp_col = vb->vb_scr_size.tp_col;
42798f7cf02SJason A. Harmening rect.tr_end.tp_row = vb->vb_history_size;
42898f7cf02SJason A. Harmening
42998f7cf02SJason A. Harmening vtbuf_do_fill(vb, &rect, VTBUF_SPACE_CHAR(ch));
43098f7cf02SJason A. Harmening }
43198f7cf02SJason A. Harmening
432ef1eabcaSJason A. Harmening static void
vtbuf_reset_scrollback(struct vt_buf * vb)433ef1eabcaSJason A. Harmening vtbuf_reset_scrollback(struct vt_buf *vb)
43498f7cf02SJason A. Harmening {
43527cf7d04SAleksandr Rybalko vb->vb_roffset = 0;
43627cf7d04SAleksandr Rybalko vb->vb_curroffset = 0;
43727cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_row = 0;
43827cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col = 0;
43927cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_row = 0;
44027cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col = 0;
441ef1eabcaSJason A. Harmening }
44227cf7d04SAleksandr Rybalko
443ef1eabcaSJason A. Harmening void
vtbuf_init_early(struct vt_buf * vb)444ef1eabcaSJason A. Harmening vtbuf_init_early(struct vt_buf *vb)
445ef1eabcaSJason A. Harmening {
446ef1eabcaSJason A. Harmening vb->vb_flags |= VBF_CURSOR;
447ef1eabcaSJason A. Harmening vtbuf_reset_scrollback(vb);
44827cf7d04SAleksandr Rybalko vtbuf_init_rows(vb);
44998f7cf02SJason A. Harmening vtbuf_do_clearhistory(vb);
45027cf7d04SAleksandr Rybalko vtbuf_make_undirty(vb);
45127cf7d04SAleksandr Rybalko if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
45227cf7d04SAleksandr Rybalko mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
45327cf7d04SAleksandr Rybalko vb->vb_flags |= VBF_MTX_INIT;
45427cf7d04SAleksandr Rybalko }
45527cf7d04SAleksandr Rybalko }
45627cf7d04SAleksandr Rybalko
45727cf7d04SAleksandr Rybalko void
vtbuf_init(struct vt_buf * vb,const term_pos_t * p)45827cf7d04SAleksandr Rybalko vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
45927cf7d04SAleksandr Rybalko {
46027cf7d04SAleksandr Rybalko int sz;
46127cf7d04SAleksandr Rybalko
46227cf7d04SAleksandr Rybalko vb->vb_scr_size = *p;
46327cf7d04SAleksandr Rybalko vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
46427cf7d04SAleksandr Rybalko
46527cf7d04SAleksandr Rybalko if ((vb->vb_flags & VBF_STATIC) == 0) {
46627cf7d04SAleksandr Rybalko sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
46727cf7d04SAleksandr Rybalko vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
46827cf7d04SAleksandr Rybalko
46927cf7d04SAleksandr Rybalko sz = vb->vb_history_size * sizeof(term_char_t *);
47027cf7d04SAleksandr Rybalko vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
47127cf7d04SAleksandr Rybalko }
47227cf7d04SAleksandr Rybalko
47327cf7d04SAleksandr Rybalko vtbuf_init_early(vb);
47427cf7d04SAleksandr Rybalko }
47527cf7d04SAleksandr Rybalko
47627cf7d04SAleksandr Rybalko void
vtbuf_clearhistory(struct vt_buf * vb)47798f7cf02SJason A. Harmening vtbuf_clearhistory(struct vt_buf *vb)
47898f7cf02SJason A. Harmening {
47998f7cf02SJason A. Harmening VTBUF_LOCK(vb);
48098f7cf02SJason A. Harmening vtbuf_do_clearhistory(vb);
481ef1eabcaSJason A. Harmening vtbuf_reset_scrollback(vb);
482ef1eabcaSJason A. Harmening vb->vb_flags &= ~VBF_HISTORY_FULL;
48398f7cf02SJason A. Harmening VTBUF_UNLOCK(vb);
48498f7cf02SJason A. Harmening }
48598f7cf02SJason A. Harmening
48698f7cf02SJason A. Harmening void
vtbuf_sethistory_size(struct vt_buf * vb,unsigned int size)487e1734edfSEd Maste vtbuf_sethistory_size(struct vt_buf *vb, unsigned int size)
48827cf7d04SAleksandr Rybalko {
48927cf7d04SAleksandr Rybalko term_pos_t p;
49027cf7d04SAleksandr Rybalko
49127cf7d04SAleksandr Rybalko /* With same size */
49227cf7d04SAleksandr Rybalko p.tp_row = vb->vb_scr_size.tp_row;
49327cf7d04SAleksandr Rybalko p.tp_col = vb->vb_scr_size.tp_col;
49427cf7d04SAleksandr Rybalko vtbuf_grow(vb, &p, size);
49527cf7d04SAleksandr Rybalko }
49627cf7d04SAleksandr Rybalko
49727cf7d04SAleksandr Rybalko void
vtbuf_grow(struct vt_buf * vb,const term_pos_t * p,unsigned int history_size)498b7fe4961SJean-Sébastien Pédron vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, unsigned int history_size)
49927cf7d04SAleksandr Rybalko {
5001454439cSJean-Sébastien Pédron term_char_t *old, *new, **rows, **oldrows, **copyrows, *row, *oldrow;
501e1734edfSEd Maste unsigned int w, h, c, r, old_history_size;
502e1734edfSEd Maste size_t bufsize, rowssize;
503e1734edfSEd Maste int history_full;
504df1bc27aSToomas Soome const teken_attr_t *a;
505df1bc27aSToomas Soome term_char_t ch;
506df1bc27aSToomas Soome
507df1bc27aSToomas Soome a = teken_get_curattr(&vb->vb_terminal->tm_emulator);
508df1bc27aSToomas Soome ch = TCOLOR_FG(a->ta_fgcolor) | TCOLOR_BG(a->ta_bgcolor);
50927cf7d04SAleksandr Rybalko
51027cf7d04SAleksandr Rybalko history_size = MAX(history_size, p->tp_row);
51127cf7d04SAleksandr Rybalko
51227cf7d04SAleksandr Rybalko /* Allocate new buffer. */
51327cf7d04SAleksandr Rybalko bufsize = history_size * p->tp_col * sizeof(term_char_t);
51427cf7d04SAleksandr Rybalko new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
51527cf7d04SAleksandr Rybalko rowssize = history_size * sizeof(term_pos_t *);
51627cf7d04SAleksandr Rybalko rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
51727cf7d04SAleksandr Rybalko
51827cf7d04SAleksandr Rybalko /* Toggle it. */
51927cf7d04SAleksandr Rybalko VTBUF_LOCK(vb);
52027cf7d04SAleksandr Rybalko old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
52127cf7d04SAleksandr Rybalko oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
52227cf7d04SAleksandr Rybalko copyrows = vb->vb_rows;
5231454439cSJean-Sébastien Pédron
52427cf7d04SAleksandr Rybalko w = vb->vb_scr_size.tp_col;
5251454439cSJean-Sébastien Pédron h = vb->vb_scr_size.tp_row;
5261454439cSJean-Sébastien Pédron old_history_size = vb->vb_history_size;
527e1734edfSEd Maste history_full = vb->vb_flags & VBF_HISTORY_FULL ||
528e1734edfSEd Maste vb->vb_curroffset + h >= history_size;
52927cf7d04SAleksandr Rybalko
53027cf7d04SAleksandr Rybalko vb->vb_history_size = history_size;
53127cf7d04SAleksandr Rybalko vb->vb_buffer = new;
53227cf7d04SAleksandr Rybalko vb->vb_rows = rows;
53327cf7d04SAleksandr Rybalko vb->vb_flags &= ~VBF_STATIC;
53427cf7d04SAleksandr Rybalko vb->vb_scr_size = *p;
53527cf7d04SAleksandr Rybalko vtbuf_init_rows(vb);
53627cf7d04SAleksandr Rybalko
5371454439cSJean-Sébastien Pédron /*
5381454439cSJean-Sébastien Pédron * Copy rows to the new buffer. The first row in the history
5391454439cSJean-Sébastien Pédron * is back to index 0, ie. the new buffer doesn't cycle.
5401454439cSJean-Sébastien Pédron */
541e1734edfSEd Maste if (history_size > old_history_size) {
5421454439cSJean-Sébastien Pédron for (r = 0; r < old_history_size; r++) {
5431454439cSJean-Sébastien Pédron row = rows[r];
5441454439cSJean-Sébastien Pédron
5451454439cSJean-Sébastien Pédron /* Compute the corresponding row in the old buffer. */
546e1734edfSEd Maste if (history_full)
5471454439cSJean-Sébastien Pédron /*
5481454439cSJean-Sébastien Pédron * The buffer is full, the "top" row is
5491454439cSJean-Sébastien Pédron * the one just after the viewable area
5501454439cSJean-Sébastien Pédron * (curroffset + viewable height) in the
5511454439cSJean-Sébastien Pédron * cycling buffer. The corresponding row
5521454439cSJean-Sébastien Pédron * is computed from this top row.
5531454439cSJean-Sébastien Pédron */
5541454439cSJean-Sébastien Pédron oldrow = copyrows[
5551454439cSJean-Sébastien Pédron (vb->vb_curroffset + h + r) %
5561454439cSJean-Sébastien Pédron old_history_size];
5571454439cSJean-Sébastien Pédron else
5581454439cSJean-Sébastien Pédron /*
5591454439cSJean-Sébastien Pédron * The buffer is not full, therefore,
5601454439cSJean-Sébastien Pédron * we didn't cycle already. The
5611454439cSJean-Sébastien Pédron * corresponding rows are the same in
5621454439cSJean-Sébastien Pédron * both buffers.
5631454439cSJean-Sébastien Pédron */
5641454439cSJean-Sébastien Pédron oldrow = copyrows[r];
5651454439cSJean-Sébastien Pédron
5661454439cSJean-Sébastien Pédron memmove(row, oldrow,
5671454439cSJean-Sébastien Pédron MIN(p->tp_col, w) * sizeof(term_char_t));
5681454439cSJean-Sébastien Pédron
5697344ee18SMarius Strobl /*
5707344ee18SMarius Strobl * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
5717344ee18SMarius Strobl * extended lines of kernel text using the wrong
5727344ee18SMarius Strobl * background color.
5737344ee18SMarius Strobl */
5741454439cSJean-Sébastien Pédron for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
575df1bc27aSToomas Soome row[c] = VTBUF_SPACE_CHAR(ch);
57627cf7d04SAleksandr Rybalko }
5771454439cSJean-Sébastien Pédron }
5781454439cSJean-Sébastien Pédron
5791454439cSJean-Sébastien Pédron /* Fill remaining rows. */
580e1734edfSEd Maste for (r = old_history_size; r < history_size; r++) {
581e1734edfSEd Maste row = rows[r];
582e1734edfSEd Maste for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
583df1bc27aSToomas Soome row[c] = VTBUF_SPACE_CHAR(ch);
584e1734edfSEd Maste }
585e1734edfSEd Maste }
5861454439cSJean-Sébastien Pédron
5871454439cSJean-Sébastien Pédron vb->vb_flags &= ~VBF_HISTORY_FULL;
588e1734edfSEd Maste
589e1734edfSEd Maste /*
590e1734edfSEd Maste * If the screen is already filled (there are non-visible lines
591e1734edfSEd Maste * above the current viewable area), adjust curroffset to the
592e1734edfSEd Maste * new viewable area.
593e1734edfSEd Maste *
594e1734edfSEd Maste * If the old buffer was full, set curroffset to the
595e1734edfSEd Maste * <h>th most recent line of history in the new, non-cycled
596e1734edfSEd Maste * buffer. Otherwise, it didn't cycle, so the old curroffset
597e1734edfSEd Maste * is the same in the new buffer.
598e1734edfSEd Maste */
599e1734edfSEd Maste if (history_full)
600e1734edfSEd Maste vb->vb_curroffset = old_history_size - h;
6011454439cSJean-Sébastien Pédron } else {
6021454439cSJean-Sébastien Pédron /*
6031454439cSJean-Sébastien Pédron * (old_history_size - history_size) lines of history are
6041454439cSJean-Sébastien Pédron * dropped.
6051454439cSJean-Sébastien Pédron */
6061454439cSJean-Sébastien Pédron for (r = 0; r < history_size; r++) {
6071454439cSJean-Sébastien Pédron row = rows[r];
6081454439cSJean-Sébastien Pédron
6091454439cSJean-Sébastien Pédron /*
6101454439cSJean-Sébastien Pédron * Compute the corresponding row in the old buffer.
6111454439cSJean-Sébastien Pédron *
6121454439cSJean-Sébastien Pédron * See the equivalent if{} block above for an
6131454439cSJean-Sébastien Pédron * explanation.
6141454439cSJean-Sébastien Pédron */
615e1734edfSEd Maste if (history_full)
6161454439cSJean-Sébastien Pédron oldrow = copyrows[
6171454439cSJean-Sébastien Pédron (vb->vb_curroffset + h + r +
6181454439cSJean-Sébastien Pédron (old_history_size - history_size)) %
6191454439cSJean-Sébastien Pédron old_history_size];
6201454439cSJean-Sébastien Pédron else
621e1734edfSEd Maste oldrow = copyrows[r];
6221454439cSJean-Sébastien Pédron
6231454439cSJean-Sébastien Pédron memmove(row, oldrow,
6241454439cSJean-Sébastien Pédron MIN(p->tp_col, w) * sizeof(term_char_t));
6251454439cSJean-Sébastien Pédron
6261454439cSJean-Sébastien Pédron /*
6271454439cSJean-Sébastien Pédron * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
6281454439cSJean-Sébastien Pédron * extended lines of kernel text using the wrong
6291454439cSJean-Sébastien Pédron * background color.
6301454439cSJean-Sébastien Pédron */
6311454439cSJean-Sébastien Pédron for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
632df1bc27aSToomas Soome row[c] = VTBUF_SPACE_CHAR(ch);
63327cf7d04SAleksandr Rybalko }
63427cf7d04SAleksandr Rybalko }
6351454439cSJean-Sébastien Pédron
636e1734edfSEd Maste if (history_full) {
637e1734edfSEd Maste vb->vb_curroffset = history_size - h;
6381454439cSJean-Sébastien Pédron vb->vb_flags |= VBF_HISTORY_FULL;
6391454439cSJean-Sébastien Pédron }
6401454439cSJean-Sébastien Pédron }
6411454439cSJean-Sébastien Pédron
642e1734edfSEd Maste vb->vb_roffset = vb->vb_curroffset;
643e1734edfSEd Maste
644da49f6bcSJean-Sébastien Pédron /* Adjust cursor position. */
645da49f6bcSJean-Sébastien Pédron if (vb->vb_cursor.tp_col > p->tp_col - 1)
646da49f6bcSJean-Sébastien Pédron /*
647da49f6bcSJean-Sébastien Pédron * Move cursor to the last column, in case its previous
648da49f6bcSJean-Sébastien Pédron * position is outside of the new screen area.
649da49f6bcSJean-Sébastien Pédron */
650da49f6bcSJean-Sébastien Pédron vb->vb_cursor.tp_col = p->tp_col - 1;
651da49f6bcSJean-Sébastien Pédron
652da49f6bcSJean-Sébastien Pédron if (vb->vb_curroffset > 0 || vb->vb_cursor.tp_row > p->tp_row - 1)
653da49f6bcSJean-Sébastien Pédron /* Move cursor to the last line on the screen. */
654da49f6bcSJean-Sébastien Pédron vb->vb_cursor.tp_row = p->tp_row - 1;
655da49f6bcSJean-Sébastien Pédron
65627cf7d04SAleksandr Rybalko VTBUF_UNLOCK(vb);
6571454439cSJean-Sébastien Pédron
65827cf7d04SAleksandr Rybalko /* Deallocate old buffer. */
65927cf7d04SAleksandr Rybalko free(old, M_VTBUF);
66027cf7d04SAleksandr Rybalko free(oldrows, M_VTBUF);
66127cf7d04SAleksandr Rybalko }
66227cf7d04SAleksandr Rybalko
66327cf7d04SAleksandr Rybalko void
vtbuf_putchar(struct vt_buf * vb,const term_pos_t * p,term_char_t c)66427cf7d04SAleksandr Rybalko vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
66527cf7d04SAleksandr Rybalko {
66627cf7d04SAleksandr Rybalko term_char_t *row;
66727cf7d04SAleksandr Rybalko
66827cf7d04SAleksandr Rybalko KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
66927cf7d04SAleksandr Rybalko ("vtbuf_putchar tp_row %d must be less than screen width %d",
67027cf7d04SAleksandr Rybalko p->tp_row, vb->vb_scr_size.tp_row));
67127cf7d04SAleksandr Rybalko KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
67227cf7d04SAleksandr Rybalko ("vtbuf_putchar tp_col %d must be less than screen height %d",
67327cf7d04SAleksandr Rybalko p->tp_col, vb->vb_scr_size.tp_col));
67427cf7d04SAleksandr Rybalko
67527cf7d04SAleksandr Rybalko row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
67627cf7d04SAleksandr Rybalko VTBUF_MAX_HEIGHT(vb)];
67727cf7d04SAleksandr Rybalko if (row[p->tp_col] != c) {
67827cf7d04SAleksandr Rybalko row[p->tp_col] = c;
679547e74a8SJean-Sébastien Pédron vtbuf_dirty_cell(vb, p);
68027cf7d04SAleksandr Rybalko }
68127cf7d04SAleksandr Rybalko }
68227cf7d04SAleksandr Rybalko
68327cf7d04SAleksandr Rybalko void
vtbuf_cursor_position(struct vt_buf * vb,const term_pos_t * p)68427cf7d04SAleksandr Rybalko vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
68527cf7d04SAleksandr Rybalko {
68627cf7d04SAleksandr Rybalko if (vb->vb_flags & VBF_CURSOR) {
687547e74a8SJean-Sébastien Pédron vtbuf_dirty_cell(vb, &vb->vb_cursor);
68827cf7d04SAleksandr Rybalko vb->vb_cursor = *p;
689547e74a8SJean-Sébastien Pédron vtbuf_dirty_cell(vb, &vb->vb_cursor);
69027cf7d04SAleksandr Rybalko } else {
69127cf7d04SAleksandr Rybalko vb->vb_cursor = *p;
69227cf7d04SAleksandr Rybalko }
69327cf7d04SAleksandr Rybalko }
69427cf7d04SAleksandr Rybalko
6950f49db6eSAleksandr Rybalko #ifndef SC_NO_CUTPASTE
69627cf7d04SAleksandr Rybalko static void
vtbuf_flush_mark(struct vt_buf * vb)69727cf7d04SAleksandr Rybalko vtbuf_flush_mark(struct vt_buf *vb)
69827cf7d04SAleksandr Rybalko {
69927cf7d04SAleksandr Rybalko term_rect_t area;
70027cf7d04SAleksandr Rybalko int s, e;
70127cf7d04SAleksandr Rybalko
70227cf7d04SAleksandr Rybalko /* Notify renderer to update marked region. */
703c0e295deSAleksandr Rybalko if ((vb->vb_mark_start.tp_col != vb->vb_mark_end.tp_col) ||
704c0e295deSAleksandr Rybalko (vb->vb_mark_start.tp_row != vb->vb_mark_end.tp_row)) {
70527cf7d04SAleksandr Rybalko s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
70627cf7d04SAleksandr Rybalko e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
70727cf7d04SAleksandr Rybalko
70827cf7d04SAleksandr Rybalko area.tr_begin.tp_col = 0;
70927cf7d04SAleksandr Rybalko area.tr_begin.tp_row = MIN(s, e);
71027cf7d04SAleksandr Rybalko
71127cf7d04SAleksandr Rybalko area.tr_end.tp_col = vb->vb_scr_size.tp_col;
71227cf7d04SAleksandr Rybalko area.tr_end.tp_row = MAX(s, e) + 1;
71327cf7d04SAleksandr Rybalko
714547e74a8SJean-Sébastien Pédron VTBUF_LOCK(vb);
71527cf7d04SAleksandr Rybalko vtbuf_dirty(vb, &area);
716547e74a8SJean-Sébastien Pédron VTBUF_UNLOCK(vb);
71727cf7d04SAleksandr Rybalko }
71827cf7d04SAleksandr Rybalko }
71927cf7d04SAleksandr Rybalko
72027cf7d04SAleksandr Rybalko int
vtbuf_get_marked_len(struct vt_buf * vb)72127cf7d04SAleksandr Rybalko vtbuf_get_marked_len(struct vt_buf *vb)
72227cf7d04SAleksandr Rybalko {
72327cf7d04SAleksandr Rybalko int ei, si, sz;
72427cf7d04SAleksandr Rybalko term_pos_t s, e;
72527cf7d04SAleksandr Rybalko
72627cf7d04SAleksandr Rybalko /* Swap according to window coordinates. */
72727cf7d04SAleksandr Rybalko if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
72827cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col) >
72927cf7d04SAleksandr Rybalko POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
73027cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col)) {
73127cf7d04SAleksandr Rybalko POS_COPY(e, vb->vb_mark_start);
73227cf7d04SAleksandr Rybalko POS_COPY(s, vb->vb_mark_end);
73327cf7d04SAleksandr Rybalko } else {
73427cf7d04SAleksandr Rybalko POS_COPY(s, vb->vb_mark_start);
73527cf7d04SAleksandr Rybalko POS_COPY(e, vb->vb_mark_end);
73627cf7d04SAleksandr Rybalko }
73727cf7d04SAleksandr Rybalko
73827cf7d04SAleksandr Rybalko si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
73927cf7d04SAleksandr Rybalko ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
74027cf7d04SAleksandr Rybalko
7415fe0a825SHans Petter Selasky /* Number symbols and number of rows to inject \r */
74290b89100SIvan Quitschal sz = ei - si + (1 + e.tp_row - s.tp_row);
74327cf7d04SAleksandr Rybalko
74427cf7d04SAleksandr Rybalko return (sz * sizeof(term_char_t));
74527cf7d04SAleksandr Rybalko }
74627cf7d04SAleksandr Rybalko
7479971e6afSHans Petter Selasky static bool
tchar_is_word_separator(term_char_t ch)7489971e6afSHans Petter Selasky tchar_is_word_separator(term_char_t ch)
7499971e6afSHans Petter Selasky {
7509971e6afSHans Petter Selasky /* List of unicode word separator characters: */
7519971e6afSHans Petter Selasky switch (TCHAR_CHARACTER(ch)) {
7529971e6afSHans Petter Selasky case 0x0020: /* SPACE */
7539971e6afSHans Petter Selasky case 0x180E: /* MONGOLIAN VOWEL SEPARATOR */
7549971e6afSHans Petter Selasky case 0x2002: /* EN SPACE (nut) */
7559971e6afSHans Petter Selasky case 0x2003: /* EM SPACE (mutton) */
7569971e6afSHans Petter Selasky case 0x2004: /* THREE-PER-EM SPACE (thick space) */
7579971e6afSHans Petter Selasky case 0x2005: /* FOUR-PER-EM SPACE (mid space) */
7589971e6afSHans Petter Selasky case 0x2006: /* SIX-PER-EM SPACE */
7599971e6afSHans Petter Selasky case 0x2008: /* PUNCTUATION SPACE */
7609971e6afSHans Petter Selasky case 0x2009: /* THIN SPACE */
7619971e6afSHans Petter Selasky case 0x200A: /* HAIR SPACE */
7629971e6afSHans Petter Selasky case 0x200B: /* ZERO WIDTH SPACE */
7639971e6afSHans Petter Selasky case 0x3000: /* IDEOGRAPHIC SPACE */
7649971e6afSHans Petter Selasky return (true);
7659971e6afSHans Petter Selasky default:
7669971e6afSHans Petter Selasky return (false);
7679971e6afSHans Petter Selasky }
7689971e6afSHans Petter Selasky }
7699971e6afSHans Petter Selasky
77027cf7d04SAleksandr Rybalko void
vtbuf_extract_marked(struct vt_buf * vb,term_char_t * buf,int sz,int mark)77190b89100SIvan Quitschal vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz, int mark)
77227cf7d04SAleksandr Rybalko {
7735fe0a825SHans Petter Selasky int i, j, r, c, cs, ce;
77427cf7d04SAleksandr Rybalko term_pos_t s, e;
77527cf7d04SAleksandr Rybalko
77627cf7d04SAleksandr Rybalko /* Swap according to window coordinates. */
77727cf7d04SAleksandr Rybalko if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
77827cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col) >
77927cf7d04SAleksandr Rybalko POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
78027cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col)) {
78127cf7d04SAleksandr Rybalko POS_COPY(e, vb->vb_mark_start);
78227cf7d04SAleksandr Rybalko POS_COPY(s, vb->vb_mark_end);
78327cf7d04SAleksandr Rybalko } else {
78427cf7d04SAleksandr Rybalko POS_COPY(s, vb->vb_mark_start);
78527cf7d04SAleksandr Rybalko POS_COPY(e, vb->vb_mark_end);
78627cf7d04SAleksandr Rybalko }
78727cf7d04SAleksandr Rybalko
78827cf7d04SAleksandr Rybalko i = 0;
78927cf7d04SAleksandr Rybalko for (r = s.tp_row; r <= e.tp_row; r++) {
79027cf7d04SAleksandr Rybalko cs = (r == s.tp_row)?s.tp_col:0;
79127cf7d04SAleksandr Rybalko ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
7925fe0a825SHans Petter Selasky
7935fe0a825SHans Petter Selasky /* Copy characters from terminal window. */
7945fe0a825SHans Petter Selasky j = i;
7955fe0a825SHans Petter Selasky for (c = cs; c < ce; c++)
79627cf7d04SAleksandr Rybalko buf[i++] = vb->vb_rows[r][c];
7975fe0a825SHans Petter Selasky
7985fe0a825SHans Petter Selasky /* For all rows, but the last one. */
79990b89100SIvan Quitschal if (r != e.tp_row || mark == VTB_MARK_ROW) {
8005fe0a825SHans Petter Selasky /* Trim trailing word separators, if any. */
8015fe0a825SHans Petter Selasky for (; i != j; i--) {
8029971e6afSHans Petter Selasky if (!tchar_is_word_separator(buf[i - 1]))
8035fe0a825SHans Petter Selasky break;
8045fe0a825SHans Petter Selasky }
8055fe0a825SHans Petter Selasky /* Add newline character as expected by TTY. */
80627cf7d04SAleksandr Rybalko buf[i++] = '\r';
80727cf7d04SAleksandr Rybalko }
80827cf7d04SAleksandr Rybalko }
8095fe0a825SHans Petter Selasky /* Zero rest of expected buffer size, if any. */
8105fe0a825SHans Petter Selasky while ((i * sizeof(buf[0])) < sz)
8115fe0a825SHans Petter Selasky buf[i++] = '\0';
8125fe0a825SHans Petter Selasky
8135fe0a825SHans Petter Selasky MPASS((i * sizeof(buf[0])) == sz);
81427cf7d04SAleksandr Rybalko }
81527cf7d04SAleksandr Rybalko
81627cf7d04SAleksandr Rybalko int
vtbuf_set_mark(struct vt_buf * vb,int type,int col,int row)81727cf7d04SAleksandr Rybalko vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
81827cf7d04SAleksandr Rybalko {
81927cf7d04SAleksandr Rybalko term_char_t *r;
82027cf7d04SAleksandr Rybalko int i;
82127cf7d04SAleksandr Rybalko
82227cf7d04SAleksandr Rybalko switch (type) {
82327cf7d04SAleksandr Rybalko case VTB_MARK_END: /* B1 UP */
82427cf7d04SAleksandr Rybalko if (vb->vb_mark_last != VTB_MARK_MOVE)
82527cf7d04SAleksandr Rybalko return (0);
82627cf7d04SAleksandr Rybalko /* FALLTHROUGH */
82727cf7d04SAleksandr Rybalko case VTB_MARK_MOVE:
82827cf7d04SAleksandr Rybalko case VTB_MARK_EXTEND:
82927cf7d04SAleksandr Rybalko vtbuf_flush_mark(vb); /* Clean old mark. */
83027cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col = col;
83127cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
83227cf7d04SAleksandr Rybalko break;
83327cf7d04SAleksandr Rybalko case VTB_MARK_START:
83427cf7d04SAleksandr Rybalko vtbuf_flush_mark(vb); /* Clean old mark. */
83527cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col = col;
83627cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
83727cf7d04SAleksandr Rybalko /* Start again, so clear end point. */
83827cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col = col;
83927cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
84027cf7d04SAleksandr Rybalko break;
84127cf7d04SAleksandr Rybalko case VTB_MARK_WORD:
84227cf7d04SAleksandr Rybalko vtbuf_flush_mark(vb); /* Clean old mark. */
84327cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
84427cf7d04SAleksandr Rybalko vtbuf_wth(vb, row);
84527cf7d04SAleksandr Rybalko r = vb->vb_rows[vb->vb_mark_start.tp_row];
84627cf7d04SAleksandr Rybalko for (i = col; i >= 0; i --) {
8479971e6afSHans Petter Selasky if (tchar_is_word_separator(r[i])) {
84827cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col = i + 1;
84927cf7d04SAleksandr Rybalko break;
85027cf7d04SAleksandr Rybalko }
85127cf7d04SAleksandr Rybalko }
852692bb3f0SEd Maste /* No space - word extends to beginning of line. */
853692bb3f0SEd Maste if (i == -1)
854692bb3f0SEd Maste vb->vb_mark_start.tp_col = 0;
85527cf7d04SAleksandr Rybalko for (i = col; i < vb->vb_scr_size.tp_col; i++) {
8569971e6afSHans Petter Selasky if (tchar_is_word_separator(r[i])) {
85727cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col = i;
85827cf7d04SAleksandr Rybalko break;
85927cf7d04SAleksandr Rybalko }
86027cf7d04SAleksandr Rybalko }
861521dbfd6SEd Maste /* No space - word extends to end of line. */
862521dbfd6SEd Maste if (i == vb->vb_scr_size.tp_col)
863521dbfd6SEd Maste vb->vb_mark_end.tp_col = i;
864521dbfd6SEd Maste
86527cf7d04SAleksandr Rybalko if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
86627cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
86727cf7d04SAleksandr Rybalko break;
86827cf7d04SAleksandr Rybalko case VTB_MARK_ROW:
86927cf7d04SAleksandr Rybalko vtbuf_flush_mark(vb); /* Clean old mark. */
87027cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_col = 0;
87127cf7d04SAleksandr Rybalko vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
87227cf7d04SAleksandr Rybalko vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
87327cf7d04SAleksandr Rybalko vtbuf_wth(vb, row);
87427cf7d04SAleksandr Rybalko break;
87527cf7d04SAleksandr Rybalko case VTB_MARK_NONE:
87627cf7d04SAleksandr Rybalko vb->vb_mark_last = type;
87727cf7d04SAleksandr Rybalko /* FALLTHROUGH */
87827cf7d04SAleksandr Rybalko default:
87927cf7d04SAleksandr Rybalko /* panic? */
88027cf7d04SAleksandr Rybalko return (0);
88127cf7d04SAleksandr Rybalko }
88227cf7d04SAleksandr Rybalko
88327cf7d04SAleksandr Rybalko vb->vb_mark_last = type;
88427cf7d04SAleksandr Rybalko /* Draw new marked region. */
88527cf7d04SAleksandr Rybalko vtbuf_flush_mark(vb);
88627cf7d04SAleksandr Rybalko return (1);
88727cf7d04SAleksandr Rybalko }
8880f49db6eSAleksandr Rybalko #endif
88927cf7d04SAleksandr Rybalko
89027cf7d04SAleksandr Rybalko void
vtbuf_cursor_visibility(struct vt_buf * vb,int yes)89127cf7d04SAleksandr Rybalko vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
89227cf7d04SAleksandr Rybalko {
89327cf7d04SAleksandr Rybalko int oflags, nflags;
89427cf7d04SAleksandr Rybalko
89527cf7d04SAleksandr Rybalko oflags = vb->vb_flags;
89627cf7d04SAleksandr Rybalko if (yes)
89727cf7d04SAleksandr Rybalko vb->vb_flags |= VBF_CURSOR;
89827cf7d04SAleksandr Rybalko else
89927cf7d04SAleksandr Rybalko vb->vb_flags &= ~VBF_CURSOR;
90027cf7d04SAleksandr Rybalko nflags = vb->vb_flags;
90127cf7d04SAleksandr Rybalko
90227cf7d04SAleksandr Rybalko if (oflags != nflags)
903547e74a8SJean-Sébastien Pédron vtbuf_dirty_cell(vb, &vb->vb_cursor);
90427cf7d04SAleksandr Rybalko }
90527cf7d04SAleksandr Rybalko
90627cf7d04SAleksandr Rybalko void
vtbuf_scroll_mode(struct vt_buf * vb,int yes)90727cf7d04SAleksandr Rybalko vtbuf_scroll_mode(struct vt_buf *vb, int yes)
90827cf7d04SAleksandr Rybalko {
90927cf7d04SAleksandr Rybalko int oflags, nflags;
91027cf7d04SAleksandr Rybalko
91127cf7d04SAleksandr Rybalko VTBUF_LOCK(vb);
91227cf7d04SAleksandr Rybalko oflags = vb->vb_flags;
91327cf7d04SAleksandr Rybalko if (yes)
91427cf7d04SAleksandr Rybalko vb->vb_flags |= VBF_SCROLL;
91527cf7d04SAleksandr Rybalko else
91627cf7d04SAleksandr Rybalko vb->vb_flags &= ~VBF_SCROLL;
91727cf7d04SAleksandr Rybalko nflags = vb->vb_flags;
91827cf7d04SAleksandr Rybalko
91927cf7d04SAleksandr Rybalko if (oflags != nflags)
920547e74a8SJean-Sébastien Pédron vtbuf_dirty_cell(vb, &vb->vb_cursor);
92100c33067SJean-Sébastien Pédron VTBUF_UNLOCK(vb);
92227cf7d04SAleksandr Rybalko }
923