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,
34 static int screen_write_overwrite(struct screen_write_ctx
*,
35 struct grid_cell
*, u_int
);
36 static const struct grid_cell
*screen_write_combine(struct screen_write_ctx
*,
37 const struct utf8_data
*, u_int
*);
39 struct screen_write_citem
{
43 enum { TEXT
, CLEAR
} type
;
49 TAILQ_ENTRY(screen_write_citem
) entry
;
51 struct screen_write_cline
{
53 TAILQ_HEAD(, screen_write_citem
) items
;
55 TAILQ_HEAD(, screen_write_citem
) screen_write_citem_freelist
=
56 TAILQ_HEAD_INITIALIZER(screen_write_citem_freelist
);
58 static struct screen_write_citem
*
59 screen_write_get_citem(void)
61 struct screen_write_citem
*ci
;
63 ci
= TAILQ_FIRST(&screen_write_citem_freelist
);
65 TAILQ_REMOVE(&screen_write_citem_freelist
, ci
, entry
);
66 memset(ci
, 0, sizeof *ci
);
69 return (xcalloc(1, sizeof *ci
));
73 screen_write_free_citem(struct screen_write_citem
*ci
)
75 TAILQ_INSERT_TAIL(&screen_write_citem_freelist
, ci
, entry
);
79 screen_write_offset_timer(__unused
int fd
, __unused
short events
, void *data
)
81 struct window
*w
= data
;
83 tty_update_window_offset(w
);
86 /* Set cursor position. */
88 screen_write_set_cursor(struct screen_write_ctx
*ctx
, int cx
, int cy
)
90 struct window_pane
*wp
= ctx
->wp
;
92 struct screen
*s
= ctx
->s
;
93 struct timeval tv
= { .tv_usec
= 10000 };
95 if (cx
!= -1 && (u_int
)cx
== s
->cx
&& cy
!= -1 && (u_int
)cy
== s
->cy
)
99 if ((u_int
)cx
> screen_size_x(s
)) /* allow last column */
100 cx
= screen_size_x(s
) - 1;
104 if ((u_int
)cy
> screen_size_y(s
) - 1)
105 cy
= screen_size_y(s
) - 1;
113 if (!event_initialized(&w
->offset_timer
))
114 evtimer_set(&w
->offset_timer
, screen_write_offset_timer
, w
);
115 if (!evtimer_pending(&w
->offset_timer
, NULL
))
116 evtimer_add(&w
->offset_timer
, &tv
);
119 /* Do a full redraw. */
121 screen_write_redraw_cb(const struct tty_ctx
*ttyctx
)
123 struct window_pane
*wp
= ttyctx
->arg
;
126 wp
->flags
|= PANE_REDRAW
;
129 /* Update context for client. */
131 screen_write_set_client_cb(struct tty_ctx
*ttyctx
, struct client
*c
)
133 struct window_pane
*wp
= ttyctx
->arg
;
135 if (c
->session
->curw
->window
!= wp
->window
)
137 if (wp
->layout_cell
== NULL
)
140 if (wp
->flags
& (PANE_REDRAW
|PANE_DROP
))
142 if (c
->flags
& CLIENT_REDRAWPANES
) {
144 * Redraw is already deferred to redraw another pane - redraw
145 * this one also when that happens.
147 log_debug("%s: adding %%%u to deferred redraw", __func__
,
149 wp
->flags
|= PANE_REDRAW
;
153 ttyctx
->bigger
= tty_window_offset(&c
->tty
, &ttyctx
->wox
, &ttyctx
->woy
,
154 &ttyctx
->wsx
, &ttyctx
->wsy
);
156 ttyctx
->xoff
= ttyctx
->rxoff
= wp
->xoff
;
157 ttyctx
->yoff
= ttyctx
->ryoff
= wp
->yoff
;
159 if (status_at_line(c
) == 0)
160 ttyctx
->yoff
+= status_line_size(c
);
165 /* Set up context for TTY command. */
167 screen_write_initctx(struct screen_write_ctx
*ctx
, struct tty_ctx
*ttyctx
,
170 struct screen
*s
= ctx
->s
;
172 memset(ttyctx
, 0, sizeof *ttyctx
);
175 ttyctx
->sx
= screen_size_x(s
);
176 ttyctx
->sy
= screen_size_y(s
);
180 ttyctx
->orlower
= s
->rlower
;
181 ttyctx
->orupper
= s
->rupper
;
183 memcpy(&ttyctx
->defaults
, &grid_default_cell
, sizeof ttyctx
->defaults
);
184 if (ctx
->init_ctx_cb
!= NULL
) {
185 ctx
->init_ctx_cb(ctx
, ttyctx
);
186 if (ttyctx
->palette
!= NULL
) {
187 if (ttyctx
->defaults
.fg
== 8)
188 ttyctx
->defaults
.fg
= ttyctx
->palette
->fg
;
189 if (ttyctx
->defaults
.bg
== 8)
190 ttyctx
->defaults
.bg
= ttyctx
->palette
->bg
;
193 ttyctx
->redraw_cb
= screen_write_redraw_cb
;
194 if (ctx
->wp
!= NULL
) {
195 tty_default_colours(&ttyctx
->defaults
, ctx
->wp
);
196 ttyctx
->palette
= &ctx
->wp
->palette
;
197 ttyctx
->set_client_cb
= screen_write_set_client_cb
;
198 ttyctx
->arg
= ctx
->wp
;
202 if (~ctx
->flags
& SCREEN_WRITE_SYNC
) {
204 * For the active pane or for an overlay (no pane), we want to
205 * only use synchronized updates if requested (commands that
206 * move the cursor); for other panes, always use it, since the
207 * cursor will have to move.
209 if (ctx
->wp
!= NULL
) {
210 if (ctx
->wp
!= ctx
->wp
->window
->active
)
215 ttyctx
->num
= 0x10|sync
;
216 tty_write(tty_cmd_syncstart
, ttyctx
);
217 ctx
->flags
|= SCREEN_WRITE_SYNC
;
221 /* Make write list. */
223 screen_write_make_list(struct screen
*s
)
227 s
->write_list
= xcalloc(screen_size_y(s
), sizeof *s
->write_list
);
228 for (y
= 0; y
< screen_size_y(s
); y
++)
229 TAILQ_INIT(&s
->write_list
[y
].items
);
232 /* Free write list. */
234 screen_write_free_list(struct screen
*s
)
238 for (y
= 0; y
< screen_size_y(s
); y
++)
239 free(s
->write_list
[y
].data
);
243 /* Set up for writing. */
245 screen_write_init(struct screen_write_ctx
*ctx
, struct screen
*s
)
247 memset(ctx
, 0, sizeof *ctx
);
251 if (ctx
->s
->write_list
== NULL
)
252 screen_write_make_list(ctx
->s
);
253 ctx
->item
= screen_write_get_citem();
259 /* Initialize writing with a pane. */
261 screen_write_start_pane(struct screen_write_ctx
*ctx
, struct window_pane
*wp
,
266 screen_write_init(ctx
, s
);
269 if (log_get_level() != 0) {
270 log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
271 __func__
, screen_size_x(ctx
->s
), screen_size_y(ctx
->s
),
272 wp
->id
, wp
->xoff
, wp
->yoff
);
276 /* Initialize writing with a callback. */
278 screen_write_start_callback(struct screen_write_ctx
*ctx
, struct screen
*s
,
279 screen_write_init_ctx_cb cb
, void *arg
)
281 screen_write_init(ctx
, s
);
283 ctx
->init_ctx_cb
= cb
;
286 if (log_get_level() != 0) {
287 log_debug("%s: size %ux%u, with callback", __func__
,
288 screen_size_x(ctx
->s
), screen_size_y(ctx
->s
));
292 /* Initialize writing. */
294 screen_write_start(struct screen_write_ctx
*ctx
, struct screen
*s
)
296 screen_write_init(ctx
, s
);
298 if (log_get_level() != 0) {
299 log_debug("%s: size %ux%u, no pane", __func__
,
300 screen_size_x(ctx
->s
), screen_size_y(ctx
->s
));
304 /* Finish writing. */
306 screen_write_stop(struct screen_write_ctx
*ctx
)
308 screen_write_collect_end(ctx
);
309 screen_write_collect_flush(ctx
, 0, __func__
);
311 screen_write_free_citem(ctx
->item
);
314 /* Reset screen state. */
316 screen_write_reset(struct screen_write_ctx
*ctx
)
318 struct screen
*s
= ctx
->s
;
320 screen_reset_tabs(s
);
321 screen_write_scrollregion(ctx
, 0, screen_size_y(s
) - 1);
323 s
->mode
= MODE_CURSOR
| MODE_WRAP
;
325 screen_write_clearscreen(ctx
, 8);
326 screen_write_set_cursor(ctx
, 0, 0);
329 /* Write character. */
331 screen_write_putc(struct screen_write_ctx
*ctx
, const struct grid_cell
*gcp
,
336 memcpy(&gc
, gcp
, sizeof gc
);
338 utf8_set(&gc
.data
, ch
);
339 screen_write_cell(ctx
, &gc
);
342 /* Calculate string length. */
344 screen_write_strlen(const char *fmt
, ...)
350 size_t left
, size
= 0;
351 enum utf8_state more
;
354 xvasprintf(&msg
, fmt
, ap
);
358 while (*ptr
!= '\0') {
359 if (*ptr
> 0x7f && utf8_open(&ud
, *ptr
) == UTF8_MORE
) {
363 if (left
< (size_t)ud
.size
- 1)
365 while ((more
= utf8_append(&ud
, *ptr
)) == UTF8_MORE
)
369 if (more
== UTF8_DONE
)
372 if (*ptr
> 0x1f && *ptr
< 0x7f)
382 /* Write string wrapped over lines. */
384 screen_write_text(struct screen_write_ctx
*ctx
, u_int cx
, u_int width
,
385 u_int lines
, int more
, const struct grid_cell
*gcp
, const char *fmt
, ...)
387 struct screen
*s
= ctx
->s
;
390 u_int cy
= s
->cy
, i
, end
, next
, idx
= 0, at
, left
;
391 struct utf8_data
*text
;
394 memcpy(&gc
, gcp
, sizeof gc
);
397 xvasprintf(&tmp
, fmt
, ap
);
400 text
= utf8_fromcstr(tmp
);
403 left
= (cx
+ width
) - s
->cx
;
405 /* Find the end of what can fit on the line. */
407 for (end
= idx
; text
[end
].size
!= 0; end
++) {
408 if (text
[end
].size
== 1 && text
[end
].data
[0] == '\n')
410 if (at
+ text
[end
].width
> left
)
412 at
+= text
[end
].width
;
416 * If we're on a space, that's the end. If not, walk back to
419 if (text
[end
].size
== 0)
421 else if (text
[end
].size
== 1 && text
[end
].data
[0] == '\n')
423 else if (text
[end
].size
== 1 && text
[end
].data
[0] == ' ')
426 for (i
= end
; i
> idx
; i
--) {
427 if (text
[i
].size
== 1 && text
[i
].data
[0] == ' ')
437 /* Print the line. */
438 for (i
= idx
; i
< end
; i
++) {
439 utf8_copy(&gc
.data
, &text
[i
]);
440 screen_write_cell(ctx
, &gc
);
443 /* If at the bottom, stop. */
445 if (s
->cy
== cy
+ lines
- 1 || text
[idx
].size
== 0)
448 screen_write_cursormove(ctx
, cx
, s
->cy
+ 1, 0);
453 * Fail if on the last line and there is more to come or at the end, or
454 * if the text was not entirely consumed.
456 if ((s
->cy
== cy
+ lines
- 1 && (!more
|| s
->cx
== cx
+ width
)) ||
457 text
[idx
].size
!= 0) {
464 * If no more to come, move to the next line. Otherwise, leave on
465 * the same line (except if at the end).
467 if (!more
|| s
->cx
== cx
+ width
)
468 screen_write_cursormove(ctx
, cx
, s
->cy
+ 1, 0);
472 /* Write simple string (no maximum length). */
474 screen_write_puts(struct screen_write_ctx
*ctx
, const struct grid_cell
*gcp
,
475 const char *fmt
, ...)
480 screen_write_vnputs(ctx
, -1, gcp
, fmt
, ap
);
484 /* Write string with length limit (-1 for unlimited). */
486 screen_write_nputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
487 const struct grid_cell
*gcp
, const char *fmt
, ...)
492 screen_write_vnputs(ctx
, maxlen
, gcp
, fmt
, ap
);
497 screen_write_vnputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
498 const struct grid_cell
*gcp
, const char *fmt
, va_list ap
)
501 struct utf8_data
*ud
= &gc
.data
;
504 size_t left
, size
= 0;
505 enum utf8_state more
;
507 memcpy(&gc
, gcp
, sizeof gc
);
508 xvasprintf(&msg
, fmt
, ap
);
511 while (*ptr
!= '\0') {
512 if (*ptr
> 0x7f && utf8_open(ud
, *ptr
) == UTF8_MORE
) {
516 if (left
< (size_t)ud
->size
- 1)
518 while ((more
= utf8_append(ud
, *ptr
)) == UTF8_MORE
)
522 if (more
!= UTF8_DONE
)
524 if (maxlen
> 0 && size
+ ud
->width
> (size_t)maxlen
) {
525 while (size
< (size_t)maxlen
) {
526 screen_write_putc(ctx
, &gc
, ' ');
532 screen_write_cell(ctx
, &gc
);
534 if (maxlen
> 0 && size
+ 1 > (size_t)maxlen
)
538 gc
.attr
^= GRID_ATTR_CHARSET
;
539 else if (*ptr
== '\n') {
540 screen_write_linefeed(ctx
, 0, 8);
541 screen_write_carriagereturn(ctx
);
542 } else if (*ptr
> 0x1f && *ptr
< 0x7f) {
544 screen_write_putc(ctx
, &gc
, *ptr
);
554 * Copy from another screen but without the selection stuff. Assumes the target
555 * region is already big enough.
558 screen_write_fast_copy(struct screen_write_ctx
*ctx
, struct screen
*src
,
559 u_int px
, u_int py
, u_int nx
, u_int ny
)
561 struct screen
*s
= ctx
->s
;
562 struct grid
*gd
= src
->grid
;
564 u_int xx
, yy
, cx
, cy
;
566 if (nx
== 0 || ny
== 0)
570 for (yy
= py
; yy
< py
+ ny
; yy
++) {
571 if (yy
>= gd
->hsize
+ gd
->sy
)
574 for (xx
= px
; xx
< px
+ nx
; xx
++) {
575 if (xx
>= grid_get_line(gd
, yy
)->cellsize
)
577 grid_get_cell(gd
, xx
, yy
, &gc
);
578 if (xx
+ gc
.data
.width
> px
+ nx
)
580 grid_view_set_cell(ctx
->s
->grid
, cx
, cy
, &gc
);
587 /* Draw a horizontal line on screen. */
589 screen_write_hline(struct screen_write_ctx
*ctx
, u_int nx
, int left
, int right
)
591 struct screen
*s
= ctx
->s
;
598 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
599 gc
.attr
|= GRID_ATTR_CHARSET
;
601 screen_write_putc(ctx
, &gc
, left
? 't' : 'q');
602 for (i
= 1; i
< nx
- 1; i
++)
603 screen_write_putc(ctx
, &gc
, 'q');
604 screen_write_putc(ctx
, &gc
, right
? 'u' : 'q');
606 screen_write_set_cursor(ctx
, cx
, cy
);
609 /* Draw a vertical line on screen. */
611 screen_write_vline(struct screen_write_ctx
*ctx
, u_int ny
, int top
, int bottom
)
613 struct screen
*s
= ctx
->s
;
620 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
621 gc
.attr
|= GRID_ATTR_CHARSET
;
623 screen_write_putc(ctx
, &gc
, top
? 'w' : 'x');
624 for (i
= 1; i
< ny
- 1; i
++) {
625 screen_write_set_cursor(ctx
, cx
, cy
+ i
);
626 screen_write_putc(ctx
, &gc
, 'x');
628 screen_write_set_cursor(ctx
, cx
, cy
+ ny
- 1);
629 screen_write_putc(ctx
, &gc
, bottom
? 'v' : 'x');
631 screen_write_set_cursor(ctx
, cx
, cy
);
634 /* Draw a menu on screen. */
636 screen_write_menu(struct screen_write_ctx
*ctx
, struct menu
*menu
,
637 int choice
, const struct grid_cell
*choice_gc
)
639 struct screen
*s
= ctx
->s
;
640 struct grid_cell default_gc
;
641 const struct grid_cell
*gc
= &default_gc
;
648 memcpy(&default_gc
, &grid_default_cell
, sizeof default_gc
);
650 screen_write_box(ctx
, menu
->width
+ 4, menu
->count
+ 2,
651 BOX_LINES_DEFAULT
, &default_gc
, menu
->title
);
653 for (i
= 0; i
< menu
->count
; i
++) {
654 name
= menu
->items
[i
].name
;
656 screen_write_cursormove(ctx
, cx
, cy
+ 1 + i
, 0);
657 screen_write_hline(ctx
, menu
->width
+ 4, 1, 1);
659 if (choice
>= 0 && i
== (u_int
)choice
&& *name
!= '-')
661 screen_write_cursormove(ctx
, cx
+ 2, cy
+ 1 + i
, 0);
662 for (j
= 0; j
< menu
->width
; j
++)
663 screen_write_putc(ctx
, gc
, ' ');
664 screen_write_cursormove(ctx
, cx
+ 2, cy
+ 1 + i
, 0);
667 default_gc
.attr
|= GRID_ATTR_DIM
;
668 format_draw(ctx
, gc
, menu
->width
, name
, NULL
,
670 default_gc
.attr
&= ~GRID_ATTR_DIM
;
672 format_draw(ctx
, gc
, menu
->width
, name
, NULL
,
678 screen_write_set_cursor(ctx
, cx
, cy
);
682 screen_write_box_border_set(enum box_lines box_lines
, int cell_type
,
683 struct grid_cell
*gc
)
688 case BOX_LINES_DOUBLE
:
689 gc
->attr
&= ~GRID_ATTR_CHARSET
;
690 utf8_copy(&gc
->data
, tty_acs_double_borders(cell_type
));
692 case BOX_LINES_HEAVY
:
693 gc
->attr
&= ~GRID_ATTR_CHARSET
;
694 utf8_copy(&gc
->data
, tty_acs_heavy_borders(cell_type
));
696 case BOX_LINES_ROUNDED
:
697 gc
->attr
&= ~GRID_ATTR_CHARSET
;
698 utf8_copy(&gc
->data
, tty_acs_rounded_borders(cell_type
));
700 case BOX_LINES_SIMPLE
:
701 gc
->attr
&= ~GRID_ATTR_CHARSET
;
702 utf8_set(&gc
->data
, SIMPLE_BORDERS
[cell_type
]);
704 case BOX_LINES_PADDED
:
705 gc
->attr
&= ~GRID_ATTR_CHARSET
;
706 utf8_set(&gc
->data
, PADDED_BORDERS
[cell_type
]);
708 case BOX_LINES_SINGLE
:
709 case BOX_LINES_DEFAULT
:
710 gc
->attr
|= GRID_ATTR_CHARSET
;
711 utf8_set(&gc
->data
, CELL_BORDERS
[cell_type
]);
716 /* Draw a box on screen. */
718 screen_write_box(struct screen_write_ctx
*ctx
, u_int nx
, u_int ny
,
719 enum box_lines lines
, const struct grid_cell
*gcp
, const char *title
)
721 struct screen
*s
= ctx
->s
;
729 memcpy(&gc
, gcp
, sizeof gc
);
731 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
733 gc
.attr
|= GRID_ATTR_CHARSET
;
734 gc
.flags
|= GRID_FLAG_NOPALETTE
;
736 /* Draw top border */
737 screen_write_box_border_set(lines
, CELL_TOPLEFT
, &gc
);
738 screen_write_cell(ctx
, &gc
);
739 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
740 for (i
= 1; i
< nx
- 1; i
++)
741 screen_write_cell(ctx
, &gc
);
742 screen_write_box_border_set(lines
, CELL_TOPRIGHT
, &gc
);
743 screen_write_cell(ctx
, &gc
);
745 /* Draw bottom border */
746 screen_write_set_cursor(ctx
, cx
, cy
+ ny
- 1);
747 screen_write_box_border_set(lines
, CELL_BOTTOMLEFT
, &gc
);
748 screen_write_cell(ctx
, &gc
);
749 screen_write_box_border_set(lines
, CELL_LEFTRIGHT
, &gc
);
750 for (i
= 1; i
< nx
- 1; i
++)
751 screen_write_cell(ctx
, &gc
);
752 screen_write_box_border_set(lines
, CELL_BOTTOMRIGHT
, &gc
);
753 screen_write_cell(ctx
, &gc
);
756 screen_write_box_border_set(lines
, CELL_TOPBOTTOM
, &gc
);
757 for (i
= 1; i
< ny
- 1; i
++) {
759 screen_write_set_cursor(ctx
, cx
, cy
+ i
);
760 screen_write_cell(ctx
, &gc
);
762 screen_write_set_cursor(ctx
, cx
+ nx
- 1, cy
+ i
);
763 screen_write_cell(ctx
, &gc
);
767 gc
.attr
&= ~GRID_ATTR_CHARSET
;
768 screen_write_cursormove(ctx
, cx
+ 2, cy
, 0);
769 format_draw(ctx
, &gc
, nx
- 4, title
, NULL
, 0);
772 screen_write_set_cursor(ctx
, cx
, cy
);
776 * Write a preview version of a window. Assumes target area is big enough and
780 screen_write_preview(struct screen_write_ctx
*ctx
, struct screen
*src
, u_int nx
,
783 struct screen
*s
= ctx
->s
;
785 u_int cx
, cy
, px
, py
;
791 * If the cursor is on, pick the area around the cursor, otherwise use
794 if (src
->mode
& MODE_CURSOR
) {
800 if (px
+ nx
> screen_size_x(src
)) {
801 if (nx
> screen_size_x(src
))
804 px
= screen_size_x(src
) - nx
;
811 if (py
+ ny
> screen_size_y(src
)) {
812 if (ny
> screen_size_y(src
))
815 py
= screen_size_y(src
) - ny
;
822 screen_write_fast_copy(ctx
, src
, px
, src
->grid
->hsize
+ py
, nx
, ny
);
824 if (src
->mode
& MODE_CURSOR
) {
825 grid_view_get_cell(src
->grid
, src
->cx
, src
->cy
, &gc
);
826 gc
.attr
|= GRID_ATTR_REVERSE
;
827 screen_write_set_cursor(ctx
, cx
+ (src
->cx
- px
),
828 cy
+ (src
->cy
- py
));
829 screen_write_cell(ctx
, &gc
);
835 screen_write_mode_set(struct screen_write_ctx
*ctx
, int mode
)
837 struct screen
*s
= ctx
->s
;
841 if (log_get_level() != 0)
842 log_debug("%s: %s", __func__
, screen_mode_to_string(mode
));
847 screen_write_mode_clear(struct screen_write_ctx
*ctx
, int mode
)
849 struct screen
*s
= ctx
->s
;
853 if (log_get_level() != 0)
854 log_debug("%s: %s", __func__
, screen_mode_to_string(mode
));
857 /* Cursor up by ny. */
859 screen_write_cursorup(struct screen_write_ctx
*ctx
, u_int ny
)
861 struct screen
*s
= ctx
->s
;
862 u_int cx
= s
->cx
, cy
= s
->cy
;
867 if (cy
< s
->rupper
) {
873 if (ny
> cy
- s
->rupper
)
876 if (cx
== screen_size_x(s
))
881 screen_write_set_cursor(ctx
, cx
, cy
);
884 /* Cursor down by ny. */
886 screen_write_cursordown(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
->rlower
) {
896 if (ny
> screen_size_y(s
) - 1 - cy
)
897 ny
= screen_size_y(s
) - 1 - cy
;
900 if (ny
> s
->rlower
- cy
)
903 if (cx
== screen_size_x(s
))
910 screen_write_set_cursor(ctx
, cx
, cy
);
913 /* Cursor right by nx. */
915 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
917 struct screen
*s
= ctx
->s
;
918 u_int cx
= s
->cx
, cy
= s
->cy
;
923 if (nx
> screen_size_x(s
) - 1 - cx
)
924 nx
= screen_size_x(s
) - 1 - cx
;
930 screen_write_set_cursor(ctx
, cx
, cy
);
933 /* Cursor left by nx. */
935 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
937 struct screen
*s
= ctx
->s
;
938 u_int cx
= s
->cx
, cy
= s
->cy
;
950 screen_write_set_cursor(ctx
, cx
, cy
);
953 /* Backspace; cursor left unless at start of wrapped line when can move up. */
955 screen_write_backspace(struct screen_write_ctx
*ctx
)
957 struct screen
*s
= ctx
->s
;
958 struct grid_line
*gl
;
959 u_int cx
= s
->cx
, cy
= s
->cy
;
964 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ cy
- 1);
965 if (gl
->flags
& GRID_LINE_WRAPPED
) {
967 cx
= screen_size_x(s
) - 1;
972 screen_write_set_cursor(ctx
, cx
, cy
);
975 /* VT100 alignment test. */
977 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
979 struct screen
*s
= ctx
->s
;
980 struct tty_ctx ttyctx
;
984 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
985 utf8_set(&gc
.data
, 'E');
987 for (yy
= 0; yy
< screen_size_y(s
); yy
++) {
988 for (xx
= 0; xx
< screen_size_x(s
); xx
++)
989 grid_view_set_cell(s
->grid
, xx
, yy
, &gc
);
992 screen_write_set_cursor(ctx
, 0, 0);
995 s
->rlower
= screen_size_y(s
) - 1;
997 screen_write_initctx(ctx
, &ttyctx
, 1);
999 screen_write_collect_clear(ctx
, 0, screen_size_y(s
) - 1);
1000 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
1003 /* Insert nx characters. */
1005 screen_write_insertcharacter(struct screen_write_ctx
*ctx
, u_int nx
, u_int bg
)
1007 struct screen
*s
= ctx
->s
;
1008 struct tty_ctx ttyctx
;
1013 if (nx
> screen_size_x(s
) - s
->cx
)
1014 nx
= screen_size_x(s
) - s
->cx
;
1018 if (s
->cx
> screen_size_x(s
) - 1)
1021 screen_write_initctx(ctx
, &ttyctx
, 0);
1024 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
, bg
);
1026 screen_write_collect_flush(ctx
, 0, __func__
);
1028 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1031 /* Delete nx characters. */
1033 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
, u_int bg
)
1035 struct screen
*s
= ctx
->s
;
1036 struct tty_ctx ttyctx
;
1041 if (nx
> screen_size_x(s
) - s
->cx
)
1042 nx
= screen_size_x(s
) - s
->cx
;
1046 if (s
->cx
> screen_size_x(s
) - 1)
1049 screen_write_initctx(ctx
, &ttyctx
, 0);
1052 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
, bg
);
1054 screen_write_collect_flush(ctx
, 0, __func__
);
1056 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
1059 /* Clear nx characters. */
1061 screen_write_clearcharacter(struct screen_write_ctx
*ctx
, u_int nx
, u_int bg
)
1063 struct screen
*s
= ctx
->s
;
1064 struct tty_ctx ttyctx
;
1069 if (nx
> screen_size_x(s
) - s
->cx
)
1070 nx
= screen_size_x(s
) - s
->cx
;
1074 if (s
->cx
> screen_size_x(s
) - 1)
1077 screen_write_initctx(ctx
, &ttyctx
, 0);
1080 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, nx
, 1, bg
);
1082 screen_write_collect_flush(ctx
, 0, __func__
);
1084 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
1087 /* Insert ny lines. */
1089 screen_write_insertline(struct screen_write_ctx
*ctx
, u_int ny
, u_int bg
)
1091 struct screen
*s
= ctx
->s
;
1092 struct grid
*gd
= s
->grid
;
1093 struct tty_ctx ttyctx
;
1098 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
1099 if (ny
> screen_size_y(s
) - s
->cy
)
1100 ny
= screen_size_y(s
) - s
->cy
;
1104 screen_write_initctx(ctx
, &ttyctx
, 1);
1107 grid_view_insert_lines(gd
, s
->cy
, ny
, bg
);
1109 screen_write_collect_flush(ctx
, 0, __func__
);
1111 tty_write(tty_cmd_insertline
, &ttyctx
);
1115 if (ny
> s
->rlower
+ 1 - s
->cy
)
1116 ny
= s
->rlower
+ 1 - s
->cy
;
1120 screen_write_initctx(ctx
, &ttyctx
, 1);
1123 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
1124 grid_view_insert_lines(gd
, s
->cy
, ny
, bg
);
1126 grid_view_insert_lines_region(gd
, s
->rlower
, s
->cy
, ny
, bg
);
1128 screen_write_collect_flush(ctx
, 0, __func__
);
1131 tty_write(tty_cmd_insertline
, &ttyctx
);
1134 /* Delete ny lines. */
1136 screen_write_deleteline(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
;
1145 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
1146 if (ny
> screen_size_y(s
) - s
->cy
)
1147 ny
= screen_size_y(s
) - s
->cy
;
1151 screen_write_initctx(ctx
, &ttyctx
, 1);
1154 grid_view_delete_lines(gd
, s
->cy
, ny
, bg
);
1156 screen_write_collect_flush(ctx
, 0, __func__
);
1158 tty_write(tty_cmd_deleteline
, &ttyctx
);
1162 if (ny
> s
->rlower
+ 1 - s
->cy
)
1163 ny
= s
->rlower
+ 1 - s
->cy
;
1167 screen_write_initctx(ctx
, &ttyctx
, 1);
1170 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
1171 grid_view_delete_lines(gd
, s
->cy
, ny
, bg
);
1173 grid_view_delete_lines_region(gd
, s
->rlower
, s
->cy
, ny
, bg
);
1175 screen_write_collect_flush(ctx
, 0, __func__
);
1177 tty_write(tty_cmd_deleteline
, &ttyctx
);
1180 /* Clear line at cursor. */
1182 screen_write_clearline(struct screen_write_ctx
*ctx
, u_int bg
)
1184 struct screen
*s
= ctx
->s
;
1185 struct grid_line
*gl
;
1186 u_int sx
= screen_size_x(s
);
1187 struct screen_write_citem
*ci
= ctx
->item
;
1189 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ s
->cy
);
1190 if (gl
->cellsize
== 0 && COLOUR_DEFAULT(bg
))
1193 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1, bg
);
1195 screen_write_collect_clear(ctx
, s
->cy
, 1);
1200 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->cy
].items
, ci
, entry
);
1201 ctx
->item
= screen_write_get_citem();
1204 /* Clear to end of line from cursor. */
1206 screen_write_clearendofline(struct screen_write_ctx
*ctx
, u_int bg
)
1208 struct screen
*s
= ctx
->s
;
1209 struct grid_line
*gl
;
1210 u_int sx
= screen_size_x(s
);
1211 struct screen_write_citem
*ci
= ctx
->item
, *before
;
1214 screen_write_clearline(ctx
, bg
);
1218 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ s
->cy
);
1219 if (s
->cx
> sx
- 1 || (s
->cx
>= gl
->cellsize
&& COLOUR_DEFAULT(bg
)))
1222 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1, bg
);
1224 before
= screen_write_collect_trim(ctx
, s
->cy
, s
->cx
, sx
- s
->cx
, NULL
);
1226 ci
->used
= sx
- s
->cx
;
1230 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->cy
].items
, ci
, entry
);
1232 TAILQ_INSERT_BEFORE(before
, ci
, entry
);
1233 ctx
->item
= screen_write_get_citem();
1236 /* Clear to start of line from cursor. */
1238 screen_write_clearstartofline(struct screen_write_ctx
*ctx
, u_int bg
)
1240 struct screen
*s
= ctx
->s
;
1241 u_int sx
= screen_size_x(s
);
1242 struct screen_write_citem
*ci
= ctx
->item
, *before
;
1244 if (s
->cx
>= sx
- 1) {
1245 screen_write_clearline(ctx
, bg
);
1250 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1, bg
);
1252 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1, bg
);
1254 before
= screen_write_collect_trim(ctx
, s
->cy
, 0, s
->cx
+ 1, NULL
);
1256 ci
->used
= s
->cx
+ 1;
1260 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->cy
].items
, ci
, entry
);
1262 TAILQ_INSERT_BEFORE(before
, ci
, entry
);
1263 ctx
->item
= screen_write_get_citem();
1266 /* Move cursor to px,py. */
1268 screen_write_cursormove(struct screen_write_ctx
*ctx
, int px
, int py
,
1271 struct screen
*s
= ctx
->s
;
1273 if (origin
&& py
!= -1 && (s
->mode
& MODE_ORIGIN
)) {
1274 if ((u_int
)py
> s
->rlower
- s
->rupper
)
1280 if (px
!= -1 && (u_int
)px
> screen_size_x(s
) - 1)
1281 px
= screen_size_x(s
) - 1;
1282 if (py
!= -1 && (u_int
)py
> screen_size_y(s
) - 1)
1283 py
= screen_size_y(s
) - 1;
1285 log_debug("%s: from %u,%u to %u,%u", __func__
, s
->cx
, s
->cy
, px
, py
);
1286 screen_write_set_cursor(ctx
, px
, py
);
1289 /* Reverse index (up with scroll). */
1291 screen_write_reverseindex(struct screen_write_ctx
*ctx
, u_int bg
)
1293 struct screen
*s
= ctx
->s
;
1294 struct tty_ctx ttyctx
;
1296 if (s
->cy
== s
->rupper
) {
1297 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
, bg
);
1298 screen_write_collect_flush(ctx
, 0, __func__
);
1300 screen_write_initctx(ctx
, &ttyctx
, 1);
1303 tty_write(tty_cmd_reverseindex
, &ttyctx
);
1304 } else if (s
->cy
> 0)
1305 screen_write_set_cursor(ctx
, -1, s
->cy
- 1);
1309 /* Set scroll region. */
1311 screen_write_scrollregion(struct screen_write_ctx
*ctx
, u_int rupper
,
1314 struct screen
*s
= ctx
->s
;
1316 if (rupper
> screen_size_y(s
) - 1)
1317 rupper
= screen_size_y(s
) - 1;
1318 if (rlower
> screen_size_y(s
) - 1)
1319 rlower
= screen_size_y(s
) - 1;
1320 if (rupper
>= rlower
) /* cannot be one line */
1323 screen_write_collect_flush(ctx
, 0, __func__
);
1325 /* Cursor moves to top-left. */
1326 screen_write_set_cursor(ctx
, 0, 0);
1334 screen_write_linefeed(struct screen_write_ctx
*ctx
, int wrapped
, u_int bg
)
1336 struct screen
*s
= ctx
->s
;
1337 struct grid
*gd
= s
->grid
;
1338 struct grid_line
*gl
;
1340 gl
= grid_get_line(gd
, gd
->hsize
+ s
->cy
);
1342 gl
->flags
|= GRID_LINE_WRAPPED
;
1344 log_debug("%s: at %u,%u (region %u-%u)", __func__
, s
->cx
, s
->cy
,
1345 s
->rupper
, s
->rlower
);
1347 if (bg
!= ctx
->bg
) {
1348 screen_write_collect_flush(ctx
, 1, __func__
);
1352 if (s
->cy
== s
->rlower
) {
1353 grid_view_scroll_region_up(gd
, s
->rupper
, s
->rlower
, bg
);
1354 screen_write_collect_scroll(ctx
, bg
);
1356 } else if (s
->cy
< screen_size_y(s
) - 1)
1357 screen_write_set_cursor(ctx
, -1, s
->cy
+ 1);
1362 screen_write_scrollup(struct screen_write_ctx
*ctx
, u_int lines
, u_int bg
)
1364 struct screen
*s
= ctx
->s
;
1365 struct grid
*gd
= s
->grid
;
1370 else if (lines
> s
->rlower
- s
->rupper
+ 1)
1371 lines
= s
->rlower
- s
->rupper
+ 1;
1373 if (bg
!= ctx
->bg
) {
1374 screen_write_collect_flush(ctx
, 1, __func__
);
1378 for (i
= 0; i
< lines
; i
++) {
1379 grid_view_scroll_region_up(gd
, s
->rupper
, s
->rlower
, bg
);
1380 screen_write_collect_scroll(ctx
, bg
);
1382 ctx
->scrolled
+= lines
;
1387 screen_write_scrolldown(struct screen_write_ctx
*ctx
, u_int lines
, u_int bg
)
1389 struct screen
*s
= ctx
->s
;
1390 struct grid
*gd
= s
->grid
;
1391 struct tty_ctx ttyctx
;
1394 screen_write_initctx(ctx
, &ttyctx
, 1);
1399 else if (lines
> s
->rlower
- s
->rupper
+ 1)
1400 lines
= s
->rlower
- s
->rupper
+ 1;
1402 for (i
= 0; i
< lines
; i
++)
1403 grid_view_scroll_region_down(gd
, s
->rupper
, s
->rlower
, bg
);
1405 screen_write_collect_flush(ctx
, 0, __func__
);
1407 tty_write(tty_cmd_scrolldown
, &ttyctx
);
1410 /* Carriage return (cursor to start of line). */
1412 screen_write_carriagereturn(struct screen_write_ctx
*ctx
)
1414 screen_write_set_cursor(ctx
, 0, -1);
1417 /* Clear to end of screen from cursor. */
1419 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
, u_int bg
)
1421 struct screen
*s
= ctx
->s
;
1422 struct grid
*gd
= s
->grid
;
1423 struct tty_ctx ttyctx
;
1424 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
1426 screen_write_initctx(ctx
, &ttyctx
, 1);
1429 /* Scroll into history if it is enabled and clearing entire screen. */
1432 (gd
->flags
& GRID_HISTORY
) &&
1434 options_get_number(ctx
->wp
->options
, "scroll-on-clear"))
1435 grid_view_clear_history(gd
, bg
);
1437 if (s
->cx
<= sx
- 1)
1438 grid_view_clear(gd
, s
->cx
, s
->cy
, sx
- s
->cx
, 1, bg
);
1439 grid_view_clear(gd
, 0, s
->cy
+ 1, sx
, sy
- (s
->cy
+ 1), bg
);
1442 screen_write_collect_clear(ctx
, s
->cy
+ 1, sy
- (s
->cy
+ 1));
1443 screen_write_collect_flush(ctx
, 0, __func__
);
1444 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
1447 /* Clear to start of screen. */
1449 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
, u_int bg
)
1451 struct screen
*s
= ctx
->s
;
1452 struct tty_ctx ttyctx
;
1453 u_int sx
= screen_size_x(s
);
1455 screen_write_initctx(ctx
, &ttyctx
, 1);
1459 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
, bg
);
1461 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1, bg
);
1463 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1, bg
);
1465 screen_write_collect_clear(ctx
, 0, s
->cy
);
1466 screen_write_collect_flush(ctx
, 0, __func__
);
1467 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
1470 /* Clear entire screen. */
1472 screen_write_clearscreen(struct screen_write_ctx
*ctx
, u_int bg
)
1474 struct screen
*s
= ctx
->s
;
1475 struct tty_ctx ttyctx
;
1476 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
1478 screen_write_initctx(ctx
, &ttyctx
, 1);
1481 /* Scroll into history if it is enabled. */
1482 if ((s
->grid
->flags
& GRID_HISTORY
) &&
1484 options_get_number(ctx
->wp
->options
, "scroll-on-clear"))
1485 grid_view_clear_history(s
->grid
, bg
);
1487 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, bg
);
1489 screen_write_collect_clear(ctx
, 0, sy
);
1490 tty_write(tty_cmd_clearscreen
, &ttyctx
);
1493 /* Clear entire history. */
1495 screen_write_clearhistory(struct screen_write_ctx
*ctx
)
1497 grid_clear_history(ctx
->s
->grid
);
1500 /* Force a full redraw. */
1502 screen_write_fullredraw(struct screen_write_ctx
*ctx
)
1504 struct tty_ctx ttyctx
;
1506 screen_write_collect_flush(ctx
, 0, __func__
);
1508 screen_write_initctx(ctx
, &ttyctx
, 1);
1509 if (ttyctx
.redraw_cb
!= NULL
)
1510 ttyctx
.redraw_cb(&ttyctx
);
1513 /* Trim collected items. */
1514 static struct screen_write_citem
*
1515 screen_write_collect_trim(struct screen_write_ctx
*ctx
, u_int y
, u_int x
,
1516 u_int used
, int *wrapped
)
1518 struct screen_write_cline
*cl
= &ctx
->s
->write_list
[y
];
1519 struct screen_write_citem
*ci
, *ci2
, *tmp
, *before
= NULL
;
1520 u_int sx
= x
, ex
= x
+ used
- 1;
1523 if (TAILQ_EMPTY(&cl
->items
))
1525 TAILQ_FOREACH_SAFE(ci
, &cl
->items
, entry
, tmp
) {
1527 cex
= ci
->x
+ ci
->used
- 1;
1529 /* Item is entirely before. */
1531 log_debug("%s: %p %u-%u before %u-%u", __func__
, ci
,
1536 /* Item is entirely after. */
1538 log_debug("%s: %p %u-%u after %u-%u", __func__
, ci
,
1544 /* Item is entirely inside. */
1545 if (csx
>= sx
&& cex
<= ex
) {
1546 log_debug("%s: %p %u-%u inside %u-%u", __func__
, ci
,
1548 TAILQ_REMOVE(&cl
->items
, ci
, entry
);
1549 screen_write_free_citem(ci
);
1550 if (csx
== 0 && ci
->wrapped
&& wrapped
!= NULL
)
1555 /* Item under the start. */
1556 if (csx
< sx
&& cex
>= sx
&& cex
<= ex
) {
1557 log_debug("%s: %p %u-%u start %u-%u", __func__
, ci
,
1559 ci
->used
= sx
- csx
;
1560 log_debug("%s: %p now %u-%u", __func__
, ci
, ci
->x
,
1561 ci
->x
+ ci
->used
+ 1);
1565 /* Item covers the end. */
1566 if (cex
> ex
&& csx
>= sx
&& csx
<= ex
) {
1567 log_debug("%s: %p %u-%u end %u-%u", __func__
, ci
,
1570 ci
->used
= cex
- ex
;
1571 log_debug("%s: %p now %u-%u", __func__
, ci
, ci
->x
,
1572 ci
->x
+ ci
->used
+ 1);
1577 /* Item must cover both sides. */
1578 log_debug("%s: %p %u-%u under %u-%u", __func__
, ci
,
1580 ci2
= screen_write_get_citem();
1581 ci2
->type
= ci
->type
;
1583 memcpy(&ci2
->gc
, &ci
->gc
, sizeof ci2
->gc
);
1584 TAILQ_INSERT_AFTER(&cl
->items
, ci
, ci2
, entry
);
1586 ci
->used
= sx
- csx
;
1588 ci2
->used
= cex
- ex
;
1590 log_debug("%s: %p now %u-%u (%p) and %u-%u (%p)", __func__
, ci
,
1591 ci
->x
, ci
->x
+ ci
->used
- 1, ci
, ci2
->x
,
1592 ci2
->x
+ ci2
->used
- 1, ci2
);
1599 /* Clear collected lines. */
1601 screen_write_collect_clear(struct screen_write_ctx
*ctx
, u_int y
, u_int n
)
1603 struct screen_write_cline
*cl
;
1606 for (i
= y
; i
< y
+ n
; i
++) {
1607 cl
= &ctx
->s
->write_list
[i
];
1608 TAILQ_CONCAT(&screen_write_citem_freelist
, &cl
->items
, entry
);
1612 /* Scroll collected lines up. */
1614 screen_write_collect_scroll(struct screen_write_ctx
*ctx
, u_int bg
)
1616 struct screen
*s
= ctx
->s
;
1617 struct screen_write_cline
*cl
;
1620 struct screen_write_citem
*ci
;
1622 log_debug("%s: at %u,%u (region %u-%u)", __func__
, s
->cx
, s
->cy
,
1623 s
->rupper
, s
->rlower
);
1625 screen_write_collect_clear(ctx
, s
->rupper
, 1);
1626 saved
= ctx
->s
->write_list
[s
->rupper
].data
;
1627 for (y
= s
->rupper
; y
< s
->rlower
; y
++) {
1628 cl
= &ctx
->s
->write_list
[y
+ 1];
1629 TAILQ_CONCAT(&ctx
->s
->write_list
[y
].items
, &cl
->items
, entry
);
1630 ctx
->s
->write_list
[y
].data
= cl
->data
;
1632 ctx
->s
->write_list
[s
->rlower
].data
= saved
;
1634 ci
= screen_write_get_citem();
1636 ci
->used
= screen_size_x(s
);
1639 TAILQ_INSERT_TAIL(&ctx
->s
->write_list
[s
->rlower
].items
, ci
, entry
);
1642 /* Flush collected lines. */
1644 screen_write_collect_flush(struct screen_write_ctx
*ctx
, int scroll_only
,
1647 struct screen
*s
= ctx
->s
;
1648 struct screen_write_citem
*ci
, *tmp
;
1649 struct screen_write_cline
*cl
;
1650 u_int y
, cx
, cy
, last
, items
= 0;
1651 struct tty_ctx ttyctx
;
1653 if (ctx
->scrolled
!= 0) {
1654 log_debug("%s: scrolled %u (region %u-%u)", __func__
,
1655 ctx
->scrolled
, s
->rupper
, s
->rlower
);
1656 if (ctx
->scrolled
> s
->rlower
- s
->rupper
+ 1)
1657 ctx
->scrolled
= s
->rlower
- s
->rupper
+ 1;
1659 screen_write_initctx(ctx
, &ttyctx
, 1);
1660 ttyctx
.num
= ctx
->scrolled
;
1661 ttyctx
.bg
= ctx
->bg
;
1662 tty_write(tty_cmd_scrollup
, &ttyctx
);
1670 cx
= s
->cx
; cy
= s
->cy
;
1671 for (y
= 0; y
< screen_size_y(s
); y
++) {
1672 cl
= &ctx
->s
->write_list
[y
];
1674 TAILQ_FOREACH_SAFE(ci
, &cl
->items
, entry
, tmp
) {
1675 if (last
!= UINT_MAX
&& ci
->x
<= last
) {
1676 fatalx("collect list not in order: %u <= %u",
1679 screen_write_set_cursor(ctx
, ci
->x
, y
);
1680 if (ci
->type
== CLEAR
) {
1681 screen_write_initctx(ctx
, &ttyctx
, 1);
1683 ttyctx
.num
= ci
->used
;
1684 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
1686 screen_write_initctx(ctx
, &ttyctx
, 0);
1687 ttyctx
.cell
= &ci
->gc
;
1688 ttyctx
.wrapped
= ci
->wrapped
;
1689 ttyctx
.ptr
= cl
->data
+ ci
->x
;
1690 ttyctx
.num
= ci
->used
;
1691 tty_write(tty_cmd_cells
, &ttyctx
);
1695 TAILQ_REMOVE(&cl
->items
, ci
, entry
);
1696 screen_write_free_citem(ci
);
1700 s
->cx
= cx
; s
->cy
= cy
;
1702 log_debug("%s: flushed %u items (%s)", __func__
, items
, from
);
1705 /* Finish and store collected cells. */
1707 screen_write_collect_end(struct screen_write_ctx
*ctx
)
1709 struct screen
*s
= ctx
->s
;
1710 struct screen_write_citem
*ci
= ctx
->item
, *before
;
1711 struct screen_write_cline
*cl
= &s
->write_list
[s
->cy
];
1712 struct grid_cell gc
;
1714 int wrapped
= ci
->wrapped
;
1719 before
= screen_write_collect_trim(ctx
, s
->cy
, s
->cx
, ci
->used
,
1722 ci
->wrapped
= wrapped
;
1724 TAILQ_INSERT_TAIL(&cl
->items
, ci
, entry
);
1726 TAILQ_INSERT_BEFORE(before
, ci
, entry
);
1727 ctx
->item
= screen_write_get_citem();
1729 log_debug("%s: %u %.*s (at %u,%u)", __func__
, ci
->used
,
1730 (int)ci
->used
, cl
->data
+ ci
->x
, s
->cx
, s
->cy
);
1733 for (xx
= s
->cx
; xx
> 0; xx
--) {
1734 grid_view_get_cell(s
->grid
, xx
, s
->cy
, &gc
);
1735 if (~gc
.flags
& GRID_FLAG_PADDING
)
1737 grid_view_set_cell(s
->grid
, xx
, s
->cy
,
1738 &grid_default_cell
);
1740 if (gc
.data
.width
> 1) {
1741 grid_view_set_cell(s
->grid
, xx
, s
->cy
,
1742 &grid_default_cell
);
1746 grid_view_set_cells(s
->grid
, s
->cx
, s
->cy
, &ci
->gc
, cl
->data
+ ci
->x
,
1748 screen_write_set_cursor(ctx
, s
->cx
+ ci
->used
, -1);
1750 for (xx
= s
->cx
; xx
< screen_size_x(s
); xx
++) {
1751 grid_view_get_cell(s
->grid
, xx
, s
->cy
, &gc
);
1752 if (~gc
.flags
& GRID_FLAG_PADDING
)
1754 grid_view_set_cell(s
->grid
, xx
, s
->cy
, &grid_default_cell
);
1758 /* Write cell data, collecting if necessary. */
1760 screen_write_collect_add(struct screen_write_ctx
*ctx
,
1761 const struct grid_cell
*gc
)
1763 struct screen
*s
= ctx
->s
;
1764 struct screen_write_citem
*ci
;
1765 u_int sx
= screen_size_x(s
);
1769 * Don't need to check that the attributes and whatnot are still the
1770 * same - input_parse will end the collection when anything that isn't
1771 * a plain character is encountered.
1775 if (gc
->data
.width
!= 1 || gc
->data
.size
!= 1 || *gc
->data
.data
>= 0x7f)
1777 else if (gc
->attr
& GRID_ATTR_CHARSET
)
1779 else if (~s
->mode
& MODE_WRAP
)
1781 else if (s
->mode
& MODE_INSERT
)
1783 else if (s
->sel
!= NULL
)
1786 screen_write_collect_end(ctx
);
1787 screen_write_collect_flush(ctx
, 0, __func__
);
1788 screen_write_cell(ctx
, gc
);
1792 if (s
->cx
> sx
- 1 || ctx
->item
->used
> sx
- 1 - s
->cx
)
1793 screen_write_collect_end(ctx
);
1794 ci
= ctx
->item
; /* may have changed */
1796 if (s
->cx
> sx
- 1) {
1797 log_debug("%s: wrapped at %u,%u", __func__
, s
->cx
, s
->cy
);
1799 screen_write_linefeed(ctx
, 1, 8);
1800 screen_write_set_cursor(ctx
, 0, -1);
1804 memcpy(&ci
->gc
, gc
, sizeof ci
->gc
);
1805 if (ctx
->s
->write_list
[s
->cy
].data
== NULL
)
1806 ctx
->s
->write_list
[s
->cy
].data
= xmalloc(screen_size_x(ctx
->s
));
1807 ctx
->s
->write_list
[s
->cy
].data
[s
->cx
+ ci
->used
++] = gc
->data
.data
[0];
1810 /* Write cell data. */
1812 screen_write_cell(struct screen_write_ctx
*ctx
, const struct grid_cell
*gc
)
1814 struct screen
*s
= ctx
->s
;
1815 struct grid
*gd
= s
->grid
;
1816 const struct utf8_data
*ud
= &gc
->data
;
1817 const struct utf8_data zwj
= { "\342\200\215", 0, 3, 0 };
1818 struct grid_line
*gl
;
1819 struct grid_cell_entry
*gce
;
1820 struct grid_cell tmp_gc
, now_gc
;
1821 struct tty_ctx ttyctx
;
1822 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
1823 u_int width
= gc
->data
.width
, xx
, last
, cy
;
1824 int selected
, skip
= 1;
1826 /* Ignore padding cells. */
1827 if (gc
->flags
& GRID_FLAG_PADDING
)
1831 * If this is a zero width joiner, set the flag so the next character
1832 * will be treated as zero width and appended. Note that we assume a
1833 * ZWJ will not change the width - the width of the first character is
1836 if (ud
->size
== 3 && memcmp(ud
->data
, "\342\200\215", 3) == 0) {
1837 log_debug("zero width joiner at %u,%u", s
->cx
, s
->cy
);
1838 ctx
->flags
|= SCREEN_WRITE_ZWJ
;
1843 * If the width is zero, combine onto the previous character. We always
1844 * combine with the cell to the left of the cursor position. In theory,
1845 * the application could have moved the cursor somewhere else, but if
1846 * they are silly enough to do that, who cares?
1848 if (ctx
->flags
& SCREEN_WRITE_ZWJ
) {
1849 screen_write_collect_flush(ctx
, 0, __func__
);
1850 screen_write_combine(ctx
, &zwj
, &xx
);
1852 if (width
== 0 || (ctx
->flags
& SCREEN_WRITE_ZWJ
)) {
1853 ctx
->flags
&= ~SCREEN_WRITE_ZWJ
;
1854 screen_write_collect_flush(ctx
, 0, __func__
);
1855 if ((gc
= screen_write_combine(ctx
, ud
, &xx
)) != NULL
) {
1857 screen_write_set_cursor(ctx
, xx
, s
->cy
);
1858 screen_write_initctx(ctx
, &ttyctx
, 0);
1860 tty_write(tty_cmd_cell
, &ttyctx
);
1861 s
->cx
= xx
+ 1 + gc
->data
.width
; s
->cy
= cy
;
1866 /* Flush any existing scrolling. */
1867 screen_write_collect_flush(ctx
, 1, __func__
);
1869 /* If this character doesn't fit, ignore it. */
1870 if ((~s
->mode
& MODE_WRAP
) &&
1872 (width
> sx
|| (s
->cx
!= sx
&& s
->cx
> sx
- width
)))
1875 /* If in insert mode, make space for the cells. */
1876 if (s
->mode
& MODE_INSERT
) {
1877 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, width
, 8);
1881 /* Check this will fit on the current line and wrap if not. */
1882 if ((s
->mode
& MODE_WRAP
) && s
->cx
> sx
- width
) {
1883 log_debug("%s: wrapped at %u,%u", __func__
, s
->cx
, s
->cy
);
1884 screen_write_linefeed(ctx
, 1, 8);
1885 screen_write_set_cursor(ctx
, 0, -1);
1886 screen_write_collect_flush(ctx
, 1, __func__
);
1889 /* Sanity check cursor position. */
1890 if (s
->cx
> sx
- width
|| s
->cy
> sy
- 1)
1892 screen_write_initctx(ctx
, &ttyctx
, 0);
1894 /* Handle overwriting of UTF-8 characters. */
1895 gl
= grid_get_line(s
->grid
, s
->grid
->hsize
+ s
->cy
);
1896 if (gl
->flags
& GRID_LINE_EXTENDED
) {
1897 grid_view_get_cell(gd
, s
->cx
, s
->cy
, &now_gc
);
1898 if (screen_write_overwrite(ctx
, &now_gc
, width
))
1903 * If the new character is UTF-8 wide, fill in padding cells. Have
1904 * already ensured there is enough room.
1906 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
1907 log_debug("%s: new padding at %u,%u", __func__
, xx
, s
->cy
);
1908 grid_view_set_padding(gd
, xx
, s
->cy
);
1912 /* If no change, do not draw. */
1914 if (s
->cx
>= gl
->cellsize
)
1915 skip
= grid_cells_equal(gc
, &grid_default_cell
);
1917 gce
= &gl
->celldata
[s
->cx
];
1918 if (gce
->flags
& GRID_FLAG_EXTENDED
)
1920 else if (gc
->flags
!= gce
->flags
)
1922 else if (gc
->attr
!= gce
->data
.attr
)
1924 else if (gc
->fg
!= gce
->data
.fg
)
1926 else if (gc
->bg
!= gce
->data
.bg
)
1928 else if (gc
->data
.width
!= 1)
1930 else if (gc
->data
.size
!= 1)
1932 else if (gce
->data
.data
!= gc
->data
.data
[0])
1937 /* Update the selected flag and set the cell. */
1938 selected
= screen_check_selection(s
, s
->cx
, s
->cy
);
1939 if (selected
&& (~gc
->flags
& GRID_FLAG_SELECTED
)) {
1940 memcpy(&tmp_gc
, gc
, sizeof tmp_gc
);
1941 tmp_gc
.flags
|= GRID_FLAG_SELECTED
;
1942 grid_view_set_cell(gd
, s
->cx
, s
->cy
, &tmp_gc
);
1943 } else if (!selected
&& (gc
->flags
& GRID_FLAG_SELECTED
)) {
1944 memcpy(&tmp_gc
, gc
, sizeof tmp_gc
);
1945 tmp_gc
.flags
&= ~GRID_FLAG_SELECTED
;
1946 grid_view_set_cell(gd
, s
->cx
, s
->cy
, &tmp_gc
);
1948 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
1953 * Move the cursor. If not wrapping, stick at the last character and
1956 last
= !(s
->mode
& MODE_WRAP
);
1957 if (s
->cx
<= sx
- last
- width
)
1958 screen_write_set_cursor(ctx
, s
->cx
+ width
, -1);
1960 screen_write_set_cursor(ctx
, sx
- last
, -1);
1962 /* Create space for character in insert mode. */
1963 if (s
->mode
& MODE_INSERT
) {
1964 screen_write_collect_flush(ctx
, 0, __func__
);
1966 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1969 /* Write to the screen. */
1972 screen_select_cell(s
, &tmp_gc
, gc
);
1973 ttyctx
.cell
= &tmp_gc
;
1976 tty_write(tty_cmd_cell
, &ttyctx
);
1980 /* Combine a UTF-8 zero-width character onto the previous. */
1981 static const struct grid_cell
*
1982 screen_write_combine(struct screen_write_ctx
*ctx
, const struct utf8_data
*ud
,
1985 struct screen
*s
= ctx
->s
;
1986 struct grid
*gd
= s
->grid
;
1987 static struct grid_cell gc
;
1990 /* Can't combine if at 0. */
1994 /* Empty data is out. */
1996 fatalx("UTF-8 data empty");
1998 /* Retrieve the previous cell. */
1999 for (n
= 1; n
<= s
->cx
; n
++) {
2000 grid_view_get_cell(gd
, s
->cx
- n
, s
->cy
, &gc
);
2001 if (~gc
.flags
& GRID_FLAG_PADDING
)
2008 /* Check there is enough space. */
2009 if (gc
.data
.size
+ ud
->size
> sizeof gc
.data
.data
)
2012 log_debug("%s: %.*s onto %.*s at %u,%u", __func__
, (int)ud
->size
,
2013 ud
->data
, (int)gc
.data
.size
, gc
.data
.data
, *xx
, s
->cy
);
2015 /* Append the data. */
2016 memcpy(gc
.data
.data
+ gc
.data
.size
, ud
->data
, ud
->size
);
2017 gc
.data
.size
+= ud
->size
;
2019 /* If this is U+FE0F VARIATION SELECTOR-16, force the width to 2. */
2020 if (gc
.data
.width
== 1 &&
2022 memcmp(ud
->data
, "\357\270\217", 3) == 0) {
2023 grid_view_set_padding(gd
, (*xx
) + 1, s
->cy
);
2027 /* Set the new cell. */
2028 grid_view_set_cell(gd
, *xx
, s
->cy
, &gc
);
2034 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
2035 * cell on the screen, so following cells must not be drawn by marking them as
2038 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
2039 * character, it is necessary to also overwrite any other cells which covered
2040 * by the same character.
2043 screen_write_overwrite(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
,
2046 struct screen
*s
= ctx
->s
;
2047 struct grid
*gd
= s
->grid
;
2048 struct grid_cell tmp_gc
;
2052 if (gc
->flags
& GRID_FLAG_PADDING
) {
2054 * A padding cell, so clear any following and leading padding
2055 * cells back to the character. Don't overwrite the current
2056 * cell as that happens later anyway.
2060 grid_view_get_cell(gd
, xx
, s
->cy
, &tmp_gc
);
2061 if (~tmp_gc
.flags
& GRID_FLAG_PADDING
)
2063 log_debug("%s: padding at %u,%u", __func__
, xx
, s
->cy
);
2064 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
2067 /* Overwrite the character at the start of this padding. */
2068 log_debug("%s: character at %u,%u", __func__
, xx
, s
->cy
);
2069 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
2074 * Overwrite any padding cells that belong to any UTF-8 characters
2075 * we'll be overwriting with the current character.
2078 gc
->data
.width
!= 1 ||
2079 gc
->flags
& GRID_FLAG_PADDING
) {
2080 xx
= s
->cx
+ width
- 1;
2081 while (++xx
< screen_size_x(s
)) {
2082 grid_view_get_cell(gd
, xx
, s
->cy
, &tmp_gc
);
2083 if (~tmp_gc
.flags
& GRID_FLAG_PADDING
)
2085 log_debug("%s: overwrite at %u,%u", __func__
, xx
,
2087 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
2095 /* Set external clipboard. */
2097 screen_write_setselection(struct screen_write_ctx
*ctx
, const char *flags
,
2098 u_char
*str
, u_int len
)
2100 struct tty_ctx ttyctx
;
2102 screen_write_initctx(ctx
, &ttyctx
, 0);
2104 ttyctx
.ptr2
= (void *)flags
;
2107 tty_write(tty_cmd_setselection
, &ttyctx
);
2110 /* Write unmodified string. */
2112 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
,
2113 int allow_invisible_panes
)
2115 struct tty_ctx ttyctx
;
2117 screen_write_initctx(ctx
, &ttyctx
, 0);
2120 ttyctx
.allow_invisible_panes
= allow_invisible_panes
;
2122 tty_write(tty_cmd_rawstring
, &ttyctx
);
2125 /* Turn alternate screen on. */
2127 screen_write_alternateon(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
,
2130 struct tty_ctx ttyctx
;
2131 struct window_pane
*wp
= ctx
->wp
;
2133 if (wp
!= NULL
&& !options_get_number(wp
->options
, "alternate-screen"))
2136 screen_write_collect_flush(ctx
, 0, __func__
);
2137 screen_alternate_on(ctx
->s
, gc
, cursor
);
2139 screen_write_initctx(ctx
, &ttyctx
, 1);
2140 if (ttyctx
.redraw_cb
!= NULL
)
2141 ttyctx
.redraw_cb(&ttyctx
);
2144 /* Turn alternate screen off. */
2146 screen_write_alternateoff(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
,
2149 struct tty_ctx ttyctx
;
2150 struct window_pane
*wp
= ctx
->wp
;
2152 if (wp
!= NULL
&& !options_get_number(wp
->options
, "alternate-screen"))
2155 screen_write_collect_flush(ctx
, 0, __func__
);
2156 screen_alternate_off(ctx
->s
, gc
, cursor
);
2158 screen_write_initctx(ctx
, &ttyctx
, 1);
2159 if (ttyctx
.redraw_cb
!= NULL
)
2160 ttyctx
.redraw_cb(&ttyctx
);