2 * Copyright (c) 2009, 2013 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/reboot.h>
42 #include <sys/systm.h>
44 #include <dev/vt/vt.h>
46 static MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
48 #define VTBUF_LOCK(vb) mtx_lock_spin(&(vb)->vb_lock)
49 #define VTBUF_UNLOCK(vb) mtx_unlock_spin(&(vb)->vb_lock)
51 #define POS_INDEX(c, r) (((r) << 12) + (c))
52 #define POS_COPY(d, s) do { \
53 (d).tp_col = (s).tp_col; \
54 (d).tp_row = (s).tp_row; \
57 #ifndef SC_NO_CUTPASTE
58 static int vtbuf_htw(const struct vt_buf *vb, int row);
59 static int vtbuf_wth(const struct vt_buf *vb, int row);
60 static int vtbuf_in_this_range(int begin, int test, int end, int sz);
65 * line5 <--- curroffset (terminal output to that line)
67 * line1 <--- roffset (history display from that point)
72 vthistory_seek(struct vt_buf *vb, int offset, int whence)
74 int diff, top, bottom, roffset;
76 /* No scrolling if not enabled. */
77 if ((vb->vb_flags & VBF_SCROLL) == 0) {
78 if (vb->vb_roffset != vb->vb_curroffset) {
79 vb->vb_roffset = vb->vb_curroffset;
82 return (0); /* No changes */
85 /* "top" may be a negative integer. */
86 bottom = vb->vb_curroffset;
87 top = (vb->vb_flags & VBF_HISTORY_FULL) ?
88 bottom + vb->vb_scr_size.tp_row - vb->vb_history_size :
91 roffset = 0; /* Make gcc happy. */
96 roffset = top + offset;
100 * Operate on copy of offset value, since it temporary
101 * can be bigger than amount of rows in buffer.
103 roffset = vb->vb_roffset;
104 if (roffset >= bottom + vb->vb_scr_size.tp_row)
105 roffset -= vb->vb_history_size;
108 roffset = MAX(roffset, top);
109 roffset = MIN(roffset, bottom);
112 roffset = vb->vb_history_size + roffset;
116 /* Go to current offset. */
117 roffset = vb->vb_curroffset;
121 diff = vb->vb_roffset != roffset;
122 vb->vb_roffset = roffset;
128 vthistory_addlines(struct vt_buf *vb, int offset)
130 #ifndef SC_NO_CUTPASTE
134 vb->vb_curroffset += offset;
135 if (vb->vb_curroffset < 0)
136 vb->vb_curroffset = 0;
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 if ((vb->vb_flags & VBF_SCROLL) == 0) {
141 vb->vb_roffset = vb->vb_curroffset;
144 #ifndef SC_NO_CUTPASTE
145 sz = vb->vb_history_size;
146 cur = vb->vb_roffset + vb->vb_scr_size.tp_row + sz - 1;
147 if (vtbuf_in_this_range(cur, vb->vb_mark_start.tp_row, cur + offset, sz) ||
148 vtbuf_in_this_range(cur, vb->vb_mark_end.tp_row, cur + offset, sz)) {
149 /* clear screen selection */
150 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row;
151 vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
157 vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
160 *offset = vb->vb_roffset;
163 #ifndef SC_NO_CUTPASTE /* Only mouse support use it now. */
164 /* Translate history row to current view row number. */
166 vtbuf_htw(const struct vt_buf *vb, int row)
171 * History offset roffset winrow
172 * 205 200 ((205 - 200 + 1000) % 1000) = 5
173 * 90 990 ((90 - 990 + 1000) % 1000) = 100
175 return ((row - vb->vb_roffset + vb->vb_history_size) %
176 vb->vb_history_size);
179 /* Translate current view row number to history row. */
181 vtbuf_wth(const struct vt_buf *vb, int row)
184 return ((vb->vb_roffset + row) % vb->vb_history_size);
188 * Test if an index in a circular buffer is within a range.
190 * begin - start index
193 * sz - size of circular buffer when it turns over
196 vtbuf_in_this_range(int begin, int test, int end, int sz)
202 /* check for inversion */
204 return (test >= begin || test < end);
206 return (test >= begin && test < end);
211 vtbuf_iscursor(const struct vt_buf *vb, int row, int col)
213 #ifndef SC_NO_CUTPASTE
214 int sc, sr, sz, ec, er, tmp;
217 if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
218 (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
221 #ifndef SC_NO_CUTPASTE
222 /* Mark cut/paste region. */
223 if (vb->vb_mark_start.tp_col == vb->vb_mark_end.tp_col &&
224 vb->vb_mark_start.tp_row == vb->vb_mark_end.tp_row)
227 sc = vb->vb_mark_start.tp_col;
228 sr = vb->vb_mark_start.tp_row;
229 ec = vb->vb_mark_end.tp_col;
230 er = vb->vb_mark_end.tp_row;
233 * Information about if the selection was made bottom-top or
234 * top-bottom is lost due to modulo arithmetics and needs to
237 sz = vb->vb_history_size;
238 tmp = (sz + er - sr) % sz;
239 row = vtbuf_wth(vb, row);
241 /* Swap start and end if start > end */
242 if ((2 * tmp) > sz || (tmp == 0 && sc > ec)) {
243 tmp = sc; sc = ec; ec = tmp;
244 tmp = sr; sr = er; er = tmp;
247 if (vtbuf_in_this_range(POS_INDEX(sc, sr), POS_INDEX(col, row),
248 POS_INDEX(ec, er), POS_INDEX(0, sz)))
256 vtbuf_dirty_locked(struct vt_buf *vb, const term_rect_t *area)
259 if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
260 vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
261 if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
262 vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
263 if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
264 vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
265 if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
266 vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
270 vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
274 vtbuf_dirty_locked(vb, area);
279 vtbuf_dirty_cell_locked(struct vt_buf *vb, const term_pos_t *p)
284 area.tr_end.tp_row = p->tp_row + 1;
285 area.tr_end.tp_col = p->tp_col + 1;
286 vtbuf_dirty_locked(vb, &area);
290 vtbuf_make_undirty(struct vt_buf *vb)
293 vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
294 vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
298 vtbuf_undirty(struct vt_buf *vb, term_rect_t *r)
302 *r = vb->vb_dirtyrect;
303 vtbuf_make_undirty(vb);
308 vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
310 const term_pos_t *p1 = &r->tr_begin;
312 unsigned int rows, cols;
315 KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
316 ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
317 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
318 KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
319 ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
320 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
322 KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
323 ("vtbuf_copy end.tp_row %d must be less than screen width %d",
324 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
325 KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
326 ("vtbuf_copy end.tp_col %d must be less than screen height %d",
327 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
329 KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
330 ("vtbuf_copy tp_row %d must be less than screen width %d",
331 p2->tp_row, vb->vb_scr_size.tp_row));
332 KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
333 ("vtbuf_copy tp_col %d must be less than screen height %d",
334 p2->tp_col, vb->vb_scr_size.tp_col));
336 rows = r->tr_end.tp_row - r->tr_begin.tp_row;
337 rdiff = r->tr_begin.tp_row - p2->tp_row;
338 cols = r->tr_end.tp_col - r->tr_begin.tp_col;
339 if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
340 r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
341 (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
342 rdiff > 0) { /* Only forward direction. Do not eat history. */
343 vthistory_addlines(vb, rdiff);
344 } else if (p2->tp_row < p1->tp_row) {
345 /* Handle overlapping copies of line segments. */
347 for (pr = 0; pr < rows; pr++)
349 &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
350 &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
351 cols * sizeof(term_char_t));
353 /* Move data down. */
354 for (pr = rows - 1; pr >= 0; pr--)
356 &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
357 &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
358 cols * sizeof(term_char_t));
362 area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
363 area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
364 vtbuf_dirty(vb, &area);
368 vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
373 for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
374 row = vb->vb_rows[(vb->vb_curroffset + pr) %
375 VTBUF_MAX_HEIGHT(vb)];
376 for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
383 vtbuf_fill_locked(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
385 KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
386 ("vtbuf_fill_locked begin.tp_row %d must be < screen height %d",
387 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
388 KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
389 ("vtbuf_fill_locked begin.tp_col %d must be < screen width %d",
390 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
392 KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
393 ("vtbuf_fill_locked end.tp_row %d must be <= screen height %d",
394 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
395 KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
396 ("vtbuf_fill_locked end.tp_col %d must be <= screen width %d",
397 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
400 vtbuf_fill(vb, r, c);
401 vtbuf_dirty_locked(vb, r);
406 vtbuf_init_rows(struct vt_buf *vb)
410 vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
412 for (r = 0; r < vb->vb_history_size; r++)
413 vb->vb_rows[r] = &vb->vb_buffer[r * vb->vb_scr_size.tp_col];
417 vtbuf_init_early(struct vt_buf *vb)
421 vb->vb_flags |= VBF_CURSOR;
423 vb->vb_curroffset = 0;
424 vb->vb_mark_start.tp_row = 0;
425 vb->vb_mark_start.tp_col = 0;
426 vb->vb_mark_end.tp_row = 0;
427 vb->vb_mark_end.tp_col = 0;
430 rect.tr_begin.tp_row = rect.tr_begin.tp_col = 0;
431 rect.tr_end.tp_col = vb->vb_scr_size.tp_col;
432 rect.tr_end.tp_row = vb->vb_history_size;
433 vtbuf_fill(vb, &rect, VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR));
434 vtbuf_make_undirty(vb);
435 if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
436 mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
437 vb->vb_flags |= VBF_MTX_INIT;
442 vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
446 vb->vb_scr_size = *p;
447 vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
449 if ((vb->vb_flags & VBF_STATIC) == 0) {
450 sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
451 vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
453 sz = vb->vb_history_size * sizeof(term_char_t *);
454 vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
457 vtbuf_init_early(vb);
461 vtbuf_sethistory_size(struct vt_buf *vb, int size)
466 p.tp_row = vb->vb_scr_size.tp_row;
467 p.tp_col = vb->vb_scr_size.tp_col;
468 vtbuf_grow(vb, &p, size);
472 vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, unsigned int history_size)
474 term_char_t *old, *new, **rows, **oldrows, **copyrows, *row, *oldrow;
475 int bufsize, rowssize, w, h, c, r, history_was_full;
476 unsigned int old_history_size;
479 history_size = MAX(history_size, p->tp_row);
481 /* Allocate new buffer. */
482 bufsize = history_size * p->tp_col * sizeof(term_char_t);
483 new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
484 rowssize = history_size * sizeof(term_pos_t *);
485 rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
489 old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
490 oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
491 copyrows = vb->vb_rows;
493 w = vb->vb_scr_size.tp_col;
494 h = vb->vb_scr_size.tp_row;
495 old_history_size = vb->vb_history_size;
496 history_was_full = vb->vb_flags & VBF_HISTORY_FULL;
498 vb->vb_history_size = history_size;
501 vb->vb_flags &= ~VBF_STATIC;
502 vb->vb_scr_size = *p;
505 /* Copy history and fill extra space if needed. */
506 if (history_size > old_history_size) {
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.
511 * The rest of the new buffer is initialized with blank
514 for (r = 0; r < old_history_size; r ++) {
517 /* Compute the corresponding row in the old buffer. */
518 if (history_was_full)
520 * The buffer is full, the "top" row is
521 * the one just after the viewable area
522 * (curroffset + viewable height) in the
523 * cycling buffer. The corresponding row
524 * is computed from this top row.
527 (vb->vb_curroffset + h + r) %
531 * The buffer is not full, therefore,
532 * we didn't cycle already. The
533 * corresponding rows are the same in
536 oldrow = copyrows[r];
539 MIN(p->tp_col, w) * sizeof(term_char_t));
542 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
543 * extended lines of kernel text using the wrong
546 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
547 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
551 /* Fill remaining rows. */
552 rect.tr_begin.tp_col = 0;
553 rect.tr_begin.tp_row = old_history_size;
554 rect.tr_end.tp_col = p->tp_col;
555 rect.tr_end.tp_row = p->tp_row;
556 vtbuf_fill(vb, &rect, VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR));
558 vb->vb_flags &= ~VBF_HISTORY_FULL;
561 * Copy rows to the new buffer. The first row in the history
562 * is back to index 0, ie. the new buffer doesn't cycle.
564 * (old_history_size - history_size) lines of history are
567 for (r = 0; r < history_size; r ++) {
571 * Compute the corresponding row in the old buffer.
573 * See the equivalent if{} block above for an
576 if (history_was_full)
578 (vb->vb_curroffset + h + r +
579 (old_history_size - history_size)) %
583 (r + (old_history_size - history_size)) %
587 MIN(p->tp_col, w) * sizeof(term_char_t));
590 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
591 * extended lines of kernel text using the wrong
594 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
595 row[c] = VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR);
599 if (!history_was_full &&
600 (vb->vb_curroffset + h) >= history_size)
601 vb->vb_flags |= VBF_HISTORY_FULL;
605 * If the screen is already filled (there are non-visible lines
606 * above the current viewable area), adjust curroffset to the
609 if (!history_was_full && vb->vb_curroffset > 0) {
610 vb->vb_curroffset = vb->vb_curroffset + h - p->tp_row;
611 if (vb->vb_curroffset < 0)
612 vb->vb_curroffset += vb->vb_history_size;
613 vb->vb_curroffset %= vb->vb_history_size;
614 vb->vb_roffset = vb->vb_curroffset;
617 /* Adjust cursor position. */
618 if (vb->vb_cursor.tp_col > p->tp_col - 1)
620 * Move cursor to the last column, in case its previous
621 * position is outside of the new screen area.
623 vb->vb_cursor.tp_col = p->tp_col - 1;
625 if (vb->vb_curroffset > 0 || vb->vb_cursor.tp_row > p->tp_row - 1)
626 /* Move cursor to the last line on the screen. */
627 vb->vb_cursor.tp_row = p->tp_row - 1;
629 vtbuf_make_undirty(vb);
632 /* Deallocate old buffer. */
634 free(oldrows, M_VTBUF);
638 vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
642 KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
643 ("vtbuf_putchar tp_row %d must be less than screen width %d",
644 p->tp_row, vb->vb_scr_size.tp_row));
645 KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
646 ("vtbuf_putchar tp_col %d must be less than screen height %d",
647 p->tp_col, vb->vb_scr_size.tp_col));
649 row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
650 VTBUF_MAX_HEIGHT(vb)];
651 if (row[p->tp_col] != c) {
654 vtbuf_dirty_cell_locked(vb, p);
660 vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
663 if (vb->vb_flags & VBF_CURSOR) {
665 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
667 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
674 #ifndef SC_NO_CUTPASTE
676 vtbuf_flush_mark(struct vt_buf *vb)
681 /* Notify renderer to update marked region. */
682 if ((vb->vb_mark_start.tp_col != vb->vb_mark_end.tp_col) ||
683 (vb->vb_mark_start.tp_row != vb->vb_mark_end.tp_row)) {
685 s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
686 e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
688 area.tr_begin.tp_col = 0;
689 area.tr_begin.tp_row = MIN(s, e);
691 area.tr_end.tp_col = vb->vb_scr_size.tp_col;
692 area.tr_end.tp_row = MAX(s, e) + 1;
694 vtbuf_dirty(vb, &area);
699 vtbuf_get_marked_len(struct vt_buf *vb)
704 /* Swap according to window coordinates. */
705 if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
706 vb->vb_mark_start.tp_col) >
707 POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
708 vb->vb_mark_end.tp_col)) {
709 POS_COPY(e, vb->vb_mark_start);
710 POS_COPY(s, vb->vb_mark_end);
712 POS_COPY(s, vb->vb_mark_start);
713 POS_COPY(e, vb->vb_mark_end);
716 si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
717 ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
719 /* Number symbols and number of rows to inject \n */
720 sz = ei - si + ((e.tp_row - s.tp_row) * 2);
722 return (sz * sizeof(term_char_t));
726 vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
731 /* Swap according to window coordinates. */
732 if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
733 vb->vb_mark_start.tp_col) >
734 POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
735 vb->vb_mark_end.tp_col)) {
736 POS_COPY(e, vb->vb_mark_start);
737 POS_COPY(s, vb->vb_mark_end);
739 POS_COPY(s, vb->vb_mark_start);
740 POS_COPY(e, vb->vb_mark_end);
744 for (r = s.tp_row; r <= e.tp_row; r ++) {
745 cs = (r == s.tp_row)?s.tp_col:0;
746 ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
747 for (c = cs; c < ce; c ++) {
748 buf[i++] = vb->vb_rows[r][c];
750 /* Add new line for all rows, but not for last one. */
759 vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
765 case VTB_MARK_END: /* B1 UP */
766 if (vb->vb_mark_last != VTB_MARK_MOVE)
770 case VTB_MARK_EXTEND:
771 vtbuf_flush_mark(vb); /* Clean old mark. */
772 vb->vb_mark_end.tp_col = col;
773 vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
776 vtbuf_flush_mark(vb); /* Clean old mark. */
777 vb->vb_mark_start.tp_col = col;
778 vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
779 /* Start again, so clear end point. */
780 vb->vb_mark_end.tp_col = col;
781 vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
784 vtbuf_flush_mark(vb); /* Clean old mark. */
785 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
787 r = vb->vb_rows[vb->vb_mark_start.tp_row];
788 for (i = col; i >= 0; i --) {
789 if (TCHAR_CHARACTER(r[i]) == ' ') {
790 vb->vb_mark_start.tp_col = i + 1;
794 for (i = col; i < vb->vb_scr_size.tp_col; i ++) {
795 if (TCHAR_CHARACTER(r[i]) == ' ') {
796 vb->vb_mark_end.tp_col = i;
800 if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
801 vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
804 vtbuf_flush_mark(vb); /* Clean old mark. */
805 vb->vb_mark_start.tp_col = 0;
806 vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
807 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
811 vb->vb_mark_last = type;
818 vb->vb_mark_last = type;
819 /* Draw new marked region. */
820 vtbuf_flush_mark(vb);
826 vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
831 oflags = vb->vb_flags;
833 vb->vb_flags |= VBF_CURSOR;
835 vb->vb_flags &= ~VBF_CURSOR;
836 nflags = vb->vb_flags;
838 if (oflags != nflags)
839 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);
844 vtbuf_scroll_mode(struct vt_buf *vb, int yes)
849 oflags = vb->vb_flags;
851 vb->vb_flags |= VBF_SCROLL;
853 vb->vb_flags &= ~VBF_SCROLL;
854 nflags = vb->vb_flags;
856 if (oflags != nflags)
857 vtbuf_dirty_cell_locked(vb, &vb->vb_cursor);