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