]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/vt/vt_buf.c
vt(4): Put for() loop outside switch() in vt_generate_cons_palette()
[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 + vb->vb_scr_size.tp_row >= vb->vb_history_size) {
138                 vb->vb_flags |= VBF_HISTORY_FULL;
139                 vb->vb_curroffset %= vb->vb_history_size;
140         }
141         if ((vb->vb_flags & VBF_SCROLL) == 0) {
142                 vb->vb_roffset = vb->vb_curroffset;
143         }
144
145 #ifndef SC_NO_CUTPASTE
146         sz = vb->vb_history_size;
147         cur = vb->vb_roffset + vb->vb_scr_size.tp_row + sz - 1;
148         if (vtbuf_in_this_range(cur, vb->vb_mark_start.tp_row, cur + offset, sz) ||
149             vtbuf_in_this_range(cur, vb->vb_mark_end.tp_row, cur + offset, sz)) {
150                 /* clear screen selection */
151                 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row;
152                 vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
153         }
154 #endif
155 }
156
157 void
158 vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
159 {
160
161         *offset = vb->vb_roffset;
162 }
163
164 #ifndef SC_NO_CUTPASTE  /* Only mouse support use it now. */
165 /* Translate history row to current view row number. */
166 static int
167 vtbuf_htw(const struct vt_buf *vb, int row)
168 {
169
170         /*
171          * total 1000 rows.
172          * History offset       roffset winrow
173          *      205             200     ((205 - 200 + 1000) % 1000) = 5
174          *      90              990     ((90 - 990 + 1000) % 1000) = 100
175          */
176         return ((row - vb->vb_roffset + vb->vb_history_size) %
177             vb->vb_history_size);
178 }
179
180 /* Translate current view row number to history row. */
181 static int
182 vtbuf_wth(const struct vt_buf *vb, int row)
183 {
184
185         return ((vb->vb_roffset + row) % vb->vb_history_size);
186 }
187
188 /*
189  * Test if an index in a circular buffer is within a range.
190  *
191  * begin - start index
192  * end - end index
193  * test - test index
194  * sz - size of circular buffer when it turns over
195  */
196 static int
197 vtbuf_in_this_range(int begin, int test, int end, int sz)
198 {
199
200         begin %= sz;
201         end %= sz;
202
203         /* check for inversion */
204         if (begin > end)
205                 return (test >= begin || test < end);
206         else
207                 return (test >= begin && test < end);
208 }
209 #endif
210
211 int
212 vtbuf_iscursor(const struct vt_buf *vb, int row, int col)
213 {
214 #ifndef SC_NO_CUTPASTE
215         int sc, sr, sz, ec, er, tmp;
216 #endif
217
218         if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
219             (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
220                 return (1);
221
222 #ifndef SC_NO_CUTPASTE
223         /* Mark cut/paste region. */
224         if (vb->vb_mark_start.tp_col == vb->vb_mark_end.tp_col &&
225             vb->vb_mark_start.tp_row == vb->vb_mark_end.tp_row)
226                 return (0);
227
228         sc = vb->vb_mark_start.tp_col;
229         sr = vb->vb_mark_start.tp_row;
230         ec = vb->vb_mark_end.tp_col;
231         er = vb->vb_mark_end.tp_row;
232
233         /*
234          * Information about if the selection was made bottom-top or
235          * top-bottom is lost due to modulo arithmetics and needs to
236          * be recovered:
237          */
238         sz = vb->vb_history_size;
239         tmp = (sz + er - sr) % sz;
240         row = vtbuf_wth(vb, row);
241
242         /* Swap start and end if start > end */
243         if ((2 * tmp) > sz || (tmp == 0 && sc > ec)) {
244                 tmp = sc; sc = ec; ec = tmp;
245                 tmp = sr; sr = er; er = tmp;
246         }
247
248         if (vtbuf_in_this_range(POS_INDEX(sc, sr), POS_INDEX(col, row),
249             POS_INDEX(ec, er), POS_INDEX(0, sz)))
250                 return (1);
251 #endif
252
253         return (0);
254 }
255
256 static inline void
257 vtbuf_dirty_locked(struct vt_buf *vb, const term_rect_t *area)
258 {
259
260         if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
261                 vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
262         if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
263                 vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
264         if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
265                 vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
266         if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
267                 vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
268 }
269
270 void
271 vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
272 {
273
274         VTBUF_LOCK(vb);
275         vtbuf_dirty_locked(vb, area);
276         VTBUF_UNLOCK(vb);
277 }
278
279 static inline void
280 vtbuf_dirty_cell_locked(struct vt_buf *vb, const term_pos_t *p)
281 {
282         term_rect_t area;
283
284         area.tr_begin = *p;
285         area.tr_end.tp_row = p->tp_row + 1;
286         area.tr_end.tp_col = p->tp_col + 1;
287         vtbuf_dirty_locked(vb, &area);
288 }
289
290 static void
291 vtbuf_make_undirty(struct vt_buf *vb)
292 {
293
294         vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
295         vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
296 }
297
298 void
299 vtbuf_undirty(struct vt_buf *vb, term_rect_t *r)
300 {
301
302         VTBUF_LOCK(vb);
303         *r = vb->vb_dirtyrect;
304         vtbuf_make_undirty(vb);
305         VTBUF_UNLOCK(vb);
306 }
307
308 void
309 vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
310 {
311         const term_pos_t *p1 = &r->tr_begin;
312         term_rect_t area;
313         unsigned int rows, cols;
314         int pr, rdiff;
315
316         KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
317             ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
318                 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
319         KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
320             ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
321                 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
322
323         KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
324             ("vtbuf_copy end.tp_row %d must be less than screen width %d",
325                 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
326         KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
327             ("vtbuf_copy end.tp_col %d must be less than screen height %d",
328                 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
329
330         KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
331             ("vtbuf_copy tp_row %d must be less than screen width %d",
332                 p2->tp_row, vb->vb_scr_size.tp_row));
333         KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
334             ("vtbuf_copy tp_col %d must be less than screen height %d",
335                 p2->tp_col, vb->vb_scr_size.tp_col));
336
337         rows = r->tr_end.tp_row - r->tr_begin.tp_row;
338         rdiff = r->tr_begin.tp_row - p2->tp_row;
339         cols = r->tr_end.tp_col - r->tr_begin.tp_col;
340         if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
341             r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
342             (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
343             rdiff > 0) { /* Only forward direction. Do not eat history. */
344                 vthistory_addlines(vb, rdiff);
345         } else if (p2->tp_row < p1->tp_row) {
346                 /* Handle overlapping copies of line segments. */
347                 /* Move data up. */
348                 for (pr = 0; pr < rows; pr++)
349                         memmove(
350                             &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
351                             &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
352                             cols * sizeof(term_char_t));
353         } else {
354                 /* Move data down. */
355                 for (pr = rows - 1; pr >= 0; pr--)
356                         memmove(
357                             &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
358                             &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
359                             cols * sizeof(term_char_t));
360         }
361
362         area.tr_begin = *p2;
363         area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
364         area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
365         vtbuf_dirty(vb, &area);
366 }
367
368 static void
369 vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
370 {
371         unsigned int pr, pc;
372         term_char_t *row;
373
374         for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
375                 row = vb->vb_rows[(vb->vb_curroffset + pr) %
376                     VTBUF_MAX_HEIGHT(vb)];
377                 for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
378                         row[pc] = c;
379                 }
380         }
381 }
382
383 void
384 vtbuf_fill_locked(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
385 {
386         KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
387             ("vtbuf_fill_locked begin.tp_row %d must be < screen height %d",
388                 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
389         KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
390             ("vtbuf_fill_locked begin.tp_col %d must be < screen width %d",
391                 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
392
393         KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
394             ("vtbuf_fill_locked end.tp_row %d must be <= screen height %d",
395                 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
396         KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
397             ("vtbuf_fill_locked end.tp_col %d must be <= screen width %d",
398                 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
399
400         VTBUF_LOCK(vb);
401         vtbuf_fill(vb, r, c);
402         vtbuf_dirty_locked(vb, r);
403         VTBUF_UNLOCK(vb);
404 }
405
406 static void
407 vtbuf_init_rows(struct vt_buf *vb)
408 {
409         int r;
410
411         vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
412
413         for (r = 0; r < vb->vb_history_size; r++)
414                 vb->vb_rows[r] = &vb->vb_buffer[r * vb->vb_scr_size.tp_col];
415 }
416
417 void
418 vtbuf_init_early(struct vt_buf *vb)
419 {
420         term_rect_t rect;
421
422         vb->vb_flags |= VBF_CURSOR;
423         vb->vb_roffset = 0;
424         vb->vb_curroffset = 0;
425         vb->vb_mark_start.tp_row = 0;
426         vb->vb_mark_start.tp_col = 0;
427         vb->vb_mark_end.tp_row = 0;
428         vb->vb_mark_end.tp_col = 0;
429
430         vtbuf_init_rows(vb);
431         rect.tr_begin.tp_row = rect.tr_begin.tp_col = 0;
432         rect.tr_end.tp_col = vb->vb_scr_size.tp_col;
433         rect.tr_end.tp_row = vb->vb_history_size;
434         vtbuf_fill(vb, &rect, VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR));
435         vtbuf_make_undirty(vb);
436         if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
437                 mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
438                 vb->vb_flags |= VBF_MTX_INIT;
439         }
440 }
441
442 void
443 vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
444 {
445         int sz;
446
447         vb->vb_scr_size = *p;
448         vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
449
450         if ((vb->vb_flags & VBF_STATIC) == 0) {
451                 sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
452                 vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
453
454                 sz = vb->vb_history_size * sizeof(term_char_t *);
455                 vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
456         }
457
458         vtbuf_init_early(vb);
459 }
460
461 void
462 vtbuf_sethistory_size(struct vt_buf *vb, unsigned int size)
463 {
464         term_pos_t p;
465
466         /* With same size */
467         p.tp_row = vb->vb_scr_size.tp_row;
468         p.tp_col = vb->vb_scr_size.tp_col;
469         vtbuf_grow(vb, &p, size);
470 }
471
472 void
473 vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, unsigned int history_size)
474 {
475         term_char_t *old, *new, **rows, **oldrows, **copyrows, *row, *oldrow;
476         unsigned int w, h, c, r, old_history_size;
477         size_t bufsize, rowssize;
478         int history_full;
479
480         history_size = MAX(history_size, p->tp_row);
481
482         /* Allocate new buffer. */
483         bufsize = history_size * p->tp_col * sizeof(term_char_t);
484         new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
485         rowssize = history_size * sizeof(term_pos_t *);
486         rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
487
488         /* Toggle it. */
489         VTBUF_LOCK(vb);
490         old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
491         oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
492         copyrows = vb->vb_rows;
493
494         w = vb->vb_scr_size.tp_col;
495         h = vb->vb_scr_size.tp_row;
496         old_history_size = vb->vb_history_size;
497         history_full = vb->vb_flags & VBF_HISTORY_FULL ||
498             vb->vb_curroffset + h >= history_size;
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         /*
508          * Copy rows to the new buffer. The first row in the history
509          * is back to index 0, ie. the new buffer doesn't cycle.
510          */
511         if (history_size > old_history_size) {
512                 for (r = 0; r < old_history_size; r ++) {
513                         row = rows[r];
514
515                         /* Compute the corresponding row in the old buffer. */
516                         if (history_full)
517                                 /*
518                                  * The buffer is full, the "top" row is
519                                  * the one just after the viewable area
520                                  * (curroffset + viewable height) in the
521                                  * cycling buffer. The corresponding row
522                                  * is computed from this top row.
523                                  */
524                                 oldrow = copyrows[
525                                     (vb->vb_curroffset + h + r) %
526                                     old_history_size];
527                         else
528                                 /*
529                                  * The buffer is not full, therefore,
530                                  * we didn't cycle already. The
531                                  * corresponding rows are the same in
532                                  * both buffers.
533                                  */
534                                 oldrow = copyrows[r];
535
536                         memmove(row, oldrow,
537                             MIN(p->tp_col, w) * sizeof(term_char_t));
538
539                         /*
540                          * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
541                          * extended lines of kernel text using the wrong
542                          * background color.
543                          */
544                         for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
545                                 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
546                         }
547                 }
548
549                 /* Fill remaining rows. */
550                 for (r = old_history_size; r < history_size; r++) {
551                         row = rows[r];
552                         for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
553                                 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
554                         }
555                 }
556
557                 vb->vb_flags &= ~VBF_HISTORY_FULL;
558
559                 /*
560                  * If the screen is already filled (there are non-visible lines
561                  * above the current viewable area), adjust curroffset to the
562                  * new viewable area.
563                  *
564                  * If the old buffer was full, set curroffset to the
565                  * <h>th most recent line of history in the new, non-cycled
566                  * buffer. Otherwise, it didn't cycle, so the old curroffset
567                  * is the same in the new buffer.
568                  */
569                 if (history_full)
570                         vb->vb_curroffset = old_history_size - h;
571         } else {
572                 /*
573                  * (old_history_size - history_size) lines of history are
574                  * dropped.
575                  */
576                 for (r = 0; r < history_size; r ++) {
577                         row = rows[r];
578
579                         /*
580                          * Compute the corresponding row in the old buffer.
581                          *
582                          * See the equivalent if{} block above for an
583                          * explanation.
584                          */
585                         if (history_full)
586                                 oldrow = copyrows[
587                                     (vb->vb_curroffset + h + r +
588                                      (old_history_size - history_size)) %
589                                     old_history_size];
590                         else
591                                 oldrow = copyrows[r];
592
593                         memmove(row, oldrow,
594                             MIN(p->tp_col, w) * sizeof(term_char_t));
595
596                         /*
597                          * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
598                          * extended lines of kernel text using the wrong
599                          * background color.
600                          */
601                         for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
602                                 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
603                         }
604                 }
605
606                 if (history_full) {
607                         vb->vb_curroffset = history_size - h;
608                         vb->vb_flags |= VBF_HISTORY_FULL;
609                 }
610         }
611
612         vb->vb_roffset = vb->vb_curroffset;
613
614         /* Adjust cursor position. */
615         if (vb->vb_cursor.tp_col > p->tp_col - 1)
616                 /*
617                  * Move cursor to the last column, in case its previous
618                  * position is outside of the new screen area.
619                  */
620                 vb->vb_cursor.tp_col = p->tp_col - 1;
621
622         if (vb->vb_curroffset > 0 || vb->vb_cursor.tp_row > p->tp_row - 1)
623                 /* Move cursor to the last line on the screen. */
624                 vb->vb_cursor.tp_row = p->tp_row - 1;
625
626         VTBUF_UNLOCK(vb);
627
628         /* Deallocate old buffer. */
629         free(old, M_VTBUF);
630         free(oldrows, M_VTBUF);
631 }
632
633 void
634 vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
635 {
636         term_char_t *row;
637
638         KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
639             ("vtbuf_putchar tp_row %d must be less than screen width %d",
640                 p->tp_row, vb->vb_scr_size.tp_row));
641         KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
642             ("vtbuf_putchar tp_col %d must be less than screen height %d",
643                 p->tp_col, vb->vb_scr_size.tp_col));
644
645         row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
646             VTBUF_MAX_HEIGHT(vb)];
647         if (row[p->tp_col] != c) {
648                 VTBUF_LOCK(vb);
649                 row[p->tp_col] = c;
650                 vtbuf_dirty_cell_locked(vb, p);
651                 VTBUF_UNLOCK(vb);
652         }
653 }
654
655 void
656 vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
657 {
658
659         if (vb->vb_flags & VBF_CURSOR) {
660                 VTBUF_LOCK(vb);
661                 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
662                 vb->vb_cursor = *p;
663                 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
664                 VTBUF_UNLOCK(vb);
665         } else {
666                 vb->vb_cursor = *p;
667         }
668 }
669
670 #ifndef SC_NO_CUTPASTE
671 static void
672 vtbuf_flush_mark(struct vt_buf *vb)
673 {
674         term_rect_t area;
675         int s, e;
676
677         /* Notify renderer to update marked region. */
678         if ((vb->vb_mark_start.tp_col != vb->vb_mark_end.tp_col) ||
679             (vb->vb_mark_start.tp_row != vb->vb_mark_end.tp_row)) {
680
681                 s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
682                 e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
683
684                 area.tr_begin.tp_col = 0;
685                 area.tr_begin.tp_row = MIN(s, e);
686
687                 area.tr_end.tp_col = vb->vb_scr_size.tp_col;
688                 area.tr_end.tp_row = MAX(s, e) + 1;
689
690                 vtbuf_dirty(vb, &area);
691         }
692 }
693
694 int
695 vtbuf_get_marked_len(struct vt_buf *vb)
696 {
697         int ei, si, sz;
698         term_pos_t s, e;
699
700         /* Swap according to window coordinates. */
701         if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
702             vb->vb_mark_start.tp_col) >
703             POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
704             vb->vb_mark_end.tp_col)) {
705                 POS_COPY(e, vb->vb_mark_start);
706                 POS_COPY(s, vb->vb_mark_end);
707         } else {
708                 POS_COPY(s, vb->vb_mark_start);
709                 POS_COPY(e, vb->vb_mark_end);
710         }
711
712         si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
713         ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
714
715         /* Number symbols and number of rows to inject \n */
716         sz = ei - si + ((e.tp_row - s.tp_row) * 2);
717
718         return (sz * sizeof(term_char_t));
719 }
720
721 void
722 vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
723 {
724         int i, r, c, cs, ce;
725         term_pos_t s, e;
726
727         /* Swap according to window coordinates. */
728         if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
729             vb->vb_mark_start.tp_col) >
730             POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
731             vb->vb_mark_end.tp_col)) {
732                 POS_COPY(e, vb->vb_mark_start);
733                 POS_COPY(s, vb->vb_mark_end);
734         } else {
735                 POS_COPY(s, vb->vb_mark_start);
736                 POS_COPY(e, vb->vb_mark_end);
737         }
738
739         i = 0;
740         for (r = s.tp_row; r <= e.tp_row; r ++) {
741                 cs = (r == s.tp_row)?s.tp_col:0;
742                 ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
743                 for (c = cs; c < ce; c ++) {
744                         buf[i++] = vb->vb_rows[r][c];
745                 }
746                 /* Add new line for all rows, but not for last one. */
747                 if (r != e.tp_row) {
748                         buf[i++] = '\r';
749                         buf[i++] = '\n';
750                 }
751         }
752 }
753
754 int
755 vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
756 {
757         term_char_t *r;
758         int i;
759
760         switch (type) {
761         case VTB_MARK_END:      /* B1 UP */
762                 if (vb->vb_mark_last != VTB_MARK_MOVE)
763                         return (0);
764                 /* FALLTHROUGH */
765         case VTB_MARK_MOVE:
766         case VTB_MARK_EXTEND:
767                 vtbuf_flush_mark(vb); /* Clean old mark. */
768                 vb->vb_mark_end.tp_col = col;
769                 vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
770                 break;
771         case VTB_MARK_START:
772                 vtbuf_flush_mark(vb); /* Clean old mark. */
773                 vb->vb_mark_start.tp_col = col;
774                 vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
775                 /* Start again, so clear end point. */
776                 vb->vb_mark_end.tp_col = col;
777                 vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
778                 break;
779         case VTB_MARK_WORD:
780                 vtbuf_flush_mark(vb); /* Clean old mark. */
781                 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
782                     vtbuf_wth(vb, row);
783                 r = vb->vb_rows[vb->vb_mark_start.tp_row];
784                 for (i = col; i >= 0; i --) {
785                         if (TCHAR_CHARACTER(r[i]) == ' ') {
786                                 vb->vb_mark_start.tp_col = i + 1;
787                                 break;
788                         }
789                 }
790                 for (i = col; i < vb->vb_scr_size.tp_col; i ++) {
791                         if (TCHAR_CHARACTER(r[i]) == ' ') {
792                                 vb->vb_mark_end.tp_col = i;
793                                 break;
794                         }
795                 }
796                 if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
797                         vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
798                 break;
799         case VTB_MARK_ROW:
800                 vtbuf_flush_mark(vb); /* Clean old mark. */
801                 vb->vb_mark_start.tp_col = 0;
802                 vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
803                 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
804                     vtbuf_wth(vb, row);
805                 break;
806         case VTB_MARK_NONE:
807                 vb->vb_mark_last = type;
808                 /* FALLTHROUGH */
809         default:
810                 /* panic? */
811                 return (0);
812         }
813
814         vb->vb_mark_last = type;
815         /* Draw new marked region. */
816         vtbuf_flush_mark(vb);
817         return (1);
818 }
819 #endif
820
821 void
822 vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
823 {
824         int oflags, nflags;
825
826         VTBUF_LOCK(vb);
827         oflags = vb->vb_flags;
828         if (yes)
829                 vb->vb_flags |= VBF_CURSOR;
830         else
831                 vb->vb_flags &= ~VBF_CURSOR;
832         nflags = vb->vb_flags;
833
834         if (oflags != nflags)
835                 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
836         VTBUF_UNLOCK(vb);
837 }
838
839 void
840 vtbuf_scroll_mode(struct vt_buf *vb, int yes)
841 {
842         int oflags, nflags;
843
844         VTBUF_LOCK(vb);
845         oflags = vb->vb_flags;
846         if (yes)
847                 vb->vb_flags |= VBF_SCROLL;
848         else
849                 vb->vb_flags &= ~VBF_SCROLL;
850         nflags = vb->vb_flags;
851
852         if (oflags != nflags)
853                 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
854         VTBUF_UNLOCK(vb);
855 }
856