2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, 2013 The FreeBSD Foundation
7 * This software was developed by Ed Schouten under sponsorship from the
10 * Portions of this software were developed by Oleksandr Rybalko
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/reboot.h>
44 #include <sys/systm.h>
46 #include <dev/vt/vt.h>
48 static MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
50 #define VTBUF_LOCK(vb) mtx_lock_spin(&(vb)->vb_lock)
51 #define VTBUF_UNLOCK(vb) mtx_unlock_spin(&(vb)->vb_lock)
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; \
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);
67 * line5 <--- curroffset (terminal output to that line)
69 * line1 <--- roffset (history display from that point)
74 vthistory_seek(struct vt_buf *vb, int offset, int whence)
76 int diff, top, bottom, roffset;
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;
84 return (0); /* No changes */
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 :
93 roffset = 0; /* Make gcc happy. */
98 roffset = top + offset;
102 * Operate on copy of offset value, since it temporary
103 * can be bigger than amount of rows in buffer.
105 roffset = vb->vb_roffset;
106 if (roffset >= bottom + vb->vb_scr_size.tp_row)
107 roffset -= vb->vb_history_size;
110 roffset = MAX(roffset, top);
111 roffset = MIN(roffset, bottom);
114 roffset = vb->vb_history_size + roffset;
118 /* Go to current offset. */
119 roffset = vb->vb_curroffset;
123 diff = vb->vb_roffset != roffset;
124 vb->vb_roffset = roffset;
130 vthistory_addlines(struct vt_buf *vb, int offset)
132 #ifndef SC_NO_CUTPASTE
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;
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;
159 vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
162 *offset = vb->vb_roffset;
165 #ifndef SC_NO_CUTPASTE /* Only mouse support use it now. */
166 /* Translate history row to current view row number. */
168 vtbuf_htw(const struct vt_buf *vb, int row)
173 * History offset roffset winrow
174 * 205 200 ((205 - 200 + 1000) % 1000) = 5
175 * 90 990 ((90 - 990 + 1000) % 1000) = 100
177 return ((row - vb->vb_roffset + vb->vb_history_size) %
178 vb->vb_history_size);
181 /* Translate current view row number to history row. */
183 vtbuf_wth(const struct vt_buf *vb, int row)
186 return ((vb->vb_roffset + row) % vb->vb_history_size);
190 * Test if an index in a circular buffer is within a range.
192 * begin - start index
195 * sz - size of circular buffer when it turns over
198 vtbuf_in_this_range(int begin, int test, int end, int sz)
204 /* check for inversion */
206 return (test >= begin || test < end);
208 return (test >= begin && test < end);
213 vtbuf_iscursor(const struct vt_buf *vb, int row, int col)
215 #ifndef SC_NO_CUTPASTE
216 int sc, sr, sz, ec, er, tmp;
219 if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
220 (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
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)
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;
235 * Information about if the selection was made bottom-top or
236 * top-bottom is lost due to modulo arithmetics and needs to
239 sz = vb->vb_history_size;
240 tmp = (sz + er - sr) % sz;
241 row = vtbuf_wth(vb, row);
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;
249 if (vtbuf_in_this_range(POS_INDEX(sc, sr), POS_INDEX(col, row),
250 POS_INDEX(ec, er), POS_INDEX(0, sz)))
258 vtbuf_dirty_locked(struct vt_buf *vb, const term_rect_t *area)
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;
272 vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
276 vtbuf_dirty_locked(vb, area);
281 vtbuf_dirty_cell_locked(struct vt_buf *vb, const term_pos_t *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);
292 vtbuf_make_undirty(struct vt_buf *vb)
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;
300 vtbuf_undirty(struct vt_buf *vb, term_rect_t *r)
304 *r = vb->vb_dirtyrect;
305 vtbuf_make_undirty(vb);
310 vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
312 const term_pos_t *p1 = &r->tr_begin;
314 unsigned int rows, cols;
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));
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));
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));
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. */
349 for (pr = 0; pr < rows; pr++)
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));
355 /* Move data down. */
356 for (pr = rows - 1; pr >= 0; pr--)
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));
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);
370 vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
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++) {
385 vtbuf_fill_locked(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
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));
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));
402 vtbuf_fill(vb, r, c);
403 vtbuf_dirty_locked(vb, r);
408 vtbuf_init_rows(struct vt_buf *vb)
412 vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
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];
419 vtbuf_init_early(struct vt_buf *vb)
423 vb->vb_flags |= VBF_CURSOR;
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;
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;
444 vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
448 vb->vb_scr_size = *p;
449 vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
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);
455 sz = vb->vb_history_size * sizeof(term_char_t *);
456 vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
459 vtbuf_init_early(vb);
463 vtbuf_sethistory_size(struct vt_buf *vb, int 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);
474 vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, unsigned int history_size)
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;
481 history_size = MAX(history_size, p->tp_row);
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);
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;
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;
500 vb->vb_history_size = history_size;
503 vb->vb_flags &= ~VBF_STATIC;
504 vb->vb_scr_size = *p;
507 /* Copy history and fill extra space if needed. */
508 if (history_size > old_history_size) {
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.
513 * The rest of the new buffer is initialized with blank
516 for (r = 0; r < old_history_size; r ++) {
519 /* Compute the corresponding row in the old buffer. */
520 if (history_was_full)
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.
529 (vb->vb_curroffset + h + r) %
533 * The buffer is not full, therefore,
534 * we didn't cycle already. The
535 * corresponding rows are the same in
538 oldrow = copyrows[r];
541 MIN(p->tp_col, w) * sizeof(term_char_t));
544 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
545 * extended lines of kernel text using the wrong
548 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
549 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
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));
560 vb->vb_flags &= ~VBF_HISTORY_FULL;
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.
566 * (old_history_size - history_size) lines of history are
569 for (r = 0; r < history_size; r ++) {
573 * Compute the corresponding row in the old buffer.
575 * See the equivalent if{} block above for an
578 if (history_was_full)
580 (vb->vb_curroffset + h + r +
581 (old_history_size - history_size)) %
585 (r + (old_history_size - history_size)) %
589 MIN(p->tp_col, w) * sizeof(term_char_t));
592 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
593 * extended lines of kernel text using the wrong
596 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
597 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
601 if (!history_was_full &&
602 (vb->vb_curroffset + h) >= history_size)
603 vb->vb_flags |= VBF_HISTORY_FULL;
607 * If the screen is already filled (there are non-visible lines
608 * above the current viewable area), adjust curroffset to the
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;
619 /* Adjust cursor position. */
620 if (vb->vb_cursor.tp_col > p->tp_col - 1)
622 * Move cursor to the last column, in case its previous
623 * position is outside of the new screen area.
625 vb->vb_cursor.tp_col = p->tp_col - 1;
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;
631 vtbuf_make_undirty(vb);
634 /* Deallocate old buffer. */
636 free(oldrows, M_VTBUF);
640 vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
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));
651 row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
652 VTBUF_MAX_HEIGHT(vb)];
653 if (row[p->tp_col] != c) {
656 vtbuf_dirty_cell_locked(vb, p);
662 vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
665 if (vb->vb_flags & VBF_CURSOR) {
667 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
669 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
676 #ifndef SC_NO_CUTPASTE
678 vtbuf_flush_mark(struct vt_buf *vb)
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)) {
687 s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
688 e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
690 area.tr_begin.tp_col = 0;
691 area.tr_begin.tp_row = MIN(s, e);
693 area.tr_end.tp_col = vb->vb_scr_size.tp_col;
694 area.tr_end.tp_row = MAX(s, e) + 1;
696 vtbuf_dirty(vb, &area);
701 vtbuf_get_marked_len(struct vt_buf *vb)
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);
714 POS_COPY(s, vb->vb_mark_start);
715 POS_COPY(e, vb->vb_mark_end);
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;
721 /* Number symbols and number of rows to inject \n */
722 sz = ei - si + ((e.tp_row - s.tp_row) * 2);
724 return (sz * sizeof(term_char_t));
728 vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
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);
741 POS_COPY(s, vb->vb_mark_start);
742 POS_COPY(e, vb->vb_mark_end);
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];
752 /* Add new line for all rows, but not for last one. */
761 vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
767 case VTB_MARK_END: /* B1 UP */
768 if (vb->vb_mark_last != 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);
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);
786 vtbuf_flush_mark(vb); /* Clean old mark. */
787 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_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;
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;
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;
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 =
813 vb->vb_mark_last = type;
820 vb->vb_mark_last = type;
821 /* Draw new marked region. */
822 vtbuf_flush_mark(vb);
828 vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
833 oflags = vb->vb_flags;
835 vb->vb_flags |= VBF_CURSOR;
837 vb->vb_flags &= ~VBF_CURSOR;
838 nflags = vb->vb_flags;
840 if (oflags != nflags)
841 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
846 vtbuf_scroll_mode(struct vt_buf *vb, int yes)
851 oflags = vb->vb_flags;
853 vb->vb_flags |= VBF_SCROLL;
855 vb->vb_flags &= ~VBF_SCROLL;
856 nflags = vb->vb_flags;
858 if (oflags != nflags)
859 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);