4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
26 static struct screen_write_citem
*screen_write_collect_trim(
27 struct screen_write_ctx
*, u_int
, u_int
, u_int
, int *);
28 static void screen_write_collect_clear(struct screen_write_ctx
*, u_int
,
30 static void screen_write_collect_scroll(struct screen_write_ctx
*, u_int
);
31 static void screen_write_collect_flush(struct screen_write_ctx
*, int,
33 static int screen_write_overwrite(struct screen_write_ctx
*,
34 struct grid_cell
*, u_int
);
35 static const struct grid_cell
*screen_write_combine(struct screen_write_ctx
*,
36 const struct utf8_data
*, u_int
*, u_int
*);
38 struct screen_write_citem
{
42 enum { TEXT
, CLEAR
} type
;
48 TAILQ_ENTRY(screen_write_citem
) entry
;
50 struct screen_write_cline
{
52 TAILQ_HEAD(, screen_write_citem
) items
;
54 TAILQ_HEAD(, screen_write_citem
) screen_write_citem_freelist
=
55 TAILQ_HEAD_INITIALIZER(screen_write_citem_freelist
);
57 static struct screen_write_citem
*
58 screen_write_get_citem(void)
60 struct screen_write_citem
*ci
;
62 ci
= TAILQ_FIRST(&screen_write_citem_freelist
);
64 TAILQ_REMOVE(&screen_write_citem_freelist
, ci
, entry
);
65 memset(ci
, 0, sizeof *ci
);
68 return (xcalloc(1, sizeof *ci
));
72 screen_write_free_citem(struct screen_write_citem
*ci
)
74 TAILQ_INSERT_TAIL(&screen_write_citem_freelist
, ci
, entry
);
78 screen_write_offset_timer(__unused
int fd
, __unused
short events
, void *data
)
80 struct window
*w
= data
;
82 tty_update_window_offset(w
);
85 /* Set cursor position. */
87 screen_write_set_cursor(struct screen_write_ctx
*ctx
, int cx
, int cy
)
89 struct window_pane
*wp
= ctx
->wp
;
91 struct screen
*s
= ctx
->s
;
92 struct timeval tv
= { .tv_usec
= 10000 };
94 if (cx
!= -1 && (u_int
)cx
== s
->cx
&& cy
!= -1 && (u_int
)cy
== s
->cy
)
98 if ((u_int
)cx
> screen_size_x(s
)) /* allow last column */
99 cx
= screen_size_x(s
) - 1;
103 if ((u_int
)cy
> screen_size_y(s
) - 1)
104 cy
= screen_size_y(s
) - 1;
112 if (!event_initialized(&w
->offset_timer
))
113 evtimer_set(&w
->offset_timer
, screen_write_offset_timer
, w
);
114 if (!evtimer_pending(&w
->offset_timer
, NULL
))
115 evtimer_add(&w
->offset_timer
, &tv
);
118 /* Do a full redraw. */
120 screen_write_redraw_cb(const struct tty_ctx
*ttyctx
)
122 struct window_pane
*wp
= ttyctx
->arg
;
125 wp
->flags
|= PANE_REDRAW
;
128 /* Update context for client. */
130 screen_write_set_client_cb(struct tty_ctx
*ttyctx
, struct client
*c
)
132 struct window_pane
*wp
= ttyctx
->arg
;
134 if (ttyctx
->allow_invisible_panes
) {
135 if (session_has(c
->session
, wp
->window
))
140 if (c
->session
->curw
->window
!= wp
->window
)
142 if (wp
->layout_cell
== NULL
)
145 if (wp
->flags
& (PANE_REDRAW
|PANE_DROP
))
147 if (c
->flags
& CLIENT_REDRAWPANES
) {
149 * Redraw is already deferred to redraw another pane - redraw
150 * this one also when that happens.
152 log_debug("%s: adding %%%u to deferred redraw", __func__
,
154 wp
->flags
|= PANE_REDRAW
;
158 ttyctx
->bigger
= tty_window_offset(&c
->tty
, &ttyctx
->wox
, &ttyctx
->woy
,
159 &ttyctx
->wsx
, &ttyctx
->wsy
);
161 ttyctx
->xoff
= ttyctx
->rxoff
= wp
->xoff
;
162 ttyctx
->yoff
= ttyctx
->ryoff
= wp
->yoff
;
164 if (status_at_line(c
) == 0)
165 ttyctx
->yoff
+= status_line_size(c
);
170 /* Set up context for TTY command. */
172 screen_write_initctx(struct screen_write_ctx
*ctx
, struct tty_ctx
*ttyctx
,
175 struct screen
*s
= ctx
->s
;
177 memset(ttyctx
, 0, sizeof *ttyctx
);
180 ttyctx
->sx
= screen_size_x(s
);
181 ttyctx
->sy
= screen_size_y(s
);
185 ttyctx
->orlower
= s
->rlower
;
186 ttyctx
->orupper
= s
->rupper
;
188 memcpy(&ttyctx
->defaults
, &grid_default_cell
, sizeof ttyctx
->defaults
);
189 if (ctx
->init_ctx_cb
!= NULL
) {
190 ctx
->init_ctx_cb(ctx
, ttyctx
);
191 if (ttyctx
->palette
!= NULL
) {
192 if (ttyctx
->defaults
.fg
== 8)
193 ttyctx
->defaults
.fg
= ttyctx
->palette
->fg
;
194 if (ttyctx
->defaults
.bg
== 8)
195 ttyctx
->defaults
.bg
= ttyctx
->palette
->bg
;
198 ttyctx
->redraw_cb
= screen_write_redraw_cb
;
199 if (ctx
->wp
!= NULL
) {
200 tty_default_colours(&ttyctx
->defaults
, ctx
->wp
);
201 ttyctx
->palette
= &ctx
->wp
->palette
;
202 ttyctx
->set_client_cb
= screen_write_set_client_cb
;
203 ttyctx
->arg
= ctx
->wp
;
207 if (~ctx
->flags
& SCREEN_WRITE_SYNC
) {
209 * For the active pane or for an overlay (no pane), we want to
210 * only use synchronized updates if requested (commands that
211 * move the cursor); for other panes, always use it, since the
212 * cursor will have to move.
214 if (ctx
->wp
!= NULL
) {
215 if (ctx
->wp
!= ctx
->wp
->window
->active
)
220 ttyctx
->num
= 0x10|sync
;
221 tty_write(tty_cmd_syncstart
, ttyctx
);
222 ctx
->flags
|= SCREEN_WRITE_SYNC
;
226 /* Make write list. */
228 screen_write_make_list(struct screen
*s
)
232 s
->write_list
= xcalloc(screen_size_y(s
), sizeof *s
->write_list
);
233 for (y
= 0; y
< screen_size_y(s
); y
++)
234 TAILQ_INIT(&s
->write_list
[y
].items
);
237 /* Free write list. */
239 screen_write_free_list(struct screen
*s
)
243 for (y
= 0; y
< screen_size_y(s
); y
++)
244 free(s
->write_list
[y
].data
);
248 /* Set up for writing. */
250 screen_write_init(struct screen_write_ctx
*ctx
, struct screen
*s
)
252 memset(ctx
, 0, sizeof *ctx
);
256 if (ctx
->s
->write_list
== NULL
)
257 screen_write_make_list(ctx
->s
);
258 ctx
->item
= screen_write_get_citem();
264 /* Initialize writing with a pane. */
266 screen_write_start_pane(struct screen_write_ctx
*ctx
, struct window_pane
*wp
,
271 screen_write_init(ctx
, s
);
274 if (log_get_level() != 0) {
275 log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
276 __func__
, screen_size_x(ctx
->s
), screen_size_y(ctx
->s
),
277 wp
->id
, wp
->xoff
, wp
->yoff
);
281 /* Initialize writing with a callback. */
283 screen_write_start_callback(struct screen_write_ctx
*ctx
, struct screen
*s
,
284 screen_write_init_ctx_cb cb
, void *arg
)
286 screen_write_init(ctx
, s
);
288 ctx
->init_ctx_cb
= cb
;
291 if (log_get_level() != 0) {
292 log_debug("%s: size %ux%u, with callback", __func__
,
293 screen_size_x(ctx
->s
), screen_size_y(ctx
->s
));
297 /* Initialize writing. */
299 screen_write_start(struct screen_write_ctx
*ctx
, struct screen
*s
)
301 screen_write_init(ctx
, s
);
303 if (log_get_level() != 0) {
304 log_debug("%s: size %ux%u, no pane", __func__
,
305 screen_size_x(ctx
->s
), screen_size_y(ctx
->s
));
309 /* Finish writing. */
311 screen_write_stop(struct screen_write_ctx
*ctx
)
313 screen_write_collect_end(ctx
);
314 screen_write_collect_flush(ctx
, 0, __func__
);
316 screen_write_free_citem(ctx
->item
);
319 /* Reset screen state. */
321 screen_write_reset(struct screen_write_ctx
*ctx
)
323 struct screen
*s
= ctx
->s
;
325 screen_reset_tabs(s
);
326 screen_write_scrollregion(ctx
, 0, screen_size_y(s
) - 1);
328 s
->mode
= MODE_CURSOR
|MODE_WRAP
;
329 if (options_get_number(global_options
, "extended-keys") == 2)
330 s
->mode
|= MODE_KEXTENDED
;
332 screen_write_clearscreen(ctx
, 8);
333 screen_write_set_cursor(ctx
, 0, 0);
336 /* Write character. */
338 screen_write_putc(struct screen_write_ctx
*ctx
, const struct grid_cell
*gcp
,
343 memcpy(&gc
, gcp
, sizeof gc
);
345 utf8_set(&gc
.data
, ch
);
346 screen_write_cell(ctx
, &gc
);
349 /* Calculate string length. */
351 screen_write_strlen(const char *fmt
, ...)
357 size_t left
, size
= 0;
358 enum utf8_state more
;
361 xvasprintf(&msg
, fmt
, ap
);
365 while (*ptr
!= '\0') {
366 if (*ptr
> 0x7f && utf8_open(&ud
, *ptr
) == UTF8_MORE
) {
370 if (left
< (size_t)ud
.size
- 1)
372 while ((more
= utf8_append(&ud
, *ptr
)) == UTF8_MORE
)
376 if (more
== UTF8_DONE
)
379 if (*ptr
> 0x1f && *ptr
< 0x7f)
389 /* Write string wrapped over lines. */
391 screen_write_text(struct screen_write_ctx
*ctx
, u_int cx
, u_int width
,
392 u_int lines
, int more
, const struct grid_cell
*gcp
, const char *fmt
, ...)
394 struct screen
*s
= ctx
->s
;
397 u_int cy
= s
->cy
, i
, end
, next
, idx
= 0, at
, left
;
398 struct utf8_data
*text
;
401 memcpy(&gc
, gcp
, sizeof gc
);
404 xvasprintf(&tmp
, fmt
, ap
);
407 text
= utf8_fromcstr(tmp
);
410 left
= (cx
+ width
) - s
->cx
;
412 /* Find the end of what can fit on the line. */
414 for (end
= idx
; text
[end
].size
!= 0; end
++) {
415 if (text
[end
].size
== 1 && text
[end
].data
[0] == '\n')
417 if (at
+ text
[end
].width
> left
)
419 at
+= text
[end
].width
;
423 * If we're on a space, that's the end. If not, walk back to
426 if (text
[end
].size
== 0)
428 else if (text
[end
].size
== 1 && text
[end
].data
[0] == '\n')
430 else if (text
[end
].size
== 1 && text
[end
].data
[0] == ' ')
433 for (i
= end
; i
> idx
; i
--) {
434 if (text
[i
].size
== 1 && text
[i
].data
[0] == ' ')
444 /* Print the line. */
445 for (i
= idx
; i
< end
; i
++) {
446 utf8_copy(&gc
.data
, &text
[i
]);
447 screen_write_cell(ctx
, &gc
);
450 /* If at the bottom, stop. */
452 if (s
->cy
== cy
+ lines
- 1 || text
[idx
].size
== 0)
455 screen_write_cursormove(ctx
, cx
, s
->cy
+ 1, 0);
460 * Fail if on the last line and there is more to come or at the end, or
461 * if the text was not entirely consumed.
463 if ((s
->cy
== cy
+ lines
- 1 && (!more
|| s
->cx
== cx
+ width
)) ||
464 text
[idx
].size
!= 0) {
471 * If no more to come, move to the next line. Otherwise, leave on
472 * the same line (except if at the end).
474 if (!more
|| s
->cx
== cx
+ width
)
475 screen_write_cursormove(ctx
, cx
, s
->cy
+ 1, 0);
479 /* Write simple string (no maximum length). */
481 screen_write_puts(struct screen_write_ctx
*ctx
, const struct grid_cell
*gcp
,
482 const char *fmt
, ...)
487 screen_write_vnputs(ctx
, -1, gcp
, fmt
, ap
);
491 /* Write string with length limit (-1 for unlimited). */
493 screen_write_nputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
494 const struct grid_cell
*gcp
, const char *fmt
, ...)
499 screen_write_vnputs(ctx
, maxlen
, gcp
, fmt
, ap
);
504 screen_write_vnputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
505 const struct grid_cell
*gcp
, const char *fmt
, va_list ap
)
508 struct utf8_data
*ud
= &gc
.data
;
511 size_t left
, size
= 0;
512 enum utf8_state more
;
514 memcpy(&gc
, gcp
, sizeof gc
);
515 xvasprintf(&msg
, fmt
, ap
);
518 while (*ptr
!= '\0') {
519 if (*ptr
> 0x7f && utf8_open(ud
, *ptr
) == UTF8_MORE
) {
523 if (left
< (size_t)ud
->size
- 1)
525 while ((more
= utf8_append(ud
, *ptr
)) == UTF8_MORE
)
529 if (more
!= UTF8_DONE
)
531 if (maxlen
> 0 && size
+ ud
->width
> (size_t)maxlen
) {
532 while (size
< (size_t)maxlen
) {
533 screen_write_putc(ctx
, &gc
, ' ');
539 screen_write_cell(ctx
, &gc
);
541 if (maxlen
> 0 && size
+ 1 > (size_t)maxlen
)
545 gc
.attr
^= GRID_ATTR_CHARSET
;
546 else if (*ptr
== '\n') {
547 screen_write_linefeed(ctx
, 0, 8);
548 screen_write_carriagereturn(ctx
);
549 } else if (*ptr
> 0x1f && *ptr
< 0x7f) {
551 screen_write_putc(ctx
, &gc
, *ptr
);
561 * Copy from another screen but without the selection stuff. Assumes the target
562 * region is already big enough.
565 screen_write_fast_copy(struct screen_write_ctx
*ctx
, struct screen
*src
,
566 u_int px
, u_int py
, u_int nx
, u_int ny
)
568 struct screen
*s
= ctx
->s
;
569 struct grid
*gd
= src
->grid
;
571 u_int xx
, yy
, cx
, cy
;
573 if (nx
== 0 || ny
== 0)
577 for (yy
= py
; yy
< py
+ ny
; yy
++) {
578 if (yy
>= gd
->hsize
+ gd
->sy
)
581 for (xx
= px
; xx
< px
+ nx
; xx
++) {
582 if (xx
>= grid_get_line(gd
, yy
)->cellsize
)
584 grid_get_cell(gd
, xx
, yy
, &gc
);
585 if (xx
+ gc
.data
.width
> px
+ nx
)
587 grid_view_set_cell(ctx
->s
->grid
, cx
, cy
, &gc
);
594 /* Select character set for drawing border lines. */
596 screen_write_box_border_set(enum box_lines lines
, int cell_type
,
597 struct grid_cell
*gc
)
602 case BOX_LINES_DOUBLE
:
603 gc
->attr
&= ~GRID_ATTR_CHARSET
;
604 utf8_copy(&gc
->data
, tty_acs_double_borders(cell_type
));
606 case BOX_LINES_HEAVY
:
607 gc
->attr
&= ~GRID_ATTR_CHARSET
;
608 utf8_copy(&gc
->data
, tty_acs_heavy_borders(cell_type
));
610 case BOX_LINES_ROUNDED
:
611 gc
->attr
&= ~GRID_ATTR_CHARSET
;
612 utf8_copy(&gc
->data
, tty_acs_rounded_borders(cell_type
));
614 case BOX_LINES_SIMPLE
:
615 gc
->attr
&= ~GRID_ATTR_CHARSET
;
616 utf8_set(&gc
->data
, SIMPLE_BORDERS
[cell_type
]);
618 case BOX_LINES_PADDED
:
619 gc
->attr
&= ~GRID_ATTR_CHARSET
;
620 utf8_set(&gc
->data
, PADDED_BORDERS
[cell_type
]);
622 case BOX_LINES_SINGLE
:
623 case BOX_LINES_DEFAULT
:
624 gc
->attr
|= GRID_ATTR_CHARSET
;
625 utf8_set(&gc
->data
, CELL_BORDERS
[cell_type
]);
630 /* Draw a horizontal line on screen. */
632 screen_write_hline(struct screen_write_ctx
*ctx
, u_int nx
, int left
, int right
,
633 enum box_lines lines
, const struct grid_cell
*border_gc
)
635 struct screen
*s
= ctx
->s
;
642 if (border_gc
!= NULL
)
643 memcpy(&gc
, border_gc
, sizeof gc
);
645 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
646 gc
.attr
|= GRID_ATTR_CHARSET
;
649 screen_write_box_border_set(lines
, CELL_LEFTJOIN
, &gc
);
651 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
652 screen_write_cell(ctx
, &gc
);
654 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
655 for (i
= 1; i
< nx
- 1; i
++)
656 screen_write_cell(ctx
, &gc
);
659 screen_write_box_border_set(lines
, CELL_RIGHTJOIN
, &gc
);
661 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
662 screen_write_cell(ctx
, &gc
);
664 screen_write_set_cursor(ctx
, cx
, cy
);
667 /* Draw a vertical line on screen. */
669 screen_write_vline(struct screen_write_ctx
*ctx
, u_int ny
, int top
, int bottom
)
671 struct screen
*s
= ctx
->s
;
678 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
679 gc
.attr
|= GRID_ATTR_CHARSET
;
681 screen_write_putc(ctx
, &gc
, top
? 'w' : 'x');
682 for (i
= 1; i
< ny
- 1; i
++) {
683 screen_write_set_cursor(ctx
, cx
, cy
+ i
);
684 screen_write_putc(ctx
, &gc
, 'x');
686 screen_write_set_cursor(ctx
, cx
, cy
+ ny
- 1);
687 screen_write_putc(ctx
, &gc
, bottom
? 'v' : 'x');
689 screen_write_set_cursor(ctx
, cx
, cy
);
692 /* Draw a menu on screen. */
694 screen_write_menu(struct screen_write_ctx
*ctx
, struct menu
*menu
, int choice
,
695 enum box_lines lines
, const struct grid_cell
*menu_gc
,
696 const struct grid_cell
*border_gc
, const struct grid_cell
*choice_gc
)
698 struct screen
*s
= ctx
->s
;
699 struct grid_cell default_gc
;
700 const struct grid_cell
*gc
= &default_gc
;
701 u_int cx
, cy
, i
, j
, width
= menu
->width
;
707 memcpy(&default_gc
, menu_gc
, sizeof default_gc
);
709 screen_write_box(ctx
, menu
->width
+ 4, menu
->count
+ 2, lines
,
710 border_gc
, menu
->title
);
712 for (i
= 0; i
< menu
->count
; i
++) {
713 name
= menu
->items
[i
].name
;
715 screen_write_cursormove(ctx
, cx
, cy
+ 1 + i
, 0);
716 screen_write_hline(ctx
, width
+ 4, 1, 1, lines
,
721 if (choice
>= 0 && i
== (u_int
)choice
&& *name
!= '-')
724 screen_write_cursormove(ctx
, cx
+ 1, cy
+ 1 + i
, 0);
725 for (j
= 0; j
< width
+ 2; j
++)
726 screen_write_putc(ctx
, gc
, ' ');
728 screen_write_cursormove(ctx
, cx
+ 2, cy
+ 1 + i
, 0);
730 default_gc
.attr
|= GRID_ATTR_DIM
;
731 format_draw(ctx
, gc
, width
, name
+ 1, NULL
, 0);
732 default_gc
.attr
&= ~GRID_ATTR_DIM
;
736 format_draw(ctx
, gc
, width
, name
, NULL
, 0);
740 screen_write_set_cursor(ctx
, cx
, cy
);
743 /* Draw a box on screen. */
745 screen_write_box(struct screen_write_ctx
*ctx
, u_int nx
, u_int ny
,
746 enum box_lines lines
, const struct grid_cell
*gcp
, const char *title
)
748 struct screen
*s
= ctx
->s
;
756 memcpy(&gc
, gcp
, sizeof gc
);
758 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
760 gc
.attr
|= GRID_ATTR_CHARSET
;
761 gc
.flags
|= GRID_FLAG_NOPALETTE
;
763 /* Draw top border */
764 screen_write_box_border_set(lines
, CELL_TOPLEFT
, &gc
);
765 screen_write_cell(ctx
, &gc
);
766 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
767 for (i
= 1; i
< nx
- 1; i
++)
768 screen_write_cell(ctx
, &gc
);
769 screen_write_box_border_set(lines
, CELL_TOPRIGHT
, &gc
);
770 screen_write_cell(ctx
, &gc
);
772 /* Draw bottom border */
773 screen_write_set_cursor(ctx
, cx
, cy
+ ny
- 1);
774 screen_write_box_border_set(lines
, CELL_BOTTOMLEFT
, &gc
);
775 screen_write_cell(ctx
, &gc
);
776 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
777 for (i
= 1; i
< nx
- 1; i
++)
778 screen_write_cell(ctx
, &gc
);
779 screen_write_box_border_set(lines
, CELL_BOTTOMRIGHT
, &gc
);
780 screen_write_cell(ctx
, &gc
);
783 screen_write_box_border_set(lines
, CELL_TOPBOTTOM
, &gc
);
784 for (i
= 1; i
< ny
- 1; i
++) {
786 screen_write_set_cursor(ctx
, cx
, cy
+ i
);
787 screen_write_cell(ctx
, &gc
);
789 screen_write_set_cursor(ctx
, cx
+ nx
- 1, cy
+ i
);
790 screen_write_cell(ctx
, &gc
);
794 gc
.attr
&= ~GRID_ATTR_CHARSET
;
795 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
796 format_draw(ctx
, &gc
, nx
- 4, title
, NULL
, 0);
799 screen_write_set_cursor(ctx
, cx
, cy
);
803 * Write a preview version of a window. Assumes target area is big enough and
807 screen_write_preview(struct screen_write_ctx
*ctx
, struct screen
*src
, u_int nx
,
810 struct screen
*s
= ctx
->s
;
812 u_int cx
, cy
, px
, py
;
818 * If the cursor is on, pick the area around the cursor, otherwise use
821 if (src
->mode
& MODE_CURSOR
) {
827 if (px
+ nx
> screen_size_x(src
)) {
828 if (nx
> screen_size_x(src
))
831 px
= screen_size_x(src
) - nx
;
838 if (py
+ ny
> screen_size_y(src
)) {
839 if (ny
> screen_size_y(src
))
842 py
= screen_size_y(src
) - ny
;
849 screen_write_fast_copy(ctx
, src
, px
, src
->grid
->hsize
+ py
, nx
, ny
);
851 if (src
->mode
& MODE_CURSOR
) {
852 grid_view_get_cell(src
->grid
, src
->cx
, src
->cy
, &gc
);
853 gc
.attr
|= GRID_ATTR_REVERSE
;
854 screen_write_set_cursor(ctx
, cx
+ (src
->cx
- px
),
855 cy
+ (src
->cy
- py
));
856 screen_write_cell(ctx
, &gc
);
862 screen_write_mode_set(struct screen_write_ctx
*ctx
, int mode
)
864 struct screen
*s
= ctx
->s
;
868 if (log_get_level() != 0)
869 log_debug("%s: %s", __func__
, screen_mode_to_string(mode
));
874 screen_write_mode_clear(struct screen_write_ctx
*ctx
, int mode
)
876 struct screen
*s
= ctx
->s
;
880 if (log_get_level() != 0)
881 log_debug("%s: %s", __func__
, screen_mode_to_string(mode
));
884 /* Cursor up by ny. */
886 screen_write_cursorup(struct screen_write_ctx
*ctx
, u_int ny
)
888 struct screen
*s
= ctx
->s
;
889 u_int cx
= s
->cx
, cy
= s
->cy
;
894 if (cy
< s
->rupper
) {
900 if (ny
> cy
- s
->rupper
)
903 if (cx
== screen_size_x(s
))
908 screen_write_set_cursor(ctx
, cx
, cy
);
911 /* Cursor down by ny. */
913 screen_write_cursordown(struct screen_write_ctx
*ctx
, u_int ny
)
915 struct screen
*s
= ctx
->s
;
916 u_int cx
= s
->cx
, cy
= s
->cy
;
921 if (cy
> s
->rlower
) {
923 if (ny
> screen_size_y(s
) - 1 - cy
)
924 ny
= screen_size_y(s
) - 1 - cy
;
927 if (ny
> s
->rlower
- cy
)
930 if (cx
== screen_size_x(s
))
937 screen_write_set_cursor(ctx
, cx
, cy
);
940 /* Cursor right by nx. */
942 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
944 struct screen
*s
= ctx
->s
;
945 u_int cx
= s
->cx
, cy
= s
->cy
;
950 if (nx
> screen_size_x(s
) - 1 - cx
)
951 nx
= screen_size_x(s
) - 1 - cx
;
957 screen_write_set_cursor(ctx
, cx
, cy
);
960 /* Cursor left by nx. */
962 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
964 struct screen
*s
= ctx
->s
;
965 u_int cx
= s
->cx
, cy
= s
->cy
;
977 screen_write_set_cursor(ctx
, cx
, cy
);
980 /* Backspace; cursor left unless at start of wrapped line when can move up. */
982 screen_write_backspace(struct screen_write_ctx
*ctx
)
984 struct screen
*s
= ctx
->s
;
985 struct grid_line
*gl
;
986 u_int cx
= s
->cx
, cy
= s
->cy
;
991 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ cy
- 1);
992 if (gl
->flags
& GRID_LINE_WRAPPED
) {
994 cx
= screen_size_x(s
) - 1;
999 screen_write_set_cursor(ctx
, cx
, cy
);
1002 /* VT100 alignment test. */
1004 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
1006 struct screen
*s
= ctx
->s
;
1007 struct tty_ctx ttyctx
;
1008 struct grid_cell gc
;
1011 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1012 utf8_set(&gc
.data
, 'E');
1015 if (image_free_all(s
) && ctx
->wp
!= NULL
)
1016 ctx
->wp
->flags
|= PANE_REDRAW
;
1019 for (yy
= 0; yy
< screen_size_y(s
); yy
++) {
1020 for (xx
= 0; xx
< screen_size_x(s
); xx
++)
1021 grid_view_set_cell(s
->grid
, xx
, yy
, &gc
);
1024 screen_write_set_cursor(ctx
, 0, 0);
1027 s
->rlower
= screen_size_y(s
) - 1;
1029 screen_write_initctx(ctx
, &ttyctx
, 1);
1031 screen_write_collect_clear(ctx
, 0, screen_size_y(s
) - 1);
1032 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
1035 /* Insert nx characters. */
1037 screen_write_insertcharacter(struct screen_write_ctx
*ctx
, u_int nx
, u_int bg
)
1039 struct screen
*s
= ctx
->s
;
1040 struct tty_ctx ttyctx
;
1045 if (nx
> screen_size_x(s
) - s
->cx
)
1046 nx
= screen_size_x(s
) - s
->cx
;
1050 if (s
->cx
> screen_size_x(s
) - 1)
1054 if (image_check_line(s
, s
->cy
, 1) && ctx
->wp
!= NULL
)
1055 ctx
->wp
->flags
|= PANE_REDRAW
;
1058 screen_write_initctx(ctx
, &ttyctx
, 0);
1061 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
, bg
);
1063 screen_write_collect_flush(ctx
, 0, __func__
);
1065 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1068 /* Delete nx characters. */
1070 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
, u_int bg
)
1072 struct screen
*s
= ctx
->s
;
1073 struct tty_ctx ttyctx
;
1078 if (nx
> screen_size_x(s
) - s
->cx
)
1079 nx
= screen_size_x(s
) - s
->cx
;
1083 if (s
->cx
> screen_size_x(s
) - 1)
1087 if (image_check_line(s
, s
->cy
, 1) && ctx
->wp
!= NULL
)
1088 ctx
->wp
->flags
|= PANE_REDRAW
;
1091 screen_write_initctx(ctx
, &ttyctx
, 0);
1094 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
, bg
);
1096 screen_write_collect_flush(ctx
, 0, __func__
);
1098 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
1101 /* Clear nx characters. */
1103 screen_write_clearcharacter(struct screen_write_ctx
*ctx
, u_int nx
, u_int bg
)
1105 struct screen
*s
= ctx
->s
;
1106 struct tty_ctx ttyctx
;
1111 if (nx
> screen_size_x(s
) - s
->cx
)
1112 nx
= screen_size_x(s
) - s
->cx
;
1116 if (s
->cx
> screen_size_x(s
) - 1)
1120 if (image_check_line(s
, s
->cy
, 1) && ctx
->wp
!= NULL
)
1121 ctx
->wp
->flags
|= PANE_REDRAW
;
1124 screen_write_initctx(ctx
, &ttyctx
, 0);
1127 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, nx
, 1, bg
);
1129 screen_write_collect_flush(ctx
, 0, __func__
);
1131 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
1134 /* Insert ny lines. */
1136 screen_write_insertline(struct screen_write_ctx
*ctx
, u_int ny
, u_int bg
)
1138 struct screen
*s
= ctx
->s
;
1139 struct grid
*gd
= s
->grid
;
1140 struct tty_ctx ttyctx
;
1143 u_int sy
= screen_size_y(s
);
1150 if (image_check_line(s
, s
->cy
, sy
- s
->cy
) && ctx
->wp
!= NULL
)
1151 ctx
->wp
->flags
|= PANE_REDRAW
;
1154 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
1155 if (ny
> screen_size_y(s
) - s
->cy
)
1156 ny
= screen_size_y(s
) - s
->cy
;
1160 screen_write_initctx(ctx
, &ttyctx
, 1);
1163 grid_view_insert_lines(gd
, s
->cy
, ny
, bg
);
1165 screen_write_collect_flush(ctx
, 0, __func__
);
1167 tty_write(tty_cmd_insertline
, &ttyctx
);
1171 if (ny
> s
->rlower
+ 1 - s
->cy
)
1172 ny
= s
->rlower
+ 1 - s
->cy
;
1176 screen_write_initctx(ctx
, &ttyctx
, 1);
1179 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
1180 grid_view_insert_lines(gd
, s
->cy
, ny
, bg
);
1182 grid_view_insert_lines_region(gd
, s
->rlower
, s
->cy
, ny
, bg
);
1184 screen_write_collect_flush(ctx
, 0, __func__
);
1187 tty_write(tty_cmd_insertline
, &ttyctx
);
1190 /* Delete ny lines. */
1192 screen_write_deleteline(struct screen_write_ctx
*ctx
, u_int ny
, u_int bg
)
1194 struct screen
*s
= ctx
->s
;
1195 struct grid
*gd
= s
->grid
;
1196 struct tty_ctx ttyctx
;
1197 u_int sy
= screen_size_y(s
);
1203 if (image_check_line(s
, s
->cy
, sy
- s
->cy
) && ctx
->wp
!= NULL
)
1204 ctx
->wp
->flags
|= PANE_REDRAW
;
1207 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
1208 if (ny
> sy
- s
->cy
)
1213 screen_write_initctx(ctx
, &ttyctx
, 1);
1216 grid_view_delete_lines(gd
, s
->cy
, ny
, bg
);
1218 screen_write_collect_flush(ctx
, 0, __func__
);
1220 tty_write(tty_cmd_deleteline
, &ttyctx
);
1224 if (ny
> s
->rlower
+ 1 - s
->cy
)
1225 ny
= s
->rlower
+ 1 - s
->cy
;
1229 screen_write_initctx(ctx
, &ttyctx
, 1);
1232 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
1233 grid_view_delete_lines(gd
, s
->cy
, ny
, bg
);
1235 grid_view_delete_lines_region(gd
, s
->rlower
, s
->cy
, ny
, bg
);
1237 screen_write_collect_flush(ctx
, 0, __func__
);
1239 tty_write(tty_cmd_deleteline
, &ttyctx
);
1242 /* Clear line at cursor. */
1244 screen_write_clearline(struct screen_write_ctx
*ctx
, u_int bg
)
1246 struct screen
*s
= ctx
->s
;
1247 struct grid_line
*gl
;
1248 u_int sx
= screen_size_x(s
);
1249 struct screen_write_citem
*ci
= ctx
->item
;
1251 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ s
->cy
);
1252 if (gl
->cellsize
== 0 && COLOUR_DEFAULT(bg
))
1256 if (image_check_line(s
, s
->cy
, 1) && ctx
->wp
!= NULL
)
1257 ctx
->wp
->flags
|= PANE_REDRAW
;
1260 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1, bg
);
1262 screen_write_collect_clear(ctx
, s
->cy
, 1);
1267 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->cy
].items
, ci
, entry
);
1268 ctx
->item
= screen_write_get_citem();
1271 /* Clear to end of line from cursor. */
1273 screen_write_clearendofline(struct screen_write_ctx
*ctx
, u_int bg
)
1275 struct screen
*s
= ctx
->s
;
1276 struct grid_line
*gl
;
1277 u_int sx
= screen_size_x(s
);
1278 struct screen_write_citem
*ci
= ctx
->item
, *before
;
1281 screen_write_clearline(ctx
, bg
);
1285 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ s
->cy
);
1286 if (s
->cx
> sx
- 1 || (s
->cx
>= gl
->cellsize
&& COLOUR_DEFAULT(bg
)))
1290 if (image_check_line(s
, s
->cy
, 1) && ctx
->wp
!= NULL
)
1291 ctx
->wp
->flags
|= PANE_REDRAW
;
1294 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1, bg
);
1296 before
= screen_write_collect_trim(ctx
, s
->cy
, s
->cx
, sx
- s
->cx
, NULL
);
1298 ci
->used
= sx
- s
->cx
;
1302 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->cy
].items
, ci
, entry
);
1304 TAILQ_INSERT_BEFORE(before
, ci
, entry
);
1305 ctx
->item
= screen_write_get_citem();
1308 /* Clear to start of line from cursor. */
1310 screen_write_clearstartofline(struct screen_write_ctx
*ctx
, u_int bg
)
1312 struct screen
*s
= ctx
->s
;
1313 u_int sx
= screen_size_x(s
);
1314 struct screen_write_citem
*ci
= ctx
->item
, *before
;
1316 if (s
->cx
>= sx
- 1) {
1317 screen_write_clearline(ctx
, bg
);
1322 if (image_check_line(s
, s
->cy
, 1) && ctx
->wp
!= NULL
)
1323 ctx
->wp
->flags
|= PANE_REDRAW
;
1327 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1, bg
);
1329 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1, bg
);
1331 before
= screen_write_collect_trim(ctx
, s
->cy
, 0, s
->cx
+ 1, NULL
);
1333 ci
->used
= s
->cx
+ 1;
1337 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->cy
].items
, ci
, entry
);
1339 TAILQ_INSERT_BEFORE(before
, ci
, entry
);
1340 ctx
->item
= screen_write_get_citem();
1343 /* Move cursor to px,py. */
1345 screen_write_cursormove(struct screen_write_ctx
*ctx
, int px
, int py
,
1348 struct screen
*s
= ctx
->s
;
1350 if (origin
&& py
!= -1 && (s
->mode
& MODE_ORIGIN
)) {
1351 if ((u_int
)py
> s
->rlower
- s
->rupper
)
1357 if (px
!= -1 && (u_int
)px
> screen_size_x(s
) - 1)
1358 px
= screen_size_x(s
) - 1;
1359 if (py
!= -1 && (u_int
)py
> screen_size_y(s
) - 1)
1360 py
= screen_size_y(s
) - 1;
1362 log_debug("%s: from %u,%u to %u,%u", __func__
, s
->cx
, s
->cy
, px
, py
);
1363 screen_write_set_cursor(ctx
, px
, py
);
1366 /* Reverse index (up with scroll). */
1368 screen_write_reverseindex(struct screen_write_ctx
*ctx
, u_int bg
)
1370 struct screen
*s
= ctx
->s
;
1371 struct tty_ctx ttyctx
;
1373 if (s
->cy
== s
->rupper
) {
1375 if (image_free_all(s
) && ctx
->wp
!= NULL
)
1376 ctx
->wp
->flags
|= PANE_REDRAW
;
1379 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
, bg
);
1380 screen_write_collect_flush(ctx
, 0, __func__
);
1382 screen_write_initctx(ctx
, &ttyctx
, 1);
1385 tty_write(tty_cmd_reverseindex
, &ttyctx
);
1386 } else if (s
->cy
> 0)
1387 screen_write_set_cursor(ctx
, -1, s
->cy
- 1);
1391 /* Set scroll region. */
1393 screen_write_scrollregion(struct screen_write_ctx
*ctx
, u_int rupper
,
1396 struct screen
*s
= ctx
->s
;
1398 if (rupper
> screen_size_y(s
) - 1)
1399 rupper
= screen_size_y(s
) - 1;
1400 if (rlower
> screen_size_y(s
) - 1)
1401 rlower
= screen_size_y(s
) - 1;
1402 if (rupper
>= rlower
) /* cannot be one line */
1405 screen_write_collect_flush(ctx
, 0, __func__
);
1407 /* Cursor moves to top-left. */
1408 screen_write_set_cursor(ctx
, 0, 0);
1416 screen_write_linefeed(struct screen_write_ctx
*ctx
, int wrapped
, u_int bg
)
1418 struct screen
*s
= ctx
->s
;
1419 struct grid
*gd
= s
->grid
;
1420 struct grid_line
*gl
;
1422 u_int rupper
= s
->rupper
, rlower
= s
->rlower
;
1424 gl
= grid_get_line(gd
, gd
->hsize
+ s
->cy
);
1426 gl
->flags
|= GRID_LINE_WRAPPED
;
1428 log_debug("%s: at %u,%u (region %u-%u)", __func__
, s
->cx
, s
->cy
,
1431 if (bg
!= ctx
->bg
) {
1432 screen_write_collect_flush(ctx
, 1, __func__
);
1436 if (s
->cy
== s
->rlower
) {
1438 if (rlower
== screen_size_y(s
) - 1)
1439 redraw
= image_scroll_up(s
, 1);
1441 redraw
= image_check_line(s
, rupper
, rlower
- rupper
);
1442 if (redraw
&& ctx
->wp
!= NULL
)
1443 ctx
->wp
->flags
|= PANE_REDRAW
;
1445 grid_view_scroll_region_up(gd
, s
->rupper
, s
->rlower
, bg
);
1446 screen_write_collect_scroll(ctx
, bg
);
1448 } else if (s
->cy
< screen_size_y(s
) - 1)
1449 screen_write_set_cursor(ctx
, -1, s
->cy
+ 1);
1454 screen_write_scrollup(struct screen_write_ctx
*ctx
, u_int lines
, u_int bg
)
1456 struct screen
*s
= ctx
->s
;
1457 struct grid
*gd
= s
->grid
;
1462 else if (lines
> s
->rlower
- s
->rupper
+ 1)
1463 lines
= s
->rlower
- s
->rupper
+ 1;
1465 if (bg
!= ctx
->bg
) {
1466 screen_write_collect_flush(ctx
, 1, __func__
);
1471 if (image_scroll_up(s
, lines
) && ctx
->wp
!= NULL
)
1472 ctx
->wp
->flags
|= PANE_REDRAW
;
1475 for (i
= 0; i
< lines
; i
++) {
1476 grid_view_scroll_region_up(gd
, s
->rupper
, s
->rlower
, bg
);
1477 screen_write_collect_scroll(ctx
, bg
);
1479 ctx
->scrolled
+= lines
;
1484 screen_write_scrolldown(struct screen_write_ctx
*ctx
, u_int lines
, u_int bg
)
1486 struct screen
*s
= ctx
->s
;
1487 struct grid
*gd
= s
->grid
;
1488 struct tty_ctx ttyctx
;
1491 screen_write_initctx(ctx
, &ttyctx
, 1);
1496 else if (lines
> s
->rlower
- s
->rupper
+ 1)
1497 lines
= s
->rlower
- s
->rupper
+ 1;
1500 if (image_free_all(s
) && ctx
->wp
!= NULL
)
1501 ctx
->wp
->flags
|= PANE_REDRAW
;
1504 for (i
= 0; i
< lines
; i
++)
1505 grid_view_scroll_region_down(gd
, s
->rupper
, s
->rlower
, bg
);
1507 screen_write_collect_flush(ctx
, 0, __func__
);
1509 tty_write(tty_cmd_scrolldown
, &ttyctx
);
1512 /* Carriage return (cursor to start of line). */
1514 screen_write_carriagereturn(struct screen_write_ctx
*ctx
)
1516 screen_write_set_cursor(ctx
, 0, -1);
1519 /* Clear to end of screen from cursor. */
1521 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
, u_int bg
)
1523 struct screen
*s
= ctx
->s
;
1524 struct grid
*gd
= s
->grid
;
1525 struct tty_ctx ttyctx
;
1526 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
1529 if (image_check_line(s
, s
->cy
, sy
- s
->cy
) && ctx
->wp
!= NULL
)
1530 ctx
->wp
->flags
|= PANE_REDRAW
;
1533 screen_write_initctx(ctx
, &ttyctx
, 1);
1536 /* Scroll into history if it is enabled and clearing entire screen. */
1539 (gd
->flags
& GRID_HISTORY
) &&
1541 options_get_number(ctx
->wp
->options
, "scroll-on-clear"))
1542 grid_view_clear_history(gd
, bg
);
1544 if (s
->cx
<= sx
- 1)
1545 grid_view_clear(gd
, s
->cx
, s
->cy
, sx
- s
->cx
, 1, bg
);
1546 grid_view_clear(gd
, 0, s
->cy
+ 1, sx
, sy
- (s
->cy
+ 1), bg
);
1549 screen_write_collect_clear(ctx
, s
->cy
+ 1, sy
- (s
->cy
+ 1));
1550 screen_write_collect_flush(ctx
, 0, __func__
);
1551 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
1554 /* Clear to start of screen. */
1556 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
, u_int bg
)
1558 struct screen
*s
= ctx
->s
;
1559 struct tty_ctx ttyctx
;
1560 u_int sx
= screen_size_x(s
);
1563 if (image_check_line(s
, 0, s
->cy
- 1) && ctx
->wp
!= NULL
)
1564 ctx
->wp
->flags
|= PANE_REDRAW
;
1567 screen_write_initctx(ctx
, &ttyctx
, 1);
1571 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
, bg
);
1573 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1, bg
);
1575 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1, bg
);
1577 screen_write_collect_clear(ctx
, 0, s
->cy
);
1578 screen_write_collect_flush(ctx
, 0, __func__
);
1579 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
1582 /* Clear entire screen. */
1584 screen_write_clearscreen(struct screen_write_ctx
*ctx
, u_int bg
)
1586 struct screen
*s
= ctx
->s
;
1587 struct tty_ctx ttyctx
;
1588 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
1591 if (image_free_all(s
) && ctx
->wp
!= NULL
)
1592 ctx
->wp
->flags
|= PANE_REDRAW
;
1595 screen_write_initctx(ctx
, &ttyctx
, 1);
1598 /* Scroll into history if it is enabled. */
1599 if ((s
->grid
->flags
& GRID_HISTORY
) &&
1601 options_get_number(ctx
->wp
->options
, "scroll-on-clear"))
1602 grid_view_clear_history(s
->grid
, bg
);
1604 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, bg
);
1606 screen_write_collect_clear(ctx
, 0, sy
);
1607 tty_write(tty_cmd_clearscreen
, &ttyctx
);
1610 /* Clear entire history. */
1612 screen_write_clearhistory(struct screen_write_ctx
*ctx
)
1614 grid_clear_history(ctx
->s
->grid
);
1617 /* Force a full redraw. */
1619 screen_write_fullredraw(struct screen_write_ctx
*ctx
)
1621 struct tty_ctx ttyctx
;
1623 screen_write_collect_flush(ctx
, 0, __func__
);
1625 screen_write_initctx(ctx
, &ttyctx
, 1);
1626 if (ttyctx
.redraw_cb
!= NULL
)
1627 ttyctx
.redraw_cb(&ttyctx
);
1630 /* Trim collected items. */
1631 static struct screen_write_citem
*
1632 screen_write_collect_trim(struct screen_write_ctx
*ctx
, u_int y
, u_int x
,
1633 u_int used
, int *wrapped
)
1635 struct screen_write_cline
*cl
= &ctx
->s
->write_list
[y
];
1636 struct screen_write_citem
*ci
, *ci2
, *tmp
, *before
= NULL
;
1637 u_int sx
= x
, ex
= x
+ used
- 1;
1640 if (TAILQ_EMPTY(&cl
->items
))
1642 TAILQ_FOREACH_SAFE(ci
, &cl
->items
, entry
, tmp
) {
1644 cex
= ci
->x
+ ci
->used
- 1;
1646 /* Item is entirely before. */
1648 log_debug("%s: %p %u-%u before %u-%u", __func__
, ci
,
1653 /* Item is entirely after. */
1655 log_debug("%s: %p %u-%u after %u-%u", __func__
, ci
,
1661 /* Item is entirely inside. */
1662 if (csx
>= sx
&& cex
<= ex
) {
1663 log_debug("%s: %p %u-%u inside %u-%u", __func__
, ci
,
1665 TAILQ_REMOVE(&cl
->items
, ci
, entry
);
1666 screen_write_free_citem(ci
);
1667 if (csx
== 0 && ci
->wrapped
&& wrapped
!= NULL
)
1672 /* Item under the start. */
1673 if (csx
< sx
&& cex
>= sx
&& cex
<= ex
) {
1674 log_debug("%s: %p %u-%u start %u-%u", __func__
, ci
,
1676 ci
->used
= sx
- csx
;
1677 log_debug("%s: %p now %u-%u", __func__
, ci
, ci
->x
,
1678 ci
->x
+ ci
->used
+ 1);
1682 /* Item covers the end. */
1683 if (cex
> ex
&& csx
>= sx
&& csx
<= ex
) {
1684 log_debug("%s: %p %u-%u end %u-%u", __func__
, ci
,
1687 ci
->used
= cex
- ex
;
1688 log_debug("%s: %p now %u-%u", __func__
, ci
, ci
->x
,
1689 ci
->x
+ ci
->used
+ 1);
1694 /* Item must cover both sides. */
1695 log_debug("%s: %p %u-%u under %u-%u", __func__
, ci
,
1697 ci2
= screen_write_get_citem();
1698 ci2
->type
= ci
->type
;
1700 memcpy(&ci2
->gc
, &ci
->gc
, sizeof ci2
->gc
);
1701 TAILQ_INSERT_AFTER(&cl
->items
, ci
, ci2
, entry
);
1703 ci
->used
= sx
- csx
;
1705 ci2
->used
= cex
- ex
;
1707 log_debug("%s: %p now %u-%u (%p) and %u-%u (%p)", __func__
, ci
,
1708 ci
->x
, ci
->x
+ ci
->used
- 1, ci
, ci2
->x
,
1709 ci2
->x
+ ci2
->used
- 1, ci2
);
1716 /* Clear collected lines. */
1718 screen_write_collect_clear(struct screen_write_ctx
*ctx
, u_int y
, u_int n
)
1720 struct screen_write_cline
*cl
;
1723 for (i
= y
; i
< y
+ n
; i
++) {
1724 cl
= &ctx
->s
->write_list
[i
];
1725 TAILQ_CONCAT(&screen_write_citem_freelist
, &cl
->items
, entry
);
1729 /* Scroll collected lines up. */
1731 screen_write_collect_scroll(struct screen_write_ctx
*ctx
, u_int bg
)
1733 struct screen
*s
= ctx
->s
;
1734 struct screen_write_cline
*cl
;
1737 struct screen_write_citem
*ci
;
1739 log_debug("%s: at %u,%u (region %u-%u)", __func__
, s
->cx
, s
->cy
,
1740 s
->rupper
, s
->rlower
);
1742 screen_write_collect_clear(ctx
, s
->rupper
, 1);
1743 saved
= ctx
->s
->write_list
[s
->rupper
].data
;
1744 for (y
= s
->rupper
; y
< s
->rlower
; y
++) {
1745 cl
= &ctx
->s
->write_list
[y
+ 1];
1746 TAILQ_CONCAT(&ctx
->s
->write_list
[y
].items
, &cl
->items
, entry
);
1747 ctx
->s
->write_list
[y
].data
= cl
->data
;
1749 ctx
->s
->write_list
[s
->rlower
].data
= saved
;
1751 ci
= screen_write_get_citem();
1753 ci
->used
= screen_size_x(s
);
1756 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->rlower
].items
, ci
, entry
);
1759 /* Flush collected lines. */
1761 screen_write_collect_flush(struct screen_write_ctx
*ctx
, int scroll_only
,
1764 struct screen
*s
= ctx
->s
;
1765 struct screen_write_citem
*ci
, *tmp
;
1766 struct screen_write_cline
*cl
;
1767 u_int y
, cx
, cy
, last
, items
= 0;
1768 struct tty_ctx ttyctx
;
1770 if (ctx
->scrolled
!= 0) {
1771 log_debug("%s: scrolled %u (region %u-%u)", __func__
,
1772 ctx
->scrolled
, s
->rupper
, s
->rlower
);
1773 if (ctx
->scrolled
> s
->rlower
- s
->rupper
+ 1)
1774 ctx
->scrolled
= s
->rlower
- s
->rupper
+ 1;
1776 screen_write_initctx(ctx
, &ttyctx
, 1);
1777 ttyctx
.num
= ctx
->scrolled
;
1778 ttyctx
.bg
= ctx
->bg
;
1779 tty_write(tty_cmd_scrollup
, &ttyctx
);
1787 cx
= s
->cx
; cy
= s
->cy
;
1788 for (y
= 0; y
< screen_size_y(s
); y
++) {
1789 cl
= &ctx
->s
->write_list
[y
];
1791 TAILQ_FOREACH_SAFE(ci
, &cl
->items
, entry
, tmp
) {
1792 if (last
!= UINT_MAX
&& ci
->x
<= last
) {
1793 fatalx("collect list not in order: %u <= %u",
1796 screen_write_set_cursor(ctx
, ci
->x
, y
);
1797 if (ci
->type
== CLEAR
) {
1798 screen_write_initctx(ctx
, &ttyctx
, 1);
1800 ttyctx
.num
= ci
->used
;
1801 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
1803 screen_write_initctx(ctx
, &ttyctx
, 0);
1804 ttyctx
.cell
= &ci
->gc
;
1805 ttyctx
.wrapped
= ci
->wrapped
;
1806 ttyctx
.ptr
= cl
->data
+ ci
->x
;
1807 ttyctx
.num
= ci
->used
;
1808 tty_write(tty_cmd_cells
, &ttyctx
);
1812 TAILQ_REMOVE(&cl
->items
, ci
, entry
);
1813 screen_write_free_citem(ci
);
1817 s
->cx
= cx
; s
->cy
= cy
;
1819 log_debug("%s: flushed %u items (%s)", __func__
, items
, from
);
1822 /* Finish and store collected cells. */
1824 screen_write_collect_end(struct screen_write_ctx
*ctx
)
1826 struct screen
*s
= ctx
->s
;
1827 struct screen_write_citem
*ci
= ctx
->item
, *before
;
1828 struct screen_write_cline
*cl
= &s
->write_list
[s
->cy
];
1829 struct grid_cell gc
;
1831 int wrapped
= ci
->wrapped
;
1835 ctx
->flags
&= ~SCREEN_WRITE_COMBINE
;
1837 before
= screen_write_collect_trim(ctx
, s
->cy
, s
->cx
, ci
->used
,
1840 ci
->wrapped
= wrapped
;
1842 TAILQ_INSERT_TAIL(&cl
->items
, ci
, entry
);
1844 TAILQ_INSERT_BEFORE(before
, ci
, entry
);
1845 ctx
->item
= screen_write_get_citem();
1847 log_debug("%s: %u %.*s (at %u,%u)", __func__
, ci
->used
,
1848 (int)ci
->used
, cl
->data
+ ci
->x
, s
->cx
, s
->cy
);
1851 for (xx
= s
->cx
; xx
> 0; xx
--) {
1852 grid_view_get_cell(s
->grid
, xx
, s
->cy
, &gc
);
1853 if (~gc
.flags
& GRID_FLAG_PADDING
)
1855 grid_view_set_cell(s
->grid
, xx
, s
->cy
,
1856 &grid_default_cell
);
1858 if (gc
.data
.width
> 1) {
1859 grid_view_set_cell(s
->grid
, xx
, s
->cy
,
1860 &grid_default_cell
);
1865 if (image_check_area(s
, s
->cx
, s
->cy
, ci
->used
, 1) && ctx
->wp
!= NULL
)
1866 ctx
->wp
->flags
|= PANE_REDRAW
;
1869 grid_view_set_cells(s
->grid
, s
->cx
, s
->cy
, &ci
->gc
, cl
->data
+ ci
->x
,
1871 screen_write_set_cursor(ctx
, s
->cx
+ ci
->used
, -1);
1873 for (xx
= s
->cx
; xx
< screen_size_x(s
); xx
++) {
1874 grid_view_get_cell(s
->grid
, xx
, s
->cy
, &gc
);
1875 if (~gc
.flags
& GRID_FLAG_PADDING
)
1877 grid_view_set_cell(s
->grid
, xx
, s
->cy
, &grid_default_cell
);
1881 /* Write cell data, collecting if necessary. */
1883 screen_write_collect_add(struct screen_write_ctx
*ctx
,
1884 const struct grid_cell
*gc
)
1886 struct screen
*s
= ctx
->s
;
1887 struct screen_write_citem
*ci
;
1888 u_int sx
= screen_size_x(s
);
1892 * Don't need to check that the attributes and whatnot are still the
1893 * same - input_parse will end the collection when anything that isn't
1894 * a plain character is encountered.
1898 if (gc
->data
.width
!= 1 || gc
->data
.size
!= 1 || *gc
->data
.data
>= 0x7f)
1900 else if (gc
->attr
& GRID_ATTR_CHARSET
)
1902 else if (~s
->mode
& MODE_WRAP
)
1904 else if (s
->mode
& MODE_INSERT
)
1906 else if (s
->sel
!= NULL
)
1909 screen_write_collect_end(ctx
);
1910 screen_write_collect_flush(ctx
, 0, __func__
);
1911 screen_write_cell(ctx
, gc
);
1915 if (s
->cx
> sx
- 1 || ctx
->item
->used
> sx
- 1 - s
->cx
)
1916 screen_write_collect_end(ctx
);
1917 ci
= ctx
->item
; /* may have changed */
1919 if (s
->cx
> sx
- 1) {
1920 log_debug("%s: wrapped at %u,%u", __func__
, s
->cx
, s
->cy
);
1922 screen_write_linefeed(ctx
, 1, 8);
1923 screen_write_set_cursor(ctx
, 0, -1);
1927 memcpy(&ci
->gc
, gc
, sizeof ci
->gc
);
1928 if (ctx
->s
->write_list
[s
->cy
].data
== NULL
)
1929 ctx
->s
->write_list
[s
->cy
].data
= xmalloc(screen_size_x(ctx
->s
));
1930 ctx
->s
->write_list
[s
->cy
].data
[s
->cx
+ ci
->used
++] = gc
->data
.data
[0];
1933 /* Write cell data. */
1935 screen_write_cell(struct screen_write_ctx
*ctx
, const struct grid_cell
*gc
)
1937 struct screen
*s
= ctx
->s
;
1938 struct grid
*gd
= s
->grid
;
1939 struct grid_cell copy
;
1940 const struct utf8_data
*ud
= &gc
->data
, *previous
= NULL
, *combine
;
1941 struct grid_line
*gl
;
1942 struct grid_cell_entry
*gce
;
1943 struct grid_cell tmp_gc
, now_gc
;
1944 struct tty_ctx ttyctx
;
1945 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
1946 u_int width
= ud
->width
, xx
, last
, cx
, cy
;
1947 int selected
, skip
= 1;
1949 /* Ignore padding cells. */
1950 if (gc
->flags
& GRID_FLAG_PADDING
)
1953 /* Check if this cell needs to be combined with the previous cell. */
1954 if (ctx
->flags
& SCREEN_WRITE_COMBINE
)
1955 previous
= &ctx
->previous
;
1956 switch (utf8_try_combined(ud
, previous
, &combine
, &width
)) {
1957 case UTF8_DISCARD_NOW
:
1958 log_debug("%s: UTF8_DISCARD_NOW (width %u)", __func__
, width
);
1959 ctx
->flags
&= ~SCREEN_WRITE_COMBINE
;
1961 case UTF8_WRITE_NOW
:
1962 log_debug("%s: UTF8_WRITE_NOW (width %u)", __func__
, width
);
1963 ctx
->flags
&= ~SCREEN_WRITE_COMBINE
;
1965 case UTF8_COMBINE_NOW
:
1966 log_debug("%s: UTF8_COMBINE_NOW (width %u)", __func__
, width
);
1967 screen_write_collect_flush(ctx
, 0, __func__
);
1968 gc
= screen_write_combine(ctx
, combine
, &xx
, &cx
);
1970 cx
= s
->cx
; cy
= s
->cy
;
1971 screen_write_set_cursor(ctx
, xx
, s
->cy
);
1972 screen_write_initctx(ctx
, &ttyctx
, 0);
1974 tty_write(tty_cmd_cell
, &ttyctx
);
1975 s
->cx
= cx
; s
->cy
= cy
;
1977 ctx
->flags
&= ~SCREEN_WRITE_COMBINE
;
1979 case UTF8_WRITE_MAYBE_COMBINE
:
1980 log_debug("%s: UTF8_WRITE_MAYBE_COMBINE (width %u)", __func__
,
1982 utf8_copy(&ctx
->previous
, ud
);
1983 ctx
->flags
|= SCREEN_WRITE_COMBINE
;
1985 case UTF8_DISCARD_MAYBE_COMBINE
:
1986 log_debug("%s: UTF8_DISCARD_MAYBE_COMBINE (width %u)", __func__
,
1988 utf8_copy(&ctx
->previous
, ud
);
1989 ctx
->flags
|= SCREEN_WRITE_COMBINE
;
1992 if (width
!= ud
->width
) {
1993 memcpy(©
, gc
, sizeof copy
);
1994 copy
.data
.width
= width
;
1999 /* Flush any existing scrolling. */
2000 screen_write_collect_flush(ctx
, 1, __func__
);
2002 /* If this character doesn't fit, ignore it. */
2003 if ((~s
->mode
& MODE_WRAP
) &&
2005 (width
> sx
|| (s
->cx
!= sx
&& s
->cx
> sx
- width
)))
2008 /* If in insert mode, make space for the cells. */
2009 if (s
->mode
& MODE_INSERT
) {
2010 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, width
, 8);
2014 /* Check this will fit on the current line and wrap if not. */
2015 if ((s
->mode
& MODE_WRAP
) && s
->cx
> sx
- width
) {
2016 log_debug("%s: wrapped at %u,%u", __func__
, s
->cx
, s
->cy
);
2017 screen_write_linefeed(ctx
, 1, 8);
2018 screen_write_set_cursor(ctx
, 0, -1);
2019 screen_write_collect_flush(ctx
, 1, __func__
);
2022 /* Sanity check cursor position. */
2023 if (s
->cx
> sx
- width
|| s
->cy
> sy
- 1)
2025 screen_write_initctx(ctx
, &ttyctx
, 0);
2027 /* Handle overwriting of UTF-8 characters. */
2028 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ s
->cy
);
2029 if (gl
->flags
& GRID_LINE_EXTENDED
) {
2030 grid_view_get_cell(gd
, s
->cx
, s
->cy
, &now_gc
);
2031 if (screen_write_overwrite(ctx
, &now_gc
, width
))
2036 * If the new character is UTF-8 wide, fill in padding cells. Have
2037 * already ensured there is enough room.
2039 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
2040 log_debug("%s: new padding at %u,%u", __func__
, xx
, s
->cy
);
2041 grid_view_set_padding(gd
, xx
, s
->cy
);
2045 /* If no change, do not draw. */
2047 if (s
->cx
>= gl
->cellsize
)
2048 skip
= grid_cells_equal(gc
, &grid_default_cell
);
2050 gce
= &gl
->celldata
[s
->cx
];
2051 if (gce
->flags
& GRID_FLAG_EXTENDED
)
2053 else if (gc
->flags
!= gce
->flags
)
2055 else if (gc
->attr
!= gce
->data
.attr
)
2057 else if (gc
->fg
!= gce
->data
.fg
)
2059 else if (gc
->bg
!= gce
->data
.bg
)
2061 else if (gc
->data
.width
!= 1)
2063 else if (gc
->data
.size
!= 1)
2065 else if (gce
->data
.data
!= gc
->data
.data
[0])
2070 /* Update the selected flag and set the cell. */
2071 selected
= screen_check_selection(s
, s
->cx
, s
->cy
);
2072 if (selected
&& (~gc
->flags
& GRID_FLAG_SELECTED
)) {
2073 memcpy(&tmp_gc
, gc
, sizeof tmp_gc
);
2074 tmp_gc
.flags
|= GRID_FLAG_SELECTED
;
2075 grid_view_set_cell(gd
, s
->cx
, s
->cy
, &tmp_gc
);
2076 } else if (!selected
&& (gc
->flags
& GRID_FLAG_SELECTED
)) {
2077 memcpy(&tmp_gc
, gc
, sizeof tmp_gc
);
2078 tmp_gc
.flags
&= ~GRID_FLAG_SELECTED
;
2079 grid_view_set_cell(gd
, s
->cx
, s
->cy
, &tmp_gc
);
2081 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
2086 * Move the cursor. If not wrapping, stick at the last character and
2089 last
= !(s
->mode
& MODE_WRAP
);
2090 if (s
->cx
<= sx
- last
- width
)
2091 screen_write_set_cursor(ctx
, s
->cx
+ width
, -1);
2093 screen_write_set_cursor(ctx
, sx
- last
, -1);
2095 /* Create space for character in insert mode. */
2096 if (s
->mode
& MODE_INSERT
) {
2097 screen_write_collect_flush(ctx
, 0, __func__
);
2099 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
2102 /* Write to the screen. */
2105 screen_select_cell(s
, &tmp_gc
, gc
);
2106 ttyctx
.cell
= &tmp_gc
;
2109 tty_write(tty_cmd_cell
, &ttyctx
);
2113 /* Combine a UTF-8 zero-width character onto the previous. */
2114 static const struct grid_cell
*
2115 screen_write_combine(struct screen_write_ctx
*ctx
, const struct utf8_data
*ud
,
2116 u_int
*xx
, u_int
*cx
)
2118 struct screen
*s
= ctx
->s
;
2119 struct grid
*gd
= s
->grid
;
2120 static struct grid_cell gc
;
2123 /* Can't combine if at 0. */
2130 /* Empty data is out. */
2132 fatalx("UTF-8 data empty");
2134 /* Retrieve the previous cell. */
2135 for (n
= 1; n
<= s
->cx
; n
++) {
2136 grid_view_get_cell(gd
, s
->cx
- n
, s
->cy
, &gc
);
2137 if (~gc
.flags
& GRID_FLAG_PADDING
)
2143 /* Check there is enough space. */
2144 if (gc
.data
.size
+ ud
->size
> sizeof gc
.data
.data
)
2148 log_debug("%s: %.*s onto %.*s at %u,%u (width %u)", __func__
,
2149 (int)ud
->size
, ud
->data
, (int)gc
.data
.size
, gc
.data
.data
, *xx
,
2150 s
->cy
, gc
.data
.width
);
2152 /* Append the data. */
2153 memcpy(gc
.data
.data
+ gc
.data
.size
, ud
->data
, ud
->size
);
2154 gc
.data
.size
+= ud
->size
;
2155 width
= gc
.data
.width
;
2157 /* If this is U+FE0F VARIATION SELECTOR-16, force the width to 2. */
2158 if (gc
.data
.width
== 1 &&
2160 memcmp(ud
->data
, "\357\270\217", 3) == 0) {
2161 grid_view_set_padding(gd
, (*xx
) + 1, s
->cy
);
2166 /* Set the new cell. */
2167 grid_view_set_cell(gd
, *xx
, s
->cy
, &gc
);
2169 *cx
= (*xx
) + width
;
2170 log_debug("%s: character at %u; cursor at %u", __func__
, *xx
, *cx
);
2175 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
2176 * cell on the screen, so following cells must not be drawn by marking them as
2179 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
2180 * character, it is necessary to also overwrite any other cells which covered
2181 * by the same character.
2184 screen_write_overwrite(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
,
2187 struct screen
*s
= ctx
->s
;
2188 struct grid
*gd
= s
->grid
;
2189 struct grid_cell tmp_gc
;
2193 if (gc
->flags
& GRID_FLAG_PADDING
) {
2195 * A padding cell, so clear any following and leading padding
2196 * cells back to the character. Don't overwrite the current
2197 * cell as that happens later anyway.
2201 grid_view_get_cell(gd
, xx
, s
->cy
, &tmp_gc
);
2202 if (~tmp_gc
.flags
& GRID_FLAG_PADDING
)
2204 log_debug("%s: padding at %u,%u", __func__
, xx
, s
->cy
);
2205 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
2208 /* Overwrite the character at the start of this padding. */
2209 log_debug("%s: character at %u,%u", __func__
, xx
, s
->cy
);
2210 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
2215 * Overwrite any padding cells that belong to any UTF-8 characters
2216 * we'll be overwriting with the current character.
2219 gc
->data
.width
!= 1 ||
2220 gc
->flags
& GRID_FLAG_PADDING
) {
2221 xx
= s
->cx
+ width
- 1;
2222 while (++xx
< screen_size_x(s
)) {
2223 grid_view_get_cell(gd
, xx
, s
->cy
, &tmp_gc
);
2224 if (~tmp_gc
.flags
& GRID_FLAG_PADDING
)
2226 log_debug("%s: overwrite at %u,%u", __func__
, xx
,
2228 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
2236 /* Set external clipboard. */
2238 screen_write_setselection(struct screen_write_ctx
*ctx
, const char *flags
,
2239 u_char
*str
, u_int len
)
2241 struct tty_ctx ttyctx
;
2243 screen_write_initctx(ctx
, &ttyctx
, 0);
2245 ttyctx
.ptr2
= (void *)flags
;
2248 tty_write(tty_cmd_setselection
, &ttyctx
);
2251 /* Write unmodified string. */
2253 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
,
2254 int allow_invisible_panes
)
2256 struct tty_ctx ttyctx
;
2258 screen_write_initctx(ctx
, &ttyctx
, 0);
2261 ttyctx
.allow_invisible_panes
= allow_invisible_panes
;
2263 tty_write(tty_cmd_rawstring
, &ttyctx
);
2267 /* Write a SIXEL image. */
2269 screen_write_sixelimage(struct screen_write_ctx
*ctx
, struct sixel_image
*si
,
2272 struct screen
*s
= ctx
->s
;
2273 struct grid
*gd
= s
->grid
;
2274 struct tty_ctx ttyctx
;
2275 u_int x
, y
, sx
, sy
, cx
= s
->cx
, cy
= s
->cy
, i
, lines
;
2276 struct sixel_image
*new;
2278 sixel_size_in_cells(si
, &x
, &y
);
2279 if (x
> screen_size_x(s
) || y
> screen_size_y(s
)) {
2280 if (x
> screen_size_x(s
) - cx
)
2281 sx
= screen_size_x(s
) - cx
;
2284 if (y
> screen_size_y(s
) - 1)
2285 sy
= screen_size_y(s
) - 1;
2288 new = sixel_scale(si
, 0, 0, 0, y
- sy
, sx
, sy
, 1);
2291 sixel_size_in_cells(si
, &x
, &y
);
2294 sy
= screen_size_y(s
) - cy
;
2297 if (image_scroll_up(s
, lines
) && ctx
->wp
!= NULL
)
2298 ctx
->wp
->flags
|= PANE_REDRAW
;
2299 for (i
= 0; i
< lines
; i
++) {
2300 grid_view_scroll_region_up(gd
, 0, screen_size_y(s
) - 1,
2302 screen_write_collect_scroll(ctx
, bg
);
2304 ctx
->scrolled
+= lines
;
2306 screen_write_cursormove(ctx
, -1, 0, 0);
2308 screen_write_cursormove(ctx
, -1, cy
- lines
, 0);
2310 screen_write_collect_flush(ctx
, 0, __func__
);
2312 screen_write_initctx(ctx
, &ttyctx
, 0);
2313 ttyctx
.ptr
= image_store(s
, si
);
2315 tty_write(tty_cmd_sixelimage
, &ttyctx
);
2317 screen_write_cursormove(ctx
, 0, cy
+ y
, 0);
2321 /* Turn alternate screen on. */
2323 screen_write_alternateon(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
,
2326 struct tty_ctx ttyctx
;
2327 struct window_pane
*wp
= ctx
->wp
;
2329 if (wp
!= NULL
&& !options_get_number(wp
->options
, "alternate-screen"))
2332 screen_write_collect_flush(ctx
, 0, __func__
);
2333 screen_alternate_on(ctx
->s
, gc
, cursor
);
2335 screen_write_initctx(ctx
, &ttyctx
, 1);
2336 if (ttyctx
.redraw_cb
!= NULL
)
2337 ttyctx
.redraw_cb(&ttyctx
);
2340 /* Turn alternate screen off. */
2342 screen_write_alternateoff(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
,
2345 struct tty_ctx ttyctx
;
2346 struct window_pane
*wp
= ctx
->wp
;
2348 if (wp
!= NULL
&& !options_get_number(wp
->options
, "alternate-screen"))
2351 screen_write_collect_flush(ctx
, 0, __func__
);
2352 screen_alternate_off(ctx
->s
, gc
, cursor
);
2354 screen_write_initctx(ctx
, &ttyctx
, 1);
2355 if (ttyctx
.redraw_cb
!= NULL
)
2356 ttyctx
.redraw_cb(&ttyctx
);