xref: /openbsd/usr.bin/tmux/screen-write.c (revision c851f995)
1 /* $OpenBSD: screen-write.c,v 1.230 2024/11/08 08:51:36 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
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,
29 		    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,
32 		    const char *);
33 static int	screen_write_overwrite(struct screen_write_ctx *,
34 		    struct grid_cell *, u_int);
35 static int	screen_write_combine(struct screen_write_ctx *,
36 		    const struct grid_cell *);
37 
38 struct screen_write_citem {
39 	u_int				x;
40 	int				wrapped;
41 
42 	enum { TEXT, CLEAR }		type;
43 	u_int				used;
44 	u_int				bg;
45 
46 	struct grid_cell		gc;
47 
48 	TAILQ_ENTRY(screen_write_citem) entry;
49 };
50 struct screen_write_cline {
51 	char				*data;
52 	TAILQ_HEAD(, screen_write_citem) items;
53 };
54 TAILQ_HEAD(, screen_write_citem)  screen_write_citem_freelist =
55     TAILQ_HEAD_INITIALIZER(screen_write_citem_freelist);
56 
57 static struct screen_write_citem *
screen_write_get_citem(void)58 screen_write_get_citem(void)
59 {
60     struct screen_write_citem	*ci;
61 
62     ci = TAILQ_FIRST(&screen_write_citem_freelist);
63     if (ci != NULL) {
64         TAILQ_REMOVE(&screen_write_citem_freelist, ci, entry);
65         memset(ci, 0, sizeof *ci);
66         return (ci);
67     }
68     return (xcalloc(1, sizeof *ci));
69 }
70 
71 static void
screen_write_free_citem(struct screen_write_citem * ci)72 screen_write_free_citem(struct screen_write_citem *ci)
73 {
74     TAILQ_INSERT_TAIL(&screen_write_citem_freelist, ci, entry);
75 }
76 
77 static void
screen_write_offset_timer(__unused int fd,__unused short events,void * data)78 screen_write_offset_timer(__unused int fd, __unused short events, void *data)
79 {
80 	struct window	*w = data;
81 
82 	tty_update_window_offset(w);
83 }
84 
85 /* Set cursor position. */
86 static void
screen_write_set_cursor(struct screen_write_ctx * ctx,int cx,int cy)87 screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
88 {
89 	struct window_pane	*wp = ctx->wp;
90 	struct window		*w;
91 	struct screen		*s = ctx->s;
92 	struct timeval		 tv = { .tv_usec = 10000 };
93 
94 	if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
95 		return;
96 
97 	if (cx != -1) {
98 		if ((u_int)cx > screen_size_x(s)) /* allow last column */
99 			cx = screen_size_x(s) - 1;
100 		s->cx = cx;
101 	}
102 	if (cy != -1) {
103 		if ((u_int)cy > screen_size_y(s) - 1)
104 			cy = screen_size_y(s) - 1;
105 		s->cy = cy;
106 	}
107 
108 	if (wp == NULL)
109 		return;
110 	w = wp->window;
111 
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);
116 }
117 
118 /* Do a full redraw. */
119 static void
screen_write_redraw_cb(const struct tty_ctx * ttyctx)120 screen_write_redraw_cb(const struct tty_ctx *ttyctx)
121 {
122 	struct window_pane	*wp = ttyctx->arg;
123 
124 	if (wp != NULL)
125 		wp->flags |= PANE_REDRAW;
126 }
127 
128 /* Update context for client. */
129 static int
screen_write_set_client_cb(struct tty_ctx * ttyctx,struct client * c)130 screen_write_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
131 {
132 	struct window_pane	*wp = ttyctx->arg;
133 
134 	if (ttyctx->allow_invisible_panes) {
135 		if (session_has(c->session, wp->window))
136 			return (1);
137 		return (0);
138 	}
139 
140 	if (c->session->curw->window != wp->window)
141 		return (0);
142 	if (wp->layout_cell == NULL)
143 		return (0);
144 
145 	if (wp->flags & (PANE_REDRAW|PANE_DROP))
146 		return (-1);
147 	if (c->flags & CLIENT_REDRAWPANES) {
148 		/*
149 		 * Redraw is already deferred to redraw another pane - redraw
150 		 * this one also when that happens.
151 		 */
152 		log_debug("%s: adding %%%u to deferred redraw", __func__,
153 		    wp->id);
154 		wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR);
155 		return (-1);
156 	}
157 
158 	ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
159 	    &ttyctx->wsx, &ttyctx->wsy);
160 
161 	ttyctx->xoff = ttyctx->rxoff = wp->xoff;
162 	ttyctx->yoff = ttyctx->ryoff = wp->yoff;
163 
164 	if (status_at_line(c) == 0)
165 		ttyctx->yoff += status_line_size(c);
166 
167 	return (1);
168 }
169 
170 /* Set up context for TTY command. */
171 static void
screen_write_initctx(struct screen_write_ctx * ctx,struct tty_ctx * ttyctx,int sync)172 screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
173     int sync)
174 {
175 	struct screen	*s = ctx->s;
176 
177 	memset(ttyctx, 0, sizeof *ttyctx);
178 
179 	ttyctx->s = s;
180 	ttyctx->sx = screen_size_x(s);
181 	ttyctx->sy = screen_size_y(s);
182 
183 	ttyctx->ocx = s->cx;
184 	ttyctx->ocy = s->cy;
185 	ttyctx->orlower = s->rlower;
186 	ttyctx->orupper = s->rupper;
187 
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;
196 		}
197 	} else {
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;
204 		}
205 	}
206 
207 	if (~ctx->flags & SCREEN_WRITE_SYNC) {
208 		/*
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.
213 		 */
214 		if (ctx->wp != NULL) {
215 			if (ctx->wp != ctx->wp->window->active)
216 				ttyctx->num = 1;
217 			else
218 				ttyctx->num = sync;
219 		} else
220 			ttyctx->num = 0x10|sync;
221 		tty_write(tty_cmd_syncstart, ttyctx);
222 		ctx->flags |= SCREEN_WRITE_SYNC;
223 	}
224 }
225 
226 /* Make write list. */
227 void
screen_write_make_list(struct screen * s)228 screen_write_make_list(struct screen *s)
229 {
230 	u_int	y;
231 
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);
235 }
236 
237 /* Free write list. */
238 void
screen_write_free_list(struct screen * s)239 screen_write_free_list(struct screen *s)
240 {
241 	u_int	y;
242 
243 	for (y = 0; y < screen_size_y(s); y++)
244 		free(s->write_list[y].data);
245 	free(s->write_list);
246 }
247 
248 /* Set up for writing. */
249 static void
screen_write_init(struct screen_write_ctx * ctx,struct screen * s)250 screen_write_init(struct screen_write_ctx *ctx, struct screen *s)
251 {
252 	memset(ctx, 0, sizeof *ctx);
253 
254 	ctx->s = s;
255 
256 	if (ctx->s->write_list == NULL)
257 		screen_write_make_list(ctx->s);
258 	ctx->item = screen_write_get_citem();
259 
260 	ctx->scrolled = 0;
261 	ctx->bg = 8;
262 }
263 
264 /* Initialize writing with a pane. */
265 void
screen_write_start_pane(struct screen_write_ctx * ctx,struct window_pane * wp,struct screen * s)266 screen_write_start_pane(struct screen_write_ctx *ctx, struct window_pane *wp,
267     struct screen *s)
268 {
269 	if (s == NULL)
270 		s = wp->screen;
271 	screen_write_init(ctx, s);
272 	ctx->wp = wp;
273 
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);
278 	}
279 }
280 
281 /* Initialize writing with a callback. */
282 void
screen_write_start_callback(struct screen_write_ctx * ctx,struct screen * s,screen_write_init_ctx_cb cb,void * arg)283 screen_write_start_callback(struct screen_write_ctx *ctx, struct screen *s,
284     screen_write_init_ctx_cb cb, void *arg)
285 {
286 	screen_write_init(ctx, s);
287 
288 	ctx->init_ctx_cb = cb;
289 	ctx->arg = arg;
290 
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));
294 	}
295 }
296 
297 /* Initialize writing. */
298 void
screen_write_start(struct screen_write_ctx * ctx,struct screen * s)299 screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
300 {
301 	screen_write_init(ctx, s);
302 
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));
306 	}
307 }
308 
309 /* Finish writing. */
310 void
screen_write_stop(struct screen_write_ctx * ctx)311 screen_write_stop(struct screen_write_ctx *ctx)
312 {
313 	screen_write_collect_end(ctx);
314 	screen_write_collect_flush(ctx, 0, __func__);
315 
316 	screen_write_free_citem(ctx->item);
317 }
318 
319 /* Reset screen state. */
320 void
screen_write_reset(struct screen_write_ctx * ctx)321 screen_write_reset(struct screen_write_ctx *ctx)
322 {
323 	struct screen	*s = ctx->s;
324 
325 	screen_reset_tabs(s);
326 	screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
327 
328 	s->mode = MODE_CURSOR|MODE_WRAP;
329 
330 	if (options_get_number(global_options, "extended-keys") == 2)
331 		s->mode = (s->mode & ~EXTENDED_KEY_MODES)|MODE_KEYS_EXTENDED;
332 
333 	screen_write_clearscreen(ctx, 8);
334 	screen_write_set_cursor(ctx, 0, 0);
335 }
336 
337 /* Write character. */
338 void
screen_write_putc(struct screen_write_ctx * ctx,const struct grid_cell * gcp,u_char ch)339 screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
340     u_char ch)
341 {
342 	struct grid_cell	gc;
343 
344 	memcpy(&gc, gcp, sizeof gc);
345 
346 	utf8_set(&gc.data, ch);
347 	screen_write_cell(ctx, &gc);
348 }
349 
350 /* Calculate string length. */
351 size_t
screen_write_strlen(const char * fmt,...)352 screen_write_strlen(const char *fmt, ...)
353 {
354 	va_list			ap;
355 	char   	       	       *msg;
356 	struct utf8_data	ud;
357 	u_char 	      	       *ptr;
358 	size_t			left, size = 0;
359 	enum utf8_state		more;
360 
361 	va_start(ap, fmt);
362 	xvasprintf(&msg, fmt, ap);
363 	va_end(ap);
364 
365 	ptr = msg;
366 	while (*ptr != '\0') {
367 		if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
368 			ptr++;
369 
370 			left = strlen(ptr);
371 			if (left < (size_t)ud.size - 1)
372 				break;
373 			while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
374 				ptr++;
375 			ptr++;
376 
377 			if (more == UTF8_DONE)
378 				size += ud.width;
379 		} else {
380 			if (*ptr == '\t' || (*ptr > 0x1f && *ptr < 0x7f))
381 				size++;
382 			ptr++;
383 		}
384 	}
385 
386 	free(msg);
387 	return (size);
388 }
389 
390 /* Write string wrapped over lines. */
391 int
screen_write_text(struct screen_write_ctx * ctx,u_int cx,u_int width,u_int lines,int more,const struct grid_cell * gcp,const char * fmt,...)392 screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
393     u_int lines, int more, const struct grid_cell *gcp, const char *fmt, ...)
394 {
395 	struct screen		*s = ctx->s;
396 	va_list			 ap;
397 	char			*tmp;
398 	u_int			 cy = s->cy, i, end, next, idx = 0, at, left;
399 	struct utf8_data	*text;
400 	struct grid_cell	 gc;
401 
402 	memcpy(&gc, gcp, sizeof gc);
403 
404 	va_start(ap, fmt);
405 	xvasprintf(&tmp, fmt, ap);
406 	va_end(ap);
407 
408 	text = utf8_fromcstr(tmp);
409 	free(tmp);
410 
411 	left = (cx + width) - s->cx;
412 	for (;;) {
413 		/* Find the end of what can fit on the line. */
414 		at = 0;
415 		for (end = idx; text[end].size != 0; end++) {
416 			if (text[end].size == 1 && text[end].data[0] == '\n')
417 				break;
418 			if (at + text[end].width > left)
419 				break;
420 			at += text[end].width;
421 		}
422 
423 		/*
424 		 * If we're on a space, that's the end. If not, walk back to
425 		 * try and find one.
426 		 */
427 		if (text[end].size == 0)
428 			next = end;
429 		else if (text[end].size == 1 && text[end].data[0] == '\n')
430 			next = end + 1;
431 		else if (text[end].size == 1 && text[end].data[0] == ' ')
432 			next = end + 1;
433 		else {
434 			for (i = end; i > idx; i--) {
435 				if (text[i].size == 1 && text[i].data[0] == ' ')
436 					break;
437 			}
438 			if (i != idx) {
439 				next = i + 1;
440 				end = i;
441 			} else
442 				next = end;
443 		}
444 
445 		/* Print the line. */
446 		for (i = idx; i < end; i++) {
447 			utf8_copy(&gc.data, &text[i]);
448 			screen_write_cell(ctx, &gc);
449 		}
450 
451 		/* If at the bottom, stop. */
452 		idx = next;
453 		if (s->cy == cy + lines - 1 || text[idx].size == 0)
454 			break;
455 
456 		screen_write_cursormove(ctx, cx, s->cy + 1, 0);
457 		left = width;
458 	}
459 
460 	/*
461 	 * Fail if on the last line and there is more to come or at the end, or
462 	 * if the text was not entirely consumed.
463 	 */
464 	if ((s->cy == cy + lines - 1 && (!more || s->cx == cx + width)) ||
465 	    text[idx].size != 0) {
466 		free(text);
467 		return (0);
468 	}
469 	free(text);
470 
471 	/*
472 	 * If no more to come, move to the next line. Otherwise, leave on
473 	 * the same line (except if at the end).
474 	 */
475 	if (!more || s->cx == cx + width)
476 		screen_write_cursormove(ctx, cx, s->cy + 1, 0);
477 	return (1);
478 }
479 
480 /* Write simple string (no maximum length). */
481 void
screen_write_puts(struct screen_write_ctx * ctx,const struct grid_cell * gcp,const char * fmt,...)482 screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
483     const char *fmt, ...)
484 {
485 	va_list	ap;
486 
487 	va_start(ap, fmt);
488 	screen_write_vnputs(ctx, -1, gcp, fmt, ap);
489 	va_end(ap);
490 }
491 
492 /* Write string with length limit (-1 for unlimited). */
493 void
screen_write_nputs(struct screen_write_ctx * ctx,ssize_t maxlen,const struct grid_cell * gcp,const char * fmt,...)494 screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
495     const struct grid_cell *gcp, const char *fmt, ...)
496 {
497 	va_list	ap;
498 
499 	va_start(ap, fmt);
500 	screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
501 	va_end(ap);
502 }
503 
504 void
screen_write_vnputs(struct screen_write_ctx * ctx,ssize_t maxlen,const struct grid_cell * gcp,const char * fmt,va_list ap)505 screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
506     const struct grid_cell *gcp, const char *fmt, va_list ap)
507 {
508 	struct grid_cell	gc;
509 	struct utf8_data       *ud = &gc.data;
510 	char   		       *msg;
511 	u_char 		       *ptr;
512 	size_t		 	left, size = 0;
513 	enum utf8_state		more;
514 
515 	memcpy(&gc, gcp, sizeof gc);
516 	xvasprintf(&msg, fmt, ap);
517 
518 	ptr = msg;
519 	while (*ptr != '\0') {
520 		if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
521 			ptr++;
522 
523 			left = strlen(ptr);
524 			if (left < (size_t)ud->size - 1)
525 				break;
526 			while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
527 				ptr++;
528 			ptr++;
529 
530 			if (more != UTF8_DONE)
531 				continue;
532 			if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
533 				while (size < (size_t)maxlen) {
534 					screen_write_putc(ctx, &gc, ' ');
535 					size++;
536 				}
537 				break;
538 			}
539 			size += ud->width;
540 			screen_write_cell(ctx, &gc);
541 		} else {
542 			if (maxlen > 0 && size + 1 > (size_t)maxlen)
543 				break;
544 
545 			if (*ptr == '\001')
546 				gc.attr ^= GRID_ATTR_CHARSET;
547 			else if (*ptr == '\n') {
548 				screen_write_linefeed(ctx, 0, 8);
549 				screen_write_carriagereturn(ctx);
550 			} else if (*ptr == '\t' || (*ptr > 0x1f && *ptr < 0x7f)) {
551 				size++;
552 				screen_write_putc(ctx, &gc, *ptr);
553 			}
554 			ptr++;
555 		}
556 	}
557 
558 	free(msg);
559 }
560 
561 /*
562  * Copy from another screen but without the selection stuff. Assumes the target
563  * region is already big enough.
564  */
565 void
screen_write_fast_copy(struct screen_write_ctx * ctx,struct screen * src,u_int px,u_int py,u_int nx,u_int ny)566 screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
567     u_int px, u_int py, u_int nx, u_int ny)
568 {
569 	struct screen		*s = ctx->s;
570 	struct window_pane	*wp = ctx->wp;
571 	struct tty_ctx	 	 ttyctx;
572 	struct grid		*gd = src->grid;
573 	struct grid_cell	 gc;
574 	u_int		 	 xx, yy, cx = s->cx, cy = s->cy;
575 
576 	if (nx == 0 || ny == 0)
577 		return;
578 
579 	cy = s->cy;
580 	for (yy = py; yy < py + ny; yy++) {
581 		if (yy >= gd->hsize + gd->sy)
582 			break;
583 		s->cx = cx;
584 		if (wp != NULL)
585 			screen_write_initctx(ctx, &ttyctx, 0);
586 		for (xx = px; xx < px + nx; xx++) {
587 			if (xx >= grid_get_line(gd, yy)->cellsize)
588 				break;
589 			grid_get_cell(gd, xx, yy, &gc);
590 			if (xx + gc.data.width > px + nx)
591 				break;
592 			grid_view_set_cell(ctx->s->grid, s->cx, s->cy, &gc);
593 			if (wp != NULL) {
594 				ttyctx.cell = &gc;
595 				tty_write(tty_cmd_cell, &ttyctx);
596 				ttyctx.ocx++;
597 			}
598 			s->cx++;
599 		}
600 		s->cy++;
601 	}
602 
603 	s->cx = cx;
604 	s->cy = cy;
605 }
606 
607 /* Select character set for drawing border lines. */
608 static void
screen_write_box_border_set(enum box_lines lines,int cell_type,struct grid_cell * gc)609 screen_write_box_border_set(enum box_lines lines, int cell_type,
610     struct grid_cell *gc)
611 {
612 	switch (lines) {
613         case BOX_LINES_NONE:
614 		break;
615         case BOX_LINES_DOUBLE:
616                 gc->attr &= ~GRID_ATTR_CHARSET;
617                 utf8_copy(&gc->data, tty_acs_double_borders(cell_type));
618 		break;
619         case BOX_LINES_HEAVY:
620                 gc->attr &= ~GRID_ATTR_CHARSET;
621                 utf8_copy(&gc->data, tty_acs_heavy_borders(cell_type));
622 		break;
623         case BOX_LINES_ROUNDED:
624                 gc->attr &= ~GRID_ATTR_CHARSET;
625                 utf8_copy(&gc->data, tty_acs_rounded_borders(cell_type));
626 		break;
627         case BOX_LINES_SIMPLE:
628                 gc->attr &= ~GRID_ATTR_CHARSET;
629                 utf8_set(&gc->data, SIMPLE_BORDERS[cell_type]);
630                 break;
631         case BOX_LINES_PADDED:
632                 gc->attr &= ~GRID_ATTR_CHARSET;
633                 utf8_set(&gc->data, PADDED_BORDERS[cell_type]);
634                 break;
635 	case BOX_LINES_SINGLE:
636 	case BOX_LINES_DEFAULT:
637 		gc->attr |= GRID_ATTR_CHARSET;
638 		utf8_set(&gc->data, CELL_BORDERS[cell_type]);
639 		break;
640 	}
641 }
642 
643 /* Draw a horizontal line on screen. */
644 void
screen_write_hline(struct screen_write_ctx * ctx,u_int nx,int left,int right,enum box_lines lines,const struct grid_cell * border_gc)645 screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right,
646    enum box_lines lines, const struct grid_cell *border_gc)
647 {
648 	struct screen		*s = ctx->s;
649 	struct grid_cell	 gc;
650 	u_int			 cx, cy, i;
651 
652 	cx = s->cx;
653 	cy = s->cy;
654 
655 	if (border_gc != NULL)
656 		memcpy(&gc, border_gc, sizeof gc);
657 	else
658 		memcpy(&gc, &grid_default_cell, sizeof gc);
659 	gc.attr |= GRID_ATTR_CHARSET;
660 
661 	if (left)
662 		screen_write_box_border_set(lines, CELL_LEFTJOIN, &gc);
663 	else
664 		screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
665 	screen_write_cell(ctx, &gc);
666 
667 	screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
668 	for (i = 1; i < nx - 1; i++)
669 		screen_write_cell(ctx, &gc);
670 
671 	if (right)
672 		screen_write_box_border_set(lines, CELL_RIGHTJOIN, &gc);
673 	else
674 		screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
675 	screen_write_cell(ctx, &gc);
676 
677 	screen_write_set_cursor(ctx, cx, cy);
678 }
679 
680 /* Draw a vertical line on screen. */
681 void
screen_write_vline(struct screen_write_ctx * ctx,u_int ny,int top,int bottom)682 screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
683 {
684 	struct screen		*s = ctx->s;
685 	struct grid_cell	 gc;
686 	u_int			 cx, cy, i;
687 
688 	cx = s->cx;
689 	cy = s->cy;
690 
691 	memcpy(&gc, &grid_default_cell, sizeof gc);
692 	gc.attr |= GRID_ATTR_CHARSET;
693 
694 	screen_write_putc(ctx, &gc, top ? 'w' : 'x');
695 	for (i = 1; i < ny - 1; i++) {
696 		screen_write_set_cursor(ctx, cx, cy + i);
697 		screen_write_putc(ctx, &gc, 'x');
698 	}
699 	screen_write_set_cursor(ctx, cx, cy + ny - 1);
700 	screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
701 
702 	screen_write_set_cursor(ctx, cx, cy);
703 }
704 
705 /* Draw a menu on screen. */
706 void
screen_write_menu(struct screen_write_ctx * ctx,struct menu * menu,int choice,enum box_lines lines,const struct grid_cell * menu_gc,const struct grid_cell * border_gc,const struct grid_cell * choice_gc)707 screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu, int choice,
708     enum box_lines lines, const struct grid_cell *menu_gc,
709     const struct grid_cell *border_gc, const struct grid_cell *choice_gc)
710 {
711 	struct screen		*s = ctx->s;
712 	struct grid_cell	 default_gc;
713 	const struct grid_cell	*gc = &default_gc;
714 	u_int			 cx, cy, i, j, width = menu->width;
715 	const char		*name;
716 
717 	cx = s->cx;
718 	cy = s->cy;
719 
720 	memcpy(&default_gc, menu_gc, sizeof default_gc);
721 
722 	screen_write_box(ctx, menu->width + 4, menu->count + 2, lines,
723 	    border_gc, menu->title);
724 
725 	for (i = 0; i < menu->count; i++) {
726 		name = menu->items[i].name;
727 		if (name == NULL) {
728 			screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
729 			screen_write_hline(ctx, width + 4, 1, 1, lines,
730 			    border_gc);
731 			continue;
732 		}
733 
734 		if (choice >= 0 && i == (u_int)choice && *name != '-')
735 			gc = choice_gc;
736 
737 		screen_write_cursormove(ctx, cx + 1, cy + 1 + i, 0);
738 		for (j = 0; j < width + 2; j++)
739 			screen_write_putc(ctx, gc, ' ');
740 
741 		screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
742 		if (*name == '-') {
743 			default_gc.attr |= GRID_ATTR_DIM;
744 			format_draw(ctx, gc, width, name + 1, NULL, 0);
745 			default_gc.attr &= ~GRID_ATTR_DIM;
746 			continue;
747 		}
748 
749 		format_draw(ctx, gc, width, name, NULL, 0);
750 		gc = &default_gc;
751 	}
752 
753 	screen_write_set_cursor(ctx, cx, cy);
754 }
755 
756 /* Draw a box on screen. */
757 void
screen_write_box(struct screen_write_ctx * ctx,u_int nx,u_int ny,enum box_lines lines,const struct grid_cell * gcp,const char * title)758 screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny,
759     enum box_lines lines, const struct grid_cell *gcp, const char *title)
760 {
761 	struct screen		*s = ctx->s;
762 	struct grid_cell         gc;
763 	u_int			 cx, cy, i;
764 
765 	cx = s->cx;
766 	cy = s->cy;
767 
768 	if (gcp != NULL)
769 		memcpy(&gc, gcp, sizeof gc);
770 	else
771 		memcpy(&gc, &grid_default_cell, sizeof gc);
772 
773 	gc.attr |= GRID_ATTR_CHARSET;
774 	gc.flags |= GRID_FLAG_NOPALETTE;
775 
776 	/* Draw top border */
777 	screen_write_box_border_set(lines, CELL_TOPLEFT, &gc);
778 	screen_write_cell(ctx, &gc);
779 	screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
780 	for (i = 1; i < nx - 1; i++)
781 		screen_write_cell(ctx, &gc);
782 	screen_write_box_border_set(lines, CELL_TOPRIGHT, &gc);
783 	screen_write_cell(ctx, &gc);
784 
785 	/* Draw bottom border */
786 	screen_write_set_cursor(ctx, cx, cy + ny - 1);
787 	screen_write_box_border_set(lines, CELL_BOTTOMLEFT, &gc);
788 	screen_write_cell(ctx, &gc);
789 	screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
790 	for (i = 1; i < nx - 1; i++)
791 		screen_write_cell(ctx, &gc);
792 	screen_write_box_border_set(lines, CELL_BOTTOMRIGHT, &gc);
793 	screen_write_cell(ctx, &gc);
794 
795 	/* Draw sides */
796 	screen_write_box_border_set(lines, CELL_TOPBOTTOM, &gc);
797 	for (i = 1; i < ny - 1; i++) {
798 		/* left side */
799 		screen_write_set_cursor(ctx, cx, cy + i);
800 		screen_write_cell(ctx, &gc);
801 		/* right side */
802 		screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
803 		screen_write_cell(ctx, &gc);
804 	}
805 
806 	if (title != NULL) {
807 		gc.attr &= ~GRID_ATTR_CHARSET;
808 		screen_write_cursormove(ctx, cx + 2, cy, 0);
809 		format_draw(ctx, &gc, nx - 4, title, NULL, 0);
810 	}
811 
812 	screen_write_set_cursor(ctx, cx, cy);
813 }
814 
815 /*
816  * Write a preview version of a window. Assumes target area is big enough and
817  * already cleared.
818  */
819 void
screen_write_preview(struct screen_write_ctx * ctx,struct screen * src,u_int nx,u_int ny)820 screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
821     u_int ny)
822 {
823 	struct screen		*s = ctx->s;
824 	struct grid_cell	 gc;
825 	u_int			 cx, cy, px, py;
826 
827 	cx = s->cx;
828 	cy = s->cy;
829 
830 	/*
831 	 * If the cursor is on, pick the area around the cursor, otherwise use
832 	 * the top left.
833 	 */
834 	if (src->mode & MODE_CURSOR) {
835 		px = src->cx;
836 		if (px < nx / 3)
837 			px = 0;
838 		else
839 			px = px - nx / 3;
840 		if (px + nx > screen_size_x(src)) {
841 			if (nx > screen_size_x(src))
842 				px = 0;
843 			else
844 				px = screen_size_x(src) - nx;
845 		}
846 		py = src->cy;
847 		if (py < ny / 3)
848 			py = 0;
849 		else
850 			py = py - ny / 3;
851 		if (py + ny > screen_size_y(src)) {
852 			if (ny > screen_size_y(src))
853 				py = 0;
854 			else
855 				py = screen_size_y(src) - ny;
856 		}
857 	} else {
858 		px = 0;
859 		py = 0;
860 	}
861 
862 	screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
863 
864 	if (src->mode & MODE_CURSOR) {
865 		grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
866 		gc.attr |= GRID_ATTR_REVERSE;
867 		screen_write_set_cursor(ctx, cx + (src->cx - px),
868 		    cy + (src->cy - py));
869 		screen_write_cell(ctx, &gc);
870 	}
871 }
872 
873 /* Set a mode. */
874 void
screen_write_mode_set(struct screen_write_ctx * ctx,int mode)875 screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
876 {
877 	struct screen	*s = ctx->s;
878 
879 	s->mode |= mode;
880 
881 	if (log_get_level() != 0)
882 		log_debug("%s: %s", __func__, screen_mode_to_string(mode));
883 }
884 
885 /* Clear a mode. */
886 void
screen_write_mode_clear(struct screen_write_ctx * ctx,int mode)887 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
888 {
889 	struct screen	*s = ctx->s;
890 
891 	s->mode &= ~mode;
892 
893 	if (log_get_level() != 0)
894 		log_debug("%s: %s", __func__, screen_mode_to_string(mode));
895 }
896 
897 /* Cursor up by ny. */
898 void
screen_write_cursorup(struct screen_write_ctx * ctx,u_int ny)899 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
900 {
901 	struct screen	*s = ctx->s;
902 	u_int		 cx = s->cx, cy = s->cy;
903 
904 	if (ny == 0)
905 		ny = 1;
906 
907 	if (cy < s->rupper) {
908 		/* Above region. */
909 		if (ny > cy)
910 			ny = cy;
911 	} else {
912 		/* Below region. */
913 		if (ny > cy - s->rupper)
914 			ny = cy - s->rupper;
915 	}
916 	if (cx == screen_size_x(s))
917 		cx--;
918 
919 	cy -= ny;
920 
921 	screen_write_set_cursor(ctx, cx, cy);
922 }
923 
924 /* Cursor down by ny. */
925 void
screen_write_cursordown(struct screen_write_ctx * ctx,u_int ny)926 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
927 {
928 	struct screen	*s = ctx->s;
929 	u_int		 cx = s->cx, cy = s->cy;
930 
931 	if (ny == 0)
932 		ny = 1;
933 
934 	if (cy > s->rlower) {
935 		/* Below region. */
936 		if (ny > screen_size_y(s) - 1 - cy)
937 			ny = screen_size_y(s) - 1 - cy;
938 	} else {
939 		/* Above region. */
940 		if (ny > s->rlower - cy)
941 			ny = s->rlower - cy;
942 	}
943 	if (cx == screen_size_x(s))
944 	    cx--;
945 	else if (ny == 0)
946 		return;
947 
948 	cy += ny;
949 
950 	screen_write_set_cursor(ctx, cx, cy);
951 }
952 
953 /* Cursor right by nx. */
954 void
screen_write_cursorright(struct screen_write_ctx * ctx,u_int nx)955 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
956 {
957 	struct screen	*s = ctx->s;
958 	u_int		 cx = s->cx, cy = s->cy;
959 
960 	if (nx == 0)
961 		nx = 1;
962 
963 	if (nx > screen_size_x(s) - 1 - cx)
964 		nx = screen_size_x(s) - 1 - cx;
965 	if (nx == 0)
966 		return;
967 
968 	cx += nx;
969 
970 	screen_write_set_cursor(ctx, cx, cy);
971 }
972 
973 /* Cursor left by nx. */
974 void
screen_write_cursorleft(struct screen_write_ctx * ctx,u_int nx)975 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
976 {
977 	struct screen	*s = ctx->s;
978 	u_int		 cx = s->cx, cy = s->cy;
979 
980 	if (nx == 0)
981 		nx = 1;
982 
983 	if (nx > cx)
984 		nx = cx;
985 	if (nx == 0)
986 		return;
987 
988 	cx -= nx;
989 
990 	screen_write_set_cursor(ctx, cx, cy);
991 }
992 
993 /* Backspace; cursor left unless at start of wrapped line when can move up. */
994 void
screen_write_backspace(struct screen_write_ctx * ctx)995 screen_write_backspace(struct screen_write_ctx *ctx)
996 {
997 	struct screen		*s = ctx->s;
998 	struct grid_line	*gl;
999 	u_int			 cx = s->cx, cy = s->cy;
1000 
1001 	if (cx == 0) {
1002 		if (cy == 0)
1003 			return;
1004 		gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
1005 		if (gl->flags & GRID_LINE_WRAPPED) {
1006 			cy--;
1007 			cx = screen_size_x(s) - 1;
1008 		}
1009 	} else
1010 		cx--;
1011 
1012 	screen_write_set_cursor(ctx, cx, cy);
1013 }
1014 
1015 /* VT100 alignment test. */
1016 void
screen_write_alignmenttest(struct screen_write_ctx * ctx)1017 screen_write_alignmenttest(struct screen_write_ctx *ctx)
1018 {
1019 	struct screen		*s = ctx->s;
1020 	struct tty_ctx	 	 ttyctx;
1021 	struct grid_cell       	 gc;
1022 	u_int			 xx, yy;
1023 
1024 	memcpy(&gc, &grid_default_cell, sizeof gc);
1025 	utf8_set(&gc.data, 'E');
1026 
1027 	for (yy = 0; yy < screen_size_y(s); yy++) {
1028 		for (xx = 0; xx < screen_size_x(s); xx++)
1029 			grid_view_set_cell(s->grid, xx, yy, &gc);
1030 	}
1031 
1032 	screen_write_set_cursor(ctx, 0, 0);
1033 
1034 	s->rupper = 0;
1035 	s->rlower = screen_size_y(s) - 1;
1036 
1037 	screen_write_initctx(ctx, &ttyctx, 1);
1038 
1039 	screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1040 	tty_write(tty_cmd_alignmenttest, &ttyctx);
1041 }
1042 
1043 /* Insert nx characters. */
1044 void
screen_write_insertcharacter(struct screen_write_ctx * ctx,u_int nx,u_int bg)1045 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1046 {
1047 	struct screen	*s = ctx->s;
1048 	struct tty_ctx	 ttyctx;
1049 
1050 	if (nx == 0)
1051 		nx = 1;
1052 
1053 	if (nx > screen_size_x(s) - s->cx)
1054 		nx = screen_size_x(s) - s->cx;
1055 	if (nx == 0)
1056 		return;
1057 
1058 	if (s->cx > screen_size_x(s) - 1)
1059 		return;
1060 
1061 	screen_write_initctx(ctx, &ttyctx, 0);
1062 	ttyctx.bg = bg;
1063 
1064 	grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1065 
1066 	screen_write_collect_flush(ctx, 0, __func__);
1067 	ttyctx.num = nx;
1068 	tty_write(tty_cmd_insertcharacter, &ttyctx);
1069 }
1070 
1071 /* Delete nx characters. */
1072 void
screen_write_deletecharacter(struct screen_write_ctx * ctx,u_int nx,u_int bg)1073 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1074 {
1075 	struct screen	*s = ctx->s;
1076 	struct tty_ctx	 ttyctx;
1077 
1078 	if (nx == 0)
1079 		nx = 1;
1080 
1081 	if (nx > screen_size_x(s) - s->cx)
1082 		nx = screen_size_x(s) - s->cx;
1083 	if (nx == 0)
1084 		return;
1085 
1086 	if (s->cx > screen_size_x(s) - 1)
1087 		return;
1088 
1089 	screen_write_initctx(ctx, &ttyctx, 0);
1090 	ttyctx.bg = bg;
1091 
1092 	grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1093 
1094 	screen_write_collect_flush(ctx, 0, __func__);
1095 	ttyctx.num = nx;
1096 	tty_write(tty_cmd_deletecharacter, &ttyctx);
1097 }
1098 
1099 /* Clear nx characters. */
1100 void
screen_write_clearcharacter(struct screen_write_ctx * ctx,u_int nx,u_int bg)1101 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1102 {
1103 	struct screen	*s = ctx->s;
1104 	struct tty_ctx	 ttyctx;
1105 
1106 	if (nx == 0)
1107 		nx = 1;
1108 
1109 	if (nx > screen_size_x(s) - s->cx)
1110 		nx = screen_size_x(s) - s->cx;
1111 	if (nx == 0)
1112 		return;
1113 
1114 	if (s->cx > screen_size_x(s) - 1)
1115 		return;
1116 
1117 	screen_write_initctx(ctx, &ttyctx, 0);
1118 	ttyctx.bg = bg;
1119 
1120 	grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1121 
1122 	screen_write_collect_flush(ctx, 0, __func__);
1123 	ttyctx.num = nx;
1124 	tty_write(tty_cmd_clearcharacter, &ttyctx);
1125 }
1126 
1127 /* Insert ny lines. */
1128 void
screen_write_insertline(struct screen_write_ctx * ctx,u_int ny,u_int bg)1129 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1130 {
1131 	struct screen	*s = ctx->s;
1132 	struct grid	*gd = s->grid;
1133 	struct tty_ctx	 ttyctx;
1134 
1135 	if (ny == 0)
1136 		ny = 1;
1137 
1138 	if (s->cy < s->rupper || s->cy > s->rlower) {
1139 		if (ny > screen_size_y(s) - s->cy)
1140 			ny = screen_size_y(s) - s->cy;
1141 		if (ny == 0)
1142 			return;
1143 
1144 		screen_write_initctx(ctx, &ttyctx, 1);
1145 		ttyctx.bg = bg;
1146 
1147 		grid_view_insert_lines(gd, s->cy, ny, bg);
1148 
1149 		screen_write_collect_flush(ctx, 0, __func__);
1150 		ttyctx.num = ny;
1151 		tty_write(tty_cmd_insertline, &ttyctx);
1152 		return;
1153 	}
1154 
1155 	if (ny > s->rlower + 1 - s->cy)
1156 		ny = s->rlower + 1 - s->cy;
1157 	if (ny == 0)
1158 		return;
1159 
1160 	screen_write_initctx(ctx, &ttyctx, 1);
1161 	ttyctx.bg = bg;
1162 
1163 	if (s->cy < s->rupper || s->cy > s->rlower)
1164 		grid_view_insert_lines(gd, s->cy, ny, bg);
1165 	else
1166 		grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1167 
1168 	screen_write_collect_flush(ctx, 0, __func__);
1169 
1170 	ttyctx.num = ny;
1171 	tty_write(tty_cmd_insertline, &ttyctx);
1172 }
1173 
1174 /* Delete ny lines. */
1175 void
screen_write_deleteline(struct screen_write_ctx * ctx,u_int ny,u_int bg)1176 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1177 {
1178 	struct screen	*s = ctx->s;
1179 	struct grid	*gd = s->grid;
1180 	struct tty_ctx	 ttyctx;
1181 	u_int		 sy = screen_size_y(s);
1182 
1183 	if (ny == 0)
1184 		ny = 1;
1185 
1186 	if (s->cy < s->rupper || s->cy > s->rlower) {
1187 		if (ny > sy - s->cy)
1188 			ny = sy - s->cy;
1189 		if (ny == 0)
1190 			return;
1191 
1192 		screen_write_initctx(ctx, &ttyctx, 1);
1193 		ttyctx.bg = bg;
1194 
1195 		grid_view_delete_lines(gd, s->cy, ny, bg);
1196 
1197 		screen_write_collect_flush(ctx, 0, __func__);
1198 		ttyctx.num = ny;
1199 		tty_write(tty_cmd_deleteline, &ttyctx);
1200 		return;
1201 	}
1202 
1203 	if (ny > s->rlower + 1 - s->cy)
1204 		ny = s->rlower + 1 - s->cy;
1205 	if (ny == 0)
1206 		return;
1207 
1208 	screen_write_initctx(ctx, &ttyctx, 1);
1209 	ttyctx.bg = bg;
1210 
1211 	if (s->cy < s->rupper || s->cy > s->rlower)
1212 		grid_view_delete_lines(gd, s->cy, ny, bg);
1213 	else
1214 		grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1215 
1216 	screen_write_collect_flush(ctx, 0, __func__);
1217 	ttyctx.num = ny;
1218 	tty_write(tty_cmd_deleteline, &ttyctx);
1219 }
1220 
1221 /* Clear line at cursor. */
1222 void
screen_write_clearline(struct screen_write_ctx * ctx,u_int bg)1223 screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1224 {
1225 	struct screen			*s = ctx->s;
1226 	struct grid_line		*gl;
1227 	u_int				 sx = screen_size_x(s);
1228 	struct screen_write_citem	*ci = ctx->item;
1229 
1230 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1231 	if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1232 		return;
1233 
1234 	grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1235 
1236 	screen_write_collect_clear(ctx, s->cy, 1);
1237 	ci->x = 0;
1238 	ci->used = sx;
1239 	ci->type = CLEAR;
1240 	ci->bg = bg;
1241 	TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1242 	ctx->item = screen_write_get_citem();
1243 }
1244 
1245 /* Clear to end of line from cursor. */
1246 void
screen_write_clearendofline(struct screen_write_ctx * ctx,u_int bg)1247 screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1248 {
1249 	struct screen			*s = ctx->s;
1250 	struct grid_line		*gl;
1251 	u_int				 sx = screen_size_x(s);
1252 	struct screen_write_citem	*ci = ctx->item, *before;
1253 
1254 	if (s->cx == 0) {
1255 		screen_write_clearline(ctx, bg);
1256 		return;
1257 	}
1258 
1259 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1260 	if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1261 		return;
1262 
1263 	grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
1264 
1265  	before = screen_write_collect_trim(ctx, s->cy, s->cx, sx - s->cx, NULL);
1266 	ci->x = s->cx;
1267 	ci->used = sx - s->cx;
1268 	ci->type = CLEAR;
1269 	ci->bg = bg;
1270 	if (before == NULL)
1271 		TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1272 	else
1273 		TAILQ_INSERT_BEFORE(before, ci, entry);
1274 	ctx->item = screen_write_get_citem();
1275 }
1276 
1277 /* Clear to start of line from cursor. */
1278 void
screen_write_clearstartofline(struct screen_write_ctx * ctx,u_int bg)1279 screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1280 {
1281 	struct screen			 *s = ctx->s;
1282 	u_int				 sx = screen_size_x(s);
1283 	struct screen_write_citem	*ci = ctx->item, *before;
1284 
1285 	if (s->cx >= sx - 1) {
1286 		screen_write_clearline(ctx, bg);
1287 		return;
1288 	}
1289 
1290 	if (s->cx > sx - 1)
1291 		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1292 	else
1293 		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1294 
1295 	before = screen_write_collect_trim(ctx, s->cy, 0, s->cx + 1, NULL);
1296 	ci->x = 0;
1297 	ci->used = s->cx + 1;
1298 	ci->type = CLEAR;
1299 	ci->bg = bg;
1300 	if (before == NULL)
1301 		TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1302 	else
1303 		TAILQ_INSERT_BEFORE(before, ci, entry);
1304 	ctx->item = screen_write_get_citem();
1305 }
1306 
1307 /* Move cursor to px,py. */
1308 void
screen_write_cursormove(struct screen_write_ctx * ctx,int px,int py,int origin)1309 screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1310     int origin)
1311 {
1312 	struct screen	*s = ctx->s;
1313 
1314 	if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1315 		if ((u_int)py > s->rlower - s->rupper)
1316 			py = s->rlower;
1317 		else
1318 			py += s->rupper;
1319 	}
1320 
1321 	if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1322 		px = screen_size_x(s) - 1;
1323 	if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1324 		py = screen_size_y(s) - 1;
1325 
1326 	log_debug("%s: from %u,%u to %u,%u", __func__, s->cx, s->cy, px, py);
1327 	screen_write_set_cursor(ctx, px, py);
1328 }
1329 
1330 /* Reverse index (up with scroll). */
1331 void
screen_write_reverseindex(struct screen_write_ctx * ctx,u_int bg)1332 screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1333 {
1334 	struct screen	*s = ctx->s;
1335 	struct tty_ctx	 ttyctx;
1336 
1337 	if (s->cy == s->rupper) {
1338 		grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1339 		screen_write_collect_flush(ctx, 0, __func__);
1340 
1341 		screen_write_initctx(ctx, &ttyctx, 1);
1342 		ttyctx.bg = bg;
1343 
1344 		tty_write(tty_cmd_reverseindex, &ttyctx);
1345 	} else if (s->cy > 0)
1346 		screen_write_set_cursor(ctx, -1, s->cy - 1);
1347 
1348 }
1349 
1350 /* Set scroll region. */
1351 void
screen_write_scrollregion(struct screen_write_ctx * ctx,u_int rupper,u_int rlower)1352 screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1353     u_int rlower)
1354 {
1355 	struct screen	*s = ctx->s;
1356 
1357 	if (rupper > screen_size_y(s) - 1)
1358 		rupper = screen_size_y(s) - 1;
1359 	if (rlower > screen_size_y(s) - 1)
1360 		rlower = screen_size_y(s) - 1;
1361 	if (rupper >= rlower)	/* cannot be one line */
1362 		return;
1363 
1364 	screen_write_collect_flush(ctx, 0, __func__);
1365 
1366 	/* Cursor moves to top-left. */
1367 	screen_write_set_cursor(ctx, 0, 0);
1368 
1369 	s->rupper = rupper;
1370 	s->rlower = rlower;
1371 }
1372 
1373 /* Line feed. */
1374 void
screen_write_linefeed(struct screen_write_ctx * ctx,int wrapped,u_int bg)1375 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1376 {
1377 	struct screen		*s = ctx->s;
1378 	struct grid		*gd = s->grid;
1379 	struct grid_line	*gl;
1380 	u_int			 rupper = s->rupper, rlower = s->rlower;
1381 
1382 	gl = grid_get_line(gd, gd->hsize + s->cy);
1383 	if (wrapped)
1384 		gl->flags |= GRID_LINE_WRAPPED;
1385 
1386 	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1387 	    rupper, rlower);
1388 
1389 	if (bg != ctx->bg) {
1390 		screen_write_collect_flush(ctx, 1, __func__);
1391 		ctx->bg = bg;
1392 	}
1393 
1394 	if (s->cy == s->rlower) {
1395 		grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1396 		screen_write_collect_scroll(ctx, bg);
1397 		ctx->scrolled++;
1398 	} else if (s->cy < screen_size_y(s) - 1)
1399 		screen_write_set_cursor(ctx, -1, s->cy + 1);
1400 }
1401 
1402 /* Scroll up. */
1403 void
screen_write_scrollup(struct screen_write_ctx * ctx,u_int lines,u_int bg)1404 screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1405 {
1406 	struct screen	*s = ctx->s;
1407 	struct grid	*gd = s->grid;
1408 	u_int		 i;
1409 
1410 	if (lines == 0)
1411 		lines = 1;
1412 	else if (lines > s->rlower - s->rupper + 1)
1413 		lines = s->rlower - s->rupper + 1;
1414 
1415 	if (bg != ctx->bg) {
1416 		screen_write_collect_flush(ctx, 1, __func__);
1417 		ctx->bg = bg;
1418 	}
1419 
1420 	for (i = 0; i < lines; i++) {
1421 		grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1422 		screen_write_collect_scroll(ctx, bg);
1423 	}
1424 	ctx->scrolled += lines;
1425 }
1426 
1427 /* Scroll down. */
1428 void
screen_write_scrolldown(struct screen_write_ctx * ctx,u_int lines,u_int bg)1429 screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1430 {
1431 	struct screen	*s = ctx->s;
1432 	struct grid	*gd = s->grid;
1433 	struct tty_ctx	 ttyctx;
1434 	u_int		 i;
1435 
1436 	screen_write_initctx(ctx, &ttyctx, 1);
1437 	ttyctx.bg = bg;
1438 
1439 	if (lines == 0)
1440 		lines = 1;
1441 	else if (lines > s->rlower - s->rupper + 1)
1442 		lines = s->rlower - s->rupper + 1;
1443 
1444 	for (i = 0; i < lines; i++)
1445 		grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg);
1446 
1447 	screen_write_collect_flush(ctx, 0, __func__);
1448 	ttyctx.num = lines;
1449 	tty_write(tty_cmd_scrolldown, &ttyctx);
1450 }
1451 
1452 /* Carriage return (cursor to start of line). */
1453 void
screen_write_carriagereturn(struct screen_write_ctx * ctx)1454 screen_write_carriagereturn(struct screen_write_ctx *ctx)
1455 {
1456 	screen_write_set_cursor(ctx, 0, -1);
1457 }
1458 
1459 /* Clear to end of screen from cursor. */
1460 void
screen_write_clearendofscreen(struct screen_write_ctx * ctx,u_int bg)1461 screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1462 {
1463 	struct screen	*s = ctx->s;
1464 	struct grid	*gd = s->grid;
1465 	struct tty_ctx	 ttyctx;
1466 	u_int		 sx = screen_size_x(s), sy = screen_size_y(s);
1467 
1468 	screen_write_initctx(ctx, &ttyctx, 1);
1469 	ttyctx.bg = bg;
1470 
1471 	/* Scroll into history if it is enabled and clearing entire screen. */
1472 	if (s->cx == 0 &&
1473 	    s->cy == 0 &&
1474 	    (gd->flags & GRID_HISTORY) &&
1475 	    ctx->wp != NULL &&
1476 	    options_get_number(ctx->wp->options, "scroll-on-clear"))
1477 		grid_view_clear_history(gd, bg);
1478 	else {
1479 		if (s->cx <= sx - 1)
1480 			grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1481 		grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1482 	}
1483 
1484 	screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1485 	screen_write_collect_flush(ctx, 0, __func__);
1486 	tty_write(tty_cmd_clearendofscreen, &ttyctx);
1487 }
1488 
1489 /* Clear to start of screen. */
1490 void
screen_write_clearstartofscreen(struct screen_write_ctx * ctx,u_int bg)1491 screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1492 {
1493 	struct screen	*s = ctx->s;
1494 	struct tty_ctx	 ttyctx;
1495 	u_int		 sx = screen_size_x(s);
1496 
1497 	screen_write_initctx(ctx, &ttyctx, 1);
1498 	ttyctx.bg = bg;
1499 
1500 	if (s->cy > 0)
1501 		grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1502 	if (s->cx > sx - 1)
1503 		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1504 	else
1505 		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1506 
1507 	screen_write_collect_clear(ctx, 0, s->cy);
1508 	screen_write_collect_flush(ctx, 0, __func__);
1509 	tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1510 }
1511 
1512 /* Clear entire screen. */
1513 void
screen_write_clearscreen(struct screen_write_ctx * ctx,u_int bg)1514 screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1515 {
1516 	struct screen	*s = ctx->s;
1517 	struct tty_ctx	 ttyctx;
1518 	u_int		 sx = screen_size_x(s), sy = screen_size_y(s);
1519 
1520 	screen_write_initctx(ctx, &ttyctx, 1);
1521 	ttyctx.bg = bg;
1522 
1523 	/* Scroll into history if it is enabled. */
1524 	if ((s->grid->flags & GRID_HISTORY) &&
1525 	    ctx->wp != NULL &&
1526 	    options_get_number(ctx->wp->options, "scroll-on-clear"))
1527 		grid_view_clear_history(s->grid, bg);
1528 	else
1529 		grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1530 
1531 	screen_write_collect_clear(ctx, 0, sy);
1532 	tty_write(tty_cmd_clearscreen, &ttyctx);
1533 }
1534 
1535 /* Clear entire history. */
1536 void
screen_write_clearhistory(struct screen_write_ctx * ctx)1537 screen_write_clearhistory(struct screen_write_ctx *ctx)
1538 {
1539 	grid_clear_history(ctx->s->grid);
1540 }
1541 
1542 /* Force a full redraw. */
1543 void
screen_write_fullredraw(struct screen_write_ctx * ctx)1544 screen_write_fullredraw(struct screen_write_ctx *ctx)
1545 {
1546 	struct tty_ctx	 ttyctx;
1547 
1548 	screen_write_collect_flush(ctx, 0, __func__);
1549 
1550 	screen_write_initctx(ctx, &ttyctx, 1);
1551 	if (ttyctx.redraw_cb != NULL)
1552 		ttyctx.redraw_cb(&ttyctx);
1553 }
1554 
1555 /* Trim collected items. */
1556 static struct screen_write_citem *
screen_write_collect_trim(struct screen_write_ctx * ctx,u_int y,u_int x,u_int used,int * wrapped)1557 screen_write_collect_trim(struct screen_write_ctx *ctx, u_int y, u_int x,
1558     u_int used, int *wrapped)
1559 {
1560 	struct screen_write_cline	*cl = &ctx->s->write_list[y];
1561 	struct screen_write_citem	*ci, *ci2, *tmp, *before = NULL;
1562 	u_int				 sx = x, ex = x + used - 1;
1563 	u_int				 csx, cex;
1564 
1565 	if (TAILQ_EMPTY(&cl->items))
1566 		return (NULL);
1567 	TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1568 		csx = ci->x;
1569 		cex = ci->x + ci->used - 1;
1570 
1571 		/* Item is entirely before. */
1572 		if (cex < sx) {
1573 			log_debug("%s: %p %u-%u before %u-%u", __func__, ci,
1574 			    csx, cex, sx, ex);
1575 			continue;
1576 		}
1577 
1578 		/* Item is entirely after. */
1579 		if (csx > ex) {
1580 			log_debug("%s: %p %u-%u after %u-%u", __func__, ci,
1581 			    csx, cex, sx, ex);
1582 			before = ci;
1583 			break;
1584 		}
1585 
1586 		/* Item is entirely inside. */
1587 		if (csx >= sx && cex <= ex) {
1588 			log_debug("%s: %p %u-%u inside %u-%u", __func__, ci,
1589 			    csx, cex, sx, ex);
1590 			TAILQ_REMOVE(&cl->items, ci, entry);
1591 			screen_write_free_citem(ci);
1592 			if (csx == 0 && ci->wrapped && wrapped != NULL)
1593 				*wrapped = 1;
1594 			continue;
1595 		}
1596 
1597 		/* Item under the start. */
1598 		if (csx < sx && cex >= sx && cex <= ex) {
1599 			log_debug("%s: %p %u-%u start %u-%u", __func__, ci,
1600 			    csx, cex, sx, ex);
1601 			ci->used = sx - csx;
1602 			log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1603 			    ci->x + ci->used + 1);
1604 			continue;
1605 		}
1606 
1607 		/* Item covers the end. */
1608 		if (cex > ex && csx >= sx && csx <= ex) {
1609 			log_debug("%s: %p %u-%u end %u-%u", __func__, ci,
1610 			    csx, cex, sx, ex);
1611 			ci->x = ex + 1;
1612 			ci->used = cex - ex;
1613 			log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1614 			    ci->x + ci->used + 1);
1615 			before = ci;
1616 			break;
1617 		}
1618 
1619 		/* Item must cover both sides. */
1620 		log_debug("%s: %p %u-%u under %u-%u", __func__, ci,
1621 		    csx, cex, sx, ex);
1622 		ci2 = screen_write_get_citem();
1623 		ci2->type = ci->type;
1624 		ci2->bg = ci->bg;
1625 		memcpy(&ci2->gc, &ci->gc, sizeof ci2->gc);
1626 		TAILQ_INSERT_AFTER(&cl->items, ci, ci2, entry);
1627 
1628 		ci->used = sx - csx;
1629 		ci2->x = ex + 1;
1630 		ci2->used = cex - ex;
1631 
1632 		log_debug("%s: %p now %u-%u (%p) and %u-%u (%p)", __func__, ci,
1633 		    ci->x, ci->x + ci->used - 1, ci, ci2->x,
1634 		    ci2->x + ci2->used - 1, ci2);
1635 		before = ci2;
1636 		break;
1637 	}
1638 	return (before);
1639 }
1640 
1641 /* Clear collected lines. */
1642 static void
screen_write_collect_clear(struct screen_write_ctx * ctx,u_int y,u_int n)1643 screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1644 {
1645 	struct screen_write_cline	*cl;
1646 	u_int				 i;
1647 
1648 	for (i = y; i < y + n; i++) {
1649 		cl = &ctx->s->write_list[i];
1650 		TAILQ_CONCAT(&screen_write_citem_freelist, &cl->items, entry);
1651 	}
1652 }
1653 
1654 /* Scroll collected lines up. */
1655 static void
screen_write_collect_scroll(struct screen_write_ctx * ctx,u_int bg)1656 screen_write_collect_scroll(struct screen_write_ctx *ctx, u_int bg)
1657 {
1658 	struct screen			*s = ctx->s;
1659 	struct screen_write_cline	*cl;
1660 	u_int				 y;
1661 	char				*saved;
1662 	struct screen_write_citem	*ci;
1663 
1664 	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1665 	    s->rupper, s->rlower);
1666 
1667 	screen_write_collect_clear(ctx, s->rupper, 1);
1668 	saved = ctx->s->write_list[s->rupper].data;
1669 	for (y = s->rupper; y < s->rlower; y++) {
1670 		cl = &ctx->s->write_list[y + 1];
1671 		TAILQ_CONCAT(&ctx->s->write_list[y].items, &cl->items, entry);
1672 		ctx->s->write_list[y].data = cl->data;
1673 	}
1674 	ctx->s->write_list[s->rlower].data = saved;
1675 
1676 	ci = screen_write_get_citem();
1677 	ci->x = 0;
1678 	ci->used = screen_size_x(s);
1679 	ci->type = CLEAR;
1680 	ci->bg = bg;
1681 	TAILQ_INSERT_TAIL(&ctx->s->write_list[s->rlower].items, ci, entry);
1682 }
1683 
1684 /* Flush collected lines. */
1685 static void
screen_write_collect_flush(struct screen_write_ctx * ctx,int scroll_only,const char * from)1686 screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
1687     const char *from)
1688 {
1689 	struct screen			*s = ctx->s;
1690 	struct screen_write_citem	*ci, *tmp;
1691 	struct screen_write_cline	*cl;
1692 	u_int				 y, cx, cy, last, items = 0;
1693 	struct tty_ctx			 ttyctx;
1694 
1695 	if (ctx->scrolled != 0) {
1696 		log_debug("%s: scrolled %u (region %u-%u)", __func__,
1697 		    ctx->scrolled, s->rupper, s->rlower);
1698 		if (ctx->scrolled > s->rlower - s->rupper + 1)
1699 			ctx->scrolled = s->rlower - s->rupper + 1;
1700 
1701 		screen_write_initctx(ctx, &ttyctx, 1);
1702 		ttyctx.num = ctx->scrolled;
1703 		ttyctx.bg = ctx->bg;
1704 		tty_write(tty_cmd_scrollup, &ttyctx);
1705 
1706 		if (ctx->wp != NULL)
1707 			ctx->wp->flags |= PANE_REDRAWSCROLLBAR;
1708 	}
1709 	ctx->scrolled = 0;
1710 	ctx->bg = 8;
1711 
1712 	if (scroll_only)
1713 		return;
1714 
1715 	cx = s->cx; cy = s->cy;
1716 	for (y = 0; y < screen_size_y(s); y++) {
1717 		cl = &ctx->s->write_list[y];
1718 		last = UINT_MAX;
1719 		TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1720 			if (last != UINT_MAX && ci->x <= last) {
1721 				fatalx("collect list not in order: %u <= %u",
1722 				    ci->x, last);
1723 			}
1724 			screen_write_set_cursor(ctx, ci->x, y);
1725 			if (ci->type == CLEAR) {
1726 				screen_write_initctx(ctx, &ttyctx, 1);
1727 				ttyctx.bg = ci->bg;
1728 				ttyctx.num = ci->used;
1729 				tty_write(tty_cmd_clearcharacter, &ttyctx);
1730 			} else {
1731 				screen_write_initctx(ctx, &ttyctx, 0);
1732 				ttyctx.cell = &ci->gc;
1733 				ttyctx.wrapped = ci->wrapped;
1734 				ttyctx.ptr = cl->data + ci->x;
1735 				ttyctx.num = ci->used;
1736 				tty_write(tty_cmd_cells, &ttyctx);
1737 			}
1738 			items++;
1739 
1740 			TAILQ_REMOVE(&cl->items, ci, entry);
1741 			screen_write_free_citem(ci);
1742 			last = ci->x;
1743 		}
1744 	}
1745 	s->cx = cx; s->cy = cy;
1746 
1747 	log_debug("%s: flushed %u items (%s)", __func__, items, from);
1748 }
1749 
1750 /* Finish and store collected cells. */
1751 void
screen_write_collect_end(struct screen_write_ctx * ctx)1752 screen_write_collect_end(struct screen_write_ctx *ctx)
1753 {
1754 	struct screen			*s = ctx->s;
1755 	struct screen_write_citem	*ci = ctx->item, *before;
1756 	struct screen_write_cline	*cl = &s->write_list[s->cy];
1757 	struct grid_cell		 gc;
1758 	u_int				 xx;
1759 	int				 wrapped = ci->wrapped;
1760 
1761 	if (ci->used == 0)
1762 		return;
1763 
1764 	before = screen_write_collect_trim(ctx, s->cy, s->cx, ci->used,
1765 	    &wrapped);
1766 	ci->x = s->cx;
1767 	ci->wrapped = wrapped;
1768 	if (before == NULL)
1769 		TAILQ_INSERT_TAIL(&cl->items, ci, entry);
1770 	else
1771 		TAILQ_INSERT_BEFORE(before, ci, entry);
1772 	ctx->item = screen_write_get_citem();
1773 
1774 	log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used,
1775 	    (int)ci->used, cl->data + ci->x, s->cx, s->cy);
1776 
1777 	if (s->cx != 0) {
1778 		for (xx = s->cx; xx > 0; xx--) {
1779 			grid_view_get_cell(s->grid, xx, s->cy, &gc);
1780 			if (~gc.flags & GRID_FLAG_PADDING)
1781 				break;
1782 			grid_view_set_cell(s->grid, xx, s->cy,
1783 			    &grid_default_cell);
1784 		}
1785 		if (gc.data.width > 1) {
1786 			grid_view_set_cell(s->grid, xx, s->cy,
1787 			    &grid_default_cell);
1788 		}
1789 	}
1790 
1791 	grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x,
1792 	    ci->used);
1793 	screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1794 
1795 	for (xx = s->cx; xx < screen_size_x(s); xx++) {
1796 		grid_view_get_cell(s->grid, xx, s->cy, &gc);
1797 		if (~gc.flags & GRID_FLAG_PADDING)
1798 			break;
1799 		grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1800 	}
1801 }
1802 
1803 /* Write cell data, collecting if necessary. */
1804 void
screen_write_collect_add(struct screen_write_ctx * ctx,const struct grid_cell * gc)1805 screen_write_collect_add(struct screen_write_ctx *ctx,
1806     const struct grid_cell *gc)
1807 {
1808 	struct screen			*s = ctx->s;
1809 	struct screen_write_citem	*ci;
1810 	u_int				 sx = screen_size_x(s);
1811 	int				 collect;
1812 
1813 	/*
1814 	 * Don't need to check that the attributes and whatnot are still the
1815 	 * same - input_parse will end the collection when anything that isn't
1816 	 * a plain character is encountered.
1817 	 */
1818 
1819 	collect = 1;
1820 	if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1821 		collect = 0;
1822 	else if (gc->flags & GRID_FLAG_TAB)
1823 		collect = 0;
1824 	else if (gc->attr & GRID_ATTR_CHARSET)
1825 		collect = 0;
1826 	else if (~s->mode & MODE_WRAP)
1827 		collect = 0;
1828 	else if (s->mode & MODE_INSERT)
1829 		collect = 0;
1830 	else if (s->sel != NULL)
1831 		collect = 0;
1832 	if (!collect) {
1833 		screen_write_collect_end(ctx);
1834 		screen_write_collect_flush(ctx, 0, __func__);
1835 		screen_write_cell(ctx, gc);
1836 		return;
1837 	}
1838 
1839 	if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1840 		screen_write_collect_end(ctx);
1841 	ci = ctx->item; /* may have changed */
1842 
1843 	if (s->cx > sx - 1) {
1844 		log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1845 		ci->wrapped = 1;
1846 		screen_write_linefeed(ctx, 1, 8);
1847 		screen_write_set_cursor(ctx, 0, -1);
1848 	}
1849 
1850 	if (ci->used == 0)
1851 		memcpy(&ci->gc, gc, sizeof ci->gc);
1852 	if (ctx->s->write_list[s->cy].data == NULL)
1853 		ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s));
1854 	ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
1855 }
1856 
1857 /* Write cell data. */
1858 void
screen_write_cell(struct screen_write_ctx * ctx,const struct grid_cell * gc)1859 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1860 {
1861 	struct screen		*s = ctx->s;
1862 	struct grid		*gd = s->grid;
1863 	const struct utf8_data	*ud = &gc->data;
1864 	struct grid_line	*gl;
1865 	struct grid_cell_entry	*gce;
1866 	struct grid_cell 	 tmp_gc, now_gc;
1867 	struct tty_ctx		 ttyctx;
1868 	u_int			 sx = screen_size_x(s), sy = screen_size_y(s);
1869 	u_int		 	 width = ud->width, xx, not_wrap;
1870 	int			 selected, skip = 1;
1871 
1872 	/* Ignore padding cells. */
1873 	if (gc->flags & GRID_FLAG_PADDING)
1874 		return;
1875 
1876 	/* Get the previous cell to check for combining. */
1877 	if (screen_write_combine(ctx, gc) != 0)
1878 		return;
1879 
1880 	/* Flush any existing scrolling. */
1881 	screen_write_collect_flush(ctx, 1, __func__);
1882 
1883 	/* If this character doesn't fit, ignore it. */
1884 	if ((~s->mode & MODE_WRAP) &&
1885 	    width > 1 &&
1886 	    (width > sx || (s->cx != sx && s->cx > sx - width)))
1887 		return;
1888 
1889 	/* If in insert mode, make space for the cells. */
1890 	if (s->mode & MODE_INSERT) {
1891 		grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1892 		skip = 0;
1893 	}
1894 
1895 	/* Check this will fit on the current line and wrap if not. */
1896 	if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1897 		log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1898 		screen_write_linefeed(ctx, 1, 8);
1899 		screen_write_set_cursor(ctx, 0, -1);
1900 		screen_write_collect_flush(ctx, 1, __func__);
1901 	}
1902 
1903 	/* Sanity check cursor position. */
1904 	if (s->cx > sx - width || s->cy > sy - 1)
1905 		return;
1906 	screen_write_initctx(ctx, &ttyctx, 0);
1907 
1908 	/* Handle overwriting of UTF-8 characters. */
1909 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1910 	if (gl->flags & GRID_LINE_EXTENDED) {
1911 		grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1912 		if (screen_write_overwrite(ctx, &now_gc, width))
1913 			skip = 0;
1914 	}
1915 
1916 	/*
1917 	 * If the new character is UTF-8 wide, fill in padding cells. Have
1918 	 * already ensured there is enough room.
1919 	 */
1920 	for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1921 		log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1922 		grid_view_set_padding(gd, xx, s->cy);
1923 		skip = 0;
1924 	}
1925 
1926 	/* If no change, do not draw. */
1927 	if (skip) {
1928 		if (s->cx >= gl->cellsize)
1929 			skip = grid_cells_equal(gc, &grid_default_cell);
1930 		else {
1931 			gce = &gl->celldata[s->cx];
1932 			if (gce->flags & GRID_FLAG_EXTENDED)
1933 				skip = 0;
1934 			else if (gc->flags != gce->flags)
1935 				skip = 0;
1936 			else if (gc->attr != gce->data.attr)
1937 				skip = 0;
1938 			else if (gc->fg != gce->data.fg)
1939 				skip = 0;
1940 			else if (gc->bg != gce->data.bg)
1941 				skip = 0;
1942 			else if (gc->data.width != 1)
1943 				skip = 0;
1944 			else if (gc->data.size != 1)
1945 				skip = 0;
1946 			else if (gce->data.data != gc->data.data[0])
1947 				skip = 0;
1948 		}
1949 	}
1950 
1951 	/* Update the selected flag and set the cell. */
1952 	selected = screen_check_selection(s, s->cx, s->cy);
1953 	if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1954 		memcpy(&tmp_gc, gc, sizeof tmp_gc);
1955 		tmp_gc.flags |= GRID_FLAG_SELECTED;
1956 		grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1957 	} else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1958 		memcpy(&tmp_gc, gc, sizeof tmp_gc);
1959 		tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1960 		grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1961 	} else if (!skip)
1962 		grid_view_set_cell(gd, s->cx, s->cy, gc);
1963 	if (selected)
1964 		skip = 0;
1965 
1966 	/*
1967 	 * Move the cursor. If not wrapping, stick at the last character and
1968 	 * replace it.
1969 	 */
1970 	not_wrap = !(s->mode & MODE_WRAP);
1971 	if (s->cx <= sx - not_wrap - width)
1972 		screen_write_set_cursor(ctx, s->cx + width, -1);
1973 	else
1974 		screen_write_set_cursor(ctx,  sx - not_wrap, -1);
1975 
1976 	/* Create space for character in insert mode. */
1977 	if (s->mode & MODE_INSERT) {
1978 		screen_write_collect_flush(ctx, 0, __func__);
1979 		ttyctx.num = width;
1980 		tty_write(tty_cmd_insertcharacter, &ttyctx);
1981 	}
1982 
1983 	/* Write to the screen. */
1984 	if (!skip) {
1985 		if (selected) {
1986 			screen_select_cell(s, &tmp_gc, gc);
1987 			ttyctx.cell = &tmp_gc;
1988 		} else
1989 			ttyctx.cell = gc;
1990 		tty_write(tty_cmd_cell, &ttyctx);
1991 	}
1992 }
1993 
1994 /* Combine a UTF-8 zero-width character onto the previous if necessary. */
1995 static int
screen_write_combine(struct screen_write_ctx * ctx,const struct grid_cell * gc)1996 screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1997 {
1998 	struct screen		*s = ctx->s;
1999 	struct grid		*gd = s->grid;
2000 	const struct utf8_data	*ud = &gc->data;
2001 	u_int			 n, cx = s->cx, cy = s->cy;
2002 	struct grid_cell	 last;
2003 	struct tty_ctx		 ttyctx;
2004 	int			 force_wide = 0, zero_width = 0;
2005 
2006 	/*
2007 	 * Is this character which makes no sense without being combined? If
2008 	 * this is true then flag it here and discard the character (return 1)
2009 	 * if we cannot combine it.
2010 	 */
2011 	if (utf8_is_zwj(ud))
2012 		zero_width = 1;
2013 	else if (utf8_is_vs(ud))
2014 		zero_width = force_wide = 1;
2015 	else if (ud->width == 0)
2016 		zero_width = 1;
2017 
2018 	/* Cannot combine empty character or at left. */
2019 	if (ud->size < 2 || cx == 0)
2020 		return (zero_width);
2021 	log_debug("%s: character %.*s at %u,%u (width %u)", __func__,
2022 	    (int)ud->size, ud->data, cx, cy, ud->width);
2023 
2024 	/* Find the cell to combine with. */
2025 	n = 1;
2026 	grid_view_get_cell(gd, cx - n, cy, &last);
2027 	if (cx != 1 && (last.flags & GRID_FLAG_PADDING)) {
2028 		n = 2;
2029 		grid_view_get_cell(gd, cx - n, cy, &last);
2030 	}
2031 	if (n != last.data.width || (last.flags & GRID_FLAG_PADDING))
2032 		return (zero_width);
2033 
2034 	/*
2035 	 * Check if we need to combine characters. This could be zero width
2036 	 * (set above), a modifier character (with an existing Unicode
2037 	 * character) or a previous ZWJ.
2038 	 */
2039 	if (!zero_width) {
2040 		if (utf8_is_modifier(ud)) {
2041 			if (last.data.size < 2)
2042 				return (0);
2043 			force_wide = 1;
2044 		} else if (!utf8_has_zwj(&last.data))
2045 			return (0);
2046 	}
2047 
2048 	/* Check if this combined character would be too long. */
2049 	if (last.data.size + ud->size > sizeof last.data.data)
2050 		return (0);
2051 
2052 	/* Combining; flush any pending output. */
2053 	screen_write_collect_flush(ctx, 0, __func__);
2054 
2055 	log_debug("%s: %.*s -> %.*s at %u,%u (offset %u, width %u)", __func__,
2056 	    (int)ud->size, ud->data, (int)last.data.size, last.data.data,
2057 	    cx - n, cy, n, last.data.width);
2058 
2059 	/* Append the data. */
2060 	memcpy(last.data.data + last.data.size, ud->data, ud->size);
2061 	last.data.size += ud->size;
2062 
2063 	/* Force the width to 2 for modifiers and variation selector. */
2064 	if (last.data.width == 1 && force_wide) {
2065 		last.data.width = 2;
2066 		n = 2;
2067 		cx++;
2068 	} else
2069 		force_wide = 0;
2070 
2071 	/* Set the new cell. */
2072 	grid_view_set_cell(gd, cx - n, cy, &last);
2073 	if (force_wide)
2074 		grid_view_set_padding(gd, cx - 1, cy);
2075 
2076 	/*
2077 	 * Redraw the combined cell. If forcing the cell to width 2, reset the
2078 	 * cached cursor position in the tty, since we don't really know
2079 	 * whether the terminal thought the character was width 1 or width 2
2080 	 * and what it is going to do now.
2081 	 */
2082 	screen_write_set_cursor(ctx, cx - n, cy);
2083 	screen_write_initctx(ctx, &ttyctx, 0);
2084 	ttyctx.cell = &last;
2085 	ttyctx.num = force_wide; /* reset cached cursor position */
2086 	tty_write(tty_cmd_cell, &ttyctx);
2087 	screen_write_set_cursor(ctx, cx, cy);
2088 
2089 	return (1);
2090 }
2091 
2092 /*
2093  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
2094  * cell on the screen, so following cells must not be drawn by marking them as
2095  * padding.
2096  *
2097  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
2098  * character, it is necessary to also overwrite any other cells which covered
2099  * by the same character.
2100  */
2101 static int
screen_write_overwrite(struct screen_write_ctx * ctx,struct grid_cell * gc,u_int width)2102 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
2103     u_int width)
2104 {
2105 	struct screen		*s = ctx->s;
2106 	struct grid		*gd = s->grid;
2107 	struct grid_cell	 tmp_gc;
2108 	u_int			 xx;
2109 	int			 done = 0;
2110 
2111 	if (gc->flags & GRID_FLAG_PADDING) {
2112 		/*
2113 		 * A padding cell, so clear any following and leading padding
2114 		 * cells back to the character. Don't overwrite the current
2115 		 * cell as that happens later anyway.
2116 		 */
2117 		xx = s->cx + 1;
2118 		while (--xx > 0) {
2119 			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2120 			if (~tmp_gc.flags & GRID_FLAG_PADDING)
2121 				break;
2122 			log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
2123 			grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2124 		}
2125 
2126 		/* Overwrite the character at the start of this padding. */
2127 		log_debug("%s: character at %u,%u", __func__, xx, s->cy);
2128 		grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2129 		done = 1;
2130 	}
2131 
2132 	/*
2133 	 * Overwrite any padding cells that belong to any UTF-8 characters
2134 	 * we'll be overwriting with the current character.
2135 	 */
2136 	if (width != 1 ||
2137 	    gc->data.width != 1 ||
2138 	    gc->flags & GRID_FLAG_PADDING) {
2139 		xx = s->cx + width - 1;
2140 		while (++xx < screen_size_x(s)) {
2141 			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2142 			if (~tmp_gc.flags & GRID_FLAG_PADDING)
2143 				break;
2144 			log_debug("%s: overwrite at %u,%u", __func__, xx,
2145 			    s->cy);
2146 			if (gc->flags & GRID_FLAG_TAB) {
2147 				memcpy(&tmp_gc, gc, sizeof tmp_gc);
2148 				memset(tmp_gc.data.data, 0,
2149 				    sizeof tmp_gc.data.data);
2150 				*tmp_gc.data.data = ' ';
2151 				tmp_gc.data.width = tmp_gc.data.size =
2152 				    tmp_gc.data.have = 1;
2153 				grid_view_set_cell(gd, xx, s->cy, &tmp_gc);
2154 			} else
2155 				grid_view_set_cell(gd, xx, s->cy,
2156 				    &grid_default_cell);
2157 			done = 1;
2158 		}
2159 	}
2160 
2161 	return (done);
2162 }
2163 
2164 /* Set external clipboard. */
2165 void
screen_write_setselection(struct screen_write_ctx * ctx,const char * flags,u_char * str,u_int len)2166 screen_write_setselection(struct screen_write_ctx *ctx, const char *flags,
2167     u_char *str, u_int len)
2168 {
2169 	struct tty_ctx	ttyctx;
2170 
2171 	screen_write_initctx(ctx, &ttyctx, 0);
2172 	ttyctx.ptr = str;
2173 	ttyctx.ptr2 = (void *)flags;
2174 	ttyctx.num = len;
2175 
2176 	tty_write(tty_cmd_setselection, &ttyctx);
2177 }
2178 
2179 /* Write unmodified string. */
2180 void
screen_write_rawstring(struct screen_write_ctx * ctx,u_char * str,u_int len,int allow_invisible_panes)2181 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len,
2182     int allow_invisible_panes)
2183 {
2184 	struct tty_ctx	ttyctx;
2185 
2186 	screen_write_initctx(ctx, &ttyctx, 0);
2187 	ttyctx.ptr = str;
2188 	ttyctx.num = len;
2189 	ttyctx.allow_invisible_panes = allow_invisible_panes;
2190 
2191 	tty_write(tty_cmd_rawstring, &ttyctx);
2192 }
2193 
2194 /* Turn alternate screen on. */
2195 void
screen_write_alternateon(struct screen_write_ctx * ctx,struct grid_cell * gc,int cursor)2196 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc,
2197     int cursor)
2198 {
2199 	struct tty_ctx		 ttyctx;
2200 	struct window_pane	*wp = ctx->wp;
2201 
2202 	if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2203 		return;
2204 
2205 	screen_write_collect_flush(ctx, 0, __func__);
2206 	screen_alternate_on(ctx->s, gc, cursor);
2207 
2208 	screen_write_initctx(ctx, &ttyctx, 1);
2209 	if (ttyctx.redraw_cb != NULL)
2210 		ttyctx.redraw_cb(&ttyctx);
2211 }
2212 
2213 /* Turn alternate screen off. */
2214 void
screen_write_alternateoff(struct screen_write_ctx * ctx,struct grid_cell * gc,int cursor)2215 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc,
2216     int cursor)
2217 {
2218 	struct tty_ctx		 ttyctx;
2219 	struct window_pane	*wp = ctx->wp;
2220 
2221 	if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2222 		return;
2223 
2224 	screen_write_collect_flush(ctx, 0, __func__);
2225 	screen_alternate_off(ctx->s, gc, cursor);
2226 
2227 	screen_write_initctx(ctx, &ttyctx, 1);
2228 	if (ttyctx.redraw_cb != NULL)
2229 		ttyctx.redraw_cb(&ttyctx);
2230 }
2231