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